| 1 |
from pypy.interpreter.error import OperationError |
| 2 |
from pypy.interpreter.gateway import unwrap_spec |
| 3 |
from pypy.interpreter.pyframe import PyFrame |
| 4 |
from pypy.interpreter.pycode import PyCode |
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
from rpython.rlib import rvmprof, jit |
| 7 |
from pypy.interpreter.error import oefmt |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
_get_code = lambda frame, w_arg_or_err: frame.pycode |
| 13 |
_decorator = rvmprof.vmprof_execute_code("pypy", _get_code, W_Root) |
| 14 |
my_execute_frame = _decorator(PyFrame.execute_frame) |
| 15 |
|
| 16 |
|
| 17 |
class __extend__(PyFrame): |
| 18 |
def execute_frame(self, w_arg_or_err=None): |
| 19 |
|
| 20 |
return my_execute_frame(self, w_arg_or_err) |
| 21 |
|
| 22 |
|
| 23 |
def _safe(s): |
| 24 |
if len(s) > 110: |
| 25 |
s = s[:107] + '...' |
| 26 |
return s.replace(':', ';') |
| 27 |
|
| 28 |
def _get_full_name(pycode): |
| 29 |
|
| 30 |
return "py:%s:%d:%s" % (_safe(pycode.co_name), pycode.co_firstlineno, |
| 31 |
_safe(pycode.co_filename)) |
| 32 |
|
| 33 |
rvmprof.register_code_object_class(PyCode, _get_full_name) |
| 34 |
|
| 35 |
|
| 36 |
def _init_ready(pycode): |
| 37 |
rvmprof.register_code(pycode, _get_full_name) |
| 38 |
|
| 39 |
PyCode._init_ready = _init_ready |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class Cache: |
| 46 |
def __init__(self, space): |
| 47 |
self.w_VMProfError = space.new_exception_class("_vmprof.VMProfError") |
| 48 |
|
| 49 |
def VMProfError(space, e): |
| 50 |
w_VMProfError = space.fromcache(Cache).w_VMProfError |
| 51 |
return OperationError(w_VMProfError, space.newtext(e.msg)) |
| 52 |
|
| 53 |
|
| 54 |
@unwrap_spec(fileno=int, period=float, memory=int, lines=int, native=int, real_time=int) |
| 55 |
def enable(space, fileno, period, memory, lines, native, real_time): |
| 56 |
"""Enable vmprof. Writes go to the given 'fileno', a file descriptor |
| 57 |
opened for writing. *The file descriptor must remain open at least |
| 58 |
until disable() is called.* |
| 59 |
|
| 60 |
'interval' is a float representing the sampling interval, in seconds. |
| 61 |
Must be smaller than 1.0 |
| 62 |
""" |
| 63 |
try: |
| 64 |
rvmprof.enable(fileno, period, memory, native, real_time) |
| 65 |
except rvmprof.VMProfError as e: |
| 66 |
raise VMProfError(space, e) |
| 67 |
|
| 68 |
def disable(space): |
| 69 |
"""Disable vmprof. Remember to close the file descriptor afterwards |
| 70 |
if necessary. |
| 71 |
""" |
| 72 |
try: |
| 73 |
rvmprof.disable() |
| 74 |
except rvmprof.VMProfError as e: |
| 75 |
raise VMProfError(space, e) |
| 76 |
|
| 77 |
def is_enabled(space): |
| 78 |
return space.newbool(rvmprof.is_enabled()) |
| 79 |
|
| 80 |
def get_profile_path(space): |
| 81 |
path = rvmprof.get_profile_path(space) |
| 82 |
if path is None: |
| 83 |
|
| 84 |
return space.w_None |
| 85 |
if path == "": |
| 86 |
|
| 87 |
raise oefmt(space.w_NotImplementedError, "platform not implemented") |
| 88 |
return space.newtext(path) |
| 89 |
|
| 90 |
def stop_sampling(space): |
| 91 |
return space.newint(rvmprof.stop_sampling()) |
| 92 |
|
| 93 |
def start_sampling(space): |
| 94 |
rvmprof.start_sampling() |
| 95 |
return space.w_None |