File: xe_compute.c

package info (click to toggle)
intel-gpu-tools 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 64,504 kB
  • sloc: xml: 781,458; ansic: 378,272; python: 8,407; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (572 lines) | stat: -rw-r--r-- 15,063 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2022 Intel Corporation
 */

/**
 * TEST: Check compute-related functionality
 * Category: Core
 * Mega feature: Compute
 * Sub-category: Compute tests
 * Test category: functionality test
 */

#include <string.h>

#include "igt.h"
#include "igt_sysfs.h"
#include "intel_compute.h"
#include "xe/xe_ioctl.h"
#include "xe/xe_query.h"

#include "igt_perf.h"
#include "igt_sriov_device.h"

#define LOOP_DURATION_2s	(1000000ull * 2)
#define DURATION_MARGIN		0.2
#define MIN_BUSYNESS		95.0

bool sriov_enabled;

struct thread_data {
	pthread_t thread;
	pthread_mutex_t *mutex;
	pthread_cond_t *cond;
	int class;
	int fd;
	int gt;
	struct user_execenv *execenv;
	struct drm_xe_engine_class_instance *eci;
	bool *go;
};

static int gt_sysfs_open(int gt)
{
	int fd, gt_fd;

	fd = drm_open_driver(DRIVER_XE);
	gt_fd = xe_sysfs_gt_open(fd, gt);
	drm_close_driver(fd);

	return gt_fd;
}

static bool get_num_cslices(u32 gt, u32 *num_slices)
{
	int gt_fd, ret;

	gt_fd = gt_sysfs_open(gt);
	ret = igt_sysfs_scanf(gt_fd, "num_cslices", "%u", num_slices);
	close(gt_fd);

	return ret > 0;
}

/* Grab GT mask in places where we don't have or want to maintain an open fd */
static uint64_t get_gt_mask(void)
{
	int fd = drm_open_driver(DRIVER_XE);
	uint64_t mask;

	mask = xe_device_get(fd)->gt_mask;
	drm_close_driver(fd);

	return mask;
}

#define for_each_bit(__mask, __bit) \
	for ( ; __bit = ffsll(__mask) - 1, __mask != 0; __mask &= ~(1ull << __bit))

/**
 * SUBTEST: ccs-mode-basic
 * GPU requirement: PVC
 * Description: Validate 'ccs_mode' sysfs uapi
 * Functionality: ccs mode
 */
static void
test_ccs_mode(void)
{
	struct drm_xe_engine_class_instance *hwe;
	u32 gt, m, ccs_mode, vm, q, num_slices;
	int fd, gt_fd, num_gt_with_ccs_mode = 0;
	uint64_t gt_mask = get_gt_mask();

	/*
	 * The loop body needs to run without any open file descriptors so we
	 * can't use xe_for_each_gt() which uses an open fd.
	 */
	for_each_bit(gt_mask, gt) {
		if (!get_num_cslices(gt, &num_slices))
			continue;

		num_gt_with_ccs_mode++;
		gt_fd = gt_sysfs_open(gt);
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", 0) < 0);
		for (m = 1; m <= num_slices; m++) {
			/* compute slices are to be equally distributed among enabled engines */
			if (num_slices % m) {
				igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", m) < 0);
				continue;
			}

			/* Validate allowed ccs modes by setting them and reading back */
			igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", m) > 0);
			igt_assert(igt_sysfs_scanf(gt_fd, "ccs_mode", "%u", &ccs_mode) > 0);
			igt_assert(m == ccs_mode);

			/* Validate exec queues creation with enabled ccs engines */
			fd = drm_open_driver(DRIVER_XE);
			vm = xe_vm_create(fd, 0, 0);
			xe_for_each_engine(fd, hwe) {
				if (hwe->gt_id != gt ||
				    hwe->engine_class != DRM_XE_ENGINE_CLASS_COMPUTE)
					continue;

				q = xe_exec_queue_create(fd, vm, hwe, 0);
				xe_exec_queue_destroy(fd, q);
			}

			/* Ensure exec queue creation fails for disabled ccs engines */
			hwe->gt_id = gt;
			hwe->engine_class = DRM_XE_ENGINE_CLASS_COMPUTE;
			hwe->engine_instance = m;
			igt_assert_neq(__xe_exec_queue_create(fd, vm, 1, 1, hwe, 0, &q), 0);

			xe_vm_destroy(fd, vm);
			drm_close_driver(fd);
		}

		/* Ensure invalid ccs mode setting is rejected */
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", m) < 0);

		/* Can't change ccs mode with an open drm clients */
		fd = drm_open_driver(DRIVER_XE);
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", 1) < 0);
		drm_close_driver(fd);

		/* Set ccs mode back to default value */
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", 1) > 0);

		close(gt_fd);
	}

	igt_require(num_gt_with_ccs_mode > 0);
}

/**
 * SUBTEST: ccs-mode-compute-kernel
 * GPU requirement: PVC
 * Description: Validate 'ccs_mode' by running compute kernel
 * Functionality: ccs mode
 */
static void
test_compute_kernel_with_ccs_mode(void)
{
	struct drm_xe_engine_class_instance *hwe;
	u32 gt, m, num_slices;
	int fd, gt_fd, num_gt_with_ccs_mode = 0;
	uint64_t gt_mask = get_gt_mask();

	/*
	 * The loop body needs to run without any open file descriptors so we
	 * can't use xe_for_each_gt() which uses an open fd.
	 */
	for_each_bit(gt_mask, gt) {
		if (!get_num_cslices(gt, &num_slices))
			continue;

		num_gt_with_ccs_mode++;
		gt_fd = gt_sysfs_open(gt);
		for (m = 1; m <= num_slices; m++) {
			if (num_slices % m)
				continue;

			igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", m) > 0);

			/* Run compute kernel on enabled ccs engines */
			fd = drm_open_driver(DRIVER_XE);
			xe_for_each_engine(fd, hwe) {
				if (hwe->gt_id != gt ||
				    hwe->engine_class != DRM_XE_ENGINE_CLASS_COMPUTE)
					continue;

				igt_info("GT-%d: Running compute kernel with ccs_mode %d on ccs engine %d\n",
					 gt, m, hwe->engine_instance);
				igt_assert_f(xe_run_intel_compute_kernel_on_engine(fd, hwe, NULL, EXECENV_PREF_SYSTEM),
					     "Unable to run compute kernel successfully\n");
			}
			drm_close_driver(fd);
		}

		/* Set ccs mode back to default value */
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", 1) > 0);

		close(gt_fd);
	}

	igt_require(num_gt_with_ccs_mode > 0);
}

static double elapsed(const struct timeval *start,
		      const struct timeval *end)
{
	return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}

/**
 * SUBTEST: loop-duration-2s
 * Functionality: OpenCL kernel
 * Description:
 *	Run an openCL loop Kernel that for duration,
 *	set in loop_kernel_duration ..
 */
static void
test_compute_kernel_loop(uint64_t loop_duration)
{
	int fd;
	unsigned int ip_ver;
	const struct intel_compute_kernels *kernels;
	struct user_execenv execenv = { 0 };
	struct drm_xe_engine_class_instance *hwe;
	struct timeval start, end;
	double elapse_time, lower_bound, upper_bound;

	fd = drm_open_driver(DRIVER_XE);
	ip_ver = intel_graphics_ver(intel_get_drm_devid(fd));
	kernels = intel_compute_square_kernels;

	while (kernels->kernel) {
		if (ip_ver == kernels->ip_ver)
			break;
		kernels++;
	}

	/* loop_kernel_duration used as sleep to make EU busy for loop_duration */
	execenv.loop_kernel_duration = loop_duration;
	execenv.kernel = kernels->loop_kernel;
	execenv.kernel_size = kernels->loop_kernel_size;

	xe_for_each_engine(fd, hwe) {
		if (hwe->engine_class != DRM_XE_ENGINE_CLASS_COMPUTE)
			continue;

		igt_info("Running loop_kernel on ccs engine %d\n", hwe->engine_instance);
		gettimeofday(&start, NULL);
		igt_assert_f(xe_run_intel_compute_kernel_on_engine(fd, hwe, &execenv,
								   EXECENV_PREF_SYSTEM),
			     "Unable to run compute kernel successfully\n");
		gettimeofday(&end, NULL);
		elapse_time = elapsed(&start, &end);
		lower_bound = loop_duration / 1e6 - DURATION_MARGIN;
		upper_bound = loop_duration / 1e6 + DURATION_MARGIN;

		igt_assert(lower_bound < elapse_time && elapse_time < upper_bound);
	}
	drm_close_driver(fd);
}

static void
*intel_compute_thread(void *data)
{
	struct thread_data *t = (struct thread_data *)data;
	char device[30];
	uint64_t type;
	uint32_t engine_class, engine_instance, gt_shift;
	uint64_t engine_active_config, engine_total_config;
	int ret, fd1, fd2;
	uint64_t val[4];
	uint64_t param_config;

	pthread_mutex_lock(t->mutex);
	while (*t->go == 0)
		pthread_cond_wait(t->cond, t->mutex);
	pthread_mutex_unlock(t->mutex);

	type = igt_perf_type_id(xe_perf_device(t->fd, device, sizeof(device)));
	igt_debug("type: %lx, device: %s\n", type, device);

	perf_event_format(device, "gt", &gt_shift);
	perf_event_format(device, "engine_class", &engine_class);
	perf_event_format(device, "engine_instance", &engine_instance);

	ret = perf_event_config(device, "engine-active-ticks", &engine_active_config);
	igt_assert_eq(ret, 0);
	ret = perf_event_config(device, "engine-total-ticks", &engine_total_config);
	igt_assert_eq(ret, 0);

	igt_debug("gt_id: %x, class: %x, instance: %x\n",
		  t->eci->gt_id, t->eci->engine_class, t->eci->engine_instance);

	/* Setting collective counters for compute engine available in t->eci */
	param_config = (uint64_t)t->eci->gt_id << gt_shift |
		       t->eci->engine_class << engine_class |
		       t->eci->engine_instance << engine_instance;

	fd1 = igt_perf_open_group(type, engine_active_config | param_config, -1);
	fd2 = igt_perf_open_group(type, engine_total_config | param_config, fd1);

	ret = read(fd1, val, sizeof(val));
	igt_assert_eq(ret, sizeof(val));
	igt_info("start - active: %ld, total: %ld, busyness: %.1f before scheduling on engine_instance :%d\n",
		 val[2], val[3], (float)val[2] / (float)val[3] * 100.0, t->eci->engine_instance);

	igt_assert_f(xe_run_intel_compute_kernel_on_engine(t->fd, t->eci, t->execenv,
							   EXECENV_PREF_VRAM_IF_POSSIBLE),
		     "Unable to run compute kernel successfully\n");

	ret = read(fd1, val, sizeof(val));
	igt_assert_eq(ret, sizeof(val));
	igt_info("end - active: %ld, total: %ld, busyness: %.1f after scheduling on engine_instance :%d\n",
		 val[2], val[3], (float)val[2] / (float)val[3] * 100.0, t->eci->engine_instance);

	igt_assert_f(((float)val[2] / (float)val[3] * 100.0) > MIN_BUSYNESS,
		     "Engines are under utilizated\n");

	close(fd1);
	close(fd2);
	return NULL;
}

static void
igt_check_supported_pipeline(void)
{
	int fd;
	unsigned int ip_ver;
	const struct intel_compute_kernels *kernels;

	fd = drm_open_driver(DRIVER_XE);
	ip_ver = intel_graphics_ver(intel_get_drm_devid(fd));
	kernels = intel_compute_square_kernels;
	drm_close_driver(fd);

	while (kernels->kernel) {
		if (ip_ver == kernels->ip_ver)
			break;
		kernels++;
	}

	/* skip if loop_kernel is not supported by pipeline */
	if (!kernels->kernel || !kernels->loop_kernel)
		igt_skip("loop_kernel not supported by pipeline\n");
}

static bool is_sriov_mode(int fd)
{
	bool is_sriov = false;

	if (igt_sriov_is_pf(fd) && igt_sriov_vfs_supported(fd))
		is_sriov = true;

	return is_sriov;
}

static void
igt_store_ccs_mode(int ccs_mode[], int size)
{
	uint64_t gt_mask;
	uint32_t gt, num_slices;
	int gt_fd;

	gt_mask = get_gt_mask();
	for_each_bit(gt_mask, gt) {
		if (!get_num_cslices(gt, &num_slices))
			continue;
		igt_assert(gt < size);

		gt_fd = gt_sysfs_open(gt);
		igt_sysfs_scanf(gt_fd, "ccs_mode", "%u", &ccs_mode[gt]);
		close(gt_fd);
	}
}

static void
igt_restore_ccs_mode(int ccs_mode[], int size)
{
	uint64_t gt_mask = get_gt_mask();
	uint32_t gt, num_slices;
	int gt_fd;

	for_each_bit(gt_mask, gt) {
		if (!get_num_cslices(gt, &num_slices))
			continue;
		igt_assert(gt < size);

		gt_fd = gt_sysfs_open(gt);
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", ccs_mode[gt]) > 0);
		close(gt_fd);
	}
}

/**
 * SUBTEST: eu-busy-10s
 * Functionality: OpenCL kernel
 * Description: Run loop_kernel for duration_sec and observe EU business
 */
static void
test_eu_busy(uint64_t duration_sec)
{
	struct user_execenv execenv = { 0 };
	struct thread_data *threads_data;
	struct drm_xe_engine_class_instance *hwe;
	const struct intel_compute_kernels *kernels;
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	u32 gt, n_instances = 0, i;
	bool go = false;
	int ccs_mode, gt_fd, fd;
	u32 num_slices, ip_ver;
	uint64_t gt_mask = get_gt_mask();

	for_each_bit(gt_mask, gt) {
		if (!get_num_cslices(gt, &num_slices))
			continue;

		gt_fd = gt_sysfs_open(gt);
		igt_assert(igt_sysfs_printf(gt_fd, "ccs_mode", "%u", num_slices) > 0);
		igt_assert(igt_sysfs_scanf(gt_fd, "ccs_mode", "%u", &ccs_mode) > 0);
		close(gt_fd);
	}

	igt_skip_on_f(ccs_mode <= 1, "Skipping test as ccs_mode <=1 not matching criteria :%d\n",
		      ccs_mode);

	fd = drm_open_driver(DRIVER_XE);

	ip_ver = intel_graphics_ver(intel_get_drm_devid(fd));
	kernels = intel_compute_square_kernels;
	while (kernels->kernel) {
		if (ip_ver == kernels->ip_ver)
			break;
		kernels++;
	}
	if (!kernels->loop_kernel_size)
		drm_close_driver(fd);
	igt_assert(kernels->loop_kernel_size);

	/*
	 * User should use different kernel if loop_kernel_duration not set
	 * With loop kernel and loop duration it assumes we stop it via memory write
	 *
	 */
	execenv.loop_kernel_duration = duration_sec;
	execenv.kernel = kernels->loop_kernel;
	execenv.kernel_size = kernels->loop_kernel_size;

	xe_for_each_engine(fd, hwe) {
		if (hwe->engine_class == DRM_XE_ENGINE_CLASS_COMPUTE)
			++n_instances;
	}

	threads_data = calloc(n_instances, sizeof(*threads_data));
	if (!threads_data)
		drm_close_driver(fd); /* drop reference for retrieve ccs_mode */
	igt_assert(threads_data);

	pthread_mutex_init(&mutex, 0);
	pthread_cond_init(&cond, 0);

	i = 0;
	xe_for_each_engine(fd, hwe) {
		if (hwe->engine_class != DRM_XE_ENGINE_CLASS_COMPUTE)
			continue;

		threads_data[i].mutex = &mutex;
		threads_data[i].cond = &cond;
		threads_data[i].fd = fd;
		threads_data[i].eci = hwe;
		threads_data[i].go = &go;
		threads_data[i].execenv = &execenv;
		pthread_create(&threads_data[i].thread, 0, intel_compute_thread,
			       &threads_data[i]);
		++i;
	}
	pthread_mutex_lock(&mutex);
	go = true;
	pthread_cond_broadcast(&cond);
	pthread_mutex_unlock(&mutex);

	for (int n = 0; n < i; ++n)
		pthread_join(threads_data[n].thread, NULL);

	free(threads_data);

	drm_close_driver(fd);
}

/**
 * SUBTEST: compute-square
 * Mega feature: WMTP
 * Sub-category: wmtp tests
 * Functionality: OpenCL kernel
 * GPU requirement: TGL, PVC, LNL, PTL
 * Description:
 *	Run an openCL Kernel that returns output[i] = input[i] * input[i],
 *	for an input dataset..
 */
static void
test_compute_square(int fd)
{
	igt_require_f(run_intel_compute_kernel(fd, NULL, EXECENV_PREF_SYSTEM),
		      "GPU not supported\n");
}

int igt_main()
{
	int xe, ccs_mode[4];
	unsigned int ip_ver;

	igt_fixture() {
		xe = drm_open_driver(DRIVER_XE);
		sriov_enabled = is_sriov_mode(xe);
		ip_ver = intel_graphics_ver(intel_get_drm_devid(xe));
		igt_store_ccs_mode(ccs_mode, ARRAY_SIZE(ccs_mode));
	}

	igt_subtest("compute-square")
		test_compute_square(xe);

	igt_fixture()
		drm_close_driver(xe);

	/* ccs mode tests should be run without open gpu file handles */
	igt_subtest("ccs-mode-basic") {
		/* skip if sriov enabled */
		if (sriov_enabled)
			igt_skip("Skipping test when SRIOV is enabled\n");
		test_ccs_mode();
	}

	igt_subtest("ccs-mode-compute-kernel") {
		/* skip if sriov enabled */
		if (sriov_enabled)
			igt_skip("Skipping test when SRIOV is enabled\n");
		test_compute_kernel_with_ccs_mode();
	}

	/* To test compute function stops after loop_kernel_duration */
	igt_subtest("loop-duration-2s") {
		/* skip test if loop_kernel not supported in pipeline */
		if (ip_ver < IP_VER(20, 0))
			igt_check_supported_pipeline();

		test_compute_kernel_loop(LOOP_DURATION_2s);
	}

	/* test to check available EU utilisation in multi-ccs case */
	igt_subtest("eu-busy-10s") {
		/* skip if sriov enabled */
		if (sriov_enabled)
			igt_skip("Skipping test when SRIOV is enabled\n");

		/* skip test if loop_kernel not supported in pipeline */
		if (ip_ver < IP_VER(20, 0))
			igt_check_supported_pipeline();

		test_eu_busy(5 * LOOP_DURATION_2s);
	}

	igt_fixture() {
		if (!sriov_enabled)
			igt_restore_ccs_mode(ccs_mode, ARRAY_SIZE(ccs_mode));
	}
}