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 | 10 |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/process/process_metrics.h" | 13 #include "base/process/process_metrics.h" |
14 #include "base/trace_event/process_memory_dump.h" | 14 #include "base/trace_event/process_memory_dump.h" |
15 #include "base/trace_event/process_memory_maps.h" | 15 #include "base/trace_event/process_memory_maps.h" |
16 #include "base/trace_event/process_memory_totals.h" | 16 #include "base/trace_event/process_memory_totals.h" |
17 #include "base/trace_event/trace_event_argument.h" | 17 #include "base/trace_event/trace_event_argument.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
21 #include <libgen.h> | 21 #include <libgen.h> |
22 #include <mach-o/dyld.h> | 22 #include <mach-o/dyld.h> |
23 #endif | 23 #endif |
24 | 24 |
| 25 #if defined(OS_WIN) |
| 26 #include <base/strings/sys_string_conversions.h> |
| 27 #endif |
| 28 |
25 namespace tracing { | 29 namespace tracing { |
26 | 30 |
27 #if defined(OS_LINUX) || defined(OS_ANDROID) | 31 #if defined(OS_LINUX) || defined(OS_ANDROID) |
28 namespace { | 32 namespace { |
29 const char kTestSmaps1[] = | 33 const char kTestSmaps1[] = |
30 "00400000-004be000 r-xp 00000000 fc:01 1234 /file/1\n" | 34 "00400000-004be000 r-xp 00000000 fc:01 1234 /file/1\n" |
31 "Size: 760 kB\n" | 35 "Size: 760 kB\n" |
32 "Rss: 296 kB\n" | 36 "Rss: 296 kB\n" |
33 "Pss: 162 kB\n" | 37 "Pss: 162 kB\n" |
34 "Shared_Clean: 228 kB\n" | 38 "Shared_Clean: 228 kB\n" |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 mdp.PollFastMemoryTotal(&value); | 271 mdp.PollFastMemoryTotal(&value); |
268 EXPECT_EQ(100 * page_size, value); | 272 EXPECT_EQ(100 * page_size, value); |
269 | 273 |
270 mdp.SuspendFastMemoryPolling(); | 274 mdp.SuspendFastMemoryPolling(); |
271 EXPECT_FALSE(mdp.fast_polling_statm_fd_.is_valid()); | 275 EXPECT_FALSE(mdp.fast_polling_statm_fd_.is_valid()); |
272 #else | 276 #else |
273 mdp.SuspendFastMemoryPolling(); | 277 mdp.SuspendFastMemoryPolling(); |
274 #endif | 278 #endif |
275 } | 279 } |
276 | 280 |
| 281 #if defined(OS_WIN) |
| 282 |
| 283 void DummyFunction() {} |
| 284 |
| 285 TEST(ProcessMetricsMemoryDumpProviderTest, TestWinModuleReading) { |
| 286 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; |
| 287 |
| 288 ProcessMetricsMemoryDumpProvider mdp(base::kNullProcessId); |
| 289 base::trace_event::MemoryDumpArgs args; |
| 290 base::trace_event::ProcessMemoryDump dump(nullptr, args); |
| 291 ASSERT_TRUE(mdp.DumpProcessMemoryMaps(args, &dump)); |
| 292 ASSERT_TRUE(dump.has_process_mmaps()); |
| 293 |
| 294 wchar_t module_name[MAX_PATH]; |
| 295 DWORD result = GetModuleFileName(nullptr, module_name, MAX_PATH); |
| 296 ASSERT_TRUE(result); |
| 297 std::string executable_name = base::SysWideToNativeMB(module_name); |
| 298 |
| 299 HMODULE module_containing_dummy = nullptr; |
| 300 uintptr_t dummy_function_address = |
| 301 reinterpret_cast<uintptr_t>(&DummyFunction); |
| 302 result = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
| 303 reinterpret_cast<LPCWSTR>(dummy_function_address), |
| 304 &module_containing_dummy); |
| 305 ASSERT_TRUE(result); |
| 306 result = GetModuleFileName(nullptr, module_name, MAX_PATH); |
| 307 ASSERT_TRUE(result); |
| 308 std::string module_containing_dummy_name = |
| 309 base::SysWideToNativeMB(module_name); |
| 310 |
| 311 bool found_executable = false; |
| 312 bool found_region_with_dummy = false; |
| 313 for (const VMRegion& region : dump.process_mmaps()->vm_regions()) { |
| 314 EXPECT_NE(0u, region.start_address); |
| 315 EXPECT_NE(0u, region.size_in_bytes); |
| 316 |
| 317 if (region.mapped_file.find(executable_name) != std::string::npos) |
| 318 found_executable = true; |
| 319 |
| 320 if (dummy_function_address >= region.start_address && |
| 321 dummy_function_address < region.start_address + region.size_in_bytes) { |
| 322 found_region_with_dummy = true; |
| 323 EXPECT_EQ(module_containing_dummy_name, region.mapped_file); |
| 324 } |
| 325 } |
| 326 EXPECT_TRUE(found_executable); |
| 327 EXPECT_TRUE(found_region_with_dummy); |
| 328 } |
| 329 #endif |
| 330 |
277 #if defined(OS_MACOSX) | 331 #if defined(OS_MACOSX) |
278 TEST(ProcessMetricsMemoryDumpProviderTest, TestMachOReading) { | 332 TEST(ProcessMetricsMemoryDumpProviderTest, TestMachOReading) { |
279 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; | 333 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; |
280 ProcessMetricsMemoryDumpProvider mdp(base::kNullProcessId); | 334 ProcessMetricsMemoryDumpProvider mdp(base::kNullProcessId); |
281 base::trace_event::MemoryDumpArgs args; | 335 base::trace_event::MemoryDumpArgs args; |
282 base::trace_event::ProcessMemoryDump dump(nullptr, args); | 336 base::trace_event::ProcessMemoryDump dump(nullptr, args); |
283 ASSERT_TRUE(mdp.DumpProcessMemoryMaps(args, &dump)); | 337 ASSERT_TRUE(mdp.DumpProcessMemoryMaps(args, &dump)); |
284 ASSERT_TRUE(dump.has_process_mmaps()); | 338 ASSERT_TRUE(dump.has_process_mmaps()); |
285 uint32_t size = 100; | 339 uint32_t size = 100; |
286 char full_path[size]; | 340 char full_path[size]; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 uint64_t last_address = 0; | 387 uint64_t last_address = 0; |
334 for (const VMRegion& region : regions) { | 388 for (const VMRegion& region : regions) { |
335 EXPECT_GE(region.start_address, last_address); | 389 EXPECT_GE(region.start_address, last_address); |
336 last_address = region.start_address + region.size_in_bytes; | 390 last_address = region.start_address + region.size_in_bytes; |
337 } | 391 } |
338 } | 392 } |
339 | 393 |
340 #endif // defined(OS_MACOSX) | 394 #endif // defined(OS_MACOSX) |
341 | 395 |
342 } // namespace tracing | 396 } // namespace tracing |
OLD | NEW |