t/io_uring: don't require a file for do_nop runs
[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 470 if (fixedbufs) {
15130e3f
JA
471 struct rlimit rlim;
472
473 rlim.rlim_cur = RLIM_INFINITY;
474 rlim.rlim_max = RLIM_INFINITY;
475 /* ignore potential error, not needed on newer kernels */
476 setrlimit(RLIMIT_MEMLOCK, &rlim);
477
2ea53ca3
JA
478 ret = io_uring_register_buffers(s);
479 if (ret < 0) {
a7abc9fb 480 perror("io_uring_register_buffers");
2ea53ca3
JA
481 return 1;
482 }
483 }
484
8c5fa755
JA
485 if (register_files) {
486 ret = io_uring_register_files(s);
487 if (ret < 0) {
488 perror("io_uring_register_files");
489 return 1;
490 }
a7abc9fb
JA
491 }
492
e2239016 493 ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
f310970e
JA
494 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
495 IORING_OFF_SQ_RING);
c3e2fc25
JA
496 printf("sq_ring ptr = 0x%p\n", ptr);
497 sring->head = ptr + p.sq_off.head;
498 sring->tail = ptr + p.sq_off.tail;
499 sring->ring_mask = ptr + p.sq_off.ring_mask;
500 sring->ring_entries = ptr + p.sq_off.ring_entries;
ce1705de 501 sring->flags = ptr + p.sq_off.flags;
ac122fea 502 sring->array = ptr + p.sq_off.array;
c3e2fc25
JA
503 sq_ring_mask = *sring->ring_mask;
504
f0403f94 505 s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
f310970e 506 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
f0403f94
JA
507 IORING_OFF_SQES);
508 printf("sqes ptr = 0x%p\n", s->sqes);
c3e2fc25 509
f0403f94 510 ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
f310970e
JA
511 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
512 IORING_OFF_CQ_RING);
c3e2fc25
JA
513 printf("cq_ring ptr = 0x%p\n", ptr);
514 cring->head = ptr + p.cq_off.head;
515 cring->tail = ptr + p.cq_off.tail;
516 cring->ring_mask = ptr + p.cq_off.ring_mask;
517 cring->ring_entries = ptr + p.cq_off.ring_entries;
f0403f94 518 cring->cqes = ptr + p.cq_off.cqes;
c3e2fc25
JA
519 cq_ring_mask = *cring->ring_mask;
520 return 0;
521}
522
2e7888ef
JA
523static void file_depths(char *buf)
524{
26976a13 525 bool prev = false;
2e7888ef 526 char *p;
54319661 527 int i, j;
2e7888ef 528
ac21bfab 529 buf[0] = '\0';
2e7888ef 530 p = buf;
54319661
JA
531 for (j = 0; j < nthreads; j++) {
532 struct submitter *s = get_submitter(j);
2e7888ef 533
54319661
JA
534 for (i = 0; i < s->nr_files; i++) {
535 struct file *f = &s->files[i];
536
26976a13
JA
537 if (prev)
538 p += sprintf(p, " %d", f->pending_ios);
54319661 539 else
4f215227 540 p += sprintf(p, "%d", f->pending_ios);
26976a13 541 prev = true;
54319661 542 }
2e7888ef
JA
543 }
544}
545
d79ff8c9 546static void usage(char *argv, int status)
e39863e3
KB
547{
548 printf("%s [options] -- [filenames]\n"
35268e11
AJ
549 " -d <int> : IO Depth, default %d\n"
550 " -s <int> : Batch submit, default %d\n"
551 " -c <int> : Batch complete, default %d\n"
552 " -b <int> : Block size, default %d\n"
553 " -p <bool> : Polled IO, default %d\n"
554 " -B <bool> : Fixed buffers, default %d\n"
555 " -F <bool> : Register files, default %d\n"
556 " -n <int> : Number of threads, default %d\n",
557 argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled,
558 fixedbufs, register_files, nthreads);
d79ff8c9 559 exit(status);
e39863e3
KB
560}
561
c9fb4c5b
JA
562int main(int argc, char *argv[])
563{
e39863e3 564 struct submitter *s;
05138221 565 unsigned long done, calls, reap;
d79ff8c9
AJ
566 int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles;
567 struct file f;
2e7888ef 568 char *fdepths;
c3e2fc25 569 void *ret;
c9fb4c5b 570
8025517d 571 if (!do_nop && argc < 2) {
e39863e3 572 printf("%s: filename [options]\n", argv[0]);
c9fb4c5b
JA
573 return 1;
574 }
575
54319661 576 while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:h?")) != -1) {
e39863e3
KB
577 switch (opt) {
578 case 'd':
579 depth = atoi(optarg);
580 break;
581 case 's':
582 batch_submit = atoi(optarg);
583 break;
584 case 'c':
585 batch_complete = atoi(optarg);
586 break;
5bd526f2
JA
587 case 'b':
588 bs = atoi(optarg);
589 break;
590 case 'p':
591 polled = !!atoi(optarg);
592 break;
6a87c3b0
JA
593 case 'B':
594 fixedbufs = !!atoi(optarg);
595 break;
596 case 'F':
597 register_files = !!atoi(optarg);
598 break;
54319661
JA
599 case 'n':
600 nthreads = atoi(optarg);
caf2b9ac
JA
601 if (!nthreads) {
602 printf("Threads must be non-zero\n");
603 usage(argv[0], 1);
604 }
54319661 605 break;
e39863e3
KB
606 case 'h':
607 case '?':
608 default:
d79ff8c9 609 usage(argv[0], 0);
e39863e3
KB
610 break;
611 }
612 }
613
54319661
JA
614 submitter = calloc(nthreads, sizeof(*submitter) +
615 depth * sizeof(struct iovec));
616 for (j = 0; j < nthreads; j++) {
617 s = get_submitter(j);
618 s->index = j;
619 s->done = s->calls = s->reaps = 0;
620 }
e39863e3 621
701d1277 622 flags = O_RDONLY | O_NOATIME;
a7086591
JA
623 if (!buffered)
624 flags |= O_DIRECT;
625
54319661 626 j = 0;
e39863e3 627 i = optind;
d79ff8c9 628 nfiles = argc - i;
2ac00585
JA
629 if (!do_nop) {
630 if (!nfiles) {
631 printf("No files specified\n");
632 usage(argv[0], 1);
633 }
634 threads_per_f = nthreads / nfiles;
635 /* make sure each thread gets assigned files */
636 if (threads_per_f == 0) {
637 threads_per_f = 1;
638 } else {
639 threads_rem = nthreads - threads_per_f * nfiles;
640 }
d79ff8c9 641 }
8025517d 642 while (!do_nop && i < argc) {
d79ff8c9
AJ
643 int k, limit;
644
645 memset(&f, 0, sizeof(f));
a7086591
JA
646
647 fd = open(argv[i], flags);
648 if (fd < 0) {
649 perror("open");
650 return 1;
651 }
d79ff8c9
AJ
652 f.real_fd = fd;
653 if (get_file_size(&f)) {
a7086591
JA
654 printf("failed getting size of device/file\n");
655 return 1;
656 }
d79ff8c9 657 if (f.max_blocks <= 1) {
a7086591
JA
658 printf("Zero file/device size?\n");
659 return 1;
660 }
d79ff8c9
AJ
661 f.max_blocks--;
662
663 limit = threads_per_f;
664 limit += threads_rem > 0 ? 1 : 0;
665 for (k = 0; k < limit; k++) {
666 s = get_submitter((j + k) % nthreads);
a7086591 667
d79ff8c9
AJ
668 if (s->nr_files == MAX_FDS) {
669 printf("Max number of files (%d) reached\n", MAX_FDS);
670 break;
671 }
672
673 memcpy(&s->files[s->nr_files], &f, sizeof(f));
674
675 printf("Added file %s (submitter %d)\n", argv[i], s->index);
676 s->nr_files++;
677 }
678 threads_rem--;
a7086591 679 i++;
d79ff8c9 680 j += limit;
a7086591
JA
681 }
682
c9fb4c5b
JA
683 arm_sig_int();
684
54319661
JA
685 for (j = 0; j < nthreads; j++) {
686 s = get_submitter(j);
687 for (i = 0; i < depth; i++) {
688 void *buf;
c9fb4c5b 689
54319661
JA
690 if (posix_memalign(&buf, bs, bs)) {
691 printf("failed alloc\n");
692 return 1;
693 }
694 s->iovecs[i].iov_base = buf;
695 s->iovecs[i].iov_len = bs;
c9fb4c5b 696 }
c9fb4c5b
JA
697 }
698
54319661
JA
699 for (j = 0; j < nthreads; j++) {
700 s = get_submitter(j);
701
702 err = setup_ring(s);
703 if (err) {
704 printf("ring setup failed: %s, %d\n", strerror(errno), err);
705 return 1;
706 }
c9fb4c5b 707 }
54319661 708 s = get_submitter(0);
6a87c3b0 709 printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered);
e39863e3 710 printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
c9fb4c5b 711
54319661
JA
712 for (j = 0; j < nthreads; j++) {
713 s = get_submitter(j);
714 pthread_create(&s->thread, NULL, submitter_fn, s);
715 }
c9fb4c5b 716
54319661 717 fdepths = malloc(8 * s->nr_files * nthreads);
05138221 718 reap = calls = done = 0;
c9fb4c5b
JA
719 do {
720 unsigned long this_done = 0;
721 unsigned long this_reap = 0;
722 unsigned long this_call = 0;
723 unsigned long rpc = 0, ipc = 0;
724
725 sleep(1);
54319661
JA
726 for (j = 0; j < nthreads; j++) {
727 this_done += s->done;
728 this_call += s->calls;
729 this_reap += s->reaps;
730 }
c9fb4c5b
JA
731 if (this_call - calls) {
732 rpc = (this_done - done) / (this_call - calls);
733 ipc = (this_reap - reap) / (this_call - calls);
191561c1
JA
734 } else
735 rpc = ipc = -1;
2e7888ef 736 file_depths(fdepths);
4f215227
JA
737 printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=(%s)\n",
738 this_done - done, rpc, ipc, fdepths);
c9fb4c5b
JA
739 done = this_done;
740 calls = this_call;
741 reap = this_reap;
742 } while (!finish);
743
54319661
JA
744 for (j = 0; j < nthreads; j++) {
745 s = get_submitter(j);
746 pthread_join(s->thread, &ret);
747 close(s->ring_fd);
748 }
2e7888ef 749 free(fdepths);
54319661 750 free(submitter);
c9fb4c5b
JA
751 return 0;
752}