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