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 groupid)
95 for (i = 0; i < thread_number; i++) {
96 struct thread_data *td = &threads[i];
98 if (groupid == TERMINATE_ALL || groupid == td->groupid) {
105 static void sig_handler(int sig)
110 disk_util_timer_arm();
113 printf("fio: terminating on signal\n");
114 terminate_threads(TERMINATE_ALL);
119 static unsigned long utime_since(struct timeval *s, struct timeval *e)
123 sec = e->tv_sec - s->tv_sec;
124 usec = e->tv_usec - s->tv_usec;
125 if (sec > 0 && usec < 0) {
130 sec *= (double) 1000000;
135 static unsigned long utime_since_now(struct timeval *s)
139 gettimeofday(&t, NULL);
140 return utime_since(s, &t);
143 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
147 sec = e->tv_sec - s->tv_sec;
148 usec = e->tv_usec - s->tv_usec;
149 if (sec > 0 && usec < 0) {
154 sec *= (double) 1000;
155 usec /= (double) 1000;
160 static unsigned long mtime_since_now(struct timeval *s)
164 gettimeofday(&t, NULL);
165 return mtime_since(s, &t);
168 static inline unsigned long msec_now(struct timeval *s)
170 return s->tv_sec * 1000 + s->tv_usec / 1000;
173 static int random_map_free(struct thread_data *td, unsigned long long block)
175 unsigned int idx = RAND_MAP_IDX(td, block);
176 unsigned int bit = RAND_MAP_BIT(td, block);
178 return (td->file_map[idx] & (1UL << bit)) == 0;
181 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
187 while ((*b) * td->min_bs < td->io_size) {
188 if (td->file_map[i] != -1UL) {
189 *b += ffz(td->file_map[i]);
193 *b += BLOCKS_PER_MAP;
200 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
202 unsigned long block = io_u->offset / td->min_bs;
203 unsigned int blocks = 0;
205 while (blocks < (io_u->buflen / td->min_bs)) {
208 if (!random_map_free(td, block))
211 idx = RAND_MAP_IDX(td, block);
212 bit = RAND_MAP_BIT(td, block);
214 assert(idx < td->num_maps);
216 td->file_map[idx] |= (1UL << bit);
221 if ((blocks * td->min_bs) < io_u->buflen)
222 io_u->buflen = blocks * td->min_bs;
225 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
227 unsigned long long b, rb;
230 if (!td->sequential) {
231 unsigned long max_blocks = td->io_size / td->min_bs;
235 lrand48_r(&td->random_state, &r);
236 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
237 rb = b + (td->file_offset / td->min_bs);
239 } while (!random_map_free(td, rb) && loops);
242 if (get_next_free_block(td, &b))
246 b = td->last_bytes / td->min_bs;
248 *offset = (b * td->min_bs) + td->file_offset;
249 if (*offset > td->file_size)
255 static unsigned int get_next_buflen(struct thread_data *td)
260 if (td->min_bs == td->max_bs)
263 lrand48_r(&td->bsrange_state, &r);
264 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
265 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
268 if (buflen > td->io_size - td->this_io_bytes)
269 buflen = td->io_size - td->this_io_bytes;
274 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
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 *log,
290 if (log->nr_samples == log->max_samples) {
291 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
293 log->log = realloc(log->log, new_size);
294 log->max_samples <<= 1;
297 log->log[log->nr_samples].val = val;
298 log->log[log->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, &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, &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, &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(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) {
715 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
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) {
740 ret = read(td->fd, io_u->buf, io_u->buflen);
741 if (ret < (int) io_u->buflen) {
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) {
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) {
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)) {
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->error = 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)) {
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) {
1206 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1207 if (td->orig_buffer == (void *) -1) {
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) {
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)
1242 unsigned long long left;
1248 * unless specifically asked for overwrite, let normal io extend it
1250 if (td_write(td) && !td->overwrite)
1253 if (!td->file_size) {
1254 fprintf(stderr, "Need size for create\n");
1259 printf("Client%d: Laying out IO file\n", td->thread_number);
1261 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1267 if (ftruncate(td->fd, td->file_size) == -1) {
1272 td->io_size = td->file_size;
1273 b = malloc(td->max_bs);
1274 memset(b, 0, td->max_bs);
1276 left = td->file_size;
1282 r = write(td->fd, b, bs);
1284 if (r == (int) bs) {
1297 if (td->create_fsync)
1306 static int file_exists(struct thread_data *td)
1310 if (stat(td->file_name, &st) != -1)
1313 return errno != ENOENT;
1316 static int file_size(struct thread_data *td)
1320 if (fstat(td->fd, &st) == -1) {
1326 if (!td->file_size || (st.st_size > td->file_size))
1327 td->file_size = st.st_size;
1330 td->file_size = 1024 * 1024 * 1024;
1336 static int bdev_size(struct thread_data *td)
1340 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1345 if (!td->file_size || (bytes > td->file_size))
1346 td->file_size = bytes;
1351 static int get_file_size(struct thread_data *td)
1355 if (td->filetype == FIO_TYPE_FILE)
1356 ret = file_size(td);
1358 ret = bdev_size(td);
1363 if (td->file_offset > td->file_size) {
1364 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1368 td->io_size = td->file_size - td->file_offset;
1369 if (td->io_size == 0) {
1370 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1378 static int setup_file_mmap(struct thread_data *td)
1387 if (td->verify != VERIFY_NONE)
1391 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1392 if (td->mmap == MAP_FAILED) {
1398 if (td->invalidate_cache) {
1399 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1405 if (td->sequential) {
1406 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1411 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1420 static int setup_file_plain(struct thread_data *td)
1422 if (td->invalidate_cache) {
1423 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1429 if (td->sequential) {
1430 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1435 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1444 static int setup_file(struct thread_data *td)
1448 if (!file_exists(td)) {
1449 if (!td->create_file) {
1453 if (create_file(td))
1461 td->fd = open(td->file_name, flags | O_RDONLY);
1470 td->fd = open(td->file_name, flags | O_CREAT, 0600);
1478 if (get_file_size(td))
1481 if (td_write(td) && ftruncate(td->fd, td->file_size) == -1) {
1487 return setup_file_plain(td);
1489 return setup_file_mmap(td);
1492 static int check_dev_match(dev_t dev, char *path)
1498 f = fopen(path, "r");
1500 perror("open path");
1504 p = fgets(line, sizeof(line), f);
1510 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1515 if (((major << 8) | minor) == dev) {
1524 static char *find_block_dir(dev_t dev, char *path)
1535 while ((dir = readdir(D)) != NULL) {
1536 char full_path[256];
1538 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1540 if (!strcmp(dir->d_name, "device"))
1543 sprintf(full_path, "%s/%s", path, dir->d_name);
1545 if (!strcmp(dir->d_name, "dev")) {
1546 if (!check_dev_match(dev, full_path)) {
1552 if (stat(full_path, &st) == -1) {
1557 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1560 if ((found = find_block_dir(dev, full_path)) != NULL)
1568 static int get_io_ticks(struct disk_util *du)
1570 int i1, i2, i3, i4, i5, i6, i7, i8, i9;
1571 unsigned long long ull1, ull2;
1576 f = fopen(du->path, "r");
1580 p = fgets(line, sizeof(line), f);
1586 if (sscanf(p, "%8u %8u %8llu %8u %8u %8u %8llu %8u %8u %8u %8u", &i1, &i2, &ull1, &i3, &i4, &i5, &ull2, &i6, &i7, &i8, &i9) != 11) {
1595 static void update_io_tick_disk(struct disk_util *du)
1597 unsigned long ticks = get_io_ticks(du);
1600 du->io_ticks += (ticks - du->last_io_ticks);
1601 du->last_io_ticks = ticks;
1602 gettimeofday(&t, NULL);
1603 du->msec += mtime_since(&du->time, &t);
1604 memcpy(&du->time, &t, sizeof(t));
1607 static void update_io_ticks(void)
1609 struct list_head *entry;
1610 struct disk_util *du;
1612 list_for_each(entry, &disk_list) {
1613 du = list_entry(entry, struct disk_util, list);
1614 update_io_tick_disk(du);
1618 static int disk_util_exists(dev_t dev)
1620 struct list_head *entry;
1621 struct disk_util *du;
1623 list_for_each(entry, &disk_list) {
1624 du = list_entry(entry, struct disk_util, list);
1633 static void disk_util_add(dev_t dev, char *path)
1635 struct disk_util *du = malloc(sizeof(*du));
1637 memset(du, 0, sizeof(*du));
1638 INIT_LIST_HEAD(&du->list);
1639 sprintf(du->path, "%s/stat", path);
1640 du->name = basename(path);
1643 gettimeofday(&du->time, NULL);
1644 du->last_io_ticks = get_io_ticks(du);
1646 list_add_tail(&du->list, &disk_list);
1649 static void init_disk_util(struct thread_data *td)
1652 char foo[256], tmp[256];
1656 if (!stat(td->file_name, &st)) {
1657 if (S_ISBLK(st.st_mode))
1663 * must be a file, open "." in that path
1665 strcpy(foo, td->file_name);
1668 perror("disk util stat");
1675 if (disk_util_exists(dev))
1678 sprintf(foo, "/sys/block");
1679 dir = find_block_dir(dev, foo);
1684 * if this is inside a partition dir, jump back to parent
1686 sprintf(tmp, "%s/queue", dir);
1687 if (stat(tmp, &st)) {
1689 sprintf(tmp, "%s/queue", p);
1690 if (stat(tmp, &st)) {
1691 fprintf(stderr, "unknown sysfs layout\n");
1694 sprintf(foo, "%s", p);
1697 disk_util_add(dev, foo);
1700 static void disk_util_timer_arm(void)
1702 struct itimerval itimer;
1704 itimer.it_value.tv_sec = 0;
1705 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1706 setitimer(ITIMER_REAL, &itimer, NULL);
1709 static void clear_io_state(struct thread_data *td)
1712 lseek(td->fd, SEEK_SET, 0);
1716 td->stat_io_bytes = 0;
1717 td->this_io_bytes = 0;
1720 memset(td->file_map, 0, td->num_maps * sizeof(long));
1723 static void update_rusage_stat(struct thread_data *td)
1728 getrusage(RUSAGE_SELF, &td->ru_end);
1730 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1731 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1732 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1735 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1738 static void *thread_main(void *data)
1740 struct thread_data *td = data;
1749 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1754 if (td->use_aio && init_aio(td))
1758 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1764 sem_post(&startup_sem);
1765 sem_wait(&td->mutex);
1767 if (!td->create_serialize && setup_file(td))
1770 if (init_random_state(td))
1773 while (td->loops--) {
1774 getrusage(RUSAGE_SELF, &td->ru_start);
1775 gettimeofday(&td->start, NULL);
1776 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1779 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1782 prune_io_piece_log(td);
1792 td->runtime += mtime_since_now(&td->start);
1793 update_rusage_stat(td);
1795 if (td->verify == VERIFY_NONE)
1803 do_async_verify(td);
1812 finish_log(td, td->bw_log, "bw");
1814 finish_log(td, td->lat_log, "lat");
1816 if (exitall_on_terminate)
1817 terminate_threads(td->groupid);
1825 munmap(td->mmap, td->file_size);
1830 sem_post(&startup_sem);
1831 sem_wait(&td->mutex);
1833 td_set_runstate(td, TD_EXITED);
1838 static void *fork_main(int shm_id, int offset)
1840 struct thread_data *td;
1843 data = shmat(shm_id, NULL, 0);
1844 if (data == (void *) -1) {
1849 td = data + offset * sizeof(struct thread_data);
1855 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1856 double *mean, double *dev)
1860 if (is->samples == 0)
1866 n = (double) is->samples;
1867 *mean = (double) is->val / n;
1868 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1872 static void show_thread_status(struct thread_data *td,
1873 struct group_run_stats *rs)
1875 int prio, prio_class;
1876 unsigned long min, max, bw = 0;
1877 double mean, dev, usr_cpu, sys_cpu;
1879 if (!td->io_bytes && !td->error)
1883 bw = td->io_bytes / td->runtime;
1885 prio = td->ioprio & 0xff;
1886 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1888 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);
1890 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1891 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1892 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1893 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1894 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1897 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1898 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);
1902 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1903 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1909 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1912 static void print_thread_status(struct thread_data *td, int nr_running,
1913 int t_rate, int m_rate)
1915 printf("Threads now running: %d", nr_running);
1916 if (m_rate || t_rate)
1917 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1918 printf(" : [%s]\r", run_str);
1922 static void check_str_update(struct thread_data *td, int n, int t, int m)
1924 char c = run_str[td->thread_number - 1];
1926 if (td->runstate == td->old_runstate)
1929 switch (td->runstate) {
1955 case TD_NOT_CREATED:
1959 printf("state %d\n", td->runstate);
1962 run_str[td->thread_number - 1] = c;
1963 print_thread_status(td, n, t, m);
1964 td->old_runstate = td->runstate;
1967 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1972 * reap exited threads (TD_EXITED -> TD_REAPED)
1974 for (i = 0; i < thread_number; i++) {
1975 struct thread_data *td = &threads[i];
1977 check_str_update(td, *nr_running, *t_rate, *m_rate);
1979 if (td->runstate != TD_EXITED)
1982 td_set_runstate(td, TD_REAPED);
1984 if (td->use_thread) {
1987 if (pthread_join(td->thread, (void *) &ret))
1988 perror("thread_join");
1990 waitpid(td->pid, NULL, 0);
1993 (*m_rate) -= td->ratemin;
1994 (*t_rate) -= td->rate;
1995 check_str_update(td, *nr_running, *t_rate, *m_rate);
1999 static void run_threads(char *argv[])
2001 struct timeval genesis;
2002 struct thread_data *td;
2003 unsigned long spent;
2004 int i, todo, nr_running, m_rate, t_rate, nr_started;
2006 printf("Starting %d threads\n", thread_number);
2009 signal(SIGINT, sig_handler);
2010 signal(SIGALRM, sig_handler);
2012 todo = thread_number;
2015 m_rate = t_rate = 0;
2017 for (i = 0; i < thread_number; i++) {
2022 if (!td->create_serialize)
2026 * do file setup here so it happens sequentially,
2027 * we don't want X number of threads getting their
2028 * client data interspersed on disk
2030 if (setup_file(td)) {
2031 td_set_runstate(td, TD_REAPED);
2036 gettimeofday(&genesis, NULL);
2040 * create threads (TD_NOT_CREATED -> TD_CREATED)
2042 for (i = 0; i < thread_number; i++) {
2045 if (td->runstate != TD_NOT_CREATED)
2049 * never got a chance to start, killed by other
2050 * thread for some reason
2052 if (td->terminate) {
2057 if (td->start_delay) {
2058 spent = mtime_since_now(&genesis);
2060 if (td->start_delay * 1000 > spent)
2064 if (td->stonewall && (nr_started || nr_running))
2067 td_set_runstate(td, TD_CREATED);
2068 check_str_update(td, nr_running, t_rate, m_rate);
2069 sem_init(&startup_sem, 1, 1);
2073 if (td->use_thread) {
2074 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2075 perror("thread_create");
2080 sem_wait(&startup_sem);
2082 fork_main(shm_id, i);
2089 * start created threads (TD_CREATED -> TD_RUNNING)
2091 for (i = 0; i < thread_number; i++) {
2092 struct thread_data *td = &threads[i];
2094 if (td->runstate != TD_CREATED)
2097 td_set_runstate(td, TD_RUNNING);
2100 m_rate += td->ratemin;
2102 check_str_update(td, nr_running, t_rate, m_rate);
2103 sem_post(&td->mutex);
2106 for (i = 0; i < thread_number; i++) {
2107 struct thread_data *td = &threads[i];
2109 if (td->runstate != TD_RUNNING &&
2110 td->runstate != TD_VERIFYING)
2113 check_str_update(td, nr_running, t_rate, m_rate);
2116 reap_threads(&nr_running, &t_rate, &m_rate);
2122 while (nr_running) {
2123 reap_threads(&nr_running, &t_rate, &m_rate);
2130 static void show_group_stats(struct group_run_stats *rs, int id)
2132 printf("\nRun status group %d:\n", id);
2134 if (rs->max_run[DDIR_READ])
2135 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]);
2136 if (rs->max_run[DDIR_WRITE])
2137 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]);
2140 static void show_disk_util(void)
2142 struct list_head *entry;
2143 struct disk_util *du;
2146 printf("\nDisk utilization:\n");
2148 list_for_each(entry, &disk_list) {
2149 du = list_entry(entry, struct disk_util, list);
2151 util = (double) 100 * du->io_ticks / (double) du->msec;
2155 printf(" %s: %3.2f%%\n", du->name, util);
2159 static void show_run_stats(void)
2161 struct group_run_stats *runstats, *rs;
2162 struct thread_data *td;
2165 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2167 for (i = 0; i < groupid + 1; i++) {
2170 memset(rs, 0, sizeof(*rs));
2171 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2172 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2175 for (i = 0; i < thread_number; i++) {
2176 unsigned long bw = 0;
2183 rs = &runstats[td->groupid];
2185 if (td->runtime < rs->min_run[td->ddir])
2186 rs->min_run[td->ddir] = td->runtime;
2187 if (td->runtime > rs->max_run[td->ddir])
2188 rs->max_run[td->ddir] = td->runtime;
2191 bw = td->io_bytes / td->runtime;
2192 if (bw < rs->min_bw[td->ddir])
2193 rs->min_bw[td->ddir] = bw;
2194 if (bw > rs->max_bw[td->ddir])
2195 rs->max_bw[td->ddir] = bw;
2197 rs->io_mb[td->ddir] += td->io_bytes >> 20;
2200 for (i = 0; i < groupid + 1; i++) {
2204 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2206 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2209 for (i = 0; i < thread_number; i++) {
2211 rs = &runstats[td->groupid];
2213 show_thread_status(td, rs);
2216 for (i = 0; i < groupid + 1; i++)
2217 show_group_stats(&runstats[i], i);
2222 int main(int argc, char *argv[])
2224 memset(run_str, 0, sizeof(run_str));
2226 if (parse_options(argc, argv))
2229 if (!thread_number) {
2230 printf("Nothing to do\n");
2234 disk_util_timer_arm();