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> | |
932131c9 | 8 | #include <math.h> |
c9fb4c5b | 9 | |
256714ea JA |
10 | #ifdef CONFIG_LIBAIO |
11 | #include <libaio.h> | |
12 | #endif | |
13 | ||
c9fb4c5b JA |
14 | #include <sys/types.h> |
15 | #include <sys/stat.h> | |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/syscall.h> | |
18 | #include <sys/resource.h> | |
c3e2fc25 | 19 | #include <sys/mman.h> |
e31b8288 | 20 | #include <sys/uio.h> |
c9fb4c5b JA |
21 | #include <linux/fs.h> |
22 | #include <fcntl.h> | |
23 | #include <unistd.h> | |
c9fb4c5b JA |
24 | #include <string.h> |
25 | #include <pthread.h> | |
26 | #include <sched.h> | |
27 | ||
74efb029 | 28 | #include "../arch/arch.h" |
57fa61f0 | 29 | #include "../lib/types.h" |
932131c9 JA |
30 | #include "../lib/roundup.h" |
31 | #include "../minmax.h" | |
f3e769a4 | 32 | #include "../os/linux/io_uring.h" |
ac122fea | 33 | |
e31b8288 | 34 | struct io_sq_ring { |
e2239016 JA |
35 | unsigned *head; |
36 | unsigned *tail; | |
37 | unsigned *ring_mask; | |
38 | unsigned *ring_entries; | |
ce1705de | 39 | unsigned *flags; |
e2239016 | 40 | unsigned *array; |
c9fb4c5b JA |
41 | }; |
42 | ||
e31b8288 | 43 | struct io_cq_ring { |
e2239016 JA |
44 | unsigned *head; |
45 | unsigned *tail; | |
46 | unsigned *ring_mask; | |
47 | unsigned *ring_entries; | |
f0403f94 | 48 | struct io_uring_cqe *cqes; |
c9fb4c5b JA |
49 | }; |
50 | ||
701d1277 | 51 | #define DEPTH 128 |
2e7888ef JA |
52 | #define BATCH_SUBMIT 32 |
53 | #define BATCH_COMPLETE 32 | |
c9fb4c5b JA |
54 | #define BS 4096 |
55 | ||
a7086591 JA |
56 | #define MAX_FDS 16 |
57 | ||
c3e2fc25 | 58 | static unsigned sq_ring_mask, cq_ring_mask; |
e39c34dc | 59 | |
a7086591 JA |
60 | struct file { |
61 | unsigned long max_blocks; | |
701d1277 | 62 | unsigned pending_ios; |
48e698fa JA |
63 | int real_fd; |
64 | int fixed_fd; | |
932131c9 | 65 | int fileno; |
a7086591 JA |
66 | }; |
67 | ||
932131c9 JA |
68 | #define PLAT_BITS 6 |
69 | #define PLAT_VAL (1 << PLAT_BITS) | |
70 | #define PLAT_GROUP_NR 29 | |
71 | #define PLAT_NR (PLAT_GROUP_NR * PLAT_VAL) | |
72 | ||
c9fb4c5b JA |
73 | struct submitter { |
74 | pthread_t thread; | |
f310970e | 75 | int ring_fd; |
54319661 | 76 | int index; |
e31b8288 | 77 | struct io_sq_ring sq_ring; |
f0403f94 | 78 | struct io_uring_sqe *sqes; |
e31b8288 | 79 | struct io_cq_ring cq_ring; |
c9fb4c5b | 80 | int inflight; |
932131c9 | 81 | int tid; |
c9fb4c5b JA |
82 | unsigned long reaps; |
83 | unsigned long done; | |
84 | unsigned long calls; | |
85 | volatile int finish; | |
701d1277 | 86 | |
48e698fa JA |
87 | __s32 *fds; |
88 | ||
932131c9 JA |
89 | unsigned long *clock_batch; |
90 | int clock_index; | |
91 | unsigned long *plat; | |
92 | ||
256714ea JA |
93 | #ifdef CONFIG_LIBAIO |
94 | io_context_t aio_ctx; | |
95 | #endif | |
96 | ||
a7086591 JA |
97 | struct file files[MAX_FDS]; |
98 | unsigned nr_files; | |
99 | unsigned cur_file; | |
e39863e3 | 100 | struct iovec iovecs[]; |
c9fb4c5b JA |
101 | }; |
102 | ||
e39863e3 | 103 | static struct submitter *submitter; |
c9fb4c5b JA |
104 | static volatile int finish; |
105 | ||
e39863e3 KB |
106 | static int depth = DEPTH; |
107 | static int batch_submit = BATCH_SUBMIT; | |
108 | static int batch_complete = BATCH_COMPLETE; | |
5bd526f2 | 109 | static int bs = BS; |
f0403f94 | 110 | static int polled = 1; /* use IO polling */ |
701d1277 | 111 | static int fixedbufs = 1; /* use fixed user buffers */ |
8c5fa755 | 112 | static int register_files = 1; /* use fixed files */ |
f0403f94 | 113 | static int buffered = 0; /* use buffered IO, not O_DIRECT */ |
3d7d00a3 JA |
114 | static int sq_thread_poll = 0; /* use kernel submission/poller thread */ |
115 | static int sq_thread_cpu = -1; /* pin above thread to this CPU */ | |
8025517d | 116 | static int do_nop = 0; /* no-op SQ ring commands */ |
54319661 | 117 | static int nthreads = 1; |
932131c9 | 118 | static int stats = 0; /* generate IO stats */ |
256714ea JA |
119 | static int aio = 0; /* use libaio */ |
120 | ||
932131c9 | 121 | static unsigned long tsc_rate; |
c9fb4c5b | 122 | |
203e4c26 JA |
123 | #define TSC_RATE_FILE "tsc-rate" |
124 | ||
84106576 | 125 | static int vectored = 1; |
b3915995 | 126 | |
932131c9 | 127 | static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, |
ad45a465 | 128 | 80.0, 90.0, 95.0, 99.0, 99.5, 99.9, 99.95, 99.99 }; |
932131c9 JA |
129 | static int plist_len = 17; |
130 | ||
131 | static unsigned long cycles_to_nsec(unsigned long cycles) | |
132 | { | |
133 | uint64_t val; | |
134 | ||
135 | if (!tsc_rate) | |
136 | return cycles; | |
137 | ||
138 | val = cycles * 1000000000ULL; | |
139 | return val / tsc_rate; | |
140 | } | |
141 | ||
142 | static unsigned long plat_idx_to_val(unsigned int idx) | |
143 | { | |
144 | unsigned int error_bits; | |
145 | unsigned long k, base; | |
146 | ||
147 | assert(idx < PLAT_NR); | |
148 | ||
149 | /* MSB <= (PLAT_BITS-1), cannot be rounded off. Use | |
150 | * all bits of the sample as index */ | |
151 | if (idx < (PLAT_VAL << 1)) | |
152 | return cycles_to_nsec(idx); | |
153 | ||
154 | /* Find the group and compute the minimum value of that group */ | |
155 | error_bits = (idx >> PLAT_BITS) - 1; | |
156 | base = ((unsigned long) 1) << (error_bits + PLAT_BITS); | |
157 | ||
158 | /* Find its bucket number of the group */ | |
159 | k = idx % PLAT_VAL; | |
160 | ||
161 | /* Return the mean of the range of the bucket */ | |
162 | return cycles_to_nsec(base + ((k + 0.5) * (1 << error_bits))); | |
163 | } | |
164 | ||
165 | unsigned int calc_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, | |
166 | unsigned long **output, | |
167 | unsigned long *maxv, unsigned long *minv) | |
168 | { | |
169 | unsigned long sum = 0; | |
170 | unsigned int len = plist_len, i, j = 0; | |
171 | unsigned long *ovals = NULL; | |
172 | bool is_last; | |
173 | ||
174 | *minv = -1ULL; | |
175 | *maxv = 0; | |
176 | ||
177 | ovals = malloc(len * sizeof(*ovals)); | |
178 | if (!ovals) | |
179 | return 0; | |
180 | ||
181 | /* | |
182 | * Calculate bucket values, note down max and min values | |
183 | */ | |
184 | is_last = false; | |
185 | for (i = 0; i < PLAT_NR && !is_last; i++) { | |
186 | sum += io_u_plat[i]; | |
187 | while (sum >= ((long double) plist[j] / 100.0 * nr)) { | |
188 | assert(plist[j] <= 100.0); | |
189 | ||
190 | ovals[j] = plat_idx_to_val(i); | |
191 | if (ovals[j] < *minv) | |
192 | *minv = ovals[j]; | |
193 | if (ovals[j] > *maxv) | |
194 | *maxv = ovals[j]; | |
195 | ||
196 | is_last = (j == len - 1) != 0; | |
197 | if (is_last) | |
198 | break; | |
199 | ||
200 | j++; | |
201 | } | |
202 | } | |
203 | ||
204 | if (!is_last) | |
205 | fprintf(stderr, "error calculating latency percentiles\n"); | |
206 | ||
207 | *output = ovals; | |
208 | return len; | |
209 | } | |
210 | ||
211 | static void show_clat_percentiles(unsigned long *io_u_plat, unsigned long nr, | |
212 | unsigned int precision) | |
213 | { | |
214 | unsigned int divisor, len, i, j = 0; | |
215 | unsigned long minv, maxv; | |
216 | unsigned long *ovals; | |
217 | int per_line, scale_down, time_width; | |
218 | bool is_last; | |
219 | char fmt[32]; | |
220 | ||
221 | len = calc_clat_percentiles(io_u_plat, nr, &ovals, &maxv, &minv); | |
222 | if (!len || !ovals) | |
223 | goto out; | |
224 | ||
225 | if (!tsc_rate) { | |
226 | scale_down = 0; | |
227 | divisor = 1; | |
228 | printf(" percentiles (tsc ticks):\n |"); | |
229 | } else if (minv > 2000 && maxv > 99999) { | |
230 | scale_down = 1; | |
231 | divisor = 1000; | |
232 | printf(" percentiles (usec):\n |"); | |
233 | } else { | |
234 | scale_down = 0; | |
235 | divisor = 1; | |
236 | printf(" percentiles (nsec):\n |"); | |
237 | } | |
238 | ||
239 | time_width = max(5, (int) (log10(maxv / divisor) + 1)); | |
240 | snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3, | |
241 | precision, time_width); | |
242 | /* fmt will be something like " %5.2fth=[%4llu]%c" */ | |
243 | per_line = (80 - 7) / (precision + 10 + time_width); | |
244 | ||
245 | for (j = 0; j < len; j++) { | |
246 | /* for formatting */ | |
247 | if (j != 0 && (j % per_line) == 0) | |
248 | printf(" |"); | |
249 | ||
250 | /* end of the list */ | |
251 | is_last = (j == len - 1) != 0; | |
252 | ||
253 | for (i = 0; i < scale_down; i++) | |
254 | ovals[j] = (ovals[j] + 999) / 1000; | |
255 | ||
256 | printf(fmt, plist[j], ovals[j], is_last ? '\n' : ','); | |
257 | ||
258 | if (is_last) | |
259 | break; | |
260 | ||
261 | if ((j % per_line) == per_line - 1) /* for formatting */ | |
262 | printf("\n"); | |
263 | } | |
264 | ||
265 | out: | |
266 | free(ovals); | |
267 | } | |
268 | ||
269 | static unsigned int plat_val_to_idx(unsigned long val) | |
270 | { | |
271 | unsigned int msb, error_bits, base, offset, idx; | |
272 | ||
273 | /* Find MSB starting from bit 0 */ | |
274 | if (val == 0) | |
275 | msb = 0; | |
276 | else | |
277 | msb = (sizeof(val)*8) - __builtin_clzll(val) - 1; | |
278 | ||
279 | /* | |
280 | * MSB <= (PLAT_BITS-1), cannot be rounded off. Use | |
281 | * all bits of the sample as index | |
282 | */ | |
283 | if (msb <= PLAT_BITS) | |
284 | return val; | |
285 | ||
286 | /* Compute the number of error bits to discard*/ | |
287 | error_bits = msb - PLAT_BITS; | |
288 | ||
289 | /* Compute the number of buckets before the group */ | |
290 | base = (error_bits + 1) << PLAT_BITS; | |
291 | ||
292 | /* | |
293 | * Discard the error bits and apply the mask to find the | |
294 | * index for the buckets in the group | |
295 | */ | |
296 | offset = (PLAT_VAL - 1) & (val >> error_bits); | |
297 | ||
298 | /* Make sure the index does not exceed (array size - 1) */ | |
299 | idx = (base + offset) < (PLAT_NR - 1) ? | |
300 | (base + offset) : (PLAT_NR - 1); | |
301 | ||
302 | return idx; | |
303 | } | |
304 | ||
305 | static void add_stat(struct submitter *s, int clock_index, int nr) | |
306 | { | |
307 | #ifdef ARCH_HAVE_CPU_CLOCK | |
308 | unsigned long cycles; | |
309 | unsigned int pidx; | |
310 | ||
311 | cycles = get_cpu_clock(); | |
312 | cycles -= s->clock_batch[clock_index]; | |
313 | pidx = plat_val_to_idx(cycles); | |
314 | s->plat[pidx] += nr; | |
315 | #endif | |
316 | } | |
317 | ||
2ea53ca3 | 318 | static int io_uring_register_buffers(struct submitter *s) |
c9fb4c5b | 319 | { |
8025517d JA |
320 | if (do_nop) |
321 | return 0; | |
322 | ||
bfed648c | 323 | return syscall(__NR_io_uring_register, s->ring_fd, |
e39863e3 | 324 | IORING_REGISTER_BUFFERS, s->iovecs, depth); |
2ea53ca3 JA |
325 | } |
326 | ||
a7abc9fb JA |
327 | static int io_uring_register_files(struct submitter *s) |
328 | { | |
48e698fa | 329 | int i; |
a7abc9fb | 330 | |
8025517d JA |
331 | if (do_nop) |
332 | return 0; | |
333 | ||
48e698fa JA |
334 | s->fds = calloc(s->nr_files, sizeof(__s32)); |
335 | for (i = 0; i < s->nr_files; i++) { | |
336 | s->fds[i] = s->files[i].real_fd; | |
337 | s->files[i].fixed_fd = i; | |
338 | } | |
a7abc9fb | 339 | |
bfed648c | 340 | return syscall(__NR_io_uring_register, s->ring_fd, |
919850d2 | 341 | IORING_REGISTER_FILES, s->fds, s->nr_files); |
a7abc9fb JA |
342 | } |
343 | ||
2ea53ca3 JA |
344 | static int io_uring_setup(unsigned entries, struct io_uring_params *p) |
345 | { | |
bfed648c | 346 | return syscall(__NR_io_uring_setup, entries, p); |
c9fb4c5b JA |
347 | } |
348 | ||
b3915995 JA |
349 | static void io_uring_probe(int fd) |
350 | { | |
351 | struct io_uring_probe *p; | |
352 | int ret; | |
353 | ||
354 | p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
355 | if (!p) | |
356 | return; | |
357 | ||
358 | memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op)); | |
359 | ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256); | |
360 | if (ret < 0) | |
361 | goto out; | |
362 | ||
363 | if (IORING_OP_READ > p->ops_len) | |
364 | goto out; | |
365 | ||
366 | if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED)) | |
84106576 | 367 | vectored = 0; |
b3915995 JA |
368 | out: |
369 | free(p); | |
370 | } | |
371 | ||
c3e2fc25 JA |
372 | static int io_uring_enter(struct submitter *s, unsigned int to_submit, |
373 | unsigned int min_complete, unsigned int flags) | |
c9fb4c5b | 374 | { |
bfed648c JA |
375 | return syscall(__NR_io_uring_enter, s->ring_fd, to_submit, min_complete, |
376 | flags, NULL, 0); | |
c9fb4c5b JA |
377 | } |
378 | ||
77d814a1 | 379 | #ifndef CONFIG_HAVE_GETTID |
c9fb4c5b JA |
380 | static int gettid(void) |
381 | { | |
382 | return syscall(__NR_gettid); | |
383 | } | |
77d814a1 | 384 | #endif |
c9fb4c5b | 385 | |
701d1277 JA |
386 | static unsigned file_depth(struct submitter *s) |
387 | { | |
e39863e3 | 388 | return (depth + s->nr_files - 1) / s->nr_files; |
701d1277 JA |
389 | } |
390 | ||
a7086591 | 391 | static void init_io(struct submitter *s, unsigned index) |
c9fb4c5b | 392 | { |
f0403f94 | 393 | struct io_uring_sqe *sqe = &s->sqes[index]; |
c9fb4c5b | 394 | unsigned long offset; |
a7086591 | 395 | struct file *f; |
c9fb4c5b JA |
396 | long r; |
397 | ||
8025517d JA |
398 | if (do_nop) { |
399 | sqe->opcode = IORING_OP_NOP; | |
400 | return; | |
401 | } | |
402 | ||
701d1277 JA |
403 | if (s->nr_files == 1) { |
404 | f = &s->files[0]; | |
405 | } else { | |
406 | f = &s->files[s->cur_file]; | |
407 | if (f->pending_ios >= file_depth(s)) { | |
408 | s->cur_file++; | |
409 | if (s->cur_file == s->nr_files) | |
410 | s->cur_file = 0; | |
93d1811c | 411 | f = &s->files[s->cur_file]; |
701d1277 JA |
412 | } |
413 | } | |
414 | f->pending_ios++; | |
a7086591 | 415 | |
5e8865c0 | 416 | r = lrand48(); |
5bd526f2 | 417 | offset = (r % (f->max_blocks - 1)) * bs; |
c9fb4c5b | 418 | |
8c5fa755 JA |
419 | if (register_files) { |
420 | sqe->flags = IOSQE_FIXED_FILE; | |
421 | sqe->fd = f->fixed_fd; | |
422 | } else { | |
423 | sqe->flags = 0; | |
424 | sqe->fd = f->real_fd; | |
425 | } | |
f0403f94 | 426 | if (fixedbufs) { |
48e698fa | 427 | sqe->opcode = IORING_OP_READ_FIXED; |
919850d2 | 428 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; |
5bd526f2 | 429 | sqe->len = bs; |
2ea53ca3 | 430 | sqe->buf_index = index; |
84106576 | 431 | } else if (!vectored) { |
b3915995 JA |
432 | sqe->opcode = IORING_OP_READ; |
433 | sqe->addr = (unsigned long) s->iovecs[index].iov_base; | |
434 | sqe->len = bs; | |
435 | sqe->buf_index = 0; | |
84106576 JA |
436 | } else { |
437 | sqe->opcode = IORING_OP_READV; | |
438 | sqe->addr = (unsigned long) &s->iovecs[index]; | |
439 | sqe->len = 1; | |
440 | sqe->buf_index = 0; | |
f0403f94 | 441 | } |
f0403f94 | 442 | sqe->ioprio = 0; |
f0403f94 | 443 | sqe->off = offset; |
932131c9 JA |
444 | sqe->user_data = (unsigned long) f->fileno; |
445 | if (stats) | |
446 | sqe->user_data |= ((unsigned long)s->clock_index << 32); | |
c9fb4c5b JA |
447 | } |
448 | ||
256714ea | 449 | static int prep_more_ios_uring(struct submitter *s, int max_ios) |
c9fb4c5b | 450 | { |
e31b8288 | 451 | struct io_sq_ring *ring = &s->sq_ring; |
e2239016 | 452 | unsigned index, tail, next_tail, prepped = 0; |
c9fb4c5b | 453 | |
c3e2fc25 | 454 | next_tail = tail = *ring->tail; |
c9fb4c5b JA |
455 | do { |
456 | next_tail++; | |
fc2dc21b | 457 | if (next_tail == atomic_load_acquire(ring->head)) |
c9fb4c5b JA |
458 | break; |
459 | ||
e39c34dc | 460 | index = tail & sq_ring_mask; |
a7086591 | 461 | init_io(s, index); |
c3e2fc25 | 462 | ring->array[index] = index; |
c9fb4c5b JA |
463 | prepped++; |
464 | tail = next_tail; | |
465 | } while (prepped < max_ios); | |
466 | ||
fc2dc21b JA |
467 | if (prepped) |
468 | atomic_store_release(ring->tail, tail); | |
c9fb4c5b JA |
469 | return prepped; |
470 | } | |
471 | ||
a7086591 | 472 | static int get_file_size(struct file *f) |
c9fb4c5b JA |
473 | { |
474 | struct stat st; | |
475 | ||
48e698fa | 476 | if (fstat(f->real_fd, &st) < 0) |
c9fb4c5b JA |
477 | return -1; |
478 | if (S_ISBLK(st.st_mode)) { | |
479 | unsigned long long bytes; | |
480 | ||
48e698fa | 481 | if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0) |
c9fb4c5b JA |
482 | return -1; |
483 | ||
5bd526f2 | 484 | f->max_blocks = bytes / bs; |
c9fb4c5b JA |
485 | return 0; |
486 | } else if (S_ISREG(st.st_mode)) { | |
5bd526f2 | 487 | f->max_blocks = st.st_size / bs; |
c9fb4c5b JA |
488 | return 0; |
489 | } | |
490 | ||
491 | return -1; | |
492 | } | |
493 | ||
256714ea | 494 | static int reap_events_uring(struct submitter *s) |
c9fb4c5b | 495 | { |
e31b8288 | 496 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 497 | struct io_uring_cqe *cqe; |
e2239016 | 498 | unsigned head, reaped = 0; |
ab85494f | 499 | int last_idx = -1, stat_nr = 0; |
c9fb4c5b | 500 | |
c3e2fc25 | 501 | head = *ring->head; |
c9fb4c5b | 502 | do { |
701d1277 JA |
503 | struct file *f; |
504 | ||
679d8352 | 505 | read_barrier(); |
fc2dc21b | 506 | if (head == atomic_load_acquire(ring->tail)) |
c9fb4c5b | 507 | break; |
f0403f94 | 508 | cqe = &ring->cqes[head & cq_ring_mask]; |
8025517d | 509 | if (!do_nop) { |
932131c9 JA |
510 | int fileno = cqe->user_data & 0xffffffff; |
511 | ||
512 | f = &s->files[fileno]; | |
8025517d | 513 | f->pending_ios--; |
5bd526f2 | 514 | if (cqe->res != bs) { |
8025517d | 515 | printf("io: unexpected ret=%d\n", cqe->res); |
154a9582 | 516 | if (polled && cqe->res == -EOPNOTSUPP) |
8066f6b6 | 517 | printf("Your filesystem/driver/kernel doesn't support polled IO\n"); |
8025517d JA |
518 | return -1; |
519 | } | |
c9fb4c5b | 520 | } |
932131c9 JA |
521 | if (stats) { |
522 | int clock_index = cqe->user_data >> 32; | |
523 | ||
ab85494f JA |
524 | if (last_idx != clock_index) { |
525 | if (last_idx != -1) { | |
526 | add_stat(s, last_idx, stat_nr); | |
527 | stat_nr = 0; | |
528 | } | |
529 | last_idx = clock_index; | |
530 | } | |
531 | stat_nr++; | |
932131c9 | 532 | } |
c9fb4c5b JA |
533 | reaped++; |
534 | head++; | |
c9fb4c5b JA |
535 | } while (1); |
536 | ||
ab85494f JA |
537 | if (stat_nr) |
538 | add_stat(s, last_idx, stat_nr); | |
539 | ||
fc2dc21b JA |
540 | if (reaped) { |
541 | s->inflight -= reaped; | |
542 | atomic_store_release(ring->head, head); | |
543 | } | |
c9fb4c5b JA |
544 | return reaped; |
545 | } | |
546 | ||
256714ea | 547 | static int submitter_init(struct submitter *s) |
c9fb4c5b | 548 | { |
256714ea | 549 | int i, nr_batch; |
c9fb4c5b | 550 | |
932131c9 JA |
551 | s->tid = gettid(); |
552 | printf("submitter=%d\n", s->tid); | |
c9fb4c5b | 553 | |
5e8865c0 | 554 | srand48(pthread_self()); |
c9fb4c5b | 555 | |
932131c9 JA |
556 | for (i = 0; i < MAX_FDS; i++) |
557 | s->files[i].fileno = i; | |
558 | ||
559 | if (stats) { | |
560 | nr_batch = roundup_pow2(depth / batch_submit); | |
561 | s->clock_batch = calloc(nr_batch, sizeof(unsigned long)); | |
562 | s->clock_index = 0; | |
563 | ||
564 | s->plat = calloc(PLAT_NR, sizeof(unsigned long)); | |
565 | } else { | |
566 | s->clock_batch = NULL; | |
567 | s->plat = NULL; | |
568 | nr_batch = 0; | |
569 | } | |
570 | ||
256714ea JA |
571 | return nr_batch; |
572 | } | |
573 | ||
574 | #ifdef CONFIG_LIBAIO | |
575 | static int prep_more_ios_aio(struct submitter *s, int max_ios, struct iocb *iocbs) | |
576 | { | |
577 | unsigned long offset, data; | |
578 | struct file *f; | |
579 | unsigned index; | |
580 | long r; | |
581 | ||
582 | index = 0; | |
583 | while (index < max_ios) { | |
584 | struct iocb *iocb = &iocbs[index]; | |
585 | ||
586 | if (s->nr_files == 1) { | |
587 | f = &s->files[0]; | |
588 | } else { | |
589 | f = &s->files[s->cur_file]; | |
590 | if (f->pending_ios >= file_depth(s)) { | |
591 | s->cur_file++; | |
592 | if (s->cur_file == s->nr_files) | |
593 | s->cur_file = 0; | |
594 | f = &s->files[s->cur_file]; | |
595 | } | |
596 | } | |
597 | f->pending_ios++; | |
598 | ||
599 | r = lrand48(); | |
600 | offset = (r % (f->max_blocks - 1)) * bs; | |
601 | io_prep_pread(iocb, f->real_fd, s->iovecs[index].iov_base, | |
602 | s->iovecs[index].iov_len, offset); | |
603 | ||
604 | data = f->fileno; | |
605 | if (stats) | |
606 | data |= ((unsigned long) s->clock_index << 32); | |
607 | iocb->data = (void *) (uintptr_t) data; | |
608 | index++; | |
609 | } | |
610 | return index; | |
611 | } | |
612 | ||
613 | static int reap_events_aio(struct submitter *s, struct io_event *events, int evs) | |
614 | { | |
615 | int last_idx = -1, stat_nr = 0; | |
616 | int reaped = 0; | |
617 | ||
618 | while (evs) { | |
619 | unsigned long data = (uintptr_t) events[reaped].data; | |
620 | struct file *f = &s->files[data & 0xffffffff]; | |
621 | ||
622 | f->pending_ios--; | |
623 | if (events[reaped].res != bs) { | |
624 | printf("io: unexpected ret=%ld\n", events[reaped].res); | |
625 | return -1; | |
626 | } | |
627 | if (stats) { | |
628 | int clock_index = data >> 32; | |
629 | ||
630 | if (last_idx != clock_index) { | |
631 | if (last_idx != -1) { | |
632 | add_stat(s, last_idx, stat_nr); | |
633 | stat_nr = 0; | |
634 | } | |
635 | last_idx = clock_index; | |
636 | } | |
637 | stat_nr++; | |
638 | } | |
639 | reaped++; | |
640 | evs--; | |
641 | } | |
642 | ||
643 | if (stat_nr) | |
644 | add_stat(s, last_idx, stat_nr); | |
645 | ||
646 | s->inflight -= reaped; | |
647 | s->done += reaped; | |
648 | return reaped; | |
649 | } | |
650 | ||
651 | static void *submitter_aio_fn(void *data) | |
652 | { | |
653 | struct submitter *s = data; | |
654 | int i, ret, prepped, nr_batch; | |
655 | struct iocb **iocbsptr; | |
656 | struct iocb *iocbs; | |
657 | struct io_event *events; | |
658 | ||
659 | nr_batch = submitter_init(s); | |
660 | ||
661 | iocbsptr = calloc(depth, sizeof(struct iocb *)); | |
662 | iocbs = calloc(depth, sizeof(struct iocb)); | |
663 | events = calloc(depth, sizeof(struct io_event)); | |
664 | ||
665 | for (i = 0; i < depth; i++) | |
666 | iocbsptr[i] = &iocbs[i]; | |
667 | ||
668 | prepped = 0; | |
669 | do { | |
670 | int to_wait, to_submit, to_prep; | |
671 | ||
672 | if (!prepped && s->inflight < depth) { | |
673 | to_prep = min(depth - s->inflight, batch_submit); | |
674 | prepped = prep_more_ios_aio(s, to_prep, iocbs); | |
675 | #ifdef ARCH_HAVE_CPU_CLOCK | |
676 | if (prepped && stats) { | |
677 | s->clock_batch[s->clock_index] = get_cpu_clock(); | |
678 | s->clock_index = (s->clock_index + 1) & (nr_batch - 1); | |
679 | } | |
680 | #endif | |
681 | } | |
682 | s->inflight += prepped; | |
683 | to_submit = prepped; | |
684 | ||
685 | if (to_submit && (s->inflight + to_submit <= depth)) | |
686 | to_wait = 0; | |
687 | else | |
688 | to_wait = min(s->inflight + to_submit, batch_complete); | |
689 | ||
690 | ret = io_submit(s->aio_ctx, to_submit, iocbsptr); | |
691 | s->calls++; | |
692 | if (ret < 0) { | |
693 | perror("io_submit"); | |
694 | break; | |
695 | } else if (ret != to_submit) { | |
696 | printf("submitted %d, wanted %d\n", ret, to_submit); | |
697 | break; | |
698 | } | |
699 | prepped = 0; | |
700 | ||
701 | if (to_wait) { | |
702 | int r; | |
703 | ||
704 | do { | |
705 | s->calls++; | |
706 | r = io_getevents(s->aio_ctx, to_wait, to_wait, events, NULL); | |
707 | if (r < 0) { | |
708 | perror("io_getevents"); | |
709 | break; | |
710 | } else if (r != to_wait) { | |
711 | printf("r=%d, wait=%d\n", r, to_wait); | |
712 | break; | |
713 | } | |
714 | r = reap_events_aio(s, events, r); | |
715 | s->reaps += r; | |
716 | to_wait -= r; | |
717 | } while (to_wait); | |
718 | } | |
719 | } while (!s->finish); | |
720 | ||
721 | free(iocbsptr); | |
722 | free(iocbs); | |
723 | free(events); | |
724 | finish = 1; | |
725 | return NULL; | |
726 | } | |
727 | #endif | |
728 | ||
729 | static void *submitter_uring_fn(void *data) | |
730 | { | |
731 | struct submitter *s = data; | |
732 | struct io_sq_ring *ring = &s->sq_ring; | |
733 | int ret, prepped, nr_batch; | |
734 | ||
735 | nr_batch = submitter_init(s); | |
736 | ||
c9fb4c5b JA |
737 | prepped = 0; |
738 | do { | |
f310970e | 739 | int to_wait, to_submit, this_reap, to_prep; |
fc2dc21b | 740 | unsigned ring_flags = 0; |
c9fb4c5b | 741 | |
e39863e3 KB |
742 | if (!prepped && s->inflight < depth) { |
743 | to_prep = min(depth - s->inflight, batch_submit); | |
256714ea | 744 | prepped = prep_more_ios_uring(s, to_prep); |
932131c9 JA |
745 | #ifdef ARCH_HAVE_CPU_CLOCK |
746 | if (prepped && stats) { | |
747 | s->clock_batch[s->clock_index] = get_cpu_clock(); | |
748 | s->clock_index = (s->clock_index + 1) & (nr_batch - 1); | |
749 | } | |
750 | #endif | |
f310970e | 751 | } |
c9fb4c5b JA |
752 | s->inflight += prepped; |
753 | submit_more: | |
754 | to_submit = prepped; | |
755 | submit: | |
e39863e3 | 756 | if (to_submit && (s->inflight + to_submit <= depth)) |
c9fb4c5b JA |
757 | to_wait = 0; |
758 | else | |
e39863e3 | 759 | to_wait = min(s->inflight + to_submit, batch_complete); |
c9fb4c5b | 760 | |
ce1705de JA |
761 | /* |
762 | * Only need to call io_uring_enter if we're not using SQ thread | |
763 | * poll, or if IORING_SQ_NEED_WAKEUP is set. | |
764 | */ | |
fc2dc21b JA |
765 | if (sq_thread_poll) |
766 | ring_flags = atomic_load_acquire(ring->flags); | |
767 | if (!sq_thread_poll || ring_flags & IORING_SQ_NEED_WAKEUP) { | |
e0abe388 JA |
768 | unsigned flags = 0; |
769 | ||
770 | if (to_wait) | |
771 | flags = IORING_ENTER_GETEVENTS; | |
fc2dc21b | 772 | if (ring_flags & IORING_SQ_NEED_WAKEUP) |
b532dd6d | 773 | flags |= IORING_ENTER_SQ_WAKEUP; |
e0abe388 | 774 | ret = io_uring_enter(s, to_submit, to_wait, flags); |
ce1705de | 775 | s->calls++; |
fc2dc21b JA |
776 | } else { |
777 | /* for SQPOLL, we submitted it all effectively */ | |
778 | ret = to_submit; | |
ce1705de | 779 | } |
c9fb4c5b | 780 | |
ce1705de JA |
781 | /* |
782 | * For non SQ thread poll, we already got the events we needed | |
783 | * through the io_uring_enter() above. For SQ thread poll, we | |
784 | * need to loop here until we find enough events. | |
785 | */ | |
786 | this_reap = 0; | |
787 | do { | |
788 | int r; | |
256714ea JA |
789 | |
790 | r = reap_events_uring(s); | |
7d1435e6 JA |
791 | if (r == -1) { |
792 | s->finish = 1; | |
ce1705de | 793 | break; |
7d1435e6 | 794 | } else if (r > 0) |
ce1705de JA |
795 | this_reap += r; |
796 | } while (sq_thread_poll && this_reap < to_wait); | |
c9fb4c5b JA |
797 | s->reaps += this_reap; |
798 | ||
799 | if (ret >= 0) { | |
800 | if (!ret) { | |
801 | to_submit = 0; | |
802 | if (s->inflight) | |
803 | goto submit; | |
804 | continue; | |
805 | } else if (ret < to_submit) { | |
806 | int diff = to_submit - ret; | |
807 | ||
808 | s->done += ret; | |
809 | prepped -= diff; | |
810 | goto submit_more; | |
811 | } | |
812 | s->done += ret; | |
813 | prepped = 0; | |
814 | continue; | |
815 | } else if (ret < 0) { | |
ac122fea | 816 | if (errno == EAGAIN) { |
c9fb4c5b JA |
817 | if (s->finish) |
818 | break; | |
819 | if (this_reap) | |
820 | goto submit; | |
c9fb4c5b JA |
821 | to_submit = 0; |
822 | goto submit; | |
823 | } | |
ac122fea | 824 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
825 | break; |
826 | } | |
827 | } while (!s->finish); | |
a7086591 | 828 | |
c9fb4c5b JA |
829 | finish = 1; |
830 | return NULL; | |
831 | } | |
832 | ||
54319661 JA |
833 | static struct submitter *get_submitter(int offset) |
834 | { | |
835 | void *ret; | |
836 | ||
837 | ret = submitter; | |
838 | if (offset) | |
839 | ret += offset * (sizeof(*submitter) + depth * sizeof(struct iovec)); | |
840 | return ret; | |
841 | } | |
842 | ||
c9fb4c5b JA |
843 | static void sig_int(int sig) |
844 | { | |
54319661 JA |
845 | int j; |
846 | ||
c9fb4c5b | 847 | printf("Exiting on signal %d\n", sig); |
54319661 JA |
848 | for (j = 0; j < nthreads; j++) { |
849 | struct submitter *s = get_submitter(j); | |
850 | s->finish = 1; | |
851 | } | |
c9fb4c5b JA |
852 | finish = 1; |
853 | } | |
854 | ||
855 | static void arm_sig_int(void) | |
856 | { | |
857 | struct sigaction act; | |
858 | ||
859 | memset(&act, 0, sizeof(act)); | |
860 | act.sa_handler = sig_int; | |
861 | act.sa_flags = SA_RESTART; | |
862 | sigaction(SIGINT, &act, NULL); | |
2cf71009 BP |
863 | |
864 | /* Windows uses SIGBREAK as a quit signal from other applications */ | |
865 | #ifdef WIN32 | |
866 | sigaction(SIGBREAK, &act, NULL); | |
867 | #endif | |
c9fb4c5b JA |
868 | } |
869 | ||
256714ea JA |
870 | static int setup_aio(struct submitter *s) |
871 | { | |
872 | #ifdef CONFIG_LIBAIO | |
873 | if (polled) { | |
874 | fprintf(stderr, "aio does not support polled IO\n"); | |
875 | polled = 0; | |
876 | } | |
877 | if (sq_thread_poll) { | |
878 | fprintf(stderr, "aio does not support SQPOLL IO\n"); | |
879 | sq_thread_poll = 0; | |
880 | } | |
881 | if (do_nop) { | |
882 | fprintf(stderr, "aio does not support polled IO\n"); | |
883 | do_nop = 0; | |
884 | } | |
885 | if (fixedbufs || register_files) { | |
886 | fprintf(stderr, "aio does not support registered files or buffers\n"); | |
887 | fixedbufs = register_files = 0; | |
888 | } | |
889 | ||
890 | return io_queue_init(depth, &s->aio_ctx); | |
891 | #else | |
892 | fprintf(stderr, "Legacy AIO not available on this system/build\n"); | |
893 | errno = EINVAL; | |
894 | return -1; | |
895 | #endif | |
896 | } | |
897 | ||
c3e2fc25 JA |
898 | static int setup_ring(struct submitter *s) |
899 | { | |
e31b8288 JA |
900 | struct io_sq_ring *sring = &s->sq_ring; |
901 | struct io_cq_ring *cring = &s->cq_ring; | |
902 | struct io_uring_params p; | |
2ea53ca3 | 903 | int ret, fd; |
c3e2fc25 | 904 | void *ptr; |
c3e2fc25 JA |
905 | |
906 | memset(&p, 0, sizeof(p)); | |
907 | ||
7d1435e6 | 908 | if (polled && !do_nop) |
0e47f11b | 909 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 | 910 | if (sq_thread_poll) { |
2ea53ca3 | 911 | p.flags |= IORING_SETUP_SQPOLL; |
3ade18a3 | 912 | if (sq_thread_cpu != -1) { |
3d7d00a3 | 913 | p.flags |= IORING_SETUP_SQ_AFF; |
3ade18a3 JA |
914 | p.sq_thread_cpu = sq_thread_cpu; |
915 | } | |
c3e2fc25 JA |
916 | } |
917 | ||
e39863e3 | 918 | fd = io_uring_setup(depth, &p); |
c3e2fc25 JA |
919 | if (fd < 0) { |
920 | perror("io_uring_setup"); | |
921 | return 1; | |
922 | } | |
f310970e | 923 | s->ring_fd = fd; |
2ea53ca3 | 924 | |
b3915995 JA |
925 | io_uring_probe(fd); |
926 | ||
2ea53ca3 | 927 | if (fixedbufs) { |
15130e3f JA |
928 | struct rlimit rlim; |
929 | ||
930 | rlim.rlim_cur = RLIM_INFINITY; | |
931 | rlim.rlim_max = RLIM_INFINITY; | |
932 | /* ignore potential error, not needed on newer kernels */ | |
933 | setrlimit(RLIMIT_MEMLOCK, &rlim); | |
934 | ||
2ea53ca3 JA |
935 | ret = io_uring_register_buffers(s); |
936 | if (ret < 0) { | |
a7abc9fb | 937 | perror("io_uring_register_buffers"); |
2ea53ca3 JA |
938 | return 1; |
939 | } | |
940 | } | |
941 | ||
8c5fa755 JA |
942 | if (register_files) { |
943 | ret = io_uring_register_files(s); | |
944 | if (ret < 0) { | |
945 | perror("io_uring_register_files"); | |
946 | return 1; | |
947 | } | |
a7abc9fb JA |
948 | } |
949 | ||
e2239016 | 950 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
951 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
952 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
953 | printf("sq_ring ptr = 0x%p\n", ptr); |
954 | sring->head = ptr + p.sq_off.head; | |
955 | sring->tail = ptr + p.sq_off.tail; | |
956 | sring->ring_mask = ptr + p.sq_off.ring_mask; | |
957 | sring->ring_entries = ptr + p.sq_off.ring_entries; | |
ce1705de | 958 | sring->flags = ptr + p.sq_off.flags; |
ac122fea | 959 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
960 | sq_ring_mask = *sring->ring_mask; |
961 | ||
f0403f94 | 962 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 963 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 JA |
964 | IORING_OFF_SQES); |
965 | printf("sqes ptr = 0x%p\n", s->sqes); | |
c3e2fc25 | 966 | |
f0403f94 | 967 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
968 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
969 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
970 | printf("cq_ring ptr = 0x%p\n", ptr); |
971 | cring->head = ptr + p.cq_off.head; | |
972 | cring->tail = ptr + p.cq_off.tail; | |
973 | cring->ring_mask = ptr + p.cq_off.ring_mask; | |
974 | cring->ring_entries = ptr + p.cq_off.ring_entries; | |
f0403f94 | 975 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
976 | cq_ring_mask = *cring->ring_mask; |
977 | return 0; | |
978 | } | |
979 | ||
2e7888ef JA |
980 | static void file_depths(char *buf) |
981 | { | |
26976a13 | 982 | bool prev = false; |
2e7888ef | 983 | char *p; |
54319661 | 984 | int i, j; |
2e7888ef | 985 | |
ac21bfab | 986 | buf[0] = '\0'; |
2e7888ef | 987 | p = buf; |
54319661 JA |
988 | for (j = 0; j < nthreads; j++) { |
989 | struct submitter *s = get_submitter(j); | |
2e7888ef | 990 | |
54319661 JA |
991 | for (i = 0; i < s->nr_files; i++) { |
992 | struct file *f = &s->files[i]; | |
993 | ||
26976a13 JA |
994 | if (prev) |
995 | p += sprintf(p, " %d", f->pending_ios); | |
54319661 | 996 | else |
4f215227 | 997 | p += sprintf(p, "%d", f->pending_ios); |
26976a13 | 998 | prev = true; |
54319661 | 999 | } |
2e7888ef JA |
1000 | } |
1001 | } | |
1002 | ||
d79ff8c9 | 1003 | static void usage(char *argv, int status) |
e39863e3 KB |
1004 | { |
1005 | printf("%s [options] -- [filenames]\n" | |
35268e11 AJ |
1006 | " -d <int> : IO Depth, default %d\n" |
1007 | " -s <int> : Batch submit, default %d\n" | |
1008 | " -c <int> : Batch complete, default %d\n" | |
1009 | " -b <int> : Block size, default %d\n" | |
1010 | " -p <bool> : Polled IO, default %d\n" | |
1011 | " -B <bool> : Fixed buffers, default %d\n" | |
1012 | " -F <bool> : Register files, default %d\n" | |
0862f718 | 1013 | " -n <int> : Number of threads, default %d\n" |
2686fc22 | 1014 | " -O <bool> : Use O_DIRECT, default %d\n" |
4a39c524 EV |
1015 | " -N <bool> : Perform just no-op requests, default %d\n" |
1016 | " -t <bool> : Track IO latencies, default %d\n" | |
256714ea JA |
1017 | " -T <int> : TSC rate in HZ\n" |
1018 | " -a <bool> : Use legacy aio, default %d\n", | |
35268e11 | 1019 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled, |
256714ea | 1020 | fixedbufs, register_files, nthreads, !buffered, do_nop, stats, aio); |
d79ff8c9 | 1021 | exit(status); |
e39863e3 KB |
1022 | } |
1023 | ||
203e4c26 JA |
1024 | static void read_tsc_rate(void) |
1025 | { | |
1026 | char buffer[32]; | |
1027 | int fd, ret; | |
1028 | ||
1029 | if (tsc_rate) | |
1030 | return; | |
1031 | ||
1032 | fd = open(TSC_RATE_FILE, O_RDONLY); | |
1033 | if (fd < 0) | |
1034 | return; | |
1035 | ||
1036 | ret = read(fd, buffer, sizeof(buffer)); | |
1037 | if (ret < 0) { | |
1038 | close(fd); | |
1039 | return; | |
1040 | } | |
1041 | ||
1042 | tsc_rate = strtoul(buffer, NULL, 10); | |
1043 | printf("Using TSC rate %luHz\n", tsc_rate); | |
1044 | close(fd); | |
1045 | } | |
1046 | ||
1047 | static void write_tsc_rate(void) | |
1048 | { | |
1049 | char buffer[32]; | |
1050 | struct stat sb; | |
1051 | int fd, ret; | |
1052 | ||
1053 | if (!stat(TSC_RATE_FILE, &sb)) | |
1054 | return; | |
1055 | ||
1056 | fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644); | |
1057 | if (fd < 0) | |
1058 | return; | |
1059 | ||
1060 | memset(buffer, 0, sizeof(buffer)); | |
1061 | sprintf(buffer, "%lu", tsc_rate); | |
1062 | ret = write(fd, buffer, strlen(buffer)); | |
1063 | if (ret < 0) | |
1064 | perror("write"); | |
1065 | close(fd); | |
1066 | } | |
1067 | ||
c9fb4c5b JA |
1068 | int main(int argc, char *argv[]) |
1069 | { | |
e39863e3 | 1070 | struct submitter *s; |
05138221 | 1071 | unsigned long done, calls, reap; |
d79ff8c9 AJ |
1072 | int err, i, j, flags, fd, opt, threads_per_f, threads_rem = 0, nfiles; |
1073 | struct file f; | |
2e7888ef | 1074 | char *fdepths; |
c3e2fc25 | 1075 | void *ret; |
c9fb4c5b | 1076 | |
0862f718 JA |
1077 | if (!do_nop && argc < 2) |
1078 | usage(argv[0], 1); | |
c9fb4c5b | 1079 | |
256714ea | 1080 | while ((opt = getopt(argc, argv, "d:s:c:b:p:B:F:n:N:O:t:T:a:h?")) != -1) { |
e39863e3 | 1081 | switch (opt) { |
256714ea JA |
1082 | case 'a': |
1083 | aio = !!atoi(optarg); | |
1084 | break; | |
e39863e3 KB |
1085 | case 'd': |
1086 | depth = atoi(optarg); | |
1087 | break; | |
1088 | case 's': | |
1089 | batch_submit = atoi(optarg); | |
932131c9 JA |
1090 | if (!batch_submit) |
1091 | batch_submit = 1; | |
e39863e3 KB |
1092 | break; |
1093 | case 'c': | |
1094 | batch_complete = atoi(optarg); | |
932131c9 JA |
1095 | if (!batch_complete) |
1096 | batch_complete = 1; | |
e39863e3 | 1097 | break; |
5bd526f2 JA |
1098 | case 'b': |
1099 | bs = atoi(optarg); | |
1100 | break; | |
1101 | case 'p': | |
1102 | polled = !!atoi(optarg); | |
1103 | break; | |
6a87c3b0 JA |
1104 | case 'B': |
1105 | fixedbufs = !!atoi(optarg); | |
1106 | break; | |
1107 | case 'F': | |
1108 | register_files = !!atoi(optarg); | |
1109 | break; | |
54319661 JA |
1110 | case 'n': |
1111 | nthreads = atoi(optarg); | |
caf2b9ac JA |
1112 | if (!nthreads) { |
1113 | printf("Threads must be non-zero\n"); | |
1114 | usage(argv[0], 1); | |
1115 | } | |
54319661 | 1116 | break; |
0862f718 JA |
1117 | case 'N': |
1118 | do_nop = !!atoi(optarg); | |
1119 | break; | |
2686fc22 JA |
1120 | case 'O': |
1121 | buffered = !atoi(optarg); | |
1122 | break; | |
932131c9 JA |
1123 | case 't': |
1124 | #ifndef ARCH_HAVE_CPU_CLOCK | |
1125 | fprintf(stderr, "Stats not supported on this CPU\n"); | |
1126 | return 1; | |
1127 | #endif | |
1128 | stats = !!atoi(optarg); | |
1129 | break; | |
1130 | case 'T': | |
1131 | #ifndef ARCH_HAVE_CPU_CLOCK | |
1132 | fprintf(stderr, "Stats not supported on this CPU\n"); | |
1133 | return 1; | |
1134 | #endif | |
1135 | tsc_rate = strtoul(optarg, NULL, 10); | |
203e4c26 | 1136 | write_tsc_rate(); |
932131c9 | 1137 | break; |
e39863e3 KB |
1138 | case 'h': |
1139 | case '?': | |
1140 | default: | |
d79ff8c9 | 1141 | usage(argv[0], 0); |
e39863e3 KB |
1142 | break; |
1143 | } | |
1144 | } | |
1145 | ||
203e4c26 JA |
1146 | if (stats) |
1147 | read_tsc_rate(); | |
1148 | ||
c5347611 JA |
1149 | if (batch_complete > depth) |
1150 | batch_complete = depth; | |
1151 | if (batch_submit > depth) | |
1152 | batch_submit = depth; | |
1153 | ||
54319661 JA |
1154 | submitter = calloc(nthreads, sizeof(*submitter) + |
1155 | depth * sizeof(struct iovec)); | |
1156 | for (j = 0; j < nthreads; j++) { | |
1157 | s = get_submitter(j); | |
1158 | s->index = j; | |
1159 | s->done = s->calls = s->reaps = 0; | |
1160 | } | |
e39863e3 | 1161 | |
701d1277 | 1162 | flags = O_RDONLY | O_NOATIME; |
a7086591 JA |
1163 | if (!buffered) |
1164 | flags |= O_DIRECT; | |
1165 | ||
54319661 | 1166 | j = 0; |
e39863e3 | 1167 | i = optind; |
d79ff8c9 | 1168 | nfiles = argc - i; |
2ac00585 JA |
1169 | if (!do_nop) { |
1170 | if (!nfiles) { | |
1171 | printf("No files specified\n"); | |
1172 | usage(argv[0], 1); | |
1173 | } | |
1174 | threads_per_f = nthreads / nfiles; | |
1175 | /* make sure each thread gets assigned files */ | |
1176 | if (threads_per_f == 0) { | |
1177 | threads_per_f = 1; | |
1178 | } else { | |
1179 | threads_rem = nthreads - threads_per_f * nfiles; | |
1180 | } | |
d79ff8c9 | 1181 | } |
8025517d | 1182 | while (!do_nop && i < argc) { |
d79ff8c9 AJ |
1183 | int k, limit; |
1184 | ||
1185 | memset(&f, 0, sizeof(f)); | |
a7086591 JA |
1186 | |
1187 | fd = open(argv[i], flags); | |
1188 | if (fd < 0) { | |
1189 | perror("open"); | |
1190 | return 1; | |
1191 | } | |
d79ff8c9 AJ |
1192 | f.real_fd = fd; |
1193 | if (get_file_size(&f)) { | |
a7086591 JA |
1194 | printf("failed getting size of device/file\n"); |
1195 | return 1; | |
1196 | } | |
d79ff8c9 | 1197 | if (f.max_blocks <= 1) { |
a7086591 JA |
1198 | printf("Zero file/device size?\n"); |
1199 | return 1; | |
1200 | } | |
d79ff8c9 AJ |
1201 | f.max_blocks--; |
1202 | ||
1203 | limit = threads_per_f; | |
1204 | limit += threads_rem > 0 ? 1 : 0; | |
1205 | for (k = 0; k < limit; k++) { | |
1206 | s = get_submitter((j + k) % nthreads); | |
a7086591 | 1207 | |
d79ff8c9 AJ |
1208 | if (s->nr_files == MAX_FDS) { |
1209 | printf("Max number of files (%d) reached\n", MAX_FDS); | |
1210 | break; | |
1211 | } | |
1212 | ||
1213 | memcpy(&s->files[s->nr_files], &f, sizeof(f)); | |
1214 | ||
1215 | printf("Added file %s (submitter %d)\n", argv[i], s->index); | |
1216 | s->nr_files++; | |
1217 | } | |
1218 | threads_rem--; | |
a7086591 | 1219 | i++; |
d79ff8c9 | 1220 | j += limit; |
a7086591 JA |
1221 | } |
1222 | ||
c9fb4c5b JA |
1223 | arm_sig_int(); |
1224 | ||
54319661 JA |
1225 | for (j = 0; j < nthreads; j++) { |
1226 | s = get_submitter(j); | |
1227 | for (i = 0; i < depth; i++) { | |
1228 | void *buf; | |
c9fb4c5b | 1229 | |
54319661 JA |
1230 | if (posix_memalign(&buf, bs, bs)) { |
1231 | printf("failed alloc\n"); | |
1232 | return 1; | |
1233 | } | |
1234 | s->iovecs[i].iov_base = buf; | |
1235 | s->iovecs[i].iov_len = bs; | |
c9fb4c5b | 1236 | } |
c9fb4c5b JA |
1237 | } |
1238 | ||
54319661 JA |
1239 | for (j = 0; j < nthreads; j++) { |
1240 | s = get_submitter(j); | |
1241 | ||
256714ea JA |
1242 | if (!aio) |
1243 | err = setup_ring(s); | |
1244 | else | |
1245 | err = setup_aio(s); | |
54319661 JA |
1246 | if (err) { |
1247 | printf("ring setup failed: %s, %d\n", strerror(errno), err); | |
1248 | return 1; | |
1249 | } | |
c9fb4c5b | 1250 | } |
54319661 | 1251 | s = get_submitter(0); |
256714ea JA |
1252 | printf("polled=%d, fixedbufs=%d, register_files=%d, buffered=%d, QD=%d\n", polled, fixedbufs, register_files, buffered, depth); |
1253 | if (!aio) | |
1254 | printf("Engine=io_uring, sq_ring=%d, cq_ring=%d\n", *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); | |
1255 | #ifdef CONFIG_LIBAIO | |
1256 | else | |
1257 | printf("Engine=aio, ctx=%p\n", &s->aio_ctx); | |
1258 | #endif | |
c9fb4c5b | 1259 | |
54319661 JA |
1260 | for (j = 0; j < nthreads; j++) { |
1261 | s = get_submitter(j); | |
256714ea JA |
1262 | if (!aio) |
1263 | pthread_create(&s->thread, NULL, submitter_uring_fn, s); | |
1264 | #ifdef CONFIG_LIBAIO | |
1265 | else | |
1266 | pthread_create(&s->thread, NULL, submitter_aio_fn, s); | |
1267 | #endif | |
54319661 | 1268 | } |
c9fb4c5b | 1269 | |
54319661 | 1270 | fdepths = malloc(8 * s->nr_files * nthreads); |
05138221 | 1271 | reap = calls = done = 0; |
c9fb4c5b JA |
1272 | do { |
1273 | unsigned long this_done = 0; | |
1274 | unsigned long this_reap = 0; | |
1275 | unsigned long this_call = 0; | |
1276 | unsigned long rpc = 0, ipc = 0; | |
f3057d26 | 1277 | unsigned long iops, bw; |
c9fb4c5b JA |
1278 | |
1279 | sleep(1); | |
54319661 JA |
1280 | for (j = 0; j < nthreads; j++) { |
1281 | this_done += s->done; | |
1282 | this_call += s->calls; | |
1283 | this_reap += s->reaps; | |
1284 | } | |
c9fb4c5b JA |
1285 | if (this_call - calls) { |
1286 | rpc = (this_done - done) / (this_call - calls); | |
1287 | ipc = (this_reap - reap) / (this_call - calls); | |
191561c1 JA |
1288 | } else |
1289 | rpc = ipc = -1; | |
2e7888ef | 1290 | file_depths(fdepths); |
22fd3501 | 1291 | iops = this_done - done; |
f3057d26 JA |
1292 | if (bs > 1048576) |
1293 | bw = iops * (bs / 1048576); | |
1294 | else | |
1295 | bw = iops / (1048576 / bs); | |
6c5d3a1c JA |
1296 | printf("IOPS=%lu, ", iops); |
1297 | if (!do_nop) | |
1298 | printf("BW=%luMiB/s, ", bw); | |
1299 | printf("IOS/call=%ld/%ld, inflight=(%s)\n", rpc, ipc, fdepths); | |
c9fb4c5b JA |
1300 | done = this_done; |
1301 | calls = this_call; | |
1302 | reap = this_reap; | |
1303 | } while (!finish); | |
1304 | ||
54319661 JA |
1305 | for (j = 0; j < nthreads; j++) { |
1306 | s = get_submitter(j); | |
1307 | pthread_join(s->thread, &ret); | |
1308 | close(s->ring_fd); | |
932131c9 JA |
1309 | |
1310 | if (stats) { | |
1311 | unsigned long nr; | |
1312 | ||
1313 | printf("%d: Latency percentiles:\n", s->tid); | |
1314 | for (i = 0, nr = 0; i < PLAT_NR; i++) | |
1315 | nr += s->plat[i]; | |
1316 | show_clat_percentiles(s->plat, nr, 4); | |
1317 | free(s->clock_batch); | |
1318 | free(s->plat); | |
1319 | } | |
54319661 | 1320 | } |
932131c9 | 1321 | |
2e7888ef | 1322 | free(fdepths); |
54319661 | 1323 | free(submitter); |
c9fb4c5b JA |
1324 | return 0; |
1325 | } |