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