Add sparc and sparc64 support
[fio.git] / io_u.c
... / ...
CommitLineData
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
6#include <assert.h>
7
8#include "fio.h"
9#include "hash.h"
10#include "lib/ffz.h"
11
12/*
13 * Change this define to play with the timeout handling
14 */
15#undef FIO_USE_TIMEOUT
16
17struct io_completion_data {
18 int nr; /* input */
19
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
23};
24
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 */
29static int random_map_free(struct fio_file *f, const unsigned long long block)
30{
31 unsigned int idx = RAND_MAP_IDX(f, block);
32 unsigned int bit = RAND_MAP_BIT(f, block);
33
34 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
35
36 return (f->file_map[idx] & (1UL << bit)) == 0;
37}
38
39/*
40 * Mark a given offset as used in the map.
41 */
42static void mark_random_map(struct thread_data *td, struct io_u *io_u)
43{
44 unsigned int min_bs = td->o.rw_min_bs;
45 struct fio_file *f = io_u->file;
46 unsigned long long block;
47 unsigned int blocks;
48 unsigned int nr_blocks;
49
50 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
51 blocks = 0;
52 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
53
54 while (blocks < nr_blocks) {
55 unsigned int idx, bit;
56
57 /*
58 * If we have a mixed random workload, we may
59 * encounter blocks we already did IO to.
60 */
61 if ((td->o.ddir_nr == 1) && !random_map_free(f, block))
62 break;
63
64 idx = RAND_MAP_IDX(f, block);
65 bit = RAND_MAP_BIT(f, block);
66
67 fio_assert(td, idx < f->num_maps);
68
69 f->file_map[idx] |= (1UL << bit);
70 block++;
71 blocks++;
72 }
73
74 if ((blocks * min_bs) < io_u->buflen)
75 io_u->buflen = blocks * min_bs;
76}
77
78static inline unsigned long long last_block(struct thread_data *td,
79 struct fio_file *f,
80 enum fio_ddir ddir)
81{
82 unsigned long long max_blocks;
83 unsigned long long max_size;
84
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];
93 if (!max_blocks)
94 return 0;
95
96 return max_blocks;
97}
98
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,
103 enum fio_ddir ddir, unsigned long long *b)
104{
105 unsigned long long min_bs = td->o.rw_min_bs;
106 int i;
107
108 i = f->last_free_lookup;
109 *b = (i * BLOCKS_PER_MAP);
110 while ((*b) * min_bs < f->real_file_size) {
111 if (f->file_map[i] != -1UL) {
112 *b += ffz(f->file_map[i]);
113 if (*b > last_block(td, f, ddir))
114 break;
115 f->last_free_lookup = i;
116 return 0;
117 }
118
119 *b += BLOCKS_PER_MAP;
120 i++;
121 }
122
123 dprint(FD_IO, "failed finding a free block\n");
124 return 1;
125}
126
127static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
128 enum fio_ddir ddir, unsigned long long *b)
129{
130 unsigned long long r;
131 int loops = 5;
132
133 do {
134 r = os_random_long(&td->random_state);
135 dprint(FD_RANDOM, "off rand %llu\n", r);
136 *b = (last_block(td, f, ddir) - 1)
137 * (r / ((unsigned long long) RAND_MAX + 1.0));
138
139 /*
140 * if we are not maintaining a random map, we are done.
141 */
142 if (!file_randommap(td, f))
143 return 0;
144
145 /*
146 * calculate map offset and check if it's free
147 */
148 if (random_map_free(f, *b))
149 return 0;
150
151 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
152 *b);
153 } while (--loops);
154
155 /*
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.
159 */
160 loops = 10;
161 do {
162 f->last_free_lookup = (f->num_maps - 1) * (r / (RAND_MAX+1.0));
163 if (!get_next_free_block(td, f, ddir, b))
164 return 0;
165
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;
173 return get_next_free_block(td, f, ddir, b);
174}
175
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 */
181static int get_next_offset(struct thread_data *td, struct io_u *io_u)
182{
183 struct fio_file *f = io_u->file;
184 unsigned long long b;
185 enum fio_ddir ddir = io_u->ddir;
186
187 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
188 td->ddir_nr = td->o.ddir_nr;
189
190 if (get_next_rand_offset(td, f, ddir, &b))
191 return 1;
192 } else {
193 if (f->last_pos >= f->real_file_size) {
194 if (!td_random(td) ||
195 get_next_rand_offset(td, f, ddir, &b))
196 return 1;
197 } else
198 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
199 }
200
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;
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);
212 return 1;
213 }
214
215 return 0;
216}
217
218static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
219{
220 const int ddir = io_u->ddir;
221 unsigned int buflen = buflen; /* silence dumb gcc warning */
222 long r;
223
224 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
225 buflen = td->o.min_bs[ddir];
226 else {
227 r = os_random_long(&td->bsrange_state);
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 {
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 }
245 if (!td->o.bs_unaligned) {
246 buflen = (buflen + td->o.min_bs[ddir] - 1)
247 & ~(td->o.min_bs[ddir] - 1);
248 }
249 }
250
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);
254 buflen = td->o.min_bs[ddir];
255 }
256
257 return buflen;
258}
259
260static void set_rwmix_bytes(struct thread_data *td)
261{
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 */
269 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
270 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
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)));
280 if (v <= td->o.rwmix[DDIR_READ])
281 return DDIR_READ;
282
283 return DDIR_WRITE;
284}
285
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 */
291static enum fio_ddir get_rw_ddir(struct thread_data *td)
292{
293 if (td_rw(td)) {
294 /*
295 * Check if it's time to seed a new data direction.
296 */
297 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
298 unsigned long long max_bytes;
299 enum fio_ddir ddir;
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];
308 if (max_bytes >=
309 (td->o.size * td->o.rwmix[ddir] / 100)) {
310 if (!td->rw_end_set[ddir])
311 td->rw_end_set[ddir] = 1;
312
313 ddir ^= 1;
314 }
315
316 if (ddir != td->rwmix_ddir)
317 set_rwmix_bytes(td);
318
319 td->rwmix_ddir = ddir;
320 }
321 return td->rwmix_ddir;
322 } else if (td_read(td))
323 return DDIR_READ;
324 else
325 return DDIR_WRITE;
326}
327
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
336void put_io_u(struct thread_data *td, struct io_u *io_u)
337{
338 assert((io_u->flags & IO_U_F_FREE) == 0);
339 io_u->flags |= IO_U_F_FREE;
340
341 if (io_u->file)
342 put_file_log(td, io_u->file);
343
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
350void requeue_io_u(struct thread_data *td, struct io_u **io_u)
351{
352 struct io_u *__io_u = *io_u;
353
354 dprint(FD_IO, "requeue %p\n", __io_u);
355
356 __io_u->flags |= IO_U_F_FREE;
357 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
358 td->io_issues[__io_u->ddir]--;
359
360 __io_u->flags &= ~IO_U_F_FLIGHT;
361
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
368static int fill_io_u(struct thread_data *td, struct io_u *io_u)
369{
370 if (td->io_ops->flags & FIO_NOIO)
371 goto out;
372
373 /*
374 * see if it's time to sync
375 */
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)) {
379 io_u->ddir = DDIR_SYNC;
380 goto out;
381 }
382
383 io_u->ddir = get_rw_ddir(td);
384
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
394 /*
395 * No log, let the seq/rand engine retrieve the next buflen and
396 * position.
397 */
398 if (get_next_offset(td, io_u)) {
399 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
400 return 1;
401 }
402
403 io_u->buflen = get_next_buflen(td, io_u);
404 if (!io_u->buflen) {
405 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
406 return 1;
407 }
408
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);
411 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
412 io_u->buflen, io_u->file->real_file_size);
413 return 1;
414 }
415
416 /*
417 * mark entry before potentially trimming io_u
418 */
419 if (td_random(td) && file_randommap(td, io_u->file))
420 mark_random_map(td, io_u);
421
422 /*
423 * If using a write iolog, store this entry.
424 */
425out:
426 dprint_io_u(io_u, "fill_io_u");
427 td->zone_bytes += io_u->buflen;
428 log_io_u(td, io_u);
429 return 0;
430}
431
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
473void io_u_mark_depth(struct thread_data *td, unsigned int nr)
474{
475 int index = 0;
476
477 switch (td->cur_depth) {
478 default:
479 index = 6;
480 break;
481 case 32 ... 63:
482 index = 5;
483 break;
484 case 16 ... 31:
485 index = 4;
486 break;
487 case 8 ... 15:
488 index = 3;
489 break;
490 case 4 ... 7:
491 index = 2;
492 break;
493 case 2 ... 3:
494 index = 1;
495 case 1:
496 break;
497 }
498
499 td->ts.io_u_map[index] += nr;
500}
501
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)
544{
545 int index = 0;
546
547 switch (msec) {
548 default:
549 index = 11;
550 break;
551 case 1000 ... 1999:
552 index = 10;
553 break;
554 case 750 ... 999:
555 index = 9;
556 break;
557 case 500 ... 749:
558 index = 8;
559 break;
560 case 250 ... 499:
561 index = 7;
562 break;
563 case 100 ... 249:
564 index = 6;
565 break;
566 case 50 ... 99:
567 index = 5;
568 break;
569 case 20 ... 49:
570 index = 4;
571 break;
572 case 10 ... 19:
573 index = 3;
574 break;
575 case 4 ... 9:
576 index = 2;
577 break;
578 case 2 ... 3:
579 index = 1;
580 case 0 ... 1:
581 break;
582 }
583
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);
594}
595
596/*
597 * Get next file to service by choosing one at random
598 */
599static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
600 int badf)
601{
602 struct fio_file *f;
603 int fno;
604
605 do {
606 long r = os_random_long(&td->next_file_state);
607
608 fno = (unsigned int) ((double) td->o.nr_files
609 * (r / (RAND_MAX + 1.0)));
610 f = td->files[fno];
611 if (f->flags & FIO_FILE_DONE)
612 continue;
613
614 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
615 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
616 return f;
617 }
618 } while (1);
619}
620
621/*
622 * Get next file to service by doing round robin between all available ones
623 */
624static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
625 int badf)
626{
627 unsigned int old_next_file = td->next_file;
628 struct fio_file *f;
629
630 do {
631 f = td->files[td->next_file];
632
633 td->next_file++;
634 if (td->next_file >= td->o.nr_files)
635 td->next_file = 0;
636
637 if (f->flags & FIO_FILE_DONE) {
638 f = NULL;
639 continue;
640 }
641
642 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
643 break;
644
645 f = NULL;
646 } while (td->next_file != old_next_file);
647
648 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
649 return f;
650}
651
652static struct fio_file *get_next_file(struct thread_data *td)
653{
654 struct fio_file *f;
655
656 assert(td->o.nr_files <= td->files_index);
657
658 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
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);
663 return NULL;
664 }
665
666 f = td->file_service_file;
667 if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
668 goto out;
669
670 if (td->o.file_service_type == FIO_FSERVICE_RR)
671 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
672 else
673 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
674
675 td->file_service_file = f;
676 td->file_service_left = td->file_service_nr - 1;
677out:
678 dprint(FD_FILE, "get_next_file: %p\n", f);
679 return f;
680}
681
682static struct fio_file *find_next_new_file(struct thread_data *td)
683{
684 struct fio_file *f;
685
686 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
687 return NULL;
688
689 if (td->o.file_service_type == FIO_FSERVICE_RR)
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
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
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) {
718 put_file_log(td, f);
719 fio_file_reset(f);
720 goto set_file;
721 }
722
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
751struct io_u *__get_io_u(struct thread_data *td)
752{
753 struct io_u *io_u = NULL;
754
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)) {
758 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
759
760 io_u->buflen = 0;
761 io_u->resid = 0;
762 io_u->file = NULL;
763 io_u->end_io = NULL;
764 }
765
766 if (io_u) {
767 assert(io_u->flags & IO_U_F_FREE);
768 io_u->flags &= ~IO_U_F_FREE;
769
770 io_u->error = 0;
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 */
783struct io_u *get_io_u(struct thread_data *td)
784{
785 struct fio_file *f;
786 struct io_u *io_u;
787
788 io_u = __get_io_u(td);
789 if (!io_u) {
790 dprint(FD_IO, "__get_io_u failed\n");
791 return NULL;
792 }
793
794 /*
795 * from a requeue, io_u already setup
796 */
797 if (io_u->file)
798 goto out;
799
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;
806 } else if (set_io_u_file(td, io_u)) {
807 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
808 goto err_put;
809 }
810
811 f = io_u->file;
812 assert(f->flags & FIO_FILE_OPEN);
813
814 if (io_u->ddir != DDIR_SYNC) {
815 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
816 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
817 goto err_put;
818 }
819
820 f->last_pos = io_u->offset + io_u->buflen;
821
822 if (td->o.verify != VERIFY_NONE)
823 populate_verify_io_u(td, io_u);
824 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
825 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
826 }
827
828 /*
829 * Set io data pointers.
830 */
831 io_u->endpos = io_u->offset + io_u->buflen;
832 io_u->xfer_buf = io_u->buf;
833 io_u->xfer_buflen = io_u->buflen;
834
835out:
836 if (!td_io_prep(td, io_u)) {
837 fio_gettime(&io_u->start_time, NULL);
838 return io_u;
839 }
840err_put:
841 dprint(FD_IO, "get_io_u failed\n");
842 put_io_u(td, io_u);
843 return NULL;
844}
845
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
857 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
858 io_u->offset, io_u->xfer_buflen);
859
860 if (!td->error)
861 td_verror(td, io_u->error, "io_u error");
862}
863
864static void io_completed(struct thread_data *td, struct io_u *io_u,
865 struct io_completion_data *icd)
866{
867 unsigned long usec;
868
869 dprint_io_u(io_u, "io complete");
870
871 assert(io_u->flags & IO_U_F_FLIGHT);
872 io_u->flags &= ~IO_U_F_FLIGHT;
873
874 if (io_u->ddir == DDIR_SYNC) {
875 td->last_was_sync = 1;
876 return;
877 }
878
879 td->last_was_sync = 0;
880
881 if (!io_u->error) {
882 unsigned int bytes = io_u->buflen - io_u->resid;
883 const enum fio_ddir idx = io_u->ddir;
884 int ret;
885
886 td->io_blocks[idx]++;
887 td->io_bytes[idx] += bytes;
888 td->this_io_bytes[idx] += bytes;
889
890 usec = utime_since(&io_u->issue_time, &icd->time);
891
892 add_clat_sample(td, idx, usec);
893 add_bw_sample(td, idx, &icd->time);
894 io_u_mark_latency(td, usec);
895
896 if (td_write(td) && idx == DDIR_WRITE &&
897 td->o.do_verify &&
898 td->o.verify != VERIFY_NONE)
899 log_io_piece(td, io_u);
900
901 icd->bytes_done[idx] += bytes;
902
903 if (io_u->end_io) {
904 ret = io_u->end_io(td, io_u);
905 if (ret && !icd->error)
906 icd->error = ret;
907 }
908 } else {
909 icd->error = io_u->error;
910 io_u_log_error(td, io_u);
911 }
912}
913
914static void init_icd(struct io_completion_data *icd, int nr)
915{
916 fio_gettime(&icd->time, NULL);
917
918 icd->nr = nr;
919
920 icd->error = 0;
921 icd->bytes_done[0] = icd->bytes_done[1] = 0;
922}
923
924static void ios_completed(struct thread_data *td,
925 struct io_completion_data *icd)
926{
927 struct io_u *io_u;
928 int i;
929
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}
937
938/*
939 * Complete a single io_u for the sync engines.
940 */
941long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
942{
943 struct io_completion_data icd;
944
945 init_icd(&icd, 1);
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
952 td_verror(td, icd.error, "io_u_sync_complete");
953 return -1;
954}
955
956/*
957 * Called to complete min_events number of io for the async engines.
958 */
959long io_u_queued_complete(struct thread_data *td, int min_events)
960{
961 struct io_completion_data icd;
962 struct timespec *tvp = NULL;
963 int ret;
964 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
965
966 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_events);
967
968 if (!min_events)
969 tvp = &ts;
970
971 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
972 if (ret < 0) {
973 td_verror(td, -ret, "td_io_getevents");
974 return ret;
975 } else if (!ret)
976 return ret;
977
978 init_icd(&icd, ret);
979 ios_completed(td, &icd);
980 if (!icd.error)
981 return icd.bytes_done[0] + icd.bytes_done[1];
982
983 td_verror(td, icd.error, "io_u_queued_complete");
984 return -1;
985}
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
994 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
995 add_slat_sample(td, io_u->ddir, slat_time);
996}
997
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
1015#ifdef FIO_USE_TIMEOUT
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}
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);
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);
1038 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
1039}
1040#else
1041void io_u_set_timeout(struct thread_data fio_unused *td)
1042{
1043}
1044#endif
1045
1046#ifdef FIO_USE_TIMEOUT
1047static void io_u_timeout_handler(int fio_unused sig)
1048{
1049 struct thread_data *td, *__td;
1050 pid_t pid = getpid();
1051 struct list_head *entry;
1052 struct io_u *io_u;
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
1078 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->o.name, td->pid);
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");
1087 exit(1);
1088}
1089#endif
1090
1091void io_u_init_timeout(void)
1092{
1093#ifdef FIO_USE_TIMEOUT
1094 signal(SIGALRM, io_u_timeout_handler);
1095#endif
1096}