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
33 #include <sys/types.h>
38 #include <sys/ioctl.h>
46 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
49 int thread_number = 0;
50 char run_str[MAX_JOBS + 1];
52 static LIST_HEAD(disk_list);
54 static void update_io_ticks(void);
55 static void disk_util_timer_arm(void);
69 #define should_fsync(td) (td_write(td) && !(td)->odirect)
71 static sem_t startup_sem;
73 #define TERMINATE_ALL (-1)
75 static void terminate_threads(int group_id)
79 for (i = 0; i < thread_number; i++) {
80 struct thread_data *td = &threads[i];
82 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
89 static void sig_handler(int sig)
94 disk_util_timer_arm();
97 printf("\nfio: terminating on signal\n");
99 terminate_threads(TERMINATE_ALL);
104 static unsigned long utime_since(struct timeval *s, struct timeval *e)
108 sec = e->tv_sec - s->tv_sec;
109 usec = e->tv_usec - s->tv_usec;
110 if (sec > 0 && usec < 0) {
115 sec *= (double) 1000000;
120 static unsigned long utime_since_now(struct timeval *s)
124 gettimeofday(&t, NULL);
125 return utime_since(s, &t);
128 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
132 sec = e->tv_sec - s->tv_sec;
133 usec = e->tv_usec - s->tv_usec;
134 if (sec > 0 && usec < 0) {
139 sec *= (double) 1000;
140 usec /= (double) 1000;
145 static unsigned long mtime_since_now(struct timeval *s)
149 gettimeofday(&t, NULL);
150 return mtime_since(s, &t);
153 static inline unsigned long msec_now(struct timeval *s)
155 return s->tv_sec * 1000 + s->tv_usec / 1000;
158 static int random_map_free(struct thread_data *td, unsigned long long block)
160 unsigned int idx = RAND_MAP_IDX(td, block);
161 unsigned int bit = RAND_MAP_BIT(td, block);
163 return (td->file_map[idx] & (1UL << bit)) == 0;
166 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
172 while ((*b) * td->min_bs < td->io_size) {
173 if (td->file_map[i] != -1UL) {
174 *b += ffz(td->file_map[i]);
178 *b += BLOCKS_PER_MAP;
185 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
187 unsigned long block = io_u->offset / td->min_bs;
188 unsigned int blocks = 0;
190 while (blocks < (io_u->buflen / td->min_bs)) {
191 unsigned int idx, bit;
193 if (!random_map_free(td, block))
196 idx = RAND_MAP_IDX(td, block);
197 bit = RAND_MAP_BIT(td, block);
199 assert(idx < td->num_maps);
201 td->file_map[idx] |= (1UL << bit);
206 if ((blocks * td->min_bs) < io_u->buflen)
207 io_u->buflen = blocks * td->min_bs;
210 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
212 unsigned long long b, rb;
215 if (!td->sequential) {
216 unsigned long max_blocks = td->io_size / td->min_bs;
220 lrand48_r(&td->random_state, &r);
221 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
222 rb = b + (td->file_offset / td->min_bs);
224 } while (!random_map_free(td, rb) && loops);
227 if (get_next_free_block(td, &b))
231 b = td->last_bytes / td->min_bs;
233 *offset = (b * td->min_bs) + td->file_offset;
234 if (*offset > td->file_size)
240 static unsigned int get_next_buflen(struct thread_data *td)
245 if (td->min_bs == td->max_bs)
248 lrand48_r(&td->bsrange_state, &r);
249 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
250 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
253 if (buflen > td->io_size - td->this_io_bytes)
254 buflen = td->io_size - td->this_io_bytes;
259 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
261 if (val > is->max_val)
263 if (val < is->min_val)
267 is->val_sq += val * val;
271 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
274 if (iolog->nr_samples == iolog->max_samples) {
275 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
277 iolog->log = realloc(iolog->log, new_size);
278 iolog->max_samples <<= 1;
281 iolog->log[iolog->nr_samples].val = val;
282 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->start);
286 static void add_clat_sample(struct thread_data *td, unsigned long msec)
288 add_stat_sample(&td->clat_stat, msec);
291 add_log_sample(td, td->lat_log, msec);
294 static void add_slat_sample(struct thread_data *td, unsigned long msec)
296 add_stat_sample(&td->slat_stat, msec);
299 static void add_bw_sample(struct thread_data *td)
301 unsigned long spent = mtime_since_now(&td->stat_sample_time);
304 if (spent < td->bw_avg_time)
307 rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
308 add_stat_sample(&td->bw_stat, rate);
311 add_log_sample(td, td->bw_log, rate);
313 gettimeofday(&td->stat_sample_time, NULL);
314 td->stat_io_bytes = td->this_io_bytes;
318 * busy looping version for the last few usec
320 static void __usec_sleep(unsigned int usec)
322 struct timeval start;
324 gettimeofday(&start, NULL);
325 while (utime_since_now(&start) < usec)
329 static void usec_sleep(struct thread_data *td, unsigned long usec)
331 struct timespec req, rem;
333 req.tv_sec = usec / 1000000;
334 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
342 rem.tv_sec = rem.tv_nsec = 0;
343 if (nanosleep(&req, &rem) < 0)
346 if ((rem.tv_sec + rem.tv_nsec) == 0)
349 req.tv_nsec = rem.tv_nsec;
350 req.tv_sec = rem.tv_sec;
352 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
353 } while (!td->terminate);
356 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
359 unsigned long usec_cycle;
364 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
366 if (time_spent < usec_cycle) {
367 unsigned long s = usec_cycle - time_spent;
369 td->rate_pending_usleep += s;
370 if (td->rate_pending_usleep >= 100000) {
371 usec_sleep(td, td->rate_pending_usleep);
372 td->rate_pending_usleep = 0;
375 long overtime = time_spent - usec_cycle;
377 td->rate_pending_usleep -= overtime;
381 static int check_min_rate(struct thread_data *td, struct timeval *now)
387 * allow a 2 second settle period in the beginning
389 if (mtime_since(&td->start, now) < 2000)
393 * if rate blocks is set, sample is running
395 if (td->rate_bytes) {
396 spent = mtime_since(&td->lastrate, now);
397 if (spent < td->ratecycle)
400 rate = (td->this_io_bytes - td->rate_bytes) / spent;
401 if (rate < td->ratemin) {
402 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
404 terminate_threads(td->groupid);
409 td->rate_bytes = td->this_io_bytes;
410 memcpy(&td->lastrate, now, sizeof(*now));
414 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
418 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
424 static void fill_random_bytes(struct thread_data *td,
425 unsigned char *p, unsigned int len)
431 drand48_r(&td->verify_state, &r);
434 * lrand48_r seems to be broken and only fill the bottom
435 * 32-bits, even on 64-bit archs with 64-bit longs
448 static void hexdump(void *buffer, int len)
450 unsigned char *p = buffer;
453 for (i = 0; i < len; i++)
454 printf("%02x", p[i]);
458 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
460 unsigned char *p = (unsigned char *) io_u->buf;
464 c = crc32(p, hdr->len - sizeof(*hdr));
466 return c != hdr->crc32;
469 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
471 unsigned char *p = (unsigned char *) io_u->buf;
472 struct md5_ctx md5_ctx;
475 memset(&md5_ctx, 0, sizeof(md5_ctx));
477 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
479 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
481 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
482 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
488 static int verify_io_u(struct io_u *io_u)
490 struct verify_header *hdr = (struct verify_header *) io_u->buf;
493 if (hdr->fio_magic != FIO_HDR_MAGIC)
496 if (hdr->verify_type == VERIFY_MD5)
497 ret = verify_io_u_md5(hdr, io_u);
498 else if (hdr->verify_type == VERIFY_CRC32)
499 ret = verify_io_u_crc32(hdr, io_u);
501 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
508 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
510 hdr->crc32 = crc32(p, len);
513 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
515 struct md5_ctx md5_ctx;
517 memset(&md5_ctx, 0, sizeof(md5_ctx));
518 md5_update(&md5_ctx, p, len);
519 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
523 * fill body of io_u->buf with random data and add a header with the
524 * (eg) sha1sum of that data.
526 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
528 unsigned char *p = (unsigned char *) io_u->buf;
529 struct verify_header hdr;
531 hdr.fio_magic = FIO_HDR_MAGIC;
532 hdr.len = io_u->buflen;
534 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
536 if (td->verify == VERIFY_MD5) {
537 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
538 hdr.verify_type = VERIFY_MD5;
540 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
541 hdr.verify_type = VERIFY_CRC32;
544 memcpy(io_u->buf, &hdr, sizeof(hdr));
547 static void put_io_u(struct thread_data *td, struct io_u *io_u)
549 list_del(&io_u->list);
550 list_add(&io_u->list, &td->io_u_freelist);
554 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
556 static struct io_u *__get_io_u(struct thread_data *td)
563 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
564 list_del(&io_u->list);
565 list_add(&io_u->list, &td->io_u_busylist);
570 static struct io_u *get_io_u(struct thread_data *td)
574 io_u = __get_io_u(td);
578 if (get_next_offset(td, &io_u->offset)) {
583 io_u->buflen = get_next_buflen(td);
589 if (io_u->buflen + io_u->offset > td->file_size)
590 io_u->buflen = td->file_size - io_u->offset;
598 mark_random_map(td, io_u);
600 td->last_bytes += io_u->buflen;
602 if (td->verify != VERIFY_NONE)
603 populate_io_u(td, io_u);
606 td->io_prep(td, io_u, td_read(td));
608 gettimeofday(&io_u->start_time, NULL);
612 static inline void td_set_runstate(struct thread_data *td, int runstate)
614 td->old_runstate = td->runstate;
615 td->runstate = runstate;
618 static int get_next_verify(struct thread_data *td,
619 unsigned long long *offset, unsigned int *len)
621 struct io_piece *ipo;
623 if (list_empty(&td->io_hist_list))
626 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
627 list_del(&ipo->list);
629 *offset = ipo->offset;
635 static void prune_io_piece_log(struct thread_data *td)
637 struct io_piece *ipo;
639 while (!list_empty(&td->io_hist_list)) {
640 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
642 list_del(&ipo->list);
648 * log a succesful write, so we can unwind the log for verify
650 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
652 struct io_piece *ipo = malloc(sizeof(struct io_piece));
653 struct list_head *entry;
655 INIT_LIST_HEAD(&ipo->list);
656 ipo->offset = io_u->offset;
657 ipo->len = io_u->buflen;
660 * for random io where the writes extend the file, it will typically
661 * be laid out with the block scattered as written. it's faster to
662 * read them in in that order again, so don't sort
664 if (td->sequential || !td->overwrite) {
665 list_add_tail(&ipo->list, &td->io_hist_list);
670 * for random io, sort the list so verify will run faster
672 entry = &td->io_hist_list;
673 while ((entry = entry->prev) != &td->io_hist_list) {
674 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
676 if (__ipo->offset < ipo->offset)
680 list_add(&ipo->list, entry);
683 static void do_sync_verify(struct thread_data *td)
686 struct io_u *io_u = NULL;
689 td_set_runstate(td, TD_VERIFYING);
691 io_u = __get_io_u(td);
695 if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
696 td_verror(td, errno);
700 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
701 td_verror(td, errno);
711 gettimeofday(&t, NULL);
712 if (runtime_exceeded(td, &t))
715 if (get_next_verify(td, &io_u->offset, &io_u->buflen))
718 if (td->cur_off != io_u->offset) {
719 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
720 td_verror(td, errno);
725 ret = read(td->fd, io_u->buf, io_u->buflen);
726 if (ret < (int) io_u->buflen) {
728 td_verror(td, errno);
736 if (verify_io_u(io_u))
739 td->cur_off = io_u->offset + io_u->buflen;
743 td_set_runstate(td, TD_RUNNING);
747 static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u)
749 unsigned long long real_off = io_u->offset - td->file_offset;
752 memcpy(io_u->buf, td->mmap + real_off, io_u->buflen);
754 memcpy(td->mmap + real_off, io_u->buf, io_u->buflen);
757 * not really direct, but should drop the pages from the cache
760 msync(td->mmap + real_off, io_u->buflen, MS_SYNC);
761 madvise(td->mmap + real_off, io_u->buflen, MADV_DONTNEED);
767 static int __do_sync_rw(struct thread_data *td, struct io_u *io_u)
769 if (td->cur_off != io_u->offset) {
770 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
771 td_verror(td, errno);
777 return read(td->fd, io_u->buf, io_u->buflen);
779 return write(td->fd, io_u->buf, io_u->buflen);
782 static void sync_td(struct thread_data *td)
787 msync(td->mmap, td->file_size, MS_SYNC);
790 static void do_sync_io(struct thread_data *td)
792 unsigned long msec, usec;
793 struct io_u *io_u = NULL;
796 while (td->this_io_bytes < td->io_size) {
807 ret = __do_sync_rw(td, io_u);
809 ret = __do_sync_mmap(td, io_u);
811 if (ret < (int) io_u->buflen) {
813 td_verror(td, errno);
818 log_io_piece(td, io_u);
821 td->io_bytes += io_u->buflen;
822 td->this_io_bytes += io_u->buflen;
823 td->cur_off = io_u->offset + io_u->buflen;
825 gettimeofday(&e, NULL);
827 usec = utime_since(&io_u->start_time, &e);
829 rate_throttle(td, usec, io_u->buflen);
831 if (check_min_rate(td, &e)) {
832 td_verror(td, ENOMEM);
837 add_clat_sample(td, msec);
840 if (runtime_exceeded(td, &e))
847 usec_sleep(td, td->thinktime);
849 if (should_fsync(td) && td->fsync_blocks &&
850 (td->io_blocks % td->fsync_blocks) == 0)
857 if (should_fsync(td))
861 static int io_u_getevents(struct thread_data *td, int min, int max,
864 return td->io_getevents(td, min, max, t);
867 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
869 return td->io_queue(td, io_u);
872 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
874 static int ios_completed(struct thread_data *td, int nr)
881 gettimeofday(&e, NULL);
883 for (i = 0, bytes_done = 0; i < nr; i++) {
884 io_u = td->io_event(td, i);
887 td->io_bytes += io_u->buflen;
888 td->this_io_bytes += io_u->buflen;
890 msec = mtime_since(&io_u->issue_time, &e);
892 add_clat_sample(td, msec);
896 log_io_piece(td, io_u);
898 bytes_done += io_u->buflen;
905 static void cleanup_pending_aio(struct thread_data *td)
907 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
908 struct list_head *entry, *n;
913 * get immediately available events, if any
915 r = io_u_getevents(td, 0, td->cur_depth, &ts);
917 ios_completed(td, r);
920 * now cancel remaining active events
922 list_for_each_safe(entry, n, &td->io_u_busylist) {
923 io_u = list_entry(entry, struct io_u, list);
925 r = td->io_cancel(td, io_u);
931 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
933 ios_completed(td, r);
937 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
939 struct io_u *v_io_u = *io_u;
943 ret = verify_io_u(v_io_u);
944 put_io_u(td, v_io_u);
951 static void do_async_verify(struct thread_data *td)
954 struct io_u *io_u, *v_io_u = NULL;
957 td_set_runstate(td, TD_VERIFYING);
963 gettimeofday(&t, NULL);
964 if (runtime_exceeded(td, &t))
967 io_u = __get_io_u(td);
971 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
976 td->io_prep(td, io_u, 1);
978 ret = io_u_queue(td, io_u);
986 * we have one pending to verify, do that while the next
987 * we are doing io on the next one
989 if (async_do_verify(td, &v_io_u))
992 ret = io_u_getevents(td, 1, 1, NULL);
999 v_io_u = td->io_event(td, 0);
1001 td->cur_off = v_io_u->offset + v_io_u->buflen;
1004 * if we can't submit more io, we need to verify now
1006 if (queue_full(td) && async_do_verify(td, &v_io_u))
1011 async_do_verify(td, &v_io_u);
1014 cleanup_pending_aio(td);
1016 td_set_runstate(td, TD_RUNNING);
1019 static void do_async_io(struct thread_data *td)
1021 struct timeval s, e;
1024 while (td->this_io_bytes < td->io_size) {
1025 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1026 struct timespec *timeout;
1027 int ret, min_evts = 0;
1029 unsigned int bytes_done;
1034 io_u = get_io_u(td);
1038 memcpy(&s, &io_u->start_time, sizeof(s));
1040 ret = io_u_queue(td, io_u);
1047 gettimeofday(&io_u->issue_time, NULL);
1048 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1049 if (td->cur_depth < td->aio_depth) {
1057 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1064 bytes_done = ios_completed(td, ret);
1067 * the rate is batched for now, it should work for batches
1068 * of completions except the very first one which may look
1071 gettimeofday(&e, NULL);
1072 usec = utime_since(&s, &e);
1074 rate_throttle(td, usec, bytes_done);
1076 if (check_min_rate(td, &e)) {
1077 td_verror(td, ENOMEM);
1081 if (runtime_exceeded(td, &e))
1085 usec_sleep(td, td->thinktime);
1087 if (should_fsync(td) && td->fsync_blocks &&
1088 (td->io_blocks % td->fsync_blocks) == 0)
1093 cleanup_pending_aio(td);
1095 if (should_fsync(td))
1099 static void cleanup_aio(struct thread_data *td)
1101 if (td->io_engine == FIO_LIBAIO)
1102 fio_libaio_cleanup(td);
1103 else if (td->io_engine == FIO_POSIXAIO)
1104 fio_posixaio_cleanup(td);
1107 static int init_aio(struct thread_data *td)
1109 if (td->io_engine == FIO_LIBAIO)
1110 return fio_libaio_init(td);
1111 else if (td->io_engine == FIO_POSIXAIO)
1112 return fio_posixaio_init(td);
1114 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1119 static void cleanup_io_u(struct thread_data *td)
1121 struct list_head *entry, *n;
1124 list_for_each_safe(entry, n, &td->io_u_freelist) {
1125 io_u = list_entry(entry, struct io_u, list);
1127 list_del(&io_u->list);
1131 if (td->mem_type == MEM_MALLOC)
1132 free(td->orig_buffer);
1133 else if (td->mem_type == MEM_SHM) {
1134 struct shmid_ds sbuf;
1136 shmdt(td->orig_buffer);
1137 shmctl(td->shm_id, IPC_RMID, &sbuf);
1138 } else if (td->mem_type == MEM_MMAP)
1139 munmap(td->orig_buffer, td->orig_buffer_size);
1141 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1143 td->orig_buffer = NULL;
1146 static int init_io_u(struct thread_data *td)
1152 if (td->io_engine == FIO_SYNCIO)
1155 max_units = td->aio_depth;
1157 td->orig_buffer_size = td->max_bs * max_units + MASK;
1159 if (td->mem_type == MEM_MALLOC)
1160 td->orig_buffer = malloc(td->orig_buffer_size);
1161 else if (td->mem_type == MEM_SHM) {
1162 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1163 if (td->shm_id < 0) {
1164 td_verror(td, errno);
1169 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1170 if (td->orig_buffer == (void *) -1) {
1171 td_verror(td, errno);
1173 td->orig_buffer = NULL;
1176 } else if (td->mem_type == MEM_MMAP) {
1177 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1178 if (td->orig_buffer == MAP_FAILED) {
1179 td_verror(td, errno);
1181 td->orig_buffer = NULL;
1186 INIT_LIST_HEAD(&td->io_u_freelist);
1187 INIT_LIST_HEAD(&td->io_u_busylist);
1188 INIT_LIST_HEAD(&td->io_hist_list);
1190 p = ALIGN(td->orig_buffer);
1191 for (i = 0; i < max_units; i++) {
1192 io_u = malloc(sizeof(*io_u));
1193 memset(io_u, 0, sizeof(*io_u));
1194 INIT_LIST_HEAD(&io_u->list);
1196 io_u->buf = p + td->max_bs * i;
1197 list_add(&io_u->list, &td->io_u_freelist);
1203 static int create_file(struct thread_data *td, unsigned long long size,
1206 unsigned long long left;
1212 * unless specifically asked for overwrite, let normal io extend it
1214 if (td_write(td) && !td->overwrite)
1218 fprintf(stderr, "Need size for create\n");
1219 td_verror(td, EINVAL);
1224 oflags = O_CREAT | O_TRUNC;
1225 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1228 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1231 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1233 td_verror(td, errno);
1237 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1238 td_verror(td, errno);
1242 td->io_size = td->file_size;
1243 b = malloc(td->max_bs);
1244 memset(b, 0, td->max_bs);
1247 while (left && !td->terminate) {
1252 r = write(td->fd, b, bs);
1254 if (r == (int) bs) {
1259 td_verror(td, errno);
1268 unlink(td->file_name);
1269 else if (td->create_fsync)
1278 static int file_size(struct thread_data *td)
1282 if (fstat(td->fd, &st) == -1) {
1283 td_verror(td, errno);
1288 td->file_size = st.st_size;
1293 static int bdev_size(struct thread_data *td)
1297 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1298 td_verror(td, errno);
1303 * no extend possibilities, so limit size to device size if too large
1305 if (!td->file_size || td->file_size > bytes)
1306 td->file_size = bytes;
1311 static int get_file_size(struct thread_data *td)
1315 if (td->filetype == FIO_TYPE_FILE)
1316 ret = file_size(td);
1318 ret = bdev_size(td);
1323 if (td->file_offset > td->file_size) {
1324 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1328 td->io_size = td->file_size - td->file_offset;
1329 if (td->io_size == 0) {
1330 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1331 td_verror(td, EINVAL);
1338 static int setup_file_mmap(struct thread_data *td)
1347 if (td->verify != VERIFY_NONE)
1351 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1352 if (td->mmap == MAP_FAILED) {
1354 td_verror(td, errno);
1358 if (td->invalidate_cache) {
1359 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1360 td_verror(td, errno);
1365 if (td->sequential) {
1366 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1367 td_verror(td, errno);
1371 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1372 td_verror(td, errno);
1380 static int setup_file_plain(struct thread_data *td)
1382 if (td->invalidate_cache) {
1383 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1384 td_verror(td, errno);
1389 if (td->sequential) {
1390 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1391 td_verror(td, errno);
1395 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1396 td_verror(td, errno);
1404 static int setup_file(struct thread_data *td)
1409 if (stat(td->file_name, &st) == -1) {
1410 if (errno != ENOENT) {
1411 td_verror(td, errno);
1414 if (!td->create_file) {
1415 td_verror(td, ENOENT);
1418 if (create_file(td, td->file_size, 0))
1420 } else if (td->filetype == FIO_TYPE_FILE) {
1421 if (st.st_size < td->file_size) {
1422 if (create_file(td, td->file_size - st.st_size, 1))
1431 td->fd = open(td->file_name, flags | O_RDONLY);
1433 if (td->filetype == FIO_TYPE_FILE) {
1444 td->fd = open(td->file_name, flags, 0600);
1448 td_verror(td, errno);
1452 if (get_file_size(td))
1456 return setup_file_plain(td);
1458 return setup_file_mmap(td);
1461 static int check_dev_match(dev_t dev, char *path)
1463 unsigned int major, minor;
1467 f = fopen(path, "r");
1469 perror("open path");
1473 p = fgets(line, sizeof(line), f);
1479 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1484 if (((major << 8) | minor) == dev) {
1493 static char *find_block_dir(dev_t dev, char *path)
1504 while ((dir = readdir(D)) != NULL) {
1505 char full_path[256];
1507 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1509 if (!strcmp(dir->d_name, "device"))
1512 sprintf(full_path, "%s/%s", path, dir->d_name);
1514 if (!strcmp(dir->d_name, "dev")) {
1515 if (!check_dev_match(dev, full_path)) {
1521 if (stat(full_path, &st) == -1) {
1526 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1529 if ((found = find_block_dir(dev, full_path)) != NULL)
1537 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1544 f = fopen(du->path, "r");
1548 p = fgets(line, sizeof(line), f);
1554 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) {
1563 static void update_io_tick_disk(struct disk_util *du)
1565 struct disk_util_stat __dus, *dus, *ldus;
1568 if (get_io_ticks(du, &__dus))
1572 ldus = &du->last_dus;
1574 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1575 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1576 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1577 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1578 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1579 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1580 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1581 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1582 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1583 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1585 gettimeofday(&t, NULL);
1586 du->msec += mtime_since(&du->time, &t);
1587 memcpy(&du->time, &t, sizeof(t));
1588 memcpy(ldus, &__dus, sizeof(__dus));
1591 static void update_io_ticks(void)
1593 struct list_head *entry;
1594 struct disk_util *du;
1596 list_for_each(entry, &disk_list) {
1597 du = list_entry(entry, struct disk_util, list);
1598 update_io_tick_disk(du);
1602 static int disk_util_exists(dev_t dev)
1604 struct list_head *entry;
1605 struct disk_util *du;
1607 list_for_each(entry, &disk_list) {
1608 du = list_entry(entry, struct disk_util, list);
1617 static void disk_util_add(dev_t dev, char *path)
1619 struct disk_util *du = malloc(sizeof(*du));
1621 memset(du, 0, sizeof(*du));
1622 INIT_LIST_HEAD(&du->list);
1623 sprintf(du->path, "%s/stat", path);
1624 du->name = strdup(basename(path));
1627 gettimeofday(&du->time, NULL);
1628 get_io_ticks(du, &du->last_dus);
1630 list_add_tail(&du->list, &disk_list);
1633 static void init_disk_util(struct thread_data *td)
1636 char foo[256], tmp[256];
1640 if (!td->do_disk_util)
1643 if (!stat(td->file_name, &st)) {
1644 if (S_ISBLK(st.st_mode))
1650 * must be a file, open "." in that path
1652 strcpy(foo, td->file_name);
1655 perror("disk util stat");
1662 if (disk_util_exists(dev))
1665 sprintf(foo, "/sys/block");
1666 dir = find_block_dir(dev, foo);
1671 * if this is inside a partition dir, jump back to parent
1673 sprintf(tmp, "%s/queue", dir);
1674 if (stat(tmp, &st)) {
1676 sprintf(tmp, "%s/queue", p);
1677 if (stat(tmp, &st)) {
1678 fprintf(stderr, "unknown sysfs layout\n");
1681 sprintf(foo, "%s", p);
1684 disk_util_add(dev, foo);
1687 static void disk_util_timer_arm(void)
1689 struct itimerval itimer;
1691 itimer.it_value.tv_sec = 0;
1692 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1693 setitimer(ITIMER_REAL, &itimer, NULL);
1696 static void clear_io_state(struct thread_data *td)
1698 if (td->io_engine == FIO_SYNCIO)
1699 lseek(td->fd, SEEK_SET, 0);
1703 td->stat_io_bytes = 0;
1704 td->this_io_bytes = 0;
1707 memset(td->file_map, 0, td->num_maps * sizeof(long));
1710 static void update_rusage_stat(struct thread_data *td)
1715 getrusage(RUSAGE_SELF, &td->ru_end);
1717 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1718 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1719 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1722 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1725 static void *thread_main(void *data)
1727 struct thread_data *td = data;
1736 if (fio_setaffinity(td) == -1) {
1737 td_verror(td, errno);
1741 if ((td->io_engine != FIO_SYNCIO) && init_aio(td))
1745 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1746 td_verror(td, errno);
1751 sem_post(&startup_sem);
1752 sem_wait(&td->mutex);
1754 if (!td->create_serialize && setup_file(td))
1757 if (init_random_state(td))
1760 gettimeofday(&td->epoch, NULL);
1762 while (td->loops--) {
1763 getrusage(RUSAGE_SELF, &td->ru_start);
1764 gettimeofday(&td->start, NULL);
1765 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1768 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1771 prune_io_piece_log(td);
1773 if (td->io_engine == FIO_SYNCIO)
1778 if (td->error || td->terminate)
1781 td->runtime += mtime_since_now(&td->start);
1782 update_rusage_stat(td);
1784 if (td->verify == VERIFY_NONE)
1789 if (td->io_engine == FIO_SYNCIO)
1792 do_async_verify(td);
1794 if (td->error || td->terminate)
1801 finish_log(td, td->bw_log, "bw");
1803 finish_log(td, td->lat_log, "lat");
1805 if (exitall_on_terminate)
1806 terminate_threads(td->groupid);
1814 munmap(td->mmap, td->file_size);
1815 if (td->io_engine != FIO_SYNCIO)
1819 sem_post(&startup_sem);
1820 sem_wait(&td->mutex);
1822 td_set_runstate(td, TD_EXITED);
1827 static void *fork_main(int shmid, int offset)
1829 struct thread_data *td;
1832 data = shmat(shmid, NULL, 0);
1833 if (data == (void *) -1) {
1838 td = data + offset * sizeof(struct thread_data);
1844 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1845 double *mean, double *dev)
1849 if (is->samples == 0)
1855 n = (double) is->samples;
1856 *mean = (double) is->val / n;
1857 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1861 static void show_thread_status(struct thread_data *td,
1862 struct group_run_stats *rs)
1864 int prio, prio_class;
1865 unsigned long min, max, bw = 0;
1866 double mean, dev, usr_cpu, sys_cpu;
1868 if (!td->io_bytes && !td->error)
1872 bw = td->io_bytes / td->runtime;
1874 prio = td->ioprio & 0xff;
1875 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1877 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);
1879 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1880 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1881 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1882 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1883 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1886 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1887 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);
1891 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1892 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1898 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1901 static void print_thread_status(int nr_running, int t_rate, int m_rate)
1903 printf("Threads now running: %d", nr_running);
1904 if (m_rate || t_rate)
1905 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1906 printf(" : [%s]\r", run_str);
1910 static void check_str_update(struct thread_data *td, int n, int t, int m)
1912 char c = run_str[td->thread_number - 1];
1914 if (td->runstate == td->old_runstate)
1917 switch (td->runstate) {
1943 case TD_NOT_CREATED:
1947 printf("state %d\n", td->runstate);
1950 run_str[td->thread_number - 1] = c;
1951 print_thread_status(n, t, m);
1952 td->old_runstate = td->runstate;
1955 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1960 * reap exited threads (TD_EXITED -> TD_REAPED)
1962 for (i = 0; i < thread_number; i++) {
1963 struct thread_data *td = &threads[i];
1965 check_str_update(td, *nr_running, *t_rate, *m_rate);
1967 if (td->runstate != TD_EXITED)
1970 td_set_runstate(td, TD_REAPED);
1972 if (td->use_thread) {
1975 if (pthread_join(td->thread, (void *) &ret))
1976 perror("thread_join");
1978 waitpid(td->pid, NULL, 0);
1981 (*m_rate) -= td->ratemin;
1982 (*t_rate) -= td->rate;
1983 check_str_update(td, *nr_running, *t_rate, *m_rate);
1987 static void run_threads(void)
1989 struct timeval genesis;
1990 struct thread_data *td;
1991 unsigned long spent;
1992 int i, todo, nr_running, m_rate, t_rate, nr_started;
1994 printf("Starting %d threads\n", thread_number);
1997 signal(SIGINT, sig_handler);
1998 signal(SIGALRM, sig_handler);
2000 todo = thread_number;
2003 m_rate = t_rate = 0;
2005 for (i = 0; i < thread_number; i++) {
2010 if (!td->create_serialize)
2014 * do file setup here so it happens sequentially,
2015 * we don't want X number of threads getting their
2016 * client data interspersed on disk
2018 if (setup_file(td)) {
2019 td_set_runstate(td, TD_REAPED);
2024 gettimeofday(&genesis, NULL);
2028 * create threads (TD_NOT_CREATED -> TD_CREATED)
2030 for (i = 0; i < thread_number; i++) {
2033 if (td->runstate != TD_NOT_CREATED)
2037 * never got a chance to start, killed by other
2038 * thread for some reason
2040 if (td->terminate) {
2045 if (td->start_delay) {
2046 spent = mtime_since_now(&genesis);
2048 if (td->start_delay * 1000 > spent)
2052 if (td->stonewall && (nr_started || nr_running))
2055 td_set_runstate(td, TD_CREATED);
2056 check_str_update(td, nr_running, t_rate, m_rate);
2057 sem_init(&startup_sem, 1, 1);
2061 if (td->use_thread) {
2062 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2063 perror("thread_create");
2068 sem_wait(&startup_sem);
2070 fork_main(shm_id, i);
2077 * start created threads (TD_CREATED -> TD_RUNNING)
2079 for (i = 0; i < thread_number; i++) {
2082 if (td->runstate != TD_CREATED)
2085 td_set_runstate(td, TD_RUNNING);
2088 m_rate += td->ratemin;
2090 check_str_update(td, nr_running, t_rate, m_rate);
2091 sem_post(&td->mutex);
2094 for (i = 0; i < thread_number; i++) {
2097 if (td->runstate != TD_RUNNING &&
2098 td->runstate != TD_VERIFYING)
2101 check_str_update(td, nr_running, t_rate, m_rate);
2104 reap_threads(&nr_running, &t_rate, &m_rate);
2110 while (nr_running) {
2111 reap_threads(&nr_running, &t_rate, &m_rate);
2118 static void show_group_stats(struct group_run_stats *rs, int id)
2120 printf("\nRun status group %d:\n", id);
2122 if (rs->max_run[DDIR_READ])
2123 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]);
2124 if (rs->max_run[DDIR_WRITE])
2125 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]);
2128 static void show_disk_util(void)
2130 struct disk_util_stat *dus;
2131 struct list_head *entry;
2132 struct disk_util *du;
2135 printf("\nDisk stats (read/write):\n");
2137 list_for_each(entry, &disk_list) {
2138 du = list_entry(entry, struct disk_util, list);
2141 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2145 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);
2149 static void show_run_stats(void)
2151 struct group_run_stats *runstats, *rs;
2152 struct thread_data *td;
2155 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2157 for (i = 0; i < groupid + 1; i++) {
2160 memset(rs, 0, sizeof(*rs));
2161 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2162 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2165 for (i = 0; i < thread_number; i++) {
2166 unsigned long bw = 0;
2171 printf("Client%d: %s\n", td->thread_number, td->verror);
2175 rs = &runstats[td->groupid];
2177 if (td->runtime < rs->min_run[td->ddir])
2178 rs->min_run[td->ddir] = td->runtime;
2179 if (td->runtime > rs->max_run[td->ddir])
2180 rs->max_run[td->ddir] = td->runtime;
2183 bw = td->io_bytes / td->runtime;
2184 if (bw < rs->min_bw[td->ddir])
2185 rs->min_bw[td->ddir] = bw;
2186 if (bw > rs->max_bw[td->ddir])
2187 rs->max_bw[td->ddir] = bw;
2189 rs->io_mb[td->ddir] += td->io_bytes >> 20;
2192 for (i = 0; i < groupid + 1; i++) {
2196 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2198 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2201 for (i = 0; i < thread_number; i++) {
2203 rs = &runstats[td->groupid];
2205 show_thread_status(td, rs);
2208 for (i = 0; i < groupid + 1; i++)
2209 show_group_stats(&runstats[i], i);
2214 int main(int argc, char *argv[])
2216 memset(run_str, 0, sizeof(run_str));
2218 if (parse_options(argc, argv))
2221 if (!thread_number) {
2222 printf("Nothing to do\n");
2226 disk_util_timer_arm();