| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/heap_profiler_event_writer.h" | 5 #include "base/trace_event/heap_profiler_event_writer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 15 #include "base/trace_event/heap_profiler_serialization_state.h" | 15 #include "base/trace_event/heap_profiler_serialization_state.h" |
| 16 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" | 16 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" |
| 17 #include "base/trace_event/heap_profiler_string_deduplicator.h" | 17 #include "base/trace_event/heap_profiler_string_deduplicator.h" |
| 18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" | 18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" |
| 19 #include "base/trace_event/sharded_allocation_register.h" | 19 #include "base/trace_event/sharded_allocation_register.h" |
| 20 #include "base/trace_event/trace_event.h" |
| 20 #include "base/trace_event/trace_event_argument.h" | 21 #include "base/trace_event/trace_event_argument.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 namespace trace_event { | 24 namespace trace_event { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 struct AggregationKey { | 28 struct AggregationKey { |
| 28 int backtrace_id; | 29 int backtrace_id; |
| 29 int type_id; | 30 int type_id; |
| 30 | 31 |
| 31 struct Hasher { | 32 struct Hasher { |
| 32 size_t operator()(const AggregationKey& key) const { | 33 size_t operator()(const AggregationKey& key) const { |
| 33 return base::HashInts(key.backtrace_id, key.type_id); | 34 return base::HashInts(key.backtrace_id, key.type_id); |
| 34 } | 35 } |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 bool operator==(const AggregationKey& other) const { | 38 bool operator==(const AggregationKey& other) const { |
| 38 return backtrace_id == other.backtrace_id && type_id == other.type_id; | 39 return backtrace_id == other.backtrace_id && type_id == other.type_id; |
| 39 } | 40 } |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 std::unique_ptr<TracedValue> SerializeHeapDump( | 45 std::unique_ptr<TracedValue> SerializeHeapDump( |
| 45 const ShardedAllocationRegister& allocation_register, | 46 const ShardedAllocationRegister& allocation_register, |
| 46 HeapProfilerSerializationState* serialization_state) { | 47 HeapProfilerSerializationState* serialization_state) { |
| 48 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("memory-infra"), "SerializeHeapDump"); |
| 47 // Aggregate allocations by {backtrace_id, type_id} key. | 49 // Aggregate allocations by {backtrace_id, type_id} key. |
| 48 using MetricsMap = std::unordered_map<AggregationKey, AllocationMetrics, | 50 using MetricsMap = std::unordered_map<AggregationKey, AllocationMetrics, |
| 49 AggregationKey::Hasher>; | 51 AggregationKey::Hasher>; |
| 50 MetricsMap metrics_by_key; | 52 MetricsMap metrics_by_key; |
| 51 | 53 |
| 52 auto visit_allocation = | 54 auto visit_allocation = |
| 53 [](HeapProfilerSerializationState* serialization_state, | 55 [](HeapProfilerSerializationState* serialization_state, |
| 54 MetricsMap* metrics_by_key, | 56 MetricsMap* metrics_by_key, |
| 55 const AllocationRegister::Allocation& allocation) { | 57 const AllocationRegister::Allocation& allocation) { |
| 56 int backtrace_id = | 58 int backtrace_id = |
| 57 serialization_state->stack_frame_deduplicator()->Insert( | 59 serialization_state->stack_frame_deduplicator()->Insert( |
| 58 std::begin(allocation.context.backtrace.frames), | 60 std::begin(allocation.context.backtrace.frames), |
| 59 std::begin(allocation.context.backtrace.frames) + | 61 std::begin(allocation.context.backtrace.frames) + |
| 60 allocation.context.backtrace.frame_count); | 62 allocation.context.backtrace.frame_count); |
| 61 | 63 |
| 62 int type_id = serialization_state->type_name_deduplicator()->Insert( | 64 int type_id = serialization_state->type_name_deduplicator()->Insert( |
| 63 allocation.context.type_name); | 65 allocation.context.type_name); |
| 64 | 66 |
| 65 AggregationKey key = {backtrace_id, type_id}; | 67 AggregationKey key = {backtrace_id, type_id}; |
| 66 AllocationMetrics& metrics = (*metrics_by_key)[key]; | 68 AllocationMetrics& metrics = (*metrics_by_key)[key]; |
| 67 metrics.size += allocation.size; | 69 metrics.size += allocation.size; |
| 68 metrics.count += 1; | 70 metrics.count += 1; |
| 69 }; | 71 }; |
| 70 allocation_register.VisitAllocations(base::BindRepeating( | 72 { |
| 71 visit_allocation, base::Unretained(serialization_state), | 73 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("memory-infra"), |
| 72 base::Unretained(&metrics_by_key))); | 74 "SerializeHeapDump.VisitAllocations"); |
| 75 allocation_register.VisitAllocations(base::BindRepeating( |
| 76 visit_allocation, base::Unretained(serialization_state), |
| 77 base::Unretained(&metrics_by_key))); |
| 78 } |
| 73 | 79 |
| 74 auto traced_value = MakeUnique<TracedValue>(); | 80 auto traced_value = MakeUnique<TracedValue>(); |
| 75 | 81 |
| 76 traced_value->BeginArray("nodes"); | 82 traced_value->BeginArray("nodes"); |
| 77 for (const auto& key_and_metrics : metrics_by_key) | 83 for (const auto& key_and_metrics : metrics_by_key) |
| 78 traced_value->AppendInteger(key_and_metrics.first.backtrace_id); | 84 traced_value->AppendInteger(key_and_metrics.first.backtrace_id); |
| 79 traced_value->EndArray(); | 85 traced_value->EndArray(); |
| 80 | 86 |
| 81 traced_value->BeginArray("types"); | 87 traced_value->BeginArray("types"); |
| 82 for (const auto& key_and_metrics : metrics_by_key) | 88 for (const auto& key_and_metrics : metrics_by_key) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 traced_value->AppendInteger( | 100 traced_value->AppendInteger( |
| 95 saturated_cast<int>(key_and_metrics.second.size)); | 101 saturated_cast<int>(key_and_metrics.second.size)); |
| 96 traced_value->EndArray(); | 102 traced_value->EndArray(); |
| 97 | 103 |
| 98 return traced_value; | 104 return traced_value; |
| 99 } | 105 } |
| 100 | 106 |
| 101 std::unique_ptr<TracedValue> SerializeHeapProfileEventData( | 107 std::unique_ptr<TracedValue> SerializeHeapProfileEventData( |
| 102 const SerializedHeapDumpsMap& heap_dumps, | 108 const SerializedHeapDumpsMap& heap_dumps, |
| 103 HeapProfilerSerializationState* serialization_state) { | 109 HeapProfilerSerializationState* serialization_state) { |
| 110 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("memory-infra"), |
| 111 "SerializeHeapProfileEventData"); |
| 104 auto traced_value = MakeUnique<TracedValue>(); | 112 auto traced_value = MakeUnique<TracedValue>(); |
| 105 | 113 |
| 106 // See brief description of the format in the header file. | 114 // See brief description of the format in the header file. |
| 107 traced_value->SetInteger("version", 1); | 115 traced_value->SetInteger("version", 1); |
| 108 | 116 |
| 109 traced_value->BeginDictionary("allocators"); | 117 traced_value->BeginDictionary("allocators"); |
| 110 for (const auto& name_and_dump : heap_dumps) { | 118 for (const auto& name_and_dump : heap_dumps) { |
| 111 traced_value->SetValueWithCopiedName(name_and_dump.first.c_str(), | 119 traced_value->SetValueWithCopiedName(name_and_dump.first.c_str(), |
| 112 *name_and_dump.second); | 120 *name_and_dump.second); |
| 113 } | 121 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 traced_value->EndArray(); | 141 traced_value->EndArray(); |
| 134 } | 142 } |
| 135 | 143 |
| 136 traced_value->EndDictionary(); | 144 traced_value->EndDictionary(); |
| 137 | 145 |
| 138 return traced_value; | 146 return traced_value; |
| 139 } | 147 } |
| 140 | 148 |
| 141 } // namespace trace_event | 149 } // namespace trace_event |
| 142 } // namespace base | 150 } // namespace base |
| OLD | NEW |