From d7329ad3d75169171a14a842bab84cd97f41e623 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 14 Oct 2025 16:07:55 +0530 Subject: [PATCH] Add BCC sync_timing example --- BCC-Examples/sync_timing.py | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 BCC-Examples/sync_timing.py diff --git a/BCC-Examples/sync_timing.py b/BCC-Examples/sync_timing.py new file mode 100644 index 0000000..37d6a03 --- /dev/null +++ b/BCC-Examples/sync_timing.py @@ -0,0 +1,52 @@ +from pythonbpf import bpf, map, section, bpfglobal, BPF, trace_fields +from pythonbpf.helper import ktime +from pythonbpf.maps import HashMap + +from ctypes import c_void_p, c_int64 + + +@bpf +@map +def last() -> HashMap: + return HashMap(key=c_int64, value=c_int64, max_entries=1) + + +@bpf +@section("tracepoint/syscalls/sys_enter_sync") +def do_trace(ctx: c_void_p) -> c_int64: + key = 0 + tsp = last.lookup(key) + if tsp: + delta = ktime() - tsp + if delta < 1000000000: + time_ms = delta // 1000000 + print(f"{time_ms}") + last.delete(key) + else: + last.update(key, ktime()) + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +# compile +b = BPF() +b.load_and_attach() + +print("Tracing for quick sync's... Ctrl-C to end") + +# format output +start = 0 +while True: + try: + task, pid, cpu, flags, ts, ms = trace_fields() + if start == 0: + start = ts + ts -= start + print(f"At time {ts} s: Multiple syncs detected, last {ms} ms ago") + except KeyboardInterrupt: + exit()