1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_BPF_COUNTER_H
3 #define __PERF_BPF_COUNTER_H 1
5 #include <linux/list.h>
6 #include <sys/resource.h>
9 #include <bpf/libbpf.h>
15 typedef int (*bpf_counter_evsel_op)(struct evsel *evsel);
16 typedef int (*bpf_counter_evsel_target_op)(struct evsel *evsel,
17 struct target *target);
18 typedef int (*bpf_counter_evsel_install_pe_op)(struct evsel *evsel,
22 struct bpf_counter_ops {
23 bpf_counter_evsel_target_op load;
24 bpf_counter_evsel_op enable;
25 bpf_counter_evsel_op disable;
26 bpf_counter_evsel_op read;
27 bpf_counter_evsel_op destroy;
28 bpf_counter_evsel_install_pe_op install_pe;
33 struct list_head list;
38 int bpf_counter__load(struct evsel *evsel, struct target *target);
39 int bpf_counter__enable(struct evsel *evsel);
40 int bpf_counter__disable(struct evsel *evsel);
41 int bpf_counter__read(struct evsel *evsel);
42 void bpf_counter__destroy(struct evsel *evsel);
43 int bpf_counter__install_pe(struct evsel *evsel, int cpu, int fd);
45 #else /* HAVE_BPF_SKEL */
47 #include <linux/err.h>
49 static inline int bpf_counter__load(struct evsel *evsel __maybe_unused,
50 struct target *target __maybe_unused)
55 static inline int bpf_counter__enable(struct evsel *evsel __maybe_unused)
60 static inline int bpf_counter__disable(struct evsel *evsel __maybe_unused)
65 static inline int bpf_counter__read(struct evsel *evsel __maybe_unused)
70 static inline void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
74 static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,
75 int cpu __maybe_unused,
76 int fd __maybe_unused)
81 #endif /* HAVE_BPF_SKEL */
83 static inline void set_max_rlimit(void)
85 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
87 setrlimit(RLIMIT_MEMLOCK, &rinf);
90 static inline __u32 bpf_link_get_id(int fd)
92 struct bpf_link_info link_info = { .id = 0, };
93 __u32 link_info_len = sizeof(link_info);
95 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
99 static inline __u32 bpf_link_get_prog_id(int fd)
101 struct bpf_link_info link_info = { .id = 0, };
102 __u32 link_info_len = sizeof(link_info);
104 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
105 return link_info.prog_id;
108 static inline __u32 bpf_map_get_id(int fd)
110 struct bpf_map_info map_info = { .id = 0, };
111 __u32 map_info_len = sizeof(map_info);
113 bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
117 /* trigger the leader program on a cpu */
118 static inline int bperf_trigger_reading(int prog_fd, int cpu)
120 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
123 .flags = BPF_F_TEST_RUN_ON_CPU,
128 return bpf_prog_test_run_opts(prog_fd, &opts);
131 #endif /* __PERF_BPF_COUNTER_H */