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