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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
272 } // namespace debug | 273 } // namespace debug |
273 } // namespace base | 274 } // namespace base |
Nico
2013/11/21 00:19:26
Can you put the tests in debug::base? It looks wei
M-A Ruel
2013/11/30 01:07:16
Done.
| |
275 | |
276 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
277 TEST(SystemMetrics2Test, GetSystemMemoryInfo) { | |
278 base::SystemMemoryInfoKB info; | |
279 EXPECT_TRUE(base::GetSystemMemoryInfo(&info)); | |
280 | |
281 // Ensure each field received a value. | |
282 EXPECT_GT(info.total, 0); | |
283 EXPECT_GT(info.free, 0); | |
284 EXPECT_GT(info.buffers, 0); | |
285 EXPECT_GT(info.cached, 0); | |
286 EXPECT_GT(info.active_anon, 0); | |
287 EXPECT_GT(info.inactive_anon, 0); | |
288 EXPECT_GT(info.active_file, 0); | |
289 EXPECT_GT(info.inactive_file, 0); | |
290 | |
291 // All the values should be less than the total amount of memory. | |
292 EXPECT_LT(info.free, info.total); | |
293 EXPECT_LT(info.buffers, info.total); | |
294 EXPECT_LT(info.cached, info.total); | |
295 EXPECT_LT(info.active_anon, info.total); | |
296 EXPECT_LT(info.inactive_anon, info.total); | |
297 EXPECT_LT(info.active_file, info.total); | |
298 EXPECT_LT(info.inactive_file, info.total); | |
299 | |
300 #if defined(OS_CHROMEOS) | |
301 // Chrome OS exposes shmem. | |
302 EXPECT_GT(info.shmem, 0); | |
303 EXPECT_LT(info.shmem, info.total); | |
304 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects | |
305 // and gem_size cannot be tested here. | |
306 #endif | |
307 } | |
308 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
309 | |
310 #if defined(OS_WIN) | |
311 // TODO(estade): if possible, port this test. | |
312 TEST(ProcessMetricsTest, CalcFreeMemoryWin) { | |
Nico
2013/11/21 00:19:26
We don't usually call tests that currently run onl
M-A Ruel
2013/11/30 01:07:16
Done.
M-A Ruel
2013/11/30 01:07:16
Done.
| |
313 scoped_ptr<base::ProcessMetrics> metrics( | |
314 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); | |
315 ASSERT_TRUE(NULL != metrics.get()); | |
316 | |
317 bool using_tcmalloc = false; | |
318 | |
319 // Detect if we are using tcmalloc | |
320 #if !defined(NO_TCMALLOC) | |
321 const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); | |
322 if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0) | |
323 using_tcmalloc = true; | |
324 #endif | |
325 | |
326 // Typical values here is ~1900 for total and ~1000 for largest. Obviously | |
327 // it depends in what other tests have done to this process. | |
328 base::FreeMBytes free_mem1 = {0}; | |
329 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); | |
330 EXPECT_LT(10u, free_mem1.total); | |
331 EXPECT_LT(10u, free_mem1.largest); | |
332 EXPECT_GT(2048u, free_mem1.total); | |
333 EXPECT_GT(2048u, free_mem1.largest); | |
334 EXPECT_GE(free_mem1.total, free_mem1.largest); | |
335 EXPECT_TRUE(NULL != free_mem1.largest_ptr); | |
336 | |
337 // Allocate 20M and check again. It should have gone down. | |
338 const int kAllocMB = 20; | |
339 scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]); | |
340 size_t expected_total = free_mem1.total - kAllocMB; | |
341 size_t expected_largest = free_mem1.largest; | |
342 | |
343 base::FreeMBytes free_mem2 = {0}; | |
344 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); | |
345 EXPECT_GE(free_mem2.total, free_mem2.largest); | |
346 // This test is flaky when using tcmalloc, because tcmalloc | |
347 // allocation strategy sometimes results in less than the | |
348 // full drop of 20Mb of free memory. | |
349 if (!using_tcmalloc) | |
350 EXPECT_GE(expected_total, free_mem2.total); | |
351 EXPECT_GE(expected_largest, free_mem2.largest); | |
352 EXPECT_TRUE(NULL != free_mem2.largest_ptr); | |
353 } | |
354 #endif // defined(OS_WIN) | |
355 | |
356 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
357 TEST(ProcessMetricsTest, ParseProcStatCPU) { | |
358 // /proc/self/stat for a process running "top". | |
359 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " | |
360 "4202496 471 0 0 0 " | |
361 "12 16 0 0 " // <- These are the goods. | |
362 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " | |
363 "4246868 140733983044336 18446744073709551615 140244213071219 " | |
364 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; | |
365 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat)); | |
366 | |
367 // cat /proc/self/stat on a random other machine I have. | |
368 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " | |
369 "0 142 0 0 0 " | |
370 "0 0 0 0 " // <- No CPU, apparently. | |
371 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " | |
372 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; | |
373 | |
374 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); | |
375 } | |
376 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
377 | |
378 // Disable on Android because base_unittests runs inside a Dalvik VM that | |
379 // starts and stop threads (crbug.com/175563). | |
380 #if defined(OS_LINUX) | |
381 TEST(ProcessMetricsTest, GetNumberOfThreads) { | |
382 const base::ProcessHandle current = base::GetCurrentProcessHandle(); | |
383 const int initial_threads = base::GetNumberOfThreads(current); | |
384 ASSERT_GT(initial_threads, 0); | |
385 const int kNumAdditionalThreads = 10; | |
386 { | |
387 scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; | |
388 for (int i = 0; i < kNumAdditionalThreads; ++i) { | |
389 my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); | |
390 my_threads[i]->Start(); | |
391 ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); | |
392 } | |
393 } | |
394 // The Thread destructor will stop them. | |
395 ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); | |
396 } | |
397 #endif // defined(OS_LINUX) | |
OLD | NEW |