Rename t/aio-ring to t/io_uring
[fio.git] / t / io_uring.c
CommitLineData
c9fb4c5b
JA
1#include <stdio.h>
2#include <errno.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stddef.h>
6#include <signal.h>
7#include <inttypes.h>
8
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/ioctl.h>
12#include <sys/syscall.h>
13#include <sys/resource.h>
c3e2fc25 14#include <sys/mman.h>
e31b8288 15#include <sys/uio.h>
c9fb4c5b
JA
16#include <linux/fs.h>
17#include <fcntl.h>
18#include <unistd.h>
c9fb4c5b
JA
19#include <string.h>
20#include <pthread.h>
21#include <sched.h>
22
74efb029
JA
23#include "../arch/arch.h"
24
fa3e6c25 25typedef uint64_t u64;
c9fb4c5b 26typedef uint32_t u32;
e31b8288 27typedef int32_t s32;
478a96a8 28typedef uint16_t u16;
e31b8288 29typedef uint8_t u8;
c9fb4c5b 30
e31b8288 31#include "../os/io_uring.h"
ac122fea 32
e31b8288 33#define barrier() __asm__ __volatile__("": : :"memory")
c3e2fc25 34
e31b8288 35#define min(a, b) ((a < b) ? (a) : (b))
c3e2fc25 36
e31b8288 37struct io_sq_ring {
c3e2fc25
JA
38 u32 *head;
39 u32 *tail;
40 u32 *ring_mask;
41 u32 *ring_entries;
42 u32 *array;
c9fb4c5b
JA
43};
44
e31b8288 45struct io_cq_ring {
c3e2fc25
JA
46 u32 *head;
47 u32 *tail;
48 u32 *ring_mask;
49 u32 *ring_entries;
e31b8288 50 struct io_uring_event *events;
c9fb4c5b
JA
51};
52
c9fb4c5b 53#define DEPTH 32
c9fb4c5b
JA
54
55#define BATCH_SUBMIT 8
56#define BATCH_COMPLETE 8
57
58#define BS 4096
59
c3e2fc25 60static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 61
c9fb4c5b
JA
62struct submitter {
63 pthread_t thread;
64 unsigned long max_blocks;
f310970e 65 int ring_fd;
c9fb4c5b 66 struct drand48_data rand;
e31b8288
JA
67 struct io_sq_ring sq_ring;
68 struct io_uring_iocb *iocbs;
c3e2fc25 69 struct iovec iovecs[DEPTH];
e31b8288 70 struct io_cq_ring cq_ring;
c9fb4c5b
JA
71 int inflight;
72 unsigned long reaps;
73 unsigned long done;
74 unsigned long calls;
305c06ce 75 unsigned long cachehit, cachemiss;
c9fb4c5b
JA
76 volatile int finish;
77 char filename[128];
78};
79
80static struct submitter submitters[1];
81static volatile int finish;
82
c3e2fc25
JA
83static int polled = 0; /* use IO polling */
84static int fixedbufs = 0; /* use fixed user buffers */
85static int buffered = 1; /* use buffered IO, not O_DIRECT */
0527b23f
JA
86static int sq_thread = 0; /* use kernel submission thread */
87static int sq_thread_cpu = 0; /* pin above thread to this CPU */
c9fb4c5b 88
c3e2fc25 89static int io_uring_setup(unsigned entries, struct iovec *iovecs,
e31b8288 90 struct io_uring_params *p)
c9fb4c5b 91{
74efb029 92 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
c9fb4c5b
JA
93}
94
c3e2fc25
JA
95static int io_uring_enter(struct submitter *s, unsigned int to_submit,
96 unsigned int min_complete, unsigned int flags)
c9fb4c5b 97{
f310970e
JA
98 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
99 min_complete, flags);
c9fb4c5b
JA
100}
101
102static int gettid(void)
103{
104 return syscall(__NR_gettid);
105}
106
c3e2fc25 107static void init_io(struct submitter *s, int fd, unsigned index)
c9fb4c5b 108{
e31b8288 109 struct io_uring_iocb *iocb = &s->iocbs[index];
c9fb4c5b
JA
110 unsigned long offset;
111 long r;
112
113 lrand48_r(&s->rand, &r);
114 offset = (r % (s->max_blocks - 1)) * BS;
115
e31b8288
JA
116 iocb->opcode = IORING_OP_READ;
117 iocb->flags = 0;
118 iocb->ioprio = 0;
119 iocb->fd = fd;
120 iocb->off = offset;
121 iocb->addr = s->iovecs[index].iov_base;
122 iocb->len = BS;
c9fb4c5b
JA
123}
124
125static int prep_more_ios(struct submitter *s, int fd, int max_ios)
126{
e31b8288 127 struct io_sq_ring *ring = &s->sq_ring;
e39c34dc 128 u32 index, tail, next_tail, prepped = 0;
c9fb4c5b 129
c3e2fc25 130 next_tail = tail = *ring->tail;
c9fb4c5b
JA
131 do {
132 next_tail++;
c9fb4c5b 133 barrier();
c3e2fc25 134 if (next_tail == *ring->head)
c9fb4c5b
JA
135 break;
136
e39c34dc 137 index = tail & sq_ring_mask;
c3e2fc25
JA
138 init_io(s, fd, index);
139 ring->array[index] = index;
c9fb4c5b
JA
140 prepped++;
141 tail = next_tail;
142 } while (prepped < max_ios);
143
c3e2fc25 144 if (*ring->tail != tail) {
c9fb4c5b
JA
145 /* order tail store with writes to iocbs above */
146 barrier();
c3e2fc25 147 *ring->tail = tail;
c9fb4c5b
JA
148 barrier();
149 }
150 return prepped;
151}
152
153static int get_file_size(int fd, unsigned long *blocks)
154{
155 struct stat st;
156
157 if (fstat(fd, &st) < 0)
158 return -1;
159 if (S_ISBLK(st.st_mode)) {
160 unsigned long long bytes;
161
162 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
163 return -1;
164
165 *blocks = bytes / BS;
166 return 0;
167 } else if (S_ISREG(st.st_mode)) {
168 *blocks = st.st_size / BS;
169 return 0;
170 }
171
172 return -1;
173}
174
175static int reap_events(struct submitter *s)
176{
e31b8288
JA
177 struct io_cq_ring *ring = &s->cq_ring;
178 struct io_uring_event *ev;
c9fb4c5b
JA
179 u32 head, reaped = 0;
180
c3e2fc25 181 head = *ring->head;
c9fb4c5b
JA
182 do {
183 barrier();
c3e2fc25 184 if (head == *ring->tail)
c9fb4c5b 185 break;
e39c34dc 186 ev = &ring->events[head & cq_ring_mask];
c9fb4c5b 187 if (ev->res != BS) {
e31b8288 188 struct io_uring_iocb *iocb = &s->iocbs[ev->index];
c9fb4c5b 189
e31b8288 190 printf("io: unexpected ret=%d\n", ev->res);
f310970e 191 printf("offset=%lu, size=%lu\n",
e31b8288
JA
192 (unsigned long) iocb->off,
193 (unsigned long) iocb->len);
c9fb4c5b
JA
194 return -1;
195 }
e31b8288 196 if (ev->flags & IOEV_FLAG_CACHEHIT)
305c06ce
JA
197 s->cachehit++;
198 else
199 s->cachemiss++;
c9fb4c5b
JA
200 reaped++;
201 head++;
c9fb4c5b
JA
202 } while (1);
203
204 s->inflight -= reaped;
c3e2fc25 205 *ring->head = head;
c9fb4c5b
JA
206 barrier();
207 return reaped;
208}
209
210static void *submitter_fn(void *data)
211{
212 struct submitter *s = data;
213 int fd, ret, prepped, flags;
214
215 printf("submitter=%d\n", gettid());
216
217 flags = O_RDONLY;
218 if (!buffered)
219 flags |= O_DIRECT;
220 fd = open(s->filename, flags);
221 if (fd < 0) {
222 perror("open");
223 goto done;
224 }
225
226 if (get_file_size(fd, &s->max_blocks)) {
227 printf("failed getting size of device/file\n");
228 goto err;
229 }
f310970e 230 if (s->max_blocks <= 1) {
c9fb4c5b
JA
231 printf("Zero file/device size?\n");
232 goto err;
233 }
c9fb4c5b
JA
234 s->max_blocks--;
235
236 srand48_r(pthread_self(), &s->rand);
237
238 prepped = 0;
239 do {
f310970e 240 int to_wait, to_submit, this_reap, to_prep;
c9fb4c5b 241
f310970e
JA
242 if (!prepped && s->inflight < DEPTH) {
243 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
244 prepped = prep_more_ios(s, fd, to_prep);
245 }
c9fb4c5b
JA
246 s->inflight += prepped;
247submit_more:
248 to_submit = prepped;
249submit:
250 if (s->inflight + BATCH_SUBMIT < DEPTH)
251 to_wait = 0;
252 else
253 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
254
f310970e
JA
255 ret = io_uring_enter(s, to_submit, to_wait,
256 IORING_ENTER_GETEVENTS);
c9fb4c5b
JA
257 s->calls++;
258
259 this_reap = reap_events(s);
260 if (this_reap == -1)
261 break;
262 s->reaps += this_reap;
263
264 if (ret >= 0) {
265 if (!ret) {
266 to_submit = 0;
267 if (s->inflight)
268 goto submit;
269 continue;
270 } else if (ret < to_submit) {
271 int diff = to_submit - ret;
272
273 s->done += ret;
274 prepped -= diff;
275 goto submit_more;
276 }
277 s->done += ret;
278 prepped = 0;
279 continue;
280 } else if (ret < 0) {
ac122fea 281 if (errno == EAGAIN) {
c9fb4c5b
JA
282 if (s->finish)
283 break;
284 if (this_reap)
285 goto submit;
c9fb4c5b
JA
286 to_submit = 0;
287 goto submit;
288 }
ac122fea 289 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
290 break;
291 }
292 } while (!s->finish);
293err:
294 close(fd);
295done:
296 finish = 1;
297 return NULL;
298}
299
300static void sig_int(int sig)
301{
302 printf("Exiting on signal %d\n", sig);
303 submitters[0].finish = 1;
304 finish = 1;
305}
306
307static void arm_sig_int(void)
308{
309 struct sigaction act;
310
311 memset(&act, 0, sizeof(act));
312 act.sa_handler = sig_int;
313 act.sa_flags = SA_RESTART;
314 sigaction(SIGINT, &act, NULL);
315}
316
c3e2fc25
JA
317static int setup_ring(struct submitter *s)
318{
e31b8288
JA
319 struct io_sq_ring *sring = &s->sq_ring;
320 struct io_cq_ring *cring = &s->cq_ring;
321 struct io_uring_params p;
c3e2fc25
JA
322 void *ptr;
323 int fd;
324
325 memset(&p, 0, sizeof(p));
326
c3e2fc25 327 if (polled)
e31b8288 328 p.flags |= IORING_SETUP_IOPOLL;
c3e2fc25 329 if (fixedbufs)
e31b8288 330 p.flags |= IORING_SETUP_FIXEDBUFS;
c3e2fc25 331 if (buffered)
e31b8288 332 p.flags |= IORING_SETUP_SQWQ;
c3e2fc25 333 else if (sq_thread) {
e31b8288 334 p.flags |= IORING_SETUP_SQTHREAD;
c3e2fc25
JA
335 p.sq_thread_cpu = sq_thread_cpu;
336 }
337
338 if (fixedbufs)
339 fd = io_uring_setup(DEPTH, s->iovecs, &p);
340 else
341 fd = io_uring_setup(DEPTH, NULL, &p);
342 if (fd < 0) {
343 perror("io_uring_setup");
344 return 1;
345 }
346
f310970e 347 s->ring_fd = fd;
ac122fea 348 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(u32),
f310970e
JA
349 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
350 IORING_OFF_SQ_RING);
c3e2fc25
JA
351 printf("sq_ring ptr = 0x%p\n", ptr);
352 sring->head = ptr + p.sq_off.head;
353 sring->tail = ptr + p.sq_off.tail;
354 sring->ring_mask = ptr + p.sq_off.ring_mask;
355 sring->ring_entries = ptr + p.sq_off.ring_entries;
ac122fea 356 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
357 sq_ring_mask = *sring->ring_mask;
358
e31b8288 359 s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb),
f310970e
JA
360 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
361 IORING_OFF_IOCB);
c3e2fc25
JA
362 printf("iocbs ptr = 0x%p\n", s->iocbs);
363
e31b8288 364 ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_uring_event),
f310970e
JA
365 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
366 IORING_OFF_CQ_RING);
c3e2fc25
JA
367 printf("cq_ring ptr = 0x%p\n", ptr);
368 cring->head = ptr + p.cq_off.head;
369 cring->tail = ptr + p.cq_off.tail;
370 cring->ring_mask = ptr + p.cq_off.ring_mask;
371 cring->ring_entries = ptr + p.cq_off.ring_entries;
ac122fea 372 cring->events = ptr + p.cq_off.events;
c3e2fc25
JA
373 cq_ring_mask = *cring->ring_mask;
374 return 0;
375}
376
c9fb4c5b
JA
377int main(int argc, char *argv[])
378{
379 struct submitter *s = &submitters[0];
305c06ce 380 unsigned long done, calls, reap, cache_hit, cache_miss;
c3e2fc25 381 int err, i;
c9fb4c5b 382 struct rlimit rlim;
c3e2fc25 383 void *ret;
c9fb4c5b
JA
384
385 if (argc < 2) {
386 printf("%s: filename\n", argv[0]);
387 return 1;
388 }
389
390 rlim.rlim_cur = RLIM_INFINITY;
391 rlim.rlim_max = RLIM_INFINITY;
392 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
393 perror("setrlimit");
394 return 1;
395 }
396
397 arm_sig_int();
398
c3e2fc25
JA
399 for (i = 0; i < DEPTH; i++) {
400 void *buf;
c9fb4c5b 401
c3e2fc25 402 if (posix_memalign(&buf, BS, BS)) {
c9fb4c5b
JA
403 printf("failed alloc\n");
404 return 1;
405 }
c3e2fc25
JA
406 s->iovecs[i].iov_base = buf;
407 s->iovecs[i].iov_len = BS;
c9fb4c5b
JA
408 }
409
c3e2fc25 410 err = setup_ring(s);
c9fb4c5b 411 if (err) {
c3e2fc25 412 printf("ring setup failed: %s, %d\n", strerror(errno), err);
c9fb4c5b
JA
413 return 1;
414 }
c3e2fc25
JA
415 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
416 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b
JA
417 strcpy(s->filename, argv[1]);
418
419 pthread_create(&s->thread, NULL, submitter_fn, s);
420
305c06ce 421 cache_hit = cache_miss = reap = calls = done = 0;
c9fb4c5b
JA
422 do {
423 unsigned long this_done = 0;
424 unsigned long this_reap = 0;
425 unsigned long this_call = 0;
305c06ce
JA
426 unsigned long this_cache_hit = 0;
427 unsigned long this_cache_miss = 0;
c9fb4c5b 428 unsigned long rpc = 0, ipc = 0;
305c06ce 429 double hit = 0.0;
c9fb4c5b
JA
430
431 sleep(1);
432 this_done += s->done;
433 this_call += s->calls;
434 this_reap += s->reaps;
305c06ce
JA
435 this_cache_hit += s->cachehit;
436 this_cache_miss += s->cachemiss;
437 if (this_cache_hit && this_cache_miss) {
438 unsigned long hits, total;
439
440 hits = this_cache_hit - cache_hit;
441 total = hits + this_cache_miss - cache_miss;
442 hit = (double) hits / (double) total;
443 hit *= 100.0;
444 }
c9fb4c5b
JA
445 if (this_call - calls) {
446 rpc = (this_done - done) / (this_call - calls);
447 ipc = (this_reap - reap) / (this_call - calls);
448 }
10c4d131 449 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
c9fb4c5b 450 this_done - done, rpc, ipc, s->inflight,
c3e2fc25 451 *s->cq_ring.head, *s->cq_ring.tail, hit);
c9fb4c5b
JA
452 done = this_done;
453 calls = this_call;
454 reap = this_reap;
305c06ce
JA
455 cache_hit = s->cachehit;
456 cache_miss = s->cachemiss;
c9fb4c5b
JA
457 } while (!finish);
458
459 pthread_join(s->thread, &ret);
e31b8288 460 close(s->ring_fd);
c9fb4c5b
JA
461 return 0;
462}