11 * The ->file_map[] contains a map of blocks we have or have not done io
12 * to yet. Used to make sure we cover the entire range in a fair fashion.
14 static int random_map_free(struct thread_data *td, struct fio_file *f,
15 unsigned long long block)
17 unsigned int idx = RAND_MAP_IDX(td, f, block);
18 unsigned int bit = RAND_MAP_BIT(td, f, block);
20 return (f->file_map[idx] & (1UL << bit)) == 0;
24 * Mark a given offset as used in the map.
26 static void mark_random_map(struct thread_data *td, struct fio_file *f,
29 unsigned int min_bs = td->rw_min_bs;
30 unsigned long long block;
32 unsigned int nr_blocks;
34 block = io_u->offset / (unsigned long long) min_bs;
36 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
38 while (blocks < nr_blocks) {
39 unsigned int idx, bit;
41 if (!random_map_free(td, f, block))
44 idx = RAND_MAP_IDX(td, f, block);
45 bit = RAND_MAP_BIT(td, f, block);
48 fio_assert(td, 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) {
141 if (buflen == td->min_bs[ddir])
144 buflen = td->min_bs[ddir];
151 * Return the data direction for the next io_u. If the job is a
152 * mixed read/write workload, check the rwmix cycle and switch if
155 static enum fio_ddir get_rw_ddir(struct thread_data *td)
159 unsigned long elapsed;
161 fio_gettime(&now, NULL);
162 elapsed = mtime_since_now(&td->rwmix_switch);
165 * Check if it's time to seed a new data direction.
167 if (elapsed >= td->rwmixcycle) {
171 r = os_random_long(&td->rwmix_state);
172 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
173 if (v < td->rwmixread)
174 td->rwmix_ddir = DDIR_READ;
176 td->rwmix_ddir = DDIR_WRITE;
177 memcpy(&td->rwmix_switch, &now, sizeof(now));
179 return td->rwmix_ddir;
180 } else if (td_read(td))
186 void put_io_u(struct thread_data *td, struct io_u *io_u)
189 list_del(&io_u->list);
190 list_add(&io_u->list, &td->io_u_freelist);
194 static int fill_io_u(struct thread_data *td, struct fio_file *f,
198 * If using an iolog, grab next piece if any available.
201 return read_iolog_get(td, io_u);
204 * see if it's time to sync
206 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
207 && should_fsync(td)) {
208 io_u->ddir = DDIR_SYNC;
213 io_u->ddir = get_rw_ddir(td);
216 * No log, let the seq/rand engine retrieve the next buflen and
219 if (get_next_offset(td, f, io_u))
222 io_u->buflen = get_next_buflen(td, f, io_u);
227 * mark entry before potentially trimming io_u
229 if (!td->read_iolog && !td->sequential && !td->norandommap)
230 mark_random_map(td, f, io_u);
233 * If using a write iolog, store this entry.
235 if (td->write_iolog_file)
236 write_iolog_put(td, io_u);
242 static void io_u_mark_depth(struct thread_data *td)
246 switch (td->cur_depth) {
263 td->io_u_map[index]++;
267 struct io_u *__get_io_u(struct thread_data *td)
269 struct io_u *io_u = NULL;
271 if (!queue_full(td)) {
272 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
277 list_del(&io_u->list);
278 list_add(&io_u->list, &td->io_u_busylist);
287 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
288 * etc. The returned io_u is fully ready to be prepped and submitted.
290 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
294 io_u = __get_io_u(td);
298 if (td->zone_bytes >= td->zone_size) {
300 f->last_pos += td->zone_skip;
303 if (fill_io_u(td, f, io_u)) {
308 if (io_u->buflen + io_u->offset > f->real_file_size) {
309 if (td->io_ops->flags & FIO_RAWIO) {
314 io_u->buflen = f->real_file_size - io_u->offset;
317 if (io_u->ddir != DDIR_SYNC) {
323 f->last_pos += io_u->buflen;
325 if (td->verify != VERIFY_NONE)
326 populate_verify_io_u(td, io_u);
329 if (td_io_prep(td, io_u)) {
335 * Set io data pointers.
337 io_u->xfer_buf = io_u->buf;
338 io_u->xfer_buflen = io_u->buflen;
340 fio_gettime(&io_u->start_time, NULL);
344 void io_completed(struct thread_data *td, struct io_u *io_u,
345 struct io_completion_data *icd)
349 if (io_u->ddir == DDIR_SYNC) {
350 td->last_was_sync = 1;
354 td->last_was_sync = 0;
357 unsigned int bytes = io_u->buflen - io_u->resid;
358 const enum fio_ddir idx = io_u->ddir;
360 td->io_blocks[idx]++;
361 td->io_bytes[idx] += bytes;
362 td->zone_bytes += bytes;
363 td->this_io_bytes[idx] += bytes;
365 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
367 msec = mtime_since(&io_u->issue_time, &icd->time);
369 add_clat_sample(td, idx, msec);
370 add_bw_sample(td, idx, &icd->time);
372 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
373 log_io_piece(td, io_u);
375 icd->bytes_done[idx] += bytes;
377 icd->error = io_u->error;
380 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
385 fio_gettime(&icd->time, NULL);
388 icd->bytes_done[0] = icd->bytes_done[1] = 0;
390 for (i = 0; i < icd->nr; i++) {
391 io_u = td->io_ops->event(td, i);
393 io_completed(td, io_u, icd);