Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <sstream> | 10 #include <sstream> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/files/file.h" | 15 #include "base/files/file.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 21 #include "base/test/multiprocess_test.h" | 21 #include "base/test/multiprocess_test.h" |
| 22 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "testing/multiprocess_func_list.h" | 25 #include "testing/multiprocess_func_list.h" |
| 26 | 26 |
| 27 #if defined(OS_MACOSX) | |
| 28 #include <sys/mman.h> | |
| 29 | |
| 30 #include "base/memory/free_deleter.h" | |
| 31 #endif | |
| 32 | |
| 27 namespace base { | 33 namespace base { |
| 28 namespace debug { | 34 namespace debug { |
| 29 | 35 |
| 30 #if defined(OS_LINUX) || defined(OS_CHROMEOS) | 36 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 31 namespace { | 37 namespace { |
| 32 | 38 |
| 33 void BusyWork(std::vector<std::string>* vec) { | 39 void BusyWork(std::vector<std::string>* vec) { |
| 34 int64_t test_value = 0; | 40 int64_t test_value = 0; |
| 35 for (int i = 0; i < 100000; ++i) { | 41 for (int i = 0; i < 100000; ++i) { |
| 36 ++test_value; | 42 ++test_value; |
| 37 vec->push_back(Int64ToString(test_value)); | 43 vec->push_back(Int64ToString(test_value)); |
| 38 } | 44 } |
| 39 } | 45 } |
| 40 | 46 |
| 41 } // namespace | 47 } // namespace |
| 42 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) | 48 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 43 | 49 |
| 44 // Tests for SystemMetrics. | 50 // Tests for SystemMetrics. |
| 45 // Exists as a class so it can be a friend of SystemMetrics. | 51 // Exists as a class so it can be a friend of SystemMetrics. |
| 46 class SystemMetricsTest : public testing::Test { | 52 class SystemMetricsTest : public testing::Test { |
| 47 public: | 53 public: |
| 48 SystemMetricsTest() {} | 54 SystemMetricsTest() {} |
| 49 | 55 |
| 50 private: | 56 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); | 57 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); |
| 52 }; | 58 }; |
| 53 | 59 |
| 54 ///////////////////////////////////////////////////////////////////////////// | 60 ///////////////////////////////////////////////////////////////////////////// |
| 55 | 61 |
| 62 #if defined(OS_MACOSX) | |
| 63 TEST_F(SystemMetricsTest, WiredBytes) { | |
| 64 ProcessHandle handle = GetCurrentProcessHandle(); | |
| 65 std::unique_ptr<ProcessMetrics> metrics( | |
| 66 ProcessMetrics::CreateProcessMetrics(handle, nullptr)); | |
| 67 | |
| 68 size_t initial_wired_bytes; | |
| 69 bool result = | |
| 70 metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &initial_wired_bytes); | |
| 71 ASSERT_TRUE(result); | |
| 72 | |
| 73 size_t size = 999 * PAGE_SIZE; | |
| 74 std::unique_ptr<char, base::FreeDeleter> memory( | |
|
Primiano Tucci (use gerrit)
2017/03/28 20:32:54
just std::unique_ptr<char[])(new char[size)); ?
erikchen
2017/03/30 00:03:14
Done.
| |
| 75 static_cast<char*>(malloc(size))); | |
| 76 int r = mlock(memory.get(), size); | |
| 77 ASSERT_EQ(r, 0); | |
| 78 | |
| 79 size_t new_wired_bytes; | |
| 80 result = metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &new_wired_bytes); | |
| 81 ASSERT_TRUE(result); | |
| 82 EXPECT_EQ(initial_wired_bytes + size, new_wired_bytes); | |
| 83 | |
| 84 r = munlock(memory.get(), size); | |
| 85 ASSERT_EQ(r, 0); | |
| 86 | |
| 87 result = metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &new_wired_bytes); | |
| 88 ASSERT_TRUE(result); | |
| 89 EXPECT_EQ(initial_wired_bytes, new_wired_bytes); | |
|
Primiano Tucci (use gerrit)
2017/03/28 20:32:54
I got bitten in the past by this sort of tests. Th
Mark Mentovai
2017/03/28 20:56:11
Is gtest really running its own threads? That’s su
erikchen
2017/03/30 00:03:14
Done.
| |
| 90 } | |
| 91 #endif // defined(OS_MACOSX) | |
| 92 | |
| 56 #if defined(OS_LINUX) || defined(OS_ANDROID) | 93 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 57 TEST_F(SystemMetricsTest, IsValidDiskName) { | 94 TEST_F(SystemMetricsTest, IsValidDiskName) { |
| 58 std::string invalid_input1 = ""; | 95 std::string invalid_input1 = ""; |
| 59 std::string invalid_input2 = "s"; | 96 std::string invalid_input2 = "s"; |
| 60 std::string invalid_input3 = "sdz+"; | 97 std::string invalid_input3 = "sdz+"; |
| 61 std::string invalid_input4 = "hda0"; | 98 std::string invalid_input4 = "hda0"; |
| 62 std::string invalid_input5 = "mmcbl"; | 99 std::string invalid_input5 = "mmcbl"; |
| 63 std::string invalid_input6 = "mmcblka"; | 100 std::string invalid_input6 = "mmcblka"; |
| 64 std::string invalid_input7 = "mmcblkb"; | 101 std::string invalid_input7 = "mmcblkb"; |
| 65 std::string invalid_input8 = "mmmblk0"; | 102 std::string invalid_input8 = "mmmblk0"; |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 | 559 |
| 523 std::unique_ptr<ProcessMetrics> metrics( | 560 std::unique_ptr<ProcessMetrics> metrics( |
| 524 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); | 561 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); |
| 525 EXPECT_EQ(0, metrics->GetOpenFdCount()); | 562 EXPECT_EQ(0, metrics->GetOpenFdCount()); |
| 526 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); | 563 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); |
| 527 } | 564 } |
| 528 #endif // defined(OS_LINUX) | 565 #endif // defined(OS_LINUX) |
| 529 | 566 |
| 530 } // namespace debug | 567 } // namespace debug |
| 531 } // namespace base | 568 } // namespace base |
| OLD | NEW |