Skip to content

Commit 8cb430e

Browse files
committed
gh-141645: Add a TUI mode to the new tachyon profiler
1 parent ed73c90 commit 8cb430e

File tree

5 files changed

+4058
-16
lines changed

5 files changed

+4058
-16
lines changed

Lib/profiling/sampling/_trace.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Tracing utility for live profiler debugging.
2+
3+
Usage:
4+
export SAMPLING_LIVE_TRACE=/tmp/live_trace.log
5+
./python -m profiling.sampling --live -p <PID>
6+
"""
7+
8+
import os
9+
import sys
10+
import time
11+
12+
trace_file = None
13+
if trace_filename := os.environ.get("SAMPLING_LIVE_TRACE"):
14+
trace_file = open(trace_filename, "a")
15+
trace_file.write(f"\n{'='*80}\n")
16+
trace_file.write(f"Live Profiler Trace Session Started: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
17+
trace_file.write(f"{'='*80}\n")
18+
trace_file.flush()
19+
20+
21+
def trace(line: str, *args, **kwargs) -> None:
22+
if trace_file is None:
23+
return
24+
25+
if args or kwargs:
26+
line = line.format(*args, **kwargs)
27+
28+
timestamp = time.strftime("%H:%M:%S")
29+
trace_file.write(f"[{timestamp}] {line}\n")
30+
trace_file.flush()
31+
32+
33+
def trace_exception(msg: str, exc: Exception) -> None:
34+
if trace_file is None:
35+
return
36+
37+
import traceback
38+
timestamp = time.strftime("%H:%M:%S")
39+
trace_file.write(f"[{timestamp}] EXCEPTION: {msg}\n")
40+
traceback.print_exc(file=trace_file)
41+
trace_file.flush()
42+
43+
44+
def close_trace() -> None:
45+
global trace_file
46+
if trace_file is not None:
47+
trace_file.write(f"\n{'='*80}\n")
48+
trace_file.write(f"Trace Session Ended: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
49+
trace_file.write(f"{'='*80}\n\n")
50+
trace_file.close()
51+
trace_file = None

0 commit comments

Comments
 (0)