summaryrefslogtreecommitdiffstats
path: root/synctest.c
blob: a96f911aa2e91f55eb8dac04f2183fbabb1034a9 (plain)
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
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sync/sync.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/* These are not exported from the library because they are for testing only. */
int sw_sync_fence_create(int fd, const char *name, unsigned value);
int sw_sync_timeline_create(void);
int sw_sync_timeline_inc(int fd, unsigned count);

#define CHK(errn) do { int ret = fc; if (ret < 0) return ret; } while (0)

#define ERROR(msg, en) do {\
		fprintf(stderr, "ERROR : %s. : %d:%s\n", (msg), (en), strerror(en)); \
	} while (0)

bool verbose = false;

static int
test_swsync_exists(void)
{
	int ret;
	int newsync = 0;
	struct stat buf = { 0 };

	ret = stat("/dev/sw_sync", &buf);
	if (ret < 0 && errno == ENOENT) {
		ret = stat("/sys/kernel/debug/sync/sw_sync", &buf);
		if (ret < 0 && errno == ENOENT) {
			ret = -errno;
			ERROR("sw_sync interface does not exist", -ret);
			goto fail;
		}
		newsync = 1;
	}
	if (ret < 0 && errno == EACCES) {
		ret = -errno;
		ERROR("No permission to access sw_sync interface", -ret);
		goto fail;
	}

	if (ret < 0) {
		ret = -errno;
		ERROR("Stat failed", -ret);
		goto fail;
	}

	/* Old sync is a character device. New is a regular file in sysfs. */
	if ((!newsync && (buf.st_mode & S_IFCHR))
	    || (newsync && (buf.st_mode & S_IFREG))) {
		ret = 0;
	}

fail:
	return ret;
}

static int
get_timeline(void)
{
	int tl = sw_sync_timeline_create();
	if (tl < 0) {
		tl = -errno;
		ERROR("Cannot create timeline", -tl);
	}
	return tl;
}

static int
get_fence(int tl, int value)
{
	int fc = sw_sync_fence_create(tl, "simple fence", value);
	if (fc < 0) {
		fc = -errno;
		ERROR("Failed to create fence", -fc);
	}
	return fc;
}

static int
fence_check(int fc)
{
	int ret;
	ret = sync_wait(fc, 0);
	if (ret < 0) {
		if (errno == ETIME)
			return 0;
		if (errno == EINVAL)
			return 1; /* timeline destroyed */
		ret = -errno;
		ERROR("Failed fence wait", -ret);
		return ret;
	}

	return 1;
}

/* Return 0 when timeout, 1 when passing, < 0 for error
*/
static int
timeline_inc(int tl, int delta)
{
	int ret;
	ret = sw_sync_timeline_inc(tl, delta);
	if (ret < 0) {
		ret = -errno;
		ERROR("Failed to increment timeline", -ret);
	}
	return ret;
}

/* Test single fence. */
static int
test_simple(void)
{
	int tl;
	int fc;
	int ret;

	tl = get_timeline();
	if (tl < 0) {
		return tl;
	}

	fc = get_fence(tl, 1);
	if (fc < 0) {
		ret = fc;
		goto fail;
	}

	ret = fence_check(fc);
	if (ret < 0) {
		goto fail_fence;
	} else if (ret == 1) {
		ret = -1;
		ERROR("Fence already signaled even though we did not signal it", 0);
		goto fail_fence;
	}

	ret = timeline_inc(tl, 1);
	if (ret < 0) {
		goto fail_fence;
	}

	ret = fence_check(fc);
	if (ret < 0) {
		goto fail_fence;
	} else if (ret == 0) {
		ret = -1;
		ERROR("Fence is not signaled even though it should be", 0);
		goto fail_fence;
	}
	ret = 0;

fail_fence:
	close(fc);
fail:
	close(tl);
	return ret;
}


#define NUM_FENCES 10
/* Test multiple fences signaled with one timeline inc. */
static int
test_multiple(void)
{
	int i;
	int tl;
	int fc[NUM_FENCES];
	int ret;
	int value = 1;

	tl = get_timeline();
	if (tl < 0) {
		return tl;
	}

	for (i = 0; i < NUM_FENCES; i++) {
		fc[i] = get_fence(tl, value++);
		if (fc[i] < 0) {
			ret = fc[i];
			goto fail;
		}
	}

	for (i = 0; i < NUM_FENCES; i++) {
		ret = fence_check(fc[i]);
		if (ret < 0) {
			goto fail_fence;
		} else if (ret == 1) {
			ret = -1;
			ERROR("Fence already signaled even though we did not signal it", 0);
			goto fail_fence;
		}
	}

	ret = timeline_inc(tl, NUM_FENCES);
	if (ret < 0) {
		goto fail_fence;
	}

	for (i = 0; i < NUM_FENCES; i++) {
		ret = fence_check(fc[i]);
		if (ret < 0) {
			goto fail_fence;
		} else if (ret == 0) {
			ret = -1;
			ERROR("Fence is not signaled even though it should be", 0);
			goto fail_fence;
		}
	}
	ret = 0;

fail_fence:
	for (i = 0; i < NUM_FENCES; i++)
		close(fc[i]);
fail:
	close(tl);
	return ret;
}
#undef NUM_FENCES

/* Test destruction of timeline. It should signal/set error condition on all
 * fences in the timeline. */
static int
test_timeline_destroy(void)
{
	int tl;
	int fc;
	int ret;

	tl = get_timeline();
	if (tl < 0) {
		return tl;
	}

	fc = get_fence(tl, 1);
	if (fc < 0) {
		ret = fc;
		goto fail;
	}

	ret = fence_check(fc);
	if (ret < 0) {
		goto fail_fence;
	} else if (ret == 1) {
		ret = -1;
		ERROR("Fence already signaled even though we did not signal it", 0);
		goto fail_fence;
	}

	close(tl);

	ret = fence_check(fc);
	if (ret < 0) {
		goto fail_fence;
	} else if (ret == 0) {
		ret = -1;
		ERROR("Fence is not signaled even though it should be", 0);
		goto fail_fence;
	}
	ret = 0;

fail_fence:
	close(fc);
fail:
	return ret;
}

static const struct { const char *name; int (*func)(void); } cases[] = {
	{ "swsync_exists", test_swsync_exists },
	{ "simple", test_simple },
	{ "multiple", test_multiple },
	{ "timeline_destroy", test_timeline_destroy },
	{ NULL, NULL }
};


static const struct option longopts[] = {
	{ "test_name", required_argument, NULL, 't' },
	{ "verbose", no_argument, NULL, 'v' },
	{ "help", no_argument, NULL, 'h' },
	{ 0, 0, 0, 0 },
};

static void print_help(const char *argv0)
{
	unsigned int u;
	printf("usage: %s -t <test_name> [-v]\n", argv0);
	printf("A valid name test is one the following:\n");
	for (u = 0; cases[u].name; u++)
		printf("%s\n", cases[u].name);
	printf("all\n");
}

int
run_synctest(const char *name)
{
	unsigned int u;
	int ret = 0;

	for (u = 0; cases[u].name; u++) {
		if (strcmp(cases[u].name, name) && strcmp(name, "all"))
			continue;
		if (verbose)
			printf("Running subtest %s\n", cases[u].name);
		ret = cases[u].func();
		if (verbose)
			printf("Subtest %s %s\n", cases[u].name, (ret < 0) ? "failed" : "passed");
		if (ret < 0)
			break;
	}

	return ret;
}

int
main(int ARGC, char *ARGV[])
{
	int c;
	char *name = strdup("all");

	while ((c = getopt_long(ARGC, ARGV, "t:vh", longopts, NULL)) != -1) {
		switch (c) {
			case 't':
				if (name) {
					free(name);
					name = NULL;
				}

				name = strdup(optarg);
				break;
			case 'v':
				verbose = true;
				break;
			case 'h':
				goto print;
			default:
				goto print;
		}
	}

	if (!name)
		goto print;

	int ret = run_synctest(name);
	if (ret == 0)
		printf("[  PASSED  ] synctest.%s\n", name);
	else if (ret < 0)
		printf("[  FAILED  ] synctest.%s\n", name);

	free(name);

	if (ret > 0) {
		printf("Unexpected test result.\n");
		goto print;
	}

	return ret;

print:
	print_help(ARGV[0]);
	return -EINVAL;

}