10 struct io_completion_data {
12 endio_handler *handler; /* input */
14 int error; /* output */
15 unsigned long bytes_done[2]; /* output */
16 struct timeval time; /* output */
20 * The ->file_map[] contains a map of blocks we have or have not done io
21 * to yet. Used to make sure we cover the entire range in a fair fashion.
23 static int random_map_free(struct thread_data *td, struct fio_file *f,
24 unsigned long long block)
26 unsigned int idx = RAND_MAP_IDX(td, f, block);
27 unsigned int bit = RAND_MAP_BIT(td, f, block);
29 return (f->file_map[idx] & (1UL << bit)) == 0;
33 * Mark a given offset as used in the map.
35 static void mark_random_map(struct thread_data *td, struct fio_file *f,
38 unsigned int min_bs = td->rw_min_bs;
39 unsigned long long block;
41 unsigned int nr_blocks;
43 block = io_u->offset / (unsigned long long) min_bs;
45 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
47 while (blocks < nr_blocks) {
48 unsigned int idx, bit;
50 if (!random_map_free(td, f, block))
53 idx = RAND_MAP_IDX(td, f, block);
54 bit = RAND_MAP_BIT(td, f, block);
56 fio_assert(td, idx < f->num_maps);
58 f->file_map[idx] |= (1UL << bit);
63 if ((blocks * min_bs) < io_u->buflen)
64 io_u->buflen = blocks * min_bs;
68 * Return the next free block in the map.
70 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
71 unsigned long long *b)
75 i = f->last_free_lookup;
76 *b = (i * BLOCKS_PER_MAP);
77 while ((*b) * td->rw_min_bs < f->real_file_size) {
78 if (f->file_map[i] != -1UL) {
79 *b += ffz(f->file_map[i]);
80 f->last_free_lookup = i;
92 * For random io, generate a random new block and see if it's used. Repeat
93 * until we find a free one. For sequential io, just return the end of
96 static int get_next_offset(struct thread_data *td, struct fio_file *f,
99 const int ddir = io_u->ddir;
100 unsigned long long b, rb;
103 if (!td->sequential) {
104 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
108 r = os_random_long(&td->random_state);
109 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
112 rb = b + (f->file_offset / td->min_bs[ddir]);
114 } while (!random_map_free(td, f, rb) && loops);
117 * if we failed to retrieve a truly random offset within
118 * the loops assigned, see if there are free ones left at all
120 if (!loops && get_next_free_block(td, f, &b))
123 b = f->last_pos / td->min_bs[ddir];
125 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
126 if (io_u->offset >= f->real_file_size)
132 static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
135 const int ddir = io_u->ddir;
139 if (td->min_bs[ddir] == td->max_bs[ddir])
140 buflen = td->min_bs[ddir];
142 r = os_random_long(&td->bsrange_state);
143 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
144 if (!td->bs_unaligned)
145 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
148 while (buflen + io_u->offset > f->real_file_size) {
149 if (buflen == td->min_bs[ddir])
152 buflen = td->min_bs[ddir];
159 * Return the data direction for the next io_u. If the job is a
160 * mixed read/write workload, check the rwmix cycle and switch if
163 static enum fio_ddir get_rw_ddir(struct thread_data *td)
167 unsigned long elapsed;
169 fio_gettime(&now, NULL);
170 elapsed = mtime_since_now(&td->rwmix_switch);
173 * Check if it's time to seed a new data direction.
175 if (elapsed >= td->rwmixcycle) {
179 r = os_random_long(&td->rwmix_state);
180 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
181 if (v < td->rwmixread)
182 td->rwmix_ddir = DDIR_READ;
184 td->rwmix_ddir = DDIR_WRITE;
185 memcpy(&td->rwmix_switch, &now, sizeof(now));
187 return td->rwmix_ddir;
188 } else if (td_read(td))
194 void put_io_u(struct thread_data *td, struct io_u *io_u)
197 list_del(&io_u->list);
198 list_add(&io_u->list, &td->io_u_freelist);
202 static int fill_io_u(struct thread_data *td, struct fio_file *f,
206 * If using an iolog, grab next piece if any available.
209 return read_iolog_get(td, io_u);
212 * see if it's time to sync
214 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
215 && should_fsync(td)) {
216 io_u->ddir = DDIR_SYNC;
221 io_u->ddir = get_rw_ddir(td);
224 * No log, let the seq/rand engine retrieve the next buflen and
227 if (get_next_offset(td, f, io_u))
230 io_u->buflen = get_next_buflen(td, f, io_u);
235 * mark entry before potentially trimming io_u
237 if (!td->read_iolog && !td->sequential && !td->norandommap)
238 mark_random_map(td, f, io_u);
241 * If using a write iolog, store this entry.
243 if (td->write_iolog_file)
244 write_iolog_put(td, io_u);
250 static void io_u_mark_depth(struct thread_data *td)
254 switch (td->cur_depth) {
271 td->io_u_map[index]++;
275 static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
306 td->io_u_lat[index]++;
309 struct io_u *__get_io_u(struct thread_data *td)
311 struct io_u *io_u = NULL;
313 if (!queue_full(td)) {
314 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
319 list_del(&io_u->list);
320 list_add(&io_u->list, &td->io_u_busylist);
329 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
330 * etc. The returned io_u is fully ready to be prepped and submitted.
332 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
336 io_u = __get_io_u(td);
340 if (td->zone_bytes >= td->zone_size) {
342 f->last_pos += td->zone_skip;
345 if (fill_io_u(td, f, io_u)) {
350 if (io_u->buflen + io_u->offset > f->real_file_size) {
351 if (td->io_ops->flags & FIO_RAWIO) {
356 io_u->buflen = f->real_file_size - io_u->offset;
359 if (io_u->ddir != DDIR_SYNC) {
365 f->last_pos = io_u->offset + io_u->buflen;
367 if (td->verify != VERIFY_NONE)
368 populate_verify_io_u(td, io_u);
372 * Set io data pointers.
374 io_u->xfer_buf = io_u->buf;
375 io_u->xfer_buflen = io_u->buflen;
377 if (td_io_prep(td, io_u)) {
382 fio_gettime(&io_u->start_time, NULL);
386 static void io_completed(struct thread_data *td, struct io_u *io_u,
387 struct io_completion_data *icd)
391 if (io_u->ddir == DDIR_SYNC) {
392 td->last_was_sync = 1;
396 td->last_was_sync = 0;
399 unsigned int bytes = io_u->buflen - io_u->resid;
400 const enum fio_ddir idx = io_u->ddir;
403 td->io_blocks[idx]++;
404 td->io_bytes[idx] += bytes;
405 td->zone_bytes += bytes;
406 td->this_io_bytes[idx] += bytes;
408 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
410 msec = mtime_since(&io_u->issue_time, &icd->time);
412 add_clat_sample(td, idx, msec);
413 add_bw_sample(td, idx, &icd->time);
414 io_u_mark_latency(td, msec);
416 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
417 log_io_piece(td, io_u);
419 icd->bytes_done[idx] += bytes;
422 ret = icd->handler(io_u);
423 if (ret && !icd->error)
427 icd->error = io_u->error;
430 static void init_icd(struct io_completion_data *icd, endio_handler *handler,
433 fio_gettime(&icd->time, NULL);
435 icd->handler = handler;
439 icd->bytes_done[0] = icd->bytes_done[1] = 0;
442 static void ios_completed(struct thread_data *td,
443 struct io_completion_data *icd)
448 for (i = 0; i < icd->nr; i++) {
449 io_u = td->io_ops->event(td, i);
451 io_completed(td, io_u, icd);
456 long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
457 endio_handler *handler)
459 struct io_completion_data icd;
461 init_icd(&icd, handler, 1);
462 io_completed(td, io_u, &icd);
466 return icd.bytes_done[0] + icd.bytes_done[1];
468 td_verror(td, icd.error);
472 long io_u_queued_complete(struct thread_data *td, int min_events,
473 endio_handler *handler)
476 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
477 struct timespec *tsp = NULL;
478 struct io_completion_data icd;
484 ret = td_io_getevents(td, min_events, td->cur_depth, tsp);
491 init_icd(&icd, handler, ret);
492 ios_completed(td, &icd);
494 return icd.bytes_done[0] + icd.bytes_done[1];
496 td_verror(td, icd.error);