From 1ec92c8c9db59db2d0adc37f47d003d3db4e1c64 Mon Sep 17 00:00:00 2001 From: not-matthias Date: Fri, 5 Jun 2026 18:01:42 +0200 Subject: [PATCH] feat: add callgrind_toggle_collect helper Expose CALLGRIND_TOGGLE_COLLECT as a wrapper next to the existing start/stop instrumentation helpers. Unlike toggling instrumentation, toggling collection does not flush the simulated cache, so integrations can exclude code regions (e.g. google benchmark's PauseTiming sections) from measurement without paying an artificial cold-cache warmup in the measured region. Refs COD-2033. --- dist/core.c | 4 ++++ includes/core.h | 7 +++++++ src/helpers/valgrind_wrapper.c | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/dist/core.c b/dist/core.c index fcd1876..3d15316 100644 --- a/dist/core.c +++ b/dist/core.c @@ -61296,6 +61296,8 @@ void callgrind_start_instrumentation() {} void callgrind_stop_instrumentation() {} +void callgrind_toggle_collect() {} + void callgrind_add_obj_skip(uint8_t const *path) { (void)path; } #else #include "callgrind.h" @@ -61315,6 +61317,8 @@ void callgrind_start_instrumentation() { CALLGRIND_START_INSTRUMENTATION; } void callgrind_stop_instrumentation() { CALLGRIND_STOP_INSTRUMENTATION; } +void callgrind_toggle_collect() { CALLGRIND_TOGGLE_COLLECT; } + void callgrind_add_obj_skip(uint8_t const *path) { CALLGRIND_ADD_OBJ_SKIP(path); } diff --git a/includes/core.h b/includes/core.h index de4e65d..0690d91 100644 --- a/includes/core.h +++ b/includes/core.h @@ -49,6 +49,13 @@ uint64_t instrument_hooks_current_timestamp(void); void callgrind_start_instrumentation(); void callgrind_stop_instrumentation(); +// Toggle callgrind cost collection on/off without re-instrumenting. +// Unlike start/stop instrumentation, this does not flush the simulated +// cache, so it can be used to exclude code regions from measurement +// without introducing artificial cold-cache costs. +// Safe to call outside Valgrind: expands to a no-op. +void callgrind_toggle_collect(); + // Register an object file path on callgrind's runtime --obj-skip list. // Equivalent to passing --obj-skip= on the valgrind command line. // Safe to call outside Valgrind: expands to a no-op. diff --git a/src/helpers/valgrind_wrapper.c b/src/helpers/valgrind_wrapper.c index 00ed042..c338c04 100644 --- a/src/helpers/valgrind_wrapper.c +++ b/src/helpers/valgrind_wrapper.c @@ -17,6 +17,8 @@ void callgrind_start_instrumentation() {} void callgrind_stop_instrumentation() {} +void callgrind_toggle_collect() {} + void callgrind_add_obj_skip(uint8_t const *path) { (void)path; } #else #include "callgrind.h" @@ -36,6 +38,8 @@ void callgrind_start_instrumentation() { CALLGRIND_START_INSTRUMENTATION; } void callgrind_stop_instrumentation() { CALLGRIND_STOP_INSTRUMENTATION; } +void callgrind_toggle_collect() { CALLGRIND_TOGGLE_COLLECT; } + void callgrind_add_obj_skip(uint8_t const *path) { CALLGRIND_ADD_OBJ_SKIP(path); }