Reviewers: Mikhail Naganov (Chromium), loislo, alexeif,

Description:
Add method for resolving SnapshotObjectId by given object

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

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

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


Index: include/v8-profiler.h
===================================================================
--- include/v8-profiler.h       (revision 11329)
+++ include/v8-profiler.h       (working copy)
@@ -413,6 +413,19 @@
   static const HeapSnapshot* FindSnapshot(unsigned uid);

   /**
+   * Returns SnapshotObjectId for a heap object referenced by |value| if
+   * it has been seen by the heap profiler, kUnknownObjectId otherwise.
+   */
+  static SnapshotObjectId GetSnapshotObjectId(Handle<Value> value);
+
+  /**
+ * A constant for invalid SnapshotObjectId. GetSnapshotObjectId will return
+   * it in case heap profiler cannot find id  for the object passed as
+ * parameter. HeapSnapshot::GetNodeById will always return NULL for such id.
+   */
+  static const SnapshotObjectId kUnknownObjectId = 0;
+
+  /**
    * Takes a heap snapshot and returns it. Title may be an empty string.
    * See HeapSnapshot::Type for types description.
    */
Index: src/api.cc
===================================================================
--- src/api.cc  (revision 11329)
+++ src/api.cc  (working copy)
@@ -6218,6 +6218,14 @@
 }


+SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Value> value) {
+  i::Isolate* isolate = i::Isolate::Current();
+  IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshotObjectId");
+  i::Handle<i::Object> obj = Utils::OpenHandle(*value);
+  return i::HeapProfiler::GetSnapshotObjectId(obj);
+}
+
+
 const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title,
                                                HeapSnapshot::Type type,
                                                ActivityControl* control) {
Index: src/heap-profiler.cc
===================================================================
--- src/heap-profiler.cc        (revision 11329)
+++ src/heap-profiler.cc        (working copy)
@@ -189,6 +189,15 @@
 }


+SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
+  if (!obj->IsHeapObject())
+    return v8::HeapProfiler::kUnknownObjectId;
+  HeapProfiler* profiler = Isolate::Current()->heap_profiler();
+  ASSERT(profiler != NULL);
+ return profiler->snapshots_->FindObjectId(HeapObject::cast(*obj) ->address());
+}
+
+
 void HeapProfiler::DeleteAllSnapshots() {
   HeapProfiler* profiler = Isolate::Current()->heap_profiler();
   ASSERT(profiler != NULL);
Index: src/heap-profiler.h
===================================================================
--- src/heap-profiler.h (revision 11329)
+++ src/heap-profiler.h (working copy)
@@ -62,6 +62,7 @@
   static int GetSnapshotsCount();
   static HeapSnapshot* GetSnapshot(int index);
   static HeapSnapshot* FindSnapshot(unsigned uid);
+  static SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj);
   static void DeleteAllSnapshots();

   void ObjectMoveEvent(Address from, Address to);
Index: src/profile-generator.cc
===================================================================
--- src/profile-generator.cc    (revision 11329)
+++ src/profile-generator.cc    (working copy)
@@ -1366,7 +1366,6 @@
   if (entry == NULL) return 0;
int entry_index = static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
   EntryInfo& entry_info = entries_->at(entry_index);
-  entry_info.accessed = true;
ASSERT(static_cast<uint32_t>(entries_->length()) > entries_map_.occupancy());
   return entry_info.id;
 }
Index: src/profile-generator.h
===================================================================
--- src/profile-generator.h     (revision 11329)
+++ src/profile-generator.h     (working copy)
@@ -788,6 +788,9 @@
   StringsStorage* names() { return &names_; }
   TokenEnumerator* token_enumerator() { return token_enumerator_; }

+  SnapshotObjectId FindObjectId(Address object_addr) {
+    return ids_.FindEntry(object_addr);
+  }
   SnapshotObjectId GetObjectId(Address object_addr, int object_size) {
     return ids_.FindOrAddEntry(object_addr, object_size);
   }
Index: test/cctest/test-heap-profiler.cc
===================================================================
--- test/cctest/test-heap-profiler.cc   (revision 11329)
+++ test/cctest/test-heap-profiler.cc   (working copy)
@@ -915,6 +915,42 @@
 }


+TEST(HeapSnapshotGetSnapshotObjectId) {
+  v8::HandleScope scope;
+  LocalContext env;
+  CompileRun("globalObject = {};\n");
+  const v8::HeapSnapshot* snapshot =
+      v8::HeapProfiler::TakeSnapshot(v8_str("get_snapshot_object_id"));
+  const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
+  const v8::HeapGraphNode* global_object =
+      GetProperty(global, v8::HeapGraphEdge::kShortcut, "globalObject");
+  CHECK(global_object);
+
+  v8::Local<v8::Value> globalObjectHandle =
+      env->Global()->Get(v8::String::New("globalObject"));
+  CHECK(!globalObjectHandle.IsEmpty());
+  CHECK(globalObjectHandle->IsObject());
+
+  v8::SnapshotObjectId id =
+      v8::HeapProfiler::GetSnapshotObjectId(globalObjectHandle);
+  CHECK_NE(static_cast<int>(v8::HeapProfiler::kUnknownObjectId),
+           id);
+  CHECK_EQ(static_cast<int>(id), global_object->GetId());
+}
+
+
+TEST(HeapSnapshotUnknownSnapshotObjectId) {
+  v8::HandleScope scope;
+  LocalContext env;
+  CompileRun("globalObject = {};\n");
+  const v8::HeapSnapshot* snapshot =
+      v8::HeapProfiler::TakeSnapshot(v8_str("unknown_object_id"));
+  const v8::HeapGraphNode* node =
+      snapshot->GetNodeById(v8::HeapProfiler::kUnknownObjectId);
+  CHECK_EQ(NULL, node);
+}
+
+
 namespace {

 class TestActivityControl : public v8::ActivityControl {


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

Reply via email to