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