From a9a953c89b5bc2b713bd190125ac44320a2e1c67 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sun, 17 Dec 2023 18:19:55 -0500 Subject: [PATCH] Minor refactoring. (#744) --- scalene/scalene_profiler.py | 58 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index 7021dab52..9ea76a1db 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -558,48 +558,44 @@ def memcpy_signal_handler( Scalene.__memcpy_sigq.put((signum, this_frame)) del this_frame + @staticmethod + def enable_signals_win32() -> None: + assert sys.platform == "win32" + Scalene.timer_signals = True + Scalene.__orig_signal( + Scalene.__signals.cpu_signal, + Scalene.cpu_signal_handler, + ) + # On Windows, we simulate timer signals by running a background thread. + Scalene.timer_signals = True + t = threading.Thread(target=Scalene.windows_timer_loop) + t.start() + Scalene.__windows_queue.put(None) + Scalene.start_signal_queues() + return + @staticmethod def enable_signals() -> None: """Set up the signal handlers to handle interrupts for profiling and start the timer interrupts.""" if sys.platform == "win32": - Scalene.timer_signals = True - Scalene.__orig_signal( - Scalene.__signals.cpu_signal, - Scalene.cpu_signal_handler, - ) - # On Windows, we simulate timer signals by running a background thread. - Scalene.timer_signals = True - t = threading.Thread(target=Scalene.windows_timer_loop) - t.start() - Scalene.__windows_queue.put(None) - Scalene.start_signal_queues() + Scalene.enable_signals_win32() return Scalene.start_signal_queues() - # Set signal handlers for memory allocation and memcpy events. - Scalene.__orig_signal( - Scalene.__signals.malloc_signal, Scalene.malloc_signal_handler - ) - Scalene.__orig_signal( - Scalene.__signals.free_signal, Scalene.free_signal_handler - ) - Scalene.__orig_signal( - Scalene.__signals.memcpy_signal, Scalene.memcpy_signal_handler - ) - Scalene.__orig_signal(signal.SIGTERM, Scalene.term_signal_handler) + # Set signal handlers for various events. + for sig, handler in [(Scalene.__signals.malloc_signal, Scalene.malloc_signal_handler), + (Scalene.__signals.free_signal, Scalene.free_signal_handler), + (Scalene.__signals.memcpy_signal, Scalene.memcpy_signal_handler), + (signal.SIGTERM, Scalene.term_signal_handler), + (Scalene.__signals.cpu_signal, Scalene.cpu_signal_handler)]: + Scalene.__orig_signal(sig, handler) # Set every signal to restart interrupted system calls. for s in Scalene.__signals.get_all_signals(): Scalene.__orig_siginterrupt(s, False) - # Turn on the CPU profiling timer to run at the sampling rate, exactly once. - Scalene.__orig_signal( - Scalene.__signals.cpu_signal, - Scalene.cpu_signal_handler, + Scalene.__orig_setitimer( + Scalene.__signals.cpu_timer_signal, + Scalene.__args.cpu_sampling_rate, ) - if sys.platform != "win32": - Scalene.__orig_setitimer( - Scalene.__signals.cpu_timer_signal, - Scalene.__args.cpu_sampling_rate, - ) def __init__( self,