Skip to content

Commit d7329ad

Browse files
committed
Add BCC sync_timing example
1 parent 903654d commit d7329ad

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

BCC-Examples/sync_timing.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from pythonbpf import bpf, map, section, bpfglobal, BPF, trace_fields
2+
from pythonbpf.helper import ktime
3+
from pythonbpf.maps import HashMap
4+
5+
from ctypes import c_void_p, c_int64
6+
7+
8+
@bpf
9+
@map
10+
def last() -> HashMap:
11+
return HashMap(key=c_int64, value=c_int64, max_entries=1)
12+
13+
14+
@bpf
15+
@section("tracepoint/syscalls/sys_enter_sync")
16+
def do_trace(ctx: c_void_p) -> c_int64:
17+
key = 0
18+
tsp = last.lookup(key)
19+
if tsp:
20+
delta = ktime() - tsp
21+
if delta < 1000000000:
22+
time_ms = delta // 1000000
23+
print(f"{time_ms}")
24+
last.delete(key)
25+
else:
26+
last.update(key, ktime())
27+
return c_int64(0)
28+
29+
30+
@bpf
31+
@bpfglobal
32+
def LICENSE() -> str:
33+
return "GPL"
34+
35+
36+
# compile
37+
b = BPF()
38+
b.load_and_attach()
39+
40+
print("Tracing for quick sync's... Ctrl-C to end")
41+
42+
# format output
43+
start = 0
44+
while True:
45+
try:
46+
task, pid, cpu, flags, ts, ms = trace_fields()
47+
if start == 0:
48+
start = ts
49+
ts -= start
50+
print(f"At time {ts} s: Multiple syncs detected, last {ms} ms ago")
51+
except KeyboardInterrupt:
52+
exit()

0 commit comments

Comments
 (0)