[PATCH] fio: time cleanups
[disktools.git] / fio.c
CommitLineData
abe4da87
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
892199bd
JA
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>
7dd1389e 29#include <ctype.h>
18e0b78c 30#include <sched.h>
43000118 31#include <libaio.h>
892199bd
JA
32#include <sys/time.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
36#include <semaphore.h>
37#include <sys/ipc.h>
38#include <sys/shm.h>
39#include <asm/unistd.h>
40
4240cfa1
JA
41#define MAX_JOBS (1024)
42
892199bd
JA
43/*
44 * assume we don't have _get either, if _set isn't defined
45 */
46#ifndef __NR_ioprio_set
47
48#if defined(__i386__)
49#define __NR_ioprio_set 289
50#define __NR_ioprio_get 290
51#elif defined(__powerpc__) || defined(__powerpc64__)
52#define __NR_ioprio_set 273
53#define __NR_ioprio_get 274
54#elif defined(__x86_64__)
55#define __NR_ioprio_set 251
56#define __NR_ioprio_get 252
57#elif defined(__ia64__)
58#define __NR_ioprio_set 1274
59#define __NR_ioprio_get 1275
60#elif defined(__alpha__)
61#define __NR_ioprio_set 442
62#define __NR_ioprio_get 443
63#elif defined(__s390x__) || defined(__s390__)
64#define __NR_ioprio_set 282
65#define __NR_ioprio_get 283
66#else
67#error "Unsupported arch"
68#endif
69
70#endif
71
72static int ioprio_set(int which, int who, int ioprio)
73{
74 return syscall(__NR_ioprio_set, which, who, ioprio);
75}
76
77enum {
78 IOPRIO_WHO_PROCESS = 1,
79 IOPRIO_WHO_PGRP,
80 IOPRIO_WHO_USER,
81};
82
83#define IOPRIO_CLASS_SHIFT 13
84
892199bd
JA
85#define MASK (4095)
86
4240cfa1
JA
87#define DEF_BS (4096)
88#define DEF_TIMEOUT (30)
89#define DEF_RATE_CYCLE (1000)
90#define DEF_ODIRECT (1)
91#define DEF_SEQUENTIAL (1)
92#define DEF_WRITESTAT (0)
93#define DEF_RAND_REPEAT (1)
94
95#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
892199bd 96
4240cfa1
JA
97static int write_stat = DEF_WRITESTAT;
98static int repeatable = DEF_RAND_REPEAT;
02bdd9ba 99static int rate_quit = 1;
892199bd 100
892199bd 101static int thread_number;
7dd1389e 102static char *ini_file;
892199bd
JA
103
104static int shm_id;
105
4240cfa1
JA
106enum {
107 DDIR_READ = 0,
108 DDIR_WRITE,
109};
892199bd 110
02bdd9ba
JA
111/*
112 * thread life cycle
113 */
114enum {
115 TD_NOT_CREATED = 0,
116 TD_CREATED,
117 TD_STARTED,
118 TD_EXITED,
119 TD_REAPED,
120};
121
892199bd
JA
122struct thread_data {
123 char file_name[256];
124 int thread_number;
125 int error;
126 int fd;
127 int stat_fd;
128 pid_t pid;
7292613b 129 char *buf;
4240cfa1 130 volatile int terminate;
02bdd9ba 131 volatile int runstate;
f737299d
JA
132 unsigned int ddir;
133 unsigned int ioprio;
134 unsigned int sequential;
135 unsigned int bs;
136 unsigned int odirect;
137 unsigned int delay_sleep;
4240cfa1 138 unsigned int fsync_blocks;
fc24389f 139 unsigned int start_delay;
47d45203 140 unsigned int timeout;
43000118 141 unsigned int use_aio;
18e0b78c 142 cpu_set_t cpumask;
86184d14 143
43000118
JA
144 io_context_t *aio_ctx;
145 struct iocb *aio_iocbs;
146 unsigned int aio_depth;
147 unsigned int aio_cur_depth;
148 struct io_event *aio_events;
149 char *aio_iocbs_status;
150
7dd1389e 151 unsigned int rate;
4240cfa1
JA
152 unsigned int ratemin;
153 unsigned int ratecycle;
154 unsigned long rate_usec_cycle;
155 long rate_pending_usleep;
156 unsigned long rate_blocks;
157 struct timeval lastrate;
86184d14 158
892199bd
JA
159 unsigned long max_latency; /* msec */
160 unsigned long min_latency; /* msec */
161 unsigned long runtime; /* sec */
162 unsigned long blocks;
4240cfa1 163 unsigned long io_blocks;
892199bd
JA
164 unsigned long last_block;
165 sem_t mutex;
892199bd
JA
166 struct drand48_data random_state;
167
168 /*
169 * bandwidth stat
170 */
171 unsigned long stat_time;
172 unsigned long stat_time_last;
173 unsigned long stat_blocks_last;
4240cfa1
JA
174
175 struct timeval start;
892199bd
JA
176};
177
178static struct thread_data *threads;
47d45203 179static struct thread_data def_thread;
892199bd
JA
180
181static sem_t startup_sem;
182
5c24b2c4 183static void sig_handler(int sig)
892199bd
JA
184{
185 int i;
186
213b446c
JA
187 for (i = 0; i < thread_number; i++) {
188 struct thread_data *td = &threads[i];
189
190 td->terminate = 1;
191 td->start_delay = 0;
192 }
02bdd9ba
JA
193}
194
5c24b2c4 195static int init_random_state(struct thread_data *td)
892199bd
JA
196{
197 unsigned long seed = 123;
198
199 if (td->sequential)
200 return 0;
201
202 if (!repeatable) {
203 int fd = open("/dev/random", O_RDONLY);
204
205 if (fd == -1) {
206 td->error = errno;
207 return 1;
208 }
209
7dd1389e 210 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
892199bd
JA
211 td->error = EIO;
212 close(fd);
213 return 1;
214 }
215
216 close(fd);
217 }
218
219 srand48_r(seed, &td->random_state);
220 return 0;
221}
222
5c24b2c4 223static void shutdown_stat_file(struct thread_data *td)
892199bd
JA
224{
225 if (td->stat_fd != -1) {
226 fsync(td->stat_fd);
227 close(td->stat_fd);
228 }
229}
230
5c24b2c4 231static int init_stat_file(struct thread_data *td)
892199bd 232{
4240cfa1 233 char n[256];
892199bd
JA
234
235 if (!write_stat)
236 return 0;
237
fe6dbcb6 238 sprintf(n, "fio_thread%d.stat", td->thread_number);
892199bd
JA
239 td->stat_fd = open(n, O_WRONLY | O_CREAT | O_TRUNC, 0644);
240 if (td->stat_fd == -1) {
599002b3 241 perror("open stat file");
892199bd
JA
242 td->error = errno;
243 return 1;
244 }
245
892199bd
JA
246 return 0;
247}
248
5c24b2c4 249static unsigned long utime_since(struct timeval *s, struct timeval *e)
892199bd
JA
250{
251 double sec, usec;
252
253 sec = e->tv_sec - s->tv_sec;
254 usec = e->tv_usec - s->tv_usec;
255 if (sec > 0 && usec < 0) {
256 sec--;
257 usec += 1000000;
258 }
259
260 sec *= (double) 1000000;
261
262 return sec + usec;
263}
264
5c24b2c4 265static unsigned long mtime_since(struct timeval *s, struct timeval *e)
892199bd
JA
266{
267 double sec, usec;
268
269 sec = e->tv_sec - s->tv_sec;
270 usec = e->tv_usec - s->tv_usec;
271 if (sec > 0 && usec < 0) {
272 sec--;
273 usec += 1000000;
274 }
275
276 sec *= (double) 1000;
277 usec /= (double) 1000;
278
279 return sec + usec;
280}
281
be33abe4
JA
282static unsigned long mtime_since_now(struct timeval *s)
283{
284 struct timeval t;
285
286 gettimeofday(&t, NULL);
287 return mtime_since(s, &t);
288}
289
98168d55
JA
290static inline unsigned long msec_now(struct timeval *s)
291{
292 return s->tv_sec * 1000 + s->tv_usec / 1000;
293}
294
5c24b2c4 295static unsigned long get_next_offset(struct thread_data *td)
892199bd
JA
296{
297 unsigned long b;
298 long r;
299
300 if (!td->sequential) {
301 lrand48_r(&td->random_state, &r);
302 b = (1+(double) (td->blocks-1) * r / (RAND_MAX+1.0));
303 } else {
304 b = td->last_block;
305 td->last_block++;
306 }
307
308 return b * td->bs;
309}
310
5c24b2c4 311static void add_stat_sample(struct thread_data *td, unsigned long msec)
892199bd
JA
312{
313 char sample[256];
314
315 if (!td->stat_fd)
316 return;
317
892199bd
JA
318 td->stat_time += msec;
319 td->stat_time_last += msec;
320 td->stat_blocks_last++;
321
322 if (td->stat_time_last >= 500) {
323 unsigned long rate = td->stat_blocks_last * td->bs / (td->stat_time_last);
324
325 td->stat_time_last = 0;
326 td->stat_blocks_last = 0;
327 sprintf(sample, "%lu, %lu\n", td->stat_time, rate);
892199bd
JA
328 write(td->stat_fd, sample, strlen(sample));
329 }
892199bd
JA
330}
331
5c24b2c4 332static void usec_sleep(int usec)
892199bd 333{
86184d14
JA
334 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
335 struct timespec rem;
892199bd
JA
336
337 do {
86184d14
JA
338 rem.tv_sec = rem.tv_nsec = 0;
339 nanosleep(&req, &rem);
340 if (!rem.tv_nsec)
892199bd 341 break;
86184d14
JA
342
343 req.tv_nsec = rem.tv_nsec;
892199bd
JA
344 } while (1);
345}
346
5c24b2c4 347static void rate_throttle(struct thread_data *td, unsigned long time_spent)
86184d14 348{
4240cfa1
JA
349 if (!td->rate)
350 return;
351
86184d14
JA
352 if (time_spent < td->rate_usec_cycle) {
353 unsigned long s = td->rate_usec_cycle - time_spent;
354
355 td->rate_pending_usleep += s;
fad86e6a 356 if (td->rate_pending_usleep >= 100000) {
86184d14
JA
357 usec_sleep(td->rate_pending_usleep);
358 td->rate_pending_usleep = 0;
359 }
4240cfa1 360 } else {
42b2b9fe
JA
361 long overtime = time_spent - td->rate_usec_cycle;
362
4240cfa1
JA
363 td->rate_pending_usleep -= overtime;
364 }
365}
366
5c24b2c4 367static int check_min_rate(struct thread_data *td, struct timeval *now)
4240cfa1 368{
7607bc6b 369 unsigned long spent;
4240cfa1
JA
370 unsigned long rate;
371
372 /*
373 * allow a 2 second settle period in the beginning
374 */
7607bc6b 375 if (mtime_since(&td->start, now) < 2000)
4240cfa1
JA
376 return 0;
377
378 /*
379 * if rate blocks is set, sample is running
380 */
381 if (td->rate_blocks) {
382 spent = mtime_since(&td->lastrate, now);
383 if (spent < td->ratecycle)
384 return 0;
385
386 rate = ((td->io_blocks - td->rate_blocks) * td->bs) / spent;
387 if (rate < td->ratemin) {
388 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
02bdd9ba 389 if (rate_quit)
e6402082 390 sig_handler(0);
4240cfa1
JA
391 return 1;
392 }
86184d14 393 }
4240cfa1
JA
394
395 td->rate_blocks = td->io_blocks;
396 memcpy(&td->lastrate, now, sizeof(*now));
397 return 0;
86184d14
JA
398}
399
67903a2e
JA
400static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
401{
402 if (mtime_since(&td->start, t) >= td->timeout * 1000)
403 return 1;
404
405 return 0;
406}
407
4240cfa1
JA
408#define should_fsync(td) ((td)->ddir == DDIR_WRITE && !(td)->odirect)
409
43000118 410static void do_sync_io(struct thread_data *td)
892199bd 411{
4240cfa1 412 struct timeval s, e;
86184d14 413 unsigned long blocks, msec, usec;
892199bd 414
892199bd
JA
415 for (blocks = 0; blocks < td->blocks; blocks++) {
416 off_t offset = get_next_offset(td);
417 int ret;
418
419 if (td->terminate)
420 break;
421
422 if (lseek(td->fd, offset, SEEK_SET) == -1) {
423 td->error = errno;
424 break;
425 }
426
427 if (td->delay_sleep)
86184d14 428 usec_sleep(td->delay_sleep);
892199bd
JA
429
430 gettimeofday(&s, NULL);
431
432 if (td->ddir == DDIR_READ)
7292613b 433 ret = read(td->fd, td->buf, td->bs);
892199bd 434 else
7292613b 435 ret = write(td->fd, td->buf, td->bs);
892199bd 436
f737299d 437 if (ret < (int) td->bs) {
892199bd
JA
438 if (ret == -1)
439 td->error = errno;
440 break;
441 }
442
4240cfa1
JA
443 td->io_blocks++;
444
445 if (should_fsync(td) && td->fsync_blocks &&
446 (td->io_blocks % td->fsync_blocks) == 0)
447 fsync(td->fd);
448
86184d14
JA
449 gettimeofday(&e, NULL);
450
451 usec = utime_since(&s, &e);
86184d14 452
4240cfa1 453 rate_throttle(td, usec);
892199bd 454
4240cfa1
JA
455 if (check_min_rate(td, &e)) {
456 td->error = ENODATA;
457 break;
458 }
892199bd 459
4240cfa1
JA
460 msec = usec / 1000;
461 add_stat_sample(td, msec);
892199bd
JA
462
463 if (msec < td->min_latency)
464 td->min_latency = msec;
465 if (msec > td->max_latency)
466 td->max_latency = msec;
67903a2e
JA
467
468 if (runtime_exceeded(td, &e))
469 break;
892199bd
JA
470 }
471
4240cfa1 472 if (should_fsync(td))
892199bd 473 fsync(td->fd);
892199bd 474}
43000118
JA
475
476static void aio_put_iocb(struct thread_data *td, struct iocb *iocb)
477{
478 long offset = ((long) iocb - (long) td->aio_iocbs)/ sizeof(struct iocb);
479
480 td->aio_iocbs_status[offset] = 0;
56b0eff0 481 td->aio_cur_depth--;
43000118
JA
482}
483
7292613b 484static struct iocb *aio_get_iocb(struct thread_data *td, struct timeval *t)
43000118
JA
485{
486 struct iocb *iocb = NULL;
56b0eff0 487 unsigned int i;
43000118
JA
488
489 for (i = 0; i < td->aio_depth; i++) {
490 if (td->aio_iocbs_status[i] == 0) {
491 td->aio_iocbs_status[i] = 1;
492 iocb = &td->aio_iocbs[i];
493 break;
494 }
495 }
496
497 if (iocb) {
498 off_t off = get_next_offset(td);
7292613b 499 char *p = td->buf + i * td->bs;
43000118
JA
500
501 if (td->ddir == DDIR_READ)
502 io_prep_pread(iocb, td->fd, p, td->bs, off);
503 else
504 io_prep_pwrite(iocb, td->fd, p, td->bs, off);
98168d55
JA
505
506 io_set_callback(iocb, (io_callback_t) msec_now(t));
43000118
JA
507 }
508
509 return iocb;
510}
511
56b0eff0
JA
512static int aio_submit(struct thread_data *td, struct iocb *iocb)
513{
514 int ret;
515
516 do {
517 ret = io_submit(*td->aio_ctx, 1, &iocb);
518 if (ret == 1)
519 return 0;
520
521 if (errno == EINTR)
522 continue;
523 else if (errno == EAGAIN)
524 usleep(100);
525 else
526 break;
527 } while (1);
528
529 return 1;
530}
531
98168d55
JA
532#define iocb_time(iocb) ((unsigned long) (iocb)->data)
533
43000118
JA
534static void do_async_io(struct thread_data *td)
535{
536 struct timeval s, e;
43000118 537 unsigned long blocks, msec, usec;
43000118 538
43000118
JA
539 for (blocks = 0; blocks < td->blocks; blocks++) {
540 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
541 struct timespec *timeout;
43000118 542 int ret, i, min_evts = 0;
8baf1bcc 543 struct iocb *iocb;
43000118
JA
544
545 if (td->terminate)
546 break;
547
548 if (td->delay_sleep)
549 usec_sleep(td->delay_sleep);
550
551 gettimeofday(&s, NULL);
552
7292613b 553 iocb = aio_get_iocb(td, &s);
8baf1bcc 554
56b0eff0
JA
555 ret = aio_submit(td, iocb);
556 if (ret) {
43000118
JA
557 td->error = errno;
558 break;
559 }
560
561 td->aio_cur_depth++;
43000118
JA
562
563 if (td->aio_cur_depth < td->aio_depth) {
564 timeout = &ts;
565 min_evts = 0;
566 } else {
567 timeout = NULL;
568 min_evts = 1;
569 }
570
571 ret = io_getevents(*td->aio_ctx, min_evts, td->aio_cur_depth, td->aio_events, timeout);
572 if (ret < 0) {
573 td->error = errno;
574 break;
575 } else if (!ret)
576 continue;
577
98168d55
JA
578 gettimeofday(&e, NULL);
579
43000118
JA
580 for (i = 0; i < ret; i++) {
581 struct io_event *ev = td->aio_events + i;
582
583 td->io_blocks++;
43000118
JA
584
585 iocb = ev->obj;
98168d55
JA
586
587 msec = msec_now(&e) - iocb_time(iocb);
588 add_stat_sample(td, msec);
589
590 if (msec < td->min_latency)
591 td->min_latency = msec;
592 if (msec > td->max_latency)
593 td->max_latency = msec;
594
43000118
JA
595 aio_put_iocb(td, iocb);
596 }
597
98168d55
JA
598 /*
599 * the rate is batched for now, it should work for batches
600 * of completions except the very first one which may look
601 * a little bursty
602 */
43000118
JA
603 usec = utime_since(&s, &e);
604
605 rate_throttle(td, usec);
606
607 if (check_min_rate(td, &e)) {
608 td->error = ENODATA;
609 break;
610 }
67903a2e
JA
611
612 if (runtime_exceeded(td, &e))
613 break;
43000118 614 }
43000118
JA
615}
616
56b0eff0 617static void cleanup_pending_aio(struct thread_data *td)
43000118 618{
56b0eff0
JA
619 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
620 unsigned int i;
621 int r;
622
43000118 623 /*
56b0eff0 624 * get immediately available events, if any
43000118 625 */
56b0eff0
JA
626 r = io_getevents(*td->aio_ctx, 0, td->aio_cur_depth, td->aio_events, &ts);
627 if (r > 0) {
628 for (i = 0; i < r; i++)
629 aio_put_iocb(td, &td->aio_iocbs[i]);
630 }
631
632 /*
633 * now cancel remaining active events
634 */
635 for (i = 0; i < td->aio_depth; i++) {
636 if (td->aio_iocbs_status[i] == 0)
637 continue;
638
639 r = io_cancel(*td->aio_ctx, &td->aio_iocbs[i], td->aio_events);
640 if (!r)
641 aio_put_iocb(td, &td->aio_iocbs[i]);
642 }
643
43000118
JA
644 if (td->aio_cur_depth)
645 io_getevents(*td->aio_ctx, td->aio_cur_depth, td->aio_cur_depth, td->aio_events, NULL);
56b0eff0
JA
646}
647
648static void cleanup_aio(struct thread_data *td)
649{
650 if (td->aio_cur_depth)
651 cleanup_pending_aio(td);
43000118
JA
652
653 if (td->aio_ctx) {
654 io_destroy(*td->aio_ctx);
655 free(td->aio_ctx);
656 }
657 if (td->aio_iocbs)
658 free(td->aio_iocbs);
659 if (td->aio_events)
660 free(td->aio_events);
661 if (td->aio_iocbs_status)
662 free(td->aio_iocbs_status);
663}
664
665static int init_aio(struct thread_data *td)
666{
667 td->aio_ctx = malloc(sizeof(*td->aio_ctx));
668
669 if (io_queue_init(td->aio_depth, td->aio_ctx)) {
670 td->error = errno;
671 return 1;
672 }
673
674 td->aio_iocbs = malloc(td->aio_depth * sizeof(struct iocb));
675 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
676 td->aio_iocbs_status = malloc(td->aio_depth * sizeof(char));
677 return 0;
678}
679
5c24b2c4 680static void *thread_main(int shm_id, int offset, char *argv[])
892199bd
JA
681{
682 struct thread_data *td;
7292613b 683 void *data, *ptr = NULL;
4240cfa1 684 struct stat st;
892199bd
JA
685 int ret = 1, flags;
686
7292613b
JA
687 setsid();
688
892199bd
JA
689 data = shmat(shm_id, NULL, 0);
690 td = data + offset * sizeof(struct thread_data);
691 td->pid = getpid();
692
18e0b78c
JA
693 td->fd = -1;
694
695 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
696 td->error = errno;
697 goto err;
698 }
699
359e4e6b 700 printf("Thread (%s) (pid=%u) (f=%s) (aio=%d) started\n", td->ddir == DDIR_READ ? "read" : "write", td->pid, td->file_name, td->aio_depth);
892199bd
JA
701 fflush(stdout);
702
4240cfa1 703 sprintf(argv[0], "fio%d", offset);
892199bd 704
7dd1389e 705 flags = 0;
892199bd
JA
706 if (td->odirect)
707 flags |= O_DIRECT;
708
7dd1389e
JA
709 if (td->ddir == DDIR_READ)
710 td->fd = open(td->file_name, flags | O_RDONLY);
711 else
712 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT | O_TRUNC, 0644);
713
892199bd
JA
714 if (td->fd == -1) {
715 td->error = errno;
716 goto err;
717 }
718
43000118
JA
719 if (td->use_aio && init_aio(td))
720 goto err;
721
892199bd 722 if (init_random_state(td))
599002b3 723 goto err;
892199bd 724 if (init_stat_file(td))
599002b3 725 goto err;
892199bd
JA
726
727 if (td->ddir == DDIR_READ) {
4240cfa1 728 if (fstat(td->fd, &st) == -1) {
892199bd 729 td->error = errno;
599002b3 730 goto err;
892199bd
JA
731 }
732
4240cfa1 733 td->blocks = st.st_size / td->bs;
892199bd
JA
734 if (!td->blocks) {
735 td->error = EINVAL;
599002b3 736 goto err;
892199bd
JA
737 }
738 } else
739 td->blocks = 1024 * 1024 * 1024 / td->bs;
740
f737299d 741 if (td->ioprio) {
892199bd
JA
742 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
743 td->error = errno;
599002b3 744 goto err;
892199bd
JA
745 }
746 }
747
748 sem_post(&startup_sem);
749 sem_wait(&td->mutex);
43000118 750
7292613b
JA
751 gettimeofday(&td->start, NULL);
752
753 if (td->ratemin)
754 memcpy(&td->lastrate, &td->start, sizeof(td->start));
755
756 if (!td->use_aio) {
757 ptr = malloc(td->bs + MASK);
758 td->buf = ALIGN(ptr);
43000118 759 do_sync_io(td);
7292613b
JA
760 } else {
761 ptr = malloc(td->bs * td->aio_depth + MASK);
762 td->buf = ALIGN(ptr);
43000118 763 do_async_io(td);
7292613b
JA
764 }
765
be33abe4 766 td->runtime = mtime_since_now(&td->start);
892199bd 767 ret = 0;
892199bd 768err:
599002b3 769 shutdown_stat_file(td);
43000118
JA
770 if (td->use_aio)
771 cleanup_aio(td);
7292613b
JA
772 if (td->fd != -1) {
773 close(td->fd);
774 td->fd = -1;
775 }
599002b3 776 if (ret) {
892199bd 777 sem_post(&startup_sem);
599002b3
JA
778 sem_wait(&td->mutex);
779 }
7292613b
JA
780 if (ptr)
781 free(ptr);
02bdd9ba 782 td->runstate = TD_EXITED;
4240cfa1 783 shmdt(data);
892199bd
JA
784 return NULL;
785}
786
5c24b2c4 787static void free_shm(void)
892199bd
JA
788{
789 shmdt(threads);
790}
791
5c24b2c4 792static void show_thread_status(struct thread_data *td)
892199bd
JA
793{
794 int prio, prio_class;
795 unsigned long bw = 0;
796
213b446c
JA
797 if (!td->io_blocks && !td->error)
798 return;
799
892199bd 800 if (td->runtime)
4240cfa1 801 bw = (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
802
803 prio = td->ioprio & 0xff;
804 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
805
4240cfa1 806 printf("thread%d (%s): err=%2d, prio=%1d/%1d maxl=%5lumsec, io=%6luMiB, bw=%6luKiB/sec\n", td->thread_number, td->ddir == DDIR_READ ? " read": "write", td->error, prio_class, prio, td->max_latency, td->io_blocks * td->bs >> 20, bw);
892199bd
JA
807}
808
5c24b2c4 809static int setup_rate(struct thread_data *td)
86184d14 810{
4240cfa1
JA
811 int nr_reads_per_sec;
812
813 if (!td->rate)
814 return 0;
815
816 if (td->rate < td->ratemin) {
817 fprintf(stderr, "min rate larger than nominal rate\n");
818 return -1;
819 }
86184d14 820
4240cfa1 821 nr_reads_per_sec = td->rate * 1024 / td->bs;
86184d14
JA
822 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
823 td->rate_pending_usleep = 0;
4240cfa1 824 return 0;
86184d14
JA
825}
826
47d45203 827static struct thread_data *get_new_job(int global)
892199bd 828{
4240cfa1
JA
829 struct thread_data *td;
830
47d45203
JA
831 if (global)
832 return &def_thread;
4240cfa1
JA
833 if (thread_number >= MAX_JOBS)
834 return NULL;
835
836 td = &threads[thread_number++];
fc24389f 837 memset(td, 0, sizeof(*td));
892199bd 838
86184d14 839 td->thread_number = thread_number;
47d45203
JA
840 td->ddir = def_thread.ddir;
841 td->bs = def_thread.bs;
842 td->odirect = def_thread.odirect;
843 td->ratecycle = def_thread.ratecycle;
844 td->sequential = def_thread.sequential;
67903a2e 845 td->timeout = def_thread.timeout;
47d45203 846 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
f737299d
JA
847
848 return td;
849}
850
4240cfa1
JA
851static void put_job(struct thread_data *td)
852{
853 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
854 thread_number--;
855}
856
5c24b2c4
JA
857static int add_job(struct thread_data *td, const char *filename, int prioclass,
858 int prio)
f737299d 859{
47d45203
JA
860 if (td == &def_thread)
861 return 0;
862
f737299d 863 strcpy(td->file_name, filename);
892199bd 864 td->stat_fd = -1;
4240cfa1 865 sem_init(&td->mutex, 1, 0);
892199bd 866 td->min_latency = 10000000;
f737299d
JA
867 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
868
43000118
JA
869 if (td->use_aio && !td->aio_depth)
870 td->aio_depth = 1;
871
4240cfa1
JA
872 if (setup_rate(td))
873 return -1;
f737299d 874
43000118 875 printf("Client%d: file=%s, rw=%d, prio=%d, seq=%d, odir=%d, bs=%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, td->ioprio, td->sequential, td->odirect, td->bs, td->rate, td->use_aio, td->aio_depth);
4240cfa1 876 return 0;
892199bd
JA
877}
878
18e0b78c
JA
879static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
880{
f737299d 881 unsigned int i;
18e0b78c
JA
882
883 CPU_ZERO(&cpumask);
884
885 for (i = 0; i < sizeof(int) * 8; i++) {
886 if ((1 << i) & cpu)
887 CPU_SET(i, &cpumask);
888 }
889}
890
5c24b2c4 891static void fill_option(const char *input, char *output)
892199bd
JA
892{
893 int i;
894
895 i = 0;
896 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
897 output[i] = input[i];
898 i++;
899 }
900
901 output[i] = '\0';
902}
903
904/*
905 * job key words:
906 *
907 * file=
908 * bs=
909 * rw=
910 * direct=
911 */
5c24b2c4 912static void parse_jobs_cmd(int argc, char *argv[], int index)
892199bd 913{
f737299d
JA
914 struct thread_data *td;
915 unsigned int prio, prioclass, cpu;
892199bd
JA
916 char *string, *filename, *p, *c;
917 int i;
918
919 string = malloc(256);
920 filename = malloc(256);
921
922 for (i = index; i < argc; i++) {
923 p = argv[i];
924
925 c = strpbrk(p, "{");
926 if (!c)
927 break;
928
929 filename[0] = 0;
4240cfa1 930
47d45203 931 td = get_new_job(0);
4240cfa1
JA
932 if (!td)
933 break;
f737299d 934
892199bd 935 prioclass = 2;
f737299d 936 prio = 4;
892199bd
JA
937
938 c = strstr(p, "rw=");
939 if (c) {
940 c += 3;
941 if (*c == '0')
f737299d 942 td->ddir = DDIR_READ;
892199bd 943 else
f737299d 944 td->ddir = DDIR_WRITE;
892199bd
JA
945 }
946
947 c = strstr(p, "prio=");
948 if (c) {
949 c += 5;
950 prio = *c - '0';
951 }
952
953 c = strstr(p, "prioclass=");
954 if (c) {
955 c += 10;
956 prioclass = *c - '0';
957 }
958
959 c = strstr(p, "file=");
960 if (c) {
961 c += 5;
962 fill_option(c, filename);
963 }
964
965 c = strstr(p, "bs=");
966 if (c) {
967 c += 3;
968 fill_option(c, string);
f737299d
JA
969 td->bs = strtoul(string, NULL, 10);
970 td->bs <<= 10;
892199bd
JA
971 }
972
973 c = strstr(p, "direct=");
974 if (c) {
975 c += 7;
976 if (*c != '0')
f737299d 977 td->odirect = 1;
892199bd 978 else
f737299d 979 td->odirect = 0;
892199bd
JA
980 }
981
982 c = strstr(p, "delay=");
983 if (c) {
984 c += 6;
985 fill_option(c, string);
f737299d 986 td->delay_sleep = strtoul(string, NULL, 10);
892199bd
JA
987 }
988
86184d14
JA
989 c = strstr(p, "rate=");
990 if (c) {
991 c += 5;
992 fill_option(c, string);
f737299d 993 td->rate = strtoul(string, NULL, 10);
86184d14
JA
994 }
995
4240cfa1
JA
996 c = strstr(p, "ratemin=");
997 if (c) {
998 c += 8;
999 fill_option(c, string);
1000 td->ratemin = strtoul(string, NULL, 10);
1001 }
1002
1003 c = strstr(p, "ratecycle=");
1004 if (c) {
1005 c += 10;
1006 fill_option(c, string);
1007 td->ratecycle = strtoul(string, NULL, 10);
1008 }
1009
18e0b78c
JA
1010 c = strstr(p, "cpumask=");
1011 if (c) {
1012 c += 8;
1013 fill_option(c, string);
1014 cpu = strtoul(string, NULL, 10);
f737299d 1015 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1016 }
1017
4240cfa1
JA
1018 c = strstr(p, "fsync=");
1019 if (c) {
1020 c += 6;
1021 fill_option(c, string);
1022 td->fsync_blocks = strtoul(string, NULL, 10);
1023 }
18e0b78c 1024
fc24389f
JA
1025 c = strstr(p, "startdelay=");
1026 if (c) {
1027 c += 11;
1028 fill_option(c, string);
1029 td->start_delay = strtoul(string, NULL, 10);
1030 }
1031
67903a2e
JA
1032 c = strstr(p, "timeout=");
1033 if (c) {
1034 c += 8;
1035 fill_option(c, string);
1036 td->timeout = strtoul(string, NULL, 10);
1037 }
1038
43000118
JA
1039 c = strstr(p, "aio_depth=");
1040 if (c) {
1041 c += 10;
1042 fill_option(c, string);
1043 td->aio_depth = strtoul(string, NULL, 10);
1044 }
1045
1046 c = strstr(p, "aio");
1047 if (c)
1048 td->use_aio = 1;
1049
892199bd
JA
1050 c = strstr(p, "random");
1051 if (c)
f737299d 1052 td->sequential = 0;
892199bd
JA
1053 c = strstr(p, "sequential");
1054 if (c)
f737299d 1055 td->sequential = 1;
892199bd 1056
4240cfa1
JA
1057 if (add_job(td, filename, prioclass, prio))
1058 put_job(td);
892199bd
JA
1059 }
1060
7dd1389e
JA
1061 free(string);
1062 free(filename);
892199bd
JA
1063}
1064
5c24b2c4 1065static int check_int(char *p, char *name, unsigned int *val)
7dd1389e
JA
1066{
1067 char str[128];
1068
1069 sprintf(str, "%s=%%d", name);
1070 if (sscanf(p, str, val) == 1)
1071 return 0;
1072
1073 sprintf(str, "%s = %%d", name);
1074 if (sscanf(p, str, val) == 1)
1075 return 0;
1076
1077 return 1;
1078}
1079
7292613b 1080static int is_empty_or_comment(char *line)
7dd1389e
JA
1081{
1082 unsigned int i;
1083
7292613b 1084 for (i = 0; i < strlen(line); i++) {
7292613b 1085 if (line[i] == ';')
47d45203
JA
1086 return 1;
1087 if (!isspace(line[i]) && !iscntrl(line[i]))
7292613b
JA
1088 return 0;
1089 }
7dd1389e
JA
1090
1091 return 1;
1092}
1093
5c24b2c4 1094static int parse_jobs_ini(char *file)
7dd1389e 1095{
47d45203 1096 unsigned int prioclass, prio, cpu, global;
f737299d 1097 struct thread_data *td;
7dd1389e
JA
1098 char *string, *name;
1099 fpos_t off;
1100 FILE *f;
1101 char *p;
1102
1103 f = fopen(file, "r");
1104 if (!f) {
1105 perror("fopen");
4240cfa1 1106 return 1;
7dd1389e
JA
1107 }
1108
1109 string = malloc(4096);
1110 name = malloc(256);
1111
7dd1389e 1112 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b
JA
1113 if (is_empty_or_comment(p))
1114 continue;
7dd1389e
JA
1115 if (sscanf(p, "[%s]", name) != 1)
1116 continue;
1117
47d45203
JA
1118 global = !strncmp(name, "global", 6);
1119
7dd1389e
JA
1120 name[strlen(name) - 1] = '\0';
1121
47d45203 1122 td = get_new_job(global);
4240cfa1
JA
1123 if (!td)
1124 break;
f737299d 1125
7dd1389e 1126 prioclass = 2;
f737299d 1127 prio = 4;
7dd1389e
JA
1128
1129 fgetpos(f, &off);
1130 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b 1131 if (is_empty_or_comment(p))
e6402082
JA
1132 continue;
1133 if (strstr(p, "["))
7dd1389e 1134 break;
f737299d
JA
1135 if (!check_int(p, "bs", &td->bs)) {
1136 td->bs <<= 10;
7dd1389e
JA
1137 fgetpos(f, &off);
1138 continue;
1139 }
f737299d 1140 if (!check_int(p, "rw", &td->ddir)) {
7dd1389e
JA
1141 fgetpos(f, &off);
1142 continue;
1143 }
1144 if (!check_int(p, "prio", &prio)) {
1145 fgetpos(f, &off);
1146 continue;
1147 }
1148 if (!check_int(p, "prioclass", &prioclass)) {
1149 fgetpos(f, &off);
1150 continue;
1151 }
f737299d 1152 if (!check_int(p, "direct", &td->odirect)) {
7dd1389e
JA
1153 fgetpos(f, &off);
1154 continue;
1155 }
f737299d 1156 if (!check_int(p, "rate", &td->rate)) {
7dd1389e
JA
1157 fgetpos(f, &off);
1158 continue;
1159 }
4240cfa1
JA
1160 if (!check_int(p, "ratemin", &td->ratemin)) {
1161 fgetpos(f, &off);
1162 continue;
1163 }
1164 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1165 fgetpos(f, &off);
1166 continue;
1167 }
f737299d 1168 if (!check_int(p, "delay", &td->delay_sleep)) {
7dd1389e
JA
1169 fgetpos(f, &off);
1170 continue;
1171 }
18e0b78c 1172 if (!check_int(p, "cpumask", &cpu)) {
f737299d 1173 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1174 fgetpos(f, &off);
1175 continue;
1176 }
4240cfa1
JA
1177 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1178 fgetpos(f, &off);
1179 continue;
1180 }
fc24389f
JA
1181 if (!check_int(p, "startdelay", &td->start_delay)) {
1182 fgetpos(f, &off);
1183 continue;
1184 }
67903a2e
JA
1185 if (!check_int(p, "timeout", &td->timeout)) {
1186 fgetpos(f, &off);
1187 continue;
1188 }
43000118
JA
1189 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1190 fgetpos(f, &off);
1191 continue;
1192 }
1193 if (!strncmp(p, "sequential", 10)) {
f737299d 1194 td->sequential = 1;
7dd1389e
JA
1195 fgetpos(f, &off);
1196 continue;
1197 }
43000118 1198 if (!strncmp(p, "random", 6)) {
f737299d 1199 td->sequential = 0;
7dd1389e
JA
1200 fgetpos(f, &off);
1201 continue;
1202 }
43000118
JA
1203 if (!strncmp(p, "aio", 3)) {
1204 td->use_aio = 1;
1205 fgetpos(f, &off);
1206 continue;
1207 }
1208
e6402082 1209 printf("Client%d: bad option %s\n",td->thread_number,p);
7dd1389e
JA
1210 }
1211 fsetpos(f, &off);
1212
4240cfa1
JA
1213 if (add_job(td, name, prioclass, prio))
1214 put_job(td);
7dd1389e
JA
1215 }
1216
1217 free(string);
1218 free(name);
fc7d63df 1219 fclose(f);
4240cfa1 1220 return 0;
7dd1389e
JA
1221}
1222
5c24b2c4 1223static int parse_options(int argc, char *argv[])
892199bd 1224{
01c4d8de 1225 int i;
892199bd
JA
1226
1227 for (i = 1; i < argc; i++) {
1228 char *parm = argv[i];
1229
1230 if (parm[0] != '-')
1231 break;
1232
1233 parm++;
1234 switch (*parm) {
1235 case 's':
1236 parm++;
47d45203 1237 def_thread.sequential = !!atoi(parm);
892199bd
JA
1238 break;
1239 case 'b':
1240 parm++;
47d45203
JA
1241 def_thread.bs = atoi(parm);
1242 def_thread.bs <<= 10;
1243 if (!def_thread.bs) {
4240cfa1 1244 printf("bad block size\n");
47d45203 1245 def_thread.bs = DEF_BS;
4240cfa1 1246 }
892199bd
JA
1247 break;
1248 case 't':
1249 parm++;
47d45203 1250 def_thread.timeout = atoi(parm);
892199bd
JA
1251 break;
1252 case 'w':
1253 parm++;
1254 write_stat = !!atoi(parm);
1255 break;
1256 case 'r':
1257 parm++;
1258 repeatable = !!atoi(parm);
1259 break;
02bdd9ba
JA
1260 case 'R':
1261 parm++;
1262 rate_quit = !!atoi(parm);
1263 break;
892199bd
JA
1264 case 'o':
1265 parm++;
47d45203 1266 def_thread.odirect = !!atoi(parm);
892199bd 1267 break;
7dd1389e
JA
1268 case 'f':
1269 if (i + 1 >= argc) {
1270 printf("-f needs file as arg\n");
1271 break;
1272 }
1273 ini_file = strdup(argv[i+1]);
1274 break;
892199bd 1275 default:
7dd1389e 1276 printf("bad option %s\n", argv[i]);
892199bd
JA
1277 break;
1278 }
1279 }
1280
892199bd
JA
1281 return i;
1282}
1283
213b446c 1284static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
02bdd9ba 1285{
213b446c 1286 int i;
02bdd9ba
JA
1287
1288 for (i = 0; i < thread_number; i++) {
1289 struct thread_data *td = &threads[i];
1290
213b446c
JA
1291 if (td->runstate != TD_EXITED)
1292 continue;
02bdd9ba 1293
213b446c
JA
1294 td->runstate = TD_REAPED;
1295 waitpid(td->pid, NULL, 0);
1296 (*nr_running)--;
1297 (*m_rate) -= td->ratemin;
1298 (*t_rate) -= td->rate;
e6402082
JA
1299
1300 if (td->terminate)
1301 continue;
1302
213b446c
JA
1303 printf("Threads now running: %d", *nr_running);
1304 if (*m_rate || *t_rate)
1305 printf(", rate %d/%dKiB/sec", *t_rate, *m_rate);
1306 printf("\n");
1307 }
02bdd9ba
JA
1308}
1309
fc24389f
JA
1310static void run_threads(char *argv[])
1311{
be33abe4 1312 struct timeval genesis;
fc24389f
JA
1313 struct thread_data *td;
1314 unsigned long spent;
213b446c 1315 int i, todo, nr_running, m_rate, t_rate;
fc24389f
JA
1316
1317 gettimeofday(&genesis, NULL);
1318
1319 printf("Starting %d threads\n", thread_number);
1320 fflush(stdout);
1321
7292613b
JA
1322 signal(SIGINT, sig_handler);
1323
fc24389f 1324 todo = thread_number;
02bdd9ba 1325 nr_running = 0;
213b446c 1326 m_rate = t_rate = 0;
fc24389f 1327
213b446c 1328 while (todo) {
fc24389f
JA
1329 for (i = 0; i < thread_number; i++) {
1330 td = &threads[i];
1331
02bdd9ba 1332 if (td->runstate != TD_NOT_CREATED)
fc24389f
JA
1333 continue;
1334
213b446c
JA
1335 /*
1336 * never got a chance to start, killed by other
1337 * thread for some reason
1338 */
1339 if (td->terminate) {
1340 todo--;
1341 continue;
1342 }
1343
fc24389f 1344 if (td->start_delay) {
be33abe4 1345 spent = mtime_since_now(&genesis);
fc24389f
JA
1346
1347 if (td->start_delay * 1000 > spent)
1348 continue;
1349 }
1350
02bdd9ba 1351 td->runstate = TD_CREATED;
fc24389f
JA
1352 sem_init(&startup_sem, 1, 1);
1353 todo--;
1354
1355 if (fork())
1356 sem_wait(&startup_sem);
1357 else {
1358 thread_main(shm_id, i, argv);
1359 exit(0);
1360 }
1361 }
1362
1363 for (i = 0; i < thread_number; i++) {
1364 struct thread_data *td = &threads[i];
1365
02bdd9ba
JA
1366 if (td->runstate == TD_CREATED) {
1367 td->runstate = TD_STARTED;
1368 nr_running++;
213b446c
JA
1369 m_rate += td->ratemin;
1370 t_rate += td->rate;
fc24389f 1371 sem_post(&td->mutex);
213b446c
JA
1372
1373 printf("Threads now running: %d", nr_running);
1374 if (m_rate || t_rate)
1375 printf(", rate %d/%dKiB/sec", t_rate, m_rate);
1376 printf("\n");
fc24389f
JA
1377 }
1378 }
1379
213b446c 1380 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba 1381
fc24389f
JA
1382 if (todo)
1383 usleep(100000);
1384 }
02bdd9ba
JA
1385
1386 while (nr_running) {
213b446c 1387 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba
JA
1388 usleep(10000);
1389 }
fc24389f
JA
1390}
1391
892199bd
JA
1392int main(int argc, char *argv[])
1393{
1394 static unsigned long max_run[2], min_run[2], total_blocks[2];
1395 static unsigned long max_bw[2], min_bw[2], maxl[2], minl[2];
1396 static unsigned long read_mb, write_mb, read_agg, write_agg;
4240cfa1 1397 int i;
18e0b78c 1398
4240cfa1 1399 shm_id = shmget(0, MAX_JOBS * sizeof(struct thread_data), IPC_CREAT | 0600);
892199bd
JA
1400 if (shm_id == -1) {
1401 perror("shmget");
1402 return 1;
1403 }
1404
1405 threads = shmat(shm_id, NULL, 0);
86184d14
JA
1406 if (threads == (void *) -1 ) {
1407 perror("shmat");
1408 return 1;
1409 }
892199bd
JA
1410
1411 atexit(free_shm);
1412
47d45203 1413 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
4240cfa1
JA
1414 perror("sched_getaffinity");
1415 return 1;
1416 }
1417
47d45203
JA
1418 /*
1419 * fill globals
1420 */
1421 def_thread.ddir = DDIR_READ;
1422 def_thread.bs = DEF_BS;
1423 def_thread.odirect = 1;
1424 def_thread.ratecycle = DEF_RATE_CYCLE;
1425 def_thread.sequential = 1;
1426 def_thread.timeout = DEF_TIMEOUT;
1427
892199bd 1428 i = parse_options(argc, argv);
7dd1389e 1429
4240cfa1
JA
1430 if (ini_file) {
1431 if (parse_jobs_ini(ini_file))
1432 return 1;
1433 } else
1434 parse_jobs_cmd(argc, argv, i);
7dd1389e 1435
4240cfa1
JA
1436 if (!thread_number) {
1437 printf("Nothing to do\n");
1438 return 1;
1439 }
7dd1389e 1440
47d45203 1441 printf("%s: %s, bs=%uKiB, timeo=%u, write_stat=%u, odirect=%d\n", argv[0], def_thread.sequential ? "sequential" : "random", def_thread.bs >> 10, def_thread.timeout, write_stat, def_thread.odirect);
7dd1389e 1442
fc24389f 1443 run_threads(argv);
892199bd 1444
892199bd
JA
1445 min_bw[0] = min_run[0] = ~0UL;
1446 min_bw[1] = min_run[1] = ~0UL;
1447 minl[0] = minl[1] = ~0UL;
1448 for (i = 0; i < thread_number; i++) {
1449 struct thread_data *td = &threads[i];
1450 unsigned long bw = 0;
1451
1452 if (td->error)
7dd1389e 1453 goto show_stat;
892199bd
JA
1454
1455 if (td->runtime < min_run[td->ddir])
1456 min_run[td->ddir] = td->runtime;
1457 if (td->runtime > max_run[td->ddir])
1458 max_run[td->ddir] = td->runtime;
1459
1460 if (td->runtime)
4240cfa1 1461 bw = (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
1462 if (bw < min_bw[td->ddir])
1463 min_bw[td->ddir] = bw;
1464 if (bw > max_bw[td->ddir])
1465 max_bw[td->ddir] = bw;
1466 if (td->max_latency < minl[td->ddir])
1467 minl[td->ddir] = td->max_latency;
1468 if (td->max_latency > maxl[td->ddir])
1469 maxl[td->ddir] = td->max_latency;
1470
4240cfa1 1471 total_blocks[td->ddir] += td->io_blocks;
892199bd
JA
1472
1473 if (td->ddir == DDIR_READ) {
4240cfa1 1474 read_mb += (td->bs * td->io_blocks) >> 20;
892199bd 1475 if (td->runtime)
4240cfa1 1476 read_agg += (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
1477 }
1478 if (td->ddir == DDIR_WRITE) {
4240cfa1 1479 write_mb += (td->bs * td->io_blocks) >> 20;
892199bd 1480 if (td->runtime)
4240cfa1 1481 write_agg += (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
1482 }
1483
7dd1389e 1484show_stat:
892199bd
JA
1485 show_thread_status(td);
1486 }
1487
1488 printf("Run status:\n");
1489 if (max_run[DDIR_READ])
1490 printf(" READ: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", read_mb, read_agg, minl[0], maxl[0], min_bw[0], max_bw[0], min_run[0], max_run[0]);
1491 if (max_run[DDIR_WRITE])
1492 printf(" WRITE: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", write_mb, write_agg, minl[1], maxl[1], min_bw[1], max_bw[1], min_run[1], max_run[1]);
fc24389f 1493
892199bd
JA
1494 return 0;
1495}