• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils/vector.h"
14 #include "src/handles/handles.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 
function_info_index()35   unsigned function_info_index() const { return function_info_index_; }
allocation_size()36   unsigned allocation_size() const { return total_size_; }
allocation_count()37   unsigned allocation_count() const { return allocation_count_; }
id()38   unsigned id() const { return id_; }
children()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() = default;
61   AllocationTraceNode* AddPathFromEnd(const Vector<unsigned>& path);
root()62   AllocationTraceNode* root() { return &root_; }
next_node_id()63   unsigned next_node_id() { return next_node_id_++; }
64   V8_EXPORT_PRIVATE 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 class V8_EXPORT_PRIVATE AddressToTraceMap {
74  public:
75   void AddRange(Address addr, int size, unsigned node_id);
76   unsigned GetTraceNodeId(Address addr);
77   void MoveObject(Address from, Address to, int size);
78   void Clear();
size()79   size_t size() { return ranges_.size(); }
80   void Print();
81 
82  private:
83   struct RangeStack {
RangeStackRangeStack84     RangeStack(Address start, unsigned node_id)
85         : start(start), trace_node_id(node_id) {}
86     Address start;
87     unsigned trace_node_id;
88   };
89   // [start, end) -> trace
90   using RangeMap = std::map<Address, RangeStack>;
91 
92   void RemoveRange(Address start, Address end);
93 
94   RangeMap ranges_;
95 };
96 
97 class AllocationTracker {
98  public:
99   struct FunctionInfo {
100     FunctionInfo();
101     const char* name;
102     SnapshotObjectId function_id;
103     const char* script_name;
104     int script_id;
105     int line;
106     int column;
107   };
108 
109   AllocationTracker(HeapObjectsMap* ids, StringsStorage* names);
110   ~AllocationTracker();
111 
112   V8_EXPORT_PRIVATE void PrepareForSerialization();
113   void AllocationEvent(Address addr, int size);
114 
trace_tree()115   AllocationTraceTree* trace_tree() { return &trace_tree_; }
function_info_list()116   const std::vector<FunctionInfo*>& function_info_list() const {
117     return function_info_list_;
118   }
address_to_trace()119   AddressToTraceMap* address_to_trace() { return &address_to_trace_; }
120 
121  private:
122   unsigned AddFunctionInfo(SharedFunctionInfo info, SnapshotObjectId id);
123   unsigned functionInfoIndexForVMState(StateTag state);
124 
125   class UnresolvedLocation {
126    public:
127     UnresolvedLocation(Script script, int start, FunctionInfo* info);
128     ~UnresolvedLocation();
129     void Resolve();
130 
131    private:
132     static void HandleWeakScript(const v8::WeakCallbackInfo<void>& data);
133 
134     Handle<Script> script_;
135     int start_position_;
136     FunctionInfo* info_;
137   };
138 
139   static const int kMaxAllocationTraceLength = 64;
140   HeapObjectsMap* ids_;
141   StringsStorage* names_;
142   AllocationTraceTree trace_tree_;
143   unsigned allocation_trace_buffer_[kMaxAllocationTraceLength];
144   std::vector<FunctionInfo*> function_info_list_;
145   base::HashMap id_to_function_info_index_;
146   std::vector<UnresolvedLocation*> unresolved_locations_;
147   unsigned info_index_for_other_state_;
148   AddressToTraceMap address_to_trace_;
149 
150   DISALLOW_COPY_AND_ASSIGN(AllocationTracker);
151 };
152 
153 }  // namespace internal
154 }  // namespace v8
155 
156 #endif  // V8_PROFILER_ALLOCATION_TRACKER_H_
157