Chromium Code Reviews| 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 #endif | |
| 48 | |
| 41 namespace tracing { | 49 namespace tracing { |
| 42 | 50 |
| 43 namespace { | 51 namespace { |
| 44 | 52 |
| 45 base::LazyInstance< | 53 base::LazyInstance< |
| 46 std::map<base::ProcessId, | 54 std::map<base::ProcessId, |
| 47 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky | 55 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky |
| 48 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; | 56 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; |
| 49 | 57 |
| 50 #if defined(OS_LINUX) || defined(OS_ANDROID) | 58 #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")); | 234 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); |
| 227 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 235 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 228 } | 236 } |
| 229 | 237 |
| 230 if (res) | 238 if (res) |
| 231 pmd->set_has_process_mmaps(); | 239 pmd->set_has_process_mmaps(); |
| 232 return res; | 240 return res; |
| 233 } | 241 } |
| 234 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 242 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 235 | 243 |
| 244 #if defined(OS_WIN) | |
| 245 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( | |
| 246 const base::trace_event::MemoryDumpArgs& args, | |
| 247 base::trace_event::ProcessMemoryDump* pmd) { | |
| 248 DWORD module_count = 1024; | |
|
Wez
2017/02/23 04:15:11
I would suggest including a brief comment to intro
erikchen
2017/02/23 21:00:14
Done.
awong
2017/02/23 22:05:44
Also, please comment on why 1024 makes sense as a
| |
| 249 std::vector<HMODULE> modules; | |
| 250 while (true) { | |
| 251 modules.resize(module_count); | |
| 252 DWORD output_size; | |
|
Wez
2017/02/23 04:15:11
nit: Even though you're about to overwrite it, ple
erikchen
2017/02/23 21:00:14
Done.
| |
| 253 BOOL result = ::EnumProcessModules(::GetCurrentProcess(), modules.data(), | |
| 254 module_count, &output_size); | |
|
Wez
2017/02/23 04:15:11
You're passing |module_count| here, where the API
erikchen
2017/02/23 21:00:14
fascinating, fixed. Also added a comment + buffer.
| |
| 255 if (!result) | |
| 256 return false; | |
| 257 DWORD actual_module_count = output_size / sizeof(HMODULE); | |
| 258 if (actual_module_count > module_count) { | |
| 259 module_count = actual_module_count; | |
| 260 continue; | |
| 261 } | |
| 262 module_count = actual_module_count; | |
| 263 break; | |
|
Wez
2017/02/23 04:15:11
Can we re-arrange this to read as:
while (true) {
erikchen
2017/02/23 21:00:14
Done.
| |
| 264 } | |
| 265 for (unsigned int i = 0; i < module_count; ++i) { | |
|
Wez
2017/02/23 04:15:11
nit: Since |module_count|'s range is defined by DW
erikchen
2017/02/23 21:00:14
Done.
| |
| 266 TCHAR module_name[MAX_PATH]; | |
|
Wez
2017/02/23 04:15:10
wchar_t
erikchen
2017/02/23 21:00:14
Done.
| |
| 267 BOOL result = GetModuleFileName(modules[i], module_name, MAX_PATH); | |
|
Wez
2017/02/23 04:15:10
nit: You used :: prefix for EnumProcessModules(),
erikchen
2017/02/23 21:00:14
Switched to :: prefixes. The filename is necessary
Wez
2017/02/23 23:35:03
Ah OK; I wondered if it might be important to get
| |
| 268 if (!result) | |
| 269 return false; | |
|
Wez
2017/02/23 04:15:11
This means failing to resolve any module's filenam
erikchen
2017/02/23 21:00:14
Done.
| |
| 270 | |
| 271 MODULEINFO module_info; | |
| 272 result = GetModuleInformation(::GetCurrentProcess(), modules[i], | |
| 273 &module_info, sizeof(MODULEINFO)); | |
| 274 if (!result) | |
| 275 return false; | |
|
Wez
2017/02/23 04:15:11
As above, consider continue'ing here rather than f
erikchen
2017/02/23 21:00:14
Done.
| |
| 276 base::trace_event::ProcessMemoryMaps::VMRegion region; | |
| 277 region.size_in_bytes = module_info.SizeOfImage; | |
| 278 region.mapped_file = base::SysWideToNativeMB(module_name); | |
| 279 region.start_address = reinterpret_cast<uint64_t>(module_info.lpBaseOfDll); | |
| 280 pmd->process_mmaps()->AddVMRegion(region); | |
| 281 } | |
| 282 | |
| 283 pmd->set_has_process_mmaps(); | |
|
Wez
2017/02/23 04:15:11
If you follow my suggestion to use continue's then
erikchen
2017/02/23 21:00:14
Done
| |
| 284 return true; | |
| 285 } | |
| 286 #endif // defined(OS_WIN) | |
| 287 | |
| 236 #if defined(OS_MACOSX) | 288 #if defined(OS_MACOSX) |
| 237 | 289 |
| 238 namespace { | 290 namespace { |
| 239 | 291 |
| 240 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; | 292 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; |
| 241 | 293 |
| 242 bool IsAddressInSharedRegion(uint64_t address) { | 294 bool IsAddressInSharedRegion(uint64_t address) { |
| 243 return address >= SHARED_REGION_BASE_X86_64 && | 295 return address >= SHARED_REGION_BASE_X86_64 && |
| 244 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 296 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
| 245 } | 297 } |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 540 | 592 |
| 541 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} | 593 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} |
| 542 | 594 |
| 543 // Called at trace dump point time. Creates a snapshot of the memory maps for | 595 // Called at trace dump point time. Creates a snapshot of the memory maps for |
| 544 // the current process. | 596 // the current process. |
| 545 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( | 597 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
| 546 const base::trace_event::MemoryDumpArgs& args, | 598 const base::trace_event::MemoryDumpArgs& args, |
| 547 base::trace_event::ProcessMemoryDump* pmd) { | 599 base::trace_event::ProcessMemoryDump* pmd) { |
| 548 bool res = DumpProcessTotals(args, pmd); | 600 bool res = DumpProcessTotals(args, pmd); |
| 549 | 601 |
| 550 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) | 602 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) || \ |
| 603 defined(OS_WIN) | |
|
Wez
2017/02/23 04:15:11
nit: Are there any other platforms? Or is this no
erikchen
2017/02/23 21:00:14
Nope, fixed.
| |
| 551 if (args.level_of_detail == | 604 if (args.level_of_detail == |
| 552 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) | 605 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) |
| 553 res &= DumpProcessMemoryMaps(args, pmd); | 606 res &= DumpProcessMemoryMaps(args, pmd); |
| 554 #endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) | 607 #endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) || |
| 608 // defined(OS_WIN) | |
| 555 return res; | 609 return res; |
| 556 } | 610 } |
| 557 | 611 |
| 558 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( | 612 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
| 559 const base::trace_event::MemoryDumpArgs& args, | 613 const base::trace_event::MemoryDumpArgs& args, |
| 560 base::trace_event::ProcessMemoryDump* pmd) { | 614 base::trace_event::ProcessMemoryDump* pmd) { |
| 561 const uint64_t rss_bytes = rss_bytes_for_testing | 615 const uint64_t rss_bytes = rss_bytes_for_testing |
| 562 ? rss_bytes_for_testing | 616 ? rss_bytes_for_testing |
| 563 : process_metrics_->GetWorkingSetSize(); | 617 : process_metrics_->GetWorkingSetSize(); |
| 564 | 618 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 #endif | 701 #endif |
| 648 } | 702 } |
| 649 | 703 |
| 650 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { | 704 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { |
| 651 #if defined(OS_LINUX) || defined(OS_ANDROID) | 705 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 652 fast_polling_statm_fd_.reset(); | 706 fast_polling_statm_fd_.reset(); |
| 653 #endif | 707 #endif |
| 654 } | 708 } |
| 655 | 709 |
| 656 } // namespace tracing | 710 } // namespace tracing |
| OLD | NEW |