2013-11-14 12:13:26 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-11-14 12:13:26 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2013-11-14 12:13:26 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation-tracker.h"
|
|
|
|
#include "src/frames-inl.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/heap-snapshot-generator.h"
|
2013-11-14 12:13:26 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
AllocationTraceNode::AllocationTraceNode(
|
2014-03-07 11:32:01 +00:00
|
|
|
AllocationTraceTree* tree, unsigned function_info_index)
|
2013-11-14 12:13:26 +00:00
|
|
|
: tree_(tree),
|
2014-03-07 11:32:01 +00:00
|
|
|
function_info_index_(function_info_index),
|
2013-11-14 12:13:26 +00:00
|
|
|
total_size_(0),
|
|
|
|
allocation_count_(0),
|
|
|
|
id_(tree->next_node_id()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTraceNode::~AllocationTraceNode() {
|
2014-02-06 07:10:59 +00:00
|
|
|
for (int i = 0; i < children_.length(); i++) delete children_[i];
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 11:32:01 +00:00
|
|
|
AllocationTraceNode* AllocationTraceNode::FindChild(
|
|
|
|
unsigned function_info_index) {
|
2013-11-14 12:13:26 +00:00
|
|
|
for (int i = 0; i < children_.length(); i++) {
|
|
|
|
AllocationTraceNode* node = children_[i];
|
2014-03-07 11:32:01 +00:00
|
|
|
if (node->function_info_index() == function_info_index) return node;
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 11:32:01 +00:00
|
|
|
AllocationTraceNode* AllocationTraceNode::FindOrAddChild(
|
|
|
|
unsigned function_info_index) {
|
|
|
|
AllocationTraceNode* child = FindChild(function_info_index);
|
2013-11-14 12:13:26 +00:00
|
|
|
if (child == NULL) {
|
2014-03-07 11:32:01 +00:00
|
|
|
child = new AllocationTraceNode(tree_, function_info_index);
|
2013-11-14 12:13:26 +00:00
|
|
|
children_.Add(child);
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTraceNode::AddAllocation(unsigned size) {
|
|
|
|
total_size_ += size;
|
|
|
|
++allocation_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTraceNode::Print(int indent, AllocationTracker* tracker) {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%10u %10u %*c", total_size_, allocation_count_, indent, ' ');
|
2013-11-14 12:13:26 +00:00
|
|
|
if (tracker != NULL) {
|
2014-03-07 11:32:01 +00:00
|
|
|
AllocationTracker::FunctionInfo* info =
|
|
|
|
tracker->function_info_list()[function_info_index_];
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%s #%u", info->name, id_);
|
2013-11-14 12:13:26 +00:00
|
|
|
} else {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%u #%u", function_info_index_, id_);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("\n");
|
2013-11-14 12:13:26 +00:00
|
|
|
indent += 2;
|
|
|
|
for (int i = 0; i < children_.length(); i++) {
|
|
|
|
children_[i]->Print(indent, tracker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTraceTree::AllocationTraceTree()
|
|
|
|
: next_node_id_(1),
|
|
|
|
root_(this, 0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTraceTree::~AllocationTraceTree() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTraceNode* AllocationTraceTree::AddPathFromEnd(
|
2014-03-07 11:32:01 +00:00
|
|
|
const Vector<unsigned>& path) {
|
2013-11-14 12:13:26 +00:00
|
|
|
AllocationTraceNode* node = root();
|
2014-03-07 11:32:01 +00:00
|
|
|
for (unsigned* entry = path.start() + path.length() - 1;
|
2013-11-14 12:13:26 +00:00
|
|
|
entry != path.start() - 1;
|
|
|
|
--entry) {
|
|
|
|
node = node->FindOrAddChild(*entry);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTraceTree::Print(AllocationTracker* tracker) {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("[AllocationTraceTree:]\n");
|
|
|
|
base::OS::Print("Total size | Allocation count | Function id | id\n");
|
2013-11-14 12:13:26 +00:00
|
|
|
root()->Print(0, tracker);
|
|
|
|
}
|
|
|
|
|
2014-03-07 11:32:01 +00:00
|
|
|
|
2013-11-14 12:13:26 +00:00
|
|
|
void AllocationTracker::DeleteUnresolvedLocation(
|
|
|
|
UnresolvedLocation** location) {
|
|
|
|
delete *location;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTracker::FunctionInfo::FunctionInfo()
|
|
|
|
: name(""),
|
2014-03-07 11:32:01 +00:00
|
|
|
function_id(0),
|
2013-11-14 12:13:26 +00:00
|
|
|
script_name(""),
|
|
|
|
script_id(0),
|
|
|
|
line(-1),
|
|
|
|
column(-1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 16:13:22 +00:00
|
|
|
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() {
|
2014-03-07 16:34:10 +00:00
|
|
|
PrintF("[AddressToTraceMap (%" V8PRIuPTR "): \n", ranges_.size());
|
2014-03-07 16:13:22 +00:00
|
|
|
for (RangeMap::iterator it = ranges_.begin(); it != ranges_.end(); ++it) {
|
|
|
|
PrintF("[%p - %p] => %u\n", it->second.start, it->first,
|
|
|
|
it->second.trace_node_id);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
while (it != ranges_.end());
|
|
|
|
|
|
|
|
ranges_.erase(to_remove_begin, it);
|
|
|
|
|
|
|
|
if (prev_range.start != 0) {
|
|
|
|
ranges_.insert(RangeMap::value_type(start, prev_range));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 11:32:01 +00:00
|
|
|
void AllocationTracker::DeleteFunctionInfo(FunctionInfo** info) {
|
|
|
|
delete *info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-14 12:13:26 +00:00
|
|
|
AllocationTracker::AllocationTracker(
|
|
|
|
HeapObjectsMap* ids, StringsStorage* names)
|
|
|
|
: ids_(ids),
|
|
|
|
names_(names),
|
2014-04-15 14:48:21 +00:00
|
|
|
id_to_function_info_index_(HashMap::PointersMatch),
|
2014-03-07 11:32:01 +00:00
|
|
|
info_index_for_other_state_(0) {
|
|
|
|
FunctionInfo* info = new FunctionInfo();
|
|
|
|
info->name = "(root)";
|
|
|
|
function_info_list_.Add(info);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTracker::~AllocationTracker() {
|
|
|
|
unresolved_locations_.Iterate(DeleteUnresolvedLocation);
|
2014-03-07 11:32:01 +00:00
|
|
|
function_info_list_.Iterate(&DeleteFunctionInfo);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTracker::PrepareForSerialization() {
|
|
|
|
List<UnresolvedLocation*> copy(unresolved_locations_.length());
|
|
|
|
copy.AddAll(unresolved_locations_);
|
|
|
|
unresolved_locations_.Clear();
|
|
|
|
for (int i = 0; i < copy.length(); i++) {
|
|
|
|
copy[i]->Resolve();
|
|
|
|
delete copy[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-29 09:54:38 +00:00
|
|
|
void AllocationTracker::AllocationEvent(Address addr, int size) {
|
2013-11-14 12:13:26 +00:00
|
|
|
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.
|
2015-01-27 14:56:51 +00:00
|
|
|
heap->CreateFillerObjectAt(addr, size);
|
2013-11-14 12:13:26 +00:00
|
|
|
|
|
|
|
Isolate* isolate = heap->isolate();
|
|
|
|
int length = 0;
|
|
|
|
StackTraceFrameIterator it(isolate);
|
|
|
|
while (!it.done() && length < kMaxAllocationTraceLength) {
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
SharedFunctionInfo* shared = frame->function()->shared();
|
2013-11-29 09:54:38 +00:00
|
|
|
SnapshotObjectId id = ids_->FindOrAddEntry(
|
|
|
|
shared->address(), shared->Size(), false);
|
2014-03-07 11:32:01 +00:00
|
|
|
allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
|
2013-11-14 12:13:26 +00:00
|
|
|
it.Advance();
|
|
|
|
}
|
2014-03-07 11:32:01 +00:00
|
|
|
if (length == 0) {
|
|
|
|
unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
|
|
|
|
if (index != 0) {
|
|
|
|
allocation_trace_buffer_[length++] = index;
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 12:13:26 +00:00
|
|
|
AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
|
2014-03-07 11:32:01 +00:00
|
|
|
Vector<unsigned>(allocation_trace_buffer_, length));
|
2013-11-14 12:13:26 +00:00
|
|
|
top_node->AddAllocation(size);
|
2014-03-07 16:13:22 +00:00
|
|
|
|
|
|
|
address_to_trace_.AddRange(addr, size, top_node->id());
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
|
|
|
|
return ComputeIntegerHash(static_cast<uint32_t>(id),
|
|
|
|
v8::internal::kZeroHashSeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 11:32:01 +00:00
|
|
|
unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
|
|
|
|
SnapshotObjectId id) {
|
2015-04-13 19:01:15 +00:00
|
|
|
HashMap::Entry* entry = id_to_function_info_index_.LookupOrInsert(
|
|
|
|
reinterpret_cast<void*>(id), SnapshotObjectIdHash(id));
|
2013-11-14 12:13:26 +00:00
|
|
|
if (entry->value == NULL) {
|
|
|
|
FunctionInfo* info = new FunctionInfo();
|
|
|
|
info->name = names_->GetFunctionName(shared->DebugName());
|
2014-03-07 11:32:01 +00:00
|
|
|
info->function_id = id;
|
2013-11-14 12:13:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
info->script_id = script->id()->value();
|
|
|
|
// Converting start offset into line and column may cause heap
|
|
|
|
// allocations so we postpone them until snapshot serialization.
|
|
|
|
unresolved_locations_.Add(new UnresolvedLocation(
|
|
|
|
script,
|
|
|
|
shared->start_position(),
|
|
|
|
info));
|
|
|
|
}
|
2014-03-07 11:32:01 +00:00
|
|
|
entry->value = reinterpret_cast<void*>(function_info_list_.length());
|
|
|
|
function_info_list_.Add(info);
|
|
|
|
}
|
|
|
|
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)";
|
|
|
|
info_index_for_other_state_ = function_info_list_.length();
|
|
|
|
function_info_list_.Add(info);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
2014-03-07 11:32:01 +00:00
|
|
|
return info_index_for_other_state_;
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTracker::UnresolvedLocation::UnresolvedLocation(
|
|
|
|
Script* script, int start, FunctionInfo* info)
|
|
|
|
: start_position_(start),
|
|
|
|
info_(info) {
|
|
|
|
script_ = Handle<Script>::cast(
|
|
|
|
script->GetIsolate()->global_handles()->Create(script));
|
2013-12-18 08:09:37 +00:00
|
|
|
GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()),
|
|
|
|
this,
|
|
|
|
&HandleWeakScript);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllocationTracker::UnresolvedLocation::~UnresolvedLocation() {
|
|
|
|
if (!script_.is_null()) {
|
2013-12-18 08:09:37 +00:00
|
|
|
GlobalHandles::Destroy(reinterpret_cast<Object**>(script_.location()));
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTracker::UnresolvedLocation::Resolve() {
|
|
|
|
if (script_.is_null()) return;
|
2014-02-18 11:30:38 +00:00
|
|
|
HandleScope scope(script_->GetIsolate());
|
2014-04-16 13:28:11 +00:00
|
|
|
info_->line = Script::GetLineNumber(script_, start_position_);
|
|
|
|
info_->column = Script::GetColumnNumber(script_, start_position_);
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AllocationTracker::UnresolvedLocation::HandleWeakScript(
|
2013-12-18 08:09:37 +00:00
|
|
|
const v8::WeakCallbackData<v8::Value, void>& data) {
|
|
|
|
UnresolvedLocation* loc =
|
|
|
|
reinterpret_cast<UnresolvedLocation*>(data.GetParameter());
|
|
|
|
GlobalHandles::Destroy(reinterpret_cast<Object**>(loc->script_.location()));
|
|
|
|
loc->script_ = Handle<Script>::null();
|
2013-11-14 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|