| 1 |
from rpython.rtyper.tool import rffi_platform |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 4 |
from rpython.rlib.objectmodel import we_are_translated |
| 5 |
from rpython.rlib import rposix |
| 6 |
from rpython.jit.backend.x86 import profagent |
| 7 |
|
| 8 |
class OProfileError(Exception): |
| 9 |
def __init__(self, errno, where): |
| 10 |
self.errno = errno |
| 11 |
self.where = where |
| 12 |
|
| 13 |
|
| 14 |
eci = ExternalCompilationInfo(includes=['stdint.h', 'opagent.h'], |
| 15 |
library_dirs=['/usr/local/lib/oprofile/'], |
| 16 |
libraries=['bfd', 'opagent']) |
| 17 |
try: |
| 18 |
rffi_platform.verify_eci(eci) |
| 19 |
except rffi_platform.CompilationError: |
| 20 |
OPROFILE_AVAILABLE = False |
| 21 |
else: |
| 22 |
OPROFILE_AVAILABLE = True |
| 23 |
AGENT = rffi.VOIDP |
| 24 |
uint64_t = rffi.ULONGLONG |
| 25 |
op_open_agent = rffi.llexternal( |
| 26 |
"op_open_agent", |
| 27 |
[], |
| 28 |
AGENT, |
| 29 |
compilation_info=eci, |
| 30 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 31 |
op_close_agent = rffi.llexternal( |
| 32 |
"op_close_agent", |
| 33 |
[AGENT], |
| 34 |
rffi.INT, |
| 35 |
compilation_info=eci, |
| 36 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 37 |
|
| 38 |
|
| 39 |
op_write_native_code = rffi.llexternal( |
| 40 |
"op_write_native_code", |
| 41 |
[AGENT, rffi.CCHARP, uint64_t, rffi.VOIDP, rffi.UINT], |
| 42 |
rffi.INT, |
| 43 |
compilation_info=eci, |
| 44 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 45 |
|
| 46 |
|
| 47 |
class OProfileAgent(profagent.ProfileAgent): |
| 48 |
|
| 49 |
def startup(self): |
| 50 |
if not OPROFILE_AVAILABLE: |
| 51 |
return |
| 52 |
agent = op_open_agent() |
| 53 |
if not agent: |
| 54 |
raise OProfileError(rposix.get_saved_errno(), "startup") |
| 55 |
self.agent = agent |
| 56 |
|
| 57 |
def shutdown(self): |
| 58 |
if not OPROFILE_AVAILABLE: |
| 59 |
return |
| 60 |
success = op_close_agent(self.agent) |
| 61 |
if success != 0: |
| 62 |
raise OProfileError(rposix.get_saved_errno(), "shutdown") |
| 63 |
|
| 64 |
def native_code_written(self, name, address, size): |
| 65 |
assert size > 0 |
| 66 |
if not OPROFILE_AVAILABLE: |
| 67 |
return |
| 68 |
uaddress = rffi.cast(rffi.ULONG, address) |
| 69 |
success = op_write_native_code(self.agent, name, uaddress, rffi.cast(rffi.VOIDP, 0), size) |
| 70 |
if success != 0: |
| 71 |
raise OProfileError(rposix.get_saved_errno(), "write") |