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