[pypy-commit] pypy better-jit-hooks: support from the pypyjit module side

fijal noreply at buildbot.pypy.org
Sun Dec 25 18:44:31 CET 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: better-jit-hooks
Changeset: r50859:79a44aea441d
Date: 2011-12-25 19:44 +0200
http://bitbucket.org/pypy/pypy/changeset/79a44aea441d/

Log:	support from the pypyjit module side

diff --git a/pypy/jit/metainterp/jitprof.py b/pypy/jit/metainterp/jitprof.py
--- a/pypy/jit/metainterp/jitprof.py
+++ b/pypy/jit/metainterp/jitprof.py
@@ -29,8 +29,9 @@
 TOTAL_FREED_BRIDGES
 """
 
+counter_names = []
+
 def _setup():
-    counter_names = []
     names = counters.split()
     for i, name in enumerate(names):
         globals()[name] = i
diff --git a/pypy/module/pypyjit/__init__.py b/pypy/module/pypyjit/__init__.py
--- a/pypy/module/pypyjit/__init__.py
+++ b/pypy/module/pypyjit/__init__.py
@@ -8,15 +8,18 @@
         'set_param':    'interp_jit.set_param',
         'residual_call': 'interp_jit.residual_call',
         'set_compile_hook': 'interp_jit.set_compile_hook',
+        'set_abort_hook': 'interp_jit.set_abort_hook',
         'DebugMergePoint': 'interp_resop.W_DebugMergePoint',
     }
 
     def setup_after_space_initialization(self):
         # force the __extend__ hacks to occur early
         from pypy.module.pypyjit.interp_jit import pypyjitdriver
+        from pypy.module.pypyjit.policy import pypy_portal
         # add the 'defaults' attribute
         from pypy.rlib.jit import PARAMETERS
         space = self.space
         pypyjitdriver.space = space
         w_obj = space.wrap(PARAMETERS)
         space.setattr(space.wrap(self), space.wrap('defaults'), w_obj)
+        pypy_portal.space = space
diff --git a/pypy/module/pypyjit/interp_jit.py b/pypy/module/pypyjit/interp_jit.py
--- a/pypy/module/pypyjit/interp_jit.py
+++ b/pypy/module/pypyjit/interp_jit.py
@@ -229,6 +229,7 @@
 
     def __init__(self, space):
         self.w_compile_hook = space.w_None
+        self.w_abort_hook = space.w_None
 
 def set_compile_hook(space, w_hook):
     """ set_compile_hook(hook)
@@ -254,3 +255,16 @@
     cache.w_compile_hook = w_hook
     cache.in_recursion = NonConstant(False)
     return space.w_None
+
+def set_abort_hook(space, w_hook):
+    """ set_abort_hook(hook)
+
+    Set a hook (callable) that will be called each time there is tracing
+    aborted due to some reason. The hook will be called with string describing
+    the reason as an argument
+    """
+    cache = space.fromcache(Cache)
+    cache.w_abort_hook = w_hook
+    cache.in_recursion = NonConstant(False)
+    return space.w_None
+    
diff --git a/pypy/module/pypyjit/policy.py b/pypy/module/pypyjit/policy.py
--- a/pypy/module/pypyjit/policy.py
+++ b/pypy/module/pypyjit/policy.py
@@ -1,4 +1,25 @@
 from pypy.jit.codewriter.policy import JitPolicy
+from pypy.rlib.jit import JitPortal
+from pypy.module.pypyjit.interp_jit import Cache
+from pypy.interpreter.error import OperationError
+from pypy.jit.metainterp.jitprof import counter_names
+
+class PyPyPortal(JitPortal):
+    def on_abort(self, reason):
+        space = self.space
+        cache = space.fromcache(Cache)
+        if cache.in_recursion:
+            return
+        if space.is_true(cache.w_abort_hook):
+            cache.in_recursion = True
+            try:
+                space.call_function(cache.w_abort_hook,
+                                    space.wrap(counter_names[reason]))
+            except OperationError, e:
+                e.write_unraisable(space, "jit hook ", cache.w_abort_hook)
+            cache.in_recursion = False
+
+pypy_portal = PyPyPortal()
 
 class PyPyJitPolicy(JitPolicy):
 
diff --git a/pypy/module/pypyjit/test/test_jit_hook.py b/pypy/module/pypyjit/test/test_jit_hook.py
--- a/pypy/module/pypyjit/test/test_jit_hook.py
+++ b/pypy/module/pypyjit/test/test_jit_hook.py
@@ -10,8 +10,10 @@
                                       cast_base_ptr_to_instance)
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.module.pypyjit.interp_jit import pypyjitdriver
+from pypy.module.pypyjit.policy import pypy_portal
 from pypy.jit.tool.oparser import parse
 from pypy.jit.metainterp.typesystem import llhelper
+from pypy.jit.metainterp.jitprof import ABORT_TOO_LONG
 
 class MockSD(object):
     class cpu(object):
@@ -46,9 +48,13 @@
 
         def interp_on_compile_bridge():
             pypyjitdriver.on_compile_bridge(logger, JitCellToken(), oplist, 0)
+
+        def interp_on_abort():
+            pypy_portal.on_abort(ABORT_TOO_LONG)
         
         cls.w_on_compile = space.wrap(interp2app(interp_on_compile))
         cls.w_on_compile_bridge = space.wrap(interp2app(interp_on_compile_bridge))
+        cls.w_on_abort = space.wrap(interp2app(interp_on_abort))
 
     def test_on_compile(self):
         import pypyjit
@@ -124,3 +130,14 @@
         import pypyjit
         dmp = pypyjit.DebugMergePoint(0, 0, self.f.func_code)
         assert dmp.code is self.f.func_code 
+
+    def test_on_abort(self):
+        import pypyjit
+        l = []
+
+        def hook(reason):
+            l.append(reason)
+        
+        pypyjit.set_abort_hook(hook)
+        self.on_abort()
+        assert l == ['ABORT_TOO_LONG']
diff --git a/pypy/translator/goal/targetpypystandalone.py b/pypy/translator/goal/targetpypystandalone.py
--- a/pypy/translator/goal/targetpypystandalone.py
+++ b/pypy/translator/goal/targetpypystandalone.py
@@ -226,8 +226,8 @@
         return self.get_entry_point(config)
 
     def jitpolicy(self, driver):
-        from pypy.module.pypyjit.policy import PyPyJitPolicy
-        return PyPyJitPolicy()
+        from pypy.module.pypyjit.policy import PyPyJitPolicy, pypy_portal
+        return PyPyJitPolicy(pypy_portal)
     
     def get_entry_point(self, config):
         from pypy.tool.lib_pypy import import_from_lib_pypy


More information about the pypy-commit mailing list