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);
47 fio_assert(td, idx < f->num_maps);
49 f->file_map[idx] |= (1UL << bit);
54 if ((blocks * min_bs) < io_u->buflen)
55 io_u->buflen = blocks * min_bs;
59 * Return the next free block in the map.
61 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
62 unsigned long long *b)
66 i = f->last_free_lookup;
67 *b = (i * BLOCKS_PER_MAP);
68 while ((*b) * td->rw_min_bs < f->real_file_size) {
69 if (f->file_map[i] != -1UL) {
70 *b += ffz(f->file_map[i]);
71 f->last_free_lookup = i;
83 * For random io, generate a random new block and see if it's used. Repeat
84 * until we find a free one. For sequential io, just return the end of
87 static int get_next_offset(struct thread_data *td, struct fio_file *f,
90 const int ddir = io_u->ddir;
91 unsigned long long b, rb;
94 if (!td->sequential) {
95 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
99 r = os_random_long(&td->random_state);
100 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
103 rb = b + (f->file_offset / td->min_bs[ddir]);
105 } while (!random_map_free(td, f, rb) && loops);
108 * if we failed to retrieve a truly random offset within
109 * the loops assigned, see if there are free ones left at all
111 if (!loops && get_next_free_block(td, f, &b))
114 b = f->last_pos / td->min_bs[ddir];
116 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
117 if (io_u->offset >= f->real_file_size)
123 static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
126 const int ddir = io_u->ddir;
130 if (td->min_bs[ddir] == td->max_bs[ddir])
131 buflen = td->min_bs[ddir];
133 r = os_random_long(&td->bsrange_state);
134 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
135 if (!td->bs_unaligned)
136 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
139 while (buflen + io_u->offset > f->real_file_size) {
140 if (buflen == td->min_bs[ddir])
143 buflen = td->min_bs[ddir];
150 * Return the data direction for the next io_u. If the job is a
151 * mixed read/write workload, check the rwmix cycle and switch if
154 static enum fio_ddir get_rw_ddir(struct thread_data *td)
158 unsigned long elapsed;
160 fio_gettime(&now, NULL);
161 elapsed = mtime_since_now(&td->rwmix_switch);
164 * Check if it's time to seed a new data direction.
166 if (elapsed >= td->rwmixcycle) {
170 r = os_random_long(&td->rwmix_state);
171 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
172 if (v < td->rwmixread)
173 td->rwmix_ddir = DDIR_READ;
175 td->rwmix_ddir = DDIR_WRITE;
176 memcpy(&td->rwmix_switch, &now, sizeof(now));
178 return td->rwmix_ddir;
179 } else if (td_read(td))
185 void put_io_u(struct thread_data *td, struct io_u *io_u)
188 list_del(&io_u->list);
189 list_add(&io_u->list, &td->io_u_freelist);
193 static int fill_io_u(struct thread_data *td, struct fio_file *f,
197 * If using an iolog, grab next piece if any available.
200 return read_iolog_get(td, io_u);
203 * see if it's time to sync
205 if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks)
206 && should_fsync(td)) {
207 io_u->ddir = DDIR_SYNC;
212 io_u->ddir = get_rw_ddir(td);
215 * No log, let the seq/rand engine retrieve the next buflen and
218 if (get_next_offset(td, f, io_u))
221 io_u->buflen = get_next_buflen(td, f, io_u);
226 * mark entry before potentially trimming io_u
228 if (!td->read_iolog && !td->sequential && !td->norandommap)
229 mark_random_map(td, f, io_u);
232 * If using a write iolog, store this entry.
234 if (td->write_iolog_file)
235 write_iolog_put(td, io_u);
241 static void io_u_mark_depth(struct thread_data *td)
245 switch (td->cur_depth) {
262 td->io_u_map[index]++;
266 static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
297 td->io_u_lat[index]++;
300 struct io_u *__get_io_u(struct thread_data *td)
302 struct io_u *io_u = NULL;
304 if (!queue_full(td)) {
305 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
310 list_del(&io_u->list);
311 list_add(&io_u->list, &td->io_u_busylist);
320 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
321 * etc. The returned io_u is fully ready to be prepped and submitted.
323 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
327 io_u = __get_io_u(td);
331 if (td->zone_bytes >= td->zone_size) {
333 f->last_pos += td->zone_skip;
336 if (fill_io_u(td, f, io_u)) {
341 if (io_u->buflen + io_u->offset > f->real_file_size) {
342 if (td->io_ops->flags & FIO_RAWIO) {
347 io_u->buflen = f->real_file_size - io_u->offset;
350 if (io_u->ddir != DDIR_SYNC) {
356 f->last_pos = io_u->offset + io_u->buflen;
358 if (td->verify != VERIFY_NONE)
359 populate_verify_io_u(td, io_u);
363 * Set io data pointers.
365 io_u->xfer_buf = io_u->buf;
366 io_u->xfer_buflen = io_u->buflen;
368 if (td_io_prep(td, io_u)) {
373 fio_gettime(&io_u->start_time, NULL);
377 void io_completed(struct thread_data *td, struct io_u *io_u,
378 struct io_completion_data *icd)
382 if (io_u->ddir == DDIR_SYNC) {
383 td->last_was_sync = 1;
387 td->last_was_sync = 0;
390 unsigned int bytes = io_u->buflen - io_u->resid;
391 const enum fio_ddir idx = io_u->ddir;
393 td->io_blocks[idx]++;
394 td->io_bytes[idx] += bytes;
395 td->zone_bytes += bytes;
396 td->this_io_bytes[idx] += bytes;
398 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
400 msec = mtime_since(&io_u->issue_time, &icd->time);
402 add_clat_sample(td, idx, msec);
403 add_bw_sample(td, idx, &icd->time);
404 io_u_mark_latency(td, msec);
406 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
407 log_io_piece(td, io_u);
409 icd->bytes_done[idx] += bytes;
411 icd->error = io_u->error;
414 void init_icd(struct io_completion_data *icd)
416 fio_gettime(&icd->time, NULL);
419 icd->bytes_done[0] = icd->bytes_done[1] = 0;
422 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
429 for (i = 0; i < icd->nr; i++) {
430 io_u = td->io_ops->event(td, i);
432 io_completed(td, io_u, icd);