• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test min timeout handling
4  *
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/resource.h>
13 #include <sys/time.h>
14 #include <pthread.h>
15 
16 #include "liburing.h"
17 #include "helpers.h"
18 
19 #define NPIPES	8
20 #define NWRITES	6
21 
22 #define WAIT_USEC	(250000)
23 
24 static int no_min_timeout;
25 
26 struct d {
27 	int fd[NPIPES];
28 	long delay;
29 };
30 
thread_fn(void * data)31 static void *thread_fn(void *data)
32 {
33 	struct d *d = data;
34 	char buf[32];
35 	int i;
36 
37 	memset(buf, 0x55, sizeof(buf));
38 
39 	for (i = 0; i < NWRITES; i++) {
40 		int ret;
41 
42 		usleep(d->delay);
43 		ret = write(d->fd[i], buf, sizeof(buf));
44 		if (ret != sizeof(buf)) {
45 			fprintf(stderr, "bad write %d\n", ret);
46 			break;
47 		}
48 	}
49 	return NULL;
50 }
51 
52 /*
53  * Allow 25% tolerance
54  */
within_range(unsigned int target,unsigned int msec)55 static int within_range(unsigned int target, unsigned int msec)
56 {
57 	unsigned int high, low;
58 
59 	low = (target * 3) / 4;
60 	high = (target * 5) / 4;
61 	return (msec >= low && msec <= high);
62 }
63 
test(int flags,int expected_ctx,int min_wait,int write_delay,int nr_cqes,int msec_target)64 static int test(int flags, int expected_ctx, int min_wait, int write_delay,
65 		int nr_cqes, int msec_target)
66 {
67 	struct io_uring_cqe *cqe;
68 	struct io_uring_sqe *sqe;
69 	struct io_uring ring;
70 	struct __kernel_timespec ts;
71 	struct rusage s, e;
72 	pthread_t thread;
73 	struct d d;
74 	struct io_uring_params p = { .flags = flags, };
75 	int ret, fds[NPIPES][2], i;
76 	struct timeval start_time;
77 	char buf[32];
78 	void *tret;
79 	long ttime;
80 
81 	ret = io_uring_queue_init_params(NPIPES, &ring, &p);
82 	if (ret == -EINVAL)
83 		return T_EXIT_SKIP;
84 	if (!(p.features & IORING_FEAT_MIN_TIMEOUT)) {
85 		no_min_timeout = 1;
86 		return T_EXIT_SKIP;
87 	}
88 
89 	for (i = 0; i < NPIPES; i++) {
90 		if (pipe(fds[i]) < 0) {
91 			perror("pipe");
92 			return 1;
93 		}
94 		d.fd[i] = fds[i][1];
95 	}
96 
97 	d.delay = write_delay;
98 	pthread_create(&thread, NULL, thread_fn, &d);
99 
100 	for (i = 0; i < NPIPES; i++) {
101 		sqe = io_uring_get_sqe(&ring);
102 		io_uring_prep_read(sqe, fds[i][0], buf, sizeof(buf), 0);
103 	}
104 
105 	ts.tv_sec = 0;
106 	ts.tv_nsec = WAIT_USEC * 1000LL;
107 
108 	gettimeofday(&start_time, NULL);
109 	getrusage(RUSAGE_THREAD, &s);
110 	ret = io_uring_submit_and_wait_min_timeout(&ring, &cqe, 8, &ts, min_wait, NULL);
111 	if (ret != NPIPES)
112 		fprintf(stderr, "submit_and_wait=%d\n", ret);
113 
114 	getrusage(RUSAGE_THREAD, &e);
115 	e.ru_nvcsw -= s.ru_nvcsw;
116 	ttime = mtime_since_now(&start_time);
117 	if (!within_range(msec_target, ttime)) {
118 		fprintf(stderr, "Expected %d msec, got %ld msec\n", msec_target,
119 								ttime);
120 		fprintf(stderr, "flags=%x, min_wait=%d, write_delay=%d\n",
121 				flags, min_wait, write_delay);
122 	}
123 	/* will usually be accurate, but allow for offset of 1 */
124 	if (e.ru_nvcsw != expected_ctx &&
125 	    (e.ru_nvcsw - expected_ctx > 1))
126 		fprintf(stderr, "%ld ctx switches, expected %d\n", e.ru_nvcsw,
127 								expected_ctx);
128 
129 	for (i = 0; i < NPIPES; i++) {
130 		ret = io_uring_peek_cqe(&ring, &cqe);
131 		if (ret)
132 			break;
133 		io_uring_cqe_seen(&ring, cqe);
134 	}
135 
136 	if (i != nr_cqes)
137 		fprintf(stderr, "Got %d CQEs, expected %d\n", i, nr_cqes);
138 
139 	pthread_join(thread, &tret);
140 
141 	for (i = 0; i < NPIPES; i++) {
142 		close(fds[i][0]);
143 		close(fds[i][1]);
144 	}
145 
146 	return T_EXIT_PASS;
147 }
148 
main(int argc,char * argv[])149 int main(int argc, char *argv[])
150 {
151 	int ret;
152 
153 	if (argc > 1)
154 		return T_EXIT_SKIP;
155 
156 	ret = test(0, NWRITES + 1, 0, 2000, NWRITES, WAIT_USEC / 1000);
157 	if (ret == T_EXIT_FAIL)
158 		return T_EXIT_FAIL;
159 	if (no_min_timeout)
160 		return T_EXIT_SKIP;
161 
162 	ret = test(0, NWRITES + 1, 50000, 2000, NWRITES, 50);
163 	if (ret == T_EXIT_FAIL)
164 		return T_EXIT_FAIL;
165 
166 	ret = test(0, NWRITES + 1, 500000, 2000, NWRITES, 500);
167 	if (ret == T_EXIT_FAIL)
168 		return T_EXIT_FAIL;
169 
170 	/* no writes within min timeout, but it's given. expect 1 cqe */
171 	ret = test(0, 1, 10000, 20000, 1, 20);
172 	if (ret == T_EXIT_FAIL)
173 		return T_EXIT_FAIL;
174 
175 	/* same as above, but no min timeout. should time out and we get 6 */
176 	ret = test(0, NWRITES + 1, 0, 20000, NWRITES, WAIT_USEC / 1000);
177 	if (ret == T_EXIT_FAIL)
178 		return T_EXIT_FAIL;
179 
180 	ret = test(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN, 1,
181 			0, 2000, NWRITES, WAIT_USEC / 1000);
182 	if (ret == T_EXIT_FAIL)
183 		return T_EXIT_FAIL;
184 
185 	ret = test(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN, 1,
186 			50000, 2000, NWRITES, 50);
187 	if (ret == T_EXIT_FAIL)
188 		return T_EXIT_FAIL;
189 
190 	ret = test(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN, 1,
191 			500000, 2000, NWRITES, 500);
192 	if (ret == T_EXIT_FAIL)
193 		return T_EXIT_FAIL;
194 
195 	/* no writes within min timeout, but it's given. expect 1 cqe */
196 	ret = test(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN, 1,
197 			10000, 20000, 1, 20);
198 	if (ret == T_EXIT_FAIL)
199 		return T_EXIT_FAIL;
200 
201 	/* same as above, but no min timeout. should time out and we get 6 */
202 	ret = test(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN, 1,
203 			0, 20000, NWRITES, WAIT_USEC / 1000);
204 	if (ret == T_EXIT_FAIL)
205 		return T_EXIT_FAIL;
206 
207 	return ret;
208 }
209