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 8
50 #define BATCH_COMPLETE 8
56 static unsigned sq_ring_mask, cq_ring_mask;
59 unsigned long max_blocks;
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;
75 unsigned long cachehit, cachemiss;
77 struct file files[MAX_FDS];
82 static struct submitter submitters[1];
83 static volatile int finish;
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_poll = 0; /* use kernel submission/poller thread */
89 static int sq_thread_cpu = -1; /* pin above thread to this CPU */
91 static int io_uring_register_buffers(struct submitter *s)
93 struct io_uring_register_buffers reg = {
98 return syscall(__NR_sys_io_uring_register, s->ring_fd,
99 IORING_REGISTER_BUFFERS, ®);
102 static int io_uring_setup(unsigned entries, struct io_uring_params *p)
104 return syscall(__NR_sys_io_uring_setup, entries, p);
107 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
108 unsigned int min_complete, unsigned int flags)
110 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
111 min_complete, flags);
114 static int gettid(void)
116 return syscall(__NR_gettid);
119 static void init_io(struct submitter *s, unsigned index)
121 struct io_uring_sqe *sqe = &s->sqes[index];
122 unsigned long offset;
126 f = &s->files[s->cur_file];
128 if (s->cur_file == s->nr_files)
131 lrand48_r(&s->rand, &r);
132 offset = (r % (f->max_blocks - 1)) * BS;
135 sqe->opcode = IORING_OP_READV;
137 sqe->addr = s->iovecs[index].iov_base;
139 sqe->buf_index = index;
140 sqe->flags |= IOSQE_FIXED_BUFFER;
142 sqe->addr = &s->iovecs[index];
151 static int prep_more_ios(struct submitter *s, int max_ios)
153 struct io_sq_ring *ring = &s->sq_ring;
154 unsigned index, tail, next_tail, prepped = 0;
156 next_tail = tail = *ring->tail;
160 if (next_tail == *ring->head)
163 index = tail & sq_ring_mask;
165 ring->array[index] = index;
168 } while (prepped < max_ios);
170 if (*ring->tail != tail) {
171 /* order tail store with writes to sqes above */
179 static int get_file_size(struct file *f)
183 if (fstat(f->fd, &st) < 0)
185 if (S_ISBLK(st.st_mode)) {
186 unsigned long long bytes;
188 if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0)
191 f->max_blocks = bytes / BS;
193 } else if (S_ISREG(st.st_mode)) {
194 f->max_blocks = st.st_size / BS;
201 static int reap_events(struct submitter *s)
203 struct io_cq_ring *ring = &s->cq_ring;
204 struct io_uring_cqe *cqe;
205 unsigned head, reaped = 0;
210 if (head == *ring->tail)
212 cqe = &ring->cqes[head & cq_ring_mask];
213 if (cqe->res != BS) {
214 printf("io: unexpected ret=%d\n", cqe->res);
217 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
225 s->inflight -= reaped;
231 static void *submitter_fn(void *data)
233 struct submitter *s = data;
236 printf("submitter=%d\n", gettid());
238 srand48_r(pthread_self(), &s->rand);
242 int to_wait, to_submit, this_reap, to_prep;
244 if (!prepped && s->inflight < DEPTH) {
245 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
246 prepped = prep_more_ios(s, to_prep);
248 s->inflight += prepped;
252 if (s->inflight + BATCH_SUBMIT < DEPTH)
255 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
257 ret = io_uring_enter(s, to_submit, to_wait,
258 IORING_ENTER_GETEVENTS);
261 this_reap = reap_events(s);
264 s->reaps += this_reap;
272 } else if (ret < to_submit) {
273 int diff = to_submit - ret;
282 } else if (ret < 0) {
283 if (errno == EAGAIN) {
291 printf("io_submit: %s\n", strerror(errno));
294 } while (!s->finish);
300 static void sig_int(int sig)
302 printf("Exiting on signal %d\n", sig);
303 submitters[0].finish = 1;
307 static void arm_sig_int(void)
309 struct sigaction act;
311 memset(&act, 0, sizeof(act));
312 act.sa_handler = sig_int;
313 act.sa_flags = SA_RESTART;
314 sigaction(SIGINT, &act, NULL);
317 static int setup_ring(struct submitter *s)
319 struct io_sq_ring *sring = &s->sq_ring;
320 struct io_cq_ring *cring = &s->cq_ring;
321 struct io_uring_params p;
325 memset(&p, 0, sizeof(p));
327 if (sq_thread_poll) {
328 p.flags |= IORING_SETUP_SQPOLL;
329 if (sq_thread_cpu != -1)
330 p.flags |= IORING_SETUP_SQ_AFF;
333 fd = io_uring_setup(DEPTH, &p);
335 perror("io_uring_setup");
341 ret = io_uring_register_buffers(s);
343 perror("io_uring_register");
348 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
349 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
351 printf("sq_ring ptr = 0x%p\n", ptr);
352 sring->head = ptr + p.sq_off.head;
353 sring->tail = ptr + p.sq_off.tail;
354 sring->ring_mask = ptr + p.sq_off.ring_mask;
355 sring->ring_entries = ptr + p.sq_off.ring_entries;
356 sring->array = ptr + p.sq_off.array;
357 sq_ring_mask = *sring->ring_mask;
359 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
360 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
362 printf("sqes ptr = 0x%p\n", s->sqes);
364 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
365 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
367 printf("cq_ring ptr = 0x%p\n", ptr);
368 cring->head = ptr + p.cq_off.head;
369 cring->tail = ptr + p.cq_off.tail;
370 cring->ring_mask = ptr + p.cq_off.ring_mask;
371 cring->ring_entries = ptr + p.cq_off.ring_entries;
372 cring->cqes = ptr + p.cq_off.cqes;
373 cq_ring_mask = *cring->ring_mask;
377 int main(int argc, char *argv[])
379 struct submitter *s = &submitters[0];
380 unsigned long done, calls, reap, cache_hit, cache_miss;
381 int err, i, flags, fd;
386 printf("%s: filename\n", argv[0]);
396 struct file *f = &s->files[s->nr_files];
398 fd = open(argv[i], flags);
404 if (get_file_size(f)) {
405 printf("failed getting size of device/file\n");
408 if (f->max_blocks <= 1) {
409 printf("Zero file/device size?\n");
414 printf("Added file %s\n", argv[i]);
419 rlim.rlim_cur = RLIM_INFINITY;
420 rlim.rlim_max = RLIM_INFINITY;
421 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
428 for (i = 0; i < DEPTH; i++) {
431 if (posix_memalign(&buf, BS, BS)) {
432 printf("failed alloc\n");
435 s->iovecs[i].iov_base = buf;
436 s->iovecs[i].iov_len = BS;
441 printf("ring setup failed: %s, %d\n", strerror(errno), err);
444 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
445 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
447 pthread_create(&s->thread, NULL, submitter_fn, s);
449 cache_hit = cache_miss = reap = calls = done = 0;
451 unsigned long this_done = 0;
452 unsigned long this_reap = 0;
453 unsigned long this_call = 0;
454 unsigned long this_cache_hit = 0;
455 unsigned long this_cache_miss = 0;
456 unsigned long rpc = 0, ipc = 0;
460 this_done += s->done;
461 this_call += s->calls;
462 this_reap += s->reaps;
463 this_cache_hit += s->cachehit;
464 this_cache_miss += s->cachemiss;
465 if (this_cache_hit && this_cache_miss) {
466 unsigned long hits, total;
468 hits = this_cache_hit - cache_hit;
469 total = hits + this_cache_miss - cache_miss;
470 hit = (double) hits / (double) total;
473 if (this_call - calls) {
474 rpc = (this_done - done) / (this_call - calls);
475 ipc = (this_reap - reap) / (this_call - calls);
477 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
478 this_done - done, rpc, ipc, s->inflight,
479 *s->cq_ring.head, *s->cq_ring.tail, hit);
483 cache_hit = s->cachehit;
484 cache_miss = s->cachemiss;
487 pthread_join(s->thread, &ret);