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>
8 #ifdef HAVE_LIBBPF_SUPPORT
11 #include <bpf/libbpf.h>
18 typedef int (*bpf_counter_evsel_op)(struct evsel *evsel);
19 typedef int (*bpf_counter_evsel_target_op)(struct evsel *evsel,
20 struct target *target);
21 typedef int (*bpf_counter_evsel_install_pe_op)(struct evsel *evsel,
25 struct bpf_counter_ops {
26 bpf_counter_evsel_target_op load;
27 bpf_counter_evsel_op enable;
28 bpf_counter_evsel_op disable;
29 bpf_counter_evsel_op read;
30 bpf_counter_evsel_op destroy;
31 bpf_counter_evsel_install_pe_op install_pe;
36 struct list_head list;
41 int bpf_counter__load(struct evsel *evsel, struct target *target);
42 int bpf_counter__enable(struct evsel *evsel);
43 int bpf_counter__disable(struct evsel *evsel);
44 int bpf_counter__read(struct evsel *evsel);
45 void bpf_counter__destroy(struct evsel *evsel);
46 int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd);
48 #else /* HAVE_BPF_SKEL */
50 #include <linux/err.h>
52 static inline int bpf_counter__load(struct evsel *evsel __maybe_unused,
53 struct target *target __maybe_unused)
58 static inline int bpf_counter__enable(struct evsel *evsel __maybe_unused)
63 static inline int bpf_counter__disable(struct evsel *evsel __maybe_unused)
68 static inline int bpf_counter__read(struct evsel *evsel __maybe_unused)
73 static inline void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
77 static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,
78 int cpu __maybe_unused,
79 int fd __maybe_unused)
84 #endif /* HAVE_BPF_SKEL */
86 static inline void set_max_rlimit(void)
88 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
90 setrlimit(RLIMIT_MEMLOCK, &rinf);
95 static inline __u32 bpf_link_get_id(int fd)
97 struct bpf_link_info link_info = { .id = 0, };
98 __u32 link_info_len = sizeof(link_info);
100 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
104 static inline __u32 bpf_link_get_prog_id(int fd)
106 struct bpf_link_info link_info = { .id = 0, };
107 __u32 link_info_len = sizeof(link_info);
109 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
110 return link_info.prog_id;
113 static inline __u32 bpf_map_get_id(int fd)
115 struct bpf_map_info map_info = { .id = 0, };
116 __u32 map_info_len = sizeof(map_info);
118 bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
122 /* trigger the leader program on a cpu */
123 static inline int bperf_trigger_reading(int prog_fd, int cpu)
125 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
128 .flags = BPF_F_TEST_RUN_ON_CPU,
133 return bpf_prog_test_run_opts(prog_fd, &opts);
135 #endif /* HAVE_BPF_SKEL */
137 #endif /* __PERF_BPF_COUNTER_H */