Improve rate usec calculation
[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"
9#include "os.h"
10
5945b9b4
JA
11/*
12 * Change this define to play with the timeout handling
13 */
14#undef FIO_USE_TIMEOUT
15
97601024
JA
16struct io_completion_data {
17 int nr; /* input */
18 endio_handler *handler; /* input */
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 */
29static int random_map_free(struct thread_data *td, struct fio_file *f,
30 unsigned long long block)
31{
32 unsigned int idx = RAND_MAP_IDX(td, f, block);
33 unsigned int bit = RAND_MAP_BIT(td, f, block);
34
35 return (f->file_map[idx] & (1UL << bit)) == 0;
36}
37
df415585
JA
38/*
39 * Mark a given offset as used in the map.
40 */
41static void mark_random_map(struct thread_data *td, struct fio_file *f,
42 struct io_u *io_u)
43{
afdbe580 44 unsigned int min_bs = td->rw_min_bs;
a00735e6
JA
45 unsigned long long block;
46 unsigned int blocks;
c685b5b2 47 unsigned int nr_blocks;
df415585 48
a00735e6
JA
49 block = io_u->offset / (unsigned long long) min_bs;
50 blocks = 0;
c685b5b2
JA
51 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
52
53 while (blocks < nr_blocks) {
df415585
JA
54 unsigned int idx, bit;
55
56 if (!random_map_free(td, f, block))
57 break;
58
59 idx = RAND_MAP_IDX(td, f, block);
60 bit = RAND_MAP_BIT(td, f, block);
61
0032bf9f 62 fio_assert(td, idx < f->num_maps);
df415585
JA
63
64 f->file_map[idx] |= (1UL << bit);
65 block++;
66 blocks++;
67 }
68
a00735e6
JA
69 if ((blocks * min_bs) < io_u->buflen)
70 io_u->buflen = blocks * min_bs;
df415585
JA
71}
72
10ba535a
JA
73/*
74 * Return the next free block in the map.
75 */
76static int get_next_free_block(struct thread_data *td, struct fio_file *f,
77 unsigned long long *b)
78{
79 int i;
80
c685b5b2
JA
81 i = f->last_free_lookup;
82 *b = (i * BLOCKS_PER_MAP);
ee88470c 83 while ((*b) * td->rw_min_bs < f->real_file_size) {
10ba535a
JA
84 if (f->file_map[i] != -1UL) {
85 *b += ffz(f->file_map[i]);
c685b5b2 86 f->last_free_lookup = i;
10ba535a
JA
87 return 0;
88 }
89
90 *b += BLOCKS_PER_MAP;
91 i++;
92 }
93
94 return 1;
95}
96
97/*
98 * For random io, generate a random new block and see if it's used. Repeat
99 * until we find a free one. For sequential io, just return the end of
100 * the last io issued.
101 */
102static int get_next_offset(struct thread_data *td, struct fio_file *f,
c685b5b2 103 struct io_u *io_u)
10ba535a 104{
c685b5b2 105 const int ddir = io_u->ddir;
10ba535a
JA
106 unsigned long long b, rb;
107 long r;
108
413dd459 109 if (td_random(td)) {
f697125a 110 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
c685b5b2 111 int loops = 5;
10ba535a
JA
112
113 do {
114 r = os_random_long(&td->random_state);
115 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
bb8895e0
JA
116 if (td->norandommap)
117 break;
a00735e6 118 rb = b + (f->file_offset / td->min_bs[ddir]);
10ba535a
JA
119 loops--;
120 } while (!random_map_free(td, f, rb) && loops);
121
bca4ed4d
JA
122 /*
123 * if we failed to retrieve a truly random offset within
124 * the loops assigned, see if there are free ones left at all
125 */
126 if (!loops && get_next_free_block(td, f, &b))
127 return 1;
10ba535a 128 } else
a00735e6 129 b = f->last_pos / td->min_bs[ddir];
10ba535a 130
c685b5b2 131 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
bca4ed4d 132 if (io_u->offset >= f->real_file_size)
10ba535a
JA
133 return 1;
134
135 return 0;
136}
137
bca4ed4d
JA
138static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
139 struct io_u *io_u)
10ba535a 140{
bca4ed4d 141 const int ddir = io_u->ddir;
10ba535a
JA
142 unsigned int buflen;
143 long r;
144
a00735e6
JA
145 if (td->min_bs[ddir] == td->max_bs[ddir])
146 buflen = td->min_bs[ddir];
10ba535a
JA
147 else {
148 r = os_random_long(&td->bsrange_state);
1e97cce9 149 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
690adba3 150 if (!td->bs_unaligned)
a00735e6 151 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
10ba535a
JA
152 }
153
bca4ed4d 154 while (buflen + io_u->offset > f->real_file_size) {
bca4ed4d
JA
155 if (buflen == td->min_bs[ddir])
156 return 0;
157
158 buflen = td->min_bs[ddir];
10ba535a
JA
159 }
160
161 return buflen;
162}
163
164/*
165 * Return the data direction for the next io_u. If the job is a
166 * mixed read/write workload, check the rwmix cycle and switch if
167 * necessary.
168 */
1e97cce9 169static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a
JA
170{
171 if (td_rw(td)) {
172 struct timeval now;
173 unsigned long elapsed;
174
02bcaa8c 175 fio_gettime(&now, NULL);
10ba535a
JA
176 elapsed = mtime_since_now(&td->rwmix_switch);
177
178 /*
179 * Check if it's time to seed a new data direction.
180 */
181 if (elapsed >= td->rwmixcycle) {
182 unsigned int v;
183 long r;
184
185 r = os_random_long(&td->rwmix_state);
186 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
187 if (v < td->rwmixread)
188 td->rwmix_ddir = DDIR_READ;
189 else
190 td->rwmix_ddir = DDIR_WRITE;
191 memcpy(&td->rwmix_switch, &now, sizeof(now));
192 }
193 return td->rwmix_ddir;
194 } else if (td_read(td))
195 return DDIR_READ;
196 else
197 return DDIR_WRITE;
198}
199
10ba535a
JA
200void put_io_u(struct thread_data *td, struct io_u *io_u)
201{
0c6e7517
JA
202 assert((io_u->flags & IO_U_F_FREE) == 0);
203 io_u->flags |= IO_U_F_FREE;
204
10ba535a
JA
205 io_u->file = NULL;
206 list_del(&io_u->list);
207 list_add(&io_u->list, &td->io_u_freelist);
208 td->cur_depth--;
209}
210
755200a3
JA
211void requeue_io_u(struct thread_data *td, struct io_u **io_u)
212{
213 struct io_u *__io_u = *io_u;
214
215 list_del(&__io_u->list);
216 list_add_tail(&__io_u->list, &td->io_u_requeues);
217 td->cur_depth--;
218 *io_u = NULL;
219}
220
10ba535a
JA
221static int fill_io_u(struct thread_data *td, struct fio_file *f,
222 struct io_u *io_u)
223{
224 /*
225 * If using an iolog, grab next piece if any available.
226 */
227 if (td->read_iolog)
228 return read_iolog_get(td, io_u);
229
87dc1ab1
JA
230 /*
231 * see if it's time to sync
232 */
755200a3
JA
233 if (td->fsync_blocks && !(td->io_issues[DDIR_WRITE] % td->fsync_blocks)
234 && td->io_issues[DDIR_WRITE] && should_fsync(td)) {
87dc1ab1
JA
235 io_u->ddir = DDIR_SYNC;
236 io_u->file = f;
237 return 0;
238 }
239
a00735e6
JA
240 io_u->ddir = get_rw_ddir(td);
241
10ba535a 242 /*
c685b5b2
JA
243 * No log, let the seq/rand engine retrieve the next buflen and
244 * position.
10ba535a 245 */
bca4ed4d
JA
246 if (get_next_offset(td, f, io_u))
247 return 1;
10ba535a 248
bca4ed4d
JA
249 io_u->buflen = get_next_buflen(td, f, io_u);
250 if (!io_u->buflen)
251 return 1;
252
253 /*
254 * mark entry before potentially trimming io_u
255 */
413dd459 256 if (!td->read_iolog && td_random(td) && !td->norandommap)
bca4ed4d
JA
257 mark_random_map(td, f, io_u);
258
259 /*
260 * If using a write iolog, store this entry.
261 */
262 if (td->write_iolog_file)
263 write_iolog_put(td, io_u);
264
265 io_u->file = f;
266 return 0;
10ba535a
JA
267}
268
71619dc2
JA
269static void io_u_mark_depth(struct thread_data *td)
270{
271 int index = 0;
272
273 switch (td->cur_depth) {
274 default:
275 index++;
276 case 32 ... 63:
277 index++;
278 case 16 ... 31:
279 index++;
280 case 8 ... 15:
281 index++;
282 case 4 ... 7:
283 index++;
284 case 2 ... 3:
285 index++;
286 case 1:
287 break;
288 }
289
290 td->io_u_map[index]++;
291 td->total_io_u++;
292}
293
ec118304
JA
294static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
295{
296 int index = 0;
297
298 switch (msec) {
299 default:
300 index++;
8abdce66 301 case 1000 ... 1999:
ec118304 302 index++;
8abdce66 303 case 750 ... 999:
ec118304 304 index++;
8abdce66 305 case 500 ... 749:
ec118304 306 index++;
8abdce66 307 case 250 ... 499:
ec118304 308 index++;
8abdce66 309 case 100 ... 249:
ec118304 310 index++;
8abdce66 311 case 50 ... 99:
ec118304 312 index++;
8abdce66 313 case 20 ... 49:
ec118304 314 index++;
8abdce66 315 case 10 ... 19:
ec118304 316 index++;
8abdce66 317 case 4 ... 9:
ec118304
JA
318 index++;
319 case 2 ... 3:
320 index++;
321 case 0 ... 1:
322 break;
323 }
324
325 td->io_u_lat[index]++;
326}
327
0aabe160
JA
328/*
329 * Get next file to service by choosing one at random
330 */
331static struct fio_file *get_next_file_rand(struct thread_data *td)
332{
333 long r = os_random_long(&td->next_file_state);
334 unsigned int fileno;
335 struct fio_file *f;
336
337 do {
338 fileno = (unsigned int) ((double) (td->nr_files - 1) * r / (RAND_MAX + 1.0));
339 f = &td->files[fileno];
340 if (f->fd != -1)
341 return f;
342 } while (1);
343}
344
345/*
346 * Get next file to service by doing round robin between all available ones
347 */
348static struct fio_file *get_next_file_rr(struct thread_data *td)
3d7c391d
JA
349{
350 unsigned int old_next_file = td->next_file;
351 struct fio_file *f;
352
353 do {
354 f = &td->files[td->next_file];
355
356 td->next_file++;
357 if (td->next_file >= td->nr_files)
358 td->next_file = 0;
359
360 if (f->fd != -1)
361 break;
362
363 f = NULL;
364 } while (td->next_file != old_next_file);
365
366 return f;
367}
368
10ba535a
JA
369struct io_u *__get_io_u(struct thread_data *td)
370{
371 struct io_u *io_u = NULL;
372
755200a3
JA
373 if (!list_empty(&td->io_u_requeues))
374 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
375 else if (!queue_full(td)) {
10ba535a
JA
376 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
377
6040dabc 378 io_u->buflen = 0;
10ba535a 379 io_u->resid = 0;
755200a3
JA
380 io_u->file = NULL;
381 }
382
383 if (io_u) {
0c6e7517
JA
384 assert(io_u->flags & IO_U_F_FREE);
385 io_u->flags &= ~IO_U_F_FREE;
386
755200a3 387 io_u->error = 0;
10ba535a
JA
388 list_del(&io_u->list);
389 list_add(&io_u->list, &td->io_u_busylist);
390 td->cur_depth++;
71619dc2 391 io_u_mark_depth(td);
10ba535a
JA
392 }
393
394 return io_u;
395}
396
397/*
398 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
399 * etc. The returned io_u is fully ready to be prepped and submitted.
400 */
3d7c391d 401struct io_u *get_io_u(struct thread_data *td)
10ba535a 402{
3d7c391d 403 struct fio_file *f;
10ba535a
JA
404 struct io_u *io_u;
405
406 io_u = __get_io_u(td);
407 if (!io_u)
408 return NULL;
409
755200a3
JA
410 /*
411 * from a requeue, io_u already setup
412 */
413 if (io_u->file)
77f392bf 414 goto out;
755200a3 415
0aabe160
JA
416 if (td->file_service_type == FIO_FSERVICE_RR)
417 f = get_next_file_rr(td);
418 else
419 f = get_next_file_rand(td);
420
3d7c391d
JA
421 if (!f) {
422 put_io_u(td, io_u);
423 return NULL;
424 }
425
426 io_u->file = f;
427
10ba535a
JA
428 if (td->zone_bytes >= td->zone_size) {
429 td->zone_bytes = 0;
430 f->last_pos += td->zone_skip;
431 }
432
433 if (fill_io_u(td, f, io_u)) {
434 put_io_u(td, io_u);
435 return NULL;
436 }
437
ee88470c 438 if (io_u->buflen + io_u->offset > f->real_file_size) {
10ba535a
JA
439 if (td->io_ops->flags & FIO_RAWIO) {
440 put_io_u(td, io_u);
441 return NULL;
442 }
443
ee88470c 444 io_u->buflen = f->real_file_size - io_u->offset;
10ba535a
JA
445 }
446
87dc1ab1
JA
447 if (io_u->ddir != DDIR_SYNC) {
448 if (!io_u->buflen) {
449 put_io_u(td, io_u);
450 return NULL;
451 }
10ba535a 452
36167d82 453 f->last_pos = io_u->offset + io_u->buflen;
10ba535a 454
87dc1ab1
JA
455 if (td->verify != VERIFY_NONE)
456 populate_verify_io_u(td, io_u);
457 }
10ba535a 458
165faf16
JA
459 /*
460 * Set io data pointers.
461 */
77f392bf 462out:
cec6b55d
JA
463 io_u->xfer_buf = io_u->buf;
464 io_u->xfer_buflen = io_u->buflen;
165faf16 465
36167d82
JA
466 if (td_io_prep(td, io_u)) {
467 put_io_u(td, io_u);
468 return NULL;
469 }
470
02bcaa8c 471 fio_gettime(&io_u->start_time, NULL);
10ba535a
JA
472 return io_u;
473}
474
97601024
JA
475static void io_completed(struct thread_data *td, struct io_u *io_u,
476 struct io_completion_data *icd)
10ba535a 477{
10ba535a
JA
478 unsigned long msec;
479
0c6e7517
JA
480 assert(io_u->flags & IO_U_F_FLIGHT);
481 io_u->flags &= ~IO_U_F_FLIGHT;
482
87dc1ab1
JA
483 if (io_u->ddir == DDIR_SYNC) {
484 td->last_was_sync = 1;
485 return;
486 }
487
488 td->last_was_sync = 0;
489
10ba535a
JA
490 if (!io_u->error) {
491 unsigned int bytes = io_u->buflen - io_u->resid;
1e97cce9 492 const enum fio_ddir idx = io_u->ddir;
3af6ef39 493 int ret;
10ba535a
JA
494
495 td->io_blocks[idx]++;
496 td->io_bytes[idx] += bytes;
497 td->zone_bytes += bytes;
498 td->this_io_bytes[idx] += bytes;
499
02bcaa8c
JA
500 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
501
502 msec = mtime_since(&io_u->issue_time, &icd->time);
10ba535a
JA
503
504 add_clat_sample(td, idx, msec);
02bcaa8c 505 add_bw_sample(td, idx, &icd->time);
ec118304 506 io_u_mark_latency(td, msec);
10ba535a
JA
507
508 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
509 log_io_piece(td, io_u);
510
511 icd->bytes_done[idx] += bytes;
3af6ef39
JA
512
513 if (icd->handler) {
514 ret = icd->handler(io_u);
515 if (ret && !icd->error)
516 icd->error = ret;
517 }
10ba535a
JA
518 } else
519 icd->error = io_u->error;
520}
521
97601024
JA
522static void init_icd(struct io_completion_data *icd, endio_handler *handler,
523 int nr)
10ba535a 524{
02bcaa8c
JA
525 fio_gettime(&icd->time, NULL);
526
3af6ef39
JA
527 icd->handler = handler;
528 icd->nr = nr;
529
10ba535a
JA
530 icd->error = 0;
531 icd->bytes_done[0] = icd->bytes_done[1] = 0;
36167d82
JA
532}
533
97601024
JA
534static void ios_completed(struct thread_data *td,
535 struct io_completion_data *icd)
36167d82
JA
536{
537 struct io_u *io_u;
538 int i;
539
10ba535a
JA
540 for (i = 0; i < icd->nr; i++) {
541 io_u = td->io_ops->event(td, i);
542
543 io_completed(td, io_u, icd);
544 put_io_u(td, io_u);
545 }
546}
97601024 547
e7e6cfb4
JA
548/*
549 * Complete a single io_u for the sync engines.
550 */
97601024
JA
551long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
552 endio_handler *handler)
553{
554 struct io_completion_data icd;
555
556 init_icd(&icd, handler, 1);
557 io_completed(td, io_u, &icd);
558 put_io_u(td, io_u);
559
560 if (!icd.error)
561 return icd.bytes_done[0] + icd.bytes_done[1];
562
97601024
JA
563 return -1;
564}
565
e7e6cfb4
JA
566/*
567 * Called to complete min_events number of io for the async engines.
568 */
97601024
JA
569long io_u_queued_complete(struct thread_data *td, int min_events,
570 endio_handler *handler)
571
572{
97601024 573 struct io_completion_data icd;
00de55ef 574 struct timespec *tvp = NULL;
97601024
JA
575 int ret;
576
755200a3 577 if (min_events > 0) {
755200a3
JA
578 ret = td_io_commit(td);
579 if (ret < 0) {
e1161c32 580 td_verror(td, -ret, "td_io_commit");
755200a3
JA
581 return ret;
582 }
00de55ef
JA
583 } else {
584 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
585
586 tvp = &ts;
755200a3 587 }
97601024 588
00de55ef 589 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
97601024 590 if (ret < 0) {
e1161c32 591 td_verror(td, -ret, "td_io_getevents");
97601024
JA
592 return ret;
593 } else if (!ret)
594 return ret;
595
596 init_icd(&icd, handler, ret);
597 ios_completed(td, &icd);
598 if (!icd.error)
599 return icd.bytes_done[0] + icd.bytes_done[1];
600
97601024
JA
601 return -1;
602}
7e77dd02
JA
603
604/*
605 * Call when io_u is really queued, to update the submission latency.
606 */
607void io_u_queued(struct thread_data *td, struct io_u *io_u)
608{
609 unsigned long slat_time;
610
611 slat_time = mtime_since(&io_u->start_time, &io_u->issue_time);
612 add_slat_sample(td, io_u->ddir, slat_time);
613}
433afcb4 614
55bc9728 615#ifdef FIO_USE_TIMEOUT
433afcb4
JA
616void io_u_set_timeout(struct thread_data *td)
617{
618 assert(td->cur_depth);
619
620 td->timer.it_interval.tv_sec = 0;
621 td->timer.it_interval.tv_usec = 0;
622 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
623 td->timer.it_value.tv_usec = 0;
624 setitimer(ITIMER_REAL, &td->timer, NULL);
625 fio_gettime(&td->timeout_end, NULL);
626}
5945b9b4
JA
627
628static void io_u_dump(struct io_u *io_u)
629{
630 unsigned long t_start = mtime_since_now(&io_u->start_time);
631 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
632
633 log_err("io_u=%p, t_start=%lu, t_issue=%lu\n", io_u, t_start, t_issue);
634 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf, io_u->xfer_buf, io_u->buflen, io_u->xfer_buflen, io_u->offset);
635 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
636}
55bc9728
JA
637#else
638void io_u_set_timeout(struct thread_data fio_unused *td)
639{
640}
641#endif
433afcb4 642
55bc9728 643#ifdef FIO_USE_TIMEOUT
433afcb4
JA
644static void io_u_timeout_handler(int fio_unused sig)
645{
646 struct thread_data *td, *__td;
647 pid_t pid = getpid();
5945b9b4
JA
648 struct list_head *entry;
649 struct io_u *io_u;
433afcb4
JA
650 int i;
651
652 log_err("fio: io_u timeout\n");
653
654 /*
655 * TLS would be nice...
656 */
657 td = NULL;
658 for_each_td(__td, i) {
659 if (__td->pid == pid) {
660 td = __td;
661 break;
662 }
663 }
664
665 if (!td) {
666 log_err("fio: io_u timeout, can't find job\n");
667 exit(1);
668 }
669
670 if (!td->cur_depth) {
671 log_err("fio: timeout without pending work?\n");
672 return;
673 }
674
675 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->name, td->pid);
5945b9b4
JA
676
677 list_for_each(entry, &td->io_u_busylist) {
678 io_u = list_entry(entry, struct io_u, list);
679
680 io_u_dump(io_u);
681 }
682
683 td_verror(td, ETIMEDOUT, "io_u timeout");
433afcb4
JA
684 exit(1);
685}
55bc9728 686#endif
433afcb4
JA
687
688void io_u_init_timeout(void)
689{
55bc9728 690#ifdef FIO_USE_TIMEOUT
433afcb4 691 signal(SIGALRM, io_u_timeout_handler);
55bc9728 692#endif
433afcb4 693}