[PATCH] Add support for read/write mixed io
[fio.git] / fio.c
CommitLineData
ebac4655
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <string.h>
26#include <errno.h>
27#include <signal.h>
28#include <time.h>
29#include <math.h>
30#include <assert.h>
31#include <dirent.h>
32#include <libgen.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
36#include <sys/ipc.h>
37#include <sys/shm.h>
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40
41#include "fio.h"
42#include "os.h"
43
44#define MASK (4095)
45
46#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
47
48int groupid = 0;
49int thread_number = 0;
50static char run_str[MAX_JOBS + 1];
51int shm_id = 0;
52static LIST_HEAD(disk_list);
53static struct itimerval itimer;
5289b847 54static struct timeval genesis;
ebac4655
JA
55
56static void update_io_ticks(void);
57static void disk_util_timer_arm(void);
58static void print_thread_status(void);
59
60/*
61 * thread life cycle
62 */
63enum {
64 TD_NOT_CREATED = 0,
65 TD_CREATED,
66 TD_RUNNING,
67 TD_VERIFYING,
68 TD_EXITED,
69 TD_REAPED,
70};
71
3d60d1ed 72#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655
JA
73
74static sem_t startup_sem;
75
76#define TERMINATE_ALL (-1)
77
78static void terminate_threads(int group_id)
79{
80 int i;
81
82 for (i = 0; i < thread_number; i++) {
83 struct thread_data *td = &threads[i];
84
85 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
86 td->terminate = 1;
87 td->start_delay = 0;
88 }
89 }
90}
91
92static void sig_handler(int sig)
93{
94 switch (sig) {
95 case SIGALRM:
96 update_io_ticks();
97 disk_util_timer_arm();
98 print_thread_status();
99 break;
100 default:
101 printf("\nfio: terminating on signal\n");
102 fflush(stdout);
103 terminate_threads(TERMINATE_ALL);
104 break;
105 }
106}
107
108static unsigned long utime_since(struct timeval *s, struct timeval *e)
109{
110 double sec, usec;
111
112 sec = e->tv_sec - s->tv_sec;
113 usec = e->tv_usec - s->tv_usec;
114 if (sec > 0 && usec < 0) {
115 sec--;
116 usec += 1000000;
117 }
118
119 sec *= (double) 1000000;
120
121 return sec + usec;
122}
123
124static unsigned long utime_since_now(struct timeval *s)
125{
126 struct timeval t;
127
128 gettimeofday(&t, NULL);
129 return utime_since(s, &t);
130}
131
132static unsigned long mtime_since(struct timeval *s, struct timeval *e)
133{
134 double sec, usec;
135
136 sec = e->tv_sec - s->tv_sec;
137 usec = e->tv_usec - s->tv_usec;
138 if (sec > 0 && usec < 0) {
139 sec--;
140 usec += 1000000;
141 }
142
143 sec *= (double) 1000;
144 usec /= (double) 1000;
145
146 return sec + usec;
147}
148
149static unsigned long mtime_since_now(struct timeval *s)
150{
151 struct timeval t;
152
153 gettimeofday(&t, NULL);
154 return mtime_since(s, &t);
155}
156
157static inline unsigned long msec_now(struct timeval *s)
158{
159 return s->tv_sec * 1000 + s->tv_usec / 1000;
160}
161
5289b847
JA
162static unsigned long time_since_now(struct timeval *s)
163{
164 return mtime_since_now(s) / 1000;
165}
166
ebac4655
JA
167static int random_map_free(struct thread_data *td, unsigned long long block)
168{
169 unsigned int idx = RAND_MAP_IDX(td, block);
170 unsigned int bit = RAND_MAP_BIT(td, block);
171
172 return (td->file_map[idx] & (1UL << bit)) == 0;
173}
174
175static int get_next_free_block(struct thread_data *td, unsigned long long *b)
176{
177 int i;
178
179 *b = 0;
180 i = 0;
181 while ((*b) * td->min_bs < td->io_size) {
182 if (td->file_map[i] != -1UL) {
183 *b += ffz(td->file_map[i]);
184 return 0;
185 }
186
187 *b += BLOCKS_PER_MAP;
188 i++;
189 }
190
191 return 1;
192}
193
194static void mark_random_map(struct thread_data *td, struct io_u *io_u)
195{
196 unsigned long block = io_u->offset / td->min_bs;
197 unsigned int blocks = 0;
198
199 while (blocks < (io_u->buflen / td->min_bs)) {
200 unsigned int idx, bit;
201
202 if (!random_map_free(td, block))
203 break;
204
205 idx = RAND_MAP_IDX(td, block);
206 bit = RAND_MAP_BIT(td, block);
207
208 assert(idx < td->num_maps);
209
210 td->file_map[idx] |= (1UL << bit);
211 block++;
212 blocks++;
213 }
214
215 if ((blocks * td->min_bs) < io_u->buflen)
216 io_u->buflen = blocks * td->min_bs;
217}
218
ebac4655
JA
219static inline void add_stat_sample(struct io_stat *is, unsigned long val)
220{
221 if (val > is->max_val)
222 is->max_val = val;
223 if (val < is->min_val)
224 is->min_val = val;
225
226 is->val += val;
227 is->val_sq += val * val;
228 is->samples++;
229}
230
231static void add_log_sample(struct thread_data *td, struct io_log *iolog,
232 unsigned long val, int ddir)
233{
234 if (iolog->nr_samples == iolog->max_samples) {
235 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
236
237 iolog->log = realloc(iolog->log, new_size);
238 iolog->max_samples <<= 1;
239 }
240
241 iolog->log[iolog->nr_samples].val = val;
242 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
243 iolog->log[iolog->nr_samples].ddir = ddir;
244 iolog->nr_samples++;
245}
246
247static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec)
248{
249 add_stat_sample(&td->clat_stat[ddir], msec);
250
251 if (td->clat_log)
252 add_log_sample(td, td->clat_log, msec, ddir);
253}
254
255static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec)
256{
257 add_stat_sample(&td->slat_stat[ddir], msec);
258
259 if (td->slat_log)
260 add_log_sample(td, td->slat_log, msec, ddir);
261}
262
263static void add_bw_sample(struct thread_data *td, int ddir)
264{
265 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
266 unsigned long rate;
267
268 if (spent < td->bw_avg_time)
269 return;
270
271 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
272 add_stat_sample(&td->bw_stat[ddir], rate);
273
274 if (td->bw_log)
275 add_log_sample(td, td->bw_log, rate, ddir);
276
277 gettimeofday(&td->stat_sample_time[ddir], NULL);
278 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
279}
280
20dc95c4
JA
281static int get_next_offset(struct thread_data *td, unsigned long long *offset)
282{
283 unsigned long long b, rb;
284 long r;
285
286 if (!td->sequential) {
287 unsigned long max_blocks = td->io_size / td->min_bs;
288 int loops = 50;
289
290 do {
291 lrand48_r(&td->random_state, &r);
292 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
293 rb = b + (td->file_offset / td->min_bs);
294 loops--;
295 } while (!random_map_free(td, rb) && loops);
296
297 if (!loops) {
298 if (get_next_free_block(td, &b))
299 return 1;
300 }
301 } else
302 b = td->last_pos / td->min_bs;
303
304 *offset = (b * td->min_bs) + td->file_offset;
838a3cd3 305 if (*offset > td->real_file_size)
20dc95c4
JA
306 return 1;
307
308 return 0;
309}
310
311static unsigned int get_next_buflen(struct thread_data *td)
312{
313 unsigned int buflen;
314 long r;
315
316 if (td->min_bs == td->max_bs)
317 buflen = td->min_bs;
318 else {
319 lrand48_r(&td->bsrange_state, &r);
320 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
321 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
322 }
323
324 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
325 buflen = td->io_size - td->this_io_bytes[td->ddir];
326
327 return buflen;
328}
329
ebac4655
JA
330/*
331 * busy looping version for the last few usec
332 */
333static void __usec_sleep(unsigned int usec)
334{
335 struct timeval start;
336
337 gettimeofday(&start, NULL);
338 while (utime_since_now(&start) < usec)
339 nop;
340}
341
342static void usec_sleep(struct thread_data *td, unsigned long usec)
343{
344 struct timespec req, rem;
345
346 req.tv_sec = usec / 1000000;
347 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
348
349 do {
350 if (usec < 5000) {
351 __usec_sleep(usec);
352 break;
353 }
354
355 rem.tv_sec = rem.tv_nsec = 0;
356 if (nanosleep(&req, &rem) < 0)
357 break;
358
359 if ((rem.tv_sec + rem.tv_nsec) == 0)
360 break;
361
362 req.tv_nsec = rem.tv_nsec;
363 req.tv_sec = rem.tv_sec;
364
365 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
366 } while (!td->terminate);
367}
368
369static void rate_throttle(struct thread_data *td, unsigned long time_spent,
370 unsigned int bytes)
371{
372 unsigned long usec_cycle;
373
374 if (!td->rate)
375 return;
376
377 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
378
379 if (time_spent < usec_cycle) {
380 unsigned long s = usec_cycle - time_spent;
381
382 td->rate_pending_usleep += s;
383 if (td->rate_pending_usleep >= 100000) {
384 usec_sleep(td, td->rate_pending_usleep);
385 td->rate_pending_usleep = 0;
386 }
387 } else {
388 long overtime = time_spent - usec_cycle;
389
390 td->rate_pending_usleep -= overtime;
391 }
392}
393
394static int check_min_rate(struct thread_data *td, struct timeval *now)
395{
396 unsigned long spent;
397 unsigned long rate;
398 int ddir = td->ddir;
399
400 /*
401 * allow a 2 second settle period in the beginning
402 */
403 if (mtime_since(&td->start, now) < 2000)
404 return 0;
405
406 /*
407 * if rate blocks is set, sample is running
408 */
409 if (td->rate_bytes) {
410 spent = mtime_since(&td->lastrate, now);
411 if (spent < td->ratecycle)
412 return 0;
413
414 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
415 if (rate < td->ratemin) {
416 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
417 if (rate_quit)
418 terminate_threads(td->groupid);
419 return 1;
420 }
421 }
422
423 td->rate_bytes = td->this_io_bytes[ddir];
424 memcpy(&td->lastrate, now, sizeof(*now));
425 return 0;
426}
427
428static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
429{
430 if (!td->timeout)
431 return 0;
432 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
433 return 1;
434
435 return 0;
436}
437
438static void fill_random_bytes(struct thread_data *td,
439 unsigned char *p, unsigned int len)
440{
441 unsigned int todo;
442 double r;
443
444 while (len) {
445 drand48_r(&td->verify_state, &r);
446
447 /*
448 * lrand48_r seems to be broken and only fill the bottom
449 * 32-bits, even on 64-bit archs with 64-bit longs
450 */
451 todo = sizeof(r);
452 if (todo > len)
453 todo = len;
454
455 memcpy(p, &r, todo);
456
457 len -= todo;
458 p += todo;
459 }
460}
461
462static void hexdump(void *buffer, int len)
463{
464 unsigned char *p = buffer;
465 int i;
466
467 for (i = 0; i < len; i++)
468 printf("%02x", p[i]);
469 printf("\n");
470}
471
472static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
473{
474 unsigned char *p = (unsigned char *) io_u->buf;
475 unsigned long c;
476 int ret;
477
478 p += sizeof(*hdr);
479 c = crc32(p, hdr->len - sizeof(*hdr));
480 ret = c != hdr->crc32;
481
482 if (ret) {
483 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
484 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
485 }
486
487 return ret;
488}
489
490static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
491{
492 unsigned char *p = (unsigned char *) io_u->buf;
493 struct md5_ctx md5_ctx;
494 int ret;
495
496 memset(&md5_ctx, 0, sizeof(md5_ctx));
497 p += sizeof(*hdr);
498 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
499
500 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
501 if (ret) {
502 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
503 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
504 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
505 }
506
507 return ret;
508}
509
510static int verify_io_u(struct io_u *io_u)
511{
512 struct verify_header *hdr = (struct verify_header *) io_u->buf;
513 int ret;
514
515 if (hdr->fio_magic != FIO_HDR_MAGIC)
516 return 1;
517
518 if (hdr->verify_type == VERIFY_MD5)
519 ret = verify_io_u_md5(hdr, io_u);
520 else if (hdr->verify_type == VERIFY_CRC32)
521 ret = verify_io_u_crc32(hdr, io_u);
522 else {
523 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
524 ret = 1;
525 }
526
527 return ret;
528}
529
530static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
531{
532 hdr->crc32 = crc32(p, len);
533}
534
535static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
536{
537 struct md5_ctx md5_ctx;
538
539 memset(&md5_ctx, 0, sizeof(md5_ctx));
540 md5_update(&md5_ctx, p, len);
541 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
542}
543
3d60d1ed
JA
544unsigned int hweight32(unsigned int w)
545{
546 unsigned int res = w - ((w >> 1) & 0x55555555);
547
548 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
549 res = (res + (res >> 4)) & 0x0F0F0F0F;
550 res = res + (res >> 8);
551
552 return (res + (res >> 16)) & 0x000000FF;
553}
554
555unsigned long hweight64(unsigned long long w)
556{
557#if __WORDSIZE == 32
558 return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
559#elif __WORDSIZE == 64
560 unsigned long long v = w - ((w >> 1) & 0x5555555555555555ul);
561
562 v = (v & 0x3333333333333333ul) + ((v >> 2) & 0x3333333333333333ul);
563 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
564 v = v + (v >> 8);
565 v = v + (v >> 16);
566
567 return (v + (v >> 32)) & 0x00000000000000FFul;
568#else
569#error __WORDSIZE not defined
570#endif
571}
572
573static int get_rw_ddir(struct thread_data *td)
574{
575 /*
576 * perhaps cheasy, but use the hamming weight of the position
577 * as a randomizer for data direction.
578 */
579 if (td_rw(td))
580 return hweight64(td->last_pos) & 1;
581 else if (td_read(td))
582 return DDIR_READ;
583 else
584 return DDIR_WRITE;
585}
586
ebac4655
JA
587/*
588 * fill body of io_u->buf with random data and add a header with the
589 * (eg) sha1sum of that data.
590 */
591static void populate_io_u(struct thread_data *td, struct io_u *io_u)
592{
593 unsigned char *p = (unsigned char *) io_u->buf;
594 struct verify_header hdr;
595
596 hdr.fio_magic = FIO_HDR_MAGIC;
597 hdr.len = io_u->buflen;
598 p += sizeof(hdr);
599 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
600
601 if (td->verify == VERIFY_MD5) {
602 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
603 hdr.verify_type = VERIFY_MD5;
604 } else {
605 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
606 hdr.verify_type = VERIFY_CRC32;
607 }
608
609 memcpy(io_u->buf, &hdr, sizeof(hdr));
610}
611
20dc95c4
JA
612static int td_io_prep(struct thread_data *td, struct io_u *io_u, int read)
613{
614 if (read)
615 io_u->ddir = DDIR_READ;
616 else
617 io_u->ddir = DDIR_WRITE;
618
619 if (td->io_prep && td->io_prep(td, io_u))
620 return 1;
621
622 return 0;
623}
624
b1ff3403 625void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
626{
627 list_del(&io_u->list);
628 list_add(&io_u->list, &td->io_u_freelist);
629 td->cur_depth--;
630}
631
632#define queue_full(td) (list_empty(&(td)->io_u_freelist))
633
b1ff3403 634struct io_u *__get_io_u(struct thread_data *td)
ebac4655
JA
635{
636 struct io_u *io_u;
637
638 if (queue_full(td))
639 return NULL;
640
641 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
642 io_u->error = 0;
643 io_u->resid = 0;
644 list_del(&io_u->list);
645 list_add(&io_u->list, &td->io_u_busylist);
646 td->cur_depth++;
647 return io_u;
648}
649
ebac4655
JA
650static struct io_u *get_io_u(struct thread_data *td)
651{
652 struct io_u *io_u;
653
654 io_u = __get_io_u(td);
655 if (!io_u)
656 return NULL;
657
20dc95c4
JA
658 if (td->zone_bytes >= td->zone_size) {
659 td->zone_bytes = 0;
660 td->last_pos += td->zone_skip;
661 }
662
ebac4655
JA
663 if (get_next_offset(td, &io_u->offset)) {
664 put_io_u(td, io_u);
665 return NULL;
666 }
667
668 io_u->buflen = get_next_buflen(td);
669 if (!io_u->buflen) {
670 put_io_u(td, io_u);
671 return NULL;
672 }
673
838a3cd3
JA
674 if (io_u->buflen + io_u->offset > td->real_file_size)
675 io_u->buflen = td->real_file_size - io_u->offset;
ebac4655
JA
676
677 if (!io_u->buflen) {
678 put_io_u(td, io_u);
679 return NULL;
680 }
681
682 if (!td->sequential)
683 mark_random_map(td, io_u);
684
20dc95c4 685 td->last_pos += io_u->buflen;
ebac4655
JA
686
687 if (td->verify != VERIFY_NONE)
688 populate_io_u(td, io_u);
689
3d60d1ed 690 if (td_io_prep(td, io_u, get_rw_ddir(td))) {
ebac4655
JA
691 put_io_u(td, io_u);
692 return NULL;
693 }
694
695 gettimeofday(&io_u->start_time, NULL);
696 return io_u;
697}
698
699static inline void td_set_runstate(struct thread_data *td, int runstate)
700{
701 td->old_runstate = td->runstate;
702 td->runstate = runstate;
703}
704
705static int get_next_verify(struct thread_data *td,
706 unsigned long long *offset, unsigned int *len)
707{
708 struct io_piece *ipo;
709
710 if (list_empty(&td->io_hist_list))
711 return 1;
712
713 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
714 list_del(&ipo->list);
715
716 *offset = ipo->offset;
717 *len = ipo->len;
718 free(ipo);
719 return 0;
720}
721
722static void prune_io_piece_log(struct thread_data *td)
723{
724 struct io_piece *ipo;
725
726 while (!list_empty(&td->io_hist_list)) {
727 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
728
729 list_del(&ipo->list);
730 free(ipo);
731 }
732}
733
734/*
735 * log a succesful write, so we can unwind the log for verify
736 */
737static void log_io_piece(struct thread_data *td, struct io_u *io_u)
738{
739 struct io_piece *ipo = malloc(sizeof(struct io_piece));
740 struct list_head *entry;
741
742 INIT_LIST_HEAD(&ipo->list);
743 ipo->offset = io_u->offset;
744 ipo->len = io_u->buflen;
745
746 /*
747 * for random io where the writes extend the file, it will typically
748 * be laid out with the block scattered as written. it's faster to
749 * read them in in that order again, so don't sort
750 */
751 if (td->sequential || !td->overwrite) {
752 list_add_tail(&ipo->list, &td->io_hist_list);
753 return;
754 }
755
756 /*
757 * for random io, sort the list so verify will run faster
758 */
759 entry = &td->io_hist_list;
760 while ((entry = entry->prev) != &td->io_hist_list) {
761 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
762
763 if (__ipo->offset < ipo->offset)
764 break;
765 }
766
767 list_add(&ipo->list, entry);
768}
769
770static int sync_td(struct thread_data *td)
771{
772 if (td->io_sync)
773 return td->io_sync(td);
774
775 return 0;
776}
777
778static int io_u_getevents(struct thread_data *td, int min, int max,
779 struct timespec *t)
780{
781 return td->io_getevents(td, min, max, t);
782}
783
784static int io_u_queue(struct thread_data *td, struct io_u *io_u)
785{
786 gettimeofday(&io_u->issue_time, NULL);
787
788 return td->io_queue(td, io_u);
789}
790
791#define iocb_time(iocb) ((unsigned long) (iocb)->data)
792
793static void io_completed(struct thread_data *td, struct io_u *io_u,
794 struct io_completion_data *icd)
795{
796 struct timeval e;
797 unsigned long msec;
798
799 gettimeofday(&e, NULL);
800
801 if (!io_u->error) {
20dc95c4 802 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 803 const int idx = io_u->ddir;
ebac4655
JA
804
805 td->io_blocks[idx]++;
20dc95c4
JA
806 td->io_bytes[idx] += bytes;
807 td->zone_bytes += bytes;
808 td->this_io_bytes[idx] += bytes;
ebac4655
JA
809
810 msec = mtime_since(&io_u->issue_time, &e);
811
3d60d1ed
JA
812 add_clat_sample(td, idx, msec);
813 add_bw_sample(td, idx);
ebac4655 814
3d60d1ed 815 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
816 log_io_piece(td, io_u);
817
20dc95c4 818 icd->bytes_done[idx] += bytes;
ebac4655
JA
819 } else
820 icd->error = io_u->error;
821}
822
823static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
824{
825 struct io_u *io_u;
826 int i;
827
828 icd->error = 0;
829 icd->bytes_done[0] = icd->bytes_done[1] = 0;
830
831 for (i = 0; i < icd->nr; i++) {
832 io_u = td->io_event(td, i);
833
834 io_completed(td, io_u, icd);
835 put_io_u(td, io_u);
836 }
837}
838
839static void cleanup_pending_aio(struct thread_data *td)
840{
841 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
842 struct list_head *entry, *n;
843 struct io_completion_data icd;
844 struct io_u *io_u;
845 int r;
846
847 /*
848 * get immediately available events, if any
849 */
850 r = io_u_getevents(td, 0, td->cur_depth, &ts);
851 if (r > 0) {
852 icd.nr = r;
853 ios_completed(td, &icd);
854 }
855
856 /*
857 * now cancel remaining active events
858 */
859 if (td->io_cancel) {
860 list_for_each_safe(entry, n, &td->io_u_busylist) {
861 io_u = list_entry(entry, struct io_u, list);
862
863 r = td->io_cancel(td, io_u);
864 if (!r)
865 put_io_u(td, io_u);
866 }
867 }
868
869 if (td->cur_depth) {
870 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
871 if (r > 0) {
872 icd.nr = r;
873 ios_completed(td, &icd);
874 }
875 }
876}
877
878static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
879{
880 struct io_u *v_io_u = *io_u;
881 int ret = 0;
882
883 if (v_io_u) {
884 ret = verify_io_u(v_io_u);
885 put_io_u(td, v_io_u);
886 *io_u = NULL;
887 }
888
889 return ret;
890}
891
892static void do_verify(struct thread_data *td)
893{
894 struct timeval t;
895 struct io_u *io_u, *v_io_u = NULL;
896 struct io_completion_data icd;
897 int ret;
898
899 td_set_runstate(td, TD_VERIFYING);
900
901 do {
902 if (td->terminate)
903 break;
904
905 gettimeofday(&t, NULL);
906 if (runtime_exceeded(td, &t))
907 break;
908
909 io_u = __get_io_u(td);
910 if (!io_u)
911 break;
912
913 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
914 put_io_u(td, io_u);
915 break;
916 }
917
918 if (td_io_prep(td, io_u, 1)) {
919 put_io_u(td, io_u);
920 break;
921 }
922
923 ret = io_u_queue(td, io_u);
924 if (ret) {
925 put_io_u(td, io_u);
926 td_verror(td, ret);
927 break;
928 }
929
930 /*
931 * we have one pending to verify, do that while
932 * we are doing io on the next one
933 */
934 if (do_io_u_verify(td, &v_io_u))
935 break;
936
937 ret = io_u_getevents(td, 1, 1, NULL);
938 if (ret != 1) {
939 if (ret < 0)
940 td_verror(td, ret);
941 break;
942 }
943
944 v_io_u = td->io_event(td, 0);
945 icd.nr = 1;
946 icd.error = 0;
947 io_completed(td, v_io_u, &icd);
948
949 if (icd.error) {
950 td_verror(td, icd.error);
951 put_io_u(td, v_io_u);
952 v_io_u = NULL;
953 break;
954 }
955
ebac4655
JA
956 /*
957 * if we can't submit more io, we need to verify now
958 */
959 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
960 break;
961
962 } while (1);
963
964 do_io_u_verify(td, &v_io_u);
965
966 if (td->cur_depth)
967 cleanup_pending_aio(td);
968
969 td_set_runstate(td, TD_RUNNING);
970}
971
972static void do_io(struct thread_data *td)
973{
974 struct io_completion_data icd;
975 struct timeval s, e;
976 unsigned long usec;
977
978 while (td->this_io_bytes[td->ddir] < td->io_size) {
979 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
980 struct timespec *timeout;
981 int ret, min_evts = 0;
982 struct io_u *io_u;
983
984 if (td->terminate)
985 break;
986
987 io_u = get_io_u(td);
988 if (!io_u)
989 break;
990
991 memcpy(&s, &io_u->start_time, sizeof(s));
992
993 ret = io_u_queue(td, io_u);
994 if (ret) {
995 put_io_u(td, io_u);
996 td_verror(td, ret);
997 break;
998 }
999
1000 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
1001
1002 if (td->cur_depth < td->iodepth) {
1003 timeout = &ts;
1004 min_evts = 0;
1005 } else {
1006 timeout = NULL;
1007 min_evts = 1;
1008 }
1009
1010 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1011 if (ret < 0) {
1012 td_verror(td, ret);
1013 break;
1014 } else if (!ret)
1015 continue;
1016
1017 icd.nr = ret;
1018 ios_completed(td, &icd);
1019 if (icd.error) {
1020 td_verror(td, icd.error);
1021 break;
1022 }
1023
1024 /*
1025 * the rate is batched for now, it should work for batches
1026 * of completions except the very first one which may look
1027 * a little bursty
1028 */
1029 gettimeofday(&e, NULL);
1030 usec = utime_since(&s, &e);
1031
1032 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
1033
1034 if (check_min_rate(td, &e)) {
1035 td_verror(td, ENOMEM);
1036 break;
1037 }
1038
1039 if (runtime_exceeded(td, &e))
1040 break;
1041
1042 if (td->thinktime)
1043 usec_sleep(td, td->thinktime);
1044
1045 if (should_fsync(td) && td->fsync_blocks &&
1046 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
1047 sync_td(td);
1048 }
1049
1050 if (td->cur_depth)
1051 cleanup_pending_aio(td);
1052
1053 if (should_fsync(td))
1054 sync_td(td);
1055}
1056
1057static void cleanup_io(struct thread_data *td)
1058{
1059 if (td->io_cleanup)
1060 td->io_cleanup(td);
1061}
1062
1063static int init_io(struct thread_data *td)
1064{
1065 if (td->io_engine == FIO_SYNCIO)
1066 return fio_syncio_init(td);
1067 else if (td->io_engine == FIO_MMAPIO)
1068 return fio_mmapio_init(td);
1069 else if (td->io_engine == FIO_LIBAIO)
1070 return fio_libaio_init(td);
1071 else if (td->io_engine == FIO_POSIXAIO)
1072 return fio_posixaio_init(td);
1073 else if (td->io_engine == FIO_SGIO)
1074 return fio_sgio_init(td);
1075 else {
1076 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1077 return 1;
1078 }
1079}
1080
1081static void cleanup_io_u(struct thread_data *td)
1082{
1083 struct list_head *entry, *n;
1084 struct io_u *io_u;
1085
1086 list_for_each_safe(entry, n, &td->io_u_freelist) {
1087 io_u = list_entry(entry, struct io_u, list);
1088
1089 list_del(&io_u->list);
1090 free(io_u);
1091 }
1092
1093 if (td->mem_type == MEM_MALLOC)
1094 free(td->orig_buffer);
1095 else if (td->mem_type == MEM_SHM) {
1096 struct shmid_ds sbuf;
1097
1098 shmdt(td->orig_buffer);
1099 shmctl(td->shm_id, IPC_RMID, &sbuf);
1100 } else if (td->mem_type == MEM_MMAP)
1101 munmap(td->orig_buffer, td->orig_buffer_size);
1102 else
1103 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1104
1105 td->orig_buffer = NULL;
1106}
1107
1108static int init_io_u(struct thread_data *td)
1109{
1110 struct io_u *io_u;
1111 int i, max_units;
1112 char *p;
1113
1114 if (td->io_engine & FIO_SYNCIO)
1115 max_units = 1;
1116 else
1117 max_units = td->iodepth;
1118
1119 td->orig_buffer_size = td->max_bs * max_units + MASK;
1120
1121 if (td->mem_type == MEM_MALLOC)
1122 td->orig_buffer = malloc(td->orig_buffer_size);
1123 else if (td->mem_type == MEM_SHM) {
1124 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1125 if (td->shm_id < 0) {
1126 td_verror(td, errno);
1127 perror("shmget");
1128 return 1;
1129 }
1130
1131 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1132 if (td->orig_buffer == (void *) -1) {
1133 td_verror(td, errno);
1134 perror("shmat");
1135 td->orig_buffer = NULL;
1136 return 1;
1137 }
1138 } else if (td->mem_type == MEM_MMAP) {
1139 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1140 if (td->orig_buffer == MAP_FAILED) {
1141 td_verror(td, errno);
1142 perror("mmap");
1143 td->orig_buffer = NULL;
1144 return 1;
1145 }
1146 }
1147
1148 INIT_LIST_HEAD(&td->io_u_freelist);
1149 INIT_LIST_HEAD(&td->io_u_busylist);
1150 INIT_LIST_HEAD(&td->io_hist_list);
1151
1152 p = ALIGN(td->orig_buffer);
1153 for (i = 0; i < max_units; i++) {
1154 io_u = malloc(sizeof(*io_u));
1155 memset(io_u, 0, sizeof(*io_u));
1156 INIT_LIST_HEAD(&io_u->list);
1157
1158 io_u->buf = p + td->max_bs * i;
b1ff3403 1159 io_u->index = i;
ebac4655
JA
1160 list_add(&io_u->list, &td->io_u_freelist);
1161 }
1162
1163 return 0;
1164}
1165
1166static int create_file(struct thread_data *td, unsigned long long size,
1167 int extend)
1168{
1169 unsigned long long left;
1170 unsigned int bs;
1171 int r, oflags;
1172 char *b;
1173
1174 /*
1175 * unless specifically asked for overwrite, let normal io extend it
1176 */
1177 if (td_write(td) && !td->overwrite)
1178 return 0;
1179
1180 if (!size) {
1181 fprintf(stderr, "Need size for create\n");
1182 td_verror(td, EINVAL);
1183 return 1;
1184 }
1185
1186 if (!extend) {
1187 oflags = O_CREAT | O_TRUNC;
1188 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1189 } else {
1190 oflags = O_APPEND;
1191 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1192 }
1193
1194 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1195 if (td->fd < 0) {
1196 td_verror(td, errno);
1197 return 1;
1198 }
1199
1200 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1201 td_verror(td, errno);
1202 return 1;
1203 }
1204
1205 td->io_size = td->file_size;
1206 b = malloc(td->max_bs);
1207 memset(b, 0, td->max_bs);
1208
1209 left = size;
1210 while (left && !td->terminate) {
1211 bs = td->max_bs;
1212 if (bs > left)
1213 bs = left;
1214
1215 r = write(td->fd, b, bs);
1216
1217 if (r == (int) bs) {
1218 left -= bs;
1219 continue;
1220 } else {
1221 if (r < 0)
1222 td_verror(td, errno);
1223 else
1224 td_verror(td, EIO);
1225
1226 break;
1227 }
1228 }
1229
1230 if (td->terminate)
1231 unlink(td->file_name);
1232 else if (td->create_fsync)
1233 fsync(td->fd);
1234
1235 close(td->fd);
1236 td->fd = -1;
1237 free(b);
1238 return 0;
1239}
1240
1241static int file_size(struct thread_data *td)
1242{
1243 struct stat st;
1244
1245 if (fstat(td->fd, &st) == -1) {
1246 td_verror(td, errno);
1247 return 1;
1248 }
1249
838a3cd3
JA
1250 td->real_file_size = st.st_size;
1251
1252 if (!td->file_size || td->file_size > td->real_file_size)
1253 td->file_size = td->real_file_size;
ebac4655 1254
e8e387c1 1255 td->file_size -= td->file_offset;
ebac4655
JA
1256 return 0;
1257}
1258
1259static int bdev_size(struct thread_data *td)
1260{
9104f874 1261 unsigned long long bytes;
ebac4655
JA
1262 int r;
1263
1264 r = blockdev_size(td->fd, &bytes);
1265 if (r) {
1266 td_verror(td, r);
1267 return 1;
1268 }
1269
838a3cd3
JA
1270 td->real_file_size = bytes;
1271
ebac4655
JA
1272 /*
1273 * no extend possibilities, so limit size to device size if too large
1274 */
838a3cd3
JA
1275 if (!td->file_size || td->file_size > td->real_file_size)
1276 td->file_size = td->real_file_size;
ebac4655 1277
e8e387c1 1278 td->file_size -= td->file_offset;
ebac4655
JA
1279 return 0;
1280}
1281
1282static int get_file_size(struct thread_data *td)
1283{
0af7b542 1284 int ret = 0;
ebac4655
JA
1285
1286 if (td->filetype == FIO_TYPE_FILE)
1287 ret = file_size(td);
0af7b542 1288 else if (td->filetype == FIO_TYPE_BD)
ebac4655 1289 ret = bdev_size(td);
0af7b542
JA
1290 else
1291 td->real_file_size = -1;
ebac4655
JA
1292
1293 if (ret)
1294 return ret;
1295
e8e387c1
JA
1296 if (td->file_offset > td->real_file_size) {
1297 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
ebac4655
JA
1298 return 1;
1299 }
1300
838a3cd3 1301 td->io_size = td->file_size;
ebac4655
JA
1302 if (td->io_size == 0) {
1303 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1304 td_verror(td, EINVAL);
1305 return 1;
1306 }
1307
20dc95c4
JA
1308 if (!td->zone_size)
1309 td->zone_size = td->io_size;
1310
ebac4655
JA
1311 td->total_io_size = td->io_size * td->loops;
1312 return 0;
1313}
1314
1315static int setup_file_mmap(struct thread_data *td)
1316{
1317 int flags;
1318
3d60d1ed
JA
1319 if (td_rw(td))
1320 flags = PROT_READ | PROT_WRITE;
1321 else if (td_write(td)) {
ebac4655
JA
1322 flags = PROT_WRITE;
1323
1324 if (td->verify != VERIFY_NONE)
1325 flags |= PROT_READ;
3d60d1ed
JA
1326 } else
1327 flags = PROT_READ;
ebac4655
JA
1328
1329 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1330 if (td->mmap == MAP_FAILED) {
1331 td->mmap = NULL;
1332 td_verror(td, errno);
1333 return 1;
1334 }
1335
1336 if (td->invalidate_cache) {
1337 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1338 td_verror(td, errno);
1339 return 1;
1340 }
1341 }
1342
1343 if (td->sequential) {
1344 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1345 td_verror(td, errno);
1346 return 1;
1347 }
1348 } else {
1349 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1350 td_verror(td, errno);
1351 return 1;
1352 }
1353 }
1354
1355 return 0;
1356}
1357
1358static int setup_file_plain(struct thread_data *td)
1359{
1360 if (td->invalidate_cache) {
1361 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1362 td_verror(td, errno);
1363 return 1;
1364 }
1365 }
1366
1367 if (td->sequential) {
1368 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1369 td_verror(td, errno);
1370 return 1;
1371 }
1372 } else {
1373 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1374 td_verror(td, errno);
1375 return 1;
1376 }
1377 }
1378
1379 return 0;
1380}
1381
1382static int setup_file(struct thread_data *td)
1383{
1384 struct stat st;
1385 int flags = 0;
1386
1387 if (stat(td->file_name, &st) == -1) {
1388 if (errno != ENOENT) {
1389 td_verror(td, errno);
1390 return 1;
1391 }
1392 if (!td->create_file) {
1393 td_verror(td, ENOENT);
1394 return 1;
1395 }
1396 if (create_file(td, td->file_size, 0))
1397 return 1;
1398 } else if (td->filetype == FIO_TYPE_FILE) {
6a0106a0 1399 if (st.st_size < (off_t) td->file_size) {
ebac4655
JA
1400 if (create_file(td, td->file_size - st.st_size, 1))
1401 return 1;
1402 }
1403 }
1404
1405 if (td->odirect)
1406 flags |= O_DIRECT;
1407
3d60d1ed 1408 if (td_write(td) || td_rw(td)) {
ebac4655
JA
1409 if (td->filetype == FIO_TYPE_FILE) {
1410 if (!td->overwrite)
1411 flags |= O_TRUNC;
1412
1413 flags |= O_CREAT;
1414 }
1415 if (td->sync_io)
1416 flags |= O_SYNC;
1417
1418 flags |= O_RDWR;
1419
1420 td->fd = open(td->file_name, flags, 0600);
3d60d1ed
JA
1421 } else {
1422 if (td->filetype == FIO_TYPE_CHAR)
1423 flags |= O_RDWR;
1424 else
1425 flags |= O_RDONLY;
1426
1427 td->fd = open(td->file_name, flags);
ebac4655
JA
1428 }
1429
1430 if (td->fd == -1) {
1431 td_verror(td, errno);
1432 return 1;
1433 }
1434
1435 if (get_file_size(td))
1436 return 1;
1437
1438 if (td->io_engine != FIO_MMAPIO)
1439 return setup_file_plain(td);
1440 else
1441 return setup_file_mmap(td);
1442}
1443
1444static int check_dev_match(dev_t dev, char *path)
1445{
1446 unsigned int major, minor;
1447 char line[256], *p;
1448 FILE *f;
1449
1450 f = fopen(path, "r");
1451 if (!f) {
1452 perror("open path");
1453 return 1;
1454 }
1455
1456 p = fgets(line, sizeof(line), f);
1457 if (!p) {
1458 fclose(f);
1459 return 1;
1460 }
1461
1462 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1463 fclose(f);
1464 return 1;
1465 }
1466
1467 if (((major << 8) | minor) == dev) {
1468 fclose(f);
1469 return 0;
1470 }
1471
1472 fclose(f);
1473 return 1;
1474}
1475
1476static int find_block_dir(dev_t dev, char *path)
1477{
1478 struct dirent *dir;
1479 struct stat st;
1480 int found = 0;
1481 DIR *D;
1482
1483 D = opendir(path);
1484 if (!D)
1485 return 0;
1486
1487 while ((dir = readdir(D)) != NULL) {
1488 char full_path[256];
1489
1490 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1491 continue;
1492 if (!strcmp(dir->d_name, "device"))
1493 continue;
1494
1495 sprintf(full_path, "%s/%s", path, dir->d_name);
1496
1497 if (!strcmp(dir->d_name, "dev")) {
1498 if (!check_dev_match(dev, full_path)) {
1499 found = 1;
1500 break;
1501 }
1502 }
1503
1504 if (stat(full_path, &st) == -1) {
1505 perror("stat");
1506 break;
1507 }
1508
1509 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1510 continue;
1511
1512 found = find_block_dir(dev, full_path);
1513 if (found) {
1514 strcpy(path, full_path);
1515 break;
1516 }
1517 }
1518
1519 closedir(D);
1520 return found;
1521}
1522
1523static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1524{
1525 unsigned in_flight;
1526 char line[256];
1527 FILE *f;
1528 char *p;
1529
1530 f = fopen(du->path, "r");
1531 if (!f)
1532 return 1;
1533
1534 p = fgets(line, sizeof(line), f);
1535 if (!p) {
1536 fclose(f);
1537 return 1;
1538 }
1539
1540 if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
1541 fclose(f);
1542 return 1;
1543 }
1544
1545 fclose(f);
1546 return 0;
1547}
1548
1549static void update_io_tick_disk(struct disk_util *du)
1550{
1551 struct disk_util_stat __dus, *dus, *ldus;
1552 struct timeval t;
1553
1554 if (get_io_ticks(du, &__dus))
1555 return;
1556
1557 dus = &du->dus;
1558 ldus = &du->last_dus;
1559
1560 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1561 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1562 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1563 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1564 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1565 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1566 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1567 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1568 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1569 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1570
1571 gettimeofday(&t, NULL);
1572 du->msec += mtime_since(&du->time, &t);
1573 memcpy(&du->time, &t, sizeof(t));
1574 memcpy(ldus, &__dus, sizeof(__dus));
1575}
1576
1577static void update_io_ticks(void)
1578{
1579 struct list_head *entry;
1580 struct disk_util *du;
1581
1582 list_for_each(entry, &disk_list) {
1583 du = list_entry(entry, struct disk_util, list);
1584 update_io_tick_disk(du);
1585 }
1586}
1587
1588static int disk_util_exists(dev_t dev)
1589{
1590 struct list_head *entry;
1591 struct disk_util *du;
1592
1593 list_for_each(entry, &disk_list) {
1594 du = list_entry(entry, struct disk_util, list);
1595
1596 if (du->dev == dev)
1597 return 1;
1598 }
1599
1600 return 0;
1601}
1602
1603static void disk_util_add(dev_t dev, char *path)
1604{
1605 struct disk_util *du = malloc(sizeof(*du));
1606
1607 memset(du, 0, sizeof(*du));
1608 INIT_LIST_HEAD(&du->list);
1609 sprintf(du->path, "%s/stat", path);
1610 du->name = strdup(basename(path));
1611 du->dev = dev;
1612
1613 gettimeofday(&du->time, NULL);
1614 get_io_ticks(du, &du->last_dus);
1615
1616 list_add_tail(&du->list, &disk_list);
1617}
1618
1619static void init_disk_util(struct thread_data *td)
1620{
1621 struct stat st;
1622 char foo[256], tmp[256];
1623 dev_t dev;
1624 char *p;
1625
1626 if (!td->do_disk_util)
1627 return;
1628
1629 if (!stat(td->file_name, &st)) {
1630 if (S_ISBLK(st.st_mode))
1631 dev = st.st_rdev;
1632 else
1633 dev = st.st_dev;
1634 } else {
1635 /*
1636 * must be a file, open "." in that path
1637 */
1638 strcpy(foo, td->file_name);
1639 p = dirname(foo);
1640 if (stat(p, &st)) {
1641 perror("disk util stat");
1642 return;
1643 }
1644
1645 dev = st.st_dev;
1646 }
1647
1648 if (disk_util_exists(dev))
1649 return;
1650
1651 sprintf(foo, "/sys/block");
1652 if (!find_block_dir(dev, foo))
1653 return;
1654
1655 /*
64cb3e10 1656 * for md/dm, there's no queue dir. we already have the right place
ebac4655 1657 */
64cb3e10 1658 sprintf(tmp, "%s/stat", foo);
ebac4655 1659 if (stat(tmp, &st)) {
64cb3e10
JA
1660 /*
1661 * if this is inside a partition dir, jump back to parent
1662 */
1663 sprintf(tmp, "%s/queue", foo);
ebac4655 1664 if (stat(tmp, &st)) {
64cb3e10
JA
1665 p = dirname(foo);
1666 sprintf(tmp, "%s/queue", p);
1667 if (stat(tmp, &st)) {
1668 fprintf(stderr, "unknown sysfs layout\n");
1669 return;
1670 }
1671 sprintf(foo, "%s", p);
ebac4655 1672 }
ebac4655
JA
1673 }
1674
1675 disk_util_add(dev, foo);
1676}
1677
1678static void disk_util_timer_arm(void)
1679{
1680 itimer.it_value.tv_sec = 0;
1681 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1682 setitimer(ITIMER_REAL, &itimer, NULL);
1683}
1684
1685static void clear_io_state(struct thread_data *td)
1686{
1687 if (td->io_engine == FIO_SYNCIO)
1688 lseek(td->fd, SEEK_SET, 0);
1689
20dc95c4 1690 td->last_pos = 0;
ebac4655
JA
1691 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1692 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 1693 td->zone_bytes = 0;
ebac4655
JA
1694
1695 if (td->file_map)
1696 memset(td->file_map, 0, td->num_maps * sizeof(long));
1697}
1698
1699static void update_rusage_stat(struct thread_data *td)
1700{
1701 if (!(td->runtime[0] + td->runtime[1]))
1702 return;
1703
1704 getrusage(RUSAGE_SELF, &td->ru_end);
1705
1706 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1707 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1708 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1709
1710
1711 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1712}
1713
1714static void *thread_main(void *data)
1715{
1716 struct thread_data *td = data;
1717 int ret = 1;
1718
1719 if (!td->use_thread)
1720 setsid();
1721
1722 td->pid = getpid();
1723
1724 if (init_io_u(td))
1725 goto err;
1726
1727 if (fio_setaffinity(td) == -1) {
1728 td_verror(td, errno);
1729 goto err;
1730 }
1731
1732 if (init_io(td))
1733 goto err;
1734
1735 if (td->ioprio) {
1736 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1737 td_verror(td, errno);
1738 goto err;
1739 }
1740 }
1741
1742 sem_post(&startup_sem);
1743 sem_wait(&td->mutex);
1744
1745 if (!td->create_serialize && setup_file(td))
1746 goto err;
1747
1748 if (init_random_state(td))
1749 goto err;
1750
1751 gettimeofday(&td->epoch, NULL);
1752
1753 while (td->loops--) {
1754 getrusage(RUSAGE_SELF, &td->ru_start);
1755 gettimeofday(&td->start, NULL);
1756 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1757
1758 if (td->ratemin)
1759 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1760
1761 clear_io_state(td);
1762 prune_io_piece_log(td);
1763
1764 do_io(td);
1765
1766 td->runtime[td->ddir] += mtime_since_now(&td->start);
3d60d1ed
JA
1767 if (td_rw(td))
1768 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1769
ebac4655
JA
1770 update_rusage_stat(td);
1771
1772 if (td->error || td->terminate)
1773 break;
1774
1775 if (td->verify == VERIFY_NONE)
1776 continue;
1777
1778 clear_io_state(td);
1779 gettimeofday(&td->start, NULL);
1780
1781 do_verify(td);
1782
1783 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1784
1785 if (td->error || td->terminate)
1786 break;
1787 }
1788
1789 ret = 0;
1790
1791 if (td->bw_log)
1792 finish_log(td, td->bw_log, "bw");
1793 if (td->slat_log)
1794 finish_log(td, td->slat_log, "slat");
1795 if (td->clat_log)
1796 finish_log(td, td->clat_log, "clat");
1797
1798 if (exitall_on_terminate)
1799 terminate_threads(td->groupid);
1800
1801err:
1802 if (td->fd != -1) {
1803 close(td->fd);
1804 td->fd = -1;
1805 }
1806 if (td->mmap)
1807 munmap(td->mmap, td->file_size);
1808 cleanup_io(td);
1809 cleanup_io_u(td);
1810 if (ret) {
1811 sem_post(&startup_sem);
1812 sem_wait(&td->mutex);
1813 }
1814 td_set_runstate(td, TD_EXITED);
1815 return NULL;
1816
1817}
1818
1819static void *fork_main(int shmid, int offset)
1820{
1821 struct thread_data *td;
1822 void *data;
1823
1824 data = shmat(shmid, NULL, 0);
1825 if (data == (void *) -1) {
1826 perror("shmat");
1827 return NULL;
1828 }
1829
1830 td = data + offset * sizeof(struct thread_data);
1831 thread_main(td);
1832 shmdt(data);
1833 return NULL;
1834}
1835
1836static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1837 double *mean, double *dev)
1838{
1839 double n;
1840
1841 if (is->samples == 0)
1842 return 0;
1843
1844 *min = is->min_val;
1845 *max = is->max_val;
1846
1847 n = (double) is->samples;
1848 *mean = (double) is->val / n;
1849 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1850 if (!(*min + *max) && !(*mean + *dev))
1851 return 0;
1852
1853 return 1;
1854}
1855
1856static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
1857 int ddir)
1858{
1859 char *ddir_str[] = { "read ", "write" };
1860 unsigned long min, max, bw;
1861 double mean, dev;
1862
1863 if (!td->runtime[ddir])
1864 return;
1865
1866 bw = td->io_bytes[ddir] / td->runtime[ddir];
9104f874 1867 printf(" %s: io=%6lluMiB, bw=%6luKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
ebac4655
JA
1868
1869 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
1870 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1871
1872 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
1873 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1874
1875 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
1876 double p_of_agg;
1877
1878 p_of_agg = mean * 100 / (double) rs->agg[ddir];
1879 printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev);
1880 }
1881}
1882
1883static void show_thread_status(struct thread_data *td,
1884 struct group_run_stats *rs)
1885{
1886 double usr_cpu, sys_cpu;
1887
1888 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
1889 return;
1890
1891 printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error);
1892
1893 show_ddir_status(td, rs, td->ddir);
1894 show_ddir_status(td, rs, td->ddir ^ 1);
1895
1896 if (td->runtime[0] + td->runtime[1]) {
1897 double runt = td->runtime[0] + td->runtime[1];
1898
1899 usr_cpu = (double) td->usr_time * 100 / runt;
1900 sys_cpu = (double) td->sys_time * 100 / runt;
1901 } else {
1902 usr_cpu = 0;
1903 sys_cpu = 0;
1904 }
1905
1906 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1907}
1908
1909static void check_str_update(struct thread_data *td)
1910{
1911 char c = run_str[td->thread_number - 1];
1912
1913 if (td->runstate == td->old_runstate)
1914 return;
1915
1916 switch (td->runstate) {
1917 case TD_REAPED:
1918 c = '_';
1919 break;
1920 case TD_EXITED:
1921 c = 'E';
1922 break;
1923 case TD_RUNNING:
3d60d1ed
JA
1924 if (td_rw(td)) {
1925 if (td->sequential)
1926 c = 'M';
1927 else
1928 c = 'm';
1929 } else if (td_read(td)) {
ebac4655
JA
1930 if (td->sequential)
1931 c = 'R';
1932 else
1933 c = 'r';
1934 } else {
1935 if (td->sequential)
1936 c = 'W';
1937 else
1938 c = 'w';
1939 }
1940 break;
1941 case TD_VERIFYING:
1942 c = 'V';
1943 break;
1944 case TD_CREATED:
1945 c = 'C';
1946 break;
1947 case TD_NOT_CREATED:
1948 c = 'P';
1949 break;
1950 default:
1951 printf("state %d\n", td->runstate);
1952 }
1953
1954 run_str[td->thread_number - 1] = c;
1955 td->old_runstate = td->runstate;
1956}
1957
5289b847
JA
1958static void eta_to_str(char *str, int eta_sec)
1959{
1960 unsigned int d, h, m, s;
1961 static int always_d, always_h;
1962
1963 d = h = m = s = 0;
1964
1965 s = eta_sec % 60;
1966 eta_sec /= 60;
1967 m = eta_sec % 60;
1968 eta_sec /= 60;
1969 h = eta_sec % 24;
1970 eta_sec /= 24;
1971 d = eta_sec;
1972
1973 if (d || always_d) {
1974 always_d = 1;
1975 str += sprintf(str, "%02dd:", d);
1976 }
1977 if (h || always_h) {
1978 always_h = 1;
1979 str += sprintf(str, "%02dh:", h);
1980 }
1981
1982 str += sprintf(str, "%02dm:", m);
1983 str += sprintf(str, "%02ds", s);
1984}
1985
6a0106a0
JA
1986static int thread_eta(struct thread_data *td, unsigned long elapsed)
1987{
1988 unsigned long long bytes_total, bytes_done;
1989 unsigned int eta_sec = 0;
1990
1991 bytes_total = td->total_io_size;
3d60d1ed
JA
1992
1993 /*
1994 * if writing, bytes_total will be twice the size. If mixing,
1995 * assume a 50/50 split and thus bytes_total will be 50% larger.
1996 */
1997 if (td->verify) {
1998 if (td_rw(td))
1999 bytes_total = bytes_total * 3 / 2;
2000 else
2001 bytes_total <<= 1;
2002 }
6a0106a0
JA
2003 if (td->zone_size && td->zone_skip)
2004 bytes_total /= (td->zone_skip / td->zone_size);
2005
2006 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
2007 double perc;
8b611c34 2008
6a0106a0
JA
2009 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
2010 perc = (double) bytes_done / (double) bytes_total;
2011 if (perc > 1.0)
2012 perc = 1.0;
2013
2014 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
2015
8b611c34 2016 if (td->timeout && eta_sec > (td->timeout - elapsed))
6a0106a0
JA
2017 eta_sec = td->timeout - elapsed;
2018 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED) {
2019 int t_eta = 0, r_eta = 0;
2020
2021 /*
2022 * We can only guess - assume it'll run the full timeout
2023 * if given, otherwise assume it'll run at the specified rate.
2024 */
2025 if (td->timeout)
2026 t_eta = td->timeout + td->start_delay - elapsed;
2027 if (td->rate) {
2028 r_eta = (bytes_total / 1024) / td->rate;
2029 r_eta += td->start_delay - elapsed;
2030 }
2031
2032 if (r_eta && t_eta)
2033 eta_sec = min(r_eta, t_eta);
2034 else if (r_eta)
2035 eta_sec = r_eta;
2036 else if (t_eta)
2037 eta_sec = t_eta;
2038 else
2039 eta_sec = INT_MAX;
2040 } else {
2041 /*
2042 * thread is already done
2043 */
2044 eta_sec = 0;
2045 }
2046
2047 return eta_sec;
2048}
2049
ebac4655
JA
2050static void print_thread_status(void)
2051{
6a0106a0
JA
2052 unsigned long elapsed = time_since_now(&genesis);
2053 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
5289b847 2054 char eta_str[32];
6a0106a0
JA
2055 double perc = 0.0;
2056
2057 eta_secs = malloc(thread_number * sizeof(int));
2058 memset(eta_secs, 0, thread_number * sizeof(int));
ebac4655 2059
ebac4655
JA
2060 nr_running = t_rate = m_rate = 0;
2061 for (i = 0; i < thread_number; i++) {
2062 struct thread_data *td = &threads[i];
2063
2064 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
2065 nr_running++;
2066 t_rate += td->rate;
2067 m_rate += td->ratemin;
2068 }
2069
6a0106a0
JA
2070 if (elapsed >= 3)
2071 eta_secs[i] = thread_eta(td, elapsed);
8b611c34
JA
2072 else
2073 eta_secs[i] = INT_MAX;
ebac4655
JA
2074
2075 check_str_update(td);
2076 }
2077
6a0106a0
JA
2078 if (exitall_on_terminate)
2079 eta_sec = INT_MAX;
2080 else
2081 eta_sec = 0;
5289b847 2082
6a0106a0
JA
2083 for (i = 0; i < thread_number; i++) {
2084 if (exitall_on_terminate) {
2085 if (eta_secs[i] < eta_sec)
2086 eta_sec = eta_secs[i];
2087 } else {
2088 if (eta_secs[i] > eta_sec)
2089 eta_sec = eta_secs[i];
5289b847 2090 }
6a0106a0 2091 }
5289b847 2092
8b611c34 2093 if (eta_sec != INT_MAX && elapsed) {
6a0106a0
JA
2094 perc = (double) elapsed / (double) (elapsed + eta_sec);
2095 eta_to_str(eta_str, eta_sec);
ebac4655
JA
2096 }
2097
5289b847 2098 printf("Threads now running (%d)", nr_running);
ebac4655
JA
2099 if (m_rate || t_rate)
2100 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8b611c34 2101 if (eta_sec != INT_MAX) {
6a0106a0
JA
2102 perc *= 100.0;
2103 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
2104 }
5289b847 2105 printf("\r");
ebac4655 2106 fflush(stdout);
6a0106a0 2107 free(eta_secs);
ebac4655
JA
2108}
2109
2110static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2111{
2112 int i;
2113
2114 /*
2115 * reap exited threads (TD_EXITED -> TD_REAPED)
2116 */
2117 for (i = 0; i < thread_number; i++) {
2118 struct thread_data *td = &threads[i];
2119
2120 if (td->runstate != TD_EXITED)
2121 continue;
2122
2123 td_set_runstate(td, TD_REAPED);
2124
2125 if (td->use_thread) {
2126 long ret;
2127
2128 if (pthread_join(td->thread, (void *) &ret))
2129 perror("thread_join");
2130 } else
2131 waitpid(td->pid, NULL, 0);
2132
2133 (*nr_running)--;
2134 (*m_rate) -= td->ratemin;
2135 (*t_rate) -= td->rate;
2136 }
2137}
2138
2139static void run_threads(void)
2140{
ebac4655
JA
2141 struct thread_data *td;
2142 unsigned long spent;
2143 int i, todo, nr_running, m_rate, t_rate, nr_started;
2144
2145 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
2146 fflush(stdout);
2147
2148 signal(SIGINT, sig_handler);
2149 signal(SIGALRM, sig_handler);
2150
2151 todo = thread_number;
2152 nr_running = 0;
2153 nr_started = 0;
2154 m_rate = t_rate = 0;
2155
2156 for (i = 0; i < thread_number; i++) {
2157 td = &threads[i];
2158
2159 run_str[td->thread_number - 1] = 'P';
2160
2161 init_disk_util(td);
2162
2163 if (!td->create_serialize)
2164 continue;
2165
2166 /*
2167 * do file setup here so it happens sequentially,
2168 * we don't want X number of threads getting their
2169 * client data interspersed on disk
2170 */
2171 if (setup_file(td)) {
2172 td_set_runstate(td, TD_REAPED);
2173 todo--;
2174 }
2175 }
2176
2177 gettimeofday(&genesis, NULL);
2178
2179 while (todo) {
2180 /*
2181 * create threads (TD_NOT_CREATED -> TD_CREATED)
2182 */
2183 for (i = 0; i < thread_number; i++) {
2184 td = &threads[i];
2185
2186 if (td->runstate != TD_NOT_CREATED)
2187 continue;
2188
2189 /*
2190 * never got a chance to start, killed by other
2191 * thread for some reason
2192 */
2193 if (td->terminate) {
2194 todo--;
2195 continue;
2196 }
2197
2198 if (td->start_delay) {
2199 spent = mtime_since_now(&genesis);
2200
2201 if (td->start_delay * 1000 > spent)
2202 continue;
2203 }
2204
2205 if (td->stonewall && (nr_started || nr_running))
2206 break;
2207
2208 td_set_runstate(td, TD_CREATED);
2209 sem_init(&startup_sem, 0, 1);
2210 todo--;
2211 nr_started++;
2212
2213 if (td->use_thread) {
2214 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2215 perror("thread_create");
2216 nr_started--;
2217 }
2218 } else {
2219 if (fork())
2220 sem_wait(&startup_sem);
2221 else {
2222 fork_main(shm_id, i);
2223 exit(0);
2224 }
2225 }
2226 }
2227
2228 /*
2229 * start created threads (TD_CREATED -> TD_RUNNING)
2230 */
2231 for (i = 0; i < thread_number; i++) {
2232 td = &threads[i];
2233
2234 if (td->runstate != TD_CREATED)
2235 continue;
2236
2237 td_set_runstate(td, TD_RUNNING);
2238 nr_running++;
2239 nr_started--;
2240 m_rate += td->ratemin;
2241 t_rate += td->rate;
2242 sem_post(&td->mutex);
2243 }
2244
2245 reap_threads(&nr_running, &t_rate, &m_rate);
2246
2247 if (todo)
2248 usleep(100000);
2249 }
2250
2251 while (nr_running) {
2252 reap_threads(&nr_running, &t_rate, &m_rate);
2253 usleep(10000);
2254 }
2255
2256 update_io_ticks();
2257}
2258
2259static void show_group_stats(struct group_run_stats *rs, int id)
2260{
2261 printf("\nRun status group %d (all jobs):\n", id);
2262
2263 if (rs->max_run[DDIR_READ])
9104f874 2264 printf(" READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]);
ebac4655 2265 if (rs->max_run[DDIR_WRITE])
9104f874 2266 printf(" WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
ebac4655
JA
2267}
2268
2269static void show_disk_util(void)
2270{
2271 struct disk_util_stat *dus;
2272 struct list_head *entry;
2273 struct disk_util *du;
2274 double util;
2275
2276 printf("\nDisk stats (read/write):\n");
2277
2278 list_for_each(entry, &disk_list) {
2279 du = list_entry(entry, struct disk_util, list);
2280 dus = &du->dus;
2281
2282 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2283 if (util > 100.0)
2284 util = 100.0;
2285
2286 printf(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
2287 }
2288}
2289
2290static void show_run_stats(void)
2291{
2292 struct group_run_stats *runstats, *rs;
2293 struct thread_data *td;
2294 int i;
2295
2296 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2297
2298 for (i = 0; i < groupid + 1; i++) {
2299 rs = &runstats[i];
2300
2301 memset(rs, 0, sizeof(*rs));
2302 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2303 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2304 }
2305
2306 for (i = 0; i < thread_number; i++) {
2307 unsigned long rbw, wbw;
2308
2309 td = &threads[i];
2310
2311 if (td->error) {
2312 printf("Client%d: %s\n", td->thread_number, td->verror);
2313 continue;
2314 }
2315
2316 rs = &runstats[td->groupid];
2317
2318 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
2319 rs->min_run[0] = td->runtime[0];
2320 if (td->runtime[0] > rs->max_run[0])
2321 rs->max_run[0] = td->runtime[0];
2322 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
2323 rs->min_run[1] = td->runtime[1];
2324 if (td->runtime[1] > rs->max_run[1])
2325 rs->max_run[1] = td->runtime[1];
2326
2327 rbw = wbw = 0;
2328 if (td->runtime[0])
2329 rbw = td->io_bytes[0] / td->runtime[0];
2330 if (td->runtime[1])
2331 wbw = td->io_bytes[1] / td->runtime[1];
2332
2333 if (rbw < rs->min_bw[0])
2334 rs->min_bw[0] = rbw;
2335 if (wbw < rs->min_bw[1])
2336 rs->min_bw[1] = wbw;
2337 if (rbw > rs->max_bw[0])
2338 rs->max_bw[0] = rbw;
2339 if (wbw > rs->max_bw[1])
2340 rs->max_bw[1] = wbw;
2341
2342 rs->io_mb[0] += td->io_bytes[0] >> 20;
2343 rs->io_mb[1] += td->io_bytes[1] >> 20;
2344 }
2345
2346 for (i = 0; i < groupid + 1; i++) {
2347 rs = &runstats[i];
2348
2349 if (rs->max_run[0])
2350 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2351 if (rs->max_run[1])
2352 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2353 }
2354
2355 /*
2356 * don't overwrite last signal output
2357 */
2358 printf("\n");
2359
2360 for (i = 0; i < thread_number; i++) {
2361 td = &threads[i];
2362 rs = &runstats[td->groupid];
2363
2364 show_thread_status(td, rs);
2365 }
2366
2367 for (i = 0; i < groupid + 1; i++)
2368 show_group_stats(&runstats[i], i);
2369
2370 show_disk_util();
2371}
2372
2373int main(int argc, char *argv[])
2374{
2375 if (parse_options(argc, argv))
2376 return 1;
2377
2378 if (!thread_number) {
2379 printf("Nothing to do\n");
2380 return 1;
2381 }
2382
2383 disk_util_timer_arm();
2384
2385 run_threads();
2386 show_run_stats();
2387
2388 return 0;
2389}