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 */ |
0527b23f JA |
88 | static int sq_thread = 0; /* use kernel submission thread */ |
89 | static int sq_thread_cpu = 0; /* pin above thread to this CPU */ | |
c9fb4c5b | 90 | |
c3e2fc25 | 91 | static int io_uring_setup(unsigned entries, struct iovec *iovecs, |
411a0821 | 92 | unsigned nr_iovecs, struct io_uring_params *p) |
c9fb4c5b | 93 | { |
411a0821 | 94 | return syscall(__NR_sys_io_uring_setup, entries, iovecs, nr_iovecs, p); |
c9fb4c5b JA |
95 | } |
96 | ||
c3e2fc25 JA |
97 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
98 | unsigned int min_complete, unsigned int flags) | |
c9fb4c5b | 99 | { |
f310970e JA |
100 | return syscall(__NR_sys_io_uring_enter, s->ring_fd, to_submit, |
101 | min_complete, flags); | |
c9fb4c5b JA |
102 | } |
103 | ||
104 | static int gettid(void) | |
105 | { | |
106 | return syscall(__NR_gettid); | |
107 | } | |
108 | ||
a7086591 | 109 | static void init_io(struct submitter *s, unsigned index) |
c9fb4c5b | 110 | { |
f0403f94 | 111 | struct io_uring_sqe *sqe = &s->sqes[index]; |
c9fb4c5b | 112 | unsigned long offset; |
a7086591 | 113 | struct file *f; |
c9fb4c5b JA |
114 | long r; |
115 | ||
a7086591 JA |
116 | f = &s->files[s->cur_file]; |
117 | s->cur_file++; | |
118 | if (s->cur_file == s->nr_files) | |
119 | s->cur_file = 0; | |
120 | ||
c9fb4c5b | 121 | lrand48_r(&s->rand, &r); |
a7086591 | 122 | offset = (r % (f->max_blocks - 1)) * BS; |
c9fb4c5b | 123 | |
f0403f94 JA |
124 | if (fixedbufs) { |
125 | sqe->opcode = IORING_OP_READ_FIXED; | |
126 | sqe->addr = s->iovecs[index].iov_base; | |
127 | sqe->len = BS; | |
a7086591 | 128 | sqe->index = index; |
f0403f94 JA |
129 | } else { |
130 | sqe->opcode = IORING_OP_READV; | |
131 | sqe->addr = &s->iovecs[index]; | |
132 | sqe->len = 1; | |
133 | } | |
134 | sqe->flags = 0; | |
135 | sqe->ioprio = 0; | |
a7086591 | 136 | sqe->fd = f->fd; |
f0403f94 | 137 | sqe->off = offset; |
c9fb4c5b JA |
138 | } |
139 | ||
a7086591 | 140 | static int prep_more_ios(struct submitter *s, int max_ios) |
c9fb4c5b | 141 | { |
e31b8288 | 142 | struct io_sq_ring *ring = &s->sq_ring; |
e2239016 | 143 | unsigned index, tail, next_tail, prepped = 0; |
c9fb4c5b | 144 | |
c3e2fc25 | 145 | next_tail = tail = *ring->tail; |
c9fb4c5b JA |
146 | do { |
147 | next_tail++; | |
c9fb4c5b | 148 | barrier(); |
c3e2fc25 | 149 | if (next_tail == *ring->head) |
c9fb4c5b JA |
150 | break; |
151 | ||
e39c34dc | 152 | index = tail & sq_ring_mask; |
a7086591 | 153 | init_io(s, index); |
c3e2fc25 | 154 | ring->array[index] = index; |
c9fb4c5b JA |
155 | prepped++; |
156 | tail = next_tail; | |
157 | } while (prepped < max_ios); | |
158 | ||
c3e2fc25 | 159 | if (*ring->tail != tail) { |
f0403f94 | 160 | /* order tail store with writes to sqes above */ |
c9fb4c5b | 161 | barrier(); |
c3e2fc25 | 162 | *ring->tail = tail; |
c9fb4c5b JA |
163 | barrier(); |
164 | } | |
165 | return prepped; | |
166 | } | |
167 | ||
a7086591 | 168 | static int get_file_size(struct file *f) |
c9fb4c5b JA |
169 | { |
170 | struct stat st; | |
171 | ||
a7086591 | 172 | if (fstat(f->fd, &st) < 0) |
c9fb4c5b JA |
173 | return -1; |
174 | if (S_ISBLK(st.st_mode)) { | |
175 | unsigned long long bytes; | |
176 | ||
a7086591 | 177 | if (ioctl(f->fd, BLKGETSIZE64, &bytes) != 0) |
c9fb4c5b JA |
178 | return -1; |
179 | ||
a7086591 | 180 | f->max_blocks = bytes / BS; |
c9fb4c5b JA |
181 | return 0; |
182 | } else if (S_ISREG(st.st_mode)) { | |
a7086591 | 183 | f->max_blocks = st.st_size / BS; |
c9fb4c5b JA |
184 | return 0; |
185 | } | |
186 | ||
187 | return -1; | |
188 | } | |
189 | ||
190 | static int reap_events(struct submitter *s) | |
191 | { | |
e31b8288 | 192 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 193 | struct io_uring_cqe *cqe; |
e2239016 | 194 | unsigned head, reaped = 0; |
c9fb4c5b | 195 | |
c3e2fc25 | 196 | head = *ring->head; |
c9fb4c5b JA |
197 | do { |
198 | barrier(); | |
c3e2fc25 | 199 | if (head == *ring->tail) |
c9fb4c5b | 200 | break; |
f0403f94 JA |
201 | cqe = &ring->cqes[head & cq_ring_mask]; |
202 | if (cqe->res != BS) { | |
f0403f94 | 203 | printf("io: unexpected ret=%d\n", cqe->res); |
c9fb4c5b JA |
204 | return -1; |
205 | } | |
f0403f94 | 206 | if (cqe->flags & IOCQE_FLAG_CACHEHIT) |
305c06ce JA |
207 | s->cachehit++; |
208 | else | |
209 | s->cachemiss++; | |
c9fb4c5b JA |
210 | reaped++; |
211 | head++; | |
c9fb4c5b JA |
212 | } while (1); |
213 | ||
214 | s->inflight -= reaped; | |
c3e2fc25 | 215 | *ring->head = head; |
c9fb4c5b JA |
216 | barrier(); |
217 | return reaped; | |
218 | } | |
219 | ||
220 | static void *submitter_fn(void *data) | |
221 | { | |
222 | struct submitter *s = data; | |
a7086591 | 223 | int ret, prepped; |
c9fb4c5b JA |
224 | |
225 | printf("submitter=%d\n", gettid()); | |
226 | ||
c9fb4c5b JA |
227 | srand48_r(pthread_self(), &s->rand); |
228 | ||
229 | prepped = 0; | |
230 | do { | |
f310970e | 231 | int to_wait, to_submit, this_reap, to_prep; |
c9fb4c5b | 232 | |
f310970e JA |
233 | if (!prepped && s->inflight < DEPTH) { |
234 | to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT); | |
a7086591 | 235 | prepped = prep_more_ios(s, to_prep); |
f310970e | 236 | } |
c9fb4c5b JA |
237 | s->inflight += prepped; |
238 | submit_more: | |
239 | to_submit = prepped; | |
240 | submit: | |
241 | if (s->inflight + BATCH_SUBMIT < DEPTH) | |
242 | to_wait = 0; | |
243 | else | |
244 | to_wait = min(s->inflight + to_submit, BATCH_COMPLETE); | |
245 | ||
f310970e JA |
246 | ret = io_uring_enter(s, to_submit, to_wait, |
247 | IORING_ENTER_GETEVENTS); | |
c9fb4c5b JA |
248 | s->calls++; |
249 | ||
250 | this_reap = reap_events(s); | |
251 | if (this_reap == -1) | |
252 | break; | |
253 | s->reaps += this_reap; | |
254 | ||
255 | if (ret >= 0) { | |
256 | if (!ret) { | |
257 | to_submit = 0; | |
258 | if (s->inflight) | |
259 | goto submit; | |
260 | continue; | |
261 | } else if (ret < to_submit) { | |
262 | int diff = to_submit - ret; | |
263 | ||
264 | s->done += ret; | |
265 | prepped -= diff; | |
266 | goto submit_more; | |
267 | } | |
268 | s->done += ret; | |
269 | prepped = 0; | |
270 | continue; | |
271 | } else if (ret < 0) { | |
ac122fea | 272 | if (errno == EAGAIN) { |
c9fb4c5b JA |
273 | if (s->finish) |
274 | break; | |
275 | if (this_reap) | |
276 | goto submit; | |
c9fb4c5b JA |
277 | to_submit = 0; |
278 | goto submit; | |
279 | } | |
ac122fea | 280 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
281 | break; |
282 | } | |
283 | } while (!s->finish); | |
a7086591 | 284 | |
c9fb4c5b JA |
285 | finish = 1; |
286 | return NULL; | |
287 | } | |
288 | ||
289 | static void sig_int(int sig) | |
290 | { | |
291 | printf("Exiting on signal %d\n", sig); | |
292 | submitters[0].finish = 1; | |
293 | finish = 1; | |
294 | } | |
295 | ||
296 | static void arm_sig_int(void) | |
297 | { | |
298 | struct sigaction act; | |
299 | ||
300 | memset(&act, 0, sizeof(act)); | |
301 | act.sa_handler = sig_int; | |
302 | act.sa_flags = SA_RESTART; | |
303 | sigaction(SIGINT, &act, NULL); | |
304 | } | |
305 | ||
c3e2fc25 JA |
306 | static int setup_ring(struct submitter *s) |
307 | { | |
e31b8288 JA |
308 | struct io_sq_ring *sring = &s->sq_ring; |
309 | struct io_cq_ring *cring = &s->cq_ring; | |
310 | struct io_uring_params p; | |
c3e2fc25 JA |
311 | void *ptr; |
312 | int fd; | |
313 | ||
314 | memset(&p, 0, sizeof(p)); | |
315 | ||
c3e2fc25 | 316 | if (polled) |
e31b8288 | 317 | p.flags |= IORING_SETUP_IOPOLL; |
c3e2fc25 | 318 | if (buffered) |
e31b8288 | 319 | p.flags |= IORING_SETUP_SQWQ; |
c3e2fc25 | 320 | else if (sq_thread) { |
e31b8288 | 321 | p.flags |= IORING_SETUP_SQTHREAD; |
c3e2fc25 JA |
322 | p.sq_thread_cpu = sq_thread_cpu; |
323 | } | |
324 | ||
325 | if (fixedbufs) | |
411a0821 | 326 | fd = io_uring_setup(DEPTH, s->iovecs, DEPTH, &p); |
c3e2fc25 | 327 | else |
411a0821 | 328 | fd = io_uring_setup(DEPTH, NULL, 0, &p); |
c3e2fc25 JA |
329 | if (fd < 0) { |
330 | perror("io_uring_setup"); | |
331 | return 1; | |
332 | } | |
333 | ||
f310970e | 334 | s->ring_fd = fd; |
e2239016 | 335 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
336 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
337 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
338 | printf("sq_ring ptr = 0x%p\n", ptr); |
339 | sring->head = ptr + p.sq_off.head; | |
340 | sring->tail = ptr + p.sq_off.tail; | |
341 | sring->ring_mask = ptr + p.sq_off.ring_mask; | |
342 | sring->ring_entries = ptr + p.sq_off.ring_entries; | |
ac122fea | 343 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
344 | sq_ring_mask = *sring->ring_mask; |
345 | ||
f0403f94 | 346 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 347 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 JA |
348 | IORING_OFF_SQES); |
349 | printf("sqes ptr = 0x%p\n", s->sqes); | |
c3e2fc25 | 350 | |
f0403f94 | 351 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
352 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
353 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
354 | printf("cq_ring ptr = 0x%p\n", ptr); |
355 | cring->head = ptr + p.cq_off.head; | |
356 | cring->tail = ptr + p.cq_off.tail; | |
357 | cring->ring_mask = ptr + p.cq_off.ring_mask; | |
358 | cring->ring_entries = ptr + p.cq_off.ring_entries; | |
f0403f94 | 359 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
360 | cq_ring_mask = *cring->ring_mask; |
361 | return 0; | |
362 | } | |
363 | ||
c9fb4c5b JA |
364 | int main(int argc, char *argv[]) |
365 | { | |
366 | struct submitter *s = &submitters[0]; | |
305c06ce | 367 | unsigned long done, calls, reap, cache_hit, cache_miss; |
a7086591 | 368 | int err, i, flags, fd; |
c9fb4c5b | 369 | struct rlimit rlim; |
c3e2fc25 | 370 | void *ret; |
c9fb4c5b JA |
371 | |
372 | if (argc < 2) { | |
373 | printf("%s: filename\n", argv[0]); | |
374 | return 1; | |
375 | } | |
376 | ||
a7086591 JA |
377 | flags = O_RDONLY; |
378 | if (!buffered) | |
379 | flags |= O_DIRECT; | |
380 | ||
381 | i = 1; | |
382 | while (i < argc) { | |
383 | struct file *f = &s->files[s->nr_files]; | |
384 | ||
385 | fd = open(argv[i], flags); | |
386 | if (fd < 0) { | |
387 | perror("open"); | |
388 | return 1; | |
389 | } | |
390 | f->fd = fd; | |
391 | if (get_file_size(f)) { | |
392 | printf("failed getting size of device/file\n"); | |
393 | return 1; | |
394 | } | |
395 | if (f->max_blocks <= 1) { | |
396 | printf("Zero file/device size?\n"); | |
397 | return 1; | |
398 | } | |
399 | f->max_blocks--; | |
400 | ||
401 | printf("Added file %s\n", argv[i]); | |
402 | s->nr_files++; | |
403 | i++; | |
404 | } | |
405 | ||
c9fb4c5b JA |
406 | rlim.rlim_cur = RLIM_INFINITY; |
407 | rlim.rlim_max = RLIM_INFINITY; | |
408 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
409 | perror("setrlimit"); | |
410 | return 1; | |
411 | } | |
412 | ||
413 | arm_sig_int(); | |
414 | ||
c3e2fc25 JA |
415 | for (i = 0; i < DEPTH; i++) { |
416 | void *buf; | |
c9fb4c5b | 417 | |
c3e2fc25 | 418 | if (posix_memalign(&buf, BS, BS)) { |
c9fb4c5b JA |
419 | printf("failed alloc\n"); |
420 | return 1; | |
421 | } | |
c3e2fc25 JA |
422 | s->iovecs[i].iov_base = buf; |
423 | s->iovecs[i].iov_len = BS; | |
c9fb4c5b JA |
424 | } |
425 | ||
c3e2fc25 | 426 | err = setup_ring(s); |
c9fb4c5b | 427 | if (err) { |
c3e2fc25 | 428 | printf("ring setup failed: %s, %d\n", strerror(errno), err); |
c9fb4c5b JA |
429 | return 1; |
430 | } | |
c3e2fc25 JA |
431 | printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered); |
432 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); | |
c9fb4c5b JA |
433 | |
434 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
435 | ||
305c06ce | 436 | cache_hit = cache_miss = reap = calls = done = 0; |
c9fb4c5b JA |
437 | do { |
438 | unsigned long this_done = 0; | |
439 | unsigned long this_reap = 0; | |
440 | unsigned long this_call = 0; | |
305c06ce JA |
441 | unsigned long this_cache_hit = 0; |
442 | unsigned long this_cache_miss = 0; | |
c9fb4c5b | 443 | unsigned long rpc = 0, ipc = 0; |
305c06ce | 444 | double hit = 0.0; |
c9fb4c5b JA |
445 | |
446 | sleep(1); | |
447 | this_done += s->done; | |
448 | this_call += s->calls; | |
449 | this_reap += s->reaps; | |
305c06ce JA |
450 | this_cache_hit += s->cachehit; |
451 | this_cache_miss += s->cachemiss; | |
452 | if (this_cache_hit && this_cache_miss) { | |
453 | unsigned long hits, total; | |
454 | ||
455 | hits = this_cache_hit - cache_hit; | |
456 | total = hits + this_cache_miss - cache_miss; | |
457 | hit = (double) hits / (double) total; | |
458 | hit *= 100.0; | |
459 | } | |
c9fb4c5b JA |
460 | if (this_call - calls) { |
461 | rpc = (this_done - done) / (this_call - calls); | |
462 | ipc = (this_reap - reap) / (this_call - calls); | |
463 | } | |
10c4d131 | 464 | printf("IOPS=%lu, IOS/call=%lu/%lu, inflight=%u (head=%u tail=%u), Cachehit=%0.2f%%\n", |
c9fb4c5b | 465 | this_done - done, rpc, ipc, s->inflight, |
c3e2fc25 | 466 | *s->cq_ring.head, *s->cq_ring.tail, hit); |
c9fb4c5b JA |
467 | done = this_done; |
468 | calls = this_call; | |
469 | reap = this_reap; | |
305c06ce JA |
470 | cache_hit = s->cachehit; |
471 | cache_miss = s->cachemiss; | |
c9fb4c5b JA |
472 | } while (!finish); |
473 | ||
474 | pthread_join(s->thread, &ret); | |
e31b8288 | 475 | close(s->ring_fd); |
c9fb4c5b JA |
476 | return 0; |
477 | } |