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 #endif | |
30 | |
31 namespace base { | 27 namespace base { |
32 namespace debug { | 28 namespace debug { |
33 | 29 |
34 #if defined(OS_LINUX) || defined(OS_CHROMEOS) | 30 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
35 namespace { | 31 namespace { |
36 | 32 |
37 void BusyWork(std::vector<std::string>* vec) { | 33 void BusyWork(std::vector<std::string>* vec) { |
38 int64_t test_value = 0; | 34 int64_t test_value = 0; |
39 for (int i = 0; i < 100000; ++i) { | 35 for (int i = 0; i < 100000; ++i) { |
40 ++test_value; | 36 ++test_value; |
41 vec->push_back(Int64ToString(test_value)); | 37 vec->push_back(Int64ToString(test_value)); |
42 } | 38 } |
43 } | 39 } |
44 | 40 |
45 } // namespace | 41 } // namespace |
46 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) | 42 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
47 | 43 |
48 // Tests for SystemMetrics. | 44 // Tests for SystemMetrics. |
49 // Exists as a class so it can be a friend of SystemMetrics. | 45 // Exists as a class so it can be a friend of SystemMetrics. |
50 class SystemMetricsTest : public testing::Test { | 46 class SystemMetricsTest : public testing::Test { |
51 public: | 47 public: |
52 SystemMetricsTest() {} | 48 SystemMetricsTest() {} |
53 | 49 |
54 private: | 50 private: |
55 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); | 51 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); |
56 }; | 52 }; |
57 | 53 |
58 ///////////////////////////////////////////////////////////////////////////// | 54 ///////////////////////////////////////////////////////////////////////////// |
59 | 55 |
60 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
61 TEST_F(SystemMetricsTest, LockedBytes) { | |
62 ProcessHandle handle = GetCurrentProcessHandle(); | |
63 std::unique_ptr<ProcessMetrics> metrics( | |
64 ProcessMetrics::CreateProcessMetrics(handle, nullptr)); | |
65 | |
66 size_t initial_locked_bytes; | |
67 bool result = | |
68 metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &initial_locked_bytes); | |
69 ASSERT_TRUE(result); | |
70 | |
71 size_t size = 8 * 1024 * 1024; | |
72 std::unique_ptr<char[]> memory(new char[size]); | |
73 int r = mlock(memory.get(), size); | |
74 ASSERT_EQ(0, r); | |
75 | |
76 size_t new_locked_bytes; | |
77 result = | |
78 metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &new_locked_bytes); | |
79 ASSERT_TRUE(result); | |
80 | |
81 // There should be around |size| more locked bytes, but multi-threading might | |
82 // cause noise. | |
83 EXPECT_LT(initial_locked_bytes + size / 2, new_locked_bytes); | |
84 EXPECT_GT(initial_locked_bytes + size * 1.5, new_locked_bytes); | |
85 | |
86 r = munlock(memory.get(), size); | |
87 ASSERT_EQ(0, r); | |
88 | |
89 result = | |
90 metrics->GetMemoryBytes(nullptr, nullptr, nullptr, &new_locked_bytes); | |
91 ASSERT_TRUE(result); | |
92 EXPECT_EQ(initial_locked_bytes, new_locked_bytes); | |
93 } | |
94 #endif // defined(OS_MACOSX) | |
95 | |
96 #if defined(OS_LINUX) || defined(OS_ANDROID) | 56 #if defined(OS_LINUX) || defined(OS_ANDROID) |
97 TEST_F(SystemMetricsTest, IsValidDiskName) { | 57 TEST_F(SystemMetricsTest, IsValidDiskName) { |
98 std::string invalid_input1 = ""; | 58 std::string invalid_input1 = ""; |
99 std::string invalid_input2 = "s"; | 59 std::string invalid_input2 = "s"; |
100 std::string invalid_input3 = "sdz+"; | 60 std::string invalid_input3 = "sdz+"; |
101 std::string invalid_input4 = "hda0"; | 61 std::string invalid_input4 = "hda0"; |
102 std::string invalid_input5 = "mmcbl"; | 62 std::string invalid_input5 = "mmcbl"; |
103 std::string invalid_input6 = "mmcblka"; | 63 std::string invalid_input6 = "mmcblka"; |
104 std::string invalid_input7 = "mmcblkb"; | 64 std::string invalid_input7 = "mmcblkb"; |
105 std::string invalid_input8 = "mmmblk0"; | 65 std::string invalid_input8 = "mmmblk0"; |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 | 522 |
563 std::unique_ptr<ProcessMetrics> metrics( | 523 std::unique_ptr<ProcessMetrics> metrics( |
564 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); | 524 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); |
565 EXPECT_EQ(0, metrics->GetOpenFdCount()); | 525 EXPECT_EQ(0, metrics->GetOpenFdCount()); |
566 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); | 526 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); |
567 } | 527 } |
568 #endif // defined(OS_LINUX) | 528 #endif // defined(OS_LINUX) |
569 | 529 |
570 } // namespace debug | 530 } // namespace debug |
571 } // namespace base | 531 } // namespace base |
OLD | NEW |