1 // SPDX-License-Identifier: GPL-2.0
5 #include "parse-events.h"
8 #include <subcmd/parse-options.h>
12 typedef void (*setup_probe_fn_t
)(struct perf_evsel
*evsel
);
14 static int perf_do_probe_api(setup_probe_fn_t fn
, int cpu
, const char *str
)
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;
22 evlist
= perf_evlist__new();
26 if (parse_events(evlist
, str
, NULL
))
29 evsel
= perf_evlist__first(evlist
);
32 fd
= sys_perf_event_open(&evsel
->attr
, pid
, cpu
, -1, flags
);
34 if (pid
== -1 && errno
== EACCES
) {
46 fd
= sys_perf_event_open(&evsel
->attr
, pid
, cpu
, -1, flags
);
56 perf_evlist__delete(evlist
);
60 static bool perf_probe_api(setup_probe_fn_t fn
)
62 const char *try[] = {"cycles:u", "instructions:u", "cpu-clock:u", NULL
};
66 cpus
= cpu_map__new(NULL
);
73 ret
= perf_do_probe_api(fn
, cpu
, try[i
++]);
76 } while (ret
== -EAGAIN
&& try[i
]);
81 static void perf_probe_sample_identifier(struct perf_evsel
*evsel
)
83 evsel
->attr
.sample_type
|= PERF_SAMPLE_IDENTIFIER
;
86 static void perf_probe_comm_exec(struct perf_evsel
*evsel
)
88 evsel
->attr
.comm_exec
= 1;
91 static void perf_probe_context_switch(struct perf_evsel
*evsel
)
93 evsel
->attr
.context_switch
= 1;
96 bool perf_can_sample_identifier(void)
98 return perf_probe_api(perf_probe_sample_identifier
);
101 static bool perf_can_comm_exec(void)
103 return perf_probe_api(perf_probe_comm_exec
);
106 bool perf_can_record_switch_events(void)
108 return perf_probe_api(perf_probe_context_switch
);
111 bool perf_can_record_cpu_wide(void)
113 struct perf_event_attr attr
= {
114 .type
= PERF_TYPE_SOFTWARE
,
115 .config
= PERF_COUNT_SW_CPU_CLOCK
,
118 struct cpu_map
*cpus
;
121 cpus
= cpu_map__new(NULL
);
127 fd
= sys_perf_event_open(&attr
, -1, cpu
, -1, 0);
135 void perf_evlist__config(struct perf_evlist
*evlist
, struct record_opts
*opts
,
136 struct callchain_param
*callchain
)
138 struct perf_evsel
*evsel
;
139 bool use_sample_identifier
= false;
141 bool sample_id
= opts
->sample_id
;
144 * Set the evsel leader links before we configure attributes,
145 * since some might depend on this info.
148 perf_evlist__set_leader(evlist
);
150 if (evlist
->cpus
->map
[0] < 0)
151 opts
->no_inherit
= true;
153 use_comm_exec
= perf_can_comm_exec();
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;
161 if (opts
->full_auxtrace
) {
163 * Need to be able to synthesize and parse selected events with
164 * arbitrary sample types, which requires always being able to
167 use_sample_identifier
= perf_can_sample_identifier();
169 } else if (evlist
->nr_entries
> 1) {
170 struct perf_evsel
*first
= perf_evlist__first(evlist
);
172 evlist__for_each_entry(evlist
, evsel
) {
173 if (evsel
->attr
.sample_type
== first
->attr
.sample_type
)
175 use_sample_identifier
= perf_can_sample_identifier();
182 evlist__for_each_entry(evlist
, evsel
)
183 perf_evsel__set_sample_id(evsel
, use_sample_identifier
);
186 perf_evlist__set_id_pos(evlist
);
189 static int get_max_rate(unsigned int *rate
)
191 return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate
);
194 static int record_opts__config_freq(struct record_opts
*opts
)
196 bool user_freq
= opts
->user_freq
!= UINT_MAX
;
197 unsigned int max_rate
;
199 if (opts
->user_interval
!= ULLONG_MAX
)
200 opts
->default_interval
= opts
->user_interval
;
202 opts
->freq
= opts
->user_freq
;
205 * User specified count overrides default frequency.
207 if (opts
->default_interval
)
209 else if (opts
->freq
) {
210 opts
->default_interval
= opts
->freq
;
212 pr_err("frequency and count are zero, aborting\n");
216 if (get_max_rate(&max_rate
))
220 * User specified frequency is over current maximum.
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",
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
);
236 opts
->freq
= max_rate
;
241 * Default frequency is over current maximum.
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",
248 opts
->freq
= max_rate
;
254 int record_opts__config(struct record_opts
*opts
)
256 return record_opts__config_freq(opts
);
259 bool perf_evlist__can_select_event(struct perf_evlist
*evlist
, const char *str
)
261 struct perf_evlist
*temp_evlist
;
262 struct perf_evsel
*evsel
;
267 temp_evlist
= perf_evlist__new();
271 err
= parse_events(temp_evlist
, str
, NULL
);
275 evsel
= perf_evlist__last(temp_evlist
);
277 if (!evlist
|| cpu_map__empty(evlist
->cpus
)) {
278 struct cpu_map
*cpus
= cpu_map__new(NULL
);
280 cpu
= cpus
? cpus
->map
[0] : 0;
283 cpu
= evlist
->cpus
->map
[0];
287 fd
= sys_perf_event_open(&evsel
->attr
, pid
, cpu
, -1,
288 perf_event_open_cloexec_flag());
290 if (pid
== -1 && errno
== EACCES
) {
302 perf_evlist__delete(temp_evlist
);
306 int record__parse_freq(const struct option
*opt
, const char *str
, int unset __maybe_unused
)
309 struct record_opts
*opts
= opt
->value
;
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");
319 pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq
);
324 opts
->user_freq
= freq
;