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