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" |
f3e769a4 | 25 | #include "../os/linux/io_uring.h" |
ac122fea | 26 | |
e31b8288 | 27 | #define min(a, b) ((a < b) ? (a) : (b)) |
c3e2fc25 | 28 | |
e31b8288 | 29 | struct 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 | 38 | struct 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 | 53 | static unsigned sq_ring_mask, cq_ring_mask; |
e39c34dc | 54 | |
a7086591 JA |
55 | struct 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 |
62 | struct 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 | 83 | static struct submitter *submitter; |
c9fb4c5b JA |
84 | static volatile int finish; |
85 | ||
e39863e3 KB |
86 | static int depth = DEPTH; |
87 | static int batch_submit = BATCH_SUBMIT; | |
88 | static int batch_complete = BATCH_COMPLETE; | |
5bd526f2 | 89 | static int bs = BS; |
f0403f94 | 90 | static int polled = 1; /* use IO polling */ |
701d1277 | 91 | static int fixedbufs = 1; /* use fixed user buffers */ |
8c5fa755 | 92 | static int register_files = 1; /* use fixed files */ |
f0403f94 | 93 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
3d7d00a3 JA |
94 | static int sq_thread_poll = 0; /* use kernel submission/poller thread */ |
95 | static int sq_thread_cpu = -1; /* pin above thread to this CPU */ | |
8025517d | 96 | static int do_nop = 0; /* no-op SQ ring commands */ |
54319661 | 97 | static int nthreads = 1; |
c9fb4c5b | 98 | |
84106576 | 99 | static int vectored = 1; |
b3915995 | 100 | |
2ea53ca3 | 101 | static 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 |
110 | static 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 |
127 | static 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 |
132 | static 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 |
151 | out: |
152 | free(p); | |
153 | } | |
154 | ||
c3e2fc25 JA |
155 | static 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 |
163 | static int gettid(void) |
164 | { | |
165 | return syscall(__NR_gettid); | |
166 | } | |
77d814a1 | 167 | #endif |
c9fb4c5b | 168 | |
701d1277 JA |
169 | static unsigned file_depth(struct submitter *s) |
170 | { | |
e39863e3 | 171 | return (depth + s->nr_files - 1) / s->nr_files; |
701d1277 JA |
172 | } |
173 | ||
a7086591 | 174 | static 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 | 230 | static 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 | 253 | static 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 | ||
275 | static 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 | ||
310 | static 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; |
330 | submit_more: | |
331 | to_submit = prepped; | |
332 | submit: | |
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 |
409 | static 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 |
419 | static 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 | ||
431 | static 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 |
441 | static 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 |
516 | static 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 |
539 | static void usage(char *argv) |
540 | { | |
541 | printf("%s [options] -- [filenames]\n" | |
542 | " -d <int> : IO Depth, default %d\n" | |
543 | " -s <int> : Batch submit, default %d\n" | |
5bd526f2 JA |
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 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled); | |
e39863e3 KB |
548 | exit(0); |
549 | } | |
550 | ||
c9fb4c5b JA |
551 | int main(int argc, char *argv[]) |
552 | { | |
e39863e3 | 553 | struct submitter *s; |
05138221 | 554 | unsigned long done, calls, reap; |
54319661 | 555 | int err, i, j, flags, fd, opt; |
2e7888ef | 556 | char *fdepths; |
c3e2fc25 | 557 | void *ret; |
c9fb4c5b | 558 | |
8025517d | 559 | if (!do_nop && argc < 2) { |
e39863e3 | 560 | printf("%s: filename [options]\n", argv[0]); |
c9fb4c5b JA |
561 | return 1; |
562 | } | |
563 | ||
54319661 | 564 | while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:h?")) != -1) { |
e39863e3 KB |
565 | switch (opt) { |
566 | case 'd': | |
567 | depth = atoi(optarg); | |
568 | break; | |
569 | case 's': | |
570 | batch_submit = atoi(optarg); | |
571 | break; | |
572 | case 'c': | |
573 | batch_complete = atoi(optarg); | |
574 | break; | |
5bd526f2 JA |
575 | case 'b': |
576 | bs = atoi(optarg); | |
577 | break; | |
578 | case 'p': | |
579 | polled = !!atoi(optarg); | |
580 | break; | |
6a87c3b0 JA |
581 | case 'B': |
582 | fixedbufs = !!atoi(optarg); | |
583 | break; | |
584 | case 'F': | |
585 | register_files = !!atoi(optarg); | |
586 | break; | |
54319661 JA |
587 | case 'n': |
588 | nthreads = atoi(optarg); | |
589 | break; | |
e39863e3 KB |
590 | case 'h': |
591 | case '?': | |
592 | default: | |
593 | usage(argv[0]); | |
594 | break; | |
595 | } | |
596 | } | |
597 | ||
54319661 JA |
598 | submitter = calloc(nthreads, sizeof(*submitter) + |
599 | depth * sizeof(struct iovec)); | |
600 | for (j = 0; j < nthreads; j++) { | |
601 | s = get_submitter(j); | |
602 | s->index = j; | |
603 | s->done = s->calls = s->reaps = 0; | |
604 | } | |
e39863e3 | 605 | |
701d1277 | 606 | flags = O_RDONLY | O_NOATIME; |
a7086591 JA |
607 | if (!buffered) |
608 | flags |= O_DIRECT; | |
609 | ||
54319661 | 610 | j = 0; |
e39863e3 | 611 | i = optind; |
54319661 | 612 | printf("i %d, argc %d\n", i, argc); |
8025517d | 613 | while (!do_nop && i < argc) { |
be26a982 | 614 | struct file *f; |
a7086591 | 615 | |
54319661 | 616 | s = get_submitter(j); |
be26a982 JA |
617 | if (s->nr_files == MAX_FDS) { |
618 | printf("Max number of files (%d) reached\n", MAX_FDS); | |
619 | break; | |
620 | } | |
a7086591 JA |
621 | fd = open(argv[i], flags); |
622 | if (fd < 0) { | |
623 | perror("open"); | |
624 | return 1; | |
625 | } | |
be26a982 JA |
626 | |
627 | f = &s->files[s->nr_files]; | |
48e698fa | 628 | f->real_fd = fd; |
a7086591 JA |
629 | if (get_file_size(f)) { |
630 | printf("failed getting size of device/file\n"); | |
631 | return 1; | |
632 | } | |
633 | if (f->max_blocks <= 1) { | |
634 | printf("Zero file/device size?\n"); | |
635 | return 1; | |
636 | } | |
637 | f->max_blocks--; | |
638 | ||
54319661 | 639 | printf("Added file %s (submitter %d)\n", argv[i], s->index); |
a7086591 JA |
640 | s->nr_files++; |
641 | i++; | |
54319661 JA |
642 | if (++j >= nthreads) |
643 | j = 0; | |
a7086591 JA |
644 | } |
645 | ||
b3ce355d JA |
646 | if (fixedbufs) { |
647 | struct rlimit rlim; | |
648 | ||
649 | rlim.rlim_cur = RLIM_INFINITY; | |
650 | rlim.rlim_max = RLIM_INFINITY; | |
651 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
652 | perror("setrlimit"); | |
653 | return 1; | |
654 | } | |
c9fb4c5b JA |
655 | } |
656 | ||
657 | arm_sig_int(); | |
658 | ||
54319661 JA |
659 | for (j = 0; j < nthreads; j++) { |
660 | s = get_submitter(j); | |
661 | for (i = 0; i < depth; i++) { | |
662 | void *buf; | |
c9fb4c5b | 663 | |
54319661 JA |
664 | if (posix_memalign(&buf, bs, bs)) { |
665 | printf("failed alloc\n"); | |
666 | return 1; | |
667 | } | |
668 | s->iovecs[i].iov_base = buf; | |
669 | s->iovecs[i].iov_len = bs; | |
c9fb4c5b | 670 | } |
c9fb4c5b JA |
671 | } |
672 | ||
54319661 JA |
673 | for (j = 0; j < nthreads; j++) { |
674 | s = get_submitter(j); | |
675 | ||
676 | err = setup_ring(s); | |
677 | if (err) { | |
678 | printf("ring setup failed: %s, %d\n", strerror(errno), err); | |
679 | return 1; | |
680 | } | |
c9fb4c5b | 681 | } |
54319661 | 682 | s = get_submitter(0); |
6a87c3b0 | 683 | printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered); |
e39863e3 | 684 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); |
c9fb4c5b | 685 | |
54319661 JA |
686 | for (j = 0; j < nthreads; j++) { |
687 | s = get_submitter(j); | |
688 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
689 | } | |
c9fb4c5b | 690 | |
54319661 | 691 | fdepths = malloc(8 * s->nr_files * nthreads); |
05138221 | 692 | reap = calls = done = 0; |
c9fb4c5b JA |
693 | do { |
694 | unsigned long this_done = 0; | |
695 | unsigned long this_reap = 0; | |
696 | unsigned long this_call = 0; | |
697 | unsigned long rpc = 0, ipc = 0; | |
698 | ||
699 | sleep(1); | |
54319661 JA |
700 | for (j = 0; j < nthreads; j++) { |
701 | this_done += s->done; | |
702 | this_call += s->calls; | |
703 | this_reap += s->reaps; | |
704 | } | |
c9fb4c5b JA |
705 | if (this_call - calls) { |
706 | rpc = (this_done - done) / (this_call - calls); | |
707 | ipc = (this_reap - reap) / (this_call - calls); | |
191561c1 JA |
708 | } else |
709 | rpc = ipc = -1; | |
2e7888ef | 710 | file_depths(fdepths); |
4f215227 JA |
711 | printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=(%s)\n", |
712 | this_done - done, rpc, ipc, fdepths); | |
c9fb4c5b JA |
713 | done = this_done; |
714 | calls = this_call; | |
715 | reap = this_reap; | |
716 | } while (!finish); | |
717 | ||
54319661 JA |
718 | for (j = 0; j < nthreads; j++) { |
719 | s = get_submitter(j); | |
720 | pthread_join(s->thread, &ret); | |
721 | close(s->ring_fd); | |
722 | } | |
2e7888ef | 723 | free(fdepths); |
54319661 | 724 | free(submitter); |
c9fb4c5b JA |
725 | return 0; |
726 | } |