OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | |
6 #define BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <memory> | |
11 #include <set> | |
12 #include <unordered_map> | |
13 | |
14 #include "base/base_export.h" | |
15 #include "base/macros.h" | |
16 #include "base/trace_event/heap_profiler_allocation_context.h" | |
17 | |
18 namespace base { | |
19 namespace trace_event { | |
20 | |
21 class HeapProfilerSerializationState; | |
22 class StackFrameDeduplicator; | |
23 class TracedValue; | |
24 class TypeNameDeduplicator; | |
25 | |
26 // Aggregates |metrics_by_context|, recursively breaks down the heap, and | |
27 // returns a traced value with an "entries" array that can be dumped in the | |
28 // trace log, following the format described in https://goo.gl/KY7zVE. The | |
29 // number of entries is kept reasonable because long tails are not included. | |
30 BASE_EXPORT std::unique_ptr<TracedValue> ExportHeapDump( | |
31 const std::unordered_map<AllocationContext, AllocationMetrics>& | |
32 metrics_by_context, | |
33 const HeapProfilerSerializationState& heap_profiler_serialization_state); | |
34 | |
35 namespace internal { | |
36 | |
37 namespace { | |
38 struct Bucket; | |
39 } | |
40 | |
41 // An entry in the "entries" array as described in https://goo.gl/KY7zVE. | |
42 struct BASE_EXPORT Entry { | |
43 size_t size; | |
44 size_t count; | |
45 | |
46 // References a backtrace in the stack frame deduplicator. -1 means empty | |
47 // backtrace (the root of the tree). | |
48 int stack_frame_id; | |
49 | |
50 // References a type name in the type name deduplicator. -1 indicates that | |
51 // the size is the cumulative size for all types (the root of the tree). | |
52 int type_id; | |
53 }; | |
54 | |
55 // Comparison operator to enable putting |Entry| in a |std::set|. | |
56 BASE_EXPORT bool operator<(Entry lhs, Entry rhs); | |
57 | |
58 // Serializes entries to an "entries" array in a traced value. | |
59 BASE_EXPORT std::unique_ptr<TracedValue> Serialize(const std::set<Entry>& dump); | |
60 | |
61 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | |
62 // bookkeeping structure into a |TracedValue|. This class is intended to be | |
63 // used as a one-shot local instance on the stack. | |
64 class BASE_EXPORT HeapDumpWriter { | |
65 public: | |
66 // The |stack_frame_deduplicator| and |type_name_deduplicator| are not owned. | |
67 // The heap dump writer assumes exclusive access to them during the lifetime | |
68 // of the dump writer. The heap dumps are broken down for allocations bigger | |
69 // than |breakdown_threshold_bytes|. | |
70 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, | |
71 TypeNameDeduplicator* type_name_deduplicator, | |
72 uint32_t breakdown_threshold_bytes); | |
73 | |
74 ~HeapDumpWriter(); | |
75 | |
76 // Aggregates allocations to compute the total size of the heap, then breaks | |
77 // down the heap recursively. This produces the values that should be dumped | |
78 // in the "entries" array. The number of entries is kept reasonable because | |
79 // long tails are not included. Use |Serialize| to convert to a traced value. | |
80 const std::set<Entry>& Summarize( | |
81 const std::unordered_map<AllocationContext, AllocationMetrics>& | |
82 metrics_by_context); | |
83 | |
84 private: | |
85 // Inserts an |Entry| for |Bucket| into |entries_|. Returns false if the | |
86 // entry was present before, true if it was not. | |
87 bool AddEntryForBucket(const Bucket& bucket); | |
88 | |
89 // Recursively breaks down a bucket into smaller buckets and adds entries for | |
90 // the buckets worth dumping to |entries_|. | |
91 void BreakDown(const Bucket& bucket); | |
92 | |
93 // The collection of entries that is filled by |Summarize|. | |
94 std::set<Entry> entries_; | |
95 | |
96 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | |
97 // this heap dump writer instance. | |
98 StackFrameDeduplicator* const stack_frame_deduplicator_; | |
99 | |
100 // Helper for converting type names to IDs. Not owned, must outlive this heap | |
101 // dump writer instance. | |
102 TypeNameDeduplicator* const type_name_deduplicator_; | |
103 | |
104 // Minimum size of an allocation for which an allocation bucket will be | |
105 // broken down with children. | |
106 uint32_t breakdown_threshold_bytes_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); | |
109 }; | |
110 | |
111 } // namespace internal | |
112 } // namespace trace_event | |
113 } // namespace base | |
114 | |
115 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | |
OLD | NEW |