Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include "evlist.h"
3#include "evsel.h"
4#include "cpumap.h"
5#include "parse-events.h"
6#include <errno.h>
7#include <api/fs/fs.h>
8#include <subcmd/parse-options.h>
9#include "util.h"
10#include "cloexec.h"
11
12typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
13
14static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
15{
16 struct perf_evlist *evlist;
17 struct perf_evsel *evsel;
18 unsigned long flags = perf_event_open_cloexec_flag();
19 int err = -EAGAIN, fd;
20 static pid_t pid = -1;
21
22 evlist = perf_evlist__new();
23 if (!evlist)
24 return -ENOMEM;
25
26 if (parse_events(evlist, str, NULL))
27 goto out_delete;
28
29 evsel = perf_evlist__first(evlist);
30
31 while (1) {
32 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
33 if (fd < 0) {
34 if (pid == -1 && errno == EACCES) {
35 pid = 0;
36 continue;
37 }
38 goto out_delete;
39 }
40 break;
41 }
42 close(fd);
43
44 fn(evsel);
45
46 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1, flags);
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:
56 perf_evlist__delete(evlist);
57 return err;
58}
59
60static bool perf_probe_api(setup_probe_fn_t fn)
61{
62 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL};
63 struct cpu_map *cpus;
64 int cpu, ret, i = 0;
65
66 cpus = cpu_map__new(NULL);
67 if (!cpus)
68 return false;
69 cpu = cpus->map[0];
70 cpu_map__put(cpus);
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
81static void perf_probe_sample_identifier(struct perf_evsel *evsel)
82{
83 evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
84}
85
86static void perf_probe_comm_exec(struct perf_evsel *evsel)
87{
88 evsel->attr.comm_exec = 1;
89}
90
91static void perf_probe_context_switch(struct perf_evsel *evsel)
92{
93 evsel->attr.context_switch = 1;
94}
95
96bool perf_can_sample_identifier(void)
97{
98 return perf_probe_api(perf_probe_sample_identifier);
99}
100
101static bool perf_can_comm_exec(void)
102{
103 return perf_probe_api(perf_probe_comm_exec);
104}
105
106bool perf_can_record_switch_events(void)
107{
108 return perf_probe_api(perf_probe_context_switch);
109}
110
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 };
118 struct cpu_map *cpus;
119 int cpu, fd;
120
121 cpus = cpu_map__new(NULL);
122 if (!cpus)
123 return false;
124 cpu = cpus->map[0];
125 cpu_map__put(cpus);
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
135void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
136 struct callchain_param *callchain)
137{
138 struct perf_evsel *evsel;
139 bool use_sample_identifier = false;
140 bool use_comm_exec;
141 bool sample_id = opts->sample_id;
142
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
153 use_comm_exec = perf_can_comm_exec();
154
155 evlist__for_each_entry(evlist, evsel) {
156 perf_evsel__config(evsel, opts, callchain);
157 if (evsel->tracking && use_comm_exec)
158 evsel->attr.comm_exec = 1;
159 }
160
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();
168 sample_id = true;
169 } else if (evlist->nr_entries > 1) {
170 struct perf_evsel *first = perf_evlist__first(evlist);
171
172 evlist__for_each_entry(evlist, evsel) {
173 if (evsel->attr.sample_type == first->attr.sample_type)
174 continue;
175 use_sample_identifier = perf_can_sample_identifier();
176 break;
177 }
178 sample_id = true;
179 }
180
181 if (sample_id) {
182 evlist__for_each_entry(evlist, evsel)
183 perf_evsel__set_sample_id(evsel, use_sample_identifier);
184 }
185
186 perf_evlist__set_id_pos(evlist);
187}
188
189static int get_max_rate(unsigned int *rate)
190{
191 return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
192}
193
194static int record_opts__config_freq(struct record_opts *opts)
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)) {
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 }
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
254int record_opts__config(struct record_opts *opts)
255{
256 return record_opts__config_freq(opts);
257}
258
259bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
260{
261 struct perf_evlist *temp_evlist;
262 struct perf_evsel *evsel;
263 int err, fd, cpu;
264 bool ret = false;
265 pid_t pid = -1;
266
267 temp_evlist = perf_evlist__new();
268 if (!temp_evlist)
269 return false;
270
271 err = parse_events(temp_evlist, str, NULL);
272 if (err)
273 goto out_delete;
274
275 evsel = perf_evlist__last(temp_evlist);
276
277 if (!evlist || cpu_map__empty(evlist->cpus)) {
278 struct cpu_map *cpus = cpu_map__new(NULL);
279
280 cpu = cpus ? cpus->map[0] : 0;
281 cpu_map__put(cpus);
282 } else {
283 cpu = evlist->cpus->map[0];
284 }
285
286 while (1) {
287 fd = sys_perf_event_open(&evsel->attr, pid, cpu, -1,
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;
297 }
298 close(fd);
299 ret = true;
300
301out_delete:
302 perf_evlist__delete(temp_evlist);
303 return ret;
304}
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}
1// SPDX-License-Identifier: GPL-2.0
2#include "debug.h"
3#include "evlist.h"
4#include "evsel.h"
5#include "evsel_config.h"
6#include "parse-events.h"
7#include <errno.h>
8#include <limits.h>
9#include <stdlib.h>
10#include <api/fs/fs.h>
11#include <subcmd/parse-options.h>
12#include <perf/cpumap.h>
13#include "cloexec.h"
14#include "util/perf_api_probe.h"
15#include "record.h"
16#include "../perf-sys.h"
17
18/*
19 * evsel__config_leader_sampling() uses special rules for leader sampling.
20 * However, if the leader is an AUX area event, then assume the event to sample
21 * is the next event.
22 */
23static struct evsel *evsel__read_sampler(struct evsel *evsel, struct evlist *evlist)
24{
25 struct evsel *leader = evsel->leader;
26
27 if (evsel__is_aux_event(leader)) {
28 evlist__for_each_entry(evlist, evsel) {
29 if (evsel->leader == leader && evsel != evsel->leader)
30 return evsel;
31 }
32 }
33
34 return leader;
35}
36
37static u64 evsel__config_term_mask(struct evsel *evsel)
38{
39 struct evsel_config_term *term;
40 struct list_head *config_terms = &evsel->config_terms;
41 u64 term_types = 0;
42
43 list_for_each_entry(term, config_terms, list) {
44 term_types |= 1 << term->type;
45 }
46 return term_types;
47}
48
49static void evsel__config_leader_sampling(struct evsel *evsel, struct evlist *evlist)
50{
51 struct perf_event_attr *attr = &evsel->core.attr;
52 struct evsel *leader = evsel->leader;
53 struct evsel *read_sampler;
54 u64 term_types, freq_mask;
55
56 if (!leader->sample_read)
57 return;
58
59 read_sampler = evsel__read_sampler(evsel, evlist);
60
61 if (evsel == read_sampler)
62 return;
63
64 term_types = evsel__config_term_mask(evsel);
65 /*
66 * Disable sampling for all group members except those with explicit
67 * config terms or the leader. In the case of an AUX area event, the 2nd
68 * event in the group is the one that 'leads' the sampling.
69 */
70 freq_mask = (1 << EVSEL__CONFIG_TERM_FREQ) | (1 << EVSEL__CONFIG_TERM_PERIOD);
71 if ((term_types & freq_mask) == 0) {
72 attr->freq = 0;
73 attr->sample_freq = 0;
74 attr->sample_period = 0;
75 }
76 if ((term_types & (1 << EVSEL__CONFIG_TERM_OVERWRITE)) == 0)
77 attr->write_backward = 0;
78
79 /*
80 * We don't get a sample for slave events, we make them when delivering
81 * the group leader sample. Set the slave event to follow the master
82 * sample_type to ease up reporting.
83 * An AUX area event also has sample_type requirements, so also include
84 * the sample type bits from the leader's sample_type to cover that
85 * case.
86 */
87 attr->sample_type = read_sampler->core.attr.sample_type |
88 leader->core.attr.sample_type;
89}
90
91void perf_evlist__config(struct evlist *evlist, struct record_opts *opts,
92 struct callchain_param *callchain)
93{
94 struct evsel *evsel;
95 bool use_sample_identifier = false;
96 bool use_comm_exec;
97 bool sample_id = opts->sample_id;
98
99 /*
100 * Set the evsel leader links before we configure attributes,
101 * since some might depend on this info.
102 */
103 if (opts->group)
104 perf_evlist__set_leader(evlist);
105
106 if (evlist->core.cpus->map[0] < 0)
107 opts->no_inherit = true;
108
109 use_comm_exec = perf_can_comm_exec();
110
111 evlist__for_each_entry(evlist, evsel) {
112 evsel__config(evsel, opts, callchain);
113 if (evsel->tracking && use_comm_exec)
114 evsel->core.attr.comm_exec = 1;
115 }
116
117 /* Configure leader sampling here now that the sample type is known */
118 evlist__for_each_entry(evlist, evsel)
119 evsel__config_leader_sampling(evsel, evlist);
120
121 if (opts->full_auxtrace) {
122 /*
123 * Need to be able to synthesize and parse selected events with
124 * arbitrary sample types, which requires always being able to
125 * match the id.
126 */
127 use_sample_identifier = perf_can_sample_identifier();
128 sample_id = true;
129 } else if (evlist->core.nr_entries > 1) {
130 struct evsel *first = evlist__first(evlist);
131
132 evlist__for_each_entry(evlist, evsel) {
133 if (evsel->core.attr.sample_type == first->core.attr.sample_type)
134 continue;
135 use_sample_identifier = perf_can_sample_identifier();
136 break;
137 }
138 sample_id = true;
139 }
140
141 if (sample_id) {
142 evlist__for_each_entry(evlist, evsel)
143 evsel__set_sample_id(evsel, use_sample_identifier);
144 }
145
146 perf_evlist__set_id_pos(evlist);
147}
148
149static int get_max_rate(unsigned int *rate)
150{
151 return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
152}
153
154static int record_opts__config_freq(struct record_opts *opts)
155{
156 bool user_freq = opts->user_freq != UINT_MAX;
157 unsigned int max_rate;
158
159 if (opts->user_interval != ULLONG_MAX)
160 opts->default_interval = opts->user_interval;
161 if (user_freq)
162 opts->freq = opts->user_freq;
163
164 /*
165 * User specified count overrides default frequency.
166 */
167 if (opts->default_interval)
168 opts->freq = 0;
169 else if (opts->freq) {
170 opts->default_interval = opts->freq;
171 } else {
172 pr_err("frequency and count are zero, aborting\n");
173 return -1;
174 }
175
176 if (get_max_rate(&max_rate))
177 return 0;
178
179 /*
180 * User specified frequency is over current maximum.
181 */
182 if (user_freq && (max_rate < opts->freq)) {
183 if (opts->strict_freq) {
184 pr_err("error: Maximum frequency rate (%'u Hz) exceeded.\n"
185 " Please use -F freq option with a lower value or consider\n"
186 " tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
187 max_rate);
188 return -1;
189 } else {
190 pr_warning("warning: Maximum frequency rate (%'u Hz) exceeded, throttling from %'u Hz to %'u Hz.\n"
191 " The limit can be raised via /proc/sys/kernel/perf_event_max_sample_rate.\n"
192 " The kernel will lower it when perf's interrupts take too long.\n"
193 " Use --strict-freq to disable this throttling, refusing to record.\n",
194 max_rate, opts->freq, max_rate);
195
196 opts->freq = max_rate;
197 }
198 }
199
200 /*
201 * Default frequency is over current maximum.
202 */
203 if (max_rate < opts->freq) {
204 pr_warning("Lowering default frequency rate to %u.\n"
205 "Please consider tweaking "
206 "/proc/sys/kernel/perf_event_max_sample_rate.\n",
207 max_rate);
208 opts->freq = max_rate;
209 }
210
211 return 0;
212}
213
214int record_opts__config(struct record_opts *opts)
215{
216 return record_opts__config_freq(opts);
217}
218
219bool perf_evlist__can_select_event(struct evlist *evlist, const char *str)
220{
221 struct evlist *temp_evlist;
222 struct evsel *evsel;
223 int err, fd, cpu;
224 bool ret = false;
225 pid_t pid = -1;
226
227 temp_evlist = evlist__new();
228 if (!temp_evlist)
229 return false;
230
231 err = parse_events(temp_evlist, str, NULL);
232 if (err)
233 goto out_delete;
234
235 evsel = evlist__last(temp_evlist);
236
237 if (!evlist || perf_cpu_map__empty(evlist->core.cpus)) {
238 struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
239
240 cpu = cpus ? cpus->map[0] : 0;
241 perf_cpu_map__put(cpus);
242 } else {
243 cpu = evlist->core.cpus->map[0];
244 }
245
246 while (1) {
247 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, -1,
248 perf_event_open_cloexec_flag());
249 if (fd < 0) {
250 if (pid == -1 && errno == EACCES) {
251 pid = 0;
252 continue;
253 }
254 goto out_delete;
255 }
256 break;
257 }
258 close(fd);
259 ret = true;
260
261out_delete:
262 evlist__delete(temp_evlist);
263 return ret;
264}
265
266int record__parse_freq(const struct option *opt, const char *str, int unset __maybe_unused)
267{
268 unsigned int freq;
269 struct record_opts *opts = opt->value;
270
271 if (!str)
272 return -EINVAL;
273
274 if (strcasecmp(str, "max") == 0) {
275 if (get_max_rate(&freq)) {
276 pr_err("couldn't read /proc/sys/kernel/perf_event_max_sample_rate\n");
277 return -1;
278 }
279 pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq);
280 } else {
281 freq = atoi(str);
282 }
283
284 opts->user_freq = freq;
285 return 0;
286}