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