Home | History | Annotate | Download | only in trace_event
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_
      6 #define BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/base_export.h"
     13 #include "base/macros.h"
     14 #include "base/trace_event/heap_profiler_allocation_context.h"
     15 #include "base/trace_event/trace_event_impl.h"
     16 
     17 namespace base {
     18 namespace trace_event {
     19 
     20 class TraceEventMemoryOverhead;
     21 
     22 // A data structure that allows grouping a set of backtraces in a space-
     23 // efficient manner by creating a call tree and writing it as a set of (node,
     24 // parent) pairs. The tree nodes reference both parent and children. The parent
     25 // is referenced by index into |frames_|. The children are referenced via a map
     26 // of |StackFrame|s to index into |frames_|. So there is a trie for bottum-up
     27 // lookup of a backtrace for deduplication, and a tree for compact storage in
     28 // the trace log.
     29 class BASE_EXPORT StackFrameDeduplicator : public ConvertableToTraceFormat {
     30  public:
     31   // A node in the call tree.
     32   struct FrameNode {
     33     FrameNode(StackFrame frame, int parent_frame_index);
     34     ~FrameNode();
     35 
     36     StackFrame frame;
     37 
     38     // The index of the parent stack frame in |frames_|, or -1 if there is no
     39     // parent frame (when it is at the bottom of the call stack).
     40     int parent_frame_index;
     41 
     42     // Indices into |frames_| of frames called from the current frame.
     43     std::map<StackFrame, int> children;
     44   };
     45 
     46   using ConstIterator = std::vector<FrameNode>::const_iterator;
     47 
     48   StackFrameDeduplicator();
     49 
     50   // Inserts a backtrace where |beginFrame| is a pointer to the bottom frame
     51   // (e.g. main) and |endFrame| is a pointer past the top frame (most recently
     52   // called function), and returns the index of its leaf node in |frames_|.
     53   // Returns -1 if the backtrace is empty.
     54   int Insert(const StackFrame* beginFrame, const StackFrame* endFrame);
     55 
     56   // Iterators over the frame nodes in the call tree.
     57   ConstIterator begin() const { return frames_.begin(); }
     58   ConstIterator end() const { return frames_.end(); }
     59 
     60   // Writes the |stackFrames| dictionary as defined in https://goo.gl/GerkV8 to
     61   // the trace log.
     62   void AppendAsTraceFormat(std::string* out) const override;
     63 
     64   // Estimates memory overhead including |sizeof(StackFrameDeduplicator)|.
     65   void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override;
     66 
     67  private:
     68   ~StackFrameDeduplicator() override;
     69 
     70   std::map<StackFrame, int> roots_;
     71   std::vector<FrameNode> frames_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(StackFrameDeduplicator);
     74 };
     75 
     76 }  // namespace trace_event
     77 }  // namespace base
     78 
     79 #endif  // BASE_TRACE_EVENT_HEAP_PROFILER_STACK_FRAME_DEDUPLICATOR_H_
     80