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