blob: 65ebaa6694fbd2c15778713940e18eddc6280dc6 [file] [log] [blame]
Song Liufa853c42020-12-29 13:42:14 -08001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __PERF_BPF_COUNTER_H
3#define __PERF_BPF_COUNTER_H 1
4
5#include <linux/list.h>
Namhyung Kimd6a735e2021-06-25 00:18:25 -07006#include <sys/resource.h>
7#include <bpf/bpf.h>
8#include <bpf/btf.h>
9#include <bpf/libbpf.h>
Song Liufa853c42020-12-29 13:42:14 -080010
11struct evsel;
12struct target;
13struct bpf_counter;
14
15typedef int (*bpf_counter_evsel_op)(struct evsel *evsel);
16typedef int (*bpf_counter_evsel_target_op)(struct evsel *evsel,
17 struct target *target);
18typedef int (*bpf_counter_evsel_install_pe_op)(struct evsel *evsel,
19 int cpu,
20 int fd);
21
22struct bpf_counter_ops {
23 bpf_counter_evsel_target_op load;
24 bpf_counter_evsel_op enable;
Song Liu5508c9d2021-04-25 14:43:33 -070025 bpf_counter_evsel_op disable;
Song Liufa853c42020-12-29 13:42:14 -080026 bpf_counter_evsel_op read;
27 bpf_counter_evsel_op destroy;
28 bpf_counter_evsel_install_pe_op install_pe;
29};
30
31struct bpf_counter {
32 void *skel;
33 struct list_head list;
34};
35
36#ifdef HAVE_BPF_SKEL
37
38int bpf_counter__load(struct evsel *evsel, struct target *target);
39int bpf_counter__enable(struct evsel *evsel);
Song Liu5508c9d2021-04-25 14:43:33 -070040int bpf_counter__disable(struct evsel *evsel);
Song Liufa853c42020-12-29 13:42:14 -080041int bpf_counter__read(struct evsel *evsel);
42void bpf_counter__destroy(struct evsel *evsel);
43int bpf_counter__install_pe(struct evsel *evsel, int cpu, int fd);
44
45#else /* HAVE_BPF_SKEL */
46
Ian Rogers509bbd72021-03-06 00:08:40 -080047#include <linux/err.h>
Song Liufa853c42020-12-29 13:42:14 -080048
49static inline int bpf_counter__load(struct evsel *evsel __maybe_unused,
50 struct target *target __maybe_unused)
51{
52 return 0;
53}
54
55static inline int bpf_counter__enable(struct evsel *evsel __maybe_unused)
56{
57 return 0;
58}
59
Song Liu5508c9d2021-04-25 14:43:33 -070060static inline int bpf_counter__disable(struct evsel *evsel __maybe_unused)
61{
62 return 0;
63}
64
Song Liufa853c42020-12-29 13:42:14 -080065static inline int bpf_counter__read(struct evsel *evsel __maybe_unused)
66{
67 return -EAGAIN;
68}
69
70static inline void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
71{
72}
73
74static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,
75 int cpu __maybe_unused,
76 int fd __maybe_unused)
77{
78 return 0;
79}
80
81#endif /* HAVE_BPF_SKEL */
82
Namhyung Kimd6a735e2021-06-25 00:18:25 -070083static inline void set_max_rlimit(void)
84{
85 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
86
87 setrlimit(RLIMIT_MEMLOCK, &rinf);
88}
89
90static inline __u32 bpf_link_get_id(int fd)
91{
92 struct bpf_link_info link_info = { .id = 0, };
93 __u32 link_info_len = sizeof(link_info);
94
95 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
96 return link_info.id;
97}
98
99static inline __u32 bpf_link_get_prog_id(int fd)
100{
101 struct bpf_link_info link_info = { .id = 0, };
102 __u32 link_info_len = sizeof(link_info);
103
104 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
105 return link_info.prog_id;
106}
107
108static inline __u32 bpf_map_get_id(int fd)
109{
110 struct bpf_map_info map_info = { .id = 0, };
111 __u32 map_info_len = sizeof(map_info);
112
113 bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
114 return map_info.id;
115}
116
117/* trigger the leader program on a cpu */
118static inline int bperf_trigger_reading(int prog_fd, int cpu)
119{
120 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
121 .ctx_in = NULL,
122 .ctx_size_in = 0,
123 .flags = BPF_F_TEST_RUN_ON_CPU,
124 .cpu = cpu,
125 .retval = 0,
126 );
127
128 return bpf_prog_test_run_opts(prog_fd, &opts);
129}
130
Song Liufa853c42020-12-29 13:42:14 -0800131#endif /* __PERF_BPF_COUNTER_H */