xref: /linux/tools/perf/util/s390-sample-raw.c (revision ec319c4d35523e12630631b3ae875758cfa3c6e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2019
4  * Author(s): Thomas Richter <tmricht@linux.ibm.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License (version 2 only)
8  * as published by the Free Software Foundation.
9  *
10  * Architecture specific trace_event function. Save event's bc000 raw data
11  * to file. File name is aux.ctr.## where ## stands for the CPU number the
12  * sample was taken from.
13  */
14 
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <inttypes.h>
19 
20 #include <sys/stat.h>
21 #include <linux/compiler.h>
22 #include <linux/err.h>
23 #include <asm/byteorder.h>
24 
25 #include "debug.h"
26 #include "session.h"
27 #include "evlist.h"
28 #include "color.h"
29 #include "hashmap.h"
30 #include "sample-raw.h"
31 #include "s390-cpumcf-kernel.h"
32 #include "util/pmu.h"
33 #include "util/sample.h"
34 
35 static size_t ctrset_size(struct cf_ctrset_entry *set)
36 {
37 	return sizeof(*set) + set->ctr * sizeof(u64);
38 }
39 
40 static bool ctrset_valid(struct cf_ctrset_entry *set)
41 {
42 	return set->def == S390_CPUMCF_DIAG_DEF;
43 }
44 
45 /* CPU Measurement Counter Facility raw data is a byte stream. It is 8 byte
46  * aligned and might have trailing padding bytes.
47  * Display the raw data on screen.
48  */
49 static bool s390_cpumcfdg_testctr(struct perf_sample *sample)
50 {
51 	size_t len = sample->raw_size, offset = 0;
52 	unsigned char *buf = sample->raw_data;
53 	struct cf_trailer_entry *te;
54 	struct cf_ctrset_entry *cep, ce;
55 
56 	while (offset < len) {
57 		cep = (struct cf_ctrset_entry *)(buf + offset);
58 		ce.def = be16_to_cpu(cep->def);
59 		ce.set = be16_to_cpu(cep->set);
60 		ce.ctr = be16_to_cpu(cep->ctr);
61 		ce.res1 = be16_to_cpu(cep->res1);
62 
63 		if (!ctrset_valid(&ce) || offset + ctrset_size(&ce) > len) {
64 			/* Raw data for counter sets are always multiple of 8
65 			 * bytes. Prepending a 4 bytes size field to the
66 			 * raw data block in the sample causes the perf tool
67 			 * to append 4 padding bytes to make the raw data part
68 			 * of the sample a multiple of eight bytes again.
69 			 *
70 			 * If the last entry (trailer) is 4 bytes off the raw
71 			 * area data end, all is good.
72 			 */
73 			if (len - offset - sizeof(*te) == 4)
74 				break;
75 			pr_err("Invalid counter set entry at %zd\n", offset);
76 			return false;
77 		}
78 		offset += ctrset_size(&ce);
79 	}
80 	return true;
81 }
82 
83 /* Dump event bc000 on screen, already tested on correctness. */
84 static void s390_cpumcfdg_dumptrail(const char *color, size_t offset,
85 				    struct cf_trailer_entry *tep)
86 {
87 	struct cf_trailer_entry  te;
88 
89 	te.flags = be64_to_cpu(tep->flags);
90 	te.cfvn = be16_to_cpu(tep->cfvn);
91 	te.csvn = be16_to_cpu(tep->csvn);
92 	te.cpu_speed = be32_to_cpu(tep->cpu_speed);
93 	te.timestamp = be64_to_cpu(tep->timestamp);
94 	te.progusage1 = be64_to_cpu(tep->progusage1);
95 	te.progusage2 = be64_to_cpu(tep->progusage2);
96 	te.progusage3 = be64_to_cpu(tep->progusage3);
97 	te.tod_base = be64_to_cpu(tep->tod_base);
98 	te.mach_type = be16_to_cpu(tep->mach_type);
99 	te.res1 = be16_to_cpu(tep->res1);
100 	te.res2 = be32_to_cpu(tep->res2);
101 
102 	color_fprintf(stdout, color, "    [%#08zx] Trailer:%c%c%c%c%c"
103 		      " Cfvn:%d Csvn:%d Speed:%d TOD:%#lx\n",
104 		      offset, te.clock_base ? 'T' : ' ',
105 		      te.speed ? 'S' : ' ', te.mtda ? 'M' : ' ',
106 		      te.caca ? 'C' : ' ', te.lcda ? 'L' : ' ',
107 		      te.cfvn, te.csvn, te.cpu_speed, te.timestamp);
108 	color_fprintf(stdout, color, "\t\t1:%lx 2:%lx 3:%lx TOD-Base:%#lx"
109 		      " Type:%x\n\n",
110 		      te.progusage1, te.progusage2, te.progusage3,
111 		      te.tod_base, te.mach_type);
112 }
113 
114 /* Return starting number of a counter set */
115 static int get_counterset_start(int setnr)
116 {
117 	switch (setnr) {
118 	case CPUMF_CTR_SET_BASIC:		/* Basic counter set */
119 		return 0;
120 	case CPUMF_CTR_SET_USER:		/* Problem state counter set */
121 		return 32;
122 	case CPUMF_CTR_SET_CRYPTO:		/* Crypto counter set */
123 		return 64;
124 	case CPUMF_CTR_SET_EXT:			/* Extended counter set */
125 		return 128;
126 	case CPUMF_CTR_SET_MT_DIAG:		/* Diagnostic counter set */
127 		return 448;
128 	case PERF_EVENT_PAI_NNPA_ALL:		/* PAI NNPA counter set */
129 	case PERF_EVENT_PAI_CRYPTO_ALL:		/* PAI CRYPTO counter set */
130 		return setnr;
131 	default:
132 		return -1;
133 	}
134 }
135 
136 struct get_counter_name_data {
137 	long wanted;
138 	const char *result;
139 };
140 
141 static int get_counter_name_callback(void *vdata, struct pmu_event_info *info)
142 {
143 	struct get_counter_name_data *data = vdata;
144 	int rc, event_nr;
145 	const char *event_str;
146 
147 	if (info->str == NULL)
148 		return 0;
149 
150 	event_str = strstr(info->str, "event=");
151 	if (!event_str)
152 		return 0;
153 
154 	rc = sscanf(event_str, "event=%x", &event_nr);
155 	if (rc == 1 && event_nr == data->wanted) {
156 		data->result = info->name;
157 		return 1; /* Terminate the search. */
158 	}
159 	return 0;
160 }
161 
162 static size_t get_counter_name_hash_fn(long key, void *ctx __maybe_unused)
163 {
164 	return key;
165 }
166 
167 static bool get_counter_name_hashmap_equal_fn(long key1, long key2, void *ctx __maybe_unused)
168 {
169 	return key1 == key2;
170 }
171 
172 /* Scan the PMU and extract the logical name of a counter from the event. Input
173  * is the counter set and counter number with in the set. Construct the event
174  * number and use this as key. If they match return the name of this counter.
175  * If no match is found a NULL pointer is returned.
176  */
177 static char *get_counter_name(int set, int nr, struct perf_pmu *pmu)
178 {
179 	static struct hashmap *cache;
180 	static struct perf_pmu *cache_pmu;
181 	long cache_key = get_counterset_start(set) + nr;
182 	struct get_counter_name_data data = {
183 		.wanted = cache_key,
184 		.result = NULL,
185 	};
186 	char *result = NULL;
187 
188 	if (!pmu)
189 		return NULL;
190 
191 	if (cache_pmu == pmu && hashmap__find(cache, cache_key, &result))
192 		return strdup(result);
193 
194 	perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ true,
195 				 &data, get_counter_name_callback);
196 
197 	result = strdup(data.result ?: "<unknown>");
198 
199 	if (cache_pmu == NULL) {
200 		struct hashmap *tmp = hashmap__new(get_counter_name_hash_fn,
201 						   get_counter_name_hashmap_equal_fn,
202 						   /*ctx=*/NULL);
203 
204 		if (!IS_ERR(tmp)) {
205 			cache = tmp;
206 			cache_pmu = pmu;
207 		}
208 	}
209 
210 	if (cache_pmu == pmu && result) {
211 		char *old_value = NULL, *new_value = strdup(result);
212 
213 		if (new_value) {
214 			hashmap__set(cache, cache_key, new_value, /*old_key=*/NULL, &old_value);
215 			/*
216 			 * Free in case of a race, but resizing would be broken
217 			 * in that case.
218 			 */
219 			free(old_value);
220 		}
221 	}
222 	return result;
223 }
224 
225 static void s390_cpumcfdg_dump(struct perf_sample *sample)
226 {
227 	struct perf_pmu *pmu = sample->evsel->pmu;
228 	size_t i, len = sample->raw_size, offset = 0;
229 	unsigned char *buf = sample->raw_data;
230 	const char *color = PERF_COLOR_BLUE;
231 	struct cf_ctrset_entry *cep, ce;
232 	u64 *p;
233 
234 	while (offset < len) {
235 		cep = (struct cf_ctrset_entry *)(buf + offset);
236 
237 		ce.def = be16_to_cpu(cep->def);
238 		ce.set = be16_to_cpu(cep->set);
239 		ce.ctr = be16_to_cpu(cep->ctr);
240 		ce.res1 = be16_to_cpu(cep->res1);
241 
242 		if (!ctrset_valid(&ce)) {	/* Print trailer */
243 			s390_cpumcfdg_dumptrail(color, offset,
244 						(struct cf_trailer_entry *)cep);
245 			return;
246 		}
247 
248 		color_fprintf(stdout, color, "    [%#08zx] Counterset:%d"
249 			      " Counters:%d\n", offset, ce.set, ce.ctr);
250 		for (i = 0, p = (u64 *)(cep + 1); i < ce.ctr; ++i, ++p) {
251 			char *ev_name = get_counter_name(ce.set, i, pmu);
252 
253 			color_fprintf(stdout, color,
254 				      "\tCounter:%03zd %s Value:%#018"PRIx64"\n", i,
255 				      ev_name ?: "<unknown>", be64_to_cpu(*p));
256 			free(ev_name);
257 		}
258 		offset += ctrset_size(&ce);
259 	}
260 }
261 
262 #pragma GCC diagnostic push
263 #pragma GCC diagnostic ignored "-Wpacked"
264 #pragma GCC diagnostic ignored "-Wattributes"
265 /*
266  * Check for consistency of PAI_CRYPTO/PAI_NNPA raw data.
267  */
268 struct pai_data {		/* Event number and value */
269 	u16 event_nr;
270 	u64 event_val;
271 } __packed;
272 
273 #pragma GCC diagnostic pop
274 
275 /*
276  * Test for valid raw data. At least one PAI event should be in the raw
277  * data section.
278  */
279 static bool s390_pai_all_test(struct perf_sample *sample)
280 {
281 	size_t len = sample->raw_size;
282 
283 	if (len < 0xa)
284 		return false;
285 	return true;
286 }
287 
288 static void s390_pai_all_dump(struct perf_sample *sample)
289 {
290 	struct evsel *evsel = sample->evsel;
291 	size_t len = sample->raw_size, offset = 0;
292 	unsigned char *p = sample->raw_data;
293 	const char *color = PERF_COLOR_BLUE;
294 	struct pai_data pai_data;
295 	char *ev_name;
296 
297 	while (offset < len) {
298 		memcpy(&pai_data.event_nr, p, sizeof(pai_data.event_nr));
299 		pai_data.event_nr = be16_to_cpu(pai_data.event_nr);
300 		p += sizeof(pai_data.event_nr);
301 		offset += sizeof(pai_data.event_nr);
302 
303 		memcpy(&pai_data.event_val, p, sizeof(pai_data.event_val));
304 		pai_data.event_val = be64_to_cpu(pai_data.event_val);
305 		p += sizeof(pai_data.event_val);
306 		offset += sizeof(pai_data.event_val);
307 
308 		ev_name = get_counter_name(evsel->core.attr.config,
309 					   pai_data.event_nr, evsel->pmu);
310 		color_fprintf(stdout, color, "\tCounter:%03d %s Value:%#018"PRIx64"\n",
311 			      pai_data.event_nr, ev_name ?: "<unknown>",
312 			      pai_data.event_val);
313 		free(ev_name);
314 
315 		if (offset + 0xa > len)
316 			break;
317 	}
318 	color_fprintf(stdout, color, "\n");
319 }
320 
321 /* S390 specific trace event function. Check for PERF_RECORD_SAMPLE events
322  * and if the event was triggered by a
323  * - counter set diagnostic event
324  * - processor activity assist (PAI) crypto counter event
325  * - processor activity assist (PAI) neural network processor assist (NNPA)
326  *   counter event
327  * display its raw data.
328  * The function is only invoked when the dump flag -D is set.
329  *
330  * Function evlist__s390_sample_raw() is defined as call back after it has
331  * been verified that the perf.data file was created on s390 platform.
332  */
333 void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event,
334 			     struct perf_sample *sample)
335 {
336 	const char *pai_name;
337 
338 	if (event->header.type != PERF_RECORD_SAMPLE)
339 		return;
340 
341 	if (!sample->evsel) {
342 		sample->evsel = evlist__event2evsel(evlist, event);
343 		if (!sample->evsel)
344 			return;
345 	}
346 
347 	/* Check for raw data in sample */
348 	if (!sample->raw_size || !sample->raw_data)
349 		return;
350 
351 	/* Display raw data on screen */
352 	if (sample->evsel->core.attr.config == PERF_EVENT_CPUM_CF_DIAG) {
353 		if (!sample->evsel->pmu)
354 			sample->evsel->pmu = perf_pmus__find("cpum_cf");
355 		if (!s390_cpumcfdg_testctr(sample))
356 			pr_err("Invalid counter set data encountered\n");
357 		else
358 			s390_cpumcfdg_dump(sample);
359 		return;
360 	}
361 
362 	switch (sample->evsel->core.attr.config) {
363 	case PERF_EVENT_PAI_NNPA_ALL:
364 		pai_name = "NNPA_ALL";
365 		break;
366 	case PERF_EVENT_PAI_CRYPTO_ALL:
367 		pai_name = "CRYPTO_ALL";
368 		break;
369 	default:
370 		return;
371 	}
372 
373 	if (!s390_pai_all_test(sample)) {
374 		pr_err("Invalid %s raw data encountered\n", pai_name);
375 	} else {
376 		if (!sample->evsel->pmu)
377 			sample->evsel->pmu = perf_pmus__find_by_type(sample->evsel->core.attr.type);
378 		s390_pai_all_dump(sample);
379 	}
380 }
381