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 | |
27 namespace base { | 31 namespace base { |
28 namespace debug { | 32 namespace debug { |
29 | 33 |
30 #if defined(OS_LINUX) || defined(OS_CHROMEOS) | 34 #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
31 namespace { | 35 namespace { |
32 | 36 |
33 void BusyWork(std::vector<std::string>* vec) { | 37 void BusyWork(std::vector<std::string>* vec) { |
34 int64_t test_value = 0; | 38 int64_t test_value = 0; |
35 for (int i = 0; i < 100000; ++i) { | 39 for (int i = 0; i < 100000; ++i) { |
36 ++test_value; | 40 ++test_value; |
37 vec->push_back(Int64ToString(test_value)); | 41 vec->push_back(Int64ToString(test_value)); |
38 } | 42 } |
39 } | 43 } |
40 | 44 |
41 } // namespace | 45 } // namespace |
42 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) | 46 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) |
43 | 47 |
44 // Tests for SystemMetrics. | 48 // Tests for SystemMetrics. |
45 // Exists as a class so it can be a friend of SystemMetrics. | 49 // Exists as a class so it can be a friend of SystemMetrics. |
46 class SystemMetricsTest : public testing::Test { | 50 class SystemMetricsTest : public testing::Test { |
47 public: | 51 public: |
48 SystemMetricsTest() {} | 52 SystemMetricsTest() {} |
49 | 53 |
50 private: | 54 private: |
51 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); | 55 DISALLOW_COPY_AND_ASSIGN(SystemMetricsTest); |
52 }; | 56 }; |
53 | 57 |
54 ///////////////////////////////////////////////////////////////////////////// | 58 ///////////////////////////////////////////////////////////////////////////// |
55 | 59 |
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(r, 0); | |
Lei Zhang
2017/03/30 04:26:35
ASSERT_EQ(expected, actual);
Mark Mentovai
2017/03/30 11:56:15
Lei Zhang wrote:
erikchen
2017/03/30 18:02:48
Done.
| |
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(r, 0); | |
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 | |
56 #if defined(OS_LINUX) || defined(OS_ANDROID) | 96 #if defined(OS_LINUX) || defined(OS_ANDROID) |
57 TEST_F(SystemMetricsTest, IsValidDiskName) { | 97 TEST_F(SystemMetricsTest, IsValidDiskName) { |
58 std::string invalid_input1 = ""; | 98 std::string invalid_input1 = ""; |
59 std::string invalid_input2 = "s"; | 99 std::string invalid_input2 = "s"; |
60 std::string invalid_input3 = "sdz+"; | 100 std::string invalid_input3 = "sdz+"; |
61 std::string invalid_input4 = "hda0"; | 101 std::string invalid_input4 = "hda0"; |
62 std::string invalid_input5 = "mmcbl"; | 102 std::string invalid_input5 = "mmcbl"; |
63 std::string invalid_input6 = "mmcblka"; | 103 std::string invalid_input6 = "mmcblka"; |
64 std::string invalid_input7 = "mmcblkb"; | 104 std::string invalid_input7 = "mmcblkb"; |
65 std::string invalid_input8 = "mmmblk0"; | 105 std::string invalid_input8 = "mmmblk0"; |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 | 562 |
523 std::unique_ptr<ProcessMetrics> metrics( | 563 std::unique_ptr<ProcessMetrics> metrics( |
524 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); | 564 ProcessMetrics::CreateProcessMetrics(spawn_child.process.Handle())); |
525 EXPECT_EQ(0, metrics->GetOpenFdCount()); | 565 EXPECT_EQ(0, metrics->GetOpenFdCount()); |
526 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); | 566 ASSERT_TRUE(spawn_child.process.Terminate(0, true)); |
527 } | 567 } |
528 #endif // defined(OS_LINUX) | 568 #endif // defined(OS_LINUX) |
529 | 569 |
530 } // namespace debug | 570 } // namespace debug |
531 } // namespace base | 571 } // namespace base |
OLD | NEW |