Index: base/process/process_metrics_unittest.cc |
diff --git a/base/process/process_metrics_unittests.cc b/base/process/process_metrics_unittest.cc |
similarity index 65% |
rename from base/process/process_metrics_unittests.cc |
rename to base/process/process_metrics_unittest.cc |
index 50145579a6c2d7c556856b9eedfedbda3a68159b..5d365d9a203e4fec4cb3777b5815c58955f6a321 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 |