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