Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Dec 17, 2023
1 parent fca8de3 commit 0b865ab
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0b865ab

Please sign in to comment.