Add recursive dereferencing and get example working

This commit is contained in:
2025-09-13 00:12:04 +05:30
parent ca203a1fdd
commit 9f858bd159
4 changed files with 47 additions and 10 deletions

View File

@ -1,9 +1,14 @@
from pythonbpf import bpf, map, section, bpfglobal, compile
from pythonbpf.helpers import ktime
from pythonbpf.helpers import ktime, deref
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_uint64
# Instructions to how to run this program
# 1. Install PythonBPF: pip install pythonbpf
# 2. Run the program: python demo/pybpf.py
# 3. Run the program with sudo: sudo examples/check.sh run demo/pybpf.o
# 4. Start a Python repl and `import os` and then keep entering `os.sync()` to see reponses.
@bpf
@map
@ -12,16 +17,20 @@ def last() -> HashMap:
@bpf
@section("tracepoint/syscalls/sys_sync")
@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)
kt = ktime()
delta = (kt - tsp)
if delta < 1000000000:
time_ms = (delta // 1000000)
print(f"sync called within last second, last {time_ms} ms ago")
last().delete(key)
else:
kt = ktime()
last().update(key, kt)
return c_int64(0)