^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <bpf/bpf_tracing.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) __type(key, long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) __type(value, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) __uint(max_entries, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) } my_map SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * example will no longer be meaningful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) SEC("kprobe/blk_mq_start_request")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int bpf_prog1(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) long rq = PT_REGS_PARM1(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u64 val = bpf_ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static unsigned int log2l(unsigned long long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int i = -(n == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) S(32); S(16); S(8); S(4); S(2); S(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #undef S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define SLOTS 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __uint(key_size, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __uint(value_size, sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __uint(max_entries, SLOTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } lat_map SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) SEC("kprobe/blk_account_io_done")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int bpf_prog2(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) long rq = PT_REGS_PARM1(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u64 *value, l, base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) value = bpf_map_lookup_elem(&my_map, &rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u64 cur_time = bpf_ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u64 delta = cur_time - *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bpf_map_delete_elem(&my_map, &rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* the lines below are computing index = log10(delta)*10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * using integer arithmetic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * index = 29 ~ 1 usec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * index = 59 ~ 1 msec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * index = 89 ~ 1 sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * index = 99 ~ 10sec or more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) l = log2l(delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) base = 1ll << l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (index >= SLOTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) index = SLOTS - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) value = bpf_map_lookup_elem(&lat_map, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *value += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) char _license[] SEC("license") = "GPL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u32 _version SEC("version") = LINUX_VERSION_CODE;