-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime_obj_skip_py.py
More file actions
70 lines (60 loc) · 2.15 KB
/
runtime_obj_skip_py.py
File metadata and controls
70 lines (60 loc) · 2.15 KB
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
"""Resolve libpython at runtime via sysconfig (mirrors pytest-codspeed's
approach in instruments/walltime.py), register it for obj-skip via the
client-request shim, then turn instrumentation on and run a small
integer workload.
We pass both the sysconfig path AND its os.path.realpath because
callgrind stores the realpath in obj_node->name (after symlink
resolution), and the runtime obj-skip check uses exact strcmp."""
import ctypes
import os
import sys
import sysconfig
def libpython_candidates() -> list[str]:
ldlibrary = sysconfig.get_config_var("LDLIBRARY")
libdir = sysconfig.get_config_var("LIBDIR")
paths: list[str] = []
if ldlibrary and libdir:
paths.append(os.path.join(libdir, ldlibrary))
if ldlibrary:
paths.append(os.path.join(sys.prefix, "lib", ldlibrary))
# Add realpath variants so the exact-match obj-skip finds the
# file under whichever name the loader actually mapped.
resolved: list[str] = []
seen: set[str] = set()
for p in paths:
if not p:
continue
if p not in seen and os.path.exists(p):
resolved.append(p)
seen.add(p)
try:
r = os.path.realpath(p)
except OSError:
continue
if r not in seen and os.path.exists(r):
resolved.append(r)
seen.add(r)
return resolved
def main() -> None:
here = os.path.dirname(os.path.abspath(__file__))
shim = ctypes.CDLL(os.path.join(here, "runtime_obj_skip_py_shim.so"))
shim.add_obj_skip.argtypes = [ctypes.c_char_p]
shim.add_obj_skip.restype = None
shim.start_instr.argtypes = []
shim.start_instr.restype = None
shim.stop_instr.argtypes = []
shim.stop_instr.restype = None
for path in libpython_candidates():
shim.add_obj_skip(path.encode())
if sys.executable:
shim.add_obj_skip(sys.executable.encode())
real = os.path.realpath(sys.executable)
if real != sys.executable:
shim.add_obj_skip(real.encode())
shim.start_instr()
acc = 0
for i in range(10_000):
acc = (acc + i * i) ^ (i << 1)
shim.stop_instr()
if __name__ == "__main__":
main()