1fa853c4bSSong Liu // SPDX-License-Identifier: GPL-2.0
2fa853c4bSSong Liu
3fa853c4bSSong Liu /* Copyright (c) 2019 Facebook */
4fa853c4bSSong Liu
5fa853c4bSSong Liu #include <assert.h>
6fa853c4bSSong Liu #include <limits.h>
7fa853c4bSSong Liu #include <unistd.h>
87fac83aaSSong Liu #include <sys/file.h>
9fa853c4bSSong Liu #include <sys/time.h>
10fa853c4bSSong Liu #include <linux/err.h>
11fa853c4bSSong Liu #include <linux/zalloc.h>
127fac83aaSSong Liu #include <api/fs/fs.h>
13ec8149fbSSong Liu #include <perf/bpf_perf.h>
14fa853c4bSSong Liu
15fa853c4bSSong Liu #include "bpf_counter.h"
166ac22d03SDave Marchevsky #include "bpf-utils.h"
17fa853c4bSSong Liu #include "counts.h"
18fa853c4bSSong Liu #include "debug.h"
19fa853c4bSSong Liu #include "evsel.h"
207fac83aaSSong Liu #include "evlist.h"
21fa853c4bSSong Liu #include "target.h"
22944138f0SNamhyung Kim #include "cgroup.h"
237fac83aaSSong Liu #include "cpumap.h"
247fac83aaSSong Liu #include "thread_map.h"
25fa853c4bSSong Liu
26fa853c4bSSong Liu #include "bpf_skel/bpf_prog_profiler.skel.h"
277fac83aaSSong Liu #include "bpf_skel/bperf_u.h"
287fac83aaSSong Liu #include "bpf_skel/bperf_leader.skel.h"
297fac83aaSSong Liu #include "bpf_skel/bperf_follower.skel.h"
307fac83aaSSong Liu
317fac83aaSSong Liu #define ATTR_MAP_SIZE 16
32fa853c4bSSong Liu
u64_to_ptr(__u64 ptr)33fa853c4bSSong Liu static inline void *u64_to_ptr(__u64 ptr)
34fa853c4bSSong Liu {
35fa853c4bSSong Liu return (void *)(unsigned long)ptr;
36fa853c4bSSong Liu }
37fa853c4bSSong Liu
bpf_counter_alloc(void)38fa853c4bSSong Liu static struct bpf_counter *bpf_counter_alloc(void)
39fa853c4bSSong Liu {
40fa853c4bSSong Liu struct bpf_counter *counter;
41fa853c4bSSong Liu
42fa853c4bSSong Liu counter = zalloc(sizeof(*counter));
43fa853c4bSSong Liu if (counter)
44fa853c4bSSong Liu INIT_LIST_HEAD(&counter->list);
45fa853c4bSSong Liu return counter;
46fa853c4bSSong Liu }
47fa853c4bSSong Liu
bpf_program_profiler__destroy(struct evsel * evsel)48fa853c4bSSong Liu static int bpf_program_profiler__destroy(struct evsel *evsel)
49fa853c4bSSong Liu {
50fa853c4bSSong Liu struct bpf_counter *counter, *tmp;
51fa853c4bSSong Liu
52fa853c4bSSong Liu list_for_each_entry_safe(counter, tmp,
53fa853c4bSSong Liu &evsel->bpf_counter_list, list) {
54fa853c4bSSong Liu list_del_init(&counter->list);
55fa853c4bSSong Liu bpf_prog_profiler_bpf__destroy(counter->skel);
56fa853c4bSSong Liu free(counter);
57fa853c4bSSong Liu }
58fa853c4bSSong Liu assert(list_empty(&evsel->bpf_counter_list));
59fa853c4bSSong Liu
60fa853c4bSSong Liu return 0;
61fa853c4bSSong Liu }
62fa853c4bSSong Liu
bpf_target_prog_name(int tgt_fd)63fa853c4bSSong Liu static char *bpf_target_prog_name(int tgt_fd)
64fa853c4bSSong Liu {
65fa853c4bSSong Liu struct bpf_func_info *func_info;
666ac22d03SDave Marchevsky struct perf_bpil *info_linear;
67fa853c4bSSong Liu const struct btf_type *t;
68369e955bSQuentin Monnet struct btf *btf = NULL;
69fa853c4bSSong Liu char *name = NULL;
70fa853c4bSSong Liu
716ac22d03SDave Marchevsky info_linear = get_bpf_prog_info_linear(tgt_fd, 1UL << PERF_BPIL_FUNC_INFO);
72fa853c4bSSong Liu if (IS_ERR_OR_NULL(info_linear)) {
73fa853c4bSSong Liu pr_debug("failed to get info_linear for prog FD %d\n", tgt_fd);
74fa853c4bSSong Liu return NULL;
75fa853c4bSSong Liu }
76fa853c4bSSong Liu
7786f4b7f2SQuentin Monnet if (info_linear->info.btf_id == 0) {
78fa853c4bSSong Liu pr_debug("prog FD %d doesn't have valid btf\n", tgt_fd);
79fa853c4bSSong Liu goto out;
80fa853c4bSSong Liu }
81fa853c4bSSong Liu
8286f4b7f2SQuentin Monnet btf = btf__load_from_kernel_by_id(info_linear->info.btf_id);
8386f4b7f2SQuentin Monnet if (libbpf_get_error(btf)) {
8486f4b7f2SQuentin Monnet pr_debug("failed to load btf for prog FD %d\n", tgt_fd);
8586f4b7f2SQuentin Monnet goto out;
8686f4b7f2SQuentin Monnet }
8786f4b7f2SQuentin Monnet
88fa853c4bSSong Liu func_info = u64_to_ptr(info_linear->info.func_info);
89fa853c4bSSong Liu t = btf__type_by_id(btf, func_info[0].type_id);
90fa853c4bSSong Liu if (!t) {
91fa853c4bSSong Liu pr_debug("btf %d doesn't have type %d\n",
92fa853c4bSSong Liu info_linear->info.btf_id, func_info[0].type_id);
93fa853c4bSSong Liu goto out;
94fa853c4bSSong Liu }
95fa853c4bSSong Liu name = strdup(btf__name_by_offset(btf, t->name_off));
96fa853c4bSSong Liu out:
97369e955bSQuentin Monnet btf__free(btf);
98fa853c4bSSong Liu free(info_linear);
99fa853c4bSSong Liu return name;
100fa853c4bSSong Liu }
101fa853c4bSSong Liu
bpf_program_profiler_load_one(struct evsel * evsel,u32 prog_id)102fa853c4bSSong Liu static int bpf_program_profiler_load_one(struct evsel *evsel, u32 prog_id)
103fa853c4bSSong Liu {
104fa853c4bSSong Liu struct bpf_prog_profiler_bpf *skel;
105fa853c4bSSong Liu struct bpf_counter *counter;
106fa853c4bSSong Liu struct bpf_program *prog;
107fa853c4bSSong Liu char *prog_name;
108fa853c4bSSong Liu int prog_fd;
109fa853c4bSSong Liu int err;
110fa853c4bSSong Liu
111fa853c4bSSong Liu prog_fd = bpf_prog_get_fd_by_id(prog_id);
112fa853c4bSSong Liu if (prog_fd < 0) {
113fa853c4bSSong Liu pr_err("Failed to open fd for bpf prog %u\n", prog_id);
114fa853c4bSSong Liu return -1;
115fa853c4bSSong Liu }
116fa853c4bSSong Liu counter = bpf_counter_alloc();
117fa853c4bSSong Liu if (!counter) {
118fa853c4bSSong Liu close(prog_fd);
119fa853c4bSSong Liu return -1;
120fa853c4bSSong Liu }
121fa853c4bSSong Liu
122fa853c4bSSong Liu skel = bpf_prog_profiler_bpf__open();
123fa853c4bSSong Liu if (!skel) {
124fa853c4bSSong Liu pr_err("Failed to open bpf skeleton\n");
125fa853c4bSSong Liu goto err_out;
126fa853c4bSSong Liu }
127fa853c4bSSong Liu
128fa853c4bSSong Liu skel->rodata->num_cpu = evsel__nr_cpus(evsel);
129fa853c4bSSong Liu
130ddf0d4deSMuhammad Falak R Wani bpf_map__set_max_entries(skel->maps.events, evsel__nr_cpus(evsel));
131ddf0d4deSMuhammad Falak R Wani bpf_map__set_max_entries(skel->maps.fentry_readings, 1);
132ddf0d4deSMuhammad Falak R Wani bpf_map__set_max_entries(skel->maps.accum_readings, 1);
133fa853c4bSSong Liu
134fa853c4bSSong Liu prog_name = bpf_target_prog_name(prog_fd);
135fa853c4bSSong Liu if (!prog_name) {
136fa853c4bSSong Liu pr_err("Failed to get program name for bpf prog %u. Does it have BTF?\n", prog_id);
137fa853c4bSSong Liu goto err_out;
138fa853c4bSSong Liu }
139fa853c4bSSong Liu
140fa853c4bSSong Liu bpf_object__for_each_program(prog, skel->obj) {
141fa853c4bSSong Liu err = bpf_program__set_attach_target(prog, prog_fd, prog_name);
142fa853c4bSSong Liu if (err) {
143fa853c4bSSong Liu pr_err("bpf_program__set_attach_target failed.\n"
144fa853c4bSSong Liu "Does bpf prog %u have BTF?\n", prog_id);
145fa853c4bSSong Liu goto err_out;
146fa853c4bSSong Liu }
147fa853c4bSSong Liu }
148fa853c4bSSong Liu set_max_rlimit();
149fa853c4bSSong Liu err = bpf_prog_profiler_bpf__load(skel);
150fa853c4bSSong Liu if (err) {
151fa853c4bSSong Liu pr_err("bpf_prog_profiler_bpf__load failed\n");
152fa853c4bSSong Liu goto err_out;
153fa853c4bSSong Liu }
154fa853c4bSSong Liu
155fa853c4bSSong Liu assert(skel != NULL);
156fa853c4bSSong Liu counter->skel = skel;
157fa853c4bSSong Liu list_add(&counter->list, &evsel->bpf_counter_list);
158fa853c4bSSong Liu close(prog_fd);
159fa853c4bSSong Liu return 0;
160fa853c4bSSong Liu err_out:
161fa853c4bSSong Liu bpf_prog_profiler_bpf__destroy(skel);
162fa853c4bSSong Liu free(counter);
163fa853c4bSSong Liu close(prog_fd);
164fa853c4bSSong Liu return -1;
165fa853c4bSSong Liu }
166fa853c4bSSong Liu
bpf_program_profiler__load(struct evsel * evsel,struct target * target)167fa853c4bSSong Liu static int bpf_program_profiler__load(struct evsel *evsel, struct target *target)
168fa853c4bSSong Liu {
169fa853c4bSSong Liu char *bpf_str, *bpf_str_, *tok, *saveptr = NULL, *p;
170fa853c4bSSong Liu u32 prog_id;
171fa853c4bSSong Liu int ret;
172fa853c4bSSong Liu
173fa853c4bSSong Liu bpf_str_ = bpf_str = strdup(target->bpf_str);
174fa853c4bSSong Liu if (!bpf_str)
175fa853c4bSSong Liu return -1;
176fa853c4bSSong Liu
177fa853c4bSSong Liu while ((tok = strtok_r(bpf_str, ",", &saveptr)) != NULL) {
178fa853c4bSSong Liu prog_id = strtoul(tok, &p, 10);
179fa853c4bSSong Liu if (prog_id == 0 || prog_id == UINT_MAX ||
180fa853c4bSSong Liu (*p != '\0' && *p != ',')) {
181fa853c4bSSong Liu pr_err("Failed to parse bpf prog ids %s\n",
182fa853c4bSSong Liu target->bpf_str);
183fa853c4bSSong Liu return -1;
184fa853c4bSSong Liu }
185fa853c4bSSong Liu
186fa853c4bSSong Liu ret = bpf_program_profiler_load_one(evsel, prog_id);
187fa853c4bSSong Liu if (ret) {
188fa853c4bSSong Liu bpf_program_profiler__destroy(evsel);
189fa853c4bSSong Liu free(bpf_str_);
190fa853c4bSSong Liu return -1;
191fa853c4bSSong Liu }
192fa853c4bSSong Liu bpf_str = NULL;
193fa853c4bSSong Liu }
194fa853c4bSSong Liu free(bpf_str_);
195fa853c4bSSong Liu return 0;
196fa853c4bSSong Liu }
197fa853c4bSSong Liu
bpf_program_profiler__enable(struct evsel * evsel)198fa853c4bSSong Liu static int bpf_program_profiler__enable(struct evsel *evsel)
199fa853c4bSSong Liu {
200fa853c4bSSong Liu struct bpf_counter *counter;
201fa853c4bSSong Liu int ret;
202fa853c4bSSong Liu
203fa853c4bSSong Liu list_for_each_entry(counter, &evsel->bpf_counter_list, list) {
204fa853c4bSSong Liu assert(counter->skel != NULL);
205fa853c4bSSong Liu ret = bpf_prog_profiler_bpf__attach(counter->skel);
206fa853c4bSSong Liu if (ret) {
207fa853c4bSSong Liu bpf_program_profiler__destroy(evsel);
208fa853c4bSSong Liu return ret;
209fa853c4bSSong Liu }
210fa853c4bSSong Liu }
211fa853c4bSSong Liu return 0;
212fa853c4bSSong Liu }
213fa853c4bSSong Liu
bpf_program_profiler__disable(struct evsel * evsel)2145508c9daSSong Liu static int bpf_program_profiler__disable(struct evsel *evsel)
2155508c9daSSong Liu {
2165508c9daSSong Liu struct bpf_counter *counter;
2175508c9daSSong Liu
2185508c9daSSong Liu list_for_each_entry(counter, &evsel->bpf_counter_list, list) {
2195508c9daSSong Liu assert(counter->skel != NULL);
2205508c9daSSong Liu bpf_prog_profiler_bpf__detach(counter->skel);
2215508c9daSSong Liu }
2225508c9daSSong Liu return 0;
2235508c9daSSong Liu }
2245508c9daSSong Liu
bpf_program_profiler__read(struct evsel * evsel)225fa853c4bSSong Liu static int bpf_program_profiler__read(struct evsel *evsel)
226fa853c4bSSong Liu {
227fa853c4bSSong Liu // BPF_MAP_TYPE_PERCPU_ARRAY uses /sys/devices/system/cpu/possible
228fa853c4bSSong Liu // Sometimes possible > online, like on a Ryzen 3900X that has 24
229fa853c4bSSong Liu // threads but its possible showed 0-31 -acme
230fa853c4bSSong Liu int num_cpu_bpf = libbpf_num_possible_cpus();
231fa853c4bSSong Liu struct bpf_perf_event_value values[num_cpu_bpf];
232fa853c4bSSong Liu struct bpf_counter *counter;
23354668a4eSIan Rogers struct perf_counts_values *counts;
234fa853c4bSSong Liu int reading_map_fd;
235fa853c4bSSong Liu __u32 key = 0;
23654668a4eSIan Rogers int err, idx, bpf_cpu;
237fa853c4bSSong Liu
238fa853c4bSSong Liu if (list_empty(&evsel->bpf_counter_list))
239fa853c4bSSong Liu return -EAGAIN;
240fa853c4bSSong Liu
24154668a4eSIan Rogers perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) {
24254668a4eSIan Rogers counts = perf_counts(evsel->counts, idx, 0);
24354668a4eSIan Rogers counts->val = 0;
24454668a4eSIan Rogers counts->ena = 0;
24554668a4eSIan Rogers counts->run = 0;
246fa853c4bSSong Liu }
247fa853c4bSSong Liu list_for_each_entry(counter, &evsel->bpf_counter_list, list) {
248fa853c4bSSong Liu struct bpf_prog_profiler_bpf *skel = counter->skel;
249fa853c4bSSong Liu
250fa853c4bSSong Liu assert(skel != NULL);
251fa853c4bSSong Liu reading_map_fd = bpf_map__fd(skel->maps.accum_readings);
252fa853c4bSSong Liu
253fa853c4bSSong Liu err = bpf_map_lookup_elem(reading_map_fd, &key, values);
254fa853c4bSSong Liu if (err) {
255fa853c4bSSong Liu pr_err("failed to read value\n");
256fa853c4bSSong Liu return err;
257fa853c4bSSong Liu }
258fa853c4bSSong Liu
25954668a4eSIan Rogers for (bpf_cpu = 0; bpf_cpu < num_cpu_bpf; bpf_cpu++) {
26054668a4eSIan Rogers idx = perf_cpu_map__idx(evsel__cpus(evsel),
26154668a4eSIan Rogers (struct perf_cpu){.cpu = bpf_cpu});
26254668a4eSIan Rogers if (idx == -1)
26354668a4eSIan Rogers continue;
26454668a4eSIan Rogers counts = perf_counts(evsel->counts, idx, 0);
26554668a4eSIan Rogers counts->val += values[bpf_cpu].counter;
26654668a4eSIan Rogers counts->ena += values[bpf_cpu].enabled;
26754668a4eSIan Rogers counts->run += values[bpf_cpu].running;
268fa853c4bSSong Liu }
269fa853c4bSSong Liu }
270fa853c4bSSong Liu return 0;
271fa853c4bSSong Liu }
272fa853c4bSSong Liu
bpf_program_profiler__install_pe(struct evsel * evsel,int cpu_map_idx,int fd)2737263f349SIan Rogers static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu_map_idx,
274fa853c4bSSong Liu int fd)
275fa853c4bSSong Liu {
276fa853c4bSSong Liu struct bpf_prog_profiler_bpf *skel;
277fa853c4bSSong Liu struct bpf_counter *counter;
278fa853c4bSSong Liu int ret;
279fa853c4bSSong Liu
280fa853c4bSSong Liu list_for_each_entry(counter, &evsel->bpf_counter_list, list) {
281fa853c4bSSong Liu skel = counter->skel;
282fa853c4bSSong Liu assert(skel != NULL);
283fa853c4bSSong Liu
284fa853c4bSSong Liu ret = bpf_map_update_elem(bpf_map__fd(skel->maps.events),
2857263f349SIan Rogers &cpu_map_idx, &fd, BPF_ANY);
286fa853c4bSSong Liu if (ret)
287fa853c4bSSong Liu return ret;
288fa853c4bSSong Liu }
289fa853c4bSSong Liu return 0;
290fa853c4bSSong Liu }
291fa853c4bSSong Liu
292fa853c4bSSong Liu struct bpf_counter_ops bpf_program_profiler_ops = {
293fa853c4bSSong Liu .load = bpf_program_profiler__load,
294fa853c4bSSong Liu .enable = bpf_program_profiler__enable,
2955508c9daSSong Liu .disable = bpf_program_profiler__disable,
296fa853c4bSSong Liu .read = bpf_program_profiler__read,
297fa853c4bSSong Liu .destroy = bpf_program_profiler__destroy,
298fa853c4bSSong Liu .install_pe = bpf_program_profiler__install_pe,
299fa853c4bSSong Liu };
300fa853c4bSSong Liu
bperf_attr_map_compatible(int attr_map_fd)301fe3dd826SSong Liu static bool bperf_attr_map_compatible(int attr_map_fd)
302fe3dd826SSong Liu {
303fe3dd826SSong Liu struct bpf_map_info map_info = {0};
304fe3dd826SSong Liu __u32 map_info_len = sizeof(map_info);
305fe3dd826SSong Liu int err;
306fe3dd826SSong Liu
307fe3dd826SSong Liu err = bpf_obj_get_info_by_fd(attr_map_fd, &map_info, &map_info_len);
308fe3dd826SSong Liu
309fe3dd826SSong Liu if (err)
310fe3dd826SSong Liu return false;
311fe3dd826SSong Liu return (map_info.key_size == sizeof(struct perf_event_attr)) &&
312fe3dd826SSong Liu (map_info.value_size == sizeof(struct perf_event_attr_map_entry));
313fe3dd826SSong Liu }
314fe3dd826SSong Liu
bperf_lock_attr_map(struct target * target)3157fac83aaSSong Liu static int bperf_lock_attr_map(struct target *target)
3167fac83aaSSong Liu {
3177fac83aaSSong Liu char path[PATH_MAX];
3187fac83aaSSong Liu int map_fd, err;
3197fac83aaSSong Liu
3207fac83aaSSong Liu if (target->attr_map) {
3217fac83aaSSong Liu scnprintf(path, PATH_MAX, "%s", target->attr_map);
3227fac83aaSSong Liu } else {
323ec8149fbSSong Liu scnprintf(path, PATH_MAX, "%s/fs/bpf/%s", sysfs__mountpoint(),
324ec8149fbSSong Liu BPF_PERF_DEFAULT_ATTR_MAP_PATH);
3257fac83aaSSong Liu }
3267fac83aaSSong Liu
3277fac83aaSSong Liu if (access(path, F_OK)) {
3288d0f9e73SSong Liu map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
3297fac83aaSSong Liu sizeof(struct perf_event_attr),
3307fac83aaSSong Liu sizeof(struct perf_event_attr_map_entry),
3318d0f9e73SSong Liu ATTR_MAP_SIZE, NULL);
3327fac83aaSSong Liu if (map_fd < 0)
3337fac83aaSSong Liu return -1;
3347fac83aaSSong Liu
3357fac83aaSSong Liu err = bpf_obj_pin(map_fd, path);
3367fac83aaSSong Liu if (err) {
3377fac83aaSSong Liu /* someone pinned the map in parallel? */
3387fac83aaSSong Liu close(map_fd);
3397fac83aaSSong Liu map_fd = bpf_obj_get(path);
3407fac83aaSSong Liu if (map_fd < 0)
3417fac83aaSSong Liu return -1;
3427fac83aaSSong Liu }
3437fac83aaSSong Liu } else {
3447fac83aaSSong Liu map_fd = bpf_obj_get(path);
3457fac83aaSSong Liu if (map_fd < 0)
3467fac83aaSSong Liu return -1;
3477fac83aaSSong Liu }
3487fac83aaSSong Liu
349fe3dd826SSong Liu if (!bperf_attr_map_compatible(map_fd)) {
350fe3dd826SSong Liu close(map_fd);
351fe3dd826SSong Liu return -1;
352fe3dd826SSong Liu
353fe3dd826SSong Liu }
3547fac83aaSSong Liu err = flock(map_fd, LOCK_EX);
3557fac83aaSSong Liu if (err) {
3567fac83aaSSong Liu close(map_fd);
3577fac83aaSSong Liu return -1;
3587fac83aaSSong Liu }
3597fac83aaSSong Liu return map_fd;
3607fac83aaSSong Liu }
3617fac83aaSSong Liu
bperf_check_target(struct evsel * evsel,struct target * target,enum bperf_filter_type * filter_type,__u32 * filter_entry_cnt)3627fac83aaSSong Liu static int bperf_check_target(struct evsel *evsel,
3637fac83aaSSong Liu struct target *target,
3647fac83aaSSong Liu enum bperf_filter_type *filter_type,
3657fac83aaSSong Liu __u32 *filter_entry_cnt)
3667fac83aaSSong Liu {
367fba7c866SJiri Olsa if (evsel->core.leader->nr_members > 1) {
3687fac83aaSSong Liu pr_err("bpf managed perf events do not yet support groups.\n");
3697fac83aaSSong Liu return -1;
3707fac83aaSSong Liu }
3717fac83aaSSong Liu
3727fac83aaSSong Liu /* determine filter type based on target */
3737fac83aaSSong Liu if (target->system_wide) {
3747fac83aaSSong Liu *filter_type = BPERF_FILTER_GLOBAL;
3757fac83aaSSong Liu *filter_entry_cnt = 1;
3767fac83aaSSong Liu } else if (target->cpu_list) {
3777fac83aaSSong Liu *filter_type = BPERF_FILTER_CPU;
3787fac83aaSSong Liu *filter_entry_cnt = perf_cpu_map__nr(evsel__cpus(evsel));
3797fac83aaSSong Liu } else if (target->tid) {
3807fac83aaSSong Liu *filter_type = BPERF_FILTER_PID;
3817fac83aaSSong Liu *filter_entry_cnt = perf_thread_map__nr(evsel->core.threads);
3827fac83aaSSong Liu } else if (target->pid || evsel->evlist->workload.pid != -1) {
3837fac83aaSSong Liu *filter_type = BPERF_FILTER_TGID;
3847fac83aaSSong Liu *filter_entry_cnt = perf_thread_map__nr(evsel->core.threads);
3857fac83aaSSong Liu } else {
3867fac83aaSSong Liu pr_err("bpf managed perf events do not yet support these targets.\n");
3877fac83aaSSong Liu return -1;
3887fac83aaSSong Liu }
3897fac83aaSSong Liu
3907fac83aaSSong Liu return 0;
3917fac83aaSSong Liu }
3927fac83aaSSong Liu
3937fac83aaSSong Liu static struct perf_cpu_map *all_cpu_map;
3947fac83aaSSong Liu
bperf_reload_leader_program(struct evsel * evsel,int attr_map_fd,struct perf_event_attr_map_entry * entry)3957fac83aaSSong Liu static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
3967fac83aaSSong Liu struct perf_event_attr_map_entry *entry)
3977fac83aaSSong Liu {
3987fac83aaSSong Liu struct bperf_leader_bpf *skel = bperf_leader_bpf__open();
3997fac83aaSSong Liu int link_fd, diff_map_fd, err;
4007fac83aaSSong Liu struct bpf_link *link = NULL;
4017fac83aaSSong Liu
4027fac83aaSSong Liu if (!skel) {
4037fac83aaSSong Liu pr_err("Failed to open leader skeleton\n");
4047fac83aaSSong Liu return -1;
4057fac83aaSSong Liu }
4067fac83aaSSong Liu
407ddf0d4deSMuhammad Falak R Wani bpf_map__set_max_entries(skel->maps.events, libbpf_num_possible_cpus());
4087fac83aaSSong Liu err = bperf_leader_bpf__load(skel);
4097fac83aaSSong Liu if (err) {
4107fac83aaSSong Liu pr_err("Failed to load leader skeleton\n");
4117fac83aaSSong Liu goto out;
4127fac83aaSSong Liu }
4137fac83aaSSong Liu
4147fac83aaSSong Liu link = bpf_program__attach(skel->progs.on_switch);
415c673b7f5SNamhyung Kim if (IS_ERR(link)) {
4167fac83aaSSong Liu pr_err("Failed to attach leader program\n");
417c673b7f5SNamhyung Kim err = PTR_ERR(link);
4187fac83aaSSong Liu goto out;
4197fac83aaSSong Liu }
4207fac83aaSSong Liu
4217fac83aaSSong Liu link_fd = bpf_link__fd(link);
4227fac83aaSSong Liu diff_map_fd = bpf_map__fd(skel->maps.diff_readings);
4237fac83aaSSong Liu entry->link_id = bpf_link_get_id(link_fd);
4247fac83aaSSong Liu entry->diff_map_id = bpf_map_get_id(diff_map_fd);
4257fac83aaSSong Liu err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, entry, BPF_ANY);
4267fac83aaSSong Liu assert(err == 0);
4277fac83aaSSong Liu
4287fac83aaSSong Liu evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry->link_id);
4297fac83aaSSong Liu assert(evsel->bperf_leader_link_fd >= 0);
4307fac83aaSSong Liu
4317fac83aaSSong Liu /*
4327fac83aaSSong Liu * save leader_skel for install_pe, which is called within
4337fac83aaSSong Liu * following evsel__open_per_cpu call
4347fac83aaSSong Liu */
4357fac83aaSSong Liu evsel->leader_skel = skel;
4367fac83aaSSong Liu evsel__open_per_cpu(evsel, all_cpu_map, -1);
4377fac83aaSSong Liu
4387fac83aaSSong Liu out:
4397fac83aaSSong Liu bperf_leader_bpf__destroy(skel);
4407fac83aaSSong Liu bpf_link__destroy(link);
4417fac83aaSSong Liu return err;
4427fac83aaSSong Liu }
4437fac83aaSSong Liu
bperf__load(struct evsel * evsel,struct target * target)4447fac83aaSSong Liu static int bperf__load(struct evsel *evsel, struct target *target)
4457fac83aaSSong Liu {
4467fac83aaSSong Liu struct perf_event_attr_map_entry entry = {0xffffffff, 0xffffffff};
4477fac83aaSSong Liu int attr_map_fd, diff_map_fd = -1, err;
4487fac83aaSSong Liu enum bperf_filter_type filter_type;
4497fac83aaSSong Liu __u32 filter_entry_cnt, i;
4507fac83aaSSong Liu
4517fac83aaSSong Liu if (bperf_check_target(evsel, target, &filter_type, &filter_entry_cnt))
4527fac83aaSSong Liu return -1;
4537fac83aaSSong Liu
4547fac83aaSSong Liu if (!all_cpu_map) {
4557fac83aaSSong Liu all_cpu_map = perf_cpu_map__new(NULL);
4567fac83aaSSong Liu if (!all_cpu_map)
4577fac83aaSSong Liu return -1;
4587fac83aaSSong Liu }
4597fac83aaSSong Liu
4607fac83aaSSong Liu evsel->bperf_leader_prog_fd = -1;
4617fac83aaSSong Liu evsel->bperf_leader_link_fd = -1;
4627fac83aaSSong Liu
4637fac83aaSSong Liu /*
4647fac83aaSSong Liu * Step 1: hold a fd on the leader program and the bpf_link, if
4657fac83aaSSong Liu * the program is not already gone, reload the program.
4667fac83aaSSong Liu * Use flock() to ensure exclusive access to the perf_event_attr
4677fac83aaSSong Liu * map.
4687fac83aaSSong Liu */
4697fac83aaSSong Liu attr_map_fd = bperf_lock_attr_map(target);
4707fac83aaSSong Liu if (attr_map_fd < 0) {
4717fac83aaSSong Liu pr_err("Failed to lock perf_event_attr map\n");
4727fac83aaSSong Liu return -1;
4737fac83aaSSong Liu }
4747fac83aaSSong Liu
4757fac83aaSSong Liu err = bpf_map_lookup_elem(attr_map_fd, &evsel->core.attr, &entry);
4767fac83aaSSong Liu if (err) {
4777fac83aaSSong Liu err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, &entry, BPF_ANY);
4787fac83aaSSong Liu if (err)
4797fac83aaSSong Liu goto out;
4807fac83aaSSong Liu }
4817fac83aaSSong Liu
4827fac83aaSSong Liu evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry.link_id);
4837fac83aaSSong Liu if (evsel->bperf_leader_link_fd < 0 &&
484d3fddc35SYu Kuai bperf_reload_leader_program(evsel, attr_map_fd, &entry)) {
485d3fddc35SYu Kuai err = -1;
4867fac83aaSSong Liu goto out;
487d3fddc35SYu Kuai }
4887fac83aaSSong Liu /*
4897fac83aaSSong Liu * The bpf_link holds reference to the leader program, and the
4907fac83aaSSong Liu * leader program holds reference to the maps. Therefore, if
4917fac83aaSSong Liu * link_id is valid, diff_map_id should also be valid.
4927fac83aaSSong Liu */
4937fac83aaSSong Liu evsel->bperf_leader_prog_fd = bpf_prog_get_fd_by_id(
4947fac83aaSSong Liu bpf_link_get_prog_id(evsel->bperf_leader_link_fd));
4957fac83aaSSong Liu assert(evsel->bperf_leader_prog_fd >= 0);
4967fac83aaSSong Liu
4977fac83aaSSong Liu diff_map_fd = bpf_map_get_fd_by_id(entry.diff_map_id);
4987fac83aaSSong Liu assert(diff_map_fd >= 0);
4997fac83aaSSong Liu
5007fac83aaSSong Liu /*
5017fac83aaSSong Liu * bperf uses BPF_PROG_TEST_RUN to get accurate reading. Check
5027fac83aaSSong Liu * whether the kernel support it
5037fac83aaSSong Liu */
5047fac83aaSSong Liu err = bperf_trigger_reading(evsel->bperf_leader_prog_fd, 0);
5057fac83aaSSong Liu if (err) {
5067fac83aaSSong Liu pr_err("The kernel does not support test_run for raw_tp BPF programs.\n"
5077fac83aaSSong Liu "Therefore, --use-bpf might show inaccurate readings\n");
5087fac83aaSSong Liu goto out;
5097fac83aaSSong Liu }
5107fac83aaSSong Liu
5117fac83aaSSong Liu /* Step 2: load the follower skeleton */
5127fac83aaSSong Liu evsel->follower_skel = bperf_follower_bpf__open();
5137fac83aaSSong Liu if (!evsel->follower_skel) {
514d3fddc35SYu Kuai err = -1;
5157fac83aaSSong Liu pr_err("Failed to open follower skeleton\n");
5167fac83aaSSong Liu goto out;
5177fac83aaSSong Liu }
5187fac83aaSSong Liu
5197fac83aaSSong Liu /* attach fexit program to the leader program */
5207fac83aaSSong Liu bpf_program__set_attach_target(evsel->follower_skel->progs.fexit_XXX,
5217fac83aaSSong Liu evsel->bperf_leader_prog_fd, "on_switch");
5227fac83aaSSong Liu
5237fac83aaSSong Liu /* connect to leader diff_reading map */
5247fac83aaSSong Liu bpf_map__reuse_fd(evsel->follower_skel->maps.diff_readings, diff_map_fd);
5257fac83aaSSong Liu
5267fac83aaSSong Liu /* set up reading map */
5277fac83aaSSong Liu bpf_map__set_max_entries(evsel->follower_skel->maps.accum_readings,
5287fac83aaSSong Liu filter_entry_cnt);
5297fac83aaSSong Liu /* set up follower filter based on target */
5307fac83aaSSong Liu bpf_map__set_max_entries(evsel->follower_skel->maps.filter,
5317fac83aaSSong Liu filter_entry_cnt);
5327fac83aaSSong Liu err = bperf_follower_bpf__load(evsel->follower_skel);
5337fac83aaSSong Liu if (err) {
5347fac83aaSSong Liu pr_err("Failed to load follower skeleton\n");
5357fac83aaSSong Liu bperf_follower_bpf__destroy(evsel->follower_skel);
5367fac83aaSSong Liu evsel->follower_skel = NULL;
5377fac83aaSSong Liu goto out;
5387fac83aaSSong Liu }
5397fac83aaSSong Liu
5407fac83aaSSong Liu for (i = 0; i < filter_entry_cnt; i++) {
5417fac83aaSSong Liu int filter_map_fd;
5427fac83aaSSong Liu __u32 key;
5437fac83aaSSong Liu
5447fac83aaSSong Liu if (filter_type == BPERF_FILTER_PID ||
5457fac83aaSSong Liu filter_type == BPERF_FILTER_TGID)
546fd3f518fSIan Rogers key = perf_thread_map__pid(evsel->core.threads, i);
5477fac83aaSSong Liu else if (filter_type == BPERF_FILTER_CPU)
54839b5e434SIan Rogers key = perf_cpu_map__cpu(evsel->core.cpus, i).cpu;
5497fac83aaSSong Liu else
5507fac83aaSSong Liu break;
5517fac83aaSSong Liu
5527fac83aaSSong Liu filter_map_fd = bpf_map__fd(evsel->follower_skel->maps.filter);
5537fac83aaSSong Liu bpf_map_update_elem(filter_map_fd, &key, &i, BPF_ANY);
5547fac83aaSSong Liu }
5557fac83aaSSong Liu
5567fac83aaSSong Liu evsel->follower_skel->bss->type = filter_type;
5577fac83aaSSong Liu
5587fac83aaSSong Liu err = bperf_follower_bpf__attach(evsel->follower_skel);
5597fac83aaSSong Liu
5607fac83aaSSong Liu out:
5617fac83aaSSong Liu if (err && evsel->bperf_leader_link_fd >= 0)
5627fac83aaSSong Liu close(evsel->bperf_leader_link_fd);
5637fac83aaSSong Liu if (err && evsel->bperf_leader_prog_fd >= 0)
5647fac83aaSSong Liu close(evsel->bperf_leader_prog_fd);
5657fac83aaSSong Liu if (diff_map_fd >= 0)
5667fac83aaSSong Liu close(diff_map_fd);
5677fac83aaSSong Liu
5687fac83aaSSong Liu flock(attr_map_fd, LOCK_UN);
5697fac83aaSSong Liu close(attr_map_fd);
5707fac83aaSSong Liu
5717fac83aaSSong Liu return err;
5727fac83aaSSong Liu }
5737fac83aaSSong Liu
bperf__install_pe(struct evsel * evsel,int cpu_map_idx,int fd)5747263f349SIan Rogers static int bperf__install_pe(struct evsel *evsel, int cpu_map_idx, int fd)
5757fac83aaSSong Liu {
5767fac83aaSSong Liu struct bperf_leader_bpf *skel = evsel->leader_skel;
5777fac83aaSSong Liu
5787fac83aaSSong Liu return bpf_map_update_elem(bpf_map__fd(skel->maps.events),
5797263f349SIan Rogers &cpu_map_idx, &fd, BPF_ANY);
5807fac83aaSSong Liu }
5817fac83aaSSong Liu
5827fac83aaSSong Liu /*
5837fac83aaSSong Liu * trigger the leader prog on each cpu, so the accum_reading map could get
5847fac83aaSSong Liu * the latest readings.
5857fac83aaSSong Liu */
bperf_sync_counters(struct evsel * evsel)5867fac83aaSSong Liu static int bperf_sync_counters(struct evsel *evsel)
5877fac83aaSSong Liu {
5887fac83aaSSong Liu int num_cpu, i, cpu;
5897fac83aaSSong Liu
59039b5e434SIan Rogers num_cpu = perf_cpu_map__nr(all_cpu_map);
5917fac83aaSSong Liu for (i = 0; i < num_cpu; i++) {
59239b5e434SIan Rogers cpu = perf_cpu_map__cpu(all_cpu_map, i).cpu;
5937fac83aaSSong Liu bperf_trigger_reading(evsel->bperf_leader_prog_fd, cpu);
5947fac83aaSSong Liu }
5957fac83aaSSong Liu return 0;
5967fac83aaSSong Liu }
5977fac83aaSSong Liu
bperf__enable(struct evsel * evsel)5987fac83aaSSong Liu static int bperf__enable(struct evsel *evsel)
5997fac83aaSSong Liu {
6007fac83aaSSong Liu evsel->follower_skel->bss->enabled = 1;
6017fac83aaSSong Liu return 0;
6027fac83aaSSong Liu }
6037fac83aaSSong Liu
bperf__disable(struct evsel * evsel)6045508c9daSSong Liu static int bperf__disable(struct evsel *evsel)
6055508c9daSSong Liu {
6065508c9daSSong Liu evsel->follower_skel->bss->enabled = 0;
6075508c9daSSong Liu return 0;
6085508c9daSSong Liu }
6095508c9daSSong Liu
bperf__read(struct evsel * evsel)6107fac83aaSSong Liu static int bperf__read(struct evsel *evsel)
6117fac83aaSSong Liu {
6127fac83aaSSong Liu struct bperf_follower_bpf *skel = evsel->follower_skel;
6136d18804bSIan Rogers __u32 num_cpu_bpf = cpu__max_cpu().cpu;
6147fac83aaSSong Liu struct bpf_perf_event_value values[num_cpu_bpf];
61554668a4eSIan Rogers struct perf_counts_values *counts;
6167fac83aaSSong Liu int reading_map_fd, err = 0;
6177263f349SIan Rogers __u32 i;
6187263f349SIan Rogers int j;
6197fac83aaSSong Liu
6207fac83aaSSong Liu bperf_sync_counters(evsel);
6217fac83aaSSong Liu reading_map_fd = bpf_map__fd(skel->maps.accum_readings);
6227fac83aaSSong Liu
6237fac83aaSSong Liu for (i = 0; i < bpf_map__max_entries(skel->maps.accum_readings); i++) {
6246d18804bSIan Rogers struct perf_cpu entry;
6257fac83aaSSong Liu __u32 cpu;
6267fac83aaSSong Liu
6277fac83aaSSong Liu err = bpf_map_lookup_elem(reading_map_fd, &i, values);
6287fac83aaSSong Liu if (err)
6297fac83aaSSong Liu goto out;
6307fac83aaSSong Liu switch (evsel->follower_skel->bss->type) {
6317fac83aaSSong Liu case BPERF_FILTER_GLOBAL:
6327fac83aaSSong Liu assert(i == 0);
6337fac83aaSSong Liu
63454668a4eSIan Rogers perf_cpu_map__for_each_cpu(entry, j, evsel__cpus(evsel)) {
63554668a4eSIan Rogers counts = perf_counts(evsel->counts, j, 0);
63654668a4eSIan Rogers counts->val = values[entry.cpu].counter;
63754668a4eSIan Rogers counts->ena = values[entry.cpu].enabled;
63854668a4eSIan Rogers counts->run = values[entry.cpu].running;
6397fac83aaSSong Liu }
6407fac83aaSSong Liu break;
6417fac83aaSSong Liu case BPERF_FILTER_CPU:
64254668a4eSIan Rogers cpu = perf_cpu_map__cpu(evsel__cpus(evsel), i).cpu;
64354668a4eSIan Rogers assert(cpu >= 0);
64454668a4eSIan Rogers counts = perf_counts(evsel->counts, i, 0);
64554668a4eSIan Rogers counts->val = values[cpu].counter;
64654668a4eSIan Rogers counts->ena = values[cpu].enabled;
64754668a4eSIan Rogers counts->run = values[cpu].running;
6487fac83aaSSong Liu break;
6497fac83aaSSong Liu case BPERF_FILTER_PID:
6507fac83aaSSong Liu case BPERF_FILTER_TGID:
65154668a4eSIan Rogers counts = perf_counts(evsel->counts, 0, i);
65254668a4eSIan Rogers counts->val = 0;
65354668a4eSIan Rogers counts->ena = 0;
65454668a4eSIan Rogers counts->run = 0;
6557fac83aaSSong Liu
6567fac83aaSSong Liu for (cpu = 0; cpu < num_cpu_bpf; cpu++) {
65754668a4eSIan Rogers counts->val += values[cpu].counter;
65854668a4eSIan Rogers counts->ena += values[cpu].enabled;
65954668a4eSIan Rogers counts->run += values[cpu].running;
6607fac83aaSSong Liu }
6617fac83aaSSong Liu break;
6627fac83aaSSong Liu default:
6637fac83aaSSong Liu break;
6647fac83aaSSong Liu }
6657fac83aaSSong Liu }
6667fac83aaSSong Liu out:
6677fac83aaSSong Liu return err;
6687fac83aaSSong Liu }
6697fac83aaSSong Liu
bperf__destroy(struct evsel * evsel)6707fac83aaSSong Liu static int bperf__destroy(struct evsel *evsel)
6717fac83aaSSong Liu {
6727fac83aaSSong Liu bperf_follower_bpf__destroy(evsel->follower_skel);
6737fac83aaSSong Liu close(evsel->bperf_leader_prog_fd);
6747fac83aaSSong Liu close(evsel->bperf_leader_link_fd);
6757fac83aaSSong Liu return 0;
6767fac83aaSSong Liu }
6777fac83aaSSong Liu
6787fac83aaSSong Liu /*
6797fac83aaSSong Liu * bperf: share hardware PMCs with BPF
6807fac83aaSSong Liu *
6817fac83aaSSong Liu * perf uses performance monitoring counters (PMC) to monitor system
6827fac83aaSSong Liu * performance. The PMCs are limited hardware resources. For example,
6837fac83aaSSong Liu * Intel CPUs have 3x fixed PMCs and 4x programmable PMCs per cpu.
6847fac83aaSSong Liu *
6857fac83aaSSong Liu * Modern data center systems use these PMCs in many different ways:
6867fac83aaSSong Liu * system level monitoring, (maybe nested) container level monitoring, per
6877fac83aaSSong Liu * process monitoring, profiling (in sample mode), etc. In some cases,
6887fac83aaSSong Liu * there are more active perf_events than available hardware PMCs. To allow
6897fac83aaSSong Liu * all perf_events to have a chance to run, it is necessary to do expensive
6907fac83aaSSong Liu * time multiplexing of events.
6917fac83aaSSong Liu *
6927fac83aaSSong Liu * On the other hand, many monitoring tools count the common metrics
6937fac83aaSSong Liu * (cycles, instructions). It is a waste to have multiple tools create
6947fac83aaSSong Liu * multiple perf_events of "cycles" and occupy multiple PMCs.
6957fac83aaSSong Liu *
6967fac83aaSSong Liu * bperf tries to reduce such wastes by allowing multiple perf_events of
6977fac83aaSSong Liu * "cycles" or "instructions" (at different scopes) to share PMUs. Instead
6987fac83aaSSong Liu * of having each perf-stat session to read its own perf_events, bperf uses
6997fac83aaSSong Liu * BPF programs to read the perf_events and aggregate readings to BPF maps.
7007fac83aaSSong Liu * Then, the perf-stat session(s) reads the values from these BPF maps.
7017fac83aaSSong Liu *
7027fac83aaSSong Liu * ||
7037fac83aaSSong Liu * shared progs and maps <- || -> per session progs and maps
7047fac83aaSSong Liu * ||
7057fac83aaSSong Liu * --------------- ||
7067fac83aaSSong Liu * | perf_events | ||
7077fac83aaSSong Liu * --------------- fexit || -----------------
7087fac83aaSSong Liu * | --------||----> | follower prog |
7097fac83aaSSong Liu * --------------- / || --- -----------------
7107fac83aaSSong Liu * cs -> | leader prog |/ ||/ | |
7117fac83aaSSong Liu * --> --------------- /|| -------------- ------------------
7127fac83aaSSong Liu * / | | / || | filter map | | accum_readings |
7137fac83aaSSong Liu * / ------------ ------------ || -------------- ------------------
7147fac83aaSSong Liu * | | prev map | | diff map | || |
7157fac83aaSSong Liu * | ------------ ------------ || |
7167fac83aaSSong Liu * \ || |
7177fac83aaSSong Liu * = \ ==================================================== | ============
7187fac83aaSSong Liu * \ / user space
7197fac83aaSSong Liu * \ /
7207fac83aaSSong Liu * \ /
7217fac83aaSSong Liu * BPF_PROG_TEST_RUN BPF_MAP_LOOKUP_ELEM
7227fac83aaSSong Liu * \ /
7237fac83aaSSong Liu * \ /
7247fac83aaSSong Liu * \------ perf-stat ----------------------/
7257fac83aaSSong Liu *
7267fac83aaSSong Liu * The figure above shows the architecture of bperf. Note that the figure
7277fac83aaSSong Liu * is divided into 3 regions: shared progs and maps (top left), per session
7287fac83aaSSong Liu * progs and maps (top right), and user space (bottom).
7297fac83aaSSong Liu *
7307fac83aaSSong Liu * The leader prog is triggered on each context switch (cs). The leader
7317fac83aaSSong Liu * prog reads perf_events and stores the difference (current_reading -
7327fac83aaSSong Liu * previous_reading) to the diff map. For the same metric, e.g. "cycles",
7337fac83aaSSong Liu * multiple perf-stat sessions share the same leader prog.
7347fac83aaSSong Liu *
7357fac83aaSSong Liu * Each perf-stat session creates a follower prog as fexit program to the
7367fac83aaSSong Liu * leader prog. It is possible to attach up to BPF_MAX_TRAMP_PROGS (38)
7377fac83aaSSong Liu * follower progs to the same leader prog. The follower prog checks current
7387fac83aaSSong Liu * task and processor ID to decide whether to add the value from the diff
7397fac83aaSSong Liu * map to its accumulated reading map (accum_readings).
7407fac83aaSSong Liu *
7417fac83aaSSong Liu * Finally, perf-stat user space reads the value from accum_reading map.
7427fac83aaSSong Liu *
7437fac83aaSSong Liu * Besides context switch, it is also necessary to trigger the leader prog
7447fac83aaSSong Liu * before perf-stat reads the value. Otherwise, the accum_reading map may
7457fac83aaSSong Liu * not have the latest reading from the perf_events. This is achieved by
7467fac83aaSSong Liu * triggering the event via sys_bpf(BPF_PROG_TEST_RUN) to each CPU.
7477fac83aaSSong Liu *
7487fac83aaSSong Liu * Comment before the definition of struct perf_event_attr_map_entry
7497fac83aaSSong Liu * describes how different sessions of perf-stat share information about
7507fac83aaSSong Liu * the leader prog.
7517fac83aaSSong Liu */
7527fac83aaSSong Liu
7537fac83aaSSong Liu struct bpf_counter_ops bperf_ops = {
7547fac83aaSSong Liu .load = bperf__load,
7557fac83aaSSong Liu .enable = bperf__enable,
7565508c9daSSong Liu .disable = bperf__disable,
7577fac83aaSSong Liu .read = bperf__read,
7587fac83aaSSong Liu .install_pe = bperf__install_pe,
7597fac83aaSSong Liu .destroy = bperf__destroy,
7607fac83aaSSong Liu };
7617fac83aaSSong Liu
762944138f0SNamhyung Kim extern struct bpf_counter_ops bperf_cgrp_ops;
763944138f0SNamhyung Kim
bpf_counter_skip(struct evsel * evsel)7647fac83aaSSong Liu static inline bool bpf_counter_skip(struct evsel *evsel)
7657fac83aaSSong Liu {
766d180aa56SNamhyung Kim return evsel->bpf_counter_ops == NULL;
7677fac83aaSSong Liu }
7687fac83aaSSong Liu
bpf_counter__install_pe(struct evsel * evsel,int cpu_map_idx,int fd)7697263f349SIan Rogers int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd)
770fa853c4bSSong Liu {
7717fac83aaSSong Liu if (bpf_counter_skip(evsel))
772fa853c4bSSong Liu return 0;
7737263f349SIan Rogers return evsel->bpf_counter_ops->install_pe(evsel, cpu_map_idx, fd);
774fa853c4bSSong Liu }
775fa853c4bSSong Liu
bpf_counter__load(struct evsel * evsel,struct target * target)776fa853c4bSSong Liu int bpf_counter__load(struct evsel *evsel, struct target *target)
777fa853c4bSSong Liu {
7787fac83aaSSong Liu if (target->bpf_str)
779fa853c4bSSong Liu evsel->bpf_counter_ops = &bpf_program_profiler_ops;
780944138f0SNamhyung Kim else if (cgrp_event_expanded && target->use_bpf)
781944138f0SNamhyung Kim evsel->bpf_counter_ops = &bperf_cgrp_ops;
78201bd8efcSSong Liu else if (target->use_bpf || evsel->bpf_counter ||
783112cb561SSong Liu evsel__match_bpf_counter_events(evsel->name))
7847fac83aaSSong Liu evsel->bpf_counter_ops = &bperf_ops;
785fa853c4bSSong Liu
786fa853c4bSSong Liu if (evsel->bpf_counter_ops)
787fa853c4bSSong Liu return evsel->bpf_counter_ops->load(evsel, target);
788fa853c4bSSong Liu return 0;
789fa853c4bSSong Liu }
790fa853c4bSSong Liu
bpf_counter__enable(struct evsel * evsel)791fa853c4bSSong Liu int bpf_counter__enable(struct evsel *evsel)
792fa853c4bSSong Liu {
7937fac83aaSSong Liu if (bpf_counter_skip(evsel))
794fa853c4bSSong Liu return 0;
795fa853c4bSSong Liu return evsel->bpf_counter_ops->enable(evsel);
796fa853c4bSSong Liu }
797fa853c4bSSong Liu
bpf_counter__disable(struct evsel * evsel)7985508c9daSSong Liu int bpf_counter__disable(struct evsel *evsel)
7995508c9daSSong Liu {
8005508c9daSSong Liu if (bpf_counter_skip(evsel))
8015508c9daSSong Liu return 0;
8025508c9daSSong Liu return evsel->bpf_counter_ops->disable(evsel);
8035508c9daSSong Liu }
8045508c9daSSong Liu
bpf_counter__read(struct evsel * evsel)805fa853c4bSSong Liu int bpf_counter__read(struct evsel *evsel)
806fa853c4bSSong Liu {
8077fac83aaSSong Liu if (bpf_counter_skip(evsel))
808fa853c4bSSong Liu return -EAGAIN;
809fa853c4bSSong Liu return evsel->bpf_counter_ops->read(evsel);
810fa853c4bSSong Liu }
811fa853c4bSSong Liu
bpf_counter__destroy(struct evsel * evsel)812fa853c4bSSong Liu void bpf_counter__destroy(struct evsel *evsel)
813fa853c4bSSong Liu {
8147fac83aaSSong Liu if (bpf_counter_skip(evsel))
815fa853c4bSSong Liu return;
816fa853c4bSSong Liu evsel->bpf_counter_ops->destroy(evsel);
817fa853c4bSSong Liu evsel->bpf_counter_ops = NULL;
818*e0137336SIan Rogers evsel->bpf_skel = NULL;
819fa853c4bSSong Liu }
820