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