| 1 |
import py |
| 2 |
|
| 3 |
from pypy.interpreter.baseobjspace import W_Root |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.interpreter.function import BuiltinFunction, Method, Function |
| 6 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 7 |
from pypy.interpreter.typedef import (TypeDef, GetSetProperty, |
| 8 |
interp_attrproperty) |
| 9 |
from rpython.rlib import jit |
| 10 |
from rpython.rlib.objectmodel import we_are_translated, always_inline |
| 11 |
from rpython.rlib.rtimer import read_timestamp, _is_64_bit |
| 12 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 13 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 14 |
from rpython.translator import cdir |
| 15 |
from rpython.rlib.rarithmetic import r_longlong |
| 16 |
|
| 17 |
import time, sys |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
srcdir = py.path.local(cdir).join('src') |
| 22 |
eci = ExternalCompilationInfo( |
| 23 |
include_dirs = [cdir], |
| 24 |
separate_module_files = [srcdir.join('profiling.c')]) |
| 25 |
|
| 26 |
c_setup_profiling = rffi.llexternal('pypy_setup_profiling', |
| 27 |
[], lltype.Void, |
| 28 |
compilation_info = eci) |
| 29 |
c_teardown_profiling = rffi.llexternal('pypy_teardown_profiling', |
| 30 |
[], lltype.Void, |
| 31 |
compilation_info = eci) |
| 32 |
|
| 33 |
if _is_64_bit: |
| 34 |
timer_size_int = int |
| 35 |
else: |
| 36 |
timer_size_int = r_longlong |
| 37 |
|
| 38 |
class W_StatsEntry(W_Root): |
| 39 |
def __init__(self, space, frame, callcount, reccallcount, tt, it, |
| 40 |
w_sublist): |
| 41 |
self.frame = frame |
| 42 |
self.callcount = callcount |
| 43 |
self.reccallcount = reccallcount |
| 44 |
self.it = it |
| 45 |
self.tt = tt |
| 46 |
self.w_calls = w_sublist |
| 47 |
|
| 48 |
def get_calls(self, space): |
| 49 |
return self.w_calls |
| 50 |
|
| 51 |
def repr(self, space): |
| 52 |
frame_repr = space.text_w(space.repr(self.frame)) |
| 53 |
if not self.w_calls: |
| 54 |
calls_repr = "None" |
| 55 |
else: |
| 56 |
calls_repr = space.text_w(space.repr(self.w_calls)) |
| 57 |
return space.newtext('("%s", %d, %d, %f, %f, %s)' % ( |
| 58 |
frame_repr, self.callcount, self.reccallcount, |
| 59 |
self.tt, self.it, calls_repr)) |
| 60 |
|
| 61 |
def get_code(self, space): |
| 62 |
return returns_code(space, self.frame) |
| 63 |
|
| 64 |
W_StatsEntry.typedef = TypeDef( |
| 65 |
'_lsprof.StatsEntry', |
| 66 |
code = GetSetProperty(W_StatsEntry.get_code), |
| 67 |
callcount = interp_attrproperty('callcount', W_StatsEntry, |
| 68 |
wrapfn="newint"), |
| 69 |
reccallcount = interp_attrproperty('reccallcount', W_StatsEntry, |
| 70 |
wrapfn="newint"), |
| 71 |
inlinetime = interp_attrproperty('it', W_StatsEntry, |
| 72 |
wrapfn="newfloat"), |
| 73 |
totaltime = interp_attrproperty('tt', W_StatsEntry, |
| 74 |
wrapfn="newfloat"), |
| 75 |
calls = GetSetProperty(W_StatsEntry.get_calls), |
| 76 |
__repr__ = interp2app(W_StatsEntry.repr), |
| 77 |
) |
| 78 |
|
| 79 |
class W_StatsSubEntry(W_Root): |
| 80 |
def __init__(self, space, frame, callcount, reccallcount, tt, it): |
| 81 |
self.frame = frame |
| 82 |
self.callcount = callcount |
| 83 |
self.reccallcount = reccallcount |
| 84 |
self.it = it |
| 85 |
self.tt = tt |
| 86 |
|
| 87 |
def repr(self, space): |
| 88 |
frame_repr = space.text_w(space.repr(self.frame)) |
| 89 |
return space.newtext('("%s", %d, %d, %f, %f)' % ( |
| 90 |
frame_repr, self.callcount, self.reccallcount, self.tt, self.it)) |
| 91 |
|
| 92 |
def get_code(self, space): |
| 93 |
return returns_code(space, self.frame) |
| 94 |
|
| 95 |
W_StatsSubEntry.typedef = TypeDef( |
| 96 |
'_lsprof.SubStatsEntry', |
| 97 |
code = GetSetProperty(W_StatsSubEntry.get_code), |
| 98 |
callcount = interp_attrproperty('callcount', W_StatsSubEntry, |
| 99 |
wrapfn="newint"), |
| 100 |
reccallcount = interp_attrproperty('reccallcount', W_StatsSubEntry, |
| 101 |
wrapfn="newint"), |
| 102 |
inlinetime = interp_attrproperty('it', W_StatsSubEntry, |
| 103 |
wrapfn="newfloat"), |
| 104 |
totaltime = interp_attrproperty('tt', W_StatsSubEntry, |
| 105 |
wrapfn="newfloat"), |
| 106 |
__repr__ = interp2app(W_StatsSubEntry.repr), |
| 107 |
) |
| 108 |
|
| 109 |
def stats(space, values, factor): |
| 110 |
l_w = [] |
| 111 |
for v in values: |
| 112 |
if v.callcount != 0: |
| 113 |
l_w.append(v.stats(space, None, factor)) |
| 114 |
return space.newlist(l_w) |
| 115 |
|
| 116 |
class ProfilerSubEntry(object): |
| 117 |
def __init__(self, frame): |
| 118 |
self.frame = frame |
| 119 |
self.ll_tt = r_longlong(0) |
| 120 |
self.ll_it = r_longlong(0) |
| 121 |
self.callcount = 0 |
| 122 |
self.recursivecallcount = 0 |
| 123 |
self.recursionLevel = 0 |
| 124 |
|
| 125 |
def stats(self, space, parent, factor): |
| 126 |
w_sse = W_StatsSubEntry(space, self.frame, |
| 127 |
self.callcount, self.recursivecallcount, |
| 128 |
factor * float(self.ll_tt), |
| 129 |
factor * float(self.ll_it)) |
| 130 |
return w_sse |
| 131 |
|
| 132 |
def _stop(self, tt, it): |
| 133 |
if not we_are_translated(): |
| 134 |
assert type(tt) is timer_size_int |
| 135 |
assert type(it) is timer_size_int |
| 136 |
self.recursionLevel -= 1 |
| 137 |
if self.recursionLevel == 0: |
| 138 |
self.ll_tt += tt |
| 139 |
else: |
| 140 |
self.recursivecallcount += 1 |
| 141 |
self.ll_it += it |
| 142 |
self.callcount += 1 |
| 143 |
|
| 144 |
class ProfilerEntry(ProfilerSubEntry): |
| 145 |
def __init__(self, frame): |
| 146 |
ProfilerSubEntry.__init__(self, frame) |
| 147 |
self.calls = {} |
| 148 |
|
| 149 |
def stats(self, space, dummy, factor): |
| 150 |
if self.calls: |
| 151 |
w_sublist = space.newlist([sub_entry.stats(space, self, factor) |
| 152 |
for sub_entry in self.calls.values()]) |
| 153 |
else: |
| 154 |
w_sublist = space.w_None |
| 155 |
w_se = W_StatsEntry(space, self.frame, self.callcount, |
| 156 |
self.recursivecallcount, |
| 157 |
factor * float(self.ll_tt), |
| 158 |
factor * float(self.ll_it), w_sublist) |
| 159 |
return w_se |
| 160 |
|
| 161 |
@jit.elidable |
| 162 |
def _get_or_make_subentry(self, entry, make=True): |
| 163 |
try: |
| 164 |
return self.calls[entry] |
| 165 |
except KeyError: |
| 166 |
if make: |
| 167 |
subentry = ProfilerSubEntry(entry.frame) |
| 168 |
self.calls[entry] = subentry |
| 169 |
return subentry |
| 170 |
raise |
| 171 |
|
| 172 |
class ProfilerContext(object): |
| 173 |
def __init__(self, profobj, entry): |
| 174 |
self.entry = entry |
| 175 |
self.ll_subt = timer_size_int(0) |
| 176 |
self.previous = profobj.current_context |
| 177 |
entry.recursionLevel += 1 |
| 178 |
if profobj.subcalls and self.previous: |
| 179 |
caller = jit.promote(self.previous.entry) |
| 180 |
subentry = caller._get_or_make_subentry(entry) |
| 181 |
subentry.recursionLevel += 1 |
| 182 |
self.ll_t0 = profobj.ll_timer() |
| 183 |
|
| 184 |
def _stop(self, profobj, entry): |
| 185 |
tt = profobj.ll_timer() - self.ll_t0 |
| 186 |
it = tt - self.ll_subt |
| 187 |
if self.previous: |
| 188 |
self.previous.ll_subt += tt |
| 189 |
entry._stop(tt, it) |
| 190 |
if profobj.subcalls and self.previous: |
| 191 |
caller = jit.promote(self.previous.entry) |
| 192 |
try: |
| 193 |
subentry = caller._get_or_make_subentry(entry, False) |
| 194 |
except KeyError: |
| 195 |
pass |
| 196 |
else: |
| 197 |
subentry._stop(tt, it) |
| 198 |
|
| 199 |
|
| 200 |
def create_spec_for_method(space, w_function, w_type): |
| 201 |
class_name = None |
| 202 |
if isinstance(w_function, Function): |
| 203 |
name = w_function.name |
| 204 |
|
| 205 |
|
| 206 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 207 |
if isinstance(w_type, W_TypeObject): |
| 208 |
w_realclass, _ = space.lookup_in_type_where(w_type, name) |
| 209 |
if isinstance(w_realclass, W_TypeObject): |
| 210 |
class_name = w_realclass.name |
| 211 |
else: |
| 212 |
name = '?' |
| 213 |
if class_name is None: |
| 214 |
class_name = w_type.getname(space) |
| 215 |
return b"<method '%s' of '%s' objects>" % (name, class_name) |
| 216 |
|
| 217 |
|
| 218 |
def create_spec_for_function(space, w_func): |
| 219 |
assert isinstance(w_func, Function) |
| 220 |
if isinstance(w_func, BuiltinFunction): |
| 221 |
if w_func.w_module is not None: |
| 222 |
module = space.utf8_w(w_func.w_module) |
| 223 |
return b'<built-in method %s.%s>' % (module, w_func.getname(space)) |
| 224 |
return b'<built-in function %s>' % (w_func.getname(space),) |
| 225 |
if w_func.w_module is not None: |
| 226 |
module = space.utf8_w(w_func.w_module) |
| 227 |
return b'<%s.%s>' % (module, w_func.getname(space)) |
| 228 |
return b'<%s>' % (w_func.getname(space),) |
| 229 |
|
| 230 |
|
| 231 |
def create_spec_for_object(space, w_type): |
| 232 |
class_name = w_type.getname(space) |
| 233 |
return b"<'%s' object>" % (class_name,) |
| 234 |
|
| 235 |
|
| 236 |
class W_DelayedBuiltinStr(W_Root): |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
_immutable_fields_ = ['w_func', 'w_type'] |
| 244 |
|
| 245 |
def __init__(self, w_func, w_type): |
| 246 |
self.w_func = w_func |
| 247 |
self.w_type = w_type |
| 248 |
self.w_string = None |
| 249 |
|
| 250 |
def wrap_string(self, space): |
| 251 |
if self.w_string is None: |
| 252 |
if self.w_type is None: |
| 253 |
s = create_spec_for_function(space, self.w_func) |
| 254 |
elif self.w_func is None: |
| 255 |
s = create_spec_for_object(space, self.w_type) |
| 256 |
else: |
| 257 |
s = create_spec_for_method(space, self.w_func, self.w_type) |
| 258 |
self.w_string = space.newtext(s) |
| 259 |
return self.w_string |
| 260 |
|
| 261 |
W_DelayedBuiltinStr.typedef = TypeDef( |
| 262 |
'DelayedBuiltinStr', |
| 263 |
__str__ = interp2app(W_DelayedBuiltinStr.wrap_string), |
| 264 |
) |
| 265 |
|
| 266 |
def returns_code(space, w_frame): |
| 267 |
if isinstance(w_frame, W_DelayedBuiltinStr): |
| 268 |
return w_frame.wrap_string(space) |
| 269 |
return w_frame |
| 270 |
|
| 271 |
@always_inline |
| 272 |
def prepare_spec(space, w_arg): |
| 273 |
if isinstance(w_arg, Method): |
| 274 |
return (w_arg.w_function, space.type(w_arg.w_instance)) |
| 275 |
elif isinstance(w_arg, Function): |
| 276 |
return (w_arg, None) |
| 277 |
else: |
| 278 |
return (None, space.type(w_arg)) |
| 279 |
|
| 280 |
def lsprof_call(space, w_self, frame, event, w_arg): |
| 281 |
assert isinstance(w_self, W_Profiler) |
| 282 |
if event == 'call': |
| 283 |
code = frame.getcode() |
| 284 |
w_self._enter_call(code) |
| 285 |
elif event == 'return': |
| 286 |
code = frame.getcode() |
| 287 |
w_self._enter_return(code) |
| 288 |
elif event == 'c_call': |
| 289 |
if w_self.builtins: |
| 290 |
w_self._enter_builtin_call(w_arg) |
| 291 |
elif event == 'c_return' or event == 'c_exception': |
| 292 |
if w_self.builtins: |
| 293 |
w_self._enter_builtin_return(w_arg) |
| 294 |
else: |
| 295 |
|
| 296 |
pass |
| 297 |
|
| 298 |
|
| 299 |
class W_Profiler(W_Root): |
| 300 |
def __init__(self, space, w_callable, time_unit, subcalls, builtins): |
| 301 |
self.subcalls = subcalls |
| 302 |
self.builtins = builtins |
| 303 |
self.current_context = None |
| 304 |
self.w_callable = w_callable |
| 305 |
self.time_unit = time_unit |
| 306 |
self.data = {} |
| 307 |
self.builtin_data = {} |
| 308 |
self.space = space |
| 309 |
self.is_enabled = False |
| 310 |
self.total_timestamp = r_longlong(0) |
| 311 |
self.total_real_time = 0.0 |
| 312 |
|
| 313 |
def ll_timer(self): |
| 314 |
if self.w_callable: |
| 315 |
space = self.space |
| 316 |
try: |
| 317 |
if _is_64_bit: |
| 318 |
return space.int_w(space.call_function(self.w_callable)) |
| 319 |
else: |
| 320 |
return space.r_longlong_w(space.call_function(self.w_callable)) |
| 321 |
except OperationError as e: |
| 322 |
e.write_unraisable(space, "timer function ", |
| 323 |
self.w_callable) |
| 324 |
return timer_size_int(0) |
| 325 |
return read_timestamp() |
| 326 |
|
| 327 |
def enable(self, space, w_subcalls=None, |
| 328 |
w_builtins=None): |
| 329 |
if self.is_enabled: |
| 330 |
return |
| 331 |
if w_subcalls is not None: |
| 332 |
self.subcalls = space.bool_w(w_subcalls) |
| 333 |
if w_builtins is not None: |
| 334 |
self.builtins = space.bool_w(w_builtins) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
self.is_enabled = True |
| 339 |
self.total_real_time -= time.time() |
| 340 |
self.total_timestamp -= read_timestamp() |
| 341 |
|
| 342 |
c_setup_profiling() |
| 343 |
space.getexecutioncontext().setllprofile(lsprof_call, self) |
| 344 |
|
| 345 |
@jit.elidable |
| 346 |
def _get_or_make_entry(self, f_code, make=True): |
| 347 |
try: |
| 348 |
return self.data[f_code] |
| 349 |
except KeyError: |
| 350 |
if make: |
| 351 |
entry = ProfilerEntry(f_code) |
| 352 |
self.data[f_code] = entry |
| 353 |
return entry |
| 354 |
raise |
| 355 |
|
| 356 |
@jit.elidable_promote() |
| 357 |
def _get_or_make_builtin_entry(self, w_func, w_type, make): |
| 358 |
key = (w_func, w_type) |
| 359 |
try: |
| 360 |
return self.builtin_data[key] |
| 361 |
except KeyError: |
| 362 |
if make: |
| 363 |
entry = ProfilerEntry(W_DelayedBuiltinStr(w_func, w_type)) |
| 364 |
self.builtin_data[key] = entry |
| 365 |
return entry |
| 366 |
raise |
| 367 |
|
| 368 |
def _enter_call(self, f_code): |
| 369 |
|
| 370 |
self = jit.promote(self) |
| 371 |
entry = self._get_or_make_entry(f_code) |
| 372 |
self.current_context = ProfilerContext(self, entry) |
| 373 |
|
| 374 |
def _enter_return(self, f_code): |
| 375 |
context = self.current_context |
| 376 |
if context is None: |
| 377 |
return |
| 378 |
self = jit.promote(self) |
| 379 |
try: |
| 380 |
entry = self._get_or_make_entry(f_code, False) |
| 381 |
except KeyError: |
| 382 |
pass |
| 383 |
else: |
| 384 |
context._stop(self, entry) |
| 385 |
self.current_context = context.previous |
| 386 |
|
| 387 |
def _enter_builtin_call(self, w_arg): |
| 388 |
w_func, w_type = prepare_spec(self.space, w_arg) |
| 389 |
entry = self._get_or_make_builtin_entry(w_func, w_type, True) |
| 390 |
self.current_context = ProfilerContext(self, entry) |
| 391 |
|
| 392 |
def _enter_builtin_return(self, w_arg): |
| 393 |
context = self.current_context |
| 394 |
if context is None: |
| 395 |
return |
| 396 |
w_func, w_type = prepare_spec(self.space, w_arg) |
| 397 |
try: |
| 398 |
entry = self._get_or_make_builtin_entry(w_func, w_type, False) |
| 399 |
except KeyError: |
| 400 |
pass |
| 401 |
else: |
| 402 |
context._stop(self, entry) |
| 403 |
self.current_context = context.previous |
| 404 |
|
| 405 |
def _flush_unmatched(self): |
| 406 |
context = self.current_context |
| 407 |
while context: |
| 408 |
entry = context.entry |
| 409 |
if entry: |
| 410 |
context._stop(self, entry) |
| 411 |
context = context.previous |
| 412 |
self.current_context = None |
| 413 |
|
| 414 |
def disable(self, space): |
| 415 |
if not self.is_enabled: |
| 416 |
return |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
self.is_enabled = False |
| 421 |
self.total_timestamp += read_timestamp() |
| 422 |
self.total_real_time += time.time() |
| 423 |
|
| 424 |
space.getexecutioncontext().setllprofile(None, None) |
| 425 |
c_teardown_profiling() |
| 426 |
self._flush_unmatched() |
| 427 |
|
| 428 |
def getstats(self, space): |
| 429 |
if self.w_callable is None: |
| 430 |
if self.is_enabled: |
| 431 |
raise oefmt(space.w_RuntimeError, |
| 432 |
"Profiler instance must be disabled before " |
| 433 |
"getting the stats") |
| 434 |
if self.total_timestamp: |
| 435 |
factor = self.total_real_time / float(self.total_timestamp) |
| 436 |
else: |
| 437 |
factor = 1.0 |
| 438 |
elif self.time_unit > 0.0: |
| 439 |
factor = self.time_unit |
| 440 |
else: |
| 441 |
factor = 1.0 / sys.maxint |
| 442 |
return stats(space, self.data.values() + self.builtin_data.values(), |
| 443 |
factor) |
| 444 |
|
| 445 |
@unwrap_spec(time_unit=float, subcalls=int, builtins=int) |
| 446 |
def descr_new_profile(space, w_type, w_callable=None, time_unit=0.0, |
| 447 |
subcalls=1, builtins=1): |
| 448 |
p = space.allocate_instance(W_Profiler, w_type) |
| 449 |
p.__init__(space, w_callable, time_unit, bool(subcalls), bool(builtins)) |
| 450 |
return p |
| 451 |
|
| 452 |
W_Profiler.typedef = TypeDef( |
| 453 |
'_lsprof.Profiler', |
| 454 |
__new__ = interp2app(descr_new_profile), |
| 455 |
enable = interp2app(W_Profiler.enable), |
| 456 |
disable = interp2app(W_Profiler.disable), |
| 457 |
getstats = interp2app(W_Profiler.getstats), |
| 458 |
) |