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_helpers.h"
13 struct bpf_map_def SEC("maps") my_map = {
14 .type = BPF_MAP_TYPE_HASH,
15 .key_size = sizeof(long),
16 .value_size = sizeof(u64),
20 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
21 * example will no longer be meaningful
23 SEC("kprobe/blk_mq_start_request")
24 int bpf_prog1(struct pt_regs *ctx)
26 long rq = PT_REGS_PARM1(ctx);
27 u64 val = bpf_ktime_get_ns();
29 bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
33 static unsigned int log2l(unsigned long long n)
35 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
37 S(32); S(16); S(8); S(4); S(2); S(1);
44 struct bpf_map_def SEC("maps") lat_map = {
45 .type = BPF_MAP_TYPE_ARRAY,
46 .key_size = sizeof(u32),
47 .value_size = sizeof(u64),
51 SEC("kprobe/blk_update_request")
52 int bpf_prog2(struct pt_regs *ctx)
54 long rq = PT_REGS_PARM1(ctx);
58 value = bpf_map_lookup_elem(&my_map, &rq);
62 u64 cur_time = bpf_ktime_get_ns();
63 u64 delta = cur_time - *value;
65 bpf_map_delete_elem(&my_map, &rq);
67 /* the lines below are computing index = log10(delta)*10
68 * using integer arithmetic
72 * index = 99 ~ 10sec or more
73 * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
77 index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
82 value = bpf_map_lookup_elem(&lat_map, &index);
84 __sync_fetch_and_add((long *)value, 1);
88 char _license[] SEC("license") = "GPL";
89 u32 _version SEC("version") = LINUX_VERSION_CODE;