Fix problem with terminating on unaligned sizes
[fio.git] / io_u.c
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
15 struct io_completion_data {
16         int nr;                         /* input */
17
18         int error;                      /* output */
19         uint64_t bytes_done[DDIR_RWDIR_CNT];    /* output */
20         struct timeval time;            /* output */
21 };
22
23 /*
24  * The ->io_axmap contains a map of blocks we have or have not done io
25  * to yet. Used to make sure we cover the entire range in a fair fashion.
26  */
27 static int random_map_free(struct fio_file *f, const uint64_t block)
28 {
29         return !axmap_isset(f->io_axmap, block);
30 }
31
32 /*
33  * Mark a given offset as used in the map.
34  */
35 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
36 {
37         unsigned int min_bs = td->o.rw_min_bs;
38         struct fio_file *f = io_u->file;
39         unsigned int nr_blocks;
40         uint64_t block;
41
42         block = (io_u->offset - f->file_offset) / (uint64_t) min_bs;
43         nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
44
45         if (!(io_u->flags & IO_U_F_BUSY_OK))
46                 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
47
48         if ((nr_blocks * min_bs) < io_u->buflen)
49                 io_u->buflen = nr_blocks * min_bs;
50 }
51
52 static uint64_t last_block(struct thread_data *td, struct fio_file *f,
53                            enum fio_ddir ddir)
54 {
55         uint64_t max_blocks;
56         uint64_t max_size;
57
58         assert(ddir_rw(ddir));
59
60         /*
61          * Hmm, should we make sure that ->io_size <= ->real_file_size?
62          */
63         max_size = f->io_size;
64         if (max_size > f->real_file_size)
65                 max_size = f->real_file_size;
66
67         if (td->o.zone_range)
68                 max_size = td->o.zone_range;
69
70         max_blocks = max_size / (uint64_t) td->o.ba[ddir];
71         if (!max_blocks)
72                 return 0;
73
74         return max_blocks;
75 }
76
77 struct rand_off {
78         struct flist_head list;
79         uint64_t off;
80 };
81
82 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
83                                   enum fio_ddir ddir, uint64_t *b)
84 {
85         uint64_t r, lastb;
86
87         lastb = last_block(td, f, ddir);
88         if (!lastb)
89                 return 1;
90
91         if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE) {
92                 uint64_t rmax;
93
94                 rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
95
96                 if (td->o.use_os_rand) {
97                         rmax = OS_RAND_MAX;
98                         r = os_random_long(&td->random_state);
99                 } else {
100                         rmax = FRAND_MAX;
101                         r = __rand(&td->__random_state);
102                 }
103
104                 dprint(FD_RANDOM, "off rand %llu\n", r);
105
106                 *b = (lastb - 1) * (r / ((uint64_t) rmax + 1.0));
107         } else {
108                 uint64_t off = 0;
109
110                 if (lfsr_next(&f->lfsr, &off, lastb))
111                         return 1;
112
113                 *b = off;
114         }
115
116         /*
117          * if we are not maintaining a random map, we are done.
118          */
119         if (!file_randommap(td, f))
120                 goto ret;
121
122         /*
123          * calculate map offset and check if it's free
124          */
125         if (random_map_free(f, *b))
126                 goto ret;
127
128         dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n", *b);
129
130         *b = axmap_next_free(f->io_axmap, *b);
131         if (*b == (uint64_t) -1ULL)
132                 return 1;
133 ret:
134         return 0;
135 }
136
137 static 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
145 static 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
153 static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
154 {
155         struct rand_off *r1 = flist_entry(a, struct rand_off, list);
156         struct rand_off *r2 = flist_entry(b, struct rand_off, list);
157
158         return r1->off - r2->off;
159 }
160
161 static int get_off_from_method(struct thread_data *td, struct fio_file *f,
162                                enum fio_ddir ddir, uint64_t *b)
163 {
164         if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
165                 return __get_next_rand_offset(td, f, ddir, b);
166         else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
167                 return __get_next_rand_offset_zipf(td, f, ddir, b);
168         else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
169                 return __get_next_rand_offset_pareto(td, f, ddir, b);
170
171         log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
172         return 1;
173 }
174
175 /*
176  * Sort the reads for a verify phase in batches of verifysort_nr, if
177  * specified.
178  */
179 static inline int should_sort_io(struct thread_data *td)
180 {
181         if (!td->o.verifysort_nr || !td->o.do_verify)
182                 return 0;
183         if (!td_random(td))
184                 return 0;
185         if (td->runstate != TD_VERIFYING)
186                 return 0;
187         if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE)
188                 return 0;
189
190         return 1;
191 }
192
193 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
194                                 enum fio_ddir ddir, uint64_t *b)
195 {
196         struct rand_off *r;
197         int i, ret = 1;
198
199         if (!should_sort_io(td))
200                 return get_off_from_method(td, f, ddir, b);
201
202         if (!flist_empty(&td->next_rand_list)) {
203                 struct rand_off *r;
204 fetch:
205                 r = flist_entry(td->next_rand_list.next, struct rand_off, list);
206                 flist_del(&r->list);
207                 *b = r->off;
208                 free(r);
209                 return 0;
210         }
211
212         for (i = 0; i < td->o.verifysort_nr; i++) {
213                 r = malloc(sizeof(*r));
214
215                 ret = get_off_from_method(td, f, ddir, &r->off);
216                 if (ret) {
217                         free(r);
218                         break;
219                 }
220
221                 flist_add(&r->list, &td->next_rand_list);
222         }
223
224         if (ret && !i)
225                 return ret;
226
227         assert(!flist_empty(&td->next_rand_list));
228         flist_sort(NULL, &td->next_rand_list, flist_cmp);
229         goto fetch;
230 }
231
232 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
233                                enum fio_ddir ddir, uint64_t *b)
234 {
235         if (!get_next_rand_offset(td, f, ddir, b))
236                 return 0;
237
238         if (td->o.time_based) {
239                 fio_file_reset(td, f);
240                 if (!get_next_rand_offset(td, f, ddir, b))
241                         return 0;
242         }
243
244         dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
245                         f->file_name, f->last_pos, f->real_file_size);
246         return 1;
247 }
248
249 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
250                                enum fio_ddir ddir, uint64_t *offset)
251 {
252         assert(ddir_rw(ddir));
253
254         if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
255                 f->last_pos = f->last_pos - f->io_size;
256
257         if (f->last_pos < f->real_file_size) {
258                 uint64_t pos;
259
260                 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
261                         f->last_pos = f->real_file_size;
262
263                 pos = f->last_pos - f->file_offset;
264                 if (pos)
265                         pos += td->o.ddir_seq_add;
266
267                 *offset = pos;
268                 return 0;
269         }
270
271         return 1;
272 }
273
274 static int get_next_block(struct thread_data *td, struct io_u *io_u,
275                           enum fio_ddir ddir, int rw_seq)
276 {
277         struct fio_file *f = io_u->file;
278         uint64_t b, offset;
279         int ret;
280
281         assert(ddir_rw(ddir));
282
283         b = offset = -1ULL;
284
285         if (rw_seq) {
286                 if (td_random(td))
287                         ret = get_next_rand_block(td, f, ddir, &b);
288                 else
289                         ret = get_next_seq_offset(td, f, ddir, &offset);
290         } else {
291                 io_u->flags |= IO_U_F_BUSY_OK;
292
293                 if (td->o.rw_seq == RW_SEQ_SEQ) {
294                         ret = get_next_seq_offset(td, f, ddir, &offset);
295                         if (ret)
296                                 ret = get_next_rand_block(td, f, ddir, &b);
297                 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
298                         if (f->last_start != -1ULL)
299                                 offset = f->last_start - f->file_offset;
300                         else
301                                 offset = 0;
302                         ret = 0;
303                 } else {
304                         log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
305                         ret = 1;
306                 }
307         }
308
309         if (!ret) {
310                 if (offset != -1ULL)
311                         io_u->offset = offset;
312                 else if (b != -1ULL)
313                         io_u->offset = b * td->o.ba[ddir];
314                 else {
315                         log_err("fio: bug in offset generation: offset=%llu, b=%llu\n",
316                                                                 offset, b);
317                         ret = 1;
318                 }
319         }
320
321         return ret;
322 }
323
324 /*
325  * For random io, generate a random new block and see if it's used. Repeat
326  * until we find a free one. For sequential io, just return the end of
327  * the last io issued.
328  */
329 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
330 {
331         struct fio_file *f = io_u->file;
332         enum fio_ddir ddir = io_u->ddir;
333         int rw_seq_hit = 0;
334
335         assert(ddir_rw(ddir));
336
337         if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
338                 rw_seq_hit = 1;
339                 td->ddir_seq_nr = td->o.ddir_seq_nr;
340         }
341
342         if (get_next_block(td, io_u, ddir, rw_seq_hit))
343                 return 1;
344
345         if (io_u->offset >= f->io_size) {
346                 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
347                                         io_u->offset, f->io_size);
348                 return 1;
349         }
350
351         io_u->offset += f->file_offset;
352         if (io_u->offset >= f->real_file_size) {
353                 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
354                                         io_u->offset, f->real_file_size);
355                 return 1;
356         }
357
358         return 0;
359 }
360
361 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
362 {
363         if (td->flags & TD_F_PROFILE_OPS) {
364                 struct prof_io_ops *ops = &td->prof_io_ops;
365
366                 if (ops->fill_io_u_off)
367                         return ops->fill_io_u_off(td, io_u);
368         }
369
370         return __get_next_offset(td, io_u);
371 }
372
373 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
374                             unsigned int buflen)
375 {
376         struct fio_file *f = io_u->file;
377
378         return io_u->offset + buflen <= f->io_size + get_start_offset(td);
379 }
380
381 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
382 {
383         const int ddir = io_u->ddir;
384         unsigned int buflen = 0;
385         unsigned int minbs, maxbs;
386         unsigned long r, rand_max;
387
388         assert(ddir_rw(ddir));
389
390         minbs = td->o.min_bs[ddir];
391         maxbs = td->o.max_bs[ddir];
392
393         if (minbs == maxbs)
394                 return minbs;
395
396         /*
397          * If we can't satisfy the min block size from here, then fail
398          */
399         if (!io_u_fits(td, io_u, minbs))
400                 return 0;
401
402         if (td->o.use_os_rand)
403                 rand_max = OS_RAND_MAX;
404         else
405                 rand_max = FRAND_MAX;
406
407         do {
408                 if (td->o.use_os_rand)
409                         r = os_random_long(&td->bsrange_state);
410                 else
411                         r = __rand(&td->__bsrange_state);
412
413                 if (!td->o.bssplit_nr[ddir]) {
414                         buflen = 1 + (unsigned int) ((double) maxbs *
415                                         (r / (rand_max + 1.0)));
416                         if (buflen < minbs)
417                                 buflen = minbs;
418                 } else {
419                         long perc = 0;
420                         unsigned int i;
421
422                         for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
423                                 struct bssplit *bsp = &td->o.bssplit[ddir][i];
424
425                                 buflen = bsp->bs;
426                                 perc += bsp->perc;
427                                 if ((r <= ((rand_max / 100L) * perc)) &&
428                                     io_u_fits(td, io_u, buflen))
429                                         break;
430                         }
431                 }
432
433                 if (!td->o.bs_unaligned && is_power_of_2(minbs))
434                         buflen = (buflen + minbs - 1) & ~(minbs - 1);
435
436         } while (!io_u_fits(td, io_u, buflen));
437
438         return buflen;
439 }
440
441 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
442 {
443         if (td->flags & TD_F_PROFILE_OPS) {
444                 struct prof_io_ops *ops = &td->prof_io_ops;
445
446                 if (ops->fill_io_u_size)
447                         return ops->fill_io_u_size(td, io_u);
448         }
449
450         return __get_next_buflen(td, io_u);
451 }
452
453 static void set_rwmix_bytes(struct thread_data *td)
454 {
455         unsigned int diff;
456
457         /*
458          * we do time or byte based switch. this is needed because
459          * buffered writes may issue a lot quicker than they complete,
460          * whereas reads do not.
461          */
462         diff = td->o.rwmix[td->rwmix_ddir ^ 1];
463         td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
464 }
465
466 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
467 {
468         unsigned int v;
469         unsigned long r;
470
471         if (td->o.use_os_rand) {
472                 r = os_random_long(&td->rwmix_state);
473                 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
474         } else {
475                 r = __rand(&td->__rwmix_state);
476                 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
477         }
478
479         if (v <= td->o.rwmix[DDIR_READ])
480                 return DDIR_READ;
481
482         return DDIR_WRITE;
483 }
484
485 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
486 {
487         enum fio_ddir odir = ddir ^ 1;
488         struct timeval t;
489         long usec;
490
491         assert(ddir_rw(ddir));
492
493         if (td->rate_pending_usleep[ddir] <= 0)
494                 return ddir;
495
496         /*
497          * We have too much pending sleep in this direction. See if we
498          * should switch.
499          */
500         if (td_rw(td) && td->o.rwmix[odir]) {
501                 /*
502                  * Other direction does not have too much pending, switch
503                  */
504                 if (td->rate_pending_usleep[odir] < 100000)
505                         return odir;
506
507                 /*
508                  * Both directions have pending sleep. Sleep the minimum time
509                  * and deduct from both.
510                  */
511                 if (td->rate_pending_usleep[ddir] <=
512                         td->rate_pending_usleep[odir]) {
513                         usec = td->rate_pending_usleep[ddir];
514                 } else {
515                         usec = td->rate_pending_usleep[odir];
516                         ddir = odir;
517                 }
518         } else
519                 usec = td->rate_pending_usleep[ddir];
520
521         /*
522          * We are going to sleep, ensure that we flush anything pending as
523          * not to skew our latency numbers.
524          *
525          * Changed to only monitor 'in flight' requests here instead of the
526          * td->cur_depth, b/c td->cur_depth does not accurately represent
527          * io's that have been actually submitted to an async engine,
528          * and cur_depth is meaningless for sync engines.
529          */
530         if (td->io_u_in_flight) {
531                 int fio_unused ret;
532
533                 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
534         }
535
536         fio_gettime(&t, NULL);
537         usec_sleep(td, usec);
538         usec = utime_since_now(&t);
539
540         td->rate_pending_usleep[ddir] -= usec;
541
542         odir = ddir ^ 1;
543         if (td_rw(td) && __should_check_rate(td, odir))
544                 td->rate_pending_usleep[odir] -= usec;
545
546         if (ddir_trim(ddir))
547                 return ddir;
548
549         return ddir;
550 }
551
552 /*
553  * Return the data direction for the next io_u. If the job is a
554  * mixed read/write workload, check the rwmix cycle and switch if
555  * necessary.
556  */
557 static enum fio_ddir get_rw_ddir(struct thread_data *td)
558 {
559         enum fio_ddir ddir;
560
561         /*
562          * see if it's time to fsync
563          */
564         if (td->o.fsync_blocks &&
565            !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
566              td->io_issues[DDIR_WRITE] && should_fsync(td))
567                 return DDIR_SYNC;
568
569         /*
570          * see if it's time to fdatasync
571          */
572         if (td->o.fdatasync_blocks &&
573            !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
574              td->io_issues[DDIR_WRITE] && should_fsync(td))
575                 return DDIR_DATASYNC;
576
577         /*
578          * see if it's time to sync_file_range
579          */
580         if (td->sync_file_range_nr &&
581            !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
582              td->io_issues[DDIR_WRITE] && should_fsync(td))
583                 return DDIR_SYNC_FILE_RANGE;
584
585         if (td_rw(td)) {
586                 /*
587                  * Check if it's time to seed a new data direction.
588                  */
589                 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
590                         /*
591                          * Put a top limit on how many bytes we do for
592                          * one data direction, to avoid overflowing the
593                          * ranges too much
594                          */
595                         ddir = get_rand_ddir(td);
596
597                         if (ddir != td->rwmix_ddir)
598                                 set_rwmix_bytes(td);
599
600                         td->rwmix_ddir = ddir;
601                 }
602                 ddir = td->rwmix_ddir;
603         } else if (td_read(td))
604                 ddir = DDIR_READ;
605         else if (td_write(td))
606                 ddir = DDIR_WRITE;
607         else
608                 ddir = DDIR_TRIM;
609
610         td->rwmix_ddir = rate_ddir(td, ddir);
611         return td->rwmix_ddir;
612 }
613
614 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
615 {
616         io_u->ddir = io_u->acct_ddir = get_rw_ddir(td);
617
618         if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
619             td->o.barrier_blocks &&
620            !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
621              td->io_issues[DDIR_WRITE])
622                 io_u->flags |= IO_U_F_BARRIER;
623 }
624
625 void put_file_log(struct thread_data *td, struct fio_file *f)
626 {
627         int ret = put_file(td, f);
628
629         if (ret)
630                 td_verror(td, ret, "file close");
631 }
632
633 void put_io_u(struct thread_data *td, struct io_u *io_u)
634 {
635         td_io_u_lock(td);
636
637         if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
638                 put_file_log(td, io_u->file);
639         io_u->file = NULL;
640         io_u->flags &= ~IO_U_F_FREE_DEF;
641         io_u->flags |= IO_U_F_FREE;
642
643         if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
644                 td->cur_depth--;
645         flist_del_init(&io_u->list);
646         flist_add(&io_u->list, &td->io_u_freelist);
647         td_io_u_unlock(td);
648         td_io_u_free_notify(td);
649 }
650
651 void clear_io_u(struct thread_data *td, struct io_u *io_u)
652 {
653         io_u->flags &= ~IO_U_F_FLIGHT;
654         put_io_u(td, io_u);
655 }
656
657 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
658 {
659         struct io_u *__io_u = *io_u;
660         enum fio_ddir ddir = acct_ddir(__io_u);
661
662         dprint(FD_IO, "requeue %p\n", __io_u);
663
664         td_io_u_lock(td);
665
666         __io_u->flags |= IO_U_F_FREE;
667         if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
668                 td->io_issues[ddir]--;
669
670         __io_u->flags &= ~IO_U_F_FLIGHT;
671         if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
672                 td->cur_depth--;
673         flist_del(&__io_u->list);
674         flist_add_tail(&__io_u->list, &td->io_u_requeues);
675         td_io_u_unlock(td);
676         *io_u = NULL;
677 }
678
679 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
680 {
681         if (td->io_ops->flags & FIO_NOIO)
682                 goto out;
683
684         set_rw_ddir(td, io_u);
685
686         /*
687          * fsync() or fdatasync() or trim etc, we are done
688          */
689         if (!ddir_rw(io_u->ddir))
690                 goto out;
691
692         /*
693          * See if it's time to switch to a new zone
694          */
695         if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
696                 td->zone_bytes = 0;
697                 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
698                 io_u->file->last_pos = io_u->file->file_offset;
699                 td->io_skip_bytes += td->o.zone_skip;
700         }
701
702         /*
703          * No log, let the seq/rand engine retrieve the next buflen and
704          * position.
705          */
706         if (get_next_offset(td, io_u)) {
707                 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
708                 return 1;
709         }
710
711         io_u->buflen = get_next_buflen(td, io_u);
712         if (!io_u->buflen) {
713                 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
714                 return 1;
715         }
716
717         if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
718                 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
719                 dprint(FD_IO, "  off=%llu/%lu > %llu\n", io_u->offset,
720                                 io_u->buflen, io_u->file->real_file_size);
721                 return 1;
722         }
723
724         /*
725          * mark entry before potentially trimming io_u
726          */
727         if (td_random(td) && file_randommap(td, io_u->file))
728                 mark_random_map(td, io_u);
729
730 out:
731         dprint_io_u(io_u, "fill_io_u");
732         td->zone_bytes += io_u->buflen;
733         return 0;
734 }
735
736 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
737 {
738         int idx = 0;
739
740         switch (nr) {
741         default:
742                 idx = 6;
743                 break;
744         case 33 ... 64:
745                 idx = 5;
746                 break;
747         case 17 ... 32:
748                 idx = 4;
749                 break;
750         case 9 ... 16:
751                 idx = 3;
752                 break;
753         case 5 ... 8:
754                 idx = 2;
755                 break;
756         case 1 ... 4:
757                 idx = 1;
758         case 0:
759                 break;
760         }
761
762         map[idx]++;
763 }
764
765 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
766 {
767         __io_u_mark_map(td->ts.io_u_submit, nr);
768         td->ts.total_submit++;
769 }
770
771 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
772 {
773         __io_u_mark_map(td->ts.io_u_complete, nr);
774         td->ts.total_complete++;
775 }
776
777 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
778 {
779         int idx = 0;
780
781         switch (td->cur_depth) {
782         default:
783                 idx = 6;
784                 break;
785         case 32 ... 63:
786                 idx = 5;
787                 break;
788         case 16 ... 31:
789                 idx = 4;
790                 break;
791         case 8 ... 15:
792                 idx = 3;
793                 break;
794         case 4 ... 7:
795                 idx = 2;
796                 break;
797         case 2 ... 3:
798                 idx = 1;
799         case 1:
800                 break;
801         }
802
803         td->ts.io_u_map[idx] += nr;
804 }
805
806 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
807 {
808         int idx = 0;
809
810         assert(usec < 1000);
811
812         switch (usec) {
813         case 750 ... 999:
814                 idx = 9;
815                 break;
816         case 500 ... 749:
817                 idx = 8;
818                 break;
819         case 250 ... 499:
820                 idx = 7;
821                 break;
822         case 100 ... 249:
823                 idx = 6;
824                 break;
825         case 50 ... 99:
826                 idx = 5;
827                 break;
828         case 20 ... 49:
829                 idx = 4;
830                 break;
831         case 10 ... 19:
832                 idx = 3;
833                 break;
834         case 4 ... 9:
835                 idx = 2;
836                 break;
837         case 2 ... 3:
838                 idx = 1;
839         case 0 ... 1:
840                 break;
841         }
842
843         assert(idx < FIO_IO_U_LAT_U_NR);
844         td->ts.io_u_lat_u[idx]++;
845 }
846
847 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
848 {
849         int idx = 0;
850
851         switch (msec) {
852         default:
853                 idx = 11;
854                 break;
855         case 1000 ... 1999:
856                 idx = 10;
857                 break;
858         case 750 ... 999:
859                 idx = 9;
860                 break;
861         case 500 ... 749:
862                 idx = 8;
863                 break;
864         case 250 ... 499:
865                 idx = 7;
866                 break;
867         case 100 ... 249:
868                 idx = 6;
869                 break;
870         case 50 ... 99:
871                 idx = 5;
872                 break;
873         case 20 ... 49:
874                 idx = 4;
875                 break;
876         case 10 ... 19:
877                 idx = 3;
878                 break;
879         case 4 ... 9:
880                 idx = 2;
881                 break;
882         case 2 ... 3:
883                 idx = 1;
884         case 0 ... 1:
885                 break;
886         }
887
888         assert(idx < FIO_IO_U_LAT_M_NR);
889         td->ts.io_u_lat_m[idx]++;
890 }
891
892 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
893 {
894         if (usec < 1000)
895                 io_u_mark_lat_usec(td, usec);
896         else
897                 io_u_mark_lat_msec(td, usec / 1000);
898 }
899
900 /*
901  * Get next file to service by choosing one at random
902  */
903 static struct fio_file *get_next_file_rand(struct thread_data *td,
904                                            enum fio_file_flags goodf,
905                                            enum fio_file_flags badf)
906 {
907         struct fio_file *f;
908         int fno;
909
910         do {
911                 int opened = 0;
912                 unsigned long r;
913
914                 if (td->o.use_os_rand) {
915                         r = os_random_long(&td->next_file_state);
916                         fno = (unsigned int) ((double) td->o.nr_files
917                                 * (r / (OS_RAND_MAX + 1.0)));
918                 } else {
919                         r = __rand(&td->__next_file_state);
920                         fno = (unsigned int) ((double) td->o.nr_files
921                                 * (r / (FRAND_MAX + 1.0)));
922                 }
923
924                 f = td->files[fno];
925                 if (fio_file_done(f))
926                         continue;
927
928                 if (!fio_file_open(f)) {
929                         int err;
930
931                         err = td_io_open_file(td, f);
932                         if (err)
933                                 continue;
934                         opened = 1;
935                 }
936
937                 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
938                         dprint(FD_FILE, "get_next_file_rand: %p\n", f);
939                         return f;
940                 }
941                 if (opened)
942                         td_io_close_file(td, f);
943         } while (1);
944 }
945
946 /*
947  * Get next file to service by doing round robin between all available ones
948  */
949 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
950                                          int badf)
951 {
952         unsigned int old_next_file = td->next_file;
953         struct fio_file *f;
954
955         do {
956                 int opened = 0;
957
958                 f = td->files[td->next_file];
959
960                 td->next_file++;
961                 if (td->next_file >= td->o.nr_files)
962                         td->next_file = 0;
963
964                 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
965                 if (fio_file_done(f)) {
966                         f = NULL;
967                         continue;
968                 }
969
970                 if (!fio_file_open(f)) {
971                         int err;
972
973                         err = td_io_open_file(td, f);
974                         if (err) {
975                                 dprint(FD_FILE, "error %d on open of %s\n",
976                                         err, f->file_name);
977                                 f = NULL;
978                                 continue;
979                         }
980                         opened = 1;
981                 }
982
983                 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
984                                                                 f->flags);
985                 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
986                         break;
987
988                 if (opened)
989                         td_io_close_file(td, f);
990
991                 f = NULL;
992         } while (td->next_file != old_next_file);
993
994         dprint(FD_FILE, "get_next_file_rr: %p\n", f);
995         return f;
996 }
997
998 static struct fio_file *__get_next_file(struct thread_data *td)
999 {
1000         struct fio_file *f;
1001
1002         assert(td->o.nr_files <= td->files_index);
1003
1004         if (td->nr_done_files >= td->o.nr_files) {
1005                 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1006                                 " nr_files=%d\n", td->nr_open_files,
1007                                                   td->nr_done_files,
1008                                                   td->o.nr_files);
1009                 return NULL;
1010         }
1011
1012         f = td->file_service_file;
1013         if (f && fio_file_open(f) && !fio_file_closing(f)) {
1014                 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1015                         goto out;
1016                 if (td->file_service_left--)
1017                         goto out;
1018         }
1019
1020         if (td->o.file_service_type == FIO_FSERVICE_RR ||
1021             td->o.file_service_type == FIO_FSERVICE_SEQ)
1022                 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1023         else
1024                 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1025
1026         td->file_service_file = f;
1027         td->file_service_left = td->file_service_nr - 1;
1028 out:
1029         dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1030         return f;
1031 }
1032
1033 static struct fio_file *get_next_file(struct thread_data *td)
1034 {
1035         if (!(td->flags & TD_F_PROFILE_OPS)) {
1036                 struct prof_io_ops *ops = &td->prof_io_ops;
1037
1038                 if (ops->get_next_file)
1039                         return ops->get_next_file(td);
1040         }
1041
1042         return __get_next_file(td);
1043 }
1044
1045 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
1046 {
1047         struct fio_file *f;
1048
1049         do {
1050                 f = get_next_file(td);
1051                 if (!f)
1052                         return 1;
1053
1054                 io_u->file = f;
1055                 get_file(f);
1056
1057                 if (!fill_io_u(td, io_u))
1058                         break;
1059
1060                 put_file_log(td, f);
1061                 td_io_close_file(td, f);
1062                 io_u->file = NULL;
1063                 fio_file_set_done(f);
1064                 td->nr_done_files++;
1065                 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1066                                         td->nr_done_files, td->o.nr_files);
1067         } while (1);
1068
1069         return 0;
1070 }
1071
1072
1073 struct io_u *__get_io_u(struct thread_data *td)
1074 {
1075         struct io_u *io_u = NULL;
1076
1077         td_io_u_lock(td);
1078
1079 again:
1080         if (!flist_empty(&td->io_u_requeues))
1081                 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1082         else if (!queue_full(td)) {
1083                 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1084
1085                 io_u->buflen = 0;
1086                 io_u->resid = 0;
1087                 io_u->file = NULL;
1088                 io_u->end_io = NULL;
1089         }
1090
1091         if (io_u) {
1092                 assert(io_u->flags & IO_U_F_FREE);
1093                 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1094                 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1095                 io_u->flags &= ~IO_U_F_VER_LIST;
1096
1097                 io_u->error = 0;
1098                 io_u->acct_ddir = -1;
1099                 flist_del(&io_u->list);
1100                 flist_add_tail(&io_u->list, &td->io_u_busylist);
1101                 td->cur_depth++;
1102                 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1103         } else if (td->o.verify_async) {
1104                 /*
1105                  * We ran out, wait for async verify threads to finish and
1106                  * return one
1107                  */
1108                 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1109                 goto again;
1110         }
1111
1112         td_io_u_unlock(td);
1113         return io_u;
1114 }
1115
1116 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1117 {
1118         if (!(td->flags & TD_F_TRIM_BACKLOG))
1119                 return 0;
1120
1121         if (td->trim_entries) {
1122                 int get_trim = 0;
1123
1124                 if (td->trim_batch) {
1125                         td->trim_batch--;
1126                         get_trim = 1;
1127                 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1128                          td->last_ddir != DDIR_READ) {
1129                         td->trim_batch = td->o.trim_batch;
1130                         if (!td->trim_batch)
1131                                 td->trim_batch = td->o.trim_backlog;
1132                         get_trim = 1;
1133                 }
1134
1135                 if (get_trim && !get_next_trim(td, io_u))
1136                         return 1;
1137         }
1138
1139         return 0;
1140 }
1141
1142 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1143 {
1144         if (!(td->flags & TD_F_VER_BACKLOG))
1145                 return 0;
1146
1147         if (td->io_hist_len) {
1148                 int get_verify = 0;
1149
1150                 if (td->verify_batch)
1151                         get_verify = 1;
1152                 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1153                          td->last_ddir != DDIR_READ) {
1154                         td->verify_batch = td->o.verify_batch;
1155                         if (!td->verify_batch)
1156                                 td->verify_batch = td->o.verify_backlog;
1157                         get_verify = 1;
1158                 }
1159
1160                 if (get_verify && !get_next_verify(td, io_u)) {
1161                         td->verify_batch--;
1162                         return 1;
1163                 }
1164         }
1165
1166         return 0;
1167 }
1168
1169 /*
1170  * Fill offset and start time into the buffer content, to prevent too
1171  * easy compressible data for simple de-dupe attempts. Do this for every
1172  * 512b block in the range, since that should be the smallest block size
1173  * we can expect from a device.
1174  */
1175 static void small_content_scramble(struct io_u *io_u)
1176 {
1177         unsigned int i, nr_blocks = io_u->buflen / 512;
1178         uint64_t boffset;
1179         unsigned int offset;
1180         void *p, *end;
1181
1182         if (!nr_blocks)
1183                 return;
1184
1185         p = io_u->xfer_buf;
1186         boffset = io_u->offset;
1187         io_u->buf_filled_len = 0;
1188
1189         for (i = 0; i < nr_blocks; i++) {
1190                 /*
1191                  * Fill the byte offset into a "random" start offset of
1192                  * the buffer, given by the product of the usec time
1193                  * and the actual offset.
1194                  */
1195                 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1196                 offset &= ~(sizeof(uint64_t) - 1);
1197                 if (offset >= 512 - sizeof(uint64_t))
1198                         offset -= sizeof(uint64_t);
1199                 memcpy(p + offset, &boffset, sizeof(boffset));
1200
1201                 end = p + 512 - sizeof(io_u->start_time);
1202                 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1203                 p += 512;
1204                 boffset += 512;
1205         }
1206 }
1207
1208 /*
1209  * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1210  * etc. The returned io_u is fully ready to be prepped and submitted.
1211  */
1212 struct io_u *get_io_u(struct thread_data *td)
1213 {
1214         struct fio_file *f;
1215         struct io_u *io_u;
1216         int do_scramble = 0;
1217
1218         io_u = __get_io_u(td);
1219         if (!io_u) {
1220                 dprint(FD_IO, "__get_io_u failed\n");
1221                 return NULL;
1222         }
1223
1224         if (check_get_verify(td, io_u))
1225                 goto out;
1226         if (check_get_trim(td, io_u))
1227                 goto out;
1228
1229         /*
1230          * from a requeue, io_u already setup
1231          */
1232         if (io_u->file)
1233                 goto out;
1234
1235         /*
1236          * If using an iolog, grab next piece if any available.
1237          */
1238         if (td->flags & TD_F_READ_IOLOG) {
1239                 if (read_iolog_get(td, io_u))
1240                         goto err_put;
1241         } else if (set_io_u_file(td, io_u)) {
1242                 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1243                 goto err_put;
1244         }
1245
1246         f = io_u->file;
1247         assert(fio_file_open(f));
1248
1249         if (ddir_rw(io_u->ddir)) {
1250                 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1251                         dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1252                         goto err_put;
1253                 }
1254
1255                 f->last_start = io_u->offset;
1256                 f->last_pos = io_u->offset + io_u->buflen;
1257
1258                 if (io_u->ddir == DDIR_WRITE) {
1259                         if (td->flags & TD_F_REFILL_BUFFERS) {
1260                                 io_u_fill_buffer(td, io_u,
1261                                         io_u->xfer_buflen, io_u->xfer_buflen);
1262                         } else if (td->flags & TD_F_SCRAMBLE_BUFFERS)
1263                                 do_scramble = 1;
1264                         if (td->flags & TD_F_VER_NONE) {
1265                                 populate_verify_io_u(td, io_u);
1266                                 do_scramble = 0;
1267                         }
1268                 } else if (io_u->ddir == DDIR_READ) {
1269                         /*
1270                          * Reset the buf_filled parameters so next time if the
1271                          * buffer is used for writes it is refilled.
1272                          */
1273                         io_u->buf_filled_len = 0;
1274                 }
1275         }
1276
1277         /*
1278          * Set io data pointers.
1279          */
1280         io_u->xfer_buf = io_u->buf;
1281         io_u->xfer_buflen = io_u->buflen;
1282
1283 out:
1284         assert(io_u->file);
1285         if (!td_io_prep(td, io_u)) {
1286                 if (!td->o.disable_slat)
1287                         fio_gettime(&io_u->start_time, NULL);
1288                 if (do_scramble)
1289                         small_content_scramble(io_u);
1290                 return io_u;
1291         }
1292 err_put:
1293         dprint(FD_IO, "get_io_u failed\n");
1294         put_io_u(td, io_u);
1295         return NULL;
1296 }
1297
1298 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1299 {
1300         enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1301         const char *msg[] = { "read", "write", "sync", "datasync",
1302                                 "sync_file_range", "wait", "trim" };
1303
1304         if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1305                 return;
1306
1307         log_err("fio: io_u error");
1308
1309         if (io_u->file)
1310                 log_err(" on file %s", io_u->file->file_name);
1311
1312         log_err(": %s\n", strerror(io_u->error));
1313
1314         log_err("     %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1315                                         io_u->offset, io_u->xfer_buflen);
1316
1317         if (!td->error)
1318                 td_verror(td, io_u->error, "io_u error");
1319 }
1320
1321 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1322                                   struct io_completion_data *icd,
1323                                   const enum fio_ddir idx, unsigned int bytes)
1324 {
1325         unsigned long lusec = 0;
1326
1327         if (!td->o.disable_clat || !td->o.disable_bw)
1328                 lusec = utime_since(&io_u->issue_time, &icd->time);
1329
1330         if (!td->o.disable_lat) {
1331                 unsigned long tusec;
1332
1333                 tusec = utime_since(&io_u->start_time, &icd->time);
1334                 add_lat_sample(td, idx, tusec, bytes);
1335
1336                 if (td->o.max_latency && tusec > td->o.max_latency) {
1337                         if (!td->error)
1338                                 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1339                         td_verror(td, ETIMEDOUT, "max latency exceeded");
1340                         icd->error = ETIMEDOUT;
1341                 }
1342         }
1343
1344         if (!td->o.disable_clat) {
1345                 add_clat_sample(td, idx, lusec, bytes);
1346                 io_u_mark_latency(td, lusec);
1347         }
1348
1349         if (!td->o.disable_bw)
1350                 add_bw_sample(td, idx, bytes, &icd->time);
1351
1352         add_iops_sample(td, idx, &icd->time);
1353 }
1354
1355 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1356 {
1357         uint64_t secs, remainder, bps, bytes;
1358
1359         bytes = td->this_io_bytes[ddir];
1360         bps = td->rate_bps[ddir];
1361         secs = bytes / bps;
1362         remainder = bytes % bps;
1363         return remainder * 1000000 / bps + secs * 1000000;
1364 }
1365
1366 static void io_completed(struct thread_data *td, struct io_u *io_u,
1367                          struct io_completion_data *icd)
1368 {
1369         struct fio_file *f;
1370
1371         dprint_io_u(io_u, "io complete");
1372
1373         td_io_u_lock(td);
1374         assert(io_u->flags & IO_U_F_FLIGHT);
1375         io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1376         td_io_u_unlock(td);
1377
1378         if (ddir_sync(io_u->ddir)) {
1379                 td->last_was_sync = 1;
1380                 f = io_u->file;
1381                 if (f) {
1382                         f->first_write = -1ULL;
1383                         f->last_write = -1ULL;
1384                 }
1385                 return;
1386         }
1387
1388         td->last_was_sync = 0;
1389         td->last_ddir = io_u->ddir;
1390
1391         if (!io_u->error && ddir_rw(io_u->ddir)) {
1392                 unsigned int bytes = io_u->buflen - io_u->resid;
1393                 const enum fio_ddir idx = io_u->ddir;
1394                 const enum fio_ddir odx = io_u->ddir ^ 1;
1395                 int ret;
1396
1397                 td->io_blocks[idx]++;
1398                 td->this_io_blocks[idx]++;
1399                 td->io_bytes[idx] += bytes;
1400
1401                 if (!(io_u->flags & IO_U_F_VER_LIST))
1402                         td->this_io_bytes[idx] += bytes;
1403
1404                 if (idx == DDIR_WRITE) {
1405                         f = io_u->file;
1406                         if (f) {
1407                                 if (f->first_write == -1ULL ||
1408                                     io_u->offset < f->first_write)
1409                                         f->first_write = io_u->offset;
1410                                 if (f->last_write == -1ULL ||
1411                                     ((io_u->offset + bytes) > f->last_write))
1412                                         f->last_write = io_u->offset + bytes;
1413                         }
1414                 }
1415
1416                 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1417                                            td->runstate == TD_VERIFYING)) {
1418                         account_io_completion(td, io_u, icd, idx, bytes);
1419
1420                         if (__should_check_rate(td, idx)) {
1421                                 td->rate_pending_usleep[idx] =
1422                                         (usec_for_io(td, idx) -
1423                                          utime_since_now(&td->start));
1424                         }
1425                         if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1426                                 td->rate_pending_usleep[odx] =
1427                                         (usec_for_io(td, odx) -
1428                                          utime_since_now(&td->start));
1429                 }
1430
1431                 if (td_write(td) && idx == DDIR_WRITE &&
1432                     td->o.do_verify &&
1433                     td->o.verify != VERIFY_NONE &&
1434                     !td->o.experimental_verify)
1435                         log_io_piece(td, io_u);
1436
1437                 icd->bytes_done[idx] += bytes;
1438
1439                 if (io_u->end_io) {
1440                         ret = io_u->end_io(td, io_u);
1441                         if (ret && !icd->error)
1442                                 icd->error = ret;
1443                 }
1444         } else if (io_u->error) {
1445                 icd->error = io_u->error;
1446                 io_u_log_error(td, io_u);
1447         }
1448         if (icd->error) {
1449                 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1450                 if (!td_non_fatal_error(td, eb, icd->error))
1451                         return;
1452                 /*
1453                  * If there is a non_fatal error, then add to the error count
1454                  * and clear all the errors.
1455                  */
1456                 update_error_count(td, icd->error);
1457                 td_clear_error(td);
1458                 icd->error = 0;
1459                 io_u->error = 0;
1460         }
1461 }
1462
1463 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1464                      int nr)
1465 {
1466         int ddir;
1467         if (!td->o.disable_clat || !td->o.disable_bw)
1468                 fio_gettime(&icd->time, NULL);
1469
1470         icd->nr = nr;
1471
1472         icd->error = 0;
1473         for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1474                 icd->bytes_done[ddir] = 0;
1475 }
1476
1477 static void ios_completed(struct thread_data *td,
1478                           struct io_completion_data *icd)
1479 {
1480         struct io_u *io_u;
1481         int i;
1482
1483         for (i = 0; i < icd->nr; i++) {
1484                 io_u = td->io_ops->event(td, i);
1485
1486                 io_completed(td, io_u, icd);
1487
1488                 if (!(io_u->flags & IO_U_F_FREE_DEF))
1489                         put_io_u(td, io_u);
1490         }
1491 }
1492
1493 /*
1494  * Complete a single io_u for the sync engines.
1495  */
1496 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1497                        uint64_t *bytes)
1498 {
1499         struct io_completion_data icd;
1500
1501         init_icd(td, &icd, 1);
1502         io_completed(td, io_u, &icd);
1503
1504         if (!(io_u->flags & IO_U_F_FREE_DEF))
1505                 put_io_u(td, io_u);
1506
1507         if (icd.error) {
1508                 td_verror(td, icd.error, "io_u_sync_complete");
1509                 return -1;
1510         }
1511
1512         if (bytes) {
1513                 int ddir;
1514
1515                 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1516                         bytes[ddir] += icd.bytes_done[ddir];
1517         }
1518
1519         return 0;
1520 }
1521
1522 /*
1523  * Called to complete min_events number of io for the async engines.
1524  */
1525 int io_u_queued_complete(struct thread_data *td, int min_evts,
1526                          uint64_t *bytes)
1527 {
1528         struct io_completion_data icd;
1529         struct timespec *tvp = NULL;
1530         int ret;
1531         struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1532
1533         dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1534
1535         if (!min_evts)
1536                 tvp = &ts;
1537
1538         ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1539         if (ret < 0) {
1540                 td_verror(td, -ret, "td_io_getevents");
1541                 return ret;
1542         } else if (!ret)
1543                 return ret;
1544
1545         init_icd(td, &icd, ret);
1546         ios_completed(td, &icd);
1547         if (icd.error) {
1548                 td_verror(td, icd.error, "io_u_queued_complete");
1549                 return -1;
1550         }
1551
1552         if (bytes) {
1553                 int ddir;
1554
1555                 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1556                         bytes[ddir] += icd.bytes_done[ddir];
1557         }
1558
1559         return 0;
1560 }
1561
1562 /*
1563  * Call when io_u is really queued, to update the submission latency.
1564  */
1565 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1566 {
1567         if (!td->o.disable_slat) {
1568                 unsigned long slat_time;
1569
1570                 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1571                 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1572         }
1573 }
1574
1575 /*
1576  * "randomly" fill the buffer contents
1577  */
1578 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1579                       unsigned int min_write, unsigned int max_bs)
1580 {
1581         io_u->buf_filled_len = 0;
1582
1583         if (!td->o.zero_buffers) {
1584                 unsigned int perc = td->o.compress_percentage;
1585
1586                 if (perc) {
1587                         unsigned int seg = min_write;
1588
1589                         seg = min(min_write, td->o.compress_chunk);
1590                         fill_random_buf_percentage(&td->buf_state, io_u->buf,
1591                                                 perc, seg, max_bs);
1592                 } else
1593                         fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1594         } else
1595                 memset(io_u->buf, 0, max_bs);
1596 }