1 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
9 #include <linux/version.h>
10 #include <uapi/linux/bpf.h>
11 #include <bpf/bpf_helpers.h>
12 #include <bpf/bpf_tracing.h>
15 __uint(type, BPF_MAP_TYPE_HASH);
18 __uint(max_entries, 4096);
19 } my_map SEC(".maps");
21 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
22 * example will no longer be meaningful
24 SEC("kprobe/blk_mq_start_request")
25 int bpf_prog1(struct pt_regs *ctx)
27 long rq = PT_REGS_PARM1(ctx);
28 u64 val = bpf_ktime_get_ns();
30 bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
34 static unsigned int log2l(unsigned long long n)
36 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
38 S(32); S(16); S(8); S(4); S(2); S(1);
46 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
47 __uint(key_size, sizeof(u32));
48 __uint(value_size, sizeof(u64));
49 __uint(max_entries, SLOTS);
50 } lat_map SEC(".maps");
52 SEC("kprobe/blk_account_io_done")
53 int bpf_prog2(struct pt_regs *ctx)
55 long rq = PT_REGS_PARM1(ctx);
59 value = bpf_map_lookup_elem(&my_map, &rq);
63 u64 cur_time = bpf_ktime_get_ns();
64 u64 delta = cur_time - *value;
66 bpf_map_delete_elem(&my_map, &rq);
68 /* the lines below are computing index = log10(delta)*10
69 * using integer arithmetic
73 * index = 99 ~ 10sec or more
74 * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
78 index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
83 value = bpf_map_lookup_elem(&lat_map, &index);
89 char _license[] SEC("license") = "GPL";
90 u32 _version SEC("version") = LINUX_VERSION_CODE;