| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import platform as host_platform |
| 4 |
import py |
| 5 |
import sys |
| 6 |
import shutil |
| 7 |
from rpython.tool.udir import udir |
| 8 |
from rpython.tool.version import rpythonroot |
| 9 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 10 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 11 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 12 |
from rpython.rtyper.tool import rffi_platform as platform |
| 13 |
from rpython.rlib import rthread, jit |
| 14 |
from rpython.rlib.objectmodel import we_are_translated |
| 15 |
from rpython.config.translationoption import get_translation_config |
| 16 |
from rpython.jit.backend import detect_cpu |
| 17 |
|
| 18 |
class VMProfPlatformUnsupported(Exception): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
IS_SUPPORTED = False |
| 23 |
NATIVE_PROFILING_SUPPORTED = False |
| 24 |
if sys.platform in ('darwin', 'linux', 'linux2') or sys.platform.startswith('freebsd'): |
| 25 |
try: |
| 26 |
proc = detect_cpu.autodetect() |
| 27 |
IS_SUPPORTED = (proc.startswith('x86') |
| 28 |
or proc == 'aarch64' |
| 29 |
or proc == 'riscv64') |
| 30 |
NATIVE_PROFILING_SUPPORTED = proc.startswith('x86') |
| 31 |
except detect_cpu.ProcessorAutodetectError: |
| 32 |
print("PROCESSOR NOT DETECTED, SKIPPING VMPROF") |
| 33 |
|
| 34 |
ROOT = py.path.local(rpythonroot).join('rpython', 'rlib', 'rvmprof') |
| 35 |
SRC = ROOT.join('src') |
| 36 |
SHARED = SRC.join('shared') |
| 37 |
BACKTRACE = SHARED.join('libbacktrace') |
| 38 |
|
| 39 |
def make_eci(): |
| 40 |
if make_eci.called: |
| 41 |
raise ValueError("make_eci() should be called at most once") |
| 42 |
|
| 43 |
compile_extra = ['-DRPYTHON_VMPROF'] |
| 44 |
separate_module_files = [ |
| 45 |
SHARED.join('symboltable.c'), |
| 46 |
SHARED.join('vmprof_unix.c') |
| 47 |
] |
| 48 |
if sys.platform.startswith('linux'): |
| 49 |
separate_module_files += [ |
| 50 |
BACKTRACE.join('atomic.c'), |
| 51 |
BACKTRACE.join('backtrace.c'), |
| 52 |
BACKTRACE.join('state.c'), |
| 53 |
BACKTRACE.join('elf.c'), |
| 54 |
BACKTRACE.join('dwarf.c'), |
| 55 |
BACKTRACE.join('fileline.c'), |
| 56 |
BACKTRACE.join('mmap.c'), |
| 57 |
BACKTRACE.join('mmapio.c'), |
| 58 |
BACKTRACE.join('posix.c'), |
| 59 |
BACKTRACE.join('sort.c'), |
| 60 |
] |
| 61 |
_libs = ['dl'] |
| 62 |
compile_extra += ['-DVMPROF_UNIX'] |
| 63 |
compile_extra += ['-DVMPROF_LINUX'] |
| 64 |
elif sys.platform == 'win32': |
| 65 |
compile_extra += ['-DVMPROF_WINDOWS'] |
| 66 |
separate_module_files = [SHARED.join('vmprof_win.c')] |
| 67 |
_libs = [] |
| 68 |
else: |
| 69 |
|
| 70 |
compile_extra += ['-DVMPROF_UNIX'] |
| 71 |
if sys.platform.startswith('darwin'): |
| 72 |
compile_extra += ['-DVMPROF_APPLE'] |
| 73 |
if sys.platform.startswith('freebsd'): |
| 74 |
_libs = ['unwind'] |
| 75 |
else: |
| 76 |
_libs = [] |
| 77 |
|
| 78 |
eci_kwds = dict( |
| 79 |
include_dirs = [SRC, SHARED, BACKTRACE], |
| 80 |
includes = ['rvmprof.h','vmprof_stack.h'], |
| 81 |
libraries = _libs, |
| 82 |
separate_module_files = [ |
| 83 |
SRC.join('rvmprof.c'), |
| 84 |
SHARED.join('compat.c'), |
| 85 |
SHARED.join('machine.c'), |
| 86 |
SHARED.join('vmp_stack.c'), |
| 87 |
SHARED.join('vmprof_memory.c'), |
| 88 |
SHARED.join('vmprof_common.c'), |
| 89 |
|
| 90 |
] + separate_module_files, |
| 91 |
post_include_bits=[], |
| 92 |
compile_extra=compile_extra |
| 93 |
) |
| 94 |
if sys.platform != 'win32': |
| 95 |
eci_kwds['separate_module_files'].append( |
| 96 |
SHARED.join('vmprof_mt.c'), |
| 97 |
) |
| 98 |
make_eci.called = True |
| 99 |
return ExternalCompilationInfo(**eci_kwds), eci_kwds |
| 100 |
make_eci.called = False |
| 101 |
|
| 102 |
def configure_libbacktrace_linux(): |
| 103 |
bits = 32 if sys.maxsize == 2**31-1 else 64 |
| 104 |
|
| 105 |
|
| 106 |
specific_config = 'config-x86_%d.h' % bits |
| 107 |
config = BACKTRACE.join('config.h') |
| 108 |
shutil.copy(str(BACKTRACE.join(specific_config)), str(config)) |
| 109 |
|
| 110 |
def setup(): |
| 111 |
if not IS_SUPPORTED: |
| 112 |
raise VMProfPlatformUnsupported |
| 113 |
|
| 114 |
if sys.platform.startswith('linux'): |
| 115 |
configure_libbacktrace_linux() |
| 116 |
|
| 117 |
eci, eci_kwds = make_eci() |
| 118 |
eci_kwds['compile_extra'].append('-DRPYTHON_LL2CTYPES') |
| 119 |
platform.verify_eci(ExternalCompilationInfo( |
| 120 |
**eci_kwds)) |
| 121 |
|
| 122 |
vmprof_init = rffi.llexternal("vmprof_init", |
| 123 |
[rffi.INT, rffi.DOUBLE, rffi.INT, rffi.INT, |
| 124 |
rffi.CCHARP, rffi.INT, rffi.INT], |
| 125 |
rffi.CCHARP, compilation_info=eci) |
| 126 |
vmprof_enable = rffi.llexternal("vmprof_enable", [rffi.INT, rffi.INT, rffi.INT], |
| 127 |
rffi.INT, |
| 128 |
compilation_info=eci, |
| 129 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 130 |
vmprof_disable = rffi.llexternal("vmprof_disable", [], rffi.INT, |
| 131 |
compilation_info=eci, |
| 132 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 133 |
vmprof_register_virtual_function = rffi.llexternal( |
| 134 |
"vmprof_register_virtual_function", |
| 135 |
[rffi.CCHARP, rffi.LONG, rffi.INT], |
| 136 |
rffi.INT, compilation_info=eci) |
| 137 |
vmprof_ignore_signals = rffi.llexternal("vmprof_ignore_signals", |
| 138 |
[rffi.INT], lltype.Void, |
| 139 |
compilation_info=eci, |
| 140 |
_nowrapper=True) |
| 141 |
vmprof_get_traceback = rffi.llexternal("vmprof_get_traceback", |
| 142 |
[PVMPROFSTACK, llmemory.Address, |
| 143 |
rffi.VOIDPP, lltype.Signed], |
| 144 |
lltype.Signed, compilation_info=eci, |
| 145 |
_nowrapper=True) |
| 146 |
|
| 147 |
vmprof_get_profile_path = rffi.llexternal("vmprof_get_profile_path", [rffi.CCHARP, lltype.Signed], |
| 148 |
lltype.Signed, compilation_info=eci, |
| 149 |
_nowrapper=True) |
| 150 |
|
| 151 |
vmprof_stop_sampling = rffi.llexternal("vmprof_stop_sampling", [], |
| 152 |
rffi.INT, compilation_info=eci, |
| 153 |
_nowrapper=True) |
| 154 |
vmprof_start_sampling = rffi.llexternal("vmprof_start_sampling", [], |
| 155 |
lltype.Void, compilation_info=eci, |
| 156 |
_nowrapper=True) |
| 157 |
return CInterface(locals()) |
| 158 |
|
| 159 |
|
| 160 |
class CInterface(object): |
| 161 |
def __init__(self, namespace): |
| 162 |
for k, v in namespace.iteritems(): |
| 163 |
setattr(self, k, v) |
| 164 |
|
| 165 |
def _freeze_(self): |
| 166 |
return True |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
VMPROF_CODE_TAG = 1 |
| 172 |
|
| 173 |
VMPROFSTACK = lltype.ForwardReference() |
| 174 |
PVMPROFSTACK = lltype.Ptr(VMPROFSTACK) |
| 175 |
VMPROFSTACK.become(rffi.CStruct("vmprof_stack_s", |
| 176 |
('next', PVMPROFSTACK), |
| 177 |
('value', lltype.Signed), |
| 178 |
('kind', lltype.Signed))) |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
vmprof_tl_stack = rthread.ThreadLocalField(PVMPROFSTACK, "vmprof_tl_stack") |
| 183 |
do_use_eci = rffi.llexternal_use_eci( |
| 184 |
ExternalCompilationInfo(includes=['vmprof_stack.h'], |
| 185 |
include_dirs = [SRC])) |
| 186 |
|
| 187 |
def enter_code(unique_id): |
| 188 |
do_use_eci() |
| 189 |
s = lltype.malloc(VMPROFSTACK, flavor='raw') |
| 190 |
s.c_next = vmprof_tl_stack.get_or_make_raw() |
| 191 |
s.c_value = unique_id |
| 192 |
s.c_kind = VMPROF_CODE_TAG |
| 193 |
vmprof_tl_stack.setraw(s) |
| 194 |
return s |
| 195 |
|
| 196 |
def leave_code(s): |
| 197 |
if not we_are_translated(): |
| 198 |
assert vmprof_tl_stack.getraw() == s |
| 199 |
vmprof_tl_stack.setraw(s.c_next) |
| 200 |
lltype.free(s, flavor='raw') |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
def jit_rvmprof_code(leaving, unique_id): |
| 236 |
if leaving == 0: |
| 237 |
enter_code(unique_id) |
| 238 |
else: |
| 239 |
s = vmprof_tl_stack.getraw() |
| 240 |
if s.c_value == unique_id and s.c_kind == VMPROF_CODE_TAG: |
| 241 |
leave_code(s) |
| 242 |
else: |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
assert (s.c_next and s.c_next.c_value == unique_id and |
| 254 |
s.c_next.c_kind == VMPROF_CODE_TAG) |
| 255 |
s = vmprof_tl_stack.getraw() |
| 256 |
leave_code(s) |
| 257 |
s = vmprof_tl_stack.getraw() |
| 258 |
leave_code(s) |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
def get_rvmprof_stack(): |
| 264 |
return vmprof_tl_stack.get_or_make_raw() |