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