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.
5 #ifndef V8_ALLOCATION_TRACKER_H_
6 #define V8_ALLOCATION_TRACKER_H_
15 class AllocationTraceTree;
17 class AllocationTraceNode {
19 AllocationTraceNode(AllocationTraceTree* tree,
20 unsigned function_info_index);
21 ~AllocationTraceNode();
22 AllocationTraceNode* FindChild(unsigned function_info_index);
23 AllocationTraceNode* FindOrAddChild(unsigned function_info_index);
24 void AddAllocation(unsigned size);
26 unsigned function_info_index() const { return function_info_index_; }
27 unsigned allocation_size() const { return total_size_; }
28 unsigned allocation_count() const { return allocation_count_; }
29 unsigned id() const { return id_; }
30 Vector<AllocationTraceNode*> children() const { return children_.ToVector(); }
32 void Print(int indent, AllocationTracker* tracker);
35 AllocationTraceTree* tree_;
36 unsigned function_info_index_;
38 unsigned allocation_count_;
40 List<AllocationTraceNode*> children_;
42 DISALLOW_COPY_AND_ASSIGN(AllocationTraceNode);
46 class AllocationTraceTree {
48 AllocationTraceTree();
49 ~AllocationTraceTree();
50 AllocationTraceNode* AddPathFromEnd(const Vector<unsigned>& path);
51 AllocationTraceNode* root() { return &root_; }
52 unsigned next_node_id() { return next_node_id_++; }
53 void Print(AllocationTracker* tracker);
56 unsigned next_node_id_;
57 AllocationTraceNode root_;
59 DISALLOW_COPY_AND_ASSIGN(AllocationTraceTree);
63 class AddressToTraceMap {
65 void AddRange(Address addr, int size, unsigned node_id);
66 unsigned GetTraceNodeId(Address addr);
67 void MoveObject(Address from, Address to, int size);
69 size_t size() { return ranges_.size(); }
74 RangeStack(Address start, unsigned node_id)
75 : start(start), trace_node_id(node_id) {}
77 unsigned trace_node_id;
79 // [start, end) -> trace
80 typedef std::map<Address, RangeStack> RangeMap;
82 void RemoveRange(Address start, Address end);
87 class AllocationTracker {
92 SnapshotObjectId function_id;
93 const char* script_name;
99 AllocationTracker(HeapObjectsMap* ids, StringsStorage* names);
100 ~AllocationTracker();
102 void PrepareForSerialization();
103 void AllocationEvent(Address addr, int size);
105 AllocationTraceTree* trace_tree() { return &trace_tree_; }
106 const List<FunctionInfo*>& function_info_list() const {
107 return function_info_list_;
109 AddressToTraceMap* address_to_trace() { return &address_to_trace_; }
112 unsigned AddFunctionInfo(SharedFunctionInfo* info, SnapshotObjectId id);
113 static void DeleteFunctionInfo(FunctionInfo** info);
114 unsigned functionInfoIndexForVMState(StateTag state);
116 class UnresolvedLocation {
118 UnresolvedLocation(Script* script, int start, FunctionInfo* info);
119 ~UnresolvedLocation();
123 static void HandleWeakScript(
124 const v8::WeakCallbackData<v8::Value, void>& data);
126 Handle<Script> script_;
130 static void DeleteUnresolvedLocation(UnresolvedLocation** location);
132 static const int kMaxAllocationTraceLength = 64;
133 HeapObjectsMap* ids_;
134 StringsStorage* names_;
135 AllocationTraceTree trace_tree_;
136 unsigned allocation_trace_buffer_[kMaxAllocationTraceLength];
137 List<FunctionInfo*> function_info_list_;
138 HashMap id_to_function_info_index_;
139 List<UnresolvedLocation*> unresolved_locations_;
140 unsigned info_index_for_other_state_;
141 AddressToTraceMap address_to_trace_;
143 DISALLOW_COPY_AND_ASSIGN(AllocationTracker);
146 } } // namespace v8::internal
148 #endif // V8_ALLOCATION_TRACKER_H_