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 #include "src/profiler/allocation-tracker.h"
6
7 #include "src/execution/frames-inl.h"
8 #include "src/handles/global-handles-inl.h"
9 #include "src/objects/objects-inl.h"
10 #include "src/profiler/heap-snapshot-generator-inl.h"
11
12 namespace v8 {
13 namespace internal {
14
AllocationTraceNode(AllocationTraceTree * tree,unsigned function_info_index)15 AllocationTraceNode::AllocationTraceNode(
16 AllocationTraceTree* tree, unsigned function_info_index)
17 : tree_(tree),
18 function_info_index_(function_info_index),
19 total_size_(0),
20 allocation_count_(0),
21 id_(tree->next_node_id()) {
22 }
23
24
~AllocationTraceNode()25 AllocationTraceNode::~AllocationTraceNode() {
26 for (AllocationTraceNode* node : children_) delete node;
27 }
28
29
FindChild(unsigned function_info_index)30 AllocationTraceNode* AllocationTraceNode::FindChild(
31 unsigned function_info_index) {
32 for (AllocationTraceNode* node : children_) {
33 if (node->function_info_index() == function_info_index) return node;
34 }
35 return nullptr;
36 }
37
38
FindOrAddChild(unsigned function_info_index)39 AllocationTraceNode* AllocationTraceNode::FindOrAddChild(
40 unsigned function_info_index) {
41 AllocationTraceNode* child = FindChild(function_info_index);
42 if (child == nullptr) {
43 child = new AllocationTraceNode(tree_, function_info_index);
44 children_.push_back(child);
45 }
46 return child;
47 }
48
49
AddAllocation(unsigned size)50 void AllocationTraceNode::AddAllocation(unsigned size) {
51 total_size_ += size;
52 ++allocation_count_;
53 }
54
55
Print(int indent,AllocationTracker * tracker)56 void AllocationTraceNode::Print(int indent, AllocationTracker* tracker) {
57 base::OS::Print("%10u %10u %*c", total_size_, allocation_count_, indent, ' ');
58 if (tracker != nullptr) {
59 AllocationTracker::FunctionInfo* info =
60 tracker->function_info_list()[function_info_index_];
61 base::OS::Print("%s #%u", info->name, id_);
62 } else {
63 base::OS::Print("%u #%u", function_info_index_, id_);
64 }
65 base::OS::Print("\n");
66 indent += 2;
67 for (AllocationTraceNode* node : children_) {
68 node->Print(indent, tracker);
69 }
70 }
71
72
AllocationTraceTree()73 AllocationTraceTree::AllocationTraceTree()
74 : next_node_id_(1),
75 root_(this, 0) {
76 }
77
AddPathFromEnd(const base::Vector<unsigned> & path)78 AllocationTraceNode* AllocationTraceTree::AddPathFromEnd(
79 const base::Vector<unsigned>& path) {
80 AllocationTraceNode* node = root();
81 for (unsigned* entry = path.begin() + path.length() - 1;
82 entry != path.begin() - 1; --entry) {
83 node = node->FindOrAddChild(*entry);
84 }
85 return node;
86 }
87
Print(AllocationTracker * tracker)88 void AllocationTraceTree::Print(AllocationTracker* tracker) {
89 base::OS::Print("[AllocationTraceTree:]\n");
90 base::OS::Print("Total size | Allocation count | Function id | id\n");
91 root()->Print(0, tracker);
92 }
93
FunctionInfo()94 AllocationTracker::FunctionInfo::FunctionInfo()
95 : name(""),
96 function_id(0),
97 script_name(""),
98 script_id(0),
99 line(-1),
100 column(-1) {
101 }
102
103
AddRange(Address start,int size,unsigned trace_node_id)104 void AddressToTraceMap::AddRange(Address start, int size,
105 unsigned trace_node_id) {
106 Address end = start + size;
107 RemoveRange(start, end);
108
109 RangeStack new_range(start, trace_node_id);
110 ranges_.insert(RangeMap::value_type(end, new_range));
111 }
112
113
GetTraceNodeId(Address addr)114 unsigned AddressToTraceMap::GetTraceNodeId(Address addr) {
115 RangeMap::const_iterator it = ranges_.upper_bound(addr);
116 if (it == ranges_.end()) return 0;
117 if (it->second.start <= addr) {
118 return it->second.trace_node_id;
119 }
120 return 0;
121 }
122
123
MoveObject(Address from,Address to,int size)124 void AddressToTraceMap::MoveObject(Address from, Address to, int size) {
125 unsigned trace_node_id = GetTraceNodeId(from);
126 if (trace_node_id == 0) return;
127 RemoveRange(from, from + size);
128 AddRange(to, size, trace_node_id);
129 }
130
131
Clear()132 void AddressToTraceMap::Clear() {
133 ranges_.clear();
134 }
135
136
Print()137 void AddressToTraceMap::Print() {
138 PrintF("[AddressToTraceMap (%zu): \n", ranges_.size());
139 for (RangeMap::iterator it = ranges_.begin(); it != ranges_.end(); ++it) {
140 PrintF("[%p - %p] => %u\n", reinterpret_cast<void*>(it->second.start),
141 reinterpret_cast<void*>(it->first), it->second.trace_node_id);
142 }
143 PrintF("]\n");
144 }
145
146
RemoveRange(Address start,Address end)147 void AddressToTraceMap::RemoveRange(Address start, Address end) {
148 RangeMap::iterator it = ranges_.upper_bound(start);
149 if (it == ranges_.end()) return;
150
151 RangeStack prev_range(0, 0);
152
153 RangeMap::iterator to_remove_begin = it;
154 if (it->second.start < start) {
155 prev_range = it->second;
156 }
157 do {
158 if (it->first > end) {
159 if (it->second.start < end) {
160 it->second.start = end;
161 }
162 break;
163 }
164 ++it;
165 } while (it != ranges_.end());
166
167 ranges_.erase(to_remove_begin, it);
168
169 if (prev_range.start != 0) {
170 ranges_.insert(RangeMap::value_type(start, prev_range));
171 }
172 }
173
AllocationTracker(HeapObjectsMap * ids,StringsStorage * names)174 AllocationTracker::AllocationTracker(HeapObjectsMap* ids, StringsStorage* names)
175 : ids_(ids),
176 names_(names),
177 id_to_function_info_index_(),
178 info_index_for_other_state_(0) {
179 FunctionInfo* info = new FunctionInfo();
180 info->name = "(root)";
181 function_info_list_.push_back(info);
182 }
183
184
~AllocationTracker()185 AllocationTracker::~AllocationTracker() {
186 for (UnresolvedLocation* location : unresolved_locations_) delete location;
187 for (FunctionInfo* info : function_info_list_) delete info;
188 }
189
190
PrepareForSerialization()191 void AllocationTracker::PrepareForSerialization() {
192 for (UnresolvedLocation* location : unresolved_locations_) {
193 location->Resolve();
194 delete location;
195 }
196 unresolved_locations_.clear();
197 unresolved_locations_.shrink_to_fit();
198 }
199
200
AllocationEvent(Address addr,int size)201 void AllocationTracker::AllocationEvent(Address addr, int size) {
202 DisallowGarbageCollection no_gc;
203 Heap* heap = ids_->heap();
204
205 // Mark the new block as FreeSpace to make sure the heap is iterable
206 // while we are capturing stack trace.
207 heap->CreateFillerObjectAt(addr, size, ClearRecordedSlots::kNo);
208
209 Isolate* isolate = Isolate::FromHeap(heap);
210 int length = 0;
211 JavaScriptFrameIterator it(isolate);
212 while (!it.done() && length < kMaxAllocationTraceLength) {
213 JavaScriptFrame* frame = it.frame();
214 SharedFunctionInfo shared = frame->function().shared();
215 SnapshotObjectId id =
216 ids_->FindOrAddEntry(shared.address(), shared.Size(), false);
217 allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
218 it.Advance();
219 }
220 if (length == 0) {
221 unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
222 if (index != 0) {
223 allocation_trace_buffer_[length++] = index;
224 }
225 }
226 AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
227 base::Vector<unsigned>(allocation_trace_buffer_, length));
228 top_node->AddAllocation(size);
229
230 address_to_trace_.AddRange(addr, size, top_node->id());
231 }
232
233
SnapshotObjectIdHash(SnapshotObjectId id)234 static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
235 return ComputeUnseededHash(static_cast<uint32_t>(id));
236 }
237
AddFunctionInfo(SharedFunctionInfo shared,SnapshotObjectId id)238 unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo shared,
239 SnapshotObjectId id) {
240 base::HashMap::Entry* entry = id_to_function_info_index_.LookupOrInsert(
241 reinterpret_cast<void*>(id), SnapshotObjectIdHash(id));
242 if (entry->value == nullptr) {
243 FunctionInfo* info = new FunctionInfo();
244 info->name = names_->GetCopy(shared.DebugNameCStr().get());
245 info->function_id = id;
246 if (shared.script().IsScript()) {
247 Script script = Script::cast(shared.script());
248 if (script.name().IsName()) {
249 Name name = Name::cast(script.name());
250 info->script_name = names_->GetName(name);
251 }
252 info->script_id = script.id();
253 // Converting start offset into line and column may cause heap
254 // allocations so we postpone them until snapshot serialization.
255 unresolved_locations_.push_back(
256 new UnresolvedLocation(script, shared.StartPosition(), info));
257 }
258 entry->value = reinterpret_cast<void*>(function_info_list_.size());
259 function_info_list_.push_back(info);
260 }
261 return static_cast<unsigned>(reinterpret_cast<intptr_t>((entry->value)));
262 }
263
functionInfoIndexForVMState(StateTag state)264 unsigned AllocationTracker::functionInfoIndexForVMState(StateTag state) {
265 if (state != OTHER) return 0;
266 if (info_index_for_other_state_ == 0) {
267 FunctionInfo* info = new FunctionInfo();
268 info->name = "(V8 API)";
269 info_index_for_other_state_ =
270 static_cast<unsigned>(function_info_list_.size());
271 function_info_list_.push_back(info);
272 }
273 return info_index_for_other_state_;
274 }
275
UnresolvedLocation(Script script,int start,FunctionInfo * info)276 AllocationTracker::UnresolvedLocation::UnresolvedLocation(Script script,
277 int start,
278 FunctionInfo* info)
279 : start_position_(start), info_(info) {
280 script_ = script.GetIsolate()->global_handles()->Create(script);
281 GlobalHandles::MakeWeak(script_.location(), this, &HandleWeakScript,
282 v8::WeakCallbackType::kParameter);
283 }
284
~UnresolvedLocation()285 AllocationTracker::UnresolvedLocation::~UnresolvedLocation() {
286 if (!script_.is_null()) {
287 GlobalHandles::Destroy(script_.location());
288 }
289 }
290
291
Resolve()292 void AllocationTracker::UnresolvedLocation::Resolve() {
293 if (script_.is_null()) return;
294 HandleScope scope(script_->GetIsolate());
295 info_->line = Script::GetLineNumber(script_, start_position_);
296 info_->column = Script::GetColumnNumber(script_, start_position_);
297 }
298
HandleWeakScript(const v8::WeakCallbackInfo<void> & data)299 void AllocationTracker::UnresolvedLocation::HandleWeakScript(
300 const v8::WeakCallbackInfo<void>& data) {
301 UnresolvedLocation* loc =
302 reinterpret_cast<UnresolvedLocation*>(data.GetParameter());
303 GlobalHandles::Destroy(loc->script_.location());
304 loc->script_ = Handle<Script>::null();
305 }
306
307
308 } // namespace internal
309 } // namespace v8
310