Reviewers: Vyacheslav Egorov,

Message:
plz review

Description:
Add HeapProfiler::GetPersistentHandleCount to be able to track the number of
persistent handles

It turns out that an increasing number of persistent handles is a good signal
for bugs in the bindings layer

BUG=none
TEST=cctest/test-heap-profiler/PersistentHandleCount

Please review this at https://chromiumcodereview.appspot.com/9620007/

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

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


Index: include/v8-profiler.h
===================================================================
--- include/v8-profiler.h       (revision 10957)
+++ include/v8-profiler.h       (working copy)
@@ -430,6 +430,9 @@
    * handle.
    */
   static const uint16_t kPersistentHandleNoClassId = 0;
+
+  /** Returns the number of currently existing persistent handles. */
+  static int GetPersistentHandleCount();
 };


Index: src/api.cc
===================================================================
--- src/api.cc  (revision 10957)
+++ src/api.cc  (working copy)
@@ -6070,8 +6070,13 @@
                                                              callback);
 }

+int HeapProfiler::GetPersistentHandleCount() {
+  i::Isolate* isolate = i::Isolate::Current();
+  return isolate->global_handles()->NumberOfGlobalHandles();
+}


+
 v8::Testing::StressType internal::Testing::stress_type_ =
     v8::Testing::kStressTypeOpt;

Index: src/global-handles.cc
===================================================================
--- src/global-handles.cc       (revision 10957)
+++ src/global-handles.cc       (working copy)
@@ -384,6 +384,7 @@
     : isolate_(isolate),
       number_of_weak_handles_(0),
       number_of_global_object_weak_handles_(0),
+      number_of_global_handles_(0),
       first_block_(NULL),
       first_used_block_(NULL),
       first_free_(NULL),
@@ -403,6 +404,7 @@

 Handle<Object> GlobalHandles::Create(Object* value) {
   isolate_->counters()->global_handles()->Increment();
+  number_of_global_handles_++;
   if (first_free_ == NULL) {
     first_block_ = new NodeBlock(first_block_);
     first_block_->PutNodesOnFreeList(&first_free_);
@@ -423,6 +425,7 @@

 void GlobalHandles::Destroy(Object** location) {
   isolate_->counters()->global_handles()->Decrement();
+  number_of_global_handles_--;
   if (location == NULL) return;
   Node::FromLocation(location)->Release(this);
 }
Index: src/global-handles.h
===================================================================
--- src/global-handles.h        (revision 10957)
+++ src/global-handles.h        (working copy)
@@ -143,6 +143,11 @@
     return number_of_global_object_weak_handles_;
   }

+  // Returns the current number of handles to global objects.
+  int NumberOfGlobalHandles() {
+    return number_of_global_handles_;
+  }
+
   // Clear the weakness of a global handle.
   void ClearWeakness(Object** location);

@@ -248,6 +253,9 @@
   // number_of_weak_handles_.
   int number_of_global_object_weak_handles_;

+  // Field always containing the number of handles to global objects.
+  int number_of_global_handles_;
+
   // List of all allocated node blocks.
   NodeBlock* first_block_;

Index: test/cctest/test-heap-profiler.cc
===================================================================
--- test/cctest/test-heap-profiler.cc   (revision 10957)
+++ test/cctest/test-heap-profiler.cc   (working copy)
@@ -1279,3 +1279,36 @@
       GetProperty(fun, v8::HeapGraphEdge::kInternal, "shared");
   CHECK(HasWeakEdge(shared));
 }
+
+TEST(PersistentHandleCount) {
+  v8::HandleScope scope;
+  LocalContext env;
+
+ // V8 also uses global handles internally, so we can't test for an absolute
+  // number.
+  int global_handle_count = v8::HeapProfiler::GetPersistentHandleCount();
+
+  // Create some persistent handles.
+  v8::Persistent<v8::String> p_AAA =
+      v8::Persistent<v8::String>::New(v8_str("AAA"));
+  CHECK_EQ(global_handle_count + 1,
+           v8::HeapProfiler::GetPersistentHandleCount());
+  v8::Persistent<v8::String> p_BBB =
+      v8::Persistent<v8::String>::New(v8_str("BBB"));
+  CHECK_EQ(global_handle_count + 2,
+           v8::HeapProfiler::GetPersistentHandleCount());
+  v8::Persistent<v8::String> p_CCC =
+      v8::Persistent<v8::String>::New(v8_str("CCC"));
+  CHECK_EQ(global_handle_count + 3,
+           v8::HeapProfiler::GetPersistentHandleCount());
+
+  // Dipose the persistent handles in a different order.
+  p_AAA.Dispose();
+  CHECK_EQ(global_handle_count + 2,
+           v8::HeapProfiler::GetPersistentHandleCount());
+  p_CCC.Dispose();
+  CHECK_EQ(global_handle_count + 1,
+           v8::HeapProfiler::GetPersistentHandleCount());
+  p_BBB.Dispose();
+ CHECK_EQ(global_handle_count, v8::HeapProfiler::GetPersistentHandleCount());
+}


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

Reply via email to