| 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 <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include <mach/shared_region.h> | 31 #include <mach/shared_region.h> |
| 32 #include <sys/param.h> | 32 #include <sys/param.h> |
| 33 | 33 |
| 34 #include <mach-o/dyld_images.h> | 34 #include <mach-o/dyld_images.h> |
| 35 #include <mach-o/loader.h> | 35 #include <mach-o/loader.h> |
| 36 #include <mach/mach.h> | 36 #include <mach/mach.h> |
| 37 | 37 |
| 38 #include "base/numerics/safe_math.h" | 38 #include "base/numerics/safe_math.h" |
| 39 #endif // defined(OS_MACOSX) | 39 #endif // defined(OS_MACOSX) |
| 40 | 40 |
| 41 #if defined(OS_WIN) |
| 42 #include <psapi.h> |
| 43 #include <tchar.h> |
| 44 #include <windows.h> |
| 45 |
| 46 #include <base/strings/sys_string_conversions.h> |
| 47 #include <base/win/win_util.h> |
| 48 #endif // defined(OS_WIN) |
| 49 |
| 41 namespace tracing { | 50 namespace tracing { |
| 42 | 51 |
| 43 namespace { | 52 namespace { |
| 44 | 53 |
| 45 base::LazyInstance< | 54 base::LazyInstance< |
| 46 std::map<base::ProcessId, | 55 std::map<base::ProcessId, |
| 47 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky | 56 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky |
| 48 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; | 57 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; |
| 49 | 58 |
| 50 #if defined(OS_LINUX) || defined(OS_ANDROID) | 59 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); | 235 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); |
| 227 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 236 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 228 } | 237 } |
| 229 | 238 |
| 230 if (res) | 239 if (res) |
| 231 pmd->set_has_process_mmaps(); | 240 pmd->set_has_process_mmaps(); |
| 232 return res; | 241 return res; |
| 233 } | 242 } |
| 234 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 243 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 235 | 244 |
| 245 #if defined(OS_WIN) |
| 246 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( |
| 247 const base::trace_event::MemoryDumpArgs& args, |
| 248 base::trace_event::ProcessMemoryDump* pmd) { |
| 249 std::vector<HMODULE> modules; |
| 250 if (!base::win::GetLoadedModulesSnapshot(::GetCurrentProcess(), &modules)) |
| 251 return false; |
| 252 |
| 253 // Query the base address for each module, and attach it to the dump. |
| 254 for (size_t i = 0; i < modules.size(); ++i) { |
| 255 wchar_t module_name[MAX_PATH]; |
| 256 if (!::GetModuleFileName(modules[i], module_name, MAX_PATH)) |
| 257 continue; |
| 258 |
| 259 MODULEINFO module_info; |
| 260 if (!::GetModuleInformation(::GetCurrentProcess(), modules[i], |
| 261 &module_info, sizeof(MODULEINFO))) { |
| 262 continue; |
| 263 } |
| 264 base::trace_event::ProcessMemoryMaps::VMRegion region; |
| 265 region.size_in_bytes = module_info.SizeOfImage; |
| 266 region.mapped_file = base::SysWideToNativeMB(module_name); |
| 267 region.start_address = reinterpret_cast<uint64_t>(module_info.lpBaseOfDll); |
| 268 pmd->process_mmaps()->AddVMRegion(region); |
| 269 } |
| 270 if (!pmd->process_mmaps()->vm_regions().empty()) |
| 271 pmd->set_has_process_mmaps(); |
| 272 return true; |
| 273 } |
| 274 #endif // defined(OS_WIN) |
| 275 |
| 236 #if defined(OS_MACOSX) | 276 #if defined(OS_MACOSX) |
| 237 | 277 |
| 238 namespace { | 278 namespace { |
| 239 | 279 |
| 240 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; | 280 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; |
| 241 | 281 |
| 242 bool IsAddressInSharedRegion(uint64_t address) { | 282 bool IsAddressInSharedRegion(uint64_t address) { |
| 243 return address >= SHARED_REGION_BASE_X86_64 && | 283 return address >= SHARED_REGION_BASE_X86_64 && |
| 244 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 284 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
| 245 } | 285 } |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 | 580 |
| 541 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} | 581 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} |
| 542 | 582 |
| 543 // Called at trace dump point time. Creates a snapshot of the memory maps for | 583 // Called at trace dump point time. Creates a snapshot of the memory maps for |
| 544 // the current process. | 584 // the current process. |
| 545 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( | 585 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
| 546 const base::trace_event::MemoryDumpArgs& args, | 586 const base::trace_event::MemoryDumpArgs& args, |
| 547 base::trace_event::ProcessMemoryDump* pmd) { | 587 base::trace_event::ProcessMemoryDump* pmd) { |
| 548 bool res = DumpProcessTotals(args, pmd); | 588 bool res = DumpProcessTotals(args, pmd); |
| 549 | 589 |
| 550 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) | |
| 551 if (args.level_of_detail == | 590 if (args.level_of_detail == |
| 552 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) | 591 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) |
| 553 res &= DumpProcessMemoryMaps(args, pmd); | 592 res &= DumpProcessMemoryMaps(args, pmd); |
| 554 #endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) | |
| 555 return res; | 593 return res; |
| 556 } | 594 } |
| 557 | 595 |
| 558 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( | 596 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
| 559 const base::trace_event::MemoryDumpArgs& args, | 597 const base::trace_event::MemoryDumpArgs& args, |
| 560 base::trace_event::ProcessMemoryDump* pmd) { | 598 base::trace_event::ProcessMemoryDump* pmd) { |
| 561 const uint64_t rss_bytes = rss_bytes_for_testing | 599 const uint64_t rss_bytes = rss_bytes_for_testing |
| 562 ? rss_bytes_for_testing | 600 ? rss_bytes_for_testing |
| 563 : process_metrics_->GetWorkingSetSize(); | 601 : process_metrics_->GetWorkingSetSize(); |
| 564 | 602 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 #endif | 685 #endif |
| 648 } | 686 } |
| 649 | 687 |
| 650 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { | 688 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { |
| 651 #if defined(OS_LINUX) || defined(OS_ANDROID) | 689 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 652 fast_polling_statm_fd_.reset(); | 690 fast_polling_statm_fd_.reset(); |
| 653 #endif | 691 #endif |
| 654 } | 692 } |
| 655 | 693 |
| 656 } // namespace tracing | 694 } // namespace tracing |
| OLD | NEW |