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