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