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
34 #include <sys/types.h>
39 #include <sys/ioctl.h>
41 #include <asm/unistd.h>
48 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
51 int thread_number = 0;
52 char run_str[MAX_JOBS + 1];
54 static LIST_HEAD(disk_list);
56 static void update_io_ticks(void);
57 static void disk_util_timer_arm(void);
71 #define should_fsync(td) (td_write(td) && !(td)->odirect)
73 static sem_t startup_sem;
75 #define TERMINATE_ALL (-1)
77 static void terminate_threads(int group_id)
81 for (i = 0; i < thread_number; i++) {
82 struct thread_data *td = &threads[i];
84 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
91 static void sig_handler(int sig)
96 disk_util_timer_arm();
99 printf("\nfio: terminating on signal\n");
101 terminate_threads(TERMINATE_ALL);
106 static unsigned long utime_since(struct timeval *s, struct timeval *e)
110 sec = e->tv_sec - s->tv_sec;
111 usec = e->tv_usec - s->tv_usec;
112 if (sec > 0 && usec < 0) {
117 sec *= (double) 1000000;
122 static unsigned long utime_since_now(struct timeval *s)
126 gettimeofday(&t, NULL);
127 return utime_since(s, &t);
130 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
134 sec = e->tv_sec - s->tv_sec;
135 usec = e->tv_usec - s->tv_usec;
136 if (sec > 0 && usec < 0) {
141 sec *= (double) 1000;
142 usec /= (double) 1000;
147 static unsigned long mtime_since_now(struct timeval *s)
151 gettimeofday(&t, NULL);
152 return mtime_since(s, &t);
155 static inline unsigned long msec_now(struct timeval *s)
157 return s->tv_sec * 1000 + s->tv_usec / 1000;
160 static int random_map_free(struct thread_data *td, unsigned long long block)
162 unsigned int idx = RAND_MAP_IDX(td, block);
163 unsigned int bit = RAND_MAP_BIT(td, block);
165 return (td->file_map[idx] & (1UL << bit)) == 0;
168 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
174 while ((*b) * td->min_bs < td->io_size) {
175 if (td->file_map[i] != -1UL) {
176 *b += ffz(td->file_map[i]);
180 *b += BLOCKS_PER_MAP;
187 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
189 unsigned long block = io_u->offset / td->min_bs;
190 unsigned int blocks = 0;
192 while (blocks < (io_u->buflen / td->min_bs)) {
193 unsigned int idx, bit;
195 if (!random_map_free(td, block))
198 idx = RAND_MAP_IDX(td, block);
199 bit = RAND_MAP_BIT(td, block);
201 assert(idx < td->num_maps);
203 td->file_map[idx] |= (1UL << bit);
208 if ((blocks * td->min_bs) < io_u->buflen)
209 io_u->buflen = blocks * td->min_bs;
212 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
214 unsigned long long b, rb;
217 if (!td->sequential) {
218 unsigned long max_blocks = td->io_size / td->min_bs;
222 lrand48_r(&td->random_state, &r);
223 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
224 rb = b + (td->file_offset / td->min_bs);
226 } while (!random_map_free(td, rb) && loops);
229 if (get_next_free_block(td, &b))
233 b = td->last_bytes / td->min_bs;
235 *offset = (b * td->min_bs) + td->file_offset;
236 if (*offset > td->file_size)
242 static unsigned int get_next_buflen(struct thread_data *td)
247 if (td->min_bs == td->max_bs)
250 lrand48_r(&td->bsrange_state, &r);
251 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
252 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
255 if (buflen > td->io_size - td->this_io_bytes)
256 buflen = td->io_size - td->this_io_bytes;
261 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
263 if (val > is->max_val)
265 if (val < is->min_val)
269 is->val_sq += val * val;
273 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
276 if (iolog->nr_samples == iolog->max_samples) {
277 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
279 iolog->log = realloc(iolog->log, new_size);
280 iolog->max_samples <<= 1;
283 iolog->log[iolog->nr_samples].val = val;
284 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->start);
288 static void add_clat_sample(struct thread_data *td, unsigned long msec)
290 add_stat_sample(&td->clat_stat, msec);
293 add_log_sample(td, td->lat_log, msec);
296 static void add_slat_sample(struct thread_data *td, unsigned long msec)
298 add_stat_sample(&td->slat_stat, msec);
301 static void add_bw_sample(struct thread_data *td)
303 unsigned long spent = mtime_since_now(&td->stat_sample_time);
306 if (spent < td->bw_avg_time)
309 rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
310 add_stat_sample(&td->bw_stat, rate);
313 add_log_sample(td, td->bw_log, rate);
315 gettimeofday(&td->stat_sample_time, NULL);
316 td->stat_io_bytes = td->this_io_bytes;
320 * busy looping version for the last few usec
322 static void __usec_sleep(unsigned int usec)
324 struct timeval start;
326 gettimeofday(&start, NULL);
327 while (utime_since_now(&start) < usec)
331 static void usec_sleep(struct thread_data *td, unsigned long usec)
333 struct timespec req, rem;
335 req.tv_sec = usec / 1000000;
336 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
344 rem.tv_sec = rem.tv_nsec = 0;
345 if (nanosleep(&req, &rem) < 0)
348 if ((rem.tv_sec + rem.tv_nsec) == 0)
351 req.tv_nsec = rem.tv_nsec;
352 req.tv_sec = rem.tv_sec;
354 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
355 } while (!td->terminate);
358 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
361 unsigned long usec_cycle;
366 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
368 if (time_spent < usec_cycle) {
369 unsigned long s = usec_cycle - time_spent;
371 td->rate_pending_usleep += s;
372 if (td->rate_pending_usleep >= 100000) {
373 usec_sleep(td, td->rate_pending_usleep);
374 td->rate_pending_usleep = 0;
377 long overtime = time_spent - usec_cycle;
379 td->rate_pending_usleep -= overtime;
383 static int check_min_rate(struct thread_data *td, struct timeval *now)
389 * allow a 2 second settle period in the beginning
391 if (mtime_since(&td->start, now) < 2000)
395 * if rate blocks is set, sample is running
397 if (td->rate_bytes) {
398 spent = mtime_since(&td->lastrate, now);
399 if (spent < td->ratecycle)
402 rate = (td->this_io_bytes - td->rate_bytes) / spent;
403 if (rate < td->ratemin) {
404 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
406 terminate_threads(td->groupid);
411 td->rate_bytes = td->this_io_bytes;
412 memcpy(&td->lastrate, now, sizeof(*now));
416 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
420 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
426 static void fill_random_bytes(struct thread_data *td,
427 unsigned char *p, unsigned int len)
433 drand48_r(&td->verify_state, &r);
436 * lrand48_r seems to be broken and only fill the bottom
437 * 32-bits, even on 64-bit archs with 64-bit longs
450 static void hexdump(void *buffer, int len)
452 unsigned char *p = buffer;
455 for (i = 0; i < len; i++)
456 printf("%02x", p[i]);
460 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
462 unsigned char *p = (unsigned char *) io_u->buf;
466 c = crc32(p, hdr->len - sizeof(*hdr));
468 return c != hdr->crc32;
471 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
473 unsigned char *p = (unsigned char *) io_u->buf;
474 struct md5_ctx md5_ctx;
477 memset(&md5_ctx, 0, sizeof(md5_ctx));
479 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
481 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
483 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
484 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
490 static int verify_io_u(struct io_u *io_u)
492 struct verify_header *hdr = (struct verify_header *) io_u->buf;
495 if (hdr->fio_magic != FIO_HDR_MAGIC)
498 if (hdr->verify_type == VERIFY_MD5)
499 ret = verify_io_u_md5(hdr, io_u);
500 else if (hdr->verify_type == VERIFY_CRC32)
501 ret = verify_io_u_crc32(hdr, io_u);
503 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
510 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
512 hdr->crc32 = crc32(p, len);
515 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
517 struct md5_ctx md5_ctx;
519 memset(&md5_ctx, 0, sizeof(md5_ctx));
520 md5_update(&md5_ctx, p, len);
521 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
525 * fill body of io_u->buf with random data and add a header with the
526 * (eg) sha1sum of that data.
528 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
530 unsigned char *p = (unsigned char *) io_u->buf;
531 struct verify_header hdr;
533 hdr.fio_magic = FIO_HDR_MAGIC;
534 hdr.len = io_u->buflen;
536 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
538 if (td->verify == VERIFY_MD5) {
539 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
540 hdr.verify_type = VERIFY_MD5;
542 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
543 hdr.verify_type = VERIFY_CRC32;
546 memcpy(io_u->buf, &hdr, sizeof(hdr));
549 static void put_io_u(struct thread_data *td, struct io_u *io_u)
551 list_del(&io_u->list);
552 list_add(&io_u->list, &td->io_u_freelist);
556 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
558 static struct io_u *__get_io_u(struct thread_data *td)
565 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
566 list_del(&io_u->list);
567 list_add(&io_u->list, &td->io_u_busylist);
572 static struct io_u *get_io_u(struct thread_data *td)
576 io_u = __get_io_u(td);
580 if (get_next_offset(td, &io_u->offset)) {
585 io_u->buflen = get_next_buflen(td);
591 if (io_u->buflen + io_u->offset > td->file_size)
592 io_u->buflen = td->file_size - io_u->offset;
600 mark_random_map(td, io_u);
602 td->last_bytes += io_u->buflen;
604 if (td->verify != VERIFY_NONE)
605 populate_io_u(td, io_u);
608 td->io_prep(td, io_u, td_read(td));
610 gettimeofday(&io_u->start_time, NULL);
614 static inline void td_set_runstate(struct thread_data *td, int runstate)
616 td->old_runstate = td->runstate;
617 td->runstate = runstate;
620 static int get_next_verify(struct thread_data *td,
621 unsigned long long *offset, unsigned int *len)
623 struct io_piece *ipo;
625 if (list_empty(&td->io_hist_list))
628 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
629 list_del(&ipo->list);
631 *offset = ipo->offset;
637 static void prune_io_piece_log(struct thread_data *td)
639 struct io_piece *ipo;
641 while (!list_empty(&td->io_hist_list)) {
642 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
644 list_del(&ipo->list);
650 * log a succesful write, so we can unwind the log for verify
652 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
654 struct io_piece *ipo = malloc(sizeof(struct io_piece));
655 struct list_head *entry;
657 INIT_LIST_HEAD(&ipo->list);
658 ipo->offset = io_u->offset;
659 ipo->len = io_u->buflen;
662 * for random io where the writes extend the file, it will typically
663 * be laid out with the block scattered as written. it's faster to
664 * read them in in that order again, so don't sort
666 if (td->sequential || !td->overwrite) {
667 list_add_tail(&ipo->list, &td->io_hist_list);
672 * for random io, sort the list so verify will run faster
674 entry = &td->io_hist_list;
675 while ((entry = entry->prev) != &td->io_hist_list) {
676 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
678 if (__ipo->offset < ipo->offset)
682 list_add(&ipo->list, entry);
685 static void do_sync_verify(struct thread_data *td)
688 struct io_u *io_u = NULL;
691 td_set_runstate(td, TD_VERIFYING);
693 io_u = __get_io_u(td);
697 if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
698 td_verror(td, errno);
702 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
703 td_verror(td, errno);
713 gettimeofday(&t, NULL);
714 if (runtime_exceeded(td, &t))
717 if (get_next_verify(td, &io_u->offset, &io_u->buflen))
720 if (td->cur_off != io_u->offset) {
721 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
722 td_verror(td, errno);
727 ret = read(td->fd, io_u->buf, io_u->buflen);
728 if (ret < (int) io_u->buflen) {
730 td_verror(td, errno);
738 if (verify_io_u(io_u))
741 td->cur_off = io_u->offset + io_u->buflen;
745 td_set_runstate(td, TD_RUNNING);
749 static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u)
751 unsigned long long real_off = io_u->offset - td->file_offset;
754 memcpy(io_u->buf, td->mmap + real_off, io_u->buflen);
756 memcpy(td->mmap + real_off, io_u->buf, io_u->buflen);
759 * not really direct, but should drop the pages from the cache
762 msync(td->mmap + real_off, io_u->buflen, MS_SYNC);
763 madvise(td->mmap + real_off, io_u->buflen, MADV_DONTNEED);
769 static int __do_sync_rw(struct thread_data *td, struct io_u *io_u)
771 if (td->cur_off != io_u->offset) {
772 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
773 td_verror(td, errno);
779 return read(td->fd, io_u->buf, io_u->buflen);
781 return write(td->fd, io_u->buf, io_u->buflen);
784 static void sync_td(struct thread_data *td)
789 msync(td->mmap, td->file_size, MS_SYNC);
792 static void do_sync_io(struct thread_data *td)
794 unsigned long msec, usec;
795 struct io_u *io_u = NULL;
798 while (td->this_io_bytes < td->io_size) {
809 ret = __do_sync_rw(td, io_u);
811 ret = __do_sync_mmap(td, io_u);
813 if (ret < (int) io_u->buflen) {
815 td_verror(td, errno);
820 log_io_piece(td, io_u);
823 td->io_bytes += io_u->buflen;
824 td->this_io_bytes += io_u->buflen;
825 td->cur_off = io_u->offset + io_u->buflen;
827 gettimeofday(&e, NULL);
829 usec = utime_since(&io_u->start_time, &e);
831 rate_throttle(td, usec, io_u->buflen);
833 if (check_min_rate(td, &e)) {
834 td_verror(td, ENODATA);
839 add_clat_sample(td, msec);
842 if (runtime_exceeded(td, &e))
849 usec_sleep(td, td->thinktime);
851 if (should_fsync(td) && td->fsync_blocks &&
852 (td->io_blocks % td->fsync_blocks) == 0)
859 if (should_fsync(td))
863 static int io_u_getevents(struct thread_data *td, int min, int max,
866 return td->io_getevents(td, min, max, t);
869 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
871 return td->io_queue(td, io_u);
874 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
876 static int ios_completed(struct thread_data *td, int nr)
883 gettimeofday(&e, NULL);
885 for (i = 0, bytes_done = 0; i < nr; i++) {
886 io_u = td->io_event(td, i);
889 td->io_bytes += io_u->buflen;
890 td->this_io_bytes += io_u->buflen;
892 msec = mtime_since(&io_u->issue_time, &e);
894 add_clat_sample(td, msec);
898 log_io_piece(td, io_u);
900 bytes_done += io_u->buflen;
907 static void cleanup_pending_aio(struct thread_data *td)
909 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
910 struct list_head *entry, *n;
915 * get immediately available events, if any
917 r = io_u_getevents(td, 0, td->cur_depth, &ts);
919 ios_completed(td, r);
922 * now cancel remaining active events
924 list_for_each_safe(entry, n, &td->io_u_busylist) {
925 io_u = list_entry(entry, struct io_u, list);
927 r = td->io_cancel(td, io_u);
933 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
935 ios_completed(td, r);
939 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
941 struct io_u *v_io_u = *io_u;
945 ret = verify_io_u(v_io_u);
946 put_io_u(td, v_io_u);
953 static void do_async_verify(struct thread_data *td)
956 struct io_u *io_u, *v_io_u = NULL;
959 td_set_runstate(td, TD_VERIFYING);
965 gettimeofday(&t, NULL);
966 if (runtime_exceeded(td, &t))
969 io_u = __get_io_u(td);
973 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
978 td->io_prep(td, io_u, 1);
980 ret = io_u_queue(td, io_u);
988 * we have one pending to verify, do that while the next
989 * we are doing io on the next one
991 if (async_do_verify(td, &v_io_u))
994 ret = io_u_getevents(td, 1, 1, NULL);
1001 v_io_u = td->io_event(td, 0);
1003 td->cur_off = v_io_u->offset + v_io_u->buflen;
1006 * if we can't submit more io, we need to verify now
1008 if (queue_full(td) && async_do_verify(td, &v_io_u))
1013 async_do_verify(td, &v_io_u);
1016 cleanup_pending_aio(td);
1018 td_set_runstate(td, TD_RUNNING);
1021 static void do_async_io(struct thread_data *td)
1023 struct timeval s, e;
1026 while (td->this_io_bytes < td->io_size) {
1027 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1028 struct timespec *timeout;
1029 int ret, min_evts = 0;
1031 unsigned int bytes_done;
1036 io_u = get_io_u(td);
1040 memcpy(&s, &io_u->start_time, sizeof(s));
1042 ret = io_u_queue(td, io_u);
1049 gettimeofday(&io_u->issue_time, NULL);
1050 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1051 if (td->cur_depth < td->aio_depth) {
1059 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1066 bytes_done = ios_completed(td, ret);
1069 * the rate is batched for now, it should work for batches
1070 * of completions except the very first one which may look
1073 gettimeofday(&e, NULL);
1074 usec = utime_since(&s, &e);
1076 rate_throttle(td, usec, bytes_done);
1078 if (check_min_rate(td, &e)) {
1079 td_verror(td, ENODATA);
1083 if (runtime_exceeded(td, &e))
1087 usec_sleep(td, td->thinktime);
1089 if (should_fsync(td) && td->fsync_blocks &&
1090 (td->io_blocks % td->fsync_blocks) == 0)
1095 cleanup_pending_aio(td);
1097 if (should_fsync(td))
1101 static void cleanup_aio(struct thread_data *td)
1103 if (td->io_engine == FIO_LIBAIO)
1104 fio_libaio_cleanup(td);
1105 else if (td->io_engine == FIO_POSIXAIO)
1106 fio_posixaio_cleanup(td);
1109 static int init_aio(struct thread_data *td)
1111 if (td->io_engine == FIO_LIBAIO)
1112 return fio_libaio_init(td);
1113 else if (td->io_engine == FIO_POSIXAIO)
1114 return fio_posixaio_init(td);
1116 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1121 static void cleanup_io_u(struct thread_data *td)
1123 struct list_head *entry, *n;
1126 list_for_each_safe(entry, n, &td->io_u_freelist) {
1127 io_u = list_entry(entry, struct io_u, list);
1129 list_del(&io_u->list);
1133 if (td->mem_type == MEM_MALLOC)
1134 free(td->orig_buffer);
1135 else if (td->mem_type == MEM_SHM) {
1136 struct shmid_ds sbuf;
1138 shmdt(td->orig_buffer);
1139 shmctl(td->shm_id, IPC_RMID, &sbuf);
1140 } else if (td->mem_type == MEM_MMAP)
1141 munmap(td->orig_buffer, td->orig_buffer_size);
1143 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1145 td->orig_buffer = NULL;
1148 static int init_io_u(struct thread_data *td)
1154 if (td->io_engine == FIO_SYNCIO)
1157 max_units = td->aio_depth;
1159 td->orig_buffer_size = td->max_bs * max_units + MASK;
1161 if (td->mem_type == MEM_MALLOC)
1162 td->orig_buffer = malloc(td->orig_buffer_size);
1163 else if (td->mem_type == MEM_SHM) {
1164 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1165 if (td->shm_id < 0) {
1166 td_verror(td, errno);
1171 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1172 if (td->orig_buffer == (void *) -1) {
1173 td_verror(td, errno);
1175 td->orig_buffer = NULL;
1178 } else if (td->mem_type == MEM_MMAP) {
1179 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
1180 if (td->orig_buffer == MAP_FAILED) {
1181 td_verror(td, errno);
1183 td->orig_buffer = NULL;
1188 INIT_LIST_HEAD(&td->io_u_freelist);
1189 INIT_LIST_HEAD(&td->io_u_busylist);
1190 INIT_LIST_HEAD(&td->io_hist_list);
1192 p = ALIGN(td->orig_buffer);
1193 for (i = 0; i < max_units; i++) {
1194 io_u = malloc(sizeof(*io_u));
1195 memset(io_u, 0, sizeof(*io_u));
1196 INIT_LIST_HEAD(&io_u->list);
1198 io_u->buf = p + td->max_bs * i;
1199 list_add(&io_u->list, &td->io_u_freelist);
1205 static int create_file(struct thread_data *td, unsigned long long size,
1208 unsigned long long left;
1214 * unless specifically asked for overwrite, let normal io extend it
1216 if (td_write(td) && !td->overwrite)
1220 fprintf(stderr, "Need size for create\n");
1221 td_verror(td, EINVAL);
1226 oflags = O_CREAT | O_TRUNC;
1227 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1230 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1233 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1235 td_verror(td, errno);
1239 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1240 td_verror(td, errno);
1244 td->io_size = td->file_size;
1245 b = malloc(td->max_bs);
1246 memset(b, 0, td->max_bs);
1249 while (left && !td->terminate) {
1254 r = write(td->fd, b, bs);
1256 if (r == (int) bs) {
1261 td_verror(td, errno);
1270 unlink(td->file_name);
1271 else if (td->create_fsync)
1280 static int file_size(struct thread_data *td)
1284 if (fstat(td->fd, &st) == -1) {
1285 td_verror(td, errno);
1290 td->file_size = st.st_size;
1295 static int bdev_size(struct thread_data *td)
1299 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1300 td_verror(td, errno);
1305 * no extend possibilities, so limit size to device size if too large
1307 if (!td->file_size || td->file_size > bytes)
1308 td->file_size = bytes;
1313 static int get_file_size(struct thread_data *td)
1317 if (td->filetype == FIO_TYPE_FILE)
1318 ret = file_size(td);
1320 ret = bdev_size(td);
1325 if (td->file_offset > td->file_size) {
1326 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1330 td->io_size = td->file_size - td->file_offset;
1331 if (td->io_size == 0) {
1332 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1333 td_verror(td, EINVAL);
1340 static int setup_file_mmap(struct thread_data *td)
1349 if (td->verify != VERIFY_NONE)
1353 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1354 if (td->mmap == MAP_FAILED) {
1356 td_verror(td, errno);
1360 if (td->invalidate_cache) {
1361 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1362 td_verror(td, errno);
1367 if (td->sequential) {
1368 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1369 td_verror(td, errno);
1373 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1374 td_verror(td, errno);
1382 static int setup_file_plain(struct thread_data *td)
1384 if (td->invalidate_cache) {
1385 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1386 td_verror(td, errno);
1391 if (td->sequential) {
1392 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1393 td_verror(td, errno);
1397 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1398 td_verror(td, errno);
1406 static int setup_file(struct thread_data *td)
1411 if (stat(td->file_name, &st) == -1) {
1412 if (errno != ENOENT) {
1413 td_verror(td, errno);
1416 if (!td->create_file) {
1417 td_verror(td, ENOENT);
1420 if (create_file(td, td->file_size, 0))
1422 } else if (td->filetype == FIO_TYPE_FILE) {
1423 if (st.st_size < td->file_size) {
1424 if (create_file(td, td->file_size - st.st_size, 1))
1433 td->fd = open(td->file_name, flags | O_RDONLY);
1435 if (td->filetype == FIO_TYPE_FILE) {
1446 td->fd = open(td->file_name, flags, 0600);
1450 td_verror(td, errno);
1454 if (get_file_size(td))
1458 return setup_file_plain(td);
1460 return setup_file_mmap(td);
1463 static int check_dev_match(dev_t dev, char *path)
1465 unsigned int major, minor;
1469 f = fopen(path, "r");
1471 perror("open path");
1475 p = fgets(line, sizeof(line), f);
1481 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1486 if (((major << 8) | minor) == dev) {
1495 static char *find_block_dir(dev_t dev, char *path)
1506 while ((dir = readdir(D)) != NULL) {
1507 char full_path[256];
1509 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1511 if (!strcmp(dir->d_name, "device"))
1514 sprintf(full_path, "%s/%s", path, dir->d_name);
1516 if (!strcmp(dir->d_name, "dev")) {
1517 if (!check_dev_match(dev, full_path)) {
1523 if (stat(full_path, &st) == -1) {
1528 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1531 if ((found = find_block_dir(dev, full_path)) != NULL)
1539 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1546 f = fopen(du->path, "r");
1550 p = fgets(line, sizeof(line), f);
1556 if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
1565 static void update_io_tick_disk(struct disk_util *du)
1567 struct disk_util_stat __dus, *dus, *ldus;
1570 if (get_io_ticks(du, &__dus))
1574 ldus = &du->last_dus;
1576 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1577 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1578 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1579 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1580 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1581 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1582 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1583 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1584 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1585 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1587 gettimeofday(&t, NULL);
1588 du->msec += mtime_since(&du->time, &t);
1589 memcpy(&du->time, &t, sizeof(t));
1590 memcpy(ldus, &__dus, sizeof(__dus));
1593 static void update_io_ticks(void)
1595 struct list_head *entry;
1596 struct disk_util *du;
1598 list_for_each(entry, &disk_list) {
1599 du = list_entry(entry, struct disk_util, list);
1600 update_io_tick_disk(du);
1604 static int disk_util_exists(dev_t dev)
1606 struct list_head *entry;
1607 struct disk_util *du;
1609 list_for_each(entry, &disk_list) {
1610 du = list_entry(entry, struct disk_util, list);
1619 static void disk_util_add(dev_t dev, char *path)
1621 struct disk_util *du = malloc(sizeof(*du));
1623 memset(du, 0, sizeof(*du));
1624 INIT_LIST_HEAD(&du->list);
1625 sprintf(du->path, "%s/stat", path);
1626 du->name = strdup(basename(path));
1629 gettimeofday(&du->time, NULL);
1630 get_io_ticks(du, &du->last_dus);
1632 list_add_tail(&du->list, &disk_list);
1635 static void init_disk_util(struct thread_data *td)
1638 char foo[256], tmp[256];
1642 if (!stat(td->file_name, &st)) {
1643 if (S_ISBLK(st.st_mode))
1649 * must be a file, open "." in that path
1651 strcpy(foo, td->file_name);
1654 perror("disk util stat");
1661 if (disk_util_exists(dev))
1664 sprintf(foo, "/sys/block");
1665 dir = find_block_dir(dev, foo);
1670 * if this is inside a partition dir, jump back to parent
1672 sprintf(tmp, "%s/queue", dir);
1673 if (stat(tmp, &st)) {
1675 sprintf(tmp, "%s/queue", p);
1676 if (stat(tmp, &st)) {
1677 fprintf(stderr, "unknown sysfs layout\n");
1680 sprintf(foo, "%s", p);
1683 disk_util_add(dev, foo);
1686 static void disk_util_timer_arm(void)
1688 struct itimerval itimer;
1690 itimer.it_value.tv_sec = 0;
1691 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1692 setitimer(ITIMER_REAL, &itimer, NULL);
1695 static void clear_io_state(struct thread_data *td)
1697 if (td->io_engine == FIO_SYNCIO)
1698 lseek(td->fd, SEEK_SET, 0);
1702 td->stat_io_bytes = 0;
1703 td->this_io_bytes = 0;
1706 memset(td->file_map, 0, td->num_maps * sizeof(long));
1709 static void update_rusage_stat(struct thread_data *td)
1714 getrusage(RUSAGE_SELF, &td->ru_end);
1716 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1717 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1718 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1721 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1724 static void *thread_main(void *data)
1726 struct thread_data *td = data;
1735 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1736 td_verror(td, errno);
1740 if ((td->io_engine != FIO_SYNCIO) && init_aio(td))
1744 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1745 td_verror(td, errno);
1750 sem_post(&startup_sem);
1751 sem_wait(&td->mutex);
1753 if (!td->create_serialize && setup_file(td))
1756 if (init_random_state(td))
1759 gettimeofday(&td->epoch, NULL);
1761 while (td->loops--) {
1762 getrusage(RUSAGE_SELF, &td->ru_start);
1763 gettimeofday(&td->start, NULL);
1764 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1767 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1770 prune_io_piece_log(td);
1772 if (td->io_engine == FIO_SYNCIO)
1777 if (td->error || td->terminate)
1780 td->runtime += mtime_since_now(&td->start);
1781 update_rusage_stat(td);
1783 if (td->verify == VERIFY_NONE)
1788 if (td->io_engine == FIO_SYNCIO)
1791 do_async_verify(td);
1793 if (td->error || td->terminate)
1800 finish_log(td, td->bw_log, "bw");
1802 finish_log(td, td->lat_log, "lat");
1804 if (exitall_on_terminate)
1805 terminate_threads(td->groupid);
1813 munmap(td->mmap, td->file_size);
1814 if (td->io_engine != FIO_SYNCIO)
1818 sem_post(&startup_sem);
1819 sem_wait(&td->mutex);
1821 td_set_runstate(td, TD_EXITED);
1826 static void *fork_main(int shmid, int offset)
1828 struct thread_data *td;
1831 data = shmat(shmid, NULL, 0);
1832 if (data == (void *) -1) {
1837 td = data + offset * sizeof(struct thread_data);
1843 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1844 double *mean, double *dev)
1848 if (is->samples == 0)
1854 n = (double) is->samples;
1855 *mean = (double) is->val / n;
1856 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1860 static void show_thread_status(struct thread_data *td,
1861 struct group_run_stats *rs)
1863 int prio, prio_class;
1864 unsigned long min, max, bw = 0;
1865 double mean, dev, usr_cpu, sys_cpu;
1867 if (!td->io_bytes && !td->error)
1871 bw = td->io_bytes / td->runtime;
1873 prio = td->ioprio & 0xff;
1874 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1876 printf("Client%d (g=%d): err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->groupid, td->error, td->io_bytes >> 20, bw, td->runtime);
1878 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1879 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1880 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1881 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1882 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1885 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1886 printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev);
1890 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1891 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1897 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1900 static void print_thread_status(int nr_running, int t_rate, int m_rate)
1902 printf("Threads now running: %d", nr_running);
1903 if (m_rate || t_rate)
1904 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1905 printf(" : [%s]\r", run_str);
1909 static void check_str_update(struct thread_data *td, int n, int t, int m)
1911 char c = run_str[td->thread_number - 1];
1913 if (td->runstate == td->old_runstate)
1916 switch (td->runstate) {
1942 case TD_NOT_CREATED:
1946 printf("state %d\n", td->runstate);
1949 run_str[td->thread_number - 1] = c;
1950 print_thread_status(n, t, m);
1951 td->old_runstate = td->runstate;
1954 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1959 * reap exited threads (TD_EXITED -> TD_REAPED)
1961 for (i = 0; i < thread_number; i++) {
1962 struct thread_data *td = &threads[i];
1964 check_str_update(td, *nr_running, *t_rate, *m_rate);
1966 if (td->runstate != TD_EXITED)
1969 td_set_runstate(td, TD_REAPED);
1971 if (td->use_thread) {
1974 if (pthread_join(td->thread, (void *) &ret))
1975 perror("thread_join");
1977 waitpid(td->pid, NULL, 0);
1980 (*m_rate) -= td->ratemin;
1981 (*t_rate) -= td->rate;
1982 check_str_update(td, *nr_running, *t_rate, *m_rate);
1986 static void run_threads(void)
1988 struct timeval genesis;
1989 struct thread_data *td;
1990 unsigned long spent;
1991 int i, todo, nr_running, m_rate, t_rate, nr_started;
1993 printf("Starting %d threads\n", thread_number);
1996 signal(SIGINT, sig_handler);
1997 signal(SIGALRM, sig_handler);
1999 todo = thread_number;
2002 m_rate = t_rate = 0;
2004 for (i = 0; i < thread_number; i++) {
2009 if (!td->create_serialize)
2013 * do file setup here so it happens sequentially,
2014 * we don't want X number of threads getting their
2015 * client data interspersed on disk
2017 if (setup_file(td)) {
2018 td_set_runstate(td, TD_REAPED);
2023 gettimeofday(&genesis, NULL);
2027 * create threads (TD_NOT_CREATED -> TD_CREATED)
2029 for (i = 0; i < thread_number; i++) {
2032 if (td->runstate != TD_NOT_CREATED)
2036 * never got a chance to start, killed by other
2037 * thread for some reason
2039 if (td->terminate) {
2044 if (td->start_delay) {
2045 spent = mtime_since_now(&genesis);
2047 if (td->start_delay * 1000 > spent)
2051 if (td->stonewall && (nr_started || nr_running))
2054 td_set_runstate(td, TD_CREATED);
2055 check_str_update(td, nr_running, t_rate, m_rate);
2056 sem_init(&startup_sem, 1, 1);
2060 if (td->use_thread) {
2061 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2062 perror("thread_create");
2067 sem_wait(&startup_sem);
2069 fork_main(shm_id, i);
2076 * start created threads (TD_CREATED -> TD_RUNNING)
2078 for (i = 0; i < thread_number; i++) {
2081 if (td->runstate != TD_CREATED)
2084 td_set_runstate(td, TD_RUNNING);
2087 m_rate += td->ratemin;
2089 check_str_update(td, nr_running, t_rate, m_rate);
2090 sem_post(&td->mutex);
2093 for (i = 0; i < thread_number; i++) {
2096 if (td->runstate != TD_RUNNING &&
2097 td->runstate != TD_VERIFYING)
2100 check_str_update(td, nr_running, t_rate, m_rate);
2103 reap_threads(&nr_running, &t_rate, &m_rate);
2109 while (nr_running) {
2110 reap_threads(&nr_running, &t_rate, &m_rate);
2117 static void show_group_stats(struct group_run_stats *rs, int id)
2119 printf("\nRun status group %d:\n", id);
2121 if (rs->max_run[DDIR_READ])
2122 printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]);
2123 if (rs->max_run[DDIR_WRITE])
2124 printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
2127 static void show_disk_util(void)
2129 struct disk_util_stat *dus;
2130 struct list_head *entry;
2131 struct disk_util *du;
2134 printf("\nDisk stats (read/write):\n");
2136 list_for_each(entry, &disk_list) {
2137 du = list_entry(entry, struct disk_util, list);
2140 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2144 printf(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
2148 static void show_run_stats(void)
2150 struct group_run_stats *runstats, *rs;
2151 struct thread_data *td;
2154 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2156 for (i = 0; i < groupid + 1; i++) {
2159 memset(rs, 0, sizeof(*rs));
2160 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2161 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2164 for (i = 0; i < thread_number; i++) {
2165 unsigned long bw = 0;
2170 printf("Client%d: %s\n", td->thread_number, td->verror);
2174 rs = &runstats[td->groupid];
2176 if (td->runtime < rs->min_run[td->ddir])
2177 rs->min_run[td->ddir] = td->runtime;
2178 if (td->runtime > rs->max_run[td->ddir])
2179 rs->max_run[td->ddir] = td->runtime;
2182 bw = td->io_bytes / td->runtime;
2183 if (bw < rs->min_bw[td->ddir])
2184 rs->min_bw[td->ddir] = bw;
2185 if (bw > rs->max_bw[td->ddir])
2186 rs->max_bw[td->ddir] = bw;
2188 rs->io_mb[td->ddir] += td->io_bytes >> 20;
2191 for (i = 0; i < groupid + 1; i++) {
2195 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2197 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2200 for (i = 0; i < thread_number; i++) {
2202 rs = &runstats[td->groupid];
2204 show_thread_status(td, rs);
2207 for (i = 0; i < groupid + 1; i++)
2208 show_group_stats(&runstats[i], i);
2213 int main(int argc, char *argv[])
2215 memset(run_str, 0, sizeof(run_str));
2217 if (parse_options(argc, argv))
2220 if (!thread_number) {
2221 printf("Nothing to do\n");
2225 disk_util_timer_arm();