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.
8 #include <linux/version.h>
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
19 __uint(type, BPF_MAP_TYPE_HASH);
22 __uint(max_entries, 4096);
23 } my_map SEC(".maps");
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)
29 u64 val = bpf_ktime_get_ns();
30 struct start_key key = {
35 bpf_map_update_elem(&my_map, &key, &val, BPF_ANY);
39 static unsigned int log2l(unsigned long long n)
41 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
43 S(32); S(16); S(8); S(4); S(2); S(1);
51 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
52 __uint(key_size, sizeof(u32));
53 __uint(value_size, sizeof(u64));
54 __uint(max_entries, SLOTS);
55 } lat_map SEC(".maps");
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)
61 struct start_key key = {
69 value = bpf_map_lookup_elem(&my_map, &key);
73 u64 cur_time = bpf_ktime_get_ns();
74 u64 delta = cur_time - *value;
76 bpf_map_delete_elem(&my_map, &key);
78 /* the lines below are computing index = log10(delta)*10
79 * using integer arithmetic
83 * index = 99 ~ 10sec or more
84 * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
88 index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
93 value = bpf_map_lookup_elem(&lat_map, &index);
99 char _license[] SEC("license") = "GPL";
100 u32 _version SEC("version") = LINUX_VERSION_CODE;