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