mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Add BCC sync_timing example
This commit is contained in:
52
BCC-Examples/sync_timing.py
Normal file
52
BCC-Examples/sync_timing.py
Normal file
@ -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()
|
||||
Reference in New Issue
Block a user