-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_u.c
1830 lines (1509 loc) · 39.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 <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"
struct io_completion_data {
int nr; /* input */
int error; /* output */
uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
struct timeval 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 int 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?
*/
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;
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 r, lastb;
lastb = last_block(td, f, ddir);
if (!lastb)
return 1;
if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE) {
uint64_t rmax;
rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
if (td->o.use_os_rand) {
rmax = OS_RAND_MAX;
r = os_random_long(&td->random_state);
} else {
rmax = FRAND_MAX;
r = __rand(&td->__random_state);
}
dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
*b = (lastb - 1) * (r / ((uint64_t) rmax + 1.0));
} else {
uint64_t off = 0;
if (lfsr_next(&f->lfsr, &off, lastb))
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 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)
return __get_next_rand_offset(td, f, ddir, b);
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);
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 int should_sort_io(struct thread_data *td)
{
if (!td->o.verifysort_nr || !td->o.do_verify)
return 0;
if (!td_random(td))
return 0;
if (td->runstate != TD_VERIFYING)
return 0;
if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE)
return 0;
return 1;
}
static int should_do_random(struct thread_data *td, enum fio_ddir ddir)
{
unsigned int v;
unsigned long r;
if (td->o.perc_rand[ddir] == 100)
return 1;
if (td->o.use_os_rand) {
r = os_random_long(&td->seq_rand_state[ddir]);
v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
} else {
r = __rand(&td->__seq_rand_state[ddir]);
v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
}
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)) {
struct rand_off *r;
fetch:
r = flist_entry(td->next_rand_list.next, 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) {
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,
(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)
{
assert(ddir_rw(ddir));
if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
f->last_pos = f->last_pos - f->io_size;
if (f->last_pos < f->real_file_size) {
uint64_t pos;
if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
f->last_pos = f->real_file_size;
pos = f->last_pos - f->file_offset;
if (pos)
pos += td->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->flags |= 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->flags |= 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 != -1ULL)
offset = f->last_start - 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 int 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);
}
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;
unsigned long r, rand_max;
assert(ddir_rw(io_u->ddir));
if (td->o.bs_is_seq_rand)
ddir = is_random ? DDIR_WRITE: DDIR_READ;
else
ddir = io_u->ddir;
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;
if (td->o.use_os_rand)
rand_max = OS_RAND_MAX;
else
rand_max = FRAND_MAX;
do {
if (td->o.use_os_rand)
r = os_random_long(&td->bsrange_state);
else
r = __rand(&td->__bsrange_state);
if (!td->o.bssplit_nr[ddir]) {
buflen = 1 + (unsigned int) ((double) maxbs *
(r / (rand_max + 1.0)));
if (buflen < minbs)
buflen = minbs;
} else {
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 ((r <= ((rand_max / 100L) * perc)) &&
io_u_fits(td, io_u, buflen))
break;
}
}
if (td->o.do_verify && td->o.verify != VERIFY_NONE)
buflen = (buflen + td->o.verify_interval - 1) &
~(td->o.verify_interval - 1);
if (!td->o.bs_unaligned && is_power_of_2(minbs))
buflen = (buflen + minbs - 1) & ~(minbs - 1);
} 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;
unsigned long r;
if (td->o.use_os_rand) {
r = os_random_long(&td->rwmix_state);
v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
} else {
r = __rand(&td->__rwmix_state);
v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
}
if (v <= td->o.rwmix[DDIR_READ])
return DDIR_READ;
return DDIR_WRITE;
}
void io_u_quiesce(struct thread_data *td)
{
/*
* 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.
*/
while (td->io_u_in_flight) {
int fio_unused ret;
ret = io_u_queued_complete(td, 1, NULL);
}
}
static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
{
enum fio_ddir odir = ddir ^ 1;
struct timeval t;
long usec;
assert(ddir_rw(ddir));
if (td->rate_pending_usleep[ddir] <= 0)
return ddir;
/*
* We have too much pending sleep in this direction. See if we
* should switch.
*/
if (td_rw(td) && td->o.rwmix[odir]) {
/*
* Other direction does not have too much pending, switch
*/
if (td->rate_pending_usleep[odir] < 100000)
return odir;
/*
* Both directions have pending sleep. Sleep the minimum time
* and deduct from both.
*/
if (td->rate_pending_usleep[ddir] <=
td->rate_pending_usleep[odir]) {
usec = td->rate_pending_usleep[ddir];
} else {
usec = td->rate_pending_usleep[odir];
ddir = odir;
}
} else
usec = td->rate_pending_usleep[ddir];
io_u_quiesce(td);
fio_gettime(&t, NULL);
usec_sleep(td, usec);
usec = utime_since_now(&t);
td->rate_pending_usleep[ddir] -= usec;
odir = ddir ^ 1;
if (td_rw(td) && __should_check_rate(td, odir))
td->rate_pending_usleep[odir] -= usec;
if (ddir_trim(ddir))
return ddir;
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
*/
if (td->o.fsync_blocks &&
!(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
td->io_issues[DDIR_WRITE] && should_fsync(td))
return DDIR_SYNC;
/*
* see if it's time to fdatasync
*/
if (td->o.fdatasync_blocks &&
!(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
td->io_issues[DDIR_WRITE] && should_fsync(td))
return DDIR_DATASYNC;
/*
* see if it's time to sync_file_range
*/
if (td->sync_file_range_nr &&
!(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
td->io_issues[DDIR_WRITE] && should_fsync(td))
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
ddir = DDIR_TRIM;
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)
{
io_u->ddir = io_u->acct_ddir = get_rw_ddir(td);
if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
td->o.barrier_blocks &&
!(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
td->io_issues[DDIR_WRITE])
io_u->flags |= IO_U_F_BARRIER;
}
void put_file_log(struct thread_data *td, struct fio_file *f)
{
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)
{
td_io_u_lock(td);
if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
put_file_log(td, io_u->file);
io_u->file = NULL;
io_u->flags &= ~IO_U_F_FREE_DEF;
io_u->flags |= IO_U_F_FREE;
if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
td->cur_depth--;
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->flags &= ~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);
td_io_u_lock(td);
__io_u->flags |= IO_U_F_FREE;
if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
td->io_issues[ddir]--;
__io_u->flags &= ~IO_U_F_FLIGHT;
if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
td->cur_depth--;
io_u_rpush(&td->io_u_requeues, __io_u);
td_io_u_unlock(td);
*io_u = NULL;
}
static int fill_io_u(struct thread_data *td, struct io_u *io_u)
{
unsigned int is_random;
if (td->io_ops->flags & 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) {
td->zone_bytes = 0;
io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
io_u->file->last_pos = io_u->file->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 too large\n", io_u);
dprint(FD_IO, " off=%llu/%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_usec(struct thread_data *td, unsigned long usec)
{
int idx = 0;
assert(usec < 1000);
switch (usec) {
case 750 ... 999:
idx = 9;
break;
case 500 ... 749:
idx = 8;
break;
case 250 ... 499:
idx = 7;
break;
case 100 ... 249:
idx = 6;
break;
case 50 ... 99:
idx = 5;
break;
case 20 ... 49:
idx = 4;
break;
case 10 ... 19:
idx = 3;
break;
case 4 ... 9:
idx = 2;
break;
case 2 ... 3:
idx = 1;
case 0 ... 1:
break;
}
assert(idx < FIO_IO_U_LAT_U_NR);
td->ts.io_u_lat_u[idx]++;
}
static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
{
int idx = 0;
switch (msec) {
default:
idx = 11;
break;
case 1000 ... 1999:
idx = 10;
break;
case 750 ... 999:
idx = 9;
break;
case 500 ... 749:
idx = 8;
break;
case 250 ... 499:
idx = 7;
break;
case 100 ... 249:
idx = 6;
break;
case 50 ... 99:
idx = 5;
break;
case 20 ... 49:
idx = 4;
break;
case 10 ... 19:
idx = 3;
break;
case 4 ... 9:
idx = 2;
break;
case 2 ... 3:
idx = 1;
case 0 ... 1:
break;
}
assert(idx < FIO_IO_U_LAT_M_NR);
td->ts.io_u_lat_m[idx]++;
}
static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
{
if (usec < 1000)
io_u_mark_lat_usec(td, usec);
else
io_u_mark_lat_msec(td, usec / 1000);
}
/*
* Get next file to service by choosing one at random
*/
static struct fio_file *get_next_file_rand(struct thread_data *td,
enum fio_file_flags goodf,
enum fio_file_flags badf)
{
struct fio_file *f;
int fno;
do {
int opened = 0;
unsigned long r;
if (td->o.use_os_rand) {
r = os_random_long(&td->next_file_state);
fno = (unsigned int) ((double) td->o.nr_files
* (r / (OS_RAND_MAX + 1.0)));
} else {
r = __rand(&td->__next_file_state);
fno = (unsigned int) ((double) td->o.nr_files
* (r / (FRAND_MAX + 1.0)));
}
f = td->files[fno];
if (fio_file_done(f))
continue;
if (!fio_file_open(f)) {
int err;
if (td->nr_open_files >= td->o.open_files)
return ERR_PTR(-EBUSY);
err = td_io_open_file(td, f);
if (err)
continue;
opened = 1;
}
if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
dprint(FD_FILE, "get_next_file_rand: %p\n", f);
return f;