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>
21 __uint(type, BPF_MAP_TYPE_HASH);
24 __uint(max_entries, 4096);
25 } my_map SEC(".maps");
27 /* from /sys/kernel/tracing/events/block/block_io_start/format */
28 SEC("tracepoint/block/block_io_start")
29 int bpf_prog1(struct trace_event_raw_block_rq *ctx)
31 u64 val = bpf_ktime_get_ns();
32 struct start_key key = {
37 bpf_map_update_elem(&my_map, &key, &val, BPF_ANY);
41 static unsigned int log2l(unsigned long long n)
43 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
45 S(32); S(16); S(8); S(4); S(2); S(1);
53 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
54 __uint(key_size, sizeof(u32));
55 __uint(value_size, sizeof(u64));
56 __uint(max_entries, SLOTS);
57 } lat_map SEC(".maps");
59 /* from /sys/kernel/tracing/events/block/block_io_done/format */
60 SEC("tracepoint/block/block_io_done")
61 int bpf_prog2(struct trace_event_raw_block_rq *ctx)
63 struct start_key key = {
71 value = bpf_map_lookup_elem(&my_map, &key);
75 u64 cur_time = bpf_ktime_get_ns();
76 u64 delta = cur_time - *value;
78 bpf_map_delete_elem(&my_map, &key);
80 /* the lines below are computing index = log10(delta)*10
81 * using integer arithmetic
85 * index = 99 ~ 10sec or more
86 * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
90 index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
95 value = bpf_map_lookup_elem(&lat_map, &index);
101 char _license[] SEC("license") = "GPL";
102 u32 _version SEC("version") = LINUX_VERSION_CODE;