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;
44 unsigned *ring_entries;
45 struct io_uring_cqe *cqes;
50 #define BATCH_SUBMIT 64
51 #define BATCH_COMPLETE 64
57 static unsigned sq_ring_mask, cq_ring_mask;
60 unsigned long max_blocks;
68 struct drand48_data rand;
69 struct io_sq_ring sq_ring;
70 struct io_uring_sqe *sqes;
71 struct iovec iovecs[DEPTH];
72 struct io_cq_ring cq_ring;
77 unsigned long cachehit, cachemiss;
80 struct file files[MAX_FDS];
85 static struct submitter submitters[1];
86 static volatile int finish;
88 static int polled = 1; /* use IO polling */
89 static int fixedbufs = 1; /* use fixed user buffers */
90 static int buffered = 0; /* use buffered IO, not O_DIRECT */
91 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
92 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
94 static int io_uring_register_buffers(struct submitter *s)
96 struct io_uring_register_buffers reg = {
101 return syscall(__NR_sys_io_uring_register, s->ring_fd,
102 IORING_REGISTER_BUFFERS, ®);
105 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
107 return syscall(__NR_sys_io_uring_setup, entries, p);
110 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
111 unsigned int min_complete, unsigned int flags)
113 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
114 min_complete, flags);
117 static int gettid(void)
119 return syscall(__NR_gettid);
122 static unsigned file_depth(struct submitter *s)
124 return (DEPTH + s->nr_files - 1) / s->nr_files;
127 static void init_io(struct submitter *s, unsigned index)
129 struct io_uring_sqe *sqe = &s->sqes[index];
130 unsigned long offset;
134 if (s->nr_files == 1) {
137 f = &s->files[s->cur_file];
138 if (f->pending_ios >= file_depth(s)) {
140 if (s->cur_file == s->nr_files)
146 lrand48_r(&s->rand, &r);
147 offset = (r % (f->max_blocks - 1)) * BS;
150 sqe->opcode = IORING_OP_READV;
152 sqe->addr = s->iovecs[index].iov_base;
154 sqe->buf_index = index;
155 sqe->flags |= IOSQE_FIXED_BUFFER;
157 sqe->addr = &s->iovecs[index];
164 sqe->data = (unsigned long) f;
167 static int prep_more_ios(struct submitter *s, int max_ios)
169 struct io_sq_ring *ring = &s->sq_ring;
170 unsigned index, tail, next_tail, prepped = 0;
172 next_tail = tail = *ring->tail;
176 if (next_tail == *ring->head)
179 index = tail & sq_ring_mask;
181 ring->array[index] = index;
184 } while (prepped < max_ios);
186 if (*ring->tail != tail) {
187 /* order tail store with writes to sqes above */
195 static int get_file_size(struct file *f)
199 if (fstat(f->fd, &st) < 0)
201 if (S_ISBLK(st.st_mode)) {
202 unsigned long long bytes;
204 if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0)
207 f->max_blocks = bytes / BS;
209 } else if (S_ISREG(st.st_mode)) {
210 f->max_blocks = st.st_size / BS;
217 static int reap_events(struct submitter *s)
219 struct io_cq_ring *ring = &s->cq_ring;
220 struct io_uring_cqe *cqe;
221 unsigned head, reaped = 0;
228 if (head == *ring->tail)
230 cqe = &ring->cqes[head & cq_ring_mask];
231 f = (struct file *) cqe->data;
233 if (cqe->res != BS) {
234 printf("io: unexpected ret=%d\n", cqe->res);
237 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
245 s->inflight -= reaped;
251 static void *submitter_fn(void *data)
253 struct submitter *s = data;
254 struct io_sq_ring *ring = &s->sq_ring;
257 printf("submitter=%d\n", gettid());
259 srand48_r(pthread_self(), &s->rand);
263 int to_wait, to_submit, this_reap, to_prep;
265 if (!prepped && s->inflight < DEPTH) {
266 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
267 prepped = prep_more_ios(s, to_prep);
269 s->inflight += prepped;
273 if (s->inflight + BATCH_SUBMIT < DEPTH)
276 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
279 * Only need to call io_uring_enter if we're not using SQ thread
280 * poll, or if IORING_SQ_NEED_WAKEUP is set.
282 if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
283 ret = io_uring_enter(s, to_submit, to_wait,
284 IORING_ENTER_GETEVENTS);
289 * For non SQ thread poll, we already got the events we needed
290 * through the io_uring_enter() above. For SQ thread poll, we
291 * need to loop here until we find enough events.
301 } while (sq_thread_poll && this_reap < to_wait);
302 s->reaps += this_reap;
310 } else if (ret < to_submit) {
311 int diff = to_submit - ret;
320 } else if (ret < 0) {
321 if (errno == EAGAIN) {
329 printf("io_submit: %s\n", strerror(errno));
332 } while (!s->finish);
338 static void sig_int(int sig)
340 printf("Exiting on signal %d\n", sig);
341 submitters[0].finish = 1;
345 static void arm_sig_int(void)
347 struct sigaction act;
349 memset(&act, 0, sizeof(act));
350 act.sa_handler = sig_int;
351 act.sa_flags = SA_RESTART;
352 sigaction(SIGINT, &act, NULL);
355 static int setup_ring(struct submitter *s)
357 struct io_sq_ring *sring = &s->sq_ring;
358 struct io_cq_ring *cring = &s->cq_ring;
359 struct io_uring_params p;
363 memset(&p, 0, sizeof(p));
366 p.flags |= IORING_SETUP_IOPOLL;
367 if (sq_thread_poll) {
368 p.flags |= IORING_SETUP_SQPOLL;
369 if (sq_thread_cpu != -1)
370 p.flags |= IORING_SETUP_SQ_AFF;
373 fd = io_uring_setup(DEPTH, &p);
375 perror("io_uring_setup");
381 ret = io_uring_register_buffers(s);
383 perror("io_uring_register");
388 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
389 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
391 printf("sq_ring ptr = 0x%p\n", ptr);
392 sring->head = ptr + p.sq_off.head;
393 sring->tail = ptr + p.sq_off.tail;
394 sring->ring_mask = ptr + p.sq_off.ring_mask;
395 sring->ring_entries = ptr + p.sq_off.ring_entries;
396 sring->flags = ptr + p.sq_off.flags;
397 sring->array = ptr + p.sq_off.array;
398 sq_ring_mask = *sring->ring_mask;
400 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
401 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
403 printf("sqes ptr = 0x%p\n", s->sqes);
405 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
406 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
408 printf("cq_ring ptr = 0x%p\n", ptr);
409 cring->head = ptr + p.cq_off.head;
410 cring->tail = ptr + p.cq_off.tail;
411 cring->ring_mask = ptr + p.cq_off.ring_mask;
412 cring->ring_entries = ptr + p.cq_off.ring_entries;
413 cring->cqes = ptr + p.cq_off.cqes;
414 cq_ring_mask = *cring->ring_mask;
418 int main(int argc, char *argv[])
420 struct submitter *s = &submitters[0];
421 unsigned long done, calls, reap, cache_hit, cache_miss;
422 int err, i, flags, fd;
427 printf("%s: filename\n", argv[0]);
431 flags = O_RDONLY | O_NOATIME;
437 struct file *f = &s->files[s->nr_files];
439 fd = open(argv[i], flags);
445 if (get_file_size(f)) {
446 printf("failed getting size of device/file\n");
449 if (f->max_blocks <= 1) {
450 printf("Zero file/device size?\n");
455 printf("Added file %s\n", argv[i]);
460 rlim.rlim_cur = RLIM_INFINITY;
461 rlim.rlim_max = RLIM_INFINITY;
462 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
469 for (i = 0; i < DEPTH; i++) {
472 if (posix_memalign(&buf, BS, BS)) {
473 printf("failed alloc\n");
476 s->iovecs[i].iov_base = buf;
477 s->iovecs[i].iov_len = BS;
482 printf("ring setup failed: %s, %d\n", strerror(errno), err);
485 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
486 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
488 pthread_create(&s->thread, NULL, submitter_fn, s);
490 cache_hit = cache_miss = reap = calls = done = 0;
492 unsigned long this_done = 0;
493 unsigned long this_reap = 0;
494 unsigned long this_call = 0;
495 unsigned long this_cache_hit = 0;
496 unsigned long this_cache_miss = 0;
497 unsigned long rpc = 0, ipc = 0;
501 this_done += s->done;
502 this_call += s->calls;
503 this_reap += s->reaps;
504 this_cache_hit += s->cachehit;
505 this_cache_miss += s->cachemiss;
506 if (this_cache_hit && this_cache_miss) {
507 unsigned long hits, total;
509 hits = this_cache_hit - cache_hit;
510 total = hits + this_cache_miss - cache_miss;
511 hit = (double) hits / (double) total;
514 if (this_call - calls) {
515 rpc = (this_done - done) / (this_call - calls);
516 ipc = (this_reap - reap) / (this_call - calls);
519 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
520 this_done - done, rpc, ipc, s->inflight,
521 *s->cq_ring.head, *s->cq_ring.tail, hit);
525 cache_hit = s->cachehit;
526 cache_miss = s->cachemiss;
529 pthread_join(s->thread, &ret);