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++; | |
679d8352 | 236 | read_barrier(); |
c3e2fc25 | 237 | if (next_tail == *ring->head) |
c9fb4c5b JA |
238 | break; |
239 | ||
e39c34dc | 240 | index = tail & sq_ring_mask; |
a7086591 | 241 | init_io(s, index); |
c3e2fc25 | 242 | ring->array[index] = index; |
c9fb4c5b JA |
243 | prepped++; |
244 | tail = next_tail; | |
245 | } while (prepped < max_ios); | |
246 | ||
c3e2fc25 | 247 | if (*ring->tail != tail) { |
c3e2fc25 | 248 | *ring->tail = tail; |
679d8352 | 249 | write_barrier(); |
c9fb4c5b JA |
250 | } |
251 | return prepped; | |
252 | } | |
253 | ||
a7086591 | 254 | static int get_file_size(struct file *f) |
c9fb4c5b JA |
255 | { |
256 | struct stat st; | |
257 | ||
48e698fa | 258 | if (fstat(f->real_fd, &st) < 0) |
c9fb4c5b JA |
259 | return -1; |
260 | if (S_ISBLK(st.st_mode)) { | |
261 | unsigned long long bytes; | |
262 | ||
48e698fa | 263 | if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0) |
c9fb4c5b JA |
264 | return -1; |
265 | ||
5bd526f2 | 266 | f->max_blocks = bytes / bs; |
c9fb4c5b JA |
267 | return 0; |
268 | } else if (S_ISREG(st.st_mode)) { | |
5bd526f2 | 269 | f->max_blocks = st.st_size / bs; |
c9fb4c5b JA |
270 | return 0; |
271 | } | |
272 | ||
273 | return -1; | |
274 | } | |
275 | ||
276 | static int reap_events(struct submitter *s) | |
277 | { | |
e31b8288 | 278 | struct io_cq_ring *ring = &s->cq_ring; |
f0403f94 | 279 | struct io_uring_cqe *cqe; |
e2239016 | 280 | unsigned head, reaped = 0; |
c9fb4c5b | 281 | |
c3e2fc25 | 282 | head = *ring->head; |
c9fb4c5b | 283 | do { |
701d1277 JA |
284 | struct file *f; |
285 | ||
679d8352 | 286 | read_barrier(); |
c3e2fc25 | 287 | if (head == *ring->tail) |
c9fb4c5b | 288 | break; |
f0403f94 | 289 | cqe = &ring->cqes[head & cq_ring_mask]; |
8025517d | 290 | if (!do_nop) { |
e3466352 | 291 | f = (struct file *) (uintptr_t) cqe->user_data; |
8025517d | 292 | f->pending_ios--; |
5bd526f2 | 293 | if (cqe->res != bs) { |
8025517d | 294 | printf("io: unexpected ret=%d\n", cqe->res); |
154a9582 | 295 | if (polled && cqe->res == -EOPNOTSUPP) |
8066f6b6 | 296 | printf("Your filesystem/driver/kernel doesn't support polled IO\n"); |
8025517d JA |
297 | return -1; |
298 | } | |
c9fb4c5b JA |
299 | } |
300 | reaped++; | |
301 | head++; | |
c9fb4c5b JA |
302 | } while (1); |
303 | ||
304 | s->inflight -= reaped; | |
c3e2fc25 | 305 | *ring->head = head; |
679d8352 | 306 | write_barrier(); |
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; |
c9fb4c5b | 323 | |
e39863e3 KB |
324 | if (!prepped && s->inflight < depth) { |
325 | to_prep = min(depth - s->inflight, batch_submit); | |
a7086591 | 326 | prepped = prep_more_ios(s, to_prep); |
f310970e | 327 | } |
c9fb4c5b JA |
328 | s->inflight += prepped; |
329 | submit_more: | |
330 | to_submit = prepped; | |
331 | submit: | |
e39863e3 | 332 | if (to_submit && (s->inflight + to_submit <= depth)) |
c9fb4c5b JA |
333 | to_wait = 0; |
334 | else | |
e39863e3 | 335 | to_wait = min(s->inflight + to_submit, batch_complete); |
c9fb4c5b | 336 | |
ce1705de JA |
337 | /* |
338 | * Only need to call io_uring_enter if we're not using SQ thread | |
339 | * poll, or if IORING_SQ_NEED_WAKEUP is set. | |
340 | */ | |
341 | if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) { | |
e0abe388 JA |
342 | unsigned flags = 0; |
343 | ||
344 | if (to_wait) | |
345 | flags = IORING_ENTER_GETEVENTS; | |
e2f309e6 | 346 | if ((*ring->flags & IORING_SQ_NEED_WAKEUP)) |
b532dd6d | 347 | flags |= IORING_ENTER_SQ_WAKEUP; |
e0abe388 | 348 | ret = io_uring_enter(s, to_submit, to_wait, flags); |
ce1705de JA |
349 | s->calls++; |
350 | } | |
c9fb4c5b | 351 | |
ce1705de JA |
352 | /* |
353 | * For non SQ thread poll, we already got the events we needed | |
354 | * through the io_uring_enter() above. For SQ thread poll, we | |
355 | * need to loop here until we find enough events. | |
356 | */ | |
357 | this_reap = 0; | |
358 | do { | |
359 | int r; | |
360 | r = reap_events(s); | |
7d1435e6 JA |
361 | if (r == -1) { |
362 | s->finish = 1; | |
ce1705de | 363 | break; |
7d1435e6 | 364 | } else if (r > 0) |
ce1705de JA |
365 | this_reap += r; |
366 | } while (sq_thread_poll && this_reap < to_wait); | |
c9fb4c5b JA |
367 | s->reaps += this_reap; |
368 | ||
369 | if (ret >= 0) { | |
370 | if (!ret) { | |
371 | to_submit = 0; | |
372 | if (s->inflight) | |
373 | goto submit; | |
374 | continue; | |
375 | } else if (ret < to_submit) { | |
376 | int diff = to_submit - ret; | |
377 | ||
378 | s->done += ret; | |
379 | prepped -= diff; | |
380 | goto submit_more; | |
381 | } | |
382 | s->done += ret; | |
383 | prepped = 0; | |
384 | continue; | |
385 | } else if (ret < 0) { | |
ac122fea | 386 | if (errno == EAGAIN) { |
c9fb4c5b JA |
387 | if (s->finish) |
388 | break; | |
389 | if (this_reap) | |
390 | goto submit; | |
c9fb4c5b JA |
391 | to_submit = 0; |
392 | goto submit; | |
393 | } | |
ac122fea | 394 | printf("io_submit: %s\n", strerror(errno)); |
c9fb4c5b JA |
395 | break; |
396 | } | |
397 | } while (!s->finish); | |
a7086591 | 398 | |
c9fb4c5b JA |
399 | finish = 1; |
400 | return NULL; | |
401 | } | |
402 | ||
403 | static void sig_int(int sig) | |
404 | { | |
405 | printf("Exiting on signal %d\n", sig); | |
e39863e3 | 406 | submitter->finish = 1; |
c9fb4c5b JA |
407 | finish = 1; |
408 | } | |
409 | ||
410 | static void arm_sig_int(void) | |
411 | { | |
412 | struct sigaction act; | |
413 | ||
414 | memset(&act, 0, sizeof(act)); | |
415 | act.sa_handler = sig_int; | |
416 | act.sa_flags = SA_RESTART; | |
417 | sigaction(SIGINT, &act, NULL); | |
418 | } | |
419 | ||
c3e2fc25 JA |
420 | static int setup_ring(struct submitter *s) |
421 | { | |
e31b8288 JA |
422 | struct io_sq_ring *sring = &s->sq_ring; |
423 | struct io_cq_ring *cring = &s->cq_ring; | |
424 | struct io_uring_params p; | |
2ea53ca3 | 425 | int ret, fd; |
c3e2fc25 | 426 | void *ptr; |
c3e2fc25 JA |
427 | |
428 | memset(&p, 0, sizeof(p)); | |
429 | ||
7d1435e6 | 430 | if (polled && !do_nop) |
0e47f11b | 431 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 | 432 | if (sq_thread_poll) { |
2ea53ca3 | 433 | p.flags |= IORING_SETUP_SQPOLL; |
3ade18a3 | 434 | if (sq_thread_cpu != -1) { |
3d7d00a3 | 435 | p.flags |= IORING_SETUP_SQ_AFF; |
3ade18a3 JA |
436 | p.sq_thread_cpu = sq_thread_cpu; |
437 | } | |
c3e2fc25 JA |
438 | } |
439 | ||
e39863e3 | 440 | fd = io_uring_setup(depth, &p); |
c3e2fc25 JA |
441 | if (fd < 0) { |
442 | perror("io_uring_setup"); | |
443 | return 1; | |
444 | } | |
f310970e | 445 | s->ring_fd = fd; |
2ea53ca3 | 446 | |
b3915995 JA |
447 | io_uring_probe(fd); |
448 | ||
2ea53ca3 JA |
449 | if (fixedbufs) { |
450 | ret = io_uring_register_buffers(s); | |
451 | if (ret < 0) { | |
a7abc9fb | 452 | perror("io_uring_register_buffers"); |
2ea53ca3 JA |
453 | return 1; |
454 | } | |
455 | } | |
456 | ||
8c5fa755 JA |
457 | if (register_files) { |
458 | ret = io_uring_register_files(s); | |
459 | if (ret < 0) { | |
460 | perror("io_uring_register_files"); | |
461 | return 1; | |
462 | } | |
a7abc9fb JA |
463 | } |
464 | ||
e2239016 | 465 | ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32), |
f310970e JA |
466 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
467 | IORING_OFF_SQ_RING); | |
c3e2fc25 JA |
468 | printf("sq_ring ptr = 0x%p\n", ptr); |
469 | sring->head = ptr + p.sq_off.head; | |
470 | sring->tail = ptr + p.sq_off.tail; | |
471 | sring->ring_mask = ptr + p.sq_off.ring_mask; | |
472 | sring->ring_entries = ptr + p.sq_off.ring_entries; | |
ce1705de | 473 | sring->flags = ptr + p.sq_off.flags; |
ac122fea | 474 | sring->array = ptr + p.sq_off.array; |
c3e2fc25 JA |
475 | sq_ring_mask = *sring->ring_mask; |
476 | ||
f0403f94 | 477 | s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), |
f310970e | 478 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
f0403f94 JA |
479 | IORING_OFF_SQES); |
480 | printf("sqes ptr = 0x%p\n", s->sqes); | |
c3e2fc25 | 481 | |
f0403f94 | 482 | ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe), |
f310970e JA |
483 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, |
484 | IORING_OFF_CQ_RING); | |
c3e2fc25 JA |
485 | printf("cq_ring ptr = 0x%p\n", ptr); |
486 | cring->head = ptr + p.cq_off.head; | |
487 | cring->tail = ptr + p.cq_off.tail; | |
488 | cring->ring_mask = ptr + p.cq_off.ring_mask; | |
489 | cring->ring_entries = ptr + p.cq_off.ring_entries; | |
f0403f94 | 490 | cring->cqes = ptr + p.cq_off.cqes; |
c3e2fc25 JA |
491 | cq_ring_mask = *cring->ring_mask; |
492 | return 0; | |
493 | } | |
494 | ||
2e7888ef JA |
495 | static void file_depths(char *buf) |
496 | { | |
e39863e3 | 497 | struct submitter *s = submitter; |
2e7888ef JA |
498 | char *p; |
499 | int i; | |
500 | ||
ac21bfab | 501 | buf[0] = '\0'; |
2e7888ef JA |
502 | p = buf; |
503 | for (i = 0; i < s->nr_files; i++) { | |
504 | struct file *f = &s->files[i]; | |
505 | ||
506 | if (i + 1 == s->nr_files) | |
507 | p += sprintf(p, "%d", f->pending_ios); | |
508 | else | |
509 | p += sprintf(p, "%d, ", f->pending_ios); | |
510 | } | |
511 | } | |
512 | ||
e39863e3 KB |
513 | static void usage(char *argv) |
514 | { | |
515 | printf("%s [options] -- [filenames]\n" | |
516 | " -d <int> : IO Depth, default %d\n" | |
517 | " -s <int> : Batch submit, default %d\n" | |
5bd526f2 JA |
518 | " -c <int> : Batch complete, default %d\n" |
519 | " -b <int> : Block size, default %d\n" | |
520 | " -p <bool> : Polled IO, default %d\n", | |
521 | argv, DEPTH, BATCH_SUBMIT, BATCH_COMPLETE, BS, polled); | |
e39863e3 KB |
522 | exit(0); |
523 | } | |
524 | ||
c9fb4c5b JA |
525 | int main(int argc, char *argv[]) |
526 | { | |
e39863e3 | 527 | struct submitter *s; |
05138221 | 528 | unsigned long done, calls, reap; |
e39863e3 | 529 | int err, i, flags, fd, opt; |
2e7888ef | 530 | char *fdepths; |
c3e2fc25 | 531 | void *ret; |
c9fb4c5b | 532 | |
8025517d | 533 | if (!do_nop && argc < 2) { |
e39863e3 | 534 | printf("%s: filename [options]\n", argv[0]); |
c9fb4c5b JA |
535 | return 1; |
536 | } | |
537 | ||
5bd526f2 | 538 | while ((opt = getopt(argc, argv, "d:s:c:b:p:h?")) != -1) { |
e39863e3 KB |
539 | switch (opt) { |
540 | case 'd': | |
541 | depth = atoi(optarg); | |
542 | break; | |
543 | case 's': | |
544 | batch_submit = atoi(optarg); | |
545 | break; | |
546 | case 'c': | |
547 | batch_complete = atoi(optarg); | |
548 | break; | |
5bd526f2 JA |
549 | case 'b': |
550 | bs = atoi(optarg); | |
551 | break; | |
552 | case 'p': | |
553 | polled = !!atoi(optarg); | |
554 | break; | |
e39863e3 KB |
555 | case 'h': |
556 | case '?': | |
557 | default: | |
558 | usage(argv[0]); | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
d9c50de7 KB |
563 | submitter = malloc(sizeof(*submitter) + depth * sizeof(struct iovec)); |
564 | memset(submitter, 0, sizeof(*submitter) + depth * sizeof(struct iovec)); | |
e39863e3 KB |
565 | s = submitter; |
566 | ||
701d1277 | 567 | flags = O_RDONLY | O_NOATIME; |
a7086591 JA |
568 | if (!buffered) |
569 | flags |= O_DIRECT; | |
570 | ||
e39863e3 | 571 | i = optind; |
8025517d | 572 | while (!do_nop && i < argc) { |
be26a982 | 573 | struct file *f; |
a7086591 | 574 | |
be26a982 JA |
575 | if (s->nr_files == MAX_FDS) { |
576 | printf("Max number of files (%d) reached\n", MAX_FDS); | |
577 | break; | |
578 | } | |
a7086591 JA |
579 | fd = open(argv[i], flags); |
580 | if (fd < 0) { | |
581 | perror("open"); | |
582 | return 1; | |
583 | } | |
be26a982 JA |
584 | |
585 | f = &s->files[s->nr_files]; | |
48e698fa | 586 | f->real_fd = fd; |
a7086591 JA |
587 | if (get_file_size(f)) { |
588 | printf("failed getting size of device/file\n"); | |
589 | return 1; | |
590 | } | |
591 | if (f->max_blocks <= 1) { | |
592 | printf("Zero file/device size?\n"); | |
593 | return 1; | |
594 | } | |
595 | f->max_blocks--; | |
596 | ||
597 | printf("Added file %s\n", argv[i]); | |
598 | s->nr_files++; | |
599 | i++; | |
600 | } | |
601 | ||
b3ce355d JA |
602 | if (fixedbufs) { |
603 | struct rlimit rlim; | |
604 | ||
605 | rlim.rlim_cur = RLIM_INFINITY; | |
606 | rlim.rlim_max = RLIM_INFINITY; | |
607 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) { | |
608 | perror("setrlimit"); | |
609 | return 1; | |
610 | } | |
c9fb4c5b JA |
611 | } |
612 | ||
613 | arm_sig_int(); | |
614 | ||
e39863e3 | 615 | for (i = 0; i < depth; i++) { |
c3e2fc25 | 616 | void *buf; |
c9fb4c5b | 617 | |
5bd526f2 | 618 | if (posix_memalign(&buf, bs, bs)) { |
c9fb4c5b JA |
619 | printf("failed alloc\n"); |
620 | return 1; | |
621 | } | |
c3e2fc25 | 622 | s->iovecs[i].iov_base = buf; |
5bd526f2 | 623 | s->iovecs[i].iov_len = bs; |
c9fb4c5b JA |
624 | } |
625 | ||
c3e2fc25 | 626 | err = setup_ring(s); |
c9fb4c5b | 627 | if (err) { |
c3e2fc25 | 628 | printf("ring setup failed: %s, %d\n", strerror(errno), err); |
c9fb4c5b JA |
629 | return 1; |
630 | } | |
c3e2fc25 | 631 | printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered); |
e39863e3 | 632 | printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", depth, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries); |
c9fb4c5b JA |
633 | |
634 | pthread_create(&s->thread, NULL, submitter_fn, s); | |
635 | ||
2e7888ef | 636 | fdepths = malloc(8 * s->nr_files); |
05138221 | 637 | reap = calls = done = 0; |
c9fb4c5b JA |
638 | do { |
639 | unsigned long this_done = 0; | |
640 | unsigned long this_reap = 0; | |
641 | unsigned long this_call = 0; | |
642 | unsigned long rpc = 0, ipc = 0; | |
643 | ||
644 | sleep(1); | |
645 | this_done += s->done; | |
646 | this_call += s->calls; | |
647 | this_reap += s->reaps; | |
648 | if (this_call - calls) { | |
649 | rpc = (this_done - done) / (this_call - calls); | |
650 | ipc = (this_reap - reap) / (this_call - calls); | |
191561c1 JA |
651 | } else |
652 | rpc = ipc = -1; | |
2e7888ef | 653 | file_depths(fdepths); |
05138221 | 654 | printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n", |
c9fb4c5b | 655 | this_done - done, rpc, ipc, s->inflight, |
05138221 | 656 | fdepths); |
c9fb4c5b JA |
657 | done = this_done; |
658 | calls = this_call; | |
659 | reap = this_reap; | |
660 | } while (!finish); | |
661 | ||
662 | pthread_join(s->thread, &ret); | |
e31b8288 | 663 | close(s->ring_fd); |
2e7888ef | 664 | free(fdepths); |
c9fb4c5b JA |
665 | return 0; |
666 | } |