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 inpluded. | |
Primiano Tucci (use gerrit)
2015/12/09 17:04:00
typo on inpluded
Ruud van Asseldonk
2015/12/09 20:58:05
Fixed.
| |
27 BASE_EXPORT scoped_refptr<TracedValue> WriteHeapDump( | |
Primiano Tucci (use gerrit)
2015/12/09 17:04:00
thinking on naming: is this really a "Write" funct
Ruud van Asseldonk
2015/12/09 20:58:05
I agree, |ExportHeapDump| is better. (The file is
| |
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); | |
Primiano Tucci (use gerrit)
2015/12/09 17:04:00
Since you have Entry declared here, can you not ma
Ruud van Asseldonk
2015/12/09 20:58:05
Yes that would be possible. I didn’t because I thi
| |
53 | |
54 // Writes an "entries" array to a traced value. See https://goo.gl/KY7zVE for | |
Primiano Tucci (use gerrit)
2015/12/09 17:04:00
I think it's the 3rd time you mention the link to
Primiano Tucci (use gerrit)
2015/12/09 17:04:00
probably s/Writes/Serializes/
Ruud van Asseldonk
2015/12/09 20:58:05
Removed. This is what happens when you move things
Ruud van Asseldonk
2015/12/09 20:58:05
Done. Also renamed method to |Serialize| because l
| |
55 // a description of the format. | |
56 BASE_EXPORT scoped_refptr<TracedValue> Write(const std::set<Entry>& dump); | |
57 | |
23 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | 58 // Helper class to dump a snapshot of an |AllocationRegister| or other heap |
24 // bookkeeping structure into a |TracedValue|. This class is intended to be | 59 // 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 | 60 // 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 { | 61 class BASE_EXPORT HeapDumpWriter { |
29 public: | 62 public: |
30 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The | 63 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The |
31 // heap dump writer assumes exclusive access to them during the lifetime of | 64 // heap dump writer assumes exclusive access to them during the lifetime of |
32 // the dump writer. | 65 // the dump writer. |
33 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, | 66 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, |
34 TypeNameDeduplicator* type_name_deduplicator); | 67 TypeNameDeduplicator* type_name_deduplicator); |
68 | |
35 ~HeapDumpWriter(); | 69 ~HeapDumpWriter(); |
36 | 70 |
37 // Inserts information from which the heap dump will be generated. This method | 71 // 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. | 72 // down the heap recursively. This produces the values that should be dumped |
39 void InsertAllocation(const AllocationContext& context, size_t size); | 73 // in the "entries" array. The number of entries is kept reasonable because |
40 | 74 // long tails are not included. Use |Write| to convert to a traced value. |
41 // Aggregates allocations and writes an "entries" array to a traced value. See | 75 const std::set<Entry>& Dump( |
42 // https://goo.gl/jYN4Zn for a description of the format. | 76 const hash_map<AllocationContext, size_t>& bytes_by_context); |
43 scoped_refptr<TracedValue> WriteHeapDump(); | |
44 | 77 |
45 private: | 78 private: |
46 // Writes a "bt" key that references a stack frame in the |stackFrames| | 79 // Inserts an |Entry| for |Bucket| into |entries_|. Returns false if the |
47 // dictionary. | 80 // entry was present before, true if it was not. |
48 void WriteStackFrameIndex(int index); | 81 bool AddEntryForBucket(const Bucket& bucket); |
49 | 82 |
50 // Writes a "type" key with the stringified type ID. | 83 // Recursively breaks down a bucket into smaller buckets and adds entries for |
51 void WriteTypeId(int type_id); | 84 // the buckets worth dumping to |entries_|. |
85 void BreakDown(const Bucket& bucket); | |
52 | 86 |
53 // Writes a "size" key with value |size| as a hexidecimal string to the traced | 87 // The collection of entries that is filled by |Dump|. |
54 // value. | 88 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 | 89 |
60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | 90 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive |
61 // this heap dump writer instance. | 91 // this heap dump writer instance. |
62 StackFrameDeduplicator* const stack_frame_deduplicator_; | 92 StackFrameDeduplicator* const stack_frame_deduplicator_; |
63 | 93 |
64 // Helper for converting type names to IDs. Not owned, must outlive this heap | 94 // Helper for converting type names to IDs. Not owned, must outlive this heap |
65 // dump writer instance. | 95 // dump writer instance. |
66 TypeNameDeduplicator* const type_name_deduplicator_; | 96 TypeNameDeduplicator* const type_name_deduplicator_; |
67 | 97 |
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); | 98 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); |
77 }; | 99 }; |
78 | 100 |
101 } // namespace internal | |
79 } // namespace trace_event | 102 } // namespace trace_event |
80 } // namespace base | 103 } // namespace base |
81 | 104 |
82 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 105 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
OLD | NEW |