Skip to content

Commit 9263211

Browse files
DanielTimLeeAlexei Starovoitov
authored andcommitted
samples/bpf: fix bio latency check with tracepoint
Recently, a new tracepoint for the block layer, specifically the block_io_start/done tracepoints, was introduced in commit 5a80bd0 ("block: introduce block_io_start/block_io_done tracepoints"). Previously, the kprobe entry used for this purpose was quite unstable and inherently broke relevant probes [1]. Now that a stable tracepoint is available, this commit replaces the bio latency check with it. One of the changes made during this replacement is the key used for the hash table. Since 'struct request' cannot be used as a hash key, the approach taken follows that which was implemented in bcc/biolatency [2]. (uses dev:sector for the key) [1]: iovisor/bcc#4261 [2]: iovisor/bcc#4691 Fixes: 450b787 ("block: move blk_account_io_{start,done} to blk-mq.c") Signed-off-by: Daniel T. Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1143042 commit 9263211

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

samples/bpf/tracex3.bpf.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@
99
#include <bpf/bpf_helpers.h>
1010
#include <bpf/bpf_tracing.h>
1111

12+
struct start_key {
13+
dev_t dev;
14+
u32 _pad;
15+
sector_t sector;
16+
};
17+
1218
struct {
1319
__uint(type, BPF_MAP_TYPE_HASH);
1420
__type(key, long);
1521
__type(value, u64);
1622
__uint(max_entries, 4096);
1723
} my_map SEC(".maps");
1824

19-
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
20-
* example will no longer be meaningful
21-
*/
22-
SEC("kprobe/blk_mq_start_request")
23-
int bpf_prog1(struct pt_regs *ctx)
25+
/* from /sys/kernel/tracing/events/block/block_io_start/format */
26+
SEC("tracepoint/block/block_io_start")
27+
int bpf_prog1(struct trace_event_raw_block_rq *ctx)
2428
{
25-
long rq = PT_REGS_PARM1(ctx);
2629
u64 val = bpf_ktime_get_ns();
30+
struct start_key key = {
31+
.dev = ctx->dev,
32+
.sector = ctx->sector
33+
};
2734

28-
bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
35+
bpf_map_update_elem(&my_map, &key, &val, BPF_ANY);
2936
return 0;
3037
}
3138

@@ -47,21 +54,26 @@ struct {
4754
__uint(max_entries, SLOTS);
4855
} lat_map SEC(".maps");
4956

50-
SEC("kprobe/__blk_account_io_done")
51-
int bpf_prog2(struct pt_regs *ctx)
57+
/* from /sys/kernel/tracing/events/block/block_io_done/format */
58+
SEC("tracepoint/block/block_io_done")
59+
int bpf_prog2(struct trace_event_raw_block_rq *ctx)
5260
{
53-
long rq = PT_REGS_PARM1(ctx);
61+
struct start_key key = {
62+
.dev = ctx->dev,
63+
.sector = ctx->sector
64+
};
65+
5466
u64 *value, l, base;
5567
u32 index;
5668

57-
value = bpf_map_lookup_elem(&my_map, &rq);
69+
value = bpf_map_lookup_elem(&my_map, &key);
5870
if (!value)
5971
return 0;
6072

6173
u64 cur_time = bpf_ktime_get_ns();
6274
u64 delta = cur_time - *value;
6375

64-
bpf_map_delete_elem(&my_map, &rq);
76+
bpf_map_delete_elem(&my_map, &key);
6577

6678
/* the lines below are computing index = log10(delta)*10
6779
* using integer arithmetic

0 commit comments

Comments
 (0)