diff options
-rw-r--r-- | base/base.gyp | 4 | ||||
-rw-r--r-- | base/process/process_metrics_unittest.cc (renamed from base/process/process_metrics_unittests.cc) | 124 | ||||
-rw-r--r-- | base/process/process_metrics_unittest_ios.cc (renamed from base/process/process_util_unittest_ios.cc) | 7 | ||||
-rw-r--r-- | base/process/process_util_unittest.cc | 124 |
4 files changed, 132 insertions, 127 deletions
diff --git a/base/base.gyp b/base/base.gyp index 8a73064..d0c950b 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -601,9 +601,9 @@ 'process/memory_unittest.cc', 'process/memory_unittest_mac.h', 'process/memory_unittest_mac.mm', - 'process/process_metrics_unittests.cc', + 'process/process_metrics_unittest.cc', + 'process/process_metrics_unittest_ios.cc', 'process/process_util_unittest.cc', - 'process/process_util_unittest_ios.cc', 'profiler/tracked_time_unittest.cc', 'rand_util_unittest.cc', 'safe_numerics_unittest.cc', diff --git a/base/process/process_metrics_unittests.cc b/base/process/process_metrics_unittest.cc index 5014557..5d365d9 100644 --- a/base/process/process_metrics_unittests.cc +++ b/base/process/process_metrics_unittest.cc @@ -7,6 +7,7 @@ #include <sstream> #include <string> +#include "base/threading/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -269,5 +270,128 @@ TEST_F(SystemMetricsTest, ParseVmstat) { } #endif // defined(OS_LINUX) || defined(OS_ANDROID) +#if defined(OS_LINUX) || defined(OS_ANDROID) +TEST(SystemMetrics2Test, GetSystemMemoryInfo) { + base::SystemMemoryInfoKB info; + EXPECT_TRUE(base::GetSystemMemoryInfo(&info)); + + // Ensure each field received a value. + EXPECT_GT(info.total, 0); + EXPECT_GT(info.free, 0); + EXPECT_GT(info.buffers, 0); + EXPECT_GT(info.cached, 0); + EXPECT_GT(info.active_anon, 0); + EXPECT_GT(info.inactive_anon, 0); + EXPECT_GT(info.active_file, 0); + EXPECT_GT(info.inactive_file, 0); + + // All the values should be less than the total amount of memory. + EXPECT_LT(info.free, info.total); + EXPECT_LT(info.buffers, info.total); + EXPECT_LT(info.cached, info.total); + EXPECT_LT(info.active_anon, info.total); + EXPECT_LT(info.inactive_anon, info.total); + EXPECT_LT(info.active_file, info.total); + EXPECT_LT(info.inactive_file, info.total); + +#if defined(OS_CHROMEOS) + // Chrome OS exposes shmem. + EXPECT_GT(info.shmem, 0); + EXPECT_LT(info.shmem, info.total); + // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects + // and gem_size cannot be tested here. +#endif +} +#endif // defined(OS_LINUX) || defined(OS_ANDROID) + +#if defined(OS_WIN) +// TODO(estade): if possible, port this test. +TEST(ProcessMetricsTest, CalcFreeMemory) { + scoped_ptr<base::ProcessMetrics> metrics( + base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); + ASSERT_TRUE(NULL != metrics.get()); + + bool using_tcmalloc = false; + + // Detect if we are using tcmalloc +#if !defined(NO_TCMALLOC) + const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); + if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0) + using_tcmalloc = true; +#endif + + // Typical values here is ~1900 for total and ~1000 for largest. Obviously + // it depends in what other tests have done to this process. + base::FreeMBytes free_mem1 = {0}; + EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); + EXPECT_LT(10u, free_mem1.total); + EXPECT_LT(10u, free_mem1.largest); + EXPECT_GT(2048u, free_mem1.total); + EXPECT_GT(2048u, free_mem1.largest); + EXPECT_GE(free_mem1.total, free_mem1.largest); + EXPECT_TRUE(NULL != free_mem1.largest_ptr); + + // Allocate 20M and check again. It should have gone down. + const int kAllocMB = 20; + scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]); + size_t expected_total = free_mem1.total - kAllocMB; + size_t expected_largest = free_mem1.largest; + + base::FreeMBytes free_mem2 = {0}; + EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); + EXPECT_GE(free_mem2.total, free_mem2.largest); + // This test is flaky when using tcmalloc, because tcmalloc + // allocation strategy sometimes results in less than the + // full drop of 20Mb of free memory. + if (!using_tcmalloc) + EXPECT_GE(expected_total, free_mem2.total); + EXPECT_GE(expected_largest, free_mem2.largest); + EXPECT_TRUE(NULL != free_mem2.largest_ptr); +} +#endif // defined(OS_WIN) + +#if defined(OS_LINUX) || defined(OS_ANDROID) +TEST(ProcessMetricsTest, ParseProcStatCPU) { + // /proc/self/stat for a process running "top". + const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " + "4202496 471 0 0 0 " + "12 16 0 0 " // <- These are the goods. + "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " + "4246868 140733983044336 18446744073709551615 140244213071219 " + "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; + EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat)); + + // cat /proc/self/stat on a random other machine I have. + const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " + "0 142 0 0 0 " + "0 0 0 0 " // <- No CPU, apparently. + "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " + "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; + + EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); +} +#endif // defined(OS_LINUX) || defined(OS_ANDROID) + +// Disable on Android because base_unittests runs inside a Dalvik VM that +// starts and stop threads (crbug.com/175563). +#if defined(OS_LINUX) +TEST(ProcessMetricsTest, GetNumberOfThreads) { + const base::ProcessHandle current = base::GetCurrentProcessHandle(); + const int initial_threads = base::GetNumberOfThreads(current); + ASSERT_GT(initial_threads, 0); + const int kNumAdditionalThreads = 10; + { + scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; + for (int i = 0; i < kNumAdditionalThreads; ++i) { + my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); + my_threads[i]->Start(); + ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); + } + } + // The Thread destructor will stop them. + ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); +} +#endif // defined(OS_LINUX) + } // namespace debug } // namespace base diff --git a/base/process/process_util_unittest_ios.cc b/base/process/process_metrics_unittest_ios.cc index cad0f1b..3e1ca35 100644 --- a/base/process/process_util_unittest_ios.cc +++ b/base/process/process_metrics_unittest_ios.cc @@ -1,12 +1,13 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/memory/scoped_ptr.h" #include "base/process/process_metrics.h" + +#include "base/memory/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" -TEST(ProcessUtilTestIos, Memory) { +TEST(ProcessMetricsTestIos, Memory) { scoped_ptr<base::ProcessMetrics> process_metrics( base::ProcessMetrics::CreateProcessMetrics( base::GetCurrentProcessHandle())); diff --git a/base/process/process_util_unittest.cc b/base/process/process_util_unittest.cc index 80acb38..2e533067 100644 --- a/base/process/process_util_unittest.cc +++ b/base/process/process_util_unittest.cc @@ -353,85 +353,8 @@ TEST_F(ProcessUtilTest, SetProcessBackgroundedSelf) { EXPECT_EQ(old_priority, new_priority); } -#if defined(OS_LINUX) || defined(OS_ANDROID) -TEST_F(ProcessUtilTest, GetSystemMemoryInfo) { - base::SystemMemoryInfoKB info; - EXPECT_TRUE(base::GetSystemMemoryInfo(&info)); - - // Ensure each field received a value. - EXPECT_GT(info.total, 0); - EXPECT_GT(info.free, 0); - EXPECT_GT(info.buffers, 0); - EXPECT_GT(info.cached, 0); - EXPECT_GT(info.active_anon, 0); - EXPECT_GT(info.inactive_anon, 0); - EXPECT_GT(info.active_file, 0); - EXPECT_GT(info.inactive_file, 0); - - // All the values should be less than the total amount of memory. - EXPECT_LT(info.free, info.total); - EXPECT_LT(info.buffers, info.total); - EXPECT_LT(info.cached, info.total); - EXPECT_LT(info.active_anon, info.total); - EXPECT_LT(info.inactive_anon, info.total); - EXPECT_LT(info.active_file, info.total); - EXPECT_LT(info.inactive_file, info.total); - -#if defined(OS_CHROMEOS) - // Chrome OS exposes shmem. - EXPECT_GT(info.shmem, 0); - EXPECT_LT(info.shmem, info.total); - // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects - // and gem_size cannot be tested here. -#endif -} -#endif // defined(OS_LINUX) || defined(OS_ANDROID) - -// TODO(estade): if possible, port these 2 tests. #if defined(OS_WIN) -TEST_F(ProcessUtilTest, CalcFreeMemory) { - scoped_ptr<base::ProcessMetrics> metrics( - base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); - ASSERT_TRUE(NULL != metrics.get()); - - bool using_tcmalloc = false; - - // Detect if we are using tcmalloc -#if !defined(NO_TCMALLOC) - const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); - if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0) - using_tcmalloc = true; -#endif - - // Typical values here is ~1900 for total and ~1000 for largest. Obviously - // it depends in what other tests have done to this process. - base::FreeMBytes free_mem1 = {0}; - EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); - EXPECT_LT(10u, free_mem1.total); - EXPECT_LT(10u, free_mem1.largest); - EXPECT_GT(2048u, free_mem1.total); - EXPECT_GT(2048u, free_mem1.largest); - EXPECT_GE(free_mem1.total, free_mem1.largest); - EXPECT_TRUE(NULL != free_mem1.largest_ptr); - - // Allocate 20M and check again. It should have gone down. - const int kAllocMB = 20; - scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]); - size_t expected_total = free_mem1.total - kAllocMB; - size_t expected_largest = free_mem1.largest; - - base::FreeMBytes free_mem2 = {0}; - EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); - EXPECT_GE(free_mem2.total, free_mem2.largest); - // This test is flaky when using tcmalloc, because tcmalloc - // allocation strategy sometimes results in less than the - // full drop of 20Mb of free memory. - if (!using_tcmalloc) - EXPECT_GE(expected_total, free_mem2.total); - EXPECT_GE(expected_largest, free_mem2.largest); - EXPECT_TRUE(NULL != free_mem2.largest_ptr); -} - +// TODO(estade): if possible, port this test. TEST_F(ProcessUtilTest, GetAppOutput) { // Let's create a decently long message. std::string message; @@ -461,6 +384,7 @@ TEST_F(ProcessUtilTest, GetAppOutput) { EXPECT_EQ("", output); } +// TODO(estade): if possible, port this test. TEST_F(ProcessUtilTest, LaunchAsUser) { base::UserTokenHandle token; ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); @@ -828,50 +752,6 @@ TEST_F(ProcessUtilTest, GetParentProcessId) { EXPECT_EQ(ppid, getppid()); } -#if defined(OS_LINUX) || defined(OS_ANDROID) -TEST_F(ProcessUtilTest, ParseProcStatCPU) { - // /proc/self/stat for a process running "top". - const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " - "4202496 471 0 0 0 " - "12 16 0 0 " // <- These are the goods. - "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " - "4246868 140733983044336 18446744073709551615 140244213071219 " - "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; - EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat)); - - // cat /proc/self/stat on a random other machine I have. - const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " - "0 142 0 0 0 " - "0 0 0 0 " // <- No CPU, apparently. - "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " - "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; - - EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); -} - -// Disable on Android because base_unittests runs inside a Dalvik VM that -// starts and stop threads (crbug.com/175563). -#if !defined(OS_ANDROID) -TEST_F(ProcessUtilTest, GetNumberOfThreads) { - const base::ProcessHandle current = base::GetCurrentProcessHandle(); - const int initial_threads = base::GetNumberOfThreads(current); - ASSERT_GT(initial_threads, 0); - const int kNumAdditionalThreads = 10; - { - scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; - for (int i = 0; i < kNumAdditionalThreads; ++i) { - my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); - my_threads[i]->Start(); - ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); - } - } - // The Thread destructor will stop them. - ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); -} -#endif // !defined(OS_ANDROID) - -#endif // defined(OS_LINUX) || defined(OS_ANDROID) - // TODO(port): port those unit tests. bool IsProcessDead(base::ProcessHandle child) { // waitpid() will actually reap the process which is exactly NOT what we |