Skip to content

Commit 4a79f9b

Browse files
committed
Add sync_count BCC-Example
1 parent b676a5e commit 4a79f9b

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

BCC-Examples/sync_count.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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=2)
12+
13+
14+
@bpf
15+
@section("tracepoint/syscalls/sys_enter_sync")
16+
def do_trace(ctx: c_void_p) -> c_int64:
17+
ts_key = 0
18+
cnt_key = 1
19+
tsp = last.lookup(ts_key)
20+
cntp = last.lookup(cnt_key)
21+
if not cntp:
22+
last.update(cnt_key, 0)
23+
cntp = last.lookup(cnt_key)
24+
if tsp:
25+
delta = ktime() - tsp
26+
if delta < 1000000000:
27+
time_ms = delta // 1000000
28+
print(f"{time_ms} {cntp}")
29+
last.delete(ts_key)
30+
else:
31+
last.update(ts_key, ktime())
32+
last.update(cnt_key, cntp + 1)
33+
return 0 # type: ignore [return-value]
34+
35+
36+
@bpf
37+
@bpfglobal
38+
def LICENSE() -> str:
39+
return "GPL"
40+
41+
42+
# compile
43+
b = BPF()
44+
b.load_and_attach()
45+
46+
print("Tracing for quick sync's... Ctrl-C to end")
47+
48+
# format output
49+
start = 0
50+
while True:
51+
try:
52+
task, pid, cpu, flags, ts, msg = trace_fields()
53+
if start == 0:
54+
start = ts
55+
ts -= start
56+
ms, cnt = msg.split()
57+
print(f"At time {ts} s: Multiple syncs detected, last {ms} ms ago. Count {cnt}")
58+
except KeyboardInterrupt:
59+
exit()

0 commit comments

Comments
 (0)