Home | History | Annotate | Download | only in profiler
      1 // Copyright 2013 the V8 project 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 V8_PROFILER_ALLOCATION_TRACKER_H_
      6 #define V8_PROFILER_ALLOCATION_TRACKER_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "include/v8-profiler.h"
     12 #include "src/base/hashmap.h"
     13 #include "src/handles.h"
     14 #include "src/vector.h"
     15 
     16 namespace v8 {
     17 namespace internal {
     18 
     19 // Forward declarations.
     20 class AllocationTraceTree;
     21 class AllocationTracker;
     22 class HeapObjectsMap;
     23 class SharedFunctionInfo;
     24 class StringsStorage;
     25 
     26 class AllocationTraceNode {
     27  public:
     28   AllocationTraceNode(AllocationTraceTree* tree,
     29                       unsigned function_info_index);
     30   ~AllocationTraceNode();
     31   AllocationTraceNode* FindChild(unsigned function_info_index);
     32   AllocationTraceNode* FindOrAddChild(unsigned function_info_index);
     33   void AddAllocation(unsigned size);
     34 
     35   unsigned function_info_index() const { return function_info_index_; }
     36   unsigned allocation_size() const { return total_size_; }
     37   unsigned allocation_count() const { return allocation_count_; }
     38   unsigned id() const { return id_; }
     39   const std::vector<AllocationTraceNode*>& children() const {
     40     return children_;
     41   }
     42 
     43   void Print(int indent, AllocationTracker* tracker);
     44 
     45  private:
     46   AllocationTraceTree* tree_;
     47   unsigned function_info_index_;
     48   unsigned total_size_;
     49   unsigned allocation_count_;
     50   unsigned id_;
     51   std::vector<AllocationTraceNode*> children_;
     52 
     53   DISALLOW_COPY_AND_ASSIGN(AllocationTraceNode);
     54 };
     55 
     56 
     57 class AllocationTraceTree {
     58  public:
     59   AllocationTraceTree();
     60   ~AllocationTraceTree();
     61   AllocationTraceNode* AddPathFromEnd(const Vector<unsigned>& path);
     62   AllocationTraceNode* root() { return &root_; }
     63   unsigned next_node_id() { return next_node_id_++; }
     64   void Print(AllocationTracker* tracker);
     65 
     66  private:
     67   unsigned next_node_id_;
     68   AllocationTraceNode root_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(AllocationTraceTree);
     71 };
     72 
     73 
     74 class AddressToTraceMap {
     75  public:
     76   void AddRange(Address addr, int size, unsigned node_id);
     77   unsigned GetTraceNodeId(Address addr);
     78   void MoveObject(Address from, Address to, int size);
     79   void Clear();
     80   size_t size() { return ranges_.size(); }
     81   void Print();
     82 
     83  private:
     84   struct RangeStack {
     85     RangeStack(Address start, unsigned node_id)
     86         : start(start), trace_node_id(node_id) {}
     87     Address start;
     88     unsigned trace_node_id;
     89   };
     90   // [start, end) -> trace
     91   typedef std::map<Address, RangeStack> RangeMap;
     92 
     93   void RemoveRange(Address start, Address end);
     94 
     95   RangeMap ranges_;
     96 };
     97 
     98 class AllocationTracker {
     99  public:
    100   struct FunctionInfo {
    101     FunctionInfo();
    102     const char* name;
    103     SnapshotObjectId function_id;
    104     const char* script_name;
    105     int script_id;
    106     int line;
    107     int column;
    108   };
    109 
    110   AllocationTracker(HeapObjectsMap* ids, StringsStorage* names);
    111   ~AllocationTracker();
    112 
    113   void PrepareForSerialization();
    114   void AllocationEvent(Address addr, int size);
    115 
    116   AllocationTraceTree* trace_tree() { return &trace_tree_; }
    117   const std::vector<FunctionInfo*>& function_info_list() const {
    118     return function_info_list_;
    119   }
    120   AddressToTraceMap* address_to_trace() { return &address_to_trace_; }
    121 
    122  private:
    123   unsigned AddFunctionInfo(SharedFunctionInfo* info, SnapshotObjectId id);
    124   unsigned functionInfoIndexForVMState(StateTag state);
    125 
    126   class UnresolvedLocation {
    127    public:
    128     UnresolvedLocation(Script* script, int start, FunctionInfo* info);
    129     ~UnresolvedLocation();
    130     void Resolve();
    131 
    132    private:
    133     static void HandleWeakScript(const v8::WeakCallbackInfo<void>& data);
    134 
    135     Handle<Script> script_;
    136     int start_position_;
    137     FunctionInfo* info_;
    138   };
    139 
    140   static const int kMaxAllocationTraceLength = 64;
    141   HeapObjectsMap* ids_;
    142   StringsStorage* names_;
    143   AllocationTraceTree trace_tree_;
    144   unsigned allocation_trace_buffer_[kMaxAllocationTraceLength];
    145   std::vector<FunctionInfo*> function_info_list_;
    146   base::HashMap id_to_function_info_index_;
    147   std::vector<UnresolvedLocation*> unresolved_locations_;
    148   unsigned info_index_for_other_state_;
    149   AddressToTraceMap address_to_trace_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(AllocationTracker);
    152 };
    153 
    154 }  // namespace internal
    155 }  // namespace v8
    156 
    157 #endif  // V8_PROFILER_ALLOCATION_TRACKER_H_
    158