1 // SPDX-License-Identifier: GPL-2.0
3 * Arm Statistical Profiling Extensions (SPE) support
4 * Copyright (c) 2017-2018, Arm Ltd.
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/bitops.h>
10 #include <linux/log2.h>
11 #include <linux/zalloc.h>
14 #include "../../../util/cpumap.h"
15 #include "../../../util/event.h"
16 #include "../../../util/evsel.h"
17 #include "../../../util/evsel_config.h"
18 #include "../../../util/evlist.h"
19 #include "../../../util/session.h"
20 #include <internal/lib.h> // page_size
21 #include "../../../util/pmu.h"
22 #include "../../../util/debug.h"
23 #include "../../../util/auxtrace.h"
24 #include "../../../util/record.h"
25 #include "../../../util/arm-spe.h"
26 #include <tools/libc_compat.h> // reallocarray
28 #define KiB(x) ((x) * 1024)
29 #define MiB(x) ((x) * 1024 * 1024)
31 struct arm_spe_recording {
32 struct auxtrace_record itr;
33 struct perf_pmu *arm_spe_pmu;
34 struct evlist *evlist;
39 static void arm_spe_set_timestamp(struct auxtrace_record *itr,
42 struct arm_spe_recording *ptr;
43 struct perf_pmu *arm_spe_pmu;
44 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
45 u64 user_bits = 0, bit;
47 ptr = container_of(itr, struct arm_spe_recording, itr);
48 arm_spe_pmu = ptr->arm_spe_pmu;
51 user_bits = term->val.cfg_chg;
53 bit = perf_pmu__format_bits(&arm_spe_pmu->format, "ts_enable");
55 /* Skip if user has set it */
59 evsel->core.attr.config |= bit;
63 arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,
64 struct evlist *evlist __maybe_unused)
66 return ARM_SPE_AUXTRACE_PRIV_SIZE;
69 static int arm_spe_info_fill(struct auxtrace_record *itr,
70 struct perf_session *session,
71 struct perf_record_auxtrace_info *auxtrace_info,
74 struct arm_spe_recording *sper =
75 container_of(itr, struct arm_spe_recording, itr);
76 struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
78 if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
81 if (!session->evlist->core.nr_mmaps)
84 auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
85 auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
91 arm_spe_snapshot_resolve_auxtrace_defaults(struct record_opts *opts,
95 * The default snapshot size is the auxtrace mmap size. If neither auxtrace mmap size nor
96 * snapshot size is specified, then the default is 4MiB for privileged users, 128KiB for
99 * The default auxtrace mmap size is 4MiB/page_size for privileged users, 128KiB for
100 * unprivileged users. If an unprivileged user does not specify mmap pages, the mmap pages
101 * will be reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the
102 * user is likely to get an error as they exceed their mlock limmit.
106 * No size were given to '-S' or '-m,', so go with the default
108 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
110 opts->auxtrace_mmap_pages = MiB(4) / page_size;
112 opts->auxtrace_mmap_pages = KiB(128) / page_size;
113 if (opts->mmap_pages == UINT_MAX)
114 opts->mmap_pages = KiB(256) / page_size;
116 } else if (!opts->auxtrace_mmap_pages && !privileged && opts->mmap_pages == UINT_MAX) {
117 opts->mmap_pages = KiB(256) / page_size;
121 * '-m,xyz' was specified but no snapshot size, so make the snapshot size as big as the
122 * auxtrace mmap area.
124 if (!opts->auxtrace_snapshot_size)
125 opts->auxtrace_snapshot_size = opts->auxtrace_mmap_pages * (size_t)page_size;
128 * '-Sxyz' was specified but no auxtrace mmap area, so make the auxtrace mmap area big
129 * enough to fit the requested snapshot size.
131 if (!opts->auxtrace_mmap_pages) {
132 size_t sz = opts->auxtrace_snapshot_size;
134 sz = round_up(sz, page_size) / page_size;
135 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
139 static int arm_spe_recording_options(struct auxtrace_record *itr,
140 struct evlist *evlist,
141 struct record_opts *opts)
143 struct arm_spe_recording *sper =
144 container_of(itr, struct arm_spe_recording, itr);
145 struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
146 struct evsel *evsel, *arm_spe_evsel = NULL;
147 struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
148 bool privileged = perf_event_paranoid_check(-1);
149 struct evsel *tracking_evsel;
152 sper->evlist = evlist;
154 evlist__for_each_entry(evlist, evsel) {
155 if (evsel->core.attr.type == arm_spe_pmu->type) {
157 pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
160 evsel->core.attr.freq = 0;
161 evsel->core.attr.sample_period = arm_spe_pmu->default_config->sample_period;
162 arm_spe_evsel = evsel;
163 opts->full_auxtrace = true;
167 if (!opts->full_auxtrace)
171 * we are in snapshot mode.
173 if (opts->auxtrace_snapshot_mode) {
175 * Command arguments '-Sxyz' and/or '-m,xyz' are missing, so fill those in with
178 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages)
179 arm_spe_snapshot_resolve_auxtrace_defaults(opts, privileged);
182 * Snapshot size can't be bigger than the auxtrace area.
184 if (opts->auxtrace_snapshot_size > opts->auxtrace_mmap_pages * (size_t)page_size) {
185 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
186 opts->auxtrace_snapshot_size,
187 opts->auxtrace_mmap_pages * (size_t)page_size);
192 * Something went wrong somewhere - this shouldn't happen.
194 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
195 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
200 /* We are in full trace mode but '-m,xyz' wasn't specified */
201 if (!opts->auxtrace_mmap_pages) {
203 opts->auxtrace_mmap_pages = MiB(4) / page_size;
205 opts->auxtrace_mmap_pages = KiB(128) / page_size;
206 if (opts->mmap_pages == UINT_MAX)
207 opts->mmap_pages = KiB(256) / page_size;
211 /* Validate auxtrace_mmap_pages */
212 if (opts->auxtrace_mmap_pages) {
213 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
214 size_t min_sz = KiB(8);
216 if (sz < min_sz || !is_power_of_2(sz)) {
217 pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
223 if (opts->auxtrace_snapshot_mode)
224 pr_debug2("%sx snapshot size: %zu\n", ARM_SPE_PMU_NAME,
225 opts->auxtrace_snapshot_size);
228 * To obtain the auxtrace buffer file descriptor, the auxtrace event
231 evlist__to_front(evlist, arm_spe_evsel);
234 * In the case of per-cpu mmaps, sample CPU for AUX event;
235 * also enable the timestamp tracing for samples correlation.
237 if (!perf_cpu_map__empty(cpus)) {
238 evsel__set_sample_bit(arm_spe_evsel, CPU);
239 arm_spe_set_timestamp(itr, arm_spe_evsel);
242 /* Add dummy event to keep tracking */
243 err = parse_events(evlist, "dummy:u", NULL);
247 tracking_evsel = evlist__last(evlist);
248 evlist__set_tracking_event(evlist, tracking_evsel);
250 tracking_evsel->core.attr.freq = 0;
251 tracking_evsel->core.attr.sample_period = 1;
253 /* In per-cpu case, always need the time of mmap events etc */
254 if (!perf_cpu_map__empty(cpus)) {
255 evsel__set_sample_bit(tracking_evsel, TIME);
256 evsel__set_sample_bit(tracking_evsel, CPU);
258 /* also track task context switch */
259 if (!record_opts__no_switch_events(opts))
260 tracking_evsel->core.attr.context_switch = 1;
266 static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
267 struct record_opts *opts,
270 unsigned long long snapshot_size = 0;
274 snapshot_size = strtoull(str, &endptr, 0);
275 if (*endptr || snapshot_size > SIZE_MAX)
279 opts->auxtrace_snapshot_mode = true;
280 opts->auxtrace_snapshot_size = snapshot_size;
285 static int arm_spe_snapshot_start(struct auxtrace_record *itr)
287 struct arm_spe_recording *ptr =
288 container_of(itr, struct arm_spe_recording, itr);
291 evlist__for_each_entry(ptr->evlist, evsel) {
292 if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
293 return evsel__disable(evsel);
298 static int arm_spe_snapshot_finish(struct auxtrace_record *itr)
300 struct arm_spe_recording *ptr =
301 container_of(itr, struct arm_spe_recording, itr);
304 evlist__for_each_entry(ptr->evlist, evsel) {
305 if (evsel->core.attr.type == ptr->arm_spe_pmu->type)
306 return evsel__enable(evsel);
311 static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx)
314 int cnt = ptr->wrapped_cnt, new_cnt, i;
317 * No need to allocate, so return early.
323 * Make ptr->wrapped as big as idx.
328 * Free'ed in arm_spe_recording_free().
330 wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool));
335 * init new allocated values.
337 for (i = cnt; i < new_cnt; i++)
340 ptr->wrapped_cnt = new_cnt;
341 ptr->wrapped = wrapped;
346 static bool arm_spe_buffer_has_wrapped(unsigned char *buffer,
347 size_t buffer_size, u64 head)
350 u64 *buf = (u64 *)buffer;
351 size_t buf_size = buffer_size;
354 * Defensively handle the case where head might be continually increasing - if its value is
355 * equal or greater than the size of the ring buffer, then we can safely determine it has
356 * wrapped around. Otherwise, continue to detect if head might have wrapped.
358 if (head >= buffer_size)
362 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer.
364 watermark = buf_size - 512;
367 * The value of head is somewhere within the size of the ring buffer. This can be that there
368 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that
369 * head has numerically wrapped around. To find we need to check if we have data at the
370 * very end of the ring buffer. We can reliably do this because mmap'ed pages are zeroed
371 * out and there is a fresh mapping with every new session.
375 * head is less than 512 byte from the end of the ring buffer.
377 if (head > watermark)
381 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
383 watermark /= sizeof(u64);
384 buf_size /= sizeof(u64);
387 * If we find trace data at the end of the ring buffer, head has been there and has
388 * numerically wrapped around at least once.
390 for (i = watermark; i < buf_size; i++)
397 static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx,
398 struct auxtrace_mmap *mm, unsigned char *data,
403 struct arm_spe_recording *ptr =
404 container_of(itr, struct arm_spe_recording, itr);
407 * Allocate memory to keep track of wrapping if this is the first
408 * time we deal with this *mm.
410 if (idx >= ptr->wrapped_cnt) {
411 err = arm_spe_alloc_wrapped_array(ptr, idx);
417 * Check to see if *head has wrapped around. If it hasn't only the
418 * amount of data between *head and *old is snapshot'ed to avoid
419 * bloating the perf.data file with zeros. But as soon as *head has
420 * wrapped around the entire size of the AUX ring buffer it taken.
422 wrapped = ptr->wrapped[idx];
423 if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) {
425 ptr->wrapped[idx] = true;
428 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
429 __func__, idx, (size_t)*old, (size_t)*head, mm->len);
432 * No wrap has occurred, we can just use *head and *old.
438 * *head has wrapped around - adjust *head and *old to pickup the
439 * entire content of the AUX buffer.
441 if (*head >= mm->len) {
442 *old = *head - mm->len;
445 *old = *head - mm->len;
451 static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
455 clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
457 return ts.tv_sec ^ ts.tv_nsec;
460 static void arm_spe_recording_free(struct auxtrace_record *itr)
462 struct arm_spe_recording *sper =
463 container_of(itr, struct arm_spe_recording, itr);
469 struct auxtrace_record *arm_spe_recording_init(int *err,
470 struct perf_pmu *arm_spe_pmu)
472 struct arm_spe_recording *sper;
479 sper = zalloc(sizeof(struct arm_spe_recording));
485 sper->arm_spe_pmu = arm_spe_pmu;
486 sper->itr.pmu = arm_spe_pmu;
487 sper->itr.snapshot_start = arm_spe_snapshot_start;
488 sper->itr.snapshot_finish = arm_spe_snapshot_finish;
489 sper->itr.find_snapshot = arm_spe_find_snapshot;
490 sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options;
491 sper->itr.recording_options = arm_spe_recording_options;
492 sper->itr.info_priv_size = arm_spe_info_priv_size;
493 sper->itr.info_fill = arm_spe_info_fill;
494 sper->itr.free = arm_spe_recording_free;
495 sper->itr.reference = arm_spe_reference;
496 sper->itr.read_finish = auxtrace_record__read_finish;
497 sper->itr.alignment = 0;
503 struct perf_event_attr
504 *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
506 struct perf_event_attr *attr;
508 attr = zalloc(sizeof(struct perf_event_attr));
510 pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
515 * If kernel driver doesn't advertise a minimum,
516 * use max allowable by PMSIDR_EL1.INTERVAL
518 if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
519 &attr->sample_period) != 1) {
520 pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
521 attr->sample_period = 4096;
524 arm_spe_pmu->selectable = true;
525 arm_spe_pmu->is_uncore = false;