OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/process/process_metrics.h" | |
6 | |
7 #include <sstream> | |
8 #include <string> | |
9 | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 | |
13 namespace base { | |
14 namespace debug { | |
15 | |
16 // Tests for SystemMetrics. | |
17 // Exists as a class so it can be a friend of SystemMetrics. | |
18 class SystemMetricsTest : public testing::Test { | |
19 public: | |
20 SystemMetricsTest() {} | |
21 | |
22 private: | |
23 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); | |
24 }; | |
25 | |
26 ///////////////////////////////////////////////////////////////////////////// | |
27 | |
28 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
29 TEST_F(SystemMetricsTest, IsValidDiskName) { | |
30 std::string invalid_input1 = ""; | |
31 std::string invalid_input2 = "s"; | |
32 std::string invalid_input3 = "sdz+"; | |
33 std::string invalid_input4 = "hda0"; | |
34 std::string invalid_input5 = "mmcbl"; | |
35 std::string invalid_input6 = "mmcblka"; | |
36 std::string invalid_input7 = "mmcblkb"; | |
37 std::string invalid_input8 = "mmmblk0"; | |
38 | |
39 EXPECT_FALSE(IsValidDiskName(invalid_input1)); | |
40 EXPECT_FALSE(IsValidDiskName(invalid_input2)); | |
41 EXPECT_FALSE(IsValidDiskName(invalid_input3)); | |
42 EXPECT_FALSE(IsValidDiskName(invalid_input4)); | |
43 EXPECT_FALSE(IsValidDiskName(invalid_input5)); | |
44 EXPECT_FALSE(IsValidDiskName(invalid_input6)); | |
45 EXPECT_FALSE(IsValidDiskName(invalid_input7)); | |
46 EXPECT_FALSE(IsValidDiskName(invalid_input8)); | |
47 | |
48 std::string valid_input1 = "sda"; | |
49 std::string valid_input2 = "sdaaaa"; | |
50 std::string valid_input3 = "hdz"; | |
51 std::string valid_input4 = "mmcblk0"; | |
52 std::string valid_input5 = "mmcblk999"; | |
53 | |
54 EXPECT_TRUE(IsValidDiskName(valid_input1)); | |
55 EXPECT_TRUE(IsValidDiskName(valid_input2)); | |
56 EXPECT_TRUE(IsValidDiskName(valid_input3)); | |
57 EXPECT_TRUE(IsValidDiskName(valid_input4)); | |
58 EXPECT_TRUE(IsValidDiskName(valid_input5)); | |
59 } | |
60 | |
61 TEST_F(SystemMetricsTest, ParseMeminfo) { | |
62 struct SystemMemoryInfoKB meminfo; | |
63 std::string invalid_input1 = "abc"; | |
64 std::string invalid_input2 = "MemTotal:"; | |
65 // Partial file with no MemTotal | |
66 std::string invalid_input3 = | |
67 "MemFree: 3913968 kB\n" | |
68 "Buffers: 2348340 kB\n" | |
69 "Cached: 49071596 kB\n" | |
70 "SwapCached: 12 kB\n" | |
71 "Active: 36393900 kB\n" | |
72 "Inactive: 21221496 kB\n" | |
73 "Active(anon): 5674352 kB\n" | |
74 "Inactive(anon): 633992 kB\n"; | |
75 EXPECT_FALSE(ParseProcMeminfo(invalid_input1, &meminfo)); | |
76 EXPECT_FALSE(ParseProcMeminfo(invalid_input2, &meminfo)); | |
77 EXPECT_FALSE(ParseProcMeminfo(invalid_input3, &meminfo)); | |
78 | |
79 std::string valid_input1 = | |
80 "MemTotal: 3981504 kB\n" | |
81 "MemFree: 140764 kB\n" | |
82 "Buffers: 116480 kB\n" | |
83 "Cached: 406160 kB\n" | |
84 "SwapCached: 21304 kB\n" | |
85 "Active: 3152040 kB\n" | |
86 "Inactive: 472856 kB\n" | |
87 "Active(anon): 2972352 kB\n" | |
88 "Inactive(anon): 270108 kB\n" | |
89 "Active(file): 179688 kB\n" | |
90 "Inactive(file): 202748 kB\n" | |
91 "Unevictable: 0 kB\n" | |
92 "Mlocked: 0 kB\n" | |
93 "SwapTotal: 5832280 kB\n" | |
94 "SwapFree: 3672368 kB\n" | |
95 "Dirty: 184 kB\n" | |
96 "Writeback: 0 kB\n" | |
97 "AnonPages: 3101224 kB\n" | |
98 "Mapped: 142296 kB\n" | |
99 "Shmem: 140204 kB\n" | |
100 "Slab: 54212 kB\n" | |
101 "SReclaimable: 30936 kB\n" | |
102 "SUnreclaim: 23276 kB\n" | |
103 "KernelStack: 2464 kB\n" | |
104 "PageTables: 24812 kB\n" | |
105 "NFS_Unstable: 0 kB\n" | |
106 "Bounce: 0 kB\n" | |
107 "WritebackTmp: 0 kB\n" | |
108 "CommitLimit: 7823032 kB\n" | |
109 "Committed_AS: 7973536 kB\n" | |
110 "VmallocTotal: 34359738367 kB\n" | |
111 "VmallocUsed: 375940 kB\n" | |
112 "VmallocChunk: 34359361127 kB\n" | |
113 "DirectMap4k: 72448 kB\n" | |
114 "DirectMap2M: 4061184 kB\n"; | |
115 // output from a much older kernel where the Active and Inactive aren't | |
116 // broken down into anon and file and Huge Pages are enabled | |
117 std::string valid_input2 = | |
118 "MemTotal: 255908 kB\n" | |
119 "MemFree: 69936 kB\n" | |
120 "Buffers: 15812 kB\n" | |
121 "Cached: 115124 kB\n" | |
122 "SwapCached: 0 kB\n" | |
123 "Active: 92700 kB\n" | |
124 "Inactive: 63792 kB\n" | |
125 "HighTotal: 0 kB\n" | |
126 "HighFree: 0 kB\n" | |
127 "LowTotal: 255908 kB\n" | |
128 "LowFree: 69936 kB\n" | |
129 "SwapTotal: 524280 kB\n" | |
130 "SwapFree: 524200 kB\n" | |
131 "Dirty: 4 kB\n" | |
132 "Writeback: 0 kB\n" | |
133 "Mapped: 42236 kB\n" | |
134 "Slab: 25912 kB\n" | |
135 "Committed_AS: 118680 kB\n" | |
136 "PageTables: 1236 kB\n" | |
137 "VmallocTotal: 3874808 kB\n" | |
138 "VmallocUsed: 1416 kB\n" | |
139 "VmallocChunk: 3872908 kB\n" | |
140 "HugePages_Total: 0\n" | |
141 "HugePages_Free: 0\n" | |
142 "Hugepagesize: 4096 kB\n"; | |
143 | |
144 EXPECT_TRUE(ParseProcMeminfo(valid_input1, &meminfo)); | |
145 EXPECT_TRUE(meminfo.total == 3981504); | |
146 EXPECT_TRUE(meminfo.free == 140764); | |
147 EXPECT_TRUE(meminfo.buffers == 116480); | |
148 EXPECT_TRUE(meminfo.cached == 406160); | |
149 EXPECT_TRUE(meminfo.active_anon == 2972352); | |
150 EXPECT_TRUE(meminfo.active_file == 179688); | |
151 EXPECT_TRUE(meminfo.inactive_anon == 270108); | |
152 EXPECT_TRUE(meminfo.inactive_file == 202748); | |
153 EXPECT_TRUE(meminfo.swap_total == 5832280); | |
154 EXPECT_TRUE(meminfo.swap_free == 3672368); | |
155 EXPECT_TRUE(meminfo.dirty == 184); | |
156 #if defined(OS_CHROMEOS) | |
157 EXPECT_TRUE(meminfo.shmem == 140204); | |
158 EXPECT_TRUE(meminfo.slab == 54212); | |
159 #endif | |
160 EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo)); | |
161 EXPECT_TRUE(meminfo.total == 255908); | |
162 EXPECT_TRUE(meminfo.free == 69936); | |
163 EXPECT_TRUE(meminfo.buffers == 15812); | |
164 EXPECT_TRUE(meminfo.cached == 115124); | |
165 EXPECT_TRUE(meminfo.swap_total == 524280); | |
166 EXPECT_TRUE(meminfo.swap_free == 524200); | |
167 EXPECT_TRUE(meminfo.dirty == 4); | |
168 } | |
169 | |
170 TEST_F(SystemMetricsTest, ParseVmstat) { | |
171 struct SystemMemoryInfoKB meminfo; | |
172 // part of vmstat from a 3.2 kernel with numa enabled | |
173 std::string valid_input1 = | |
174 "nr_free_pages 905104\n" | |
175 "nr_inactive_anon 142478" | |
176 "nr_active_anon 1520046\n" | |
177 "nr_inactive_file 4481001\n" | |
178 "nr_active_file 8313439\n" | |
179 "nr_unevictable 5044\n" | |
180 "nr_mlock 5044\n" | |
181 "nr_anon_pages 1633780\n" | |
182 "nr_mapped 104742\n" | |
183 "nr_file_pages 12828218\n" | |
184 "nr_dirty 245\n" | |
185 "nr_writeback 0\n" | |
186 "nr_slab_reclaimable 831609\n" | |
187 "nr_slab_unreclaimable 41164\n" | |
188 "nr_page_table_pages 31470\n" | |
189 "nr_kernel_stack 1735\n" | |
190 "nr_unstable 0\n" | |
191 "nr_bounce 0\n" | |
192 "nr_vmscan_write 406\n" | |
193 "nr_vmscan_immediate_reclaim 281\n" | |
194 "nr_writeback_temp 0\n" | |
195 "nr_isolated_anon 0\n" | |
196 "nr_isolated_file 0\n" | |
197 "nr_shmem 28820\n" | |
198 "nr_dirtied 84674644\n" | |
199 "nr_written 75307109\n" | |
200 "nr_anon_transparent_hugepages 0\n" | |
201 "nr_dirty_threshold 1536206\n" | |
202 "nr_dirty_background_threshold 768103\n" | |
203 "pgpgin 30777108\n" | |
204 "pgpgout 319023278\n" | |
205 "pswpin 179\n" | |
206 "pswpout 406\n" | |
207 "pgalloc_dma 0\n" | |
208 "pgalloc_dma32 20833399\n" | |
209 "pgalloc_normal 1622609290\n" | |
210 "pgalloc_movable 0\n" | |
211 "pgfree 1644355583\n" | |
212 "pgactivate 75391882\n" | |
213 "pgdeactivate 4121019\n" | |
214 "pgfault 2542879679\n" | |
215 "pgmajfault 487192\n"; | |
216 std::string valid_input2 = | |
217 "nr_free_pages 180125\n" | |
218 "nr_inactive_anon 51\n" | |
219 "nr_active_anon 38832\n" | |
220 "nr_inactive_file 50171\n" | |
221 "nr_active_file 47510\n" | |
222 "nr_unevictable 0\n" | |
223 "nr_mlock 0\n" | |
224 "nr_anon_pages 38825\n" | |
225 "nr_mapped 24043\n" | |
226 "nr_file_pages 97733\n" | |
227 "nr_dirty 0\n" | |
228 "nr_writeback 0\n" | |
229 "nr_slab_reclaimable 4032\n" | |
230 "nr_slab_unreclaimable 2848\n" | |
231 "nr_page_table_pages 1505\n" | |
232 "nr_kernel_stack 626\n" | |
233 "nr_unstable 0\n" | |
234 "nr_bounce 0\n" | |
235 "nr_vmscan_write 0\n" | |
236 "nr_vmscan_immediate_reclaim 0\n" | |
237 "nr_writeback_temp 0\n" | |
238 "nr_isolated_anon 0\n" | |
239 "nr_isolated_file 0\n" | |
240 "nr_shmem 58\n" | |
241 "nr_dirtied 435358\n" | |
242 "nr_written 401258\n" | |
243 "nr_anon_transparent_hugepages 0\n" | |
244 "nr_dirty_threshold 18566\n" | |
245 "nr_dirty_background_threshold 4641\n" | |
246 "pgpgin 299464\n" | |
247 "pgpgout 2437788\n" | |
248 "pswpin 12\n" | |
249 "pswpout 901\n" | |
250 "pgalloc_normal 144213030\n" | |
251 "pgalloc_high 164501274\n" | |
252 "pgalloc_movable 0\n" | |
253 "pgfree 308894908\n" | |
254 "pgactivate 239320\n" | |
255 "pgdeactivate 1\n" | |
256 "pgfault 716044601\n" | |
257 "pgmajfault 2023\n" | |
258 "pgrefill_normal 0\n" | |
259 "pgrefill_high 0\n" | |
260 "pgrefill_movable 0\n"; | |
261 EXPECT_TRUE(ParseProcVmstat(valid_input1, &meminfo)); | |
262 EXPECT_TRUE(meminfo.pswpin == 179); | |
263 EXPECT_TRUE(meminfo.pswpout == 406); | |
264 EXPECT_TRUE(meminfo.pgmajfault == 487192); | |
265 EXPECT_TRUE(ParseProcVmstat(valid_input2, &meminfo)); | |
266 EXPECT_TRUE(meminfo.pswpin == 12); | |
267 EXPECT_TRUE(meminfo.pswpout == 901); | |
268 EXPECT_TRUE(meminfo.pgmajfault == 2023); | |
269 } | |
270 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
271 | |
272 } // namespace debug | |
273 } // namespace base | |
OLD | NEW |