forked from sched-ext/scx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched_ftrace.py
executable file
·66 lines (51 loc) · 1.68 KB
/
sched_ftrace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import os
import sys
import time
HEADER = "TASK-PID"
BUFF_STARTED = "buffer started ###"
TRACING_PATH = "/sys/kernel/debug/tracing"
TRACE_PIPE_PATH = os.path.join(TRACING_PATH, "trace_pipe")
def ftrace_trim(stream, duration, nproc):
nproc = nproc - 1
seen_header = False
proc_buffer_started = 0
start_time = time.time()
for line in stream:
if time.time() - start_time >= duration:
break
l = line.replace("\n", "")
if HEADER in l:
seen_header = True
print(l)
if BUFF_STARTED in line:
proc_buffer_started += 1
continue
if proc_buffer_started == nproc or not seen_header:
print(l)
def run_trace(duration):
tracing_on_path = os.path.join(TRACING_PATH, "tracing_on")
sched_switch_enable_path = os.path.join(
TRACING_PATH, "events/sched/sched_switch/enable"
)
# Enable tracing and sched_switch event
with open(tracing_on_path, "w") as f:
f.write("1")
with open(sched_switch_enable_path, "w") as f:
f.write("1")
# Process the sched_switch events from the trace file
try:
with open(TRACE_PIPE_PATH, "r") as trace_pipe:
ftrace_trim(trace_pipe, duration, os.cpu_count())
except KeyboardInterrupt:
pass # Allow clean termination with Ctrl+C
# Disable tracing and sched_switch event after the duration
with open(sched_switch_enable_path, "w") as f:
f.write("0")
with open(tracing_on_path, "w") as f:
f.write("0")
def main():
duration = int(sys.argv[1]) if len(sys.argv) > 1 else 5
run_trace(duration)
if __name__ == "__main__":
main()