Author: Antonio Cuni <anto.c...@gmail.com>
Branch: gc-hooks
Changeset: r94300:b41028f24d1e
Date: 2018-04-10 00:18 +0200
http://bitbucket.org/pypy/pypy/changeset/b41028f24d1e/

Log:    fix the app-level hooks to use the new 'duration' param

diff --git a/pypy/module/gc/hook.py b/pypy/module/gc/hook.py
--- a/pypy/module/gc/hook.py
+++ b/pypy/module/gc/hook.py
@@ -31,14 +31,16 @@
     def is_gc_collect_enabled(self):
         return self.w_hooks.gc_collect_enabled
 
-    def on_gc_minor(self, total_memory_used, pinned_objects):
+    def on_gc_minor(self, duration, total_memory_used, pinned_objects):
         action = self.w_hooks.gc_minor
+        action.duration = duration
         action.total_memory_used = total_memory_used
         action.pinned_objects = pinned_objects
         action.fire()
 
-    def on_gc_collect_step(self, oldstate, newstate):
+    def on_gc_collect_step(self, duration, oldstate, newstate):
         action = self.w_hooks.gc_collect_step
+        action.duration = duration
         action.oldstate = oldstate
         action.newstate = newstate
         action.fire()
@@ -106,6 +108,7 @@
 
 
 class GcMinorHookAction(AsyncAction):
+    duration = 0
     total_memory_used = 0
     pinned_objects = 0
 
@@ -118,16 +121,21 @@
         # BEFORE we do the gc transform; this makes sure that everything is
         # annotated with the correct types
         if NonConstant(False):
+            self.duration = NonConstant(-42)
             self.total_memory_used = NonConstant(r_uint(42))
             self.pinned_objects = NonConstant(-42)
             self.fire()
 
     def perform(self, ec, frame):
-        w_stats = W_GcMinorStats(self.total_memory_used, self.pinned_objects)
+        w_stats = W_GcMinorStats(
+            self.duration,
+            self.total_memory_used,
+            self.pinned_objects)
         self.space.call_function(self.w_callable, w_stats)
 
 
 class GcCollectStepHookAction(AsyncAction):
+    duration = 0
     oldstate = 0
     newstate = 0
 
@@ -140,12 +148,16 @@
         # BEFORE we do the gc transform; this makes sure that everything is
         # annotated with the correct types
         if NonConstant(False):
+            self.duration = NonConstant(-42)
             self.oldstate = NonConstant(-42)
             self.newstate = NonConstant(-42)
             self.fire()
 
     def perform(self, ec, frame):
-        w_stats = W_GcCollectStepStats(self.oldstate, self.newstate)
+        w_stats = W_GcCollectStepStats(
+            self.duration,
+            self.oldstate,
+            self.newstate)
         self.space.call_function(self.w_callable, w_stats)
 
 
@@ -186,14 +198,16 @@
 
 class W_GcMinorStats(W_Root):
 
-    def __init__(self, total_memory_used, pinned_objects):
+    def __init__(self, duration, total_memory_used, pinned_objects):
+        self.duration = duration
         self.total_memory_used = total_memory_used
         self.pinned_objects = pinned_objects
 
 
 class W_GcCollectStepStats(W_Root):
 
-    def __init__(self, oldstate, newstate):
+    def __init__(self, duration, oldstate, newstate):
+        self.duration = duration
         self.oldstate = oldstate
         self.newstate = newstate
 
@@ -239,6 +253,7 @@
 W_GcMinorStats.typedef = TypeDef(
     "GcMinorStats",
     **wrap_many_ints(W_GcMinorStats, (
+        "duration",
         "total_memory_used",
         "pinned_objects"))
     )
@@ -251,6 +266,7 @@
     STATE_FINALIZING = incminimark.STATE_FINALIZING,
     GC_STATES = tuple(incminimark.GC_STATES),
     **wrap_many_ints(W_GcCollectStepStats, (
+        "duration",
         "oldstate",
         "newstate"))
     )
diff --git a/pypy/module/gc/test/test_hook.py b/pypy/module/gc/test/test_hook.py
--- a/pypy/module/gc/test/test_hook.py
+++ b/pypy/module/gc/test/test_hook.py
@@ -12,13 +12,13 @@
         space = cls.space
         gchooks = space.fromcache(LowLevelGcHooks)
 
-        @unwrap_spec(ObjSpace, r_uint, int)
-        def fire_gc_minor(space, total_memory_used, pinned_objects):
-            gchooks.fire_gc_minor(total_memory_used, pinned_objects)
+        @unwrap_spec(ObjSpace, int, r_uint, int)
+        def fire_gc_minor(space, duration, total_memory_used, pinned_objects):
+            gchooks.fire_gc_minor(duration, total_memory_used, pinned_objects)
 
-        @unwrap_spec(ObjSpace, int, int)
-        def fire_gc_collect_step(space, oldstate, newstate):
-            gchooks.fire_gc_collect_step(oldstate, newstate)
+        @unwrap_spec(ObjSpace, int, int, int)
+        def fire_gc_collect_step(space, duration, oldstate, newstate):
+            gchooks.fire_gc_collect_step(duration, oldstate, newstate)
 
         @unwrap_spec(ObjSpace, int, int, int, r_uint, r_uint, r_uint)
         def fire_gc_collect(space, a, b, c, d, e, f):
@@ -26,8 +26,8 @@
 
         @unwrap_spec(ObjSpace)
         def fire_many(space):
-            gchooks.fire_gc_minor(0, 0)
-            gchooks.fire_gc_collect_step(0, 0)
+            gchooks.fire_gc_minor(0, 0, 0)
+            gchooks.fire_gc_collect_step(0, 0, 0)
             gchooks.fire_gc_collect(1, 2, 3, 4, 5, 6)
 
         cls.w_fire_gc_minor = space.wrap(interp2app(fire_gc_minor))
@@ -45,40 +45,42 @@
         import gc
         lst = []
         def on_gc_minor(stats):
-            lst.append((stats.total_memory_used, stats.pinned_objects))
+            tup = (stats.duration, stats.total_memory_used, 
stats.pinned_objects)
+            lst.append(tup)
         gc.hooks.on_gc_minor = on_gc_minor
-        self.fire_gc_minor(10, 20)
-        self.fire_gc_minor(30, 40)
+        self.fire_gc_minor(10, 20, 30)
+        self.fire_gc_minor(40, 50, 60)
         assert lst == [
-            (10, 20),
-            (30, 40),
+            (10, 20, 30),
+            (40, 50, 60),
             ]
         #
         gc.hooks.on_gc_minor = None
-        self.fire_gc_minor(50, 60)  # won't fire because the hooks is disabled
+        self.fire_gc_minor(70, 80, 90)  # won't fire because the hooks is 
disabled
         assert lst == [
-            (10, 20),
-            (30, 40),
+            (10, 20, 30),
+            (40, 50, 60),
             ]
 
     def test_on_gc_collect_step(self):
         import gc
         lst = []
         def on_gc_collect_step(stats):
-            lst.append((stats.oldstate, stats.newstate))
+            tup = (stats.duration, stats.oldstate, stats.newstate)
+            lst.append(tup)
         gc.hooks.on_gc_collect_step = on_gc_collect_step
-        self.fire_gc_collect_step(10, 20)
-        self.fire_gc_collect_step(30, 40)
+        self.fire_gc_collect_step(10, 20, 30)
+        self.fire_gc_collect_step(40, 50, 60)
         assert lst == [
-            (10, 20),
-            (30, 40),
+            (10, 20, 30),
+            (40, 50, 60),
             ]
         #
         gc.hooks.on_gc_collect_step = None
-        self.fire_gc_collect_step(50, 60)  # won't fire
+        self.fire_gc_collect_step(70, 80, 90)  # won't fire
         assert lst == [
-            (10, 20),
-            (30, 40),
+            (10, 20, 30),
+            (40, 50, 60),
             ]
 
     def test_on_gc_collect(self):
@@ -136,7 +138,7 @@
         self.fire_many()
         assert myhooks.lst == ['minor', 'step', 'collect']
         myhooks.lst[:] = []
-        self.fire_gc_minor(0, 0)
+        self.fire_gc_minor(0, 0, 0)
         assert myhooks.lst == ['minor']
         gc.hooks.reset()
         assert gc.hooks.on_gc_minor is None
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to