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_cqe *cqes;
49 #define BATCH_SUBMIT 64
50 #define BATCH_COMPLETE 64
56 static unsigned sq_ring_mask, cq_ring_mask;
59 unsigned long max_blocks;
67 struct drand48_data rand;
68 struct io_sq_ring sq_ring;
69 struct io_uring_sqe *sqes;
70 struct iovec iovecs[DEPTH];
71 struct io_cq_ring cq_ring;
76 unsigned long cachehit, cachemiss;
79 struct file files[MAX_FDS];
84 static struct submitter submitters[1];
85 static volatile int finish;
87 static int polled = 1; /* use IO polling */
88 static int fixedbufs = 1; /* use fixed user buffers */
89 static int buffered = 0; /* use buffered IO, not O_DIRECT */
90 static int sq_thread_poll = 0; /* use kernel submission/poller thread */
91 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
93 static int io_uring_register_buffers(struct submitter *s)
95 struct io_uring_register_buffers reg = {
100 return syscall(__NR_sys_io_uring_register, s->ring_fd,
101 IORING_REGISTER_BUFFERS, ®);
104 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
106 return syscall(__NR_sys_io_uring_setup, entries, p);
109 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
110 unsigned int min_complete, unsigned int flags)
112 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
113 min_complete, flags);
116 static int gettid(void)
118 return syscall(__NR_gettid);
121 static unsigned file_depth(struct submitter *s)
123 return (DEPTH + s->nr_files - 1) / s->nr_files;
126 static void init_io(struct submitter *s, unsigned index)
128 struct io_uring_sqe *sqe = &s->sqes[index];
129 unsigned long offset;
133 if (s->nr_files == 1) {
136 f = &s->files[s->cur_file];
137 if (f->pending_ios >= file_depth(s)) {
139 if (s->cur_file == s->nr_files)
145 lrand48_r(&s->rand, &r);
146 offset = (r % (f->max_blocks - 1)) * BS;
149 sqe->opcode = IORING_OP_READV;
151 sqe->addr = s->iovecs[index].iov_base;
153 sqe->buf_index = index;
154 sqe->flags |= IOSQE_FIXED_BUFFER;
156 sqe->addr = &s->iovecs[index];
163 sqe->data = (unsigned long) f;
166 static int prep_more_ios(struct submitter *s, int max_ios)
168 struct io_sq_ring *ring = &s->sq_ring;
169 unsigned index, tail, next_tail, prepped = 0;
171 next_tail = tail = *ring->tail;
175 if (next_tail == *ring->head)
178 index = tail & sq_ring_mask;
180 ring->array[index] = index;
183 } while (prepped < max_ios);
185 if (*ring->tail != tail) {
186 /* order tail store with writes to sqes above */
194 static int get_file_size(struct file *f)
198 if (fstat(f->fd, &st) < 0)
200 if (S_ISBLK(st.st_mode)) {
201 unsigned long long bytes;
203 if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0)
206 f->max_blocks = bytes / BS;
208 } else if (S_ISREG(st.st_mode)) {
209 f->max_blocks = st.st_size / BS;
216 static int reap_events(struct submitter *s)
218 struct io_cq_ring *ring = &s->cq_ring;
219 struct io_uring_cqe *cqe;
220 unsigned head, reaped = 0;
227 if (head == *ring->tail)
229 cqe = &ring->cqes[head & cq_ring_mask];
230 f = (struct file *) cqe->data;
232 if (cqe->res != BS) {
233 printf("io: unexpected ret=%d\n", cqe->res);
236 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
244 s->inflight -= reaped;
250 static void *submitter_fn(void *data)
252 struct submitter *s = data;
255 printf("submitter=%d\n", gettid());
257 srand48_r(pthread_self(), &s->rand);
261 int to_wait, to_submit, this_reap, to_prep;
263 if (!prepped && s->inflight < DEPTH) {
264 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
265 prepped = prep_more_ios(s, to_prep);
267 s->inflight += prepped;
271 if (s->inflight + BATCH_SUBMIT < DEPTH)
274 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
276 ret = io_uring_enter(s, to_submit, to_wait,
277 IORING_ENTER_GETEVENTS);
280 this_reap = reap_events(s);
283 s->reaps += this_reap;
291 } else if (ret < to_submit) {
292 int diff = to_submit - ret;
301 } else if (ret < 0) {
302 if (errno == EAGAIN) {
310 printf("io_submit: %s\n", strerror(errno));
313 } while (!s->finish);
319 static void sig_int(int sig)
321 printf("Exiting on signal %d\n", sig);
322 submitters[0].finish = 1;
326 static void arm_sig_int(void)
328 struct sigaction act;
330 memset(&act, 0, sizeof(act));
331 act.sa_handler = sig_int;
332 act.sa_flags = SA_RESTART;
333 sigaction(SIGINT, &act, NULL);
336 static int setup_ring(struct submitter *s)
338 struct io_sq_ring *sring = &s->sq_ring;
339 struct io_cq_ring *cring = &s->cq_ring;
340 struct io_uring_params p;
344 memset(&p, 0, sizeof(p));
347 p.flags |= IORING_SETUP_IOPOLL;
348 if (sq_thread_poll) {
349 p.flags |= IORING_SETUP_SQPOLL;
350 if (sq_thread_cpu != -1)
351 p.flags |= IORING_SETUP_SQ_AFF;
354 fd = io_uring_setup(DEPTH, &p);
356 perror("io_uring_setup");
362 ret = io_uring_register_buffers(s);
364 perror("io_uring_register");
369 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
370 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
372 printf("sq_ring ptr = 0x%p\n", ptr);
373 sring->head = ptr + p.sq_off.head;
374 sring->tail = ptr + p.sq_off.tail;
375 sring->ring_mask = ptr + p.sq_off.ring_mask;
376 sring->ring_entries = ptr + p.sq_off.ring_entries;
377 sring->array = ptr + p.sq_off.array;
378 sq_ring_mask = *sring->ring_mask;
380 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
381 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
383 printf("sqes ptr = 0x%p\n", s->sqes);
385 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
386 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
388 printf("cq_ring ptr = 0x%p\n", ptr);
389 cring->head = ptr + p.cq_off.head;
390 cring->tail = ptr + p.cq_off.tail;
391 cring->ring_mask = ptr + p.cq_off.ring_mask;
392 cring->ring_entries = ptr + p.cq_off.ring_entries;
393 cring->cqes = ptr + p.cq_off.cqes;
394 cq_ring_mask = *cring->ring_mask;
398 int main(int argc, char *argv[])
400 struct submitter *s = &submitters[0];
401 unsigned long done, calls, reap, cache_hit, cache_miss;
402 int err, i, flags, fd;
407 printf("%s: filename\n", argv[0]);
411 flags = O_RDONLY | O_NOATIME;
417 struct file *f = &s->files[s->nr_files];
419 fd = open(argv[i], flags);
425 if (get_file_size(f)) {
426 printf("failed getting size of device/file\n");
429 if (f->max_blocks <= 1) {
430 printf("Zero file/device size?\n");
435 printf("Added file %s\n", argv[i]);
440 rlim.rlim_cur = RLIM_INFINITY;
441 rlim.rlim_max = RLIM_INFINITY;
442 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
449 for (i = 0; i < DEPTH; i++) {
452 if (posix_memalign(&buf, BS, BS)) {
453 printf("failed alloc\n");
456 s->iovecs[i].iov_base = buf;
457 s->iovecs[i].iov_len = BS;
462 printf("ring setup failed: %s, %d\n", strerror(errno), err);
465 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
466 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
468 pthread_create(&s->thread, NULL, submitter_fn, s);
470 cache_hit = cache_miss = reap = calls = done = 0;
472 unsigned long this_done = 0;
473 unsigned long this_reap = 0;
474 unsigned long this_call = 0;
475 unsigned long this_cache_hit = 0;
476 unsigned long this_cache_miss = 0;
477 unsigned long rpc = 0, ipc = 0;
481 this_done += s->done;
482 this_call += s->calls;
483 this_reap += s->reaps;
484 this_cache_hit += s->cachehit;
485 this_cache_miss += s->cachemiss;
486 if (this_cache_hit && this_cache_miss) {
487 unsigned long hits, total;
489 hits = this_cache_hit - cache_hit;
490 total = hits + this_cache_miss - cache_miss;
491 hit = (double) hits / (double) total;
494 if (this_call - calls) {
495 rpc = (this_done - done) / (this_call - calls);
496 ipc = (this_reap - reap) / (this_call - calls);
498 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
499 this_done - done, rpc, ipc, s->inflight,
500 *s->cq_ring.head, *s->cq_ring.tail, hit);
504 cache_hit = s->cachehit;
505 cache_miss = s->cachemiss;
508 pthread_join(s->thread, &ret);