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
32 #include <sys/types.h>
37 #include <sys/ioctl.h>
39 #include <asm/unistd.h>
45 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
48 int thread_number = 0;
49 char run_str[MAX_JOBS + 1];
69 struct timeval start_time;
70 struct timeval issue_time;
74 unsigned long long offset;
76 struct list_head list;
79 #define should_fsync(td) (td_write(td) && !(td)->odirect)
81 static sem_t startup_sem;
83 #define TERMINATE_ALL (-1)
85 static void terminate_threads(int groupid)
89 for (i = 0; i < thread_number; i++) {
90 struct thread_data *td = &threads[i];
92 if (groupid == TERMINATE_ALL || groupid == td->groupid) {
99 static void sig_handler(int sig)
101 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)) {
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 thread_data *td, struct io_stat *is,
262 if (val > is->max_val)
264 if (val < is->min_val)
268 is->val_sq += val * val;
272 static void add_log_sample(struct thread_data *td, struct io_log *log,
275 if (log->nr_samples == log->max_samples) {
276 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
278 log->log = realloc(log->log, new_size);
279 log->max_samples <<= 1;
282 log->log[log->nr_samples].val = val;
283 log->log[log->nr_samples].time = mtime_since_now(&td->start);
287 static void add_clat_sample(struct thread_data *td, unsigned long msec)
289 add_stat_sample(td, &td->clat_stat, msec);
292 add_log_sample(td, td->lat_log, msec);
295 static void add_slat_sample(struct thread_data *td, unsigned long msec)
297 add_stat_sample(td, &td->slat_stat, msec);
300 static void add_bw_sample(struct thread_data *td)
302 unsigned long spent = mtime_since_now(&td->stat_sample_time);
305 if (spent < td->bw_avg_time)
308 rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
309 add_stat_sample(td, &td->bw_stat, rate);
312 add_log_sample(td, td->bw_log, rate);
314 gettimeofday(&td->stat_sample_time, NULL);
315 td->stat_io_bytes = td->this_io_bytes;
319 * busy looping version for the last few usec
321 static void __usec_sleep(int usec)
323 struct timeval start;
325 gettimeofday(&start, NULL);
326 while (utime_since_now(&start) < usec)
330 static void usec_sleep(struct thread_data *td, unsigned long usec)
332 struct timespec req, rem;
334 req.tv_sec = usec / 1000000;
335 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
343 rem.tv_sec = rem.tv_nsec = 0;
344 if (nanosleep(&req, &rem) < 0)
347 if ((rem.tv_sec + rem.tv_nsec) == 0)
350 req.tv_nsec = rem.tv_nsec;
351 req.tv_sec = rem.tv_sec;
353 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
354 } while (!td->terminate);
357 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
360 unsigned long usec_cycle;
365 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
367 if (time_spent < usec_cycle) {
368 unsigned long s = usec_cycle - time_spent;
370 td->rate_pending_usleep += s;
371 if (td->rate_pending_usleep >= 100000) {
372 usec_sleep(td, td->rate_pending_usleep);
373 td->rate_pending_usleep = 0;
376 long overtime = time_spent - usec_cycle;
378 td->rate_pending_usleep -= overtime;
382 static int check_min_rate(struct thread_data *td, struct timeval *now)
388 * allow a 2 second settle period in the beginning
390 if (mtime_since(&td->start, now) < 2000)
394 * if rate blocks is set, sample is running
396 if (td->rate_bytes) {
397 spent = mtime_since(&td->lastrate, now);
398 if (spent < td->ratecycle)
401 rate = (td->this_io_bytes - td->rate_bytes) / spent;
402 if (rate < td->ratemin) {
403 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
405 terminate_threads(td->groupid);
410 td->rate_bytes = td->this_io_bytes;
411 memcpy(&td->lastrate, now, sizeof(*now));
415 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
419 if (mtime_since(&td->start, t) >= td->timeout * 1000)
425 static void fill_random_bytes(struct thread_data *td,
426 unsigned char *p, unsigned int len)
432 drand48_r(&td->verify_state, &r);
435 * lrand48_r seems to be broken and only fill the bottom
436 * 32-bits, even on 64-bit archs with 64-bit longs
449 static void hexdump(void *buffer, int len)
451 unsigned char *p = buffer;
454 for (i = 0; i < len; i++)
455 printf("%02x", p[i]);
459 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
461 unsigned char *p = (unsigned char *) io_u->buf;
465 c = crc32(p, hdr->len - sizeof(*hdr));
467 return c != hdr->crc32;
470 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
472 unsigned char *p = (unsigned char *) io_u->buf;
473 struct md5_ctx md5_ctx;
476 memset(&md5_ctx, 0, sizeof(md5_ctx));
478 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
480 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
482 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
483 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
489 static int verify_io_u(struct io_u *io_u)
491 struct verify_header *hdr = (struct verify_header *) io_u->buf;
494 if (hdr->fio_magic != FIO_HDR_MAGIC)
497 if (hdr->verify_type == VERIFY_MD5)
498 ret = verify_io_u_md5(hdr, io_u);
499 else if (hdr->verify_type == VERIFY_CRC32)
500 ret = verify_io_u_crc32(hdr, io_u);
502 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
509 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
511 hdr->crc32 = crc32(p, len);
514 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
516 struct md5_ctx md5_ctx;
518 memset(&md5_ctx, 0, sizeof(md5_ctx));
519 md5_update(&md5_ctx, p, len);
520 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
524 * fill body of io_u->buf with random data and add a header with the
525 * (eg) sha1sum of that data.
527 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
529 unsigned char *p = (unsigned char *) io_u->buf;
530 struct verify_header hdr;
532 hdr.fio_magic = FIO_HDR_MAGIC;
533 hdr.len = io_u->buflen;
535 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
537 if (td->verify == VERIFY_MD5) {
538 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
539 hdr.verify_type = VERIFY_MD5;
541 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
542 hdr.verify_type = VERIFY_CRC32;
545 memcpy(io_u->buf, &hdr, sizeof(hdr));
548 static void put_io_u(struct thread_data *td, struct io_u *io_u)
550 list_del(&io_u->list);
551 list_add(&io_u->list, &td->io_u_freelist);
555 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
557 static struct io_u *__get_io_u(struct thread_data *td)
564 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
565 list_del(&io_u->list);
566 list_add(&io_u->list, &td->io_u_busylist);
571 static struct io_u *get_io_u(struct thread_data *td)
575 io_u = __get_io_u(td);
579 if (get_next_offset(td, &io_u->offset)) {
584 io_u->buflen = get_next_buflen(td);
590 if (io_u->buflen + io_u->offset > td->file_size)
591 io_u->buflen = td->file_size - io_u->offset;
594 mark_random_map(td, io_u);
596 td->last_bytes += io_u->buflen;
598 if (td->verify != VERIFY_NONE)
599 populate_io_u(td, io_u);
603 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
605 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
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) {
700 if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
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) {
725 ret = read(td->fd, io_u->buf, io_u->buflen);
726 if (ret < (int) io_u->buflen) {
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) {
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) {
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)) {
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,
867 r = io_getevents(td->aio_ctx, min, max, td->aio_events, t);
868 if (r != -EAGAIN && r != -EINTR)
875 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
877 struct iocb *iocb = &io_u->iocb;
881 ret = io_submit(td->aio_ctx, 1, &iocb);
884 else if (ret == -EAGAIN)
886 else if (ret == -EINTR)
895 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
896 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
898 static int ios_completed(struct thread_data *td, int nr)
905 gettimeofday(&e, NULL);
907 for (i = 0, bytes_done = 0; i < nr; i++) {
908 io_u = ev_to_iou(td->aio_events + i);
911 td->io_bytes += io_u->buflen;
912 td->this_io_bytes += io_u->buflen;
914 msec = mtime_since(&io_u->issue_time, &e);
916 add_clat_sample(td, msec);
920 log_io_piece(td, io_u);
922 bytes_done += io_u->buflen;
929 static void cleanup_pending_aio(struct thread_data *td)
931 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
932 struct list_head *entry, *n;
937 * get immediately available events, if any
939 r = io_u_getevents(td, 0, td->cur_depth, &ts);
941 ios_completed(td, r);
944 * now cancel remaining active events
946 list_for_each_safe(entry, n, &td->io_u_busylist) {
947 io_u = list_entry(entry, struct io_u, list);
949 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
955 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
957 ios_completed(td, r);
961 static int async_do_verify(struct thread_data *td, struct io_u **io_u)
963 struct io_u *v_io_u = *io_u;
967 ret = verify_io_u(v_io_u);
968 put_io_u(td, v_io_u);
975 static void do_async_verify(struct thread_data *td)
978 struct io_u *io_u, *v_io_u = NULL;
981 td_set_runstate(td, TD_VERIFYING);
987 gettimeofday(&t, NULL);
988 if (runtime_exceeded(td, &t))
991 io_u = __get_io_u(td);
995 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
1000 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
1001 ret = io_u_queue(td, io_u);
1009 * we have one pending to verify, do that while the next
1010 * we are doing io on the next one
1012 if (async_do_verify(td, &v_io_u))
1015 ret = io_u_getevents(td, 1, 1, NULL);
1022 v_io_u = ev_to_iou(td->aio_events);
1024 td->cur_off = v_io_u->offset + v_io_u->buflen;
1027 * if we can't submit more io, we need to verify now
1029 if (queue_full(td) && async_do_verify(td, &v_io_u))
1034 async_do_verify(td, &v_io_u);
1037 cleanup_pending_aio(td);
1039 td_set_runstate(td, TD_RUNNING);
1042 static void do_async_io(struct thread_data *td)
1044 struct timeval s, e;
1047 while (td->this_io_bytes < td->io_size) {
1048 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1049 struct timespec *timeout;
1050 int ret, min_evts = 0;
1052 unsigned int bytes_done;
1057 io_u = get_io_u(td);
1061 memcpy(&s, &io_u->start_time, sizeof(s));
1063 ret = io_u_queue(td, io_u);
1070 gettimeofday(&io_u->issue_time, NULL);
1071 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1072 if (td->cur_depth < td->aio_depth) {
1080 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1087 bytes_done = ios_completed(td, ret);
1090 * the rate is batched for now, it should work for batches
1091 * of completions except the very first one which may look
1094 gettimeofday(&e, NULL);
1095 usec = utime_since(&s, &e);
1097 rate_throttle(td, usec, bytes_done);
1099 if (check_min_rate(td, &e)) {
1100 td->error = ENODATA;
1104 if (runtime_exceeded(td, &e))
1108 usec_sleep(td, td->thinktime);
1110 if (should_fsync(td) && td->fsync_blocks &&
1111 (td->io_blocks % td->fsync_blocks) == 0)
1116 cleanup_pending_aio(td);
1118 if (should_fsync(td))
1122 static void cleanup_aio(struct thread_data *td)
1124 io_destroy(td->aio_ctx);
1127 free(td->aio_events);
1130 static int init_aio(struct thread_data *td)
1132 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
1137 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
1141 static void cleanup_io_u(struct thread_data *td)
1143 struct list_head *entry, *n;
1146 list_for_each_safe(entry, n, &td->io_u_freelist) {
1147 io_u = list_entry(entry, struct io_u, list);
1149 list_del(&io_u->list);
1153 if (td->mem_type == MEM_MALLOC)
1154 free(td->orig_buffer);
1155 else if (td->mem_type == MEM_SHM) {
1156 struct shmid_ds sbuf;
1158 shmdt(td->orig_buffer);
1159 shmctl(td->shm_id, IPC_RMID, &sbuf);
1160 } else if (td->mem_type == MEM_MMAP)
1161 munmap(td->orig_buffer, td->orig_buffer_size);
1163 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1165 td->orig_buffer = NULL;
1168 static int init_io_u(struct thread_data *td)
1177 max_units = td->aio_depth;
1179 td->orig_buffer_size = td->max_bs * max_units + MASK;
1181 if (td->mem_type == MEM_MALLOC)
1182 td->orig_buffer = malloc(td->orig_buffer_size);
1183 else if (td->mem_type == MEM_SHM) {
1184 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1185 if (td->shm_id < 0) {
1191 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1192 if (td->orig_buffer == (void *) -1) {
1195 td->orig_buffer = NULL;
1198 } else if (td->mem_type == MEM_MMAP) {
1199 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
1200 if (td->orig_buffer == MAP_FAILED) {
1203 td->orig_buffer = NULL;
1208 INIT_LIST_HEAD(&td->io_u_freelist);
1209 INIT_LIST_HEAD(&td->io_u_busylist);
1210 INIT_LIST_HEAD(&td->io_hist_list);
1212 p = ALIGN(td->orig_buffer);
1213 for (i = 0; i < max_units; i++) {
1214 io_u = malloc(sizeof(*io_u));
1215 memset(io_u, 0, sizeof(*io_u));
1216 INIT_LIST_HEAD(&io_u->list);
1218 io_u->buf = p + td->max_bs * i;
1219 list_add(&io_u->list, &td->io_u_freelist);
1225 static int create_file(struct thread_data *td)
1227 unsigned long long left;
1233 * unless specifically asked for overwrite, let normal io extend it
1235 if (td_write(td) && !td->overwrite)
1238 if (!td->file_size) {
1239 fprintf(stderr, "Need size for create\n");
1244 printf("Client%d: Laying out IO file\n", td->thread_number);
1246 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1252 if (ftruncate(td->fd, td->file_size) == -1) {
1257 td->io_size = td->file_size;
1258 b = malloc(td->max_bs);
1259 memset(b, 0, td->max_bs);
1261 left = td->file_size;
1267 r = write(td->fd, b, bs);
1269 if (r == (int) bs) {
1282 if (td->create_fsync)
1291 static int file_exists(struct thread_data *td)
1295 if (stat(td->file_name, &st) != -1)
1298 return errno != ENOENT;
1301 static int file_size(struct thread_data *td)
1305 if (fstat(td->fd, &st) == -1) {
1311 if (!td->file_size || td->file_size > st.st_size)
1312 td->file_size = st.st_size;
1315 td->file_size = 1024 * 1024 * 1024;
1321 static int bdev_size(struct thread_data *td)
1325 if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
1330 if (!td->file_size || (td->file_size > bytes))
1331 td->file_size = bytes;
1336 static int get_file_size(struct thread_data *td)
1340 if (td->filetype == FIO_TYPE_FILE)
1341 ret = file_size(td);
1343 ret = bdev_size(td);
1348 if (td->file_offset > td->file_size) {
1349 fprintf(stderr, "Client%d: offset larger than length\n", td->thread_number);
1353 td->io_size = td->file_size - td->file_offset;
1354 if (td->io_size == 0) {
1355 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1363 static int setup_file_mmap(struct thread_data *td)
1372 if (td->verify != VERIFY_NONE)
1376 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1377 if (td->mmap == MAP_FAILED) {
1383 if (td->invalidate_cache) {
1384 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1390 if (td->sequential) {
1391 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1396 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1405 static int setup_file_plain(struct thread_data *td)
1407 if (td->invalidate_cache) {
1408 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1414 if (td->sequential) {
1415 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1420 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1429 static int setup_file(struct thread_data *td)
1433 if (!file_exists(td)) {
1434 if (!td->create_file) {
1438 if (create_file(td))
1446 td->fd = open(td->file_name, flags | O_RDONLY);
1455 td->fd = open(td->file_name, flags | O_CREAT, 0600);
1463 if (get_file_size(td))
1466 if (td_write(td) && ftruncate(td->fd, td->file_size) == -1) {
1472 return setup_file_plain(td);
1474 return setup_file_mmap(td);
1477 static void clear_io_state(struct thread_data *td)
1480 lseek(td->fd, SEEK_SET, 0);
1484 td->stat_io_bytes = 0;
1485 td->this_io_bytes = 0;
1488 memset(td->file_map, 0, td->num_maps * sizeof(long));
1491 static void update_rusage_stat(struct thread_data *td)
1496 getrusage(RUSAGE_SELF, &td->ru_end);
1498 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1499 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1500 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1503 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1506 static void *thread_main(void *data)
1508 struct thread_data *td = data;
1517 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1522 if (td->use_aio && init_aio(td))
1526 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1532 sem_post(&startup_sem);
1533 sem_wait(&td->mutex);
1535 if (!td->create_serialize && setup_file(td))
1538 if (init_random_state(td))
1541 while (td->loops--) {
1542 getrusage(RUSAGE_SELF, &td->ru_start);
1543 gettimeofday(&td->start, NULL);
1544 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1547 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1550 prune_io_piece_log(td);
1560 td->runtime += mtime_since_now(&td->start);
1561 update_rusage_stat(td);
1563 if (td->verify == VERIFY_NONE)
1571 do_async_verify(td);
1580 finish_log(td, td->bw_log, "bw");
1582 finish_log(td, td->lat_log, "lat");
1584 if (exitall_on_terminate)
1585 terminate_threads(td->groupid);
1593 munmap(td->mmap, td->file_size);
1598 sem_post(&startup_sem);
1599 sem_wait(&td->mutex);
1601 td_set_runstate(td, TD_EXITED);
1606 static void *fork_main(int shm_id, int offset)
1608 struct thread_data *td;
1611 data = shmat(shm_id, NULL, 0);
1612 if (data == (void *) -1) {
1617 td = data + offset * sizeof(struct thread_data);
1623 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1624 double *mean, double *dev)
1628 if (is->samples == 0)
1634 n = (double) is->samples;
1635 *mean = (double) is->val / n;
1636 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1640 static void show_thread_status(struct thread_data *td,
1641 struct group_run_stats *rs)
1643 int prio, prio_class;
1644 unsigned long min, max, bw = 0;
1645 double mean, dev, usr_cpu, sys_cpu;
1647 if (!td->io_bytes && !td->error)
1651 bw = td->io_bytes / td->runtime;
1653 prio = td->ioprio & 0xff;
1654 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1656 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);
1658 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1659 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1660 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1661 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1662 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
1665 p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
1666 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);
1670 usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
1671 sys_cpu = (double) td->sys_time * 100 / (double) td->runtime;
1677 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1680 static void print_thread_status(struct thread_data *td, int nr_running,
1681 int t_rate, int m_rate)
1683 printf("Threads now running: %d", nr_running);
1684 if (m_rate || t_rate)
1685 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1686 printf(" : [%s]\r", run_str);
1690 static void check_str_update(struct thread_data *td, int n, int t, int m)
1692 char c = run_str[td->thread_number - 1];
1694 if (td->runstate == td->old_runstate)
1697 switch (td->runstate) {
1723 case TD_NOT_CREATED:
1727 printf("state %d\n", td->runstate);
1730 run_str[td->thread_number - 1] = c;
1731 print_thread_status(td, n, t, m);
1732 td->old_runstate = td->runstate;
1735 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1740 * reap exited threads (TD_EXITED -> TD_REAPED)
1742 for (i = 0; i < thread_number; i++) {
1743 struct thread_data *td = &threads[i];
1745 check_str_update(td, *nr_running, *t_rate, *m_rate);
1747 if (td->runstate != TD_EXITED)
1750 td_set_runstate(td, TD_REAPED);
1752 if (td->use_thread) {
1755 if (pthread_join(td->thread, (void *) &ret))
1756 perror("thread_join");
1758 waitpid(td->pid, NULL, 0);
1761 (*m_rate) -= td->ratemin;
1762 (*t_rate) -= td->rate;
1763 check_str_update(td, *nr_running, *t_rate, *m_rate);
1767 static void run_threads(char *argv[])
1769 struct timeval genesis;
1770 struct thread_data *td;
1771 unsigned long spent;
1772 int i, todo, nr_running, m_rate, t_rate, nr_started;
1774 printf("Starting %d threads\n", thread_number);
1777 signal(SIGINT, sig_handler);
1779 todo = thread_number;
1782 m_rate = t_rate = 0;
1784 for (i = 0; i < thread_number; i++) {
1787 if (!td->create_serialize)
1791 * do file setup here so it happens sequentially,
1792 * we don't want X number of threads getting their
1793 * client data interspersed on disk
1795 if (setup_file(td)) {
1796 td_set_runstate(td, TD_REAPED);
1801 gettimeofday(&genesis, NULL);
1805 * create threads (TD_NOT_CREATED -> TD_CREATED)
1807 for (i = 0; i < thread_number; i++) {
1810 if (td->runstate != TD_NOT_CREATED)
1814 * never got a chance to start, killed by other
1815 * thread for some reason
1817 if (td->terminate) {
1822 if (td->start_delay) {
1823 spent = mtime_since_now(&genesis);
1825 if (td->start_delay * 1000 > spent)
1829 if (td->stonewall && (nr_started || nr_running))
1832 td_set_runstate(td, TD_CREATED);
1833 check_str_update(td, nr_running, t_rate, m_rate);
1834 sem_init(&startup_sem, 1, 1);
1838 if (td->use_thread) {
1839 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1840 perror("thread_create");
1845 sem_wait(&startup_sem);
1847 fork_main(shm_id, i);
1854 * start created threads (TD_CREATED -> TD_RUNNING)
1856 for (i = 0; i < thread_number; i++) {
1857 struct thread_data *td = &threads[i];
1859 if (td->runstate != TD_CREATED)
1862 td_set_runstate(td, TD_RUNNING);
1865 m_rate += td->ratemin;
1867 check_str_update(td, nr_running, t_rate, m_rate);
1868 sem_post(&td->mutex);
1871 for (i = 0; i < thread_number; i++) {
1872 struct thread_data *td = &threads[i];
1874 if (td->runstate != TD_RUNNING &&
1875 td->runstate != TD_VERIFYING)
1878 check_str_update(td, nr_running, t_rate, m_rate);
1881 reap_threads(&nr_running, &t_rate, &m_rate);
1887 while (nr_running) {
1888 reap_threads(&nr_running, &t_rate, &m_rate);
1893 static void show_group_stats(struct group_run_stats *rs, int id)
1895 printf("\nRun status group %d:\n", id);
1897 if (rs->max_run[DDIR_READ])
1898 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]);
1899 if (rs->max_run[DDIR_WRITE])
1900 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]);
1903 static void show_run_stats(void)
1905 struct group_run_stats *runstats, *rs;
1906 struct thread_data *td;
1909 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1911 for (i = 0; i < groupid + 1; i++) {
1914 memset(rs, 0, sizeof(*rs));
1915 rs->min_bw[0] = rs->min_run[0] = ~0UL;
1916 rs->min_bw[1] = rs->min_run[1] = ~0UL;
1919 for (i = 0; i < thread_number; i++) {
1920 unsigned long bw = 0;
1927 rs = &runstats[td->groupid];
1929 if (td->runtime < rs->min_run[td->ddir])
1930 rs->min_run[td->ddir] = td->runtime;
1931 if (td->runtime > rs->max_run[td->ddir])
1932 rs->max_run[td->ddir] = td->runtime;
1935 bw = td->io_bytes / td->runtime;
1936 if (bw < rs->min_bw[td->ddir])
1937 rs->min_bw[td->ddir] = bw;
1938 if (bw > rs->max_bw[td->ddir])
1939 rs->max_bw[td->ddir] = bw;
1941 rs->io_mb[td->ddir] += td->io_bytes >> 20;
1944 for (i = 0; i < groupid + 1; i++) {
1948 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
1950 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
1953 for (i = 0; i < thread_number; i++) {
1955 rs = &runstats[td->groupid];
1957 show_thread_status(td, rs);
1960 for (i = 0; i < groupid + 1; i++)
1961 show_group_stats(&runstats[i], i);
1964 int main(int argc, char *argv[])
1966 memset(run_str, 0, sizeof(run_str));
1968 if (parse_options(argc, argv))
1971 if (!thread_number) {
1972 printf("Nothing to do\n");