Home | History | Annotate | Download | only in profiler
      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/global-handles.h"
      9 #include "src/objects-inl.h"
     10 #include "src/profiler/heap-snapshot-generator-inl.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 
     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 
     25 AllocationTraceNode::~AllocationTraceNode() {
     26   for (AllocationTraceNode* node : children_) delete node;
     27 }
     28 
     29 
     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 
     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 
     50 void AllocationTraceNode::AddAllocation(unsigned size) {
     51   total_size_ += size;
     52   ++allocation_count_;
     53 }
     54 
     55 
     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 
     73 AllocationTraceTree::AllocationTraceTree()
     74     : next_node_id_(1),
     75       root_(this, 0) {
     76 }
     77 
     78 
     79 AllocationTraceTree::~AllocationTraceTree() {
     80 }
     81 
     82 
     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 
     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 AllocationTracker::FunctionInfo::FunctionInfo()
    102     : name(""),
    103       function_id(0),
    104       script_name(""),
    105       script_id(0),
    106       line(-1),
    107       column(-1) {
    108 }
    109 
    110 
    111 void AddressToTraceMap::AddRange(Address start, int size,
    112                                  unsigned trace_node_id) {
    113   Address end = start + size;
    114   RemoveRange(start, end);
    115 
    116   RangeStack new_range(start, trace_node_id);
    117   ranges_.insert(RangeMap::value_type(end, new_range));
    118 }
    119 
    120 
    121 unsigned AddressToTraceMap::GetTraceNodeId(Address addr) {
    122   RangeMap::const_iterator it = ranges_.upper_bound(addr);
    123   if (it == ranges_.end()) return 0;
    124   if (it->second.start <= addr) {
    125     return it->second.trace_node_id;
    126   }
    127   return 0;
    128 }
    129 
    130 
    131 void AddressToTraceMap::MoveObject(Address from, Address to, int size) {
    132   unsigned trace_node_id = GetTraceNodeId(from);
    133   if (trace_node_id == 0) return;
    134   RemoveRange(from, from + size);
    135   AddRange(to, size, trace_node_id);
    136 }
    137 
    138 
    139 void AddressToTraceMap::Clear() {
    140   ranges_.clear();
    141 }
    142 
    143 
    144 void AddressToTraceMap::Print() {
    145   PrintF("[AddressToTraceMap (%" PRIuS "): \n", ranges_.size());
    146   for (RangeMap::iterator it = ranges_.begin(); it != ranges_.end(); ++it) {
    147     PrintF("[%p - %p] => %u\n", reinterpret_cast<void*>(it->second.start),
    148            reinterpret_cast<void*>(it->first), it->second.trace_node_id);
    149   }
    150   PrintF("]\n");
    151 }
    152 
    153 
    154 void AddressToTraceMap::RemoveRange(Address start, Address end) {
    155   RangeMap::iterator it = ranges_.upper_bound(start);
    156   if (it == ranges_.end()) return;
    157 
    158   RangeStack prev_range(0, 0);
    159 
    160   RangeMap::iterator to_remove_begin = it;
    161   if (it->second.start < start) {
    162     prev_range = it->second;
    163   }
    164   do {
    165     if (it->first > end) {
    166       if (it->second.start < end) {
    167         it->second.start = end;
    168       }
    169       break;
    170     }
    171     ++it;
    172   } while (it != ranges_.end());
    173 
    174   ranges_.erase(to_remove_begin, it);
    175 
    176   if (prev_range.start != 0) {
    177     ranges_.insert(RangeMap::value_type(start, prev_range));
    178   }
    179 }
    180 
    181 AllocationTracker::AllocationTracker(HeapObjectsMap* ids, StringsStorage* names)
    182     : ids_(ids),
    183       names_(names),
    184       id_to_function_info_index_(),
    185       info_index_for_other_state_(0) {
    186   FunctionInfo* info = new FunctionInfo();
    187   info->name = "(root)";
    188   function_info_list_.push_back(info);
    189 }
    190 
    191 
    192 AllocationTracker::~AllocationTracker() {
    193   for (UnresolvedLocation* location : unresolved_locations_) delete location;
    194   for (FunctionInfo* info : function_info_list_) delete info;
    195 }
    196 
    197 
    198 void AllocationTracker::PrepareForSerialization() {
    199   for (UnresolvedLocation* location : unresolved_locations_) {
    200     location->Resolve();
    201     delete location;
    202   }
    203   unresolved_locations_.clear();
    204   unresolved_locations_.shrink_to_fit();
    205 }
    206 
    207 
    208 void AllocationTracker::AllocationEvent(Address addr, int size) {
    209   DisallowHeapAllocation no_allocation;
    210   Heap* heap = ids_->heap();
    211 
    212   // Mark the new block as FreeSpace to make sure the heap is iterable
    213   // while we are capturing stack trace.
    214   heap->CreateFillerObjectAt(addr, size, ClearRecordedSlots::kNo);
    215 
    216   Isolate* isolate = heap->isolate();
    217   int length = 0;
    218   JavaScriptFrameIterator it(isolate);
    219   while (!it.done() && length < kMaxAllocationTraceLength) {
    220     JavaScriptFrame* frame = it.frame();
    221     SharedFunctionInfo* shared = frame->function()->shared();
    222     SnapshotObjectId id = ids_->FindOrAddEntry(
    223         shared->address(), shared->Size(), false);
    224     allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
    225     it.Advance();
    226   }
    227   if (length == 0) {
    228     unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
    229     if (index != 0) {
    230       allocation_trace_buffer_[length++] = index;
    231     }
    232   }
    233   AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
    234       Vector<unsigned>(allocation_trace_buffer_, length));
    235   top_node->AddAllocation(size);
    236 
    237   address_to_trace_.AddRange(addr, size, top_node->id());
    238 }
    239 
    240 
    241 static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
    242   return ComputeIntegerHash(static_cast<uint32_t>(id));
    243 }
    244 
    245 
    246 unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
    247                                             SnapshotObjectId id) {
    248   base::HashMap::Entry* entry = id_to_function_info_index_.LookupOrInsert(
    249       reinterpret_cast<void*>(id), SnapshotObjectIdHash(id));
    250   if (entry->value == nullptr) {
    251     FunctionInfo* info = new FunctionInfo();
    252     info->name = names_->GetName(shared->DebugName());
    253     info->function_id = id;
    254     if (shared->script()->IsScript()) {
    255       Script* script = Script::cast(shared->script());
    256       if (script->name()->IsName()) {
    257         Name* name = Name::cast(script->name());
    258         info->script_name = names_->GetName(name);
    259       }
    260       info->script_id = script->id();
    261       // Converting start offset into line and column may cause heap
    262       // allocations so we postpone them until snapshot serialization.
    263       unresolved_locations_.push_back(
    264           new UnresolvedLocation(script, shared->StartPosition(), info));
    265     }
    266     entry->value = reinterpret_cast<void*>(function_info_list_.size());
    267     function_info_list_.push_back(info);
    268   }
    269   return static_cast<unsigned>(reinterpret_cast<intptr_t>((entry->value)));
    270 }
    271 
    272 
    273 unsigned AllocationTracker::functionInfoIndexForVMState(StateTag state) {
    274   if (state != OTHER) return 0;
    275   if (info_index_for_other_state_ == 0) {
    276     FunctionInfo* info = new FunctionInfo();
    277     info->name = "(V8 API)";
    278     info_index_for_other_state_ =
    279         static_cast<unsigned>(function_info_list_.size());
    280     function_info_list_.push_back(info);
    281   }
    282   return info_index_for_other_state_;
    283 }
    284 
    285 
    286 AllocationTracker::UnresolvedLocation::UnresolvedLocation(
    287     Script* script, int start, FunctionInfo* info)
    288     : start_position_(start),
    289       info_(info) {
    290   script_ = script->GetIsolate()->global_handles()->Create(script);
    291   GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), this,
    292                           &HandleWeakScript, v8::WeakCallbackType::kParameter);
    293 }
    294 
    295 
    296 AllocationTracker::UnresolvedLocation::~UnresolvedLocation() {
    297   if (!script_.is_null()) {
    298     GlobalHandles::Destroy(reinterpret_cast<Object**>(script_.location()));
    299   }
    300 }
    301 
    302 
    303 void AllocationTracker::UnresolvedLocation::Resolve() {
    304   if (script_.is_null()) return;
    305   HandleScope scope(script_->GetIsolate());
    306   info_->line = Script::GetLineNumber(script_, start_position_);
    307   info_->column = Script::GetColumnNumber(script_, start_position_);
    308 }
    309 
    310 void AllocationTracker::UnresolvedLocation::HandleWeakScript(
    311     const v8::WeakCallbackInfo<void>& data) {
    312   UnresolvedLocation* loc =
    313       reinterpret_cast<UnresolvedLocation*>(data.GetParameter());
    314   GlobalHandles::Destroy(reinterpret_cast<Object**>(loc->script_.location()));
    315   loc->script_ = Handle<Script>::null();
    316 }
    317 
    318 
    319 }  // namespace internal
    320 }  // namespace v8
    321