summaryrefslogtreecommitdiff
path: root/mini-scalene.py
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-06-08 19:27:32 -0400
committerbd <bdunahu@operationnull.com>2025-06-08 19:27:32 -0400
commitb2e45ec5af0b312aa82b950d284ad44f72702f6f (patch)
tree0a7e6158010f9f7260eddb8b05a9e932ab3398b4 /mini-scalene.py
initial commit
Diffstat (limited to 'mini-scalene.py')
-rw-r--r--mini-scalene.py56
1 files changed, 56 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()