io_uring: io_uring_setup(2) takes a 'nr_iovecs' field now
[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 = 0;       /* use kernel submission thread */
89 static int sq_thread_cpu = 0;   /* pin above thread to this CPU */
90
91 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
92                           unsigned nr_iovecs, struct io_uring_params *p)
93 {
94         return syscall(__NR_sys_io_uring_setup, entries, iovecs, nr_iovecs, p);
95 }
96
97 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
98                           unsigned int min_complete, unsigned int flags)
99 {
100         return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
101                         min_complete, flags);
102 }
103
104 static int gettid(void)
105 {
106         return syscall(__NR_gettid);
107 }
108
109 static void init_io(struct submitter *s, unsigned index)
110 {
111         struct io_uring_sqe *sqe = &s->sqes[index];
112         unsigned long offset;
113         struct file *f;
114         long r;
115
116         f = &s->files[s->cur_file];
117         s->cur_file++;
118         if (s->cur_file == s->nr_files)
119                 s->cur_file = 0;
120
121         lrand48_r(&s->rand, &r);
122         offset = (r % (f->max_blocks - 1)) * BS;
123
124         if (fixedbufs) {
125                 sqe->opcode = IORING_OP_READ_FIXED;
126                 sqe->addr = s->iovecs[index].iov_base;
127                 sqe->len = BS;
128                 sqe->index = index;
129         } else {
130                 sqe->opcode = IORING_OP_READV;
131                 sqe->addr = &s->iovecs[index];
132                 sqe->len = 1;
133         }
134         sqe->flags = 0;
135         sqe->ioprio = 0;
136         sqe->fd = f->fd;
137         sqe->off = offset;
138 }
139
140 static int prep_more_ios(struct submitter *s, int max_ios)
141 {
142         struct io_sq_ring *ring = &s->sq_ring;
143         unsigned index, tail, next_tail, prepped = 0;
144
145         next_tail = tail = *ring->tail;
146         do {
147                 next_tail++;
148                 barrier();
149                 if (next_tail == *ring->head)
150                         break;
151
152                 index = tail & sq_ring_mask;
153                 init_io(s, index);
154                 ring->array[index] = index;
155                 prepped++;
156                 tail = next_tail;
157         } while (prepped < max_ios);
158
159         if (*ring->tail != tail) {
160                 /* order tail store with writes to sqes above */
161                 barrier();
162                 *ring->tail = tail;
163                 barrier();
164         }
165         return prepped;
166 }
167
168 static int get_file_size(struct file *f)
169 {
170         struct stat st;
171
172         if (fstat(f->fd, &st) < 0)
173                 return -1;
174         if (S_ISBLK(st.st_mode)) {
175                 unsigned long long bytes;
176
177                 if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0)
178                         return -1;
179
180                 f->max_blocks = bytes / BS;
181                 return 0;
182         } else if (S_ISREG(st.st_mode)) {
183                 f->max_blocks = st.st_size / BS;
184                 return 0;
185         }
186
187         return -1;
188 }
189
190 static int reap_events(struct submitter *s)
191 {
192         struct io_cq_ring *ring = &s->cq_ring;
193         struct io_uring_cqe *cqe;
194         unsigned head, reaped = 0;
195
196         head = *ring->head;
197         do {
198                 barrier();
199                 if (head == *ring->tail)
200                         break;
201                 cqe = &ring->cqes[head & cq_ring_mask];
202                 if (cqe->res != BS) {
203                         printf("io: unexpected ret=%d\n", cqe->res);
204                         return -1;
205                 }
206                 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
207                         s->cachehit++;
208                 else
209                         s->cachemiss++;
210                 reaped++;
211                 head++;
212         } while (1);
213
214         s->inflight -= reaped;
215         *ring->head = head;
216         barrier();
217         return reaped;
218 }
219
220 static void *submitter_fn(void *data)
221 {
222         struct submitter *s = data;
223         int ret, prepped;
224
225         printf("submitter=%d\n", gettid());
226
227         srand48_r(pthread_self(), &s->rand);
228
229         prepped = 0;
230         do {
231                 int to_wait, to_submit, this_reap, to_prep;
232
233                 if (!prepped && s->inflight < DEPTH) {
234                         to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
235                         prepped = prep_more_ios(s, to_prep);
236                 }
237                 s->inflight += prepped;
238 submit_more:
239                 to_submit = prepped;
240 submit:
241                 if (s->inflight + BATCH_SUBMIT < DEPTH)
242                         to_wait = 0;
243                 else
244                         to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
245
246                 ret = io_uring_enter(s, to_submit, to_wait,
247                                         IORING_ENTER_GETEVENTS);
248                 s->calls++;
249
250                 this_reap = reap_events(s);
251                 if (this_reap == -1)
252                         break;
253                 s->reaps += this_reap;
254
255                 if (ret >= 0) {
256                         if (!ret) {
257                                 to_submit = 0;
258                                 if (s->inflight)
259                                         goto submit;
260                                 continue;
261                         } else if (ret < to_submit) {
262                                 int diff = to_submit - ret;
263
264                                 s->done += ret;
265                                 prepped -= diff;
266                                 goto submit_more;
267                         }
268                         s->done += ret;
269                         prepped = 0;
270                         continue;
271                 } else if (ret < 0) {
272                         if (errno == EAGAIN) {
273                                 if (s->finish)
274                                         break;
275                                 if (this_reap)
276                                         goto submit;
277                                 to_submit = 0;
278                                 goto submit;
279                         }
280                         printf("io_submit: %s\n", strerror(errno));
281                         break;
282                 }
283         } while (!s->finish);
284
285         finish = 1;
286         return NULL;
287 }
288
289 static void sig_int(int sig)
290 {
291         printf("Exiting on signal %d\n", sig);
292         submitters[0].finish = 1;
293         finish = 1;
294 }
295
296 static void arm_sig_int(void)
297 {
298         struct sigaction act;
299
300         memset(&act, 0, sizeof(act));
301         act.sa_handler = sig_int;
302         act.sa_flags = SA_RESTART;
303         sigaction(SIGINT, &act, NULL);
304 }
305
306 static int setup_ring(struct submitter *s)
307 {
308         struct io_sq_ring *sring = &s->sq_ring;
309         struct io_cq_ring *cring = &s->cq_ring;
310         struct io_uring_params p;
311         void *ptr;
312         int fd;
313
314         memset(&p, 0, sizeof(p));
315
316         if (polled)
317                 p.flags |= IORING_SETUP_IOPOLL;
318         if (buffered)
319                 p.flags |= IORING_SETUP_SQWQ;
320         else if (sq_thread) {
321                 p.flags |= IORING_SETUP_SQTHREAD;
322                 p.sq_thread_cpu = sq_thread_cpu;
323         }
324
325         if (fixedbufs)
326                 fd = io_uring_setup(DEPTH, s->iovecs, DEPTH, &p);
327         else
328                 fd = io_uring_setup(DEPTH, NULL, 0, &p);
329         if (fd < 0) {
330                 perror("io_uring_setup");
331                 return 1;
332         }
333
334         s->ring_fd = fd;
335         ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
336                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
337                         IORING_OFF_SQ_RING);
338         printf("sq_ring ptr = 0x%p\n", ptr);
339         sring->head = ptr + p.sq_off.head;
340         sring->tail = ptr + p.sq_off.tail;
341         sring->ring_mask = ptr + p.sq_off.ring_mask;
342         sring->ring_entries = ptr + p.sq_off.ring_entries;
343         sring->array = ptr + p.sq_off.array;
344         sq_ring_mask = *sring->ring_mask;
345
346         s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
347                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
348                         IORING_OFF_SQES);
349         printf("sqes ptr    = 0x%p\n", s->sqes);
350
351         ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
352                         PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
353                         IORING_OFF_CQ_RING);
354         printf("cq_ring ptr = 0x%p\n", ptr);
355         cring->head = ptr + p.cq_off.head;
356         cring->tail = ptr + p.cq_off.tail;
357         cring->ring_mask = ptr + p.cq_off.ring_mask;
358         cring->ring_entries = ptr + p.cq_off.ring_entries;
359         cring->cqes = ptr + p.cq_off.cqes;
360         cq_ring_mask = *cring->ring_mask;
361         return 0;
362 }
363
364 int main(int argc, char *argv[])
365 {
366         struct submitter *s = &submitters[0];
367         unsigned long done, calls, reap, cache_hit, cache_miss;
368         int err, i, flags, fd;
369         struct rlimit rlim;
370         void *ret;
371
372         if (argc < 2) {
373                 printf("%s: filename\n", argv[0]);
374                 return 1;
375         }
376
377         flags = O_RDONLY;
378         if (!buffered)
379                 flags |= O_DIRECT;
380
381         i = 1;
382         while (i < argc) {
383                 struct file *f = &s->files[s->nr_files];
384
385                 fd = open(argv[i], flags);
386                 if (fd < 0) {
387                         perror("open");
388                         return 1;
389                 }
390                 f->fd = fd;
391                 if (get_file_size(f)) {
392                         printf("failed getting size of device/file\n");
393                         return 1;
394                 }
395                 if (f->max_blocks <= 1) {
396                         printf("Zero file/device size?\n");
397                         return 1;
398                 }
399                 f->max_blocks--;
400
401                 printf("Added file %s\n", argv[i]);
402                 s->nr_files++;
403                 i++;
404         }
405
406         rlim.rlim_cur = RLIM_INFINITY;
407         rlim.rlim_max = RLIM_INFINITY;
408         if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
409                 perror("setrlimit");
410                 return 1;
411         }
412
413         arm_sig_int();
414
415         for (i = 0; i < DEPTH; i++) {
416                 void *buf;
417
418                 if (posix_memalign(&buf, BS, BS)) {
419                         printf("failed alloc\n");
420                         return 1;
421                 }
422                 s->iovecs[i].iov_base = buf;
423                 s->iovecs[i].iov_len = BS;
424         }
425
426         err = setup_ring(s);
427         if (err) {
428                 printf("ring setup failed: %s, %d\n", strerror(errno), err);
429                 return 1;
430         }
431         printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
432         printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
433
434         pthread_create(&s->thread, NULL, submitter_fn, s);
435
436         cache_hit = cache_miss = reap = calls = done = 0;
437         do {
438                 unsigned long this_done = 0;
439                 unsigned long this_reap = 0;
440                 unsigned long this_call = 0;
441                 unsigned long this_cache_hit = 0;
442                 unsigned long this_cache_miss = 0;
443                 unsigned long rpc = 0, ipc = 0;
444                 double hit = 0.0;
445
446                 sleep(1);
447                 this_done += s->done;
448                 this_call += s->calls;
449                 this_reap += s->reaps;
450                 this_cache_hit += s->cachehit;
451                 this_cache_miss += s->cachemiss;
452                 if (this_cache_hit && this_cache_miss) {
453                         unsigned long hits, total;
454
455                         hits = this_cache_hit - cache_hit;
456                         total = hits + this_cache_miss - cache_miss;
457                         hit = (double) hits / (double) total;
458                         hit *= 100.0;
459                 }
460                 if (this_call - calls) {
461                         rpc = (this_done - done) / (this_call - calls);
462                         ipc = (this_reap - reap) / (this_call - calls);
463                 }
464                 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
465                                 this_done - done, rpc, ipc, s->inflight,
466                                 *s->cq_ring.head, *s->cq_ring.tail, hit);
467                 done = this_done;
468                 calls = this_call;
469                 reap = this_reap;
470                 cache_hit = s->cachehit;
471                 cache_miss = s->cachemiss;
472         } while (!finish);
473
474         pthread_join(s->thread, &ret);
475         close(s->ring_fd);
476         return 0;
477 }