New Heap Profiler: add API method for finding a graph node by id.

TEST=cctest/test-heap-profiler/HeapSnapshotGetNodeById

Review URL: http://codereview.chromium.org/5537001

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5914 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent fdca2c31
......@@ -358,6 +358,9 @@ class V8EXPORT HeapSnapshot {
/** Returns the root node of the heap graph. */
const HeapGraphNode* GetRoot() const;
/** Returns a node by its id. */
const HeapGraphNode* GetNodeById(uint64_t id) const;
/**
* Returns a diff between this snapshot and another one. Only snapshots
* of the same type can be compared.
......
......@@ -4872,6 +4872,13 @@ const HeapGraphNode* HeapSnapshot::GetRoot() const {
}
const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const {
IsDeadCheck("v8::HeapSnapshot::GetNodeById");
return reinterpret_cast<const HeapGraphNode*>(
ToInternal(this)->GetEntryById(id));
}
const HeapSnapshotsDiff* HeapSnapshot::CompareWith(
const HeapSnapshot* snapshot) const {
IsDeadCheck("v8::HeapSnapshot::CompareWith");
......
......@@ -1544,6 +1544,29 @@ HeapSnapshotsDiff* HeapSnapshot::CompareWith(HeapSnapshot* snapshot) {
}
HeapEntry* HeapSnapshot::GetEntryById(uint64_t id) {
// GetSortedEntriesList is used in diff algorithm and sorts
// entries by their id.
List<HeapEntry*>* entries_by_id = GetSortedEntriesList();
// Perform a binary search by id.
int low = 0;
int high = entries_by_id->length() - 1;
while (low <= high) {
int mid =
(static_cast<unsigned int>(low) + static_cast<unsigned int>(high)) >> 1;
uint64_t mid_id = entries_by_id->at(mid)->id();
if (mid_id > id)
high = mid - 1;
else if (mid_id < id)
low = mid + 1;
else
return entries_by_id->at(mid);
}
return NULL;
}
List<HeapGraphPath*>* HeapSnapshot::GetRetainingPaths(HeapEntry* entry) {
HashMap::Entry* p =
retaining_paths_.Lookup(entry, HeapEntry::Hash(entry), true);
......
......@@ -696,6 +696,7 @@ class HeapSnapshot {
void ApproximateRetainedSizes();
void ClearPaint();
HeapSnapshotsDiff* CompareWith(HeapSnapshot* snapshot);
HeapEntry* GetEntryById(uint64_t id);
List<HeapGraphPath*>* GetRetainingPaths(HeapEntry* entry);
List<HeapEntry*>* GetSortedEntriesList();
template<class Visitor>
......
......@@ -1193,4 +1193,22 @@ TEST(AggregatedHeapSnapshotJSONSerialization) {
CHECK_EQ(1, stream.eos_signaled());
}
TEST(HeapSnapshotGetNodeById) {
v8::HandleScope scope;
LocalContext env;
const v8::HeapSnapshot* snapshot =
v8::HeapProfiler::TakeSnapshot(v8::String::New("id"));
const v8::HeapGraphNode* root = snapshot->GetRoot();
CHECK_EQ(root, snapshot->GetNodeById(root->GetId()));
for (int i = 0, count = root->GetChildrenCount(); i < count; ++i) {
const v8::HeapGraphEdge* prop = root->GetChild(i);
CHECK_EQ(
prop->GetToNode(), snapshot->GetNodeById(prop->GetToNode()->GetId()));
}
// Check a big id, which should not exist yet.
CHECK_EQ(NULL, snapshot->GetNodeById(0x1000000UL));
}
#endif // ENABLE_LOGGING_AND_PROFILING
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment