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