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