t/io_uring: fixes in output
[fio.git] / t / io_uring.c
CommitLineData
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"
f3e769a4 25#include "../os/linux/io_uring.h"
ac122fea 26
e31b8288 27#define min(a, b) ((a < b) ? (a) : (b))
c3e2fc25 28
e31b8288 29struct io_sq_ring {
e2239016
JA
30 unsigned *head;
31 unsigned *tail;
32 unsigned *ring_mask;
33 unsigned *ring_entries;
ce1705de 34 unsigned *flags;
e2239016 35 unsigned *array;
c9fb4c5b
JA
36};
37
e31b8288 38struct io_cq_ring {
e2239016
JA
39 unsigned *head;
40 unsigned *tail;
41 unsigned *ring_mask;
42 unsigned *ring_entries;
f0403f94 43 struct io_uring_cqe *cqes;
c9fb4c5b
JA
44};
45
701d1277 46#define DEPTH 128
2e7888ef
JA
47#define BATCH_SUBMIT 32
48#define BATCH_COMPLETE 32
c9fb4c5b
JA
49#define BS 4096
50
a7086591
JA
51#define MAX_FDS 16
52
c3e2fc25 53static unsigned sq_ring_mask, cq_ring_mask;
e39c34dc 54
a7086591
JA
55struct file {
56 unsigned long max_blocks;
701d1277 57 unsigned pending_ios;
48e698fa
JA
58 int real_fd;
59 int fixed_fd;
a7086591
JA
60};
61
c9fb4c5b
JA
62struct submitter {
63 pthread_t thread;
f310970e 64 int ring_fd;
54319661 65 int index;
e31b8288 66 struct io_sq_ring sq_ring;
f0403f94 67 struct io_uring_sqe *sqes;
e31b8288 68 struct io_cq_ring cq_ring;
c9fb4c5b
JA
69 int inflight;
70 unsigned long reaps;
71 unsigned long done;
72 unsigned long calls;
73 volatile int finish;
701d1277 74
48e698fa
JA
75 __s32 *fds;
76
a7086591
JA
77 struct file files[MAX_FDS];
78 unsigned nr_files;
79 unsigned cur_file;
e39863e3 80 struct iovec iovecs[];
c9fb4c5b
JA
81};
82
e39863e3 83static struct submitter *submitter;
c9fb4c5b
JA
84static volatile int finish;
85
e39863e3
KB
86static int depth = DEPTH;
87static int batch_submit = BATCH_SUBMIT;
88static int batch_complete = BATCH_COMPLETE;
5bd526f2 89static int bs = BS;
f0403f94 90static int polled = 1; /* use IO polling */
701d1277 91static int fixedbufs = 1; /* use fixed user buffers */
8c5fa755 92static int register_files = 1; /* use fixed files */
f0403f94 93static int buffered = 0; /* use buffered IO, not O_DIRECT */
3d7d00a3
JA
94static int sq_thread_poll = 0; /* use kernel submission/poller thread */
95static int sq_thread_cpu = -1; /* pin above thread to this CPU */
8025517d 96static int do_nop = 0; /* no-op SQ ring commands */
54319661 97static int nthreads = 1;
c9fb4c5b 98
84106576 99static int vectored = 1;
b3915995 100
2ea53ca3 101static int io_uring_register_buffers(struct submitter *s)
c9fb4c5b 102{
8025517d
JA
103 if (do_nop)
104 return 0;
105
bfed648c 106 return syscall(__NR_io_uring_register, s->ring_fd,
e39863e3 107 IORING_REGISTER_BUFFERS, s->iovecs, depth);
2ea53ca3
JA
108}
109
a7abc9fb
JA
110static int io_uring_register_files(struct submitter *s)
111{
48e698fa 112 int i;
a7abc9fb 113
8025517d
JA
114 if (do_nop)
115 return 0;
116
48e698fa
JA
117 s->fds = calloc(s->nr_files, sizeof(__s32));
118 for (i = 0; i < s->nr_files; i++) {
119 s->fds[i] = s->files[i].real_fd;
120 s->files[i].fixed_fd = i;
121 }
a7abc9fb 122
bfed648c 123 return syscall(__NR_io_uring_register, s->ring_fd,
919850d2 124 IORING_REGISTER_FILES, s->fds, s->nr_files);
a7abc9fb
JA
125}
126
2ea53ca3
JA
127static int io_uring_setup(unsigned entries, struct io_uring_params *p)
128{
bfed648c 129 return syscall(__NR_io_uring_setup, entries, p);
c9fb4c5b
JA
130}
131
b3915995
JA
132static void io_uring_probe(int fd)
133{
134 struct io_uring_probe *p;
135 int ret;
136
137 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
138 if (!p)
139 return;
140
141 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
142 ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
143 if (ret < 0)
144 goto out;
145
146 if (IORING_OP_READ > p->ops_len)
147 goto out;
148
149 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED))
84106576 150 vectored = 0;
b3915995
JA
151out:
152 free(p);
153}
154
c3e2fc25
JA
155static int io_uring_enter(struct submitter *s, unsigned int to_submit,
156 unsigned int min_complete, unsigned int flags)
c9fb4c5b 157{
bfed648c
JA
158 return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete,
159 flags, NULL, 0);
c9fb4c5b
JA
160}
161
77d814a1 162#ifndef CONFIG_HAVE_GETTID
c9fb4c5b
JA
163static int gettid(void)
164{
165 return syscall(__NR_gettid);
166}
77d814a1 167#endif
c9fb4c5b 168
701d1277
JA
169static unsigned file_depth(struct submitter *s)
170{
e39863e3 171 return (depth + s->nr_files - 1) / s->nr_files;
701d1277
JA
172}
173
a7086591 174static void init_io(struct submitter *s, unsigned index)
c9fb4c5b 175{
f0403f94 176 struct io_uring_sqe *sqe = &s->sqes[index];
c9fb4c5b 177 unsigned long offset;
a7086591 178 struct file *f;
c9fb4c5b
JA
179 long r;
180
8025517d
JA
181 if (do_nop) {
182 sqe->opcode = IORING_OP_NOP;
183 return;
184 }
185
701d1277
JA
186 if (s->nr_files == 1) {
187 f = &s->files[0];
188 } else {
189 f = &s->files[s->cur_file];
190 if (f->pending_ios >= file_depth(s)) {
191 s->cur_file++;
192 if (s->cur_file == s->nr_files)
193 s->cur_file = 0;
93d1811c 194 f = &s->files[s->cur_file];
701d1277
JA
195 }
196 }
197 f->pending_ios++;
a7086591 198
5e8865c0 199 r = lrand48();
5bd526f2 200 offset = (r % (f->max_blocks - 1)) * bs;
c9fb4c5b 201
8c5fa755
JA
202 if (register_files) {
203 sqe->flags = IOSQE_FIXED_FILE;
204 sqe->fd = f->fixed_fd;
205 } else {
206 sqe->flags = 0;
207 sqe->fd = f->real_fd;
208 }
f0403f94 209 if (fixedbufs) {
48e698fa 210 sqe->opcode = IORING_OP_READ_FIXED;
919850d2 211 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
5bd526f2 212 sqe->len = bs;
2ea53ca3 213 sqe->buf_index = index;
84106576 214 } else if (!vectored) {
b3915995
JA
215 sqe->opcode = IORING_OP_READ;
216 sqe->addr = (unsigned long) s->iovecs[index].iov_base;
217 sqe->len = bs;
218 sqe->buf_index = 0;
84106576
JA
219 } else {
220 sqe->opcode = IORING_OP_READV;
221 sqe->addr = (unsigned long) &s->iovecs[index];
222 sqe->len = 1;
223 sqe->buf_index = 0;
f0403f94 224 }
f0403f94 225 sqe->ioprio = 0;
f0403f94 226 sqe->off = offset;
48e698fa 227 sqe->user_data = (unsigned long) f;
c9fb4c5b
JA
228}
229
a7086591 230static int prep_more_ios(struct submitter *s, int max_ios)
c9fb4c5b 231{
e31b8288 232 struct io_sq_ring *ring = &s->sq_ring;
e2239016 233 unsigned index, tail, next_tail, prepped = 0;
c9fb4c5b 234
c3e2fc25 235 next_tail = tail = *ring->tail;
c9fb4c5b
JA
236 do {
237 next_tail++;
fc2dc21b 238 if (next_tail == atomic_load_acquire(ring->head))
c9fb4c5b
JA
239 break;
240
e39c34dc 241 index = tail & sq_ring_mask;
a7086591 242 init_io(s, index);
c3e2fc25 243 ring->array[index] = index;
c9fb4c5b
JA
244 prepped++;
245 tail = next_tail;
246 } while (prepped < max_ios);
247
fc2dc21b
JA
248 if (prepped)
249 atomic_store_release(ring->tail, tail);
c9fb4c5b
JA
250 return prepped;
251}
252
a7086591 253static int get_file_size(struct file *f)
c9fb4c5b
JA
254{
255 struct stat st;
256
48e698fa 257 if (fstat(f->real_fd, &st) < 0)
c9fb4c5b
JA
258 return -1;
259 if (S_ISBLK(st.st_mode)) {
260 unsigned long long bytes;
261
48e698fa 262 if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
c9fb4c5b
JA
263 return -1;
264
5bd526f2 265 f->max_blocks = bytes / bs;
c9fb4c5b
JA
266 return 0;
267 } else if (S_ISREG(st.st_mode)) {
5bd526f2 268 f->max_blocks = st.st_size / bs;
c9fb4c5b
JA
269 return 0;
270 }
271
272 return -1;
273}
274
275static int reap_events(struct submitter *s)
276{
e31b8288 277 struct io_cq_ring *ring = &s->cq_ring;
f0403f94 278 struct io_uring_cqe *cqe;
e2239016 279 unsigned head, reaped = 0;
c9fb4c5b 280
c3e2fc25 281 head = *ring->head;
c9fb4c5b 282 do {
701d1277
JA
283 struct file *f;
284
679d8352 285 read_barrier();
fc2dc21b 286 if (head == atomic_load_acquire(ring->tail))
c9fb4c5b 287 break;
f0403f94 288 cqe = &ring->cqes[head & cq_ring_mask];
8025517d 289 if (!do_nop) {
e3466352 290 f = (struct file *) (uintptr_t) cqe->user_data;
8025517d 291 f->pending_ios--;
5bd526f2 292 if (cqe->res != bs) {
8025517d 293 printf("io: unexpected ret=%d\n", cqe->res);
154a9582 294 if (polled && cqe->res == -EOPNOTSUPP)
8066f6b6 295 printf("Your filesystem/driver/kernel doesn't support polled IO\n");
8025517d
JA
296 return -1;
297 }
c9fb4c5b
JA
298 }
299 reaped++;
300 head++;
c9fb4c5b
JA
301 } while (1);
302
fc2dc21b
JA
303 if (reaped) {
304 s->inflight -= reaped;
305 atomic_store_release(ring->head, head);
306 }
c9fb4c5b
JA
307 return reaped;
308}
309
310static void *submitter_fn(void *data)
311{
312 struct submitter *s = data;
ce1705de 313 struct io_sq_ring *ring = &s->sq_ring;
a7086591 314 int ret, prepped;
c9fb4c5b
JA
315
316 printf("submitter=%d\n", gettid());
317
5e8865c0 318 srand48(pthread_self());
c9fb4c5b
JA
319
320 prepped = 0;
321 do {
f310970e 322 int to_wait, to_submit, this_reap, to_prep;
fc2dc21b 323 unsigned ring_flags = 0;
c9fb4c5b 324
e39863e3
KB
325 if (!prepped && s->inflight < depth) {
326 to_prep = min(depth - s->inflight, batch_submit);
a7086591 327 prepped = prep_more_ios(s, to_prep);
f310970e 328 }
c9fb4c5b
JA
329 s->inflight += prepped;
330submit_more:
331 to_submit = prepped;
332submit:
e39863e3 333 if (to_submit && (s->inflight + to_submit <= depth))
c9fb4c5b
JA
334 to_wait = 0;
335 else
e39863e3 336 to_wait = min(s->inflight + to_submit, batch_complete);
c9fb4c5b 337
ce1705de
JA
338 /*
339 * Only need to call io_uring_enter if we're not using SQ thread
340 * poll, or if IORING_SQ_NEED_WAKEUP is set.
341 */
fc2dc21b
JA
342 if (sq_thread_poll)
343 ring_flags = atomic_load_acquire(ring->flags);
344 if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) {
e0abe388
JA
345 unsigned flags = 0;
346
347 if (to_wait)
348 flags = IORING_ENTER_GETEVENTS;
fc2dc21b 349 if (ring_flags & IORING_SQ_NEED_WAKEUP)
b532dd6d 350 flags |= IORING_ENTER_SQ_WAKEUP;
e0abe388 351 ret = io_uring_enter(s, to_submit, to_wait, flags);
ce1705de 352 s->calls++;
fc2dc21b
JA
353 } else {
354 /* for SQPOLL, we submitted it all effectively */
355 ret = to_submit;
ce1705de 356 }
c9fb4c5b 357
ce1705de
JA
358 /*
359 * For non SQ thread poll, we already got the events we needed
360 * through the io_uring_enter() above. For SQ thread poll, we
361 * need to loop here until we find enough events.
362 */
363 this_reap = 0;
364 do {
365 int r;
366 r = reap_events(s);
7d1435e6
JA
367 if (r == -1) {
368 s->finish = 1;
ce1705de 369 break;
7d1435e6 370 } else if (r > 0)
ce1705de
JA
371 this_reap += r;
372 } while (sq_thread_poll && this_reap < to_wait);
c9fb4c5b
JA
373 s->reaps += this_reap;
374
375 if (ret >= 0) {
376 if (!ret) {
377 to_submit = 0;
378 if (s->inflight)
379 goto submit;
380 continue;
381 } else if (ret < to_submit) {
382 int diff = to_submit - ret;
383
384 s->done += ret;
385 prepped -= diff;
386 goto submit_more;
387 }
388 s->done += ret;
389 prepped = 0;
390 continue;
391 } else if (ret < 0) {
ac122fea 392 if (errno == EAGAIN) {
c9fb4c5b
JA
393 if (s->finish)
394 break;
395 if (this_reap)
396 goto submit;
c9fb4c5b
JA
397 to_submit = 0;
398 goto submit;
399 }
ac122fea 400 printf("io_submit: %s\n", strerror(errno));
c9fb4c5b
JA
401 break;
402 }
403 } while (!s->finish);
a7086591 404
c9fb4c5b
JA
405 finish = 1;
406 return NULL;
407}
408
54319661
JA
409static struct submitter *get_submitter(int offset)
410{
411 void *ret;
412
413 ret = submitter;
414 if (offset)
415 ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec));
416 return ret;
417}
418
c9fb4c5b
JA
419static void sig_int(int sig)
420{
54319661
JA
421 int j;
422
c9fb4c5b 423 printf("Exiting on signal %d\n", sig);
54319661
JA
424 for (j = 0; j < nthreads; j++) {
425 struct submitter *s = get_submitter(j);
426 s->finish = 1;
427 }
c9fb4c5b
JA
428 finish = 1;
429}
430
431static void arm_sig_int(void)
432{
433 struct sigaction act;
434
435 memset(&act, 0, sizeof(act));
436 act.sa_handler = sig_int;
437 act.sa_flags = SA_RESTART;
438 sigaction(SIGINT, &act, NULL);
439}
440
c3e2fc25
JA
441static int setup_ring(struct submitter *s)
442{
e31b8288
JA
443 struct io_sq_ring *sring = &s->sq_ring;
444 struct io_cq_ring *cring = &s->cq_ring;
445 struct io_uring_params p;
2ea53ca3 446 int ret, fd;
c3e2fc25 447 void *ptr;
c3e2fc25
JA
448
449 memset(&p, 0, sizeof(p));
450
7d1435e6 451 if (polled && !do_nop)
0e47f11b 452 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3 453 if (sq_thread_poll) {
2ea53ca3 454 p.flags |= IORING_SETUP_SQPOLL;
3ade18a3 455 if (sq_thread_cpu != -1) {
3d7d00a3 456 p.flags |= IORING_SETUP_SQ_AFF;
3ade18a3
JA
457 p.sq_thread_cpu = sq_thread_cpu;
458 }
c3e2fc25
JA
459 }
460
e39863e3 461 fd = io_uring_setup(depth, &p);
c3e2fc25
JA
462 if (fd < 0) {
463 perror("io_uring_setup");
464 return 1;
465 }
f310970e 466 s->ring_fd = fd;
2ea53ca3 467
b3915995
JA
468 io_uring_probe(fd);
469
2ea53ca3
JA
470 if (fixedbufs) {
471 ret = io_uring_register_buffers(s);
472 if (ret < 0) {
a7abc9fb 473 perror("io_uring_register_buffers");
2ea53ca3
JA
474 return 1;
475 }
476 }
477
8c5fa755
JA
478 if (register_files) {
479 ret = io_uring_register_files(s);
480 if (ret < 0) {
481 perror("io_uring_register_files");
482 return 1;
483 }
a7abc9fb
JA
484 }
485
e2239016 486 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
487 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
488 IORING_OFF_SQ_RING);
c3e2fc25
JA
489 printf("sq_ring ptr = 0x%p\n", ptr);
490 sring->head = ptr + p.sq_off.head;
491 sring->tail = ptr + p.sq_off.tail;
492 sring->ring_mask = ptr + p.sq_off.ring_mask;
493 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 494 sring->flags = ptr + p.sq_off.flags;
ac122fea 495 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
496 sq_ring_mask = *sring->ring_mask;
497
f0403f94 498 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 499 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
500 IORING_OFF_SQES);
501 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 502
f0403f94 503 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
504 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
505 IORING_OFF_CQ_RING);
c3e2fc25
JA
506 printf("cq_ring ptr = 0x%p\n", ptr);
507 cring->head = ptr + p.cq_off.head;
508 cring->tail = ptr + p.cq_off.tail;
509 cring->ring_mask = ptr + p.cq_off.ring_mask;
510 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 511 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
512 cq_ring_mask = *cring->ring_mask;
513 return 0;
514}
515
2e7888ef
JA
516static void file_depths(char *buf)
517{
26976a13 518 bool prev = false;
2e7888ef 519 char *p;
54319661 520 int i, j;
2e7888ef 521
ac21bfab 522 buf[0] = '\0';
2e7888ef 523 p = buf;
54319661
JA
524 for (j = 0; j < nthreads; j++) {
525 struct submitter *s = get_submitter(j);
2e7888ef 526
54319661
JA
527 for (i = 0; i < s->nr_files; i++) {
528 struct file *f = &s->files[i];
529
26976a13
JA
530 if (prev)
531 p += sprintf(p, " %d", f->pending_ios);
54319661 532 else
4f215227 533 p += sprintf(p, "%d", f->pending_ios);
26976a13 534 prev = true;
54319661 535 }
2e7888ef
JA
536 }
537}
538
e39863e3
KB
539static void usage(char *argv)
540{
541 printf("%s [options] -- [filenames]\n"
35268e11
AJ
542 " -d <int> : IO Depth, default %d\n"
543 " -s <int> : Batch submit, default %d\n"
544 " -c <int> : Batch complete, default %d\n"
545 " -b <int> : Block size, default %d\n"
546 " -p <bool> : Polled IO, default %d\n"
547 " -B <bool> : Fixed buffers, default %d\n"
548 " -F <bool> : Register files, default %d\n"
549 " -n <int> : Number of threads, default %d\n",
550 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
551 fixedbufs, register_files, nthreads);
e39863e3
KB
552 exit(0);
553}
554
c9fb4c5b
JA
555int main(int argc, char *argv[])
556{
e39863e3 557 struct submitter *s;
05138221 558 unsigned long done, calls, reap;
54319661 559 int err, i, j, flags, fd, opt;
2e7888ef 560 char *fdepths;
c3e2fc25 561 void *ret;
c9fb4c5b 562
8025517d 563 if (!do_nop && argc < 2) {
e39863e3 564 printf("%s: filename [options]\n", argv[0]);
c9fb4c5b
JA
565 return 1;
566 }
567
54319661 568 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:h?")) != -1) {
e39863e3
KB
569 switch (opt) {
570 case 'd':
571 depth = atoi(optarg);
572 break;
573 case 's':
574 batch_submit = atoi(optarg);
575 break;
576 case 'c':
577 batch_complete = atoi(optarg);
578 break;
5bd526f2
JA
579 case 'b':
580 bs = atoi(optarg);
581 break;
582 case 'p':
583 polled = !!atoi(optarg);
584 break;
6a87c3b0
JA
585 case 'B':
586 fixedbufs = !!atoi(optarg);
587 break;
588 case 'F':
589 register_files = !!atoi(optarg);
590 break;
54319661
JA
591 case 'n':
592 nthreads = atoi(optarg);
593 break;
e39863e3
KB
594 case 'h':
595 case '?':
596 default:
597 usage(argv[0]);
598 break;
599 }
600 }
601
54319661
JA
602 submitter = calloc(nthreads, sizeof(*submitter) +
603 depth * sizeof(struct iovec));
604 for (j = 0; j < nthreads; j++) {
605 s = get_submitter(j);
606 s->index = j;
607 s->done = s->calls = s->reaps = 0;
608 }
e39863e3 609
701d1277 610 flags = O_RDONLY | O_NOATIME;
a7086591
JA
611 if (!buffered)
612 flags |= O_DIRECT;
613
54319661 614 j = 0;
e39863e3 615 i = optind;
8025517d 616 while (!do_nop && i < argc) {
be26a982 617 struct file *f;
a7086591 618
54319661 619 s = get_submitter(j);
be26a982
JA
620 if (s->nr_files == MAX_FDS) {
621 printf("Max number of files (%d) reached\n", MAX_FDS);
622 break;
623 }
a7086591
JA
624 fd = open(argv[i], flags);
625 if (fd < 0) {
626 perror("open");
627 return 1;
628 }
be26a982
JA
629
630 f = &s->files[s->nr_files];
48e698fa 631 f->real_fd = fd;
a7086591
JA
632 if (get_file_size(f)) {
633 printf("failed getting size of device/file\n");
634 return 1;
635 }
636 if (f->max_blocks <= 1) {
637 printf("Zero file/device size?\n");
638 return 1;
639 }
640 f->max_blocks--;
641
54319661 642 printf("Added file %s (submitter %d)\n", argv[i], s->index);
a7086591
JA
643 s->nr_files++;
644 i++;
54319661
JA
645 if (++j >= nthreads)
646 j = 0;
a7086591
JA
647 }
648
b3ce355d
JA
649 if (fixedbufs) {
650 struct rlimit rlim;
651
652 rlim.rlim_cur = RLIM_INFINITY;
653 rlim.rlim_max = RLIM_INFINITY;
654 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
655 perror("setrlimit");
656 return 1;
657 }
c9fb4c5b
JA
658 }
659
660 arm_sig_int();
661
54319661
JA
662 for (j = 0; j < nthreads; j++) {
663 s = get_submitter(j);
664 for (i = 0; i < depth; i++) {
665 void *buf;
c9fb4c5b 666
54319661
JA
667 if (posix_memalign(&buf, bs, bs)) {
668 printf("failed alloc\n");
669 return 1;
670 }
671 s->iovecs[i].iov_base = buf;
672 s->iovecs[i].iov_len = bs;
c9fb4c5b 673 }
c9fb4c5b
JA
674 }
675
54319661
JA
676 for (j = 0; j < nthreads; j++) {
677 s = get_submitter(j);
678
679 err = setup_ring(s);
680 if (err) {
681 printf("ring setup failed: %s, %d\n", strerror(errno), err);
682 return 1;
683 }
c9fb4c5b 684 }
54319661 685 s = get_submitter(0);
6a87c3b0 686 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered);
e39863e3 687 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b 688
54319661
JA
689 for (j = 0; j < nthreads; j++) {
690 s = get_submitter(j);
691 pthread_create(&s->thread, NULL, submitter_fn, s);
692 }
c9fb4c5b 693
54319661 694 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 695 reap = calls = done = 0;
c9fb4c5b
JA
696 do {
697 unsigned long this_done = 0;
698 unsigned long this_reap = 0;
699 unsigned long this_call = 0;
700 unsigned long rpc = 0, ipc = 0;
701
702 sleep(1);
54319661
JA
703 for (j = 0; j < nthreads; j++) {
704 this_done += s->done;
705 this_call += s->calls;
706 this_reap += s->reaps;
707 }
c9fb4c5b
JA
708 if (this_call - calls) {
709 rpc = (this_done - done) / (this_call - calls);
710 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
711 } else
712 rpc = ipc = -1;
2e7888ef 713 file_depths(fdepths);
4f215227
JA
714 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=(%s)\n",
715 this_done - done, rpc, ipc, fdepths);
c9fb4c5b
JA
716 done = this_done;
717 calls = this_call;
718 reap = this_reap;
719 } while (!finish);
720
54319661
JA
721 for (j = 0; j < nthreads; j++) {
722 s = get_submitter(j);
723 pthread_join(s->thread, &ret);
724 close(s->ring_fd);
725 }
2e7888ef 726 free(fdepths);
54319661 727 free(submitter);
c9fb4c5b
JA
728 return 0;
729}