forked from axboe/fio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_u.c
2481 lines (2061 loc) · 55.4 KB
/
io_u.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "fio.h"
#include "verify.h"
#include "trim.h"
#include "lib/rand.h"
#include "lib/axmap.h"
#include "err.h"
#include "lib/pow2.h"
#include "minmax.h"
#include "zbd.h"
struct io_completion_data {
int nr; /* input */
int error; /* output */
uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
struct timespec time; /* output */
};
/*
* The ->io_axmap contains a map of blocks we have or have not done io
* to yet. Used to make sure we cover the entire range in a fair fashion.
*/
static bool random_map_free(struct fio_file *f, const uint64_t block)
{
return !axmap_isset(f->io_axmap, block);
}
/*
* Mark a given offset as used in the map.
*/
static uint64_t mark_random_map(struct thread_data *td, struct io_u *io_u,
uint64_t offset, uint64_t buflen)
{
unsigned long long min_bs = td->o.min_bs[io_u->ddir];
struct fio_file *f = io_u->file;
unsigned long long nr_blocks;
uint64_t block;
block = (offset - f->file_offset) / (uint64_t) min_bs;
nr_blocks = (buflen + min_bs - 1) / min_bs;
assert(nr_blocks > 0);
if (!(io_u->flags & IO_U_F_BUSY_OK)) {
nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
assert(nr_blocks > 0);
}
if ((nr_blocks * min_bs) < buflen)
buflen = nr_blocks * min_bs;
return buflen;
}
static uint64_t last_block(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir)
{
uint64_t max_blocks;
uint64_t max_size;
assert(ddir_rw(ddir));
/*
* Hmm, should we make sure that ->io_size <= ->real_file_size?
* -> not for now since there is code assuming it could go either.
*/
max_size = f->io_size;
if (max_size > f->real_file_size)
max_size = f->real_file_size;
if (td->o.zone_mode == ZONE_MODE_STRIDED && td->o.zone_range)
max_size = td->o.zone_range;
if (td->o.min_bs[ddir] > td->o.ba[ddir])
max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
max_blocks = max_size / (uint64_t) td->o.ba[ddir];
if (!max_blocks)
return 0;
return max_blocks;
}
static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir, uint64_t *b,
uint64_t lastb)
{
uint64_t r;
if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
r = __rand(&td->random_state);
dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
*b = lastb * (r / (rand_max(&td->random_state) + 1.0));
} else {
uint64_t off = 0;
assert(fio_file_lfsr(f));
if (lfsr_next(&f->lfsr, &off))
return 1;
*b = off;
}
/*
* if we are not maintaining a random map, we are done.
*/
if (!file_randommap(td, f))
goto ret;
/*
* calculate map offset and check if it's free
*/
if (random_map_free(f, *b))
goto ret;
dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
(unsigned long long) *b);
*b = axmap_next_free(f->io_axmap, *b);
if (*b == (uint64_t) -1ULL)
return 1;
ret:
return 0;
}
static int __get_next_rand_offset_zipf(struct thread_data *td,
struct fio_file *f, enum fio_ddir ddir,
uint64_t *b)
{
*b = zipf_next(&f->zipf);
return 0;
}
static int __get_next_rand_offset_pareto(struct thread_data *td,
struct fio_file *f, enum fio_ddir ddir,
uint64_t *b)
{
*b = pareto_next(&f->zipf);
return 0;
}
static int __get_next_rand_offset_gauss(struct thread_data *td,
struct fio_file *f, enum fio_ddir ddir,
uint64_t *b)
{
*b = gauss_next(&f->gauss);
return 0;
}
static int __get_next_rand_offset_zoned_abs(struct thread_data *td,
struct fio_file *f,
enum fio_ddir ddir, uint64_t *b)
{
struct zone_split_index *zsi;
uint64_t lastb, send, stotal;
unsigned int v;
lastb = last_block(td, f, ddir);
if (!lastb)
return 1;
if (!td->o.zone_split_nr[ddir]) {
bail:
return __get_next_rand_offset(td, f, ddir, b, lastb);
}
/*
* Generate a value, v, between 1 and 100, both inclusive
*/
v = rand_between(&td->zone_state, 1, 100);
/*
* Find our generated table. 'send' is the end block of this zone,
* 'stotal' is our start offset.
*/
zsi = &td->zone_state_index[ddir][v - 1];
stotal = zsi->size_prev / td->o.ba[ddir];
send = zsi->size / td->o.ba[ddir];
/*
* Should never happen
*/
if (send == -1U) {
if (!fio_did_warn(FIO_WARN_ZONED_BUG))
log_err("fio: bug in zoned generation\n");
goto bail;
} else if (send > lastb) {
/*
* This happens if the user specifies ranges that exceed
* the file/device size. We can't handle that gracefully,
* so error and exit.
*/
log_err("fio: zoned_abs sizes exceed file size\n");
return 1;
}
/*
* Generate index from 0..send-stotal
*/
if (__get_next_rand_offset(td, f, ddir, b, send - stotal) == 1)
return 1;
*b += stotal;
return 0;
}
static int __get_next_rand_offset_zoned(struct thread_data *td,
struct fio_file *f, enum fio_ddir ddir,
uint64_t *b)
{
unsigned int v, send, stotal;
uint64_t offset, lastb;
struct zone_split_index *zsi;
lastb = last_block(td, f, ddir);
if (!lastb)
return 1;
if (!td->o.zone_split_nr[ddir]) {
bail:
return __get_next_rand_offset(td, f, ddir, b, lastb);
}
/*
* Generate a value, v, between 1 and 100, both inclusive
*/
v = rand_between(&td->zone_state, 1, 100);
zsi = &td->zone_state_index[ddir][v - 1];
stotal = zsi->size_perc_prev;
send = zsi->size_perc;
/*
* Should never happen
*/
if (send == -1U) {
if (!fio_did_warn(FIO_WARN_ZONED_BUG))
log_err("fio: bug in zoned generation\n");
goto bail;
}
/*
* 'send' is some percentage below or equal to 100 that
* marks the end of the current IO range. 'stotal' marks
* the start, in percent.
*/
if (stotal)
offset = stotal * lastb / 100ULL;
else
offset = 0;
lastb = lastb * (send - stotal) / 100ULL;
/*
* Generate index from 0..send-of-lastb
*/
if (__get_next_rand_offset(td, f, ddir, b, lastb) == 1)
return 1;
/*
* Add our start offset, if any
*/
if (offset)
*b += offset;
return 0;
}
static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir, uint64_t *b)
{
if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) {
uint64_t lastb;
lastb = last_block(td, f, ddir);
if (!lastb)
return 1;
return __get_next_rand_offset(td, f, ddir, b, lastb);
} else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
return __get_next_rand_offset_zipf(td, f, ddir, b);
else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
return __get_next_rand_offset_pareto(td, f, ddir, b);
else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
return __get_next_rand_offset_gauss(td, f, ddir, b);
else if (td->o.random_distribution == FIO_RAND_DIST_ZONED)
return __get_next_rand_offset_zoned(td, f, ddir, b);
else if (td->o.random_distribution == FIO_RAND_DIST_ZONED_ABS)
return __get_next_rand_offset_zoned_abs(td, f, ddir, b);
log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
return 1;
}
static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
{
unsigned int v;
if (td->o.perc_rand[ddir] == 100)
return true;
v = rand_between(&td->seq_rand_state[ddir], 1, 100);
return v <= td->o.perc_rand[ddir];
}
static void loop_cache_invalidate(struct thread_data *td, struct fio_file *f)
{
struct thread_options *o = &td->o;
if (o->invalidate_cache && !o->odirect) {
int fio_unused ret;
ret = file_invalidate_cache(td, f);
}
}
static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir, uint64_t *b)
{
if (!get_next_rand_offset(td, f, ddir, b))
return 0;
if (td->o.time_based ||
(td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
fio_file_reset(td, f);
loop_cache_invalidate(td, f);
if (!get_next_rand_offset(td, f, ddir, b))
return 0;
}
dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
f->file_name, (unsigned long long) f->last_pos[ddir],
(unsigned long long) f->real_file_size);
return 1;
}
static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir, uint64_t *offset)
{
struct thread_options *o = &td->o;
assert(ddir_rw(ddir));
/*
* If we reach the end for a time based run, reset us back to 0
* and invalidate the cache, if we need to.
*/
if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
o->time_based && o->nr_files == 1) {
f->last_pos[ddir] = f->file_offset;
loop_cache_invalidate(td, f);
}
if (f->last_pos[ddir] < f->real_file_size) {
uint64_t pos;
/*
* Only rewind if we already hit the end
*/
if (f->last_pos[ddir] == f->file_offset &&
f->file_offset && o->ddir_seq_add < 0) {
if (f->real_file_size > f->io_size)
f->last_pos[ddir] = f->io_size;
else
f->last_pos[ddir] = f->real_file_size;
}
pos = f->last_pos[ddir] - f->file_offset;
if (pos && o->ddir_seq_add) {
pos += o->ddir_seq_add;
/*
* If we reach beyond the end of the file
* with holed IO, wrap around to the
* beginning again. If we're doing backwards IO,
* wrap to the end.
*/
if (pos >= f->real_file_size) {
if (o->ddir_seq_add > 0)
pos = f->file_offset;
else {
if (f->real_file_size > f->io_size)
pos = f->io_size;
else
pos = f->real_file_size;
pos += o->ddir_seq_add;
}
}
}
*offset = pos;
return 0;
}
return 1;
}
static int get_next_block(struct thread_data *td, struct io_u *io_u,
enum fio_ddir ddir, int rw_seq,
bool *is_random)
{
struct fio_file *f = io_u->file;
uint64_t b, offset;
int ret;
assert(ddir_rw(ddir));
b = offset = -1ULL;
if (td_randtrimwrite(td) && ddir == DDIR_WRITE) {
/* don't mark randommap for these writes */
io_u_set(td, io_u, IO_U_F_BUSY_OK);
offset = f->last_start[DDIR_TRIM];
*is_random = true;
ret = 0;
} else if (rw_seq) {
if (td_random(td)) {
if (should_do_random(td, ddir)) {
ret = get_next_rand_block(td, f, ddir, &b);
*is_random = true;
} else {
*is_random = false;
io_u_set(td, io_u, IO_U_F_BUSY_OK);
ret = get_next_seq_offset(td, f, ddir, &offset);
if (ret)
ret = get_next_rand_block(td, f, ddir, &b);
}
} else {
*is_random = false;
ret = get_next_seq_offset(td, f, ddir, &offset);
}
} else {
io_u_set(td, io_u, IO_U_F_BUSY_OK);
*is_random = false;
if (td->o.rw_seq == RW_SEQ_SEQ) {
ret = get_next_seq_offset(td, f, ddir, &offset);
if (ret) {
ret = get_next_rand_block(td, f, ddir, &b);
*is_random = false;
}
} else if (td->o.rw_seq == RW_SEQ_IDENT) {
if (f->last_start[ddir] != -1ULL)
offset = f->last_start[ddir] - f->file_offset;
else
offset = 0;
ret = 0;
} else {
log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
ret = 1;
}
}
if (!ret) {
if (offset != -1ULL)
io_u->offset = offset;
else if (b != -1ULL)
io_u->offset = b * td->o.ba[ddir];
else {
log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
ret = 1;
}
io_u->verify_offset = io_u->offset;
}
return ret;
}
/*
* For random io, generate a random new block and see if it's used. Repeat
* until we find a free one. For sequential io, just return the end of
* the last io issued.
*/
static int get_next_offset(struct thread_data *td, struct io_u *io_u,
bool *is_random)
{
struct fio_file *f = io_u->file;
enum fio_ddir ddir = io_u->ddir;
int rw_seq_hit = 0;
assert(ddir_rw(ddir));
if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
rw_seq_hit = 1;
td->ddir_seq_nr = td->o.ddir_seq_nr;
}
if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
return 1;
if (io_u->offset >= f->io_size) {
dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
(unsigned long long) io_u->offset,
(unsigned long long) f->io_size);
return 1;
}
io_u->offset += f->file_offset;
if (io_u->offset >= f->real_file_size) {
dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
(unsigned long long) io_u->offset,
(unsigned long long) f->real_file_size);
return 1;
}
/*
* For randtrimwrite, we decide whether to issue a trim or a write
* based on whether the offsets for the most recent trim and write
* operations match. If they don't match that means we just issued a
* new trim and the next operation should be a write. If they *do*
* match that means we just completed a trim+write pair and the next
* command should be a trim.
*
* This works fine for sequential workloads but for random workloads
* it's possible to complete a trim+write pair and then have the next
* randomly generated offset match the previous offset. If that happens
* we need to alter the offset for the last write operation in order
* to ensure that we issue a write operation the next time through.
*/
if (td_randtrimwrite(td) && ddir == DDIR_TRIM &&
f->last_start[DDIR_TRIM] == io_u->offset)
f->last_start[DDIR_WRITE]--;
io_u->verify_offset = io_u->offset;
return 0;
}
static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
unsigned long long buflen)
{
struct fio_file *f = io_u->file;
return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
}
static unsigned long long get_next_buflen(struct thread_data *td, struct io_u *io_u,
bool is_random)
{
int ddir = io_u->ddir;
unsigned long long buflen = 0;
unsigned long long minbs, maxbs;
uint64_t frand_max, r;
bool power_2;
assert(ddir_rw(ddir));
if (td_randtrimwrite(td) && ddir == DDIR_WRITE) {
struct fio_file *f = io_u->file;
return f->last_pos[DDIR_TRIM] - f->last_start[DDIR_TRIM];
}
if (td->o.bs_is_seq_rand)
ddir = is_random ? DDIR_WRITE : DDIR_READ;
minbs = td->o.min_bs[ddir];
maxbs = td->o.max_bs[ddir];
if (minbs == maxbs)
return minbs;
/*
* If we can't satisfy the min block size from here, then fail
*/
if (!io_u_fits(td, io_u, minbs))
return 0;
frand_max = rand_max(&td->bsrange_state[ddir]);
do {
r = __rand(&td->bsrange_state[ddir]);
if (!td->o.bssplit_nr[ddir]) {
buflen = minbs + (unsigned long long) ((double) maxbs *
(r / (frand_max + 1.0)));
} else {
long long perc = 0;
unsigned int i;
for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
struct bssplit *bsp = &td->o.bssplit[ddir][i];
if (!bsp->perc)
continue;
buflen = bsp->bs;
perc += bsp->perc;
if ((r / perc <= frand_max / 100ULL) &&
io_u_fits(td, io_u, buflen))
break;
}
}
power_2 = is_power_of_2(minbs);
if (!td->o.bs_unaligned && power_2)
buflen &= ~(minbs - 1);
else if (!td->o.bs_unaligned && !power_2)
buflen -= buflen % minbs;
if (buflen > maxbs)
buflen = maxbs;
} while (!io_u_fits(td, io_u, buflen));
return buflen;
}
static void set_rwmix_bytes(struct thread_data *td)
{
unsigned int diff;
/*
* we do time or byte based switch. this is needed because
* buffered writes may issue a lot quicker than they complete,
* whereas reads do not.
*/
diff = td->o.rwmix[td->rwmix_ddir ^ 1];
td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
}
static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
{
unsigned int v;
v = rand_between(&td->rwmix_state, 1, 100);
if (v <= td->o.rwmix[DDIR_READ])
return DDIR_READ;
return DDIR_WRITE;
}
int io_u_quiesce(struct thread_data *td)
{
int ret = 0, completed = 0, err = 0;
/*
* We are going to sleep, ensure that we flush anything pending as
* not to skew our latency numbers.
*
* Changed to only monitor 'in flight' requests here instead of the
* td->cur_depth, b/c td->cur_depth does not accurately represent
* io's that have been actually submitted to an async engine,
* and cur_depth is meaningless for sync engines.
*/
if (td->io_u_queued || td->cur_depth)
td_io_commit(td);
while (td->io_u_in_flight) {
ret = io_u_queued_complete(td, 1);
if (ret > 0)
completed += ret;
else if (ret < 0)
err = ret;
}
if (td->flags & TD_F_REGROW_LOGS)
regrow_logs(td);
if (completed)
return completed;
return err;
}
static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
{
enum fio_ddir odir = ddir ^ 1;
uint64_t usec;
uint64_t now;
assert(ddir_rw(ddir));
now = utime_since_now(&td->epoch);
/*
* if rate_next_io_time is in the past, need to catch up to rate
*/
if (td->rate_next_io_time[ddir] <= now)
return ddir;
/*
* We are ahead of rate in this direction. See if we
* should switch.
*/
if (td_rw(td) && td->o.rwmix[odir]) {
/*
* Other direction is behind rate, switch
*/
if (td->rate_next_io_time[odir] <= now)
return odir;
/*
* Both directions are ahead of rate. sleep the min,
* switch if necessary
*/
if (td->rate_next_io_time[ddir] <=
td->rate_next_io_time[odir]) {
usec = td->rate_next_io_time[ddir] - now;
} else {
usec = td->rate_next_io_time[odir] - now;
ddir = odir;
}
} else
usec = td->rate_next_io_time[ddir] - now;
if (td->o.io_submit_mode == IO_MODE_INLINE)
io_u_quiesce(td);
if (td->o.timeout && ((usec + now) > td->o.timeout)) {
/*
* check if the usec is capable of taking negative values
*/
if (now > td->o.timeout) {
ddir = DDIR_TIMEOUT;
return ddir;
}
usec = td->o.timeout - now;
}
usec_sleep(td, usec);
now = utime_since_now(&td->epoch);
if ((td->o.timeout && (now > td->o.timeout)) || td->terminate)
ddir = DDIR_TIMEOUT;
return ddir;
}
/*
* Return the data direction for the next io_u. If the job is a
* mixed read/write workload, check the rwmix cycle and switch if
* necessary.
*/
static enum fio_ddir get_rw_ddir(struct thread_data *td)
{
enum fio_ddir ddir;
/*
* See if it's time to fsync/fdatasync/sync_file_range first,
* and if not then move on to check regular I/Os.
*/
if (should_fsync(td)) {
if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
!(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
return DDIR_SYNC;
if (td->o.fdatasync_blocks && td->io_issues[DDIR_WRITE] &&
!(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks))
return DDIR_DATASYNC;
if (td->sync_file_range_nr && td->io_issues[DDIR_WRITE] &&
!(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr))
return DDIR_SYNC_FILE_RANGE;
}
if (td_rw(td)) {
/*
* Check if it's time to seed a new data direction.
*/
if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
/*
* Put a top limit on how many bytes we do for
* one data direction, to avoid overflowing the
* ranges too much
*/
ddir = get_rand_ddir(td);
if (ddir != td->rwmix_ddir)
set_rwmix_bytes(td);
td->rwmix_ddir = ddir;
}
ddir = td->rwmix_ddir;
} else if (td_read(td))
ddir = DDIR_READ;
else if (td_write(td))
ddir = DDIR_WRITE;
else if (td_trim(td))
ddir = DDIR_TRIM;
else
ddir = DDIR_INVAL;
if (!should_check_rate(td)) {
/*
* avoid time-consuming call to utime_since_now() if rate checking
* isn't being used. this imrpoves IOPs 50%. See:
* https://github.com/axboe/fio/issues/1501#issuecomment-1418327049
*/
td->rwmix_ddir = ddir;
} else
td->rwmix_ddir = rate_ddir(td, ddir);
return td->rwmix_ddir;
}
static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
{
enum fio_ddir ddir = get_rw_ddir(td);
if (td->o.zone_mode == ZONE_MODE_ZBD)
ddir = zbd_adjust_ddir(td, io_u, ddir);
if (td_trimwrite(td)) {
struct fio_file *f = io_u->file;
if (f->last_start[DDIR_WRITE] == f->last_start[DDIR_TRIM])
ddir = DDIR_TRIM;
else
ddir = DDIR_WRITE;
}
io_u->ddir = io_u->acct_ddir = ddir;
if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
td->o.barrier_blocks &&
!(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
td->io_issues[DDIR_WRITE])
io_u_set(td, io_u, IO_U_F_BARRIER);
}
void put_file_log(struct thread_data *td, struct fio_file *f)
{
unsigned int ret = put_file(td, f);
if (ret)
td_verror(td, ret, "file close");
}
void put_io_u(struct thread_data *td, struct io_u *io_u)
{
const bool needs_lock = td_async_processing(td);
zbd_put_io_u(td, io_u);
if (td->parent)
td = td->parent;
if (needs_lock)
__td_io_u_lock(td);
if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
put_file_log(td, io_u->file);
io_u->file = NULL;
io_u_set(td, io_u, IO_U_F_FREE);
if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
td->cur_depth--;
assert(!(td->flags & TD_F_CHILD));
}
io_u_qpush(&td->io_u_freelist, io_u);
td_io_u_free_notify(td);
if (needs_lock)
__td_io_u_unlock(td);
}
void clear_io_u(struct thread_data *td, struct io_u *io_u)
{
io_u_clear(td, io_u, IO_U_F_FLIGHT);
put_io_u(td, io_u);
}
void requeue_io_u(struct thread_data *td, struct io_u **io_u)
{
const bool needs_lock = td_async_processing(td);
struct io_u *__io_u = *io_u;
enum fio_ddir ddir = acct_ddir(__io_u);
dprint(FD_IO, "requeue %p\n", __io_u);
if (td->parent)
td = td->parent;
if (needs_lock)
__td_io_u_lock(td);
io_u_set(td, __io_u, IO_U_F_FREE);
if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
td->io_issues[ddir]--;
io_u_clear(td, __io_u, IO_U_F_FLIGHT);
if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
td->cur_depth--;
assert(!(td->flags & TD_F_CHILD));
}
io_u_rpush(&td->io_u_requeues, __io_u);
td_io_u_free_notify(td);
if (needs_lock)
__td_io_u_unlock(td);
*io_u = NULL;
}
static void setup_strided_zone_mode(struct thread_data *td, struct io_u *io_u)
{
struct fio_file *f = io_u->file;
assert(td->o.zone_mode == ZONE_MODE_STRIDED);
assert(td->o.zone_size);
assert(td->o.zone_range);
/*
* See if it's time to switch to a new zone
*/
if (td->zone_bytes >= td->o.zone_size) {
td->zone_bytes = 0;
f->file_offset += td->o.zone_range + td->o.zone_skip;
/*
* Wrap from the beginning, if we exceed the file size
*/
if (f->file_offset >= f->real_file_size)
f->file_offset = get_start_offset(td, f);
f->last_pos[io_u->ddir] = f->file_offset;
td->io_skip_bytes += td->o.zone_skip;
}
/*
* If zone_size > zone_range, then maintain the same zone until
* zone_bytes >= zone_size.
*/
if (f->last_pos[io_u->ddir] >= (f->file_offset + td->o.zone_range)) {
dprint(FD_IO, "io_u maintain zone offset=%" PRIu64 "/last_pos=%" PRIu64 "\n",
f->file_offset, f->last_pos[io_u->ddir]);
f->last_pos[io_u->ddir] = f->file_offset;
}
/*
* For random: if 'norandommap' is not set and zone_size > zone_range,
* map needs to be reset as it's done with zone_range everytime.
*/
if ((td->zone_bytes % td->o.zone_range) == 0)
fio_file_reset(td, f);
}
static int fill_multi_range_io_u(struct thread_data *td, struct io_u *io_u)
{
bool is_random;
uint64_t buflen, i = 0;
struct trim_range *range;
struct fio_file *f = io_u->file;
uint8_t *buf;
buf = io_u->buf;
buflen = 0;
while (i < td->o.num_range) {
range = (struct trim_range *)buf;
if (get_next_offset(td, io_u, &is_random)) {
dprint(FD_IO, "io_u %p, failed getting offset\n",
io_u);
break;
}
io_u->buflen = get_next_buflen(td, io_u, is_random);
if (!io_u->buflen) {
dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
break;
}
if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
io_u,
(unsigned long long) io_u->offset, io_u->buflen,
(unsigned long long) io_u->file->real_file_size);
break;
}
range->start = io_u->offset;
range->len = io_u->buflen;
buflen += io_u->buflen;
f->last_start[io_u->ddir] = io_u->offset;
f->last_pos[io_u->ddir] = io_u->offset + range->len;
buf += sizeof(struct trim_range);
i++;
if (td_random(td) && file_randommap(td, io_u->file))
mark_random_map(td, io_u, io_u->offset, io_u->buflen);
dprint_io_u(io_u, "fill");
}
if (buflen) {
/*
* Set buffer length as overall trim length for this IO, and
* tell the ioengine about the number of ranges to be trimmed.
*/
io_u->buflen = buflen;
io_u->number_trim = i;
return 0;
}
return 1;
}