allocation-tracker.cc 9.09 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/profiler/allocation-tracker.h"
6

7
#include "src/frames-inl.h"
8
#include "src/global-handles.h"
9
#include "src/objects-inl.h"
10
#include "src/profiler/heap-snapshot-generator-inl.h"
11 12 13 14 15

namespace v8 {
namespace internal {

AllocationTraceNode::AllocationTraceNode(
16
    AllocationTraceTree* tree, unsigned function_info_index)
17
    : tree_(tree),
18
      function_info_index_(function_info_index),
19 20 21 22 23 24 25
      total_size_(0),
      allocation_count_(0),
      id_(tree->next_node_id()) {
}


AllocationTraceNode::~AllocationTraceNode() {
26
  for (AllocationTraceNode* node : children_) delete node;
27 28 29
}


30 31
AllocationTraceNode* AllocationTraceNode::FindChild(
    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
}


39 40 41
AllocationTraceNode* AllocationTraceNode::FindOrAddChild(
    unsigned function_info_index) {
  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 47 48 49 50 51 52 53 54 55 56
  }
  return child;
}


void AllocationTraceNode::AddAllocation(unsigned size) {
  total_size_ += size;
  ++allocation_count_;
}


void AllocationTraceNode::Print(int indent, AllocationTracker* tracker) {
57
  base::OS::Print("%10u %10u %*c", total_size_, allocation_count_, indent, ' ');
58
  if (tracker != nullptr) {
59 60
    AllocationTracker::FunctionInfo* info =
        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 68
  for (AllocationTraceNode* node : children_) {
    node->Print(indent, tracker);
69 70 71 72 73 74 75 76 77 78
  }
}


AllocationTraceTree::AllocationTraceTree()
    : next_node_id_(1),
      root_(this, 0) {
}

AllocationTraceNode* AllocationTraceTree::AddPathFromEnd(
79
    const Vector<unsigned>& path) {
80
  AllocationTraceNode* node = root();
81
  for (unsigned* entry = path.start() + path.length() - 1;
82 83 84 85 86 87 88 89 90
       entry != path.start() - 1;
       --entry) {
    node = node->FindOrAddChild(*entry);
  }
  return node;
}


void AllocationTraceTree::Print(AllocationTracker* tracker) {
91 92
  base::OS::Print("[AllocationTraceTree:]\n");
  base::OS::Print("Total size | Allocation count | Function id | id\n");
93 94 95 96 97
  root()->Print(0, tracker);
}

AllocationTracker::FunctionInfo::FunctionInfo()
    : name(""),
98
      function_id(0),
99 100 101 102 103 104 105
      script_name(""),
      script_id(0),
      line(-1),
      column(-1) {
}


106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void AddressToTraceMap::AddRange(Address start, int size,
                                 unsigned trace_node_id) {
  Address end = start + size;
  RemoveRange(start, end);

  RangeStack new_range(start, trace_node_id);
  ranges_.insert(RangeMap::value_type(end, new_range));
}


unsigned AddressToTraceMap::GetTraceNodeId(Address addr) {
  RangeMap::const_iterator it = ranges_.upper_bound(addr);
  if (it == ranges_.end()) return 0;
  if (it->second.start <= addr) {
    return it->second.trace_node_id;
  }
  return 0;
}


void AddressToTraceMap::MoveObject(Address from, Address to, int size) {
  unsigned trace_node_id = GetTraceNodeId(from);
  if (trace_node_id == 0) return;
  RemoveRange(from, from + size);
  AddRange(to, size, trace_node_id);
}


void AddressToTraceMap::Clear() {
  ranges_.clear();
}


void AddressToTraceMap::Print() {
jfb's avatar
jfb committed
140
  PrintF("[AddressToTraceMap (%" PRIuS "): \n", ranges_.size());
141
  for (RangeMap::iterator it = ranges_.begin(); it != ranges_.end(); ++it) {
142 143
    PrintF("[%p - %p] => %u\n", reinterpret_cast<void*>(it->second.start),
           reinterpret_cast<void*>(it->first), it->second.trace_node_id);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  }
  PrintF("]\n");
}


void AddressToTraceMap::RemoveRange(Address start, Address end) {
  RangeMap::iterator it = ranges_.upper_bound(start);
  if (it == ranges_.end()) return;

  RangeStack prev_range(0, 0);

  RangeMap::iterator to_remove_begin = it;
  if (it->second.start < start) {
    prev_range = it->second;
  }
  do {
    if (it->first > end) {
      if (it->second.start < end) {
        it->second.start = end;
      }
      break;
    }
    ++it;
167
  } while (it != ranges_.end());
168 169 170 171 172 173 174 175

  ranges_.erase(to_remove_begin, it);

  if (prev_range.start != 0) {
    ranges_.insert(RangeMap::value_type(start, prev_range));
  }
}

lpy's avatar
lpy committed
176
AllocationTracker::AllocationTracker(HeapObjectsMap* ids, StringsStorage* names)
177 178
    : ids_(ids),
      names_(names),
179
      id_to_function_info_index_(),
180 181 182
      info_index_for_other_state_(0) {
  FunctionInfo* info = new FunctionInfo();
  info->name = "(root)";
183
  function_info_list_.push_back(info);
184 185 186 187
}


AllocationTracker::~AllocationTracker() {
188 189
  for (UnresolvedLocation* location : unresolved_locations_) delete location;
  for (FunctionInfo* info : function_info_list_) delete info;
190 191 192 193
}


void AllocationTracker::PrepareForSerialization() {
194 195 196
  for (UnresolvedLocation* location : unresolved_locations_) {
    location->Resolve();
    delete location;
197
  }
198 199
  unresolved_locations_.clear();
  unresolved_locations_.shrink_to_fit();
200 201 202
}


203
void AllocationTracker::AllocationEvent(Address addr, int size) {
204 205 206 207 208
  DisallowHeapAllocation no_allocation;
  Heap* heap = ids_->heap();

  // Mark the new block as FreeSpace to make sure the heap is iterable
  // while we are capturing stack trace.
209
  heap->CreateFillerObjectAt(addr, size, ClearRecordedSlots::kNo);
210 211 212

  Isolate* isolate = heap->isolate();
  int length = 0;
213
  JavaScriptFrameIterator it(isolate);
214
  while (!it.done() && length < kMaxAllocationTraceLength) {
215
    JavaScriptFrame* frame = it.frame();
216
    SharedFunctionInfo* shared = frame->function()->shared();
217 218
    SnapshotObjectId id = ids_->FindOrAddEntry(
        shared->address(), shared->Size(), false);
219
    allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
220 221
    it.Advance();
  }
222 223 224 225 226 227
  if (length == 0) {
    unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
    if (index != 0) {
      allocation_trace_buffer_[length++] = index;
    }
  }
228
  AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
229
      Vector<unsigned>(allocation_trace_buffer_, length));
230
  top_node->AddAllocation(size);
231 232

  address_to_trace_.AddRange(addr, size, top_node->id());
233 234 235 236
}


static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
237
  return ComputeIntegerHash(static_cast<uint32_t>(id));
238 239 240
}


241 242
unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
                                            SnapshotObjectId id) {
lpy's avatar
lpy committed
243
  base::HashMap::Entry* entry = id_to_function_info_index_.LookupOrInsert(
244
      reinterpret_cast<void*>(id), SnapshotObjectIdHash(id));
245
  if (entry->value == nullptr) {
246
    FunctionInfo* info = new FunctionInfo();
247
    info->name = names_->GetName(shared->DebugName());
248
    info->function_id = id;
249 250 251 252 253 254
    if (shared->script()->IsScript()) {
      Script* script = Script::cast(shared->script());
      if (script->name()->IsName()) {
        Name* name = Name::cast(script->name());
        info->script_name = names_->GetName(name);
      }
255
      info->script_id = script->id();
256 257
      // Converting start offset into line and column may cause heap
      // allocations so we postpone them until snapshot serialization.
258
      unresolved_locations_.push_back(
259
          new UnresolvedLocation(script, shared->StartPosition(), info));
260
    }
261 262
    entry->value = reinterpret_cast<void*>(function_info_list_.size());
    function_info_list_.push_back(info);
263 264 265 266 267 268 269 270 271 272
  }
  return static_cast<unsigned>(reinterpret_cast<intptr_t>((entry->value)));
}


unsigned AllocationTracker::functionInfoIndexForVMState(StateTag state) {
  if (state != OTHER) return 0;
  if (info_index_for_other_state_ == 0) {
    FunctionInfo* info = new FunctionInfo();
    info->name = "(V8 API)";
273 274 275
    info_index_for_other_state_ =
        static_cast<unsigned>(function_info_list_.size());
    function_info_list_.push_back(info);
276
  }
277
  return info_index_for_other_state_;
278 279 280 281 282 283 284
}


AllocationTracker::UnresolvedLocation::UnresolvedLocation(
    Script* script, int start, FunctionInfo* info)
    : start_position_(start),
      info_(info) {
285
  script_ = script->GetIsolate()->global_handles()->Create(script);
286 287
  GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this,
                          &HandleWeakScript, v8::WeakCallbackType::kParameter);
288 289 290 291 292
}


AllocationTracker::UnresolvedLocation::~UnresolvedLocation() {
  if (!script_.is_null()) {
293
    GlobalHandles::Destroy(reinterpret_cast<Object**>(script_.location()));
294 295 296 297 298 299
  }
}


void AllocationTracker::UnresolvedLocation::Resolve() {
  if (script_.is_null()) return;
300
  HandleScope scope(script_->GetIsolate());
301 302
  info_->line = Script::GetLineNumber(script_, start_position_);
  info_->column = Script::GetColumnNumber(script_, start_position_);
303 304 305
}

void AllocationTracker::UnresolvedLocation::HandleWeakScript(
306
    const v8::WeakCallbackInfo<void>& data) {
307 308 309 310
  UnresolvedLocation* loc =
      reinterpret_cast<UnresolvedLocation*>(data.GetParameter());
  GlobalHandles::Destroy(reinterpret_cast<Object**>(loc->script_.location()));
  loc->script_ = Handle<Script>::null();
311 312 313
}


314 315
}  // namespace internal
}  // namespace v8