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 namespace { | |
24 struct Bucket; | |
25 } | |
26 | |
23 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | 27 // Helper class to dump a snapshot of an |AllocationRegister| or other heap |
24 // bookkeeping structure into a |TracedValue|. This class is intended to be | 28 // 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 | 29 // used as a one-shot local instance on the stack. To write heap dumps, call |
26 // |InsertAllocation| for every captured allocation, then call |WriteHeapDump| | 30 // |InsertAllocation| for every captured allocation, then call |Dump| to do |
27 // to do the processing and generate a heap dump value for the trace log. | 31 // the processing, and pass its return value to |Write| to get a |TracedValue| |
32 // that can be dumped into the trace log. | |
28 class BASE_EXPORT HeapDumpWriter { | 33 class BASE_EXPORT HeapDumpWriter { |
29 public: | 34 public: |
35 // An entry in the "entries" array as described in https://goo.gl/KY7zVE. | |
36 struct Entry { | |
37 size_t size; | |
38 | |
39 // References a backtrace in the stack frame deduplicator. -1 means empty | |
40 // backtrace (the root of the tree). | |
41 int stack_frame_id; | |
42 | |
43 // References a type name in the type name deduplicator. -1 indicates that | |
44 // the size is the cumulative size for all types (the root of the tree). | |
45 int type_id; | |
46 }; | |
47 | |
30 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The | 48 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The |
31 // heap dump writer assumes exclusive access to them during the lifetime of | 49 // heap dump writer assumes exclusive access to them during the lifetime of |
32 // the dump writer. | 50 // the dump writer. |
33 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, | 51 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, |
34 TypeNameDeduplicator* type_name_deduplicator); | 52 TypeNameDeduplicator* type_name_deduplicator); |
53 | |
35 ~HeapDumpWriter(); | 54 ~HeapDumpWriter(); |
36 | 55 |
37 // Inserts information from which the heap dump will be generated. This method | 56 // Inserts information from which the heap dump will be generated. This method |
38 // does minimal processing, so it can be called when a lock is held. | 57 // does minimal processing, so it can be called when a lock is held. |
39 void InsertAllocation(const AllocationContext& context, size_t size); | 58 void InsertAllocation(const AllocationContext& context, size_t size); |
40 | 59 |
41 // Aggregates allocations and writes an "entries" array to a traced value. See | 60 // Aggregates allocations to compute the total size of the heap, then breaks |
42 // https://goo.gl/jYN4Zn for a description of the format. | 61 // down the heap recursively. This produces the values that should be dumped |
43 scoped_refptr<TracedValue> WriteHeapDump(); | 62 // in the "entries" array. The number of entries is kept reasonable because |
63 // long tails are not included. Use |Write| to convert to a traced value. | |
64 const std::set<Entry>& Dump(); | |
Primiano Tucci (use gerrit)
2015/12/08 18:05:30
I understand this is for testability, but I'd like
Ruud van Asseldonk
2015/12/09 14:07:53
Obsolete now. I put it in a namespace instead.
| |
65 | |
66 // Writes an "entries" array to a traced value. See https://goo.gl/KY7zVE for | |
67 // a description of the format. | |
68 static scoped_refptr<TracedValue> Write(const std::set<Entry>& dump); | |
Primiano Tucci (use gerrit)
2015/12/08 18:05:30
and obviously drop (privatize) this
Ruud van Asseldonk
2015/12/09 14:07:54
Obsolete now.
| |
44 | 69 |
45 private: | 70 private: |
46 // Writes a "bt" key that references a stack frame in the |stackFrames| | 71 // Inserts an |Entry| for |Bucket| into |entries_|. Returns false if the |
47 // dictionary. | 72 // entry was present before, true if it was not. |
48 void WriteStackFrameIndex(int index); | 73 bool InsertEntry(const Bucket& bucket); |
49 | 74 |
50 // Writes a "type" key with the stringified type ID. | 75 // Recursively breaks down a bucket into smaller buckets and adds entries for |
51 void WriteTypeId(int type_id); | 76 // the buckets worth dumping to |entries_|. |
77 void BreakDown(const Bucket& bucket); | |
52 | 78 |
53 // Writes a "size" key with value |size| as a hexidecimal string to the traced | 79 // A map map from allocation context to the number of bytes allocated for that |
54 // value. | 80 // context. |
55 void WriteSize(size_t size); | 81 hash_map<AllocationContext, size_t> bytes_by_context_; |
56 | 82 |
57 // The value that this heap dumper writes to. | 83 // The collection of entries that is filled by |Dump|. |
58 const scoped_refptr<TracedValue> traced_value_; | 84 std::set<Entry> entries_; |
59 | 85 |
60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | 86 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive |
61 // this heap dump writer instance. | 87 // this heap dump writer instance. |
62 StackFrameDeduplicator* const stack_frame_deduplicator_; | 88 StackFrameDeduplicator* const stack_frame_deduplicator_; |
63 | 89 |
64 // Helper for converting type names to IDs. Not owned, must outlive this heap | 90 // Helper for converting type names to IDs. Not owned, must outlive this heap |
65 // dump writer instance. | 91 // dump writer instance. |
66 TypeNameDeduplicator* const type_name_deduplicator_; | 92 TypeNameDeduplicator* const type_name_deduplicator_; |
67 | 93 |
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); | 94 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); |
77 }; | 95 }; |
78 | 96 |
97 // Comparison operator to enable putting |Entry| in a |std::set|. | |
98 BASE_EXPORT bool operator<(HeapDumpWriter::Entry lhs, | |
99 HeapDumpWriter::Entry rhs); | |
100 | |
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 |