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
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: tests for getevents timeout
*
*/
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include "liburing.h"
#define TIMEOUT_MSEC 200
#define TIMEOUT_SEC 10
static int thread_ret0, thread_ret1;
static int cnt = 0;
static pthread_mutex_t mutex;
static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
{
ts->tv_sec = msec / 1000;
ts->tv_nsec = (msec % 1000) * 1000000;
}
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);
}
static int test_return_before_timeout(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret;
bool retried = false;
struct __kernel_timespec ts;
msec_to_ts(&ts, TIMEOUT_MSEC);
sqe = io_uring_get_sqe(ring);
io_uring_prep_nop(sqe);
ret = io_uring_submit(ring);
if (ret <= 0) {
fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
return 1;
}
again:
ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
if (ret == -ETIME && (ring->flags & IORING_SETUP_SQPOLL) && !retried) {
/*
* there is a small chance SQPOLL hasn't been waked up yet,
* give it one more try.
*/
printf("warning: funky SQPOLL timing\n");
sleep(1);
retried = true;
goto again;
} else if (ret < 0) {
fprintf(stderr, "%s: timeout error: %d\n", __FUNCTION__, ret);
return 1;
}
io_uring_cqe_seen(ring, cqe);
return 0;
}
static int test_return_after_timeout(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
int ret;
struct __kernel_timespec ts;
struct timeval tv;
unsigned long long exp;
msec_to_ts(&ts, TIMEOUT_MSEC);
gettimeofday(&tv, NULL);
ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
exp = mtime_since_now(&tv);
if (ret != -ETIME) {
fprintf(stderr, "%s: timeout error: %d\n", __FUNCTION__, ret);
return 1;
}
if (exp < TIMEOUT_MSEC / 2 || exp > (TIMEOUT_MSEC * 3) / 2) {
fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
return 1;
}
return 0;
}
static int __reap_thread_fn(void *data)
{
struct io_uring *ring = (struct io_uring *)data;
struct io_uring_cqe *cqe;
struct __kernel_timespec ts;
msec_to_ts(&ts, TIMEOUT_SEC);
pthread_mutex_lock(&mutex);
cnt++;
pthread_mutex_unlock(&mutex);
return io_uring_wait_cqe_timeout(ring, &cqe, &ts);
}
static void *reap_thread_fn0(void *data)
{
thread_ret0 = __reap_thread_fn(data);
return NULL;
}
static void *reap_thread_fn1(void *data)
{
thread_ret1 = __reap_thread_fn(data);
return NULL;
}
/*
* This is to test issuing a sqe in main thread and reaping it in two child-thread
* at the same time. To see if timeout feature works or not.
*/
static int test_multi_threads_timeout(void)
{
struct io_uring ring;
int ret;
bool both_wait = false;
pthread_t reap_thread0, reap_thread1;
struct io_uring_sqe *sqe;
ret = io_uring_queue_init(8, &ring, 0);
if (ret) {
fprintf(stderr, "%s: ring setup failed: %d\n", __FUNCTION__, ret);
return 1;
}
pthread_create(&reap_thread0, NULL, reap_thread_fn0, &ring);
pthread_create(&reap_thread1, NULL, reap_thread_fn1, &ring);
/*
* make two threads both enter io_uring_wait_cqe_timeout() before issuing the sqe
* as possible as we can. So that there are two threads in the ctx->wait queue.
* In this way, we can test if a cqe wakes up two threads at the same time.
*/
while(!both_wait) {
pthread_mutex_lock(&mutex);
if (cnt == 2)
both_wait = true;
pthread_mutex_unlock(&mutex);
sleep(1);
}
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
goto err;
}
io_uring_prep_nop(sqe);
ret = io_uring_submit(&ring);
if (ret <= 0) {
fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
goto err;
}
pthread_join(reap_thread0, NULL);
pthread_join(reap_thread1, NULL);
if ((thread_ret0 && thread_ret0 != -ETIME) || (thread_ret1 && thread_ret1 != -ETIME)) {
fprintf(stderr, "%s: thread wait cqe timeout failed: %d %d\n",
__FUNCTION__, thread_ret0, thread_ret1);
goto err;
}
return 0;
err:
return 1;
}
int main(int argc, char *argv[])
{
struct io_uring ring_normal, ring_sq;
int ret;
if (argc > 1)
return 0;
ret = io_uring_queue_init(8, &ring_normal, 0);
if (ret) {
fprintf(stderr, "ring_normal setup failed: %d\n", ret);
return 1;
}
if (!(ring_normal.features & IORING_FEAT_EXT_ARG)) {
fprintf(stderr, "feature IORING_FEAT_EXT_ARG not supported, skipping.\n");
return 0;
}
ret = test_return_before_timeout(&ring_normal);
if (ret) {
fprintf(stderr, "ring_normal: test_return_before_timeout failed\n");
return ret;
}
ret = test_return_after_timeout(&ring_normal);
if (ret) {
fprintf(stderr, "ring_normal: test_return_after_timeout failed\n");
return ret;
}
ret = io_uring_queue_init(8, &ring_sq, IORING_SETUP_SQPOLL);
if (ret) {
fprintf(stderr, "ring_sq setup failed: %d\n", ret);
return 1;
}
ret = test_return_before_timeout(&ring_sq);
if (ret) {
fprintf(stderr, "ring_sq: test_return_before_timeout failed\n");
return ret;
}
ret = test_return_after_timeout(&ring_sq);
if (ret) {
fprintf(stderr, "ring_sq: test_return_after_timeout failed\n");
return ret;
}
ret = test_multi_threads_timeout();
if (ret) {
fprintf(stderr, "test_multi_threads_timeout failed\n");
return ret;
}
return 0;
}
|