brintos

brintos / linux-shallow public Read only

0
0
Text · 14.1 KiB · 2be99fd Raw
538 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * Arm Statistical Profiling Extensions (SPE) support4 * Copyright (c) 2017-2018, Arm Ltd.5 */6 7#include <linux/kernel.h>8#include <linux/types.h>9#include <linux/bitops.h>10#include <linux/log2.h>11#include <linux/string.h>12#include <linux/zalloc.h>13#include <time.h>14 15#include "../../../util/cpumap.h"16#include "../../../util/event.h"17#include "../../../util/evsel.h"18#include "../../../util/evsel_config.h"19#include "../../../util/evlist.h"20#include "../../../util/session.h"21#include <internal/lib.h> // page_size22#include "../../../util/pmu.h"23#include "../../../util/debug.h"24#include "../../../util/auxtrace.h"25#include "../../../util/record.h"26#include "../../../util/arm-spe.h"27#include <tools/libc_compat.h> // reallocarray28 29#define KiB(x) ((x) * 1024)30#define MiB(x) ((x) * 1024 * 1024)31 32struct arm_spe_recording {33	struct auxtrace_record		itr;34	struct perf_pmu			*arm_spe_pmu;35	struct evlist		*evlist;36	int			wrapped_cnt;37	bool			*wrapped;38};39 40static size_t41arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,42		       struct evlist *evlist __maybe_unused)43{44	return ARM_SPE_AUXTRACE_PRIV_SIZE;45}46 47static int arm_spe_info_fill(struct auxtrace_record *itr,48			     struct perf_session *session,49			     struct perf_record_auxtrace_info *auxtrace_info,50			     size_t priv_size)51{52	struct arm_spe_recording *sper =53			container_of(itr, struct arm_spe_recording, itr);54	struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;55 56	if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)57		return -EINVAL;58 59	if (!session->evlist->core.nr_mmaps)60		return -EINVAL;61 62	auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;63	auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;64 65	return 0;66}67 68static void69arm_spe_snapshot_resolve_auxtrace_defaults(struct record_opts *opts,70					   bool privileged)71{72	/*73	 * The default snapshot size is the auxtrace mmap size. If neither auxtrace mmap size nor74	 * snapshot size is specified, then the default is 4MiB for privileged users, 128KiB for75	 * unprivileged users.76	 *77	 * The default auxtrace mmap size is 4MiB/page_size for privileged users, 128KiB for78	 * unprivileged users. If an unprivileged user does not specify mmap pages, the mmap pages79	 * will be reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the80	 * user is likely to get an error as they exceed their mlock limmit.81	 */82 83	/*84	 * No size were given to '-S' or '-m,', so go with the default85	 */86	if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {87		if (privileged) {88			opts->auxtrace_mmap_pages = MiB(4) / page_size;89		} else {90			opts->auxtrace_mmap_pages = KiB(128) / page_size;91			if (opts->mmap_pages == UINT_MAX)92				opts->mmap_pages = KiB(256) / page_size;93		}94	} else if (!opts->auxtrace_mmap_pages && !privileged && opts->mmap_pages == UINT_MAX) {95		opts->mmap_pages = KiB(256) / page_size;96	}97 98	/*99	 * '-m,xyz' was specified but no snapshot size, so make the snapshot size as big as the100	 * auxtrace mmap area.101	 */102	if (!opts->auxtrace_snapshot_size)103		opts->auxtrace_snapshot_size = opts->auxtrace_mmap_pages * (size_t)page_size;104 105	/*106	 * '-Sxyz' was specified but no auxtrace mmap area, so make the auxtrace mmap area big107	 * enough to fit the requested snapshot size.108	 */109	if (!opts->auxtrace_mmap_pages) {110		size_t sz = opts->auxtrace_snapshot_size;111 112		sz = round_up(sz, page_size) / page_size;113		opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);114	}115}116 117static __u64 arm_spe_pmu__sample_period(const struct perf_pmu *arm_spe_pmu)118{119	static __u64 sample_period;120 121	if (sample_period)122		return sample_period;123 124	/*125	 * If kernel driver doesn't advertise a minimum,126	 * use max allowable by PMSIDR_EL1.INTERVAL127	 */128	if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",129				&sample_period) != 1) {130		pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");131		sample_period = 4096;132	}133	return sample_period;134}135 136static void arm_spe_setup_evsel(struct evsel *evsel, struct perf_cpu_map *cpus)137{138	u64 bit;139 140	evsel->core.attr.freq = 0;141	evsel->core.attr.sample_period = arm_spe_pmu__sample_period(evsel->pmu);142	evsel->needs_auxtrace_mmap = true;143 144	/*145	 * To obtain the auxtrace buffer file descriptor, the auxtrace event146	 * must come first.147	 */148	evlist__to_front(evsel->evlist, evsel);149 150	/*151	 * In the case of per-cpu mmaps, sample CPU for AUX event;152	 * also enable the timestamp tracing for samples correlation.153	 */154	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {155		evsel__set_sample_bit(evsel, CPU);156		evsel__set_config_if_unset(evsel->pmu, evsel, "ts_enable", 1);157	}158 159	/*160	 * Set this only so that perf report knows that SPE generates memory info. It has no effect161	 * on the opening of the event or the SPE data produced.162	 */163	evsel__set_sample_bit(evsel, DATA_SRC);164 165	/*166	 * The PHYS_ADDR flag does not affect the driver behaviour, it is used to167	 * inform that the resulting output's SPE samples contain physical addresses168	 * where applicable.169	 */170	bit = perf_pmu__format_bits(evsel->pmu, "pa_enable");171	if (evsel->core.attr.config & bit)172		evsel__set_sample_bit(evsel, PHYS_ADDR);173}174 175static int arm_spe_recording_options(struct auxtrace_record *itr,176				     struct evlist *evlist,177				     struct record_opts *opts)178{179	struct arm_spe_recording *sper =180			container_of(itr, struct arm_spe_recording, itr);181	struct evsel *evsel, *tmp;182	struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;183	bool privileged = perf_event_paranoid_check(-1);184	struct evsel *tracking_evsel;185	int err;186 187	sper->evlist = evlist;188 189	evlist__for_each_entry(evlist, evsel) {190		if (evsel__is_aux_event(evsel)) {191			if (!strstarts(evsel->pmu_name, ARM_SPE_PMU_NAME)) {192				pr_err("Found unexpected auxtrace event: %s\n",193				       evsel->pmu_name);194				return -EINVAL;195			}196			opts->full_auxtrace = true;197		}198	}199 200	if (!opts->full_auxtrace)201		return 0;202 203	/*204	 * we are in snapshot mode.205	 */206	if (opts->auxtrace_snapshot_mode) {207		/*208		 * Command arguments '-Sxyz' and/or '-m,xyz' are missing, so fill those in with209		 * default values.210		 */211		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages)212			arm_spe_snapshot_resolve_auxtrace_defaults(opts, privileged);213 214		/*215		 * Snapshot size can't be bigger than the auxtrace area.216		 */217		if (opts->auxtrace_snapshot_size > opts->auxtrace_mmap_pages * (size_t)page_size) {218			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",219			       opts->auxtrace_snapshot_size,220			       opts->auxtrace_mmap_pages * (size_t)page_size);221			return -EINVAL;222		}223 224		/*225		 * Something went wrong somewhere - this shouldn't happen.226		 */227		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {228			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");229			return -EINVAL;230		}231	}232 233	/* We are in full trace mode but '-m,xyz' wasn't specified */234	if (!opts->auxtrace_mmap_pages) {235		if (privileged) {236			opts->auxtrace_mmap_pages = MiB(4) / page_size;237		} else {238			opts->auxtrace_mmap_pages = KiB(128) / page_size;239			if (opts->mmap_pages == UINT_MAX)240				opts->mmap_pages = KiB(256) / page_size;241		}242	}243 244	/* Validate auxtrace_mmap_pages */245	if (opts->auxtrace_mmap_pages) {246		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;247		size_t min_sz = KiB(8);248 249		if (sz < min_sz || !is_power_of_2(sz)) {250			pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",251			       min_sz / 1024);252			return -EINVAL;253		}254	}255 256	if (opts->auxtrace_snapshot_mode)257		pr_debug2("%sx snapshot size: %zu\n", ARM_SPE_PMU_NAME,258			  opts->auxtrace_snapshot_size);259 260	evlist__for_each_entry_safe(evlist, tmp, evsel) {261		if (evsel__is_aux_event(evsel))262			arm_spe_setup_evsel(evsel, cpus);263	}264 265	/* Add dummy event to keep tracking */266	err = parse_event(evlist, "dummy:u");267	if (err)268		return err;269 270	tracking_evsel = evlist__last(evlist);271	evlist__set_tracking_event(evlist, tracking_evsel);272 273	tracking_evsel->core.attr.freq = 0;274	tracking_evsel->core.attr.sample_period = 1;275 276	/* In per-cpu case, always need the time of mmap events etc */277	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {278		evsel__set_sample_bit(tracking_evsel, TIME);279		evsel__set_sample_bit(tracking_evsel, CPU);280 281		/* also track task context switch */282		if (!record_opts__no_switch_events(opts))283			tracking_evsel->core.attr.context_switch = 1;284	}285 286	return 0;287}288 289static int arm_spe_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,290					 struct record_opts *opts,291					 const char *str)292{293	unsigned long long snapshot_size = 0;294	char *endptr;295 296	if (str) {297		snapshot_size = strtoull(str, &endptr, 0);298		if (*endptr || snapshot_size > SIZE_MAX)299			return -1;300	}301 302	opts->auxtrace_snapshot_mode = true;303	opts->auxtrace_snapshot_size = snapshot_size;304 305	return 0;306}307 308static int arm_spe_snapshot_start(struct auxtrace_record *itr)309{310	struct arm_spe_recording *ptr =311			container_of(itr, struct arm_spe_recording, itr);312	struct evsel *evsel;313	int ret = -EINVAL;314 315	evlist__for_each_entry(ptr->evlist, evsel) {316		if (evsel__is_aux_event(evsel)) {317			ret = evsel__disable(evsel);318			if (ret < 0)319				return ret;320		}321	}322	return ret;323}324 325static int arm_spe_snapshot_finish(struct auxtrace_record *itr)326{327	struct arm_spe_recording *ptr =328			container_of(itr, struct arm_spe_recording, itr);329	struct evsel *evsel;330	int ret = -EINVAL;331 332	evlist__for_each_entry(ptr->evlist, evsel) {333		if (evsel__is_aux_event(evsel)) {334			ret = evsel__enable(evsel);335			if (ret < 0)336				return ret;337		}338	}339	return ret;340}341 342static int arm_spe_alloc_wrapped_array(struct arm_spe_recording *ptr, int idx)343{344	bool *wrapped;345	int cnt = ptr->wrapped_cnt, new_cnt, i;346 347	/*348	 * No need to allocate, so return early.349	 */350	if (idx < cnt)351		return 0;352 353	/*354	 * Make ptr->wrapped as big as idx.355	 */356	new_cnt = idx + 1;357 358	/*359	 * Free'ed in arm_spe_recording_free().360	 */361	wrapped = reallocarray(ptr->wrapped, new_cnt, sizeof(bool));362	if (!wrapped)363		return -ENOMEM;364 365	/*366	 * init new allocated values.367	 */368	for (i = cnt; i < new_cnt; i++)369		wrapped[i] = false;370 371	ptr->wrapped_cnt = new_cnt;372	ptr->wrapped = wrapped;373 374	return 0;375}376 377static bool arm_spe_buffer_has_wrapped(unsigned char *buffer,378				      size_t buffer_size, u64 head)379{380	u64 i, watermark;381	u64 *buf = (u64 *)buffer;382	size_t buf_size = buffer_size;383 384	/*385	 * Defensively handle the case where head might be continually increasing - if its value is386	 * equal or greater than the size of the ring buffer, then we can safely determine it has387	 * wrapped around. Otherwise, continue to detect if head might have wrapped.388	 */389	if (head >= buffer_size)390		return true;391 392	/*393	 * We want to look the very last 512 byte (chosen arbitrarily) in the ring buffer.394	 */395	watermark = buf_size - 512;396 397	/*398	 * The value of head is somewhere within the size of the ring buffer. This can be that there399	 * hasn't been enough data to fill the ring buffer yet or the trace time was so long that400	 * head has numerically wrapped around.  To find we need to check if we have data at the401	 * very end of the ring buffer.  We can reliably do this because mmap'ed pages are zeroed402	 * out and there is a fresh mapping with every new session.403	 */404 405	/*406	 * head is less than 512 byte from the end of the ring buffer.407	 */408	if (head > watermark)409		watermark = head;410 411	/*412	 * Speed things up by using 64 bit transactions (see "u64 *buf" above)413	 */414	watermark /= sizeof(u64);415	buf_size /= sizeof(u64);416 417	/*418	 * If we find trace data at the end of the ring buffer, head has been there and has419	 * numerically wrapped around at least once.420	 */421	for (i = watermark; i < buf_size; i++)422		if (buf[i])423			return true;424 425	return false;426}427 428static int arm_spe_find_snapshot(struct auxtrace_record *itr, int idx,429				  struct auxtrace_mmap *mm, unsigned char *data,430				  u64 *head, u64 *old)431{432	int err;433	bool wrapped;434	struct arm_spe_recording *ptr =435			container_of(itr, struct arm_spe_recording, itr);436 437	/*438	 * Allocate memory to keep track of wrapping if this is the first439	 * time we deal with this *mm.440	 */441	if (idx >= ptr->wrapped_cnt) {442		err = arm_spe_alloc_wrapped_array(ptr, idx);443		if (err)444			return err;445	}446 447	/*448	 * Check to see if *head has wrapped around.  If it hasn't only the449	 * amount of data between *head and *old is snapshot'ed to avoid450	 * bloating the perf.data file with zeros.  But as soon as *head has451	 * wrapped around the entire size of the AUX ring buffer it taken.452	 */453	wrapped = ptr->wrapped[idx];454	if (!wrapped && arm_spe_buffer_has_wrapped(data, mm->len, *head)) {455		wrapped = true;456		ptr->wrapped[idx] = true;457	}458 459	pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",460		  __func__, idx, (size_t)*old, (size_t)*head, mm->len);461 462	/*463	 * No wrap has occurred, we can just use *head and *old.464	 */465	if (!wrapped)466		return 0;467 468	/*469	 * *head has wrapped around - adjust *head and *old to pickup the470	 * entire content of the AUX buffer.471	 */472	if (*head >= mm->len) {473		*old = *head - mm->len;474	} else {475		*head += mm->len;476		*old = *head - mm->len;477	}478 479	return 0;480}481 482static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)483{484	struct timespec ts;485 486	clock_gettime(CLOCK_MONOTONIC_RAW, &ts);487 488	return ts.tv_sec ^ ts.tv_nsec;489}490 491static void arm_spe_recording_free(struct auxtrace_record *itr)492{493	struct arm_spe_recording *sper =494			container_of(itr, struct arm_spe_recording, itr);495 496	zfree(&sper->wrapped);497	free(sper);498}499 500struct auxtrace_record *arm_spe_recording_init(int *err,501					       struct perf_pmu *arm_spe_pmu)502{503	struct arm_spe_recording *sper;504 505	if (!arm_spe_pmu) {506		*err = -ENODEV;507		return NULL;508	}509 510	sper = zalloc(sizeof(struct arm_spe_recording));511	if (!sper) {512		*err = -ENOMEM;513		return NULL;514	}515 516	sper->arm_spe_pmu = arm_spe_pmu;517	sper->itr.snapshot_start = arm_spe_snapshot_start;518	sper->itr.snapshot_finish = arm_spe_snapshot_finish;519	sper->itr.find_snapshot = arm_spe_find_snapshot;520	sper->itr.parse_snapshot_options = arm_spe_parse_snapshot_options;521	sper->itr.recording_options = arm_spe_recording_options;522	sper->itr.info_priv_size = arm_spe_info_priv_size;523	sper->itr.info_fill = arm_spe_info_fill;524	sper->itr.free = arm_spe_recording_free;525	sper->itr.reference = arm_spe_reference;526	sper->itr.read_finish = auxtrace_record__read_finish;527	sper->itr.alignment = 0;528 529	*err = 0;530	return &sper->itr;531}532 533void534arm_spe_pmu_default_config(const struct perf_pmu *arm_spe_pmu, struct perf_event_attr *attr)535{536	attr->sample_period = arm_spe_pmu__sample_period(arm_spe_pmu);537}538