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>
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);
75 struct timeval start_time;
76 struct timeval issue_time;
80 unsigned long long offset;
82 struct list_head list;
85 #define should_fsync(td) (td_write(td) && !(td)->odirect)
87 static sem_t startup_sem;
89 #define TERMINATE_ALL (-1)
91 static void terminate_threads(int group_id)
95 for (i = 0; i < thread_number; i++) {
96 struct thread_data *td = &threads[i];
98 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
105 static void sig_handler(int sig)
110 disk_util_timer_arm();
113 printf("\nfio: terminating on signal\n");
115 terminate_threads(TERMINATE_ALL);
120 static unsigned long utime_since(struct timeval *s, struct timeval *e)
124 sec = e->tv_sec - s->tv_sec;
125 usec = e->tv_usec - s->tv_usec;
126 if (sec > 0 && usec < 0) {
131 sec *= (double) 1000000;
136 static unsigned long utime_since_now(struct timeval *s)
140 gettimeofday(&t, NULL);
141 return utime_since(s, &t);
144 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
148 sec = e->tv_sec - s->tv_sec;
149 usec = e->tv_usec - s->tv_usec;
150 if (sec > 0 && usec < 0) {
155 sec *= (double) 1000;
156 usec /= (double) 1000;
161 static unsigned long mtime_since_now(struct timeval *s)
165 gettimeofday(&t, NULL);
166 return mtime_since(s, &t);
169 static inline unsigned long msec_now(struct timeval *s)
171 return s->tv_sec * 1000 + s->tv_usec / 1000;
174 static int random_map_free(struct thread_data *td, unsigned long long block)
176 unsigned int idx = RAND_MAP_IDX(td, block);
177 unsigned int bit = RAND_MAP_BIT(td, block);
179 return (td->file_map[idx] & (1UL << bit)) == 0;
182 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
188 while ((*b) * td->min_bs < td->io_size) {
189 if (td->file_map[i] != -1UL) {
190 *b += ffz(td->file_map[i]);
194 *b += BLOCKS_PER_MAP;
201 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
203 unsigned long block = io_u->offset / td->min_bs;
204 unsigned int blocks = 0;
206 while (blocks < (io_u->buflen / td->min_bs)) {
207 unsigned int idx, bit;
209 if (!random_map_free(td, block))
212 idx = RAND_MAP_IDX(td, block);
213 bit = RAND_MAP_BIT(td, block);
215 assert(idx < td->num_maps);
217 td->file_map[idx] |= (1UL << bit);
222 if ((blocks * td->min_bs) < io_u->buflen)
223 io_u->buflen = blocks * td->min_bs;
226 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
228 unsigned long long b, rb;
231 if (!td->sequential) {
232 unsigned long max_blocks = td->io_size / td->min_bs;
236 lrand48_r(&td->random_state, &r);
237 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
238 rb = b + (td->file_offset / td->min_bs);
240 } while (!random_map_free(td, rb) && loops);
243 if (get_next_free_block(td, &b))
247 b = td->last_bytes / td->min_bs;
249 *offset = (b * td->min_bs) + td->file_offset;
250 if (*offset > td->file_size)
256 static unsigned int get_next_buflen(struct thread_data *td)
261 if (td->min_bs == td->max_bs)
264 lrand48_r(&td->bsrange_state, &r);
265 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
266 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
269 if (buflen > td->io_size - td->this_io_bytes)
270 buflen = td->io_size - td->this_io_bytes;
275 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
277 if (val > is->max_val)
279 if (val < is->min_val)
283 is->val_sq += val * val;
287 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
290 if (iolog->nr_samples == iolog->max_samples) {
291 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
293 iolog->log = realloc(iolog->log, new_size);
294 iolog->max_samples <<= 1;
297 iolog->log[iolog->nr_samples].val = val;
298 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->start);
302 static void add_clat_sample(struct thread_data *td, unsigned long msec)
304 add_stat_sample(&td->clat_stat, msec);
307 add_log_sample(td, td->lat_log, msec);
310 static void add_slat_sample(struct thread_data *td, unsigned long msec)
312 add_stat_sample(&td->slat_stat, msec);
315 static void add_bw_sample(struct thread_data *td)
317 unsigned long spent = mtime_since_now(&td->stat_sample_time);
320 if (spent < td->bw_avg_time)
323 rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
324 add_stat_sample(&td->bw_stat, rate);
327 add_log_sample(td, td->bw_log, rate);
329 gettimeofday(&td->stat_sample_time, NULL);
330 td->stat_io_bytes = td->this_io_bytes;
334 * busy looping version for the last few usec
336 static void __usec_sleep(unsigned int usec)
338 struct timeval start;
340 gettimeofday(&start, NULL);
341 while (utime_since_now(&start) < usec)
345 static void usec_sleep(struct thread_data *td, unsigned long usec)
347 struct timespec req, rem;
349 req.tv_sec = usec / 1000000;
350 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
358 rem.tv_sec = rem.tv_nsec = 0;
359 if (nanosleep(&req, &rem) < 0)
362 if ((rem.tv_sec + rem.tv_nsec) == 0)
365 req.tv_nsec = rem.tv_nsec;
366 req.tv_sec = rem.tv_sec;
368 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
369 } while (!td->terminate);
372 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
375 unsigned long usec_cycle;
380 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
382 if (time_spent < usec_cycle) {
383 unsigned long s = usec_cycle - time_spent;
385 td->rate_pending_usleep += s;
386 if (td->rate_pending_usleep >= 100000) {
387 usec_sleep(td, td->rate_pending_usleep);
388 td->rate_pending_usleep = 0;
391 long overtime = time_spent - usec_cycle;
393 td->rate_pending_usleep -= overtime;
397 static int check_min_rate(struct thread_data *td, struct timeval *now)
403 * allow a 2 second settle period in the beginning
405 if (mtime_since(&td->start, now) < 2000)
409 * if rate blocks is set, sample is running
411 if (td->rate_bytes) {
412 spent = mtime_since(&td->lastrate, now);
413 if (spent < td->ratecycle)
416 rate = (td->this_io_bytes - td->rate_bytes) / spent;
417 if (rate < td->ratemin) {
418 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
420 terminate_threads(td->groupid);
425 td->rate_bytes = td->this_io_bytes;
426 memcpy(&td->lastrate, now, sizeof(*now));
430 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
434 if (mtime_since(&td->start, t) >= td->timeout * 1000)
440 static void fill_random_bytes(struct thread_data *td,
441 unsigned char *p, unsigned int len)
447 drand48_r(&td->verify_state, &r);
450 * lrand48_r seems to be broken and only fill the bottom
451 * 32-bits, even on 64-bit archs with 64-bit longs
464 static void hexdump(void *buffer, int len)
466 unsigned char *p = buffer;
469 for (i = 0; i < len; i++)
470 printf("%02x", p[i]);
474 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
476 unsigned char *p = (unsigned char *) io_u->buf;
480 c = crc32(p, hdr->len - sizeof(*hdr));
482 return c != hdr->crc32;
485 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
487 unsigned char *p = (unsigned char *) io_u->buf;
488 struct md5_ctx md5_ctx;
491 memset(&md5_ctx, 0, sizeof(md5_ctx));
493 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
495 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
497 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
498 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
504 static int verify_io_u(struct io_u *io_u)
506 struct verify_header *hdr = (struct verify_header *) io_u->buf;
509 if (hdr->fio_magic != FIO_HDR_MAGIC)
512 if (hdr->verify_type == VERIFY_MD5)
513 ret = verify_io_u_md5(hdr, io_u);
514 else if (hdr->verify_type == VERIFY_CRC32)
515 ret = verify_io_u_crc32(hdr, io_u);
517 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
524 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
526 hdr->crc32 = crc32(p, len);
529 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
531 struct md5_ctx md5_ctx;
533 memset(&md5_ctx, 0, sizeof(md5_ctx));
534 md5_update(&md5_ctx, p, len);
535 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
539 * fill body of io_u->buf with random data and add a header with the
540 * (eg) sha1sum of that data.
542 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
544 unsigned char *p = (unsigned char *) io_u->buf;
545 struct verify_header hdr;
547 hdr.fio_magic = FIO_HDR_MAGIC;
548 hdr.len = io_u->buflen;
550 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
552 if (td->verify == VERIFY_MD5) {
553 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
554 hdr.verify_type = VERIFY_MD5;
556 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
557 hdr.verify_type = VERIFY_CRC32;
560 memcpy(io_u->buf, &hdr, sizeof(hdr));
563 static void put_io_u(struct thread_data *td, struct io_u *io_u)
565 list_del(&io_u->list);
566 list_add(&io_u->list, &td->io_u_freelist);
570 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
572 static struct io_u *__get_io_u(struct thread_data *td)
579 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
580 list_del(&io_u->list);
581 list_add(&io_u->list, &td->io_u_busylist);
586 static struct io_u *get_io_u(struct thread_data *td)
590 io_u = __get_io_u(td);
594 if (get_next_offset(td, &io_u->offset)) {
599 io_u->buflen = get_next_buflen(td);
605 if (io_u->buflen + io_u->offset > td->file_size)
606 io_u->buflen = td->file_size - io_u->offset;
609 mark_random_map(td, io_u);
611 td->last_bytes += io_u->buflen;
613 if (td->verify != VERIFY_NONE)
614 populate_io_u(td, io_u);
618 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
620 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
623 gettimeofday(&io_u->start_time, NULL);
627 static inline void td_set_runstate(struct thread_data *td, int runstate)
629 td->old_runstate = td->runstate;
630 td->runstate = runstate;
633 static int get_next_verify(struct thread_data *td,
634 unsigned long long *offset, unsigned int *len)
636 struct io_piece *ipo;
638 if (list_empty(&td->io_hist_list))
641 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
642 list_del(&ipo->list);
644 *offset = ipo->offset;
650 static void prune_io_piece_log(struct thread_data *td)
652 struct io_piece *ipo;
654 while (!list_empty(&td->io_hist_list)) {
655 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
657 list_del(&ipo->list);
663 * log a succesful write, so we can unwind the log for verify
665 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
667 struct io_piece *ipo = malloc(sizeof(struct io_piece));
668 struct list_head *entry;
670 INIT_LIST_HEAD(&ipo->list);
671 ipo->offset = io_u->offset;
672 ipo->len = io_u->buflen;
675 * for random io where the writes extend the file, it will typically
676 * be laid out with the block scattered as written. it's faster to
677 * read them in in that order again, so don't sort
679 if (td->sequential || !td->overwrite) {
680 list_add_tail(&ipo->list, &td->io_hist_list);
685 * for random io, sort the list so verify will run faster
687 entry = &td->io_hist_list;
688 while ((entry = entry->prev) != &td->io_hist_list) {
689 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
691 if (__ipo->offset < ipo->offset)
695 list_add(&ipo->list, entry);
698 static void do_sync_verify(struct thread_data *td)
701 struct io_u *io_u = NULL;
704 td_set_runstate(td, TD_VERIFYING);
706 io_u = __get_io_u(td);
710 if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
711 td_verror(td, errno);
715 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
716 td_verror(td, errno);
726 gettimeofday(&t, NULL);
727 if (runtime_exceeded(td, &t))
730 if (get_next_verify(td, &io_u->offset, &io_u->buflen))
733 if (td->cur_off != io_u->offset) {
734 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
735 td_verror(td, errno);
740 ret = read(td->fd, io_u->buf, io_u->buflen);
741 if (ret < (int) io_u->buflen) {
743 td_verror(td, errno);
751 if (verify_io_u(io_u))
754 td->cur_off = io_u->offset + io_u->buflen;
758 td_set_runstate(td, TD_RUNNING);
762 static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u)
764 unsigned long long real_off = io_u->offset - td->file_offset;
767 memcpy(io_u->buf, td->mmap + real_off, io_u->buflen);
769 memcpy(td->mmap + real_off, io_u->buf, io_u->buflen);
772 * not really direct, but should drop the pages from the cache
775 msync(td->mmap + real_off, io_u->buflen, MS_SYNC);
776 madvise(td->mmap + real_off, io_u->buflen, MADV_DONTNEED);
782 static int __do_sync_rw(struct thread_data *td, struct io_u *io_u)
784 if (td->cur_off != io_u->offset) {
785 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
786 td_verror(td, errno);
792 return read(td->fd, io_u->buf, io_u->buflen);
794 return write(td->fd, io_u->buf, io_u->buflen);
797 static void sync_td(struct thread_data *td)
802 msync(td->mmap, td->file_size, MS_SYNC);
805 static void do_sync_io(struct thread_data *td)
807 unsigned long msec, usec;
808 struct io_u *io_u = NULL;
811 while (td->this_io_bytes < td->io_size) {
822 ret = __do_sync_rw(td, io_u);
824 ret = __do_sync_mmap(td, io_u);
826 if (ret < (int) io_u->buflen) {
828 td_verror(td, errno);
833 log_io_piece(td, io_u);
836 td->io_bytes += io_u->buflen;
837 td->this_io_bytes += io_u->buflen;
838 td->cur_off = io_u->offset + io_u->buflen;
840 gettimeofday(&e, NULL);
842 usec = utime_since(&io_u->start_time, &e);
844 rate_throttle(td, usec, io_u->buflen);
846 if (check_min_rate(td, &e)) {
847 td_verror(td, ENODATA);
852 add_clat_sample(td, msec);
855 if (runtime_exceeded(td, &e))
862 usec_sleep(td, td->thinktime);
864 if (should_fsync(td) && td->fsync_blocks &&
865 (td->io_blocks % td->fsync_blocks) == 0)
872 if (should_fsync(td))
876 static int io_u_getevents(struct thread_data *td, int min, int max,
882 r = io_getevents(td->aio_ctx, min, max, td->aio_events, t);
883 if (r != -EAGAIN && r != -EINTR)
890 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
892 struct iocb *iocb = &io_u->iocb;
896 ret = io_submit(td->aio_ctx, 1, &iocb);
899 else if (ret == -EAGAIN)
901 else if (ret == -EINTR)
910 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
911 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
913 static int ios_completed(struct thread_data *td, int nr)
920 gettimeofday(&e, NULL);
922 for (i = 0, bytes_done = 0; i < nr; i++) {
923 io_u = ev_to_iou(td->aio_events + i);
926 td->io_bytes += io_u->buflen;
927 td->this_io_bytes += io_u->buflen;
929 msec = mtime_since(&io_u->issue_time, &e);
931 add_clat_sample(td, msec);
935 log_io_piece(td, io_u);
937 bytes_done += io_u->buflen;
944 static void cleanup_pending_aio(struct thread_data *td)
946 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
947 struct list_head *entry, *n;
952 * get immediately available events, if any
954 r = io_u_getevents(td, 0, td->cur_depth, &ts);
956 ios_completed(td, r);
959 * now cancel remaining active events
961 list_for_each_safe(entry, n, &td->io_u_busylist) {
962 io_u = list_entry(entry, struct io_u, list);
964 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
970 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
972 ios_completed(td, r);
976 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
978 struct io_u *v_io_u = *io_u;
982 ret = verify_io_u(v_io_u);
983 put_io_u(td, v_io_u);
990 static void do_async_verify(struct thread_data *td)
993 struct io_u *io_u, *v_io_u = NULL;
996 td_set_runstate(td, TD_VERIFYING);
1002 gettimeofday(&t, NULL);
1003 if (runtime_exceeded(td, &t))
1006 io_u = __get_io_u(td);
1010 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
1015 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
1016 ret = io_u_queue(td, io_u);
1024 * we have one pending to verify, do that while the next
1025 * we are doing io on the next one
1027 if (async_do_verify(td, &v_io_u))
1030 ret = io_u_getevents(td, 1, 1, NULL);
1037 v_io_u = ev_to_iou(td->aio_events);
1039 td->cur_off = v_io_u->offset + v_io_u->buflen;
1042 * if we can't submit more io, we need to verify now
1044 if (queue_full(td) && async_do_verify(td, &v_io_u))
1049 async_do_verify(td, &v_io_u);
1052 cleanup_pending_aio(td);
1054 td_set_runstate(td, TD_RUNNING);
1057 static void do_async_io(struct thread_data *td)
1059 struct timeval s, e;
1062 while (td->this_io_bytes < td->io_size) {
1063 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1064 struct timespec *timeout;
1065 int ret, min_evts = 0;
1067 unsigned int bytes_done;
1072 io_u = get_io_u(td);
1076 memcpy(&s, &io_u->start_time, sizeof(s));
1078 ret = io_u_queue(td, io_u);
1085 gettimeofday(&io_u->issue_time, NULL);
1086 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1087 if (td->cur_depth < td->aio_depth) {
1095 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1102 bytes_done = ios_completed(td, ret);
1105 * the rate is batched for now, it should work for batches
1106 * of completions except the very first one which may look
1109 gettimeofday(&e, NULL);
1110 usec = utime_since(&s, &e);
1112 rate_throttle(td, usec, bytes_done);
1114 if (check_min_rate(td, &e)) {
1115 td_verror(td, ENODATA);
1119 if (runtime_exceeded(td, &e))
1123 usec_sleep(td, td->thinktime);
1125 if (should_fsync(td) && td->fsync_blocks &&
1126 (td->io_blocks % td->fsync_blocks) == 0)
1131 cleanup_pending_aio(td);
1133 if (should_fsync(td))
1137 static void cleanup_aio(struct thread_data *td)
1139 io_destroy(td->aio_ctx);
1142 free(td->aio_events);
1145 static int init_aio(struct thread_data *td)
1147 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
1148 td_verror(td, errno);
1152 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
1156 static void cleanup_io_u(struct thread_data *td)
1158 struct list_head *entry, *n;
1161 list_for_each_safe(entry, n, &td->io_u_freelist) {
1162 io_u = list_entry(entry, struct io_u, list);
1164 list_del(&io_u->list);
1168 if (td->mem_type == MEM_MALLOC)
1169 free(td->orig_buffer);
1170 else if (td->mem_type == MEM_SHM) {
1171 struct shmid_ds sbuf;
1173 shmdt(td->orig_buffer);
1174 shmctl(td->shm_id, IPC_RMID, &sbuf);
1175 } else if (td->mem_type == MEM_MMAP)
1176 munmap(td->orig_buffer, td->orig_buffer_size);
1178 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1180 td->orig_buffer = NULL;
1183 static int init_io_u(struct thread_data *td)
1192 max_units = td->aio_depth;
1194 td->orig_buffer_size = td->max_bs * max_units + MASK;
1196 if (td->mem_type == MEM_MALLOC)
1197 td->orig_buffer = malloc(td->orig_buffer_size);
1198 else if (td->mem_type == MEM_SHM) {
1199 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1200 if (td->shm_id < 0) {
1201 td_verror(td, errno);
1206 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1207 if (td->orig_buffer == (void *) -1) {
1208 td_verror(td, errno);
1210 td->orig_buffer = NULL;
1213 } else if (td->mem_type == MEM_MMAP) {
1214 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
1215 if (td->orig_buffer == MAP_FAILED) {
1216 td_verror(td, errno);
1218 td->orig_buffer = NULL;
1223 INIT_LIST_HEAD(&td->io_u_freelist);
1224 INIT_LIST_HEAD(&td->io_u_busylist);
1225 INIT_LIST_HEAD(&td->io_hist_list);
1227 p = ALIGN(td->orig_buffer);
1228 for (i = 0; i < max_units; i++) {
1229 io_u = malloc(sizeof(*io_u));
1230 memset(io_u, 0, sizeof(*io_u));
1231 INIT_LIST_HEAD(&io_u->list);
1233 io_u->buf = p + td->max_bs * i;
1234 list_add(&io_u->list, &td->io_u_freelist);
1240 static int create_file(struct thread_data *td, unsigned long long size,
1243 unsigned long long left;
1249 * unless specifically asked for overwrite, let normal io extend it
1251 if (td_write(td) && !td->overwrite)
1255 fprintf(stderr, "Need size for create\n");
1256 td_verror(td, EINVAL);
1261 oflags = O_CREAT | O_TRUNC;
1262 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1265 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1268 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1270 td_verror(td, errno);
1274 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1275 td_verror(td, errno);
1279 td->io_size = td->file_size;
1280 b = malloc(td->max_bs);
1281 memset(b, 0, td->max_bs);
1284 while (left && !td->terminate) {
1289 r = write(td->fd, b, bs);
1291 if (r == (int) bs) {
1296 td_verror(td, errno);
1305 unlink(td->file_name);
1306 else if (td->create_fsync)
1315 static int file_size(struct thread_data *td)
1319 if (fstat(td->fd, &st) == -1) {
1320 td_verror(td, errno);
1325 td->file_size = st.st_size;
1330 static int bdev_size(struct thread_data *td)
1334 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1335 td_verror(td, errno);
1340 * no extend possibilities, so limit size to device size if too large
1342 if (!td->file_size || td->file_size > bytes)
1343 td->file_size = bytes;
1348 static int get_file_size(struct thread_data *td)
1352 if (td->filetype == FIO_TYPE_FILE)
1353 ret = file_size(td);
1355 ret = bdev_size(td);
1360 if (td->file_offset > td->file_size) {
1361 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1365 td->io_size = td->file_size - td->file_offset;
1366 if (td->io_size == 0) {
1367 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1368 td_verror(td, EINVAL);
1375 static int setup_file_mmap(struct thread_data *td)
1384 if (td->verify != VERIFY_NONE)
1388 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1389 if (td->mmap == MAP_FAILED) {
1391 td_verror(td, errno);
1395 if (td->invalidate_cache) {
1396 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1397 td_verror(td, errno);
1402 if (td->sequential) {
1403 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1404 td_verror(td, errno);
1408 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1409 td_verror(td, errno);
1417 static int setup_file_plain(struct thread_data *td)
1419 if (td->invalidate_cache) {
1420 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1421 td_verror(td, errno);
1426 if (td->sequential) {
1427 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1428 td_verror(td, errno);
1432 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1433 td_verror(td, errno);
1441 static int setup_file(struct thread_data *td)
1446 if (stat(td->file_name, &st) == -1) {
1447 if (errno != ENOENT) {
1448 td_verror(td, errno);
1451 if (!td->create_file) {
1452 td_verror(td, ENOENT);
1455 if (create_file(td, td->file_size, 0))
1457 } else if (td->filetype == FIO_TYPE_FILE) {
1458 if (st.st_size < td->file_size) {
1459 if (create_file(td, td->file_size - st.st_size, 1))
1468 td->fd = open(td->file_name, flags | O_RDONLY);
1470 if (td->filetype == FIO_TYPE_FILE) {
1481 td->fd = open(td->file_name, flags, 0600);
1485 td_verror(td, errno);
1489 if (get_file_size(td))
1493 return setup_file_plain(td);
1495 return setup_file_mmap(td);
1498 static int check_dev_match(dev_t dev, char *path)
1500 unsigned int major, minor;
1504 f = fopen(path, "r");
1506 perror("open path");
1510 p = fgets(line, sizeof(line), f);
1516 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1521 if (((major << 8) | minor) == dev) {
1530 static char *find_block_dir(dev_t dev, char *path)
1541 while ((dir = readdir(D)) != NULL) {
1542 char full_path[256];
1544 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1546 if (!strcmp(dir->d_name, "device"))
1549 sprintf(full_path, "%s/%s", path, dir->d_name);
1551 if (!strcmp(dir->d_name, "dev")) {
1552 if (!check_dev_match(dev, full_path)) {
1558 if (stat(full_path, &st) == -1) {
1563 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1566 if ((found = find_block_dir(dev, full_path)) != NULL)
1574 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1581 f = fopen(du->path, "r");
1585 p = fgets(line, sizeof(line), f);
1591 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) {
1600 static void update_io_tick_disk(struct disk_util *du)
1602 struct disk_util_stat __dus, *dus, *ldus;
1605 if (get_io_ticks(du, &__dus))
1609 ldus = &du->last_dus;
1611 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1612 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1613 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1614 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1615 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1616 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1617 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1618 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1619 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1620 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1622 gettimeofday(&t, NULL);
1623 du->msec += mtime_since(&du->time, &t);
1624 memcpy(&du->time, &t, sizeof(t));
1625 memcpy(ldus, &__dus, sizeof(__dus));
1628 static void update_io_ticks(void)
1630 struct list_head *entry;
1631 struct disk_util *du;
1633 list_for_each(entry, &disk_list) {
1634 du = list_entry(entry, struct disk_util, list);
1635 update_io_tick_disk(du);
1639 static int disk_util_exists(dev_t dev)
1641 struct list_head *entry;
1642 struct disk_util *du;
1644 list_for_each(entry, &disk_list) {
1645 du = list_entry(entry, struct disk_util, list);
1654 static void disk_util_add(dev_t dev, char *path)
1656 struct disk_util *du = malloc(sizeof(*du));
1658 memset(du, 0, sizeof(*du));
1659 INIT_LIST_HEAD(&du->list);
1660 sprintf(du->path, "%s/stat", path);
1661 du->name = strdup(basename(path));
1664 gettimeofday(&du->time, NULL);
1665 get_io_ticks(du, &du->last_dus);
1667 list_add_tail(&du->list, &disk_list);
1670 static void init_disk_util(struct thread_data *td)
1673 char foo[256], tmp[256];
1677 if (!stat(td->file_name, &st)) {
1678 if (S_ISBLK(st.st_mode))
1684 * must be a file, open "." in that path
1686 strcpy(foo, td->file_name);
1689 perror("disk util stat");
1696 if (disk_util_exists(dev))
1699 sprintf(foo, "/sys/block");
1700 dir = find_block_dir(dev, foo);
1705 * if this is inside a partition dir, jump back to parent
1707 sprintf(tmp, "%s/queue", dir);
1708 if (stat(tmp, &st)) {
1710 sprintf(tmp, "%s/queue", p);
1711 if (stat(tmp, &st)) {
1712 fprintf(stderr, "unknown sysfs layout\n");
1715 sprintf(foo, "%s", p);
1718 disk_util_add(dev, foo);
1721 static void disk_util_timer_arm(void)
1723 struct itimerval itimer;
1725 itimer.it_value.tv_sec = 0;
1726 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1727 setitimer(ITIMER_REAL, &itimer, NULL);
1730 static void clear_io_state(struct thread_data *td)
1733 lseek(td->fd, SEEK_SET, 0);
1737 td->stat_io_bytes = 0;
1738 td->this_io_bytes = 0;
1741 memset(td->file_map, 0, td->num_maps * sizeof(long));
1744 static void update_rusage_stat(struct thread_data *td)
1749 getrusage(RUSAGE_SELF, &td->ru_end);
1751 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1752 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1753 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1756 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1759 static void *thread_main(void *data)
1761 struct thread_data *td = data;
1770 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1771 td_verror(td, errno);
1775 if (td->use_aio && init_aio(td))
1779 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1780 td_verror(td, errno);
1785 sem_post(&startup_sem);
1786 sem_wait(&td->mutex);
1788 if (!td->create_serialize && setup_file(td))
1791 if (init_random_state(td))
1794 while (td->loops--) {
1795 getrusage(RUSAGE_SELF, &td->ru_start);
1796 gettimeofday(&td->start, NULL);
1797 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1800 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1803 prune_io_piece_log(td);
1813 td->runtime += mtime_since_now(&td->start);
1814 update_rusage_stat(td);
1816 if (td->verify == VERIFY_NONE)
1824 do_async_verify(td);
1833 finish_log(td, td->bw_log, "bw");
1835 finish_log(td, td->lat_log, "lat");
1837 if (exitall_on_terminate)
1838 terminate_threads(td->groupid);
1846 munmap(td->mmap, td->file_size);
1851 sem_post(&startup_sem);
1852 sem_wait(&td->mutex);
1854 td_set_runstate(td, TD_EXITED);
1859 static void *fork_main(int shmid, int offset)
1861 struct thread_data *td;
1864 data = shmat(shmid, NULL, 0);
1865 if (data == (void *) -1) {
1870 td = data + offset * sizeof(struct thread_data);
1876 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1877 double *mean, double *dev)
1881 if (is->samples == 0)
1887 n = (double) is->samples;
1888 *mean = (double) is->val / n;
1889 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1893 static void show_thread_status(struct thread_data *td,
1894 struct group_run_stats *rs)
1896 int prio, prio_class;
1897 unsigned long min, max, bw = 0;
1898 double mean, dev, usr_cpu, sys_cpu;
1900 if (!td->io_bytes && !td->error)
1904 bw = td->io_bytes / td->runtime;
1906 prio = td->ioprio & 0xff;
1907 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1909 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);
1911 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1912 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1913 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1914 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1915 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1918 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1919 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);
1923 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1924 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1930 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1933 static void print_thread_status(int nr_running, int t_rate, int m_rate)
1935 printf("Threads now running: %d", nr_running);
1936 if (m_rate || t_rate)
1937 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1938 printf(" : [%s]\r", run_str);
1942 static void check_str_update(struct thread_data *td, int n, int t, int m)
1944 char c = run_str[td->thread_number - 1];
1946 if (td->runstate == td->old_runstate)
1949 switch (td->runstate) {
1975 case TD_NOT_CREATED:
1979 printf("state %d\n", td->runstate);
1982 run_str[td->thread_number - 1] = c;
1983 print_thread_status(n, t, m);
1984 td->old_runstate = td->runstate;
1987 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1992 * reap exited threads (TD_EXITED -> TD_REAPED)
1994 for (i = 0; i < thread_number; i++) {
1995 struct thread_data *td = &threads[i];
1997 check_str_update(td, *nr_running, *t_rate, *m_rate);
1999 if (td->runstate != TD_EXITED)
2002 td_set_runstate(td, TD_REAPED);
2004 if (td->use_thread) {
2007 if (pthread_join(td->thread, (void *) &ret))
2008 perror("thread_join");
2010 waitpid(td->pid, NULL, 0);
2013 (*m_rate) -= td->ratemin;
2014 (*t_rate) -= td->rate;
2015 check_str_update(td, *nr_running, *t_rate, *m_rate);
2019 static void run_threads(void)
2021 struct timeval genesis;
2022 struct thread_data *td;
2023 unsigned long spent;
2024 int i, todo, nr_running, m_rate, t_rate, nr_started;
2026 printf("Starting %d threads\n", thread_number);
2029 signal(SIGINT, sig_handler);
2030 signal(SIGALRM, sig_handler);
2032 todo = thread_number;
2035 m_rate = t_rate = 0;
2037 for (i = 0; i < thread_number; i++) {
2042 if (!td->create_serialize)
2046 * do file setup here so it happens sequentially,
2047 * we don't want X number of threads getting their
2048 * client data interspersed on disk
2050 if (setup_file(td)) {
2051 td_set_runstate(td, TD_REAPED);
2056 gettimeofday(&genesis, NULL);
2060 * create threads (TD_NOT_CREATED -> TD_CREATED)
2062 for (i = 0; i < thread_number; i++) {
2065 if (td->runstate != TD_NOT_CREATED)
2069 * never got a chance to start, killed by other
2070 * thread for some reason
2072 if (td->terminate) {
2077 if (td->start_delay) {
2078 spent = mtime_since_now(&genesis);
2080 if (td->start_delay * 1000 > spent)
2084 if (td->stonewall && (nr_started || nr_running))
2087 td_set_runstate(td, TD_CREATED);
2088 check_str_update(td, nr_running, t_rate, m_rate);
2089 sem_init(&startup_sem, 1, 1);
2093 if (td->use_thread) {
2094 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2095 perror("thread_create");
2100 sem_wait(&startup_sem);
2102 fork_main(shm_id, i);
2109 * start created threads (TD_CREATED -> TD_RUNNING)
2111 for (i = 0; i < thread_number; i++) {
2114 if (td->runstate != TD_CREATED)
2117 td_set_runstate(td, TD_RUNNING);
2120 m_rate += td->ratemin;
2122 check_str_update(td, nr_running, t_rate, m_rate);
2123 sem_post(&td->mutex);
2126 for (i = 0; i < thread_number; i++) {
2129 if (td->runstate != TD_RUNNING &&
2130 td->runstate != TD_VERIFYING)
2133 check_str_update(td, nr_running, t_rate, m_rate);
2136 reap_threads(&nr_running, &t_rate, &m_rate);
2142 while (nr_running) {
2143 reap_threads(&nr_running, &t_rate, &m_rate);
2150 static void show_group_stats(struct group_run_stats *rs, int id)
2152 printf("\nRun status group %d:\n", id);
2154 if (rs->max_run[DDIR_READ])
2155 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]);
2156 if (rs->max_run[DDIR_WRITE])
2157 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]);
2160 static void show_disk_util(void)
2162 struct disk_util_stat *dus;
2163 struct list_head *entry;
2164 struct disk_util *du;
2167 printf("\nDisk stats (read/write):\n");
2169 list_for_each(entry, &disk_list) {
2170 du = list_entry(entry, struct disk_util, list);
2173 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2177 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);
2181 static void show_run_stats(void)
2183 struct group_run_stats *runstats, *rs;
2184 struct thread_data *td;
2187 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2189 for (i = 0; i < groupid + 1; i++) {
2192 memset(rs, 0, sizeof(*rs));
2193 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2194 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2197 for (i = 0; i < thread_number; i++) {
2198 unsigned long bw = 0;
2203 printf("Client%d: %s\n", td->thread_number, td->verror);
2207 rs = &runstats[td->groupid];
2209 if (td->runtime < rs->min_run[td->ddir])
2210 rs->min_run[td->ddir] = td->runtime;
2211 if (td->runtime > rs->max_run[td->ddir])
2212 rs->max_run[td->ddir] = td->runtime;
2215 bw = td->io_bytes / td->runtime;
2216 if (bw < rs->min_bw[td->ddir])
2217 rs->min_bw[td->ddir] = bw;
2218 if (bw > rs->max_bw[td->ddir])
2219 rs->max_bw[td->ddir] = bw;
2221 rs->io_mb[td->ddir] += td->io_bytes >> 20;
2224 for (i = 0; i < groupid + 1; i++) {
2228 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2230 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2233 for (i = 0; i < thread_number; i++) {
2235 rs = &runstats[td->groupid];
2237 show_thread_status(td, rs);
2240 for (i = 0; i < groupid + 1; i++)
2241 show_group_stats(&runstats[i], i);
2246 int main(int argc, char *argv[])
2248 memset(run_str, 0, sizeof(run_str));
2250 if (parse_options(argc, argv))
2253 if (!thread_number) {
2254 printf("Nothing to do\n");
2258 disk_util_timer_arm();