mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Add side by side view
This commit is contained in:
46
demo/bcc.py
Normal file
46
demo/bcc.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
from bcc import BPF
|
||||||
|
from bcc.utils import printb
|
||||||
|
|
||||||
|
# load BPF program
|
||||||
|
b = BPF(text="""
|
||||||
|
#include <uapi/linux/ptrace.h>
|
||||||
|
|
||||||
|
BPF_HASH(last);
|
||||||
|
|
||||||
|
int do_trace(struct pt_regs *ctx) {
|
||||||
|
u64 ts, *tsp, delta, key = 0;
|
||||||
|
|
||||||
|
// attempt to read stored timestamp
|
||||||
|
tsp = last.lookup(&key);
|
||||||
|
if (tsp != NULL) {
|
||||||
|
delta = bpf_ktime_get_ns() - *tsp;
|
||||||
|
if (delta < 1000000000) {
|
||||||
|
// output if time is less than 1 second
|
||||||
|
bpf_trace_printk("%d\\n", delta / 1000000);
|
||||||
|
}
|
||||||
|
last.delete(&key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// update stored timestamp
|
||||||
|
ts = bpf_ktime_get_ns();
|
||||||
|
last.update(&key, &ts);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
b.attach_kprobe(event=b.get_syscall_fnname("sync"), fn_name="do_trace")
|
||||||
|
print("Tracing for quick sync's... Ctrl-C to end")
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
# format output
|
||||||
|
start = 0
|
||||||
|
while 1:
|
||||||
|
try:
|
||||||
|
(task, pid, cpu, flags, ts, ms) = b.trace_fields()
|
||||||
|
if start == 0:
|
||||||
|
start = ts
|
||||||
|
ts = ts - start
|
||||||
|
printb(b"At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
exit()
|
||||||
34
demo/pybpf.py
Normal file
34
demo/pybpf.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
|
from pythonbpf.helpers import ktime
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
|
from ctypes import c_void_p, c_int64, c_uint64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def last() -> HashMap:
|
||||||
|
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=3)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_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"sync called within last second, last {time_ms} ms ago")
|
||||||
|
last().delete(key)
|
||||||
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
compile()
|
||||||
Reference in New Issue
Block a user