From cbc6b93cd82b1cb4df929504ea945183302f41bf Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 30 Sep 2025 19:01:46 +0530 Subject: [PATCH] restructure maps dir, fix imports --- examples/c-form/ex8.bpf.c | 47 +++++++++++++++++++++++++++++++ examples/execve2.py | 2 +- examples/execve3.py | 2 +- pythonbpf/codegen.py | 6 ++-- pythonbpf/{ => maps}/maps.py | 0 pythonbpf/{ => maps}/maps_pass.py | 12 ++------ 6 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 examples/c-form/ex8.bpf.c rename pythonbpf/{ => maps}/maps.py (100%) rename pythonbpf/{ => maps}/maps_pass.py (97%) diff --git a/examples/c-form/ex8.bpf.c b/examples/c-form/ex8.bpf.c new file mode 100644 index 0000000..4163abd --- /dev/null +++ b/examples/c-form/ex8.bpf.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#define __TARGET_ARCH_aarch64 +#define u64 unsigned long long + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 10240); + __type(key, struct request *); + __type(value, u64); +} start SEC(".maps"); + +SEC("kprobe/blk_start_request") +int BPF_KPROBE(trace_start_req, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +SEC("kprobe/blk_mq_start_request") +int BPF_KPROBE(trace_start_mq, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +SEC("kprobe/blk_account_io_completion") +int BPF_KPROBE(trace_completion, struct request *req) +{ + u64 *tsp, delta; + + tsp = bpf_map_lookup_elem(&start, &req); + if (tsp) { + delta = bpf_ktime_get_ns() - *tsp; + bpf_printk("%d %x %d\n", req->__data_len, + req->cmd_flags, delta / 1000); + bpf_map_delete_elem(&start, &req); + } + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; diff --git a/examples/execve2.py b/examples/execve2.py index fadc396..a5272af 100644 --- a/examples/execve2.py +++ b/examples/execve2.py @@ -1,7 +1,7 @@ from pythonbpf.decorators import bpf, map, section, bpfglobal from ctypes import c_void_p, c_int64, c_int32, c_uint64 from pythonbpf.helpers import ktime -from pythonbpf.maps import HashMap +from pythonbpf.maps.maps import HashMap @bpf diff --git a/examples/execve3.py b/examples/execve3.py index f636a34..03e0b35 100644 --- a/examples/execve3.py +++ b/examples/execve3.py @@ -1,6 +1,6 @@ from pythonbpf import bpf, map, section, bpfglobal, compile from pythonbpf.helpers import ktime, deref -from pythonbpf.maps import HashMap +from pythonbpf.maps.maps import HashMap from ctypes import c_void_p, c_int64, c_int32, c_uint64 diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 7eaa713..c3c1ae4 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -2,7 +2,7 @@ import ast from llvmlite import ir from .license_pass import license_processing from .functions_pass import func_proc -from .maps_pass import maps_proc +from pythonbpf.maps.maps_pass import maps_proc from .structs.structs_pass import structs_proc from .globals_pass import globals_processing import os @@ -125,8 +125,8 @@ def BPF() -> BpfProgram: caller_frame = inspect.stack()[1] src = inspect.getsource(caller_frame.frame) with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".py") as f, \ - tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \ - tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file: + tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \ + tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file: f.write(src) f.flush() source = f.name diff --git a/pythonbpf/maps.py b/pythonbpf/maps/maps.py similarity index 100% rename from pythonbpf/maps.py rename to pythonbpf/maps/maps.py diff --git a/pythonbpf/maps_pass.py b/pythonbpf/maps/maps_pass.py similarity index 97% rename from pythonbpf/maps_pass.py rename to pythonbpf/maps/maps_pass.py index eeb5338..cec0772 100644 --- a/pythonbpf/maps_pass.py +++ b/pythonbpf/maps/maps_pass.py @@ -1,7 +1,7 @@ import ast from llvmlite import ir -from .type_deducer import ctypes_to_ir -from . import dwarf_constants as dc +from pythonbpf.type_deducer import ctypes_to_ir +from pythonbpf import dwarf_constants as dc from enum import Enum map_sym_tab = {} @@ -28,12 +28,6 @@ class BPFMapType(Enum): PERF_EVENT_ARRAY = 4 -BPF_MAP_MAPPINGS = { - "HASH": 1, # BPF_MAP_TYPE_HASH - "PERF_EVENT_ARRAY": 4, # BPF_MAP_TYPE_PERF_EVENT_ARRAY -} - - def create_bpf_map(module, map_name, map_params): """Create a BPF map in the module with the given parameters and debug info""" @@ -191,7 +185,7 @@ def create_map_debug_info(module, map_global, map_name, map_params): def process_hash_map(map_name, rval, module): print(f"Creating HashMap map: {map_name}") - map_params: = {"type": BPFMapType.HASH} + map_params = {"type": BPFMapType.HASH} # Assuming order: key_type, value_type, max_entries if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):