OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/tracing/common/process_metrics_memory_dump_provider.h" | 5 #include "components/tracing/common/process_metrics_memory_dump_provider.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
| 10 #include <unordered_set> |
10 | 11 |
11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/process/process_metrics.h" | 14 #include "base/process/process_metrics.h" |
| 15 #include "base/trace_event/memory_dump_manager.h" |
14 #include "base/trace_event/process_memory_dump.h" | 16 #include "base/trace_event/process_memory_dump.h" |
15 #include "base/trace_event/process_memory_maps.h" | 17 #include "base/trace_event/process_memory_maps.h" |
16 #include "base/trace_event/process_memory_totals.h" | 18 #include "base/trace_event/process_memory_totals.h" |
17 #include "base/trace_event/trace_event_argument.h" | 19 #include "base/trace_event/trace_event_argument.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
21 #include <libgen.h> | 23 #include <libgen.h> |
22 #include <mach-o/dyld.h> | 24 #include <mach-o/dyld.h> |
23 #endif | 25 #endif |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 file->reset(temp_file); | 128 file->reset(temp_file); |
127 ASSERT_TRUE(temp_file); | 129 ASSERT_TRUE(temp_file); |
128 | 130 |
129 ASSERT_TRUE( | 131 ASSERT_TRUE( |
130 base::WriteFileDescriptor(fileno(temp_file), contents, strlen(contents))); | 132 base::WriteFileDescriptor(fileno(temp_file), contents, strlen(contents))); |
131 } | 133 } |
132 | 134 |
133 } // namespace | 135 } // namespace |
134 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 136 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
135 | 137 |
| 138 class MockMemoryDumpProvider : public ProcessMetricsMemoryDumpProvider { |
| 139 public: |
| 140 MockMemoryDumpProvider(base::ProcessId process); |
| 141 ~MockMemoryDumpProvider() override; |
| 142 }; |
| 143 |
| 144 std::unordered_set<MockMemoryDumpProvider*> g_live_mocks; |
| 145 std::unordered_set<MockMemoryDumpProvider*> g_dead_mocks; |
| 146 |
| 147 MockMemoryDumpProvider::MockMemoryDumpProvider(base::ProcessId process) |
| 148 : ProcessMetricsMemoryDumpProvider(process) { |
| 149 g_live_mocks.insert(this); |
| 150 } |
| 151 |
| 152 MockMemoryDumpProvider::~MockMemoryDumpProvider() { |
| 153 g_live_mocks.erase(this); |
| 154 g_dead_mocks.insert(this); |
| 155 } |
| 156 |
136 TEST(ProcessMetricsMemoryDumpProviderTest, DumpRSS) { | 157 TEST(ProcessMetricsMemoryDumpProviderTest, DumpRSS) { |
137 const base::trace_event::MemoryDumpArgs high_detail_args = { | 158 const base::trace_event::MemoryDumpArgs high_detail_args = { |
138 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 159 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
139 std::unique_ptr<ProcessMetricsMemoryDumpProvider> pmtdp( | 160 std::unique_ptr<ProcessMetricsMemoryDumpProvider> pmtdp( |
140 new ProcessMetricsMemoryDumpProvider(base::kNullProcessId)); | 161 new ProcessMetricsMemoryDumpProvider(base::kNullProcessId)); |
141 std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd_before( | 162 std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd_before( |
142 new base::trace_event::ProcessMemoryDump(nullptr, high_detail_args)); | 163 new base::trace_event::ProcessMemoryDump(nullptr, high_detail_args)); |
143 std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd_after( | 164 std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd_after( |
144 new base::trace_event::ProcessMemoryDump(nullptr, high_detail_args)); | 165 new base::trace_event::ProcessMemoryDump(nullptr, high_detail_args)); |
145 | 166 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes); | 258 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes); |
238 EXPECT_EQ(0U, regions_2[0].protection_flags); | 259 EXPECT_EQ(0U, regions_2[0].protection_flags); |
239 EXPECT_EQ("", regions_2[0].mapped_file); | 260 EXPECT_EQ("", regions_2[0].mapped_file); |
240 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident); | 261 EXPECT_EQ(32 * 1024UL, regions_2[0].byte_stats_proportional_resident); |
241 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_shared_clean_resident); | 262 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_shared_clean_resident); |
242 EXPECT_EQ(12 * 1024UL, regions_2[0].byte_stats_shared_dirty_resident); | 263 EXPECT_EQ(12 * 1024UL, regions_2[0].byte_stats_shared_dirty_resident); |
243 EXPECT_EQ(8 * 1024UL, regions_2[0].byte_stats_private_clean_resident); | 264 EXPECT_EQ(8 * 1024UL, regions_2[0].byte_stats_private_clean_resident); |
244 EXPECT_EQ(4 * 1024UL, regions_2[0].byte_stats_private_dirty_resident); | 265 EXPECT_EQ(4 * 1024UL, regions_2[0].byte_stats_private_dirty_resident); |
245 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped); | 266 EXPECT_EQ(0 * 1024UL, regions_2[0].byte_stats_swapped); |
246 } | 267 } |
| 268 |
| 269 TEST(ProcessMetricsMemoryDumpProviderTest, DoubleRegister) { |
| 270 auto factory = [](base::ProcessId process) { |
| 271 return std::unique_ptr<ProcessMetricsMemoryDumpProvider>( |
| 272 new MockMemoryDumpProvider(process)); |
| 273 }; |
| 274 ProcessMetricsMemoryDumpProvider::factory_for_testing = factory; |
| 275 ProcessMetricsMemoryDumpProvider::RegisterForProcess(1); |
| 276 ProcessMetricsMemoryDumpProvider::RegisterForProcess(1); |
| 277 ASSERT_EQ(1u, g_live_mocks.size()); |
| 278 ASSERT_EQ(1u, g_dead_mocks.size()); |
| 279 auto* manager = base::trace_event::MemoryDumpManager::GetInstance(); |
| 280 MockMemoryDumpProvider* live_mock = *g_live_mocks.begin(); |
| 281 EXPECT_TRUE(manager->IsDumpProviderRegisteredForTesting(live_mock)); |
| 282 auto* dead_mock = *g_dead_mocks.begin(); |
| 283 EXPECT_FALSE(manager->IsDumpProviderRegisteredForTesting(dead_mock)); |
| 284 ProcessMetricsMemoryDumpProvider::UnregisterForProcess(1); |
| 285 g_live_mocks.clear(); |
| 286 g_dead_mocks.clear(); |
| 287 } |
| 288 |
247 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 289 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
248 | 290 |
249 TEST(ProcessMetricsMemoryDumpProviderTest, TestPollFastMemoryTotal) { | 291 TEST(ProcessMetricsMemoryDumpProviderTest, TestPollFastMemoryTotal) { |
250 ProcessMetricsMemoryDumpProvider mdp(base::kNullProcessId); | 292 ProcessMetricsMemoryDumpProvider mdp(base::kNullProcessId); |
251 | 293 |
252 uint64_t total1, total2; | 294 uint64_t total1, total2; |
253 mdp.PollFastMemoryTotal(&total1); | 295 mdp.PollFastMemoryTotal(&total1); |
254 ASSERT_GT(total1, 0u); | 296 ASSERT_GT(total1, 0u); |
255 size_t kBufSize = 16 * 1024 * 1024; | 297 size_t kBufSize = 16 * 1024 * 1024; |
256 auto buf = base::MakeUnique<char[]>(kBufSize); | 298 auto buf = base::MakeUnique<char[]>(kBufSize); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 return a.start_address < b.start_address; | 427 return a.start_address < b.start_address; |
386 }); | 428 }); |
387 uint64_t last_address = 0; | 429 uint64_t last_address = 0; |
388 for (const VMRegion& region : regions) { | 430 for (const VMRegion& region : regions) { |
389 EXPECT_GE(region.start_address, last_address); | 431 EXPECT_GE(region.start_address, last_address); |
390 last_address = region.start_address + region.size_in_bytes; | 432 last_address = region.start_address + region.size_in_bytes; |
391 } | 433 } |
392 } | 434 } |
393 | 435 |
394 #endif // defined(OS_MACOSX) | 436 #endif // defined(OS_MACOSX) |
395 | |
396 } // namespace tracing | 437 } // namespace tracing |
OLD | NEW |