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 | 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 |
523 | static 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 | 546 | static 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" | |
0862f718 | 556 | " -n <int> : Number of threads, default %d\n" |
2686fc22 | 557 | " -O <bool> : Use O_DIRECT, default %d\n" |
0862f718 | 558 | " -N <bool> : Perform just no-op requests, default %d\n", |
35268e11 | 559 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled, |
2686fc22 | 560 | fixedbufs, register_files, nthreads, !buffered, do_nop); |
d79ff8c9 | 561 | exit(status); |
e39863e3 KB |
562 | } |
563 | ||
c9fb4c5b JA |
564 | int main(int argc, char *argv[]) |
565 | { | |
e39863e3 | 566 | struct submitter *s; |
05138221 | 567 | unsigned long done, calls, reap; |
d79ff8c9 AJ |
568 | int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles; |
569 | struct file f; | |
2e7888ef | 570 | char *fdepths; |
c3e2fc25 | 571 | void *ret; |
c9fb4c5b | 572 | |
0862f718 JA |
573 | if (!do_nop && argc < 2) |
574 | usage(argv[0], 1); | |
c9fb4c5b | 575 | |
2686fc22 | 576 | while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O: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; |
0862f718 JA |
606 | case 'N': |
607 | do_nop = !!atoi(optarg); | |
608 | break; | |
2686fc22 JA |
609 | case 'O': |
610 | buffered = !atoi(optarg); | |
611 | break; | |
e39863e3 KB |
612 | case 'h': |
613 | case '?': | |
614 | default: | |
d79ff8c9 | 615 | usage(argv[0], 0); |
e39863e3 KB |
616 | break; |
617 | } | |
618 | } | |
619 | ||
c5347611 JA |
620 | if (batch_complete > depth) |
621 | batch_complete = depth; | |
622 | if (batch_submit > depth) | |
623 | batch_submit = depth; | |
624 | ||
54319661 JA |
625 | submitter = calloc(nthreads, sizeof(*submitter) + |
626 | depth * sizeof(struct iovec)); | |
627 | for (j = 0; j < nthreads; j++) { | |
628 | s = get_submitter(j); | |
629 | s->index = j; | |
630 | s->done = s->calls = s->reaps = 0; | |
631 | } | |
e39863e3 | 632 | |
701d1277 | 633 | flags = O_RDONLY | O_NOATIME; |
a7086591 JA |
634 | if (!buffered) |
635 | flags |= O_DIRECT; | |
636 | ||
54319661 | 637 | j = 0; |
e39863e3 | 638 | i = optind; |
d79ff8c9 | 639 | nfiles = argc - i; |
2ac00585 JA |
640 | if (!do_nop) { |
641 | if (!nfiles) { | |
642 | printf("No files specified\n"); | |
643 | usage(argv[0], 1); | |
644 | } | |
645 | threads_per_f = nthreads / nfiles; | |
646 | /* make sure each thread gets assigned files */ | |
647 | if (threads_per_f == 0) { | |
648 | threads_per_f = 1; | |
649 | } else { | |
650 | threads_rem = nthreads - threads_per_f * nfiles; | |
651 | } | |
d79ff8c9 | 652 | } |
8025517d | 653 | while (!do_nop && i < argc) { |
d79ff8c9 AJ |
654 | int k, limit; |
655 | ||
656 | memset(&f, 0, sizeof(f)); | |
a7086591 JA |
657 | |
658 | fd = open(argv[i], flags); | |
659 | if (fd < 0) { | |
660 | perror("open"); | |
661 | return 1; | |
662 | } | |
d79ff8c9 AJ |
663 | f.real_fd = fd; |
664 | if (get_file_size(&f)) { | |
a7086591 JA |
665 | printf("failed getting size of device/file\n"); |
666 | return 1; | |
667 | } | |
d79ff8c9 | 668 | if (f.max_blocks <= 1) { |
a7086591 JA |
669 | printf("Zero file/device size?\n"); |
670 | return 1; | |
671 | } | |
d79ff8c9 AJ |
672 | f.max_blocks--; |
673 | ||
674 | limit = threads_per_f; | |
675 | limit += threads_rem > 0 ? 1 : 0; | |
676 | for (k = 0; k < limit; k++) { | |
677 | s = get_submitter((j + k) % nthreads); | |
a7086591 | 678 | |
d79ff8c9 AJ |
679 | if (s->nr_files == MAX_FDS) { |
680 | printf("Max number of files (%d) reached\n", MAX_FDS); | |
681 | break; | |
682 | } | |
683 | ||
684 | memcpy(&s->files[s->nr_files], &f, sizeof(f)); | |
685 | ||
686 | printf("Added file %s (submitter %d)\n", argv[i], s->index); | |
687 | s->nr_files++; | |
688 | } | |
689 | threads_rem--; | |
a7086591 | 690 | i++; |
d79ff8c9 | 691 | j += limit; |
a7086591 JA |
692 | } |
693 | ||
c9fb4c5b JA |
694 | arm_sig_int(); |
695 | ||
54319661 JA |
696 | for (j = 0; j < nthreads; j++) { |
697 | s = get_submitter(j); | |
698 | for (i = 0; i < depth; i++) { | |
699 | void *buf; | |
c9fb4c5b | 700 | |
54319661 JA |
701 | if (posix_memalign(&buf, bs, bs)) { |
702 | printf("failed alloc\n"); | |
703 | return 1; | |
704 | } | |
705 | s->iovecs[i].iov_base = buf; | |
706 | s->iovecs[i].iov_len = bs; | |
c9fb4c5b | 707 | } |
c9fb4c5b JA |
708 | } |
709 | ||
54319661 JA |
710 | for (j = 0; j < nthreads; j++) { |
711 | s = get_submitter(j); | |
712 | ||
713 | err = setup_ring(s); | |
714 | if (err) { | |
715 | printf("ring setup failed: %s, %d\n", strerror(errno), err); | |
716 | return 1; | |
717 | } | |
c9fb4c5b | 718 | } |
54319661 | 719 | s = get_submitter(0); |
6a87c3b0 | 720 | printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d", polled, fixedbufs, register_files, buffered); |
e39863e3 | 721 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); |
c9fb4c5b | 722 | |
54319661 JA |
723 | for (j = 0; j < nthreads; j++) { |
724 | s = get_submitter(j); | |
725 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
726 | } | |
c9fb4c5b | 727 | |
54319661 | 728 | fdepths = malloc(8 * s->nr_files * nthreads); |
05138221 | 729 | reap = calls = done = 0; |
c9fb4c5b JA |
730 | do { |
731 | unsigned long this_done = 0; | |
732 | unsigned long this_reap = 0; | |
733 | unsigned long this_call = 0; | |
734 | unsigned long rpc = 0, ipc = 0; | |
f3057d26 | 735 | unsigned long iops, bw; |
c9fb4c5b JA |
736 | |
737 | sleep(1); | |
54319661 JA |
738 | for (j = 0; j < nthreads; j++) { |
739 | this_done += s->done; | |
740 | this_call += s->calls; | |
741 | this_reap += s->reaps; | |
742 | } | |
c9fb4c5b JA |
743 | if (this_call - calls) { |
744 | rpc = (this_done - done) / (this_call - calls); | |
745 | ipc = (this_reap - reap) / (this_call - calls); | |
191561c1 JA |
746 | } else |
747 | rpc = ipc = -1; | |
2e7888ef | 748 | file_depths(fdepths); |
22fd3501 | 749 | iops = this_done - done; |
f3057d26 JA |
750 | if (bs > 1048576) |
751 | bw = iops * (bs / 1048576); | |
752 | else | |
753 | bw = iops / (1048576 / bs); | |
22fd3501 | 754 | printf("IOPS=%lu, BW=%luMiB/s, IOS/call=%ld/%ld, inflight=(%s)\n", |
f3057d26 | 755 | iops, bw, rpc, ipc, fdepths); |
c9fb4c5b JA |
756 | done = this_done; |
757 | calls = this_call; | |
758 | reap = this_reap; | |
759 | } while (!finish); | |
760 | ||
54319661 JA |
761 | for (j = 0; j < nthreads; j++) { |
762 | s = get_submitter(j); | |
763 | pthread_join(s->thread, &ret); | |
764 | close(s->ring_fd); | |
765 | } | |
2e7888ef | 766 | free(fdepths); |
54319661 | 767 | free(submitter); |
c9fb4c5b JA |
768 | return 0; |
769 | } |