| 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 "base/trace_event/process_memory_dump.h" | 5 #include "base/trace_event/process_memory_dump.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/shared_memory_tracker.h" | 13 #include "base/memory/shared_memory_tracker.h" |
| 13 #include "base/process/process_metrics.h" | 14 #include "base/process/process_metrics.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | 16 #include "base/trace_event/heap_profiler_event_writer.h" |
| 16 #include "base/trace_event/heap_profiler_serialization_state.h" | |
| 17 #include "base/trace_event/memory_infra_background_whitelist.h" | 17 #include "base/trace_event/memory_infra_background_whitelist.h" |
| 18 #include "base/trace_event/process_memory_totals.h" | 18 #include "base/trace_event/process_memory_totals.h" |
| 19 #include "base/trace_event/sharded_allocation_register.h" |
| 19 #include "base/trace_event/trace_event_argument.h" | 20 #include "base/trace_event/trace_event_argument.h" |
| 20 #include "base/unguessable_token.h" | 21 #include "base/unguessable_token.h" |
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 22 | 23 |
| 23 #if defined(OS_IOS) | 24 #if defined(OS_IOS) |
| 24 #include <mach/vm_page_size.h> | 25 #include <mach/vm_page_size.h> |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 #if defined(OS_POSIX) | 28 #if defined(OS_POSIX) |
| 28 #include <sys/mman.h> | 29 #include <sys/mman.h> |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 mad->set_flags(MemoryAllocatorDump::Flags::WEAK); | 247 mad->set_flags(MemoryAllocatorDump::Flags::WEAK); |
| 247 return mad; | 248 return mad; |
| 248 } | 249 } |
| 249 | 250 |
| 250 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( | 251 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( |
| 251 const MemoryAllocatorDumpGuid& guid) const { | 252 const MemoryAllocatorDumpGuid& guid) const { |
| 252 return GetAllocatorDump(GetSharedGlobalAllocatorDumpName(guid)); | 253 return GetAllocatorDump(GetSharedGlobalAllocatorDumpName(guid)); |
| 253 } | 254 } |
| 254 | 255 |
| 255 void ProcessMemoryDump::DumpHeapUsage( | 256 void ProcessMemoryDump::DumpHeapUsage( |
| 256 const std::unordered_map<base::trace_event::AllocationContext, | 257 const ShardedAllocationRegister& allocation_register, |
| 257 base::trace_event::AllocationMetrics>& | |
| 258 metrics_by_context, | |
| 259 base::trace_event::TraceEventMemoryOverhead& overhead, | |
| 260 const char* allocator_name) { | 258 const char* allocator_name) { |
| 261 if (!metrics_by_context.empty()) { | 259 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
| 262 // We shouldn't end up here unless we're doing a detailed dump with | 260 // We shouldn't end up here unless we're doing a detailed dump with |
| 263 // heap profiling enabled and if that is the case tracing should be | 261 // heap profiling enabled and if that is the case tracing should be |
| 264 // enabled which sets up the heap profiler serialization state. | 262 // enabled which sets up the heap profiler serialization state. |
| 265 if (!heap_profiler_serialization_state()) { | 263 if (!heap_profiler_serialization_state()) { |
| 266 NOTREACHED(); | 264 NOTREACHED(); |
| 267 return; | 265 return; |
| 268 } | 266 } |
| 269 DCHECK_EQ(0ul, heap_dumps_.count(allocator_name)); | 267 DCHECK_EQ(0ul, heap_dumps_.count(allocator_name)); |
| 270 std::unique_ptr<TracedValue> heap_dump = ExportHeapDump( | 268 std::unique_ptr<TracedValue> heap_dump = SerializeHeapDump( |
| 271 metrics_by_context, *heap_profiler_serialization_state()); | 269 allocation_register, heap_profiler_serialization_state_.get()); |
| 272 heap_dumps_[allocator_name] = std::move(heap_dump); | 270 heap_dumps_[allocator_name] = std::move(heap_dump); |
| 273 } | 271 } |
| 274 | 272 |
| 273 TraceEventMemoryOverhead overhead; |
| 274 allocation_register.EstimateTraceMemoryOverhead(&overhead); |
| 275 std::string base_name = base::StringPrintf("tracing/heap_profiler_%s", | 275 std::string base_name = base::StringPrintf("tracing/heap_profiler_%s", |
| 276 allocator_name); | 276 allocator_name); |
| 277 overhead.DumpInto(base_name.c_str(), this); | 277 overhead.DumpInto(base_name.c_str(), this); |
| 278 } | 278 } |
| 279 | 279 |
| 280 void ProcessMemoryDump::Clear() { | 280 void ProcessMemoryDump::Clear() { |
| 281 if (has_process_totals_) { | 281 if (has_process_totals_) { |
| 282 process_totals_.Clear(); | 282 process_totals_.Clear(); |
| 283 has_process_totals_ = false; | 283 has_process_totals_ = false; |
| 284 } | 284 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 } | 328 } |
| 329 | 329 |
| 330 if (allocator_dumps_.size() > 0) { | 330 if (allocator_dumps_.size() > 0) { |
| 331 value->BeginDictionary("allocators"); | 331 value->BeginDictionary("allocators"); |
| 332 for (const auto& allocator_dump_it : allocator_dumps_) | 332 for (const auto& allocator_dump_it : allocator_dumps_) |
| 333 allocator_dump_it.second->AsValueInto(value); | 333 allocator_dump_it.second->AsValueInto(value); |
| 334 value->EndDictionary(); | 334 value->EndDictionary(); |
| 335 } | 335 } |
| 336 | 336 |
| 337 if (heap_dumps_.size() > 0) { | 337 if (heap_dumps_.size() > 0) { |
| 338 value->BeginDictionary("heaps"); | 338 auto profile_data = SerializeHeapProfileEventData( |
| 339 for (const auto& name_and_dump : heap_dumps_) | 339 heap_dumps_, heap_profiler_serialization_state_.get()); |
| 340 value->SetValueWithCopiedName(name_and_dump.first, *name_and_dump.second); | 340 value->SetValue("heaps_v2", *profile_data); |
| 341 value->EndDictionary(); // "heaps" | |
| 342 } | 341 } |
| 343 | 342 |
| 344 value->BeginArray("allocators_graph"); | 343 value->BeginArray("allocators_graph"); |
| 345 for (const auto& it : allocator_dumps_edges_) { | 344 for (const auto& it : allocator_dumps_edges_) { |
| 346 const MemoryAllocatorDumpEdge& edge = it.second; | 345 const MemoryAllocatorDumpEdge& edge = it.second; |
| 347 value->BeginDictionary(); | 346 value->BeginDictionary(); |
| 348 value->SetString("source", edge.source.ToString()); | 347 value->SetString("source", edge.source.ToString()); |
| 349 value->SetString("target", edge.target.ToString()); | 348 value->SetString("target", edge.target.ToString()); |
| 350 value->SetInteger("importance", edge.importance); | 349 value->SetInteger("importance", edge.importance); |
| 351 value->SetString("type", edge.type); | 350 value->SetString("type", edge.type); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 | 459 |
| 461 MemoryAllocatorDump* ProcessMemoryDump::GetBlackHoleMad() { | 460 MemoryAllocatorDump* ProcessMemoryDump::GetBlackHoleMad() { |
| 462 DCHECK(is_black_hole_non_fatal_for_testing_); | 461 DCHECK(is_black_hole_non_fatal_for_testing_); |
| 463 if (!black_hole_mad_) | 462 if (!black_hole_mad_) |
| 464 black_hole_mad_.reset(new MemoryAllocatorDump("discarded", this)); | 463 black_hole_mad_.reset(new MemoryAllocatorDump("discarded", this)); |
| 465 return black_hole_mad_.get(); | 464 return black_hole_mad_.get(); |
| 466 } | 465 } |
| 467 | 466 |
| 468 } // namespace trace_event | 467 } // namespace trace_event |
| 469 } // namespace base | 468 } // namespace base |
| OLD | NEW |