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