Tweak the new context disposal GC heuristic to cleanly separate
authorkasperl@chromium.org <kasperl@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Mar 2010 08:49:33 +0000 (08:49 +0000)
committerkasperl@chromium.org <kasperl@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Mon, 1 Mar 2010 08:49:33 +0000 (08:49 +0000)
it from the old heuristic (disabled if the new notications are
used) and make sure to not force a GC if one has already happened
after V8 received the last context disposal notification.
Review URL: http://codereview.chromium.org/660267

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3984 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

include/v8.h
src/api.cc
src/heap.cc
src/heap.h

index e8b19ec635a6b639afd57cc5ba0ed130aad6b090..44338b94d391ff345e215256b052f7d28d512913 100644 (file)
@@ -2474,9 +2474,8 @@ class V8EXPORT V8 {
   static void LowMemoryNotification();
 
   /**
-   * Optional notification that one or more context have been
-   * disposed. V8 may choose to collect garbage to get rid of any
-   * external memory associated with the disposed contexts.
+   * Optional notification that a context has been disposed. V8 uses
+   * these notifications to guide the garbage collection heuristic.
    */
   static void ContextDisposedNotification();
 
index c0e806b6632595d681a51cfa50c4480c0f0c4bd3..cb97e7fdcf77c6fc899359a8bf36bba48cbd3169 100644 (file)
@@ -438,7 +438,7 @@ bool V8::IsGlobalWeak(i::Object** obj) {
 void V8::DisposeGlobal(i::Object** obj) {
   LOG_API("DisposeGlobal");
   if (!i::V8::IsRunning()) return;
-  if ((*obj)->IsGlobalContext()) i::Heap::NotifyContextDisposed();
+  if ((*obj)->IsGlobalContext()) i::Heap::NotifyContextDisposedDeprecated();
   i::GlobalHandles::Destroy(obj);
 }
 
@@ -2823,7 +2823,7 @@ void v8::V8::LowMemoryNotification() {
 
 void v8::V8::ContextDisposedNotification() {
   if (!i::V8::IsRunning()) return;
-  i::Heap::CollectAllGarbageIfContextDisposed(true);
+  i::Heap::NotifyContextDisposed();
 }
 
 
@@ -2863,7 +2863,7 @@ Persistent<Context> v8::Context::New(
     // decide when should make a full GC.
 #else
     // Give the heap a chance to cleanup if we've disposed contexts.
-    i::Heap::CollectAllGarbageIfContextDisposed(false);
+    i::Heap::CollectAllGarbageIfContextDisposedDeprecated();
 #endif
     v8::Handle<ObjectTemplate> proxy_template = global_template;
     i::Handle<i::FunctionTemplateInfo> proxy_constructor;
index 6b3eeec629e586b4a6b7cb8615f32fb6ea81335d..c57ed5c34637cf9c9396fa4be1a533b96a739aee 100644 (file)
@@ -115,7 +115,10 @@ int Heap::gc_count_ = 0;
 
 int Heap::always_allocate_scope_depth_ = 0;
 int Heap::linear_allocation_scope_depth_ = 0;
-bool Heap::context_disposed_pending_ = false;
+
+int Heap::contexts_disposed_ = 0;
+bool Heap::context_disposed_use_deprecated_heuristic_ = true;
+bool Heap::context_disposed_deprecated_pending_ = false;
 
 #ifdef DEBUG
 bool Heap::allocation_allowed_ = true;
@@ -371,29 +374,29 @@ void Heap::CollectAllGarbage(bool force_compaction) {
 }
 
 
-void Heap::CollectAllGarbageIfContextDisposed(bool notified) {
-  // If the request has ever been the result of an explicit
-  // notification, we ignore non-notified requests. This is a
-  // temporary solution to let the two ways of achieving GC at
-  // context disposal time co-exist.
-  static bool ever_notified = false;
-  if (notified) ever_notified = true;
-  if (ever_notified && !notified) return;
-
+void Heap::CollectAllGarbageIfContextDisposedDeprecated() {
+  if (!context_disposed_use_deprecated_heuristic_) return;
   // If the garbage collector interface is exposed through the global
   // gc() function, we avoid being clever about forcing GCs when
   // contexts are disposed and leave it to the embedder to make
   // informed decisions about when to force a collection.
-  if (!FLAG_expose_gc && (notified || context_disposed_pending_)) {
+  if (!FLAG_expose_gc && context_disposed_deprecated_pending_) {
     HistogramTimerScope scope(&Counters::gc_context);
     CollectAllGarbage(false);
   }
-  context_disposed_pending_ = false;
+  context_disposed_deprecated_pending_ = false;
 }
 
 
 void Heap::NotifyContextDisposed() {
-  context_disposed_pending_ = true;
+  context_disposed_use_deprecated_heuristic_ = false;
+  contexts_disposed_++;
+}
+
+
+void Heap::NotifyContextDisposedDeprecated() {
+  if (!context_disposed_use_deprecated_heuristic_) return;
+  context_disposed_deprecated_pending_ = true;
 }
 
 
@@ -639,7 +642,9 @@ void Heap::MarkCompact(GCTracer* tracer) {
   Shrink();
 
   Counters::objs_since_last_full.Set(0);
-  context_disposed_pending_ = false;
+
+  contexts_disposed_ = 0;
+  context_disposed_deprecated_pending_ = false;
 }
 
 
@@ -3088,6 +3093,13 @@ bool Heap::IdleNotification() {
   static int number_idle_notifications = 0;
   static int last_gc_count = gc_count_;
 
+  if (!FLAG_expose_gc && (contexts_disposed_ > 0)) {
+    HistogramTimerScope scope(&Counters::gc_context);
+    CollectAllGarbage(false);
+    ASSERT(contexts_disposed_ == 0);
+    return false;
+  }
+
   bool finished = false;
 
   if (last_gc_count == gc_count_) {
index 41e18e7f7e131fae22afe36fece110dc68ecbb25..318aac32823879750cc16df6e32d1d07792cf6f7 100644 (file)
@@ -635,10 +635,11 @@ class Heap : public AllStatic {
 
   // Performs a full garbage collection if a context has been disposed
   // since the last time the check was performed.
-  static void CollectAllGarbageIfContextDisposed(bool notified);
+  static void CollectAllGarbageIfContextDisposedDeprecated();
 
   // Notify the heap that a context has been disposed.
   static void NotifyContextDisposed();
+  static void NotifyContextDisposedDeprecated();
 
   // Utility to invoke the scavenger. This is needed in test code to
   // ensure correct callback for weak global handles.
@@ -909,7 +910,11 @@ class Heap : public AllStatic {
 
   static int always_allocate_scope_depth_;
   static int linear_allocation_scope_depth_;
-  static bool context_disposed_pending_;
+
+  // For keeping track of context disposals.
+  static int contexts_disposed_;
+  static bool context_disposed_use_deprecated_heuristic_;
+  static bool context_disposed_deprecated_pending_;
 
 #if defined(V8_TARGET_ARCH_X64)
   static const int kMaxObjectSizeInNewSpace = 512*KB;