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
11namespace v8 {
12namespace internal {
13
14AllocationTraceNode::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
24AllocationTraceNode::~AllocationTraceNode() {
25  for (int i = 0; i < children_.length(); i++) delete children_[i];
26}
27
28
29AllocationTraceNode* 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
39AllocationTraceNode* 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
50void AllocationTraceNode::AddAllocation(unsigned size) {
51  total_size_ += size;
52  ++allocation_count_;
53}
54
55
56void 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
73AllocationTraceTree::AllocationTraceTree()
74    : next_node_id_(1),
75      root_(this, 0) {
76}
77
78
79AllocationTraceTree::~AllocationTraceTree() {
80}
81
82
83AllocationTraceNode* 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
95void 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
102void AllocationTracker::DeleteUnresolvedLocation(
103    UnresolvedLocation** location) {
104  delete *location;
105}
106
107
108AllocationTracker::FunctionInfo::FunctionInfo()
109    : name(""),
110      function_id(0),
111      script_name(""),
112      script_id(0),
113      line(-1),
114      column(-1) {
115}
116
117
118void 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
128unsigned 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
138void 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
146void AddressToTraceMap::Clear() {
147  ranges_.clear();
148}
149
150
151void AddressToTraceMap::Print() {
152  PrintF("[AddressToTraceMap (%" PRIuS "): \n", ranges_.size());
153  for (RangeMap::iterator it = ranges_.begin(); it != ranges_.end(); ++it) {
154    PrintF("[%p - %p] => %u\n", static_cast<void*>(it->second.start),
155           static_cast<void*>(it->first), it->second.trace_node_id);
156  }
157  PrintF("]\n");
158}
159
160
161void 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
189void AllocationTracker::DeleteFunctionInfo(FunctionInfo** info) {
190    delete *info;
191}
192
193AllocationTracker::AllocationTracker(HeapObjectsMap* ids, StringsStorage* names)
194    : ids_(ids),
195      names_(names),
196      id_to_function_info_index_(),
197      info_index_for_other_state_(0) {
198  FunctionInfo* info = new FunctionInfo();
199  info->name = "(root)";
200  function_info_list_.Add(info);
201}
202
203
204AllocationTracker::~AllocationTracker() {
205  unresolved_locations_.Iterate(DeleteUnresolvedLocation);
206  function_info_list_.Iterate(&DeleteFunctionInfo);
207}
208
209
210void AllocationTracker::PrepareForSerialization() {
211  List<UnresolvedLocation*> copy(unresolved_locations_.length());
212  copy.AddAll(unresolved_locations_);
213  unresolved_locations_.Clear();
214  for (int i = 0; i < copy.length(); i++) {
215    copy[i]->Resolve();
216    delete copy[i];
217  }
218}
219
220
221void AllocationTracker::AllocationEvent(Address addr, int size) {
222  DisallowHeapAllocation no_allocation;
223  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  heap->CreateFillerObjectAt(addr, size, ClearRecordedSlots::kNo);
228
229  Isolate* isolate = heap->isolate();
230  int length = 0;
231  JavaScriptFrameIterator it(isolate);
232  while (!it.done() && length < kMaxAllocationTraceLength) {
233    JavaScriptFrame* frame = it.frame();
234    SharedFunctionInfo* shared = frame->function()->shared();
235    SnapshotObjectId id = ids_->FindOrAddEntry(
236        shared->address(), shared->Size(), false);
237    allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
238    it.Advance();
239  }
240  if (length == 0) {
241    unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
242    if (index != 0) {
243      allocation_trace_buffer_[length++] = index;
244    }
245  }
246  AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
247      Vector<unsigned>(allocation_trace_buffer_, length));
248  top_node->AddAllocation(size);
249
250  address_to_trace_.AddRange(addr, size, top_node->id());
251}
252
253
254static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
255  return ComputeIntegerHash(static_cast<uint32_t>(id),
256                            v8::internal::kZeroHashSeed);
257}
258
259
260unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
261                                            SnapshotObjectId id) {
262  base::HashMap::Entry* entry = id_to_function_info_index_.LookupOrInsert(
263      reinterpret_cast<void*>(id), SnapshotObjectIdHash(id));
264  if (entry->value == NULL) {
265    FunctionInfo* info = new FunctionInfo();
266    info->name = names_->GetFunctionName(shared->DebugName());
267    info->function_id = id;
268    if (shared->script()->IsScript()) {
269      Script* script = Script::cast(shared->script());
270      if (script->name()->IsName()) {
271        Name* name = Name::cast(script->name());
272        info->script_name = names_->GetName(name);
273      }
274      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          info));
281    }
282    entry->value = reinterpret_cast<void*>(function_info_list_.length());
283    function_info_list_.Add(info);
284  }
285  return static_cast<unsigned>(reinterpret_cast<intptr_t>((entry->value)));
286}
287
288
289unsigned AllocationTracker::functionInfoIndexForVMState(StateTag state) {
290  if (state != OTHER) return 0;
291  if (info_index_for_other_state_ == 0) {
292    FunctionInfo* info = new FunctionInfo();
293    info->name = "(V8 API)";
294    info_index_for_other_state_ = function_info_list_.length();
295    function_info_list_.Add(info);
296  }
297  return info_index_for_other_state_;
298}
299
300
301AllocationTracker::UnresolvedLocation::UnresolvedLocation(
302    Script* script, int start, FunctionInfo* info)
303    : start_position_(start),
304      info_(info) {
305  script_ = Handle<Script>::cast(
306      script->GetIsolate()->global_handles()->Create(script));
307  GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this,
308                          &HandleWeakScript, v8::WeakCallbackType::kParameter);
309}
310
311
312AllocationTracker::UnresolvedLocation::~UnresolvedLocation() {
313  if (!script_.is_null()) {
314    GlobalHandles::Destroy(reinterpret_cast<Object**>(script_.location()));
315  }
316}
317
318
319void AllocationTracker::UnresolvedLocation::Resolve() {
320  if (script_.is_null()) return;
321  HandleScope scope(script_->GetIsolate());
322  info_->line = Script::GetLineNumber(script_, start_position_);
323  info_->column = Script::GetColumnNumber(script_, start_position_);
324}
325
326void AllocationTracker::UnresolvedLocation::HandleWeakScript(
327    const v8::WeakCallbackInfo<void>& data) {
328  UnresolvedLocation* loc =
329      reinterpret_cast<UnresolvedLocation*>(data.GetParameter());
330  GlobalHandles::Destroy(reinterpret_cast<Object**>(loc->script_.location()));
331  loc->script_ = Handle<Script>::null();
332}
333
334
335}  // namespace internal
336}  // namespace v8
337