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