[PATCH] fio: avoid lseek() if cur_off == offset
[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)
4240cfa1
JA
128
129#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
892199bd 130
4240cfa1 131static int repeatable = DEF_RAND_REPEAT;
02bdd9ba 132static int rate_quit = 1;
892199bd 133
892199bd 134static int thread_number;
7dd1389e 135static char *ini_file;
892199bd 136
8867c0a8
JA
137static int max_jobs = MAX_JOBS;
138
892199bd
JA
139static int shm_id;
140
4240cfa1
JA
141enum {
142 DDIR_READ = 0,
143 DDIR_WRITE,
144};
892199bd 145
02bdd9ba
JA
146/*
147 * thread life cycle
148 */
149enum {
150 TD_NOT_CREATED = 0,
151 TD_CREATED,
152 TD_STARTED,
153 TD_EXITED,
154 TD_REAPED,
155};
156
2c83567e
JA
157/*
158 * The io unit
159 */
160struct io_u {
161 struct iocb iocb;
162 struct timeval issue_time;
163
164 void *mem;
165 char *buf;
166 unsigned int buflen;
167 off_t offset;
168
169 struct list_head list;
170};
171
02983297
JA
172#define td_read(td) ((td)->ddir == DDIR_READ)
173#define should_fsync(td) (!td_read(td) && !(td)->odirect)
174
892199bd
JA
175struct thread_data {
176 char file_name[256];
177 int thread_number;
178 int error;
179 int fd;
892199bd 180 pid_t pid;
4240cfa1 181 volatile int terminate;
02bdd9ba 182 volatile int runstate;
f737299d
JA
183 unsigned int ddir;
184 unsigned int ioprio;
185 unsigned int sequential;
186 unsigned int bs;
187 unsigned int odirect;
188 unsigned int delay_sleep;
4240cfa1 189 unsigned int fsync_blocks;
fc24389f 190 unsigned int start_delay;
47d45203 191 unsigned int timeout;
43000118 192 unsigned int use_aio;
02983297
JA
193 unsigned int create_file;
194 unsigned int overwrite;
b95799ca 195 unsigned int invalidate_cache;
02983297
JA
196 unsigned long long file_size;
197 unsigned long long file_offset;
18e0b78c 198 cpu_set_t cpumask;
86184d14 199
63a09e51
JA
200 off_t cur_off;
201
254605cd 202 io_context_t aio_ctx;
43000118 203 unsigned int aio_depth;
43000118 204 struct io_event *aio_events;
2c83567e
JA
205
206 unsigned int cur_depth;
207 struct list_head io_u_freelist;
208 struct list_head io_u_busylist;
43000118 209
7dd1389e 210 unsigned int rate;
4240cfa1
JA
211 unsigned int ratemin;
212 unsigned int ratecycle;
213 unsigned long rate_usec_cycle;
214 long rate_pending_usleep;
215 unsigned long rate_blocks;
216 struct timeval lastrate;
86184d14 217
892199bd
JA
218 unsigned long max_latency; /* msec */
219 unsigned long min_latency; /* msec */
220 unsigned long runtime; /* sec */
221 unsigned long blocks;
4240cfa1 222 unsigned long io_blocks;
892199bd
JA
223 unsigned long last_block;
224 sem_t mutex;
892199bd
JA
225 struct drand48_data random_state;
226
227 /*
e128065d 228 * bandwidth and latency stats
892199bd
JA
229 */
230 unsigned long stat_time;
e128065d 231 unsigned long stat_time_sq;
fd1ae4c9
JA
232 unsigned long stat_time_samples;
233 unsigned long stat_io_blocks;
234 unsigned long stat_bw;
235 unsigned long stat_bw_sq;
236 unsigned long stat_bw_samples;
237 struct timeval stat_sample_time;
4240cfa1
JA
238
239 struct timeval start;
892199bd
JA
240};
241
242static struct thread_data *threads;
47d45203 243static struct thread_data def_thread;
892199bd
JA
244
245static sem_t startup_sem;
246
5c24b2c4 247static void sig_handler(int sig)
892199bd
JA
248{
249 int i;
250
213b446c
JA
251 for (i = 0; i < thread_number; i++) {
252 struct thread_data *td = &threads[i];
253
254 td->terminate = 1;
255 td->start_delay = 0;
256 }
02bdd9ba
JA
257}
258
5c24b2c4 259static int init_random_state(struct thread_data *td)
892199bd
JA
260{
261 unsigned long seed = 123;
262
263 if (td->sequential)
264 return 0;
265
266 if (!repeatable) {
267 int fd = open("/dev/random", O_RDONLY);
268
269 if (fd == -1) {
270 td->error = errno;
271 return 1;
272 }
273
7dd1389e 274 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
892199bd
JA
275 td->error = EIO;
276 close(fd);
277 return 1;
278 }
279
280 close(fd);
281 }
282
283 srand48_r(seed, &td->random_state);
284 return 0;
285}
286
5c24b2c4 287static unsigned long utime_since(struct timeval *s, struct timeval *e)
892199bd
JA
288{
289 double sec, usec;
290
291 sec = e->tv_sec - s->tv_sec;
292 usec = e->tv_usec - s->tv_usec;
293 if (sec > 0 && usec < 0) {
294 sec--;
295 usec += 1000000;
296 }
297
298 sec *= (double) 1000000;
299
300 return sec + usec;
301}
302
5c24b2c4 303static unsigned long mtime_since(struct timeval *s, struct timeval *e)
892199bd
JA
304{
305 double sec, usec;
306
307 sec = e->tv_sec - s->tv_sec;
308 usec = e->tv_usec - s->tv_usec;
309 if (sec > 0 && usec < 0) {
310 sec--;
311 usec += 1000000;
312 }
313
314 sec *= (double) 1000;
315 usec /= (double) 1000;
316
317 return sec + usec;
318}
319
be33abe4
JA
320static unsigned long mtime_since_now(struct timeval *s)
321{
322 struct timeval t;
323
324 gettimeofday(&t, NULL);
325 return mtime_since(s, &t);
326}
327
98168d55
JA
328static inline unsigned long msec_now(struct timeval *s)
329{
330 return s->tv_sec * 1000 + s->tv_usec / 1000;
331}
332
5c24b2c4 333static unsigned long get_next_offset(struct thread_data *td)
892199bd
JA
334{
335 unsigned long b;
336 long r;
337
338 if (!td->sequential) {
339 lrand48_r(&td->random_state, &r);
340 b = (1+(double) (td->blocks-1) * r / (RAND_MAX+1.0));
341 } else {
342 b = td->last_block;
343 td->last_block++;
344 }
345
02983297 346 return b * td->bs + td->file_offset;
892199bd
JA
347}
348
5c24b2c4 349static void add_stat_sample(struct thread_data *td, unsigned long msec)
892199bd 350{
fd1ae4c9
JA
351 unsigned long spent;
352
892199bd 353 td->stat_time += msec;
e128065d 354 td->stat_time_sq += msec * msec;
fd1ae4c9
JA
355 td->stat_time_samples++;
356
357 spent = mtime_since_now(&td->stat_sample_time);
358 if (spent >= 500) {
359 unsigned long rate = ((td->io_blocks - td->stat_io_blocks) * td->bs) / spent;
360
361 td->stat_bw += rate;
362 td->stat_bw_sq += rate * rate;
363 gettimeofday(&td->stat_sample_time, NULL);
364 td->stat_io_blocks = td->io_blocks;
365 td->stat_bw_samples++;
366 }
892199bd
JA
367}
368
5c24b2c4 369static void usec_sleep(int usec)
892199bd 370{
86184d14
JA
371 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
372 struct timespec rem;
892199bd
JA
373
374 do {
86184d14
JA
375 rem.tv_sec = rem.tv_nsec = 0;
376 nanosleep(&req, &rem);
377 if (!rem.tv_nsec)
892199bd 378 break;
86184d14
JA
379
380 req.tv_nsec = rem.tv_nsec;
892199bd
JA
381 } while (1);
382}
383
5c24b2c4 384static void rate_throttle(struct thread_data *td, unsigned long time_spent)
86184d14 385{
4240cfa1
JA
386 if (!td->rate)
387 return;
388
86184d14
JA
389 if (time_spent < td->rate_usec_cycle) {
390 unsigned long s = td->rate_usec_cycle - time_spent;
391
392 td->rate_pending_usleep += s;
fad86e6a 393 if (td->rate_pending_usleep >= 100000) {
86184d14
JA
394 usec_sleep(td->rate_pending_usleep);
395 td->rate_pending_usleep = 0;
396 }
4240cfa1 397 } else {
42b2b9fe
JA
398 long overtime = time_spent - td->rate_usec_cycle;
399
4240cfa1
JA
400 td->rate_pending_usleep -= overtime;
401 }
402}
403
5c24b2c4 404static int check_min_rate(struct thread_data *td, struct timeval *now)
4240cfa1 405{
7607bc6b 406 unsigned long spent;
4240cfa1
JA
407 unsigned long rate;
408
409 /*
410 * allow a 2 second settle period in the beginning
411 */
7607bc6b 412 if (mtime_since(&td->start, now) < 2000)
4240cfa1
JA
413 return 0;
414
415 /*
416 * if rate blocks is set, sample is running
417 */
418 if (td->rate_blocks) {
419 spent = mtime_since(&td->lastrate, now);
420 if (spent < td->ratecycle)
421 return 0;
422
423 rate = ((td->io_blocks - td->rate_blocks) * td->bs) / spent;
424 if (rate < td->ratemin) {
425 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
02bdd9ba 426 if (rate_quit)
e6402082 427 sig_handler(0);
4240cfa1
JA
428 return 1;
429 }
86184d14 430 }
4240cfa1
JA
431
432 td->rate_blocks = td->io_blocks;
433 memcpy(&td->lastrate, now, sizeof(*now));
434 return 0;
86184d14
JA
435}
436
67903a2e
JA
437static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
438{
439 if (mtime_since(&td->start, t) >= td->timeout * 1000)
440 return 1;
441
442 return 0;
443}
444
2c83567e
JA
445static void put_io_u(struct thread_data *td, struct io_u *io_u)
446{
447 list_del(&io_u->list);
448 list_add(&io_u->list, &td->io_u_freelist);
449 td->cur_depth--;
450}
451
452static struct io_u *get_io_u(struct thread_data *td)
453{
454 struct io_u *io_u;
455
456 if (list_empty(&td->io_u_freelist))
457 return NULL;
458
459 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
460 list_del(&io_u->list);
461 list_add(&io_u->list, &td->io_u_busylist);
462
463 io_u->offset = get_next_offset(td);
464
465 if (td->use_aio) {
466 if (td_read(td))
467 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
468 else
469 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
470 }
471
472 gettimeofday(&io_u->issue_time, NULL);
473 td->cur_depth++;
474 return io_u;
475}
476
43000118 477static void do_sync_io(struct thread_data *td)
892199bd 478{
86184d14 479 unsigned long blocks, msec, usec;
2c83567e 480 struct timeval e;
892199bd 481
63a09e51
JA
482 td->cur_off = 0;
483
892199bd 484 for (blocks = 0; blocks < td->blocks; blocks++) {
2c83567e 485 struct io_u *io_u;
892199bd
JA
486 int ret;
487
488 if (td->terminate)
489 break;
490
2c83567e
JA
491 io_u = get_io_u(td);
492
63a09e51
JA
493 if (td->cur_off != io_u->offset) {
494 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
495 td->error = errno;
496 break;
497 }
892199bd
JA
498 }
499
500 if (td->delay_sleep)
86184d14 501 usec_sleep(td->delay_sleep);
892199bd 502
02983297 503 if (td_read(td))
2c83567e 504 ret = read(td->fd, io_u->buf, io_u->buflen);
892199bd 505 else
2c83567e 506 ret = write(td->fd, io_u->buf, io_u->buflen);
892199bd 507
2c83567e 508 if (ret < (int) io_u->buflen) {
892199bd
JA
509 if (ret == -1)
510 td->error = errno;
511 break;
512 }
513
4240cfa1 514 td->io_blocks++;
63a09e51 515 td->cur_off = io_u->offset + io_u->buflen;
4240cfa1
JA
516
517 if (should_fsync(td) && td->fsync_blocks &&
518 (td->io_blocks % td->fsync_blocks) == 0)
519 fsync(td->fd);
520
86184d14
JA
521 gettimeofday(&e, NULL);
522
2c83567e 523 usec = utime_since(&io_u->issue_time, &e);
86184d14 524
4240cfa1 525 rate_throttle(td, usec);
892199bd 526
4240cfa1
JA
527 if (check_min_rate(td, &e)) {
528 td->error = ENODATA;
529 break;
530 }
892199bd 531
4240cfa1
JA
532 msec = usec / 1000;
533 add_stat_sample(td, msec);
892199bd
JA
534
535 if (msec < td->min_latency)
536 td->min_latency = msec;
537 if (msec > td->max_latency)
538 td->max_latency = msec;
67903a2e
JA
539
540 if (runtime_exceeded(td, &e))
541 break;
2c83567e
JA
542
543 put_io_u(td, io_u);
892199bd
JA
544 }
545
4240cfa1 546 if (should_fsync(td))
892199bd 547 fsync(td->fd);
892199bd 548}
43000118 549
2c83567e 550static int io_u_queue(struct thread_data *td, struct io_u *io_u)
56b0eff0 551{
2c83567e 552 struct iocb *iocb = &io_u->iocb;
56b0eff0
JA
553 int ret;
554
555 do {
254605cd 556 ret = io_submit(td->aio_ctx, 1, &iocb);
56b0eff0
JA
557 if (ret == 1)
558 return 0;
559
560 if (errno == EINTR)
561 continue;
562 else if (errno == EAGAIN)
563 usleep(100);
564 else
565 break;
566 } while (1);
567
568 return 1;
569}
570
98168d55 571#define iocb_time(iocb) ((unsigned long) (iocb)->data)
2c83567e
JA
572#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
573
574static void ios_completed(struct thread_data *td, int nr)
575{
576 unsigned long msec;
577 struct io_u *io_u;
578 struct timeval e;
579 int i;
580
581 gettimeofday(&e, NULL);
582
583 for (i = 0; i < nr; i++) {
584 td->io_blocks++;
585
586 io_u = ev_to_iou(td->aio_events + i);
587
588 msec = mtime_since(&io_u->issue_time, &e);
589
590 add_stat_sample(td, msec);
591
592 if (msec < td->min_latency)
593 td->min_latency = msec;
594 if (msec > td->max_latency)
595 td->max_latency = msec;
596
597 put_io_u(td, io_u);
598 }
599}
600
601static void cleanup_pending_aio(struct thread_data *td)
602{
603 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
604 struct list_head *entry, *n;
605 struct io_u *io_u;
606 int r;
607
608 /*
609 * get immediately available events, if any
610 */
611 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
612 if (r > 0)
613 ios_completed(td, r);
614
615 /*
616 * now cancel remaining active events
617 */
618 list_for_each_safe(entry, n, &td->io_u_busylist) {
619 io_u = list_entry(entry, struct io_u, list);
620
621 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
622 if (!r)
623 put_io_u(td, io_u);
624 }
625
626 if (td->cur_depth) {
627 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
628 if (r > 0)
629 ios_completed(td, r);
630 }
631}
98168d55 632
43000118
JA
633static void do_async_io(struct thread_data *td)
634{
635 struct timeval s, e;
2c83567e 636 unsigned long blocks, usec;
43000118 637
43000118
JA
638 for (blocks = 0; blocks < td->blocks; blocks++) {
639 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
640 struct timespec *timeout;
2c83567e
JA
641 int ret, min_evts = 0;
642 struct io_u *io_u;
43000118
JA
643
644 if (td->terminate)
645 break;
646
647 if (td->delay_sleep)
648 usec_sleep(td->delay_sleep);
649
2c83567e 650 io_u = get_io_u(td);
43000118 651
2c83567e 652 memcpy(&s, &io_u->issue_time, sizeof(s));
8baf1bcc 653
2c83567e 654 ret = io_u_queue(td, io_u);
56b0eff0 655 if (ret) {
43000118
JA
656 td->error = errno;
657 break;
658 }
659
2c83567e 660 if (td->cur_depth < td->aio_depth) {
43000118
JA
661 timeout = &ts;
662 min_evts = 0;
663 } else {
664 timeout = NULL;
665 min_evts = 1;
666 }
667
2c83567e 668 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
43000118
JA
669 if (ret < 0) {
670 td->error = errno;
671 break;
672 } else if (!ret)
673 continue;
674
2c83567e 675 ios_completed(td, ret);
43000118 676
98168d55
JA
677 /*
678 * the rate is batched for now, it should work for batches
679 * of completions except the very first one which may look
680 * a little bursty
681 */
2c83567e 682 gettimeofday(&e, NULL);
43000118
JA
683 usec = utime_since(&s, &e);
684
685 rate_throttle(td, usec);
686
687 if (check_min_rate(td, &e)) {
688 td->error = ENODATA;
689 break;
690 }
67903a2e
JA
691
692 if (runtime_exceeded(td, &e))
693 break;
43000118 694 }
56b0eff0 695
2c83567e
JA
696 if (td->cur_depth)
697 cleanup_pending_aio(td);
56b0eff0
JA
698}
699
700static void cleanup_aio(struct thread_data *td)
701{
254605cd
JA
702 io_destroy(td->aio_ctx);
703
43000118
JA
704 if (td->aio_events)
705 free(td->aio_events);
43000118
JA
706}
707
708static int init_aio(struct thread_data *td)
709{
254605cd 710 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
43000118
JA
711 td->error = errno;
712 return 1;
713 }
714
43000118 715 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
43000118
JA
716 return 0;
717}
718
2c83567e
JA
719static void cleanup_io_u(struct thread_data *td)
720{
721 struct list_head *entry, *n;
722 struct io_u *io_u;
723
724 list_for_each_safe(entry, n, &td->io_u_freelist) {
725 io_u = list_entry(entry, struct io_u, list);
726
727 list_del(&io_u->list);
728 free(io_u->mem);
729 free(io_u);
730 }
731}
732
733static void init_io_u(struct thread_data *td)
734{
735 struct io_u *io_u;
736 int i, max_units;
737
738 if (!td->use_aio)
739 max_units = 1;
740 else
741 max_units = td->aio_depth;
742
743 INIT_LIST_HEAD(&td->io_u_freelist);
744 INIT_LIST_HEAD(&td->io_u_busylist);
745
746 for (i = 0; i < max_units; i++) {
747 io_u = malloc(sizeof(*io_u));
748 memset(io_u, 0, sizeof(*io_u));
749 INIT_LIST_HEAD(&io_u->list);
750
751 io_u->mem = malloc(td->bs + MASK);
752 io_u->buf = ALIGN(io_u->mem);
753 io_u->buflen = td->bs;
754
755 list_add(&io_u->list, &td->io_u_freelist);
756 }
757}
758
02983297
JA
759static int create_file(struct thread_data *td)
760{
761 unsigned int i;
762 char *b;
763
764 if (!td->file_size) {
765 fprintf(stderr, "Need size for create\n");
766 td->error = EINVAL;
767 return 1;
768 }
769
770 /*
771 * unless specifically asked for overwrite, let normal io extend it
772 */
773 if (!td_read(td) && !td->overwrite)
774 return 0;
775
776 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
777 if (td->fd < 0) {
778 td->error = errno;
779 return 1;
780 }
781
782 td->blocks = td->file_size / td->bs;
783 b = malloc(td->bs);
784 memset(b, 0, td->bs);
785
786 for (i = 0; i < td->blocks; i++) {
787 int r = write(td->fd, b, td->bs);
788
789 if (r == td->bs)
790 continue;
791 else {
792 if (r < 0)
793 td->error = errno;
794 else
795 td->error = EIO;
796
797 break;
798 }
799 }
800
801 fsync(td->fd);
802 close(td->fd);
803 td->fd = -1;
804 free(b);
805 return 0;
806}
807
808static int file_exists(struct thread_data *td)
809{
810 struct stat st;
811
812 if (stat(td->file_name, &st) != -1)
813 return 1;
814
815 return errno != ENOENT;
816}
817
818static int setup_file(struct thread_data *td)
819{
820 struct stat st;
821 int flags = 0;
822
823 if (!file_exists(td)) {
824 if (!td->create_file) {
825 td->error = ENOENT;
826 return 1;
827 }
828 if (create_file(td))
829 return 1;
830 }
831
832 if (td->odirect)
833 flags |= O_DIRECT;
834
835 if (td_read(td))
836 td->fd = open(td->file_name, flags | O_RDONLY);
837 else {
838 if (!td->overwrite)
839 flags |= O_TRUNC;
840
841 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT, 0600);
842 }
843
844 if (td->fd == -1) {
845 td->error = errno;
846 return 1;
847 }
848
849 if (td_read(td)) {
850 if (fstat(td->fd, &st) == -1) {
851 td->error = errno;
852 return 1;
853 }
854
855 if (td->file_size > st.st_size)
856 st.st_size = td->file_size;
857 } else {
858 if (!td->file_size)
859 td->file_size = 1024 * 1024 * 1024;
860
861 st.st_size = td->file_size;
862 }
863
864 td->blocks = (st.st_size - td->file_offset) / td->bs;
865 if (!td->blocks) {
866 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
867 td->error = EINVAL;
868 return 1;
869 }
870
b95799ca
JA
871 if (td->invalidate_cache) {
872 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
873 td->error = errno;
874 return 1;
875 }
876 }
877
02983297
JA
878 return 0;
879}
880
5c24b2c4 881static void *thread_main(int shm_id, int offset, char *argv[])
892199bd
JA
882{
883 struct thread_data *td;
02983297 884 int ret = 1;
2c83567e 885 void *data;
892199bd 886
7292613b
JA
887 setsid();
888
892199bd
JA
889 data = shmat(shm_id, NULL, 0);
890 td = data + offset * sizeof(struct thread_data);
891 td->pid = getpid();
892
2c83567e
JA
893 init_io_u(td);
894
18e0b78c
JA
895 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
896 td->error = errno;
897 goto err;
898 }
899
a642279f 900 printf("Client%d (pid=%u) started\n", td->thread_number, td->pid);
892199bd 901
4240cfa1 902 sprintf(argv[0], "fio%d", offset);
892199bd 903
43000118
JA
904 if (td->use_aio && init_aio(td))
905 goto err;
906
892199bd 907 if (init_random_state(td))
599002b3 908 goto err;
892199bd 909
f737299d 910 if (td->ioprio) {
892199bd
JA
911 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
912 td->error = errno;
599002b3 913 goto err;
892199bd
JA
914 }
915 }
916
02983297
JA
917 if (setup_file(td))
918 goto err;
919
892199bd
JA
920 sem_post(&startup_sem);
921 sem_wait(&td->mutex);
43000118 922
7292613b
JA
923 gettimeofday(&td->start, NULL);
924
925 if (td->ratemin)
926 memcpy(&td->lastrate, &td->start, sizeof(td->start));
927
fd1ae4c9
JA
928 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
929
e4ed35c3 930 if (!td->use_aio)
43000118 931 do_sync_io(td);
e4ed35c3 932 else
43000118 933 do_async_io(td);
7292613b 934
be33abe4 935 td->runtime = mtime_since_now(&td->start);
892199bd 936 ret = 0;
892199bd 937err:
43000118
JA
938 if (td->use_aio)
939 cleanup_aio(td);
7292613b
JA
940 if (td->fd != -1) {
941 close(td->fd);
942 td->fd = -1;
943 }
2c83567e 944 cleanup_io_u(td);
599002b3 945 if (ret) {
892199bd 946 sem_post(&startup_sem);
599002b3
JA
947 sem_wait(&td->mutex);
948 }
02bdd9ba 949 td->runstate = TD_EXITED;
4240cfa1 950 shmdt(data);
892199bd
JA
951 return NULL;
952}
953
5c24b2c4 954static void free_shm(void)
892199bd
JA
955{
956 shmdt(threads);
957}
958
5c24b2c4 959static void show_thread_status(struct thread_data *td)
892199bd
JA
960{
961 int prio, prio_class;
962 unsigned long bw = 0;
fd1ae4c9 963 double n_lat, n_bw, m_lat, m_bw, dev_lat, dev_bw;
892199bd 964
213b446c
JA
965 if (!td->io_blocks && !td->error)
966 return;
967
892199bd 968 if (td->runtime)
4240cfa1 969 bw = (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
970
971 prio = td->ioprio & 0xff;
972 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
973
fd1ae4c9
JA
974 n_lat = (double) td->stat_time_samples;
975 n_bw = (double) td->stat_bw_samples;
976
977 m_lat = (double) td->stat_time / n_lat;
978 dev_lat = sqrt(((double) td->stat_time_sq - (m_lat * m_lat) / n_lat) / (n_lat - 1));
979 m_bw = (double) td->stat_bw / n_bw;
980 dev_bw = sqrt(((double) td->stat_bw_sq - (m_bw * m_bw) / n_bw) / (n_bw - 1));
e128065d 981
a642279f 982 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/sec, latmax=%5lumsec, latavg=%5.02fmsec, latdev=%5.02fmsec, bwavg=%5.02fKiB/sec, bwdev=%5.02fKiB/sec\n", td->thread_number, td->error, td->io_blocks * td->bs >> 20, bw, td->max_latency, m_lat, dev_lat, m_bw, dev_bw);
892199bd
JA
983}
984
5c24b2c4 985static int setup_rate(struct thread_data *td)
86184d14 986{
4240cfa1
JA
987 int nr_reads_per_sec;
988
989 if (!td->rate)
990 return 0;
991
992 if (td->rate < td->ratemin) {
993 fprintf(stderr, "min rate larger than nominal rate\n");
994 return -1;
995 }
86184d14 996
4240cfa1 997 nr_reads_per_sec = td->rate * 1024 / td->bs;
86184d14
JA
998 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
999 td->rate_pending_usleep = 0;
4240cfa1 1000 return 0;
86184d14
JA
1001}
1002
47d45203 1003static struct thread_data *get_new_job(int global)
892199bd 1004{
4240cfa1
JA
1005 struct thread_data *td;
1006
47d45203
JA
1007 if (global)
1008 return &def_thread;
8867c0a8 1009 if (thread_number >= max_jobs)
4240cfa1
JA
1010 return NULL;
1011
1012 td = &threads[thread_number++];
fc24389f 1013 memset(td, 0, sizeof(*td));
892199bd 1014
e4ed35c3 1015 td->fd = -1;
86184d14 1016 td->thread_number = thread_number;
47d45203
JA
1017 td->ddir = def_thread.ddir;
1018 td->bs = def_thread.bs;
1019 td->odirect = def_thread.odirect;
1020 td->ratecycle = def_thread.ratecycle;
1021 td->sequential = def_thread.sequential;
67903a2e 1022 td->timeout = def_thread.timeout;
02983297
JA
1023 td->create_file = def_thread.create_file;
1024 td->overwrite = def_thread.overwrite;
b95799ca 1025 td->invalidate_cache = def_thread.invalidate_cache;
47d45203 1026 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
f737299d
JA
1027
1028 return td;
1029}
1030
4240cfa1
JA
1031static void put_job(struct thread_data *td)
1032{
1033 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1034 thread_number--;
1035}
1036
5c24b2c4
JA
1037static int add_job(struct thread_data *td, const char *filename, int prioclass,
1038 int prio)
f737299d 1039{
47d45203
JA
1040 if (td == &def_thread)
1041 return 0;
1042
f737299d 1043 strcpy(td->file_name, filename);
4240cfa1 1044 sem_init(&td->mutex, 1, 0);
892199bd 1045 td->min_latency = 10000000;
f737299d
JA
1046 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1047
ed797f6e
JA
1048 if (td->use_aio) {
1049 if (!td->aio_depth)
1050 td->aio_depth = 1;
1051
1052 /*
1053 * no buffered aio support for now
1054 */
1055 if (!td->odirect)
1056 td->odirect = 1;
1057 }
43000118 1058
4240cfa1
JA
1059 if (setup_rate(td))
1060 return -1;
f737299d 1061
e128065d 1062 printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->bs, td->rate, td->use_aio, td->aio_depth);
4240cfa1 1063 return 0;
892199bd
JA
1064}
1065
18e0b78c
JA
1066static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1067{
f737299d 1068 unsigned int i;
18e0b78c
JA
1069
1070 CPU_ZERO(&cpumask);
1071
1072 for (i = 0; i < sizeof(int) * 8; i++) {
1073 if ((1 << i) & cpu)
1074 CPU_SET(i, &cpumask);
1075 }
1076}
1077
5c24b2c4 1078static void fill_option(const char *input, char *output)
892199bd
JA
1079{
1080 int i;
1081
1082 i = 0;
1083 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
1084 output[i] = input[i];
1085 i++;
1086 }
1087
1088 output[i] = '\0';
1089}
1090
02983297
JA
1091/*
1092 * convert string after '=' into decimal value, noting any size suffix
1093 */
1094static int str_cnv(char *p, unsigned long long *val)
1095{
1096 unsigned long mult;
1097 char *str;
1098 int len;
1099
1100 str = strstr(p, "=");
1101 if (!str)
1102 return 1;
1103
1104 str++;
1105 len = strlen(str);
1106 mult = 1;
1107
1108 switch (str[len - 2]) {
1109 case 'k':
1110 case 'K':
1111 mult = 1024;
1112 break;
1113 case 'm':
1114 case 'M':
1115 mult = 1024 * 1024;
1116 break;
1117 case 'g':
1118 case 'G':
1119 mult = 1024 * 1024 * 1024;
1120 break;
1121 }
1122
1123 *val = strtoul(str, NULL, 10);
1124 if (*val == ULONG_MAX && errno == ERANGE)
1125 return 1;
1126
1127 *val *= mult;
1128 return 0;
1129
1130}
1131
892199bd
JA
1132/*
1133 * job key words:
1134 *
1135 * file=
1136 * bs=
1137 * rw=
1138 * direct=
1139 */
5c24b2c4 1140static void parse_jobs_cmd(int argc, char *argv[], int index)
892199bd 1141{
f737299d
JA
1142 struct thread_data *td;
1143 unsigned int prio, prioclass, cpu;
892199bd
JA
1144 char *string, *filename, *p, *c;
1145 int i;
1146
1147 string = malloc(256);
1148 filename = malloc(256);
1149
1150 for (i = index; i < argc; i++) {
1151 p = argv[i];
1152
1153 c = strpbrk(p, "{");
1154 if (!c)
1155 break;
1156
1157 filename[0] = 0;
4240cfa1 1158
47d45203 1159 td = get_new_job(0);
4240cfa1
JA
1160 if (!td)
1161 break;
f737299d 1162
892199bd 1163 prioclass = 2;
f737299d 1164 prio = 4;
892199bd
JA
1165
1166 c = strstr(p, "rw=");
1167 if (c) {
1168 c += 3;
1169 if (*c == '0')
f737299d 1170 td->ddir = DDIR_READ;
892199bd 1171 else
f737299d 1172 td->ddir = DDIR_WRITE;
892199bd
JA
1173 }
1174
1175 c = strstr(p, "prio=");
1176 if (c) {
1177 c += 5;
1178 prio = *c - '0';
1179 }
1180
1181 c = strstr(p, "prioclass=");
1182 if (c) {
1183 c += 10;
1184 prioclass = *c - '0';
1185 }
1186
1187 c = strstr(p, "file=");
1188 if (c) {
1189 c += 5;
1190 fill_option(c, filename);
1191 }
1192
1193 c = strstr(p, "bs=");
1194 if (c) {
1195 c += 3;
1196 fill_option(c, string);
f737299d
JA
1197 td->bs = strtoul(string, NULL, 10);
1198 td->bs <<= 10;
892199bd
JA
1199 }
1200
1201 c = strstr(p, "direct=");
1202 if (c) {
1203 c += 7;
1204 if (*c != '0')
f737299d 1205 td->odirect = 1;
892199bd 1206 else
f737299d 1207 td->odirect = 0;
892199bd
JA
1208 }
1209
1210 c = strstr(p, "delay=");
1211 if (c) {
1212 c += 6;
1213 fill_option(c, string);
f737299d 1214 td->delay_sleep = strtoul(string, NULL, 10);
892199bd
JA
1215 }
1216
86184d14
JA
1217 c = strstr(p, "rate=");
1218 if (c) {
1219 c += 5;
1220 fill_option(c, string);
f737299d 1221 td->rate = strtoul(string, NULL, 10);
86184d14
JA
1222 }
1223
4240cfa1
JA
1224 c = strstr(p, "ratemin=");
1225 if (c) {
1226 c += 8;
1227 fill_option(c, string);
1228 td->ratemin = strtoul(string, NULL, 10);
1229 }
1230
1231 c = strstr(p, "ratecycle=");
1232 if (c) {
1233 c += 10;
1234 fill_option(c, string);
1235 td->ratecycle = strtoul(string, NULL, 10);
1236 }
1237
18e0b78c
JA
1238 c = strstr(p, "cpumask=");
1239 if (c) {
1240 c += 8;
1241 fill_option(c, string);
1242 cpu = strtoul(string, NULL, 10);
f737299d 1243 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1244 }
1245
4240cfa1
JA
1246 c = strstr(p, "fsync=");
1247 if (c) {
1248 c += 6;
1249 fill_option(c, string);
1250 td->fsync_blocks = strtoul(string, NULL, 10);
1251 }
18e0b78c 1252
fc24389f
JA
1253 c = strstr(p, "startdelay=");
1254 if (c) {
1255 c += 11;
1256 fill_option(c, string);
1257 td->start_delay = strtoul(string, NULL, 10);
1258 }
1259
67903a2e
JA
1260 c = strstr(p, "timeout=");
1261 if (c) {
1262 c += 8;
1263 fill_option(c, string);
1264 td->timeout = strtoul(string, NULL, 10);
1265 }
1266
b95799ca
JA
1267 c = strstr(p, "invalidate=");
1268 if (c) {
1269 c += 11;
1270 if (*c != '0')
1271 td->invalidate_cache = 1;
1272 else
1273 td->invalidate_cache = 0;
1274 }
1275
02983297
JA
1276 c = strstr(p, "size=");
1277 if (c) {
1278 c += 5;
1279 str_cnv(c, &td->file_size);
1280 }
1281
1282 c = strstr(p, "offset=");
1283 if (c) {
1284 c += 7;
1285 str_cnv(c, &td->file_offset);
1286 }
1287
43000118
JA
1288 c = strstr(p, "aio_depth=");
1289 if (c) {
1290 c += 10;
1291 fill_option(c, string);
1292 td->aio_depth = strtoul(string, NULL, 10);
1293 }
1294
1295 c = strstr(p, "aio");
1296 if (c)
1297 td->use_aio = 1;
1298
02983297
JA
1299 c = strstr(p, "create");
1300 if (c)
1301 td->create_file = 1;
1302
1303 c = strstr(p, "overwrite");
1304 if (c)
1305 td->overwrite = 1;
1306
892199bd
JA
1307 c = strstr(p, "random");
1308 if (c)
f737299d 1309 td->sequential = 0;
892199bd
JA
1310 c = strstr(p, "sequential");
1311 if (c)
f737299d 1312 td->sequential = 1;
892199bd 1313
4240cfa1
JA
1314 if (add_job(td, filename, prioclass, prio))
1315 put_job(td);
892199bd
JA
1316 }
1317
7dd1389e
JA
1318 free(string);
1319 free(filename);
892199bd
JA
1320}
1321
02983297
JA
1322static int check_strcnv(char *p, char *name, unsigned long long *val)
1323{
1324 if (!strstr(p, name))
1325 return 1;
1326
1327 return str_cnv(p, val);
1328}
1329
5c24b2c4 1330static int check_int(char *p, char *name, unsigned int *val)
7dd1389e
JA
1331{
1332 char str[128];
1333
1334 sprintf(str, "%s=%%d", name);
1335 if (sscanf(p, str, val) == 1)
1336 return 0;
1337
1338 sprintf(str, "%s = %%d", name);
1339 if (sscanf(p, str, val) == 1)
1340 return 0;
1341
1342 return 1;
1343}
1344
7292613b 1345static int is_empty_or_comment(char *line)
7dd1389e
JA
1346{
1347 unsigned int i;
1348
7292613b 1349 for (i = 0; i < strlen(line); i++) {
7292613b 1350 if (line[i] == ';')
47d45203
JA
1351 return 1;
1352 if (!isspace(line[i]) && !iscntrl(line[i]))
7292613b
JA
1353 return 0;
1354 }
7dd1389e
JA
1355
1356 return 1;
1357}
1358
5c24b2c4 1359static int parse_jobs_ini(char *file)
7dd1389e 1360{
47d45203 1361 unsigned int prioclass, prio, cpu, global;
f737299d 1362 struct thread_data *td;
7dd1389e
JA
1363 char *string, *name;
1364 fpos_t off;
1365 FILE *f;
1366 char *p;
1367
1368 f = fopen(file, "r");
1369 if (!f) {
1370 perror("fopen");
4240cfa1 1371 return 1;
7dd1389e
JA
1372 }
1373
1374 string = malloc(4096);
1375 name = malloc(256);
1376
7dd1389e 1377 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b
JA
1378 if (is_empty_or_comment(p))
1379 continue;
7dd1389e
JA
1380 if (sscanf(p, "[%s]", name) != 1)
1381 continue;
1382
47d45203
JA
1383 global = !strncmp(name, "global", 6);
1384
7dd1389e
JA
1385 name[strlen(name) - 1] = '\0';
1386
47d45203 1387 td = get_new_job(global);
4240cfa1
JA
1388 if (!td)
1389 break;
f737299d 1390
7dd1389e 1391 prioclass = 2;
f737299d 1392 prio = 4;
7dd1389e
JA
1393
1394 fgetpos(f, &off);
1395 while ((p = fgets(string, 4096, f)) != NULL) {
7292613b 1396 if (is_empty_or_comment(p))
e6402082
JA
1397 continue;
1398 if (strstr(p, "["))
7dd1389e 1399 break;
f737299d
JA
1400 if (!check_int(p, "bs", &td->bs)) {
1401 td->bs <<= 10;
7dd1389e
JA
1402 fgetpos(f, &off);
1403 continue;
1404 }
f737299d 1405 if (!check_int(p, "rw", &td->ddir)) {
7dd1389e
JA
1406 fgetpos(f, &off);
1407 continue;
1408 }
1409 if (!check_int(p, "prio", &prio)) {
1410 fgetpos(f, &off);
1411 continue;
1412 }
1413 if (!check_int(p, "prioclass", &prioclass)) {
1414 fgetpos(f, &off);
1415 continue;
1416 }
f737299d 1417 if (!check_int(p, "direct", &td->odirect)) {
7dd1389e
JA
1418 fgetpos(f, &off);
1419 continue;
1420 }
f737299d 1421 if (!check_int(p, "rate", &td->rate)) {
7dd1389e
JA
1422 fgetpos(f, &off);
1423 continue;
1424 }
4240cfa1
JA
1425 if (!check_int(p, "ratemin", &td->ratemin)) {
1426 fgetpos(f, &off);
1427 continue;
1428 }
1429 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1430 fgetpos(f, &off);
1431 continue;
1432 }
f737299d 1433 if (!check_int(p, "delay", &td->delay_sleep)) {
7dd1389e
JA
1434 fgetpos(f, &off);
1435 continue;
1436 }
18e0b78c 1437 if (!check_int(p, "cpumask", &cpu)) {
f737299d 1438 fill_cpu_mask(td->cpumask, cpu);
18e0b78c
JA
1439 fgetpos(f, &off);
1440 continue;
1441 }
4240cfa1
JA
1442 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1443 fgetpos(f, &off);
1444 continue;
1445 }
fc24389f
JA
1446 if (!check_int(p, "startdelay", &td->start_delay)) {
1447 fgetpos(f, &off);
1448 continue;
1449 }
67903a2e
JA
1450 if (!check_int(p, "timeout", &td->timeout)) {
1451 fgetpos(f, &off);
1452 continue;
1453 }
b95799ca
JA
1454 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1455 fgetpos(f, &off);
1456 continue;
1457 }
43000118
JA
1458 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1459 fgetpos(f, &off);
1460 continue;
1461 }
02983297
JA
1462 if (!check_strcnv(p, "size", &td->file_size)) {
1463 fgetpos(f, &off);
1464 continue;
1465 }
1466 if (!check_strcnv(p, "offset", &td->file_offset)) {
1467 fgetpos(f, &off);
1468 continue;
1469 }
43000118 1470 if (!strncmp(p, "sequential", 10)) {
f737299d 1471 td->sequential = 1;
7dd1389e
JA
1472 fgetpos(f, &off);
1473 continue;
1474 }
43000118 1475 if (!strncmp(p, "random", 6)) {
f737299d 1476 td->sequential = 0;
7dd1389e
JA
1477 fgetpos(f, &off);
1478 continue;
1479 }
43000118
JA
1480 if (!strncmp(p, "aio", 3)) {
1481 td->use_aio = 1;
1482 fgetpos(f, &off);
1483 continue;
1484 }
02983297
JA
1485 if (!strncmp(p, "create", 6)) {
1486 td->create_file = 1;
1487 fgetpos(f, &off);
1488 continue;
1489 }
1490 if (!strncmp(p, "overwrite", 9)) {
1491 td->overwrite = 1;
1492 fgetpos(f, &off);
1493 continue;
1494 }
e6402082 1495 printf("Client%d: bad option %s\n",td->thread_number,p);
7dd1389e
JA
1496 }
1497 fsetpos(f, &off);
1498
4240cfa1
JA
1499 if (add_job(td, name, prioclass, prio))
1500 put_job(td);
7dd1389e
JA
1501 }
1502
1503 free(string);
1504 free(name);
fc7d63df 1505 fclose(f);
4240cfa1 1506 return 0;
7dd1389e
JA
1507}
1508
5c24b2c4 1509static int parse_options(int argc, char *argv[])
892199bd 1510{
01c4d8de 1511 int i;
892199bd
JA
1512
1513 for (i = 1; i < argc; i++) {
1514 char *parm = argv[i];
1515
1516 if (parm[0] != '-')
1517 break;
1518
1519 parm++;
1520 switch (*parm) {
1521 case 's':
1522 parm++;
47d45203 1523 def_thread.sequential = !!atoi(parm);
892199bd
JA
1524 break;
1525 case 'b':
1526 parm++;
47d45203
JA
1527 def_thread.bs = atoi(parm);
1528 def_thread.bs <<= 10;
1529 if (!def_thread.bs) {
4240cfa1 1530 printf("bad block size\n");
47d45203 1531 def_thread.bs = DEF_BS;
4240cfa1 1532 }
892199bd
JA
1533 break;
1534 case 't':
1535 parm++;
47d45203 1536 def_thread.timeout = atoi(parm);
892199bd 1537 break;
892199bd
JA
1538 case 'r':
1539 parm++;
1540 repeatable = !!atoi(parm);
1541 break;
02bdd9ba
JA
1542 case 'R':
1543 parm++;
1544 rate_quit = !!atoi(parm);
1545 break;
892199bd
JA
1546 case 'o':
1547 parm++;
47d45203 1548 def_thread.odirect = !!atoi(parm);
892199bd 1549 break;
7dd1389e
JA
1550 case 'f':
1551 if (i + 1 >= argc) {
1552 printf("-f needs file as arg\n");
1553 break;
1554 }
1555 ini_file = strdup(argv[i+1]);
a642279f 1556 i++;
7dd1389e 1557 break;
892199bd 1558 default:
7dd1389e 1559 printf("bad option %s\n", argv[i]);
892199bd
JA
1560 break;
1561 }
1562 }
1563
892199bd
JA
1564 return i;
1565}
1566
3f39453a
JA
1567static void print_thread_status(struct thread_data *td, int nr_running,
1568 int t_rate, int m_rate, int die)
1569{
1570 printf("Client%d: %s\n", td->thread_number, die ? "exited" : "spawned");
1571
1572 printf("Threads now running: %d", nr_running);
1573 if (m_rate || t_rate)
1574 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1575 printf("\n");
1576}
1577
213b446c 1578static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
02bdd9ba 1579{
213b446c 1580 int i;
02bdd9ba 1581
3f39453a
JA
1582 /*
1583 * reap exited threads (TD_EXITED -> TD_REAPED)
1584 */
02bdd9ba
JA
1585 for (i = 0; i < thread_number; i++) {
1586 struct thread_data *td = &threads[i];
1587
213b446c
JA
1588 if (td->runstate != TD_EXITED)
1589 continue;
02bdd9ba 1590
213b446c
JA
1591 td->runstate = TD_REAPED;
1592 waitpid(td->pid, NULL, 0);
1593 (*nr_running)--;
1594 (*m_rate) -= td->ratemin;
1595 (*t_rate) -= td->rate;
e6402082
JA
1596
1597 if (td->terminate)
1598 continue;
1599
3f39453a 1600 print_thread_status(td, *nr_running, *t_rate, *m_rate, 1);
213b446c 1601 }
02bdd9ba
JA
1602}
1603
fc24389f
JA
1604static void run_threads(char *argv[])
1605{
be33abe4 1606 struct timeval genesis;
fc24389f
JA
1607 struct thread_data *td;
1608 unsigned long spent;
213b446c 1609 int i, todo, nr_running, m_rate, t_rate;
fc24389f
JA
1610
1611 gettimeofday(&genesis, NULL);
1612
1613 printf("Starting %d threads\n", thread_number);
1614 fflush(stdout);
1615
7292613b
JA
1616 signal(SIGINT, sig_handler);
1617
fc24389f 1618 todo = thread_number;
02bdd9ba 1619 nr_running = 0;
213b446c 1620 m_rate = t_rate = 0;
fc24389f 1621
213b446c 1622 while (todo) {
3f39453a
JA
1623 /*
1624 * create threads (TD_NOT_CREATED -> TD_CREATED)
1625 */
fc24389f
JA
1626 for (i = 0; i < thread_number; i++) {
1627 td = &threads[i];
1628
02bdd9ba 1629 if (td->runstate != TD_NOT_CREATED)
fc24389f
JA
1630 continue;
1631
213b446c
JA
1632 /*
1633 * never got a chance to start, killed by other
1634 * thread for some reason
1635 */
1636 if (td->terminate) {
1637 todo--;
1638 continue;
1639 }
1640
fc24389f 1641 if (td->start_delay) {
be33abe4 1642 spent = mtime_since_now(&genesis);
fc24389f
JA
1643
1644 if (td->start_delay * 1000 > spent)
1645 continue;
1646 }
1647
02bdd9ba 1648 td->runstate = TD_CREATED;
fc24389f
JA
1649 sem_init(&startup_sem, 1, 1);
1650 todo--;
1651
1652 if (fork())
1653 sem_wait(&startup_sem);
1654 else {
1655 thread_main(shm_id, i, argv);
1656 exit(0);
1657 }
1658 }
1659
3f39453a
JA
1660 /*
1661 * start created threads (TD_CREATED -> TD_STARTED)
1662 */
fc24389f
JA
1663 for (i = 0; i < thread_number; i++) {
1664 struct thread_data *td = &threads[i];
1665
3f39453a
JA
1666 if (td->runstate != TD_CREATED)
1667 continue;
1668
1669 td->runstate = TD_STARTED;
1670 nr_running++;
1671 m_rate += td->ratemin;
1672 t_rate += td->rate;
1673 sem_post(&td->mutex);
1674
1675 print_thread_status(td, nr_running, t_rate, m_rate, 0);
fc24389f
JA
1676 }
1677
213b446c 1678 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba 1679
fc24389f
JA
1680 if (todo)
1681 usleep(100000);
1682 }
02bdd9ba
JA
1683
1684 while (nr_running) {
213b446c 1685 reap_threads(&nr_running, &t_rate, &m_rate);
02bdd9ba
JA
1686 usleep(10000);
1687 }
fc24389f
JA
1688}
1689
8867c0a8 1690int setup_thread_area(void)
892199bd 1691{
8867c0a8
JA
1692 /*
1693 * 1024 is too much on some machines, scale max_jobs if
1694 * we get a failure that looks like too large a shm segment
1695 */
1696 do {
1697 int s = max_jobs * sizeof(struct thread_data);
18e0b78c 1698
8867c0a8
JA
1699 shm_id = shmget(0, s, IPC_CREAT | 0600);
1700 if (shm_id != -1)
1701 break;
1702 if (errno != EINVAL) {
1703 perror("shmget");
1704 break;
1705 }
1706
1707 max_jobs >>= 1;
d4fac444 1708 } while (max_jobs);
8867c0a8
JA
1709
1710 if (shm_id == -1)
892199bd 1711 return 1;
892199bd
JA
1712
1713 threads = shmat(shm_id, NULL, 0);
8867c0a8 1714 if (threads == (void *) -1) {
86184d14
JA
1715 perror("shmat");
1716 return 1;
1717 }
892199bd
JA
1718
1719 atexit(free_shm);
8867c0a8
JA
1720 return 0;
1721}
1722
1723int main(int argc, char *argv[])
1724{
1725 static unsigned long max_run[2], min_run[2], total_blocks[2];
1726 static unsigned long max_bw[2], min_bw[2], maxl[2], minl[2];
1727 static unsigned long read_mb, write_mb, read_agg, write_agg;
1728 int i;
1729
1730 if (setup_thread_area())
1731 return 1;
892199bd 1732
47d45203 1733 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
4240cfa1
JA
1734 perror("sched_getaffinity");
1735 return 1;
1736 }
1737
47d45203
JA
1738 /*
1739 * fill globals
1740 */
1741 def_thread.ddir = DDIR_READ;
1742 def_thread.bs = DEF_BS;
02983297 1743 def_thread.odirect = DEF_ODIRECT;
47d45203 1744 def_thread.ratecycle = DEF_RATE_CYCLE;
02983297 1745 def_thread.sequential = DEF_SEQUENTIAL;
47d45203 1746 def_thread.timeout = DEF_TIMEOUT;
02983297
JA
1747 def_thread.create_file = DEF_CREATE;
1748 def_thread.overwrite = DEF_OVERWRITE;
b95799ca 1749 def_thread.invalidate_cache = DEF_INVALIDATE;
47d45203 1750
892199bd 1751 i = parse_options(argc, argv);
7dd1389e 1752
4240cfa1
JA
1753 if (ini_file) {
1754 if (parse_jobs_ini(ini_file))
1755 return 1;
1756 } else
1757 parse_jobs_cmd(argc, argv, i);
7dd1389e 1758
4240cfa1
JA
1759 if (!thread_number) {
1760 printf("Nothing to do\n");
1761 return 1;
1762 }
7dd1389e 1763
fc24389f 1764 run_threads(argv);
892199bd 1765
892199bd
JA
1766 min_bw[0] = min_run[0] = ~0UL;
1767 min_bw[1] = min_run[1] = ~0UL;
1768 minl[0] = minl[1] = ~0UL;
1769 for (i = 0; i < thread_number; i++) {
1770 struct thread_data *td = &threads[i];
1771 unsigned long bw = 0;
1772
1773 if (td->error)
7dd1389e 1774 goto show_stat;
892199bd
JA
1775
1776 if (td->runtime < min_run[td->ddir])
1777 min_run[td->ddir] = td->runtime;
1778 if (td->runtime > max_run[td->ddir])
1779 max_run[td->ddir] = td->runtime;
1780
1781 if (td->runtime)
4240cfa1 1782 bw = (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
1783 if (bw < min_bw[td->ddir])
1784 min_bw[td->ddir] = bw;
1785 if (bw > max_bw[td->ddir])
1786 max_bw[td->ddir] = bw;
1787 if (td->max_latency < minl[td->ddir])
1788 minl[td->ddir] = td->max_latency;
1789 if (td->max_latency > maxl[td->ddir])
1790 maxl[td->ddir] = td->max_latency;
1791
4240cfa1 1792 total_blocks[td->ddir] += td->io_blocks;
892199bd 1793
02983297 1794 if (td_read(td)) {
4240cfa1 1795 read_mb += (td->bs * td->io_blocks) >> 20;
892199bd 1796 if (td->runtime)
4240cfa1 1797 read_agg += (td->io_blocks * td->bs) / td->runtime;
02983297 1798 } else {
4240cfa1 1799 write_mb += (td->bs * td->io_blocks) >> 20;
892199bd 1800 if (td->runtime)
4240cfa1 1801 write_agg += (td->io_blocks * td->bs) / td->runtime;
892199bd
JA
1802 }
1803
7dd1389e 1804show_stat:
892199bd
JA
1805 show_thread_status(td);
1806 }
1807
1808 printf("Run status:\n");
1809 if (max_run[DDIR_READ])
1810 printf(" READ: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", read_mb, read_agg, minl[0], maxl[0], min_bw[0], max_bw[0], min_run[0], max_run[0]);
1811 if (max_run[DDIR_WRITE])
1812 printf(" WRITE: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", write_mb, write_agg, minl[1], maxl[1], min_bw[1], max_bw[1], min_run[1], max_run[1]);
fc24389f 1813
892199bd
JA
1814 return 0;
1815}