Home | History | Annotate | Download | only in process
      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 "base/threading/thread.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 
     14 namespace base {
     15 namespace debug {
     16 
     17 // Tests for SystemMetrics.
     18 // Exists as a class so it can be a friend of SystemMetrics.
     19 class SystemMetricsTest : public testing::Test {
     20  public:
     21   SystemMetricsTest() {}
     22 
     23  private:
     24   DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest);
     25 };
     26 
     27 /////////////////////////////////////////////////////////////////////////////
     28 
     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";
     39 
     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));
     48 
     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";
     54 
     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));
     60 }
     61 
     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"
     71     "SwapCached:           12 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));
     79 
     80   std::string valid_input1 =
     81     "MemTotal:        3981504 kB\n"
     82     "MemFree:          140764 kB\n"
     83     "Buffers:          116480 kB\n"
     84     "Cached:           406160 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"
     92     "Unevictable:           0 kB\n"
     93     "Mlocked:               0 kB\n"
     94     "SwapTotal:       5832280 kB\n"
     95     "SwapFree:        3672368 kB\n"
     96     "Dirty:               184 kB\n"
     97     "Writeback:             0 kB\n"
     98     "AnonPages:       3101224 kB\n"
     99     "Mapped:           142296 kB\n"
    100     "Shmem:            140204 kB\n"
    101     "Slab:              54212 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"
    107     "Bounce:                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"
    123     "SwapCached:          0 kB\n"
    124     "Active:          92700 kB\n"
    125     "Inactive:        63792 kB\n"
    126     "HighTotal:           0 kB\n"
    127     "HighFree:            0 kB\n"
    128     "LowTotal:       255908 kB\n"
    129     "LowFree:         69936 kB\n"
    130     "SwapTotal:      524280 kB\n"
    131     "SwapFree:       524200 kB\n"
    132     "Dirty:               4 kB\n"
    133     "Writeback:           0 kB\n"
    134     "Mapped:          42236 kB\n"
    135     "Slab:            25912 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";
    144 
    145   EXPECT_TRUE(ParseProcMeminfo(valid_input1, &meminfo));
    146   EXPECT_TRUE(meminfo.total == 3981504);
    147   EXPECT_TRUE(meminfo.free == 140764);
    148   EXPECT_TRUE(meminfo.buffers == 116480);
    149   EXPECT_TRUE(meminfo.cached == 406160);
    150   EXPECT_TRUE(meminfo.active_anon == 2972352);
    151   EXPECT_TRUE(meminfo.active_file == 179688);
    152   EXPECT_TRUE(meminfo.inactive_anon == 270108);
    153   EXPECT_TRUE(meminfo.inactive_file == 202748);
    154   EXPECT_TRUE(meminfo.swap_total == 5832280);
    155   EXPECT_TRUE(meminfo.swap_free == 3672368);
    156   EXPECT_TRUE(meminfo.dirty == 184);
    157 #if defined(OS_CHROMEOS)
    158   EXPECT_TRUE(meminfo.shmem == 140204);
    159   EXPECT_TRUE(meminfo.slab == 54212);
    160 #endif
    161   EXPECT_TRUE(ParseProcMeminfo(valid_input2, &meminfo));
    162   EXPECT_TRUE(meminfo.total == 255908);
    163   EXPECT_TRUE(meminfo.free == 69936);
    164   EXPECT_TRUE(meminfo.buffers == 15812);
    165   EXPECT_TRUE(meminfo.cached == 115124);
    166   EXPECT_TRUE(meminfo.swap_total == 524280);
    167   EXPECT_TRUE(meminfo.swap_free == 524200);
    168   EXPECT_TRUE(meminfo.dirty == 4);
    169 }
    170 
    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"
    181     "nr_mlock 5044\n"
    182     "nr_anon_pages 1633780\n"
    183     "nr_mapped 104742\n"
    184     "nr_file_pages 12828218\n"
    185     "nr_dirty 245\n"
    186     "nr_writeback 0\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"
    191     "nr_unstable 0\n"
    192     "nr_bounce 0\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"
    198     "nr_shmem 28820\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"
    204     "pgpgin 30777108\n"
    205     "pgpgout 319023278\n"
    206     "pswpin 179\n"
    207     "pswpout 406\n"
    208     "pgalloc_dma 0\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"
    223     "nr_unevictable 0\n"
    224     "nr_mlock 0\n"
    225     "nr_anon_pages 38825\n"
    226     "nr_mapped 24043\n"
    227     "nr_file_pages 97733\n"
    228     "nr_dirty 0\n"
    229     "nr_writeback 0\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"
    234     "nr_unstable 0\n"
    235     "nr_bounce 0\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"
    241     "nr_shmem 58\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"
    247     "pgpgin 299464\n"
    248     "pgpgout 2437788\n"
    249     "pswpin 12\n"
    250     "pswpout 901\n"
    251     "pgalloc_normal 144213030\n"
    252     "pgalloc_high 164501274\n"
    253     "pgalloc_movable 0\n"
    254     "pgfree 308894908\n"
    255     "pgactivate 239320\n"
    256     "pgdeactivate 1\n"
    257     "pgfault 716044601\n"
    258     "pgmajfault 2023\n"
    259     "pgrefill_normal 0\n"
    260     "pgrefill_high 0\n"
    261     "pgrefill_movable 0\n";
    262   EXPECT_TRUE(ParseProcVmstat(valid_input1, &meminfo));
    263   EXPECT_TRUE(meminfo.pswpin == 179);
    264   EXPECT_TRUE(meminfo.pswpout == 406);
    265   EXPECT_TRUE(meminfo.pgmajfault == 487192);
    266   EXPECT_TRUE(ParseProcVmstat(valid_input2, &meminfo));
    267   EXPECT_TRUE(meminfo.pswpin == 12);
    268   EXPECT_TRUE(meminfo.pswpout == 901);
    269   EXPECT_TRUE(meminfo.pgmajfault == 2023);
    270 }
    271 #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
    272 
    273 #if defined(OS_LINUX) || defined(OS_ANDROID)
    274 TEST(SystemMetrics2Test, GetSystemMemoryInfo) {
    275   base::SystemMemoryInfoKB info;
    276   EXPECT_TRUE(base::GetSystemMemoryInfo(&info));
    277 
    278   // Ensure each field received a value.
    279   EXPECT_GT(info.total, 0);
    280   EXPECT_GT(info.free, 0);
    281   EXPECT_GT(info.buffers, 0);
    282   EXPECT_GT(info.cached, 0);
    283   EXPECT_GT(info.active_anon, 0);
    284   EXPECT_GT(info.inactive_anon, 0);
    285   EXPECT_GT(info.active_file, 0);
    286   EXPECT_GT(info.inactive_file, 0);
    287 
    288   // All the values should be less than the total amount of memory.
    289   EXPECT_LT(info.free, info.total);
    290   EXPECT_LT(info.buffers, info.total);
    291   EXPECT_LT(info.cached, info.total);
    292   EXPECT_LT(info.active_anon, info.total);
    293   EXPECT_LT(info.inactive_anon, info.total);
    294   EXPECT_LT(info.active_file, info.total);
    295   EXPECT_LT(info.inactive_file, info.total);
    296 
    297 #if defined(OS_CHROMEOS)
    298   // Chrome OS exposes shmem.
    299   EXPECT_GT(info.shmem, 0);
    300   EXPECT_LT(info.shmem, info.total);
    301   // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
    302   // and gem_size cannot be tested here.
    303 #endif
    304 }
    305 #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
    306 
    307 #if defined(OS_WIN)
    308 // TODO(estade): if possible, port this test.
    309 TEST(ProcessMetricsTest, CalcFreeMemory) {
    310   scoped_ptr<base::ProcessMetrics> metrics(
    311       base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()));
    312   ASSERT_TRUE(NULL != metrics.get());
    313 
    314   bool using_tcmalloc = false;
    315 
    316   // Detect if we are using tcmalloc
    317 #if !defined(NO_TCMALLOC)
    318   const char* chrome_allocator = getenv("CHROME_ALLOCATOR");
    319   if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0)
    320     using_tcmalloc = true;
    321 #endif
    322 
    323   // Typical values here is ~1900 for total and ~1000 for largest. Obviously
    324   // it depends in what other tests have done to this process.
    325   base::FreeMBytes free_mem1 = {0};
    326   EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1));
    327   EXPECT_LT(10u, free_mem1.total);
    328   EXPECT_LT(10u, free_mem1.largest);
    329   EXPECT_GT(2048u, free_mem1.total);
    330   EXPECT_GT(2048u, free_mem1.largest);
    331   EXPECT_GE(free_mem1.total, free_mem1.largest);
    332   EXPECT_TRUE(NULL != free_mem1.largest_ptr);
    333 
    334   // Allocate 20M and check again. It should have gone down.
    335   const int kAllocMB = 20;
    336   scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]);
    337   size_t expected_total = free_mem1.total - kAllocMB;
    338   size_t expected_largest = free_mem1.largest;
    339 
    340   base::FreeMBytes free_mem2 = {0};
    341   EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2));
    342   EXPECT_GE(free_mem2.total, free_mem2.largest);
    343   // This test is flaky when using tcmalloc, because tcmalloc
    344   // allocation strategy sometimes results in less than the
    345   // full drop of 20Mb of free memory.
    346   if (!using_tcmalloc)
    347     EXPECT_GE(expected_total, free_mem2.total);
    348   EXPECT_GE(expected_largest, free_mem2.largest);
    349   EXPECT_TRUE(NULL != free_mem2.largest_ptr);
    350 }
    351 #endif  // defined(OS_WIN)
    352 
    353 #if defined(OS_LINUX) || defined(OS_ANDROID)
    354 TEST(ProcessMetricsTest, ParseProcStatCPU) {
    355   // /proc/self/stat for a process running "top".
    356   const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
    357       "4202496 471 0 0 0 "
    358       "12 16 0 0 "  // <- These are the goods.
    359       "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
    360       "4246868 140733983044336 18446744073709551615 140244213071219 "
    361       "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
    362   EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat));
    363 
    364   // cat /proc/self/stat on a random other machine I have.
    365   const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
    366       "0 142 0 0 0 "
    367       "0 0 0 0 "  // <- No CPU, apparently.
    368       "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
    369       "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
    370 
    371   EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
    372 }
    373 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
    374 
    375 // Disable on Android because base_unittests runs inside a Dalvik VM that
    376 // starts and stop threads (crbug.com/175563).
    377 #if defined(OS_LINUX)
    378 TEST(ProcessMetricsTest, GetNumberOfThreads) {
    379   const base::ProcessHandle current = base::GetCurrentProcessHandle();
    380   const int initial_threads = base::GetNumberOfThreads(current);
    381   ASSERT_GT(initial_threads, 0);
    382   const int kNumAdditionalThreads = 10;
    383   {
    384     scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads];
    385     for (int i = 0; i < kNumAdditionalThreads; ++i) {
    386       my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest"));
    387       my_threads[i]->Start();
    388       ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i);
    389     }
    390   }
    391   // The Thread destructor will stop them.
    392   ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current));
    393 }
    394 #endif  // defined(OS_LINUX)
    395 
    396 }  // namespace debug
    397 }  // namespace base
    398