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->min_bs[io_u->ddir];
31 unsigned long long block;
34 block = io_u->offset / (unsigned long long) min_bs;
36 while (blocks < (io_u->buflen / min_bs)) {
37 unsigned int idx, bit;
39 if (!random_map_free(td, f, block))
42 idx = RAND_MAP_IDX(td, f, block);
43 bit = RAND_MAP_BIT(td, f, block);
45 assert(idx < f->num_maps);
47 f->file_map[idx] |= (1UL << bit);
52 if ((blocks * min_bs) < io_u->buflen)
53 io_u->buflen = blocks * min_bs;
57 * Return the next free block in the map.
59 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
60 unsigned long long *b)
66 while ((*b) * td->rw_min_bs < f->file_size) {
67 if (f->file_map[i] != -1UL) {
68 *b += ffz(f->file_map[i]);
80 * For random io, generate a random new block and see if it's used. Repeat
81 * until we find a free one. For sequential io, just return the end of
84 static int get_next_offset(struct thread_data *td, struct fio_file *f,
85 unsigned long long *offset, int ddir)
87 unsigned long long b, rb;
90 if (!td->sequential) {
91 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
95 r = os_random_long(&td->random_state);
96 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
99 rb = b + (f->file_offset / td->min_bs[ddir]);
101 } while (!random_map_free(td, f, rb) && loops);
104 if (get_next_free_block(td, f, &b))
108 b = f->last_pos / td->min_bs[ddir];
110 *offset = (b * td->min_bs[ddir]) + f->file_offset;
111 if (*offset > f->file_size)
117 static unsigned int get_next_buflen(struct thread_data *td, int ddir)
122 if (td->min_bs[ddir] == td->max_bs[ddir])
123 buflen = td->min_bs[ddir];
125 r = os_random_long(&td->bsrange_state);
126 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
127 if (!td->bs_unaligned)
128 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
131 if (buflen > td->io_size - td->this_io_bytes[ddir]) {
133 * if using direct/raw io, we may not be able to
134 * shrink the size. so just fail it.
136 if (td->io_ops->flags & FIO_RAWIO)
139 buflen = td->io_size - td->this_io_bytes[ddir];
146 * Return the data direction for the next io_u. If the job is a
147 * mixed read/write workload, check the rwmix cycle and switch if
150 static enum fio_ddir get_rw_ddir(struct thread_data *td)
154 unsigned long elapsed;
156 fio_gettime(&now, NULL);
157 elapsed = mtime_since_now(&td->rwmix_switch);
160 * Check if it's time to seed a new data direction.
162 if (elapsed >= td->rwmixcycle) {
166 r = os_random_long(&td->rwmix_state);
167 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
168 if (v < td->rwmixread)
169 td->rwmix_ddir = DDIR_READ;
171 td->rwmix_ddir = DDIR_WRITE;
172 memcpy(&td->rwmix_switch, &now, sizeof(now));
174 return td->rwmix_ddir;
175 } else if (td_read(td))
181 void put_io_u(struct thread_data *td, struct io_u *io_u)
184 list_del(&io_u->list);
185 list_add(&io_u->list, &td->io_u_freelist);
189 static int fill_io_u(struct thread_data *td, struct fio_file *f,
193 * If using an iolog, grab next piece if any available.
196 return read_iolog_get(td, io_u);
199 * see if it's time to sync
201 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
202 && should_fsync(td)) {
203 io_u->ddir = DDIR_SYNC;
208 io_u->ddir = get_rw_ddir(td);
211 * No log, let the seq/rand engine retrieve the next position.
213 if (!get_next_offset(td, f, &io_u->offset, io_u->ddir)) {
214 io_u->buflen = get_next_buflen(td, io_u->ddir);
217 * If using a write iolog, store this entry.
219 if (td->write_iolog_file)
220 write_iolog_put(td, io_u);
230 struct io_u *__get_io_u(struct thread_data *td)
232 struct io_u *io_u = NULL;
234 if (!queue_full(td)) {
235 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
240 list_del(&io_u->list);
241 list_add(&io_u->list, &td->io_u_busylist);
249 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
250 * etc. The returned io_u is fully ready to be prepped and submitted.
252 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
256 io_u = __get_io_u(td);
260 if (td->zone_bytes >= td->zone_size) {
262 f->last_pos += td->zone_skip;
265 if (fill_io_u(td, f, io_u)) {
270 if (io_u->buflen + io_u->offset > f->file_size) {
271 if (td->io_ops->flags & FIO_RAWIO) {
276 io_u->buflen = f->file_size - io_u->offset;
279 if (io_u->ddir != DDIR_SYNC) {
285 if (!td->read_iolog && !td->sequential && !td->norandommap)
286 mark_random_map(td, f, io_u);
288 f->last_pos += io_u->buflen;
290 if (td->verify != VERIFY_NONE)
291 populate_verify_io_u(td, io_u);
294 if (td_io_prep(td, io_u)) {
299 fio_gettime(&io_u->start_time, NULL);
303 void io_completed(struct thread_data *td, struct io_u *io_u,
304 struct io_completion_data *icd)
308 if (io_u->ddir == DDIR_SYNC) {
309 td->last_was_sync = 1;
313 td->last_was_sync = 0;
316 unsigned int bytes = io_u->buflen - io_u->resid;
317 const enum fio_ddir idx = io_u->ddir;
319 td->io_blocks[idx]++;
320 td->io_bytes[idx] += bytes;
321 td->zone_bytes += bytes;
322 td->this_io_bytes[idx] += bytes;
324 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
326 msec = mtime_since(&io_u->issue_time, &icd->time);
328 add_clat_sample(td, idx, msec);
329 add_bw_sample(td, idx, &icd->time);
331 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
332 log_io_piece(td, io_u);
334 icd->bytes_done[idx] += bytes;
336 icd->error = io_u->error;
339 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
344 fio_gettime(&icd->time, NULL);
347 icd->bytes_done[0] = icd->bytes_done[1] = 0;
349 for (i = 0; i < icd->nr; i++) {
350 io_u = td->io_ops->event(td, i);
352 io_completed(td, io_u, icd);