xref: /linux/tools/perf/util/bpf_counter.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1fa853c4bSSong Liu /* SPDX-License-Identifier: GPL-2.0 */
2fa853c4bSSong Liu #ifndef __PERF_BPF_COUNTER_H
3fa853c4bSSong Liu #define __PERF_BPF_COUNTER_H 1
4fa853c4bSSong Liu 
5fa853c4bSSong Liu #include <linux/list.h>
6d6a735efSNamhyung Kim #include <sys/resource.h>
7*d891f2b7SIan Rogers 
8*d891f2b7SIan Rogers #ifdef HAVE_LIBBPF_SUPPORT
9d6a735efSNamhyung Kim #include <bpf/bpf.h>
10d6a735efSNamhyung Kim #include <bpf/btf.h>
11d6a735efSNamhyung Kim #include <bpf/libbpf.h>
12*d891f2b7SIan Rogers #endif
13fa853c4bSSong Liu 
14fa853c4bSSong Liu struct evsel;
15fa853c4bSSong Liu struct target;
16fa853c4bSSong Liu struct bpf_counter;
17fa853c4bSSong Liu 
18fa853c4bSSong Liu typedef int (*bpf_counter_evsel_op)(struct evsel *evsel);
19fa853c4bSSong Liu typedef int (*bpf_counter_evsel_target_op)(struct evsel *evsel,
20fa853c4bSSong Liu 					   struct target *target);
21fa853c4bSSong Liu typedef int (*bpf_counter_evsel_install_pe_op)(struct evsel *evsel,
227263f349SIan Rogers 					       int cpu_map_idx,
23fa853c4bSSong Liu 					       int fd);
24fa853c4bSSong Liu 
25fa853c4bSSong Liu struct bpf_counter_ops {
26fa853c4bSSong Liu 	bpf_counter_evsel_target_op load;
27fa853c4bSSong Liu 	bpf_counter_evsel_op enable;
285508c9daSSong Liu 	bpf_counter_evsel_op disable;
29fa853c4bSSong Liu 	bpf_counter_evsel_op read;
30fa853c4bSSong Liu 	bpf_counter_evsel_op destroy;
31fa853c4bSSong Liu 	bpf_counter_evsel_install_pe_op install_pe;
32fa853c4bSSong Liu };
33fa853c4bSSong Liu 
34fa853c4bSSong Liu struct bpf_counter {
35fa853c4bSSong Liu 	void *skel;
36fa853c4bSSong Liu 	struct list_head list;
37fa853c4bSSong Liu };
38fa853c4bSSong Liu 
39fa853c4bSSong Liu #ifdef HAVE_BPF_SKEL
40fa853c4bSSong Liu 
41fa853c4bSSong Liu int bpf_counter__load(struct evsel *evsel, struct target *target);
42fa853c4bSSong Liu int bpf_counter__enable(struct evsel *evsel);
435508c9daSSong Liu int bpf_counter__disable(struct evsel *evsel);
44fa853c4bSSong Liu int bpf_counter__read(struct evsel *evsel);
45fa853c4bSSong Liu void bpf_counter__destroy(struct evsel *evsel);
467263f349SIan Rogers int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd);
47fa853c4bSSong Liu 
48fa853c4bSSong Liu #else /* HAVE_BPF_SKEL */
49fa853c4bSSong Liu 
50fa853c4bSSong Liu #include <linux/err.h>
51fa853c4bSSong Liu 
bpf_counter__load(struct evsel * evsel __maybe_unused,struct target * target __maybe_unused)52fa853c4bSSong Liu static inline int bpf_counter__load(struct evsel *evsel __maybe_unused,
53fa853c4bSSong Liu 				    struct target *target __maybe_unused)
54fa853c4bSSong Liu {
55fa853c4bSSong Liu 	return 0;
56fa853c4bSSong Liu }
57fa853c4bSSong Liu 
bpf_counter__enable(struct evsel * evsel __maybe_unused)58fa853c4bSSong Liu static inline int bpf_counter__enable(struct evsel *evsel __maybe_unused)
59fa853c4bSSong Liu {
60fa853c4bSSong Liu 	return 0;
61fa853c4bSSong Liu }
62fa853c4bSSong Liu 
bpf_counter__disable(struct evsel * evsel __maybe_unused)635508c9daSSong Liu static inline int bpf_counter__disable(struct evsel *evsel __maybe_unused)
645508c9daSSong Liu {
655508c9daSSong Liu 	return 0;
665508c9daSSong Liu }
675508c9daSSong Liu 
bpf_counter__read(struct evsel * evsel __maybe_unused)68fa853c4bSSong Liu static inline int bpf_counter__read(struct evsel *evsel __maybe_unused)
69fa853c4bSSong Liu {
70fa853c4bSSong Liu 	return -EAGAIN;
71fa853c4bSSong Liu }
72fa853c4bSSong Liu 
bpf_counter__destroy(struct evsel * evsel __maybe_unused)73fa853c4bSSong Liu static inline void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
74fa853c4bSSong Liu {
75fa853c4bSSong Liu }
76fa853c4bSSong Liu 
bpf_counter__install_pe(struct evsel * evsel __maybe_unused,int cpu __maybe_unused,int fd __maybe_unused)77fa853c4bSSong Liu static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,
78fa853c4bSSong Liu 					  int cpu __maybe_unused,
79fa853c4bSSong Liu 					  int fd __maybe_unused)
80fa853c4bSSong Liu {
81fa853c4bSSong Liu 	return 0;
82fa853c4bSSong Liu }
83fa853c4bSSong Liu 
84fa853c4bSSong Liu #endif /* HAVE_BPF_SKEL */
85fa853c4bSSong Liu 
set_max_rlimit(void)86d6a735efSNamhyung Kim static inline void set_max_rlimit(void)
87d6a735efSNamhyung Kim {
88d6a735efSNamhyung Kim 	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
89d6a735efSNamhyung Kim 
90d6a735efSNamhyung Kim 	setrlimit(RLIMIT_MEMLOCK, &rinf);
91d6a735efSNamhyung Kim }
92d6a735efSNamhyung Kim 
93*d891f2b7SIan Rogers #ifdef HAVE_BPF_SKEL
94*d891f2b7SIan Rogers 
bpf_link_get_id(int fd)95d6a735efSNamhyung Kim static inline __u32 bpf_link_get_id(int fd)
96d6a735efSNamhyung Kim {
97d6a735efSNamhyung Kim 	struct bpf_link_info link_info = { .id = 0, };
98d6a735efSNamhyung Kim 	__u32 link_info_len = sizeof(link_info);
99d6a735efSNamhyung Kim 
100d6a735efSNamhyung Kim 	bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
101d6a735efSNamhyung Kim 	return link_info.id;
102d6a735efSNamhyung Kim }
103d6a735efSNamhyung Kim 
bpf_link_get_prog_id(int fd)104d6a735efSNamhyung Kim static inline __u32 bpf_link_get_prog_id(int fd)
105d6a735efSNamhyung Kim {
106d6a735efSNamhyung Kim 	struct bpf_link_info link_info = { .id = 0, };
107d6a735efSNamhyung Kim 	__u32 link_info_len = sizeof(link_info);
108d6a735efSNamhyung Kim 
109d6a735efSNamhyung Kim 	bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
110d6a735efSNamhyung Kim 	return link_info.prog_id;
111d6a735efSNamhyung Kim }
112d6a735efSNamhyung Kim 
bpf_map_get_id(int fd)113d6a735efSNamhyung Kim static inline __u32 bpf_map_get_id(int fd)
114d6a735efSNamhyung Kim {
115d6a735efSNamhyung Kim 	struct bpf_map_info map_info = { .id = 0, };
116d6a735efSNamhyung Kim 	__u32 map_info_len = sizeof(map_info);
117d6a735efSNamhyung Kim 
118d6a735efSNamhyung Kim 	bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
119d6a735efSNamhyung Kim 	return map_info.id;
120d6a735efSNamhyung Kim }
121d6a735efSNamhyung Kim 
122d6a735efSNamhyung Kim /* trigger the leader program on a cpu */
bperf_trigger_reading(int prog_fd,int cpu)123d6a735efSNamhyung Kim static inline int bperf_trigger_reading(int prog_fd, int cpu)
124d6a735efSNamhyung Kim {
125d6a735efSNamhyung Kim 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
126d6a735efSNamhyung Kim 			    .ctx_in = NULL,
127d6a735efSNamhyung Kim 			    .ctx_size_in = 0,
128d6a735efSNamhyung Kim 			    .flags = BPF_F_TEST_RUN_ON_CPU,
129d6a735efSNamhyung Kim 			    .cpu = cpu,
130d6a735efSNamhyung Kim 			    .retval = 0,
131d6a735efSNamhyung Kim 		);
132d6a735efSNamhyung Kim 
133d6a735efSNamhyung Kim 	return bpf_prog_test_run_opts(prog_fd, &opts);
134d6a735efSNamhyung Kim }
135*d891f2b7SIan Rogers #endif /* HAVE_BPF_SKEL */
136d6a735efSNamhyung Kim 
137fa853c4bSSong Liu #endif /* __PERF_BPF_COUNTER_H */
138