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