forked from axboe/fio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_u.c
2198 lines (1809 loc) · 47.2 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 <fcntl.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <assert.h>
#include "fio.h"
#include "hash.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"
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 void mark_random_map(struct thread_data *td, struct io_u *io_u)
{
unsigned int min_bs = td->o.rw_min_bs;
struct fio_file *f = io_u->file;
unsigned int nr_blocks;
uint64_t block;
block = (io_u->offset - f->file_offset) / (uint64_t) min_bs;
nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
if (!(io_u->flags & IO_U_F_BUSY_OK))
nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
if ((nr_blocks * min_bs) < io_u->buflen)
io_u->buflen = nr_blocks * min_bs;
}
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_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;
}
struct rand_off {
struct flist_head list;
uint64_t off;
};
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(struct thread_data *td,
struct fio_file *f, enum fio_ddir ddir,
uint64_t *b)
{
unsigned int v, send, stotal;
uint64_t offset, lastb;
static int warned;
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 = rand32_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 (!warned) {
log_err("fio: bug in zoned generation\n");
warned = 1;
}
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 flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
{
struct rand_off *r1 = flist_entry(a, struct rand_off, list);
struct rand_off *r2 = flist_entry(b, struct rand_off, list);
return r1->off - r2->off;
}
static int get_off_from_method(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);
log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
return 1;
}
/*
* Sort the reads for a verify phase in batches of verifysort_nr, if
* specified.
*/
static inline bool should_sort_io(struct thread_data *td)
{
if (!td->o.verifysort_nr || !td->o.do_verify)
return false;
if (!td_random(td))
return false;
if (td->runstate != TD_VERIFYING)
return false;
if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
return false;
return true;
}
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 = rand32_between(&td->seq_rand_state[ddir], 1, 100);
return v <= td->o.perc_rand[ddir];
}
static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
enum fio_ddir ddir, uint64_t *b)
{
struct rand_off *r;
int i, ret = 1;
if (!should_sort_io(td))
return get_off_from_method(td, f, ddir, b);
if (!flist_empty(&td->next_rand_list)) {
fetch:
r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
flist_del(&r->list);
*b = r->off;
free(r);
return 0;
}
for (i = 0; i < td->o.verifysort_nr; i++) {
r = malloc(sizeof(*r));
ret = get_off_from_method(td, f, ddir, &r->off);
if (ret) {
free(r);
break;
}
flist_add(&r->list, &td->next_rand_list);
}
if (ret && !i)
return ret;
assert(!flist_empty(&td->next_rand_list));
flist_sort(NULL, &td->next_rand_list, flist_cmp);
goto fetch;
}
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);
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 (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
o->time_based) {
struct thread_options *o = &td->o;
uint64_t io_size = f->io_size + (f->io_size % o->min_bs[ddir]);
if (io_size > f->last_pos[ddir])
f->last_pos[ddir] = 0;
else
f->last_pos[ddir] = f->last_pos[ddir] - io_size;
}
if (f->last_pos[ddir] < f->real_file_size) {
uint64_t pos;
if (f->last_pos[ddir] == 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,
unsigned int *is_random)
{
struct fio_file *f = io_u->file;
uint64_t b, offset;
int ret;
assert(ddir_rw(ddir));
b = offset = -1ULL;
if (rw_seq) {
if (td_random(td)) {
if (should_do_random(td, ddir)) {
ret = get_next_rand_block(td, f, ddir, &b);
*is_random = 1;
} else {
*is_random = 0;
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 = 0;
ret = get_next_seq_offset(td, f, ddir, &offset);
}
} else {
io_u_set(td, io_u, IO_U_F_BUSY_OK);
*is_random = 0;
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 = 0;
}
} 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;
}
}
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,
unsigned int *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;
}
return 0;
}
static int get_next_offset(struct thread_data *td, struct io_u *io_u,
unsigned int *is_random)
{
if (td->flags & TD_F_PROFILE_OPS) {
struct prof_io_ops *ops = &td->prof_io_ops;
if (ops->fill_io_u_off)
return ops->fill_io_u_off(td, io_u, is_random);
}
return __get_next_offset(td, io_u, is_random);
}
static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
unsigned int buflen)
{
struct fio_file *f = io_u->file;
return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
}
static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
unsigned int is_random)
{
int ddir = io_u->ddir;
unsigned int buflen = 0;
unsigned int minbs, maxbs;
uint64_t frand_max, r;
bool power_2;
assert(ddir_rw(ddir));
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 = 1 + (unsigned int) ((double) maxbs *
(r / (frand_max + 1.0)));
if (buflen < minbs)
buflen = minbs;
} 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];
buflen = bsp->bs;
perc += bsp->perc;
if (!perc)
break;
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;
} while (!io_u_fits(td, io_u, buflen));
return buflen;
}
static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u,
unsigned int is_random)
{
if (td->flags & TD_F_PROFILE_OPS) {
struct prof_io_ops *ops = &td->prof_io_ops;
if (ops->fill_io_u_size)
return ops->fill_io_u_size(td, io_u, is_random);
}
return __get_next_buflen(td, io_u, is_random);
}
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 = rand32_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 completed = 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) {
int fio_unused ret;
ret = td_io_commit(td);
}
while (td->io_u_in_flight) {
int ret;
ret = io_u_queued_complete(td, 1);
if (ret > 0)
completed += ret;
}
if (td->flags & TD_F_REGROW_LOGS)
regrow_logs(td);
return completed;
}
static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
{
enum fio_ddir odir = ddir ^ 1;
long usec;
uint64_t now;
assert(ddir_rw(ddir));
now = utime_since_now(&td->start);
/*
* 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 necissary
*/
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);
usec = usec_sleep(td, usec);
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;
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_trimwrite(td)) {
struct fio_file *f = io_u->file;
if (f->last_pos[DDIR_WRITE] == f->last_pos[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)
{
if (td->parent)
td = td->parent;
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_unlock(td);
td_io_u_free_notify(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)
{
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;
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_unlock(td);
td_io_u_free_notify(td);
*io_u = NULL;
}
static int fill_io_u(struct thread_data *td, struct io_u *io_u)
{
unsigned int is_random;
if (td_ioengine_flagged(td, FIO_NOIO))
goto out;
set_rw_ddir(td, io_u);
/*
* fsync() or fdatasync() or trim etc, we are done
*/
if (!ddir_rw(io_u->ddir))
goto out;
/*
* See if it's time to switch to a new zone
*/
if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
struct fio_file *f = io_u->file;
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 = f->real_file_size - f->file_offset;
f->last_pos[io_u->ddir] = f->file_offset;
td->io_skip_bytes += td->o.zone_skip;
}
/*
* No log, let the seq/rand engine retrieve the next buflen and
* position.
*/
if (get_next_offset(td, io_u, &is_random)) {
dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
return 1;
}
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);
return 1;
}
if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
dprint(FD_IO, "io_u %p, offset + buflen exceeds file size\n",
io_u);
dprint(FD_IO, " offset=%llu/buflen=%lu > %llu\n",
(unsigned long long) io_u->offset, io_u->buflen,
(unsigned long long) io_u->file->real_file_size);
return 1;
}
/*
* mark entry before potentially trimming io_u
*/
if (td_random(td) && file_randommap(td, io_u->file))
mark_random_map(td, io_u);
out:
dprint_io_u(io_u, "fill_io_u");
td->zone_bytes += io_u->buflen;
return 0;
}
static void __io_u_mark_map(unsigned int *map, unsigned int nr)
{
int idx = 0;
switch (nr) {
default:
idx = 6;
break;
case 33 ... 64:
idx = 5;
break;
case 17 ... 32:
idx = 4;
break;
case 9 ... 16:
idx = 3;
break;
case 5 ... 8:
idx = 2;
break;
case 1 ... 4:
idx = 1;
case 0:
break;
}
map[idx]++;
}
void io_u_mark_submit(struct thread_data *td, unsigned int nr)
{
__io_u_mark_map(td->ts.io_u_submit, nr);
td->ts.total_submit++;
}
void io_u_mark_complete(struct thread_data *td, unsigned int nr)
{
__io_u_mark_map(td->ts.io_u_complete, nr);
td->ts.total_complete++;
}
void io_u_mark_depth(struct thread_data *td, unsigned int nr)
{
int idx = 0;
switch (td->cur_depth) {
default:
idx = 6;
break;
case 32 ... 63:
idx = 5;
break;
case 16 ... 31:
idx = 4;
break;
case 8 ... 15:
idx = 3;
break;
case 4 ... 7:
idx = 2;
break;
case 2 ... 3:
idx = 1;
case 1:
break;
}
td->ts.io_u_map[idx] += nr;
}
static void io_u_mark_lat_nsec(struct thread_data *td, unsigned long long nsec)
{
int idx = 0;
assert(nsec < 1000);
switch (nsec) {
case 750 ... 999:
idx = 9;