summaryrefslogtreecommitdiff
path: root/test/timeout.c
blob: 00581e0c1a718483732025949fd0f42b7fd61017 (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
/*
 * Description: run various timeout tests
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>

#include "liburing.h"

#define TIMEOUT_MSEC	1000
static int not_supported;

static unsigned long long mtime_since(const struct timeval *s,
				      const struct timeval *e)
{
	long long sec, usec;

	sec = e->tv_sec - s->tv_sec;
	usec = (e->tv_usec - s->tv_usec);
	if (sec > 0 && usec < 0) {
		sec--;
		usec += 1000000;
	}

	sec *= 1000;
	usec /= 1000;
	return sec + usec;
}

static unsigned long long mtime_since_now(struct timeval *tv)
{
	struct timeval end;

	gettimeofday(&end, NULL);
	return mtime_since(tv, &end);
}

/*
 * Test that we return to userspace if a timeout triggers, even if we
 * don't satisfy the number of events asked for.
 */
static int test_single_timeout_many(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	unsigned long long exp;
	struct timespec ts;
	struct timeval tv;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		printf("get sqe failed\n");
		goto err;
	}

	ts.tv_sec = TIMEOUT_MSEC / 1000;
	ts.tv_nsec = 0;
	io_uring_prep_timeout(sqe, &ts, 0);

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		printf("sqe submit failed: %d\n", ret);
		goto err;
	}

	gettimeofday(&tv, NULL);
	ret = io_uring_enter(ring->ring_fd, 0, 4, IORING_ENTER_GETEVENTS, NULL);
	if (ret < 0) {
		printf("io_uring_enter %d\n", ret);
		goto err;
	}

	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret < 0) {
		printf("wait completion %d\n", ret);
		goto err;
	}
	if (cqe->res == -EINVAL) {
		printf("Timeout not supported, ignored\n");
		not_supported = 1;
		return 0;
	} else if (cqe->res != -ETIME) {
		printf("Timeout: %s\n", strerror(-cqe->res));
		goto err;
	}
	io_uring_cqe_seen(ring, cqe);

	exp = mtime_since_now(&tv);
	if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
		return 0;
	printf("Timeout seems wonky (got %llu)\n", exp);
err:
	return 1;
}

/*
 * Test numbered trigger of timeout
 */
static int test_single_timeout_nr(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct timespec ts;
	int i, ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		printf("get sqe failed\n");
		goto err;
	}

	ts.tv_sec = TIMEOUT_MSEC / 1000;
	ts.tv_nsec = 0;
	io_uring_prep_timeout(sqe, &ts, 2);

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_nop(sqe);
	io_uring_sqe_set_data(sqe, (void *) 1);
	sqe = io_uring_get_sqe(ring);
	io_uring_prep_nop(sqe);
	io_uring_sqe_set_data(sqe, (void *) 1);

	ret = io_uring_submit_and_wait(ring, 4);
	if (ret <= 0) {
		printf("sqe submit failed: %d\n", ret);
		goto err;
	}

	i = 0;
	while (i < 3) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret < 0) {
			printf("wait completion %d\n", ret);
			goto err;
		}

		/*
		 * NOP commands have user_data as 1. Check that we get the
		 * two NOPs first, then the successfully removed timout as
		 * the last one.
		 */
		switch (i) {
		case 0:
		case 1:
			if (io_uring_cqe_get_data(cqe) != (void *) 1) {
				printf("nop not seen as 1 or 2\n");
				goto err;
			}
			break;
		case 2:
			if (io_uring_cqe_get_data(cqe) != NULL) {
				printf("timeout not last\n");
				goto err;
			}
			break;
		}

		if (cqe->res < 0) {
			printf("Timeout: %s\n", strerror(-cqe->res));
			goto err;
		} else if (cqe->res) {
			printf("res: %d\n", cqe->res);
			goto err;
		}
		io_uring_cqe_seen(ring, cqe);
		i++;
	};

	return 0;
err:
	return 1;
}

static int test_single_timeout_wait(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct timespec ts;
	int i, ret;

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_nop(sqe);
	io_uring_sqe_set_data(sqe, (void *) 1);

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_nop(sqe);
	io_uring_sqe_set_data(sqe, (void *) 1);

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		printf("sqe submit failed: %d\n", ret);
		goto err;
	}

	ts.tv_sec = 1;
	ts.tv_nsec = 0;

	i = 0;
	do {
		ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
		if (ret == -ETIME)
			break;
		if (ret < 0) {
			printf("wait timeout failed: %d\n", ret);
			goto err;
		}

		if (cqe->res < 0) {
			printf("res: %d\n", cqe->res);
			goto err;
		}
		io_uring_cqe_seen(ring, cqe);
		i++;
	} while (1);

	if (i != 2) {
		printf("got %d completions\n", i);
		goto err;
	}
	return 0;
err:
	return 1;
}

/*
 * Test single timeout waking us up
 */
static int test_single_timeout(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	unsigned long long exp;
	struct timespec ts;
	struct timeval tv;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		printf("get sqe failed\n");
		goto err;
	}

	ts.tv_sec = TIMEOUT_MSEC / 1000;
	ts.tv_nsec = 0;
	io_uring_prep_timeout(sqe, &ts, 0);

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		printf("sqe submit failed: %d\n", ret);
		goto err;
	}

	gettimeofday(&tv, NULL);
	ret = io_uring_wait_cqe(ring, &cqe);
	if (ret < 0) {
		printf("wait completion %d\n", ret);
		goto err;
	}
	if (cqe->res == -EINVAL) {
		printf("Timeout not supported, ignored\n");
		not_supported = 1;
		return 0;
	} else if (cqe->res != -ETIME) {
		printf("Timeout: %s\n", strerror(-cqe->res));
		goto err;
	}
	io_uring_cqe_seen(ring, cqe);

	exp = mtime_since_now(&tv);
	if (exp >= TIMEOUT_MSEC / 2 && exp <= (TIMEOUT_MSEC * 3) / 2)
		return 0;
	printf("Timeout seems wonky (got %llu)\n", exp);
err:
	return 1;
}

/*
 * Test that timeout is canceled on exit
 */
static int test_single_timeout_exit(struct io_uring *ring)
{
	struct io_uring_sqe *sqe;
	struct timespec ts;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		printf("get sqe failed\n");
		goto err;
	}

	ts.tv_sec = 30;
	ts.tv_nsec = 0;
	io_uring_prep_timeout(sqe, &ts, 0);

	ret = io_uring_submit(ring);
	if (ret <= 0) {
		printf("sqe submit failed: %d\n", ret);
		goto err;
	}

	io_uring_queue_exit(ring);
	return 0;
err:
	io_uring_queue_exit(ring);
	return 1;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	int ret;

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret) {
		printf("ring setup failed\n");
		return 1;

	}

	ret = test_single_timeout(&ring);
	if (ret) {
		printf("test_single_timeout failed\n");
		return ret;
	}
	if (not_supported)
		return 0;

	ret = test_single_timeout_many(&ring);
	if (ret) {
		printf("test_single_timeout_many failed\n");
		return ret;
	}

	ret = test_single_timeout_nr(&ring);
	if (ret) {
		printf("test_single_timeout_nr failed\n");
		return ret;
	}

	ret = test_single_timeout_wait(&ring);
	if (ret) {
		printf("test_single_timeout_wait failed\n");
		return ret;
	}

	/*
	 * this test must go last, it kills the ring
	 */
	ret = test_single_timeout_exit(&ring);
	if (ret) {
		printf("test_single_timeout_nr failed\n");
		return ret;
	}

	return 0;
}