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 |
| 18 class Value; |
| 19 |
17 namespace trace_event { | 20 namespace trace_event { |
18 | 21 |
19 class StackFrameDeduplicator; | 22 class StackFrameDeduplicator; |
20 class TracedValue; | 23 class TracedValue; |
21 class TypeNameDeduplicator; | 24 class TypeNameDeduplicator; |
22 | 25 |
23 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | 26 // Aggregates and breaks down allocations and writes an "entries" array with |
24 // bookkeeping structure into a |TracedValue|. This class is intended to be | 27 // the most significant entries to a traced value. See https://goo.gl/KY7zVE |
25 // used as a one-shot local instance on the stack. To write heap dumps, call | 28 // for a description of the format. |
26 // |InsertAllocation| for every captured allocation, then call |WriteHeapDump| | 29 scoped_refptr<TracedValue> BASE_EXPORT |
27 // to do the processing and generate a heap dump value for the trace log. | 30 WriteHeapDump(const base::hash_map<AllocationContext, size_t>& allocations, |
28 class BASE_EXPORT HeapDumpWriter { | 31 StackFrameDeduplicator* stack_frame_deduplicator, |
29 public: | 32 TypeNameDeduplicator* type_name_deduplicator); |
30 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The | |
31 // heap dump writer assumes exclusive access to them during the lifetime of | |
32 // the dump writer. | |
33 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, | |
34 TypeNameDeduplicator* type_name_deduplicator); | |
35 ~HeapDumpWriter(); | |
36 | 33 |
37 // Inserts information from which the heap dump will be generated. This method | 34 namespace heap_dump { |
38 // does minimal processing, so it can be called when a lock is held. | |
39 void InsertAllocation(const AllocationContext& context, size_t size); | |
40 | 35 |
41 // Aggregates allocations and writes an "entries" array to a traced value. See | 36 // An entry in the "entries" array as described in https://goo.gl/KY7zVE. |
42 // https://goo.gl/jYN4Zn for a description of the format. | 37 struct BASE_EXPORT HeapEntry { |
43 scoped_refptr<TracedValue> WriteHeapDump(); | 38 size_t size; |
44 | 39 |
45 private: | 40 // References a backtrace in the stack frame deduplicator. -1 means empty |
46 // Writes a "bt" key that references a stack frame in the |stackFrames| | 41 // backtrace (the root of the tree). |
47 // dictionary. | 42 int stack_frame_id; |
48 void WriteStackFrameIndex(int index); | |
49 | 43 |
50 // Writes a "type" key with the stringified type ID. | 44 // References a type name in the type name deduplicator. -1 indicates that |
51 void WriteTypeId(int type_id); | 45 // the size is the cumulative size for all types (the root of the tree). |
52 | 46 int type_id; |
53 // Writes a "size" key with value |size| as a hexidecimal string to the traced | |
54 // value. | |
55 void WriteSize(size_t size); | |
56 | |
57 // The value that this heap dumper writes to. | |
58 const scoped_refptr<TracedValue> traced_value_; | |
59 | |
60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | |
61 // this heap dump writer instance. | |
62 StackFrameDeduplicator* const stack_frame_deduplicator_; | |
63 | |
64 // Helper for converting type names to IDs. Not owned, must outlive this heap | |
65 // dump writer instance. | |
66 TypeNameDeduplicator* const type_name_deduplicator_; | |
67 | |
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); | |
77 }; | 47 }; |
78 | 48 |
| 49 // Comparison operator to enable putting |HeapEntry| in a |std::set|. |
| 50 bool BASE_EXPORT operator<(HeapEntry lhs, HeapEntry rhs); |
| 51 |
| 52 // Aggregates allocations to compute the total size of the heap, then breaks |
| 53 // down the heap recursively. This produces the values that should be dumped |
| 54 // in the "entries" array. The number of entries is kept reasonable because |
| 55 // long tails are not included. Use |Write| to convert to a traced value. |
| 56 std::set<HeapEntry> BASE_EXPORT |
| 57 Dump(const base::hash_map<AllocationContext, size_t>& allocations, |
| 58 StackFrameDeduplicator* stack_frame_deduplicator, |
| 59 TypeNameDeduplicator* type_name_deduplicator); |
| 60 |
| 61 // Writes an "entries" array to a traced value. See https://goo.gl/KY7zVE for |
| 62 // a description of the format. |
| 63 scoped_refptr<TracedValue> BASE_EXPORT Write(const std::set<HeapEntry>& dump); |
| 64 |
| 65 } // namespace heap_dump |
79 } // namespace trace_event | 66 } // namespace trace_event |
80 } // namespace base | 67 } // namespace base |
81 | 68 |
82 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 69 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
OLD | NEW |