Kaixu Xia | 5ed3ccb | 2015-08-12 09:37:53 +0000 | [diff] [blame] | 1 | #include <linux/ptrace.h> |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 2 | #include <linux/version.h> |
| 3 | #include <uapi/linux/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 4 | #include <bpf/bpf_helpers.h> |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 5 | |
Daniel T. Lee | 59929cd | 2020-05-16 13:06:08 +0900 | [diff] [blame] | 6 | struct { |
| 7 | __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); |
| 8 | __uint(key_size, sizeof(int)); |
| 9 | __uint(value_size, sizeof(u32)); |
| 10 | __uint(max_entries, 64); |
| 11 | } counters SEC(".maps"); |
| 12 | |
| 13 | struct { |
| 14 | __uint(type, BPF_MAP_TYPE_HASH); |
| 15 | __type(key, int); |
| 16 | __type(value, u64); |
| 17 | __uint(max_entries, 64); |
| 18 | } values SEC(".maps"); |
| 19 | |
| 20 | struct { |
| 21 | __uint(type, BPF_MAP_TYPE_HASH); |
| 22 | __type(key, int); |
| 23 | __type(value, struct bpf_perf_event_value); |
| 24 | __uint(max_entries, 64); |
| 25 | } values2 SEC(".maps"); |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 26 | |
Teng Qin | 41e9a80 | 2017-06-02 21:03:53 -0700 | [diff] [blame] | 27 | SEC("kprobe/htab_map_get_next_key") |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 28 | int bpf_prog1(struct pt_regs *ctx) |
| 29 | { |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 30 | u32 key = bpf_get_smp_processor_id(); |
Teng Qin | 41e9a80 | 2017-06-02 21:03:53 -0700 | [diff] [blame] | 31 | u64 count, *val; |
| 32 | s64 error; |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 33 | |
Teng Qin | 41e9a80 | 2017-06-02 21:03:53 -0700 | [diff] [blame] | 34 | count = bpf_perf_event_read(&counters, key); |
| 35 | error = (s64)count; |
| 36 | if (error <= -2 && error >= -22) |
| 37 | return 0; |
| 38 | |
| 39 | val = bpf_map_lookup_elem(&values, &key); |
| 40 | if (val) |
| 41 | *val = count; |
| 42 | else |
| 43 | bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST); |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Yonghong Song | 020a32d | 2017-10-05 09:19:21 -0700 | [diff] [blame] | 48 | SEC("kprobe/htab_map_lookup_elem") |
| 49 | int bpf_prog2(struct pt_regs *ctx) |
| 50 | { |
| 51 | u32 key = bpf_get_smp_processor_id(); |
| 52 | struct bpf_perf_event_value *val, buf; |
| 53 | int error; |
| 54 | |
| 55 | error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf)); |
| 56 | if (error) |
| 57 | return 0; |
| 58 | |
| 59 | val = bpf_map_lookup_elem(&values2, &key); |
| 60 | if (val) |
| 61 | *val = buf; |
| 62 | else |
| 63 | bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Kaixu Xia | 47efb30 | 2015-08-06 07:02:36 +0000 | [diff] [blame] | 68 | char _license[] SEC("license") = "GPL"; |
| 69 | u32 _version SEC("version") = LINUX_VERSION_CODE; |