[pypy-commit] pypy gc-hooks: simplify MyGcHooks: instead of using a global GC_HOOK_STATS, we store the stats as an attribute and we ensure that we don't see gchooks from the main rpython program. This is closer to the approach that we will use for the real applevel hooks

antocuni pypy.commits at gmail.com
Fri Mar 30 19:59:57 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: gc-hooks
Changeset: r94192:ecb86e9ba2d6
Date: 2018-03-31 01:33 +0200
http://bitbucket.org/pypy/pypy/changeset/ecb86e9ba2d6/

Log:	simplify MyGcHooks: instead of using a global GC_HOOK_STATS, we
	store the stats as an attribute and we ensure that we don't see
	gchooks from the main rpython program. This is closer to the
	approach that we will use for the real applevel hooks

diff --git a/rpython/memory/test/test_transformed_gc.py b/rpython/memory/test/test_transformed_gc.py
--- a/rpython/memory/test/test_transformed_gc.py
+++ b/rpython/memory/test/test_transformed_gc.py
@@ -1398,21 +1398,14 @@
     collects = 0
 
     def reset(self):
-        self.minors = 0
-        self.steps = 0
-        self.collects = 0
+        # the NonConstant are needed so that the annotator annotates the
+        # fields as a generic SomeInteger(), instead of a constant 0. A call
+        # to this method MUST be seen during normal annotation, else the class
+        # is annotated only during GC transform, when it's too late
+        self.minors = NonConstant(0)
+        self.steps = NonConstant(0)
+        self.collects = NonConstant(0)
 
-    @staticmethod
-    def fix_annotation():
-        # this is needed to "fix" the annotation of GcHooksStats early, and
-        # must be called from the "main" program. Else, we change the
-        # annotation during the GC transform, when it's too late
-        if NonConstant(False):
-            GC_HOOKS_STATS.collects += 42
-            GC_HOOKS_STATS.steps += 42
-            GC_HOOKS_STATS.minors += 42
-
-GC_HOOKS_STATS = GcHooksStats()
 
 class MyGcHooks(GcHooks):
 
@@ -1420,17 +1413,18 @@
         self.gc_minor_enabled = True
         self.gc_collect_step_enabled = True
         self.gc_collect_enabled = True
+        self.stats = GcHooksStats()
 
     def on_gc_minor(self, total_memory_used, pinned_objects):
-        GC_HOOKS_STATS.minors += 1
+        self.stats.minors += 1
 
     def on_gc_collect_step(self, oldstate, newstate):
-        GC_HOOKS_STATS.steps += 1
+        self.stats.steps += 1
         
     def on_gc_collect(self, count, arenas_count_before, arenas_count_after,
                       arenas_bytes, rawmalloc_bytes_before,
                       rawmalloc_bytes_after):
-        GC_HOOKS_STATS.collects += 1
+        self.stats.collects += 1
 
 
 class TestIncrementalMiniMarkGC(TestMiniMarkGC):
@@ -1487,15 +1481,17 @@
 
     def define_gc_hooks(cls):
         gchooks = cls.gchooks
+        # it is important that we fish .stats OUTSIDE f(); we cannot see
+        # gchooks from within RPython code
+        stats = gchooks.stats
         def f():
-            GC_HOOKS_STATS.fix_annotation()
-            GC_HOOKS_STATS.reset()
+            stats.reset()
             # trigger two major collections
             llop.gc__collect(lltype.Void)
             llop.gc__collect(lltype.Void)
-            return (10000 * GC_HOOKS_STATS.collects +
-                      100 * GC_HOOKS_STATS.steps +
-                        1 * GC_HOOKS_STATS.minors)
+            return (10000 * stats.collects +
+                      100 * stats.steps +
+                        1 * stats.minors)
         return f
 
     def test_gc_hooks(self):
diff --git a/rpython/translator/goal/targetgcbench.py b/rpython/translator/goal/targetgcbench.py
--- a/rpython/translator/goal/targetgcbench.py
+++ b/rpython/translator/goal/targetgcbench.py
@@ -1,10 +1,10 @@
 from rpython.translator.goal import gcbench
-from rpython.memory.test.test_transformed_gc import MyGcHooks, GC_HOOKS_STATS
+from rpython.memory.test.test_transformed_gc import MyGcHooks
 
 # _____ Define and setup target ___
 
 def entry_point(argv):
-    GC_HOOKS_STATS.fix_annotation()
+    GC_HOOKS_STATS.reset()
     ret = gcbench.entry_point(argv)
     minors = GC_HOOKS_STATS.minors
     steps = GC_HOOKS_STATS.steps
@@ -16,6 +16,7 @@
     return ret
 
 gchooks = MyGcHooks()
+GC_HOOKS_STATS = gchooks.stats
 
 def target(*args):
     gcbench.ENABLE_THREADS = False    # not RPython


More information about the pypy-commit mailing list