Author: Antonio Cuni <[email protected]>
Branch: gc-hooks
Changeset: r94186:e6a1a204ce7a
Date: 2018-03-29 19:06 +0200
http://bitbucket.org/pypy/pypy/changeset/e6a1a204ce7a/

Log:    make it possible to disable the gc hooks

diff --git a/rpython/memory/gc/hook.py b/rpython/memory/gc/hook.py
--- a/rpython/memory/gc/hook.py
+++ b/rpython/memory/gc/hook.py
@@ -1,5 +1,10 @@
 class GcHooks(object):
 
+    def __init__(self):
+        self.gc_minor_enabled = False
+        self.gc_collect_step_enabled = False
+        self.gc_collect_enabled = False
+
     def on_gc_minor(self, total_memory_used, pinned_objects):
         """
         Called after a minor collection
diff --git a/rpython/memory/gc/incminimark.py b/rpython/memory/gc/incminimark.py
--- a/rpython/memory/gc/incminimark.py
+++ b/rpython/memory/gc/incminimark.py
@@ -1838,8 +1838,9 @@
         self.root_walker.finished_minor_collection()
         #
         debug_stop("gc-minor")
-        self.hooks.on_gc_minor(total_memory_used=total_memory_used,
-                               pinned_objects=self.pinned_objects_in_nursery)
+        if self.hooks.gc_minor_enabled:
+            self.hooks.on_gc_minor(total_memory_used=total_memory_used,
+                                   
pinned_objects=self.pinned_objects_in_nursery)
 
     def _reset_flag_old_objects_pointing_to_pinned(self, obj, ignore):
         ll_assert(self.header(obj).tid & GCFLAG_PINNED_OBJECT_PARENT_KNOWN != 
0,
@@ -2423,13 +2424,14 @@
                             self.stat_rawmalloced_total_size, " => ",
                             self.rawmalloced_total_size)
                 debug_stop("gc-collect-done")
-                self.hooks.on_gc_collect(
-                    count=self.num_major_collects,
-                    arenas_count_before=self.stat_ac_arenas_count,
-                    arenas_count_after=self.ac.arenas_count,
-                    arenas_bytes=self.ac.total_memory_used,
-                    rawmalloc_bytes_before=self.stat_rawmalloced_total_size,
-                    rawmalloc_bytes_after=self.rawmalloced_total_size)
+                if self.hooks.gc_collect_enabled:
+                    self.hooks.on_gc_collect(
+                        count=self.num_major_collects,
+                        arenas_count_before=self.stat_ac_arenas_count,
+                        arenas_count_after=self.ac.arenas_count,
+                        arenas_bytes=self.ac.total_memory_used,
+                        
rawmalloc_bytes_before=self.stat_rawmalloced_total_size,
+                        rawmalloc_bytes_after=self.rawmalloced_total_size)
                 #
                 # Set the threshold for the next major collection to be when we
                 # have allocated 'major_collection_threshold' times more than
@@ -2481,8 +2483,9 @@
 
         debug_print("stopping, now in gc state: ", GC_STATES[self.gc_state])
         debug_stop("gc-collect-step")
-        self.hooks.on_gc_collect_step(oldstate=oldstate,
-                                      newstate=self.gc_state)
+        if self.hooks.gc_collect_step_enabled:
+            self.hooks.on_gc_collect_step(oldstate=oldstate,
+                                          newstate=self.gc_state)
 
     def _sweep_old_objects_pointing_to_pinned(self, obj, new_list):
         if self.header(obj).tid & GCFLAG_VISITED:
diff --git a/rpython/memory/gc/test/test_hook.py 
b/rpython/memory/gc/test/test_hook.py
--- a/rpython/memory/gc/test/test_hook.py
+++ b/rpython/memory/gc/test/test_hook.py
@@ -9,6 +9,7 @@
 class MyGcHooks(GcHooks):
 
     def __init__(self):
+        GcHooks.__init__(self)
         self.reset()
 
     def reset(self):
@@ -50,6 +51,7 @@
         self.size_of_S = llmemory.raw_malloc_usage(size)
 
     def test_on_gc_minor(self):
+        self.gc.hooks.gc_minor_enabled = True
         self.malloc(S)
         self.gc._minor_collection()
         assert self.gc.hooks.minors == [
@@ -67,6 +69,8 @@
 
     def test_on_gc_collect(self):
         from rpython.memory.gc import incminimark as m
+        self.gc.hooks.gc_collect_step_enabled = True
+        self.gc.hooks.gc_collect_enabled = True
         self.malloc(S)
         self.gc.collect()
         assert self.gc.hooks.steps == [
@@ -95,3 +99,10 @@
              'rawmalloc_bytes_after': 0,
              'rawmalloc_bytes_before': 0}
             ]
+
+    def test_hook_disabled(self):
+        self.gc._minor_collection()
+        self.gc.collect()
+        assert self.gc.hooks.minors == []
+        assert self.gc.hooks.steps == []
+        assert self.gc.hooks.collects == []
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to