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> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/process/process_metrics.h" | 18 #include "base/process/process_metrics.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/trace_event/memory_dump_manager.h" | 21 #include "base/trace_event/memory_dump_manager.h" |
| 22 #include "base/trace_event/process_memory_dump.h" | 22 #include "base/trace_event/process_memory_dump.h" |
| 23 #include "base/trace_event/process_memory_maps.h" | 23 #include "base/trace_event/process_memory_maps.h" |
| 24 #include "base/trace_event/process_memory_totals.h" | 24 #include "base/trace_event/process_memory_totals.h" |
| 25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 26 | 26 |
| 27 #if defined(OS_MACOSX) | |
| 28 #include <libproc.h> | |
| 29 #include <mach/mach.h> | |
| 30 #include <mach/mach_vm.h> | |
| 31 #include <sys/param.h> | |
| 32 #endif // defined(OS_MACOSX) | |
| 33 | |
| 27 namespace tracing { | 34 namespace tracing { |
| 28 | 35 |
| 29 namespace { | 36 namespace { |
| 30 | 37 |
| 31 base::LazyInstance< | 38 base::LazyInstance< |
| 32 std::map<base::ProcessId, | 39 std::map<base::ProcessId, |
| 33 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky | 40 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky |
| 34 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; | 41 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; |
| 35 | 42 |
| 36 #if defined(OS_LINUX) || defined(OS_ANDROID) | 43 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); | 219 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); |
| 213 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 220 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 214 } | 221 } |
| 215 | 222 |
| 216 if (res) | 223 if (res) |
| 217 pmd->set_has_process_mmaps(); | 224 pmd->set_has_process_mmaps(); |
| 218 return res; | 225 return res; |
| 219 } | 226 } |
| 220 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 227 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 221 | 228 |
| 229 #if defined(OS_MACOSX) | |
| 230 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( | |
| 231 const base::trace_event::MemoryDumpArgs& args, | |
| 232 base::trace_event::ProcessMemoryDump* pmd) { | |
| 233 const int pid = getpid(); | |
| 234 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; | |
| 235 using RegionInfo = vm_region_submap_info_64; | |
| 236 mach_vm_address_t address = 0; | |
| 237 mach_vm_size_t vmsize = 0; | |
| 238 natural_t depth = 0; | |
| 239 RegionInfo vminfo; | |
| 240 mach_msg_type_number_t count = sizeof(RegionInfo); | |
| 241 while (true) { | |
| 242 kern_return_t kr = mach_vm_region_recurse(mach_task_self(), &address, | |
| 243 &vmsize, &depth, reinterpret_cast<vm_region_info_t>(&vminfo), &count); | |
| 244 if (kr == KERN_INVALID_ADDRESS) // nothing else left | |
| 245 break; | |
| 246 if (kr != KERN_SUCCESS) // something bad | |
| 247 return false; | |
| 248 | |
| 249 if (vminfo.is_submap) { | |
| 250 ++depth; | |
| 251 continue; | |
| 252 } | |
| 253 | |
| 254 VMRegion region; | |
| 255 | |
| 256 // See vm_map_region_walk and vm_map_region_look_for_page in | |
| 257 // osfmk/vm/vm_map.c for more details. | |
| 258 if (vminfo.share_mode == SM_EMPTY) { | |
| 259 if (vminfo.share_mode == SM_LARGE_PAGE) | |
|
Mark Mentovai
2017/01/31 20:29:13
How can share_mode be both SM_EMPTY and SM_LARGE_P
erikchen
2017/01/31 20:54:10
whoops, left over debugging that wasn't even in th
| |
| 260 LOG(ERROR) << "large page"; | |
| 261 } else if (vminfo.share_mode == SM_COW) { | |
| 262 region.byte_stats_private_dirty_resident = | |
| 263 vminfo.pages_shared_now_private * PAGE_SIZE; | |
| 264 region.byte_stats_shared_clean_resident = | |
| 265 (vminfo.pages_resident - vminfo.pages_shared_now_private) * | |
| 266 PAGE_SIZE; | |
| 267 } else if (vminfo.share_mode == SM_PRIVATE || | |
| 268 vminfo.share_mode == SM_LARGE_PAGE) { | |
| 269 region.byte_stats_private_dirty_resident = | |
| 270 vminfo.pages_dirtied * PAGE_SIZE; | |
| 271 region.byte_stats_private_clean_resident = | |
| 272 (vminfo.pages_resident - vminfo.pages_dirtied) * PAGE_SIZE; | |
| 273 } else if (vminfo.share_mode == SM_SHARED || | |
| 274 vminfo.share_mode == SM_PRIVATE_ALIASED || | |
| 275 vminfo.share_mode == SM_TRUESHARED || | |
| 276 vminfo.share_mode == SM_SHARED_ALIASED) { | |
| 277 region.byte_stats_shared_dirty_resident = | |
| 278 vminfo.pages_dirtied * PAGE_SIZE; | |
| 279 region.byte_stats_shared_clean_resident = | |
| 280 (vminfo.pages_resident - vminfo.pages_dirtied) * PAGE_SIZE; | |
| 281 } else { | |
| 282 NOTREACHED(); | |
| 283 } | |
| 284 | |
| 285 if (vminfo.protection & VM_PROT_READ) | |
| 286 region.protection_flags |= VMRegion::kProtectionFlagsRead; | |
| 287 if (vminfo.protection & VM_PROT_WRITE) | |
| 288 region.protection_flags |= VMRegion::kProtectionFlagsWrite; | |
| 289 if (vminfo.protection & VM_PROT_EXECUTE) | |
| 290 region.protection_flags |= VMRegion::kProtectionFlagsExec; | |
| 291 | |
| 292 char buffer[MAXPATHLEN]; | |
| 293 int length = proc_regionfilename(pid, address, buffer, MAXPATHLEN); | |
| 294 if (length != 0) | |
| 295 region.mapped_file.assign(buffer, length); | |
| 296 | |
| 297 region.byte_stats_swapped = vminfo.pages_swapped_out * PAGE_SIZE; | |
| 298 region.start_address = address; | |
| 299 region.size_in_bytes = vmsize; | |
| 300 pmd->process_mmaps()->AddVMRegion(region); | |
| 301 | |
| 302 if (__builtin_add_overflow(address, vmsize, &address)) | |
| 303 break; | |
| 304 } | |
| 305 | |
| 306 pmd->set_has_process_mmaps(); | |
| 307 return true; | |
| 308 } | |
| 309 #endif // defined(OS_MACOSX) | |
| 310 | |
| 222 // static | 311 // static |
| 223 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( | 312 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( |
| 224 base::ProcessId process) { | 313 base::ProcessId process) { |
| 225 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( | 314 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( |
| 226 new ProcessMetricsMemoryDumpProvider(process)); | 315 new ProcessMetricsMemoryDumpProvider(process)); |
| 227 base::trace_event::MemoryDumpProvider::Options options; | 316 base::trace_event::MemoryDumpProvider::Options options; |
| 228 options.target_pid = process; | 317 options.target_pid = process; |
| 229 options.is_fast_polling_supported = true; | 318 options.is_fast_polling_supported = true; |
| 230 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 319 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 231 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); | 320 metrics_provider.get(), "ProcessMemoryMetrics", nullptr, options); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 260 | 349 |
| 261 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} | 350 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} |
| 262 | 351 |
| 263 // Called at trace dump point time. Creates a snapshot of the memory maps for | 352 // Called at trace dump point time. Creates a snapshot of the memory maps for |
| 264 // the current process. | 353 // the current process. |
| 265 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( | 354 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
| 266 const base::trace_event::MemoryDumpArgs& args, | 355 const base::trace_event::MemoryDumpArgs& args, |
| 267 base::trace_event::ProcessMemoryDump* pmd) { | 356 base::trace_event::ProcessMemoryDump* pmd) { |
| 268 bool res = DumpProcessTotals(args, pmd); | 357 bool res = DumpProcessTotals(args, pmd); |
| 269 | 358 |
| 270 #if defined(OS_LINUX) || defined(OS_ANDROID) | 359 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) |
| 271 if (args.level_of_detail == | 360 if (args.level_of_detail == |
| 272 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) | 361 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) |
| 273 res &= DumpProcessMemoryMaps(args, pmd); | 362 res &= DumpProcessMemoryMaps(args, pmd); |
| 274 #endif | 363 #endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) |
| 275 return res; | 364 return res; |
| 276 } | 365 } |
| 277 | 366 |
| 278 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( | 367 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( |
| 279 const base::trace_event::MemoryDumpArgs& args, | 368 const base::trace_event::MemoryDumpArgs& args, |
| 280 base::trace_event::ProcessMemoryDump* pmd) { | 369 base::trace_event::ProcessMemoryDump* pmd) { |
| 281 const uint64_t rss_bytes = rss_bytes_for_testing | 370 const uint64_t rss_bytes = rss_bytes_for_testing |
| 282 ? rss_bytes_for_testing | 371 ? rss_bytes_for_testing |
| 283 : process_metrics_->GetWorkingSetSize(); | 372 : process_metrics_->GetWorkingSetSize(); |
| 284 | 373 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 #endif | 456 #endif |
| 368 } | 457 } |
| 369 | 458 |
| 370 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { | 459 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { |
| 371 #if defined(OS_LINUX) || defined(OS_ANDROID) | 460 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 372 fast_polling_statm_fd_.reset(); | 461 fast_polling_statm_fd_.reset(); |
| 373 #endif | 462 #endif |
| 374 } | 463 } |
| 375 | 464 |
| 376 } // namespace tracing | 465 } // namespace tracing |
| OLD | NEW |