mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-02-11 15:40:56 +00:00
Compare commits
7 Commits
5bba8dce12
...
d0fecbc03c
| Author | SHA1 | Date | |
|---|---|---|---|
| d0fecbc03c | |||
| 174095973b | |||
| 3273620447 | |||
| 610cbe82a8 | |||
| 54c97e648b | |||
| dd9411b7b9 | |||
| aa85d0e0ef |
@ -15,9 +15,10 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
# compile
|
# Compile and load
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load_and_attach()
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
# header
|
# header
|
||||||
print(f"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}")
|
print(f"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}")
|
||||||
|
|||||||
@ -37,7 +37,7 @@ def LICENSE() -> str:
|
|||||||
# Compile and load
|
# Compile and load
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load()
|
b.load()
|
||||||
attached = b.attach_all()
|
b.attach_all()
|
||||||
|
|
||||||
start = 0
|
start = 0
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,9 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
# Compile and load
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load_and_attach()
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
trace_pipe()
|
trace_pipe()
|
||||||
|
|||||||
@ -37,9 +37,10 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
# compile
|
# Compile and load
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load_and_attach()
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
print("Tracing for quick sync's... Ctrl-C to end")
|
print("Tracing for quick sync's... Ctrl-C to end")
|
||||||
|
|
||||||
|
|||||||
78
BCC-Examples/sync_perf_output.py
Normal file
78
BCC-Examples/sync_perf_output.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
from pythonbpf import bpf, map, struct, section, bpfglobal, BPF
|
||||||
|
from pythonbpf.helper import ktime
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
from pythonbpf.maps import PerfEventArray
|
||||||
|
from ctypes import c_void_p, c_int64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@struct
|
||||||
|
class data_t:
|
||||||
|
ts: c_int64
|
||||||
|
ms: c_int64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def events() -> PerfEventArray:
|
||||||
|
return PerfEventArray(key_size=c_int64, value_size=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:
|
||||||
|
dat, dat.ts, key = data_t(), ktime(), 0
|
||||||
|
tsp = last.lookup(key)
|
||||||
|
if tsp:
|
||||||
|
delta = ktime() - tsp
|
||||||
|
if delta < 1000000000:
|
||||||
|
dat.ms = delta // 1000000
|
||||||
|
events.output(dat)
|
||||||
|
last.delete(key)
|
||||||
|
else:
|
||||||
|
last.update(key, ktime())
|
||||||
|
return 0 # type: ignore [return-value]
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
# Compile and load
|
||||||
|
b = BPF()
|
||||||
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
|
print("Tracing for quick sync's... Ctrl-C to end")
|
||||||
|
|
||||||
|
# format output
|
||||||
|
start = 0
|
||||||
|
|
||||||
|
|
||||||
|
def callback(cpu, event):
|
||||||
|
global start
|
||||||
|
if start == 0:
|
||||||
|
start = event.ts
|
||||||
|
event.ts -= start
|
||||||
|
print(
|
||||||
|
f"At time {event.ts / 1e9} s: Multiple sync detected, Last sync: {event.ms} ms ago"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
perf = b["events"].open_perf_buffer(callback, struct_name="data_t")
|
||||||
|
print("Starting to poll... (Ctrl+C to stop)")
|
||||||
|
print("Try running: fork() or clone() system calls to trigger events")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
b["events"].poll(1000)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Stopping...")
|
||||||
@ -33,9 +33,10 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
# compile
|
# Compile and load
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load_and_attach()
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
|
|
||||||
print("Tracing for quick sync's... Ctrl-C to end")
|
print("Tracing for quick sync's... Ctrl-C to end")
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,9 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
BPF().load_and_attach()
|
# Compile and load
|
||||||
|
b = BPF()
|
||||||
|
b.load()
|
||||||
|
b.attach_all()
|
||||||
print("Tracing sys_sync()... Ctrl-C to end.")
|
print("Tracing sys_sync()... Ctrl-C to end.")
|
||||||
trace_pipe()
|
trace_pipe()
|
||||||
|
|||||||
Reference in New Issue
Block a user