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