12 * Change this define to play with the timeout handling
14 #undef FIO_USE_TIMEOUT
16 struct io_completion_data {
18 endio_handler *handler; /* input */
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
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.
29 static int random_map_free(struct thread_data *td, struct fio_file *f,
30 unsigned long long block)
32 unsigned int idx = RAND_MAP_IDX(td, f, block);
33 unsigned int bit = RAND_MAP_BIT(td, f, block);
35 return (f->file_map[idx] & (1UL << bit)) == 0;
39 * Mark a given offset as used in the map.
41 static void mark_random_map(struct thread_data *td, struct fio_file *f,
44 unsigned int min_bs = td->rw_min_bs;
45 unsigned long long block;
47 unsigned int nr_blocks;
49 block = io_u->offset / (unsigned long long) min_bs;
51 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
53 while (blocks < nr_blocks) {
54 unsigned int idx, bit;
56 if (!random_map_free(td, f, block))
59 idx = RAND_MAP_IDX(td, f, block);
60 bit = RAND_MAP_BIT(td, f, block);
62 fio_assert(td, idx < f->num_maps);
64 f->file_map[idx] |= (1UL << bit);
69 if ((blocks * min_bs) < io_u->buflen)
70 io_u->buflen = blocks * min_bs;
74 * Return the next free block in the map.
76 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
77 unsigned long long *b)
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;
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.
102 static int get_next_offset(struct thread_data *td, struct fio_file *f,
105 const int ddir = io_u->ddir;
106 unsigned long long b, rb;
110 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
114 r = os_random_long(&td->random_state);
115 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
118 rb = b + (f->file_offset / td->min_bs[ddir]);
120 } while (!random_map_free(td, f, rb) && loops);
123 * if we failed to retrieve a truly random offset within
124 * the loops assigned, see if there are free ones left at all
126 if (!loops && get_next_free_block(td, f, &b))
129 b = f->last_pos / td->min_bs[ddir];
131 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
132 if (io_u->offset >= f->real_file_size)
138 static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
141 const int ddir = io_u->ddir;
145 if (td->min_bs[ddir] == td->max_bs[ddir])
146 buflen = td->min_bs[ddir];
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);
154 while (buflen + io_u->offset > f->real_file_size) {
155 if (buflen == td->min_bs[ddir])
158 buflen = td->min_bs[ddir];
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
169 static enum fio_ddir get_rw_ddir(struct thread_data *td)
173 unsigned long elapsed;
175 fio_gettime(&now, NULL);
176 elapsed = mtime_since_now(&td->rwmix_switch);
179 * Check if it's time to seed a new data direction.
181 if (elapsed >= td->rwmixcycle) {
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;
190 td->rwmix_ddir = DDIR_WRITE;
191 memcpy(&td->rwmix_switch, &now, sizeof(now));
193 return td->rwmix_ddir;
194 } else if (td_read(td))
200 void put_io_u(struct thread_data *td, struct io_u *io_u)
202 assert((io_u->flags & IO_U_F_FREE) == 0);
203 io_u->flags |= IO_U_F_FREE;
206 list_del(&io_u->list);
207 list_add(&io_u->list, &td->io_u_freelist);
211 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
213 struct io_u *__io_u = *io_u;
215 list_del(&__io_u->list);
216 list_add_tail(&__io_u->list, &td->io_u_requeues);
221 static int fill_io_u(struct thread_data *td, struct fio_file *f,
225 * If using an iolog, grab next piece if any available.
228 return read_iolog_get(td, io_u);
231 * see if it's time to sync
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;
240 io_u->ddir = get_rw_ddir(td);
243 * No log, let the seq/rand engine retrieve the next buflen and
246 if (get_next_offset(td, f, io_u))
249 io_u->buflen = get_next_buflen(td, f, io_u);
254 * mark entry before potentially trimming io_u
256 if (!td->read_iolog && td_random(td) && !td->norandommap)
257 mark_random_map(td, f, io_u);
260 * If using a write iolog, store this entry.
262 if (td->write_iolog_file)
263 write_iolog_put(td, io_u);
269 static void io_u_mark_depth(struct thread_data *td)
273 switch (td->cur_depth) {
290 td->io_u_map[index]++;
294 static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
325 td->io_u_lat[index]++;
329 * Get next file to service by choosing one at random
331 static struct fio_file *get_next_file_rand(struct thread_data *td)
333 long r = os_random_long(&td->next_file_state);
338 fileno = (unsigned int) ((double) (td->nr_files - 1) * r / (RAND_MAX + 1.0));
339 f = &td->files[fileno];
346 * Get next file to service by doing round robin between all available ones
348 static struct fio_file *get_next_file_rr(struct thread_data *td)
350 unsigned int old_next_file = td->next_file;
354 f = &td->files[td->next_file];
357 if (td->next_file >= td->nr_files)
364 } while (td->next_file != old_next_file);
369 struct io_u *__get_io_u(struct thread_data *td)
371 struct io_u *io_u = NULL;
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);
384 assert(io_u->flags & IO_U_F_FREE);
385 io_u->flags &= ~IO_U_F_FREE;
388 list_del(&io_u->list);
389 list_add(&io_u->list, &td->io_u_busylist);
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.
401 struct io_u *get_io_u(struct thread_data *td)
406 io_u = __get_io_u(td);
411 * from a requeue, io_u already setup
416 if (td->file_service_type == FIO_FSERVICE_RR)
417 f = get_next_file_rr(td);
419 f = get_next_file_rand(td);
428 if (td->zone_bytes >= td->zone_size) {
430 f->last_pos += td->zone_skip;
433 if (fill_io_u(td, f, io_u)) {
438 if (io_u->buflen + io_u->offset > f->real_file_size) {
439 if (td->io_ops->flags & FIO_RAWIO) {
444 io_u->buflen = f->real_file_size - io_u->offset;
447 if (io_u->ddir != DDIR_SYNC) {
453 f->last_pos = io_u->offset + io_u->buflen;
455 if (td->verify != VERIFY_NONE)
456 populate_verify_io_u(td, io_u);
460 * Set io data pointers.
463 io_u->xfer_buf = io_u->buf;
464 io_u->xfer_buflen = io_u->buflen;
466 if (td_io_prep(td, io_u)) {
471 fio_gettime(&io_u->start_time, NULL);
475 static void io_completed(struct thread_data *td, struct io_u *io_u,
476 struct io_completion_data *icd)
480 assert(io_u->flags & IO_U_F_FLIGHT);
481 io_u->flags &= ~IO_U_F_FLIGHT;
483 if (io_u->ddir == DDIR_SYNC) {
484 td->last_was_sync = 1;
488 td->last_was_sync = 0;
491 unsigned int bytes = io_u->buflen - io_u->resid;
492 const enum fio_ddir idx = io_u->ddir;
495 td->io_blocks[idx]++;
496 td->io_bytes[idx] += bytes;
497 td->zone_bytes += bytes;
498 td->this_io_bytes[idx] += bytes;
500 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
502 msec = mtime_since(&io_u->issue_time, &icd->time);
504 add_clat_sample(td, idx, msec);
505 add_bw_sample(td, idx, &icd->time);
506 io_u_mark_latency(td, msec);
508 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
509 log_io_piece(td, io_u);
511 icd->bytes_done[idx] += bytes;
514 ret = icd->handler(io_u);
515 if (ret && !icd->error)
519 icd->error = io_u->error;
522 static void init_icd(struct io_completion_data *icd, endio_handler *handler,
525 fio_gettime(&icd->time, NULL);
527 icd->handler = handler;
531 icd->bytes_done[0] = icd->bytes_done[1] = 0;
534 static void ios_completed(struct thread_data *td,
535 struct io_completion_data *icd)
540 for (i = 0; i < icd->nr; i++) {
541 io_u = td->io_ops->event(td, i);
543 io_completed(td, io_u, icd);
549 * Complete a single io_u for the sync engines.
551 long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
552 endio_handler *handler)
554 struct io_completion_data icd;
556 init_icd(&icd, handler, 1);
557 io_completed(td, io_u, &icd);
561 return icd.bytes_done[0] + icd.bytes_done[1];
567 * Called to complete min_events number of io for the async engines.
569 long io_u_queued_complete(struct thread_data *td, int min_events,
570 endio_handler *handler)
573 struct io_completion_data icd;
574 struct timespec *tvp = NULL;
577 if (min_events > 0) {
578 ret = td_io_commit(td);
580 td_verror(td, -ret, "td_io_commit");
584 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
589 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
591 td_verror(td, -ret, "td_io_getevents");
596 init_icd(&icd, handler, ret);
597 ios_completed(td, &icd);
599 return icd.bytes_done[0] + icd.bytes_done[1];
605 * Call when io_u is really queued, to update the submission latency.
607 void io_u_queued(struct thread_data *td, struct io_u *io_u)
609 unsigned long slat_time;
611 slat_time = mtime_since(&io_u->start_time, &io_u->issue_time);
612 add_slat_sample(td, io_u->ddir, slat_time);
615 #ifdef FIO_USE_TIMEOUT
616 void io_u_set_timeout(struct thread_data *td)
618 assert(td->cur_depth);
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);
628 static void io_u_dump(struct io_u *io_u)
630 unsigned long t_start = mtime_since_now(&io_u->start_time);
631 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
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);
638 void io_u_set_timeout(struct thread_data fio_unused *td)
643 #ifdef FIO_USE_TIMEOUT
644 static void io_u_timeout_handler(int fio_unused sig)
646 struct thread_data *td, *__td;
647 pid_t pid = getpid();
648 struct list_head *entry;
652 log_err("fio: io_u timeout\n");
655 * TLS would be nice...
658 for_each_td(__td, i) {
659 if (__td->pid == pid) {
666 log_err("fio: io_u timeout, can't find job\n");
670 if (!td->cur_depth) {
671 log_err("fio: timeout without pending work?\n");
675 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->name, td->pid);
677 list_for_each(entry, &td->io_u_busylist) {
678 io_u = list_entry(entry, struct io_u, list);
683 td_verror(td, ETIMEDOUT, "io_u timeout");
688 void io_u_init_timeout(void)
690 #ifdef FIO_USE_TIMEOUT
691 signal(SIGALRM, io_u_timeout_handler);