Skip to content

Commit 5bba8dc

Browse files
committed
Complete hello_perf_output BCC example
1 parent 8c976e4 commit 5bba8dc

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

BCC-Examples/hello_perf_output.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from pythonbpf import bpf, map, struct, section, bpfglobal, BPF, trace_pipe
1+
from pythonbpf import bpf, map, struct, section, bpfglobal, BPF
22
from pythonbpf.helper import ktime, pid, comm
33
from pythonbpf.maps import PerfEventArray
4-
54
from ctypes import c_void_p, c_int64, c_uint64
65

76

@@ -25,7 +24,6 @@ def hello(ctx: c_void_p) -> c_int64:
2524
dataobj = data_t()
2625
dataobj.pid, dataobj.ts = pid(), ktime()
2726
comm(dataobj.comm)
28-
print(f"clone called at {dataobj.ts} by pid {dataobj.pid}, comm {dataobj.comm}")
2927
events.output(dataobj)
3028
return 0 # type: ignore [return-value]
3129

@@ -36,8 +34,28 @@ def LICENSE() -> str:
3634
return "GPL"
3735

3836

39-
# compile
40-
BPF().load_and_attach()
37+
# Compile and load
38+
b = BPF()
39+
b.load()
40+
attached = b.attach_all()
41+
42+
start = 0
43+
44+
45+
def callback(cpu, event):
46+
global start
47+
if start == 0:
48+
start = event.ts
49+
ts = (event.ts - start) / 1e9
50+
print(f"[CPU {cpu}] PID: {event.pid}, TS: {ts}, COMM: {event.comm.decode()}")
51+
52+
53+
perf = b["events"].open_perf_buffer(callback, struct_name="data_t")
54+
print("Starting to poll... (Ctrl+C to stop)")
55+
print("Try running: fork() or clone() system calls to trigger events")
4156

42-
print("Tracing clone()... Ctrl-C to end")
43-
trace_pipe()
57+
try:
58+
while True:
59+
b["events"].poll(1000)
60+
except KeyboardInterrupt:
61+
print("Stopping...")

0 commit comments

Comments
 (0)