Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: components/tracing/common/process_metrics_memory_dump_provider.cc

Issue 2674973004: mac: Emit dyld info from the memory dump provider. (Closed)
Patch Set: Use initprot instead of maxprot. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/tracing/common/process_metrics_memory_dump_provider_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
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) 27 #if defined(OS_MACOSX)
28 #include <libproc.h> 28 #include <libproc.h>
29 #include <mach/mach.h> 29 #include <mach/mach.h>
30 #include <mach/mach_vm.h> 30 #include <mach/mach_vm.h>
31 #include <mach/shared_region.h>
31 #include <sys/param.h> 32 #include <sys/param.h>
32 33
34 #include <mach-o/dyld_images.h>
35 #include <mach-o/loader.h>
36 #include <mach/mach.h>
37
33 #include "base/numerics/safe_math.h" 38 #include "base/numerics/safe_math.h"
34 #endif // defined(OS_MACOSX) 39 #endif // defined(OS_MACOSX)
35 40
36 namespace tracing { 41 namespace tracing {
37 42
38 namespace { 43 namespace {
39 44
40 base::LazyInstance< 45 base::LazyInstance<
41 std::map<base::ProcessId, 46 std::map<base::ProcessId,
42 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky 47 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); 227 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps());
223 } 228 }
224 229
225 if (res) 230 if (res)
226 pmd->set_has_process_mmaps(); 231 pmd->set_has_process_mmaps();
227 return res; 232 return res;
228 } 233 }
229 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 234 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
230 235
231 #if defined(OS_MACOSX) 236 #if defined(OS_MACOSX)
232 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( 237
233 const base::trace_event::MemoryDumpArgs& args, 238 namespace {
234 base::trace_event::ProcessMemoryDump* pmd) { 239
240 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion;
241
242 bool IsAddressInSharedRegion(uint64_t address) {
243 return address >= SHARED_REGION_BASE_X86_64 &&
244 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
245 }
246
247 // Creates VMRegions for all dyld images. Returns whether the operation
248 // succeeded.
249 bool GetDyldRegions(std::vector<VMRegion>* regions) {
250 task_dyld_info_data_t dyld_info;
251 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
252 kern_return_t kr =
253 task_info(mach_task_self(), TASK_DYLD_INFO,
254 reinterpret_cast<task_info_t>(&dyld_info), &count);
255 if (kr != KERN_SUCCESS)
256 return false;
257
258 const struct dyld_all_image_infos* all_image_infos =
259 reinterpret_cast<const struct dyld_all_image_infos*>(
260 dyld_info.all_image_info_addr);
261
262 for (size_t i = 0; i < all_image_infos->infoArrayCount; i++) {
263 const char* image_name = all_image_infos->infoArray[i].imageFilePath;
264
265 // The public definition for dyld_all_image_infos/dyld_image_info is wrong
266 // for 64-bit platforms. We explicitly cast to struct mach_header_64 even
267 // though the public definition claims that this is a struct mach_header.
268 const struct mach_header_64* const header =
269 reinterpret_cast<const struct mach_header_64* const>(
270 all_image_infos->infoArray[i].imageLoadAddress);
271
272 uint64_t next_command = reinterpret_cast<uint64_t>(header + 1);
273 uint64_t command_end = next_command + header->sizeofcmds;
274 for (unsigned int i = 0; i < header->ncmds; ++i) {
275 // Ensure that next_command doesn't run past header->sizeofcmds.
276 if (next_command + sizeof(struct load_command) > command_end)
277 return false;
278 const struct load_command* load_cmd =
279 reinterpret_cast<const struct load_command*>(next_command);
280 next_command += load_cmd->cmdsize;
281
282 if (load_cmd->cmd == LC_SEGMENT_64) {
283 if (load_cmd->cmdsize < sizeof(segment_command_64))
284 return false;
285 const segment_command_64* seg =
286 reinterpret_cast<const segment_command_64*>(load_cmd);
287 if (strcmp(seg->segname, SEG_PAGEZERO) == 0)
288 continue;
289
290 uint32_t protection_flags = 0;
291 if (seg->initprot & VM_PROT_READ)
292 protection_flags |= VMRegion::kProtectionFlagsRead;
293 if (seg->initprot & VM_PROT_WRITE)
294 protection_flags |= VMRegion::kProtectionFlagsWrite;
295 if (seg->initprot & VM_PROT_EXECUTE)
296 protection_flags |= VMRegion::kProtectionFlagsExec;
297
298 VMRegion region;
299 region.size_in_bytes = seg->vmsize;
300 region.protection_flags = protection_flags;
301 region.mapped_file = image_name;
302 region.start_address =
303 reinterpret_cast<uint64_t>(header) + seg->fileoff;
304
305 // We intentionally avoid setting any page information, which is not
306 // available from dyld. The fields will be populated later.
307 regions->push_back(region);
308 }
309 }
310 }
311 return true;
312 }
313
314 void PopulateByteStats(VMRegion* region, const vm_region_submap_info_64& info) {
315 uint32_t share_mode = info.share_mode;
316 if (share_mode == SM_COW && info.ref_count == 1)
317 share_mode = SM_PRIVATE;
318
319 uint64_t dirty_bytes = info.pages_dirtied * PAGE_SIZE;
320 uint64_t clean_bytes =
321 (info.pages_resident - info.pages_reusable - info.pages_dirtied) *
322 PAGE_SIZE;
323 switch (share_mode) {
324 case SM_LARGE_PAGE:
325 case SM_PRIVATE:
326 region->byte_stats_private_dirty_resident = dirty_bytes;
327 region->byte_stats_private_clean_resident = clean_bytes;
328 break;
329 case SM_COW:
330 region->byte_stats_private_dirty_resident = dirty_bytes;
331 region->byte_stats_shared_clean_resident = clean_bytes;
332 break;
333 case SM_SHARED:
334 case SM_PRIVATE_ALIASED:
335 case SM_TRUESHARED:
336 case SM_SHARED_ALIASED:
337 region->byte_stats_shared_dirty_resident = dirty_bytes;
338 region->byte_stats_shared_clean_resident = clean_bytes;
339 break;
340 case SM_EMPTY:
341 break;
342 default:
343 NOTREACHED();
344 break;
345 }
346 }
347
348 // Creates VMRegions from mach_vm_region_recurse. Returns whether the operation
349 // succeeded.
350 bool GetAllRegions(std::vector<VMRegion>* regions) {
235 const int pid = getpid(); 351 const int pid = getpid();
236 task_t task = mach_task_self(); 352 task_t task = mach_task_self();
237 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion;
238 mach_vm_size_t size = 0; 353 mach_vm_size_t size = 0;
239 vm_region_submap_info_64 info; 354 vm_region_submap_info_64 info;
240 natural_t depth = 1; 355 natural_t depth = 1;
241 mach_msg_type_number_t count = sizeof(info); 356 mach_msg_type_number_t count = sizeof(info);
242 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { 357 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
243 memset(&info, 0, sizeof(info)); 358 memset(&info, 0, sizeof(info));
244 kern_return_t kr = mach_vm_region_recurse( 359 kern_return_t kr = mach_vm_region_recurse(
245 task, &address, &size, &depth, 360 task, &address, &size, &depth,
246 reinterpret_cast<vm_region_info_t>(&info), &count); 361 reinterpret_cast<vm_region_info_t>(&info), &count);
247 if (kr == KERN_INVALID_ADDRESS) // nothing else left 362 if (kr == KERN_INVALID_ADDRESS) // nothing else left
248 break; 363 break;
249 if (kr != KERN_SUCCESS) // something bad 364 if (kr != KERN_SUCCESS) // something bad
250 return false; 365 return false;
251 if (info.is_submap) { 366 if (info.is_submap) {
252 size = 0; 367 size = 0;
253 ++depth; 368 ++depth;
254 continue; 369 continue;
255 } 370 }
256 371
257 if (info.share_mode == SM_COW && info.ref_count == 1)
258 info.share_mode = SM_PRIVATE;
259
260 VMRegion region; 372 VMRegion region;
261 uint64_t dirty_bytes = info.pages_dirtied * PAGE_SIZE; 373 PopulateByteStats(&region, info);
262 uint64_t clean_bytes =
263 (info.pages_resident - info.pages_reusable - info.pages_dirtied) *
264 PAGE_SIZE;
265 switch (info.share_mode) {
266 case SM_LARGE_PAGE:
267 case SM_PRIVATE:
268 region.byte_stats_private_dirty_resident = dirty_bytes;
269 region.byte_stats_private_clean_resident = clean_bytes;
270 break;
271 case SM_COW:
272 region.byte_stats_private_dirty_resident = dirty_bytes;
273 region.byte_stats_shared_clean_resident = clean_bytes;
274 break;
275 case SM_SHARED:
276 case SM_PRIVATE_ALIASED:
277 case SM_TRUESHARED:
278 case SM_SHARED_ALIASED:
279 region.byte_stats_shared_dirty_resident = dirty_bytes;
280 region.byte_stats_shared_clean_resident = clean_bytes;
281 break;
282 case SM_EMPTY:
283 break;
284 default:
285 NOTREACHED();
286 break;
287 }
288 374
289 if (info.protection & VM_PROT_READ) 375 if (info.protection & VM_PROT_READ)
290 region.protection_flags |= VMRegion::kProtectionFlagsRead; 376 region.protection_flags |= VMRegion::kProtectionFlagsRead;
291 if (info.protection & VM_PROT_WRITE) 377 if (info.protection & VM_PROT_WRITE)
292 region.protection_flags |= VMRegion::kProtectionFlagsWrite; 378 region.protection_flags |= VMRegion::kProtectionFlagsWrite;
293 if (info.protection & VM_PROT_EXECUTE) 379 if (info.protection & VM_PROT_EXECUTE)
294 region.protection_flags |= VMRegion::kProtectionFlagsExec; 380 region.protection_flags |= VMRegion::kProtectionFlagsExec;
295 381
296 char buffer[MAXPATHLEN]; 382 char buffer[MAXPATHLEN];
297 int length = proc_regionfilename(pid, address, buffer, MAXPATHLEN); 383 int length = proc_regionfilename(pid, address, buffer, MAXPATHLEN);
298 if (length != 0) 384 if (length != 0)
299 region.mapped_file.assign(buffer, length); 385 region.mapped_file.assign(buffer, length);
300 386
301 region.byte_stats_swapped = info.pages_swapped_out * PAGE_SIZE; 387 region.byte_stats_swapped = info.pages_swapped_out * PAGE_SIZE;
302 region.start_address = address; 388 region.start_address = address;
303 region.size_in_bytes = size; 389 region.size_in_bytes = size;
304 pmd->process_mmaps()->AddVMRegion(region); 390 regions->push_back(region);
305 391
306 base::CheckedNumeric<mach_vm_address_t> numeric(address); 392 base::CheckedNumeric<mach_vm_address_t> numeric(address);
307 numeric += size; 393 numeric += size;
308 if (!numeric.IsValid()) 394 if (!numeric.IsValid())
309 break; 395 return false;
310 address = numeric.ValueOrDie(); 396 address = numeric.ValueOrDie();
311 } 397 }
398 return true;
399 }
400
401 void CopyRegionByteStats(VMRegion* dest, const VMRegion& source) {
402 dest->byte_stats_private_dirty_resident =
403 source.byte_stats_private_dirty_resident;
404 dest->byte_stats_private_clean_resident =
405 source.byte_stats_private_clean_resident;
406 dest->byte_stats_shared_dirty_resident =
407 source.byte_stats_shared_dirty_resident;
408 dest->byte_stats_shared_clean_resident =
409 source.byte_stats_shared_clean_resident;
410 dest->byte_stats_swapped = source.byte_stats_swapped;
411 dest->byte_stats_proportional_resident =
412 source.byte_stats_proportional_resident;
413 }
414
415 } // namespace
416
417 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps(
418 const base::trace_event::MemoryDumpArgs& args,
419 base::trace_event::ProcessMemoryDump* pmd) {
420 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion;
421
422 std::vector<VMRegion> dyld_regions;
423 if (!GetDyldRegions(&dyld_regions))
424 return false;
425 std::vector<VMRegion> all_regions;
426 if (!GetAllRegions(&all_regions))
427 return false;
428
429 // Cache information from dyld_regions in a data-structure more conducive to
430 // fast lookups.
431 std::unordered_map<uint64_t, VMRegion*> address_to_vm_region;
432 std::vector<uint64_t> addresses_in_shared_region;
433 for (VMRegion& region : dyld_regions) {
434 if (IsAddressInSharedRegion(region.start_address))
435 addresses_in_shared_region.push_back(region.start_address);
436 address_to_vm_region[region.start_address] = &region;
437 }
438
439 // Merge information from dyld regions and all regions.
440 for (const VMRegion& region : all_regions) {
441 // Check to see if the region already has a VMRegion created from a dyld
442 // load command. If so, copy the byte stats and move on.
443 auto it = address_to_vm_region.find(region.start_address);
444 if (it != address_to_vm_region.end() &&
445 it->second->size_in_bytes == region.size_in_bytes) {
446 CopyRegionByteStats(it->second, region);
447 continue;
448 }
449
450 // Check to see if the region is likely used for the dyld shared cache.
451 if (IsAddressInSharedRegion(region.start_address)) {
452 uint64_t end_address = region.start_address + region.size_in_bytes;
453 for (uint64_t address : addresses_in_shared_region) {
454 // This region is likely used for the dyld shared cache. Don't record
455 // any byte stats since:
456 // 1. It's not possible to figure out which dyld regions the byte
457 // stats correspond to.
458 // 2. The region is likely shared by non-Chrome processes, so there's
459 // no point in charging the pages towards Chrome.
460 if (address >= region.start_address && address < end_address) {
461 continue;
462 }
463 }
464 }
465 pmd->process_mmaps()->AddVMRegion(region);
466 }
467
468 for (VMRegion& region : dyld_regions) {
469 pmd->process_mmaps()->AddVMRegion(region);
470 }
312 471
313 pmd->set_has_process_mmaps(); 472 pmd->set_has_process_mmaps();
314 return true; 473 return true;
315 } 474 }
316 #endif // defined(OS_MACOSX) 475 #endif // defined(OS_MACOSX)
317 476
318 // static 477 // static
319 void ProcessMetricsMemoryDumpProvider::RegisterForProcess( 478 void ProcessMetricsMemoryDumpProvider::RegisterForProcess(
320 base::ProcessId process) { 479 base::ProcessId process) {
321 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider( 480 std::unique_ptr<ProcessMetricsMemoryDumpProvider> metrics_provider(
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 #endif 622 #endif
464 } 623 }
465 624
466 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { 625 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() {
467 #if defined(OS_LINUX) || defined(OS_ANDROID) 626 #if defined(OS_LINUX) || defined(OS_ANDROID)
468 fast_polling_statm_fd_.reset(); 627 fast_polling_statm_fd_.reset();
469 #endif 628 #endif
470 } 629 }
471 630
472 } // namespace tracing 631 } // namespace tracing
OLDNEW
« no previous file with comments | « no previous file | components/tracing/common/process_metrics_memory_dump_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698