12 * The ->file_map[] contains a map of blocks we have or have not done io
13 * to yet. Used to make sure we cover the entire range in a fair fashion.
15 static int random_map_free(struct thread_data *td, struct fio_file *f,
16 unsigned long long block)
18 unsigned int idx = RAND_MAP_IDX(td, f, block);
19 unsigned int bit = RAND_MAP_BIT(td, f, block);
21 return (f->file_map[idx] & (1UL << bit)) == 0;
25 * Mark a given offset as used in the map.
27 static void mark_random_map(struct thread_data *td, struct fio_file *f,
30 unsigned int min_bs = td->rw_min_bs;
31 unsigned long long block;
33 unsigned int nr_blocks;
35 block = io_u->offset / (unsigned long long) min_bs;
37 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
39 while (blocks < nr_blocks) {
40 unsigned int idx, bit;
42 if (!random_map_free(td, f, block))
45 idx = RAND_MAP_IDX(td, f, block);
46 bit = RAND_MAP_BIT(td, f, block);
48 assert(idx < f->num_maps);
50 f->file_map[idx] |= (1UL << bit);
55 if ((blocks * min_bs) < io_u->buflen)
56 io_u->buflen = blocks * min_bs;
60 * Return the next free block in the map.
62 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
63 unsigned long long *b)
67 i = f->last_free_lookup;
68 *b = (i * BLOCKS_PER_MAP);
69 while ((*b) * td->rw_min_bs < f->real_file_size) {
70 if (f->file_map[i] != -1UL) {
71 *b += ffz(f->file_map[i]);
72 f->last_free_lookup = i;
84 * For random io, generate a random new block and see if it's used. Repeat
85 * until we find a free one. For sequential io, just return the end of
88 static int get_next_offset(struct thread_data *td, struct fio_file *f,
91 const int ddir = io_u->ddir;
92 unsigned long long b, rb;
95 if (!td->sequential) {
96 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
100 r = os_random_long(&td->random_state);
101 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
104 rb = b + (f->file_offset / td->min_bs[ddir]);
106 } while (!random_map_free(td, f, rb) && loops);
109 * if we failed to retrieve a truly random offset within
110 * the loops assigned, see if there are free ones left at all
112 if (!loops && get_next_free_block(td, f, &b))
115 b = f->last_pos / td->min_bs[ddir];
117 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
118 if (io_u->offset >= f->real_file_size)
124 static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
127 const int ddir = io_u->ddir;
131 if (td->min_bs[ddir] == td->max_bs[ddir])
132 buflen = td->min_bs[ddir];
134 r = os_random_long(&td->bsrange_state);
135 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
136 if (!td->bs_unaligned)
137 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
140 while (buflen + io_u->offset > f->real_file_size) {
142 * if using direct/raw io, we may not be able to
143 * shrink the size. so just fail it.
145 if (td->io_ops->flags & FIO_RAWIO)
148 if (buflen == td->min_bs[ddir])
151 buflen = td->min_bs[ddir];
158 * Return the data direction for the next io_u. If the job is a
159 * mixed read/write workload, check the rwmix cycle and switch if
162 static enum fio_ddir get_rw_ddir(struct thread_data *td)
166 unsigned long elapsed;
168 fio_gettime(&now, NULL);
169 elapsed = mtime_since_now(&td->rwmix_switch);
172 * Check if it's time to seed a new data direction.
174 if (elapsed >= td->rwmixcycle) {
178 r = os_random_long(&td->rwmix_state);
179 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
180 if (v < td->rwmixread)
181 td->rwmix_ddir = DDIR_READ;
183 td->rwmix_ddir = DDIR_WRITE;
184 memcpy(&td->rwmix_switch, &now, sizeof(now));
186 return td->rwmix_ddir;
187 } else if (td_read(td))
193 void put_io_u(struct thread_data *td, struct io_u *io_u)
196 list_del(&io_u->list);
197 list_add(&io_u->list, &td->io_u_freelist);
201 static int fill_io_u(struct thread_data *td, struct fio_file *f,
205 * If using an iolog, grab next piece if any available.
208 return read_iolog_get(td, io_u);
211 * see if it's time to sync
213 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
214 && should_fsync(td)) {
215 io_u->ddir = DDIR_SYNC;
220 io_u->ddir = get_rw_ddir(td);
223 * No log, let the seq/rand engine retrieve the next buflen and
226 if (get_next_offset(td, f, io_u))
229 io_u->buflen = get_next_buflen(td, f, io_u);
234 * mark entry before potentially trimming io_u
236 if (!td->read_iolog && !td->sequential && !td->norandommap)
237 mark_random_map(td, f, io_u);
240 * If using a write iolog, store this entry.
242 if (td->write_iolog_file)
243 write_iolog_put(td, io_u);
249 static void io_u_mark_depth(struct thread_data *td)
253 switch (td->cur_depth) {
270 td->io_u_map[index]++;
274 struct io_u *__get_io_u(struct thread_data *td)
276 struct io_u *io_u = NULL;
278 if (!queue_full(td)) {
279 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
284 list_del(&io_u->list);
285 list_add(&io_u->list, &td->io_u_busylist);
294 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
295 * etc. The returned io_u is fully ready to be prepped and submitted.
297 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
301 io_u = __get_io_u(td);
305 if (td->zone_bytes >= td->zone_size) {
307 f->last_pos += td->zone_skip;
310 if (fill_io_u(td, f, io_u)) {
315 if (io_u->buflen + io_u->offset > f->real_file_size) {
316 if (td->io_ops->flags & FIO_RAWIO) {
321 io_u->buflen = f->real_file_size - io_u->offset;
324 if (io_u->ddir != DDIR_SYNC) {
330 f->last_pos += io_u->buflen;
332 if (td->verify != VERIFY_NONE)
333 populate_verify_io_u(td, io_u);
336 if (td_io_prep(td, io_u)) {
342 * Set io data pointers.
344 io_u->xfer_buf = io_u->buf;
345 io_u->xfer_buflen = io_u->buflen;
347 fio_gettime(&io_u->start_time, NULL);
351 void io_completed(struct thread_data *td, struct io_u *io_u,
352 struct io_completion_data *icd)
356 if (io_u->ddir == DDIR_SYNC) {
357 td->last_was_sync = 1;
361 td->last_was_sync = 0;
364 unsigned int bytes = io_u->buflen - io_u->resid;
365 const enum fio_ddir idx = io_u->ddir;
367 td->io_blocks[idx]++;
368 td->io_bytes[idx] += bytes;
369 td->zone_bytes += bytes;
370 td->this_io_bytes[idx] += bytes;
372 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
374 msec = mtime_since(&io_u->issue_time, &icd->time);
376 add_clat_sample(td, idx, msec);
377 add_bw_sample(td, idx, &icd->time);
379 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
380 log_io_piece(td, io_u);
382 icd->bytes_done[idx] += bytes;
384 icd->error = io_u->error;
387 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
392 fio_gettime(&icd->time, NULL);
395 icd->bytes_done[0] = icd->bytes_done[1] = 0;
397 for (i = 0; i < icd->nr; i++) {
398 io_u = td->io_ops->event(td, i);
400 io_completed(td, io_u, icd);