[pypy-commit] pypy vmprof: refactor the code to not do anything in case of lack of vmprof

fijal noreply at buildbot.pypy.org
Thu Mar 19 10:44:09 CET 2015


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: vmprof
Changeset: r76467:995ef117566b
Date: 2015-03-19 11:44 +0200
http://bitbucket.org/pypy/pypy/changeset/995ef117566b/

Log:	refactor the code to not do anything in case of lack of vmprof

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -666,30 +666,15 @@
             assert ec is not None
             return ec
 
-    def _open_code_info_file(self):
+    def register_code_callback(self, callback):
         ec = self.getexecutioncontext()
-        try:
-            ec.code_info_file = os.tmpfile()
-        except:
-            ec.code_info_file_present = False # we failed to open it
+        ec._code_callback = callback
 
     def register_code_object(self, pycode):
         ec = self.getexecutioncontext()
-        if ec.code_info_file is None:
-            self._open_code_info_file()
-        if not ec.code_info_file_present:
+        if ec._code_callback is None:
             return
-        try:
-            rfile.write_int(ec.code_info_file, pycode._unique_id)
-            s = pycode._get_full_name()
-            rfile.write_int(ec.code_info_file, len(s))
-            ec.code_info_file.write(s)
-        except OSError:
-            ec.code_info_file_present = False
-            try:
-                ec.code_info_file.close()
-            except:
-                pass # can fail, ignore
+        ec._code_callback(self, pycode)
     
     def _freeze_(self):
         return True
diff --git a/pypy/interpreter/executioncontext.py b/pypy/interpreter/executioncontext.py
--- a/pypy/interpreter/executioncontext.py
+++ b/pypy/interpreter/executioncontext.py
@@ -33,13 +33,12 @@
         self.profilefunc = None
         self.w_profilefuncarg = None
         self.thread_disappeared = False   # might be set to True after os.fork()
-        self.code_info_file = None
-        self.code_info_file_present = True
 
         if sys.maxint == 2147483647:
             self._code_unique_id = 0 # XXX this is wrong, it won't work on 32bit
         else:
             self._code_unique_id = 0x7000000000000000
+        self._code_callback = None
 
     @staticmethod
     def _mark_thread_disappeared(space):
diff --git a/pypy/module/_vmprof/interp_vmprof.py b/pypy/module/_vmprof/interp_vmprof.py
--- a/pypy/module/_vmprof/interp_vmprof.py
+++ b/pypy/module/_vmprof/interp_vmprof.py
@@ -3,13 +3,14 @@
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rtyper.annlowlevel import cast_instance_to_gcref, cast_base_ptr_to_instance
 from rpython.rlib.objectmodel import we_are_translated
-from rpython.rlib import jit, rposix, entrypoint
+from rpython.rlib import jit, rposix, entrypoint, rgc
 from rpython.rtyper.tool import rffi_platform as platform
 from rpython.rlib.rstring import StringBuilder
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import oefmt, wrap_oserror, OperationError
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter.pyframe import PyFrame
+from pypy.interpreter.pycode import PyCode
 
 ROOT = py.path.local(__file__).join('..')
 SRC = ROOT.join('src')
@@ -120,6 +121,9 @@
         b.append(chr((l >> 48) & 0xff))
         b.append(chr((l >> 56) & 0xff))
 
+def try_cast_to_pycode(gcref):
+    return rgc.try_cast_gcref_to_instance(PyCode, gcref)
+
 class VMProf(object):
     def __init__(self):
         self.is_enabled = False
@@ -137,6 +141,8 @@
             if we_are_translated():
                 pypy_vmprof_init()
             self.ever_enabled = True
+        self.gather_all_code_objs(space)
+        space.register_code_callback(self.register_code)
         if we_are_translated():
             # does not work untranslated
             res = vmprof_enable(fileno, period, 0,
@@ -147,6 +153,11 @@
             raise wrap_oserror(space, OSError(rposix.get_saved_errno(),
                                               "_vmprof.enable"))
 
+    def gather_all_code_objs(self, space):
+        all_code_objs = rgc.do_get_objects(try_cast_to_pycode)
+        for code in all_code_objs:
+            self.register_code(space, code)
+
     def write_header(self, fileno, period):
         if period == -1:
             period_usec = 1000000 / 100 #  100hz
@@ -177,6 +188,7 @@
             raise oefmt(space.w_ValueError, "_vmprof not enabled")
         self.is_enabled = False
         self.fileno = -1
+        space.register_code_callback(None)
         if we_are_translated():
            # does not work untranslated
             res = vmprof_disable()
diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -695,7 +695,7 @@
 
 @specialize.arg(0)
 def do_get_objects(callback):
-    """ Get all the objects that satisfy callback(gcref) -> True
+    """ Get all the objects that satisfy callback(gcref) -> obj
     """
     roots = [gcref for gcref in get_rpy_roots() if gcref]
     pending = roots[:]


More information about the pypy-commit mailing list