Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / perf_api_probe.c
blob3840d02f0f7b0aaa0d8730565eebd910943fc3ec
1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include "perf-sys.h"
4 #include "util/cloexec.h"
5 #include "util/evlist.h"
6 #include "util/evsel.h"
7 #include "util/parse-events.h"
8 #include "util/perf_api_probe.h"
9 #include <perf/cpumap.h>
10 #include <errno.h>
12 typedef void (*setup_probe_fn_t)(struct evsel *evsel);
14 static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
16 struct evlist *evlist;
17 struct evsel *evsel;
18 unsigned long flags = perf_event_open_cloexec_flag();
19 int err = -EAGAIN, fd;
20 static pid_t pid = -1;
22 evlist = evlist__new();
23 if (!evlist)
24 return -ENOMEM;
26 if (parse_events(evlist, str, NULL))
27 goto out_delete;
29 evsel = evlist__first(evlist);
31 while (1) {
32 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
33 if (fd < 0) {
34 if (pid == -1 && errno == EACCES) {
35 pid = 0;
36 continue;
38 goto out_delete;
40 break;
42 close(fd);
44 fn(evsel);
46 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
47 if (fd < 0) {
48 if (errno == EINVAL)
49 err = -EINVAL;
50 goto out_delete;
52 close(fd);
53 err = 0;
55 out_delete:
56 evlist__delete(evlist);
57 return err;
60 static bool perf_probe_api(setup_probe_fn_t fn)
62 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
63 struct perf_cpu_map *cpus;
64 int cpu, ret, i = 0;
66 cpus = perf_cpu_map__new(NULL);
67 if (!cpus)
68 return false;
69 cpu = cpus->map[0];
70 perf_cpu_map__put(cpus);
72 do {
73 ret = perf_do_probe_api(fn, cpu, try[i++]);
74 if (!ret)
75 return true;
76 } while (ret == -EAGAIN && try[i]);
78 return false;
81 static void perf_probe_sample_identifier(struct evsel *evsel)
83 evsel->core.attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
86 static void perf_probe_comm_exec(struct evsel *evsel)
88 evsel->core.attr.comm_exec = 1;
91 static void perf_probe_context_switch(struct evsel *evsel)
93 evsel->core.attr.context_switch = 1;
96 static void perf_probe_text_poke(struct evsel *evsel)
98 evsel->core.attr.text_poke = 1;
101 bool perf_can_sample_identifier(void)
103 return perf_probe_api(perf_probe_sample_identifier);
106 bool perf_can_comm_exec(void)
108 return perf_probe_api(perf_probe_comm_exec);
111 bool perf_can_record_switch_events(void)
113 return perf_probe_api(perf_probe_context_switch);
116 bool perf_can_record_text_poke_events(void)
118 return perf_probe_api(perf_probe_text_poke);
121 bool perf_can_record_cpu_wide(void)
123 struct perf_event_attr attr = {
124 .type = PERF_TYPE_SOFTWARE,
125 .config = PERF_COUNT_SW_CPU_CLOCK,
126 .exclude_kernel = 1,
128 struct perf_cpu_map *cpus;
129 int cpu, fd;
131 cpus = perf_cpu_map__new(NULL);
132 if (!cpus)
133 return false;
134 cpu = cpus->map[0];
135 perf_cpu_map__put(cpus);
137 fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
138 if (fd < 0)
139 return false;
140 close(fd);
142 return true;
146 * Architectures are expected to know if AUX area sampling is supported by the
147 * hardware. Here we check for kernel support.
149 bool perf_can_aux_sample(void)
151 struct perf_event_attr attr = {
152 .size = sizeof(struct perf_event_attr),
153 .exclude_kernel = 1,
155 * Non-zero value causes the kernel to calculate the effective
156 * attribute size up to that byte.
158 .aux_sample_size = 1,
160 int fd;
162 fd = sys_perf_event_open(&attr, -1, 0, -1, 0);
164 * If the kernel attribute is big enough to contain aux_sample_size
165 * then we assume that it is supported. We are relying on the kernel to
166 * validate the attribute size before anything else that could be wrong.
168 if (fd < 0 && errno == E2BIG)
169 return false;
170 if (fd >= 0)
171 close(fd);
173 return true;