Revision: 5914
Author: [email protected]
Date: Thu Dec  2 07:38:51 2010
Log: 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
http://code.google.com/p/v8/source/detail?r=5914

Modified:
 /branches/bleeding_edge/include/v8-profiler.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/profile-generator.cc
 /branches/bleeding_edge/src/profile-generator.h
 /branches/bleeding_edge/test/cctest/test-heap-profiler.cc

=======================================
--- /branches/bleeding_edge/include/v8-profiler.h       Mon Nov 22 06:00:40 2010
+++ /branches/bleeding_edge/include/v8-profiler.h       Thu Dec  2 07:38:51 2010
@@ -358,6 +358,9 @@
   /** 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.
=======================================
--- /branches/bleeding_edge/src/api.cc  Thu Nov 25 00:04:12 2010
+++ /branches/bleeding_edge/src/api.cc  Thu Dec  2 07:38:51 2010
@@ -4870,6 +4870,13 @@
   IsDeadCheck("v8::HeapSnapshot::GetHead");
   return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
 }
+
+
+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(
=======================================
--- /branches/bleeding_edge/src/profile-generator.cc Thu Nov 25 00:04:12 2010 +++ /branches/bleeding_edge/src/profile-generator.cc Thu Dec 2 07:38:51 2010
@@ -1542,6 +1542,29 @@
 HeapSnapshotsDiff* HeapSnapshot::CompareWith(HeapSnapshot* snapshot) {
   return collection_->CompareSnapshots(this, 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) {
=======================================
--- /branches/bleeding_edge/src/profile-generator.h     Thu Nov 25 00:04:12 2010
+++ /branches/bleeding_edge/src/profile-generator.h     Thu Dec  2 07:38:51 2010
@@ -696,6 +696,7 @@
   void ApproximateRetainedSizes();
   void ClearPaint();
   HeapSnapshotsDiff* CompareWith(HeapSnapshot* snapshot);
+  HeapEntry* GetEntryById(uint64_t id);
   List<HeapGraphPath*>* GetRetainingPaths(HeapEntry* entry);
   List<HeapEntry*>* GetSortedEntriesList();
   template<class Visitor>
=======================================
--- /branches/bleeding_edge/test/cctest/test-heap-profiler.cc Wed Nov 24 02:47:18 2010 +++ /branches/bleeding_edge/test/cctest/test-heap-profiler.cc Thu Dec 2 07:38:51 2010
@@ -1192,5 +1192,23 @@
   CHECK_GT(stream.size(), 0);
   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

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to