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>
47 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
50 int thread_number = 0;
51 char run_str[MAX_JOBS + 1];
53 static LIST_HEAD(disk_list);
55 static void update_io_ticks(void);
56 static void disk_util_timer_arm(void);
70 #define should_fsync(td) (td_write(td) && !(td)->odirect)
72 static sem_t startup_sem;
74 #define TERMINATE_ALL (-1)
76 static void terminate_threads(int group_id)
80 for (i = 0; i < thread_number; i++) {
81 struct thread_data *td = &threads[i];
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
90 static void sig_handler(int sig)
95 disk_util_timer_arm();
98 printf("\nfio: terminating on signal\n");
100 terminate_threads(TERMINATE_ALL);
105 static unsigned long utime_since(struct timeval *s, struct timeval *e)
109 sec = e->tv_sec - s->tv_sec;
110 usec = e->tv_usec - s->tv_usec;
111 if (sec > 0 && usec < 0) {
116 sec *= (double) 1000000;
121 static unsigned long utime_since_now(struct timeval *s)
125 gettimeofday(&t, NULL);
126 return utime_since(s, &t);
129 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
133 sec = e->tv_sec - s->tv_sec;
134 usec = e->tv_usec - s->tv_usec;
135 if (sec > 0 && usec < 0) {
140 sec *= (double) 1000;
141 usec /= (double) 1000;
146 static unsigned long mtime_since_now(struct timeval *s)
150 gettimeofday(&t, NULL);
151 return mtime_since(s, &t);
154 static inline unsigned long msec_now(struct timeval *s)
156 return s->tv_sec * 1000 + s->tv_usec / 1000;
159 static int random_map_free(struct thread_data *td, unsigned long long block)
161 unsigned int idx = RAND_MAP_IDX(td, block);
162 unsigned int bit = RAND_MAP_BIT(td, block);
164 return (td->file_map[idx] & (1UL << bit)) == 0;
167 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
173 while ((*b) * td->min_bs < td->io_size) {
174 if (td->file_map[i] != -1UL) {
175 *b += ffz(td->file_map[i]);
179 *b += BLOCKS_PER_MAP;
186 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
188 unsigned long block = io_u->offset / td->min_bs;
189 unsigned int blocks = 0;
191 while (blocks < (io_u->buflen / td->min_bs)) {
192 unsigned int idx, bit;
194 if (!random_map_free(td, block))
197 idx = RAND_MAP_IDX(td, block);
198 bit = RAND_MAP_BIT(td, block);
200 assert(idx < td->num_maps);
202 td->file_map[idx] |= (1UL << bit);
207 if ((blocks * td->min_bs) < io_u->buflen)
208 io_u->buflen = blocks * td->min_bs;
211 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
213 unsigned long long b, rb;
216 if (!td->sequential) {
217 unsigned long max_blocks = td->io_size / td->min_bs;
221 lrand48_r(&td->random_state, &r);
222 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
223 rb = b + (td->file_offset / td->min_bs);
225 } while (!random_map_free(td, rb) && loops);
228 if (get_next_free_block(td, &b))
232 b = td->last_bytes / td->min_bs;
234 *offset = (b * td->min_bs) + td->file_offset;
235 if (*offset > td->file_size)
241 static unsigned int get_next_buflen(struct thread_data *td)
246 if (td->min_bs == td->max_bs)
249 lrand48_r(&td->bsrange_state, &r);
250 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
251 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
254 if (buflen > td->io_size - td->this_io_bytes)
255 buflen = td->io_size - td->this_io_bytes;
260 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
262 if (val > is->max_val)
264 if (val < is->min_val)
268 is->val_sq += val * val;
272 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
275 if (iolog->nr_samples == iolog->max_samples) {
276 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
278 iolog->log = realloc(iolog->log, new_size);
279 iolog->max_samples <<= 1;
282 iolog->log[iolog->nr_samples].val = val;
283 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->start);
287 static void add_clat_sample(struct thread_data *td, unsigned long msec)
289 add_stat_sample(&td->clat_stat, msec);
292 add_log_sample(td, td->lat_log, msec);
295 static void add_slat_sample(struct thread_data *td, unsigned long msec)
297 add_stat_sample(&td->slat_stat, msec);
300 static void add_bw_sample(struct thread_data *td)
302 unsigned long spent = mtime_since_now(&td->stat_sample_time);
305 if (spent < td->bw_avg_time)
308 rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
309 add_stat_sample(&td->bw_stat, rate);
312 add_log_sample(td, td->bw_log, rate);
314 gettimeofday(&td->stat_sample_time, NULL);
315 td->stat_io_bytes = td->this_io_bytes;
319 * busy looping version for the last few usec
321 static void __usec_sleep(unsigned int usec)
323 struct timeval start;
325 gettimeofday(&start, NULL);
326 while (utime_since_now(&start) < usec)
330 static void usec_sleep(struct thread_data *td, unsigned long usec)
332 struct timespec req, rem;
334 req.tv_sec = usec / 1000000;
335 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
343 rem.tv_sec = rem.tv_nsec = 0;
344 if (nanosleep(&req, &rem) < 0)
347 if ((rem.tv_sec + rem.tv_nsec) == 0)
350 req.tv_nsec = rem.tv_nsec;
351 req.tv_sec = rem.tv_sec;
353 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
354 } while (!td->terminate);
357 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
360 unsigned long usec_cycle;
365 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
367 if (time_spent < usec_cycle) {
368 unsigned long s = usec_cycle - time_spent;
370 td->rate_pending_usleep += s;
371 if (td->rate_pending_usleep >= 100000) {
372 usec_sleep(td, td->rate_pending_usleep);
373 td->rate_pending_usleep = 0;
376 long overtime = time_spent - usec_cycle;
378 td->rate_pending_usleep -= overtime;
382 static int check_min_rate(struct thread_data *td, struct timeval *now)
388 * allow a 2 second settle period in the beginning
390 if (mtime_since(&td->start, now) < 2000)
394 * if rate blocks is set, sample is running
396 if (td->rate_bytes) {
397 spent = mtime_since(&td->lastrate, now);
398 if (spent < td->ratecycle)
401 rate = (td->this_io_bytes - td->rate_bytes) / spent;
402 if (rate < td->ratemin) {
403 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
405 terminate_threads(td->groupid);
410 td->rate_bytes = td->this_io_bytes;
411 memcpy(&td->lastrate, now, sizeof(*now));
415 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
419 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
425 static void fill_random_bytes(struct thread_data *td,
426 unsigned char *p, unsigned int len)
432 drand48_r(&td->verify_state, &r);
435 * lrand48_r seems to be broken and only fill the bottom
436 * 32-bits, even on 64-bit archs with 64-bit longs
449 static void hexdump(void *buffer, int len)
451 unsigned char *p = buffer;
454 for (i = 0; i < len; i++)
455 printf("%02x", p[i]);
459 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
461 unsigned char *p = (unsigned char *) io_u->buf;
465 c = crc32(p, hdr->len - sizeof(*hdr));
467 return c != hdr->crc32;
470 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
472 unsigned char *p = (unsigned char *) io_u->buf;
473 struct md5_ctx md5_ctx;
476 memset(&md5_ctx, 0, sizeof(md5_ctx));
478 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
480 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
482 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
483 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
489 static int verify_io_u(struct io_u *io_u)
491 struct verify_header *hdr = (struct verify_header *) io_u->buf;
494 if (hdr->fio_magic != FIO_HDR_MAGIC)
497 if (hdr->verify_type == VERIFY_MD5)
498 ret = verify_io_u_md5(hdr, io_u);
499 else if (hdr->verify_type == VERIFY_CRC32)
500 ret = verify_io_u_crc32(hdr, io_u);
502 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
509 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
511 hdr->crc32 = crc32(p, len);
514 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
516 struct md5_ctx md5_ctx;
518 memset(&md5_ctx, 0, sizeof(md5_ctx));
519 md5_update(&md5_ctx, p, len);
520 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
524 * fill body of io_u->buf with random data and add a header with the
525 * (eg) sha1sum of that data.
527 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
529 unsigned char *p = (unsigned char *) io_u->buf;
530 struct verify_header hdr;
532 hdr.fio_magic = FIO_HDR_MAGIC;
533 hdr.len = io_u->buflen;
535 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
537 if (td->verify == VERIFY_MD5) {
538 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
539 hdr.verify_type = VERIFY_MD5;
541 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
542 hdr.verify_type = VERIFY_CRC32;
545 memcpy(io_u->buf, &hdr, sizeof(hdr));
548 static void put_io_u(struct thread_data *td, struct io_u *io_u)
550 list_del(&io_u->list);
551 list_add(&io_u->list, &td->io_u_freelist);
555 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
557 static struct io_u *__get_io_u(struct thread_data *td)
564 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
565 list_del(&io_u->list);
566 list_add(&io_u->list, &td->io_u_busylist);
571 static struct io_u *get_io_u(struct thread_data *td)
575 io_u = __get_io_u(td);
579 if (get_next_offset(td, &io_u->offset)) {
584 io_u->buflen = get_next_buflen(td);
590 if (io_u->buflen + io_u->offset > td->file_size)
591 io_u->buflen = td->file_size - io_u->offset;
599 mark_random_map(td, io_u);
601 td->last_bytes += io_u->buflen;
603 if (td->verify != VERIFY_NONE)
604 populate_io_u(td, io_u);
607 td->io_prep(td, io_u, td_read(td));
609 gettimeofday(&io_u->start_time, NULL);
613 static inline void td_set_runstate(struct thread_data *td, int runstate)
615 td->old_runstate = td->runstate;
616 td->runstate = runstate;
619 static int get_next_verify(struct thread_data *td,
620 unsigned long long *offset, unsigned int *len)
622 struct io_piece *ipo;
624 if (list_empty(&td->io_hist_list))
627 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
628 list_del(&ipo->list);
630 *offset = ipo->offset;
636 static void prune_io_piece_log(struct thread_data *td)
638 struct io_piece *ipo;
640 while (!list_empty(&td->io_hist_list)) {
641 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
643 list_del(&ipo->list);
649 * log a succesful write, so we can unwind the log for verify
651 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
653 struct io_piece *ipo = malloc(sizeof(struct io_piece));
654 struct list_head *entry;
656 INIT_LIST_HEAD(&ipo->list);
657 ipo->offset = io_u->offset;
658 ipo->len = io_u->buflen;
661 * for random io where the writes extend the file, it will typically
662 * be laid out with the block scattered as written. it's faster to
663 * read them in in that order again, so don't sort
665 if (td->sequential || !td->overwrite) {
666 list_add_tail(&ipo->list, &td->io_hist_list);
671 * for random io, sort the list so verify will run faster
673 entry = &td->io_hist_list;
674 while ((entry = entry->prev) != &td->io_hist_list) {
675 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
677 if (__ipo->offset < ipo->offset)
681 list_add(&ipo->list, entry);
684 static void do_sync_verify(struct thread_data *td)
687 struct io_u *io_u = NULL;
690 td_set_runstate(td, TD_VERIFYING);
692 io_u = __get_io_u(td);
696 if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
697 td_verror(td, errno);
701 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
702 td_verror(td, errno);
712 gettimeofday(&t, NULL);
713 if (runtime_exceeded(td, &t))
716 if (get_next_verify(td, &io_u->offset, &io_u->buflen))
719 if (td->cur_off != io_u->offset) {
720 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
721 td_verror(td, errno);
726 ret = read(td->fd, io_u->buf, io_u->buflen);
727 if (ret < (int) io_u->buflen) {
729 td_verror(td, errno);
737 if (verify_io_u(io_u))
740 td->cur_off = io_u->offset + io_u->buflen;
744 td_set_runstate(td, TD_RUNNING);
748 static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u)
750 unsigned long long real_off = io_u->offset - td->file_offset;
753 memcpy(io_u->buf, td->mmap + real_off, io_u->buflen);
755 memcpy(td->mmap + real_off, io_u->buf, io_u->buflen);
758 * not really direct, but should drop the pages from the cache
761 msync(td->mmap + real_off, io_u->buflen, MS_SYNC);
762 madvise(td->mmap + real_off, io_u->buflen, MADV_DONTNEED);
768 static int __do_sync_rw(struct thread_data *td, struct io_u *io_u)
770 if (td->cur_off != io_u->offset) {
771 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
772 td_verror(td, errno);
778 return read(td->fd, io_u->buf, io_u->buflen);
780 return write(td->fd, io_u->buf, io_u->buflen);
783 static void sync_td(struct thread_data *td)
788 msync(td->mmap, td->file_size, MS_SYNC);
791 static void do_sync_io(struct thread_data *td)
793 unsigned long msec, usec;
794 struct io_u *io_u = NULL;
797 while (td->this_io_bytes < td->io_size) {
808 ret = __do_sync_rw(td, io_u);
810 ret = __do_sync_mmap(td, io_u);
812 if (ret < (int) io_u->buflen) {
814 td_verror(td, errno);
819 log_io_piece(td, io_u);
822 td->io_bytes += io_u->buflen;
823 td->this_io_bytes += io_u->buflen;
824 td->cur_off = io_u->offset + io_u->buflen;
826 gettimeofday(&e, NULL);
828 usec = utime_since(&io_u->start_time, &e);
830 rate_throttle(td, usec, io_u->buflen);
832 if (check_min_rate(td, &e)) {
833 td_verror(td, ENODATA);
838 add_clat_sample(td, msec);
841 if (runtime_exceeded(td, &e))
848 usec_sleep(td, td->thinktime);
850 if (should_fsync(td) && td->fsync_blocks &&
851 (td->io_blocks % td->fsync_blocks) == 0)
858 if (should_fsync(td))
862 static int io_u_getevents(struct thread_data *td, int min, int max,
865 return td->io_getevents(td, min, max, t);
868 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
870 return td->io_queue(td, io_u);
873 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
875 static int ios_completed(struct thread_data *td, int nr)
882 gettimeofday(&e, NULL);
884 for (i = 0, bytes_done = 0; i < nr; i++) {
885 io_u = td->io_event(td, i);
888 td->io_bytes += io_u->buflen;
889 td->this_io_bytes += io_u->buflen;
891 msec = mtime_since(&io_u->issue_time, &e);
893 add_clat_sample(td, msec);
897 log_io_piece(td, io_u);
899 bytes_done += io_u->buflen;
906 static void cleanup_pending_aio(struct thread_data *td)
908 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
909 struct list_head *entry, *n;
914 * get immediately available events, if any
916 r = io_u_getevents(td, 0, td->cur_depth, &ts);
918 ios_completed(td, r);
921 * now cancel remaining active events
923 list_for_each_safe(entry, n, &td->io_u_busylist) {
924 io_u = list_entry(entry, struct io_u, list);
926 r = td->io_cancel(td, io_u);
932 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
934 ios_completed(td, r);
938 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
940 struct io_u *v_io_u = *io_u;
944 ret = verify_io_u(v_io_u);
945 put_io_u(td, v_io_u);
952 static void do_async_verify(struct thread_data *td)
955 struct io_u *io_u, *v_io_u = NULL;
958 td_set_runstate(td, TD_VERIFYING);
964 gettimeofday(&t, NULL);
965 if (runtime_exceeded(td, &t))
968 io_u = __get_io_u(td);
972 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
977 td->io_prep(td, io_u, 1);
979 ret = io_u_queue(td, io_u);
987 * we have one pending to verify, do that while the next
988 * we are doing io on the next one
990 if (async_do_verify(td, &v_io_u))
993 ret = io_u_getevents(td, 1, 1, NULL);
1000 v_io_u = td->io_event(td, 0);
1002 td->cur_off = v_io_u->offset + v_io_u->buflen;
1005 * if we can't submit more io, we need to verify now
1007 if (queue_full(td) && async_do_verify(td, &v_io_u))
1012 async_do_verify(td, &v_io_u);
1015 cleanup_pending_aio(td);
1017 td_set_runstate(td, TD_RUNNING);
1020 static void do_async_io(struct thread_data *td)
1022 struct timeval s, e;
1025 while (td->this_io_bytes < td->io_size) {
1026 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1027 struct timespec *timeout;
1028 int ret, min_evts = 0;
1030 unsigned int bytes_done;
1035 io_u = get_io_u(td);
1039 memcpy(&s, &io_u->start_time, sizeof(s));
1041 ret = io_u_queue(td, io_u);
1048 gettimeofday(&io_u->issue_time, NULL);
1049 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1050 if (td->cur_depth < td->aio_depth) {
1058 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1065 bytes_done = ios_completed(td, ret);
1068 * the rate is batched for now, it should work for batches
1069 * of completions except the very first one which may look
1072 gettimeofday(&e, NULL);
1073 usec = utime_since(&s, &e);
1075 rate_throttle(td, usec, bytes_done);
1077 if (check_min_rate(td, &e)) {
1078 td_verror(td, ENODATA);
1082 if (runtime_exceeded(td, &e))
1086 usec_sleep(td, td->thinktime);
1088 if (should_fsync(td) && td->fsync_blocks &&
1089 (td->io_blocks % td->fsync_blocks) == 0)
1094 cleanup_pending_aio(td);
1096 if (should_fsync(td))
1100 static void cleanup_aio(struct thread_data *td)
1102 if (td->io_engine == FIO_LIBAIO)
1103 fio_libaio_cleanup(td);
1104 else if (td->io_engine == FIO_POSIXAIO)
1105 fio_posixaio_cleanup(td);
1108 static int init_aio(struct thread_data *td)
1110 if (td->io_engine == FIO_LIBAIO)
1111 return fio_libaio_init(td);
1112 else if (td->io_engine == FIO_POSIXAIO)
1113 return fio_posixaio_init(td);
1115 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1120 static void cleanup_io_u(struct thread_data *td)
1122 struct list_head *entry, *n;
1125 list_for_each_safe(entry, n, &td->io_u_freelist) {
1126 io_u = list_entry(entry, struct io_u, list);
1128 list_del(&io_u->list);
1132 if (td->mem_type == MEM_MALLOC)
1133 free(td->orig_buffer);
1134 else if (td->mem_type == MEM_SHM) {
1135 struct shmid_ds sbuf;
1137 shmdt(td->orig_buffer);
1138 shmctl(td->shm_id, IPC_RMID, &sbuf);
1139 } else if (td->mem_type == MEM_MMAP)
1140 munmap(td->orig_buffer, td->orig_buffer_size);
1142 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1144 td->orig_buffer = NULL;
1147 static int init_io_u(struct thread_data *td)
1153 if (td->io_engine == FIO_SYNCIO)
1156 max_units = td->aio_depth;
1158 td->orig_buffer_size = td->max_bs * max_units + MASK;
1160 if (td->mem_type == MEM_MALLOC)
1161 td->orig_buffer = malloc(td->orig_buffer_size);
1162 else if (td->mem_type == MEM_SHM) {
1163 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1164 if (td->shm_id < 0) {
1165 td_verror(td, errno);
1170 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1171 if (td->orig_buffer == (void *) -1) {
1172 td_verror(td, errno);
1174 td->orig_buffer = NULL;
1177 } else if (td->mem_type == MEM_MMAP) {
1178 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1179 if (td->orig_buffer == MAP_FAILED) {
1180 td_verror(td, errno);
1182 td->orig_buffer = NULL;
1187 INIT_LIST_HEAD(&td->io_u_freelist);
1188 INIT_LIST_HEAD(&td->io_u_busylist);
1189 INIT_LIST_HEAD(&td->io_hist_list);
1191 p = ALIGN(td->orig_buffer);
1192 for (i = 0; i < max_units; i++) {
1193 io_u = malloc(sizeof(*io_u));
1194 memset(io_u, 0, sizeof(*io_u));
1195 INIT_LIST_HEAD(&io_u->list);
1197 io_u->buf = p + td->max_bs * i;
1198 list_add(&io_u->list, &td->io_u_freelist);
1204 static int create_file(struct thread_data *td, unsigned long long size,
1207 unsigned long long left;
1213 * unless specifically asked for overwrite, let normal io extend it
1215 if (td_write(td) && !td->overwrite)
1219 fprintf(stderr, "Need size for create\n");
1220 td_verror(td, EINVAL);
1225 oflags = O_CREAT | O_TRUNC;
1226 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1229 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1232 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1234 td_verror(td, errno);
1238 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1239 td_verror(td, errno);
1243 td->io_size = td->file_size;
1244 b = malloc(td->max_bs);
1245 memset(b, 0, td->max_bs);
1248 while (left && !td->terminate) {
1253 r = write(td->fd, b, bs);
1255 if (r == (int) bs) {
1260 td_verror(td, errno);
1269 unlink(td->file_name);
1270 else if (td->create_fsync)
1279 static int file_size(struct thread_data *td)
1283 if (fstat(td->fd, &st) == -1) {
1284 td_verror(td, errno);
1289 td->file_size = st.st_size;
1294 static int bdev_size(struct thread_data *td)
1298 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1299 td_verror(td, errno);
1304 * no extend possibilities, so limit size to device size if too large
1306 if (!td->file_size || td->file_size > bytes)
1307 td->file_size = bytes;
1312 static int get_file_size(struct thread_data *td)
1316 if (td->filetype == FIO_TYPE_FILE)
1317 ret = file_size(td);
1319 ret = bdev_size(td);
1324 if (td->file_offset > td->file_size) {
1325 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1329 td->io_size = td->file_size - td->file_offset;
1330 if (td->io_size == 0) {
1331 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1332 td_verror(td, EINVAL);
1339 static int setup_file_mmap(struct thread_data *td)
1348 if (td->verify != VERIFY_NONE)
1352 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1353 if (td->mmap == MAP_FAILED) {
1355 td_verror(td, errno);
1359 if (td->invalidate_cache) {
1360 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1361 td_verror(td, errno);
1366 if (td->sequential) {
1367 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1368 td_verror(td, errno);
1372 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1373 td_verror(td, errno);
1381 static int setup_file_plain(struct thread_data *td)
1383 if (td->invalidate_cache) {
1384 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1385 td_verror(td, errno);
1390 if (td->sequential) {
1391 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1392 td_verror(td, errno);
1396 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1397 td_verror(td, errno);
1405 static int setup_file(struct thread_data *td)
1410 if (stat(td->file_name, &st) == -1) {
1411 if (errno != ENOENT) {
1412 td_verror(td, errno);
1415 if (!td->create_file) {
1416 td_verror(td, ENOENT);
1419 if (create_file(td, td->file_size, 0))
1421 } else if (td->filetype == FIO_TYPE_FILE) {
1422 if (st.st_size < td->file_size) {
1423 if (create_file(td, td->file_size - st.st_size, 1))
1432 td->fd = open(td->file_name, flags | O_RDONLY);
1434 if (td->filetype == FIO_TYPE_FILE) {
1445 td->fd = open(td->file_name, flags, 0600);
1449 td_verror(td, errno);
1453 if (get_file_size(td))
1457 return setup_file_plain(td);
1459 return setup_file_mmap(td);
1462 static int check_dev_match(dev_t dev, char *path)
1464 unsigned int major, minor;
1468 f = fopen(path, "r");
1470 perror("open path");
1474 p = fgets(line, sizeof(line), f);
1480 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1485 if (((major << 8) | minor) == dev) {
1494 static char *find_block_dir(dev_t dev, char *path)
1505 while ((dir = readdir(D)) != NULL) {
1506 char full_path[256];
1508 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1510 if (!strcmp(dir->d_name, "device"))
1513 sprintf(full_path, "%s/%s", path, dir->d_name);
1515 if (!strcmp(dir->d_name, "dev")) {
1516 if (!check_dev_match(dev, full_path)) {
1522 if (stat(full_path, &st) == -1) {
1527 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1530 if ((found = find_block_dir(dev, full_path)) != NULL)
1538 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1545 f = fopen(du->path, "r");
1549 p = fgets(line, sizeof(line), f);
1555 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) {
1564 static void update_io_tick_disk(struct disk_util *du)
1566 struct disk_util_stat __dus, *dus, *ldus;
1569 if (get_io_ticks(du, &__dus))
1573 ldus = &du->last_dus;
1575 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1576 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1577 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1578 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1579 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1580 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1581 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1582 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1583 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1584 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1586 gettimeofday(&t, NULL);
1587 du->msec += mtime_since(&du->time, &t);
1588 memcpy(&du->time, &t, sizeof(t));
1589 memcpy(ldus, &__dus, sizeof(__dus));
1592 static void update_io_ticks(void)
1594 struct list_head *entry;
1595 struct disk_util *du;
1597 list_for_each(entry, &disk_list) {
1598 du = list_entry(entry, struct disk_util, list);
1599 update_io_tick_disk(du);
1603 static int disk_util_exists(dev_t dev)
1605 struct list_head *entry;
1606 struct disk_util *du;
1608 list_for_each(entry, &disk_list) {
1609 du = list_entry(entry, struct disk_util, list);
1618 static void disk_util_add(dev_t dev, char *path)
1620 struct disk_util *du = malloc(sizeof(*du));
1622 memset(du, 0, sizeof(*du));
1623 INIT_LIST_HEAD(&du->list);
1624 sprintf(du->path, "%s/stat", path);
1625 du->name = strdup(basename(path));
1628 gettimeofday(&du->time, NULL);
1629 get_io_ticks(du, &du->last_dus);
1631 list_add_tail(&du->list, &disk_list);
1634 static void init_disk_util(struct thread_data *td)
1637 char foo[256], tmp[256];
1641 if (!stat(td->file_name, &st)) {
1642 if (S_ISBLK(st.st_mode))
1648 * must be a file, open "." in that path
1650 strcpy(foo, td->file_name);
1653 perror("disk util stat");
1660 if (disk_util_exists(dev))
1663 sprintf(foo, "/sys/block");
1664 dir = find_block_dir(dev, foo);
1669 * if this is inside a partition dir, jump back to parent
1671 sprintf(tmp, "%s/queue", dir);
1672 if (stat(tmp, &st)) {
1674 sprintf(tmp, "%s/queue", p);
1675 if (stat(tmp, &st)) {
1676 fprintf(stderr, "unknown sysfs layout\n");
1679 sprintf(foo, "%s", p);
1682 disk_util_add(dev, foo);
1685 static void disk_util_timer_arm(void)
1687 struct itimerval itimer;
1689 itimer.it_value.tv_sec = 0;
1690 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1691 setitimer(ITIMER_REAL, &itimer, NULL);
1694 static void clear_io_state(struct thread_data *td)
1696 if (td->io_engine == FIO_SYNCIO)
1697 lseek(td->fd, SEEK_SET, 0);
1701 td->stat_io_bytes = 0;
1702 td->this_io_bytes = 0;
1705 memset(td->file_map, 0, td->num_maps * sizeof(long));
1708 static void update_rusage_stat(struct thread_data *td)
1713 getrusage(RUSAGE_SELF, &td->ru_end);
1715 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1716 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1717 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1720 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1723 static void *thread_main(void *data)
1725 struct thread_data *td = data;
1734 if (fio_setaffinity(td) == -1) {
1735 td_verror(td, errno);
1739 if ((td->io_engine != FIO_SYNCIO) && init_aio(td))
1743 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1744 td_verror(td, errno);
1749 sem_post(&startup_sem);
1750 sem_wait(&td->mutex);
1752 if (!td->create_serialize && setup_file(td))
1755 if (init_random_state(td))
1758 gettimeofday(&td->epoch, NULL);
1760 while (td->loops--) {
1761 getrusage(RUSAGE_SELF, &td->ru_start);
1762 gettimeofday(&td->start, NULL);
1763 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1766 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1769 prune_io_piece_log(td);
1771 if (td->io_engine == FIO_SYNCIO)
1776 if (td->error || td->terminate)
1779 td->runtime += mtime_since_now(&td->start);
1780 update_rusage_stat(td);
1782 if (td->verify == VERIFY_NONE)
1787 if (td->io_engine == FIO_SYNCIO)
1790 do_async_verify(td);
1792 if (td->error || td->terminate)
1799 finish_log(td, td->bw_log, "bw");
1801 finish_log(td, td->lat_log, "lat");
1803 if (exitall_on_terminate)
1804 terminate_threads(td->groupid);
1812 munmap(td->mmap, td->file_size);
1813 if (td->io_engine != FIO_SYNCIO)
1817 sem_post(&startup_sem);
1818 sem_wait(&td->mutex);
1820 td_set_runstate(td, TD_EXITED);
1825 static void *fork_main(int shmid, int offset)
1827 struct thread_data *td;
1830 data = shmat(shmid, NULL, 0);
1831 if (data == (void *) -1) {
1836 td = data + offset * sizeof(struct thread_data);
1842 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1843 double *mean, double *dev)
1847 if (is->samples == 0)
1853 n = (double) is->samples;
1854 *mean = (double) is->val / n;
1855 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1859 static void show_thread_status(struct thread_data *td,
1860 struct group_run_stats *rs)
1862 int prio, prio_class;
1863 unsigned long min, max, bw = 0;
1864 double mean, dev, usr_cpu, sys_cpu;
1866 if (!td->io_bytes && !td->error)
1870 bw = td->io_bytes / td->runtime;
1872 prio = td->ioprio & 0xff;
1873 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1875 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);
1877 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1878 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1879 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1880 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1881 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1884 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1885 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);
1889 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1890 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1896 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1899 static void print_thread_status(int nr_running, int t_rate, int m_rate)
1901 printf("Threads now running: %d", nr_running);
1902 if (m_rate || t_rate)
1903 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1904 printf(" : [%s]\r", run_str);
1908 static void check_str_update(struct thread_data *td, int n, int t, int m)
1910 char c = run_str[td->thread_number - 1];
1912 if (td->runstate == td->old_runstate)
1915 switch (td->runstate) {
1941 case TD_NOT_CREATED:
1945 printf("state %d\n", td->runstate);
1948 run_str[td->thread_number - 1] = c;
1949 print_thread_status(n, t, m);
1950 td->old_runstate = td->runstate;
1953 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1958 * reap exited threads (TD_EXITED -> TD_REAPED)
1960 for (i = 0; i < thread_number; i++) {
1961 struct thread_data *td = &threads[i];
1963 check_str_update(td, *nr_running, *t_rate, *m_rate);
1965 if (td->runstate != TD_EXITED)
1968 td_set_runstate(td, TD_REAPED);
1970 if (td->use_thread) {
1973 if (pthread_join(td->thread, (void *) &ret))
1974 perror("thread_join");
1976 waitpid(td->pid, NULL, 0);
1979 (*m_rate) -= td->ratemin;
1980 (*t_rate) -= td->rate;
1981 check_str_update(td, *nr_running, *t_rate, *m_rate);
1985 static void run_threads(void)
1987 struct timeval genesis;
1988 struct thread_data *td;
1989 unsigned long spent;
1990 int i, todo, nr_running, m_rate, t_rate, nr_started;
1992 printf("Starting %d threads\n", thread_number);
1995 signal(SIGINT, sig_handler);
1996 signal(SIGALRM, sig_handler);
1998 todo = thread_number;
2001 m_rate = t_rate = 0;
2003 for (i = 0; i < thread_number; i++) {
2008 if (!td->create_serialize)
2012 * do file setup here so it happens sequentially,
2013 * we don't want X number of threads getting their
2014 * client data interspersed on disk
2016 if (setup_file(td)) {
2017 td_set_runstate(td, TD_REAPED);
2022 gettimeofday(&genesis, NULL);
2026 * create threads (TD_NOT_CREATED -> TD_CREATED)
2028 for (i = 0; i < thread_number; i++) {
2031 if (td->runstate != TD_NOT_CREATED)
2035 * never got a chance to start, killed by other
2036 * thread for some reason
2038 if (td->terminate) {
2043 if (td->start_delay) {
2044 spent = mtime_since_now(&genesis);
2046 if (td->start_delay * 1000 > spent)
2050 if (td->stonewall && (nr_started || nr_running))
2053 td_set_runstate(td, TD_CREATED);
2054 check_str_update(td, nr_running, t_rate, m_rate);
2055 sem_init(&startup_sem, 1, 1);
2059 if (td->use_thread) {
2060 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2061 perror("thread_create");
2066 sem_wait(&startup_sem);
2068 fork_main(shm_id, i);
2075 * start created threads (TD_CREATED -> TD_RUNNING)
2077 for (i = 0; i < thread_number; i++) {
2080 if (td->runstate != TD_CREATED)
2083 td_set_runstate(td, TD_RUNNING);
2086 m_rate += td->ratemin;
2088 check_str_update(td, nr_running, t_rate, m_rate);
2089 sem_post(&td->mutex);
2092 for (i = 0; i < thread_number; i++) {
2095 if (td->runstate != TD_RUNNING &&
2096 td->runstate != TD_VERIFYING)
2099 check_str_update(td, nr_running, t_rate, m_rate);
2102 reap_threads(&nr_running, &t_rate, &m_rate);
2108 while (nr_running) {
2109 reap_threads(&nr_running, &t_rate, &m_rate);
2116 static void show_group_stats(struct group_run_stats *rs, int id)
2118 printf("\nRun status group %d:\n", id);
2120 if (rs->max_run[DDIR_READ])
2121 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]);
2122 if (rs->max_run[DDIR_WRITE])
2123 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]);
2126 static void show_disk_util(void)
2128 struct disk_util_stat *dus;
2129 struct list_head *entry;
2130 struct disk_util *du;
2133 printf("\nDisk stats (read/write):\n");
2135 list_for_each(entry, &disk_list) {
2136 du = list_entry(entry, struct disk_util, list);
2139 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2143 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);
2147 static void show_run_stats(void)
2149 struct group_run_stats *runstats, *rs;
2150 struct thread_data *td;
2153 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2155 for (i = 0; i < groupid + 1; i++) {
2158 memset(rs, 0, sizeof(*rs));
2159 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2160 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2163 for (i = 0; i < thread_number; i++) {
2164 unsigned long bw = 0;
2169 printf("Client%d: %s\n", td->thread_number, td->verror);
2173 rs = &runstats[td->groupid];
2175 if (td->runtime < rs->min_run[td->ddir])
2176 rs->min_run[td->ddir] = td->runtime;
2177 if (td->runtime > rs->max_run[td->ddir])
2178 rs->max_run[td->ddir] = td->runtime;
2181 bw = td->io_bytes / td->runtime;
2182 if (bw < rs->min_bw[td->ddir])
2183 rs->min_bw[td->ddir] = bw;
2184 if (bw > rs->max_bw[td->ddir])
2185 rs->max_bw[td->ddir] = bw;
2187 rs->io_mb[td->ddir] += td->io_bytes >> 20;
2190 for (i = 0; i < groupid + 1; i++) {
2194 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2196 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2199 for (i = 0; i < thread_number; i++) {
2201 rs = &runstats[td->groupid];
2203 show_thread_status(td, rs);
2206 for (i = 0; i < groupid + 1; i++)
2207 show_group_stats(&runstats[i], i);
2212 int main(int argc, char *argv[])
2214 memset(run_str, 0, sizeof(run_str));
2216 if (parse_options(argc, argv))
2219 if (!thread_number) {
2220 printf("Nothing to do\n");
2224 disk_util_timer_arm();