[pypy-svn] r70514 - in pypy/branch/jit-profiling/pypy: interpreter interpreter/test module/_lsprof
fijal at codespeak.net
fijal at codespeak.net
Mon Jan 11 21:01:17 CET 2010
Author: fijal
Date: Mon Jan 11 21:01:15 2010
New Revision: 70514
Modified:
pypy/branch/jit-profiling/pypy/interpreter/executioncontext.py
pypy/branch/jit-profiling/pypy/interpreter/test/test_executioncontext.py
pypy/branch/jit-profiling/pypy/module/_lsprof/interp_lsprof.py
Log:
Change the internal mode of profiling to pass around numbers instead of strings.
Modified: pypy/branch/jit-profiling/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/jit-profiling/pypy/interpreter/executioncontext.py (original)
+++ pypy/branch/jit-profiling/pypy/interpreter/executioncontext.py Mon Jan 11 21:01:15 2010
@@ -5,10 +5,27 @@
from pypy.rlib.unroll import unrolling_iterable
from pypy.rlib import jit
+TRACE_EXCEPTION = 0
+TRACE_RETURN = 1
+_PROFILING_FIRST = 2
+TRACE_CALL = 2
+TRACE_LEAVEFRAME = 3
+TRACE_C_CALL = 4
+TRACE_C_RETURN = 5
+TRACE_C_EXCEPTION = 6
+_PROFILING_LAST = 6
+TRACE_LINE = 7
+
+TRACE_TO_NAME = []
+for i, k in enumerate(['exception', 'return', 'call', 'leaveframe', 'c_call',
+ 'c_return', 'c_exception', 'line']):
+ TRACE_TO_NAME.append(k)
+ assert globals()['TRACE_' + k.upper()] == i
+
def app_profile_call(space, w_callable, frame, event, w_arg):
space.call_function(w_callable,
space.wrap(frame),
- space.wrap(event), w_arg)
+ space.wrap(TRACE_TO_NAME[event]), w_arg)
class ExecutionContext(object):
"""An ExecutionContext holds the state of an execution thread
@@ -59,7 +76,7 @@
def leave(self, frame):
try:
if self.profilefunc:
- self._trace(frame, 'leaveframe', self.space.w_None)
+ self._trace(frame, TRACE_LEAVEFRAME, self.space.w_None)
finally:
self.topframeref = frame.f_backref
self.framestackdepth -= 1
@@ -144,33 +161,33 @@
if self.profilefunc is None:
frame.is_being_profiled = False
else:
- self._trace(frame, 'c_call', w_func)
+ self._trace(frame, TRACE_C_CALL, w_func)
def c_return_trace(self, frame, w_retval):
"Profile the return from a builtin function"
if self.profilefunc is None:
frame.is_being_profiled = False
else:
- self._trace(frame, 'c_return', w_retval)
+ self._trace(frame, TRACE_C_RETURN, w_retval)
def c_exception_trace(self, frame, w_exc):
"Profile function called upon OperationError."
if self.profilefunc is None:
frame.is_being_profiled = False
else:
- self._trace(frame, 'c_exception', w_exc)
+ self._trace(frame, TRACE_C_EXCEPTION, w_exc)
def call_trace(self, frame):
"Trace the call of a function"
if self.w_tracefunc is not None or self.profilefunc is not None:
- self._trace(frame, 'call', self.space.w_None)
+ self._trace(frame, TRACE_CALL, self.space.w_None)
if self.profilefunc:
frame.is_being_profiled = True
def return_trace(self, frame, w_retval):
"Trace the return from a function"
if self.w_tracefunc is not None:
- self._trace(frame, 'return', w_retval)
+ self._trace(frame, TRACE_RETURN, w_retval)
def bytecode_trace(self, frame):
"Trace function called before each bytecode."
@@ -197,7 +214,7 @@
"Trace function called upon OperationError."
operationerr.record_interpreter_traceback()
if self.w_tracefunc is not None:
- self._trace(frame, 'exception', None, operationerr)
+ self._trace(frame, TRACE_EXCEPTION, None, operationerr)
#operationerr.print_detailed_traceback(self.space)
def sys_exc_info(self): # attn: the result is not the wrapped sys.exc_info() !!!
@@ -260,18 +277,19 @@
self.is_tracing = is_tracing
def _trace(self, frame, event, w_arg, operr=None):
+ assert isinstance(event, int)
if self.is_tracing or frame.hide():
return
space = self.space
# Tracing cases
- if event == 'call':
+ if event == TRACE_CALL:
w_callback = self.w_tracefunc
else:
w_callback = frame.w_f_trace
- if w_callback is not None and event != "leaveframe":
+ if w_callback is not None and event != TRACE_LEAVEFRAME:
if operr is not None:
w_arg = space.newtuple([operr.w_type, operr.w_value,
space.wrap(operr.application_traceback)])
@@ -280,7 +298,7 @@
self.is_tracing += 1
try:
try:
- w_result = space.call_function(w_callback, space.wrap(frame), space.wrap(event), w_arg)
+ w_result = space.call_function(w_callback, space.wrap(frame), space.wrap(TRACE_TO_NAME[event]), w_arg)
if space.is_w(w_result, space.w_None):
frame.w_f_trace = None
else:
@@ -296,14 +314,13 @@
# Profile cases
if self.profilefunc is not None:
- if event not in ['leaveframe', 'call', 'c_call',
- 'c_return', 'c_exception']:
+ if event < _PROFILING_FIRST or event > _PROFILING_LAST:
return
last_exception = None
- if event == 'leaveframe':
+ if event == TRACE_LEAVEFRAME:
last_exception = frame.last_exception
- event = 'return'
+ event = TRACE_RETURN
assert self.is_tracing == 0
self.is_tracing += 1
@@ -523,7 +540,7 @@
if frame.instr_lb <= frame.last_instr < frame.instr_ub:
if frame.last_instr <= frame.instr_prev:
# We jumped backwards in the same line.
- executioncontext._trace(frame, 'line', self.space.w_None)
+ executioncontext._trace(frame, TRACE_LINE, self.space.w_None)
else:
size = len(code.co_lnotab) / 2
addr = 0
@@ -557,7 +574,7 @@
if frame.instr_lb == frame.last_instr: # At start of line!
frame.f_lineno = line
- executioncontext._trace(frame, 'line', self.space.w_None)
+ executioncontext._trace(frame, TRACE_LINE, self.space.w_None)
frame.instr_prev = frame.last_instr
self.space.frame_trace_action.fire() # continue tracing
Modified: pypy/branch/jit-profiling/pypy/interpreter/test/test_executioncontext.py
==============================================================================
--- pypy/branch/jit-profiling/pypy/interpreter/test/test_executioncontext.py (original)
+++ pypy/branch/jit-profiling/pypy/interpreter/test/test_executioncontext.py Mon Jan 11 21:01:15 2010
@@ -1,6 +1,9 @@
import py
from pypy.interpreter import executioncontext
from pypy.conftest import gettestobjspace
+from pypy.interpreter.executioncontext import (TRACE_CALL, TRACE_RETURN,
+ TRACE_C_CALL, TRACE_C_RETURN, TRACE_C_EXCEPTION, TRACE_EXCEPTION,
+ TRACE_LEAVEFRAME, TRACE_LINE)
class Finished(Exception):
pass
@@ -74,7 +77,7 @@
pass
""")
space.getexecutioncontext().setllprofile(None, None)
- assert l == ['call', 'return', 'call', 'return']
+ assert l == [TRACE_CALL, TRACE_RETURN] * 2
def test_llprofile_c_call(self):
l = []
@@ -92,7 +95,8 @@
return
""" % snippet)
space.getexecutioncontext().setllprofile(None, None)
- assert l == ['call', 'return', 'call', 'c_call', 'c_return', 'return']
+ assert l == [TRACE_CALL, TRACE_RETURN, TRACE_CALL, TRACE_C_CALL,
+ TRACE_C_RETURN, TRACE_RETURN]
check_snippet('l = []; l.append(42)')
check_snippet('max(1, 2)')
@@ -120,7 +124,8 @@
return
""" % snippet)
space.getexecutioncontext().setllprofile(None, None)
- assert l == ['call', 'return', 'call', 'c_call', 'c_exception', 'return']
+ assert l == [TRACE_CALL, TRACE_RETURN, TRACE_CALL,
+ TRACE_C_CALL, TRACE_C_EXCEPTION, TRACE_RETURN]
check_snippet('d = {}; d.__getitem__(42)')
@@ -144,7 +149,8 @@
return l
""")
events = space.unwrap(w_events)
- assert events == ['return', 'c_call', 'c_return', 'return', 'c_call']
+ assert events == [TRACE_RETURN, TRACE_C_CALL, TRACE_C_RETURN,
+ TRACE_RETURN, TRACE_C_CALL]
def test_c_call_setprofile_strange_method(self):
space = self.space
@@ -173,7 +179,8 @@
return l
""")
events = space.unwrap(w_events)
- assert events == ['return', 'call', 'return', 'return', 'c_call']
+ assert events == [TRACE_RETURN, TRACE_CALL, TRACE_RETURN,
+ TRACE_RETURN, TRACE_C_CALL]
def test_c_call_profiles_immediately(self):
space = self.space
@@ -192,7 +199,8 @@
return l
""")
events = space.unwrap(w_events)
- assert [i[0] for i in events] == ['c_call', 'c_return', 'return', 'c_call']
+ assert [i[0] for i in events] == [TRACE_C_CALL, TRACE_C_RETURN,
+ TRACE_RETURN, TRACE_C_CALL]
assert events[0][1] == events[1][1]
def test_tracing_range_builtinshortcut(self):
@@ -218,4 +226,5 @@
return l
""")
events = space.unwrap(w_events)
- assert [i[0] for i in events] == ['c_call', 'c_return', 'c_call']
+ assert [i[0] for i in events] == [TRACE_C_CALL, TRACE_C_RETURN,
+ TRACE_C_CALL]
Modified: pypy/branch/jit-profiling/pypy/module/_lsprof/interp_lsprof.py
==============================================================================
--- pypy/branch/jit-profiling/pypy/module/_lsprof/interp_lsprof.py (original)
+++ pypy/branch/jit-profiling/pypy/module/_lsprof/interp_lsprof.py Mon Jan 11 21:01:15 2010
@@ -5,6 +5,8 @@
interp_attrproperty)
from pypy.interpreter.gateway import interp2app, NoneNotWrapped
from pypy.interpreter.function import Method, Function
+from pypy.interpreter.executioncontext import (TRACE_CALL, TRACE_RETURN,
+ TRACE_C_CALL, TRACE_C_RETURN)
import time, sys
class W_StatsEntry(Wrappable):
@@ -180,17 +182,17 @@
def lsprof_call(space, w_self, frame, event, w_arg):
assert isinstance(w_self, W_Profiler)
- if event == 'call':
+ if event == TRACE_CALL:
code = frame.getcode()
w_self._enter_call(code)
- elif event == 'return':
+ elif event == TRACE_RETURN:
code = frame.getcode()
w_self._enter_return(code)
- elif event == 'c_call':
+ elif event == TRACE_C_CALL:
if w_self.builtins:
key = create_spec(space, w_arg)
w_self._enter_builtin_call(key)
- elif event == 'c_return':
+ elif event == TRACE_C_RETURN:
if w_self.builtins:
key = create_spec(space, w_arg)
w_self._enter_builtin_return(key)
More information about the Pypy-commit
mailing list