Skip to content

Commit cbc6b93

Browse files
committed
restructure maps dir, fix imports
1 parent 9ae1b6c commit cbc6b93

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

examples/c-form/ex8.bpf.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include <linux/blkdev.h>
6+
#define __TARGET_ARCH_aarch64
7+
#define u64 unsigned long long
8+
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_HASH);
11+
__uint(max_entries, 10240);
12+
__type(key, struct request *);
13+
__type(value, u64);
14+
} start SEC(".maps");
15+
16+
SEC("kprobe/blk_start_request")
17+
int BPF_KPROBE(trace_start_req, struct request *req)
18+
{
19+
u64 ts = bpf_ktime_get_ns();
20+
bpf_map_update_elem(&start, &req, &ts, BPF_ANY);
21+
return 0;
22+
}
23+
24+
SEC("kprobe/blk_mq_start_request")
25+
int BPF_KPROBE(trace_start_mq, struct request *req)
26+
{
27+
u64 ts = bpf_ktime_get_ns();
28+
bpf_map_update_elem(&start, &req, &ts, BPF_ANY);
29+
return 0;
30+
}
31+
32+
SEC("kprobe/blk_account_io_completion")
33+
int BPF_KPROBE(trace_completion, struct request *req)
34+
{
35+
u64 *tsp, delta;
36+
37+
tsp = bpf_map_lookup_elem(&start, &req);
38+
if (tsp) {
39+
delta = bpf_ktime_get_ns() - *tsp;
40+
bpf_printk("%d %x %d\n", req->__data_len,
41+
req->cmd_flags, delta / 1000);
42+
bpf_map_delete_elem(&start, &req);
43+
}
44+
return 0;
45+
}
46+
47+
char LICENSE[] SEC("license") = "GPL";

examples/execve2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pythonbpf.decorators import bpf, map, section, bpfglobal
22
from ctypes import c_void_p, c_int64, c_int32, c_uint64
33
from pythonbpf.helpers import ktime
4-
from pythonbpf.maps import HashMap
4+
from pythonbpf.maps.maps import HashMap
55

66

77
@bpf

examples/execve3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pythonbpf import bpf, map, section, bpfglobal, compile
22
from pythonbpf.helpers import ktime, deref
3-
from pythonbpf.maps import HashMap
3+
from pythonbpf.maps.maps import HashMap
44

55
from ctypes import c_void_p, c_int64, c_int32, c_uint64
66

pythonbpf/codegen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from llvmlite import ir
33
from .license_pass import license_processing
44
from .functions_pass import func_proc
5-
from .maps_pass import maps_proc
5+
from pythonbpf.maps.maps_pass import maps_proc
66
from .structs.structs_pass import structs_proc
77
from .globals_pass import globals_processing
88
import os
@@ -125,8 +125,8 @@ def BPF() -> BpfProgram:
125125
caller_frame = inspect.stack()[1]
126126
src = inspect.getsource(caller_frame.frame)
127127
with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".py") as f, \
128-
tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \
129-
tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file:
128+
tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \
129+
tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file:
130130
f.write(src)
131131
f.flush()
132132
source = f.name
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ast
22
from llvmlite import ir
3-
from .type_deducer import ctypes_to_ir
4-
from . import dwarf_constants as dc
3+
from pythonbpf.type_deducer import ctypes_to_ir
4+
from pythonbpf import dwarf_constants as dc
55
from enum import Enum
66

77
map_sym_tab = {}
@@ -28,12 +28,6 @@ class BPFMapType(Enum):
2828
PERF_EVENT_ARRAY = 4
2929

3030

31-
BPF_MAP_MAPPINGS = {
32-
"HASH": 1, # BPF_MAP_TYPE_HASH
33-
"PERF_EVENT_ARRAY": 4, # BPF_MAP_TYPE_PERF_EVENT_ARRAY
34-
}
35-
36-
3731
def create_bpf_map(module, map_name, map_params):
3832
"""Create a BPF map in the module with the given parameters and debug info"""
3933

@@ -191,7 +185,7 @@ def create_map_debug_info(module, map_global, map_name, map_params):
191185

192186
def process_hash_map(map_name, rval, module):
193187
print(f"Creating HashMap map: {map_name}")
194-
map_params: = {"type": BPFMapType.HASH}
188+
map_params = {"type": BPFMapType.HASH}
195189

196190
# Assuming order: key_type, value_type, max_entries
197191
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):

0 commit comments

Comments
 (0)