[PATCH] fio: get_io_u() doesn't need the on-stack length/offset anymore
[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;
40ef7f64 404 double r;
892199bd
JA
405
406 if (!td->sequential) {
40ef7f64 407 drand48_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;
40ef7f64 418 double r;
7889f07b
JA
419
420 if (td->min_bs == td->max_bs)
421 buflen = td->min_bs;
422 else {
40ef7f64 423 drand48_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
605 if (hdr->fio_magic != FIO_HDR_MAGIC)
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
623 *back = 0;
624
625 left = io_u->buflen;
626 buf = io_u->buf;
627 off = io_u->offset;
628 while (left) {
629 hdr = (struct verify_header *) buf;
630 i.buf = buf;
631 i.buflen = hdr->len;
632
633 if (hdr->len > left) {
634 *back = left;
635 return 0;
636 }
637
638 if (verify_io_u(td, &i)) {
639 printf("failed verify at offset %lu\n", (unsigned long) off);
640 return 1;
641 }
642
643 buf += hdr->len;
644 left -= hdr->len;
645 off += hdr->len;
646 }
647
648 return 0;
649}
650
cfc702bd
JA
651/*
652 * fill body of io_u->buf with random data and add a header with the
653 * (eg) sha1sum of that data.
654 */
e8457004 655static void populate_io_u(struct thread_data *td, struct io_u *io_u)
cfc702bd 656{
e8457004
JA
657 struct md5_ctx md5_ctx;
658 struct verify_header hdr;
659 unsigned char *p = (unsigned char *) io_u->buf;
660
661 hdr.fio_magic = FIO_HDR_MAGIC;
662 hdr.len = io_u->buflen;
663 p += sizeof(hdr);
664 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
665
666 memset(&md5_ctx, 0, sizeof(md5_ctx));
667 md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr));
668 memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
669 memcpy(io_u->buf, &hdr, sizeof(hdr));
cfc702bd
JA
670}
671
2c83567e
JA
672static void put_io_u(struct thread_data *td, struct io_u *io_u)
673{
674 list_del(&io_u->list);
675 list_add(&io_u->list, &td->io_u_freelist);
676 td->cur_depth--;
677}
678
e8457004
JA
679static struct io_u *__get_io_u(struct thread_data *td)
680{
681 struct io_u *io_u;
682
683 if (list_empty(&td->io_u_freelist))
684 return NULL;
685
686 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
687 list_del(&io_u->list);
688 list_add(&io_u->list, &td->io_u_busylist);
689 return io_u;
690}
691
2c83567e
JA
692static struct io_u *get_io_u(struct thread_data *td)
693{
694 struct io_u *io_u;
695
e8457004
JA
696 io_u = __get_io_u(td);
697 if (!io_u)
2c83567e
JA
698 return NULL;
699
b2a369fb
JA
700 io_u->offset = get_next_offset(td);
701 io_u->buflen = get_next_buflen(td);
702 if (!io_u->buflen) {
e8457004 703 put_io_u(td, io_u);
7889f07b 704 return NULL;
e8457004 705 }
2c83567e 706
cfc702bd 707 if (!td_read(td) && td->verify)
e8457004 708 populate_io_u(td, io_u);
cfc702bd 709
2c83567e
JA
710 if (td->use_aio) {
711 if (td_read(td))
712 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
713 else
714 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
715 }
716
57d753e3 717 gettimeofday(&io_u->start_time, NULL);
2c83567e
JA
718 td->cur_depth++;
719 return io_u;
720}
721
40ef7f64
JA
722static inline void td_set_runstate(struct thread_data *td, int runstate)
723{
724 td->old_runstate = td->runstate;
725 td->runstate = runstate;
726}
727
cfc702bd
JA
728static int do_sync_verify(struct thread_data *td)
729{
40ef7f64 730 struct timeval t;
e8457004
JA
731 struct io_u *io_u = NULL;
732 loff_t off = 0;
733 int back, ret;
734
40ef7f64 735 td_set_runstate(td, TD_VERIFYING);
e8457004
JA
736
737 io_u = __get_io_u(td);
738
739 if (lseek(td->fd, 0, SEEK_SET) < 0) {
740 td->error = errno;
741 goto out;
742 }
743
40ef7f64
JA
744 if (!td->odirect) {
745 unsigned long size = td->kb << 10;
746
747 if (fadvise(td->fd, 0, size, POSIX_FADV_DONTNEED) < 0) {
748 td->error = errno;
749 goto out;
750 }
751 }
752
e8457004
JA
753 do {
754 if (td->terminate)
755 break;
40ef7f64
JA
756
757 gettimeofday(&t, NULL);
758 if (runtime_exceeded(td, &t))
759 break;
760
e8457004
JA
761 io_u->offset = off;
762 io_u->buflen = td->max_bs;
763
764 ret = read(td->fd, io_u->buf, io_u->buflen);
765 if (ret < (int) io_u->buflen) {
766 if (ret == -1) {
767 td->error = errno;
768 break;
769 } else if (!ret)
770 break;
771 else
772 io_u->buflen = ret;
773 }
774
775 if (verify_io_us(td, io_u, &back))
776 break;
777
778 if (back) {
f8e3d840 779 ret -= back;
e8457004
JA
780 if (lseek(td->fd, -back, SEEK_CUR) < 0) {
781 td->error = errno;
782 break;
783 }
784 }
785
786 off += ret;
787 } while (1);
788
789out:
40ef7f64 790 td_set_runstate(td, TD_RUNNING);
e8457004
JA
791 put_io_u(td, io_u);
792
793 return td->error == 0;
cfc702bd
JA
794}
795
43000118 796static void do_sync_io(struct thread_data *td)
892199bd 797{
7889f07b 798 unsigned long msec, usec;
e8457004 799 struct io_u *io_u = NULL;
2c83567e 800 struct timeval e;
892199bd 801
63a09e51 802 td->cur_off = 0;
e8457004 803 td->last_kb = 0;
63a09e51 804
f8e3d840 805 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
892199bd
JA
806 int ret;
807
808 if (td->terminate)
809 break;
810
2c83567e 811 io_u = get_io_u(td);
7889f07b
JA
812 if (!io_u)
813 break;
2c83567e 814
63a09e51
JA
815 if (td->cur_off != io_u->offset) {
816 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
817 td->error = errno;
818 break;
819 }
892199bd
JA
820 }
821
02983297 822 if (td_read(td))
2c83567e 823 ret = read(td->fd, io_u->buf, io_u->buflen);
892199bd 824 else
2c83567e 825 ret = write(td->fd, io_u->buf, io_u->buflen);
892199bd 826
2c83567e 827 if (ret < (int) io_u->buflen) {
892199bd
JA
828 if (ret == -1)
829 td->error = errno;
830 break;
831 }
832
4240cfa1 833 td->io_blocks++;
8c033f93 834 td->io_kb += io_u->buflen >> 10;
f8e3d840 835 td->this_io_kb += io_u->buflen >> 10;
63a09e51 836 td->cur_off = io_u->offset + io_u->buflen;
4240cfa1 837
86184d14
JA
838 gettimeofday(&e, NULL);
839
57d753e3 840 usec = utime_since(&io_u->start_time, &e);
86184d14 841
9e850933 842 rate_throttle(td, usec, io_u->buflen);
892199bd 843
4240cfa1
JA
844 if (check_min_rate(td, &e)) {
845 td->error = ENODATA;
846 break;
847 }
892199bd 848
4240cfa1 849 msec = usec / 1000;
57d753e3
JA
850 add_clat_sample(td, msec);
851 add_bw_sample(td, msec);
67903a2e
JA
852
853 if (runtime_exceeded(td, &e))
854 break;
2c83567e 855
cdf92433 856 put_io_u(td, io_u);
e8457004 857 io_u = NULL;
cdf92433 858
e97712ed
JA
859 if (td->thinktime)
860 usec_sleep(td->thinktime);
861
cdf92433
JA
862 if (should_fsync(td) && td->fsync_blocks &&
863 (td->io_blocks % td->fsync_blocks) == 0)
864 fsync(td->fd);
892199bd
JA
865 }
866
e8457004
JA
867 if (io_u)
868 put_io_u(td, io_u);
869
4240cfa1 870 if (should_fsync(td))
892199bd 871 fsync(td->fd);
892199bd 872}
43000118 873
2c83567e 874static int io_u_queue(struct thread_data *td, struct io_u *io_u)
56b0eff0 875{
2c83567e 876 struct iocb *iocb = &io_u->iocb;
56b0eff0
JA
877 int ret;
878
879 do {
254605cd 880 ret = io_submit(td->aio_ctx, 1, &iocb);
56b0eff0
JA
881 if (ret == 1)
882 return 0;
a592bd33 883 else if (ret == EAGAIN)
56b0eff0 884 usleep(100);
a592bd33
JA
885 else if (ret == EINTR)
886 continue;
56b0eff0
JA
887 else
888 break;
889 } while (1);
890
a592bd33 891 return ret;
56b0eff0
JA
892}
893
98168d55 894#define iocb_time(iocb) ((unsigned long) (iocb)->data)
2c83567e
JA
895#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
896
9e850933 897static int ios_completed(struct thread_data *td, int nr)
2c83567e
JA
898{
899 unsigned long msec;
900 struct io_u *io_u;
901 struct timeval e;
9e850933 902 int i, bytes_done;
2c83567e
JA
903
904 gettimeofday(&e, NULL);
905
9e850933 906 for (i = 0, bytes_done = 0; i < nr; i++) {
2c83567e
JA
907 io_u = ev_to_iou(td->aio_events + i);
908
8c033f93
JA
909 td->io_blocks++;
910 td->io_kb += io_u->buflen >> 10;
f8e3d840 911 td->this_io_kb += io_u->buflen >> 10;
9e850933 912 bytes_done += io_u->buflen;
8c033f93 913
2c83567e
JA
914 msec = mtime_since(&io_u->issue_time, &e);
915
57d753e3
JA
916 add_clat_sample(td, msec);
917 add_bw_sample(td, msec);
2c83567e
JA
918
919 put_io_u(td, io_u);
920 }
9e850933
JA
921
922 return bytes_done;
2c83567e
JA
923}
924
925static void cleanup_pending_aio(struct thread_data *td)
926{
927 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
928 struct list_head *entry, *n;
929 struct io_u *io_u;
930 int r;
931
932 /*
933 * get immediately available events, if any
934 */
935 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
936 if (r > 0)
937 ios_completed(td, r);
938
939 /*
940 * now cancel remaining active events
941 */
942 list_for_each_safe(entry, n, &td->io_u_busylist) {
943 io_u = list_entry(entry, struct io_u, list);
944
945 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
946 if (!r)
947 put_io_u(td, io_u);
948 }
949
950 if (td->cur_depth) {
951 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
952 if (r > 0)
953 ios_completed(td, r);
954 }
955}
98168d55 956
cfc702bd
JA
957static int do_async_verify(struct thread_data *td)
958{
e8457004
JA
959 /*
960 * need to implement the async version
961 */
962 return do_sync_verify(td);
cfc702bd
JA
963}
964
43000118
JA
965static void do_async_io(struct thread_data *td)
966{
967 struct timeval s, e;
7889f07b 968 unsigned long usec;
43000118 969
e8457004
JA
970 td->cur_off = 0;
971 td->last_kb = 0;
972
f8e3d840 973 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
43000118
JA
974 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
975 struct timespec *timeout;
2c83567e
JA
976 int ret, min_evts = 0;
977 struct io_u *io_u;
9e850933 978 unsigned int bytes_done;
43000118
JA
979
980 if (td->terminate)
981 break;
982
2c83567e 983 io_u = get_io_u(td);
7889f07b
JA
984 if (!io_u)
985 break;
43000118 986
57d753e3 987 memcpy(&s, &io_u->start_time, sizeof(s));
8baf1bcc 988
2c83567e 989 ret = io_u_queue(td, io_u);
56b0eff0 990 if (ret) {
a3fdb993 991 put_io_u(td, io_u);
a592bd33 992 td->error = ret;
43000118
JA
993 break;
994 }
995
57d753e3
JA
996 gettimeofday(&io_u->issue_time, NULL);
997 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
2c83567e 998 if (td->cur_depth < td->aio_depth) {
43000118
JA
999 timeout = &ts;
1000 min_evts = 0;
1001 } else {
1002 timeout = NULL;
1003 min_evts = 1;
1004 }
1005
2c83567e 1006 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
43000118
JA
1007 if (ret < 0) {
1008 td->error = errno;
1009 break;
1010 } else if (!ret)
1011 continue;
1012
9e850933 1013 bytes_done = ios_completed(td, ret);
43000118 1014
98168d55
JA
1015 /*
1016 * the rate is batched for now, it should work for batches
1017 * of completions except the very first one which may look
1018 * a little bursty
1019 */
2c83567e 1020 gettimeofday(&e, NULL);
43000118
JA
1021 usec = utime_since(&s, &e);
1022
9e850933 1023 rate_throttle(td, usec, bytes_done);
43000118
JA
1024
1025 if (check_min_rate(td, &e)) {
1026 td->error = ENODATA;
1027 break;
1028 }
67903a2e
JA
1029
1030 if (runtime_exceeded(td, &e))
1031 break;
765d9223
JA
1032
1033 if (td->thinktime)
1034 usec_sleep(td->thinktime);
cdf92433
JA
1035
1036 if (should_fsync(td) && td->fsync_blocks &&
1037 (td->io_blocks % td->fsync_blocks) == 0)
1038 fsync(td->fd);
43000118 1039 }
56b0eff0 1040
2c83567e
JA
1041 if (td->cur_depth)
1042 cleanup_pending_aio(td);
4ac89145
JA
1043
1044 if (should_fsync(td))
1045 fsync(td->fd);
56b0eff0
JA
1046}
1047
1048static void cleanup_aio(struct thread_data *td)
1049{
254605cd
JA
1050 io_destroy(td->aio_ctx);
1051
43000118
JA
1052 if (td->aio_events)
1053 free(td->aio_events);
43000118
JA
1054}
1055
1056static int init_aio(struct thread_data *td)
1057{
254605cd 1058 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
43000118
JA
1059 td->error = errno;
1060 return 1;
1061 }
1062
43000118 1063 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
43000118
JA
1064 return 0;
1065}
1066
2c83567e
JA
1067static void cleanup_io_u(struct thread_data *td)
1068{
1069 struct list_head *entry, *n;
1070 struct io_u *io_u;
1071
1072 list_for_each_safe(entry, n, &td->io_u_freelist) {
1073 io_u = list_entry(entry, struct io_u, list);
1074
1075 list_del(&io_u->list);
2c83567e
JA
1076 free(io_u);
1077 }
6b71c826 1078
99c6704f
JA
1079 if (td->mem_type == MEM_MALLOC)
1080 free(td->orig_buffer);
1081 else if (td->mem_type == MEM_SHM) {
1082 struct shmid_ds sbuf;
1083
1084 shmdt(td->orig_buffer);
1085 shmctl(td->shm_id, IPC_RMID, &sbuf);
1086 }
2c83567e
JA
1087}
1088
99c6704f 1089static int init_io_u(struct thread_data *td)
2c83567e
JA
1090{
1091 struct io_u *io_u;
99c6704f 1092 int i, max_units, mem_size;
6b71c826 1093 char *p;
2c83567e
JA
1094
1095 if (!td->use_aio)
1096 max_units = 1;
1097 else
1098 max_units = td->aio_depth;
1099
7889f07b 1100 mem_size = td->max_bs * max_units + MASK;
99c6704f
JA
1101
1102 if (td->mem_type == MEM_MALLOC)
1103 td->orig_buffer = malloc(mem_size);
1104 else if (td->mem_type == MEM_SHM) {
1105 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
1106 if (td->shm_id < 0) {
1107 td->error = errno;
1108 perror("shmget");
1109 return 1;
1110 }
1111
1112 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1113 if (td->orig_buffer == (void *) -1) {
1114 td->error = errno;
1115 perror("shmat");
1116 return 1;
1117 }
1118 }
6b71c826 1119
2c83567e
JA
1120 INIT_LIST_HEAD(&td->io_u_freelist);
1121 INIT_LIST_HEAD(&td->io_u_busylist);
1122
99c6704f 1123 p = ALIGN(td->orig_buffer);
2c83567e
JA
1124 for (i = 0; i < max_units; i++) {
1125 io_u = malloc(sizeof(*io_u));
1126 memset(io_u, 0, sizeof(*io_u));
1127 INIT_LIST_HEAD(&io_u->list);
1128
7889f07b 1129 io_u->buf = p + td->max_bs * i;
2c83567e
JA
1130 list_add(&io_u->list, &td->io_u_freelist);
1131 }
99c6704f
JA
1132
1133 return 0;
2c83567e
JA
1134}
1135
a0a9b35b
JA
1136static void setup_log(struct io_log **log)
1137{
1138 struct io_log *l = malloc(sizeof(*l));
1139
1140 l->nr_samples = 0;
1141 l->max_samples = 1024;
1142 l->log = malloc(l->max_samples * sizeof(struct io_sample));
1143 *log = l;
1144}
1145
1146static void finish_log(struct thread_data *td, struct io_log *log, char *name)
1147{
1148 char file_name[128];
1149 FILE *f;
1150 int i;
1151
1152 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
1153 f = fopen(file_name, "w");
1154 if (!f) {
1155 perror("fopen log");
1156 return;
1157 }
1158
1159 for (i = 0; i < log->nr_samples; i++)
1160 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
1161
1162 fclose(f);
1163 free(log->log);
1164 free(log);
1165}
1166
02983297
JA
1167static int create_file(struct thread_data *td)
1168{
7889f07b 1169 unsigned long long left;
02983297 1170 char *b;
7889f07b 1171 int r, bs;
02983297 1172
02983297
JA
1173 /*
1174 * unless specifically asked for overwrite, let normal io extend it
1175 */
1176 if (!td_read(td) && !td->overwrite)
1177 return 0;
1178
57d753e3
JA
1179 if (!td->file_size) {
1180 fprintf(stderr, "Need size for create\n");
1181 td->error = EINVAL;
1182 return 1;
1183 }
1184
42fd89a7
JA
1185 printf("Client%d: Laying out IO file\n", td->thread_number);
1186
02983297
JA
1187 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1188 if (td->fd < 0) {
1189 td->error = errno;
1190 return 1;
1191 }
1192
7889f07b
JA
1193 td->kb = td->file_size >> 10;
1194 b = malloc(td->max_bs);
1195 memset(b, 0, td->max_bs);
1196
1197 left = td->file_size;
1198 while (left) {
1199 bs = td->max_bs;
1200 if (bs > left)
1201 bs = left;
02983297 1202
7889f07b 1203 r = write(td->fd, b, bs);
02983297 1204
7889f07b
JA
1205 if (r == bs) {
1206 left -= bs;
02983297 1207 continue;
7889f07b 1208 } else {
02983297
JA
1209 if (r < 0)
1210 td->error = errno;
1211 else
1212 td->error = EIO;
1213
1214 break;
1215 }
1216 }
1217
fc097bfe
JA
1218 if (td->create_fsync)
1219 fsync(td->fd);
1220
02983297
JA
1221 close(td->fd);
1222 td->fd = -1;
1223 free(b);
1224 return 0;
1225}
1226
1227static int file_exists(struct thread_data *td)
1228{
1229 struct stat st;
1230
1231 if (stat(td->file_name, &st) != -1)
1232 return 1;
1233
1234 return errno != ENOENT;
1235}
1236
1237static int setup_file(struct thread_data *td)
1238{
1239 struct stat st;
1240 int flags = 0;
1241
1242 if (!file_exists(td)) {
1243 if (!td->create_file) {
1244 td->error = ENOENT;
1245 return 1;
1246 }
1247 if (create_file(td))
1248 return 1;
1249 }
1250
1251 if (td->odirect)
1252 flags |= O_DIRECT;
1253
1254 if (td_read(td))
1255 td->fd = open(td->file_name, flags | O_RDONLY);
1256 else {
1257 if (!td->overwrite)
1258 flags |= O_TRUNC;
74b4b5fb
JA
1259 if (td->sync_io)
1260 flags |= O_SYNC;
e8457004
JA
1261 if (td->verify)
1262 flags |= O_RDWR;
1263 else
1264 flags |= O_WRONLY;
02983297 1265
e8457004 1266 td->fd = open(td->file_name, flags | O_CREAT, 0600);
02983297
JA
1267 }
1268
1269 if (td->fd == -1) {
1270 td->error = errno;
1271 return 1;
1272 }
1273
1274 if (td_read(td)) {
1275 if (fstat(td->fd, &st) == -1) {
1276 td->error = errno;
1277 return 1;
1278 }
1279
1280 if (td->file_size > st.st_size)
1281 st.st_size = td->file_size;
1282 } else {
1283 if (!td->file_size)
1284 td->file_size = 1024 * 1024 * 1024;
1285
1286 st.st_size = td->file_size;
1287 }
1288
7889f07b
JA
1289 td->kb = (st.st_size - td->file_offset) / 1024;
1290 if (!td->kb) {
02983297
JA
1291 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1292 td->error = EINVAL;
1293 return 1;
1294 }
1295
b95799ca
JA
1296 if (td->invalidate_cache) {
1297 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1298 td->error = errno;
1299 return 1;
1300 }
1301 }
1302
02983297
JA
1303 return 0;
1304}
1305
5c24b2c4 1306static void *thread_main(int shm_id, int offset, char *argv[])
892199bd
JA
1307{
1308 struct thread_data *td;
02983297 1309 int ret = 1;
2c83567e 1310 void *data;
892199bd 1311
7292613b
JA
1312 setsid();
1313
892199bd 1314 data = shmat(shm_id, NULL, 0);
4ac89145
JA
1315 if (data == (void *) -1) {
1316 perror("shmat");
1317 return NULL;
1318 }
1319
892199bd
JA
1320 td = data + offset * sizeof(struct thread_data);
1321 td->pid = getpid();
1322
99c6704f
JA
1323 if (init_io_u(td))
1324 goto err;
2c83567e 1325
18e0b78c
JA
1326 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1327 td->error = errno;
1328 goto err;
1329 }
1330
4240cfa1 1331 sprintf(argv[0], "fio%d", offset);
892199bd 1332
43000118
JA
1333 if (td->use_aio && init_aio(td))
1334 goto err;
1335
892199bd 1336 if (init_random_state(td))
599002b3 1337 goto err;
892199bd 1338
f737299d 1339 if (td->ioprio) {
892199bd
JA
1340 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1341 td->error = errno;
599002b3 1342 goto err;
892199bd
JA
1343 }
1344 }
1345
1346 sem_post(&startup_sem);
1347 sem_wait(&td->mutex);
43000118 1348
fc097bfe
JA
1349 if (!td->create_serialize && setup_file(td))
1350 goto err;
1351
7292613b
JA
1352 gettimeofday(&td->start, NULL);
1353
1354 if (td->ratemin)
1355 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1356
fd1ae4c9
JA
1357 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1358
b6794fbf 1359 while (td->loops--) {
cfc702bd 1360 if (!td->use_aio) {
b6794fbf 1361 do_sync_io(td);
cfc702bd
JA
1362
1363 if (td->verify) {
f8e3d840 1364 if (!do_sync_verify(td))
cfc702bd
JA
1365 break;
1366 }
1367 } else {
b6794fbf 1368 do_async_io(td);
cfc702bd
JA
1369
1370 if (td->verify) {
f8e3d840 1371 if (!do_async_verify(td))
cfc702bd
JA
1372 break;
1373 }
1374 }
b6794fbf 1375 }
7292613b 1376
be33abe4 1377 td->runtime = mtime_since_now(&td->start);
892199bd 1378 ret = 0;
a0a9b35b
JA
1379
1380 if (td->bw_log)
1381 finish_log(td, td->bw_log, "bw");
1382 if (td->lat_log)
1383 finish_log(td, td->lat_log, "lat");
4ac89145 1384
98dd52d6
JA
1385 if (exitall_on_terminate)
1386 sig_handler(0);
1387
892199bd 1388err:
7292613b
JA
1389 if (td->fd != -1) {
1390 close(td->fd);
1391 td->fd = -1;
1392 }
4ac89145
JA
1393 if (td->use_aio)
1394 cleanup_aio(td);
2c83567e 1395 cleanup_io_u(td);
599002b3 1396 if (ret) {
892199bd 1397 sem_post(&startup_sem);
599002b3
JA
1398 sem_wait(&td->mutex);
1399 }
40ef7f64 1400 td_set_runstate(td, TD_EXITED);
4240cfa1 1401 shmdt(data);
892199bd
JA
1402 return NULL;
1403}
1404
5c24b2c4 1405static void free_shm(void)
892199bd 1406{
c269123b
JA
1407 struct shmid_ds sbuf;
1408
1409 if (threads) {
1410 shmdt(threads);
1411 threads = NULL;
1412 shmctl(shm_id, IPC_RMID, &sbuf);
1413 }
892199bd
JA
1414}
1415
57d753e3
JA
1416static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1417 double *mean, double *dev)
1418{
1419 double n;
1420
1421 if (is->samples == 0)
1422 return 0;
1423
1424 *min = is->min_val;
1425 *max = is->max_val;
1426
1427 n = (double) is->samples;
1428 *mean = (double) is->val / n;
1429 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1430 return 1;
1431}
1432
5c24b2c4 1433static void show_thread_status(struct thread_data *td)
892199bd
JA
1434{
1435 int prio, prio_class;
57d753e3
JA
1436 unsigned long min, max, bw = 0;
1437 double mean, dev;
892199bd 1438
8c033f93 1439 if (!td->io_kb && !td->error)
213b446c
JA
1440 return;
1441
892199bd 1442 if (td->runtime)
7889f07b 1443 bw = td->io_kb * 1024 / td->runtime;
892199bd
JA
1444
1445 prio = td->ioprio & 0xff;
1446 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1447
396fe7cd 1448 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 1449
57d753e3
JA
1450 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1451 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1452 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1453 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1454 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1455 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
892199bd
JA
1456}
1457
5c24b2c4 1458static int setup_rate(struct thread_data *td)
86184d14 1459{
4240cfa1
JA
1460 int nr_reads_per_sec;
1461
1462 if (!td->rate)
1463 return 0;
1464
1465 if (td->rate < td->ratemin) {
1466 fprintf(stderr, "min rate larger than nominal rate\n");
1467 return -1;
1468 }
86184d14 1469
7889f07b 1470 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
86184d14
JA
1471 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1472 td->rate_pending_usleep = 0;
4240cfa1 1473 return 0;
86184d14
JA
1474}
1475
47d45203 1476static struct thread_data *get_new_job(int global)
892199bd 1477{
4240cfa1
JA
1478 struct thread_data *td;
1479
47d45203
JA
1480 if (global)
1481 return &def_thread;
8867c0a8 1482 if (thread_number >= max_jobs)
4240cfa1
JA
1483 return NULL;
1484
1485 td = &threads[thread_number++];
fc24389f 1486 memset(td, 0, sizeof(*td));
892199bd 1487
e4ed35c3 1488 td->fd = -1;
86184d14 1489 td->thread_number = thread_number;
76cb7b42 1490
47d45203 1491 td->ddir = def_thread.ddir;
76cb7b42
JA
1492 td->ioprio = def_thread.ioprio;
1493 td->sequential = def_thread.sequential;
47d45203 1494 td->bs = def_thread.bs;
8c033f93
JA
1495 td->min_bs = def_thread.min_bs;
1496 td->max_bs = def_thread.max_bs;
47d45203 1497 td->odirect = def_thread.odirect;
e97712ed 1498 td->thinktime = def_thread.thinktime;
76cb7b42
JA
1499 td->fsync_blocks = def_thread.fsync_blocks;
1500 td->start_delay = def_thread.start_delay;
67903a2e 1501 td->timeout = def_thread.timeout;
76cb7b42 1502 td->use_aio = def_thread.use_aio;
02983297
JA
1503 td->create_file = def_thread.create_file;
1504 td->overwrite = def_thread.overwrite;
b95799ca 1505 td->invalidate_cache = def_thread.invalidate_cache;
76cb7b42 1506 td->file_size = def_thread.file_size;
9b5cf6c0 1507 td->file_offset = def_thread.file_offset;
76cb7b42
JA
1508 td->rate = def_thread.rate;
1509 td->ratemin = def_thread.ratemin;
1510 td->ratecycle = def_thread.ratecycle;
1511 td->aio_depth = def_thread.aio_depth;
99c6704f
JA
1512 td->sync_io = def_thread.sync_io;
1513 td->mem_type = def_thread.mem_type;
1d035750 1514 td->bw_avg_time = def_thread.bw_avg_time;
fc097bfe
JA
1515 td->create_serialize = def_thread.create_serialize;
1516 td->create_fsync = def_thread.create_fsync;
b6794fbf 1517 td->loops = def_thread.loops;
cfc702bd 1518 td->verify = def_thread.verify;
47d45203 1519 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
f737299d
JA
1520
1521 return td;
1522}
1523
4240cfa1
JA
1524static void put_job(struct thread_data *td)
1525{
1526 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1527 thread_number--;
1528}
1529
5c24b2c4
JA
1530static int add_job(struct thread_data *td, const char *filename, int prioclass,
1531 int prio)
f737299d 1532{
47d45203
JA
1533 if (td == &def_thread)
1534 return 0;
1535
f737299d 1536 strcpy(td->file_name, filename);
4240cfa1 1537 sem_init(&td->mutex, 1, 0);
f737299d
JA
1538 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1539
57d753e3
JA
1540 td->clat_stat.min_val = ULONG_MAX;
1541 td->slat_stat.min_val = ULONG_MAX;
1542 td->bw_stat.min_val = ULONG_MAX;
1543
8dbff0b1
JA
1544 run_str[td->thread_number - 1] = 'P';
1545
4ac89145
JA
1546 if (td->use_aio && !td->aio_depth)
1547 td->aio_depth = 1;
43000118 1548
8bdcfab5
JA
1549 if (td->min_bs == -1)
1550 td->min_bs = td->bs;
1551 if (td->max_bs == -1)
1552 td->max_bs = td->bs;
1553
4240cfa1
JA
1554 if (setup_rate(td))
1555 return -1;
f737299d 1556
a0a9b35b
JA
1557 if (write_lat_log)
1558 setup_log(&td->lat_log);
1559 if (write_bw_log)
1560 setup_log(&td->bw_log);
1561
7889f07b 1562 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 1563 return 0;
892199bd
JA
1564}
1565
18e0b78c
JA
1566static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1567{
f737299d 1568 unsigned int i;
18e0b78c
JA
1569
1570 CPU_ZERO(&cpumask);
1571
1572 for (i = 0; i < sizeof(int) * 8; i++) {
1573 if ((1 << i) & cpu)
1574 CPU_SET(i, &cpumask);
1575 }
1576}
1577
7889f07b
JA
1578unsigned long get_mult(char c)
1579{
1580 switch (c) {
1581 case 'k':
1582 case 'K':
1583 return 1024;
1584 case 'm':
1585 case 'M':
1586 return 1024 * 1024;
1587 case 'g':
1588 case 'G':
1589 return 1024 * 1024 * 1024;
1590 default:
1591 return 1;
1592 }
1593}
1594
02983297
JA
1595/*
1596 * convert string after '=' into decimal value, noting any size suffix
1597 */
1598static int str_cnv(char *p, unsigned long long *val)
1599{
02983297
JA
1600 char *str;
1601 int len;
1602
1603 str = strstr(p, "=");
1604 if (!str)
1605 return 1;
1606
1607 str++;
1608 len = strlen(str);
02983297
JA
1609
1610 *val = strtoul(str, NULL, 10);
1611 if (*val == ULONG_MAX && errno == ERANGE)
1612 return 1;
1613
7889f07b 1614 *val *= get_mult(str[len - 2]);
02983297 1615 return 0;
02983297
JA
1616}
1617
02983297
JA
1618static int check_strcnv(char *p, char *name, unsigned long long *val)
1619{
1620 if (!strstr(p, name))
1621 return 1;
1622
1623 return str_cnv(p, val);
1624}
1625
99c6704f
JA
1626static int check_str(char *p, char *name, char *option)
1627{
1628 char *s = strstr(p, name);
1629
1630 if (!s)
1631 return 1;
1632
1633 s += strlen(name);
1634 if (strstr(s, option))
1635 return 0;
1636
1637 return 1;
1638}
1639
7889f07b
JA
1640static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1641{
1642 char str[128];
1643 char s1, s2;
1644
1645 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1646 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1647 *s *= get_mult(s1);
1648 *e *= get_mult(s2);
1649 return 0;
1650 }
1651
1652 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1653 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1654 *s *= get_mult(s1);
1655 *e *= get_mult(s2);
1656 return 0;
1657 }
1658
1659 sprintf(str, "%s=%%lu-%%lu", name);
1660 if (sscanf(p, str, s, e) == 2)
1661 return 0;
1662
1663 sprintf(str, "%s = %%lu-%%lu", name);
1664 if (sscanf(p, str, s, e) == 2)
1665 return 0;
1666
1667 return 1;
1668
1669}
1670
5c24b2c4 1671static int check_int(char *p, char *name, unsigned int *val)
7dd1389e
JA
1672{
1673 char str[128];
1674
1675 sprintf(str, "%s=%%d", name);
1676 if (sscanf(p, str, val) == 1)
1677 return 0;
1678
1679 sprintf(str, "%s = %%d", name);
1680 if (sscanf(p, str, val) == 1)
1681 return 0;
1682
1683 return 1;
1684}
1685
7292613b 1686static int is_empty_or_comment(char *line)
7dd1389e
JA
1687{
1688 unsigned int i;
1689
7292613b 1690 for (i = 0; i < strlen(line); i++) {
7292613b 1691 if (line[i] == ';')
47d45203
JA
1692 return 1;
1693 if (!isspace(line[i]) && !iscntrl(line[i]))
7292613b
JA
1694 return 0;
1695 }
7dd1389e
JA
1696
1697 return 1;
1698}
1699
5c24b2c4 1700static int parse_jobs_ini(char *file)
7dd1389e 1701{
47d45203 1702 unsigned int prioclass, prio, cpu, global;
8c033f93 1703 unsigned long long ull;
7889f07b 1704 unsigned long ul1, ul2;
f737299d 1705 struct thread_data *td;
7dd1389e
JA
1706 char *string, *name;
1707 fpos_t off;
1708 FILE *f;
1709 char *p;
1710
1711 f = fopen(file, "r");
1712 if (!f) {
1713 perror("fopen");
4240cfa1 1714 return 1;
7dd1389e
JA
1715 }
1716
1717 string = malloc(4096);
1718 name = malloc(256);
1719
7dd1389e 1720 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b
JA
1721 if (is_empty_or_comment(p))
1722 continue;
7dd1389e
JA
1723 if (sscanf(p, "[%s]", name) != 1)
1724 continue;
1725
47d45203
JA
1726 global = !strncmp(name, "global", 6);
1727
7dd1389e
JA
1728 name[strlen(name) - 1] = '\0';
1729
47d45203 1730 td = get_new_job(global);
4240cfa1
JA
1731 if (!td)
1732 break;
f737299d 1733
7dd1389e 1734 prioclass = 2;
f737299d 1735 prio = 4;
7dd1389e
JA
1736
1737 fgetpos(f, &off);
1738 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b 1739 if (is_empty_or_comment(p))
e6402082
JA
1740 continue;
1741 if (strstr(p, "["))
7dd1389e 1742 break;
f737299d 1743 if (!check_int(p, "rw", &td->ddir)) {
7dd1389e
JA
1744 fgetpos(f, &off);
1745 continue;
1746 }
1747 if (!check_int(p, "prio", &prio)) {
1748 fgetpos(f, &off);
1749 continue;
1750 }
1751 if (!check_int(p, "prioclass", &prioclass)) {
1752 fgetpos(f, &off);
1753 continue;
1754 }
f737299d 1755 if (!check_int(p, "direct", &td->odirect)) {
7dd1389e
JA
1756 fgetpos(f, &off);
1757 continue;
1758 }
f737299d 1759 if (!check_int(p, "rate", &td->rate)) {
7dd1389e
JA
1760 fgetpos(f, &off);
1761 continue;
1762 }
4240cfa1
JA
1763 if (!check_int(p, "ratemin", &td->ratemin)) {
1764 fgetpos(f, &off);
1765 continue;
1766 }
1767 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1768 fgetpos(f, &off);
1769 continue;
1770 }
e97712ed 1771 if (!check_int(p, "thinktime", &td->thinktime)) {
7dd1389e
JA
1772 fgetpos(f, &off);
1773 continue;
1774 }
18e0b78c 1775 if (!check_int(p, "cpumask", &cpu)) {
f737299d 1776 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1777 fgetpos(f, &off);
1778 continue;
1779 }
4240cfa1
JA
1780 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1781 fgetpos(f, &off);
1782 continue;
1783 }
fc24389f
JA
1784 if (!check_int(p, "startdelay", &td->start_delay)) {
1785 fgetpos(f, &off);
1786 continue;
1787 }
67903a2e
JA
1788 if (!check_int(p, "timeout", &td->timeout)) {
1789 fgetpos(f, &off);
1790 continue;
1791 }
b95799ca
JA
1792 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1793 fgetpos(f, &off);
1794 continue;
1795 }
43000118
JA
1796 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1797 fgetpos(f, &off);
1798 continue;
1799 }
74b4b5fb
JA
1800 if (!check_int(p, "sync", &td->sync_io)) {
1801 fgetpos(f, &off);
1802 continue;
1803 }
1d035750
JA
1804 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
1805 fgetpos(f, &off);
1806 continue;
1807 }
fc097bfe
JA
1808 if (!check_int(p, "create_serialize", &td->create_serialize)) {
1809 fgetpos(f, &off);
1810 continue;
1811 }
1812 if (!check_int(p, "create_fsync", &td->create_fsync)) {
1813 fgetpos(f, &off);
1814 continue;
1815 }
b6794fbf
JA
1816 if (!check_int(p, "loops", &td->loops)) {
1817 fgetpos(f, &off);
1818 continue;
1819 }
e8457004
JA
1820 if (!check_int(p, "verify", &td->verify)) {
1821 fgetpos(f, &off);
1822 continue;
1823 }
7889f07b
JA
1824 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1825 td->min_bs = ul1;
1826 td->max_bs = ul2;
1827 fgetpos(f, &off);
1828 continue;
1829 }
8c033f93
JA
1830 if (!check_strcnv(p, "bs", &ull)) {
1831 td->bs = ull;
1832 fgetpos(f, &off);
1833 continue;
1834 }
02983297
JA
1835 if (!check_strcnv(p, "size", &td->file_size)) {
1836 fgetpos(f, &off);
1837 continue;
1838 }
1839 if (!check_strcnv(p, "offset", &td->file_offset)) {
1840 fgetpos(f, &off);
1841 continue;
1842 }
99c6704f 1843 if (!check_str(p, "mem", "malloc")) {
99c6704f
JA
1844 td->mem_type = MEM_MALLOC;
1845 fgetpos(f, &off);
1846 continue;
1847 }
1848 if (!check_str(p, "mem", "shm")) {
99c6704f
JA
1849 td->mem_type = MEM_SHM;
1850 fgetpos(f, &off);
1851 continue;
1852 }
43000118 1853 if (!strncmp(p, "sequential", 10)) {
f737299d 1854 td->sequential = 1;
7dd1389e
JA
1855 fgetpos(f, &off);
1856 continue;
1857 }
43000118 1858 if (!strncmp(p, "random", 6)) {
f737299d 1859 td->sequential = 0;
7dd1389e
JA
1860 fgetpos(f, &off);
1861 continue;
1862 }
43000118
JA
1863 if (!strncmp(p, "aio", 3)) {
1864 td->use_aio = 1;
1865 fgetpos(f, &off);
1866 continue;
1867 }
02983297
JA
1868 if (!strncmp(p, "create", 6)) {
1869 td->create_file = 1;
1870 fgetpos(f, &off);
1871 continue;
1872 }
1873 if (!strncmp(p, "overwrite", 9)) {
1874 td->overwrite = 1;
1875 fgetpos(f, &off);
1876 continue;
1877 }
98dd52d6
JA
1878 if (!strncmp(p, "exitall", 7)) {
1879 exitall_on_terminate = 1;
1880 fgetpos(f, &off);
1881 continue;
1882 }
e6402082 1883 printf("Client%d: bad option %s\n",td->thread_number,p);
7dd1389e
JA
1884 }
1885 fsetpos(f, &off);
1886
4240cfa1
JA
1887 if (add_job(td, name, prioclass, prio))
1888 put_job(td);
7dd1389e
JA
1889 }
1890
1891 free(string);
1892 free(name);
fc7d63df 1893 fclose(f);
4240cfa1 1894 return 0;
7dd1389e
JA
1895}
1896
5c24b2c4 1897static int parse_options(int argc, char *argv[])
892199bd 1898{
01c4d8de 1899 int i;
892199bd
JA
1900
1901 for (i = 1; i < argc; i++) {
1902 char *parm = argv[i];
1903
1904 if (parm[0] != '-')
1905 break;
1906
1907 parm++;
1908 switch (*parm) {
1909 case 's':
1910 parm++;
47d45203 1911 def_thread.sequential = !!atoi(parm);
892199bd
JA
1912 break;
1913 case 'b':
1914 parm++;
47d45203
JA
1915 def_thread.bs = atoi(parm);
1916 def_thread.bs <<= 10;
1917 if (!def_thread.bs) {
4240cfa1 1918 printf("bad block size\n");
47d45203 1919 def_thread.bs = DEF_BS;
4240cfa1 1920 }
892199bd
JA
1921 break;
1922 case 't':
1923 parm++;
47d45203 1924 def_thread.timeout = atoi(parm);
892199bd 1925 break;
892199bd
JA
1926 case 'r':
1927 parm++;
1928 repeatable = !!atoi(parm);
1929 break;
02bdd9ba
JA
1930 case 'R':
1931 parm++;
1932 rate_quit = !!atoi(parm);
1933 break;
892199bd
JA
1934 case 'o':
1935 parm++;
47d45203 1936 def_thread.odirect = !!atoi(parm);
892199bd 1937 break;
7dd1389e
JA
1938 case 'f':
1939 if (i + 1 >= argc) {
1940 printf("-f needs file as arg\n");
1941 break;
1942 }
1943 ini_file = strdup(argv[i+1]);
a642279f 1944 i++;
7dd1389e 1945 break;
a0a9b35b
JA
1946 case 'l':
1947 write_lat_log = 1;
1948 break;
1949 case 'w':
1950 write_bw_log = 1;
1951 break;
892199bd 1952 default:
7dd1389e 1953 printf("bad option %s\n", argv[i]);
892199bd
JA
1954 break;
1955 }
1956 }
1957
892199bd
JA
1958 return i;
1959}
1960
3f39453a 1961static void print_thread_status(struct thread_data *td, int nr_running,
8dbff0b1 1962 int t_rate, int m_rate)
3f39453a 1963{
3f39453a
JA
1964 printf("Threads now running: %d", nr_running);
1965 if (m_rate || t_rate)
1966 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8dbff0b1
JA
1967 printf(" : [%s]\r", run_str);
1968 fflush(stdout);
3f39453a
JA
1969}
1970
40ef7f64
JA
1971static void check_str_update(struct thread_data *td, int n, int t, int m)
1972{
1973 char c = run_str[td->thread_number - 1];
1974
1975 if (td->runstate == td->old_runstate)
1976 return;
1977
1978 switch (td->runstate) {
1979 case TD_REAPED:
1980 c = '_';
1981 break;
1982 case TD_RUNNING:
1983 c = '+';
1984 break;
1985 case TD_VERIFYING:
1986 c = 'V';
1987 break;
1988 case TD_CREATED:
1989 c = 'C';
1990 break;
1991 case TD_NOT_CREATED:
1992 c = 'P';
1993 break;
1994 default:
1995 printf("state %d\n", td->runstate);
1996 }
1997
1998 run_str[td->thread_number - 1] = c;
1999 print_thread_status(td, n, t, m);
2000 td->old_runstate = td->runstate;
2001}
2002
213b446c 2003static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
02bdd9ba 2004{
213b446c 2005 int i;
02bdd9ba 2006
3f39453a
JA
2007 /*
2008 * reap exited threads (TD_EXITED -> TD_REAPED)
2009 */
02bdd9ba
JA
2010 for (i = 0; i < thread_number; i++) {
2011 struct thread_data *td = &threads[i];
2012
40ef7f64
JA
2013 check_str_update(td, *nr_running, *t_rate, *m_rate);
2014
213b446c
JA
2015 if (td->runstate != TD_EXITED)
2016 continue;
02bdd9ba 2017
40ef7f64 2018 td_set_runstate(td, TD_REAPED);
213b446c
JA
2019 waitpid(td->pid, NULL, 0);
2020 (*nr_running)--;
2021 (*m_rate) -= td->ratemin;
2022 (*t_rate) -= td->rate;
40ef7f64 2023 check_str_update(td, *nr_running, *t_rate, *m_rate);
e6402082
JA
2024
2025 if (td->terminate)
2026 continue;
213b446c 2027 }
02bdd9ba
JA
2028}
2029
fc24389f
JA
2030static void run_threads(char *argv[])
2031{
be33abe4 2032 struct timeval genesis;
fc24389f
JA
2033 struct thread_data *td;
2034 unsigned long spent;
213b446c 2035 int i, todo, nr_running, m_rate, t_rate;
fc24389f 2036
fc24389f
JA
2037 printf("Starting %d threads\n", thread_number);
2038 fflush(stdout);
2039
7292613b
JA
2040 signal(SIGINT, sig_handler);
2041
fc24389f 2042 todo = thread_number;
02bdd9ba 2043 nr_running = 0;
213b446c 2044 m_rate = t_rate = 0;
fc24389f 2045
8bdcfab5
JA
2046 for (i = 0; i < thread_number; i++) {
2047 td = &threads[i];
2048
fc097bfe
JA
2049 if (!td->create_serialize)
2050 continue;
2051
8bdcfab5
JA
2052 /*
2053 * do file setup here so it happens sequentially,
2054 * we don't want X number of threads getting their
2055 * client data interspersed on disk
2056 */
2057 if (setup_file(td)) {
40ef7f64 2058 td_set_runstate(td, TD_REAPED);
8bdcfab5
JA
2059 todo--;
2060 }
2061 }
2062
2063 gettimeofday(&genesis, NULL);
2064
213b446c 2065 while (todo) {
3f39453a
JA
2066 /*
2067 * create threads (TD_NOT_CREATED -> TD_CREATED)
2068 */
fc24389f
JA
2069 for (i = 0; i < thread_number; i++) {
2070 td = &threads[i];
2071
02bdd9ba 2072 if (td->runstate != TD_NOT_CREATED)
fc24389f
JA
2073 continue;
2074
213b446c
JA
2075 /*
2076 * never got a chance to start, killed by other
2077 * thread for some reason
2078 */
2079 if (td->terminate) {
2080 todo--;
2081 continue;
2082 }
2083
fc24389f 2084 if (td->start_delay) {
be33abe4 2085 spent = mtime_since_now(&genesis);
fc24389f
JA
2086
2087 if (td->start_delay * 1000 > spent)
2088 continue;
2089 }
2090
40ef7f64
JA
2091 td_set_runstate(td, TD_CREATED);
2092 check_str_update(td, nr_running, t_rate, m_rate);
fc24389f
JA
2093 sem_init(&startup_sem, 1, 1);
2094 todo--;
2095
2096 if (fork())
2097 sem_wait(&startup_sem);
2098 else {
2099 thread_main(shm_id, i, argv);
2100 exit(0);
2101 }
2102 }
2103
3f39453a 2104 /*
e8457004 2105 * start created threads (TD_CREATED -> TD_RUNNING)
3f39453a 2106 */
fc24389f
JA
2107 for (i = 0; i < thread_number; i++) {
2108 struct thread_data *td = &threads[i];
2109
3f39453a
JA
2110 if (td->runstate != TD_CREATED)
2111 continue;
2112
40ef7f64 2113 td_set_runstate(td, TD_RUNNING);
3f39453a
JA
2114 nr_running++;
2115 m_rate += td->ratemin;
2116 t_rate += td->rate;
40ef7f64 2117 check_str_update(td, nr_running, t_rate, m_rate);
3f39453a 2118 sem_post(&td->mutex);
fc24389f
JA
2119 }
2120
e8457004
JA
2121 for (i = 0; i < thread_number; i++) {
2122 struct thread_data *td = &threads[i];
2123
2124 if (td->runstate == TD_RUNNING)
2125 run_str[td->thread_number - 1] = '+';
2126 else if (td->runstate == TD_VERIFYING)
2127 run_str[td->thread_number - 1] = 'V';
2128 else
2129 continue;
2130
40ef7f64 2131 check_str_update(td, nr_running, t_rate, m_rate);
e8457004
JA
2132 }
2133
213b446c 2134 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba 2135
fc24389f
JA
2136 if (todo)
2137 usleep(100000);
2138 }
02bdd9ba
JA
2139
2140 while (nr_running) {
213b446c 2141 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba
JA
2142 usleep(10000);
2143 }
fc24389f
JA
2144}
2145
8867c0a8 2146int setup_thread_area(void)
892199bd 2147{
8867c0a8
JA
2148 /*
2149 * 1024 is too much on some machines, scale max_jobs if
2150 * we get a failure that looks like too large a shm segment
2151 */
2152 do {
2153 int s = max_jobs * sizeof(struct thread_data);
18e0b78c 2154
8867c0a8
JA
2155 shm_id = shmget(0, s, IPC_CREAT | 0600);
2156 if (shm_id != -1)
2157 break;
2158 if (errno != EINVAL) {
2159 perror("shmget");
2160 break;
2161 }
2162
2163 max_jobs >>= 1;
d4fac444 2164 } while (max_jobs);
8867c0a8
JA
2165
2166 if (shm_id == -1)
892199bd 2167 return 1;
892199bd
JA
2168
2169 threads = shmat(shm_id, NULL, 0);
8867c0a8 2170 if (threads == (void *) -1) {
86184d14
JA
2171 perror("shmat");
2172 return 1;
2173 }
892199bd
JA
2174
2175 atexit(free_shm);
8867c0a8
JA
2176 return 0;
2177}
2178
2179int main(int argc, char *argv[])
2180{
8c033f93 2181 static unsigned long max_run[2], min_run[2];
57d753e3 2182 static unsigned long max_bw[2], min_bw[2];
22334044 2183 static unsigned long io_mb[2], agg[2];
8867c0a8
JA
2184 int i;
2185
2186 if (setup_thread_area())
2187 return 1;
892199bd 2188
47d45203 2189 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
4240cfa1
JA
2190 perror("sched_getaffinity");
2191 return 1;
2192 }
2193
47d45203
JA
2194 /*
2195 * fill globals
2196 */
2197 def_thread.ddir = DDIR_READ;
2198 def_thread.bs = DEF_BS;
7889f07b
JA
2199 def_thread.min_bs = -1;
2200 def_thread.max_bs = -1;
02983297 2201 def_thread.odirect = DEF_ODIRECT;
47d45203 2202 def_thread.ratecycle = DEF_RATE_CYCLE;
02983297 2203 def_thread.sequential = DEF_SEQUENTIAL;
47d45203 2204 def_thread.timeout = DEF_TIMEOUT;
02983297
JA
2205 def_thread.create_file = DEF_CREATE;
2206 def_thread.overwrite = DEF_OVERWRITE;
b95799ca 2207 def_thread.invalidate_cache = DEF_INVALIDATE;
99c6704f
JA
2208 def_thread.sync_io = DEF_SYNCIO;
2209 def_thread.mem_type = MEM_MALLOC;
1d035750 2210 def_thread.bw_avg_time = DEF_BWAVGTIME;
fc097bfe
JA
2211 def_thread.create_serialize = DEF_CREATE_SER;
2212 def_thread.create_fsync = DEF_CREATE_FSYNC;
b6794fbf 2213 def_thread.loops = DEF_LOOPS;
cfc702bd 2214 def_thread.verify = DEF_VERIFY;
47d45203 2215
892199bd 2216 i = parse_options(argc, argv);
7dd1389e 2217
5961d92c
JA
2218 if (!ini_file) {
2219 printf("Need job file\n");
2220 return 1;
2221 }
2222
2223 if (parse_jobs_ini(ini_file))
2224 return 1;
7dd1389e 2225
4240cfa1
JA
2226 if (!thread_number) {
2227 printf("Nothing to do\n");
2228 return 1;
2229 }
7dd1389e 2230
fc24389f 2231 run_threads(argv);
892199bd 2232
892199bd
JA
2233 min_bw[0] = min_run[0] = ~0UL;
2234 min_bw[1] = min_run[1] = ~0UL;
22334044
JA
2235 io_mb[0] = io_mb[1] = 0;
2236 agg[0] = agg[1] = 0;
892199bd
JA
2237 for (i = 0; i < thread_number; i++) {
2238 struct thread_data *td = &threads[i];
2239 unsigned long bw = 0;
2240
22334044
JA
2241 if (!td->error) {
2242 if (td->runtime < min_run[td->ddir])
2243 min_run[td->ddir] = td->runtime;
2244 if (td->runtime > max_run[td->ddir])
2245 max_run[td->ddir] = td->runtime;
892199bd 2246
892199bd 2247 if (td->runtime)
22334044
JA
2248 bw = td->io_kb * 1024 / td->runtime;
2249 if (bw < min_bw[td->ddir])
2250 min_bw[td->ddir] = bw;
2251 if (bw > max_bw[td->ddir])
2252 max_bw[td->ddir] = bw;
2253
2254 io_mb[td->ddir] += td->io_kb >> 10;
892199bd
JA
2255 }
2256
2257 show_thread_status(td);
2258 }
22334044
JA
2259
2260 if (max_run[0])
2261 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2262 if (max_run[1])
2263 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2264
57d753e3 2265 printf("\nRun status:\n");
892199bd 2266 if (max_run[DDIR_READ])
22334044 2267 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 2268 if (max_run[DDIR_WRITE])
22334044 2269 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 2270
892199bd
JA
2271 return 0;
2272}