Reviewers: Søren Gjesse,

Description:
Heap profiler: add an ability to iterate over snapshot's nodes.

This is a preparation for removing aggregated heap snapshots.
W/o this API, counting object instances in a snapshot is very hard.

[email protected]
BUG=1481
TEST=cctest/test-heap-profiler/NodesIteration


Please review this at http://codereview.chromium.org/7204040/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8-profiler.h
  M src/api.cc
  M test/cctest/test-heap-profiler.cc


Index: include/v8-profiler.h
diff --git a/include/v8-profiler.h b/include/v8-profiler.h
index 940a35c152d7c32398767949af849d807eecb842..dad18a8442db16f3feacf7f7855767833dc2a3d8 100644
--- a/include/v8-profiler.h
+++ b/include/v8-profiler.h
@@ -346,6 +346,12 @@ class V8EXPORT HeapSnapshot {
   /** Returns a node by its id. */
   const HeapGraphNode* GetNodeById(uint64_t id) const;

+  /** Returns total nodes count in the snapshot. */
+  int GetNodesCount() const;
+
+  /** Returns a node by index. */
+  const HeapGraphNode* GetNode(int index) const;
+
   /**
    * Deletes the snapshot and removes it from HeapProfiler's list.
    * All pointers to nodes, edges and paths previously returned become
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 7f68b922b19cf268cf3bd006a0c697894bcef440..3fe5621703ef3ca04f8f6af312ece30c318a883b 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -5886,6 +5886,29 @@ const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const {
 }


+int HeapSnapshot::GetNodesCount() const {
+#ifdef ENABLE_LOGGING_AND_PROFILING
+  i::Isolate* isolate = i::Isolate::Current();
+  IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodesCount");
+  return ToInternal(this)->entries()->length();
+#else
+  return 0;
+#endif
+}
+
+
+const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
+#ifdef ENABLE_LOGGING_AND_PROFILING
+  i::Isolate* isolate = i::Isolate::Current();
+  IsDeadCheck(isolate, "v8::HeapSnapshot::GetNode");
+  return reinterpret_cast<const HeapGraphNode*>(
+      ToInternal(this)->entries()->at(index));
+#else
+  return 0;
+#endif
+}
+
+
 void HeapSnapshot::Serialize(OutputStream* stream,
HeapSnapshot::SerializationFormat format) const {
 #ifdef ENABLE_LOGGING_AND_PROFILING
Index: test/cctest/test-heap-profiler.cc
diff --git a/test/cctest/test-heap-profiler.cc b/test/cctest/test-heap-profiler.cc index 213ed9aa90112a5e8a7c5af166aca861bcc7c038..a3e14cb74fbf6bdc6924ae7eafed12087e0c9be9 100644
--- a/test/cctest/test-heap-profiler.cc
+++ b/test/cctest/test-heap-profiler.cc
@@ -1372,4 +1372,22 @@ TEST(DocumentURLWithException) {
                reinterpret_cast<const i::HeapEntry*>(global))->name());
 }

+
+TEST(NodesIteration) {
+  v8::HandleScope scope;
+  LocalContext env;
+  const v8::HeapSnapshot* snapshot =
+      v8::HeapProfiler::TakeSnapshot(v8::String::New("iteration"));
+  const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
+  CHECK_NE(NULL, global);
+  // Verify that we can find this object by iteration.
+  const int nodes_count = snapshot->GetNodesCount();
+  int count = 0;
+  for (int i = 0; i < nodes_count; ++i) {
+    if (snapshot->GetNode(i) == global)
+      ++count;
+  }
+  CHECK_EQ(1, count);
+}
+
 #endif  // ENABLE_LOGGING_AND_PROFILING


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

Reply via email to