11 #include <sys/ioctl.h>
12 #include <sys/syscall.h>
13 #include <sys/resource.h>
23 #include "../arch/arch.h"
24 #include "../lib/types.h"
25 #include "../os/io_uring.h"
27 #define barrier() __asm__ __volatile__("": : :"memory")
29 #define min(a, b) ((a < b) ? (a) : (b))
35 unsigned *ring_entries;
43 unsigned *ring_entries;
44 struct io_uring_event *events;
49 #define BATCH_SUBMIT 8
50 #define BATCH_COMPLETE 8
54 static unsigned sq_ring_mask, cq_ring_mask;
58 unsigned long max_blocks;
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;
69 unsigned long cachehit, cachemiss;
74 static struct submitter submitters[1];
75 static volatile int finish;
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 */
83 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
84 struct io_uring_params *p)
86 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
89 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
90 unsigned int min_complete, unsigned int flags)
92 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
96 static int gettid(void)
98 return syscall(__NR_gettid);
101 static void init_io(struct submitter *s, int fd, unsigned index)
103 struct io_uring_iocb *iocb = &s->iocbs[index];
104 unsigned long offset;
107 lrand48_r(&s->rand, &r);
108 offset = (r % (s->max_blocks - 1)) * BS;
111 iocb->opcode = IORING_OP_READ_FIXED;
113 iocb->opcode = IORING_OP_READ;
118 iocb->addr = s->iovecs[index].iov_base;
122 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
124 struct io_sq_ring *ring = &s->sq_ring;
125 unsigned index, tail, next_tail, prepped = 0;
127 next_tail = tail = *ring->tail;
131 if (next_tail == *ring->head)
134 index = tail & sq_ring_mask;
135 init_io(s, fd, index);
136 ring->array[index] = index;
139 } while (prepped < max_ios);
141 if (*ring->tail != tail) {
142 /* order tail store with writes to iocbs above */
150 static int get_file_size(int fd, unsigned long *blocks)
154 if (fstat(fd, &st) < 0)
156 if (S_ISBLK(st.st_mode)) {
157 unsigned long long bytes;
159 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
162 *blocks = bytes / BS;
164 } else if (S_ISREG(st.st_mode)) {
165 *blocks = st.st_size / BS;
172 static int reap_events(struct submitter *s)
174 struct io_cq_ring *ring = &s->cq_ring;
175 struct io_uring_event *ev;
176 unsigned head, reaped = 0;
181 if (head == *ring->tail)
183 ev = &ring->events[head & cq_ring_mask];
185 struct io_uring_iocb *iocb = &s->iocbs[ev->index];
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);
193 if (ev->flags & IOEV_FLAG_CACHEHIT)
201 s->inflight -= reaped;
207 static void *submitter_fn(void *data)
209 struct submitter *s = data;
210 int fd, ret, prepped, flags;
212 printf("submitter=%d\n", gettid());
217 fd = open(s->filename, flags);
223 if (get_file_size(fd, &s->max_blocks)) {
224 printf("failed getting size of device/file\n");
227 if (s->max_blocks <= 1) {
228 printf("Zero file/device size?\n");
233 srand48_r(pthread_self(), &s->rand);
237 int to_wait, to_submit, this_reap, to_prep;
239 if (!prepped && s->inflight < DEPTH) {
240 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
241 prepped = prep_more_ios(s, fd, to_prep);
243 s->inflight += prepped;
247 if (s->inflight + BATCH_SUBMIT < DEPTH)
250 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
252 ret = io_uring_enter(s, to_submit, to_wait,
253 IORING_ENTER_GETEVENTS);
256 this_reap = reap_events(s);
259 s->reaps += this_reap;
267 } else if (ret < to_submit) {
268 int diff = to_submit - ret;
277 } else if (ret < 0) {
278 if (errno == EAGAIN) {
286 printf("io_submit: %s\n", strerror(errno));
289 } while (!s->finish);
297 static void sig_int(int sig)
299 printf("Exiting on signal %d\n", sig);
300 submitters[0].finish = 1;
304 static void arm_sig_int(void)
306 struct sigaction act;
308 memset(&act, 0, sizeof(act));
309 act.sa_handler = sig_int;
310 act.sa_flags = SA_RESTART;
311 sigaction(SIGINT, &act, NULL);
314 static int setup_ring(struct submitter *s)
316 struct io_sq_ring *sring = &s->sq_ring;
317 struct io_cq_ring *cring = &s->cq_ring;
318 struct io_uring_params p;
322 memset(&p, 0, sizeof(p));
325 p.flags |= IORING_SETUP_IOPOLL;
327 p.flags |= IORING_SETUP_FIXEDBUFS;
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;
336 fd = io_uring_setup(DEPTH, s->iovecs, &p);
338 fd = io_uring_setup(DEPTH, NULL, &p);
340 perror("io_uring_setup");
345 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
346 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
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;
356 s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb),
357 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
359 printf("iocbs ptr = 0x%p\n", s->iocbs);
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,
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;
374 int main(int argc, char *argv[])
376 struct submitter *s = &submitters[0];
377 unsigned long done, calls, reap, cache_hit, cache_miss;
383 printf("%s: filename\n", argv[0]);
387 rlim.rlim_cur = RLIM_INFINITY;
388 rlim.rlim_max = RLIM_INFINITY;
389 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
396 for (i = 0; i < DEPTH; i++) {
399 if (posix_memalign(&buf, BS, BS)) {
400 printf("failed alloc\n");
403 s->iovecs[i].iov_base = buf;
404 s->iovecs[i].iov_len = BS;
409 printf("ring setup failed: %s, %d\n", strerror(errno), err);
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]);
416 pthread_create(&s->thread, NULL, submitter_fn, s);
418 cache_hit = cache_miss = reap = calls = done = 0;
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;
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;
437 hits = this_cache_hit - cache_hit;
438 total = hits + this_cache_miss - cache_miss;
439 hit = (double) hits / (double) total;
442 if (this_call - calls) {
443 rpc = (this_done - done) / (this_call - calls);
444 ipc = (this_reap - reap) / (this_call - calls);
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);
452 cache_hit = s->cachehit;
453 cache_miss = s->cachemiss;
456 pthread_join(s->thread, &ret);