• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: Test that io_uring_submit_and_wait_timeout() returns the
4  * right value (submit count) and that it doesn't end up waiting twice.
5  *
6  */
7 #include <errno.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/time.h>
14 
15 #include "liburing.h"
16 #include "helpers.h"
17 #include "test.h"
18 
test(struct io_uring * ring)19 static int test(struct io_uring *ring)
20 {
21 	struct io_uring_cqe *cqe;
22 	struct io_uring_sqe *sqe;
23 	struct __kernel_timespec ts;
24 	struct timeval tv;
25 	int ret, i;
26 
27 	for (i = 0; i < 1; i++) {
28 		sqe = io_uring_get_sqe(ring);
29 		if (!sqe) {
30 			fprintf(stderr, "get sqe failed at %d\n", i);
31 			goto err;
32 		}
33 		io_uring_prep_nop(sqe);
34 	}
35 
36 	ts.tv_sec = 1;
37 	ts.tv_nsec = 0;
38 	gettimeofday(&tv, NULL);
39 	ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
40 	if (ret < 0) {
41 		fprintf(stderr, "submit_and_wait_timeout: %d\n", ret);
42 		goto err;
43 	}
44 	ret = mtime_since_now(&tv);
45 	/* allow some slack, should be around 1s */
46 	if (ret > 1200) {
47 		fprintf(stderr, "wait took too long: %d\n", ret);
48 		goto err;
49 	}
50 	return 0;
51 err:
52 	return 1;
53 }
54 
test_ring(void)55 static int test_ring(void)
56 {
57 	struct io_uring ring;
58 	struct io_uring_params p = { };
59 	int ret;
60 
61 	p.flags = 0;
62 	ret = io_uring_queue_init_params(8, &ring, &p);
63 	if (ret) {
64 		fprintf(stderr, "ring setup failed: %d\n", ret);
65 		return 1;
66 	}
67 
68 	ret = test(&ring);
69 	if (ret) {
70 		fprintf(stderr, "test failed\n");
71 		goto err;
72 	}
73 err:
74 	io_uring_queue_exit(&ring);
75 	return ret;
76 }
77 
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 	if (argc > 1)
81 		return 0;
82 
83 	return test_ring();
84 }
85