ffz() takes ints, so change the file_map to being int based
[fio.git] / io_u.c
CommitLineData
10ba535a
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
0c6e7517 6#include <assert.h>
10ba535a
JA
7
8#include "fio.h"
5973cafb 9#include "hash.h"
697a606c 10#include "lib/ffz.h"
10ba535a 11
5945b9b4
JA
12/*
13 * Change this define to play with the timeout handling
14 */
15#undef FIO_USE_TIMEOUT
16
97601024
JA
17struct io_completion_data {
18 int nr; /* input */
97601024
JA
19
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
23};
24
10ba535a
JA
25/*
26 * The ->file_map[] contains a map of blocks we have or have not done io
27 * to yet. Used to make sure we cover the entire range in a fair fashion.
28 */
aec2de20 29static int random_map_free(struct fio_file *f, const unsigned long long block)
10ba535a 30{
aec2de20
JA
31 unsigned int idx = RAND_MAP_IDX(f, block);
32 unsigned int bit = RAND_MAP_BIT(f, block);
10ba535a 33
84422acd
JA
34 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
35
de605666 36 return (f->file_map[idx] & (1 << bit)) == 0;
10ba535a
JA
37}
38
df415585
JA
39/*
40 * Mark a given offset as used in the map.
41 */
9bf2061e 42static void mark_random_map(struct thread_data *td, struct io_u *io_u)
df415585 43{
2dc1bbeb 44 unsigned int min_bs = td->o.rw_min_bs;
9bf2061e 45 struct fio_file *f = io_u->file;
a00735e6
JA
46 unsigned long long block;
47 unsigned int blocks;
c685b5b2 48 unsigned int nr_blocks;
df415585 49
b9c5b644 50 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
a00735e6 51 blocks = 0;
c685b5b2
JA
52 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
53
54 while (blocks < nr_blocks) {
df415585
JA
55 unsigned int idx, bit;
56
1e3d53ac
JA
57 /*
58 * If we have a mixed random workload, we may
59 * encounter blocks we already did IO to.
60 */
aec2de20 61 if ((td->o.ddir_nr == 1) && !random_map_free(f, block))
df415585
JA
62 break;
63
aec2de20
JA
64 idx = RAND_MAP_IDX(f, block);
65 bit = RAND_MAP_BIT(f, block);
df415585 66
0032bf9f 67 fio_assert(td, idx < f->num_maps);
df415585 68
de605666 69 f->file_map[idx] |= (1 << bit);
df415585
JA
70 block++;
71 blocks++;
72 }
73
a00735e6
JA
74 if ((blocks * min_bs) < io_u->buflen)
75 io_u->buflen = blocks * min_bs;
df415585
JA
76}
77
2ba1c290 78static inline unsigned long long last_block(struct thread_data *td,
4ba66134
JA
79 struct fio_file *f,
80 enum fio_ddir ddir)
2ba1c290
JA
81{
82 unsigned long long max_blocks;
d9dd70f7 83 unsigned long long max_size;
2ba1c290 84
d9dd70f7
JA
85 /*
86 * Hmm, should we make sure that ->io_size <= ->real_file_size?
87 */
88 max_size = f->io_size;
89 if (max_size > f->real_file_size)
90 max_size = f->real_file_size;
91
92 max_blocks = max_size / (unsigned long long) td->o.min_bs[ddir];
2ba1c290
JA
93 if (!max_blocks)
94 return 0;
95
67778e88 96 return max_blocks;
2ba1c290
JA
97}
98
10ba535a
JA
99/*
100 * Return the next free block in the map.
101 */
102static int get_next_free_block(struct thread_data *td, struct fio_file *f,
4ba66134 103 enum fio_ddir ddir, unsigned long long *b)
10ba535a 104{
84422acd 105 unsigned long long min_bs = td->o.rw_min_bs;
10ba535a
JA
106 int i;
107
c685b5b2
JA
108 i = f->last_free_lookup;
109 *b = (i * BLOCKS_PER_MAP);
84422acd 110 while ((*b) * min_bs < f->real_file_size) {
de605666 111 if (f->file_map[i] != (unsigned int) -1) {
697a606c 112 *b += ffz(f->file_map[i]);
4ba66134 113 if (*b > last_block(td, f, ddir))
2ba1c290 114 break;
c685b5b2 115 f->last_free_lookup = i;
10ba535a
JA
116 return 0;
117 }
118
119 *b += BLOCKS_PER_MAP;
120 i++;
121 }
122
2ba1c290 123 dprint(FD_IO, "failed finding a free block\n");
10ba535a
JA
124 return 1;
125}
126
ec4015da 127static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
4ba66134 128 enum fio_ddir ddir, unsigned long long *b)
ec4015da 129{
84422acd 130 unsigned long long r;
ec4015da
JA
131 int loops = 5;
132
133 do {
134 r = os_random_long(&td->random_state);
84422acd 135 dprint(FD_RANDOM, "off rand %llu\n", r);
5ec10eaa
JA
136 *b = (last_block(td, f, ddir) - 1)
137 * (r / ((unsigned long long) RAND_MAX + 1.0));
2ba1c290 138
43c63a78
JA
139 /*
140 * if we are not maintaining a random map, we are done.
141 */
303032ae 142 if (!file_randommap(td, f))
43c63a78
JA
143 return 0;
144
145 /*
84422acd 146 * calculate map offset and check if it's free
43c63a78 147 */
aec2de20 148 if (random_map_free(f, *b))
43c63a78
JA
149 return 0;
150
84422acd
JA
151 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
152 *b);
43c63a78 153 } while (--loops);
ec4015da
JA
154
155 /*
43c63a78
JA
156 * we get here, if we didn't suceed in looking up a block. generate
157 * a random start offset into the filemap, and find the first free
158 * block from there.
ec4015da 159 */
43c63a78
JA
160 loops = 10;
161 do {
162 f->last_free_lookup = (f->num_maps - 1) * (r / (RAND_MAX+1.0));
4ba66134 163 if (!get_next_free_block(td, f, ddir, b))
43c63a78 164 return 0;
ec4015da 165
43c63a78
JA
166 r = os_random_long(&td->random_state);
167 } while (--loops);
168
169 /*
170 * that didn't work either, try exhaustive search from the start
171 */
172 f->last_free_lookup = 0;
4ba66134 173 return get_next_free_block(td, f, ddir, b);
ec4015da
JA
174}
175
10ba535a
JA
176/*
177 * For random io, generate a random new block and see if it's used. Repeat
178 * until we find a free one. For sequential io, just return the end of
179 * the last io issued.
180 */
9bf2061e 181static int get_next_offset(struct thread_data *td, struct io_u *io_u)
10ba535a 182{
9bf2061e 183 struct fio_file *f = io_u->file;
ec4015da 184 unsigned long long b;
4ba66134 185 enum fio_ddir ddir = io_u->ddir;
10ba535a 186
ec4015da
JA
187 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
188 td->ddir_nr = td->o.ddir_nr;
211097b2 189
4ba66134 190 if (get_next_rand_offset(td, f, ddir, &b))
bca4ed4d 191 return 1;
43063a1c 192 } else {
0c3d768a 193 if (f->last_pos >= f->real_file_size) {
4ba66134
JA
194 if (!td_random(td) ||
195 get_next_rand_offset(td, f, ddir, &b))
0c3d768a 196 return 1;
bcdedd0a 197 } else
4ba66134 198 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
43063a1c 199 }
10ba535a 200
009bd847
JA
201 io_u->offset = b * td->o.min_bs[ddir];
202 if (io_u->offset >= f->io_size) {
203 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
204 io_u->offset, f->io_size);
205 return 1;
206 }
207
208 io_u->offset += f->file_offset;
2ba1c290
JA
209 if (io_u->offset >= f->real_file_size) {
210 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
211 io_u->offset, f->real_file_size);
10ba535a 212 return 1;
2ba1c290 213 }
10ba535a
JA
214
215 return 0;
216}
217
9bf2061e 218static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
10ba535a 219{
bca4ed4d 220 const int ddir = io_u->ddir;
fc4398fd 221 unsigned int buflen = buflen; /* silence dumb gcc warning */
10ba535a
JA
222 long r;
223
2dc1bbeb
JA
224 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
225 buflen = td->o.min_bs[ddir];
10ba535a
JA
226 else {
227 r = os_random_long(&td->bsrange_state);
5ec10eaa
JA
228 if (!td->o.bssplit_nr) {
229 buflen = (unsigned int)
230 (1 + (double) (td->o.max_bs[ddir] - 1)
231 * r / (RAND_MAX + 1.0));
232 } else {
564ca972
JA
233 long perc = 0;
234 unsigned int i;
235
236 for (i = 0; i < td->o.bssplit_nr; i++) {
237 struct bssplit *bsp = &td->o.bssplit[i];
238
239 buflen = bsp->bs;
240 perc += bsp->perc;
241 if (r <= ((LONG_MAX / 100L) * perc))
242 break;
243 }
244 }
5ec10eaa
JA
245 if (!td->o.bs_unaligned) {
246 buflen = (buflen + td->o.min_bs[ddir] - 1)
247 & ~(td->o.min_bs[ddir] - 1);
248 }
10ba535a
JA
249 }
250
4ba66134
JA
251 if (io_u->offset + buflen > io_u->file->real_file_size) {
252 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
253 td->o.min_bs[ddir], ddir);
6a5e6884 254 buflen = td->o.min_bs[ddir];
4ba66134 255 }
6a5e6884 256
10ba535a
JA
257 return buflen;
258}
259
afe24a5a
JA
260static void set_rwmix_bytes(struct thread_data *td)
261{
afe24a5a
JA
262 unsigned int diff;
263
264 /*
265 * we do time or byte based switch. this is needed because
266 * buffered writes may issue a lot quicker than they complete,
267 * whereas reads do not.
268 */
e47f799f 269 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
04c540d9 270 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
e47f799f
JA
271}
272
273static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
274{
275 unsigned int v;
276 long r;
277
278 r = os_random_long(&td->rwmix_state);
279 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
04c540d9 280 if (v <= td->o.rwmix[DDIR_READ])
e47f799f
JA
281 return DDIR_READ;
282
283 return DDIR_WRITE;
afe24a5a
JA
284}
285
10ba535a
JA
286/*
287 * Return the data direction for the next io_u. If the job is a
288 * mixed read/write workload, check the rwmix cycle and switch if
289 * necessary.
290 */
1e97cce9 291static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a
JA
292{
293 if (td_rw(td)) {
10ba535a
JA
294 /*
295 * Check if it's time to seed a new data direction.
296 */
e4928662 297 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
e47f799f 298 unsigned long long max_bytes;
5ec10eaa 299 enum fio_ddir ddir;
e47f799f
JA
300
301 /*
302 * Put a top limit on how many bytes we do for
303 * one data direction, to avoid overflowing the
304 * ranges too much
305 */
306 ddir = get_rand_ddir(td);
307 max_bytes = td->this_io_bytes[ddir];
5ec10eaa
JA
308 if (max_bytes >=
309 (td->o.size * td->o.rwmix[ddir] / 100)) {
e4928662 310 if (!td->rw_end_set[ddir])
38d77cae 311 td->rw_end_set[ddir] = 1;
e4928662 312
e47f799f 313 ddir ^= 1;
38d77cae 314 }
e47f799f
JA
315
316 if (ddir != td->rwmix_ddir)
317 set_rwmix_bytes(td);
318
319 td->rwmix_ddir = ddir;
10ba535a
JA
320 }
321 return td->rwmix_ddir;
322 } else if (td_read(td))
323 return DDIR_READ;
324 else
325 return DDIR_WRITE;
326}
327
60f2c658
JA
328static void put_file_log(struct thread_data *td, struct fio_file *f)
329{
330 int ret = put_file(td, f);
331
332 if (ret)
333 td_verror(td, ret, "file close");
334}
335
10ba535a
JA
336void put_io_u(struct thread_data *td, struct io_u *io_u)
337{
0c6e7517
JA
338 assert((io_u->flags & IO_U_F_FREE) == 0);
339 io_u->flags |= IO_U_F_FREE;
340
60f2c658
JA
341 if (io_u->file)
342 put_file_log(td, io_u->file);
2dbdab7e 343
10ba535a
JA
344 io_u->file = NULL;
345 list_del(&io_u->list);
346 list_add(&io_u->list, &td->io_u_freelist);
347 td->cur_depth--;
348}
349
755200a3
JA
350void requeue_io_u(struct thread_data *td, struct io_u **io_u)
351{
352 struct io_u *__io_u = *io_u;
353
465221b0
JA
354 dprint(FD_IO, "requeue %p\n", __io_u);
355
4d2e0f49 356 __io_u->flags |= IO_U_F_FREE;
e4f54adb
JA
357 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
358 td->io_issues[__io_u->ddir]--;
5ec10eaa 359
4d2e0f49
JA
360 __io_u->flags &= ~IO_U_F_FLIGHT;
361
755200a3
JA
362 list_del(&__io_u->list);
363 list_add_tail(&__io_u->list, &td->io_u_requeues);
364 td->cur_depth--;
365 *io_u = NULL;
366}
367
9bf2061e 368static int fill_io_u(struct thread_data *td, struct io_u *io_u)
10ba535a 369{
b4c5e1ac
JA
370 if (td->io_ops->flags & FIO_NOIO)
371 goto out;
372
87dc1ab1
JA
373 /*
374 * see if it's time to sync
375 */
2dc1bbeb
JA
376 if (td->o.fsync_blocks &&
377 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
378 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
87dc1ab1 379 io_u->ddir = DDIR_SYNC;
c38e9468 380 goto out;
87dc1ab1
JA
381 }
382
a00735e6
JA
383 io_u->ddir = get_rw_ddir(td);
384
48f5abd3
JA
385 /*
386 * See if it's time to switch to a new zone
387 */
388 if (td->zone_bytes >= td->o.zone_size) {
389 td->zone_bytes = 0;
390 io_u->file->last_pos += td->o.zone_skip;
391 td->io_skip_bytes += td->o.zone_skip;
392 }
393
10ba535a 394 /*
c685b5b2
JA
395 * No log, let the seq/rand engine retrieve the next buflen and
396 * position.
10ba535a 397 */
2ba1c290
JA
398 if (get_next_offset(td, io_u)) {
399 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
bca4ed4d 400 return 1;
2ba1c290 401 }
10ba535a 402
9bf2061e 403 io_u->buflen = get_next_buflen(td, io_u);
2ba1c290
JA
404 if (!io_u->buflen) {
405 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
bca4ed4d 406 return 1;
2ba1c290 407 }
bca4ed4d 408
2ba1c290
JA
409 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
410 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
4ba66134
JA
411 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
412 io_u->buflen, io_u->file->real_file_size);
6a5e6884 413 return 1;
2ba1c290 414 }
6a5e6884 415
bca4ed4d
JA
416 /*
417 * mark entry before potentially trimming io_u
418 */
303032ae 419 if (td_random(td) && file_randommap(td, io_u->file))
9bf2061e 420 mark_random_map(td, io_u);
bca4ed4d
JA
421
422 /*
423 * If using a write iolog, store this entry.
424 */
c38e9468 425out:
2ba1c290 426 dprint_io_u(io_u, "fill_io_u");
d9d91e39 427 td->zone_bytes += io_u->buflen;
f29b25a3 428 log_io_u(td, io_u);
bca4ed4d 429 return 0;
10ba535a
JA
430}
431
838bc709
JA
432static void __io_u_mark_map(unsigned int *map, unsigned int nr)
433{
434 int index = 0;
435
436 switch (nr) {
437 default:
438 index = 6;
439 break;
440 case 33 ... 64:
441 index = 5;
442 break;
443 case 17 ... 32:
444 index = 4;
445 break;
446 case 9 ... 16:
447 index = 3;
448 break;
449 case 5 ... 8:
450 index = 2;
451 break;
452 case 1 ... 4:
453 index = 1;
454 case 0:
455 break;
456 }
457
458 map[index]++;
459}
460
461void io_u_mark_submit(struct thread_data *td, unsigned int nr)
462{
463 __io_u_mark_map(td->ts.io_u_submit, nr);
464 td->ts.total_submit++;
465}
466
467void io_u_mark_complete(struct thread_data *td, unsigned int nr)
468{
469 __io_u_mark_map(td->ts.io_u_complete, nr);
470 td->ts.total_complete++;
471}
472
d8005759 473void io_u_mark_depth(struct thread_data *td, unsigned int nr)
71619dc2
JA
474{
475 int index = 0;
476
477 switch (td->cur_depth) {
478 default:
a783e61a
JA
479 index = 6;
480 break;
71619dc2 481 case 32 ... 63:
a783e61a
JA
482 index = 5;
483 break;
71619dc2 484 case 16 ... 31:
a783e61a
JA
485 index = 4;
486 break;
71619dc2 487 case 8 ... 15:
a783e61a
JA
488 index = 3;
489 break;
71619dc2 490 case 4 ... 7:
a783e61a
JA
491 index = 2;
492 break;
71619dc2 493 case 2 ... 3:
a783e61a 494 index = 1;
71619dc2
JA
495 case 1:
496 break;
497 }
498
3bec7ae0 499 td->ts.io_u_map[index] += nr;
71619dc2
JA
500}
501
04a0feae
JA
502static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
503{
504 int index = 0;
505
506 assert(usec < 1000);
507
508 switch (usec) {
509 case 750 ... 999:
510 index = 9;
511 break;
512 case 500 ... 749:
513 index = 8;
514 break;
515 case 250 ... 499:
516 index = 7;
517 break;
518 case 100 ... 249:
519 index = 6;
520 break;
521 case 50 ... 99:
522 index = 5;
523 break;
524 case 20 ... 49:
525 index = 4;
526 break;
527 case 10 ... 19:
528 index = 3;
529 break;
530 case 4 ... 9:
531 index = 2;
532 break;
533 case 2 ... 3:
534 index = 1;
535 case 0 ... 1:
536 break;
537 }
538
539 assert(index < FIO_IO_U_LAT_U_NR);
540 td->ts.io_u_lat_u[index]++;
541}
542
543static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
ec118304
JA
544{
545 int index = 0;
546
547 switch (msec) {
548 default:
04a0feae
JA
549 index = 11;
550 break;
8abdce66 551 case 1000 ... 1999:
04a0feae
JA
552 index = 10;
553 break;
8abdce66 554 case 750 ... 999:
04a0feae
JA
555 index = 9;
556 break;
8abdce66 557 case 500 ... 749:
04a0feae
JA
558 index = 8;
559 break;
8abdce66 560 case 250 ... 499:
04a0feae
JA
561 index = 7;
562 break;
8abdce66 563 case 100 ... 249:
04a0feae
JA
564 index = 6;
565 break;
8abdce66 566 case 50 ... 99:
04a0feae
JA
567 index = 5;
568 break;
8abdce66 569 case 20 ... 49:
04a0feae
JA
570 index = 4;
571 break;
8abdce66 572 case 10 ... 19:
04a0feae
JA
573 index = 3;
574 break;
8abdce66 575 case 4 ... 9:
04a0feae
JA
576 index = 2;
577 break;
ec118304 578 case 2 ... 3:
04a0feae 579 index = 1;
ec118304
JA
580 case 0 ... 1:
581 break;
582 }
583
04a0feae
JA
584 assert(index < FIO_IO_U_LAT_M_NR);
585 td->ts.io_u_lat_m[index]++;
586}
587
588static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
589{
590 if (usec < 1000)
591 io_u_mark_lat_usec(td, usec);
592 else
593 io_u_mark_lat_msec(td, usec / 1000);
ec118304
JA
594}
595
0aabe160
JA
596/*
597 * Get next file to service by choosing one at random
598 */
1c178180
JA
599static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
600 int badf)
0aabe160 601{
0aabe160 602 struct fio_file *f;
1c178180 603 int fno;
0aabe160
JA
604
605 do {
7c83c089
JA
606 long r = os_random_long(&td->next_file_state);
607
5ec10eaa
JA
608 fno = (unsigned int) ((double) td->o.nr_files
609 * (r / (RAND_MAX + 1.0)));
126d65c6 610 f = td->files[fno];
059e63c0
JA
611 if (f->flags & FIO_FILE_DONE)
612 continue;
1c178180 613
2ba1c290
JA
614 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
615 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
0aabe160 616 return f;
2ba1c290 617 }
0aabe160
JA
618 } while (1);
619}
620
621/*
622 * Get next file to service by doing round robin between all available ones
623 */
1c178180
JA
624static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
625 int badf)
3d7c391d
JA
626{
627 unsigned int old_next_file = td->next_file;
628 struct fio_file *f;
629
630 do {
126d65c6 631 f = td->files[td->next_file];
3d7c391d
JA
632
633 td->next_file++;
2dc1bbeb 634 if (td->next_file >= td->o.nr_files)
3d7c391d
JA
635 td->next_file = 0;
636
d5ed68ea
JA
637 if (f->flags & FIO_FILE_DONE) {
638 f = NULL;
059e63c0 639 continue;
d5ed68ea 640 }
059e63c0 641
1c178180 642 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
3d7c391d
JA
643 break;
644
645 f = NULL;
646 } while (td->next_file != old_next_file);
647
2ba1c290 648 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
3d7c391d
JA
649 return f;
650}
651
bdb4e2e9
JA
652static struct fio_file *get_next_file(struct thread_data *td)
653{
1907dbc6
JA
654 struct fio_file *f;
655
2dc1bbeb 656 assert(td->o.nr_files <= td->files_index);
1c178180 657
2ba1c290 658 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
5ec10eaa
JA
659 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
660 " nr_files=%d\n", td->nr_open_files,
661 td->nr_done_files,
662 td->o.nr_files);
bdb4e2e9 663 return NULL;
2ba1c290 664 }
bdb4e2e9 665
1907dbc6 666 f = td->file_service_file;
f11bd94d 667 if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
2ba1c290 668 goto out;
1907dbc6 669
2dc1bbeb 670 if (td->o.file_service_type == FIO_FSERVICE_RR)
1c178180 671 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
bdb4e2e9 672 else
1c178180 673 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
1907dbc6
JA
674
675 td->file_service_file = f;
676 td->file_service_left = td->file_service_nr - 1;
2ba1c290
JA
677out:
678 dprint(FD_FILE, "get_next_file: %p\n", f);
1907dbc6 679 return f;
bdb4e2e9
JA
680}
681
1c178180
JA
682static struct fio_file *find_next_new_file(struct thread_data *td)
683{
684 struct fio_file *f;
685
1020a139
JA
686 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
687 return NULL;
688
2dc1bbeb 689 if (td->o.file_service_type == FIO_FSERVICE_RR)
1c178180
JA
690 f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
691 else
692 f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
693
694 return f;
695}
696
429f6675
JA
697static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
698{
699 struct fio_file *f;
700
701 do {
702 f = get_next_file(td);
703 if (!f)
704 return 1;
705
706set_file:
707 io_u->file = f;
708 get_file(f);
709
710 if (!fill_io_u(td, io_u))
711 break;
712
009bd847
JA
713 /*
714 * optimization to prevent close/open of the same file. This
715 * way we preserve queueing etc.
716 */
717 if (td->o.nr_files == 1 && td->o.time_based) {
60f2c658 718 put_file_log(td, f);
009bd847
JA
719 fio_file_reset(f);
720 goto set_file;
721 }
722
429f6675
JA
723 /*
724 * td_io_close() does a put_file() as well, so no need to
725 * do that here.
726 */
727 io_u->file = NULL;
728 td_io_close_file(td, f);
729 f->flags |= FIO_FILE_DONE;
730 td->nr_done_files++;
731
732 /*
733 * probably not the right place to do this, but see
734 * if we need to open a new file
735 */
736 if (td->nr_open_files < td->o.open_files &&
737 td->o.open_files != td->o.nr_files) {
738 f = find_next_new_file(td);
739
740 if (!f || td_io_open_file(td, f))
741 return 1;
742
743 goto set_file;
744 }
745 } while (1);
746
747 return 0;
748}
749
750
10ba535a
JA
751struct io_u *__get_io_u(struct thread_data *td)
752{
753 struct io_u *io_u = NULL;
754
755200a3
JA
755 if (!list_empty(&td->io_u_requeues))
756 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
757 else if (!queue_full(td)) {
10ba535a
JA
758 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
759
6040dabc 760 io_u->buflen = 0;
10ba535a 761 io_u->resid = 0;
755200a3 762 io_u->file = NULL;
d7762cf8 763 io_u->end_io = NULL;
755200a3
JA
764 }
765
766 if (io_u) {
0c6e7517
JA
767 assert(io_u->flags & IO_U_F_FREE);
768 io_u->flags &= ~IO_U_F_FREE;
769
755200a3 770 io_u->error = 0;
10ba535a
JA
771 list_del(&io_u->list);
772 list_add(&io_u->list, &td->io_u_busylist);
773 td->cur_depth++;
774 }
775
776 return io_u;
777}
778
779/*
780 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
781 * etc. The returned io_u is fully ready to be prepped and submitted.
782 */
3d7c391d 783struct io_u *get_io_u(struct thread_data *td)
10ba535a 784{
3d7c391d 785 struct fio_file *f;
10ba535a
JA
786 struct io_u *io_u;
787
788 io_u = __get_io_u(td);
2ba1c290
JA
789 if (!io_u) {
790 dprint(FD_IO, "__get_io_u failed\n");
10ba535a 791 return NULL;
2ba1c290 792 }
10ba535a 793
755200a3
JA
794 /*
795 * from a requeue, io_u already setup
796 */
797 if (io_u->file)
77f392bf 798 goto out;
755200a3 799
429f6675
JA
800 /*
801 * If using an iolog, grab next piece if any available.
802 */
803 if (td->o.read_iolog_file) {
804 if (read_iolog_get(td, io_u))
805 goto err_put;
2ba1c290
JA
806 } else if (set_io_u_file(td, io_u)) {
807 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
429f6675 808 goto err_put;
2ba1c290 809 }
5ec10eaa 810
429f6675
JA
811 f = io_u->file;
812 assert(f->flags & FIO_FILE_OPEN);
97af62ce 813
87dc1ab1 814 if (io_u->ddir != DDIR_SYNC) {
d0656a93 815 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
2ba1c290 816 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
429f6675 817 goto err_put;
2ba1c290 818 }
10ba535a 819
36167d82 820 f->last_pos = io_u->offset + io_u->buflen;
10ba535a 821
2dc1bbeb 822 if (td->o.verify != VERIFY_NONE)
87dc1ab1 823 populate_verify_io_u(td, io_u);
e4dad9c6
JA
824 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
825 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
87dc1ab1 826 }
10ba535a 827
165faf16
JA
828 /*
829 * Set io data pointers.
830 */
d460eb31 831 io_u->endpos = io_u->offset + io_u->buflen;
cec6b55d
JA
832 io_u->xfer_buf = io_u->buf;
833 io_u->xfer_buflen = io_u->buflen;
5973cafb 834
6ac7a331 835out:
429f6675
JA
836 if (!td_io_prep(td, io_u)) {
837 fio_gettime(&io_u->start_time, NULL);
838 return io_u;
36167d82 839 }
429f6675 840err_put:
2ba1c290 841 dprint(FD_IO, "get_io_u failed\n");
429f6675
JA
842 put_io_u(td, io_u);
843 return NULL;
10ba535a
JA
844}
845
5451792e
JA
846void io_u_log_error(struct thread_data *td, struct io_u *io_u)
847{
848 const char *msg[] = { "read", "write", "sync" };
849
850 log_err("fio: io_u error");
851
852 if (io_u->file)
853 log_err(" on file %s", io_u->file->file_name);
854
855 log_err(": %s\n", strerror(io_u->error));
856
5ec10eaa
JA
857 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
858 io_u->offset, io_u->xfer_buflen);
5451792e
JA
859
860 if (!td->error)
861 td_verror(td, io_u->error, "io_u error");
862}
863
97601024
JA
864static void io_completed(struct thread_data *td, struct io_u *io_u,
865 struct io_completion_data *icd)
10ba535a 866{
d85f5118 867 unsigned long usec;
10ba535a 868
2ba1c290
JA
869 dprint_io_u(io_u, "io complete");
870
0c6e7517
JA
871 assert(io_u->flags & IO_U_F_FLIGHT);
872 io_u->flags &= ~IO_U_F_FLIGHT;
873
87dc1ab1
JA
874 if (io_u->ddir == DDIR_SYNC) {
875 td->last_was_sync = 1;
876 return;
877 }
878
879 td->last_was_sync = 0;
880
10ba535a
JA
881 if (!io_u->error) {
882 unsigned int bytes = io_u->buflen - io_u->resid;
1e97cce9 883 const enum fio_ddir idx = io_u->ddir;
3af6ef39 884 int ret;
10ba535a
JA
885
886 td->io_blocks[idx]++;
887 td->io_bytes[idx] += bytes;
10ba535a
JA
888 td->this_io_bytes[idx] += bytes;
889
d85f5118 890 usec = utime_since(&io_u->issue_time, &icd->time);
10ba535a 891
d85f5118 892 add_clat_sample(td, idx, usec);
02bcaa8c 893 add_bw_sample(td, idx, &icd->time);
04a0feae 894 io_u_mark_latency(td, usec);
10ba535a 895
660a1cb5 896 if (td_write(td) && idx == DDIR_WRITE &&
8670579e 897 td->o.do_verify &&
41128405 898 td->o.verify != VERIFY_NONE)
10ba535a
JA
899 log_io_piece(td, io_u);
900
901 icd->bytes_done[idx] += bytes;
3af6ef39 902
d7762cf8 903 if (io_u->end_io) {
36690c9b 904 ret = io_u->end_io(td, io_u);
3af6ef39
JA
905 if (ret && !icd->error)
906 icd->error = ret;
907 }
5451792e 908 } else {
10ba535a 909 icd->error = io_u->error;
5451792e
JA
910 io_u_log_error(td, io_u);
911 }
10ba535a
JA
912}
913
d7762cf8 914static void init_icd(struct io_completion_data *icd, int nr)
10ba535a 915{
02bcaa8c
JA
916 fio_gettime(&icd->time, NULL);
917
3af6ef39
JA
918 icd->nr = nr;
919
10ba535a
JA
920 icd->error = 0;
921 icd->bytes_done[0] = icd->bytes_done[1] = 0;
36167d82
JA
922}
923
97601024
JA
924static void ios_completed(struct thread_data *td,
925 struct io_completion_data *icd)
36167d82
JA
926{
927 struct io_u *io_u;
928 int i;
929
10ba535a
JA
930 for (i = 0; i < icd->nr; i++) {
931 io_u = td->io_ops->event(td, i);
932
933 io_completed(td, io_u, icd);
934 put_io_u(td, io_u);
935 }
936}
97601024 937
e7e6cfb4
JA
938/*
939 * Complete a single io_u for the sync engines.
940 */
d7762cf8 941long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
97601024
JA
942{
943 struct io_completion_data icd;
944
d7762cf8 945 init_icd(&icd, 1);
97601024
JA
946 io_completed(td, io_u, &icd);
947 put_io_u(td, io_u);
948
949 if (!icd.error)
950 return icd.bytes_done[0] + icd.bytes_done[1];
951
37e974a5 952 td_verror(td, icd.error, "io_u_sync_complete");
97601024
JA
953 return -1;
954}
955
e7e6cfb4
JA
956/*
957 * Called to complete min_events number of io for the async engines.
958 */
d7762cf8 959long io_u_queued_complete(struct thread_data *td, int min_events)
97601024 960{
97601024 961 struct io_completion_data icd;
00de55ef 962 struct timespec *tvp = NULL;
97601024 963 int ret;
4d06a338 964 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
97601024 965
b271fe62
JA
966 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_events);
967
4d06a338 968 if (!min_events)
00de55ef 969 tvp = &ts;
97601024 970
00de55ef 971 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
97601024 972 if (ret < 0) {
e1161c32 973 td_verror(td, -ret, "td_io_getevents");
97601024
JA
974 return ret;
975 } else if (!ret)
976 return ret;
977
d7762cf8 978 init_icd(&icd, ret);
97601024
JA
979 ios_completed(td, &icd);
980 if (!icd.error)
981 return icd.bytes_done[0] + icd.bytes_done[1];
982
37e974a5 983 td_verror(td, icd.error, "io_u_queued_complete");
97601024
JA
984 return -1;
985}
7e77dd02
JA
986
987/*
988 * Call when io_u is really queued, to update the submission latency.
989 */
990void io_u_queued(struct thread_data *td, struct io_u *io_u)
991{
992 unsigned long slat_time;
993
d85f5118 994 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
7e77dd02
JA
995 add_slat_sample(td, io_u->ddir, slat_time);
996}
433afcb4 997
5973cafb
JA
998/*
999 * "randomly" fill the buffer contents
1000 */
1001void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1002 unsigned int max_bs)
1003{
1004 long *ptr = io_u->buf;
1005
1006 if (!td->o.zero_buffers) {
1007 while ((void *) ptr - io_u->buf < max_bs) {
1008 *ptr = rand() * GOLDEN_RATIO_PRIME;
1009 ptr++;
1010 }
1011 } else
1012 memset(ptr, 0, max_bs);
1013}
1014
55bc9728 1015#ifdef FIO_USE_TIMEOUT
433afcb4
JA
1016void io_u_set_timeout(struct thread_data *td)
1017{
1018 assert(td->cur_depth);
1019
1020 td->timer.it_interval.tv_sec = 0;
1021 td->timer.it_interval.tv_usec = 0;
1022 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
1023 td->timer.it_value.tv_usec = 0;
1024 setitimer(ITIMER_REAL, &td->timer, NULL);
1025 fio_gettime(&td->timeout_end, NULL);
1026}
5945b9b4
JA
1027
1028static void io_u_dump(struct io_u *io_u)
1029{
1030 unsigned long t_start = mtime_since_now(&io_u->start_time);
1031 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
1032
1033 log_err("io_u=%p, t_start=%lu, t_issue=%lu\n", io_u, t_start, t_issue);
5ec10eaa
JA
1034 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf,
1035 io_u->xfer_buf, io_u->buflen,
1036 io_u->xfer_buflen,
1037 io_u->offset);
5945b9b4
JA
1038 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
1039}
55bc9728
JA
1040#else
1041void io_u_set_timeout(struct thread_data fio_unused *td)
1042{
1043}
1044#endif
433afcb4 1045
55bc9728 1046#ifdef FIO_USE_TIMEOUT
433afcb4
JA
1047static void io_u_timeout_handler(int fio_unused sig)
1048{
1049 struct thread_data *td, *__td;
1050 pid_t pid = getpid();
5945b9b4
JA
1051 struct list_head *entry;
1052 struct io_u *io_u;
433afcb4
JA
1053 int i;
1054
1055 log_err("fio: io_u timeout\n");
1056
1057 /*
1058 * TLS would be nice...
1059 */
1060 td = NULL;
1061 for_each_td(__td, i) {
1062 if (__td->pid == pid) {
1063 td = __td;
1064 break;
1065 }
1066 }
1067
1068 if (!td) {
1069 log_err("fio: io_u timeout, can't find job\n");
1070 exit(1);
1071 }
1072
1073 if (!td->cur_depth) {
1074 log_err("fio: timeout without pending work?\n");
1075 return;
1076 }
1077
15506d09 1078 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->o.name, td->pid);
5945b9b4
JA
1079
1080 list_for_each(entry, &td->io_u_busylist) {
1081 io_u = list_entry(entry, struct io_u, list);
1082
1083 io_u_dump(io_u);
1084 }
1085
1086 td_verror(td, ETIMEDOUT, "io_u timeout");
433afcb4
JA
1087 exit(1);
1088}
55bc9728 1089#endif
433afcb4
JA
1090
1091void io_u_init_timeout(void)
1092{
55bc9728 1093#ifdef FIO_USE_TIMEOUT
433afcb4 1094 signal(SIGALRM, io_u_timeout_handler);
55bc9728 1095#endif
433afcb4 1096}