OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/heap_profiler_event_writer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <tuple> |
| 10 #include <unordered_map> |
| 11 |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/trace_event/heap_profiler_allocation_register.h" |
| 15 #include "base/trace_event/heap_profiler_serialization_state.h" |
| 16 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" |
| 17 #include "base/trace_event/heap_profiler_string_deduplicator.h" |
| 18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" |
| 19 #include "base/trace_event/trace_event_argument.h" |
| 20 |
| 21 namespace base { |
| 22 namespace trace_event { |
| 23 |
| 24 namespace { |
| 25 |
| 26 struct AggregationKey { |
| 27 int backtrace_id; |
| 28 int type_id; |
| 29 |
| 30 struct Hasher { |
| 31 size_t operator()(const AggregationKey& key) const { |
| 32 return base::HashInts(key.backtrace_id, key.type_id); |
| 33 } |
| 34 }; |
| 35 |
| 36 bool operator==(const AggregationKey& other) const { |
| 37 return backtrace_id == other.backtrace_id && type_id == other.type_id; |
| 38 } |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 std::unique_ptr<TracedValue> SerializeHeapDump( |
| 44 const AllocationRegister& allocation_register, |
| 45 const HeapProfilerSerializationState& serialization_state) { |
| 46 // Aggregate allocations by {backtrace_id, type_id} key. |
| 47 std::unordered_map<AggregationKey, AllocationMetrics, AggregationKey::Hasher> |
| 48 metrics_by_key; |
| 49 for (const auto& allocation : allocation_register) { |
| 50 int backtrace_id = serialization_state.stack_frame_deduplicator()->Insert( |
| 51 std::begin(allocation.context.backtrace.frames), |
| 52 std::begin(allocation.context.backtrace.frames) + |
| 53 allocation.context.backtrace.frame_count); |
| 54 |
| 55 int type_id = serialization_state.type_name_deduplicator()->Insert( |
| 56 allocation.context.type_name); |
| 57 |
| 58 AggregationKey key = {backtrace_id, type_id}; |
| 59 AllocationMetrics& metrics = metrics_by_key[key]; |
| 60 metrics.size += allocation.size; |
| 61 metrics.count += 1; |
| 62 } |
| 63 |
| 64 auto traced_value = MakeUnique<TracedValue>(); |
| 65 |
| 66 traced_value->BeginArray("nodes"); |
| 67 for (const auto& key_and_metrics : metrics_by_key) |
| 68 traced_value->AppendInteger(key_and_metrics.first.backtrace_id); |
| 69 traced_value->EndArray(); |
| 70 |
| 71 traced_value->BeginArray("types"); |
| 72 for (const auto& key_and_metrics : metrics_by_key) |
| 73 traced_value->AppendInteger(key_and_metrics.first.type_id); |
| 74 traced_value->EndArray(); |
| 75 |
| 76 traced_value->BeginArray("counts"); |
| 77 for (const auto& key_and_metrics : metrics_by_key) |
| 78 traced_value->AppendInteger( |
| 79 saturated_cast<int>(key_and_metrics.second.count)); |
| 80 traced_value->EndArray(); |
| 81 |
| 82 traced_value->BeginArray("sizes"); |
| 83 for (const auto& key_and_metrics : metrics_by_key) |
| 84 traced_value->AppendInteger( |
| 85 saturated_cast<int>(key_and_metrics.second.size)); |
| 86 traced_value->EndArray(); |
| 87 |
| 88 return traced_value; |
| 89 } |
| 90 |
| 91 std::unique_ptr<TracedValue> SerializeHeapProfileEventData( |
| 92 const SerializedHeapDumpsMap& heap_dumps, |
| 93 const HeapProfilerSerializationState& serialization_state) { |
| 94 auto traced_value = MakeUnique<TracedValue>(); |
| 95 |
| 96 // See brief description of the format in the header file. |
| 97 traced_value->SetInteger("version", 1); |
| 98 |
| 99 traced_value->BeginDictionary("allocators"); |
| 100 for (const auto& name_and_dump : heap_dumps) { |
| 101 traced_value->SetValueWithCopiedName(name_and_dump.first.c_str(), |
| 102 *name_and_dump.second); |
| 103 } |
| 104 traced_value->EndDictionary(); |
| 105 |
| 106 traced_value->BeginDictionary("maps"); |
| 107 |
| 108 if (auto* deduplicator = serialization_state.stack_frame_deduplicator()) { |
| 109 traced_value->BeginArray("nodes"); |
| 110 deduplicator->SerializeIncrementally(&*traced_value); |
| 111 traced_value->EndArray(); |
| 112 } |
| 113 |
| 114 if (auto* deduplicator = serialization_state.type_name_deduplicator()) { |
| 115 traced_value->BeginArray("types"); |
| 116 deduplicator->SerializeIncrementally(&*traced_value); |
| 117 traced_value->EndArray(); |
| 118 } |
| 119 |
| 120 if (auto* deduplicator = serialization_state.string_deduplicator()) { |
| 121 traced_value->BeginArray("strings"); |
| 122 deduplicator->SerializeIncrementally(&*traced_value); |
| 123 traced_value->EndArray(); |
| 124 } |
| 125 |
| 126 traced_value->EndDictionary(); |
| 127 |
| 128 return traced_value; |
| 129 } |
| 130 |
| 131 } // namespace trace_event |
| 132 } // namespace base |
OLD | NEW |