Commit | Line | Data |
---|---|---|
c9fb4c5b JA |
1 | #include <stdio.h> |
2 | #include <errno.h> | |
3 | #include <assert.h> | |
4 | #include <stdlib.h> | |
5 | #include <stddef.h> | |
6 | #include <signal.h> | |
7 | #include <inttypes.h> | |
8 | ||
9 | #include <sys/types.h> | |
10 | #include <sys/stat.h> | |
11 | #include <sys/ioctl.h> | |
12 | #include <sys/syscall.h> | |
13 | #include <sys/resource.h> | |
c3e2fc25 | 14 | #include <sys/mman.h> |
e31b8288 | 15 | #include <sys/uio.h> |
c9fb4c5b JA |
16 | #include <linux/fs.h> |
17 | #include <fcntl.h> | |
18 | #include <unistd.h> | |
c9fb4c5b JA |
19 | #include <string.h> |
20 | #include <pthread.h> | |
21 | #include <sched.h> | |
22 | ||
74efb029 | 23 | #include "../arch/arch.h" |
57fa61f0 | 24 | #include "../lib/types.h" |
e31b8288 | 25 | #include "../os/io_uring.h" |
ac122fea | 26 | |
e31b8288 | 27 | #define barrier() __asm__ __volatile__("": : :"memory") |
c3e2fc25 | 28 | |
e31b8288 | 29 | #define min(a, b) ((a < b) ? (a) : (b)) |
c3e2fc25 | 30 | |
e31b8288 | 31 | struct io_sq_ring { |
e2239016 JA |
32 | unsigned *head; |
33 | unsigned *tail; | |
34 | unsigned *ring_mask; | |
35 | unsigned *ring_entries; | |
36 | unsigned *array; | |
c9fb4c5b JA |
37 | }; |
38 | ||
e31b8288 | 39 | struct io_cq_ring { |
e2239016 JA |
40 | unsigned *head; |
41 | unsigned *tail; | |
42 | unsigned *ring_mask; | |
43 | unsigned *ring_entries; | |
f0403f94 | 44 | struct io_uring_cqe *cqes; |
c9fb4c5b JA |
45 | }; |
46 | ||
c9fb4c5b | 47 | #define DEPTH 32 |
c9fb4c5b JA |
48 | |
49 | #define BATCH_SUBMIT 8 | |
50 | #define BATCH_COMPLETE 8 | |
51 | ||
52 | #define BS 4096 | |
53 | ||
a7086591 JA |
54 | #define MAX_FDS 16 |
55 | ||
c3e2fc25 | 56 | static unsigned sq_ring_mask, cq_ring_mask; |
e39c34dc | 57 | |
a7086591 JA |
58 | struct file { |
59 | unsigned long max_blocks; | |
60 | int fd; | |
61 | }; | |
62 | ||
c9fb4c5b JA |
63 | struct submitter { |
64 | pthread_t thread; | |
f310970e | 65 | int ring_fd; |
c9fb4c5b | 66 | struct drand48_data rand; |
e31b8288 | 67 | struct io_sq_ring sq_ring; |
f0403f94 | 68 | struct io_uring_sqe *sqes; |
c3e2fc25 | 69 | struct iovec iovecs[DEPTH]; |
e31b8288 | 70 | struct io_cq_ring cq_ring; |
c9fb4c5b JA |
71 | int inflight; |
72 | unsigned long reaps; | |
73 | unsigned long done; | |
74 | unsigned long calls; | |
305c06ce | 75 | unsigned long cachehit, cachemiss; |
c9fb4c5b | 76 | volatile int finish; |
a7086591 JA |
77 | struct file files[MAX_FDS]; |
78 | unsigned nr_files; | |
79 | unsigned cur_file; | |
c9fb4c5b JA |
80 | }; |
81 | ||
82 | static struct submitter submitters[1]; | |
83 | static volatile int finish; | |
84 | ||
f0403f94 | 85 | static int polled = 1; /* use IO polling */ |
c3e2fc25 | 86 | static int fixedbufs = 0; /* use fixed user buffers */ |
f0403f94 | 87 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
3d7d00a3 JA |
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 */ | |
c9fb4c5b | 90 | |
2ea53ca3 | 91 | static int io_uring_register_buffers(struct submitter *s) |
c9fb4c5b | 92 | { |
2ea53ca3 JA |
93 | struct io_uring_register_buffers reg = { |
94 | .iovecs = s->iovecs, | |
95 | .nr_iovecs = DEPTH | |
96 | }; | |
97 | ||
98 | return syscall(__NR_sys_io_uring_register, s->ring_fd, | |
99 | IORING_REGISTER_BUFFERS, ®); | |
100 | } | |
101 | ||
102 | static int io_uring_setup(unsigned entries, struct io_uring_params *p) | |
103 | { | |
104 | return syscall(__NR_sys_io_uring_setup, entries, p); | |
c9fb4c5b JA |
105 | } |
106 | ||
c3e2fc25 JA |
107 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
108 | unsigned int min_complete, unsigned int flags) | |
c9fb4c5b | 109 | { |
f310970e JA |
110 | return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit, |
111 | min_complete, flags); | |
c9fb4c5b JA |
112 | } |
113 | ||
114 | static int gettid(void) | |
115 | { | |
116 | return syscall(__NR_gettid); | |
117 | } | |
118 | ||
a7086591 | 119 | static void init_io(struct submitter *s, unsigned index) |
c9fb4c5b | 120 | { |
f0403f94 | 121 | struct io_uring_sqe *sqe = &s->sqes[index]; |
c9fb4c5b | 122 | unsigned long offset; |
a7086591 | 123 | struct file *f; |
c9fb4c5b JA |
124 | long r; |
125 | ||
a7086591 JA |
126 | f = &s->files[s->cur_file]; |
127 | s->cur_file++; | |
128 | if (s->cur_file == s->nr_files) | |
129 | s->cur_file = 0; | |
130 | ||
c9fb4c5b | 131 | lrand48_r(&s->rand, &r); |
a7086591 | 132 | offset = (r % (f->max_blocks - 1)) * BS; |
c9fb4c5b | 133 | |
2ea53ca3 JA |
134 | sqe->flags = 0; |
135 | sqe->opcode = IORING_OP_READV; | |
f0403f94 | 136 | if (fixedbufs) { |
f0403f94 JA |
137 | sqe->addr = s->iovecs[index].iov_base; |
138 | sqe->len = BS; | |
2ea53ca3 JA |
139 | sqe->buf_index = index; |
140 | sqe->flags |= IOSQE_FIXED_BUFFER; | |
f0403f94 | 141 | } else { |
f0403f94 JA |
142 | sqe->addr = &s->iovecs[index]; |
143 | sqe->len = 1; | |
2ea53ca3 | 144 | sqe->buf_index = 0; |
f0403f94 | 145 | } |
f0403f94 | 146 | sqe->ioprio = 0; |
a7086591 | 147 | sqe->fd = f->fd; |
f0403f94 | 148 | sqe->off = offset; |
c9fb4c5b JA |
149 | } |
150 | ||
a7086591 | 151 | static int prep_more_ios(struct submitter *s, int max_ios) |
c9fb4c5b | 152 | { |
e31b8288 | 153 | struct io_sq_ring *ring = &s->sq_ring; |
e2239016 | 154 | unsigned index, tail, next_tail, prepped = 0; |
c9fb4c5b | 155 | |
c3e2fc25 | 156 | next_tail = tail = *ring->tail; |
c9fb4c5b JA |
157 | do { |
158 | next_tail++; | |
c9fb4c5b | 159 | barrier(); |
c3e2fc25 | 160 | if (next_tail == *ring->head) |
c9fb4c5b JA |
161 | break; |
162 | ||
e39c34dc | 163 | index = tail & sq_ring_mask; |
a7086591 | 164 | init_io(s, index); |
c3e2fc25 | 165 | ring->array[index] = index; |
c9fb4c5b JA |
166 | prepped++; |
167 | tail = next_tail; | |
168 | } while (prepped < max_ios); | |
169 | ||
c3e2fc25 | 170 | if (*ring->tail != tail) { |
f0403f94 | 171 | /* order tail store with writes to sqes above */ |
c9fb4c5b | 172 | barrier(); |
c3e2fc25 | 173 | *ring->tail = tail; |
c9fb4c5b JA |
174 | barrier(); |
175 | } | |
176 | return prepped; | |
177 | } | |
178 | ||
a7086591 | 179 | static int get_file_size(struct file *f) |
c9fb4c5b JA |
180 | { |
181 | struct stat st; | |
182 | ||
a7086591 | 183 | if (fstat(f->fd, &st) < 0) |
c9fb4c5b JA |
184 | return -1; |
185 | if (S_ISBLK(st.st_mode)) { | |
186 | unsigned long long bytes; | |
187 | ||
a7086591 | 188 | if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0) |
c9fb4c5b JA |
189 | return -1; |
190 | ||
a7086591 | 191 | f->max_blocks = bytes / BS; |
c9fb4c5b JA |
192 | return 0; |
193 | } else if (S_ISREG(st.st_mode)) { | |
a7086591 | 194 | f->max_blocks = st.st_size / BS; |
c9fb4c5b JA |
195 | return 0; |
196 | } | |
197 | ||
198 | return -1; | |
199 | } | |
200 | ||
201 | static int reap_events(struct submitter *s) | |
202 | { | |
e31b8288 | 203 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 204 | struct io_uring_cqe *cqe; |
e2239016 | 205 | unsigned head, reaped = 0; |
c9fb4c5b | 206 | |
c3e2fc25 | 207 | head = *ring->head; |
c9fb4c5b JA |
208 | do { |
209 | barrier(); | |
c3e2fc25 | 210 | if (head == *ring->tail) |
c9fb4c5b | 211 | break; |
f0403f94 JA |
212 | cqe = &ring->cqes[head & cq_ring_mask]; |
213 | if (cqe->res != BS) { | |
f0403f94 | 214 | printf("io: unexpected ret=%d\n", cqe->res); |
c9fb4c5b JA |
215 | return -1; |
216 | } | |
f0403f94 | 217 | if (cqe->flags & IOCQE_FLAG_CACHEHIT) |
305c06ce JA |
218 | s->cachehit++; |
219 | else | |
220 | s->cachemiss++; | |
c9fb4c5b JA |
221 | reaped++; |
222 | head++; | |
c9fb4c5b JA |
223 | } while (1); |
224 | ||
225 | s->inflight -= reaped; | |
c3e2fc25 | 226 | *ring->head = head; |
c9fb4c5b JA |
227 | barrier(); |
228 | return reaped; | |
229 | } | |
230 | ||
231 | static void *submitter_fn(void *data) | |
232 | { | |
233 | struct submitter *s = data; | |
a7086591 | 234 | int ret, prepped; |
c9fb4c5b JA |
235 | |
236 | printf("submitter=%d\n", gettid()); | |
237 | ||
c9fb4c5b JA |
238 | srand48_r(pthread_self(), &s->rand); |
239 | ||
240 | prepped = 0; | |
241 | do { | |
f310970e | 242 | int to_wait, to_submit, this_reap, to_prep; |
c9fb4c5b | 243 | |
f310970e JA |
244 | if (!prepped && s->inflight < DEPTH) { |
245 | to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT); | |
a7086591 | 246 | prepped = prep_more_ios(s, to_prep); |
f310970e | 247 | } |
c9fb4c5b JA |
248 | s->inflight += prepped; |
249 | submit_more: | |
250 | to_submit = prepped; | |
251 | submit: | |
252 | if (s->inflight + BATCH_SUBMIT < DEPTH) | |
253 | to_wait = 0; | |
254 | else | |
255 | to_wait = min(s->inflight + to_submit, BATCH_COMPLETE); | |
256 | ||
f310970e JA |
257 | ret = io_uring_enter(s, to_submit, to_wait, |
258 | IORING_ENTER_GETEVENTS); | |
c9fb4c5b JA |
259 | s->calls++; |
260 | ||
261 | this_reap = reap_events(s); | |
262 | if (this_reap == -1) | |
263 | break; | |
264 | s->reaps += this_reap; | |
265 | ||
266 | if (ret >= 0) { | |
267 | if (!ret) { | |
268 | to_submit = 0; | |
269 | if (s->inflight) | |
270 | goto submit; | |
271 | continue; | |
272 | } else if (ret < to_submit) { | |
273 | int diff = to_submit - ret; | |
274 | ||
275 | s->done += ret; | |
276 | prepped -= diff; | |
277 | goto submit_more; | |
278 | } | |
279 | s->done += ret; | |
280 | prepped = 0; | |
281 | continue; | |
282 | } else if (ret < 0) { | |
ac122fea | 283 | if (errno == EAGAIN) { |
c9fb4c5b JA |
284 | if (s->finish) |
285 | break; | |
286 | if (this_reap) | |
287 | goto submit; | |
c9fb4c5b JA |
288 | to_submit = 0; |
289 | goto submit; | |
290 | } | |
ac122fea | 291 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
292 | break; |
293 | } | |
294 | } while (!s->finish); | |
a7086591 | 295 | |
c9fb4c5b JA |
296 | finish = 1; |
297 | return NULL; | |
298 | } | |
299 | ||
300 | static void sig_int(int sig) | |
301 | { | |
302 | printf("Exiting on signal %d\n", sig); | |
303 | submitters[0].finish = 1; | |
304 | finish = 1; | |
305 | } | |
306 | ||
307 | static void arm_sig_int(void) | |
308 | { | |
309 | struct sigaction act; | |
310 | ||
311 | memset(&act, 0, sizeof(act)); | |
312 | act.sa_handler = sig_int; | |
313 | act.sa_flags = SA_RESTART; | |
314 | sigaction(SIGINT, &act, NULL); | |
315 | } | |
316 | ||
c3e2fc25 JA |
317 | static int setup_ring(struct submitter *s) |
318 | { | |
e31b8288 JA |
319 | struct io_sq_ring *sring = &s->sq_ring; |
320 | struct io_cq_ring *cring = &s->cq_ring; | |
321 | struct io_uring_params p; | |
2ea53ca3 | 322 | int ret, fd; |
c3e2fc25 | 323 | void *ptr; |
c3e2fc25 JA |
324 | |
325 | memset(&p, 0, sizeof(p)); | |
326 | ||
3d7d00a3 | 327 | if (sq_thread_poll) { |
2ea53ca3 | 328 | p.flags |= IORING_SETUP_SQPOLL; |
3d7d00a3 JA |
329 | if (sq_thread_cpu != -1) |
330 | p.flags |= IORING_SETUP_SQ_AFF; | |
c3e2fc25 JA |
331 | } |
332 | ||
2ea53ca3 | 333 | fd = io_uring_setup(DEPTH, &p); |
c3e2fc25 JA |
334 | if (fd < 0) { |
335 | perror("io_uring_setup"); | |
336 | return 1; | |
337 | } | |
f310970e | 338 | s->ring_fd = fd; |
2ea53ca3 JA |
339 | |
340 | if (fixedbufs) { | |
341 | ret = io_uring_register_buffers(s); | |
342 | if (ret < 0) { | |
343 | perror("io_uring_register"); | |
344 | return 1; | |
345 | } | |
346 | } | |
347 | ||
e2239016 | 348 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
349 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
350 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
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; | |
ac122fea | 356 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
357 | sq_ring_mask = *sring->ring_mask; |
358 | ||
f0403f94 | 359 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 360 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 JA |
361 | IORING_OFF_SQES); |
362 | printf("sqes ptr = 0x%p\n", s->sqes); | |
c3e2fc25 | 363 | |
f0403f94 | 364 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
365 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
366 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
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; | |
f0403f94 | 372 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
373 | cq_ring_mask = *cring->ring_mask; |
374 | return 0; | |
375 | } | |
376 | ||
c9fb4c5b JA |
377 | int main(int argc, char *argv[]) |
378 | { | |
379 | struct submitter *s = &submitters[0]; | |
305c06ce | 380 | unsigned long done, calls, reap, cache_hit, cache_miss; |
a7086591 | 381 | int err, i, flags, fd; |
c9fb4c5b | 382 | struct rlimit rlim; |
c3e2fc25 | 383 | void *ret; |
c9fb4c5b JA |
384 | |
385 | if (argc < 2) { | |
386 | printf("%s: filename\n", argv[0]); | |
387 | return 1; | |
388 | } | |
389 | ||
a7086591 JA |
390 | flags = O_RDONLY; |
391 | if (!buffered) | |
392 | flags |= O_DIRECT; | |
393 | ||
394 | i = 1; | |
395 | while (i < argc) { | |
396 | struct file *f = &s->files[s->nr_files]; | |
397 | ||
398 | fd = open(argv[i], flags); | |
399 | if (fd < 0) { | |
400 | perror("open"); | |
401 | return 1; | |
402 | } | |
403 | f->fd = fd; | |
404 | if (get_file_size(f)) { | |
405 | printf("failed getting size of device/file\n"); | |
406 | return 1; | |
407 | } | |
408 | if (f->max_blocks <= 1) { | |
409 | printf("Zero file/device size?\n"); | |
410 | return 1; | |
411 | } | |
412 | f->max_blocks--; | |
413 | ||
414 | printf("Added file %s\n", argv[i]); | |
415 | s->nr_files++; | |
416 | i++; | |
417 | } | |
418 | ||
c9fb4c5b JA |
419 | rlim.rlim_cur = RLIM_INFINITY; |
420 | rlim.rlim_max = RLIM_INFINITY; | |
421 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
422 | perror("setrlimit"); | |
423 | return 1; | |
424 | } | |
425 | ||
426 | arm_sig_int(); | |
427 | ||
c3e2fc25 JA |
428 | for (i = 0; i < DEPTH; i++) { |
429 | void *buf; | |
c9fb4c5b | 430 | |
c3e2fc25 | 431 | if (posix_memalign(&buf, BS, BS)) { |
c9fb4c5b JA |
432 | printf("failed alloc\n"); |
433 | return 1; | |
434 | } | |
c3e2fc25 JA |
435 | s->iovecs[i].iov_base = buf; |
436 | s->iovecs[i].iov_len = BS; | |
c9fb4c5b JA |
437 | } |
438 | ||
c3e2fc25 | 439 | err = setup_ring(s); |
c9fb4c5b | 440 | if (err) { |
c3e2fc25 | 441 | printf("ring setup failed: %s, %d\n", strerror(errno), err); |
c9fb4c5b JA |
442 | return 1; |
443 | } | |
c3e2fc25 JA |
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); | |
c9fb4c5b JA |
446 | |
447 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
448 | ||
305c06ce | 449 | cache_hit = cache_miss = reap = calls = done = 0; |
c9fb4c5b JA |
450 | do { |
451 | unsigned long this_done = 0; | |
452 | unsigned long this_reap = 0; | |
453 | unsigned long this_call = 0; | |
305c06ce JA |
454 | unsigned long this_cache_hit = 0; |
455 | unsigned long this_cache_miss = 0; | |
c9fb4c5b | 456 | unsigned long rpc = 0, ipc = 0; |
305c06ce | 457 | double hit = 0.0; |
c9fb4c5b JA |
458 | |
459 | sleep(1); | |
460 | this_done += s->done; | |
461 | this_call += s->calls; | |
462 | this_reap += s->reaps; | |
305c06ce JA |
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; | |
467 | ||
468 | hits = this_cache_hit - cache_hit; | |
469 | total = hits + this_cache_miss - cache_miss; | |
470 | hit = (double) hits / (double) total; | |
471 | hit *= 100.0; | |
472 | } | |
c9fb4c5b JA |
473 | if (this_call - calls) { |
474 | rpc = (this_done - done) / (this_call - calls); | |
475 | ipc = (this_reap - reap) / (this_call - calls); | |
476 | } | |
10c4d131 | 477 | printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n", |
c9fb4c5b | 478 | this_done - done, rpc, ipc, s->inflight, |
c3e2fc25 | 479 | *s->cq_ring.head, *s->cq_ring.tail, hit); |
c9fb4c5b JA |
480 | done = this_done; |
481 | calls = this_call; | |
482 | reap = this_reap; | |
305c06ce JA |
483 | cache_hit = s->cachehit; |
484 | cache_miss = s->cachemiss; | |
c9fb4c5b JA |
485 | } while (!finish); |
486 | ||
487 | pthread_join(s->thread, &ret); | |
e31b8288 | 488 | close(s->ring_fd); |
c9fb4c5b JA |
489 | return 0; |
490 | } |