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 long long block = io_u->offset / (unsigned long long) td->min_bs;
31 unsigned int blocks = 0;
33 while (blocks < (io_u->buflen / td->min_bs)) {
34 unsigned int idx, bit;
36 if (!random_map_free(td, f, block))
39 idx = RAND_MAP_IDX(td, f, block);
40 bit = RAND_MAP_BIT(td, f, block);
42 assert(idx < f->num_maps);
44 f->file_map[idx] |= (1UL << bit);
49 if ((blocks * td->min_bs) < io_u->buflen)
50 io_u->buflen = blocks * td->min_bs;
54 * Return the next free block in the map.
56 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
57 unsigned long long *b)
63 while ((*b) * td->min_bs < f->file_size) {
64 if (f->file_map[i] != -1UL) {
65 *b += ffz(f->file_map[i]);
77 * For random io, generate a random new block and see if it's used. Repeat
78 * until we find a free one. For sequential io, just return the end of
81 static int get_next_offset(struct thread_data *td, struct fio_file *f,
82 unsigned long long *offset)
84 unsigned long long b, rb;
87 if (!td->sequential) {
88 unsigned long long max_blocks = td->io_size / td->min_bs;
92 r = os_random_long(&td->random_state);
93 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
94 rb = b + (f->file_offset / td->min_bs);
96 } while (!random_map_free(td, f, rb) && loops);
99 if (get_next_free_block(td, f, &b))
103 b = f->last_pos / td->min_bs;
105 *offset = (b * td->min_bs) + f->file_offset;
106 if (*offset > f->file_size)
112 static unsigned int get_next_buflen(struct thread_data *td)
117 if (td->min_bs == td->max_bs)
120 r = os_random_long(&td->bsrange_state);
121 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
122 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
125 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
127 * if using direct/raw io, we may not be able to
128 * shrink the size. so just fail it.
130 if (td->io_ops->flags & FIO_RAWIO)
133 buflen = td->io_size - td->this_io_bytes[td->ddir];
140 * Return the data direction for the next io_u. If the job is a
141 * mixed read/write workload, check the rwmix cycle and switch if
144 static int get_rw_ddir(struct thread_data *td)
148 unsigned long elapsed;
150 gettimeofday(&now, NULL);
151 elapsed = mtime_since_now(&td->rwmix_switch);
154 * Check if it's time to seed a new data direction.
156 if (elapsed >= td->rwmixcycle) {
160 r = os_random_long(&td->rwmix_state);
161 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
162 if (v < td->rwmixread)
163 td->rwmix_ddir = DDIR_READ;
165 td->rwmix_ddir = DDIR_WRITE;
166 memcpy(&td->rwmix_switch, &now, sizeof(now));
168 return td->rwmix_ddir;
169 } else if (td_read(td))
175 void put_io_u(struct thread_data *td, struct io_u *io_u)
178 list_del(&io_u->list);
179 list_add(&io_u->list, &td->io_u_freelist);
183 static int fill_io_u(struct thread_data *td, struct fio_file *f,
187 * If using an iolog, grab next piece if any available.
190 return read_iolog_get(td, io_u);
193 * No log, let the seq/rand engine retrieve the next position.
195 if (!get_next_offset(td, f, &io_u->offset)) {
196 io_u->buflen = get_next_buflen(td);
199 io_u->ddir = get_rw_ddir(td);
202 * If using a write iolog, store this entry.
205 write_iolog_put(td, io_u);
215 struct io_u *__get_io_u(struct thread_data *td)
217 struct io_u *io_u = NULL;
219 if (!queue_full(td)) {
220 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
224 list_del(&io_u->list);
225 list_add(&io_u->list, &td->io_u_busylist);
233 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
234 * etc. The returned io_u is fully ready to be prepped and submitted.
236 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
240 io_u = __get_io_u(td);
244 if (td->zone_bytes >= td->zone_size) {
246 f->last_pos += td->zone_skip;
249 if (fill_io_u(td, f, io_u)) {
254 if (io_u->buflen + io_u->offset > f->file_size) {
255 if (td->io_ops->flags & FIO_RAWIO) {
260 io_u->buflen = f->file_size - io_u->offset;
268 if (!td->read_iolog && !td->sequential)
269 mark_random_map(td, f, io_u);
271 f->last_pos += io_u->buflen;
273 if (td->verify != VERIFY_NONE)
274 populate_verify_io_u(td, io_u);
276 if (td_io_prep(td, io_u)) {
281 gettimeofday(&io_u->start_time, NULL);
285 void io_completed(struct thread_data *td, struct io_u *io_u,
286 struct io_completion_data *icd)
291 gettimeofday(&e, NULL);
294 unsigned int bytes = io_u->buflen - io_u->resid;
295 const int idx = io_u->ddir;
297 td->io_blocks[idx]++;
298 td->io_bytes[idx] += bytes;
299 td->zone_bytes += bytes;
300 td->this_io_bytes[idx] += bytes;
302 msec = mtime_since(&io_u->issue_time, &e);
304 add_clat_sample(td, idx, msec);
305 add_bw_sample(td, idx);
307 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
308 log_io_piece(td, io_u);
310 icd->bytes_done[idx] += bytes;
312 icd->error = io_u->error;
315 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
321 icd->bytes_done[0] = icd->bytes_done[1] = 0;
323 for (i = 0; i < icd->nr; i++) {
324 io_u = td->io_ops->event(td, i);
326 io_completed(td, io_u, icd);