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