diff options
| author | bd <bdunahu@operationnull.com> | 2025-06-08 19:27:32 -0400 |
|---|---|---|
| committer | bd <bdunahu@operationnull.com> | 2025-06-08 19:27:32 -0400 |
| commit | b2e45ec5af0b312aa82b950d284ad44f72702f6f (patch) | |
| tree | 0a7e6158010f9f7260eddb8b05a9e932ab3398b4 | |
initial commit
| -rw-r--r-- | mini-scalene.py | 56 | ||||
| -rw-r--r-- | tests/successive_sleep.py | 15 |
2 files changed, 71 insertions, 0 deletions
diff --git a/mini-scalene.py b/mini-scalene.py new file mode 100644 index 0000000..e593c94 --- /dev/null +++ b/mini-scalene.py @@ -0,0 +1,56 @@ +import runpy +import os +import sys +import time +import threading + + +# in milliseconds +SAMPLE_INTERVAL = 10 + + +def die(message): + print(message, file=sys.stderr) + # use 'os' over 'sys', since we are calling from a child thread + os._exit(1) + + +def get_main_thread_id(): + for thread_id, frame in sys._current_frames().items(): + if threading.main_thread().ident == thread_id: + return thread_id + die("[mini-scalene] Couldn't find main thread--likely an unimplemented feature.") + + +def sample_running_function(): + main_thread_id = get_main_thread_id() + + frame = sys._current_frames()[main_thread_id] + code = frame.f_code + print(f"[mini-scalene] Currently executing: {code.co_name} in {code.co_filename}:{frame.f_lineno}") + + +def sampling_loop(): + while True: + sample_running_function() + time.sleep(SAMPLE_INTERVAL / 1000.0) + + +def main(): + if len(sys.argv) < 2: + die("[mini-scalene] (Usage): python3 mini-scalene.py file.py {args ...}") + + script = sys.argv[1] + sys.argv = sys.argv[1:] + + t = threading.Thread(target=sampling_loop, daemon=True) + t.start() + + try: + runpy.run_path(script, run_name="__main__") + except Exception as e: + die(f"[mini-scalene] Script died unexpectedly: {e}") + + +if __name__ == "__main__": + main() diff --git a/tests/successive_sleep.py b/tests/successive_sleep.py new file mode 100644 index 0000000..fc9df70 --- /dev/null +++ b/tests/successive_sleep.py @@ -0,0 +1,15 @@ +import time + + +def slow(): + time.sleep(0.4) + + +def main(): + for i in range(3): + time.sleep(0.2) + slow() + + +if __name__ == "__main__": + main() |
