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.
5 #include "base/process/process_metrics.h"
10 #include "base/threading/thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Tests for SystemMetrics.
18 // Exists as a class so it can be a friend of SystemMetrics.
19 class SystemMetricsTest
: public testing::Test
{
21 SystemMetricsTest() {}
24 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest
);
27 /////////////////////////////////////////////////////////////////////////////
29 #if defined(OS_LINUX) || defined(OS_ANDROID)
30 TEST_F(SystemMetricsTest
, IsValidDiskName
) {
31 std::string invalid_input1
= "";
32 std::string invalid_input2
= "s";
33 std::string invalid_input3
= "sdz+";
34 std::string invalid_input4
= "hda0";
35 std::string invalid_input5
= "mmcbl";
36 std::string invalid_input6
= "mmcblka";
37 std::string invalid_input7
= "mmcblkb";
38 std::string invalid_input8
= "mmmblk0";
40 EXPECT_FALSE(IsValidDiskName(invalid_input1
));
41 EXPECT_FALSE(IsValidDiskName(invalid_input2
));
42 EXPECT_FALSE(IsValidDiskName(invalid_input3
));
43 EXPECT_FALSE(IsValidDiskName(invalid_input4
));
44 EXPECT_FALSE(IsValidDiskName(invalid_input5
));
45 EXPECT_FALSE(IsValidDiskName(invalid_input6
));
46 EXPECT_FALSE(IsValidDiskName(invalid_input7
));
47 EXPECT_FALSE(IsValidDiskName(invalid_input8
));
49 std::string valid_input1
= "sda";
50 std::string valid_input2
= "sdaaaa";
51 std::string valid_input3
= "hdz";
52 std::string valid_input4
= "mmcblk0";
53 std::string valid_input5
= "mmcblk999";
55 EXPECT_TRUE(IsValidDiskName(valid_input1
));
56 EXPECT_TRUE(IsValidDiskName(valid_input2
));
57 EXPECT_TRUE(IsValidDiskName(valid_input3
));
58 EXPECT_TRUE(IsValidDiskName(valid_input4
));
59 EXPECT_TRUE(IsValidDiskName(valid_input5
));
62 TEST_F(SystemMetricsTest
, ParseMeminfo
) {
63 struct SystemMemoryInfoKB meminfo
;
64 std::string invalid_input1
= "abc";
65 std::string invalid_input2
= "MemTotal:";
66 // Partial file with no MemTotal
67 std::string invalid_input3
=
68 "MemFree: 3913968 kB\n"
69 "Buffers: 2348340 kB\n"
70 "Cached: 49071596 kB\n"
72 "Active: 36393900 kB\n"
73 "Inactive: 21221496 kB\n"
74 "Active(anon): 5674352 kB\n"
75 "Inactive(anon): 633992 kB\n";
76 EXPECT_FALSE(ParseProcMeminfo(invalid_input1
, &meminfo
));
77 EXPECT_FALSE(ParseProcMeminfo(invalid_input2
, &meminfo
));
78 EXPECT_FALSE(ParseProcMeminfo(invalid_input3
, &meminfo
));
80 std::string valid_input1
=
81 "MemTotal: 3981504 kB\n"
82 "MemFree: 140764 kB\n"
83 "Buffers: 116480 kB\n"
85 "SwapCached: 21304 kB\n"
86 "Active: 3152040 kB\n"
87 "Inactive: 472856 kB\n"
88 "Active(anon): 2972352 kB\n"
89 "Inactive(anon): 270108 kB\n"
90 "Active(file): 179688 kB\n"
91 "Inactive(file): 202748 kB\n"
94 "SwapTotal: 5832280 kB\n"
95 "SwapFree: 3672368 kB\n"
98 "AnonPages: 3101224 kB\n"
102 "SReclaimable: 30936 kB\n"
103 "SUnreclaim: 23276 kB\n"
104 "KernelStack: 2464 kB\n"
105 "PageTables: 24812 kB\n"
106 "NFS_Unstable: 0 kB\n"
108 "WritebackTmp: 0 kB\n"
109 "CommitLimit: 7823032 kB\n"
110 "Committed_AS: 7973536 kB\n"
111 "VmallocTotal: 34359738367 kB\n"
112 "VmallocUsed: 375940 kB\n"
113 "VmallocChunk: 34359361127 kB\n"
114 "DirectMap4k: 72448 kB\n"
115 "DirectMap2M: 4061184 kB\n";
116 // output from a much older kernel where the Active and Inactive aren't
117 // broken down into anon and file and Huge Pages are enabled
118 std::string valid_input2
=
119 "MemTotal: 255908 kB\n"
120 "MemFree: 69936 kB\n"
121 "Buffers: 15812 kB\n"
122 "Cached: 115124 kB\n"
125 "Inactive: 63792 kB\n"
128 "LowTotal: 255908 kB\n"
129 "LowFree: 69936 kB\n"
130 "SwapTotal: 524280 kB\n"
131 "SwapFree: 524200 kB\n"
136 "Committed_AS: 118680 kB\n"
137 "PageTables: 1236 kB\n"
138 "VmallocTotal: 3874808 kB\n"
139 "VmallocUsed: 1416 kB\n"
140 "VmallocChunk: 3872908 kB\n"
141 "HugePages_Total: 0\n"
142 "HugePages_Free: 0\n"
143 "Hugepagesize: 4096 kB\n";
145 EXPECT_TRUE(ParseProcMeminfo(valid_input1
, &meminfo
));
146 EXPECT_EQ(meminfo
.total
, 3981504);
147 EXPECT_EQ(meminfo
.free
, 140764);
148 EXPECT_EQ(meminfo
.buffers
, 116480);
149 EXPECT_EQ(meminfo
.cached
, 406160);
150 EXPECT_EQ(meminfo
.active_anon
, 2972352);
151 EXPECT_EQ(meminfo
.active_file
, 179688);
152 EXPECT_EQ(meminfo
.inactive_anon
, 270108);
153 EXPECT_EQ(meminfo
.inactive_file
, 202748);
154 EXPECT_EQ(meminfo
.swap_total
, 5832280);
155 EXPECT_EQ(meminfo
.swap_free
, 3672368);
156 EXPECT_EQ(meminfo
.dirty
, 184);
157 #if defined(OS_CHROMEOS)
158 EXPECT_EQ(meminfo
.shmem
, 140204);
159 EXPECT_EQ(meminfo
.slab
, 54212);
161 EXPECT_TRUE(ParseProcMeminfo(valid_input2
, &meminfo
));
162 EXPECT_EQ(meminfo
.total
, 255908);
163 EXPECT_EQ(meminfo
.free
, 69936);
164 EXPECT_EQ(meminfo
.buffers
, 15812);
165 EXPECT_EQ(meminfo
.cached
, 115124);
166 EXPECT_EQ(meminfo
.swap_total
, 524280);
167 EXPECT_EQ(meminfo
.swap_free
, 524200);
168 EXPECT_EQ(meminfo
.dirty
, 4);
171 TEST_F(SystemMetricsTest
, ParseVmstat
) {
172 struct SystemMemoryInfoKB meminfo
;
173 // part of vmstat from a 3.2 kernel with numa enabled
174 std::string valid_input1
=
175 "nr_free_pages 905104\n"
176 "nr_inactive_anon 142478"
177 "nr_active_anon 1520046\n"
178 "nr_inactive_file 4481001\n"
179 "nr_active_file 8313439\n"
180 "nr_unevictable 5044\n"
182 "nr_anon_pages 1633780\n"
184 "nr_file_pages 12828218\n"
187 "nr_slab_reclaimable 831609\n"
188 "nr_slab_unreclaimable 41164\n"
189 "nr_page_table_pages 31470\n"
190 "nr_kernel_stack 1735\n"
193 "nr_vmscan_write 406\n"
194 "nr_vmscan_immediate_reclaim 281\n"
195 "nr_writeback_temp 0\n"
196 "nr_isolated_anon 0\n"
197 "nr_isolated_file 0\n"
199 "nr_dirtied 84674644\n"
200 "nr_written 75307109\n"
201 "nr_anon_transparent_hugepages 0\n"
202 "nr_dirty_threshold 1536206\n"
203 "nr_dirty_background_threshold 768103\n"
205 "pgpgout 319023278\n"
209 "pgalloc_dma32 20833399\n"
210 "pgalloc_normal 1622609290\n"
211 "pgalloc_movable 0\n"
212 "pgfree 1644355583\n"
213 "pgactivate 75391882\n"
214 "pgdeactivate 4121019\n"
215 "pgfault 2542879679\n"
216 "pgmajfault 487192\n";
217 std::string valid_input2
=
218 "nr_free_pages 180125\n"
219 "nr_inactive_anon 51\n"
220 "nr_active_anon 38832\n"
221 "nr_inactive_file 50171\n"
222 "nr_active_file 47510\n"
225 "nr_anon_pages 38825\n"
227 "nr_file_pages 97733\n"
230 "nr_slab_reclaimable 4032\n"
231 "nr_slab_unreclaimable 2848\n"
232 "nr_page_table_pages 1505\n"
233 "nr_kernel_stack 626\n"
236 "nr_vmscan_write 0\n"
237 "nr_vmscan_immediate_reclaim 0\n"
238 "nr_writeback_temp 0\n"
239 "nr_isolated_anon 0\n"
240 "nr_isolated_file 0\n"
242 "nr_dirtied 435358\n"
243 "nr_written 401258\n"
244 "nr_anon_transparent_hugepages 0\n"
245 "nr_dirty_threshold 18566\n"
246 "nr_dirty_background_threshold 4641\n"
251 "pgalloc_normal 144213030\n"
252 "pgalloc_high 164501274\n"
253 "pgalloc_movable 0\n"
255 "pgactivate 239320\n"
257 "pgfault 716044601\n"
259 "pgrefill_normal 0\n"
261 "pgrefill_movable 0\n";
262 EXPECT_TRUE(ParseProcVmstat(valid_input1
, &meminfo
));
263 EXPECT_EQ(meminfo
.pswpin
, 179);
264 EXPECT_EQ(meminfo
.pswpout
, 406);
265 EXPECT_EQ(meminfo
.pgmajfault
, 487192);
266 EXPECT_TRUE(ParseProcVmstat(valid_input2
, &meminfo
));
267 EXPECT_EQ(meminfo
.pswpin
, 12);
268 EXPECT_EQ(meminfo
.pswpout
, 901);
269 EXPECT_EQ(meminfo
.pgmajfault
, 2023);
271 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
273 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) || \
274 defined(OS_LINUX) || defined(OS_ANDROID)
275 TEST(SystemMetrics2Test
, GetSystemMemoryInfo
) {
276 base::SystemMemoryInfoKB info
;
277 EXPECT_TRUE(base::GetSystemMemoryInfo(&info
));
279 // Ensure each field received a value.
280 EXPECT_GT(info
.total
, 0);
281 EXPECT_GT(info
.free
, 0);
282 #if defined(OS_LINUX) || defined(OS_ANDROID)
283 EXPECT_GT(info
.buffers
, 0);
284 EXPECT_GT(info
.cached
, 0);
285 EXPECT_GT(info
.active_anon
, 0);
286 EXPECT_GT(info
.inactive_anon
, 0);
287 EXPECT_GT(info
.active_file
, 0);
288 EXPECT_GT(info
.inactive_file
, 0);
289 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
291 // All the values should be less than the total amount of memory.
292 EXPECT_LT(info
.free
, info
.total
);
293 #if defined(OS_LINUX) || defined(OS_ANDROID)
294 EXPECT_LT(info
.buffers
, info
.total
);
295 EXPECT_LT(info
.cached
, info
.total
);
296 EXPECT_LT(info
.active_anon
, info
.total
);
297 EXPECT_LT(info
.inactive_anon
, info
.total
);
298 EXPECT_LT(info
.active_file
, info
.total
);
299 EXPECT_LT(info
.inactive_file
, info
.total
);
300 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
302 #if defined(OS_CHROMEOS)
303 // Chrome OS exposes shmem.
304 EXPECT_GT(info
.shmem
, 0);
305 EXPECT_LT(info
.shmem
, info
.total
);
306 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
307 // and gem_size cannot be tested here.
310 #endif // defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) ||
311 // defined(OS_LINUX) || defined(OS_ANDROID)
313 #if defined(OS_LINUX) || defined(OS_ANDROID)
314 TEST(ProcessMetricsTest
, ParseProcStatCPU
) {
315 // /proc/self/stat for a process running "top".
316 const char kTopStat
[] = "960 (top) S 16230 960 16230 34818 960 "
318 "12 16 0 0 " // <- These are the goods.
319 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
320 "4246868 140733983044336 18446744073709551615 140244213071219 "
321 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
322 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat
));
324 // cat /proc/self/stat on a random other machine I have.
325 const char kSelfStat
[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
327 "0 0 0 0 " // <- No CPU, apparently.
328 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
329 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
331 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat
));
333 // Some weird long-running process with a weird name that I created for the
334 // purposes of this test.
335 const char kWeirdNameStat
[] = "26115 (Hello) You ())) ) R 24614 26115 24614"
336 " 34839 26115 4218880 227 0 0 0 "
338 "20 0 1 0 36933953 4296704 90 18446744073709551615 4194304 4196116 "
339 "140735857761568 140735857761160 4195644 0 0 0 0 0 0 0 17 14 0 0 0 0 0 "
340 "6295056 6295616 16519168 140735857770710 140735857770737 "
341 "140735857770737 140735857774557 0";
342 EXPECT_EQ(5186 + 11, base::ParseProcStatCPU(kWeirdNameStat
));
344 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
346 // Disable on Android because base_unittests runs inside a Dalvik VM that
347 // starts and stop threads (crbug.com/175563).
348 #if defined(OS_LINUX)
349 // http://crbug.com/396455
350 TEST(ProcessMetricsTest
, DISABLED_GetNumberOfThreads
) {
351 const base::ProcessHandle current
= base::GetCurrentProcessHandle();
352 const int initial_threads
= base::GetNumberOfThreads(current
);
353 ASSERT_GT(initial_threads
, 0);
354 const int kNumAdditionalThreads
= 10;
356 scoped_ptr
<base::Thread
> my_threads
[kNumAdditionalThreads
];
357 for (int i
= 0; i
< kNumAdditionalThreads
; ++i
) {
358 my_threads
[i
].reset(new base::Thread("GetNumberOfThreadsTest"));
359 my_threads
[i
]->Start();
360 ASSERT_EQ(base::GetNumberOfThreads(current
), initial_threads
+ 1 + i
);
363 // The Thread destructor will stop them.
364 ASSERT_EQ(initial_threads
, base::GetNumberOfThreads(current
));
366 #endif // defined(OS_LINUX)