[PATCH] fio: Add start of 'verify' option
[disktools.git] / fio.c
CommitLineData
abe4da87
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
892199bd
JA
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <string.h>
26#include <errno.h>
27#include <signal.h>
28#include <time.h>
7dd1389e 29#include <ctype.h>
18e0b78c 30#include <sched.h>
43000118 31#include <libaio.h>
e128065d 32#include <math.h>
02983297 33#include <limits.h>
892199bd
JA
34#include <sys/time.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/wait.h>
38#include <semaphore.h>
39#include <sys/ipc.h>
40#include <sys/shm.h>
41#include <asm/unistd.h>
42
2c83567e
JA
43#include "list.h"
44
4240cfa1
JA
45#define MAX_JOBS (1024)
46
892199bd
JA
47/*
48 * assume we don't have _get either, if _set isn't defined
49 */
50#ifndef __NR_ioprio_set
892199bd
JA
51#if defined(__i386__)
52#define __NR_ioprio_set 289
53#define __NR_ioprio_get 290
54#elif defined(__powerpc__) || defined(__powerpc64__)
55#define __NR_ioprio_set 273
56#define __NR_ioprio_get 274
57#elif defined(__x86_64__)
58#define __NR_ioprio_set 251
59#define __NR_ioprio_get 252
60#elif defined(__ia64__)
61#define __NR_ioprio_set 1274
62#define __NR_ioprio_get 1275
63#elif defined(__alpha__)
64#define __NR_ioprio_set 442
65#define __NR_ioprio_get 443
66#elif defined(__s390x__) || defined(__s390__)
67#define __NR_ioprio_set 282
68#define __NR_ioprio_get 283
69#else
70#error "Unsupported arch"
71#endif
b95799ca 72#endif
892199bd 73
b95799ca
JA
74#ifndef __NR_fadvise64
75#if defined(__i386__)
76#define __NR_fadvise64 250
77#elif defined(__powerpc__) || defined(__powerpc64__)
78#define __NR_fadvise64 233
79#elif defined(__x86_64__)
80#define __NR_fadvise64 221
81#elif defined(__ia64__)
82#define __NR_fadvise64 1234
83#elif defined(__alpha__)
84#define __NR_fadvise64 413
85#elif defined(__s390x__) || defined(__s390__)
86#define __NR_fadvise64 253
87#else
88#error "Unsupported arch"
89#endif
892199bd
JA
90#endif
91
92static int ioprio_set(int which, int who, int ioprio)
93{
94 return syscall(__NR_ioprio_set, which, who, ioprio);
95}
96
b95799ca
JA
97/*
98 * we want fadvise64 really, but it's so tangled... later
99 */
100static int fadvise(int fd, loff_t offset, size_t len, int advice)
101{
102#if 0
103 return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice);
104#else
105 return posix_fadvise(fd, (off_t) offset, len, advice);
106#endif
107}
108
892199bd
JA
109enum {
110 IOPRIO_WHO_PROCESS = 1,
111 IOPRIO_WHO_PGRP,
112 IOPRIO_WHO_USER,
113};
114
115#define IOPRIO_CLASS_SHIFT 13
116
892199bd
JA
117#define MASK (4095)
118
4240cfa1
JA
119#define DEF_BS (4096)
120#define DEF_TIMEOUT (30)
121#define DEF_RATE_CYCLE (1000)
122#define DEF_ODIRECT (1)
123#define DEF_SEQUENTIAL (1)
4240cfa1 124#define DEF_RAND_REPEAT (1)
02983297
JA
125#define DEF_OVERWRITE (0)
126#define DEF_CREATE (1)
b95799ca 127#define DEF_INVALIDATE (1)
99c6704f 128#define DEF_SYNCIO (0)
eb39963f 129#define DEF_RANDSEED (0xb1899bedUL)
1d035750 130#define DEF_BWAVGTIME (500)
fc097bfe
JA
131#define DEF_CREATE_SER (1)
132#define DEF_CREATE_FSYNC (1)
b6794fbf 133#define DEF_LOOPS (1)
cfc702bd 134#define DEF_VERIFY (0)
4240cfa1
JA
135
136#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
892199bd 137
4240cfa1 138static int repeatable = DEF_RAND_REPEAT;
02bdd9ba 139static int rate_quit = 1;
a0a9b35b
JA
140static int write_lat_log;
141static int write_bw_log;
98dd52d6 142static int exitall_on_terminate;
892199bd 143
892199bd 144static int thread_number;
7dd1389e 145static char *ini_file;
892199bd 146
8867c0a8
JA
147static int max_jobs = MAX_JOBS;
148
8dbff0b1
JA
149static char run_str[MAX_JOBS + 1];
150
892199bd
JA
151static int shm_id;
152
4240cfa1
JA
153enum {
154 DDIR_READ = 0,
155 DDIR_WRITE,
156};
892199bd 157
02bdd9ba
JA
158/*
159 * thread life cycle
160 */
161enum {
162 TD_NOT_CREATED = 0,
163 TD_CREATED,
164 TD_STARTED,
165 TD_EXITED,
166 TD_REAPED,
167};
168
99c6704f
JA
169enum {
170 MEM_MALLOC,
171 MEM_SHM,
172};
173
2c83567e
JA
174/*
175 * The io unit
176 */
177struct io_u {
178 struct iocb iocb;
57d753e3 179 struct timeval start_time;
2c83567e
JA
180 struct timeval issue_time;
181
2c83567e
JA
182 char *buf;
183 unsigned int buflen;
4ac89145 184 unsigned long long offset;
2c83567e
JA
185
186 struct list_head list;
187};
188
57d753e3
JA
189struct io_stat {
190 unsigned long val;
191 unsigned long val_sq;
192 unsigned long max_val;
193 unsigned long min_val;
194 unsigned long samples;
195};
196
a0a9b35b
JA
197struct io_sample {
198 unsigned long time;
199 unsigned long val;
200};
201
202struct io_log {
203 unsigned long nr_samples;
204 unsigned long max_samples;
205 struct io_sample *log;
206};
207
02983297
JA
208#define td_read(td) ((td)->ddir == DDIR_READ)
209#define should_fsync(td) (!td_read(td) && !(td)->odirect)
210
892199bd
JA
211struct thread_data {
212 char file_name[256];
213 int thread_number;
214 int error;
215 int fd;
892199bd 216 pid_t pid;
6b71c826 217 char *orig_buffer;
4240cfa1 218 volatile int terminate;
02bdd9ba 219 volatile int runstate;
f737299d
JA
220 unsigned int ddir;
221 unsigned int ioprio;
222 unsigned int sequential;
223 unsigned int bs;
8c033f93
JA
224 unsigned int min_bs;
225 unsigned int max_bs;
f737299d 226 unsigned int odirect;
e97712ed 227 unsigned int thinktime;
4240cfa1 228 unsigned int fsync_blocks;
fc24389f 229 unsigned int start_delay;
47d45203 230 unsigned int timeout;
43000118 231 unsigned int use_aio;
02983297
JA
232 unsigned int create_file;
233 unsigned int overwrite;
b95799ca 234 unsigned int invalidate_cache;
1d035750 235 unsigned int bw_avg_time;
fc097bfe
JA
236 unsigned int create_serialize;
237 unsigned int create_fsync;
b6794fbf 238 unsigned int loops;
02983297
JA
239 unsigned long long file_size;
240 unsigned long long file_offset;
74b4b5fb 241 unsigned int sync_io;
99c6704f 242 unsigned int mem_type;
cfc702bd 243 unsigned int verify;
18e0b78c 244 cpu_set_t cpumask;
86184d14 245
7889f07b
JA
246 struct drand48_data bsrange_state;
247
99c6704f
JA
248 int shm_id;
249
63a09e51
JA
250 off_t cur_off;
251
254605cd 252 io_context_t aio_ctx;
43000118 253 unsigned int aio_depth;
43000118 254 struct io_event *aio_events;
2c83567e
JA
255
256 unsigned int cur_depth;
257 struct list_head io_u_freelist;
258 struct list_head io_u_busylist;
43000118 259
7dd1389e 260 unsigned int rate;
4240cfa1
JA
261 unsigned int ratemin;
262 unsigned int ratecycle;
263 unsigned long rate_usec_cycle;
264 long rate_pending_usleep;
8c033f93 265 unsigned long rate_kb;
4240cfa1 266 struct timeval lastrate;
86184d14 267
892199bd 268 unsigned long runtime; /* sec */
7889f07b 269 unsigned long kb;
4240cfa1 270 unsigned long io_blocks;
8c033f93 271 unsigned long io_kb;
7889f07b 272 unsigned long last_kb;
892199bd 273 sem_t mutex;
892199bd
JA
274 struct drand48_data random_state;
275
276 /*
e128065d 277 * bandwidth and latency stats
892199bd 278 */
57d753e3
JA
279 struct io_stat clat_stat; /* completion latency */
280 struct io_stat slat_stat; /* submission latency */
281
282 struct io_stat bw_stat; /* bandwidth stats */
8c033f93 283 unsigned long stat_io_kb;
fd1ae4c9 284 struct timeval stat_sample_time;
4240cfa1 285
a0a9b35b
JA
286 struct io_log *lat_log;
287 struct io_log *bw_log;
288
4240cfa1 289 struct timeval start;
892199bd
JA
290};
291
292static struct thread_data *threads;
47d45203 293static struct thread_data def_thread;
892199bd
JA
294
295static sem_t startup_sem;
296
5c24b2c4 297static void sig_handler(int sig)
892199bd
JA
298{
299 int i;
300
213b446c
JA
301 for (i = 0; i < thread_number; i++) {
302 struct thread_data *td = &threads[i];
303
304 td->terminate = 1;
305 td->start_delay = 0;
306 }
02bdd9ba
JA
307}
308
5c24b2c4 309static int init_random_state(struct thread_data *td)
892199bd 310{
7889f07b
JA
311 unsigned long seed = DEF_RANDSEED;
312
313 srand48_r(seed, &td->bsrange_state);
892199bd
JA
314
315 if (td->sequential)
316 return 0;
317
318 if (!repeatable) {
319 int fd = open("/dev/random", O_RDONLY);
320
321 if (fd == -1) {
322 td->error = errno;
323 return 1;
324 }
325
7dd1389e 326 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
892199bd
JA
327 td->error = EIO;
328 close(fd);
329 return 1;
330 }
331
332 close(fd);
7889f07b 333 }
892199bd
JA
334
335 srand48_r(seed, &td->random_state);
336 return 0;
337}
338
5c24b2c4 339static unsigned long utime_since(struct timeval *s, struct timeval *e)
892199bd
JA
340{
341 double sec, usec;
342
343 sec = e->tv_sec - s->tv_sec;
344 usec = e->tv_usec - s->tv_usec;
345 if (sec > 0 && usec < 0) {
346 sec--;
347 usec += 1000000;
348 }
349
350 sec *= (double) 1000000;
351
352 return sec + usec;
353}
354
5c24b2c4 355static unsigned long mtime_since(struct timeval *s, struct timeval *e)
892199bd
JA
356{
357 double sec, usec;
358
359 sec = e->tv_sec - s->tv_sec;
360 usec = e->tv_usec - s->tv_usec;
361 if (sec > 0 && usec < 0) {
362 sec--;
363 usec += 1000000;
364 }
365
366 sec *= (double) 1000;
367 usec /= (double) 1000;
368
369 return sec + usec;
370}
371
be33abe4
JA
372static unsigned long mtime_since_now(struct timeval *s)
373{
374 struct timeval t;
375
376 gettimeofday(&t, NULL);
377 return mtime_since(s, &t);
378}
379
98168d55
JA
380static inline unsigned long msec_now(struct timeval *s)
381{
382 return s->tv_sec * 1000 + s->tv_usec / 1000;
383}
384
4ac89145 385static unsigned long long get_next_offset(struct thread_data *td)
892199bd 386{
f31fd9c8 387 unsigned long long kb;
892199bd
JA
388 long r;
389
390 if (!td->sequential) {
391 lrand48_r(&td->random_state, &r);
f31fd9c8 392 kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
7889f07b 393 } else
f31fd9c8 394 kb = td->last_kb;
7889f07b 395
f31fd9c8 396 return (kb << 10) + td->file_offset;
7889f07b
JA
397}
398
399static unsigned int get_next_buflen(struct thread_data *td)
400{
401 unsigned int buflen;
402 long r;
403
404 if (td->min_bs == td->max_bs)
405 buflen = td->min_bs;
406 else {
407 lrand48_r(&td->bsrange_state, &r);
408 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
409 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
892199bd
JA
410 }
411
7889f07b
JA
412 if (buflen > ((td->kb - td->io_kb) << 10))
413 buflen = (td->kb - td->io_kb) << 10;
414
415 td->last_kb += buflen >> 10;
416 return buflen;
892199bd
JA
417}
418
57d753e3
JA
419static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
420 unsigned long val)
892199bd 421{
57d753e3
JA
422 if (val > is->max_val)
423 is->max_val = val;
424 if (val < is->min_val)
425 is->min_val = val;
426
427 is->val += val;
428 is->val_sq += val * val;
429 is->samples++;
430}
fd1ae4c9 431
a0a9b35b
JA
432static void add_log_sample(struct thread_data *td, struct io_log *log,
433 unsigned long val)
434{
435 if (log->nr_samples == log->max_samples) {
436 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
437
438 log->log = realloc(log->log, new_size);
439 log->max_samples <<= 1;
440 }
441
442 log->log[log->nr_samples].val = val;
443 log->log[log->nr_samples].time = mtime_since_now(&td->start);
444 log->nr_samples++;
445}
446
57d753e3
JA
447static void add_clat_sample(struct thread_data *td, unsigned long msec)
448{
449 add_stat_sample(td, &td->clat_stat, msec);
a0a9b35b
JA
450
451 if (td->lat_log)
452 add_log_sample(td, td->lat_log, msec);
57d753e3 453}
fd1ae4c9 454
57d753e3
JA
455static void add_slat_sample(struct thread_data *td, unsigned long msec)
456{
457 add_stat_sample(td, &td->slat_stat, msec);
458}
fd1ae4c9 459
57d753e3
JA
460static void add_bw_sample(struct thread_data *td, unsigned long msec)
461{
462 unsigned long spent = mtime_since_now(&td->stat_sample_time);
463 unsigned long rate;
464
1d035750 465 if (spent < td->bw_avg_time)
57d753e3
JA
466 return;
467
7889f07b 468 rate = ((td->io_kb - td->stat_io_kb) * 1024) / spent;
57d753e3
JA
469 add_stat_sample(td, &td->bw_stat, rate);
470
a0a9b35b
JA
471 if (td->bw_log)
472 add_log_sample(td, td->bw_log, rate);
473
57d753e3 474 gettimeofday(&td->stat_sample_time, NULL);
8c033f93 475 td->stat_io_kb = td->io_kb;
892199bd
JA
476}
477
5c24b2c4 478static void usec_sleep(int usec)
892199bd 479{
86184d14
JA
480 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
481 struct timespec rem;
892199bd
JA
482
483 do {
86184d14
JA
484 rem.tv_sec = rem.tv_nsec = 0;
485 nanosleep(&req, &rem);
486 if (!rem.tv_nsec)
892199bd 487 break;
86184d14
JA
488
489 req.tv_nsec = rem.tv_nsec;
892199bd
JA
490 } while (1);
491}
492
9e850933
JA
493static void rate_throttle(struct thread_data *td, unsigned long time_spent,
494 unsigned int bytes)
86184d14 495{
9e850933
JA
496 unsigned long usec_cycle;
497
4240cfa1
JA
498 if (!td->rate)
499 return;
500
9e850933
JA
501 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
502
503 if (time_spent < usec_cycle) {
504 unsigned long s = usec_cycle - time_spent;
86184d14
JA
505
506 td->rate_pending_usleep += s;
fad86e6a 507 if (td->rate_pending_usleep >= 100000) {
86184d14
JA
508 usec_sleep(td->rate_pending_usleep);
509 td->rate_pending_usleep = 0;
510 }
4240cfa1 511 } else {
9e850933 512 long overtime = time_spent - usec_cycle;
42b2b9fe 513
4240cfa1
JA
514 td->rate_pending_usleep -= overtime;
515 }
516}
517
5c24b2c4 518static int check_min_rate(struct thread_data *td, struct timeval *now)
4240cfa1 519{
7607bc6b 520 unsigned long spent;
4240cfa1
JA
521 unsigned long rate;
522
523 /*
524 * allow a 2 second settle period in the beginning
525 */
7607bc6b 526 if (mtime_since(&td->start, now) < 2000)
4240cfa1
JA
527 return 0;
528
529 /*
530 * if rate blocks is set, sample is running
531 */
8c033f93 532 if (td->rate_kb) {
4240cfa1
JA
533 spent = mtime_since(&td->lastrate, now);
534 if (spent < td->ratecycle)
535 return 0;
536
7889f07b 537 rate = ((td->io_kb - td->rate_kb) * 1024) / spent;
4240cfa1
JA
538 if (rate < td->ratemin) {
539 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
02bdd9ba 540 if (rate_quit)
e6402082 541 sig_handler(0);
4240cfa1
JA
542 return 1;
543 }
86184d14 544 }
4240cfa1 545
8c033f93 546 td->rate_kb = td->io_kb;
4240cfa1
JA
547 memcpy(&td->lastrate, now, sizeof(*now));
548 return 0;
86184d14
JA
549}
550
67903a2e
JA
551static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
552{
553 if (mtime_since(&td->start, t) >= td->timeout * 1000)
554 return 1;
555
556 return 0;
557}
558
cfc702bd
JA
559/*
560 * fill body of io_u->buf with random data and add a header with the
561 * (eg) sha1sum of that data.
562 */
563static void populate_io_u(struct io_u *io_u)
564{
565}
566
2c83567e
JA
567static void put_io_u(struct thread_data *td, struct io_u *io_u)
568{
569 list_del(&io_u->list);
570 list_add(&io_u->list, &td->io_u_freelist);
571 td->cur_depth--;
572}
573
574static struct io_u *get_io_u(struct thread_data *td)
575{
576 struct io_u *io_u;
7889f07b
JA
577 unsigned int len;
578 unsigned long long off;
2c83567e
JA
579
580 if (list_empty(&td->io_u_freelist))
581 return NULL;
582
7889f07b
JA
583 off = get_next_offset(td);
584 len = get_next_buflen(td);
585 if (!len)
586 return NULL;
587
2c83567e
JA
588 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
589 list_del(&io_u->list);
590 list_add(&io_u->list, &td->io_u_busylist);
591
7889f07b
JA
592 io_u->offset = off;
593 io_u->buflen = len;
2c83567e 594
cfc702bd
JA
595 if (!td_read(td) && td->verify)
596 populate_io_u(io_u);
597
2c83567e
JA
598 if (td->use_aio) {
599 if (td_read(td))
600 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
601 else
602 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
603 }
604
57d753e3 605 gettimeofday(&io_u->start_time, NULL);
2c83567e
JA
606 td->cur_depth++;
607 return io_u;
608}
609
cfc702bd
JA
610static int do_sync_verify(struct thread_data *td)
611{
612 return 0;
613}
614
43000118 615static void do_sync_io(struct thread_data *td)
892199bd 616{
7889f07b 617 unsigned long msec, usec;
2c83567e 618 struct timeval e;
892199bd 619
63a09e51
JA
620 td->cur_off = 0;
621
7889f07b 622 for (td->io_kb = 0; td->io_kb < td->kb;) {
2c83567e 623 struct io_u *io_u;
892199bd
JA
624 int ret;
625
626 if (td->terminate)
627 break;
628
2c83567e 629 io_u = get_io_u(td);
7889f07b
JA
630 if (!io_u)
631 break;
2c83567e 632
63a09e51
JA
633 if (td->cur_off != io_u->offset) {
634 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
635 td->error = errno;
636 break;
637 }
892199bd
JA
638 }
639
02983297 640 if (td_read(td))
2c83567e 641 ret = read(td->fd, io_u->buf, io_u->buflen);
892199bd 642 else
2c83567e 643 ret = write(td->fd, io_u->buf, io_u->buflen);
892199bd 644
2c83567e 645 if (ret < (int) io_u->buflen) {
892199bd
JA
646 if (ret == -1)
647 td->error = errno;
648 break;
649 }
650
4240cfa1 651 td->io_blocks++;
8c033f93 652 td->io_kb += io_u->buflen >> 10;
63a09e51 653 td->cur_off = io_u->offset + io_u->buflen;
4240cfa1 654
86184d14
JA
655 gettimeofday(&e, NULL);
656
57d753e3 657 usec = utime_since(&io_u->start_time, &e);
86184d14 658
9e850933 659 rate_throttle(td, usec, io_u->buflen);
892199bd 660
4240cfa1
JA
661 if (check_min_rate(td, &e)) {
662 td->error = ENODATA;
663 break;
664 }
892199bd 665
4240cfa1 666 msec = usec / 1000;
57d753e3
JA
667 add_clat_sample(td, msec);
668 add_bw_sample(td, msec);
67903a2e
JA
669
670 if (runtime_exceeded(td, &e))
671 break;
2c83567e 672
cdf92433
JA
673 put_io_u(td, io_u);
674
e97712ed
JA
675 if (td->thinktime)
676 usec_sleep(td->thinktime);
677
cdf92433
JA
678 if (should_fsync(td) && td->fsync_blocks &&
679 (td->io_blocks % td->fsync_blocks) == 0)
680 fsync(td->fd);
892199bd
JA
681 }
682
4240cfa1 683 if (should_fsync(td))
892199bd 684 fsync(td->fd);
892199bd 685}
43000118 686
2c83567e 687static int io_u_queue(struct thread_data *td, struct io_u *io_u)
56b0eff0 688{
2c83567e 689 struct iocb *iocb = &io_u->iocb;
56b0eff0
JA
690 int ret;
691
692 do {
254605cd 693 ret = io_submit(td->aio_ctx, 1, &iocb);
56b0eff0
JA
694 if (ret == 1)
695 return 0;
a592bd33 696 else if (ret == EAGAIN)
56b0eff0 697 usleep(100);
a592bd33
JA
698 else if (ret == EINTR)
699 continue;
56b0eff0
JA
700 else
701 break;
702 } while (1);
703
a592bd33 704 return ret;
56b0eff0
JA
705}
706
98168d55 707#define iocb_time(iocb) ((unsigned long) (iocb)->data)
2c83567e
JA
708#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
709
9e850933 710static int ios_completed(struct thread_data *td, int nr)
2c83567e
JA
711{
712 unsigned long msec;
713 struct io_u *io_u;
714 struct timeval e;
9e850933 715 int i, bytes_done;
2c83567e
JA
716
717 gettimeofday(&e, NULL);
718
9e850933 719 for (i = 0, bytes_done = 0; i < nr; i++) {
2c83567e
JA
720 io_u = ev_to_iou(td->aio_events + i);
721
8c033f93
JA
722 td->io_blocks++;
723 td->io_kb += io_u->buflen >> 10;
9e850933 724 bytes_done += io_u->buflen;
8c033f93 725
2c83567e
JA
726 msec = mtime_since(&io_u->issue_time, &e);
727
57d753e3
JA
728 add_clat_sample(td, msec);
729 add_bw_sample(td, msec);
2c83567e
JA
730
731 put_io_u(td, io_u);
732 }
9e850933
JA
733
734 return bytes_done;
2c83567e
JA
735}
736
737static void cleanup_pending_aio(struct thread_data *td)
738{
739 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
740 struct list_head *entry, *n;
741 struct io_u *io_u;
742 int r;
743
744 /*
745 * get immediately available events, if any
746 */
747 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
748 if (r > 0)
749 ios_completed(td, r);
750
751 /*
752 * now cancel remaining active events
753 */
754 list_for_each_safe(entry, n, &td->io_u_busylist) {
755 io_u = list_entry(entry, struct io_u, list);
756
757 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
758 if (!r)
759 put_io_u(td, io_u);
760 }
761
762 if (td->cur_depth) {
763 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
764 if (r > 0)
765 ios_completed(td, r);
766 }
767}
98168d55 768
cfc702bd
JA
769static int do_async_verify(struct thread_data *td)
770{
771 return 0;
772}
773
43000118
JA
774static void do_async_io(struct thread_data *td)
775{
776 struct timeval s, e;
7889f07b 777 unsigned long usec;
43000118 778
7889f07b 779 for (td->io_kb = 0; td->io_kb < td->kb;) {
43000118
JA
780 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
781 struct timespec *timeout;
2c83567e
JA
782 int ret, min_evts = 0;
783 struct io_u *io_u;
9e850933 784 unsigned int bytes_done;
43000118
JA
785
786 if (td->terminate)
787 break;
788
2c83567e 789 io_u = get_io_u(td);
7889f07b
JA
790 if (!io_u)
791 break;
43000118 792
57d753e3 793 memcpy(&s, &io_u->start_time, sizeof(s));
8baf1bcc 794
2c83567e 795 ret = io_u_queue(td, io_u);
56b0eff0 796 if (ret) {
a3fdb993 797 put_io_u(td, io_u);
a592bd33 798 td->error = ret;
43000118
JA
799 break;
800 }
801
57d753e3
JA
802 gettimeofday(&io_u->issue_time, NULL);
803 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
2c83567e 804 if (td->cur_depth < td->aio_depth) {
43000118
JA
805 timeout = &ts;
806 min_evts = 0;
807 } else {
808 timeout = NULL;
809 min_evts = 1;
810 }
811
2c83567e 812 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
43000118
JA
813 if (ret < 0) {
814 td->error = errno;
815 break;
816 } else if (!ret)
817 continue;
818
9e850933 819 bytes_done = ios_completed(td, ret);
43000118 820
98168d55
JA
821 /*
822 * the rate is batched for now, it should work for batches
823 * of completions except the very first one which may look
824 * a little bursty
825 */
2c83567e 826 gettimeofday(&e, NULL);
43000118
JA
827 usec = utime_since(&s, &e);
828
9e850933 829 rate_throttle(td, usec, bytes_done);
43000118
JA
830
831 if (check_min_rate(td, &e)) {
832 td->error = ENODATA;
833 break;
834 }
67903a2e
JA
835
836 if (runtime_exceeded(td, &e))
837 break;
765d9223
JA
838
839 if (td->thinktime)
840 usec_sleep(td->thinktime);
cdf92433
JA
841
842 if (should_fsync(td) && td->fsync_blocks &&
843 (td->io_blocks % td->fsync_blocks) == 0)
844 fsync(td->fd);
43000118 845 }
56b0eff0 846
2c83567e
JA
847 if (td->cur_depth)
848 cleanup_pending_aio(td);
4ac89145
JA
849
850 if (should_fsync(td))
851 fsync(td->fd);
56b0eff0
JA
852}
853
854static void cleanup_aio(struct thread_data *td)
855{
254605cd
JA
856 io_destroy(td->aio_ctx);
857
43000118
JA
858 if (td->aio_events)
859 free(td->aio_events);
43000118
JA
860}
861
862static int init_aio(struct thread_data *td)
863{
254605cd 864 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
43000118
JA
865 td->error = errno;
866 return 1;
867 }
868
43000118 869 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
43000118
JA
870 return 0;
871}
872
2c83567e
JA
873static void cleanup_io_u(struct thread_data *td)
874{
875 struct list_head *entry, *n;
876 struct io_u *io_u;
877
878 list_for_each_safe(entry, n, &td->io_u_freelist) {
879 io_u = list_entry(entry, struct io_u, list);
880
881 list_del(&io_u->list);
2c83567e
JA
882 free(io_u);
883 }
6b71c826 884
99c6704f
JA
885 if (td->mem_type == MEM_MALLOC)
886 free(td->orig_buffer);
887 else if (td->mem_type == MEM_SHM) {
888 struct shmid_ds sbuf;
889
890 shmdt(td->orig_buffer);
891 shmctl(td->shm_id, IPC_RMID, &sbuf);
892 }
2c83567e
JA
893}
894
99c6704f 895static int init_io_u(struct thread_data *td)
2c83567e
JA
896{
897 struct io_u *io_u;
99c6704f 898 int i, max_units, mem_size;
6b71c826 899 char *p;
2c83567e
JA
900
901 if (!td->use_aio)
902 max_units = 1;
903 else
904 max_units = td->aio_depth;
905
7889f07b 906 mem_size = td->max_bs * max_units + MASK;
99c6704f
JA
907
908 if (td->mem_type == MEM_MALLOC)
909 td->orig_buffer = malloc(mem_size);
910 else if (td->mem_type == MEM_SHM) {
911 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
912 if (td->shm_id < 0) {
913 td->error = errno;
914 perror("shmget");
915 return 1;
916 }
917
918 td->orig_buffer = shmat(td->shm_id, NULL, 0);
919 if (td->orig_buffer == (void *) -1) {
920 td->error = errno;
921 perror("shmat");
922 return 1;
923 }
924 }
6b71c826 925
2c83567e
JA
926 INIT_LIST_HEAD(&td->io_u_freelist);
927 INIT_LIST_HEAD(&td->io_u_busylist);
928
99c6704f 929 p = ALIGN(td->orig_buffer);
2c83567e
JA
930 for (i = 0; i < max_units; i++) {
931 io_u = malloc(sizeof(*io_u));
932 memset(io_u, 0, sizeof(*io_u));
933 INIT_LIST_HEAD(&io_u->list);
934
7889f07b 935 io_u->buf = p + td->max_bs * i;
2c83567e
JA
936 list_add(&io_u->list, &td->io_u_freelist);
937 }
99c6704f
JA
938
939 return 0;
2c83567e
JA
940}
941
a0a9b35b
JA
942static void setup_log(struct io_log **log)
943{
944 struct io_log *l = malloc(sizeof(*l));
945
946 l->nr_samples = 0;
947 l->max_samples = 1024;
948 l->log = malloc(l->max_samples * sizeof(struct io_sample));
949 *log = l;
950}
951
952static void finish_log(struct thread_data *td, struct io_log *log, char *name)
953{
954 char file_name[128];
955 FILE *f;
956 int i;
957
958 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
959 f = fopen(file_name, "w");
960 if (!f) {
961 perror("fopen log");
962 return;
963 }
964
965 for (i = 0; i < log->nr_samples; i++)
966 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
967
968 fclose(f);
969 free(log->log);
970 free(log);
971}
972
02983297
JA
973static int create_file(struct thread_data *td)
974{
7889f07b 975 unsigned long long left;
02983297 976 char *b;
7889f07b 977 int r, bs;
02983297 978
02983297
JA
979 /*
980 * unless specifically asked for overwrite, let normal io extend it
981 */
982 if (!td_read(td) && !td->overwrite)
983 return 0;
984
57d753e3
JA
985 if (!td->file_size) {
986 fprintf(stderr, "Need size for create\n");
987 td->error = EINVAL;
988 return 1;
989 }
990
42fd89a7
JA
991 printf("Client%d: Laying out IO file\n", td->thread_number);
992
02983297
JA
993 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
994 if (td->fd < 0) {
995 td->error = errno;
996 return 1;
997 }
998
7889f07b
JA
999 td->kb = td->file_size >> 10;
1000 b = malloc(td->max_bs);
1001 memset(b, 0, td->max_bs);
1002
1003 left = td->file_size;
1004 while (left) {
1005 bs = td->max_bs;
1006 if (bs > left)
1007 bs = left;
02983297 1008
7889f07b 1009 r = write(td->fd, b, bs);
02983297 1010
7889f07b
JA
1011 if (r == bs) {
1012 left -= bs;
02983297 1013 continue;
7889f07b 1014 } else {
02983297
JA
1015 if (r < 0)
1016 td->error = errno;
1017 else
1018 td->error = EIO;
1019
1020 break;
1021 }
1022 }
1023
fc097bfe
JA
1024 if (td->create_fsync)
1025 fsync(td->fd);
1026
02983297
JA
1027 close(td->fd);
1028 td->fd = -1;
1029 free(b);
1030 return 0;
1031}
1032
1033static int file_exists(struct thread_data *td)
1034{
1035 struct stat st;
1036
1037 if (stat(td->file_name, &st) != -1)
1038 return 1;
1039
1040 return errno != ENOENT;
1041}
1042
1043static int setup_file(struct thread_data *td)
1044{
1045 struct stat st;
1046 int flags = 0;
1047
1048 if (!file_exists(td)) {
1049 if (!td->create_file) {
1050 td->error = ENOENT;
1051 return 1;
1052 }
1053 if (create_file(td))
1054 return 1;
1055 }
1056
1057 if (td->odirect)
1058 flags |= O_DIRECT;
1059
1060 if (td_read(td))
1061 td->fd = open(td->file_name, flags | O_RDONLY);
1062 else {
1063 if (!td->overwrite)
1064 flags |= O_TRUNC;
74b4b5fb
JA
1065 if (td->sync_io)
1066 flags |= O_SYNC;
02983297
JA
1067
1068 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT, 0600);
1069 }
1070
1071 if (td->fd == -1) {
1072 td->error = errno;
1073 return 1;
1074 }
1075
1076 if (td_read(td)) {
1077 if (fstat(td->fd, &st) == -1) {
1078 td->error = errno;
1079 return 1;
1080 }
1081
1082 if (td->file_size > st.st_size)
1083 st.st_size = td->file_size;
1084 } else {
1085 if (!td->file_size)
1086 td->file_size = 1024 * 1024 * 1024;
1087
1088 st.st_size = td->file_size;
1089 }
1090
7889f07b
JA
1091 td->kb = (st.st_size - td->file_offset) / 1024;
1092 if (!td->kb) {
02983297
JA
1093 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1094 td->error = EINVAL;
1095 return 1;
1096 }
1097
b95799ca
JA
1098 if (td->invalidate_cache) {
1099 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1100 td->error = errno;
1101 return 1;
1102 }
1103 }
1104
02983297
JA
1105 return 0;
1106}
1107
5c24b2c4 1108static void *thread_main(int shm_id, int offset, char *argv[])
892199bd
JA
1109{
1110 struct thread_data *td;
02983297 1111 int ret = 1;
2c83567e 1112 void *data;
892199bd 1113
7292613b
JA
1114 setsid();
1115
892199bd 1116 data = shmat(shm_id, NULL, 0);
4ac89145
JA
1117 if (data == (void *) -1) {
1118 perror("shmat");
1119 return NULL;
1120 }
1121
892199bd
JA
1122 td = data + offset * sizeof(struct thread_data);
1123 td->pid = getpid();
1124
99c6704f
JA
1125 if (init_io_u(td))
1126 goto err;
2c83567e 1127
18e0b78c
JA
1128 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1129 td->error = errno;
1130 goto err;
1131 }
1132
4240cfa1 1133 sprintf(argv[0], "fio%d", offset);
892199bd 1134
43000118
JA
1135 if (td->use_aio && init_aio(td))
1136 goto err;
1137
892199bd 1138 if (init_random_state(td))
599002b3 1139 goto err;
892199bd 1140
f737299d 1141 if (td->ioprio) {
892199bd
JA
1142 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1143 td->error = errno;
599002b3 1144 goto err;
892199bd
JA
1145 }
1146 }
1147
1148 sem_post(&startup_sem);
1149 sem_wait(&td->mutex);
43000118 1150
fc097bfe
JA
1151 if (!td->create_serialize && setup_file(td))
1152 goto err;
1153
7292613b
JA
1154 gettimeofday(&td->start, NULL);
1155
1156 if (td->ratemin)
1157 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1158
fd1ae4c9
JA
1159 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1160
b6794fbf 1161 while (td->loops--) {
cfc702bd 1162 if (!td->use_aio) {
b6794fbf 1163 do_sync_io(td);
cfc702bd
JA
1164
1165 if (td->verify) {
1166 if (do_sync_verify(td))
1167 break;
1168 }
1169 } else {
b6794fbf 1170 do_async_io(td);
cfc702bd
JA
1171
1172 if (td->verify) {
1173 if (do_async_verify(td))
1174 break;
1175 }
1176 }
b6794fbf 1177 }
7292613b 1178
be33abe4 1179 td->runtime = mtime_since_now(&td->start);
892199bd 1180 ret = 0;
a0a9b35b
JA
1181
1182 if (td->bw_log)
1183 finish_log(td, td->bw_log, "bw");
1184 if (td->lat_log)
1185 finish_log(td, td->lat_log, "lat");
4ac89145 1186
98dd52d6
JA
1187 if (exitall_on_terminate)
1188 sig_handler(0);
1189
892199bd 1190err:
7292613b
JA
1191 if (td->fd != -1) {
1192 close(td->fd);
1193 td->fd = -1;
1194 }
4ac89145
JA
1195 if (td->use_aio)
1196 cleanup_aio(td);
2c83567e 1197 cleanup_io_u(td);
599002b3 1198 if (ret) {
892199bd 1199 sem_post(&startup_sem);
599002b3
JA
1200 sem_wait(&td->mutex);
1201 }
02bdd9ba 1202 td->runstate = TD_EXITED;
4240cfa1 1203 shmdt(data);
892199bd
JA
1204 return NULL;
1205}
1206
5c24b2c4 1207static void free_shm(void)
892199bd 1208{
c269123b
JA
1209 struct shmid_ds sbuf;
1210
1211 if (threads) {
1212 shmdt(threads);
1213 threads = NULL;
1214 shmctl(shm_id, IPC_RMID, &sbuf);
1215 }
892199bd
JA
1216}
1217
57d753e3
JA
1218static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1219 double *mean, double *dev)
1220{
1221 double n;
1222
1223 if (is->samples == 0)
1224 return 0;
1225
1226 *min = is->min_val;
1227 *max = is->max_val;
1228
1229 n = (double) is->samples;
1230 *mean = (double) is->val / n;
1231 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1232 return 1;
1233}
1234
5c24b2c4 1235static void show_thread_status(struct thread_data *td)
892199bd
JA
1236{
1237 int prio, prio_class;
57d753e3
JA
1238 unsigned long min, max, bw = 0;
1239 double mean, dev;
892199bd 1240
8c033f93 1241 if (!td->io_kb && !td->error)
213b446c
JA
1242 return;
1243
892199bd 1244 if (td->runtime)
7889f07b 1245 bw = td->io_kb * 1024 / td->runtime;
892199bd
JA
1246
1247 prio = td->ioprio & 0xff;
1248 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1249
396fe7cd 1250 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->error, td->io_kb >> 10, bw, td->runtime);
fd1ae4c9 1251
57d753e3
JA
1252 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1253 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1254 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1255 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1256 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1257 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
892199bd
JA
1258}
1259
5c24b2c4 1260static int setup_rate(struct thread_data *td)
86184d14 1261{
4240cfa1
JA
1262 int nr_reads_per_sec;
1263
1264 if (!td->rate)
1265 return 0;
1266
1267 if (td->rate < td->ratemin) {
1268 fprintf(stderr, "min rate larger than nominal rate\n");
1269 return -1;
1270 }
86184d14 1271
7889f07b 1272 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
86184d14
JA
1273 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1274 td->rate_pending_usleep = 0;
4240cfa1 1275 return 0;
86184d14
JA
1276}
1277
47d45203 1278static struct thread_data *get_new_job(int global)
892199bd 1279{
4240cfa1
JA
1280 struct thread_data *td;
1281
47d45203
JA
1282 if (global)
1283 return &def_thread;
8867c0a8 1284 if (thread_number >= max_jobs)
4240cfa1
JA
1285 return NULL;
1286
1287 td = &threads[thread_number++];
fc24389f 1288 memset(td, 0, sizeof(*td));
892199bd 1289
e4ed35c3 1290 td->fd = -1;
86184d14 1291 td->thread_number = thread_number;
76cb7b42 1292
47d45203 1293 td->ddir = def_thread.ddir;
76cb7b42
JA
1294 td->ioprio = def_thread.ioprio;
1295 td->sequential = def_thread.sequential;
47d45203 1296 td->bs = def_thread.bs;
8c033f93
JA
1297 td->min_bs = def_thread.min_bs;
1298 td->max_bs = def_thread.max_bs;
47d45203 1299 td->odirect = def_thread.odirect;
e97712ed 1300 td->thinktime = def_thread.thinktime;
76cb7b42
JA
1301 td->fsync_blocks = def_thread.fsync_blocks;
1302 td->start_delay = def_thread.start_delay;
67903a2e 1303 td->timeout = def_thread.timeout;
76cb7b42 1304 td->use_aio = def_thread.use_aio;
02983297
JA
1305 td->create_file = def_thread.create_file;
1306 td->overwrite = def_thread.overwrite;
b95799ca 1307 td->invalidate_cache = def_thread.invalidate_cache;
76cb7b42 1308 td->file_size = def_thread.file_size;
9b5cf6c0 1309 td->file_offset = def_thread.file_offset;
76cb7b42
JA
1310 td->rate = def_thread.rate;
1311 td->ratemin = def_thread.ratemin;
1312 td->ratecycle = def_thread.ratecycle;
1313 td->aio_depth = def_thread.aio_depth;
99c6704f
JA
1314 td->sync_io = def_thread.sync_io;
1315 td->mem_type = def_thread.mem_type;
1d035750 1316 td->bw_avg_time = def_thread.bw_avg_time;
fc097bfe
JA
1317 td->create_serialize = def_thread.create_serialize;
1318 td->create_fsync = def_thread.create_fsync;
b6794fbf 1319 td->loops = def_thread.loops;
cfc702bd 1320 td->verify = def_thread.verify;
47d45203 1321 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
f737299d
JA
1322
1323 return td;
1324}
1325
4240cfa1
JA
1326static void put_job(struct thread_data *td)
1327{
1328 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1329 thread_number--;
1330}
1331
5c24b2c4
JA
1332static int add_job(struct thread_data *td, const char *filename, int prioclass,
1333 int prio)
f737299d 1334{
47d45203
JA
1335 if (td == &def_thread)
1336 return 0;
1337
f737299d 1338 strcpy(td->file_name, filename);
4240cfa1 1339 sem_init(&td->mutex, 1, 0);
f737299d
JA
1340 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1341
57d753e3
JA
1342 td->clat_stat.min_val = ULONG_MAX;
1343 td->slat_stat.min_val = ULONG_MAX;
1344 td->bw_stat.min_val = ULONG_MAX;
1345
8dbff0b1
JA
1346 run_str[td->thread_number - 1] = 'P';
1347
4ac89145
JA
1348 if (td->use_aio && !td->aio_depth)
1349 td->aio_depth = 1;
43000118 1350
8bdcfab5
JA
1351 if (td->min_bs == -1)
1352 td->min_bs = td->bs;
1353 if (td->max_bs == -1)
1354 td->max_bs = td->bs;
1355
4240cfa1
JA
1356 if (setup_rate(td))
1357 return -1;
f737299d 1358
a0a9b35b
JA
1359 if (write_lat_log)
1360 setup_log(&td->lat_log);
1361 if (write_bw_log)
1362 setup_log(&td->bw_log);
1363
7889f07b 1364 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 1365 return 0;
892199bd
JA
1366}
1367
18e0b78c
JA
1368static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1369{
f737299d 1370 unsigned int i;
18e0b78c
JA
1371
1372 CPU_ZERO(&cpumask);
1373
1374 for (i = 0; i < sizeof(int) * 8; i++) {
1375 if ((1 << i) & cpu)
1376 CPU_SET(i, &cpumask);
1377 }
1378}
1379
7889f07b
JA
1380unsigned long get_mult(char c)
1381{
1382 switch (c) {
1383 case 'k':
1384 case 'K':
1385 return 1024;
1386 case 'm':
1387 case 'M':
1388 return 1024 * 1024;
1389 case 'g':
1390 case 'G':
1391 return 1024 * 1024 * 1024;
1392 default:
1393 return 1;
1394 }
1395}
1396
02983297
JA
1397/*
1398 * convert string after '=' into decimal value, noting any size suffix
1399 */
1400static int str_cnv(char *p, unsigned long long *val)
1401{
02983297
JA
1402 char *str;
1403 int len;
1404
1405 str = strstr(p, "=");
1406 if (!str)
1407 return 1;
1408
1409 str++;
1410 len = strlen(str);
02983297
JA
1411
1412 *val = strtoul(str, NULL, 10);
1413 if (*val == ULONG_MAX && errno == ERANGE)
1414 return 1;
1415
7889f07b 1416 *val *= get_mult(str[len - 2]);
02983297 1417 return 0;
02983297
JA
1418}
1419
02983297
JA
1420static int check_strcnv(char *p, char *name, unsigned long long *val)
1421{
1422 if (!strstr(p, name))
1423 return 1;
1424
1425 return str_cnv(p, val);
1426}
1427
99c6704f
JA
1428static int check_str(char *p, char *name, char *option)
1429{
1430 char *s = strstr(p, name);
1431
1432 if (!s)
1433 return 1;
1434
1435 s += strlen(name);
1436 if (strstr(s, option))
1437 return 0;
1438
1439 return 1;
1440}
1441
7889f07b
JA
1442static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1443{
1444 char str[128];
1445 char s1, s2;
1446
1447 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1448 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1449 *s *= get_mult(s1);
1450 *e *= get_mult(s2);
1451 return 0;
1452 }
1453
1454 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1455 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1456 *s *= get_mult(s1);
1457 *e *= get_mult(s2);
1458 return 0;
1459 }
1460
1461 sprintf(str, "%s=%%lu-%%lu", name);
1462 if (sscanf(p, str, s, e) == 2)
1463 return 0;
1464
1465 sprintf(str, "%s = %%lu-%%lu", name);
1466 if (sscanf(p, str, s, e) == 2)
1467 return 0;
1468
1469 return 1;
1470
1471}
1472
5c24b2c4 1473static int check_int(char *p, char *name, unsigned int *val)
7dd1389e
JA
1474{
1475 char str[128];
1476
1477 sprintf(str, "%s=%%d", name);
1478 if (sscanf(p, str, val) == 1)
1479 return 0;
1480
1481 sprintf(str, "%s = %%d", name);
1482 if (sscanf(p, str, val) == 1)
1483 return 0;
1484
1485 return 1;
1486}
1487
7292613b 1488static int is_empty_or_comment(char *line)
7dd1389e
JA
1489{
1490 unsigned int i;
1491
7292613b 1492 for (i = 0; i < strlen(line); i++) {
7292613b 1493 if (line[i] == ';')
47d45203
JA
1494 return 1;
1495 if (!isspace(line[i]) && !iscntrl(line[i]))
7292613b
JA
1496 return 0;
1497 }
7dd1389e
JA
1498
1499 return 1;
1500}
1501
5c24b2c4 1502static int parse_jobs_ini(char *file)
7dd1389e 1503{
47d45203 1504 unsigned int prioclass, prio, cpu, global;
8c033f93 1505 unsigned long long ull;
7889f07b 1506 unsigned long ul1, ul2;
f737299d 1507 struct thread_data *td;
7dd1389e
JA
1508 char *string, *name;
1509 fpos_t off;
1510 FILE *f;
1511 char *p;
1512
1513 f = fopen(file, "r");
1514 if (!f) {
1515 perror("fopen");
4240cfa1 1516 return 1;
7dd1389e
JA
1517 }
1518
1519 string = malloc(4096);
1520 name = malloc(256);
1521
7dd1389e 1522 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b
JA
1523 if (is_empty_or_comment(p))
1524 continue;
7dd1389e
JA
1525 if (sscanf(p, "[%s]", name) != 1)
1526 continue;
1527
47d45203
JA
1528 global = !strncmp(name, "global", 6);
1529
7dd1389e
JA
1530 name[strlen(name) - 1] = '\0';
1531
47d45203 1532 td = get_new_job(global);
4240cfa1
JA
1533 if (!td)
1534 break;
f737299d 1535
7dd1389e 1536 prioclass = 2;
f737299d 1537 prio = 4;
7dd1389e
JA
1538
1539 fgetpos(f, &off);
1540 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b 1541 if (is_empty_or_comment(p))
e6402082
JA
1542 continue;
1543 if (strstr(p, "["))
7dd1389e 1544 break;
f737299d 1545 if (!check_int(p, "rw", &td->ddir)) {
7dd1389e
JA
1546 fgetpos(f, &off);
1547 continue;
1548 }
1549 if (!check_int(p, "prio", &prio)) {
1550 fgetpos(f, &off);
1551 continue;
1552 }
1553 if (!check_int(p, "prioclass", &prioclass)) {
1554 fgetpos(f, &off);
1555 continue;
1556 }
f737299d 1557 if (!check_int(p, "direct", &td->odirect)) {
7dd1389e
JA
1558 fgetpos(f, &off);
1559 continue;
1560 }
f737299d 1561 if (!check_int(p, "rate", &td->rate)) {
7dd1389e
JA
1562 fgetpos(f, &off);
1563 continue;
1564 }
4240cfa1
JA
1565 if (!check_int(p, "ratemin", &td->ratemin)) {
1566 fgetpos(f, &off);
1567 continue;
1568 }
1569 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1570 fgetpos(f, &off);
1571 continue;
1572 }
e97712ed 1573 if (!check_int(p, "thinktime", &td->thinktime)) {
7dd1389e
JA
1574 fgetpos(f, &off);
1575 continue;
1576 }
18e0b78c 1577 if (!check_int(p, "cpumask", &cpu)) {
f737299d 1578 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1579 fgetpos(f, &off);
1580 continue;
1581 }
4240cfa1
JA
1582 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1583 fgetpos(f, &off);
1584 continue;
1585 }
fc24389f
JA
1586 if (!check_int(p, "startdelay", &td->start_delay)) {
1587 fgetpos(f, &off);
1588 continue;
1589 }
67903a2e
JA
1590 if (!check_int(p, "timeout", &td->timeout)) {
1591 fgetpos(f, &off);
1592 continue;
1593 }
b95799ca
JA
1594 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1595 fgetpos(f, &off);
1596 continue;
1597 }
43000118
JA
1598 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1599 fgetpos(f, &off);
1600 continue;
1601 }
74b4b5fb
JA
1602 if (!check_int(p, "sync", &td->sync_io)) {
1603 fgetpos(f, &off);
1604 continue;
1605 }
1d035750
JA
1606 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
1607 fgetpos(f, &off);
1608 continue;
1609 }
fc097bfe
JA
1610 if (!check_int(p, "create_serialize", &td->create_serialize)) {
1611 fgetpos(f, &off);
1612 continue;
1613 }
1614 if (!check_int(p, "create_fsync", &td->create_fsync)) {
1615 fgetpos(f, &off);
1616 continue;
1617 }
b6794fbf
JA
1618 if (!check_int(p, "loops", &td->loops)) {
1619 fgetpos(f, &off);
1620 continue;
1621 }
7889f07b
JA
1622 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1623 td->min_bs = ul1;
1624 td->max_bs = ul2;
1625 fgetpos(f, &off);
1626 continue;
1627 }
8c033f93
JA
1628 if (!check_strcnv(p, "bs", &ull)) {
1629 td->bs = ull;
1630 fgetpos(f, &off);
1631 continue;
1632 }
02983297
JA
1633 if (!check_strcnv(p, "size", &td->file_size)) {
1634 fgetpos(f, &off);
1635 continue;
1636 }
1637 if (!check_strcnv(p, "offset", &td->file_offset)) {
1638 fgetpos(f, &off);
1639 continue;
1640 }
99c6704f 1641 if (!check_str(p, "mem", "malloc")) {
99c6704f
JA
1642 td->mem_type = MEM_MALLOC;
1643 fgetpos(f, &off);
1644 continue;
1645 }
1646 if (!check_str(p, "mem", "shm")) {
99c6704f
JA
1647 td->mem_type = MEM_SHM;
1648 fgetpos(f, &off);
1649 continue;
1650 }
43000118 1651 if (!strncmp(p, "sequential", 10)) {
f737299d 1652 td->sequential = 1;
7dd1389e
JA
1653 fgetpos(f, &off);
1654 continue;
1655 }
43000118 1656 if (!strncmp(p, "random", 6)) {
f737299d 1657 td->sequential = 0;
7dd1389e
JA
1658 fgetpos(f, &off);
1659 continue;
1660 }
43000118
JA
1661 if (!strncmp(p, "aio", 3)) {
1662 td->use_aio = 1;
1663 fgetpos(f, &off);
1664 continue;
1665 }
02983297
JA
1666 if (!strncmp(p, "create", 6)) {
1667 td->create_file = 1;
1668 fgetpos(f, &off);
1669 continue;
1670 }
1671 if (!strncmp(p, "overwrite", 9)) {
1672 td->overwrite = 1;
1673 fgetpos(f, &off);
1674 continue;
1675 }
98dd52d6
JA
1676 if (!strncmp(p, "exitall", 7)) {
1677 exitall_on_terminate = 1;
1678 fgetpos(f, &off);
1679 continue;
1680 }
e6402082 1681 printf("Client%d: bad option %s\n",td->thread_number,p);
7dd1389e
JA
1682 }
1683 fsetpos(f, &off);
1684
4240cfa1
JA
1685 if (add_job(td, name, prioclass, prio))
1686 put_job(td);
7dd1389e
JA
1687 }
1688
1689 free(string);
1690 free(name);
fc7d63df 1691 fclose(f);
4240cfa1 1692 return 0;
7dd1389e
JA
1693}
1694
5c24b2c4 1695static int parse_options(int argc, char *argv[])
892199bd 1696{
01c4d8de 1697 int i;
892199bd
JA
1698
1699 for (i = 1; i < argc; i++) {
1700 char *parm = argv[i];
1701
1702 if (parm[0] != '-')
1703 break;
1704
1705 parm++;
1706 switch (*parm) {
1707 case 's':
1708 parm++;
47d45203 1709 def_thread.sequential = !!atoi(parm);
892199bd
JA
1710 break;
1711 case 'b':
1712 parm++;
47d45203
JA
1713 def_thread.bs = atoi(parm);
1714 def_thread.bs <<= 10;
1715 if (!def_thread.bs) {
4240cfa1 1716 printf("bad block size\n");
47d45203 1717 def_thread.bs = DEF_BS;
4240cfa1 1718 }
892199bd
JA
1719 break;
1720 case 't':
1721 parm++;
47d45203 1722 def_thread.timeout = atoi(parm);
892199bd 1723 break;
892199bd
JA
1724 case 'r':
1725 parm++;
1726 repeatable = !!atoi(parm);
1727 break;
02bdd9ba
JA
1728 case 'R':
1729 parm++;
1730 rate_quit = !!atoi(parm);
1731 break;
892199bd
JA
1732 case 'o':
1733 parm++;
47d45203 1734 def_thread.odirect = !!atoi(parm);
892199bd 1735 break;
7dd1389e
JA
1736 case 'f':
1737 if (i + 1 >= argc) {
1738 printf("-f needs file as arg\n");
1739 break;
1740 }
1741 ini_file = strdup(argv[i+1]);
a642279f 1742 i++;
7dd1389e 1743 break;
a0a9b35b
JA
1744 case 'l':
1745 write_lat_log = 1;
1746 break;
1747 case 'w':
1748 write_bw_log = 1;
1749 break;
892199bd 1750 default:
7dd1389e 1751 printf("bad option %s\n", argv[i]);
892199bd
JA
1752 break;
1753 }
1754 }
1755
892199bd
JA
1756 return i;
1757}
1758
3f39453a 1759static void print_thread_status(struct thread_data *td, int nr_running,
8dbff0b1 1760 int t_rate, int m_rate)
3f39453a 1761{
3f39453a
JA
1762 printf("Threads now running: %d", nr_running);
1763 if (m_rate || t_rate)
1764 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8dbff0b1
JA
1765 printf(" : [%s]\r", run_str);
1766 fflush(stdout);
3f39453a
JA
1767}
1768
213b446c 1769static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
02bdd9ba 1770{
213b446c 1771 int i;
02bdd9ba 1772
3f39453a
JA
1773 /*
1774 * reap exited threads (TD_EXITED -> TD_REAPED)
1775 */
02bdd9ba
JA
1776 for (i = 0; i < thread_number; i++) {
1777 struct thread_data *td = &threads[i];
1778
213b446c
JA
1779 if (td->runstate != TD_EXITED)
1780 continue;
02bdd9ba 1781
213b446c 1782 td->runstate = TD_REAPED;
8dbff0b1 1783 run_str[td->thread_number - 1] = '_';
213b446c
JA
1784 waitpid(td->pid, NULL, 0);
1785 (*nr_running)--;
1786 (*m_rate) -= td->ratemin;
1787 (*t_rate) -= td->rate;
e6402082
JA
1788
1789 if (td->terminate)
1790 continue;
1791
8dbff0b1 1792 print_thread_status(td, *nr_running, *t_rate, *m_rate);
213b446c 1793 }
02bdd9ba
JA
1794}
1795
fc24389f
JA
1796static void run_threads(char *argv[])
1797{
be33abe4 1798 struct timeval genesis;
fc24389f
JA
1799 struct thread_data *td;
1800 unsigned long spent;
213b446c 1801 int i, todo, nr_running, m_rate, t_rate;
fc24389f 1802
fc24389f
JA
1803 printf("Starting %d threads\n", thread_number);
1804 fflush(stdout);
1805
7292613b
JA
1806 signal(SIGINT, sig_handler);
1807
fc24389f 1808 todo = thread_number;
02bdd9ba 1809 nr_running = 0;
213b446c 1810 m_rate = t_rate = 0;
fc24389f 1811
8bdcfab5
JA
1812 for (i = 0; i < thread_number; i++) {
1813 td = &threads[i];
1814
fc097bfe
JA
1815 if (!td->create_serialize)
1816 continue;
1817
8bdcfab5
JA
1818 /*
1819 * do file setup here so it happens sequentially,
1820 * we don't want X number of threads getting their
1821 * client data interspersed on disk
1822 */
1823 if (setup_file(td)) {
1824 td->runstate = TD_REAPED;
1825 todo--;
1826 }
1827 }
1828
1829 gettimeofday(&genesis, NULL);
1830
213b446c 1831 while (todo) {
3f39453a
JA
1832 /*
1833 * create threads (TD_NOT_CREATED -> TD_CREATED)
1834 */
fc24389f
JA
1835 for (i = 0; i < thread_number; i++) {
1836 td = &threads[i];
1837
02bdd9ba 1838 if (td->runstate != TD_NOT_CREATED)
fc24389f
JA
1839 continue;
1840
213b446c
JA
1841 /*
1842 * never got a chance to start, killed by other
1843 * thread for some reason
1844 */
1845 if (td->terminate) {
1846 todo--;
1847 continue;
1848 }
1849
fc24389f 1850 if (td->start_delay) {
be33abe4 1851 spent = mtime_since_now(&genesis);
fc24389f
JA
1852
1853 if (td->start_delay * 1000 > spent)
1854 continue;
1855 }
1856
02bdd9ba 1857 td->runstate = TD_CREATED;
8dbff0b1 1858 run_str[td->thread_number - 1] = 'C';
fc24389f
JA
1859 sem_init(&startup_sem, 1, 1);
1860 todo--;
1861
1862 if (fork())
1863 sem_wait(&startup_sem);
1864 else {
1865 thread_main(shm_id, i, argv);
1866 exit(0);
1867 }
1868 }
1869
3f39453a
JA
1870 /*
1871 * start created threads (TD_CREATED -> TD_STARTED)
1872 */
fc24389f
JA
1873 for (i = 0; i < thread_number; i++) {
1874 struct thread_data *td = &threads[i];
1875
3f39453a
JA
1876 if (td->runstate != TD_CREATED)
1877 continue;
1878
1879 td->runstate = TD_STARTED;
8dbff0b1 1880 run_str[td->thread_number - 1] = '+';
3f39453a
JA
1881 nr_running++;
1882 m_rate += td->ratemin;
1883 t_rate += td->rate;
1884 sem_post(&td->mutex);
1885
8dbff0b1 1886 print_thread_status(td, nr_running, t_rate, m_rate);
fc24389f
JA
1887 }
1888
213b446c 1889 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba 1890
fc24389f
JA
1891 if (todo)
1892 usleep(100000);
1893 }
02bdd9ba
JA
1894
1895 while (nr_running) {
213b446c 1896 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba
JA
1897 usleep(10000);
1898 }
fc24389f
JA
1899}
1900
8867c0a8 1901int setup_thread_area(void)
892199bd 1902{
8867c0a8
JA
1903 /*
1904 * 1024 is too much on some machines, scale max_jobs if
1905 * we get a failure that looks like too large a shm segment
1906 */
1907 do {
1908 int s = max_jobs * sizeof(struct thread_data);
18e0b78c 1909
8867c0a8
JA
1910 shm_id = shmget(0, s, IPC_CREAT | 0600);
1911 if (shm_id != -1)
1912 break;
1913 if (errno != EINVAL) {
1914 perror("shmget");
1915 break;
1916 }
1917
1918 max_jobs >>= 1;
d4fac444 1919 } while (max_jobs);
8867c0a8
JA
1920
1921 if (shm_id == -1)
892199bd 1922 return 1;
892199bd
JA
1923
1924 threads = shmat(shm_id, NULL, 0);
8867c0a8 1925 if (threads == (void *) -1) {
86184d14
JA
1926 perror("shmat");
1927 return 1;
1928 }
892199bd
JA
1929
1930 atexit(free_shm);
8867c0a8
JA
1931 return 0;
1932}
1933
1934int main(int argc, char *argv[])
1935{
8c033f93 1936 static unsigned long max_run[2], min_run[2];
57d753e3 1937 static unsigned long max_bw[2], min_bw[2];
22334044 1938 static unsigned long io_mb[2], agg[2];
8867c0a8
JA
1939 int i;
1940
1941 if (setup_thread_area())
1942 return 1;
892199bd 1943
47d45203 1944 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
4240cfa1
JA
1945 perror("sched_getaffinity");
1946 return 1;
1947 }
1948
47d45203
JA
1949 /*
1950 * fill globals
1951 */
1952 def_thread.ddir = DDIR_READ;
1953 def_thread.bs = DEF_BS;
7889f07b
JA
1954 def_thread.min_bs = -1;
1955 def_thread.max_bs = -1;
02983297 1956 def_thread.odirect = DEF_ODIRECT;
47d45203 1957 def_thread.ratecycle = DEF_RATE_CYCLE;
02983297 1958 def_thread.sequential = DEF_SEQUENTIAL;
47d45203 1959 def_thread.timeout = DEF_TIMEOUT;
02983297
JA
1960 def_thread.create_file = DEF_CREATE;
1961 def_thread.overwrite = DEF_OVERWRITE;
b95799ca 1962 def_thread.invalidate_cache = DEF_INVALIDATE;
99c6704f
JA
1963 def_thread.sync_io = DEF_SYNCIO;
1964 def_thread.mem_type = MEM_MALLOC;
1d035750 1965 def_thread.bw_avg_time = DEF_BWAVGTIME;
fc097bfe
JA
1966 def_thread.create_serialize = DEF_CREATE_SER;
1967 def_thread.create_fsync = DEF_CREATE_FSYNC;
b6794fbf 1968 def_thread.loops = DEF_LOOPS;
cfc702bd 1969 def_thread.verify = DEF_VERIFY;
47d45203 1970
892199bd 1971 i = parse_options(argc, argv);
7dd1389e 1972
5961d92c
JA
1973 if (!ini_file) {
1974 printf("Need job file\n");
1975 return 1;
1976 }
1977
1978 if (parse_jobs_ini(ini_file))
1979 return 1;
7dd1389e 1980
4240cfa1
JA
1981 if (!thread_number) {
1982 printf("Nothing to do\n");
1983 return 1;
1984 }
7dd1389e 1985
fc24389f 1986 run_threads(argv);
892199bd 1987
892199bd
JA
1988 min_bw[0] = min_run[0] = ~0UL;
1989 min_bw[1] = min_run[1] = ~0UL;
22334044
JA
1990 io_mb[0] = io_mb[1] = 0;
1991 agg[0] = agg[1] = 0;
892199bd
JA
1992 for (i = 0; i < thread_number; i++) {
1993 struct thread_data *td = &threads[i];
1994 unsigned long bw = 0;
1995
22334044
JA
1996 if (!td->error) {
1997 if (td->runtime < min_run[td->ddir])
1998 min_run[td->ddir] = td->runtime;
1999 if (td->runtime > max_run[td->ddir])
2000 max_run[td->ddir] = td->runtime;
892199bd 2001
892199bd 2002 if (td->runtime)
22334044
JA
2003 bw = td->io_kb * 1024 / td->runtime;
2004 if (bw < min_bw[td->ddir])
2005 min_bw[td->ddir] = bw;
2006 if (bw > max_bw[td->ddir])
2007 max_bw[td->ddir] = bw;
2008
2009 io_mb[td->ddir] += td->io_kb >> 10;
892199bd
JA
2010 }
2011
2012 show_thread_status(td);
2013 }
22334044
JA
2014
2015 if (max_run[0])
2016 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2017 if (max_run[1])
2018 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2019
57d753e3 2020 printf("\nRun status:\n");
892199bd 2021 if (max_run[DDIR_READ])
22334044 2022 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 2023 if (max_run[DDIR_WRITE])
22334044 2024 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 2025
892199bd
JA
2026 return 0;
2027}