File: xe_perf_reader.c

package info (click to toggle)
intel-gpu-tools 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 63,360 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (325 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2024 Intel Corporation
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "igt_core.h"
#include "intel_chipset.h"
#include "xe/xe_oa.h"
#include "xe/xe_oa_data_reader.h"

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) > (b) ? (b) : (a))

static void
usage(void)
{
	printf("Usage: xe-perf-reader [options] file\n"
	       "Reads the content of an xe-perf recording.\n"
	       "\n"
	       "     --help,    -h             Print this screen\n"
	       "     --counters, -c c1,c2,...  List of counters to display values for.\n"
	       "                               Use 'all' to display all counters.\n"
	       "                               Use 'list' to list available counters.\n"
	       "     --reports, -r             Print out data per report.\n");
}

static struct intel_xe_perf_logical_counter *
find_counter(struct intel_xe_perf_metric_set *metric_set,
	     const char *name)
{
	for (uint32_t i = 0; i < metric_set->n_counters; i++) {
		if (!strcmp(name, metric_set->counters[i].symbol_name)) {
			return &metric_set->counters[i];
		}
	}

	return NULL;
}

static void
append_counter(struct intel_xe_perf_logical_counter ***counters,
	       int32_t *n_counters,
	       uint32_t *n_allocated_counters,
	       struct intel_xe_perf_logical_counter *counter)
{
	if (*n_counters < *n_allocated_counters) {
		(*counters)[(*n_counters)++] = counter;
		return;
	}

	*n_allocated_counters = MAX(2, *n_allocated_counters * 2);
	*counters = realloc(*counters,
			    sizeof(struct intel_xe_perf_logical_counter *) *
			    (*n_allocated_counters));
	(*counters)[(*n_counters)++] = counter;
}

static struct intel_xe_perf_logical_counter **
get_logical_counters(struct intel_xe_perf_metric_set *metric_set,
		     const char *counter_list,
		     int32_t *out_n_counters)
{
	struct intel_xe_perf_logical_counter **counters = NULL, *counter;
	uint32_t n_allocated_counters = 0;
	const char *current, *next;
	char counter_name[100];

	if (!counter_list) {
		*out_n_counters = 0;
		return NULL;
	}

	if (!strcmp(counter_list, "list")) {
		uint32_t longest_name = 0;

		*out_n_counters = -1;
		for (uint32_t i = 0; i < metric_set->n_counters; i++) {
			longest_name = MAX(longest_name,
					   strlen(metric_set->counters[i].symbol_name));
		}

		fprintf(stdout, "Available counters:\n");
		for (uint32_t i = 0; i < metric_set->n_counters; i++) {
			fprintf(stdout, "%s:%*s%s\n",
				metric_set->counters[i].symbol_name,
				(int)(longest_name -
				      strlen(metric_set->counters[i].symbol_name) + 1), " ",
				metric_set->counters[i].name);
		}
		return NULL;
	}

	if (!strcmp(counter_list, "all")) {
		counters = malloc(sizeof(*counters) * metric_set->n_counters);
		*out_n_counters = metric_set->n_counters;
		for (uint32_t i = 0; i < metric_set->n_counters; i++)
			counters[i] = &metric_set->counters[i];
		return counters;
	}

	*out_n_counters = 0;
	current = counter_list;
	while ((next = strstr(current, ","))) {
		snprintf(counter_name,
			 MIN((uint32_t)(next - current) + 1, sizeof(counter_name)),
			 "%s", current);

		counter = find_counter(metric_set, counter_name);
		if (!counter) {
			fprintf(stderr, "Unknown counter '%s'.\n", counter_name);
			free(counters);
			*out_n_counters = -1;
			return NULL;
		}

		append_counter(&counters, out_n_counters, &n_allocated_counters, counter);

		current = next + 1;
	}

	if (strlen(current) > 0) {
		counter = find_counter(metric_set, current);
		if (!counter) {
			fprintf(stderr, "Unknown counter '%s'.\n", current);
			free(counters);
			*out_n_counters = -1;
			return NULL;
		}

		append_counter(&counters, out_n_counters, &n_allocated_counters, counter);
	}

	return counters;
}

static void
print_report_deltas(const struct intel_xe_perf_data_reader *reader,
		    const struct intel_xe_perf_record_header *xe_report0,
		    const struct intel_xe_perf_record_header *xe_report1,
		    struct intel_xe_perf_logical_counter **counters,
		    uint32_t n_counters)
{
	struct intel_xe_perf_accumulator accu;

	intel_xe_perf_accumulate_reports(&accu,
				      reader->perf, reader->metric_set,
				      xe_report0, xe_report1);

	for (uint32_t c = 0; c < n_counters; c++) {
		struct intel_xe_perf_logical_counter *counter = counters[c];

		switch (counter->storage) {
		case INTEL_XE_PERF_LOGICAL_COUNTER_STORAGE_UINT64:
		case INTEL_XE_PERF_LOGICAL_COUNTER_STORAGE_UINT32:
		case INTEL_XE_PERF_LOGICAL_COUNTER_STORAGE_BOOL32:
			fprintf(stdout, "   %s: %" PRIu64 "\n",
				counter->symbol_name, counter->read_uint64(reader->perf,
									   reader->metric_set,
									   accu.deltas));
			break;
		case INTEL_XE_PERF_LOGICAL_COUNTER_STORAGE_DOUBLE:
		case INTEL_XE_PERF_LOGICAL_COUNTER_STORAGE_FLOAT:
			fprintf(stdout, "   %s: %f\n",
				counter->symbol_name, counter->read_float(reader->perf,
									  reader->metric_set,
									  accu.deltas));
			break;
		}
	}
}

int
main(int argc, char *argv[])
{
	const struct option long_options[] = {
		{"help",             no_argument, 0, 'h'},
		{"counters",   required_argument, 0, 'c'},
		{"reports",          no_argument, 0, 'r'},
		{0, 0, 0, 0}
	};
	struct intel_xe_perf_data_reader reader;
	struct intel_xe_perf_logical_counter **counters;
	const struct intel_device_info *devinfo;
	const char *counter_names = NULL;
	int32_t n_counters;
	int fd, opt;
	bool print_reports = false;

	while ((opt = getopt_long(argc, argv, "hc:r", long_options, NULL)) != -1) {
		switch (opt) {
		case 'h':
			usage();
			return EXIT_SUCCESS;
		case 'c':
			counter_names = optarg;
			break;
		case 'r':
			print_reports = true;
			break;
		default:
			fprintf(stderr, "Internal error: "
				"unexpected getopt value: %d\n", opt);
			usage();
			return EXIT_FAILURE;
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "No recording file specified.\n");
		return EXIT_FAILURE;
	}

	fd = open(argv[optind], 0, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Cannot open '%s': %s.\n",
			argv[optind], strerror(errno));
		return EXIT_FAILURE;
	}

	if (!intel_xe_perf_data_reader_init(&reader, fd)) {
		fprintf(stderr, "Unable to parse '%s': %s.\n",
			argv[optind], reader.error_msg);
		return EXIT_FAILURE;
	}

	counters = get_logical_counters(reader.metric_set, counter_names, &n_counters);
	if (n_counters < 0)
		goto exit;

	devinfo = intel_get_device_info(reader.devinfo.devid);

	fprintf(stdout, "Recorded on device=0x%x(%s) graphics_ver=%i\n",
		reader.devinfo.devid, devinfo->codename,
		reader.devinfo.graphics_ver);
	fprintf(stdout, "Metric used : %s (%s) uuid=%s\n",
		reader.metric_set->symbol_name, reader.metric_set->name,
		reader.metric_set->hw_config_guid);
	fprintf(stdout, "Reports: %u\n", reader.n_records);
	fprintf(stdout, "Context switches: %u\n", reader.n_timelines);
	fprintf(stdout, "Timestamp correlation points: %u\n", reader.n_correlations);

	if (reader.n_correlations < 2) {
		fprintf(stderr, "Less than 2 CPU/GPU timestamp correlation points.\n");
		return EXIT_FAILURE;
	}

	fprintf(stdout, "Timestamp correlation CPU range:       0x%016"PRIx64"-0x%016"PRIx64"\n",
		reader.correlations[0]->cpu_timestamp,
		reader.correlations[reader.n_correlations - 1]->cpu_timestamp);
	fprintf(stdout, "Timestamp correlation GPU range (64b): 0x%016"PRIx64"-0x%016"PRIx64"\n",
		reader.correlations[0]->gpu_timestamp,
		reader.correlations[reader.n_correlations - 1]->gpu_timestamp);
	fprintf(stdout, "Timestamp correlation GPU range (32b): 0x%016"PRIx64"-0x%016"PRIx64"\n",
		reader.correlations[0]->gpu_timestamp & 0xffffffff,
		reader.correlations[reader.n_correlations - 1]->gpu_timestamp & 0xffffffff);

	fprintf(stdout, "OA data timestamp range:               0x%016"PRIx64"-0x%016"PRIx64"\n",
		intel_xe_perf_read_record_timestamp(reader.perf,
						 reader.metric_set,
						 reader.records[0]),
		intel_xe_perf_read_record_timestamp(reader.perf,
						 reader.metric_set,
						 reader.records[reader.n_records - 1]));
	fprintf(stdout, "OA raw data timestamp range:           0x%016"PRIx64"-0x%016"PRIx64"\n",
		intel_xe_perf_read_record_timestamp_raw(reader.perf,
						     reader.metric_set,
						     reader.records[0]),
		intel_xe_perf_read_record_timestamp_raw(reader.perf,
						     reader.metric_set,
						     reader.records[reader.n_records - 1]));

	if (strcmp(reader.metric_set_uuid, reader.metric_set->hw_config_guid)) {
		fprintf(stdout,
			"WARNING: Recording used a different HW configuration.\n"
			"WARNING: This could lead to inconsistent counter values.\n");
	}

	for (uint32_t i = 0; i < reader.n_timelines; i++) {
		const struct intel_xe_perf_timeline_item *item = &reader.timelines[i];

		fprintf(stdout, "Time: CPU=0x%016" PRIx64 "-0x%016" PRIx64
			" GPU=0x%016" PRIx64 "-0x%016" PRIx64"\n",
			item->cpu_ts_start, item->cpu_ts_end,
			item->ts_start, item->ts_end);
		fprintf(stdout, "hw_id=0x%x %s\n",
			item->hw_id, item->hw_id == 0xffffffff ? "(idle)" : "");

		print_report_deltas(&reader,
				    reader.records[item->record_start],
				    reader.records[item->record_end],
				    counters, n_counters);

		if (print_reports) {
			for (uint32_t r = item->record_start; r < item->record_end; r++) {
				fprintf(stdout, " report%i = %s\n",
					r - item->record_start,
					intel_xe_perf_read_report_reason(reader.perf, reader.records[r]));
				print_report_deltas(&reader,
						    reader.records[r],
						    reader.records[r + 1],
						    counters, n_counters);
			}
		}
	}

 exit:
	intel_xe_perf_data_reader_fini(&reader);
	close(fd);

	return EXIT_SUCCESS;
}