| 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 <string> |
| 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 TracedValue; | 20 class TracedValue; |
| 20 class StackFrameDeduplicator; | 21 class TypeNameDeduplicator; |
| 21 | 22 |
| 22 // Helper class to dump a snapshot of an |AllocationRegister| or other heap | 23 // Helper class to dump a snapshot of an |AllocationRegister| or other heap |
| 23 // bookkeeping structure into a |TracedValue|. This class is intended to be | 24 // bookkeeping structure into a |TracedValue|. This class is intended to be |
| 24 // used as a one-shot local instance on the stack. To write heap dumps, call | 25 // used as a one-shot local instance on the stack. To write heap dumps, call |
| 25 // |InsertAllocation| for every captured allocation, then call |WriteHeapDump| | 26 // |InsertAllocation| for every captured allocation, then call |WriteHeapDump| |
| 26 // to do the processing and generate a heap dump value for the trace log. | 27 // to do the processing and generate a heap dump value for the trace log. |
| 27 class BASE_EXPORT HeapDumpWriter { | 28 class BASE_EXPORT HeapDumpWriter { |
| 28 public: | 29 public: |
| 29 // The |StackFrameDeduplicator| is not owned. The heap dump writer assumes | 30 // The |StackFrameDeduplicator| and |TypeNameDeduplicator| are not owned. The |
| 30 // exclusive access to it during the lifetime of the dump writer. | 31 // heap dump writer assumes exclusive access to them during the lifetime of |
| 31 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator); | 32 // the dump writer. |
| 33 HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator, |
| 34 TypeNameDeduplicator* type_name_deduplicator); |
| 32 ~HeapDumpWriter(); | 35 ~HeapDumpWriter(); |
| 33 | 36 |
| 34 // Inserts information from which the heap dump will be generated. This method | 37 // Inserts information from which the heap dump will be generated. This method |
| 35 // does minimal processing, so it can be called when a lock is held. | 38 // does minimal processing, so it can be called when a lock is held. |
| 36 void InsertAllocation(const AllocationContext& context, size_t size); | 39 void InsertAllocation(const AllocationContext& context, size_t size); |
| 37 | 40 |
| 38 // Aggregates allocations and writes an "entries" array to a traced value. See | 41 // Aggregates allocations and writes an "entries" array to a traced value. See |
| 39 // https://goo.gl/jYN4Zn for a description of the format. | 42 // https://goo.gl/jYN4Zn for a description of the format. |
| 40 scoped_refptr<TracedValue> WriteHeapDump(); | 43 scoped_refptr<TracedValue> WriteHeapDump(); |
| 41 | 44 |
| 42 private: | 45 private: |
| 43 // Writes a "bt" key that references a stack frame in the |stackFrames| | 46 // Writes a "bt" key that references a stack frame in the |stackFrames| |
| 44 // dictionary. | 47 // dictionary. |
| 45 void WriteStackFrameIndex(int index); | 48 void WriteStackFrameIndex(int index); |
| 46 | 49 |
| 47 // Writes a "type" key with the stringified type ID. | 50 // Writes a "type" key with the stringified type ID. |
| 48 void WriteTypeId(AllocationContext::TypeId type_id); | 51 void WriteTypeId(int type_id); |
| 49 | 52 |
| 50 // Writes a "size" key with value |size| as a hexidecimal string to the traced | 53 // Writes a "size" key with value |size| as a hexidecimal string to the traced |
| 51 // value. | 54 // value. |
| 52 void WriteSize(size_t size); | 55 void WriteSize(size_t size); |
| 53 | 56 |
| 54 // The value that this heap dumper writes to. | 57 // The value that this heap dumper writes to. |
| 55 const scoped_refptr<TracedValue> traced_value_; | 58 const scoped_refptr<TracedValue> traced_value_; |
| 56 | 59 |
| 57 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive | 60 // Helper for generating the |stackFrames| dictionary. Not owned, must outlive |
| 58 // this heap dump writer instance. | 61 // this heap dump writer instance. |
| 59 StackFrameDeduplicator* const stack_frame_deduplicator_; | 62 StackFrameDeduplicator* const stack_frame_deduplicator_; |
| 60 | 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 |
| 61 // A map of allocation context to the number of bytes allocated for that | 68 // A map of allocation context to the number of bytes allocated for that |
| 62 // context. | 69 // context. |
| 63 hash_map<AllocationContext, size_t> bytes_by_context_; | 70 hash_map<AllocationContext, size_t> bytes_by_context_; |
| 64 | 71 |
| 65 // Buffer for converting integers into strings, that is re-used throughout the | 72 // Buffer for converting integers into strings, that is re-used throughout the |
| 66 // dump. | 73 // dump. |
| 67 std::string buffer_; | 74 std::string buffer_; |
| 68 | 75 |
| 69 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); | 76 DISALLOW_COPY_AND_ASSIGN(HeapDumpWriter); |
| 70 }; | 77 }; |
| 71 | 78 |
| 72 } // namespace trace_event | 79 } // namespace trace_event |
| 73 } // namespace base | 80 } // namespace base |
| 74 | 81 |
| 75 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ | 82 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_HEAP_DUMP_WRITER_H_ |
| OLD | NEW |