Skip to content

Commit 3bbabe4

Browse files
committed
feat: skip Python runtime objects in callgrind
Adds a callgrind_add_obj_skip C extension function that emits the Valgrind client request VG_USERREQ__ADD_OBJ_SKIP (0x43540006), and a callgrind_skip_python_runtime() helper that skips libpython and the python executable. Called from InstrumentHooks.__init__ so Python runtime frames stop polluting the callgrind flamegraph (COD-2654).
1 parent d4d9dc6 commit 3bbabe4

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎src/pytest_codspeed/instruments/hooks/__init__.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def __init__(self) -> None:
5050
if SUPPORTS_PERF_TRAMPOLINE and not sys.is_stack_trampoline_active():
5151
sys.activate_stack_trampoline("perf") # type: ignore
5252

53+
# Ignore libpython and python executable frames in callgrind so they
54+
# don't obfuscate the flamegraph.
55+
callgrind_skip_python_runtime()
56+
5357
def __del__(self):
5458
# Don't manually deinit - let the capsule destructor handle it
5559
pass
@@ -223,3 +227,47 @@ def collect_and_write_python_environment(self) -> None:
223227
self.set_environment_list(section, "config", config_items)
224228

225229
self.write_environment()
230+
231+
232+
def callgrind_add_obj_skip(path: str) -> None:
233+
"""Tell callgrind to skip the given object file (and its realpath).
234+
235+
The actual Valgrind client-request trapdoor lives in the C extension; this
236+
just resolves the realpath so callgrind's strcmp matches either form.
237+
"""
238+
if not path or not os.path.exists(path):
239+
return
240+
try:
241+
from . import dist_instrument_hooks # type: ignore
242+
except ImportError:
243+
return
244+
245+
dist_instrument_hooks.callgrind_add_obj_skip(path.encode())
246+
247+
# The dynamic loader maps the realpath (e.g. libpython3.12.so.1.0), and
248+
# callgrind stores that in obj_node->name. Skip both so the exact strcmp
249+
# matches regardless of which path callgrind sees.
250+
real = os.path.realpath(path)
251+
if real != path:
252+
dist_instrument_hooks.callgrind_add_obj_skip(real.encode())
253+
254+
255+
def callgrind_skip_python_runtime() -> None:
256+
"""Skip libpython and the python executable from callgrind measurement."""
257+
ldlibrary = sysconfig.get_config_var("LDLIBRARY")
258+
libdir = sysconfig.get_config_var("LIBDIR")
259+
libpython = next(
260+
(
261+
p
262+
for p in (
263+
os.path.join(libdir, ldlibrary) if ldlibrary and libdir else None,
264+
os.path.join(sys.prefix, "lib", ldlibrary) if ldlibrary else None,
265+
)
266+
if p and os.path.exists(p)
267+
),
268+
None,
269+
)
270+
if libpython:
271+
callgrind_add_obj_skip(libpython)
272+
273+
callgrind_add_obj_skip(sys.executable)

‎src/pytest_codspeed/instruments/hooks/instrument_hooks_module.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#define PY_SSIZE_T_CLEAN
22
#include <Python.h>
33
#include "core.h"
4+
#include "valgrind.h"
5+
6+
/* CodSpeed-specific Valgrind client request: tell callgrind to skip an
7+
* object file by path. Not present in upstream callgrind.h. */
8+
// TODO(COD-2654): Move this to instrument-hooks and just call it here
9+
#define VG_USERREQ__ADD_OBJ_SKIP 0x43540006
410

511
/* Capsule destructor for InstrumentHooks pointer */
612
static void instrument_hooks_capsule_destructor(PyObject *capsule) {
@@ -242,6 +248,17 @@ static PyObject *py_instrument_hooks_write_environment(PyObject *self, PyObject
242248
return PyLong_FromLong(result);
243249
}
244250

251+
/* callgrind_add_obj_skip(path: bytes) -> None
252+
* Outside Valgrind this expands to a nop, so it's safe on bare metal. */
253+
static PyObject *py_callgrind_add_obj_skip(PyObject *self, PyObject *args) {
254+
const char *path;
255+
if (!PyArg_ParseTuple(args, "y", &path)) {
256+
return NULL;
257+
}
258+
VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__ADD_OBJ_SKIP, path, 0, 0, 0, 0);
259+
Py_RETURN_NONE;
260+
}
261+
245262
/* Method definitions */
246263
static PyMethodDef InstrumentHooksMethods[] = {
247264
{"instrument_hooks_init", py_instrument_hooks_init, METH_NOARGS,
@@ -272,6 +289,8 @@ static PyMethodDef InstrumentHooksMethods[] = {
272289
"Register a list of values under a named section for environment collection."},
273290
{"instrument_hooks_write_environment", py_instrument_hooks_write_environment, METH_VARARGS,
274291
"Flush all registered environment sections to disk."},
292+
{"callgrind_add_obj_skip", py_callgrind_add_obj_skip, METH_VARARGS,
293+
"Tell callgrind to skip the given object file path."},
275294
{NULL, NULL, 0, NULL}
276295
};
277296

0 commit comments

Comments
 (0)