Revision: 18334
Author:   [email protected]
Date:     Wed Dec 18 08:17:03 2013 UTC
Log: Add methods for finding object by its snapshot id and id for an object

Object<-->id mapping doesn't depend on a particular snapshot, actually same object may appear in several heap snapshots. The API for converting between id and heap object should be provided by HeapProfiler itself. There is already GetObjectId method which I extended with FindObjectById/ClearObjectIds. As the next step I'm going to deprecate and remove HeapGraphNode::GetHeapValue.

BUG=chromium:324769
LOG=N
[email protected], [email protected], [email protected]

Review URL: https://codereview.chromium.org/93843004
http://code.google.com/p/v8/source/detail?r=18334

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

=======================================
--- /branches/bleeding_edge/include/v8-profiler.h Wed Dec 18 08:09:37 2013 UTC +++ /branches/bleeding_edge/include/v8-profiler.h Wed Dec 18 08:17:03 2013 UTC
@@ -388,6 +388,19 @@
    */
   SnapshotObjectId GetObjectId(Handle<Value> value);

+  /**
+ * Returns heap object with given SnapshotObjectId if the object is alive,
+   * otherwise empty handle is returned.
+   */
+  Handle<Value> FindObjectById(SnapshotObjectId id);
+
+  /**
+ * Clears internal map from SnapshotObjectId to heap object. The new objects + * will not be added into it unless a heap snapshot is taken or heap object
+   * tracking is kicked off.
+   */
+  void ClearObjectIds();
+
   /**
* A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
    * it in case heap profiler cannot find id  for the object passed as
=======================================
--- /branches/bleeding_edge/src/api.cc  Wed Dec 18 08:09:37 2013 UTC
+++ /branches/bleeding_edge/src/api.cc  Wed Dec 18 08:17:03 2013 UTC
@@ -7233,6 +7233,19 @@
   i::Handle<i::Object> obj = Utils::OpenHandle(*value);
return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj);
 }
+
+
+Handle<Value> HeapProfiler::FindObjectById(SnapshotObjectId id) {
+  i::Handle<i::Object> obj =
+      reinterpret_cast<i::HeapProfiler*>(this)->FindHeapObjectById(id);
+  if (obj.is_null()) return Local<Value>();
+  return Utils::ToLocal(obj);
+}
+
+
+void HeapProfiler::ClearObjectIds() {
+  reinterpret_cast<i::HeapProfiler*>(this)->ClearHeapObjectMap();
+}


 const HeapSnapshot* HeapProfiler::TakeHeapSnapshot(
=======================================
--- /branches/bleeding_edge/src/heap-profiler.cc Mon Dec 9 07:41:20 2013 UTC +++ /branches/bleeding_edge/src/heap-profiler.cc Wed Dec 18 08:17:03 2013 UTC
@@ -210,6 +210,12 @@
   }
return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
 }
+
+
+void HeapProfiler::ClearHeapObjectMap() {
+  ids_.Reset(new HeapObjectsMap(heap()));
+  if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
+}


 } }  // namespace v8::internal
=======================================
--- /branches/bleeding_edge/src/heap-profiler.h Mon Dec  9 07:41:20 2013 UTC
+++ /branches/bleeding_edge/src/heap-profiler.h Wed Dec 18 08:17:03 2013 UTC
@@ -87,6 +87,7 @@
   }

   Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id);
+  void ClearHeapObjectMap();

  private:
   Heap* heap() const { return ids_->heap(); }
=======================================
--- /branches/bleeding_edge/test/cctest/test-heap-profiler.cc Mon Dec 9 07:41:20 2013 UTC +++ /branches/bleeding_edge/test/cctest/test-heap-profiler.cc Wed Dec 18 08:17:03 2013 UTC
@@ -1039,6 +1039,49 @@

   heap_profiler->StopTrackingHeapObjects();
 }
+
+
+TEST(HeapObjectIds) {
+  LocalContext env;
+  v8::Isolate* isolate = env->GetIsolate();
+  v8::HandleScope scope(isolate);
+  v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
+
+  const int kLength = 10;
+  v8::Handle<v8::Object> objects[kLength];
+  v8::SnapshotObjectId ids[kLength];
+
+  heap_profiler->StartTrackingHeapObjects(false);
+
+  for (int i = 0; i < kLength; i++) {
+    objects[i] = v8::Object::New(isolate);
+  }
+  GetHeapStatsUpdate(heap_profiler);
+
+  for (int i = 0; i < kLength; i++) {
+    v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
+    CHECK_NE(v8::HeapProfiler::kUnknownObjectId, static_cast<int>(id));
+    ids[i] = id;
+  }
+
+  heap_profiler->StopTrackingHeapObjects();
+  CcTest::heap()->CollectAllAvailableGarbage();
+
+  for (int i = 0; i < kLength; i++) {
+    v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
+    CHECK_EQ(static_cast<int>(ids[i]), static_cast<int>(id));
+    v8::Handle<v8::Value> obj = heap_profiler->FindObjectById(ids[i]);
+    CHECK_EQ(objects[i], obj);
+  }
+
+  heap_profiler->ClearObjectIds();
+  for (int i = 0; i < kLength; i++) {
+    v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
+    CHECK_EQ(v8::HeapProfiler::kUnknownObjectId, static_cast<int>(id));
+    v8::Handle<v8::Value> obj = heap_profiler->FindObjectById(ids[i]);
+    CHECK(obj.IsEmpty());
+  }
+}


 static void CheckChildrenIds(const v8::HeapSnapshot* snapshot,

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to