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