Commit | Line | Data |
---|---|---|
10ba535a JA |
1 | #include <unistd.h> |
2 | #include <fcntl.h> | |
3 | #include <string.h> | |
4 | #include <signal.h> | |
5 | #include <time.h> | |
0c6e7517 | 6 | #include <assert.h> |
10ba535a JA |
7 | |
8 | #include "fio.h" | |
5973cafb | 9 | #include "hash.h" |
4f5af7b2 | 10 | #include "verify.h" |
0d29de83 | 11 | #include "trim.h" |
1fbbf72e | 12 | #include "lib/rand.h" |
7ebd796f | 13 | #include "lib/axmap.h" |
002fe734 | 14 | #include "err.h" |
0f38bbef | 15 | #include "lib/pow2.h" |
4b157ac6 | 16 | #include "minmax.h" |
10ba535a | 17 | |
97601024 JA |
18 | struct io_completion_data { |
19 | int nr; /* input */ | |
97601024 JA |
20 | |
21 | int error; /* output */ | |
100f49f1 | 22 | uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */ |
97601024 JA |
23 | struct timeval time; /* output */ |
24 | }; | |
25 | ||
10ba535a | 26 | /* |
7ebd796f | 27 | * The ->io_axmap contains a map of blocks we have or have not done io |
10ba535a JA |
28 | * to yet. Used to make sure we cover the entire range in a fair fashion. |
29 | */ | |
1ae83d45 | 30 | static int random_map_free(struct fio_file *f, const uint64_t block) |
10ba535a | 31 | { |
7ebd796f | 32 | return !axmap_isset(f->io_axmap, block); |
10ba535a JA |
33 | } |
34 | ||
df415585 JA |
35 | /* |
36 | * Mark a given offset as used in the map. | |
37 | */ | |
9bf2061e | 38 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) |
df415585 | 39 | { |
2dc1bbeb | 40 | unsigned int min_bs = td->o.rw_min_bs; |
9bf2061e | 41 | struct fio_file *f = io_u->file; |
51ede0b1 | 42 | unsigned int nr_blocks; |
1ae83d45 | 43 | uint64_t block; |
df415585 | 44 | |
1ae83d45 | 45 | block = (io_u->offset - f->file_offset) / (uint64_t) min_bs; |
c685b5b2 | 46 | nr_blocks = (io_u->buflen + min_bs - 1) / min_bs; |
df415585 | 47 | |
2ab9e98b | 48 | if (!(io_u->flags & IO_U_F_BUSY_OK)) |
7ebd796f | 49 | nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks); |
df415585 | 50 | |
51ede0b1 JA |
51 | if ((nr_blocks * min_bs) < io_u->buflen) |
52 | io_u->buflen = nr_blocks * min_bs; | |
df415585 JA |
53 | } |
54 | ||
74776733 JA |
55 | static uint64_t last_block(struct thread_data *td, struct fio_file *f, |
56 | enum fio_ddir ddir) | |
2ba1c290 | 57 | { |
74776733 JA |
58 | uint64_t max_blocks; |
59 | uint64_t max_size; | |
2ba1c290 | 60 | |
ff58fced JA |
61 | assert(ddir_rw(ddir)); |
62 | ||
d9dd70f7 JA |
63 | /* |
64 | * Hmm, should we make sure that ->io_size <= ->real_file_size? | |
65 | */ | |
66 | max_size = f->io_size; | |
67 | if (max_size > f->real_file_size) | |
68 | max_size = f->real_file_size; | |
69 | ||
ed335855 SN |
70 | if (td->o.zone_range) |
71 | max_size = td->o.zone_range; | |
72 | ||
0412d12e JE |
73 | if (td->o.min_bs[ddir] > td->o.ba[ddir]) |
74 | max_size -= td->o.min_bs[ddir] - td->o.ba[ddir]; | |
75 | ||
1ae83d45 | 76 | max_blocks = max_size / (uint64_t) td->o.ba[ddir]; |
2ba1c290 JA |
77 | if (!max_blocks) |
78 | return 0; | |
79 | ||
67778e88 | 80 | return max_blocks; |
2ba1c290 JA |
81 | } |
82 | ||
1ae83d45 JA |
83 | struct rand_off { |
84 | struct flist_head list; | |
85 | uint64_t off; | |
86 | }; | |
87 | ||
e25839d4 | 88 | static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f, |
1ae83d45 | 89 | enum fio_ddir ddir, uint64_t *b) |
ec4015da | 90 | { |
6f49f8bc | 91 | uint64_t r; |
5e0baa7f | 92 | |
c3546b53 JA |
93 | if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE || |
94 | td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) { | |
95 | uint64_t frand_max, lastb; | |
6f49f8bc JA |
96 | |
97 | lastb = last_block(td, f, ddir); | |
98 | if (!lastb) | |
99 | return 1; | |
100 | ||
c3546b53 | 101 | frand_max = rand_max(&td->random_state); |
d6b72507 | 102 | r = __rand(&td->random_state); |
8055e41d | 103 | |
4b91ee8f | 104 | dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r); |
8055e41d | 105 | |
c3546b53 | 106 | *b = lastb * (r / ((uint64_t) frand_max + 1.0)); |
51ede0b1 | 107 | } else { |
8055e41d | 108 | uint64_t off = 0; |
43c63a78 | 109 | |
967d1b63 JA |
110 | assert(fio_file_lfsr(f)); |
111 | ||
6f49f8bc | 112 | if (lfsr_next(&f->lfsr, &off)) |
8055e41d | 113 | return 1; |
ec4015da | 114 | |
8055e41d JA |
115 | *b = off; |
116 | } | |
0ce8b119 | 117 | |
ec4015da | 118 | /* |
51ede0b1 | 119 | * if we are not maintaining a random map, we are done. |
ec4015da | 120 | */ |
51ede0b1 JA |
121 | if (!file_randommap(td, f)) |
122 | goto ret; | |
43c63a78 JA |
123 | |
124 | /* | |
51ede0b1 | 125 | * calculate map offset and check if it's free |
43c63a78 | 126 | */ |
51ede0b1 JA |
127 | if (random_map_free(f, *b)) |
128 | goto ret; | |
129 | ||
4b91ee8f JA |
130 | dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n", |
131 | (unsigned long long) *b); | |
51ede0b1 | 132 | |
7ebd796f | 133 | *b = axmap_next_free(f->io_axmap, *b); |
51ede0b1 JA |
134 | if (*b == (uint64_t) -1ULL) |
135 | return 1; | |
0ce8b119 JA |
136 | ret: |
137 | return 0; | |
ec4015da JA |
138 | } |
139 | ||
925fee33 JA |
140 | static int __get_next_rand_offset_zipf(struct thread_data *td, |
141 | struct fio_file *f, enum fio_ddir ddir, | |
1ae83d45 | 142 | uint64_t *b) |
e25839d4 | 143 | { |
9c6f6316 | 144 | *b = zipf_next(&f->zipf); |
e25839d4 JA |
145 | return 0; |
146 | } | |
147 | ||
925fee33 JA |
148 | static int __get_next_rand_offset_pareto(struct thread_data *td, |
149 | struct fio_file *f, enum fio_ddir ddir, | |
1ae83d45 | 150 | uint64_t *b) |
925fee33 | 151 | { |
9c6f6316 | 152 | *b = pareto_next(&f->zipf); |
925fee33 JA |
153 | return 0; |
154 | } | |
155 | ||
56d9fa4b JA |
156 | static int __get_next_rand_offset_gauss(struct thread_data *td, |
157 | struct fio_file *f, enum fio_ddir ddir, | |
158 | uint64_t *b) | |
159 | { | |
160 | *b = gauss_next(&f->gauss); | |
161 | return 0; | |
162 | } | |
163 | ||
164 | ||
1ae83d45 JA |
165 | static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b) |
166 | { | |
167 | struct rand_off *r1 = flist_entry(a, struct rand_off, list); | |
168 | struct rand_off *r2 = flist_entry(b, struct rand_off, list); | |
169 | ||
170 | return r1->off - r2->off; | |
171 | } | |
172 | ||
173 | static int get_off_from_method(struct thread_data *td, struct fio_file *f, | |
174 | enum fio_ddir ddir, uint64_t *b) | |
e25839d4 JA |
175 | { |
176 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) | |
177 | return __get_next_rand_offset(td, f, ddir, b); | |
178 | else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) | |
179 | return __get_next_rand_offset_zipf(td, f, ddir, b); | |
925fee33 JA |
180 | else if (td->o.random_distribution == FIO_RAND_DIST_PARETO) |
181 | return __get_next_rand_offset_pareto(td, f, ddir, b); | |
56d9fa4b JA |
182 | else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS) |
183 | return __get_next_rand_offset_gauss(td, f, ddir, b); | |
e25839d4 JA |
184 | |
185 | log_err("fio: unknown random distribution: %d\n", td->o.random_distribution); | |
186 | return 1; | |
187 | } | |
188 | ||
bcd5abfa JA |
189 | /* |
190 | * Sort the reads for a verify phase in batches of verifysort_nr, if | |
191 | * specified. | |
192 | */ | |
193 | static inline int should_sort_io(struct thread_data *td) | |
194 | { | |
195 | if (!td->o.verifysort_nr || !td->o.do_verify) | |
196 | return 0; | |
197 | if (!td_random(td)) | |
198 | return 0; | |
199 | if (td->runstate != TD_VERIFYING) | |
200 | return 0; | |
c3546b53 JA |
201 | if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE || |
202 | td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) | |
bcd5abfa JA |
203 | return 0; |
204 | ||
205 | return 1; | |
206 | } | |
207 | ||
d9472271 | 208 | static int should_do_random(struct thread_data *td, enum fio_ddir ddir) |
211c9b89 | 209 | { |
c3546b53 | 210 | uint64_t frand_max; |
211c9b89 JA |
211 | unsigned int v; |
212 | unsigned long r; | |
213 | ||
d9472271 | 214 | if (td->o.perc_rand[ddir] == 100) |
211c9b89 JA |
215 | return 1; |
216 | ||
c3546b53 | 217 | frand_max = rand_max(&td->seq_rand_state[ddir]); |
d6b72507 | 218 | r = __rand(&td->seq_rand_state[ddir]); |
c3546b53 | 219 | v = 1 + (int) (100.0 * (r / (frand_max + 1.0))); |
211c9b89 | 220 | |
d9472271 | 221 | return v <= td->o.perc_rand[ddir]; |
211c9b89 JA |
222 | } |
223 | ||
1ae83d45 JA |
224 | static int get_next_rand_offset(struct thread_data *td, struct fio_file *f, |
225 | enum fio_ddir ddir, uint64_t *b) | |
226 | { | |
227 | struct rand_off *r; | |
228 | int i, ret = 1; | |
229 | ||
bcd5abfa | 230 | if (!should_sort_io(td)) |
1ae83d45 JA |
231 | return get_off_from_method(td, f, ddir, b); |
232 | ||
233 | if (!flist_empty(&td->next_rand_list)) { | |
1ae83d45 | 234 | fetch: |
9342d5f8 | 235 | r = flist_first_entry(&td->next_rand_list, struct rand_off, list); |
1ae83d45 JA |
236 | flist_del(&r->list); |
237 | *b = r->off; | |
238 | free(r); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | for (i = 0; i < td->o.verifysort_nr; i++) { | |
243 | r = malloc(sizeof(*r)); | |
244 | ||
245 | ret = get_off_from_method(td, f, ddir, &r->off); | |
246 | if (ret) { | |
247 | free(r); | |
248 | break; | |
249 | } | |
250 | ||
251 | flist_add(&r->list, &td->next_rand_list); | |
252 | } | |
253 | ||
254 | if (ret && !i) | |
255 | return ret; | |
256 | ||
257 | assert(!flist_empty(&td->next_rand_list)); | |
258 | flist_sort(NULL, &td->next_rand_list, flist_cmp); | |
259 | goto fetch; | |
260 | } | |
261 | ||
38dad62d | 262 | static int get_next_rand_block(struct thread_data *td, struct fio_file *f, |
1ae83d45 | 263 | enum fio_ddir ddir, uint64_t *b) |
38dad62d | 264 | { |
c04e4661 DE |
265 | if (!get_next_rand_offset(td, f, ddir, b)) |
266 | return 0; | |
267 | ||
268 | if (td->o.time_based) { | |
33c48814 | 269 | fio_file_reset(td, f); |
c04e4661 DE |
270 | if (!get_next_rand_offset(td, f, ddir, b)) |
271 | return 0; | |
38dad62d JA |
272 | } |
273 | ||
c04e4661 | 274 | dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n", |
f1dfb668 | 275 | f->file_name, (unsigned long long) f->last_pos[ddir], |
4b91ee8f | 276 | (unsigned long long) f->real_file_size); |
c04e4661 | 277 | return 1; |
38dad62d JA |
278 | } |
279 | ||
37cf9e3c | 280 | static int get_next_seq_offset(struct thread_data *td, struct fio_file *f, |
1ae83d45 | 281 | enum fio_ddir ddir, uint64_t *offset) |
38dad62d | 282 | { |
ac002339 JA |
283 | struct thread_options *o = &td->o; |
284 | ||
ff58fced JA |
285 | assert(ddir_rw(ddir)); |
286 | ||
f1dfb668 | 287 | if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) && |
ac002339 | 288 | o->time_based) |
f1dfb668 | 289 | f->last_pos[ddir] = f->last_pos[ddir] - f->io_size; |
c04e4661 | 290 | |
f1dfb668 | 291 | if (f->last_pos[ddir] < f->real_file_size) { |
1ae83d45 | 292 | uint64_t pos; |
059b0802 | 293 | |
f1dfb668 JA |
294 | if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0) |
295 | f->last_pos[ddir] = f->real_file_size; | |
a66da7a2 | 296 | |
f1dfb668 | 297 | pos = f->last_pos[ddir] - f->file_offset; |
ac002339 JA |
298 | if (pos && o->ddir_seq_add) { |
299 | pos += o->ddir_seq_add; | |
300 | ||
301 | /* | |
302 | * If we reach beyond the end of the file | |
303 | * with holed IO, wrap around to the | |
304 | * beginning again. | |
305 | */ | |
306 | if (pos >= f->real_file_size) | |
307 | pos = f->file_offset; | |
308 | } | |
059b0802 | 309 | |
37cf9e3c | 310 | *offset = pos; |
38dad62d JA |
311 | return 0; |
312 | } | |
313 | ||
314 | return 1; | |
315 | } | |
316 | ||
317 | static int get_next_block(struct thread_data *td, struct io_u *io_u, | |
6aca9b3d JA |
318 | enum fio_ddir ddir, int rw_seq, |
319 | unsigned int *is_random) | |
38dad62d JA |
320 | { |
321 | struct fio_file *f = io_u->file; | |
1ae83d45 | 322 | uint64_t b, offset; |
38dad62d JA |
323 | int ret; |
324 | ||
ff58fced JA |
325 | assert(ddir_rw(ddir)); |
326 | ||
37cf9e3c JA |
327 | b = offset = -1ULL; |
328 | ||
38dad62d | 329 | if (rw_seq) { |
211c9b89 | 330 | if (td_random(td)) { |
6aca9b3d | 331 | if (should_do_random(td, ddir)) { |
211c9b89 | 332 | ret = get_next_rand_block(td, f, ddir, &b); |
6aca9b3d JA |
333 | *is_random = 1; |
334 | } else { | |
335 | *is_random = 0; | |
a9da8ab2 | 336 | io_u_set(io_u, IO_U_F_BUSY_OK); |
211c9b89 JA |
337 | ret = get_next_seq_offset(td, f, ddir, &offset); |
338 | if (ret) | |
339 | ret = get_next_rand_block(td, f, ddir, &b); | |
340 | } | |
6aca9b3d JA |
341 | } else { |
342 | *is_random = 0; | |
37cf9e3c | 343 | ret = get_next_seq_offset(td, f, ddir, &offset); |
6aca9b3d | 344 | } |
38dad62d | 345 | } else { |
a9da8ab2 | 346 | io_u_set(io_u, IO_U_F_BUSY_OK); |
6aca9b3d | 347 | *is_random = 0; |
38dad62d JA |
348 | |
349 | if (td->o.rw_seq == RW_SEQ_SEQ) { | |
37cf9e3c | 350 | ret = get_next_seq_offset(td, f, ddir, &offset); |
6aca9b3d | 351 | if (ret) { |
37cf9e3c | 352 | ret = get_next_rand_block(td, f, ddir, &b); |
6aca9b3d JA |
353 | *is_random = 0; |
354 | } | |
38dad62d | 355 | } else if (td->o.rw_seq == RW_SEQ_IDENT) { |
f1dfb668 JA |
356 | if (f->last_start[ddir] != -1ULL) |
357 | offset = f->last_start[ddir] - f->file_offset; | |
38dad62d | 358 | else |
37cf9e3c | 359 | offset = 0; |
38dad62d JA |
360 | ret = 0; |
361 | } else { | |
362 | log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq); | |
363 | ret = 1; | |
364 | } | |
365 | } | |
6d68b997 | 366 | |
37cf9e3c JA |
367 | if (!ret) { |
368 | if (offset != -1ULL) | |
369 | io_u->offset = offset; | |
370 | else if (b != -1ULL) | |
371 | io_u->offset = b * td->o.ba[ddir]; | |
372 | else { | |
4e0a8fa2 | 373 | log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b); |
37cf9e3c JA |
374 | ret = 1; |
375 | } | |
376 | } | |
377 | ||
38dad62d JA |
378 | return ret; |
379 | } | |
380 | ||
10ba535a JA |
381 | /* |
382 | * For random io, generate a random new block and see if it's used. Repeat | |
383 | * until we find a free one. For sequential io, just return the end of | |
384 | * the last io issued. | |
385 | */ | |
6aca9b3d JA |
386 | static int __get_next_offset(struct thread_data *td, struct io_u *io_u, |
387 | unsigned int *is_random) | |
10ba535a | 388 | { |
9bf2061e | 389 | struct fio_file *f = io_u->file; |
4ba66134 | 390 | enum fio_ddir ddir = io_u->ddir; |
38dad62d | 391 | int rw_seq_hit = 0; |
10ba535a | 392 | |
ff58fced JA |
393 | assert(ddir_rw(ddir)); |
394 | ||
38dad62d JA |
395 | if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) { |
396 | rw_seq_hit = 1; | |
5736c10d | 397 | td->ddir_seq_nr = td->o.ddir_seq_nr; |
38dad62d | 398 | } |
211097b2 | 399 | |
6aca9b3d | 400 | if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random)) |
38dad62d | 401 | return 1; |
10ba535a | 402 | |
009bd847 JA |
403 | if (io_u->offset >= f->io_size) { |
404 | dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n", | |
4b91ee8f JA |
405 | (unsigned long long) io_u->offset, |
406 | (unsigned long long) f->io_size); | |
009bd847 JA |
407 | return 1; |
408 | } | |
409 | ||
410 | io_u->offset += f->file_offset; | |
2ba1c290 JA |
411 | if (io_u->offset >= f->real_file_size) { |
412 | dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n", | |
4b91ee8f JA |
413 | (unsigned long long) io_u->offset, |
414 | (unsigned long long) f->real_file_size); | |
10ba535a | 415 | return 1; |
2ba1c290 | 416 | } |
10ba535a JA |
417 | |
418 | return 0; | |
419 | } | |
420 | ||
6aca9b3d JA |
421 | static int get_next_offset(struct thread_data *td, struct io_u *io_u, |
422 | unsigned int *is_random) | |
15dc1934 | 423 | { |
d72be545 JA |
424 | if (td->flags & TD_F_PROFILE_OPS) { |
425 | struct prof_io_ops *ops = &td->prof_io_ops; | |
7eb36574 | 426 | |
d72be545 | 427 | if (ops->fill_io_u_off) |
6aca9b3d | 428 | return ops->fill_io_u_off(td, io_u, is_random); |
d72be545 | 429 | } |
15dc1934 | 430 | |
6aca9b3d | 431 | return __get_next_offset(td, io_u, is_random); |
15dc1934 JA |
432 | } |
433 | ||
79944128 JA |
434 | static inline int io_u_fits(struct thread_data *td, struct io_u *io_u, |
435 | unsigned int buflen) | |
436 | { | |
437 | struct fio_file *f = io_u->file; | |
438 | ||
bedc9dc2 | 439 | return io_u->offset + buflen <= f->io_size + get_start_offset(td, f); |
79944128 JA |
440 | } |
441 | ||
6aca9b3d JA |
442 | static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u, |
443 | unsigned int is_random) | |
10ba535a | 444 | { |
6aca9b3d | 445 | int ddir = io_u->ddir; |
24d23ca7 | 446 | unsigned int buflen = 0; |
f3059de1 | 447 | unsigned int minbs, maxbs; |
c3546b53 | 448 | uint64_t frand_max; |
54a21917 | 449 | unsigned long r; |
10ba535a | 450 | |
9ee1c647 | 451 | assert(ddir_rw(ddir)); |
6aca9b3d JA |
452 | |
453 | if (td->o.bs_is_seq_rand) | |
454 | ddir = is_random ? DDIR_WRITE: DDIR_READ; | |
ff58fced | 455 | |
f3059de1 JA |
456 | minbs = td->o.min_bs[ddir]; |
457 | maxbs = td->o.max_bs[ddir]; | |
458 | ||
79944128 JA |
459 | if (minbs == maxbs) |
460 | return minbs; | |
461 | ||
52c58027 JA |
462 | /* |
463 | * If we can't satisfy the min block size from here, then fail | |
464 | */ | |
465 | if (!io_u_fits(td, io_u, minbs)) | |
466 | return 0; | |
467 | ||
c3546b53 | 468 | frand_max = rand_max(&td->bsrange_state); |
79944128 | 469 | do { |
d6b72507 | 470 | r = __rand(&td->bsrange_state); |
4c07ad86 | 471 | |
720e84ad | 472 | if (!td->o.bssplit_nr[ddir]) { |
f3059de1 | 473 | buflen = 1 + (unsigned int) ((double) maxbs * |
c3546b53 | 474 | (r / (frand_max + 1.0))); |
f3059de1 JA |
475 | if (buflen < minbs) |
476 | buflen = minbs; | |
5ec10eaa | 477 | } else { |
564ca972 JA |
478 | long perc = 0; |
479 | unsigned int i; | |
480 | ||
720e84ad JA |
481 | for (i = 0; i < td->o.bssplit_nr[ddir]; i++) { |
482 | struct bssplit *bsp = &td->o.bssplit[ddir][i]; | |
564ca972 JA |
483 | |
484 | buflen = bsp->bs; | |
485 | perc += bsp->perc; | |
c3546b53 | 486 | if ((r <= ((frand_max / 100L) * perc)) && |
79944128 | 487 | io_u_fits(td, io_u, buflen)) |
564ca972 JA |
488 | break; |
489 | } | |
490 | } | |
79944128 | 491 | |
ced5db25 | 492 | if (td->o.verify != VERIFY_NONE) |
a9f70b1f JB |
493 | buflen = (buflen + td->o.verify_interval - 1) & |
494 | ~(td->o.verify_interval - 1); | |
495 | ||
f3059de1 | 496 | if (!td->o.bs_unaligned && is_power_of_2(minbs)) |
7c306bb1 | 497 | buflen &= ~(minbs - 1); |
10ba535a | 498 | |
79944128 | 499 | } while (!io_u_fits(td, io_u, buflen)); |
6a5e6884 | 500 | |
10ba535a JA |
501 | return buflen; |
502 | } | |
503 | ||
6aca9b3d JA |
504 | static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u, |
505 | unsigned int is_random) | |
15dc1934 | 506 | { |
d72be545 JA |
507 | if (td->flags & TD_F_PROFILE_OPS) { |
508 | struct prof_io_ops *ops = &td->prof_io_ops; | |
7eb36574 | 509 | |
d72be545 | 510 | if (ops->fill_io_u_size) |
6aca9b3d | 511 | return ops->fill_io_u_size(td, io_u, is_random); |
d72be545 | 512 | } |
15dc1934 | 513 | |
6aca9b3d | 514 | return __get_next_buflen(td, io_u, is_random); |
15dc1934 JA |
515 | } |
516 | ||
afe24a5a JA |
517 | static void set_rwmix_bytes(struct thread_data *td) |
518 | { | |
afe24a5a JA |
519 | unsigned int diff; |
520 | ||
521 | /* | |
522 | * we do time or byte based switch. this is needed because | |
523 | * buffered writes may issue a lot quicker than they complete, | |
524 | * whereas reads do not. | |
525 | */ | |
e47f799f | 526 | diff = td->o.rwmix[td->rwmix_ddir ^ 1]; |
04c540d9 | 527 | td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100; |
e47f799f JA |
528 | } |
529 | ||
530 | static inline enum fio_ddir get_rand_ddir(struct thread_data *td) | |
531 | { | |
c3546b53 | 532 | uint64_t frand_max = rand_max(&td->rwmix_state); |
e47f799f | 533 | unsigned int v; |
1294c3ec | 534 | unsigned long r; |
e47f799f | 535 | |
d6b72507 | 536 | r = __rand(&td->rwmix_state); |
c3546b53 | 537 | v = 1 + (int) (100.0 * (r / (frand_max + 1.0))); |
4c07ad86 | 538 | |
04c540d9 | 539 | if (v <= td->o.rwmix[DDIR_READ]) |
e47f799f JA |
540 | return DDIR_READ; |
541 | ||
542 | return DDIR_WRITE; | |
afe24a5a JA |
543 | } |
544 | ||
002e7183 JA |
545 | void io_u_quiesce(struct thread_data *td) |
546 | { | |
547 | /* | |
548 | * We are going to sleep, ensure that we flush anything pending as | |
549 | * not to skew our latency numbers. | |
550 | * | |
551 | * Changed to only monitor 'in flight' requests here instead of the | |
552 | * td->cur_depth, b/c td->cur_depth does not accurately represent | |
553 | * io's that have been actually submitted to an async engine, | |
554 | * and cur_depth is meaningless for sync engines. | |
555 | */ | |
9cc80b6d JA |
556 | if (td->io_u_queued || td->cur_depth) { |
557 | int fio_unused ret; | |
558 | ||
559 | ret = td_io_commit(td); | |
560 | } | |
561 | ||
002e7183 JA |
562 | while (td->io_u_in_flight) { |
563 | int fio_unused ret; | |
564 | ||
55312f9f | 565 | ret = io_u_queued_complete(td, 1); |
002e7183 JA |
566 | } |
567 | } | |
568 | ||
581e7141 JA |
569 | static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir) |
570 | { | |
571 | enum fio_ddir odir = ddir ^ 1; | |
50a8ce86 | 572 | long usec, now; |
581e7141 | 573 | |
ff58fced | 574 | assert(ddir_rw(ddir)); |
50a8ce86 | 575 | now = utime_since_now(&td->start); |
ff58fced | 576 | |
50a8ce86 D |
577 | /* |
578 | * if rate_next_io_time is in the past, need to catch up to rate | |
579 | */ | |
580 | if (td->rate_next_io_time[ddir] <= now) | |
581e7141 JA |
581 | return ddir; |
582 | ||
583 | /* | |
50a8ce86 | 584 | * We are ahead of rate in this direction. See if we |
581e7141 JA |
585 | * should switch. |
586 | */ | |
315fcfec | 587 | if (td_rw(td) && td->o.rwmix[odir]) { |
581e7141 | 588 | /* |
50a8ce86 | 589 | * Other direction is behind rate, switch |
581e7141 | 590 | */ |
50a8ce86 | 591 | if (td->rate_next_io_time[odir] <= now) |
581e7141 JA |
592 | return odir; |
593 | ||
594 | /* | |
50a8ce86 D |
595 | * Both directions are ahead of rate. sleep the min |
596 | * switch if necissary | |
581e7141 | 597 | */ |
50a8ce86 D |
598 | if (td->rate_next_io_time[ddir] <= |
599 | td->rate_next_io_time[odir]) { | |
600 | usec = td->rate_next_io_time[ddir] - now; | |
581e7141 | 601 | } else { |
50a8ce86 | 602 | usec = td->rate_next_io_time[odir] - now; |
581e7141 JA |
603 | ddir = odir; |
604 | } | |
605 | } else | |
50a8ce86 | 606 | usec = td->rate_next_io_time[ddir] - now; |
581e7141 | 607 | |
a9da8ab2 JA |
608 | if (td->o.io_submit_mode == IO_MODE_INLINE) |
609 | io_u_quiesce(td); | |
78c1eda5 | 610 | |
9cc80b6d | 611 | usec = usec_sleep(td, usec); |
581e7141 | 612 | |
581e7141 JA |
613 | return ddir; |
614 | } | |
615 | ||
10ba535a JA |
616 | /* |
617 | * Return the data direction for the next io_u. If the job is a | |
618 | * mixed read/write workload, check the rwmix cycle and switch if | |
619 | * necessary. | |
620 | */ | |
1e97cce9 | 621 | static enum fio_ddir get_rw_ddir(struct thread_data *td) |
10ba535a | 622 | { |
581e7141 JA |
623 | enum fio_ddir ddir; |
624 | ||
5f9099ea JA |
625 | /* |
626 | * see if it's time to fsync | |
627 | */ | |
628 | if (td->o.fsync_blocks && | |
629 | !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) && | |
630 | td->io_issues[DDIR_WRITE] && should_fsync(td)) | |
631 | return DDIR_SYNC; | |
632 | ||
633 | /* | |
634 | * see if it's time to fdatasync | |
635 | */ | |
636 | if (td->o.fdatasync_blocks && | |
637 | !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) && | |
638 | td->io_issues[DDIR_WRITE] && should_fsync(td)) | |
639 | return DDIR_DATASYNC; | |
640 | ||
44f29692 JA |
641 | /* |
642 | * see if it's time to sync_file_range | |
643 | */ | |
644 | if (td->sync_file_range_nr && | |
645 | !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) && | |
646 | td->io_issues[DDIR_WRITE] && should_fsync(td)) | |
647 | return DDIR_SYNC_FILE_RANGE; | |
648 | ||
10ba535a | 649 | if (td_rw(td)) { |
10ba535a JA |
650 | /* |
651 | * Check if it's time to seed a new data direction. | |
652 | */ | |
e4928662 | 653 | if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) { |
e47f799f JA |
654 | /* |
655 | * Put a top limit on how many bytes we do for | |
656 | * one data direction, to avoid overflowing the | |
657 | * ranges too much | |
658 | */ | |
659 | ddir = get_rand_ddir(td); | |
e47f799f JA |
660 | |
661 | if (ddir != td->rwmix_ddir) | |
662 | set_rwmix_bytes(td); | |
663 | ||
664 | td->rwmix_ddir = ddir; | |
10ba535a | 665 | } |
581e7141 | 666 | ddir = td->rwmix_ddir; |
10ba535a | 667 | } else if (td_read(td)) |
581e7141 | 668 | ddir = DDIR_READ; |
6eaf09d6 | 669 | else if (td_write(td)) |
581e7141 | 670 | ddir = DDIR_WRITE; |
6eaf09d6 SL |
671 | else |
672 | ddir = DDIR_TRIM; | |
581e7141 JA |
673 | |
674 | td->rwmix_ddir = rate_ddir(td, ddir); | |
675 | return td->rwmix_ddir; | |
10ba535a JA |
676 | } |
677 | ||
1ef2b6be JA |
678 | static void set_rw_ddir(struct thread_data *td, struct io_u *io_u) |
679 | { | |
0e4dd95c DE |
680 | enum fio_ddir ddir = get_rw_ddir(td); |
681 | ||
82a90686 | 682 | if (td_trimwrite(td)) { |
0e4dd95c DE |
683 | struct fio_file *f = io_u->file; |
684 | if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM]) | |
685 | ddir = DDIR_TRIM; | |
686 | else | |
687 | ddir = DDIR_WRITE; | |
688 | } | |
689 | ||
690 | io_u->ddir = io_u->acct_ddir = ddir; | |
1ef2b6be JA |
691 | |
692 | if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) && | |
693 | td->o.barrier_blocks && | |
694 | !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) && | |
695 | td->io_issues[DDIR_WRITE]) | |
a9da8ab2 | 696 | io_u_set(io_u, IO_U_F_BARRIER); |
1ef2b6be JA |
697 | } |
698 | ||
e8462bd8 | 699 | void put_file_log(struct thread_data *td, struct fio_file *f) |
60f2c658 | 700 | { |
71b84caa | 701 | unsigned int ret = put_file(td, f); |
60f2c658 JA |
702 | |
703 | if (ret) | |
704 | td_verror(td, ret, "file close"); | |
705 | } | |
706 | ||
10ba535a JA |
707 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
708 | { | |
a9da8ab2 JA |
709 | if (td->parent) |
710 | td = td->parent; | |
711 | ||
e8462bd8 JA |
712 | td_io_u_lock(td); |
713 | ||
f8b0bd10 | 714 | if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT)) |
60f2c658 | 715 | put_file_log(td, io_u->file); |
f8b0bd10 | 716 | |
10ba535a | 717 | io_u->file = NULL; |
a9da8ab2 | 718 | io_u_set(io_u, IO_U_F_FREE); |
d7ee2a7d | 719 | |
a9da8ab2 | 720 | if (io_u->flags & IO_U_F_IN_CUR_DEPTH) { |
0c41214f | 721 | td->cur_depth--; |
a9da8ab2 JA |
722 | assert(!(td->flags & TD_F_CHILD)); |
723 | } | |
2ae0b204 | 724 | io_u_qpush(&td->io_u_freelist, io_u); |
e8462bd8 JA |
725 | td_io_u_unlock(td); |
726 | td_io_u_free_notify(td); | |
10ba535a JA |
727 | } |
728 | ||
f2bba182 RR |
729 | void clear_io_u(struct thread_data *td, struct io_u *io_u) |
730 | { | |
a9da8ab2 | 731 | io_u_clear(io_u, IO_U_F_FLIGHT); |
f2bba182 RR |
732 | put_io_u(td, io_u); |
733 | } | |
734 | ||
755200a3 JA |
735 | void requeue_io_u(struct thread_data *td, struct io_u **io_u) |
736 | { | |
737 | struct io_u *__io_u = *io_u; | |
bcd5abfa | 738 | enum fio_ddir ddir = acct_ddir(__io_u); |
755200a3 | 739 | |
465221b0 JA |
740 | dprint(FD_IO, "requeue %p\n", __io_u); |
741 | ||
a9da8ab2 JA |
742 | if (td->parent) |
743 | td = td->parent; | |
744 | ||
e8462bd8 JA |
745 | td_io_u_lock(td); |
746 | ||
a9da8ab2 | 747 | io_u_set(__io_u, IO_U_F_FREE); |
bcd5abfa JA |
748 | if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir)) |
749 | td->io_issues[ddir]--; | |
5ec10eaa | 750 | |
a9da8ab2 JA |
751 | io_u_clear(__io_u, IO_U_F_FLIGHT); |
752 | if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) { | |
0c41214f | 753 | td->cur_depth--; |
a9da8ab2 JA |
754 | assert(!(td->flags & TD_F_CHILD)); |
755 | } | |
2ae0b204 JA |
756 | |
757 | io_u_rpush(&td->io_u_requeues, __io_u); | |
e8462bd8 | 758 | td_io_u_unlock(td); |
a9da8ab2 | 759 | td_io_u_free_notify(td); |
755200a3 JA |
760 | *io_u = NULL; |
761 | } | |
762 | ||
9bf2061e | 763 | static int fill_io_u(struct thread_data *td, struct io_u *io_u) |
10ba535a | 764 | { |
6aca9b3d JA |
765 | unsigned int is_random; |
766 | ||
b4c5e1ac JA |
767 | if (td->io_ops->flags & FIO_NOIO) |
768 | goto out; | |
769 | ||
1ef2b6be | 770 | set_rw_ddir(td, io_u); |
5f9099ea | 771 | |
87dc1ab1 | 772 | /* |
ff58fced | 773 | * fsync() or fdatasync() or trim etc, we are done |
87dc1ab1 | 774 | */ |
ff58fced | 775 | if (!ddir_rw(io_u->ddir)) |
c38e9468 | 776 | goto out; |
a00735e6 | 777 | |
48f5abd3 JA |
778 | /* |
779 | * See if it's time to switch to a new zone | |
780 | */ | |
13af05ae | 781 | if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) { |
4c8be5b1 JA |
782 | struct fio_file *f = io_u->file; |
783 | ||
48f5abd3 | 784 | td->zone_bytes = 0; |
4c8be5b1 JA |
785 | f->file_offset += td->o.zone_range + td->o.zone_skip; |
786 | ||
787 | /* | |
788 | * Wrap from the beginning, if we exceed the file size | |
789 | */ | |
790 | if (f->file_offset >= f->real_file_size) | |
791 | f->file_offset = f->real_file_size - f->file_offset; | |
f1dfb668 | 792 | f->last_pos[io_u->ddir] = f->file_offset; |
48f5abd3 JA |
793 | td->io_skip_bytes += td->o.zone_skip; |
794 | } | |
795 | ||
10ba535a | 796 | /* |
c685b5b2 JA |
797 | * No log, let the seq/rand engine retrieve the next buflen and |
798 | * position. | |
10ba535a | 799 | */ |
6aca9b3d | 800 | if (get_next_offset(td, io_u, &is_random)) { |
2ba1c290 | 801 | dprint(FD_IO, "io_u %p, failed getting offset\n", io_u); |
bca4ed4d | 802 | return 1; |
2ba1c290 | 803 | } |
10ba535a | 804 | |
6aca9b3d | 805 | io_u->buflen = get_next_buflen(td, io_u, is_random); |
2ba1c290 JA |
806 | if (!io_u->buflen) { |
807 | dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u); | |
bca4ed4d | 808 | return 1; |
2ba1c290 | 809 | } |
bca4ed4d | 810 | |
2ba1c290 JA |
811 | if (io_u->offset + io_u->buflen > io_u->file->real_file_size) { |
812 | dprint(FD_IO, "io_u %p, offset too large\n", io_u); | |
4b91ee8f JA |
813 | dprint(FD_IO, " off=%llu/%lu > %llu\n", |
814 | (unsigned long long) io_u->offset, io_u->buflen, | |
815 | (unsigned long long) io_u->file->real_file_size); | |
6a5e6884 | 816 | return 1; |
2ba1c290 | 817 | } |
6a5e6884 | 818 | |
bca4ed4d JA |
819 | /* |
820 | * mark entry before potentially trimming io_u | |
821 | */ | |
303032ae | 822 | if (td_random(td) && file_randommap(td, io_u->file)) |
9bf2061e | 823 | mark_random_map(td, io_u); |
bca4ed4d | 824 | |
c38e9468 | 825 | out: |
2ba1c290 | 826 | dprint_io_u(io_u, "fill_io_u"); |
d9d91e39 | 827 | td->zone_bytes += io_u->buflen; |
bca4ed4d | 828 | return 0; |
10ba535a JA |
829 | } |
830 | ||
838bc709 JA |
831 | static void __io_u_mark_map(unsigned int *map, unsigned int nr) |
832 | { | |
2b13e716 | 833 | int idx = 0; |
838bc709 JA |
834 | |
835 | switch (nr) { | |
836 | default: | |
2b13e716 | 837 | idx = 6; |
838bc709 JA |
838 | break; |
839 | case 33 ... 64: | |
2b13e716 | 840 | idx = 5; |
838bc709 JA |
841 | break; |
842 | case 17 ... 32: | |
2b13e716 | 843 | idx = 4; |
838bc709 JA |
844 | break; |
845 | case 9 ... 16: | |
2b13e716 | 846 | idx = 3; |
838bc709 JA |
847 | break; |
848 | case 5 ... 8: | |
2b13e716 | 849 | idx = 2; |
838bc709 JA |
850 | break; |
851 | case 1 ... 4: | |
2b13e716 | 852 | idx = 1; |
838bc709 JA |
853 | case 0: |
854 | break; | |
855 | } | |
856 | ||
2b13e716 | 857 | map[idx]++; |
838bc709 JA |
858 | } |
859 | ||
860 | void io_u_mark_submit(struct thread_data *td, unsigned int nr) | |
861 | { | |
862 | __io_u_mark_map(td->ts.io_u_submit, nr); | |
863 | td->ts.total_submit++; | |
864 | } | |
865 | ||
866 | void io_u_mark_complete(struct thread_data *td, unsigned int nr) | |
867 | { | |
868 | __io_u_mark_map(td->ts.io_u_complete, nr); | |
869 | td->ts.total_complete++; | |
870 | } | |
871 | ||
d8005759 | 872 | void io_u_mark_depth(struct thread_data *td, unsigned int nr) |
71619dc2 | 873 | { |
2b13e716 | 874 | int idx = 0; |
71619dc2 JA |
875 | |
876 | switch (td->cur_depth) { | |
877 | default: | |
2b13e716 | 878 | idx = 6; |
a783e61a | 879 | break; |
71619dc2 | 880 | case 32 ... 63: |
2b13e716 | 881 | idx = 5; |
a783e61a | 882 | break; |
71619dc2 | 883 | case 16 ... 31: |
2b13e716 | 884 | idx = 4; |
a783e61a | 885 | break; |
71619dc2 | 886 | case 8 ... 15: |
2b13e716 | 887 | idx = 3; |
a783e61a | 888 | break; |
71619dc2 | 889 | case 4 ... 7: |
2b13e716 | 890 | idx = 2; |
a783e61a | 891 | break; |
71619dc2 | 892 | case 2 ... 3: |
2b13e716 | 893 | idx = 1; |
71619dc2 JA |
894 | case 1: |
895 | break; | |
896 | } | |
897 | ||
2b13e716 | 898 | td->ts.io_u_map[idx] += nr; |
71619dc2 JA |
899 | } |
900 | ||
04a0feae JA |
901 | static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec) |
902 | { | |
2b13e716 | 903 | int idx = 0; |
04a0feae JA |
904 | |
905 | assert(usec < 1000); | |
906 | ||
907 | switch (usec) { | |
908 | case 750 ... 999: | |
2b13e716 | 909 | idx = 9; |
04a0feae JA |
910 | break; |
911 | case 500 ... 749: | |
2b13e716 | 912 | idx = 8; |
04a0feae JA |
913 | break; |
914 | case 250 ... 499: | |
2b13e716 | 915 | idx = 7; |
04a0feae JA |
916 | break; |
917 | case 100 ... 249: | |
2b13e716 | 918 | idx = 6; |
04a0feae JA |
919 | break; |
920 | case 50 ... 99: | |
2b13e716 | 921 | idx = 5; |
04a0feae JA |
922 | break; |
923 | case 20 ... 49: | |
2b13e716 | 924 | idx = 4; |
04a0feae JA |
925 | break; |
926 | case 10 ... 19: | |
2b13e716 | 927 | idx = 3; |
04a0feae JA |
928 | break; |
929 | case 4 ... 9: | |
2b13e716 | 930 | idx = 2; |
04a0feae JA |
931 | break; |
932 | case 2 ... 3: | |
2b13e716 | 933 | idx = 1; |
04a0feae JA |
934 | case 0 ... 1: |
935 | break; | |
936 | } | |
937 | ||
2b13e716 JA |
938 | assert(idx < FIO_IO_U_LAT_U_NR); |
939 | td->ts.io_u_lat_u[idx]++; | |
04a0feae JA |
940 | } |
941 | ||
942 | static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec) | |
ec118304 | 943 | { |
2b13e716 | 944 | int idx = 0; |
ec118304 JA |
945 | |
946 | switch (msec) { | |
947 | default: | |
2b13e716 | 948 | idx = 11; |
04a0feae | 949 | break; |
8abdce66 | 950 | case 1000 ... 1999: |
2b13e716 | 951 | idx = 10; |
04a0feae | 952 | break; |
8abdce66 | 953 | case 750 ... 999: |
2b13e716 | 954 | idx = 9; |
04a0feae | 955 | break; |
8abdce66 | 956 | case 500 ... 749: |
2b13e716 | 957 | idx = 8; |
04a0feae | 958 | break; |
8abdce66 | 959 | case 250 ... 499: |
2b13e716 | 960 | idx = 7; |
04a0feae | 961 | break; |
8abdce66 | 962 | case 100 ... 249: |
2b13e716 | 963 | idx = 6; |
04a0feae | 964 | break; |
8abdce66 | 965 | case 50 ... 99: |
2b13e716 | 966 | idx = 5; |
04a0feae | 967 | break; |
8abdce66 | 968 | case 20 ... 49: |
2b13e716 | 969 | idx = 4; |
04a0feae | 970 | break; |
8abdce66 | 971 | case 10 ... 19: |
2b13e716 | 972 | idx = 3; |
04a0feae | 973 | break; |
8abdce66 | 974 | case 4 ... 9: |
2b13e716 | 975 | idx = 2; |
04a0feae | 976 | break; |
ec118304 | 977 | case 2 ... 3: |
2b13e716 | 978 | idx = 1; |
ec118304 JA |
979 | case 0 ... 1: |
980 | break; | |
981 | } | |
982 | ||
2b13e716 JA |
983 | assert(idx < FIO_IO_U_LAT_M_NR); |
984 | td->ts.io_u_lat_m[idx]++; | |
04a0feae JA |
985 | } |
986 | ||
987 | static void io_u_mark_latency(struct thread_data *td, unsigned long usec) | |
988 | { | |
989 | if (usec < 1000) | |
990 | io_u_mark_lat_usec(td, usec); | |
991 | else | |
992 | io_u_mark_lat_msec(td, usec / 1000); | |
ec118304 JA |
993 | } |
994 | ||
0aabe160 JA |
995 | /* |
996 | * Get next file to service by choosing one at random | |
997 | */ | |
2cc52930 JA |
998 | static struct fio_file *get_next_file_rand(struct thread_data *td, |
999 | enum fio_file_flags goodf, | |
d6aed795 | 1000 | enum fio_file_flags badf) |
0aabe160 | 1001 | { |
c3546b53 | 1002 | uint64_t frand_max = rand_max(&td->next_file_state); |
0aabe160 | 1003 | struct fio_file *f; |
1c178180 | 1004 | int fno; |
0aabe160 JA |
1005 | |
1006 | do { | |
87b10676 | 1007 | int opened = 0; |
1294c3ec | 1008 | unsigned long r; |
4c07ad86 | 1009 | |
d6b72507 | 1010 | r = __rand(&td->next_file_state); |
54a21917 | 1011 | fno = (unsigned int) ((double) td->o.nr_files |
c3546b53 | 1012 | * (r / (frand_max + 1.0))); |
7c83c089 | 1013 | |
126d65c6 | 1014 | f = td->files[fno]; |
d6aed795 | 1015 | if (fio_file_done(f)) |
059e63c0 | 1016 | continue; |
1c178180 | 1017 | |
d6aed795 | 1018 | if (!fio_file_open(f)) { |
87b10676 JA |
1019 | int err; |
1020 | ||
002fe734 JA |
1021 | if (td->nr_open_files >= td->o.open_files) |
1022 | return ERR_PTR(-EBUSY); | |
1023 | ||
87b10676 JA |
1024 | err = td_io_open_file(td, f); |
1025 | if (err) | |
1026 | continue; | |
1027 | opened = 1; | |
1028 | } | |
1029 | ||
2ba1c290 JA |
1030 | if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) { |
1031 | dprint(FD_FILE, "get_next_file_rand: %p\n", f); | |
0aabe160 | 1032 | return f; |
2ba1c290 | 1033 | } |
87b10676 JA |
1034 | if (opened) |
1035 | td_io_close_file(td, f); | |
0aabe160 JA |
1036 | } while (1); |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Get next file to service by doing round robin between all available ones | |
1041 | */ | |
1c178180 JA |
1042 | static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf, |
1043 | int badf) | |
3d7c391d JA |
1044 | { |
1045 | unsigned int old_next_file = td->next_file; | |
1046 | struct fio_file *f; | |
1047 | ||
1048 | do { | |
87b10676 JA |
1049 | int opened = 0; |
1050 | ||
126d65c6 | 1051 | f = td->files[td->next_file]; |
3d7c391d JA |
1052 | |
1053 | td->next_file++; | |
2dc1bbeb | 1054 | if (td->next_file >= td->o.nr_files) |
3d7c391d JA |
1055 | td->next_file = 0; |
1056 | ||
87b10676 | 1057 | dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags); |
d6aed795 | 1058 | if (fio_file_done(f)) { |
d5ed68ea | 1059 | f = NULL; |
059e63c0 | 1060 | continue; |
d5ed68ea | 1061 | } |
059e63c0 | 1062 | |
d6aed795 | 1063 | if (!fio_file_open(f)) { |
87b10676 JA |
1064 | int err; |
1065 | ||
002fe734 JA |
1066 | if (td->nr_open_files >= td->o.open_files) |
1067 | return ERR_PTR(-EBUSY); | |
1068 | ||
87b10676 | 1069 | err = td_io_open_file(td, f); |
b5696bfc JA |
1070 | if (err) { |
1071 | dprint(FD_FILE, "error %d on open of %s\n", | |
1072 | err, f->file_name); | |
87c27b45 | 1073 | f = NULL; |
87b10676 | 1074 | continue; |
b5696bfc | 1075 | } |
87b10676 JA |
1076 | opened = 1; |
1077 | } | |
1078 | ||
0b9d69ec JA |
1079 | dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf, |
1080 | f->flags); | |
1c178180 | 1081 | if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) |
3d7c391d JA |
1082 | break; |
1083 | ||
87b10676 JA |
1084 | if (opened) |
1085 | td_io_close_file(td, f); | |
1086 | ||
3d7c391d JA |
1087 | f = NULL; |
1088 | } while (td->next_file != old_next_file); | |
1089 | ||
2ba1c290 | 1090 | dprint(FD_FILE, "get_next_file_rr: %p\n", f); |
3d7c391d JA |
1091 | return f; |
1092 | } | |
1093 | ||
7eb36574 | 1094 | static struct fio_file *__get_next_file(struct thread_data *td) |
bdb4e2e9 | 1095 | { |
1907dbc6 JA |
1096 | struct fio_file *f; |
1097 | ||
2dc1bbeb | 1098 | assert(td->o.nr_files <= td->files_index); |
1c178180 | 1099 | |
b5696bfc | 1100 | if (td->nr_done_files >= td->o.nr_files) { |
5ec10eaa JA |
1101 | dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d," |
1102 | " nr_files=%d\n", td->nr_open_files, | |
1103 | td->nr_done_files, | |
1104 | td->o.nr_files); | |
bdb4e2e9 | 1105 | return NULL; |
2ba1c290 | 1106 | } |
bdb4e2e9 | 1107 | |
1907dbc6 | 1108 | f = td->file_service_file; |
d6aed795 | 1109 | if (f && fio_file_open(f) && !fio_file_closing(f)) { |
a086c257 JA |
1110 | if (td->o.file_service_type == FIO_FSERVICE_SEQ) |
1111 | goto out; | |
1112 | if (td->file_service_left--) | |
1113 | goto out; | |
1114 | } | |
1907dbc6 | 1115 | |
a086c257 JA |
1116 | if (td->o.file_service_type == FIO_FSERVICE_RR || |
1117 | td->o.file_service_type == FIO_FSERVICE_SEQ) | |
d6aed795 | 1118 | f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing); |
bdb4e2e9 | 1119 | else |
d6aed795 | 1120 | f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing); |
1907dbc6 | 1121 | |
002fe734 JA |
1122 | if (IS_ERR(f)) |
1123 | return f; | |
1124 | ||
1907dbc6 JA |
1125 | td->file_service_file = f; |
1126 | td->file_service_left = td->file_service_nr - 1; | |
2ba1c290 | 1127 | out: |
0dac421f JA |
1128 | if (f) |
1129 | dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name); | |
1130 | else | |
1131 | dprint(FD_FILE, "get_next_file: NULL\n"); | |
1907dbc6 | 1132 | return f; |
bdb4e2e9 JA |
1133 | } |
1134 | ||
7eb36574 JA |
1135 | static struct fio_file *get_next_file(struct thread_data *td) |
1136 | { | |
372d8962 | 1137 | if (td->flags & TD_F_PROFILE_OPS) { |
d72be545 | 1138 | struct prof_io_ops *ops = &td->prof_io_ops; |
7eb36574 | 1139 | |
d72be545 JA |
1140 | if (ops->get_next_file) |
1141 | return ops->get_next_file(td); | |
1142 | } | |
7eb36574 JA |
1143 | |
1144 | return __get_next_file(td); | |
1145 | } | |
1146 | ||
002fe734 | 1147 | static long set_io_u_file(struct thread_data *td, struct io_u *io_u) |
429f6675 JA |
1148 | { |
1149 | struct fio_file *f; | |
1150 | ||
1151 | do { | |
1152 | f = get_next_file(td); | |
002fe734 JA |
1153 | if (IS_ERR_OR_NULL(f)) |
1154 | return PTR_ERR(f); | |
429f6675 | 1155 | |
429f6675 JA |
1156 | io_u->file = f; |
1157 | get_file(f); | |
1158 | ||
1159 | if (!fill_io_u(td, io_u)) | |
1160 | break; | |
1161 | ||
b5696bfc | 1162 | put_file_log(td, f); |
429f6675 | 1163 | td_io_close_file(td, f); |
b5696bfc | 1164 | io_u->file = NULL; |
d6aed795 | 1165 | fio_file_set_done(f); |
429f6675 | 1166 | td->nr_done_files++; |
0b9d69ec JA |
1167 | dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name, |
1168 | td->nr_done_files, td->o.nr_files); | |
429f6675 JA |
1169 | } while (1); |
1170 | ||
1171 | return 0; | |
1172 | } | |
1173 | ||
3e260a46 JA |
1174 | static void lat_fatal(struct thread_data *td, struct io_completion_data *icd, |
1175 | unsigned long tusec, unsigned long max_usec) | |
1176 | { | |
1177 | if (!td->error) | |
1178 | log_err("fio: latency of %lu usec exceeds specified max (%lu usec)\n", tusec, max_usec); | |
1179 | td_verror(td, ETIMEDOUT, "max latency exceeded"); | |
1180 | icd->error = ETIMEDOUT; | |
1181 | } | |
1182 | ||
1183 | static void lat_new_cycle(struct thread_data *td) | |
1184 | { | |
1185 | fio_gettime(&td->latency_ts, NULL); | |
1186 | td->latency_ios = ddir_rw_sum(td->io_blocks); | |
1187 | td->latency_failed = 0; | |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * We had an IO outside the latency target. Reduce the queue depth. If we | |
1192 | * are at QD=1, then it's time to give up. | |
1193 | */ | |
1194 | static int __lat_target_failed(struct thread_data *td) | |
1195 | { | |
1196 | if (td->latency_qd == 1) | |
1197 | return 1; | |
1198 | ||
1199 | td->latency_qd_high = td->latency_qd; | |
6bb58215 JA |
1200 | |
1201 | if (td->latency_qd == td->latency_qd_low) | |
1202 | td->latency_qd_low--; | |
1203 | ||
3e260a46 JA |
1204 | td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2; |
1205 | ||
1206 | dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high); | |
1207 | ||
1208 | /* | |
1209 | * When we ramp QD down, quiesce existing IO to prevent | |
1210 | * a storm of ramp downs due to pending higher depth. | |
1211 | */ | |
1212 | io_u_quiesce(td); | |
1213 | lat_new_cycle(td); | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | static int lat_target_failed(struct thread_data *td) | |
1218 | { | |
1219 | if (td->o.latency_percentile.u.f == 100.0) | |
1220 | return __lat_target_failed(td); | |
1221 | ||
1222 | td->latency_failed++; | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | void lat_target_init(struct thread_data *td) | |
1227 | { | |
6bb58215 JA |
1228 | td->latency_end_run = 0; |
1229 | ||
3e260a46 JA |
1230 | if (td->o.latency_target) { |
1231 | dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target); | |
1232 | fio_gettime(&td->latency_ts, NULL); | |
1233 | td->latency_qd = 1; | |
1234 | td->latency_qd_high = td->o.iodepth; | |
1235 | td->latency_qd_low = 1; | |
1236 | td->latency_ios = ddir_rw_sum(td->io_blocks); | |
1237 | } else | |
1238 | td->latency_qd = td->o.iodepth; | |
1239 | } | |
1240 | ||
6bb58215 JA |
1241 | void lat_target_reset(struct thread_data *td) |
1242 | { | |
1243 | if (!td->latency_end_run) | |
1244 | lat_target_init(td); | |
1245 | } | |
1246 | ||
3e260a46 JA |
1247 | static void lat_target_success(struct thread_data *td) |
1248 | { | |
1249 | const unsigned int qd = td->latency_qd; | |
6bb58215 | 1250 | struct thread_options *o = &td->o; |
3e260a46 JA |
1251 | |
1252 | td->latency_qd_low = td->latency_qd; | |
1253 | ||
1254 | /* | |
1255 | * If we haven't failed yet, we double up to a failing value instead | |
1256 | * of bisecting from highest possible queue depth. If we have set | |
1257 | * a limit other than td->o.iodepth, bisect between that. | |
1258 | */ | |
6bb58215 | 1259 | if (td->latency_qd_high != o->iodepth) |
3e260a46 JA |
1260 | td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2; |
1261 | else | |
1262 | td->latency_qd *= 2; | |
1263 | ||
6bb58215 JA |
1264 | if (td->latency_qd > o->iodepth) |
1265 | td->latency_qd = o->iodepth; | |
3e260a46 JA |
1266 | |
1267 | dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high); | |
6bb58215 | 1268 | |
3e260a46 | 1269 | /* |
6bb58215 JA |
1270 | * Same as last one, we are done. Let it run a latency cycle, so |
1271 | * we get only the results from the targeted depth. | |
3e260a46 | 1272 | */ |
6bb58215 JA |
1273 | if (td->latency_qd == qd) { |
1274 | if (td->latency_end_run) { | |
1275 | dprint(FD_RATE, "We are done\n"); | |
1276 | td->done = 1; | |
1277 | } else { | |
1278 | dprint(FD_RATE, "Quiesce and final run\n"); | |
1279 | io_u_quiesce(td); | |
1280 | td->latency_end_run = 1; | |
1281 | reset_all_stats(td); | |
1282 | reset_io_stats(td); | |
1283 | } | |
1284 | } | |
3e260a46 JA |
1285 | |
1286 | lat_new_cycle(td); | |
1287 | } | |
1288 | ||
1289 | /* | |
1290 | * Check if we can bump the queue depth | |
1291 | */ | |
1292 | void lat_target_check(struct thread_data *td) | |
1293 | { | |
1294 | uint64_t usec_window; | |
1295 | uint64_t ios; | |
1296 | double success_ios; | |
1297 | ||
1298 | usec_window = utime_since_now(&td->latency_ts); | |
1299 | if (usec_window < td->o.latency_window) | |
1300 | return; | |
1301 | ||
1302 | ios = ddir_rw_sum(td->io_blocks) - td->latency_ios; | |
1303 | success_ios = (double) (ios - td->latency_failed) / (double) ios; | |
1304 | success_ios *= 100.0; | |
1305 | ||
1306 | dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f); | |
1307 | ||
1308 | if (success_ios >= td->o.latency_percentile.u.f) | |
1309 | lat_target_success(td); | |
1310 | else | |
1311 | __lat_target_failed(td); | |
1312 | } | |
1313 | ||
1314 | /* | |
1315 | * If latency target is enabled, we might be ramping up or down and not | |
1316 | * using the full queue depth available. | |
1317 | */ | |
effd6ff0 | 1318 | int queue_full(const struct thread_data *td) |
3e260a46 JA |
1319 | { |
1320 | const int qempty = io_u_qempty(&td->io_u_freelist); | |
1321 | ||
1322 | if (qempty) | |
1323 | return 1; | |
1324 | if (!td->o.latency_target) | |
1325 | return 0; | |
1326 | ||
1327 | return td->cur_depth >= td->latency_qd; | |
1328 | } | |
429f6675 | 1329 | |
10ba535a JA |
1330 | struct io_u *__get_io_u(struct thread_data *td) |
1331 | { | |
0cae66f6 | 1332 | struct io_u *io_u = NULL; |
10ba535a | 1333 | |
ca09be4b JA |
1334 | if (td->stop_io) |
1335 | return NULL; | |
1336 | ||
e8462bd8 JA |
1337 | td_io_u_lock(td); |
1338 | ||
1339 | again: | |
2ae0b204 JA |
1340 | if (!io_u_rempty(&td->io_u_requeues)) |
1341 | io_u = io_u_rpop(&td->io_u_requeues); | |
3e260a46 | 1342 | else if (!queue_full(td)) { |
2ae0b204 | 1343 | io_u = io_u_qpop(&td->io_u_freelist); |
10ba535a | 1344 | |
225ba9e3 | 1345 | io_u->file = NULL; |
6040dabc | 1346 | io_u->buflen = 0; |
10ba535a | 1347 | io_u->resid = 0; |
d7762cf8 | 1348 | io_u->end_io = NULL; |
755200a3 JA |
1349 | } |
1350 | ||
1351 | if (io_u) { | |
0c6e7517 | 1352 | assert(io_u->flags & IO_U_F_FREE); |
a9da8ab2 | 1353 | io_u_clear(io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT | |
f8b0bd10 JA |
1354 | IO_U_F_TRIMMED | IO_U_F_BARRIER | |
1355 | IO_U_F_VER_LIST); | |
0c6e7517 | 1356 | |
755200a3 | 1357 | io_u->error = 0; |
bcd5abfa | 1358 | io_u->acct_ddir = -1; |
10ba535a | 1359 | td->cur_depth++; |
a9da8ab2 JA |
1360 | assert(!(td->flags & TD_F_CHILD)); |
1361 | io_u_set(io_u, IO_U_F_IN_CUR_DEPTH); | |
f9401285 | 1362 | io_u->ipo = NULL; |
a9da8ab2 | 1363 | } else if (td_async_processing(td)) { |
1dec3e07 JA |
1364 | /* |
1365 | * We ran out, wait for async verify threads to finish and | |
1366 | * return one | |
1367 | */ | |
a9da8ab2 JA |
1368 | assert(!(td->flags & TD_F_CHILD)); |
1369 | assert(!pthread_cond_wait(&td->free_cond, &td->io_u_lock)); | |
1dec3e07 | 1370 | goto again; |
10ba535a JA |
1371 | } |
1372 | ||
e8462bd8 | 1373 | td_io_u_unlock(td); |
10ba535a JA |
1374 | return io_u; |
1375 | } | |
1376 | ||
0d29de83 | 1377 | static int check_get_trim(struct thread_data *td, struct io_u *io_u) |
10ba535a | 1378 | { |
d72be545 JA |
1379 | if (!(td->flags & TD_F_TRIM_BACKLOG)) |
1380 | return 0; | |
1381 | ||
1382 | if (td->trim_entries) { | |
0d29de83 | 1383 | int get_trim = 0; |
10ba535a | 1384 | |
0d29de83 JA |
1385 | if (td->trim_batch) { |
1386 | td->trim_batch--; | |
1387 | get_trim = 1; | |
1388 | } else if (!(td->io_hist_len % td->o.trim_backlog) && | |
1389 | td->last_ddir != DDIR_READ) { | |
1390 | td->trim_batch = td->o.trim_batch; | |
1391 | if (!td->trim_batch) | |
1392 | td->trim_batch = td->o.trim_backlog; | |
1393 | get_trim = 1; | |
1394 | } | |
1395 | ||
1396 | if (get_trim && !get_next_trim(td, io_u)) | |
1397 | return 1; | |
2ba1c290 | 1398 | } |
10ba535a | 1399 | |
0d29de83 JA |
1400 | return 0; |
1401 | } | |
1402 | ||
1403 | static int check_get_verify(struct thread_data *td, struct io_u *io_u) | |
1404 | { | |
d72be545 JA |
1405 | if (!(td->flags & TD_F_VER_BACKLOG)) |
1406 | return 0; | |
1407 | ||
1408 | if (td->io_hist_len) { | |
9e144189 JA |
1409 | int get_verify = 0; |
1410 | ||
d1ece0c7 | 1411 | if (td->verify_batch) |
9e144189 | 1412 | get_verify = 1; |
d1ece0c7 | 1413 | else if (!(td->io_hist_len % td->o.verify_backlog) && |
9e144189 JA |
1414 | td->last_ddir != DDIR_READ) { |
1415 | td->verify_batch = td->o.verify_batch; | |
f8a75c99 JA |
1416 | if (!td->verify_batch) |
1417 | td->verify_batch = td->o.verify_backlog; | |
9e144189 JA |
1418 | get_verify = 1; |
1419 | } | |
1420 | ||
d1ece0c7 JA |
1421 | if (get_verify && !get_next_verify(td, io_u)) { |
1422 | td->verify_batch--; | |
0d29de83 | 1423 | return 1; |
d1ece0c7 | 1424 | } |
9e144189 JA |
1425 | } |
1426 | ||
0d29de83 JA |
1427 | return 0; |
1428 | } | |
1429 | ||
de789769 JA |
1430 | /* |
1431 | * Fill offset and start time into the buffer content, to prevent too | |
23f394d5 JA |
1432 | * easy compressible data for simple de-dupe attempts. Do this for every |
1433 | * 512b block in the range, since that should be the smallest block size | |
1434 | * we can expect from a device. | |
de789769 JA |
1435 | */ |
1436 | static void small_content_scramble(struct io_u *io_u) | |
1437 | { | |
23f394d5 | 1438 | unsigned int i, nr_blocks = io_u->buflen / 512; |
1ae83d45 | 1439 | uint64_t boffset; |
23f394d5 JA |
1440 | unsigned int offset; |
1441 | void *p, *end; | |
de789769 | 1442 | |
23f394d5 JA |
1443 | if (!nr_blocks) |
1444 | return; | |
1445 | ||
1446 | p = io_u->xfer_buf; | |
fba76ee8 | 1447 | boffset = io_u->offset; |
81f0366c | 1448 | io_u->buf_filled_len = 0; |
fad82f76 | 1449 | |
23f394d5 JA |
1450 | for (i = 0; i < nr_blocks; i++) { |
1451 | /* | |
1452 | * Fill the byte offset into a "random" start offset of | |
1453 | * the buffer, given by the product of the usec time | |
1454 | * and the actual offset. | |
1455 | */ | |
fad82f76 | 1456 | offset = (io_u->start_time.tv_usec ^ boffset) & 511; |
1ae83d45 JA |
1457 | offset &= ~(sizeof(uint64_t) - 1); |
1458 | if (offset >= 512 - sizeof(uint64_t)) | |
1459 | offset -= sizeof(uint64_t); | |
fba76ee8 | 1460 | memcpy(p + offset, &boffset, sizeof(boffset)); |
23f394d5 JA |
1461 | |
1462 | end = p + 512 - sizeof(io_u->start_time); | |
1463 | memcpy(end, &io_u->start_time, sizeof(io_u->start_time)); | |
1464 | p += 512; | |
fad82f76 | 1465 | boffset += 512; |
23f394d5 | 1466 | } |
de789769 JA |
1467 | } |
1468 | ||
0d29de83 JA |
1469 | /* |
1470 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, | |
1471 | * etc. The returned io_u is fully ready to be prepped and submitted. | |
1472 | */ | |
1473 | struct io_u *get_io_u(struct thread_data *td) | |
1474 | { | |
1475 | struct fio_file *f; | |
1476 | struct io_u *io_u; | |
de789769 | 1477 | int do_scramble = 0; |
002fe734 | 1478 | long ret = 0; |
0d29de83 JA |
1479 | |
1480 | io_u = __get_io_u(td); | |
1481 | if (!io_u) { | |
1482 | dprint(FD_IO, "__get_io_u failed\n"); | |
1483 | return NULL; | |
1484 | } | |
1485 | ||
1486 | if (check_get_verify(td, io_u)) | |
1487 | goto out; | |
1488 | if (check_get_trim(td, io_u)) | |
1489 | goto out; | |
1490 | ||
755200a3 JA |
1491 | /* |
1492 | * from a requeue, io_u already setup | |
1493 | */ | |
1494 | if (io_u->file) | |
77f392bf | 1495 | goto out; |
755200a3 | 1496 | |
429f6675 JA |
1497 | /* |
1498 | * If using an iolog, grab next piece if any available. | |
1499 | */ | |
d72be545 | 1500 | if (td->flags & TD_F_READ_IOLOG) { |
429f6675 JA |
1501 | if (read_iolog_get(td, io_u)) |
1502 | goto err_put; | |
2ba1c290 | 1503 | } else if (set_io_u_file(td, io_u)) { |
002fe734 | 1504 | ret = -EBUSY; |
2ba1c290 | 1505 | dprint(FD_IO, "io_u %p, setting file failed\n", io_u); |
429f6675 | 1506 | goto err_put; |
2ba1c290 | 1507 | } |
5ec10eaa | 1508 | |
429f6675 | 1509 | f = io_u->file; |
002fe734 JA |
1510 | if (!f) { |
1511 | dprint(FD_IO, "io_u %p, setting file failed\n", io_u); | |
1512 | goto err_put; | |
1513 | } | |
1514 | ||
d6aed795 | 1515 | assert(fio_file_open(f)); |
97af62ce | 1516 | |
ff58fced | 1517 | if (ddir_rw(io_u->ddir)) { |
d0656a93 | 1518 | if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) { |
2ba1c290 | 1519 | dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u); |
429f6675 | 1520 | goto err_put; |
2ba1c290 | 1521 | } |
10ba535a | 1522 | |
f1dfb668 JA |
1523 | f->last_start[io_u->ddir] = io_u->offset; |
1524 | f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen; | |
10ba535a | 1525 | |
fd68418e | 1526 | if (io_u->ddir == DDIR_WRITE) { |
d72be545 | 1527 | if (td->flags & TD_F_REFILL_BUFFERS) { |
9c42684e | 1528 | io_u_fill_buffer(td, io_u, |
1066358a | 1529 | td->o.min_bs[DDIR_WRITE], |
9e129577 | 1530 | io_u->buflen); |
ff441ae8 | 1531 | } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) && |
bedc9dc2 | 1532 | !(td->flags & TD_F_COMPRESS)) |
fd68418e | 1533 | do_scramble = 1; |
d72be545 | 1534 | if (td->flags & TD_F_VER_NONE) { |
629f1d71 JA |
1535 | populate_verify_io_u(td, io_u); |
1536 | do_scramble = 0; | |
1537 | } | |
fd68418e | 1538 | } else if (io_u->ddir == DDIR_READ) { |
cbe8d756 RR |
1539 | /* |
1540 | * Reset the buf_filled parameters so next time if the | |
1541 | * buffer is used for writes it is refilled. | |
1542 | */ | |
cbe8d756 RR |
1543 | io_u->buf_filled_len = 0; |
1544 | } | |
87dc1ab1 | 1545 | } |
10ba535a | 1546 | |
165faf16 JA |
1547 | /* |
1548 | * Set io data pointers. | |
1549 | */ | |
cec6b55d JA |
1550 | io_u->xfer_buf = io_u->buf; |
1551 | io_u->xfer_buflen = io_u->buflen; | |
5973cafb | 1552 | |
6ac7a331 | 1553 | out: |
0d29de83 | 1554 | assert(io_u->file); |
429f6675 | 1555 | if (!td_io_prep(td, io_u)) { |
993bf48b JA |
1556 | if (!td->o.disable_slat) |
1557 | fio_gettime(&io_u->start_time, NULL); | |
de789769 JA |
1558 | if (do_scramble) |
1559 | small_content_scramble(io_u); | |
429f6675 | 1560 | return io_u; |
36167d82 | 1561 | } |
429f6675 | 1562 | err_put: |
2ba1c290 | 1563 | dprint(FD_IO, "get_io_u failed\n"); |
429f6675 | 1564 | put_io_u(td, io_u); |
002fe734 | 1565 | return ERR_PTR(ret); |
10ba535a JA |
1566 | } |
1567 | ||
a9da8ab2 | 1568 | static void __io_u_log_error(struct thread_data *td, struct io_u *io_u) |
5451792e | 1569 | { |
8b28bd41 | 1570 | enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error); |
825f818e | 1571 | |
8b28bd41 DM |
1572 | if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump) |
1573 | return; | |
5451792e | 1574 | |
709c8313 RE |
1575 | log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%lu\n", |
1576 | io_u->file ? " on file " : "", | |
1577 | io_u->file ? io_u->file->file_name : "", | |
1578 | strerror(io_u->error), | |
1579 | io_ddir_name(io_u->ddir), | |
1580 | io_u->offset, io_u->xfer_buflen); | |
5451792e | 1581 | |
5ad7be56 KD |
1582 | if (td->io_ops->errdetails) { |
1583 | char *err = td->io_ops->errdetails(io_u); | |
1584 | ||
1585 | log_err("fio: %s\n", err); | |
1586 | free(err); | |
1587 | } | |
1588 | ||
5451792e JA |
1589 | if (!td->error) |
1590 | td_verror(td, io_u->error, "io_u error"); | |
1591 | } | |
1592 | ||
a9da8ab2 JA |
1593 | void io_u_log_error(struct thread_data *td, struct io_u *io_u) |
1594 | { | |
1595 | __io_u_log_error(td, io_u); | |
1596 | if (td->parent) | |
1597 | __io_u_log_error(td, io_u); | |
1598 | } | |
1599 | ||
aba6c951 JA |
1600 | static inline int gtod_reduce(struct thread_data *td) |
1601 | { | |
729fe3af | 1602 | return td->o.disable_clat && td->o.disable_lat && td->o.disable_slat |
b74b8208 | 1603 | && td->o.disable_bw; |
aba6c951 JA |
1604 | } |
1605 | ||
c8eeb9df JA |
1606 | static void account_io_completion(struct thread_data *td, struct io_u *io_u, |
1607 | struct io_completion_data *icd, | |
1608 | const enum fio_ddir idx, unsigned int bytes) | |
1609 | { | |
a9da8ab2 | 1610 | const int no_reduce = !gtod_reduce(td); |
24d23ca7 | 1611 | unsigned long lusec = 0; |
c8eeb9df | 1612 | |
75dc383e JA |
1613 | if (td->parent) |
1614 | td = td->parent; | |
1615 | ||
a9da8ab2 | 1616 | if (no_reduce) |
c8eeb9df JA |
1617 | lusec = utime_since(&io_u->issue_time, &icd->time); |
1618 | ||
1619 | if (!td->o.disable_lat) { | |
1620 | unsigned long tusec; | |
1621 | ||
1622 | tusec = utime_since(&io_u->start_time, &icd->time); | |
ae588852 | 1623 | add_lat_sample(td, idx, tusec, bytes, io_u->offset); |
15501535 | 1624 | |
d4afedfd JA |
1625 | if (td->flags & TD_F_PROFILE_OPS) { |
1626 | struct prof_io_ops *ops = &td->prof_io_ops; | |
1627 | ||
1628 | if (ops->io_u_lat) | |
1629 | icd->error = ops->io_u_lat(td, tusec); | |
1630 | } | |
1631 | ||
3e260a46 JA |
1632 | if (td->o.max_latency && tusec > td->o.max_latency) |
1633 | lat_fatal(td, icd, tusec, td->o.max_latency); | |
1634 | if (td->o.latency_target && tusec > td->o.latency_target) { | |
1635 | if (lat_target_failed(td)) | |
1636 | lat_fatal(td, icd, tusec, td->o.latency_target); | |
15501535 | 1637 | } |
c8eeb9df JA |
1638 | } |
1639 | ||
1640 | if (!td->o.disable_clat) { | |
ae588852 | 1641 | add_clat_sample(td, idx, lusec, bytes, io_u->offset); |
c8eeb9df JA |
1642 | io_u_mark_latency(td, lusec); |
1643 | } | |
1644 | ||
1645 | if (!td->o.disable_bw) | |
1646 | add_bw_sample(td, idx, bytes, &icd->time); | |
1647 | ||
a9da8ab2 | 1648 | if (no_reduce) |
aba6c951 | 1649 | add_iops_sample(td, idx, bytes, &icd->time); |
66347cfa DE |
1650 | |
1651 | if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) { | |
1652 | uint32_t *info = io_u_block_info(td, io_u); | |
1653 | if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) { | |
1654 | if (io_u->ddir == DDIR_TRIM) { | |
1655 | *info = BLOCK_INFO(BLOCK_STATE_TRIMMED, | |
1656 | BLOCK_INFO_TRIMS(*info) + 1); | |
1657 | } else if (io_u->ddir == DDIR_WRITE) { | |
1658 | *info = BLOCK_INFO_SET_STATE(BLOCK_STATE_WRITTEN, | |
1659 | *info); | |
1660 | } | |
1661 | } | |
1662 | } | |
c8eeb9df JA |
1663 | } |
1664 | ||
f8b0bd10 | 1665 | static void io_completed(struct thread_data *td, struct io_u **io_u_ptr, |
97601024 | 1666 | struct io_completion_data *icd) |
10ba535a | 1667 | { |
f8b0bd10 JA |
1668 | struct io_u *io_u = *io_u_ptr; |
1669 | enum fio_ddir ddir = io_u->ddir; | |
1670 | struct fio_file *f = io_u->file; | |
10ba535a | 1671 | |
2ba1c290 JA |
1672 | dprint_io_u(io_u, "io complete"); |
1673 | ||
0c6e7517 | 1674 | assert(io_u->flags & IO_U_F_FLIGHT); |
a9da8ab2 | 1675 | io_u_clear(io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK); |
f9401285 JA |
1676 | |
1677 | /* | |
1678 | * Mark IO ok to verify | |
1679 | */ | |
1680 | if (io_u->ipo) { | |
890b6656 JA |
1681 | /* |
1682 | * Remove errored entry from the verification list | |
1683 | */ | |
1684 | if (io_u->error) | |
1685 | unlog_io_piece(td, io_u); | |
1686 | else { | |
1687 | io_u->ipo->flags &= ~IP_F_IN_FLIGHT; | |
1688 | write_barrier(); | |
1689 | } | |
f9401285 JA |
1690 | } |
1691 | ||
f8b0bd10 | 1692 | if (ddir_sync(ddir)) { |
87dc1ab1 | 1693 | td->last_was_sync = 1; |
44f29692 JA |
1694 | if (f) { |
1695 | f->first_write = -1ULL; | |
1696 | f->last_write = -1ULL; | |
1697 | } | |
87dc1ab1 JA |
1698 | return; |
1699 | } | |
1700 | ||
1701 | td->last_was_sync = 0; | |
f8b0bd10 | 1702 | td->last_ddir = ddir; |
87dc1ab1 | 1703 | |
f8b0bd10 | 1704 | if (!io_u->error && ddir_rw(ddir)) { |
10ba535a | 1705 | unsigned int bytes = io_u->buflen - io_u->resid; |
b29ee5b3 | 1706 | int ret; |
10ba535a | 1707 | |
f8b0bd10 JA |
1708 | td->io_blocks[ddir]++; |
1709 | td->this_io_blocks[ddir]++; | |
1710 | td->io_bytes[ddir] += bytes; | |
ae2fafc8 | 1711 | |
1712 | if (!(io_u->flags & IO_U_F_VER_LIST)) | |
f8b0bd10 JA |
1713 | td->this_io_bytes[ddir] += bytes; |
1714 | ||
ca09be4b JA |
1715 | if (ddir == DDIR_WRITE) { |
1716 | if (f) { | |
1717 | if (f->first_write == -1ULL || | |
1718 | io_u->offset < f->first_write) | |
1719 | f->first_write = io_u->offset; | |
1720 | if (f->last_write == -1ULL || | |
1721 | ((io_u->offset + bytes) > f->last_write)) | |
1722 | f->last_write = io_u->offset + bytes; | |
1723 | } | |
1724 | if (td->last_write_comp) { | |
1725 | int idx = td->last_write_idx++; | |
1726 | ||
1727 | td->last_write_comp[idx] = io_u->offset; | |
1728 | if (td->last_write_idx == td->o.iodepth) | |
1729 | td->last_write_idx = 0; | |
1730 | } | |
44f29692 JA |
1731 | } |
1732 | ||
6b1190fd | 1733 | if (ramp_time_over(td) && (td->runstate == TD_RUNNING || |
50a8ce86 | 1734 | td->runstate == TD_VERIFYING)) |
f8b0bd10 | 1735 | account_io_completion(td, io_u, icd, ddir, bytes); |
40e1a6f0 | 1736 | |
f8b0bd10 | 1737 | icd->bytes_done[ddir] += bytes; |
3af6ef39 | 1738 | |
d7762cf8 | 1739 | if (io_u->end_io) { |
f8b0bd10 JA |
1740 | ret = io_u->end_io(td, io_u_ptr); |
1741 | io_u = *io_u_ptr; | |
3af6ef39 JA |
1742 | if (ret && !icd->error) |
1743 | icd->error = ret; | |
1744 | } | |
ff58fced | 1745 | } else if (io_u->error) { |
10ba535a | 1746 | icd->error = io_u->error; |
5451792e JA |
1747 | io_u_log_error(td, io_u); |
1748 | } | |
8b28bd41 | 1749 | if (icd->error) { |
f8b0bd10 JA |
1750 | enum error_type_bit eb = td_error_type(ddir, icd->error); |
1751 | ||
8b28bd41 DM |
1752 | if (!td_non_fatal_error(td, eb, icd->error)) |
1753 | return; | |
f8b0bd10 | 1754 | |
f2bba182 RR |
1755 | /* |
1756 | * If there is a non_fatal error, then add to the error count | |
1757 | * and clear all the errors. | |
1758 | */ | |
1759 | update_error_count(td, icd->error); | |
1760 | td_clear_error(td); | |
1761 | icd->error = 0; | |
f8b0bd10 JA |
1762 | if (io_u) |
1763 | io_u->error = 0; | |
f2bba182 | 1764 | } |
10ba535a JA |
1765 | } |
1766 | ||
9520ebb9 JA |
1767 | static void init_icd(struct thread_data *td, struct io_completion_data *icd, |
1768 | int nr) | |
10ba535a | 1769 | { |
6eaf09d6 | 1770 | int ddir; |
aba6c951 JA |
1771 | |
1772 | if (!gtod_reduce(td)) | |
9520ebb9 | 1773 | fio_gettime(&icd->time, NULL); |
02bcaa8c | 1774 | |
3af6ef39 JA |
1775 | icd->nr = nr; |
1776 | ||
10ba535a | 1777 | icd->error = 0; |
6eaf09d6 SL |
1778 | for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) |
1779 | icd->bytes_done[ddir] = 0; | |
36167d82 JA |
1780 | } |
1781 | ||
97601024 JA |
1782 | static void ios_completed(struct thread_data *td, |
1783 | struct io_completion_data *icd) | |
36167d82 JA |
1784 | { |
1785 | struct io_u *io_u; | |
1786 | int i; | |
1787 | ||
10ba535a JA |
1788 | for (i = 0; i < icd->nr; i++) { |
1789 | io_u = td->io_ops->event(td, i); | |
1790 | ||
f8b0bd10 | 1791 | io_completed(td, &io_u, icd); |
e8462bd8 | 1792 | |
f8b0bd10 | 1793 | if (io_u) |
e8462bd8 | 1794 | put_io_u(td, io_u); |
10ba535a JA |
1795 | } |
1796 | } | |
97601024 | 1797 | |
e7e6cfb4 JA |
1798 | /* |
1799 | * Complete a single io_u for the sync engines. | |
1800 | */ | |
55312f9f | 1801 | int io_u_sync_complete(struct thread_data *td, struct io_u *io_u) |
97601024 JA |
1802 | { |
1803 | struct io_completion_data icd; | |
55312f9f | 1804 | int ddir; |
97601024 | 1805 | |
9520ebb9 | 1806 | init_icd(td, &icd, 1); |
f8b0bd10 | 1807 | io_completed(td, &io_u, &icd); |
e8462bd8 | 1808 | |
f8b0bd10 | 1809 | if (io_u) |
e8462bd8 | 1810 | put_io_u(td, io_u); |
97601024 | 1811 | |
581e7141 JA |
1812 | if (icd.error) { |
1813 | td_verror(td, icd.error, "io_u_sync_complete"); | |
1814 | return -1; | |
1815 | } | |
97601024 | 1816 | |
55312f9f JA |
1817 | for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) |
1818 | td->bytes_done[ddir] += icd.bytes_done[ddir]; | |
581e7141 JA |
1819 | |
1820 | return 0; | |
97601024 JA |
1821 | } |
1822 | ||
e7e6cfb4 JA |
1823 | /* |
1824 | * Called to complete min_events number of io for the async engines. | |
1825 | */ | |
55312f9f | 1826 | int io_u_queued_complete(struct thread_data *td, int min_evts) |
97601024 | 1827 | { |
97601024 | 1828 | struct io_completion_data icd; |
00de55ef | 1829 | struct timespec *tvp = NULL; |
55312f9f | 1830 | int ret, ddir; |
4d06a338 | 1831 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, }; |
97601024 | 1832 | |
4950421a | 1833 | dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts); |
b271fe62 | 1834 | |
4950421a | 1835 | if (!min_evts) |
00de55ef | 1836 | tvp = &ts; |
5fb4b366 RE |
1837 | else if (min_evts > td->cur_depth) |
1838 | min_evts = td->cur_depth; | |
97601024 | 1839 | |
82407585 RP |
1840 | /* No worries, td_io_getevents fixes min and max if they are |
1841 | * set incorrectly */ | |
1842 | ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp); | |
97601024 | 1843 | if (ret < 0) { |
e1161c32 | 1844 | td_verror(td, -ret, "td_io_getevents"); |
97601024 JA |
1845 | return ret; |
1846 | } else if (!ret) | |
1847 | return ret; | |
1848 | ||
9520ebb9 | 1849 | init_icd(td, &icd, ret); |
97601024 | 1850 | ios_completed(td, &icd); |
581e7141 JA |
1851 | if (icd.error) { |
1852 | td_verror(td, icd.error, "io_u_queued_complete"); | |
1853 | return -1; | |
1854 | } | |
97601024 | 1855 | |
55312f9f JA |
1856 | for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) |
1857 | td->bytes_done[ddir] += icd.bytes_done[ddir]; | |
581e7141 JA |
1858 | |
1859 | return 0; | |
97601024 | 1860 | } |
7e77dd02 JA |
1861 | |
1862 | /* | |
1863 | * Call when io_u is really queued, to update the submission latency. | |
1864 | */ | |
1865 | void io_u_queued(struct thread_data *td, struct io_u *io_u) | |
1866 | { | |
9520ebb9 JA |
1867 | if (!td->o.disable_slat) { |
1868 | unsigned long slat_time; | |
7e77dd02 | 1869 | |
9520ebb9 | 1870 | slat_time = utime_since(&io_u->start_time, &io_u->issue_time); |
75dc383e JA |
1871 | |
1872 | if (td->parent) | |
1873 | td = td->parent; | |
1874 | ||
ae588852 JA |
1875 | add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen, |
1876 | io_u->offset); | |
9520ebb9 | 1877 | } |
7e77dd02 | 1878 | } |
433afcb4 | 1879 | |
5c94b008 JA |
1880 | /* |
1881 | * See if we should reuse the last seed, if dedupe is enabled | |
1882 | */ | |
9451b93e | 1883 | static struct frand_state *get_buf_state(struct thread_data *td) |
5c94b008 | 1884 | { |
c3546b53 | 1885 | uint64_t frand_max; |
5c94b008 JA |
1886 | unsigned int v; |
1887 | unsigned long r; | |
1888 | ||
1889 | if (!td->o.dedupe_percentage) | |
1890 | return &td->buf_state; | |
732eedd0 | 1891 | else if (td->o.dedupe_percentage == 100) { |
9451b93e JA |
1892 | frand_copy(&td->buf_state_prev, &td->buf_state); |
1893 | return &td->buf_state; | |
732eedd0 | 1894 | } |
5c94b008 | 1895 | |
c3546b53 | 1896 | frand_max = rand_max(&td->dedupe_state); |
5c94b008 | 1897 | r = __rand(&td->dedupe_state); |
c3546b53 | 1898 | v = 1 + (int) (100.0 * (r / (frand_max + 1.0))); |
5c94b008 JA |
1899 | |
1900 | if (v <= td->o.dedupe_percentage) | |
1901 | return &td->buf_state_prev; | |
1902 | ||
1903 | return &td->buf_state; | |
1904 | } | |
1905 | ||
9451b93e | 1906 | static void save_buf_state(struct thread_data *td, struct frand_state *rs) |
5c94b008 | 1907 | { |
9451b93e JA |
1908 | if (td->o.dedupe_percentage == 100) |
1909 | frand_copy(rs, &td->buf_state_prev); | |
1910 | else if (rs == &td->buf_state) | |
5c94b008 JA |
1911 | frand_copy(&td->buf_state_prev, rs); |
1912 | } | |
1913 | ||
cc86c395 JA |
1914 | void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write, |
1915 | unsigned int max_bs) | |
5973cafb | 1916 | { |
d1af2894 JA |
1917 | struct thread_options *o = &td->o; |
1918 | ||
4eff3e57 | 1919 | if (o->compress_percentage || o->dedupe_percentage) { |
9c42684e | 1920 | unsigned int perc = td->o.compress_percentage; |
5c94b008 | 1921 | struct frand_state *rs; |
1066358a | 1922 | unsigned int left = max_bs; |
1e7f82e2 | 1923 | unsigned int this_write; |
5c94b008 | 1924 | |
1066358a | 1925 | do { |
9451b93e | 1926 | rs = get_buf_state(td); |
9c42684e | 1927 | |
1066358a | 1928 | min_write = min(min_write, left); |
f97a43a1 | 1929 | |
1066358a | 1930 | if (perc) { |
4b157ac6 JA |
1931 | this_write = min_not_zero(min_write, |
1932 | td->o.compress_chunk); | |
1e7f82e2 JA |
1933 | |
1934 | fill_random_buf_percentage(rs, buf, perc, | |
1935 | this_write, this_write, | |
1936 | o->buffer_pattern, | |
1937 | o->buffer_pattern_bytes); | |
1938 | } else { | |
1066358a | 1939 | fill_random_buf(rs, buf, min_write); |
1e7f82e2 JA |
1940 | this_write = min_write; |
1941 | } | |
1066358a | 1942 | |
1e7f82e2 JA |
1943 | buf += this_write; |
1944 | left -= this_write; | |
9451b93e | 1945 | save_buf_state(td, rs); |
1066358a | 1946 | } while (left); |
d1af2894 JA |
1947 | } else if (o->buffer_pattern_bytes) |
1948 | fill_buffer_pattern(td, buf, max_bs); | |
999d245e | 1949 | else if (o->zero_buffers) |
cc86c395 | 1950 | memset(buf, 0, max_bs); |
999d245e | 1951 | else |
9451b93e | 1952 | fill_random_buf(get_buf_state(td), buf, max_bs); |
cc86c395 JA |
1953 | } |
1954 | ||
1955 | /* | |
1956 | * "randomly" fill the buffer contents | |
1957 | */ | |
1958 | void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u, | |
1959 | unsigned int min_write, unsigned int max_bs) | |
1960 | { | |
1961 | io_u->buf_filled_len = 0; | |
1962 | fill_io_buffer(td, io_u->buf, min_write, max_bs); | |
5973cafb | 1963 | } |