mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
compact the disksnoop.py example
Signed-off-by: varun-r-mallya <varunrmallya@gmail.com>
This commit is contained in:
@ -1,11 +1,10 @@
|
|||||||
from vmlinux import struct_request, struct_pt_regs
|
from ctypes import c_int32, c_int64, c_uint64
|
||||||
from pythonbpf import bpf, section, bpfglobal, compile, map
|
|
||||||
|
from vmlinux import struct_pt_regs, struct_request
|
||||||
|
|
||||||
|
from pythonbpf import bpf, bpfglobal, compile, map, section
|
||||||
from pythonbpf.helper import ktime
|
from pythonbpf.helper import ktime
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
from ctypes import c_int64, c_uint64, c_int32
|
|
||||||
|
|
||||||
# Constants
|
|
||||||
REQ_WRITE = 1 # from include/linux/blk_types.h
|
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
@ -17,24 +16,15 @@ def start() -> HashMap:
|
|||||||
@bpf
|
@bpf
|
||||||
@section("kprobe/blk_mq_end_request")
|
@section("kprobe/blk_mq_end_request")
|
||||||
def trace_completion(ctx: struct_pt_regs) -> c_int64:
|
def trace_completion(ctx: struct_pt_regs) -> c_int64:
|
||||||
# Get request pointer from first argument
|
|
||||||
req_ptr = ctx.di
|
req_ptr = ctx.di
|
||||||
req = struct_request(ctx.di)
|
req = struct_request(ctx.di)
|
||||||
# Print: data_len, cmd_flags, latency_us
|
|
||||||
data_len = req.__data_len
|
data_len = req.__data_len
|
||||||
cmd_flags = req.cmd_flags
|
cmd_flags = req.cmd_flags
|
||||||
# Lookup start timestamp
|
|
||||||
req_tsp = start.lookup(req_ptr)
|
req_tsp = start.lookup(req_ptr)
|
||||||
if req_tsp:
|
if req_tsp:
|
||||||
# Calculate delta in nanoseconds
|
|
||||||
delta = ktime() - req_tsp
|
delta = ktime() - req_tsp
|
||||||
|
|
||||||
# Convert to microseconds for printing
|
|
||||||
delta_us = delta // 1000
|
delta_us = delta // 1000
|
||||||
|
|
||||||
print(f"{data_len} {cmd_flags:x} {delta_us}\n")
|
print(f"{data_len} {cmd_flags:x} {delta_us}\n")
|
||||||
|
|
||||||
# Delete the entry
|
|
||||||
start.delete(req_ptr)
|
start.delete(req_ptr)
|
||||||
|
|
||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user