2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
35 #include <sys/types.h>
38 #include <semaphore.h>
41 #include <asm/unistd.h>
42 #include <asm/types.h>
47 #define MAX_JOBS (1024)
50 * assume we don't have _get either, if _set isn't defined
52 #ifndef __NR_ioprio_set
54 #define __NR_ioprio_set 289
55 #define __NR_ioprio_get 290
56 #elif defined(__powerpc__) || defined(__powerpc64__)
57 #define __NR_ioprio_set 273
58 #define __NR_ioprio_get 274
59 #elif defined(__x86_64__)
60 #define __NR_ioprio_set 251
61 #define __NR_ioprio_get 252
62 #elif defined(__ia64__)
63 #define __NR_ioprio_set 1274
64 #define __NR_ioprio_get 1275
65 #elif defined(__alpha__)
66 #define __NR_ioprio_set 442
67 #define __NR_ioprio_get 443
68 #elif defined(__s390x__) || defined(__s390__)
69 #define __NR_ioprio_set 282
70 #define __NR_ioprio_get 283
72 #error "Unsupported arch"
76 #ifndef __NR_fadvise64
78 #define __NR_fadvise64 250
79 #elif defined(__powerpc__) || defined(__powerpc64__)
80 #define __NR_fadvise64 233
81 #elif defined(__x86_64__)
82 #define __NR_fadvise64 221
83 #elif defined(__ia64__)
84 #define __NR_fadvise64 1234
85 #elif defined(__alpha__)
86 #define __NR_fadvise64 413
87 #elif defined(__s390x__) || defined(__s390__)
88 #define __NR_fadvise64 253
90 #error "Unsupported arch"
94 static int ioprio_set(int which, int who, int ioprio)
96 return syscall(__NR_ioprio_set, which, who, ioprio);
100 * we want fadvise64 really, but it's so tangled... later
102 static int fadvise(int fd, loff_t offset, size_t len, int advice)
105 return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice);
107 return posix_fadvise(fd, (off_t) offset, len, advice);
112 IOPRIO_WHO_PROCESS = 1,
117 #define IOPRIO_CLASS_SHIFT 13
121 #define DEF_BS (4096)
122 #define DEF_TIMEOUT (30)
123 #define DEF_RATE_CYCLE (1000)
124 #define DEF_ODIRECT (1)
125 #define DEF_SEQUENTIAL (1)
126 #define DEF_RAND_REPEAT (1)
127 #define DEF_OVERWRITE (0)
128 #define DEF_CREATE (1)
129 #define DEF_INVALIDATE (1)
130 #define DEF_SYNCIO (0)
131 #define DEF_RANDSEED (0xb1899bedUL)
132 #define DEF_BWAVGTIME (500)
133 #define DEF_CREATE_SER (1)
134 #define DEF_CREATE_FSYNC (1)
135 #define DEF_LOOPS (1)
136 #define DEF_VERIFY (0)
138 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
140 static int repeatable = DEF_RAND_REPEAT;
141 static int rate_quit = 1;
142 static int write_lat_log;
143 static int write_bw_log;
144 static int exitall_on_terminate;
146 static int thread_number;
147 static char *ini_file;
149 static int max_jobs = MAX_JOBS;
151 static char run_str[MAX_JOBS + 1];
182 struct timeval start_time;
183 struct timeval issue_time;
187 unsigned long long offset;
189 struct list_head list;
194 unsigned long val_sq;
195 unsigned long max_val;
196 unsigned long min_val;
197 unsigned long samples;
206 unsigned long nr_samples;
207 unsigned long max_samples;
208 struct io_sample *log;
212 struct list_head list;
213 unsigned long long offset;
217 #define FIO_HDR_MAGIC 0xf00baaef
219 struct verify_header {
220 unsigned int fio_magic;
222 char md5_digest[MD5_HASH_WORDS * 4];
225 #define td_read(td) ((td)->ddir == DDIR_READ)
226 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
235 volatile int terminate;
236 volatile int runstate;
237 volatile int old_runstate;
240 unsigned int sequential;
244 unsigned int odirect;
245 unsigned int thinktime;
246 unsigned int fsync_blocks;
247 unsigned int start_delay;
248 unsigned int timeout;
249 unsigned int use_aio;
250 unsigned int create_file;
251 unsigned int overwrite;
252 unsigned int invalidate_cache;
253 unsigned int bw_avg_time;
254 unsigned int create_serialize;
255 unsigned int create_fsync;
257 unsigned long long file_size;
258 unsigned long long file_offset;
259 unsigned int sync_io;
260 unsigned int mem_type;
264 struct drand48_data bsrange_state;
265 struct drand48_data verify_state;
271 io_context_t aio_ctx;
272 unsigned int aio_depth;
273 struct io_event *aio_events;
275 unsigned int cur_depth;
276 struct list_head io_u_freelist;
277 struct list_head io_u_busylist;
280 unsigned int ratemin;
281 unsigned int ratecycle;
282 unsigned long rate_usec_cycle;
283 long rate_pending_usleep;
284 unsigned long rate_kb;
285 struct timeval lastrate;
287 unsigned long runtime; /* sec */
289 unsigned long io_blocks;
291 unsigned long this_io_kb;
292 unsigned long last_kb;
294 struct drand48_data random_state;
297 * bandwidth and latency stats
299 struct io_stat clat_stat; /* completion latency */
300 struct io_stat slat_stat; /* submission latency */
302 struct io_stat bw_stat; /* bandwidth stats */
303 unsigned long stat_io_kb;
304 struct timeval stat_sample_time;
306 struct io_log *lat_log;
307 struct io_log *bw_log;
309 struct timeval start;
311 struct list_head io_hist_list;
314 static struct thread_data *threads;
315 static struct thread_data def_thread;
317 static sem_t startup_sem;
319 static void sig_handler(int sig)
323 for (i = 0; i < thread_number; i++) {
324 struct thread_data *td = &threads[i];
331 static int init_random_state(struct thread_data *td)
336 fd = open("/dev/random", O_RDONLY);
342 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
350 srand48_r(seed, &td->bsrange_state);
351 srand48_r(seed, &td->verify_state);
359 srand48_r(seed, &td->random_state);
363 static unsigned long utime_since(struct timeval *s, struct timeval *e)
367 sec = e->tv_sec - s->tv_sec;
368 usec = e->tv_usec - s->tv_usec;
369 if (sec > 0 && usec < 0) {
374 sec *= (double) 1000000;
379 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
383 sec = e->tv_sec - s->tv_sec;
384 usec = e->tv_usec - s->tv_usec;
385 if (sec > 0 && usec < 0) {
390 sec *= (double) 1000;
391 usec /= (double) 1000;
396 static unsigned long mtime_since_now(struct timeval *s)
400 gettimeofday(&t, NULL);
401 return mtime_since(s, &t);
404 static inline unsigned long msec_now(struct timeval *s)
406 return s->tv_sec * 1000 + s->tv_usec / 1000;
409 static unsigned long long get_next_offset(struct thread_data *td)
411 unsigned long long kb;
414 if (!td->sequential) {
415 int min_bs_kb = td->min_bs >> 10;
417 lrand48_r(&td->random_state, &r);
418 kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
419 kb = (kb + min_bs_kb - 1) & ~(min_bs_kb - 1);
423 return (kb << 10) + td->file_offset;
426 static unsigned int get_next_buflen(struct thread_data *td)
431 if (td->min_bs == td->max_bs)
434 lrand48_r(&td->bsrange_state, &r);
435 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
436 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
439 if (buflen > ((td->kb - td->this_io_kb) << 10))
440 buflen = (td->kb - td->this_io_kb) << 10;
442 td->last_kb += buflen >> 10;
446 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
449 if (val > is->max_val)
451 if (val < is->min_val)
455 is->val_sq += val * val;
459 static void add_log_sample(struct thread_data *td, struct io_log *log,
462 if (log->nr_samples == log->max_samples) {
463 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
465 log->log = realloc(log->log, new_size);
466 log->max_samples <<= 1;
469 log->log[log->nr_samples].val = val;
470 log->log[log->nr_samples].time = mtime_since_now(&td->start);
474 static void add_clat_sample(struct thread_data *td, unsigned long msec)
476 add_stat_sample(td, &td->clat_stat, msec);
479 add_log_sample(td, td->lat_log, msec);
482 static void add_slat_sample(struct thread_data *td, unsigned long msec)
484 add_stat_sample(td, &td->slat_stat, msec);
487 static void add_bw_sample(struct thread_data *td)
489 unsigned long spent = mtime_since_now(&td->stat_sample_time);
492 if (spent < td->bw_avg_time)
495 rate = ((td->this_io_kb - td->stat_io_kb) * 1024) / spent;
496 add_stat_sample(td, &td->bw_stat, rate);
499 add_log_sample(td, td->bw_log, rate);
501 gettimeofday(&td->stat_sample_time, NULL);
502 td->stat_io_kb = td->this_io_kb;
505 static void usec_sleep(int usec)
507 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
511 rem.tv_sec = rem.tv_nsec = 0;
512 nanosleep(&req, &rem);
516 req.tv_nsec = rem.tv_nsec;
520 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
523 unsigned long usec_cycle;
528 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
530 if (time_spent < usec_cycle) {
531 unsigned long s = usec_cycle - time_spent;
533 td->rate_pending_usleep += s;
534 if (td->rate_pending_usleep >= 100000) {
535 usec_sleep(td->rate_pending_usleep);
536 td->rate_pending_usleep = 0;
539 long overtime = time_spent - usec_cycle;
541 td->rate_pending_usleep -= overtime;
545 static int check_min_rate(struct thread_data *td, struct timeval *now)
551 * allow a 2 second settle period in the beginning
553 if (mtime_since(&td->start, now) < 2000)
557 * if rate blocks is set, sample is running
560 spent = mtime_since(&td->lastrate, now);
561 if (spent < td->ratecycle)
564 rate = ((td->this_io_kb - td->rate_kb) * 1024) / spent;
565 if (rate < td->ratemin) {
566 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
573 td->rate_kb = td->this_io_kb;
574 memcpy(&td->lastrate, now, sizeof(*now));
578 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
580 if (mtime_since(&td->start, t) >= td->timeout * 1000)
586 static void fill_random_bytes(struct thread_data *td,
587 unsigned char *p, unsigned int len)
593 drand48_r(&td->verify_state, &r);
596 * lrand48_r seems to be broken and only fill the bottom
597 * 32-bits, even on 64-bit archs with 64-bit longs
610 static int verify_io_u(struct io_u *io_u)
612 struct verify_header *hdr = (struct verify_header *) io_u->buf;
613 unsigned char *p = (unsigned char *) io_u->buf;
614 struct md5_ctx md5_ctx;
616 if (hdr->fio_magic != FIO_HDR_MAGIC)
619 memset(&md5_ctx, 0, sizeof(md5_ctx));
621 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
623 return memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
627 * fill body of io_u->buf with random data and add a header with the
628 * (eg) sha1sum of that data.
630 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
632 struct md5_ctx md5_ctx;
633 struct verify_header hdr;
634 unsigned char *p = (unsigned char *) io_u->buf;
636 hdr.fio_magic = FIO_HDR_MAGIC;
637 hdr.len = io_u->buflen;
639 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
641 memset(&md5_ctx, 0, sizeof(md5_ctx));
642 md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr));
643 memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
644 memcpy(io_u->buf, &hdr, sizeof(hdr));
647 static void put_io_u(struct thread_data *td, struct io_u *io_u)
649 list_del(&io_u->list);
650 list_add(&io_u->list, &td->io_u_freelist);
654 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
656 static struct io_u *__get_io_u(struct thread_data *td)
663 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
664 list_del(&io_u->list);
665 list_add(&io_u->list, &td->io_u_busylist);
670 static struct io_u *get_io_u(struct thread_data *td)
674 io_u = __get_io_u(td);
678 io_u->offset = get_next_offset(td);
679 io_u->buflen = get_next_buflen(td);
685 if (!td_read(td) && td->verify)
686 populate_io_u(td, io_u);
690 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
692 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
695 gettimeofday(&io_u->start_time, NULL);
699 static inline void td_set_runstate(struct thread_data *td, int runstate)
701 td->old_runstate = td->runstate;
702 td->runstate = runstate;
705 static int get_next_verify(struct thread_data *td,
706 unsigned long long *offset, unsigned int *len)
708 struct io_piece *ipo;
710 if (list_empty(&td->io_hist_list))
713 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
714 list_del(&ipo->list);
716 *offset = ipo->offset;
722 static void do_sync_verify(struct thread_data *td)
725 struct io_u *io_u = NULL;
728 td_set_runstate(td, TD_VERIFYING);
730 io_u = __get_io_u(td);
733 unsigned long size = td->kb << 10;
735 if (fadvise(td->fd, 0, size, POSIX_FADV_DONTNEED) < 0) {
745 gettimeofday(&t, NULL);
746 if (runtime_exceeded(td, &t))
749 if (get_next_verify(td, &io_u->offset, &io_u->buflen))
752 if (td->cur_off != io_u->offset) {
753 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
759 ret = read(td->fd, io_u->buf, io_u->buflen);
760 if (ret < (int) io_u->buflen) {
770 if (verify_io_u(io_u))
773 td->cur_off = io_u->offset + io_u->buflen;
777 td_set_runstate(td, TD_RUNNING);
782 * log a succesful write, so we can unwind the log for verify
784 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
786 struct io_piece *ipo = malloc(sizeof(*ipo));
787 struct list_head *entry;
789 INIT_LIST_HEAD(&ipo->list);
790 ipo->offset = io_u->offset;
791 ipo->len = io_u->buflen;
793 if (td->sequential) {
794 list_add_tail(&ipo->list, &td->io_hist_list);
799 * for random io, sort the list so verify will run faster
801 entry = &td->io_hist_list;
802 while ((entry = entry->prev) != &td->io_hist_list) {
803 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
805 if (__ipo->offset == ipo->offset &&
806 __ipo->len == ipo->len) {
810 } else if (__ipo->offset < ipo->offset)
815 list_add(&ipo->list, entry);
818 static void do_sync_io(struct thread_data *td)
820 unsigned long msec, usec;
821 struct io_u *io_u = NULL;
824 while (td->this_io_kb < td->kb) {
834 if (td->cur_off != io_u->offset) {
835 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
842 ret = read(td->fd, io_u->buf, io_u->buflen);
844 ret = write(td->fd, io_u->buf, io_u->buflen);
846 if (ret < (int) io_u->buflen) {
853 log_io_piece(td, io_u);
856 td->io_kb += io_u->buflen >> 10;
857 td->this_io_kb += io_u->buflen >> 10;
858 td->cur_off = io_u->offset + io_u->buflen;
860 gettimeofday(&e, NULL);
862 usec = utime_since(&io_u->start_time, &e);
864 rate_throttle(td, usec, io_u->buflen);
866 if (check_min_rate(td, &e)) {
872 add_clat_sample(td, msec);
875 if (runtime_exceeded(td, &e))
882 usec_sleep(td->thinktime);
884 if (should_fsync(td) && td->fsync_blocks &&
885 (td->io_blocks % td->fsync_blocks) == 0)
892 if (should_fsync(td))
896 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
898 struct iocb *iocb = &io_u->iocb;
902 ret = io_submit(td->aio_ctx, 1, &iocb);
905 else if (ret == EAGAIN)
907 else if (ret == EINTR)
916 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
917 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
919 static int ios_completed(struct thread_data *td, int nr)
926 gettimeofday(&e, NULL);
928 for (i = 0, bytes_done = 0; i < nr; i++) {
929 io_u = ev_to_iou(td->aio_events + i);
932 td->io_kb += io_u->buflen >> 10;
933 td->this_io_kb += io_u->buflen >> 10;
935 msec = mtime_since(&io_u->issue_time, &e);
937 add_clat_sample(td, msec);
941 log_io_piece(td, io_u);
943 bytes_done += io_u->buflen;
950 static void cleanup_pending_aio(struct thread_data *td)
952 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
953 struct list_head *entry, *n;
958 * get immediately available events, if any
960 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
962 ios_completed(td, r);
965 * now cancel remaining active events
967 list_for_each_safe(entry, n, &td->io_u_busylist) {
968 io_u = list_entry(entry, struct io_u, list);
970 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
976 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
978 ios_completed(td, r);
982 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
984 struct io_u *v_io_u = *io_u;
988 ret = verify_io_u(v_io_u);
989 put_io_u(td, v_io_u);
996 static void do_async_verify(struct thread_data *td)
999 struct io_u *io_u, *v_io_u = NULL;
1002 td_set_runstate(td, TD_VERIFYING);
1008 gettimeofday(&t, NULL);
1009 if (runtime_exceeded(td, &t))
1012 io_u = __get_io_u(td);
1016 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
1021 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
1022 ret = io_u_queue(td, io_u);
1030 * we have one pending to verify, do that while the next
1031 * we are doing io on the next one
1033 if (async_do_verify(td, &v_io_u))
1036 ret = io_getevents(td->aio_ctx, 1, 1, td->aio_events, NULL);
1043 v_io_u = ev_to_iou(td->aio_events);
1045 td->cur_off = v_io_u->offset + v_io_u->buflen;
1048 * if we can't submit more io, we need to verify now
1050 if (queue_full(td) && async_do_verify(td, &v_io_u))
1055 async_do_verify(td, &v_io_u);
1058 cleanup_pending_aio(td);
1060 td_set_runstate(td, TD_RUNNING);
1063 static void do_async_io(struct thread_data *td)
1065 struct timeval s, e;
1068 while (td->this_io_kb < td->kb) {
1069 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1070 struct timespec *timeout;
1071 int ret, min_evts = 0;
1073 unsigned int bytes_done;
1078 io_u = get_io_u(td);
1082 memcpy(&s, &io_u->start_time, sizeof(s));
1084 ret = io_u_queue(td, io_u);
1091 gettimeofday(&io_u->issue_time, NULL);
1092 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1093 if (td->cur_depth < td->aio_depth) {
1101 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
1108 bytes_done = ios_completed(td, ret);
1111 * the rate is batched for now, it should work for batches
1112 * of completions except the very first one which may look
1115 gettimeofday(&e, NULL);
1116 usec = utime_since(&s, &e);
1118 rate_throttle(td, usec, bytes_done);
1120 if (check_min_rate(td, &e)) {
1121 td->error = ENODATA;
1125 if (runtime_exceeded(td, &e))
1129 usec_sleep(td->thinktime);
1131 if (should_fsync(td) && td->fsync_blocks &&
1132 (td->io_blocks % td->fsync_blocks) == 0)
1137 cleanup_pending_aio(td);
1139 if (should_fsync(td))
1143 static void cleanup_aio(struct thread_data *td)
1145 io_destroy(td->aio_ctx);
1148 free(td->aio_events);
1151 static int init_aio(struct thread_data *td)
1153 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
1158 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
1162 static void cleanup_io_u(struct thread_data *td)
1164 struct list_head *entry, *n;
1167 list_for_each_safe(entry, n, &td->io_u_freelist) {
1168 io_u = list_entry(entry, struct io_u, list);
1170 list_del(&io_u->list);
1174 if (td->mem_type == MEM_MALLOC)
1175 free(td->orig_buffer);
1176 else if (td->mem_type == MEM_SHM) {
1177 struct shmid_ds sbuf;
1179 shmdt(td->orig_buffer);
1180 shmctl(td->shm_id, IPC_RMID, &sbuf);
1184 static int init_io_u(struct thread_data *td)
1187 int i, max_units, mem_size;
1193 max_units = td->aio_depth;
1195 mem_size = td->max_bs * max_units + MASK;
1197 if (td->mem_type == MEM_MALLOC)
1198 td->orig_buffer = malloc(mem_size);
1199 else if (td->mem_type == MEM_SHM) {
1200 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
1201 if (td->shm_id < 0) {
1207 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1208 if (td->orig_buffer == (void *) -1) {
1215 INIT_LIST_HEAD(&td->io_u_freelist);
1216 INIT_LIST_HEAD(&td->io_u_busylist);
1217 INIT_LIST_HEAD(&td->io_hist_list);
1219 p = ALIGN(td->orig_buffer);
1220 for (i = 0; i < max_units; i++) {
1221 io_u = malloc(sizeof(*io_u));
1222 memset(io_u, 0, sizeof(*io_u));
1223 INIT_LIST_HEAD(&io_u->list);
1225 io_u->buf = p + td->max_bs * i;
1226 list_add(&io_u->list, &td->io_u_freelist);
1232 static void setup_log(struct io_log **log)
1234 struct io_log *l = malloc(sizeof(*l));
1237 l->max_samples = 1024;
1238 l->log = malloc(l->max_samples * sizeof(struct io_sample));
1242 static void finish_log(struct thread_data *td, struct io_log *log, char *name)
1244 char file_name[128];
1248 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
1249 f = fopen(file_name, "w");
1251 perror("fopen log");
1255 for (i = 0; i < log->nr_samples; i++)
1256 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
1263 static int create_file(struct thread_data *td)
1265 unsigned long long left;
1271 * unless specifically asked for overwrite, let normal io extend it
1273 if (!td_read(td) && !td->overwrite)
1276 if (!td->file_size) {
1277 fprintf(stderr, "Need size for create\n");
1282 printf("Client%d: Laying out IO file\n", td->thread_number);
1284 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1290 td->kb = td->file_size >> 10;
1291 b = malloc(td->max_bs);
1292 memset(b, 0, td->max_bs);
1294 left = td->file_size;
1300 r = write(td->fd, b, bs);
1302 if (r == (int) bs) {
1315 if (td->create_fsync)
1324 static int file_exists(struct thread_data *td)
1328 if (stat(td->file_name, &st) != -1)
1331 return errno != ENOENT;
1334 static int setup_file(struct thread_data *td)
1339 if (!file_exists(td)) {
1340 if (!td->create_file) {
1344 if (create_file(td))
1352 td->fd = open(td->file_name, flags | O_RDONLY);
1363 td->fd = open(td->file_name, flags | O_CREAT, 0600);
1372 if (fstat(td->fd, &st) == -1) {
1377 if (td->file_size > st.st_size)
1378 st.st_size = td->file_size;
1381 td->file_size = 1024 * 1024 * 1024;
1383 st.st_size = td->file_size;
1386 td->kb = (st.st_size - td->file_offset) / 1024;
1388 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1393 if (td->invalidate_cache) {
1394 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1403 static void clear_io_state(struct thread_data *td)
1411 static void *thread_main(int shm_id, int offset, char *argv[])
1413 struct thread_data *td;
1419 data = shmat(shm_id, NULL, 0);
1420 if (data == (void *) -1) {
1425 td = data + offset * sizeof(struct thread_data);
1431 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1436 sprintf(argv[0], "fio%d", offset);
1438 if (td->use_aio && init_aio(td))
1441 if (init_random_state(td))
1445 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1451 sem_post(&startup_sem);
1452 sem_wait(&td->mutex);
1454 if (!td->create_serialize && setup_file(td))
1457 gettimeofday(&td->start, NULL);
1459 while (td->loops--) {
1460 gettimeofday(&td->stat_sample_time, NULL);
1463 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1483 do_async_verify(td);
1489 td->runtime = mtime_since_now(&td->start);
1493 finish_log(td, td->bw_log, "bw");
1495 finish_log(td, td->lat_log, "lat");
1497 if (exitall_on_terminate)
1509 sem_post(&startup_sem);
1510 sem_wait(&td->mutex);
1512 td_set_runstate(td, TD_EXITED);
1517 static void free_shm(void)
1519 struct shmid_ds sbuf;
1524 shmctl(shm_id, IPC_RMID, &sbuf);
1528 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1529 double *mean, double *dev)
1533 if (is->samples == 0)
1539 n = (double) is->samples;
1540 *mean = (double) is->val / n;
1541 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1545 static void show_thread_status(struct thread_data *td)
1547 int prio, prio_class;
1548 unsigned long min, max, bw = 0;
1551 if (!td->io_kb && !td->error)
1555 bw = td->io_kb * 1024 / td->runtime;
1557 prio = td->ioprio & 0xff;
1558 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1560 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);
1562 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1563 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1564 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1565 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1566 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1567 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1570 static int setup_rate(struct thread_data *td)
1572 int nr_reads_per_sec;
1577 if (td->rate < td->ratemin) {
1578 fprintf(stderr, "min rate larger than nominal rate\n");
1582 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
1583 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1584 td->rate_pending_usleep = 0;
1588 static struct thread_data *get_new_job(int global)
1590 struct thread_data *td;
1594 if (thread_number >= max_jobs)
1597 td = &threads[thread_number++];
1598 memset(td, 0, sizeof(*td));
1601 td->thread_number = thread_number;
1603 td->ddir = def_thread.ddir;
1604 td->ioprio = def_thread.ioprio;
1605 td->sequential = def_thread.sequential;
1606 td->bs = def_thread.bs;
1607 td->min_bs = def_thread.min_bs;
1608 td->max_bs = def_thread.max_bs;
1609 td->odirect = def_thread.odirect;
1610 td->thinktime = def_thread.thinktime;
1611 td->fsync_blocks = def_thread.fsync_blocks;
1612 td->start_delay = def_thread.start_delay;
1613 td->timeout = def_thread.timeout;
1614 td->use_aio = def_thread.use_aio;
1615 td->create_file = def_thread.create_file;
1616 td->overwrite = def_thread.overwrite;
1617 td->invalidate_cache = def_thread.invalidate_cache;
1618 td->file_size = def_thread.file_size;
1619 td->file_offset = def_thread.file_offset;
1620 td->rate = def_thread.rate;
1621 td->ratemin = def_thread.ratemin;
1622 td->ratecycle = def_thread.ratecycle;
1623 td->aio_depth = def_thread.aio_depth;
1624 td->sync_io = def_thread.sync_io;
1625 td->mem_type = def_thread.mem_type;
1626 td->bw_avg_time = def_thread.bw_avg_time;
1627 td->create_serialize = def_thread.create_serialize;
1628 td->create_fsync = def_thread.create_fsync;
1629 td->loops = def_thread.loops;
1630 td->verify = def_thread.verify;
1631 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1636 static void put_job(struct thread_data *td)
1638 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1642 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1645 if (td == &def_thread)
1648 strcpy(td->file_name, filename);
1649 sem_init(&td->mutex, 1, 0);
1650 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1652 td->clat_stat.min_val = ULONG_MAX;
1653 td->slat_stat.min_val = ULONG_MAX;
1654 td->bw_stat.min_val = ULONG_MAX;
1656 run_str[td->thread_number - 1] = 'P';
1658 if (td->use_aio && !td->aio_depth)
1661 if (td->min_bs == -1U)
1662 td->min_bs = td->bs;
1663 if (td->max_bs == -1U)
1664 td->max_bs = td->bs;
1672 setup_log(&td->lat_log);
1674 setup_log(&td->bw_log);
1676 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);
1680 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1686 for (i = 0; i < sizeof(int) * 8; i++) {
1688 CPU_SET(i, &cpumask);
1692 unsigned long get_mult(char c)
1703 return 1024 * 1024 * 1024;
1710 * convert string after '=' into decimal value, noting any size suffix
1712 static int str_cnv(char *p, unsigned long long *val)
1717 str = strstr(p, "=");
1724 *val = strtoul(str, NULL, 10);
1725 if (*val == ULONG_MAX && errno == ERANGE)
1728 *val *= get_mult(str[len - 2]);
1732 static int check_strcnv(char *p, char *name, unsigned long long *val)
1734 if (!strstr(p, name))
1737 return str_cnv(p, val);
1740 static int check_str(char *p, char *name, char *option)
1742 char *s = strstr(p, name);
1748 if (strstr(s, option))
1754 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1759 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1760 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1766 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1767 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1773 sprintf(str, "%s=%%lu-%%lu", name);
1774 if (sscanf(p, str, s, e) == 2)
1777 sprintf(str, "%s = %%lu-%%lu", name);
1778 if (sscanf(p, str, s, e) == 2)
1785 static int check_int(char *p, char *name, unsigned int *val)
1789 sprintf(str, "%s=%%d", name);
1790 if (sscanf(p, str, val) == 1)
1793 sprintf(str, "%s = %%d", name);
1794 if (sscanf(p, str, val) == 1)
1800 static int is_empty_or_comment(char *line)
1804 for (i = 0; i < strlen(line); i++) {
1807 if (!isspace(line[i]) && !iscntrl(line[i]))
1814 static int parse_jobs_ini(char *file)
1816 unsigned int prioclass, prio, cpu, global;
1817 unsigned long long ull;
1818 unsigned long ul1, ul2;
1819 struct thread_data *td;
1820 char *string, *name;
1825 f = fopen(file, "r");
1831 string = malloc(4096);
1834 while ((p = fgets(string, 4096, f)) != NULL) {
1835 if (is_empty_or_comment(p))
1837 if (sscanf(p, "[%s]", name) != 1)
1840 global = !strncmp(name, "global", 6);
1842 name[strlen(name) - 1] = '\0';
1844 td = get_new_job(global);
1852 while ((p = fgets(string, 4096, f)) != NULL) {
1853 if (is_empty_or_comment(p))
1857 if (!check_int(p, "rw", &td->ddir)) {
1861 if (!check_int(p, "prio", &prio)) {
1865 if (!check_int(p, "prioclass", &prioclass)) {
1869 if (!check_int(p, "direct", &td->odirect)) {
1873 if (!check_int(p, "rate", &td->rate)) {
1877 if (!check_int(p, "ratemin", &td->ratemin)) {
1881 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1885 if (!check_int(p, "thinktime", &td->thinktime)) {
1889 if (!check_int(p, "cpumask", &cpu)) {
1890 fill_cpu_mask(td->cpumask, cpu);
1894 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1898 if (!check_int(p, "startdelay", &td->start_delay)) {
1902 if (!check_int(p, "timeout", &td->timeout)) {
1906 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1910 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1914 if (!check_int(p, "sync", &td->sync_io)) {
1918 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
1922 if (!check_int(p, "create_serialize", &td->create_serialize)) {
1926 if (!check_int(p, "create_fsync", &td->create_fsync)) {
1930 if (!check_int(p, "loops", &td->loops)) {
1934 if (!check_int(p, "verify", &td->verify)) {
1938 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1944 if (!check_strcnv(p, "bs", &ull)) {
1949 if (!check_strcnv(p, "size", &td->file_size)) {
1953 if (!check_strcnv(p, "offset", &td->file_offset)) {
1957 if (!check_str(p, "mem", "malloc")) {
1958 td->mem_type = MEM_MALLOC;
1962 if (!check_str(p, "mem", "shm")) {
1963 td->mem_type = MEM_SHM;
1967 if (!strncmp(p, "sequential", 10)) {
1972 if (!strncmp(p, "random", 6)) {
1977 if (!strncmp(p, "aio", 3)) {
1982 if (!strncmp(p, "create", 6)) {
1983 td->create_file = 1;
1987 if (!strncmp(p, "overwrite", 9)) {
1992 if (!strncmp(p, "exitall", 7)) {
1993 exitall_on_terminate = 1;
1997 printf("Client%d: bad option %s\n",td->thread_number,p);
2001 if (add_job(td, name, prioclass, prio))
2011 static int parse_options(int argc, char *argv[])
2015 for (i = 1; i < argc; i++) {
2016 char *parm = argv[i];
2025 def_thread.sequential = !!atoi(parm);
2029 def_thread.bs = atoi(parm);
2030 def_thread.bs <<= 10;
2031 if (!def_thread.bs) {
2032 printf("bad block size\n");
2033 def_thread.bs = DEF_BS;
2038 def_thread.timeout = atoi(parm);
2042 repeatable = !!atoi(parm);
2046 rate_quit = !!atoi(parm);
2050 def_thread.odirect = !!atoi(parm);
2053 if (i + 1 >= argc) {
2054 printf("-f needs file as arg\n");
2057 ini_file = strdup(argv[i+1]);
2067 printf("bad option %s\n", argv[i]);
2075 static void print_thread_status(struct thread_data *td, int nr_running,
2076 int t_rate, int m_rate)
2078 printf("Threads now running: %d", nr_running);
2079 if (m_rate || t_rate)
2080 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
2081 printf(" : [%s]\r", run_str);
2085 static void check_str_update(struct thread_data *td, int n, int t, int m)
2087 char c = run_str[td->thread_number - 1];
2089 if (td->runstate == td->old_runstate)
2092 switch (td->runstate) {
2108 case TD_NOT_CREATED:
2112 printf("state %d\n", td->runstate);
2115 run_str[td->thread_number - 1] = c;
2116 print_thread_status(td, n, t, m);
2117 td->old_runstate = td->runstate;
2120 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2125 * reap exited threads (TD_EXITED -> TD_REAPED)
2127 for (i = 0; i < thread_number; i++) {
2128 struct thread_data *td = &threads[i];
2130 check_str_update(td, *nr_running, *t_rate, *m_rate);
2132 if (td->runstate != TD_EXITED)
2135 td_set_runstate(td, TD_REAPED);
2136 waitpid(td->pid, NULL, 0);
2138 (*m_rate) -= td->ratemin;
2139 (*t_rate) -= td->rate;
2140 check_str_update(td, *nr_running, *t_rate, *m_rate);
2147 static void run_threads(char *argv[])
2149 struct timeval genesis;
2150 struct thread_data *td;
2151 unsigned long spent;
2152 int i, todo, nr_running, m_rate, t_rate;
2154 printf("Starting %d threads\n", thread_number);
2157 signal(SIGINT, sig_handler);
2159 todo = thread_number;
2161 m_rate = t_rate = 0;
2163 for (i = 0; i < thread_number; i++) {
2166 if (!td->create_serialize)
2170 * do file setup here so it happens sequentially,
2171 * we don't want X number of threads getting their
2172 * client data interspersed on disk
2174 if (setup_file(td)) {
2175 td_set_runstate(td, TD_REAPED);
2180 gettimeofday(&genesis, NULL);
2184 * create threads (TD_NOT_CREATED -> TD_CREATED)
2186 for (i = 0; i < thread_number; i++) {
2189 if (td->runstate != TD_NOT_CREATED)
2193 * never got a chance to start, killed by other
2194 * thread for some reason
2196 if (td->terminate) {
2201 if (td->start_delay) {
2202 spent = mtime_since_now(&genesis);
2204 if (td->start_delay * 1000 > spent)
2208 td_set_runstate(td, TD_CREATED);
2209 check_str_update(td, nr_running, t_rate, m_rate);
2210 sem_init(&startup_sem, 1, 1);
2214 sem_wait(&startup_sem);
2216 thread_main(shm_id, i, argv);
2222 * start created threads (TD_CREATED -> TD_RUNNING)
2224 for (i = 0; i < thread_number; i++) {
2225 struct thread_data *td = &threads[i];
2227 if (td->runstate != TD_CREATED)
2230 td_set_runstate(td, TD_RUNNING);
2232 m_rate += td->ratemin;
2234 check_str_update(td, nr_running, t_rate, m_rate);
2235 sem_post(&td->mutex);
2238 for (i = 0; i < thread_number; i++) {
2239 struct thread_data *td = &threads[i];
2241 if (td->runstate == TD_RUNNING)
2242 run_str[td->thread_number - 1] = '+';
2243 else if (td->runstate == TD_VERIFYING)
2244 run_str[td->thread_number - 1] = 'V';
2248 check_str_update(td, nr_running, t_rate, m_rate);
2251 reap_threads(&nr_running, &t_rate, &m_rate);
2257 while (nr_running) {
2258 reap_threads(&nr_running, &t_rate, &m_rate);
2263 int setup_thread_area(void)
2266 * 1024 is too much on some machines, scale max_jobs if
2267 * we get a failure that looks like too large a shm segment
2270 int s = max_jobs * sizeof(struct thread_data);
2272 shm_id = shmget(0, s, IPC_CREAT | 0600);
2275 if (errno != EINVAL) {
2286 threads = shmat(shm_id, NULL, 0);
2287 if (threads == (void *) -1) {
2296 int main(int argc, char *argv[])
2298 static unsigned long max_run[2], min_run[2];
2299 static unsigned long max_bw[2], min_bw[2];
2300 static unsigned long io_mb[2], agg[2];
2303 if (setup_thread_area())
2306 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
2307 perror("sched_getaffinity");
2314 def_thread.ddir = DDIR_READ;
2315 def_thread.bs = DEF_BS;
2316 def_thread.min_bs = -1;
2317 def_thread.max_bs = -1;
2318 def_thread.odirect = DEF_ODIRECT;
2319 def_thread.ratecycle = DEF_RATE_CYCLE;
2320 def_thread.sequential = DEF_SEQUENTIAL;
2321 def_thread.timeout = DEF_TIMEOUT;
2322 def_thread.create_file = DEF_CREATE;
2323 def_thread.overwrite = DEF_OVERWRITE;
2324 def_thread.invalidate_cache = DEF_INVALIDATE;
2325 def_thread.sync_io = DEF_SYNCIO;
2326 def_thread.mem_type = MEM_MALLOC;
2327 def_thread.bw_avg_time = DEF_BWAVGTIME;
2328 def_thread.create_serialize = DEF_CREATE_SER;
2329 def_thread.create_fsync = DEF_CREATE_FSYNC;
2330 def_thread.loops = DEF_LOOPS;
2331 def_thread.verify = DEF_VERIFY;
2333 i = parse_options(argc, argv);
2336 printf("Need job file\n");
2340 if (parse_jobs_ini(ini_file))
2343 if (!thread_number) {
2344 printf("Nothing to do\n");
2350 min_bw[0] = min_run[0] = ~0UL;
2351 min_bw[1] = min_run[1] = ~0UL;
2352 io_mb[0] = io_mb[1] = 0;
2353 agg[0] = agg[1] = 0;
2354 for (i = 0; i < thread_number; i++) {
2355 struct thread_data *td = &threads[i];
2356 unsigned long bw = 0;
2359 if (td->runtime < min_run[td->ddir])
2360 min_run[td->ddir] = td->runtime;
2361 if (td->runtime > max_run[td->ddir])
2362 max_run[td->ddir] = td->runtime;
2365 bw = td->io_kb * 1024 / td->runtime;
2366 if (bw < min_bw[td->ddir])
2367 min_bw[td->ddir] = bw;
2368 if (bw > max_bw[td->ddir])
2369 max_bw[td->ddir] = bw;
2371 io_mb[td->ddir] += td->io_kb >> 10;
2374 show_thread_status(td);
2378 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2380 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2382 printf("\nRun status:\n");
2383 if (max_run[DDIR_READ])
2384 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]);
2385 if (max_run[DDIR_WRITE])
2386 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]);