15ed3ccbdSKaixu Xia#include <linux/ptrace.h>
247efb302SKaixu Xia#include <linux/version.h>
347efb302SKaixu Xia#include <uapi/linux/bpf.h>
47cf245a3SToke Høiland-Jørgensen#include <bpf/bpf_helpers.h>
547efb302SKaixu Xia
659929cd1SDaniel T. Leestruct {
759929cd1SDaniel T. Lee	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
859929cd1SDaniel T. Lee	__uint(key_size, sizeof(int));
959929cd1SDaniel T. Lee	__uint(value_size, sizeof(u32));
1059929cd1SDaniel T. Lee	__uint(max_entries, 64);
1159929cd1SDaniel T. Lee} counters SEC(".maps");
1259929cd1SDaniel T. Lee
1359929cd1SDaniel T. Leestruct {
1459929cd1SDaniel T. Lee	__uint(type, BPF_MAP_TYPE_HASH);
1559929cd1SDaniel T. Lee	__type(key, int);
1659929cd1SDaniel T. Lee	__type(value, u64);
1759929cd1SDaniel T. Lee	__uint(max_entries, 64);
1859929cd1SDaniel T. Lee} values SEC(".maps");
1959929cd1SDaniel T. Lee
2059929cd1SDaniel T. Leestruct {
2159929cd1SDaniel T. Lee	__uint(type, BPF_MAP_TYPE_HASH);
2259929cd1SDaniel T. Lee	__type(key, int);
2359929cd1SDaniel T. Lee	__type(value, struct bpf_perf_event_value);
2459929cd1SDaniel T. Lee	__uint(max_entries, 64);
2559929cd1SDaniel T. Lee} values2 SEC(".maps");
2647efb302SKaixu Xia
2741e9a804STeng QinSEC("kprobe/htab_map_get_next_key")
2847efb302SKaixu Xiaint bpf_prog1(struct pt_regs *ctx)
2947efb302SKaixu Xia{
3047efb302SKaixu Xia	u32 key = bpf_get_smp_processor_id();
3141e9a804STeng Qin	u64 count, *val;
3241e9a804STeng Qin	s64 error;
3341e9a804STeng Qin
3441e9a804STeng Qin	count = bpf_perf_event_read(&counters, key);
3541e9a804STeng Qin	error = (s64)count;
3641e9a804STeng Qin	if (error <= -2 && error >= -22)
3741e9a804STeng Qin		return 0;
3847efb302SKaixu Xia
3941e9a804STeng Qin	val = bpf_map_lookup_elem(&values, &key);
4041e9a804STeng Qin	if (val)
4141e9a804STeng Qin		*val = count;
4241e9a804STeng Qin	else
4341e9a804STeng Qin		bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
4447efb302SKaixu Xia
4547efb302SKaixu Xia	return 0;
4647efb302SKaixu Xia}
4747efb302SKaixu Xia
48020a32d9SYonghong SongSEC("kprobe/htab_map_lookup_elem")
49020a32d9SYonghong Songint bpf_prog2(struct pt_regs *ctx)
50020a32d9SYonghong Song{
51020a32d9SYonghong Song	u32 key = bpf_get_smp_processor_id();
52020a32d9SYonghong Song	struct bpf_perf_event_value *val, buf;
53020a32d9SYonghong Song	int error;
54020a32d9SYonghong Song
55020a32d9SYonghong Song	error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
56020a32d9SYonghong Song	if (error)
57020a32d9SYonghong Song		return 0;
58020a32d9SYonghong Song
59020a32d9SYonghong Song	val = bpf_map_lookup_elem(&values2, &key);
60020a32d9SYonghong Song	if (val)
61020a32d9SYonghong Song		*val = buf;
62020a32d9SYonghong Song	else
63020a32d9SYonghong Song		bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
64020a32d9SYonghong Song
65020a32d9SYonghong Song	return 0;
66020a32d9SYonghong Song}
67020a32d9SYonghong Song
6847efb302SKaixu Xiachar _license[] SEC("license") = "GPL";
6947efb302SKaixu Xiau32 _version SEC("version") = LINUX_VERSION_CODE;
70