aboutsummaryrefslogtreecommitdiff
blob: c63e12d120446acd3147db304ca7854cf426fa56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
236
237
238
239
import platform as host_platform
import py
import sys
import shutil
from rpython.tool.udir import udir
from rpython.tool.version import rpythonroot
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.rtyper.tool import rffi_platform as platform
from rpython.rlib import rthread, jit
from rpython.rlib.objectmodel import we_are_translated
from rpython.config.translationoption import get_translation_config
from rpython.jit.backend import detect_cpu

class VMProfPlatformUnsupported(Exception):
    pass

# vmprof works only on x86 for now
IS_SUPPORTED = False
if sys.platform in ('darwin', 'linux', 'linux2') or sys.platform.startswith('freebsd'):
    try:
        IS_SUPPORTED = detect_cpu.autodetect().startswith('x86')
    except detect_cpu.ProcessorAutodetectError:
        pass

ROOT = py.path.local(rpythonroot).join('rpython', 'rlib', 'rvmprof')
SRC = ROOT.join('src')
SHARED = SRC.join('shared')
BACKTRACE = SHARED.join('libbacktrace')

def make_eci():
    if make_eci.called:
        raise ValueError("make_eci() should be called at most once")
    #
    compile_extra = ['-DRPYTHON_VMPROF']
    separate_module_files = [
        SHARED.join('symboltable.c'),
        SHARED.join('vmprof_unix.c')
    ]
    if sys.platform.startswith('linux'):
        separate_module_files += [
           BACKTRACE.join('atomic.c'),
           BACKTRACE.join('backtrace.c'),
           BACKTRACE.join('state.c'),
           BACKTRACE.join('elf.c'),
           BACKTRACE.join('dwarf.c'),
           BACKTRACE.join('fileline.c'),
           BACKTRACE.join('mmap.c'),
           BACKTRACE.join('mmapio.c'),
           BACKTRACE.join('posix.c'),
           BACKTRACE.join('sort.c'),
        ]
        _libs = ['dl']
        compile_extra += ['-DVMPROF_UNIX']
        compile_extra += ['-DVMPROF_LINUX']
    elif sys.platform == 'win32':
        compile_extra += ['-DVMPROF_WINDOWS']
        separate_module_files = [SHARED.join('vmprof_win.c')]
        _libs = []
    else:
        # Guessing a BSD-like Unix platform
        compile_extra += ['-DVMPROF_UNIX']
        compile_extra += ['-DVMPROF_APPLE']
        if sys.platform.startswith('freebsd'):
            _libs = ['unwind']
        else:
            _libs = []

    eci_kwds = dict(
        include_dirs = [SRC, SHARED, BACKTRACE],
        includes = ['rvmprof.h','vmprof_stack.h'],
        libraries = _libs,
        separate_module_files = [
            SRC.join('rvmprof.c'),
            SHARED.join('compat.c'),
            SHARED.join('machine.c'),
            SHARED.join('vmp_stack.c'),
            SHARED.join('vmprof_memory.c'),
            SHARED.join('vmprof_common.c'),
            # symbol table already in separate_module_files
        ] + separate_module_files,
        post_include_bits=[],
        compile_extra=compile_extra
        )
    if sys.platform != 'win32':
        eci_kwds['separate_module_files'].append(
            SHARED.join('vmprof_mt.c'),
        )
    make_eci.called = True
    return ExternalCompilationInfo(**eci_kwds), eci_kwds
make_eci.called = False

def configure_libbacktrace_linux():
    bits = 32 if sys.maxsize == 2**31-1 else 64
    # FIXME well, the config generated on x86 seems to work on s390x and ppc
    # vmprof is currently not supported there! we just need to pass compilation
    specific_config = 'config-x86_%d.h' % bits
    config = BACKTRACE.join('config.h')
    shutil.copy(str(BACKTRACE.join(specific_config)), str(config))

def setup():
    if not IS_SUPPORTED:
        raise VMProfPlatformUnsupported
    
    if sys.platform.startswith('linux'):
        configure_libbacktrace_linux()

    eci, eci_kwds = make_eci()
    eci_kwds['compile_extra'].append('-DRPYTHON_LL2CTYPES')
    platform.verify_eci(ExternalCompilationInfo(
                        **eci_kwds))

    vmprof_init = rffi.llexternal("vmprof_init",
                                  [rffi.INT, rffi.DOUBLE, rffi.INT, rffi.INT,
                                   rffi.CCHARP, rffi.INT, rffi.INT],
                                  rffi.CCHARP, compilation_info=eci)
    vmprof_enable = rffi.llexternal("vmprof_enable", [rffi.INT, rffi.INT, rffi.INT],
                                    rffi.INT,
                                    compilation_info=eci,
                                    save_err=rffi.RFFI_SAVE_ERRNO)
    vmprof_disable = rffi.llexternal("vmprof_disable", [], rffi.INT,
                                     compilation_info=eci,
                                     save_err=rffi.RFFI_SAVE_ERRNO)
    vmprof_register_virtual_function = rffi.llexternal(
                                           "vmprof_register_virtual_function",
                                           [rffi.CCHARP, rffi.LONG, rffi.INT],
                                           rffi.INT, compilation_info=eci)
    vmprof_ignore_signals = rffi.llexternal("vmprof_ignore_signals",
                                            [rffi.INT], lltype.Void,
                                            compilation_info=eci,
                                            _nowrapper=True)
    vmprof_get_traceback = rffi.llexternal("vmprof_get_traceback",
                                  [PVMPROFSTACK, llmemory.Address,
                                   rffi.SIGNEDP, lltype.Signed],
                                  lltype.Signed, compilation_info=eci,
                                  _nowrapper=True)

    vmprof_get_profile_path = rffi.llexternal("vmprof_get_profile_path", [rffi.CCHARP, lltype.Signed],
                                              lltype.Signed, compilation_info=eci,
                                              _nowrapper=True)

    vmprof_stop_sampling = rffi.llexternal("vmprof_stop_sampling", [],
                                           rffi.INT, compilation_info=eci,
                                           _nowrapper=True)
    vmprof_start_sampling = rffi.llexternal("vmprof_start_sampling", [],
                                            lltype.Void, compilation_info=eci,
                                            _nowrapper=True)

    return CInterface(locals())


class CInterface(object):
    def __init__(self, namespace):
        for k, v in namespace.iteritems():
            setattr(self, k, v)

    def _freeze_(self):
        return True


# --- copy a few declarations from src/vmprof_stack.h ---

VMPROF_CODE_TAG = 1

VMPROFSTACK = lltype.ForwardReference()
PVMPROFSTACK = lltype.Ptr(VMPROFSTACK)
VMPROFSTACK.become(rffi.CStruct("vmprof_stack_s",
                                ('next', PVMPROFSTACK),
                                ('value', lltype.Signed),
                                ('kind', lltype.Signed)))
# ----------


vmprof_tl_stack = rthread.ThreadLocalField(PVMPROFSTACK, "vmprof_tl_stack")
do_use_eci = rffi.llexternal_use_eci(
    ExternalCompilationInfo(includes=['vmprof_stack.h'],
                            include_dirs = [SRC]))

def enter_code(unique_id):
    do_use_eci()
    s = lltype.malloc(VMPROFSTACK, flavor='raw')
    s.c_next = vmprof_tl_stack.get_or_make_raw()
    s.c_value = unique_id
    s.c_kind = VMPROF_CODE_TAG
    vmprof_tl_stack.setraw(s)
    return s

def leave_code(s):
    if not we_are_translated():
        assert vmprof_tl_stack.getraw() == s
    vmprof_tl_stack.setraw(s.c_next)
    lltype.free(s, flavor='raw')

#
# JIT notes:
#
# - When running JIT-generated assembler code, we have different custom
#   code to build the VMPROFSTACK, so the functions above are not used.
#   (It uses kind == VMPROF_JITTED_TAG and the VMPROFSTACK is allocated
#   in the C stack.)
#
# - The jitcode for decorated_jitted_function() in rvmprof.py is
#   special-cased by jtransform.py to produce this:
#
#        rvmprof_code(0, unique_id)
#        res = inline_call FUNC         <- for func(*args)
#        rvmprof_code(1, unique_id)
#        return res
#
#   There is no 'catch_exception', but the second 'rvmprof_code' is
#   meant to be executed even in case there was an exception.  This is
#   done by a special case in pyjitpl.py and blackhole.py.  The point
#   is that the above simple pattern can be detected by the blackhole
#   interp, when it first rebuilds all the intermediate RPython
#   frames; at that point it needs to call jit_rvmprof_code(0) on all
#   intermediate RPython frames, so it does pattern matching to
#   recognize when it must call that and with which 'unique_id' value.
#
# - The jitcode opcode 'rvmprof_code' doesn't produce any resop.  When
#   meta-interpreting, it causes pyjitpl to call jit_rvmprof_code().
#   As mentioned above, there is logic to call jit_rvmprof_code(1)
#   even if we exit with an exception, even though there is no
#   'catch_exception'.  There is similar logic inside the blackhole
#   interpreter.


def jit_rvmprof_code(leaving, unique_id):
    if leaving == 0:
        enter_code(unique_id)    # ignore the return value
    else:
        s = vmprof_tl_stack.getraw()
        assert s.c_value == unique_id and s.c_kind == VMPROF_CODE_TAG
        leave_code(s)

#
# traceback support

def get_rvmprof_stack():
    return vmprof_tl_stack.get_or_make_raw()