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 <stddef.h>
8#include <stdint.h>
9
10#include <sstream>
11#include <string>
12
13#include "base/bind.h"
14#include "base/command_line.h"
15#include "base/files/file.h"
16#include "base/files/file_util.h"
17#include "base/files/scoped_temp_dir.h"
18#include "base/macros.h"
19#include "base/strings/string_number_conversions.h"
20#include "base/test/multiprocess_test.h"
21#include "base/threading/thread.h"
22#include "build/build_config.h"
23#include "testing/gtest/include/gtest/gtest.h"
24#include "testing/multiprocess_func_list.h"
25
26namespace base {
27namespace debug {
28
29#if defined(OS_LINUX) || defined(OS_CHROMEOS)
30namespace {
31
32void BusyWork(std::vector<std::string>* vec) {
33  int64_t test_value = 0;
34  for (int i = 0; i < 100000; ++i) {
35    ++test_value;
36    vec->push_back(Int64ToString(test_value));
37  }
38}
39
40}  // namespace
41#endif  // defined(OS_LINUX) || defined(OS_CHROMEOS)
42
43// Tests for SystemMetrics.
44// Exists as a class so it can be a friend of SystemMetrics.
45class SystemMetricsTest : public testing::Test {
46 public:
47  SystemMetricsTest() {}
48
49 private:
50  DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest);
51};
52
53/////////////////////////////////////////////////////////////////////////////
54
55#if defined(OS_LINUX) || defined(OS_ANDROID)
56TEST_F(SystemMetricsTest, IsValidDiskName) {
57  std::string invalid_input1 = "";
58  std::string invalid_input2 = "s";
59  std::string invalid_input3 = "sdz+";
60  std::string invalid_input4 = "hda0";
61  std::string invalid_input5 = "mmcbl";
62  std::string invalid_input6 = "mmcblka";
63  std::string invalid_input7 = "mmcblkb";
64  std::string invalid_input8 = "mmmblk0";
65
66  EXPECT_FALSE(IsValidDiskName(invalid_input1));
67  EXPECT_FALSE(IsValidDiskName(invalid_input2));
68  EXPECT_FALSE(IsValidDiskName(invalid_input3));
69  EXPECT_FALSE(IsValidDiskName(invalid_input4));
70  EXPECT_FALSE(IsValidDiskName(invalid_input5));
71  EXPECT_FALSE(IsValidDiskName(invalid_input6));
72  EXPECT_FALSE(IsValidDiskName(invalid_input7));
73  EXPECT_FALSE(IsValidDiskName(invalid_input8));
74
75  std::string valid_input1 = "sda";
76  std::string valid_input2 = "sdaaaa";
77  std::string valid_input3 = "hdz";
78  std::string valid_input4 = "mmcblk0";
79  std::string valid_input5 = "mmcblk999";
80
81  EXPECT_TRUE(IsValidDiskName(valid_input1));
82  EXPECT_TRUE(IsValidDiskName(valid_input2));
83  EXPECT_TRUE(IsValidDiskName(valid_input3));
84  EXPECT_TRUE(IsValidDiskName(valid_input4));
85  EXPECT_TRUE(IsValidDiskName(valid_input5));
86}
87
88TEST_F(SystemMetricsTest, ParseMeminfo) {
89  struct SystemMemoryInfoKB meminfo;
90  std::string invalid_input1 = "abc";
91  std::string invalid_input2 = "MemTotal:";
92  // Partial file with no MemTotal
93  std::string invalid_input3 =
94    "MemFree:         3913968 kB\n"
95    "Buffers:         2348340 kB\n"
96    "Cached:         49071596 kB\n"
97    "SwapCached:           12 kB\n"
98    "Active:         36393900 kB\n"
99    "Inactive:       21221496 kB\n"
100    "Active(anon):    5674352 kB\n"
101    "Inactive(anon):   633992 kB\n";
102  EXPECT_FALSE(ParseProcMeminfo(invalid_input1, &meminfo));
103  EXPECT_FALSE(ParseProcMeminfo(invalid_input2, &meminfo));
104  EXPECT_FALSE(ParseProcMeminfo(invalid_input3, &meminfo));
105
106  std::string valid_input1 =
107    "MemTotal:        3981504 kB\n"
108    "MemFree:          140764 kB\n"
109    "Buffers:          116480 kB\n"
110    "Cached:           406160 kB\n"
111    "SwapCached:        21304 kB\n"
112    "Active:          3152040 kB\n"
113    "Inactive:         472856 kB\n"
114    "Active(anon):    2972352 kB\n"
115    "Inactive(anon):   270108 kB\n"
116    "Active(file):     179688 kB\n"
117    "Inactive(file):   202748 kB\n"
118    "Unevictable:           0 kB\n"
119    "Mlocked:               0 kB\n"
120    "SwapTotal:       5832280 kB\n"
121    "SwapFree:        3672368 kB\n"
122    "Dirty:               184 kB\n"
123    "Writeback:             0 kB\n"
124    "AnonPages:       3101224 kB\n"
125    "Mapped:           142296 kB\n"
126    "Shmem:            140204 kB\n"
127    "Slab:              54212 kB\n"
128    "SReclaimable:      30936 kB\n"
129    "SUnreclaim:        23276 kB\n"
130    "KernelStack:        2464 kB\n"
131    "PageTables:        24812 kB\n"
132    "NFS_Unstable:          0 kB\n"
133    "Bounce:                0 kB\n"
134    "WritebackTmp:          0 kB\n"
135    "CommitLimit:     7823032 kB\n"
136    "Committed_AS:    7973536 kB\n"
137    "VmallocTotal:   34359738367 kB\n"
138    "VmallocUsed:      375940 kB\n"
139    "VmallocChunk:   34359361127 kB\n"
140    "DirectMap4k:       72448 kB\n"
141    "DirectMap2M:     4061184 kB\n";
142  // output from a much older kernel where the Active and Inactive aren't
143  // broken down into anon and file and Huge Pages are enabled
144  std::string valid_input2 =
145    "MemTotal:       255908 kB\n"
146    "MemFree:         69936 kB\n"
147    "Buffers:         15812 kB\n"
148    "Cached:         115124 kB\n"
149    "SwapCached:          0 kB\n"
150    "Active:          92700 kB\n"
151    "Inactive:        63792 kB\n"
152    "HighTotal:           0 kB\n"
153    "HighFree:            0 kB\n"
154    "LowTotal:       255908 kB\n"
155    "LowFree:         69936 kB\n"
156    "SwapTotal:      524280 kB\n"
157    "SwapFree:       524200 kB\n"
158    "Dirty:               4 kB\n"
159    "Writeback:           0 kB\n"
160    "Mapped:          42236 kB\n"
161    "Slab:            25912 kB\n"
162    "Committed_AS:   118680 kB\n"
163    "PageTables:       1236 kB\n"
164    "VmallocTotal:  3874808 kB\n"
165    "VmallocUsed:      1416 kB\n"
166    "VmallocChunk:  3872908 kB\n"
167    "HugePages_Total:     0\n"
168    "HugePages_Free:      0\n"
169    "Hugepagesize:     4096 kB\n";
170
171  EXPECT_TRUE(ParseProcMeminfo(valid_input1, &meminfo));
172  EXPECT_EQ(meminfo.total, 3981504);
173  EXPECT_EQ(meminfo.free, 140764);
174  EXPECT_EQ(meminfo.buffers, 116480);
175  EXPECT_EQ(meminfo.cached, 406160);
176  EXPECT_EQ(meminfo.active_anon, 2972352);
177  EXPECT_EQ(meminfo.active_file, 179688);
178  EXPECT_EQ(meminfo.inactive_anon, 270108);
179  EXPECT_EQ(meminfo.inactive_file, 202748);
180  EXPECT_EQ(meminfo.swap_total, 5832280);
181  EXPECT_EQ(meminfo.swap_free, 3672368);
182  EXPECT_EQ(meminfo.dirty, 184);
183#if defined(OS_CHROMEOS)
184  EXPECT_EQ(meminfo.shmem, 140204);
185  EXPECT_EQ(meminfo.slab, 54212);
186#endif
187  EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo));
188  EXPECT_EQ(meminfo.total, 255908);
189  EXPECT_EQ(meminfo.free, 69936);
190  EXPECT_EQ(meminfo.buffers, 15812);
191  EXPECT_EQ(meminfo.cached, 115124);
192  EXPECT_EQ(meminfo.swap_total, 524280);
193  EXPECT_EQ(meminfo.swap_free, 524200);
194  EXPECT_EQ(meminfo.dirty, 4);
195}
196
197TEST_F(SystemMetricsTest, ParseVmstat) {
198  struct SystemMemoryInfoKB meminfo;
199  // part of vmstat from a 3.2 kernel with numa enabled
200  std::string valid_input1 =
201    "nr_free_pages 905104\n"
202    "nr_inactive_anon 142478"
203    "nr_active_anon 1520046\n"
204    "nr_inactive_file 4481001\n"
205    "nr_active_file 8313439\n"
206    "nr_unevictable 5044\n"
207    "nr_mlock 5044\n"
208    "nr_anon_pages 1633780\n"
209    "nr_mapped 104742\n"
210    "nr_file_pages 12828218\n"
211    "nr_dirty 245\n"
212    "nr_writeback 0\n"
213    "nr_slab_reclaimable 831609\n"
214    "nr_slab_unreclaimable 41164\n"
215    "nr_page_table_pages 31470\n"
216    "nr_kernel_stack 1735\n"
217    "nr_unstable 0\n"
218    "nr_bounce 0\n"
219    "nr_vmscan_write 406\n"
220    "nr_vmscan_immediate_reclaim 281\n"
221    "nr_writeback_temp 0\n"
222    "nr_isolated_anon 0\n"
223    "nr_isolated_file 0\n"
224    "nr_shmem 28820\n"
225    "nr_dirtied 84674644\n"
226    "nr_written 75307109\n"
227    "nr_anon_transparent_hugepages 0\n"
228    "nr_dirty_threshold 1536206\n"
229    "nr_dirty_background_threshold 768103\n"
230    "pgpgin 30777108\n"
231    "pgpgout 319023278\n"
232    "pswpin 179\n"
233    "pswpout 406\n"
234    "pgalloc_dma 0\n"
235    "pgalloc_dma32 20833399\n"
236    "pgalloc_normal 1622609290\n"
237    "pgalloc_movable 0\n"
238    "pgfree 1644355583\n"
239    "pgactivate 75391882\n"
240    "pgdeactivate 4121019\n"
241    "pgfault 2542879679\n"
242    "pgmajfault 487192\n";
243  std::string valid_input2 =
244    "nr_free_pages 180125\n"
245    "nr_inactive_anon 51\n"
246    "nr_active_anon 38832\n"
247    "nr_inactive_file 50171\n"
248    "nr_active_file 47510\n"
249    "nr_unevictable 0\n"
250    "nr_mlock 0\n"
251    "nr_anon_pages 38825\n"
252    "nr_mapped 24043\n"
253    "nr_file_pages 97733\n"
254    "nr_dirty 0\n"
255    "nr_writeback 0\n"
256    "nr_slab_reclaimable 4032\n"
257    "nr_slab_unreclaimable 2848\n"
258    "nr_page_table_pages 1505\n"
259    "nr_kernel_stack 626\n"
260    "nr_unstable 0\n"
261    "nr_bounce 0\n"
262    "nr_vmscan_write 0\n"
263    "nr_vmscan_immediate_reclaim 0\n"
264    "nr_writeback_temp 0\n"
265    "nr_isolated_anon 0\n"
266    "nr_isolated_file 0\n"
267    "nr_shmem 58\n"
268    "nr_dirtied 435358\n"
269    "nr_written 401258\n"
270    "nr_anon_transparent_hugepages 0\n"
271    "nr_dirty_threshold 18566\n"
272    "nr_dirty_background_threshold 4641\n"
273    "pgpgin 299464\n"
274    "pgpgout 2437788\n"
275    "pswpin 12\n"
276    "pswpout 901\n"
277    "pgalloc_normal 144213030\n"
278    "pgalloc_high 164501274\n"
279    "pgalloc_movable 0\n"
280    "pgfree 308894908\n"
281    "pgactivate 239320\n"
282    "pgdeactivate 1\n"
283    "pgfault 716044601\n"
284    "pgmajfault 2023\n"
285    "pgrefill_normal 0\n"
286    "pgrefill_high 0\n"
287    "pgrefill_movable 0\n";
288  EXPECT_TRUE(ParseProcVmstat(valid_input1, &meminfo));
289  EXPECT_EQ(meminfo.pswpin, 179);
290  EXPECT_EQ(meminfo.pswpout, 406);
291  EXPECT_EQ(meminfo.pgmajfault, 487192);
292  EXPECT_TRUE(ParseProcVmstat(valid_input2, &meminfo));
293  EXPECT_EQ(meminfo.pswpin, 12);
294  EXPECT_EQ(meminfo.pswpout, 901);
295  EXPECT_EQ(meminfo.pgmajfault, 2023);
296}
297#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
298
299#if defined(OS_LINUX) || defined(OS_CHROMEOS)
300
301// Test that ProcessMetrics::GetCPUUsage() doesn't return negative values when
302// the number of threads running on the process decreases between two successive
303// calls to it.
304TEST_F(SystemMetricsTest, TestNoNegativeCpuUsage) {
305  ProcessHandle handle = GetCurrentProcessHandle();
306  scoped_ptr<ProcessMetrics> metrics(
307      ProcessMetrics::CreateProcessMetrics(handle));
308
309  EXPECT_GE(metrics->GetCPUUsage(), 0.0);
310  Thread thread1("thread1");
311  Thread thread2("thread2");
312  Thread thread3("thread3");
313
314  thread1.StartAndWaitForTesting();
315  thread2.StartAndWaitForTesting();
316  thread3.StartAndWaitForTesting();
317
318  ASSERT_TRUE(thread1.IsRunning());
319  ASSERT_TRUE(thread2.IsRunning());
320  ASSERT_TRUE(thread3.IsRunning());
321
322  std::vector<std::string> vec1;
323  std::vector<std::string> vec2;
324  std::vector<std::string> vec3;
325
326  thread1.task_runner()->PostTask(FROM_HERE, Bind(&BusyWork, &vec1));
327  thread2.task_runner()->PostTask(FROM_HERE, Bind(&BusyWork, &vec2));
328  thread3.task_runner()->PostTask(FROM_HERE, Bind(&BusyWork, &vec3));
329
330  EXPECT_GE(metrics->GetCPUUsage(), 0.0);
331
332  thread1.Stop();
333  EXPECT_GE(metrics->GetCPUUsage(), 0.0);
334
335  thread2.Stop();
336  EXPECT_GE(metrics->GetCPUUsage(), 0.0);
337
338  thread3.Stop();
339  EXPECT_GE(metrics->GetCPUUsage(), 0.0);
340}
341
342#endif  // defined(OS_LINUX) || defined(OS_CHROMEOS)
343
344#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) || \
345    defined(OS_LINUX) || defined(OS_ANDROID)
346TEST(SystemMetrics2Test, GetSystemMemoryInfo) {
347  SystemMemoryInfoKB info;
348  EXPECT_TRUE(GetSystemMemoryInfo(&info));
349
350  // Ensure each field received a value.
351  EXPECT_GT(info.total, 0);
352  EXPECT_GT(info.free, 0);
353#if defined(OS_LINUX) || defined(OS_ANDROID)
354  EXPECT_GT(info.buffers, 0);
355  EXPECT_GT(info.cached, 0);
356  EXPECT_GT(info.active_anon, 0);
357  EXPECT_GT(info.inactive_anon, 0);
358  EXPECT_GT(info.active_file, 0);
359  EXPECT_GT(info.inactive_file, 0);
360#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
361
362  // All the values should be less than the total amount of memory.
363  EXPECT_LT(info.free, info.total);
364#if defined(OS_LINUX) || defined(OS_ANDROID)
365  EXPECT_LT(info.buffers, info.total);
366  EXPECT_LT(info.cached, info.total);
367  EXPECT_LT(info.active_anon, info.total);
368  EXPECT_LT(info.inactive_anon, info.total);
369  EXPECT_LT(info.active_file, info.total);
370  EXPECT_LT(info.inactive_file, info.total);
371#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
372
373#if defined(OS_CHROMEOS)
374  // Chrome OS exposes shmem.
375  EXPECT_GT(info.shmem, 0);
376  EXPECT_LT(info.shmem, info.total);
377  // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
378  // and gem_size cannot be tested here.
379#endif
380}
381#endif  // defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) ||
382        // defined(OS_LINUX) || defined(OS_ANDROID)
383
384#if defined(OS_LINUX) || defined(OS_ANDROID)
385TEST(ProcessMetricsTest, ParseProcStatCPU) {
386  // /proc/self/stat for a process running "top".
387  const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
388      "4202496 471 0 0 0 "
389      "12 16 0 0 "  // <- These are the goods.
390      "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
391      "4246868 140733983044336 18446744073709551615 140244213071219 "
392      "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
393  EXPECT_EQ(12 + 16, ParseProcStatCPU(kTopStat));
394
395  // cat /proc/self/stat on a random other machine I have.
396  const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
397      "0 142 0 0 0 "
398      "0 0 0 0 "  // <- No CPU, apparently.
399      "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
400      "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
401
402  EXPECT_EQ(0, ParseProcStatCPU(kSelfStat));
403
404  // Some weird long-running process with a weird name that I created for the
405  // purposes of this test.
406  const char kWeirdNameStat[] = "26115 (Hello) You ()))  ) R 24614 26115 24614"
407      " 34839 26115 4218880 227 0 0 0 "
408      "5186 11 0 0 "
409      "20 0 1 0 36933953 4296704 90 18446744073709551615 4194304 4196116 "
410      "140735857761568 140735857761160 4195644 0 0 0 0 0 0 0 17 14 0 0 0 0 0 "
411      "6295056 6295616 16519168 140735857770710 140735857770737 "
412      "140735857770737 140735857774557 0";
413  EXPECT_EQ(5186 + 11, ParseProcStatCPU(kWeirdNameStat));
414}
415#endif  // defined(OS_LINUX) || defined(OS_ANDROID)
416
417// Disable on Android because base_unittests runs inside a Dalvik VM that
418// starts and stop threads (crbug.com/175563).
419#if defined(OS_LINUX)
420// http://crbug.com/396455
421TEST(ProcessMetricsTest, DISABLED_GetNumberOfThreads) {
422  const ProcessHandle current = GetCurrentProcessHandle();
423  const int initial_threads = GetNumberOfThreads(current);
424  ASSERT_GT(initial_threads, 0);
425  const int kNumAdditionalThreads = 10;
426  {
427    scoped_ptr<Thread> my_threads[kNumAdditionalThreads];
428    for (int i = 0; i < kNumAdditionalThreads; ++i) {
429      my_threads[i].reset(new Thread("GetNumberOfThreadsTest"));
430      my_threads[i]->Start();
431      ASSERT_EQ(GetNumberOfThreads(current), initial_threads + 1 + i);
432    }
433  }
434  // The Thread destructor will stop them.
435  ASSERT_EQ(initial_threads, GetNumberOfThreads(current));
436}
437#endif  // defined(OS_LINUX)
438
439#if defined(OS_LINUX)
440namespace {
441
442// Keep these in sync so the GetOpenFdCount test can refer to correct test main.
443#define ChildMain ChildFdCount
444#define ChildMainString "ChildFdCount"
445
446// Command line flag name and file name used for synchronization.
447const char kTempDirFlag[] = "temp-dir";
448const char kSignalClosed[] = "closed";
449
450bool SignalEvent(const FilePath& signal_dir, const char* signal_file) {
451  File file(signal_dir.AppendASCII(signal_file),
452            File::FLAG_CREATE | File::FLAG_WRITE);
453  return file.IsValid();
454}
455
456// Check whether an event was signaled.
457bool CheckEvent(const FilePath& signal_dir, const char* signal_file) {
458  File file(signal_dir.AppendASCII(signal_file),
459            File::FLAG_OPEN | File::FLAG_READ);
460  return file.IsValid();
461}
462
463// Busy-wait for an event to be signaled.
464void WaitForEvent(const FilePath& signal_dir, const char* signal_file) {
465  while (!CheckEvent(signal_dir, signal_file))
466    PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
467}
468
469// Subprocess to test the number of open file descriptors.
470MULTIPROCESS_TEST_MAIN(ChildMain) {
471  CommandLine* command_line = CommandLine::ForCurrentProcess();
472  const FilePath temp_path = command_line->GetSwitchValuePath(kTempDirFlag);
473  CHECK(DirectoryExists(temp_path));
474
475  // Try to close all the file descriptors, so the open count goes to 0.
476  for (size_t i = 0; i < 1000; ++i)
477    close(i);
478  CHECK(SignalEvent(temp_path, kSignalClosed));
479
480  // Wait to be terminated.
481  while (true)
482    PlatformThread::Sleep(TimeDelta::FromSeconds(1));
483  return 0;
484}
485
486}  // namespace
487
488TEST(ProcessMetricsTest, GetOpenFdCount) {
489  ScopedTempDir temp_dir;
490  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
491  const FilePath temp_path = temp_dir.path();
492  CommandLine child_command_line(GetMultiProcessTestChildBaseCommandLine());
493  child_command_line.AppendSwitchPath(kTempDirFlag, temp_path);
494  Process child = SpawnMultiProcessTestChild(
495      ChildMainString, child_command_line, LaunchOptions());
496  ASSERT_TRUE(child.IsValid());
497  WaitForEvent(temp_path, kSignalClosed);
498
499  scoped_ptr<ProcessMetrics> metrics(
500      ProcessMetrics::CreateProcessMetrics(child.Handle()));
501  EXPECT_EQ(0, metrics->GetOpenFdCount());
502  ASSERT_TRUE(child.Terminate(0, true));
503}
504#endif  // defined(OS_LINUX)
505
506}  // namespace debug
507}  // namespace base
508