[pypy-svn] r56440 - in pypy/branch/builtin-profiling/pypy/interpreter: . test

stephan at codespeak.net stephan at codespeak.net
Fri Jul 11 11:20:38 CEST 2008


Author: stephan
Date: Fri Jul 11 11:20:38 2008
New Revision: 56440

Modified:
   pypy/branch/builtin-profiling/pypy/interpreter/executioncontext.py
   pypy/branch/builtin-profiling/pypy/interpreter/gateway.py
   pypy/branch/builtin-profiling/pypy/interpreter/test/test_executioncontext.py
Log:
(stephan, anto)
first (non working) checkin to support profiling of builtin functions.
(much needs to be changed)


Modified: pypy/branch/builtin-profiling/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/builtin-profiling/pypy/interpreter/executioncontext.py	(original)
+++ pypy/branch/builtin-profiling/pypy/interpreter/executioncontext.py	Fri Jul 11 11:20:38 2008
@@ -104,6 +104,31 @@
         space.setitem(w_globals, w_key, w_value)
         return w_globals
 
+    def c_call_llprofile(self, w_func):
+        "Profile the call of a builtin function"
+        if self.profilefunc is not None:
+            self._llprofile('c_call', w_func)
+
+    def c_return_llprofile(self, w_retval):
+        "Profile the return from a builtin function"
+        if self.profilefunc is not None:
+            self._llprofile('c_return', w_retval)
+
+    def c_exception_llprofile(self, operationerr):
+        "Profile function called upon OperationError."
+        if self.profilefunc is not None:
+            self._llprofile('c_exception', operationerr)
+
+    def _llprofile(self, event, w_arg):
+        fr = self.framestack.items
+        space = self.space
+        w_callback = self.profilefunc
+        if w_callback is not None:
+            frame = None
+            if fr:
+                frame = fr[0]
+            self.profilefunc(space, self.w_profilefuncarg, frame, event, w_arg)
+
     def call_trace(self, frame):
         "Trace the call of a function"
         if self.w_tracefunc is not None or self.profilefunc is not None:
@@ -182,7 +207,7 @@
         space = self.space
         
         # Tracing cases
-        if event == 'call':
+        if event in ['call']:
             w_callback = self.w_tracefunc
         else:
             w_callback = frame.w_f_trace

Modified: pypy/branch/builtin-profiling/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/builtin-profiling/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/builtin-profiling/pypy/interpreter/gateway.py	Fri Jul 11 11:20:38 2008
@@ -480,10 +480,13 @@
 
     def funcrun(self, func, args):
         space = func.space
+        ctx = space.getexecutioncontext()
         activation = self.activation
         scope_w = args.parse(func.name, self.sig, func.defs_w)
         try:
+            ctx.c_call_llprofile(activation._run)
             w_result = activation._run(space, scope_w)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt,
                                  space.w_None) 
@@ -509,8 +512,11 @@
 
     def funcrun(self, func, args):
         space = func.space
+        ctx = space.getexecutioncontext()
         try:
+            ctx.c_call_llprofile(self.func__args__)
             w_result = self.func__args__(space, args)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 
@@ -533,13 +539,16 @@
 
     def funcrun(self, func, args):
         space = func.space
+        ctx = space.getexecutioncontext()
         try:
             w_obj, newargs = args.popfirst()
         except IndexError:
             return BuiltinCode.funcrun(self, func, args)
         else:
             try:
+                ctx.c_call_llprofile(self.func__args__)
                 w_result = self.func__args__(space, w_obj, newargs)
+                ctx.c_return_llprofile(w_result)
             except KeyboardInterrupt: 
                 raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
             except MemoryError: 
@@ -562,7 +571,10 @@
     def fastcall_0(self, space, w_func):
         self = hint(self, deepfreeze=True)
         try:
+            ctx = space.getexecutioncontext()
+            ctx.c_call_llprofile(self.fastfunc_0)
             w_result = self.fastfunc_0(space)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 
@@ -578,7 +590,10 @@
     def fastcall_1(self, space, w_func, w1):
         self = hint(self, deepfreeze=True)
         try:
+            ctx = space.getexecutioncontext()
+            ctx.c_call_llprofile(self.fastfunc_1)
             w_result = self.fastfunc_1(space, w1)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 
@@ -601,7 +616,10 @@
     def fastcall_2(self, space, w_func, w1, w2):
         self = hint(self, deepfreeze=True)
         try:
+            ctx = space.getexecutioncontext()
+            ctx.c_call_llprofile(self.fastfunc_2)
             w_result = self.fastfunc_2(space, w1, w2)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 
@@ -621,10 +639,13 @@
         return w_result
 
 class BuiltinCode3(BuiltinCode):
-    def fastcall_3(self, space, func, w1, w2, w3):
+    def fastcall_3(self, space, w_func, w1, w2, w3):
         self = hint(self, deepfreeze=True)
         try:
+            ctx = space.getexecutioncontext()
+            ctx.c_call_llprofile(self.fastfunc_3)
             w_result = self.fastfunc_3(space, w1, w2, w3)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 
@@ -644,10 +665,13 @@
         return w_result
 
 class BuiltinCode4(BuiltinCode):
-    def fastcall_4(self, space, func, w1, w2, w3, w4):
+    def fastcall_4(self, space, w_func, w1, w2, w3, w4):
         self = hint(self, deepfreeze=True)
         try:
+            ctx = space.getexecutioncontext()
+            ctx.c_call_llprofile(self.fastfunc_4)
             w_result = self.fastfunc_4(space, w1, w2, w3, w4)
+            ctx.c_return_llprofile(w_result)
         except KeyboardInterrupt: 
             raise OperationError(space.w_KeyboardInterrupt, space.w_None) 
         except MemoryError: 

Modified: pypy/branch/builtin-profiling/pypy/interpreter/test/test_executioncontext.py
==============================================================================
--- pypy/branch/builtin-profiling/pypy/interpreter/test/test_executioncontext.py	(original)
+++ pypy/branch/builtin-profiling/pypy/interpreter/test/test_executioncontext.py	Fri Jul 11 11:20:38 2008
@@ -62,12 +62,11 @@
         assert space.sys.checkinterval / 10 < i < space.sys.checkinterval * 3
 
     def test_llprofile(self):
-        py.test.skip("not working yet")
         l = []
         
         def profile_func(space, w_arg, frame, event, w_aarg):
             assert w_arg is space.w_None
-            l.append(event)
+            l.append((event, frame, w_aarg))
         
         space = self.space
         space.getexecutioncontext().setllprofile(profile_func, space.w_None)
@@ -76,5 +75,7 @@
         l.append(3)
         """)
         space.getexecutioncontext().setllprofile(None, None)
+        from pprint import pprint
+        pprint([(x, y, z) for x,y,z in l if x in ('call','c_call')])
         assert l == ['call', 'return', 'call', 'c_call', 'c_return', 'return']
 



More information about the Pypy-commit mailing list