11 struct io_completion_data {
13 endio_handler *handler; /* input */
15 int error; /* output */
16 unsigned long bytes_done[2]; /* output */
17 struct timeval time; /* output */
21 * The ->file_map[] contains a map of blocks we have or have not done io
22 * to yet. Used to make sure we cover the entire range in a fair fashion.
24 static int random_map_free(struct thread_data *td, struct fio_file *f,
25 unsigned long long block)
27 unsigned int idx = RAND_MAP_IDX(td, f, block);
28 unsigned int bit = RAND_MAP_BIT(td, f, block);
30 return (f->file_map[idx] & (1UL << bit)) == 0;
34 * Mark a given offset as used in the map.
36 static void mark_random_map(struct thread_data *td, struct fio_file *f,
39 unsigned int min_bs = td->rw_min_bs;
40 unsigned long long block;
42 unsigned int nr_blocks;
44 block = io_u->offset / (unsigned long long) min_bs;
46 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
48 while (blocks < nr_blocks) {
49 unsigned int idx, bit;
51 if (!random_map_free(td, f, block))
54 idx = RAND_MAP_IDX(td, f, block);
55 bit = RAND_MAP_BIT(td, f, block);
57 fio_assert(td, idx < f->num_maps);
59 f->file_map[idx] |= (1UL << bit);
64 if ((blocks * min_bs) < io_u->buflen)
65 io_u->buflen = blocks * min_bs;
69 * Return the next free block in the map.
71 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
72 unsigned long long *b)
76 i = f->last_free_lookup;
77 *b = (i * BLOCKS_PER_MAP);
78 while ((*b) * td->rw_min_bs < f->real_file_size) {
79 if (f->file_map[i] != -1UL) {
80 *b += ffz(f->file_map[i]);
81 f->last_free_lookup = i;
93 * For random io, generate a random new block and see if it's used. Repeat
94 * until we find a free one. For sequential io, just return the end of
97 static int get_next_offset(struct thread_data *td, struct fio_file *f,
100 const int ddir = io_u->ddir;
101 unsigned long long b, rb;
104 if (!td->sequential) {
105 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
109 r = os_random_long(&td->random_state);
110 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
113 rb = b + (f->file_offset / td->min_bs[ddir]);
115 } while (!random_map_free(td, f, rb) && loops);
118 * if we failed to retrieve a truly random offset within
119 * the loops assigned, see if there are free ones left at all
121 if (!loops && get_next_free_block(td, f, &b))
124 b = f->last_pos / td->min_bs[ddir];
126 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
127 if (io_u->offset >= f->real_file_size)
133 static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
136 const int ddir = io_u->ddir;
140 if (td->min_bs[ddir] == td->max_bs[ddir])
141 buflen = td->min_bs[ddir];
143 r = os_random_long(&td->bsrange_state);
144 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
145 if (!td->bs_unaligned)
146 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
149 while (buflen + io_u->offset > f->real_file_size) {
150 if (buflen == td->min_bs[ddir])
153 buflen = td->min_bs[ddir];
160 * Return the data direction for the next io_u. If the job is a
161 * mixed read/write workload, check the rwmix cycle and switch if
164 static enum fio_ddir get_rw_ddir(struct thread_data *td)
168 unsigned long elapsed;
170 fio_gettime(&now, NULL);
171 elapsed = mtime_since_now(&td->rwmix_switch);
174 * Check if it's time to seed a new data direction.
176 if (elapsed >= td->rwmixcycle) {
180 r = os_random_long(&td->rwmix_state);
181 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
182 if (v < td->rwmixread)
183 td->rwmix_ddir = DDIR_READ;
185 td->rwmix_ddir = DDIR_WRITE;
186 memcpy(&td->rwmix_switch, &now, sizeof(now));
188 return td->rwmix_ddir;
189 } else if (td_read(td))
195 void put_io_u(struct thread_data *td, struct io_u *io_u)
197 assert((io_u->flags & IO_U_F_FREE) == 0);
198 io_u->flags |= IO_U_F_FREE;
201 list_del(&io_u->list);
202 list_add(&io_u->list, &td->io_u_freelist);
206 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
208 struct io_u *__io_u = *io_u;
210 list_del(&__io_u->list);
211 list_add_tail(&__io_u->list, &td->io_u_requeues);
216 static int fill_io_u(struct thread_data *td, struct fio_file *f,
220 * If using an iolog, grab next piece if any available.
223 return read_iolog_get(td, io_u);
226 * see if it's time to sync
228 if (td->fsync_blocks && !(td->io_issues[DDIR_WRITE] % td->fsync_blocks)
229 && td->io_issues[DDIR_WRITE] && should_fsync(td)) {
230 io_u->ddir = DDIR_SYNC;
235 io_u->ddir = get_rw_ddir(td);
238 * No log, let the seq/rand engine retrieve the next buflen and
241 if (get_next_offset(td, f, io_u))
244 io_u->buflen = get_next_buflen(td, f, io_u);
249 * mark entry before potentially trimming io_u
251 if (!td->read_iolog && !td->sequential && !td->norandommap)
252 mark_random_map(td, f, io_u);
255 * If using a write iolog, store this entry.
257 if (td->write_iolog_file)
258 write_iolog_put(td, io_u);
264 static void io_u_mark_depth(struct thread_data *td)
268 switch (td->cur_depth) {
285 td->io_u_map[index]++;
289 static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
320 td->io_u_lat[index]++;
323 static struct fio_file *get_next_file(struct thread_data *td)
325 unsigned int old_next_file = td->next_file;
329 f = &td->files[td->next_file];
332 if (td->next_file >= td->nr_files)
339 } while (td->next_file != old_next_file);
344 struct io_u *__get_io_u(struct thread_data *td)
346 struct io_u *io_u = NULL;
348 if (!list_empty(&td->io_u_requeues))
349 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
350 else if (!queue_full(td)) {
351 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
359 assert(io_u->flags & IO_U_F_FREE);
360 io_u->flags &= ~IO_U_F_FREE;
363 list_del(&io_u->list);
364 list_add(&io_u->list, &td->io_u_busylist);
373 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
374 * etc. The returned io_u is fully ready to be prepped and submitted.
376 struct io_u *get_io_u(struct thread_data *td)
381 io_u = __get_io_u(td);
386 * from a requeue, io_u already setup
391 f = get_next_file(td);
399 if (td->zone_bytes >= td->zone_size) {
401 f->last_pos += td->zone_skip;
404 if (fill_io_u(td, f, io_u)) {
409 if (io_u->buflen + io_u->offset > f->real_file_size) {
410 if (td->io_ops->flags & FIO_RAWIO) {
415 io_u->buflen = f->real_file_size - io_u->offset;
418 if (io_u->ddir != DDIR_SYNC) {
424 f->last_pos = io_u->offset + io_u->buflen;
426 if (td->verify != VERIFY_NONE)
427 populate_verify_io_u(td, io_u);
431 * Set io data pointers.
434 io_u->xfer_buf = io_u->buf;
435 io_u->xfer_buflen = io_u->buflen;
437 if (td_io_prep(td, io_u)) {
442 fio_gettime(&io_u->start_time, NULL);
446 static void io_completed(struct thread_data *td, struct io_u *io_u,
447 struct io_completion_data *icd)
451 assert(io_u->flags & IO_U_F_FLIGHT);
452 io_u->flags &= ~IO_U_F_FLIGHT;
454 if (io_u->ddir == DDIR_SYNC) {
455 td->last_was_sync = 1;
459 td->last_was_sync = 0;
462 unsigned int bytes = io_u->buflen - io_u->resid;
463 const enum fio_ddir idx = io_u->ddir;
466 td->io_blocks[idx]++;
467 td->io_bytes[idx] += bytes;
468 td->zone_bytes += bytes;
469 td->this_io_bytes[idx] += bytes;
471 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
473 msec = mtime_since(&io_u->issue_time, &icd->time);
475 add_clat_sample(td, idx, msec);
476 add_bw_sample(td, idx, &icd->time);
477 io_u_mark_latency(td, msec);
479 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
480 log_io_piece(td, io_u);
482 icd->bytes_done[idx] += bytes;
485 ret = icd->handler(io_u);
486 if (ret && !icd->error)
490 icd->error = io_u->error;
493 static void init_icd(struct io_completion_data *icd, endio_handler *handler,
496 fio_gettime(&icd->time, NULL);
498 icd->handler = handler;
502 icd->bytes_done[0] = icd->bytes_done[1] = 0;
505 static void ios_completed(struct thread_data *td,
506 struct io_completion_data *icd)
511 for (i = 0; i < icd->nr; i++) {
512 io_u = td->io_ops->event(td, i);
514 io_completed(td, io_u, icd);
520 * Complete a single io_u for the sync engines.
522 long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
523 endio_handler *handler)
525 struct io_completion_data icd;
527 init_icd(&icd, handler, 1);
528 io_completed(td, io_u, &icd);
532 return icd.bytes_done[0] + icd.bytes_done[1];
538 * Called to complete min_events number of io for the async engines.
540 long io_u_queued_complete(struct thread_data *td, int min_events,
541 endio_handler *handler)
544 struct io_completion_data icd;
545 struct timespec *tvp = NULL;
548 if (min_events > 0) {
549 ret = td_io_commit(td);
555 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
560 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
567 init_icd(&icd, handler, ret);
568 ios_completed(td, &icd);
570 return icd.bytes_done[0] + icd.bytes_done[1];
576 * Call when io_u is really queued, to update the submission latency.
578 void io_u_queued(struct thread_data *td, struct io_u *io_u)
580 unsigned long slat_time;
582 slat_time = mtime_since(&io_u->start_time, &io_u->issue_time);
583 add_slat_sample(td, io_u->ddir, slat_time);
586 void io_u_set_timeout(struct thread_data *td)
588 assert(td->cur_depth);
590 td->timer.it_interval.tv_sec = 0;
591 td->timer.it_interval.tv_usec = 0;
592 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
593 td->timer.it_value.tv_usec = 0;
594 setitimer(ITIMER_REAL, &td->timer, NULL);
595 fio_gettime(&td->timeout_end, NULL);
598 static void io_u_timeout_handler(int fio_unused sig)
600 struct thread_data *td, *__td;
601 pid_t pid = getpid();
604 log_err("fio: io_u timeout\n");
607 * TLS would be nice...
610 for_each_td(__td, i) {
611 if (__td->pid == pid) {
618 log_err("fio: io_u timeout, can't find job\n");
622 if (!td->cur_depth) {
623 log_err("fio: timeout without pending work?\n");
627 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->name, td->pid);
628 td->error = ETIMEDOUT;
632 void io_u_init_timeout(void)
634 signal(SIGALRM, io_u_timeout_handler);