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