Chromium Code Reviews| 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 it to |Write| to get a |TracedValue| that can be |
|
petrcermak
2015/12/04 19:13:20
nit: I'd say "... and pass its return value to |Wr
Ruud van Asseldonk
2015/12/07 13:53:59
Done.
| |
| 32 // 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. Use |Write| to convert to a traced value. |
|
petrcermak
2015/12/04 19:13:20
It would probably be worth saying here that this a
Ruud van Asseldonk
2015/12/07 13:53:59
Done.
| |
| 63 const std::set<Entry>& Dump(); | |
| 64 | |
| 65 // Writes an "entries" array to a traced value. See https://goo.gl/KY7zVE for | |
| 66 // a description of the format. | |
| 67 static scoped_refptr<TracedValue> Write(const std::set<Entry>& dump); | |
| 44 | 68 |
| 45 private: | 69 private: |
| 46 // Writes a "bt" key that references a stack frame in the |stackFrames| | 70 // Inserts an |Entry| for |Bucket| into |entries_|. Returns false if the |
| 47 // dictionary. | 71 // entry was present before, true if it was not. |
| 48 void WriteStackFrameIndex(int index); | 72 bool InsertEntry(const Bucket& bucket); |
| 49 | 73 |
| 50 // Writes a "type" key with the stringified type ID. | 74 // Recursively breaks down a bucket into smaller buckets and adds entries for |
| 51 void WriteTypeId(int type_id); | 75 // the buckets worth dumping to |entries_|. |
| 76 void BreakDown(const Bucket& bucket); | |
| 52 | 77 |
| 53 // Writes a "size" key with value |size| as a hexidecimal string to the traced | 78 // A map of allocation context to the number of bytes allocated for that |
|
petrcermak
2015/12/04 19:13:20
nit: s/map of/map from/
Ruud van Asseldonk
2015/12/07 13:53:59
Done.
| |
| 54 // value. | 79 // context. |
| 55 void WriteSize(size_t size); | 80 hash_map<AllocationContext, size_t> bytes_by_context_; |
| 56 | 81 |
| 57 // The value that this heap dumper writes to. | 82 // The collection of entries that is filled by |Dump|. |
| 58 const scoped_refptr<TracedValue> traced_value_; | 83 std::set<Entry> entries_; |
| 59 | 84 |
| 60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | 85 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive |
| 61 // this heap dump writer instance. | 86 // this heap dump writer instance. |
| 62 StackFrameDeduplicator* const stack_frame_deduplicator_; | 87 StackFrameDeduplicator* const stack_frame_deduplicator_; |
| 63 | 88 |
| 64 // Helper for converting type names to IDs. Not owned, must outlive this heap | 89 // Helper for converting type names to IDs. Not owned, must outlive this heap |
| 65 // dump writer instance. | 90 // dump writer instance. |
| 66 TypeNameDeduplicator* const type_name_deduplicator_; | 91 TypeNameDeduplicator* const type_name_deduplicator_; |
| 67 | 92 |
| 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); | 93 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); |
| 77 }; | 94 }; |
| 78 | 95 |
| 96 // Comparison operator to enable putting |Entry| in a |std::set|. | |
| 97 BASE_EXPORT bool operator<(HeapDumpWriter::Entry lhs, | |
| 98 HeapDumpWriter::Entry rhs); | |
| 99 | |
| 79 } // namespace trace_event | 100 } // namespace trace_event |
| 80 } // namespace base | 101 } // namespace base |
| 81 | 102 |
| 82 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 103 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
| OLD | NEW |