blob: e7d180305974e0e581790b3134ef6d012ec6adf0 [file] [log] [blame]
Kaixu Xia5ed3ccb2015-08-12 09:37:53 +00001#include <linux/ptrace.h>
Kaixu Xia47efb302015-08-06 07:02:36 +00002#include <linux/version.h>
3#include <uapi/linux/bpf.h>
4#include "bpf_helpers.h"
5
Teng Qin41e9a802017-06-02 21:03:53 -07006struct bpf_map_def SEC("maps") counters = {
Kaixu Xia47efb302015-08-06 07:02:36 +00007 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
8 .key_size = sizeof(int),
9 .value_size = sizeof(u32),
Teng Qin41e9a802017-06-02 21:03:53 -070010 .max_entries = 64,
11};
12struct 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 Xia47efb302015-08-06 07:02:36 +000017};
18
Teng Qin41e9a802017-06-02 21:03:53 -070019SEC("kprobe/htab_map_get_next_key")
Kaixu Xia47efb302015-08-06 07:02:36 +000020int bpf_prog1(struct pt_regs *ctx)
21{
Kaixu Xia47efb302015-08-06 07:02:36 +000022 u32 key = bpf_get_smp_processor_id();
Teng Qin41e9a802017-06-02 21:03:53 -070023 u64 count, *val;
24 s64 error;
Kaixu Xia47efb302015-08-06 07:02:36 +000025
Teng Qin41e9a802017-06-02 21:03:53 -070026 count = bpf_perf_event_read(&counters, key);
27 error = (s64)count;
28 if (error <= -2 && error >= -22)
29 return 0;
30
31 val = bpf_map_lookup_elem(&values, &key);
32 if (val)
33 *val = count;
34 else
35 bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
Kaixu Xia47efb302015-08-06 07:02:36 +000036
37 return 0;
38}
39
40char _license[] SEC("license") = "GPL";
41u32 _version SEC("version") = LINUX_VERSION_CODE;