| 1 |
import sys, os |
| 2 |
from rpython.rlib.objectmodel import specialize, we_are_translated, not_rpython |
| 3 |
from rpython.rlib import jit, rposix, rgc |
| 4 |
from rpython.rlib.rvmprof import cintf |
| 5 |
from rpython.rlib.rvmprof.dummy import DummyVMProf |
| 6 |
from rpython.rtyper.annlowlevel import cast_instance_to_gcref |
| 7 |
from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance |
| 8 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 9 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 10 |
from rpython.rlib.rweaklist import RWeakListMixin |
| 11 |
|
| 12 |
MAX_FUNC_NAME = 1023 |
| 13 |
|
| 14 |
PLAT_WINDOWS = sys.platform == 'win32' |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
VMPROF_CODE_TAG = 1 |
| 20 |
VMPROF_BLACKHOLE_TAG = 2 |
| 21 |
VMPROF_JITTED_TAG = 3 |
| 22 |
VMPROF_JITTING_TAG = 4 |
| 23 |
VMPROF_GC_TAG = 5 |
| 24 |
|
| 25 |
class VMProfError(Exception): |
| 26 |
msg = '' |
| 27 |
def __init__(self, msg): |
| 28 |
self.msg = msg |
| 29 |
def __str__(self): |
| 30 |
return self.msg |
| 31 |
|
| 32 |
class FakeWeakCodeObjectList(object): |
| 33 |
def add_handle(self, handle): |
| 34 |
pass |
| 35 |
def get_all_handles(self): |
| 36 |
return [] |
| 37 |
|
| 38 |
class VMProf(object): |
| 39 |
""" |
| 40 |
NOTE: the API of this class should be kept in sync with dummy.DummyVMProf |
| 41 |
""" |
| 42 |
|
| 43 |
_immutable_fields_ = ['is_enabled?'] |
| 44 |
|
| 45 |
use_weaklist = True |
| 46 |
|
| 47 |
@not_rpython |
| 48 |
def __init__(self): |
| 49 |
"use _get_vmprof()" |
| 50 |
self._code_classes = set() |
| 51 |
self._gather_all_code_objs = lambda: None |
| 52 |
self._cleanup_() |
| 53 |
self._code_unique_id = 4 |
| 54 |
self.cintf = cintf.setup() |
| 55 |
|
| 56 |
def _cleanup_(self): |
| 57 |
self.is_enabled = False |
| 58 |
|
| 59 |
@jit.dont_look_inside |
| 60 |
@specialize.argtype(1) |
| 61 |
def register_code(self, code, full_name_func): |
| 62 |
"""Register the code object. Call when a new code object is made. |
| 63 |
""" |
| 64 |
if code._vmprof_unique_id == 0: |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
uid = self._code_unique_id + 4 |
| 72 |
code._vmprof_unique_id = uid |
| 73 |
self._code_unique_id = uid |
| 74 |
if self.is_enabled: |
| 75 |
self._write_code_registration(uid, full_name_func(code)) |
| 76 |
elif self.use_weaklist: |
| 77 |
code._vmprof_weak_list.add_handle(code) |
| 78 |
|
| 79 |
@not_rpython |
| 80 |
def register_code_object_class(self, CodeClass, full_name_func): |
| 81 |
""" |
| 82 |
Register statically the class 'CodeClass' as containing user |
| 83 |
code objects. |
| 84 |
|
| 85 |
full_name_func() is a function called at runtime with an |
| 86 |
instance of CodeClass and it should return a string. This |
| 87 |
is the string stored in the vmprof file identifying the code |
| 88 |
object. It can be directly an unbound method of CodeClass. |
| 89 |
IMPORTANT: the name returned must be at most MAX_FUNC_NAME |
| 90 |
characters long, and with exactly 3 colons, i.e. of the form |
| 91 |
|
| 92 |
class:func_name:func_line:filename |
| 93 |
|
| 94 |
where 'class' is 'py' for PyPy. |
| 95 |
|
| 96 |
Instances of the CodeClass will have a new attribute called |
| 97 |
'_vmprof_unique_id', but that's managed internally. |
| 98 |
""" |
| 99 |
if CodeClass in self._code_classes: |
| 100 |
return |
| 101 |
CodeClass._vmprof_unique_id = 0 |
| 102 |
immut = CodeClass.__dict__.get('_immutable_fields_', []) |
| 103 |
CodeClass._immutable_fields_ = list(immut) + ['_vmprof_unique_id'] |
| 104 |
attrs = CodeClass.__dict__.get('_attrs_', None) |
| 105 |
if attrs is not None: |
| 106 |
CodeClass._attrs_ = list(attrs) + ['_vmprof_unique_id'] |
| 107 |
self._code_classes.add(CodeClass) |
| 108 |
|
| 109 |
class WeakCodeObjectList(RWeakListMixin): |
| 110 |
def __init__(self): |
| 111 |
self.initialize() |
| 112 |
if self.use_weaklist: |
| 113 |
CodeClass._vmprof_weak_list = WeakCodeObjectList() |
| 114 |
else: |
| 115 |
CodeClass._vmprof_weak_list = FakeWeakCodeObjectList() |
| 116 |
|
| 117 |
def gather_all_code_objs(): |
| 118 |
all_code_wrefs = CodeClass._vmprof_weak_list.get_all_handles() |
| 119 |
for wref in all_code_wrefs: |
| 120 |
code = wref() |
| 121 |
if code is not None: |
| 122 |
uid = code._vmprof_unique_id |
| 123 |
if uid != 0: |
| 124 |
self._write_code_registration(uid, full_name_func(code)) |
| 125 |
prev() |
| 126 |
|
| 127 |
|
| 128 |
prev = self._gather_all_code_objs |
| 129 |
self._gather_all_code_objs = gather_all_code_objs |
| 130 |
|
| 131 |
@jit.dont_look_inside |
| 132 |
def enable(self, fileno, interval, memory=0, native=0, real_time=0): |
| 133 |
"""Enable vmprof. Writes go to the given 'fileno'. |
| 134 |
The sampling interval is given by 'interval' as a number of |
| 135 |
seconds, as a float which must be smaller than 1.0. |
| 136 |
Raises VMProfError if something goes wrong. |
| 137 |
""" |
| 138 |
assert fileno >= 0 |
| 139 |
if self.is_enabled: |
| 140 |
raise VMProfError("vmprof is already enabled") |
| 141 |
|
| 142 |
if PLAT_WINDOWS: |
| 143 |
native = 0 |
| 144 |
lines = 0 |
| 145 |
|
| 146 |
p_error = self.cintf.vmprof_init(fileno, interval, memory, lines, "pypy", native, real_time) |
| 147 |
if p_error: |
| 148 |
raise VMProfError(rffi.charp2str(p_error)) |
| 149 |
|
| 150 |
self._gather_all_code_objs() |
| 151 |
res = self.cintf.vmprof_enable(memory, native, real_time) |
| 152 |
if res < 0: |
| 153 |
raise VMProfError(os.strerror(rposix.get_saved_errno())) |
| 154 |
self.is_enabled = True |
| 155 |
|
| 156 |
@jit.dont_look_inside |
| 157 |
def disable(self): |
| 158 |
"""Disable vmprof. |
| 159 |
Raises VMProfError if something goes wrong. |
| 160 |
""" |
| 161 |
if not self.is_enabled: |
| 162 |
raise VMProfError("vmprof is not enabled") |
| 163 |
self.is_enabled = False |
| 164 |
res = self.cintf.vmprof_disable() |
| 165 |
if res < 0: |
| 166 |
raise VMProfError(os.strerror(rposix.get_saved_errno())) |
| 167 |
|
| 168 |
|
| 169 |
def _write_code_registration(self, uid, name): |
| 170 |
assert name.count(':') == 3 and len(name) <= MAX_FUNC_NAME, ( |
| 171 |
"the name must be 'class:func_name:func_line:filename' " |
| 172 |
"and at most %d characters; got '%s'" % (MAX_FUNC_NAME, name)) |
| 173 |
if self.cintf.vmprof_register_virtual_function(name, uid, 500000) < 0: |
| 174 |
raise VMProfError("vmprof buffers full! disk full or too slow") |
| 175 |
|
| 176 |
def stop_sampling(self): |
| 177 |
""" |
| 178 |
Temporarily stop the sampling of stack frames. Signals are still |
| 179 |
delivered, but are ignored. |
| 180 |
""" |
| 181 |
fd = self.cintf.vmprof_stop_sampling() |
| 182 |
return rffi.cast(lltype.Signed, fd) |
| 183 |
|
| 184 |
def start_sampling(self): |
| 185 |
""" |
| 186 |
Undo the effect of stop_sampling |
| 187 |
""" |
| 188 |
self.cintf.vmprof_start_sampling() |
| 189 |
|
| 190 |
|
| 191 |
def vmprof_execute_code(name, get_code_fn, result_class=None, |
| 192 |
_hack_update_stack_untranslated=False): |
| 193 |
"""Decorator to be used on the function that interprets a code object. |
| 194 |
|
| 195 |
'name' must be a unique name. |
| 196 |
|
| 197 |
'get_code_fn(*args)' is called to extract the code object from the |
| 198 |
arguments given to the decorated function. |
| 199 |
|
| 200 |
'result_class' is ignored (backward compatibility). |
| 201 |
""" |
| 202 |
if _hack_update_stack_untranslated: |
| 203 |
from rpython.rtyper.annlowlevel import llhelper |
| 204 |
enter_code = llhelper(lltype.Ptr( |
| 205 |
lltype.FuncType([lltype.Signed], cintf.PVMPROFSTACK)), |
| 206 |
cintf.enter_code) |
| 207 |
leave_code = llhelper(lltype.Ptr( |
| 208 |
lltype.FuncType([cintf.PVMPROFSTACK], lltype.Void)), |
| 209 |
cintf.leave_code) |
| 210 |
else: |
| 211 |
enter_code = cintf.enter_code |
| 212 |
leave_code = cintf.leave_code |
| 213 |
|
| 214 |
def decorate(func): |
| 215 |
try: |
| 216 |
_get_vmprof() |
| 217 |
except cintf.VMProfPlatformUnsupported: |
| 218 |
return func |
| 219 |
|
| 220 |
@jit.oopspec("rvmprof.jitted(unique_id)") |
| 221 |
def decorated_jitted_function(unique_id, *args): |
| 222 |
return func(*args) |
| 223 |
|
| 224 |
def decorated_function(*args): |
| 225 |
unique_id = get_code_fn(*args)._vmprof_unique_id |
| 226 |
unique_id = rffi.cast(lltype.Signed, unique_id) |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if not jit.we_are_jitted(): |
| 237 |
x = enter_code(unique_id) |
| 238 |
|
| 239 |
try: |
| 240 |
return func(*args) |
| 241 |
finally: |
| 242 |
|
| 243 |
leave_code(x) |
| 244 |
else: |
| 245 |
return decorated_jitted_function(unique_id, *args) |
| 246 |
|
| 247 |
decorated_function.__name__ = func.__name__ + '_rvmprof' |
| 248 |
decorated_function.c_name = '__vmprof_eval_vmprof' |
| 249 |
return decorated_function |
| 250 |
|
| 251 |
return decorate |
| 252 |
|
| 253 |
@specialize.memo() |
| 254 |
def _was_registered(CodeClass): |
| 255 |
return hasattr(CodeClass, '_vmprof_unique_id') |
| 256 |
|
| 257 |
_vmprof_instance = None |
| 258 |
|
| 259 |
@specialize.memo() |
| 260 |
def _get_vmprof(): |
| 261 |
global _vmprof_instance |
| 262 |
if _vmprof_instance is None: |
| 263 |
try: |
| 264 |
_vmprof_instance = VMProf() |
| 265 |
except cintf.VMProfPlatformUnsupported: |
| 266 |
_vmprof_instance = DummyVMProf() |
| 267 |
return _vmprof_instance |