| 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_STACK_FRAME_DEDUPLICATOR_H_ | 5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_ |
| 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_ | 6 #define BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <deque> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/containers/flat_map.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/trace_event/heap_profiler_allocation_context.h" | 15 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 namespace trace_event { | 18 namespace trace_event { |
| 18 | 19 |
| 19 class StringDeduplicator; | 20 class StringDeduplicator; |
| 20 class TraceEventMemoryOverhead; | 21 class TraceEventMemoryOverhead; |
| 21 class TracedValue; | 22 class TracedValue; |
| 22 | 23 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 38 size_t EstimateMemoryUsage() const; | 39 size_t EstimateMemoryUsage() const; |
| 39 | 40 |
| 40 StackFrame frame; | 41 StackFrame frame; |
| 41 | 42 |
| 42 // The index of the parent stack frame in |frames_|, or kInvalidFrameIndex | 43 // The index of the parent stack frame in |frames_|, or kInvalidFrameIndex |
| 43 // if there is no parent frame (when it is at the bottom of the call stack). | 44 // if there is no parent frame (when it is at the bottom of the call stack). |
| 44 int parent_frame_index; | 45 int parent_frame_index; |
| 45 constexpr static int kInvalidFrameIndex = -1; | 46 constexpr static int kInvalidFrameIndex = -1; |
| 46 | 47 |
| 47 // Indices into |frames_| of frames called from the current frame. | 48 // Indices into |frames_| of frames called from the current frame. |
| 48 std::map<StackFrame, int> children; | 49 base::flat_map<StackFrame, int> children; |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 using ConstIterator = std::vector<FrameNode>::const_iterator; | 52 using ConstIterator = std::deque<FrameNode>::const_iterator; |
| 52 | 53 |
| 53 // |string_deduplication| is used during serialization, and is expected | 54 // |string_deduplication| is used during serialization, and is expected |
| 54 // to outlive instances of this class. | 55 // to outlive instances of this class. |
| 55 explicit StackFrameDeduplicator(StringDeduplicator* string_deduplicator); | 56 explicit StackFrameDeduplicator(StringDeduplicator* string_deduplicator); |
| 56 ~StackFrameDeduplicator(); | 57 ~StackFrameDeduplicator(); |
| 57 | 58 |
| 58 // Inserts a backtrace where |beginFrame| is a pointer to the bottom frame | 59 // Inserts a backtrace where |beginFrame| is a pointer to the bottom frame |
| 59 // (e.g. main) and |endFrame| is a pointer past the top frame (most recently | 60 // (e.g. main) and |endFrame| is a pointer past the top frame (most recently |
| 60 // called function), and returns the index of its leaf node in |frames_|. | 61 // called function), and returns the index of its leaf node in |frames_|. |
| 61 // Returns -1 if the backtrace is empty. | 62 // Returns -1 if the backtrace is empty. |
| 62 int Insert(const StackFrame* beginFrame, const StackFrame* endFrame); | 63 int Insert(const StackFrame* beginFrame, const StackFrame* endFrame); |
| 63 | 64 |
| 64 // Iterators over the frame nodes in the call tree. | 65 // Iterators over the frame nodes in the call tree. |
| 65 ConstIterator begin() const { return frames_.begin(); } | 66 ConstIterator begin() const { return frames_.begin(); } |
| 66 ConstIterator end() const { return frames_.end(); } | 67 ConstIterator end() const { return frames_.end(); } |
| 67 | 68 |
| 68 // Appends new |stackFrames| dictionary items that were added after the | 69 // Appends new |stackFrames| dictionary items that were added after the |
| 69 // last call to this function. | 70 // last call to this function. |
| 70 void SerializeIncrementally(TracedValue* traced_value); | 71 void SerializeIncrementally(TracedValue* traced_value); |
| 71 | 72 |
| 72 // Estimates memory overhead including |sizeof(StackFrameDeduplicator)|. | 73 // Estimates memory overhead including |sizeof(StackFrameDeduplicator)|. |
| 73 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); | 74 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 74 | 75 |
| 75 private: | 76 private: |
| 77 // Checks that existing backtrace identified by |frame_index| equals |
| 78 // to the one identified by |begin_frame|, |end_frame|. |
| 79 bool Match(int frame_index, |
| 80 const StackFrame* begin_frame, |
| 81 const StackFrame* end_frame) const; |
| 82 |
| 76 StringDeduplicator* string_deduplicator_; | 83 StringDeduplicator* string_deduplicator_; |
| 77 | 84 |
| 78 std::map<StackFrame, int> roots_; | 85 base::flat_map<StackFrame, int> roots_; |
| 79 std::vector<FrameNode> frames_; | 86 std::deque<FrameNode> frames_; |
| 80 size_t last_exported_index_; | 87 size_t last_exported_index_; |
| 81 | 88 |
| 89 // {backtrace_hash -> frame_index} map for finding backtraces that are |
| 90 // already added. Backtraces themselves are not stored in the map, instead |
| 91 // Match() is used on the found frame_index to detect collisions. |
| 92 std::unordered_map<size_t, int> backtrace_lookup_table_; |
| 93 |
| 82 DISALLOW_COPY_AND_ASSIGN(StackFrameDeduplicator); | 94 DISALLOW_COPY_AND_ASSIGN(StackFrameDeduplicator); |
| 83 }; | 95 }; |
| 84 | 96 |
| 85 } // namespace trace_event | 97 } // namespace trace_event |
| 86 } // namespace base | 98 } // namespace base |
| 87 | 99 |
| 88 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_ | 100 #endif // BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_ |
| OLD | NEW |