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