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