Skip to content

Commit 14af7ec

Browse files
add file_io.bpf.py to make container-monitor
1 parent 536ea48 commit 14af7ec

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import logging
2+
3+
from pythonbpf import bpf, map, section, bpfglobal, struct, compile
4+
from pythonbpf.maps import HashMap
5+
from pythonbpf.helper import get_current_cgroup_id
6+
from ctypes import c_int32, c_uint64
7+
from vmlinux import struct_pt_regs
8+
9+
10+
@bpf
11+
@struct
12+
class read_stats:
13+
bytes: c_uint64
14+
ops: c_uint64
15+
16+
17+
@bpf
18+
@struct
19+
class write_stats:
20+
bytes: c_uint64
21+
ops: c_uint64
22+
23+
24+
@bpf
25+
@map
26+
def read_map() -> HashMap:
27+
return HashMap(key=c_uint64, value=read_stats, max_entries=1024)
28+
29+
30+
@bpf
31+
@map
32+
def write_map() -> HashMap:
33+
return HashMap(key=c_uint64, value=write_stats, max_entries=1024)
34+
35+
36+
#
37+
# READ PROBE
38+
#
39+
@bpf
40+
@section("kprobe/vfs_read")
41+
def trace_read(ctx: struct_pt_regs) -> c_int32:
42+
cg = get_current_cgroup_id()
43+
count = c_uint64(ctx.dx)
44+
ptr = read_map.lookup(cg)
45+
46+
if ptr:
47+
s = read_stats()
48+
s.bytes = ptr.bytes + count
49+
s.ops = ptr.ops + 1
50+
read_map.update(cg, ptr)
51+
else:
52+
print("read init")
53+
s = read_stats()
54+
s.bytes = count
55+
s.ops = c_uint64(1)
56+
read_map.update(cg, s)
57+
58+
return c_int32(0)
59+
60+
61+
#
62+
# WRITE PROBE
63+
#
64+
@bpf
65+
@section("kprobe/vfs_write")
66+
def trace_write(ctx1: struct_pt_regs) -> c_int32:
67+
cg = get_current_cgroup_id()
68+
count = c_uint64(ctx1.dx)
69+
ptr = write_map.lookup(cg)
70+
71+
if ptr:
72+
s = write_stats()
73+
s.bytes = ptr.bytes + count
74+
s.ops = ptr.ops + 1
75+
write_map.update(cg, s)
76+
else:
77+
print("write init")
78+
s = write_stats()
79+
s.bytes = count
80+
s.ops = c_uint64(1)
81+
write_map.update(cg, s)
82+
83+
return c_int32(0)
84+
85+
86+
@bpf
87+
@bpfglobal
88+
def LICENSE() -> str:
89+
return "GPL"
90+
91+
92+
compile(loglevel=logging.INFO)

0 commit comments

Comments
 (0)