11 #include <sys/ioctl.h>
12 #include <sys/syscall.h>
13 #include <sys/resource.h>
23 #include "../arch/arch.h"
24 #include "../os/io_uring.h"
26 #define barrier() __asm__ __volatile__("": : :"memory")
28 #define min(a, b) ((a < b) ? (a) : (b))
34 unsigned *ring_entries;
42 unsigned *ring_entries;
43 struct io_uring_event *events;
48 #define BATCH_SUBMIT 8
49 #define BATCH_COMPLETE 8
53 static unsigned sq_ring_mask, cq_ring_mask;
57 unsigned long max_blocks;
59 struct drand48_data rand;
60 struct io_sq_ring sq_ring;
61 struct io_uring_iocb *iocbs;
62 struct iovec iovecs[DEPTH];
63 struct io_cq_ring cq_ring;
68 unsigned long cachehit, cachemiss;
73 static struct submitter submitters[1];
74 static volatile int finish;
76 static int polled = 0; /* use IO polling */
77 static int fixedbufs = 0; /* use fixed user buffers */
78 static int buffered = 1; /* use buffered IO, not O_DIRECT */
79 static int sq_thread = 0; /* use kernel submission thread */
80 static int sq_thread_cpu = 0; /* pin above thread to this CPU */
82 static int io_uring_setup(unsigned entries, struct iovec *iovecs,
83 struct io_uring_params *p)
85 return syscall(__NR_sys_io_uring_setup, entries, iovecs, p);
88 static int io_uring_enter(struct submitter *s, unsigned int to_submit,
89 unsigned int min_complete, unsigned int flags)
91 return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit,
95 static int gettid(void)
97 return syscall(__NR_gettid);
100 static void init_io(struct submitter *s, int fd, unsigned index)
102 struct io_uring_iocb *iocb = &s->iocbs[index];
103 unsigned long offset;
106 lrand48_r(&s->rand, &r);
107 offset = (r % (s->max_blocks - 1)) * BS;
109 iocb->opcode = IORING_OP_READ;
114 iocb->addr = s->iovecs[index].iov_base;
118 static int prep_more_ios(struct submitter *s, int fd, int max_ios)
120 struct io_sq_ring *ring = &s->sq_ring;
121 unsigned index, tail, next_tail, prepped = 0;
123 next_tail = tail = *ring->tail;
127 if (next_tail == *ring->head)
130 index = tail & sq_ring_mask;
131 init_io(s, fd, index);
132 ring->array[index] = index;
135 } while (prepped < max_ios);
137 if (*ring->tail != tail) {
138 /* order tail store with writes to iocbs above */
146 static int get_file_size(int fd, unsigned long *blocks)
150 if (fstat(fd, &st) < 0)
152 if (S_ISBLK(st.st_mode)) {
153 unsigned long long bytes;
155 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
158 *blocks = bytes / BS;
160 } else if (S_ISREG(st.st_mode)) {
161 *blocks = st.st_size / BS;
168 static int reap_events(struct submitter *s)
170 struct io_cq_ring *ring = &s->cq_ring;
171 struct io_uring_event *ev;
172 unsigned head, reaped = 0;
177 if (head == *ring->tail)
179 ev = &ring->events[head & cq_ring_mask];
181 struct io_uring_iocb *iocb = &s->iocbs[ev->index];
183 printf("io: unexpected ret=%d\n", ev->res);
184 printf("offset=%lu, size=%lu\n",
185 (unsigned long) iocb->off,
186 (unsigned long) iocb->len);
189 if (ev->flags & IOEV_FLAG_CACHEHIT)
197 s->inflight -= reaped;
203 static void *submitter_fn(void *data)
205 struct submitter *s = data;
206 int fd, ret, prepped, flags;
208 printf("submitter=%d\n", gettid());
213 fd = open(s->filename, flags);
219 if (get_file_size(fd, &s->max_blocks)) {
220 printf("failed getting size of device/file\n");
223 if (s->max_blocks <= 1) {
224 printf("Zero file/device size?\n");
229 srand48_r(pthread_self(), &s->rand);
233 int to_wait, to_submit, this_reap, to_prep;
235 if (!prepped && s->inflight < DEPTH) {
236 to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
237 prepped = prep_more_ios(s, fd, to_prep);
239 s->inflight += prepped;
243 if (s->inflight + BATCH_SUBMIT < DEPTH)
246 to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
248 ret = io_uring_enter(s, to_submit, to_wait,
249 IORING_ENTER_GETEVENTS);
252 this_reap = reap_events(s);
255 s->reaps += this_reap;
263 } else if (ret < to_submit) {
264 int diff = to_submit - ret;
273 } else if (ret < 0) {
274 if (errno == EAGAIN) {
282 printf("io_submit: %s\n", strerror(errno));
285 } while (!s->finish);
293 static void sig_int(int sig)
295 printf("Exiting on signal %d\n", sig);
296 submitters[0].finish = 1;
300 static void arm_sig_int(void)
302 struct sigaction act;
304 memset(&act, 0, sizeof(act));
305 act.sa_handler = sig_int;
306 act.sa_flags = SA_RESTART;
307 sigaction(SIGINT, &act, NULL);
310 static int setup_ring(struct submitter *s)
312 struct io_sq_ring *sring = &s->sq_ring;
313 struct io_cq_ring *cring = &s->cq_ring;
314 struct io_uring_params p;
318 memset(&p, 0, sizeof(p));
321 p.flags |= IORING_SETUP_IOPOLL;
323 p.flags |= IORING_SETUP_FIXEDBUFS;
325 p.flags |= IORING_SETUP_SQWQ;
326 else if (sq_thread) {
327 p.flags |= IORING_SETUP_SQTHREAD;
328 p.sq_thread_cpu = sq_thread_cpu;
332 fd = io_uring_setup(DEPTH, s->iovecs, &p);
334 fd = io_uring_setup(DEPTH, NULL, &p);
336 perror("io_uring_setup");
341 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
342 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
344 printf("sq_ring ptr = 0x%p\n", ptr);
345 sring->head = ptr + p.sq_off.head;
346 sring->tail = ptr + p.sq_off.tail;
347 sring->ring_mask = ptr + p.sq_off.ring_mask;
348 sring->ring_entries = ptr + p.sq_off.ring_entries;
349 sring->array = ptr + p.sq_off.array;
350 sq_ring_mask = *sring->ring_mask;
352 s->iocbs = mmap(0, p.sq_entries * sizeof(struct io_uring_iocb),
353 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
355 printf("iocbs ptr = 0x%p\n", s->iocbs);
357 ptr = mmap(0, p.cq_off.events + p.cq_entries * sizeof(struct io_uring_event),
358 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
360 printf("cq_ring ptr = 0x%p\n", ptr);
361 cring->head = ptr + p.cq_off.head;
362 cring->tail = ptr + p.cq_off.tail;
363 cring->ring_mask = ptr + p.cq_off.ring_mask;
364 cring->ring_entries = ptr + p.cq_off.ring_entries;
365 cring->events = ptr + p.cq_off.events;
366 cq_ring_mask = *cring->ring_mask;
370 int main(int argc, char *argv[])
372 struct submitter *s = &submitters[0];
373 unsigned long done, calls, reap, cache_hit, cache_miss;
379 printf("%s: filename\n", argv[0]);
383 rlim.rlim_cur = RLIM_INFINITY;
384 rlim.rlim_max = RLIM_INFINITY;
385 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
392 for (i = 0; i < DEPTH; i++) {
395 if (posix_memalign(&buf, BS, BS)) {
396 printf("failed alloc\n");
399 s->iovecs[i].iov_base = buf;
400 s->iovecs[i].iov_len = BS;
405 printf("ring setup failed: %s, %d\n", strerror(errno), err);
408 printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
409 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
410 strcpy(s->filename, argv[1]);
412 pthread_create(&s->thread, NULL, submitter_fn, s);
414 cache_hit = cache_miss = reap = calls = done = 0;
416 unsigned long this_done = 0;
417 unsigned long this_reap = 0;
418 unsigned long this_call = 0;
419 unsigned long this_cache_hit = 0;
420 unsigned long this_cache_miss = 0;
421 unsigned long rpc = 0, ipc = 0;
425 this_done += s->done;
426 this_call += s->calls;
427 this_reap += s->reaps;
428 this_cache_hit += s->cachehit;
429 this_cache_miss += s->cachemiss;
430 if (this_cache_hit && this_cache_miss) {
431 unsigned long hits, total;
433 hits = this_cache_hit - cache_hit;
434 total = hits + this_cache_miss - cache_miss;
435 hit = (double) hits / (double) total;
438 if (this_call - calls) {
439 rpc = (this_done - done) / (this_call - calls);
440 ipc = (this_reap - reap) / (this_call - calls);
442 printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n",
443 this_done - done, rpc, ipc, s->inflight,
444 *s->cq_ring.head, *s->cq_ring.tail, hit);
448 cache_hit = s->cachehit;
449 cache_miss = s->cachemiss;
452 pthread_join(s->thread, &ret);