From ed26a2e8fc92a321f0afeb38c1f88db46ec957a6 Mon Sep 17 00:00:00 2001 From: Helmut Eller Date: Fri, 3 Jan 2025 17:27:10 +0100 Subject: [PATCH 1/2] Delay processing of SIGPROF to the next safepoint * src/lisp.h (process_pending_profiler_signals): New function. * src/profiler.c (pending_profiler_signals): New variable. (handle_profiler_signal): Instead of calling add_sample, set pending_signals and increment pending_profiler_signals. (process_pending_profiler_signals): New function. * src/keyboard.c (process_pending_signals): Call process_pending_profiler_signals. --- src/keyboard.c | 1 + src/lisp.h | 1 + src/profiler.c | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/keyboard.c b/src/keyboard.c index e875e98fde6..5d6cebdc990 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -8191,6 +8191,7 @@ process_pending_signals (void) handle_async_input (); do_pending_atimers (); do_async_work (); + process_pending_profiler_signals (); } /* Undo any number of BLOCK_INPUT calls down to level LEVEL, diff --git a/src/lisp.h b/src/lisp.h index 48585c2d8a1..774667a4f9c 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -5938,6 +5938,7 @@ maybe_disable_address_randomization (int argc, char **argv) extern void malloc_probe (size_t); extern void syms_of_profiler (void); extern void mark_profiler (void); +extern void process_pending_profiler_signals (void); #ifdef DOS_NT diff --git a/src/profiler.c b/src/profiler.c index 3db7fe0eb3e..47367982cab 100644 --- a/src/profiler.c +++ b/src/profiler.c @@ -387,6 +387,9 @@ add_sample (struct profiler_log *plog, EMACS_INT count) /* Hash-table log of CPU profiler. */ static struct profiler_log cpu; +/* Number of unprocessed profiler signals. */ +static uintptr_t pending_profiler_signals; + /* The current sampling interval in nanoseconds. */ static EMACS_INT current_sampling_interval; @@ -402,7 +405,19 @@ handle_profiler_signal (int signal) count += overruns; } #endif - add_sample (&cpu, count); + pending_signals = true; + pending_profiler_signals += count; +} + +void +process_pending_profiler_signals (void) +{ + uintptr_t count = pending_profiler_signals; + if (count) + { + pending_profiler_signals = 0; + add_sample (&cpu, count); + } } static void -- 2.39.5