]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/util/record.c
libperf: Move perf_event_attr field from perf's evsel to libperf's perf_evsel
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / util / record.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
faf96706
AH
2#include "evlist.h"
3#include "evsel.h"
4#include "cpumap.h"
75562573 5#include "parse-events.h"
a43783ae 6#include <errno.h>
cd0cfad7 7#include <api/fs/fs.h>
67230479 8#include <subcmd/parse-options.h>
714647bd 9#include "util.h"
57480d2c 10#include "cloexec.h"
75562573 11
32dcd021 12typedef void (*setup_probe_fn_t)(struct evsel *evsel);
75562573
AH
13
14static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
15{
63503dba 16 struct evlist *evlist;
32dcd021 17 struct evsel *evsel;
57480d2c 18 unsigned long flags = perf_event_open_cloexec_flag();
75562573 19 int err = -EAGAIN, fd;
46ec69ad 20 static pid_t pid = -1;
75562573 21
0f98b11c 22 evlist = evlist__new();
75562573
AH
23 if (!evlist)
24 return -ENOMEM;
25
b39b8393 26 if (parse_events(evlist, str, NULL))
75562573
AH
27 goto out_delete;
28
29 evsel = perf_evlist__first(evlist);
30
46ec69ad 31 while (1) {
1fc632ce 32 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
46ec69ad
AH
33 if (fd < 0) {
34 if (pid == -1 && errno == EACCES) {
35 pid = 0;
36 continue;
37 }
38 goto out_delete;
39 }
40 break;
41 }
75562573
AH
42 close(fd);
43
44 fn(evsel);
45
1fc632ce 46 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1, flags);
75562573
AH
47 if (fd < 0) {
48 if (errno == EINVAL)
49 err = -EINVAL;
50 goto out_delete;
51 }
52 close(fd);
53 err = 0;
54
55out_delete:
c12995a5 56 evlist__delete(evlist);
75562573
AH
57 return err;
58}
59
60static bool perf_probe_api(setup_probe_fn_t fn)
61{
c6fa3565 62 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
f854839b 63 struct perf_cpu_map *cpus;
75562573
AH
64 int cpu, ret, i = 0;
65
66 cpus = cpu_map__new(NULL);
67 if (!cpus)
68 return false;
69 cpu = cpus->map[0];
38f01d8d 70 perf_cpu_map__put(cpus);
75562573
AH
71
72 do {
73 ret = perf_do_probe_api(fn, cpu, try[i++]);
74 if (!ret)
75 return true;
76 } while (ret == -EAGAIN && try[i]);
77
78 return false;
79}
80
32dcd021 81static void perf_probe_sample_identifier(struct evsel *evsel)
75562573 82{
1fc632ce 83 evsel->core.attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
75562573
AH
84}
85
32dcd021 86static void perf_probe_comm_exec(struct evsel *evsel)
39e09d40 87{
1fc632ce 88 evsel->core.attr.comm_exec = 1;
39e09d40
AH
89}
90
32dcd021 91static void perf_probe_context_switch(struct evsel *evsel)
b757bb09 92{
1fc632ce 93 evsel->core.attr.context_switch = 1;
b757bb09
AH
94}
95
75562573
AH
96bool perf_can_sample_identifier(void)
97{
98 return perf_probe_api(perf_probe_sample_identifier);
99}
faf96706 100
39e09d40
AH
101static bool perf_can_comm_exec(void)
102{
103 return perf_probe_api(perf_probe_comm_exec);
104}
105
b757bb09
AH
106bool perf_can_record_switch_events(void)
107{
108 return perf_probe_api(perf_probe_context_switch);
109}
110
83509565
AH
111bool perf_can_record_cpu_wide(void)
112{
113 struct perf_event_attr attr = {
114 .type = PERF_TYPE_SOFTWARE,
115 .config = PERF_COUNT_SW_CPU_CLOCK,
116 .exclude_kernel = 1,
117 };
f854839b 118 struct perf_cpu_map *cpus;
83509565
AH
119 int cpu, fd;
120
121 cpus = cpu_map__new(NULL);
122 if (!cpus)
123 return false;
124 cpu = cpus->map[0];
38f01d8d 125 perf_cpu_map__put(cpus);
83509565
AH
126
127 fd = sys_perf_event_open(&attr, -1, cpu, -1, 0);
128 if (fd < 0)
129 return false;
130 close(fd);
131
132 return true;
133}
134
63503dba 135void perf_evlist__config(struct evlist *evlist, struct record_opts *opts,
e68ae9cf 136 struct callchain_param *callchain)
faf96706 137{
32dcd021 138 struct evsel *evsel;
75562573 139 bool use_sample_identifier = false;
39e09d40 140 bool use_comm_exec;
ad46e48c 141 bool sample_id = opts->sample_id;
75562573 142
faf96706
AH
143 /*
144 * Set the evsel leader links before we configure attributes,
145 * since some might depend on this info.
146 */
147 if (opts->group)
148 perf_evlist__set_leader(evlist);
149
150 if (evlist->cpus->map[0] < 0)
151 opts->no_inherit = true;
152
39e09d40
AH
153 use_comm_exec = perf_can_comm_exec();
154
e5cadb93 155 evlist__for_each_entry(evlist, evsel) {
e68ae9cf 156 perf_evsel__config(evsel, opts, callchain);
60b0896c 157 if (evsel->tracking && use_comm_exec)
1fc632ce 158 evsel->core.attr.comm_exec = 1;
39e09d40 159 }
faf96706 160
9e0cc4fe
AH
161 if (opts->full_auxtrace) {
162 /*
163 * Need to be able to synthesize and parse selected events with
164 * arbitrary sample types, which requires always being able to
165 * match the id.
166 */
167 use_sample_identifier = perf_can_sample_identifier();
ad46e48c 168 sample_id = true;
6484d2f9 169 } else if (evlist->core.nr_entries > 1) {
32dcd021 170 struct evsel *first = perf_evlist__first(evlist);
75562573 171
e5cadb93 172 evlist__for_each_entry(evlist, evsel) {
1fc632ce 173 if (evsel->core.attr.sample_type == first->core.attr.sample_type)
75562573
AH
174 continue;
175 use_sample_identifier = perf_can_sample_identifier();
176 break;
177 }
ad46e48c
JO
178 sample_id = true;
179 }
180
181 if (sample_id) {
e5cadb93 182 evlist__for_each_entry(evlist, evsel)
75562573 183 perf_evsel__set_sample_id(evsel, use_sample_identifier);
faf96706 184 }
75562573
AH
185
186 perf_evlist__set_id_pos(evlist);
faf96706 187}
714647bd
JO
188
189static int get_max_rate(unsigned int *rate)
190{
ce27309f 191 return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
714647bd
JO
192}
193
b4006796 194static int record_opts__config_freq(struct record_opts *opts)
714647bd
JO
195{
196 bool user_freq = opts->user_freq != UINT_MAX;
197 unsigned int max_rate;
198
199 if (opts->user_interval != ULLONG_MAX)
200 opts->default_interval = opts->user_interval;
201 if (user_freq)
202 opts->freq = opts->user_freq;
203
204 /*
205 * User specified count overrides default frequency.
206 */
207 if (opts->default_interval)
208 opts->freq = 0;
209 else if (opts->freq) {
210 opts->default_interval = opts->freq;
211 } else {
212 pr_err("frequency and count are zero, aborting\n");
213 return -1;
214 }
215
216 if (get_max_rate(&max_rate))
217 return 0;
218
219 /*
220 * User specified frequency is over current maximum.
221 */
222 if (user_freq && (max_rate < opts->freq)) {
b09c2364
ACM
223 if (opts->strict_freq) {
224 pr_err("error: Maximum frequency rate (%'u Hz) exceeded.\n"
225 " Please use -F freq option with a lower value or consider\n"
226 " tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
227 max_rate);
228 return -1;
229 } else {
230 pr_warning("warning: Maximum frequency rate (%'u Hz) exceeded, throttling from %'u Hz to %'u Hz.\n"
231 " The limit can be raised via /proc/sys/kernel/perf_event_max_sample_rate.\n"
232 " The kernel will lower it when perf's interrupts take too long.\n"
233 " Use --strict-freq to disable this throttling, refusing to record.\n",
234 max_rate, opts->freq, max_rate);
235
236 opts->freq = max_rate;
237 }
714647bd
JO
238 }
239
240 /*
241 * Default frequency is over current maximum.
242 */
243 if (max_rate < opts->freq) {
244 pr_warning("Lowering default frequency rate to %u.\n"
245 "Please consider tweaking "
246 "/proc/sys/kernel/perf_event_max_sample_rate.\n",
247 max_rate);
248 opts->freq = max_rate;
249 }
250
251 return 0;
252}
253
b4006796 254int record_opts__config(struct record_opts *opts)
714647bd 255{
b4006796 256 return record_opts__config_freq(opts);
714647bd 257}
c09ec622 258
63503dba 259bool perf_evlist__can_select_event(struct evlist *evlist, const char *str)
c09ec622 260{
63503dba 261 struct evlist *temp_evlist;
32dcd021 262 struct evsel *evsel;
c09ec622
AH
263 int err, fd, cpu;
264 bool ret = false;
46ec69ad 265 pid_t pid = -1;
c09ec622 266
0f98b11c 267 temp_evlist = evlist__new();
c09ec622
AH
268 if (!temp_evlist)
269 return false;
270
b39b8393 271 err = parse_events(temp_evlist, str, NULL);
c09ec622
AH
272 if (err)
273 goto out_delete;
274
275 evsel = perf_evlist__last(temp_evlist);
276
277 if (!evlist || cpu_map__empty(evlist->cpus)) {
f854839b 278 struct perf_cpu_map *cpus = cpu_map__new(NULL);
c09ec622
AH
279
280 cpu = cpus ? cpus->map[0] : 0;
38f01d8d 281 perf_cpu_map__put(cpus);
c09ec622
AH
282 } else {
283 cpu = evlist->cpus->map[0];
284 }
285
46ec69ad 286 while (1) {
1fc632ce 287 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1,
46ec69ad
AH
288 perf_event_open_cloexec_flag());
289 if (fd < 0) {
290 if (pid == -1 && errno == EACCES) {
291 pid = 0;
292 continue;
293 }
294 goto out_delete;
295 }
296 break;
c09ec622 297 }
46ec69ad
AH
298 close(fd);
299 ret = true;
c09ec622
AH
300
301out_delete:
c12995a5 302 evlist__delete(temp_evlist);
c09ec622
AH
303 return ret;
304}
67230479
ACM
305
306int record__parse_freq(const struct option *opt, const char *str, int unset __maybe_unused)
307{
308 unsigned int freq;
309 struct record_opts *opts = opt->value;
310
311 if (!str)
312 return -EINVAL;
313
314 if (strcasecmp(str, "max") == 0) {
315 if (get_max_rate(&freq)) {
316 pr_err("couldn't read /proc/sys/kernel/perf_event_max_sample_rate\n");
317 return -1;
318 }
319 pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq);
320 } else {
321 freq = atoi(str);
322 }
323
324 opts->user_freq = freq;
325 return 0;
326}