|
| 1 | +// disksnoop.bpf.c |
| 2 | +// eBPF program (compile with: clang -O2 -g -target bpf -c disksnoop.bpf.c -o disksnoop.bpf.o) |
| 3 | + |
| 4 | +#include "vmlinux.h" |
| 5 | +#include <bpf/bpf_helpers.h> |
| 6 | +#include <bpf/bpf_core_read.h> |
| 7 | + |
| 8 | +char LICENSE[] SEC("license") = "GPL"; |
| 9 | + |
| 10 | +struct { |
| 11 | + __uint(type, BPF_MAP_TYPE_HASH); |
| 12 | + __type(key, __u64); |
| 13 | + __type(value, __u64); |
| 14 | + __uint(max_entries, 10240); |
| 15 | +} start_map SEC(".maps"); |
| 16 | + |
| 17 | +/* kprobe: record start timestamp keyed by request pointer */ |
| 18 | +SEC("kprobe/blk_mq_start_request") |
| 19 | +int trace_start(struct pt_regs *ctx) |
| 20 | +{ |
| 21 | + /* request * is first arg */ |
| 22 | + __u64 reqp = (__u64)(ctx->di); |
| 23 | + __u64 ts = bpf_ktime_get_ns(); |
| 24 | + |
| 25 | + bpf_map_update_elem(&start_map, &reqp, &ts, BPF_ANY); |
| 26 | + |
| 27 | +// /* optional debug: |
| 28 | + bpf_printk("start: req=%llu ts=%llu\n", reqp, ts); |
| 29 | +// */ |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +/* completion: compute latency and print data_len, cmd_flags, latency_us */ |
| 34 | +SEC("kprobe/blk_mq_end_request") |
| 35 | +int trace_completion(struct pt_regs *ctx) |
| 36 | +{ |
| 37 | + __u64 reqp = (__u64)(ctx->di); |
| 38 | + __u64 *tsp; |
| 39 | + __u64 now_ns; |
| 40 | + __u64 delta_ns; |
| 41 | + __u64 delta_us = 0; |
| 42 | + bpf_printk("%lld", reqp); |
| 43 | + tsp = bpf_map_lookup_elem(&start_map, &reqp); |
| 44 | + if (!tsp) |
| 45 | + return 0; |
| 46 | + |
| 47 | + now_ns = bpf_ktime_get_ns(); |
| 48 | + delta_ns = now_ns - *tsp; |
| 49 | + delta_us = delta_ns / 1000; |
| 50 | + |
| 51 | + /* read request fields using CO-RE; needs vmlinux.h/BTF */ |
| 52 | + __u32 data_len = 0; |
| 53 | + __u32 cmd_flags = 0; |
| 54 | + |
| 55 | + /* __data_len is usually a 32/64-bit; use CORE read to be safe */ |
| 56 | + data_len = ( __u32 ) BPF_CORE_READ((struct request *)reqp, __data_len); |
| 57 | + cmd_flags = ( __u32 ) BPF_CORE_READ((struct request *)reqp, cmd_flags); |
| 58 | + |
| 59 | + /* print: "<bytes> <flags_hex> <latency_us>" */ |
| 60 | + bpf_printk("%u %x %llu\n", data_len, cmd_flags, delta_us); |
| 61 | + |
| 62 | + /* remove from map */ |
| 63 | + bpf_map_delete_elem(&start_map, &reqp); |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
0 commit comments