File: xe_sysfs_scheduler.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 (337 lines) | stat: -rw-r--r-- 8,945 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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2023 Intel Corporation
 */

/**
 * TEST: xe sysfs scheduler
 * Category: Core
 * Mega feature: SysMan
 * Sub-category: SysMan tests
 * Functionality: scheduler control interface
 *
 * SUBTEST: %s-invalid
 * Description: Test to check if %s arg[1] schedule parameter rejects any
 *		unrepresentable intervals.
 * Test category: negative test
 *
 * SUBTEST: %s-invalid-string
 * Description: Test to check if %s arg[1] schedule parameter checks for
 *		invalid string values.
 * Test category: negative test
 *
 * SUBTEST: %s-invalid-large-string
 * Description: Test to check if %s arg[1] schedule parameter checks for
 *		large invalid strings (4k).
 * Test category: negative test
 *
 * SUBTEST: %s-min-max
 * Description: Test to check if %s arg[1] schedule parameter checks for
 *		min max values.
 * Test category: functionality test
 *
 * arg[1]:
 *
 * @preempt_timeout_us:		preempt timeout us
 * @timeslice_duration_us:	timeslice duration us
 * @job_timeout_ms:		job timeout ms
 */

#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "igt.h"
#include "igt_sysfs.h"

#include "xe_drm.h"
#include "xe/xe_query.h"

#define STR_LENGTH 4096

/**
 * generate_random_string:
 * @str: pointer to string buffer that will be having random string generated.
 * @length: length of string to generate.
 *
 * Generates random string that will always contain non-numerical characters.
 */
static void generate_random_string(char *str, size_t length)
{
	int type = 0;
	int digit_count = 0;
	int max_digits = length / 4;
	int loop_count = length - 1;

	for (size_t i = 0; i < loop_count; i++) {
		if (digit_count >= max_digits)
			type = rand() % 2;
		else
			type = rand() % 3;

		switch (type) {
		case 0:
			str[i] = 'A' + (rand() % 26);
			break;
		case 1:
			str[i] = 'a' + (rand() % 26);
			break;
		case 2:
			str[i] = '0' + (rand() % 10);
			digit_count++;
			break;
		default:
			str[i] = '_';
			break;
		}
	}
	str[length - 1] = '\0';
}

static void test_invalid(int xe, int engine, const char **property,
			 uint16_t class, int gt)
{
	unsigned int saved, set;
	unsigned int min, max;

	igt_sysfs_scanf(engine, property[2], "%u", &max);
	igt_sysfs_scanf(engine, property[1], "%u", &min);

	igt_assert(igt_sysfs_scanf(engine, property[0], "%u", &saved) == 1);
	igt_debug("Initial %s:%u\n", property[0], saved);

	igt_sysfs_printf(engine, property[0], "%d", max+100);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, saved);

	igt_sysfs_printf(engine, property[0], "%d", min-100);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, saved);
}

static void test_min_max(int xe, int engine, const char **property,
			 uint16_t class, int gt)
{
	unsigned int default_max, max;
	unsigned int default_min, min;
	unsigned int set, store;
	int defaults;

	defaults = openat(engine, ".defaults", O_DIRECTORY);
	igt_require(defaults != -1);

	igt_sysfs_scanf(defaults, property[2], "%u", &default_max);
	igt_sysfs_scanf(defaults, property[1], "%u", &default_min);
	igt_sysfs_scanf(engine, property[0], "%u", &store);

	igt_sysfs_printf(engine, property[2], "%d", default_max-10);
	igt_sysfs_scanf(engine, property[2], "%u", &max);
	igt_assert_eq(max, (default_max-10));

	igt_sysfs_printf(engine, property[2], "%d", default_max+1);
	igt_sysfs_scanf(engine, property[2], "%u", &max);
	igt_assert_neq(max, (default_max+1));

	igt_sysfs_printf(engine, property[1], "%d", default_min+1);
	igt_sysfs_scanf(engine, property[1], "%u", &min);
	igt_assert_eq(min, (default_min+1));

	igt_sysfs_printf(engine, property[1], "%d", default_min-10);
	igt_sysfs_scanf(engine, property[1], "%u", &min);
	igt_assert_neq(min, (default_min-10));

	igt_sysfs_printf(engine, property[0], "%d", min);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, min);

	igt_sysfs_printf(engine, property[0], "%d", max);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, max);

	igt_sysfs_printf(engine, property[0], "%d", default_min);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_neq(set, default_min);

	igt_sysfs_printf(engine, property[0], "%d", min);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, min);

	/* Reset property, max, min to original values */
	igt_sysfs_printf(engine, property[0], "%d", store);
	igt_sysfs_scanf(engine, property[0], "%u", &set);
	igt_assert_eq(set, store);

	igt_sysfs_printf(engine, property[1], "%d", default_min);
	igt_sysfs_scanf(engine, property[1], "%u", &set);
	igt_assert_eq(set, default_min);

	igt_sysfs_printf(engine, property[2], "%d", default_max);
	igt_sysfs_scanf(engine, property[2], "%u", &set);
	igt_assert_eq(set, default_max);
}

static void test_invalid_string(int xe, int engine, const char **property,
				 uint16_t class, int gt)
{
	unsigned int saved, set;
	static const char invalid_input[] = "999abc";

	for (int i = 0; i < 3; i++) {
		igt_assert_eq(igt_sysfs_scanf(engine, property[i], "%u", &saved), 1);
		igt_info("Initial %s: %u\n", property[i], saved);
		/* Assert if the invalid write is returning negative error */
		igt_assert_lt(igt_sysfs_printf(engine, property[i], "%s",
			      invalid_input), 0);

		igt_assert_eq(igt_sysfs_scanf(engine, property[i], "%u", &set), 1);
		/* Check if the values are unchanged. */
		igt_assert_eq(set, saved);
	}
}

static void test_invalid_large_string(int xe, int engine, const char **property,
				       uint16_t class, int gt)
{
	unsigned int saved, set;
	char *random_str;

	random_str = (char *)malloc(sizeof(char) * (STR_LENGTH));
	igt_assert_f(random_str, "Memory allocation failed\n");

	generate_random_string(random_str, STR_LENGTH);
	igt_debug("Generated random string: %.10s...\n", random_str);

	for (int i = 0; i < 3; i++) {
		igt_assert_eq(igt_sysfs_scanf(engine, property[i], "%u", &saved), 1);
		igt_info("Initial %s: %u\n", property[i], saved);

		/* Assert if the invalid write is returning negative error */
		igt_assert_lt(igt_sysfs_printf(engine, property[i], "%s",
			      random_str), 0);

		igt_assert_eq(igt_sysfs_scanf(engine, property[i], "%u", &set), 1);
		/* Check if the values are unchanged. */
		igt_assert_eq(set, saved);
	}
	free(random_str);
}

#define MAX_GTS 8
igt_main
{
	static const struct {
		const char *name;
		void (*fn)(int, int, const char **, uint16_t, int);
	} tests[] = {
		{ "invalid", test_invalid },
		{ "invalid-string", test_invalid_string },
		{ "invalid-large-string", test_invalid_large_string },
		{ "min-max", test_min_max },
		{ }
	};

	const char *property[][3] = { {"preempt_timeout_us", "preempt_timeout_min", "preempt_timeout_max"},
				      {"timeslice_duration_us", "timeslice_duration_min", "timeslice_duration_max"},
				      {"job_timeout_ms", "job_timeout_min", "job_timeout_max"},
	};

	unsigned int store[MAX_GTS][3][3];
	int count = sizeof(property) / sizeof(property[0]);
	int gt_count = 0;
	int xe = -1;
	int sys_fd;
	int gt;
	int engines_fd[MAX_GTS], gt_fd[MAX_GTS];
	int *engine_list[MAX_GTS];

	igt_fixture {
		xe = drm_open_driver(DRIVER_XE);
		xe_device_get(xe);

		sys_fd = igt_sysfs_open(xe);
		igt_require(sys_fd != -1);
		close(sys_fd);

		xe_for_each_gt(xe, gt) {
			int *list, i = 0;

			igt_require(gt_count < MAX_GTS);

			gt_fd[gt_count] = xe_sysfs_gt_open(xe, gt);
			igt_require(gt_fd[gt_count] != -1);
			engines_fd[gt_count] = openat(gt_fd[gt_count], "engines", O_RDONLY);
			igt_require(engines_fd[gt_count] != -1);

			list = igt_sysfs_get_engine_list(engines_fd[gt_count]);

			while (list[i] != -1) {
				for (int j = 0; j < count; j++) {
					const char **pl = property[j];

					for (int k = 0; k < 3; k++) {
						unsigned int *loc = &store[i][j][k];

						igt_require(igt_sysfs_scanf(list[i], pl[k],
									    "%u", loc) == 1);
					}
				}
				i++;
			}

			igt_require(i > 0);
			engine_list[gt_count] = list;
			gt_count++;
		}
	}

	for (int i = 0; i < count; i++) {
		for (typeof(*tests) *t = tests; t->name; t++) {
			igt_subtest_with_dynamic_f("%s-%s", property[i][0], t->name) {
				int j = 0;
				xe_for_each_gt(xe, gt) {
					int e = engines_fd[j];

					igt_sysfs_engines(xe, e, 0, 0, property[i], t->fn);
					j++;
				}
			}
		}
	}

	igt_fixture {
		for (int gtn = gt_count - 1; gtn >= 0; gtn--) {
			int *list, i = 0;

			list = engine_list[gtn];

			while (list[i] != -1) {
				int e = list[i];

				for (int j = count - 1; j >= 0; j--) {
					const char **pl = property[j];

					for (int k = 2; k >= 0; k--) {
						unsigned int read = UINT_MAX;
						unsigned int val = store[i][j][k];

						igt_sysfs_printf(e, pl[k], "%u", val);
						igt_sysfs_scanf(e, pl[k], "%u", &read);
						igt_abort_on_f(read != val,
							       "%s not restored!\n", pl[k]);
					}
				}
				i++;
			}

			igt_sysfs_free_engine_list(list);
			close(engines_fd[gtn]);
			close(gt_fd[gtn]);
		}

		xe_device_put(xe);
		close(xe);
	}
}