Skip to content

Commit f9ee43e

Browse files
committed
Add passing test smp_processor_id.py for helpers
1 parent dabb8bf commit f9ee43e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile, struct
2+
from ctypes import c_void_p, c_int64, c_uint32, c_uint64
3+
from pythonbpf.helper import smp_processor_id, ktime
4+
5+
6+
@bpf
7+
@struct
8+
class cpu_event_t:
9+
cpu_id: c_uint32
10+
timestamp: c_uint64
11+
12+
13+
@bpf
14+
@section("tracepoint/syscalls/sys_enter_execve")
15+
def trace_with_cpu(ctx: c_void_p) -> c_int64:
16+
"""Test bpf_get_smp_processor_id helper function"""
17+
18+
# Get the current CPU ID
19+
cpu = smp_processor_id()
20+
21+
# Print it
22+
print(f"Running on CPU {cpu}")
23+
24+
# Use it in a struct
25+
event = cpu_event_t()
26+
event.cpu_id = smp_processor_id()
27+
event.timestamp = ktime()
28+
29+
print(f"Event on CPU {event.cpu_id} at time {event.timestamp}")
30+
31+
return 0
32+
33+
34+
@bpf
35+
@bpfglobal
36+
def LICENSE() -> str:
37+
return "GPL"
38+
39+
40+
compile()

0 commit comments

Comments
 (0)