IO engine callback need not dump possible values
[fio.git] / io_u.c
... / ...
CommitLineData
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
6#include <assert.h>
7
8#include "fio.h"
9#include "os.h"
10
11/*
12 * Change this define to play with the timeout handling
13 */
14#undef FIO_USE_TIMEOUT
15
16struct io_completion_data {
17 int nr; /* input */
18 endio_handler *handler; /* input */
19
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
23};
24
25/*
26 * The ->file_map[] contains a map of blocks we have or have not done io
27 * to yet. Used to make sure we cover the entire range in a fair fashion.
28 */
29static int random_map_free(struct thread_data *td, struct fio_file *f,
30 unsigned long long block)
31{
32 unsigned int idx = RAND_MAP_IDX(td, f, block);
33 unsigned int bit = RAND_MAP_BIT(td, f, block);
34
35 return (f->file_map[idx] & (1UL << bit)) == 0;
36}
37
38/*
39 * Mark a given offset as used in the map.
40 */
41static void mark_random_map(struct thread_data *td, struct fio_file *f,
42 struct io_u *io_u)
43{
44 unsigned int min_bs = td->rw_min_bs;
45 unsigned long long block;
46 unsigned int blocks;
47 unsigned int nr_blocks;
48
49 block = io_u->offset / (unsigned long long) min_bs;
50 blocks = 0;
51 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
52
53 while (blocks < nr_blocks) {
54 unsigned int idx, bit;
55
56 if (!random_map_free(td, f, block))
57 break;
58
59 idx = RAND_MAP_IDX(td, f, block);
60 bit = RAND_MAP_BIT(td, f, block);
61
62 fio_assert(td, idx < f->num_maps);
63
64 f->file_map[idx] |= (1UL << bit);
65 block++;
66 blocks++;
67 }
68
69 if ((blocks * min_bs) < io_u->buflen)
70 io_u->buflen = blocks * min_bs;
71}
72
73/*
74 * Return the next free block in the map.
75 */
76static int get_next_free_block(struct thread_data *td, struct fio_file *f,
77 unsigned long long *b)
78{
79 int i;
80
81 i = f->last_free_lookup;
82 *b = (i * BLOCKS_PER_MAP);
83 while ((*b) * td->rw_min_bs < f->real_file_size) {
84 if (f->file_map[i] != -1UL) {
85 *b += ffz(f->file_map[i]);
86 f->last_free_lookup = i;
87 return 0;
88 }
89
90 *b += BLOCKS_PER_MAP;
91 i++;
92 }
93
94 return 1;
95}
96
97/*
98 * For random io, generate a random new block and see if it's used. Repeat
99 * until we find a free one. For sequential io, just return the end of
100 * the last io issued.
101 */
102static int get_next_offset(struct thread_data *td, struct fio_file *f,
103 struct io_u *io_u)
104{
105 const int ddir = io_u->ddir;
106 unsigned long long b, rb;
107 long r;
108
109 if (td_random(td)) {
110 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
111 int loops = 5;
112
113 do {
114 r = os_random_long(&td->random_state);
115 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
116 if (td->norandommap)
117 break;
118 rb = b + (f->file_offset / td->min_bs[ddir]);
119 loops--;
120 } while (!random_map_free(td, f, rb) && loops);
121
122 /*
123 * if we failed to retrieve a truly random offset within
124 * the loops assigned, see if there are free ones left at all
125 */
126 if (!loops && get_next_free_block(td, f, &b))
127 return 1;
128 } else
129 b = f->last_pos / td->min_bs[ddir];
130
131 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
132 if (io_u->offset >= f->real_file_size)
133 return 1;
134
135 return 0;
136}
137
138static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
139 struct io_u *io_u)
140{
141 const int ddir = io_u->ddir;
142 unsigned int buflen;
143 long r;
144
145 if (td->min_bs[ddir] == td->max_bs[ddir])
146 buflen = td->min_bs[ddir];
147 else {
148 r = os_random_long(&td->bsrange_state);
149 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
150 if (!td->bs_unaligned)
151 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
152 }
153
154 while (buflen + io_u->offset > f->real_file_size) {
155 if (buflen == td->min_bs[ddir])
156 return 0;
157
158 buflen = td->min_bs[ddir];
159 }
160
161 return buflen;
162}
163
164/*
165 * Return the data direction for the next io_u. If the job is a
166 * mixed read/write workload, check the rwmix cycle and switch if
167 * necessary.
168 */
169static enum fio_ddir get_rw_ddir(struct thread_data *td)
170{
171 if (td_rw(td)) {
172 struct timeval now;
173 unsigned long elapsed;
174
175 fio_gettime(&now, NULL);
176 elapsed = mtime_since_now(&td->rwmix_switch);
177
178 /*
179 * Check if it's time to seed a new data direction.
180 */
181 if (elapsed >= td->rwmixcycle) {
182 unsigned int v;
183 long r;
184
185 r = os_random_long(&td->rwmix_state);
186 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
187 if (v < td->rwmixread)
188 td->rwmix_ddir = DDIR_READ;
189 else
190 td->rwmix_ddir = DDIR_WRITE;
191 memcpy(&td->rwmix_switch, &now, sizeof(now));
192 }
193 return td->rwmix_ddir;
194 } else if (td_read(td))
195 return DDIR_READ;
196 else
197 return DDIR_WRITE;
198}
199
200void put_io_u(struct thread_data *td, struct io_u *io_u)
201{
202 assert((io_u->flags & IO_U_F_FREE) == 0);
203 io_u->flags |= IO_U_F_FREE;
204
205 io_u->file = NULL;
206 list_del(&io_u->list);
207 list_add(&io_u->list, &td->io_u_freelist);
208 td->cur_depth--;
209}
210
211void requeue_io_u(struct thread_data *td, struct io_u **io_u)
212{
213 struct io_u *__io_u = *io_u;
214
215 list_del(&__io_u->list);
216 list_add_tail(&__io_u->list, &td->io_u_requeues);
217 td->cur_depth--;
218 *io_u = NULL;
219}
220
221static int fill_io_u(struct thread_data *td, struct fio_file *f,
222 struct io_u *io_u)
223{
224 /*
225 * If using an iolog, grab next piece if any available.
226 */
227 if (td->read_iolog)
228 return read_iolog_get(td, io_u);
229
230 /*
231 * see if it's time to sync
232 */
233 if (td->fsync_blocks && !(td->io_issues[DDIR_WRITE] % td->fsync_blocks)
234 && td->io_issues[DDIR_WRITE] && should_fsync(td)) {
235 io_u->ddir = DDIR_SYNC;
236 io_u->file = f;
237 return 0;
238 }
239
240 io_u->ddir = get_rw_ddir(td);
241
242 /*
243 * No log, let the seq/rand engine retrieve the next buflen and
244 * position.
245 */
246 if (get_next_offset(td, f, io_u))
247 return 1;
248
249 io_u->buflen = get_next_buflen(td, f, io_u);
250 if (!io_u->buflen)
251 return 1;
252
253 /*
254 * mark entry before potentially trimming io_u
255 */
256 if (!td->read_iolog && td_random(td) && !td->norandommap)
257 mark_random_map(td, f, io_u);
258
259 /*
260 * If using a write iolog, store this entry.
261 */
262 if (td->write_iolog_file)
263 write_iolog_put(td, io_u);
264
265 io_u->file = f;
266 return 0;
267}
268
269static void io_u_mark_depth(struct thread_data *td)
270{
271 int index = 0;
272
273 switch (td->cur_depth) {
274 default:
275 index++;
276 case 32 ... 63:
277 index++;
278 case 16 ... 31:
279 index++;
280 case 8 ... 15:
281 index++;
282 case 4 ... 7:
283 index++;
284 case 2 ... 3:
285 index++;
286 case 1:
287 break;
288 }
289
290 td->io_u_map[index]++;
291 td->total_io_u++;
292}
293
294static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
295{
296 int index = 0;
297
298 switch (msec) {
299 default:
300 index++;
301 case 1000 ... 1999:
302 index++;
303 case 750 ... 999:
304 index++;
305 case 500 ... 749:
306 index++;
307 case 250 ... 499:
308 index++;
309 case 100 ... 249:
310 index++;
311 case 50 ... 99:
312 index++;
313 case 20 ... 49:
314 index++;
315 case 10 ... 19:
316 index++;
317 case 4 ... 9:
318 index++;
319 case 2 ... 3:
320 index++;
321 case 0 ... 1:
322 break;
323 }
324
325 td->io_u_lat[index]++;
326}
327
328/*
329 * Get next file to service by choosing one at random
330 */
331static struct fio_file *get_next_file_rand(struct thread_data *td)
332{
333 long r = os_random_long(&td->next_file_state);
334 unsigned int fileno;
335 struct fio_file *f;
336
337 do {
338 fileno = (unsigned int) ((double) (td->nr_files - 1) * r / (RAND_MAX + 1.0));
339 f = &td->files[fileno];
340 if (f->fd != -1)
341 return f;
342 } while (1);
343}
344
345/*
346 * Get next file to service by doing round robin between all available ones
347 */
348static struct fio_file *get_next_file_rr(struct thread_data *td)
349{
350 unsigned int old_next_file = td->next_file;
351 struct fio_file *f;
352
353 do {
354 f = &td->files[td->next_file];
355
356 td->next_file++;
357 if (td->next_file >= td->nr_files)
358 td->next_file = 0;
359
360 if (f->fd != -1)
361 break;
362
363 f = NULL;
364 } while (td->next_file != old_next_file);
365
366 return f;
367}
368
369struct io_u *__get_io_u(struct thread_data *td)
370{
371 struct io_u *io_u = NULL;
372
373 if (!list_empty(&td->io_u_requeues))
374 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
375 else if (!queue_full(td)) {
376 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
377
378 io_u->buflen = 0;
379 io_u->resid = 0;
380 io_u->file = NULL;
381 }
382
383 if (io_u) {
384 assert(io_u->flags & IO_U_F_FREE);
385 io_u->flags &= ~IO_U_F_FREE;
386
387 io_u->error = 0;
388 list_del(&io_u->list);
389 list_add(&io_u->list, &td->io_u_busylist);
390 td->cur_depth++;
391 io_u_mark_depth(td);
392 }
393
394 return io_u;
395}
396
397/*
398 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
399 * etc. The returned io_u is fully ready to be prepped and submitted.
400 */
401struct io_u *get_io_u(struct thread_data *td)
402{
403 struct fio_file *f;
404 struct io_u *io_u;
405
406 io_u = __get_io_u(td);
407 if (!io_u)
408 return NULL;
409
410 /*
411 * from a requeue, io_u already setup
412 */
413 if (io_u->file)
414 goto out;
415
416 if (td->file_service_type == FIO_FSERVICE_RR)
417 f = get_next_file_rr(td);
418 else
419 f = get_next_file_rand(td);
420
421 if (!f) {
422 put_io_u(td, io_u);
423 return NULL;
424 }
425
426 io_u->file = f;
427
428 if (td->zone_bytes >= td->zone_size) {
429 td->zone_bytes = 0;
430 f->last_pos += td->zone_skip;
431 }
432
433 if (fill_io_u(td, f, io_u)) {
434 put_io_u(td, io_u);
435 return NULL;
436 }
437
438 if (io_u->buflen + io_u->offset > f->real_file_size) {
439 if (td->io_ops->flags & FIO_RAWIO) {
440 put_io_u(td, io_u);
441 return NULL;
442 }
443
444 io_u->buflen = f->real_file_size - io_u->offset;
445 }
446
447 if (io_u->ddir != DDIR_SYNC) {
448 if (!io_u->buflen) {
449 put_io_u(td, io_u);
450 return NULL;
451 }
452
453 f->last_pos = io_u->offset + io_u->buflen;
454
455 if (td->verify != VERIFY_NONE)
456 populate_verify_io_u(td, io_u);
457 }
458
459 /*
460 * Set io data pointers.
461 */
462out:
463 io_u->xfer_buf = io_u->buf;
464 io_u->xfer_buflen = io_u->buflen;
465
466 if (td_io_prep(td, io_u)) {
467 put_io_u(td, io_u);
468 return NULL;
469 }
470
471 fio_gettime(&io_u->start_time, NULL);
472 return io_u;
473}
474
475static void io_completed(struct thread_data *td, struct io_u *io_u,
476 struct io_completion_data *icd)
477{
478 unsigned long msec;
479
480 assert(io_u->flags & IO_U_F_FLIGHT);
481 io_u->flags &= ~IO_U_F_FLIGHT;
482
483 if (io_u->ddir == DDIR_SYNC) {
484 td->last_was_sync = 1;
485 return;
486 }
487
488 td->last_was_sync = 0;
489
490 if (!io_u->error) {
491 unsigned int bytes = io_u->buflen - io_u->resid;
492 const enum fio_ddir idx = io_u->ddir;
493 int ret;
494
495 td->io_blocks[idx]++;
496 td->io_bytes[idx] += bytes;
497 td->zone_bytes += bytes;
498 td->this_io_bytes[idx] += bytes;
499
500 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
501
502 msec = mtime_since(&io_u->issue_time, &icd->time);
503
504 add_clat_sample(td, idx, msec);
505 add_bw_sample(td, idx, &icd->time);
506 io_u_mark_latency(td, msec);
507
508 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
509 log_io_piece(td, io_u);
510
511 icd->bytes_done[idx] += bytes;
512
513 if (icd->handler) {
514 ret = icd->handler(io_u);
515 if (ret && !icd->error)
516 icd->error = ret;
517 }
518 } else
519 icd->error = io_u->error;
520}
521
522static void init_icd(struct io_completion_data *icd, endio_handler *handler,
523 int nr)
524{
525 fio_gettime(&icd->time, NULL);
526
527 icd->handler = handler;
528 icd->nr = nr;
529
530 icd->error = 0;
531 icd->bytes_done[0] = icd->bytes_done[1] = 0;
532}
533
534static void ios_completed(struct thread_data *td,
535 struct io_completion_data *icd)
536{
537 struct io_u *io_u;
538 int i;
539
540 for (i = 0; i < icd->nr; i++) {
541 io_u = td->io_ops->event(td, i);
542
543 io_completed(td, io_u, icd);
544 put_io_u(td, io_u);
545 }
546}
547
548/*
549 * Complete a single io_u for the sync engines.
550 */
551long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
552 endio_handler *handler)
553{
554 struct io_completion_data icd;
555
556 init_icd(&icd, handler, 1);
557 io_completed(td, io_u, &icd);
558 put_io_u(td, io_u);
559
560 if (!icd.error)
561 return icd.bytes_done[0] + icd.bytes_done[1];
562
563 return -1;
564}
565
566/*
567 * Called to complete min_events number of io for the async engines.
568 */
569long io_u_queued_complete(struct thread_data *td, int min_events,
570 endio_handler *handler)
571
572{
573 struct io_completion_data icd;
574 struct timespec *tvp = NULL;
575 int ret;
576
577 if (min_events > 0) {
578 ret = td_io_commit(td);
579 if (ret < 0) {
580 td_verror(td, -ret, "td_io_commit");
581 return ret;
582 }
583 } else {
584 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
585
586 tvp = &ts;
587 }
588
589 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
590 if (ret < 0) {
591 td_verror(td, -ret, "td_io_getevents");
592 return ret;
593 } else if (!ret)
594 return ret;
595
596 init_icd(&icd, handler, ret);
597 ios_completed(td, &icd);
598 if (!icd.error)
599 return icd.bytes_done[0] + icd.bytes_done[1];
600
601 return -1;
602}
603
604/*
605 * Call when io_u is really queued, to update the submission latency.
606 */
607void io_u_queued(struct thread_data *td, struct io_u *io_u)
608{
609 unsigned long slat_time;
610
611 slat_time = mtime_since(&io_u->start_time, &io_u->issue_time);
612 add_slat_sample(td, io_u->ddir, slat_time);
613}
614
615#ifdef FIO_USE_TIMEOUT
616void io_u_set_timeout(struct thread_data *td)
617{
618 assert(td->cur_depth);
619
620 td->timer.it_interval.tv_sec = 0;
621 td->timer.it_interval.tv_usec = 0;
622 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
623 td->timer.it_value.tv_usec = 0;
624 setitimer(ITIMER_REAL, &td->timer, NULL);
625 fio_gettime(&td->timeout_end, NULL);
626}
627
628static void io_u_dump(struct io_u *io_u)
629{
630 unsigned long t_start = mtime_since_now(&io_u->start_time);
631 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
632
633 log_err("io_u=%p, t_start=%lu, t_issue=%lu\n", io_u, t_start, t_issue);
634 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf, io_u->xfer_buf, io_u->buflen, io_u->xfer_buflen, io_u->offset);
635 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
636}
637#else
638void io_u_set_timeout(struct thread_data fio_unused *td)
639{
640}
641#endif
642
643#ifdef FIO_USE_TIMEOUT
644static void io_u_timeout_handler(int fio_unused sig)
645{
646 struct thread_data *td, *__td;
647 pid_t pid = getpid();
648 struct list_head *entry;
649 struct io_u *io_u;
650 int i;
651
652 log_err("fio: io_u timeout\n");
653
654 /*
655 * TLS would be nice...
656 */
657 td = NULL;
658 for_each_td(__td, i) {
659 if (__td->pid == pid) {
660 td = __td;
661 break;
662 }
663 }
664
665 if (!td) {
666 log_err("fio: io_u timeout, can't find job\n");
667 exit(1);
668 }
669
670 if (!td->cur_depth) {
671 log_err("fio: timeout without pending work?\n");
672 return;
673 }
674
675 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->name, td->pid);
676
677 list_for_each(entry, &td->io_u_busylist) {
678 io_u = list_entry(entry, struct io_u, list);
679
680 io_u_dump(io_u);
681 }
682
683 td_verror(td, ETIMEDOUT, "io_u timeout");
684 exit(1);
685}
686#endif
687
688void io_u_init_timeout(void)
689{
690#ifdef FIO_USE_TIMEOUT
691 signal(SIGALRM, io_u_timeout_handler);
692#endif
693}