GNU Linux-libre 6.1.90-gnu
[releases.git] / samples / bpf / tracex3_kern.c
1 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2  *
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.
6  */
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>
13
14 struct start_key {
15         dev_t dev;
16         u32 _pad;
17         sector_t sector;
18 };
19
20 struct {
21         __uint(type, BPF_MAP_TYPE_HASH);
22         __type(key, long);
23         __type(value, u64);
24         __uint(max_entries, 4096);
25 } my_map SEC(".maps");
26
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)
30 {
31         u64 val = bpf_ktime_get_ns();
32         struct start_key key = {
33                 .dev = ctx->dev,
34                 .sector = ctx->sector
35         };
36
37         bpf_map_update_elem(&my_map, &key, &val, BPF_ANY);
38         return 0;
39 }
40
41 static unsigned int log2l(unsigned long long n)
42 {
43 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
44         int i = -(n == 0);
45         S(32); S(16); S(8); S(4); S(2); S(1);
46         return i;
47 #undef S
48 }
49
50 #define SLOTS 100
51
52 struct {
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");
58
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)
62 {
63         struct start_key key = {
64                 .dev = ctx->dev,
65                 .sector = ctx->sector
66         };
67
68         u64 *value, l, base;
69         u32 index;
70
71         value = bpf_map_lookup_elem(&my_map, &key);
72         if (!value)
73                 return 0;
74
75         u64 cur_time = bpf_ktime_get_ns();
76         u64 delta = cur_time - *value;
77
78         bpf_map_delete_elem(&my_map, &key);
79
80         /* the lines below are computing index = log10(delta)*10
81          * using integer arithmetic
82          * index = 29 ~ 1 usec
83          * index = 59 ~ 1 msec
84          * index = 89 ~ 1 sec
85          * index = 99 ~ 10sec or more
86          * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
87          */
88         l = log2l(delta);
89         base = 1ll << l;
90         index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
91
92         if (index >= SLOTS)
93                 index = SLOTS - 1;
94
95         value = bpf_map_lookup_elem(&lat_map, &index);
96         if (value)
97                 *value += 1;
98
99         return 0;
100 }
101 char _license[] SEC("license") = "GPL";
102 u32 _version SEC("version") = LINUX_VERSION_CODE;