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