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 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
6 #define BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
7 | 7 |
8 #include <string> | 8 #include <set> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/trace_event/heap_profiler_allocation_context.h" | 14 #include "base/trace_event/heap_profiler_allocation_context.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 namespace trace_event { | 17 namespace trace_event { |
18 | 18 |
19 class StackFrameDeduplicator; | 19 class StackFrameDeduplicator; |
20 class TracedValue; | 20 class TracedValue; |
21 class TypeNameDeduplicator; | 21 class TypeNameDeduplicator; |
22 | 22 |
| 23 // Aggregates |bytes_by_context|, recursively breaks down the heap, and returns |
| 24 // a traced value with an "entries" array that can be dumped in the trace log, |
| 25 // following the format described in https://goo.gl/KY7zVE. The number of |
| 26 // entries is kept reasonable because long tails are not included. |
| 27 BASE_EXPORT scoped_refptr<TracedValue> ExportHeapDump( |
| 28 const hash_map<AllocationContext, size_t>& bytes_by_context, |
| 29 StackFrameDeduplicator* stack_frame_deduplicator, |
| 30 TypeNameDeduplicator* type_name_deduplicator); |
| 31 |
| 32 namespace internal { |
| 33 |
| 34 namespace { |
| 35 struct Bucket; |
| 36 } |
| 37 |
| 38 // An entry in the "entries" array as described in https://goo.gl/KY7zVE. |
| 39 struct BASE_EXPORT Entry { |
| 40 size_t size; |
| 41 |
| 42 // References a backtrace in the stack frame deduplicator. -1 means empty |
| 43 // backtrace (the root of the tree). |
| 44 int stack_frame_id; |
| 45 |
| 46 // References a type name in the type name deduplicator. -1 indicates that |
| 47 // the size is the cumulative size for all types (the root of the tree). |
| 48 int type_id; |
| 49 }; |
| 50 |
| 51 // Comparison operator to enable putting |Entry| in a |std::set|. |
| 52 BASE_EXPORT bool operator<(Entry lhs, Entry rhs); |
| 53 |
| 54 // Serializes entries to an "entries" array in a traced value. |
| 55 BASE_EXPORT scoped_refptr<TracedValue> Serialize(const std::set<Entry>& dump); |
| 56 |
23 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | 57 // Helper class to dump a snapshot of an |AllocationRegister| or other heap |
24 // bookkeeping structure into a |TracedValue|. This class is intended to be | 58 // bookkeeping structure into a |TracedValue|. This class is intended to be |
25 // used as a one-shot local instance on the stack. To write heap dumps, call | 59 // used as a one-shot local instance on the stack. |
26 // |InsertAllocation| for every captured allocation, then call |WriteHeapDump| | |
27 // to do the processing and generate a heap dump value for the trace log. | |
28 class BASE_EXPORT HeapDumpWriter { | 60 class BASE_EXPORT HeapDumpWriter { |
29 public: | 61 public: |
30 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The | 62 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The |
31 // heap dump writer assumes exclusive access to them during the lifetime of | 63 // heap dump writer assumes exclusive access to them during the lifetime of |
32 // the dump writer. | 64 // the dump writer. |
33 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, | 65 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, |
34 TypeNameDeduplicator* type_name_deduplicator); | 66 TypeNameDeduplicator* type_name_deduplicator); |
| 67 |
35 ~HeapDumpWriter(); | 68 ~HeapDumpWriter(); |
36 | 69 |
37 // Inserts information from which the heap dump will be generated. This method | 70 // Aggregates allocations to compute the total size of the heap, then breaks |
38 // does minimal processing, so it can be called when a lock is held. | 71 // down the heap recursively. This produces the values that should be dumped |
39 void InsertAllocation(const AllocationContext& context, size_t size); | 72 // in the "entries" array. The number of entries is kept reasonable because |
40 | 73 // long tails are not included. Use |Serialize| to convert to a traced value. |
41 // Aggregates allocations and writes an "entries" array to a traced value. See | 74 const std::set<Entry>& Summarize( |
42 // https://goo.gl/jYN4Zn for a description of the format. | 75 const hash_map<AllocationContext, size_t>& bytes_by_context); |
43 scoped_refptr<TracedValue> WriteHeapDump(); | |
44 | 76 |
45 private: | 77 private: |
46 // Writes a "bt" key that references a stack frame in the |stackFrames| | 78 // Inserts an |Entry| for |Bucket| into |entries_|. Returns false if the |
47 // dictionary. | 79 // entry was present before, true if it was not. |
48 void WriteStackFrameIndex(int index); | 80 bool AddEntryForBucket(const Bucket& bucket); |
49 | 81 |
50 // Writes a "type" key with the stringified type ID. | 82 // Recursively breaks down a bucket into smaller buckets and adds entries for |
51 void WriteTypeId(int type_id); | 83 // the buckets worth dumping to |entries_|. |
| 84 void BreakDown(const Bucket& bucket); |
52 | 85 |
53 // Writes a "size" key with value |size| as a hexidecimal string to the traced | 86 // The collection of entries that is filled by |Summarize|. |
54 // value. | 87 std::set<Entry> entries_; |
55 void WriteSize(size_t size); | |
56 | |
57 // The value that this heap dumper writes to. | |
58 const scoped_refptr<TracedValue> traced_value_; | |
59 | 88 |
60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | 89 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive |
61 // this heap dump writer instance. | 90 // this heap dump writer instance. |
62 StackFrameDeduplicator* const stack_frame_deduplicator_; | 91 StackFrameDeduplicator* const stack_frame_deduplicator_; |
63 | 92 |
64 // Helper for converting type names to IDs. Not owned, must outlive this heap | 93 // Helper for converting type names to IDs. Not owned, must outlive this heap |
65 // dump writer instance. | 94 // dump writer instance. |
66 TypeNameDeduplicator* const type_name_deduplicator_; | 95 TypeNameDeduplicator* const type_name_deduplicator_; |
67 | 96 |
68 // A map of allocation context to the number of bytes allocated for that | |
69 // context. | |
70 hash_map<AllocationContext, size_t> bytes_by_context_; | |
71 | |
72 // Buffer for converting integers into strings, that is re-used throughout the | |
73 // dump. | |
74 std::string buffer_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); | 97 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); |
77 }; | 98 }; |
78 | 99 |
| 100 } // namespace internal |
79 } // namespace trace_event | 101 } // namespace trace_event |
80 } // namespace base | 102 } // namespace base |
81 | 103 |
82 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 104 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
OLD | NEW |