PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rlib/rvmprof/cintf.py

https://bitbucket.org/pypy/pypy/
Python | 156 lines | 96 code | 25 blank | 35 comment | 8 complexity | 40df1fa306fda4c950e56613d1fa2180 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import platform as host_platform
  2. import py
  3. import sys
  4. from rpython.tool.udir import udir
  5. from rpython.tool.version import rpythonroot
  6. from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
  7. from rpython.translator.tool.cbuild import ExternalCompilationInfo
  8. from rpython.rtyper.tool import rffi_platform as platform
  9. from rpython.rlib import rthread, jit
  10. from rpython.rlib.objectmodel import we_are_translated
  11. class VMProfPlatformUnsupported(Exception):
  12. pass
  13. ROOT = py.path.local(rpythonroot).join('rpython', 'rlib', 'rvmprof')
  14. SRC = ROOT.join('src')
  15. if sys.platform.startswith('linux'):
  16. _libs = ['dl']
  17. else:
  18. _libs = []
  19. eci_kwds = dict(
  20. include_dirs = [SRC],
  21. includes = ['rvmprof.h', 'vmprof_stack.h'],
  22. libraries = _libs,
  23. separate_module_files = [SRC.join('rvmprof.c')],
  24. post_include_bits=['#define RPYTHON_VMPROF\n'],
  25. )
  26. global_eci = ExternalCompilationInfo(**eci_kwds)
  27. def setup():
  28. if host_platform.machine() == 's390x':
  29. raise VMProfPlatformUnsupported("rvmprof not supported on"
  30. " s390x CPUs for now")
  31. compile_extra = ['-DRPYTHON_LL2CTYPES']
  32. platform.verify_eci(ExternalCompilationInfo(
  33. compile_extra=compile_extra,
  34. **eci_kwds))
  35. eci = global_eci
  36. vmprof_init = rffi.llexternal("vmprof_init",
  37. [rffi.INT, rffi.DOUBLE, rffi.CCHARP],
  38. rffi.CCHARP, compilation_info=eci)
  39. vmprof_enable = rffi.llexternal("vmprof_enable", [], rffi.INT,
  40. compilation_info=eci,
  41. save_err=rffi.RFFI_SAVE_ERRNO)
  42. vmprof_disable = rffi.llexternal("vmprof_disable", [], rffi.INT,
  43. compilation_info=eci,
  44. save_err=rffi.RFFI_SAVE_ERRNO)
  45. vmprof_register_virtual_function = rffi.llexternal(
  46. "vmprof_register_virtual_function",
  47. [rffi.CCHARP, rffi.LONG, rffi.INT],
  48. rffi.INT, compilation_info=eci)
  49. vmprof_ignore_signals = rffi.llexternal("vmprof_ignore_signals",
  50. [rffi.INT], lltype.Void,
  51. compilation_info=eci,
  52. _nowrapper=True)
  53. return CInterface(locals())
  54. class CInterface(object):
  55. def __init__(self, namespace):
  56. for k, v in namespace.iteritems():
  57. setattr(self, k, v)
  58. def _freeze_(self):
  59. return True
  60. # --- copy a few declarations from src/vmprof_stack.h ---
  61. VMPROF_CODE_TAG = 1
  62. VMPROFSTACK = lltype.ForwardReference()
  63. PVMPROFSTACK = lltype.Ptr(VMPROFSTACK)
  64. VMPROFSTACK.become(rffi.CStruct("vmprof_stack_s",
  65. ('next', PVMPROFSTACK),
  66. ('value', lltype.Signed),
  67. ('kind', lltype.Signed)))
  68. # ----------
  69. vmprof_tl_stack = rthread.ThreadLocalField(PVMPROFSTACK, "vmprof_tl_stack")
  70. do_use_eci = rffi.llexternal_use_eci(
  71. ExternalCompilationInfo(includes=['vmprof_stack.h'],
  72. include_dirs = [SRC]))
  73. def enter_code(unique_id):
  74. do_use_eci()
  75. s = lltype.malloc(VMPROFSTACK, flavor='raw')
  76. s.c_next = vmprof_tl_stack.get_or_make_raw()
  77. s.c_value = unique_id
  78. s.c_kind = VMPROF_CODE_TAG
  79. vmprof_tl_stack.setraw(s)
  80. return s
  81. def leave_code(s):
  82. if not we_are_translated():
  83. assert vmprof_tl_stack.getraw() == s
  84. vmprof_tl_stack.setraw(s.c_next)
  85. lltype.free(s, flavor='raw')
  86. #
  87. # JIT notes:
  88. #
  89. # - When running JIT-generated assembler code, we have different custom
  90. # code to build the VMPROFSTACK, so the functions above are not used.
  91. # (It uses kind == VMPROF_JITTED_TAG and the VMPROFSTACK is allocated
  92. # in the C stack.)
  93. #
  94. # - The jitcode for decorated_jitted_function() in rvmprof.py is
  95. # special-cased by jtransform.py to produce this:
  96. #
  97. # rvmprof_code(0, unique_id)
  98. # res = inline_call FUNC <- for func(*args)
  99. # rvmprof_code(1, unique_id)
  100. # return res
  101. #
  102. # There is no 'catch_exception', but the second 'rvmprof_code' is
  103. # meant to be executed even in case there was an exception. This is
  104. # done by a special case in pyjitpl.py and blackhole.py. The point
  105. # is that the above simple pattern can be detected by the blackhole
  106. # interp, when it first rebuilds all the intermediate RPython
  107. # frames; at that point it needs to call jit_rvmprof_code(0) on all
  108. # intermediate RPython frames, so it does pattern matching to
  109. # recognize when it must call that and with which 'unique_id' value.
  110. #
  111. # - The jitcode opcode 'rvmprof_code' doesn't produce any resop. When
  112. # meta-interpreting, it causes pyjitpl to call jit_rvmprof_code().
  113. # As mentioned above, there is logic to call jit_rvmprof_code(1)
  114. # even if we exit with an exception, even though there is no
  115. # 'catch_exception'. There is similar logic inside the blackhole
  116. # interpreter.
  117. def jit_rvmprof_code(leaving, unique_id):
  118. if leaving == 0:
  119. enter_code(unique_id) # ignore the return value
  120. else:
  121. s = vmprof_tl_stack.getraw()
  122. assert s.c_value == unique_id and s.c_kind == VMPROF_CODE_TAG
  123. leave_code(s)
  124. #
  125. # stacklet support
  126. def save_rvmprof_stack():
  127. return vmprof_tl_stack.get_or_make_raw()
  128. def empty_rvmprof_stack():
  129. vmprof_tl_stack.setraw(lltype.nullptr(VMPROFSTACK))
  130. def restore_rvmprof_stack(x):
  131. vmprof_tl_stack.setraw(x)