Allocation tracker: add separate entry for allocations via V8 API

When object is creating via native V8 API calls JS callstack is empty and the allocation is indistinguishable from say compiler allocations. This change adds a separate entry for such allocations.

Since FunctionInfo not necessarily corresponds to a heap object they are now referred to using their index in the list of all FunctionInfos.

BUG=chromium:277984
LOG=N
R=loislo@chromium.org, mstarzinger@chromium.org

Review URL: https://codereview.chromium.org/177203002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19718 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
yurys@chromium.org 2014-03-07 11:32:01 +00:00
parent 06af80d42d
commit 49cd3d8a65
4 changed files with 111 additions and 68 deletions

View File

@ -36,9 +36,9 @@ namespace v8 {
namespace internal {
AllocationTraceNode::AllocationTraceNode(
AllocationTraceTree* tree, SnapshotObjectId shared_function_info_id)
AllocationTraceTree* tree, unsigned function_info_index)
: tree_(tree),
function_id_(shared_function_info_id),
function_info_index_(function_info_index),
total_size_(0),
allocation_count_(0),
id_(tree->next_node_id()) {
@ -50,19 +50,21 @@ AllocationTraceNode::~AllocationTraceNode() {
}
AllocationTraceNode* AllocationTraceNode::FindChild(SnapshotObjectId id) {
AllocationTraceNode* AllocationTraceNode::FindChild(
unsigned function_info_index) {
for (int i = 0; i < children_.length(); i++) {
AllocationTraceNode* node = children_[i];
if (node->function_id() == id) return node;
if (node->function_info_index() == function_info_index) return node;
}
return NULL;
}
AllocationTraceNode* AllocationTraceNode::FindOrAddChild(SnapshotObjectId id) {
AllocationTraceNode* child = FindChild(id);
AllocationTraceNode* AllocationTraceNode::FindOrAddChild(
unsigned function_info_index) {
AllocationTraceNode* child = FindChild(function_info_index);
if (child == NULL) {
child = new AllocationTraceNode(tree_, id);
child = new AllocationTraceNode(tree_, function_info_index);
children_.Add(child);
}
return child;
@ -78,17 +80,11 @@ void AllocationTraceNode::AddAllocation(unsigned size) {
void AllocationTraceNode::Print(int indent, AllocationTracker* tracker) {
OS::Print("%10u %10u %*c", total_size_, allocation_count_, indent, ' ');
if (tracker != NULL) {
const char* name = "<unknown function>";
if (function_id_ != 0) {
AllocationTracker::FunctionInfo* info =
tracker->GetFunctionInfo(function_id_);
if (info != NULL) {
name = info->name;
}
}
OS::Print("%s #%u", name, id_);
AllocationTracker::FunctionInfo* info =
tracker->function_info_list()[function_info_index_];
OS::Print("%s #%u", info->name, id_);
} else {
OS::Print("%u #%u", function_id_, id_);
OS::Print("%u #%u", function_info_index_, id_);
}
OS::Print("\n");
indent += 2;
@ -109,9 +105,9 @@ AllocationTraceTree::~AllocationTraceTree() {
AllocationTraceNode* AllocationTraceTree::AddPathFromEnd(
const Vector<SnapshotObjectId>& path) {
const Vector<unsigned>& path) {
AllocationTraceNode* node = root();
for (SnapshotObjectId* entry = path.start() + path.length() - 1;
for (unsigned* entry = path.start() + path.length() - 1;
entry != path.start() - 1;
--entry) {
node = node->FindOrAddChild(*entry);
@ -126,6 +122,7 @@ void AllocationTraceTree::Print(AllocationTracker* tracker) {
root()->Print(0, tracker);
}
void AllocationTracker::DeleteUnresolvedLocation(
UnresolvedLocation** location) {
delete *location;
@ -134,6 +131,7 @@ void AllocationTracker::DeleteUnresolvedLocation(
AllocationTracker::FunctionInfo::FunctionInfo()
: name(""),
function_id(0),
script_name(""),
script_id(0),
line(-1),
@ -146,21 +144,26 @@ static bool AddressesMatch(void* key1, void* key2) {
}
void AllocationTracker::DeleteFunctionInfo(FunctionInfo** info) {
delete *info;
}
AllocationTracker::AllocationTracker(
HeapObjectsMap* ids, StringsStorage* names)
: ids_(ids),
names_(names),
id_to_function_info_(AddressesMatch) {
id_to_function_info_index_(AddressesMatch),
info_index_for_other_state_(0) {
FunctionInfo* info = new FunctionInfo();
info->name = "(root)";
function_info_list_.Add(info);
}
AllocationTracker::~AllocationTracker() {
unresolved_locations_.Iterate(DeleteUnresolvedLocation);
for (HashMap::Entry* p = id_to_function_info_.Start();
p != NULL;
p = id_to_function_info_.Next(p)) {
delete reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value);
}
function_info_list_.Iterate(&DeleteFunctionInfo);
}
@ -193,12 +196,17 @@ void AllocationTracker::AllocationEvent(Address addr, int size) {
SharedFunctionInfo* shared = frame->function()->shared();
SnapshotObjectId id = ids_->FindOrAddEntry(
shared->address(), shared->Size(), false);
allocation_trace_buffer_[length++] = id;
AddFunctionInfo(shared, id);
allocation_trace_buffer_[length++] = AddFunctionInfo(shared, id);
it.Advance();
}
if (length == 0) {
unsigned index = functionInfoIndexForVMState(isolate->current_vm_state());
if (index != 0) {
allocation_trace_buffer_[length++] = index;
}
}
AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd(
Vector<SnapshotObjectId>(allocation_trace_buffer_, length));
Vector<unsigned>(allocation_trace_buffer_, length));
top_node->AddAllocation(size);
}
@ -209,24 +217,14 @@ static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) {
}
AllocationTracker::FunctionInfo* AllocationTracker::GetFunctionInfo(
SnapshotObjectId id) {
HashMap::Entry* entry = id_to_function_info_.Lookup(
reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), false);
if (entry == NULL) {
return NULL;
}
return reinterpret_cast<FunctionInfo*>(entry->value);
}
void AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
SnapshotObjectId id) {
HashMap::Entry* entry = id_to_function_info_.Lookup(
unsigned AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
SnapshotObjectId id) {
HashMap::Entry* entry = id_to_function_info_index_.Lookup(
reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), true);
if (entry->value == NULL) {
FunctionInfo* info = new FunctionInfo();
info->name = names_->GetFunctionName(shared->DebugName());
info->function_id = id;
if (shared->script()->IsScript()) {
Script* script = Script::cast(shared->script());
if (script->name()->IsName()) {
@ -241,8 +239,22 @@ void AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared,
shared->start_position(),
info));
}
entry->value = info;
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);
}
return info_index_for_other_state_;
}

View File

@ -38,13 +38,13 @@ class AllocationTraceTree;
class AllocationTraceNode {
public:
AllocationTraceNode(AllocationTraceTree* tree,
SnapshotObjectId shared_function_info_id);
unsigned function_info_index);
~AllocationTraceNode();
AllocationTraceNode* FindChild(SnapshotObjectId shared_function_info_id);
AllocationTraceNode* FindOrAddChild(SnapshotObjectId shared_function_info_id);
AllocationTraceNode* FindChild(unsigned function_info_index);
AllocationTraceNode* FindOrAddChild(unsigned function_info_index);
void AddAllocation(unsigned size);
SnapshotObjectId function_id() const { return function_id_; }
unsigned function_info_index() const { return function_info_index_; }
unsigned allocation_size() const { return total_size_; }
unsigned allocation_count() const { return allocation_count_; }
unsigned id() const { return id_; }
@ -54,7 +54,7 @@ class AllocationTraceNode {
private:
AllocationTraceTree* tree_;
SnapshotObjectId function_id_;
unsigned function_info_index_;
unsigned total_size_;
unsigned allocation_count_;
unsigned id_;
@ -68,7 +68,7 @@ class AllocationTraceTree {
public:
AllocationTraceTree();
~AllocationTraceTree();
AllocationTraceNode* AddPathFromEnd(const Vector<SnapshotObjectId>& path);
AllocationTraceNode* AddPathFromEnd(const Vector<unsigned>& path);
AllocationTraceNode* root() { return &root_; }
unsigned next_node_id() { return next_node_id_++; }
void Print(AllocationTracker* tracker);
@ -86,6 +86,7 @@ class AllocationTracker {
struct FunctionInfo {
FunctionInfo();
const char* name;
SnapshotObjectId function_id;
const char* script_name;
int script_id;
int line;
@ -99,11 +100,14 @@ class AllocationTracker {
void AllocationEvent(Address addr, int size);
AllocationTraceTree* trace_tree() { return &trace_tree_; }
HashMap* id_to_function_info() { return &id_to_function_info_; }
FunctionInfo* GetFunctionInfo(SnapshotObjectId id);
const List<FunctionInfo*>& function_info_list() const {
return function_info_list_;
}
private:
void AddFunctionInfo(SharedFunctionInfo* info, SnapshotObjectId id);
unsigned AddFunctionInfo(SharedFunctionInfo* info, SnapshotObjectId id);
static void DeleteFunctionInfo(FunctionInfo** info);
unsigned functionInfoIndexForVMState(StateTag state);
class UnresolvedLocation {
public:
@ -125,9 +129,11 @@ class AllocationTracker {
HeapObjectsMap* ids_;
StringsStorage* names_;
AllocationTraceTree trace_tree_;
SnapshotObjectId allocation_trace_buffer_[kMaxAllocationTraceLength];
HashMap id_to_function_info_;
unsigned allocation_trace_buffer_[kMaxAllocationTraceLength];
List<FunctionInfo*> function_info_list_;
HashMap id_to_function_info_index_;
List<UnresolvedLocation*> unresolved_locations_;
unsigned info_index_for_other_state_;
DISALLOW_COPY_AND_ASSIGN(AllocationTracker);
};

View File

@ -2878,7 +2878,7 @@ void HeapSnapshotJSONSerializer::SerializeSnapshot() {
JSON_S("column")) ","
JSON_S("trace_node_fields") ":" JSON_A(
JSON_S("id") ","
JSON_S("function_id") ","
JSON_S("function_info_index") ","
JSON_S("count") ","
JSON_S("size") ","
JSON_S("children"))));
@ -2893,7 +2893,7 @@ void HeapSnapshotJSONSerializer::SerializeSnapshot() {
uint32_t count = 0;
AllocationTracker* tracker = snapshot_->profiler()->allocation_tracker();
if (tracker) {
count = tracker->id_to_function_info()->occupancy();
count = tracker->function_info_list().length();
}
writer_->AddNumber(count);
}
@ -2926,7 +2926,7 @@ void HeapSnapshotJSONSerializer::SerializeTraceNode(AllocationTraceNode* node) {
int buffer_pos = 0;
buffer_pos = utoa(node->id(), buffer, buffer_pos);
buffer[buffer_pos++] = ',';
buffer_pos = utoa(node->function_id(), buffer, buffer_pos);
buffer_pos = utoa(node->function_info_index(), buffer, buffer_pos);
buffer[buffer_pos++] = ',';
buffer_pos = utoa(node->allocation_count(), buffer, buffer_pos);
buffer[buffer_pos++] = ',';
@ -2968,22 +2968,18 @@ void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() {
6 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT
+ 6 + 1 + 1;
EmbeddedVector<char, kBufferSize> buffer;
HashMap* id_to_function_info = tracker->id_to_function_info();
const List<AllocationTracker::FunctionInfo*>& list =
tracker->function_info_list();
bool first_entry = true;
for (HashMap::Entry* p = id_to_function_info->Start();
p != NULL;
p = id_to_function_info->Next(p)) {
SnapshotObjectId id =
static_cast<SnapshotObjectId>(reinterpret_cast<intptr_t>(p->key));
AllocationTracker::FunctionInfo* info =
reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value);
for (int i = 0; i < list.length(); i++) {
AllocationTracker::FunctionInfo* info = list[i];
int buffer_pos = 0;
if (first_entry) {
first_entry = false;
} else {
buffer[buffer_pos++] = ',';
}
buffer_pos = utoa(id, buffer, buffer_pos);
buffer_pos = utoa(info->function_id, buffer, buffer_pos);
buffer[buffer_pos++] = ',';
buffer_pos = utoa(GetStringId(info->name), buffer, buffer_pos);
buffer[buffer_pos++] = ',';

View File

@ -2216,8 +2216,9 @@ static AllocationTraceNode* FindNode(
Vector<AllocationTraceNode*> children = node->children();
node = NULL;
for (int j = 0; j < children.length(); j++) {
v8::SnapshotObjectId id = children[j]->function_id();
AllocationTracker::FunctionInfo* info = tracker->GetFunctionInfo(id);
unsigned index = children[j]->function_info_index();
AllocationTracker::FunctionInfo* info =
tracker->function_info_list()[index];
if (info && strcmp(info->name, name) == 0) {
node = children[j];
break;
@ -2364,6 +2365,34 @@ TEST(TrackBumpPointerAllocations) {
}
TEST(TrackV8ApiAllocation) {
v8::HandleScope scope(v8::Isolate::GetCurrent());
LocalContext env;
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
const char* names[] = { "(V8 API)" };
heap_profiler->StartTrackingHeapObjects(true);
v8::Handle<v8::Object> o1 = v8::Object::New(env->GetIsolate());
o1->Clone();
AllocationTracker* tracker =
reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
CHECK_NE(NULL, tracker);
// Resolve all function locations.
tracker->PrepareForSerialization();
// Print for better diagnostics in case of failure.
tracker->trace_tree()->Print(tracker);
AllocationTraceNode* node =
FindNode(tracker, Vector<const char*>(names, ARRAY_SIZE(names)));
CHECK_NE(NULL, node);
CHECK_GE(node->allocation_count(), 2);
CHECK_GE(node->allocation_size(), 4 * node->allocation_count());
heap_profiler->StopTrackingHeapObjects();
}
TEST(ArrayBufferAndArrayBufferView) {
LocalContext env;
v8::HandleScope scope(env->GetIsolate());