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