OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/threading/thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 namespace debug { | 15 namespace debug { |
15 | 16 |
16 // Tests for SystemMetrics. | 17 // Tests for SystemMetrics. |
17 // Exists as a class so it can be a friend of SystemMetrics. | 18 // Exists as a class so it can be a friend of SystemMetrics. |
18 class SystemMetricsTest : public testing::Test { | 19 class SystemMetricsTest : public testing::Test { |
19 public: | 20 public: |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 EXPECT_TRUE(meminfo.pswpin == 179); | 263 EXPECT_TRUE(meminfo.pswpin == 179); |
263 EXPECT_TRUE(meminfo.pswpout == 406); | 264 EXPECT_TRUE(meminfo.pswpout == 406); |
264 EXPECT_TRUE(meminfo.pgmajfault == 487192); | 265 EXPECT_TRUE(meminfo.pgmajfault == 487192); |
265 EXPECT_TRUE(ParseProcVmstat(valid_input2, &meminfo)); | 266 EXPECT_TRUE(ParseProcVmstat(valid_input2, &meminfo)); |
266 EXPECT_TRUE(meminfo.pswpin == 12); | 267 EXPECT_TRUE(meminfo.pswpin == 12); |
267 EXPECT_TRUE(meminfo.pswpout == 901); | 268 EXPECT_TRUE(meminfo.pswpout == 901); |
268 EXPECT_TRUE(meminfo.pgmajfault == 2023); | 269 EXPECT_TRUE(meminfo.pgmajfault == 2023); |
269 } | 270 } |
270 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 271 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
271 | 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 |
272 } // namespace debug | 396 } // namespace debug |
273 } // namespace base | 397 } // namespace base |
OLD | NEW |