2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
38 #include <sys/ioctl.h>
46 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
49 int thread_number = 0;
50 static char run_str[MAX_JOBS + 1];
52 static LIST_HEAD(disk_list);
53 static struct itimerval itimer;
54 static struct timeval genesis;
56 static void update_io_ticks(void);
57 static void disk_util_timer_arm(void);
58 static void print_thread_status(void);
60 extern unsigned long long mlock_size;
75 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
77 static sem_t startup_sem;
79 #define TERMINATE_ALL (-1)
80 #define JOB_START_TIMEOUT (5 * 1000)
82 static void terminate_threads(int group_id)
86 for (i = 0; i < thread_number; i++) {
87 struct thread_data *td = &threads[i];
89 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
96 static void sig_handler(int sig)
101 disk_util_timer_arm();
102 print_thread_status();
105 printf("\nfio: terminating on signal\n");
107 terminate_threads(TERMINATE_ALL);
112 static unsigned long utime_since(struct timeval *s, struct timeval *e)
116 sec = e->tv_sec - s->tv_sec;
117 usec = e->tv_usec - s->tv_usec;
118 if (sec > 0 && usec < 0) {
123 sec *= (double) 1000000;
128 static unsigned long utime_since_now(struct timeval *s)
132 gettimeofday(&t, NULL);
133 return utime_since(s, &t);
136 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
140 sec = e->tv_sec - s->tv_sec;
141 usec = e->tv_usec - s->tv_usec;
142 if (sec > 0 && usec < 0) {
147 sec *= (double) 1000;
148 usec /= (double) 1000;
153 static unsigned long mtime_since_now(struct timeval *s)
157 gettimeofday(&t, NULL);
158 return mtime_since(s, &t);
161 static inline unsigned long msec_now(struct timeval *s)
163 return s->tv_sec * 1000 + s->tv_usec / 1000;
166 static unsigned long time_since_now(struct timeval *s)
168 return mtime_since_now(s) / 1000;
171 static int random_map_free(struct thread_data *td, unsigned long long block)
173 unsigned int idx = RAND_MAP_IDX(td, block);
174 unsigned int bit = RAND_MAP_BIT(td, block);
176 return (td->file_map[idx] & (1UL << bit)) == 0;
179 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
185 while ((*b) * td->min_bs < td->io_size) {
186 if (td->file_map[i] != -1UL) {
187 *b += ffz(td->file_map[i]);
191 *b += BLOCKS_PER_MAP;
198 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
200 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
201 unsigned int blocks = 0;
203 while (blocks < (io_u->buflen / td->min_bs)) {
204 unsigned int idx, bit;
206 if (!random_map_free(td, block))
209 idx = RAND_MAP_IDX(td, block);
210 bit = RAND_MAP_BIT(td, block);
212 assert(idx < td->num_maps);
214 td->file_map[idx] |= (1UL << bit);
219 if ((blocks * td->min_bs) < io_u->buflen)
220 io_u->buflen = blocks * td->min_bs;
223 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
225 if (val > is->max_val)
227 if (val < is->min_val)
231 is->val_sq += val * val;
235 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
236 unsigned long val, int ddir)
238 if (iolog->nr_samples == iolog->max_samples) {
239 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
241 iolog->log = realloc(iolog->log, new_size);
242 iolog->max_samples <<= 1;
245 iolog->log[iolog->nr_samples].val = val;
246 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
247 iolog->log[iolog->nr_samples].ddir = ddir;
251 static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec)
253 add_stat_sample(&td->clat_stat[ddir], msec);
256 add_log_sample(td, td->clat_log, msec, ddir);
259 static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec)
261 add_stat_sample(&td->slat_stat[ddir], msec);
264 add_log_sample(td, td->slat_log, msec, ddir);
267 static void add_bw_sample(struct thread_data *td, int ddir)
269 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
272 if (spent < td->bw_avg_time)
275 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
276 add_stat_sample(&td->bw_stat[ddir], rate);
279 add_log_sample(td, td->bw_log, rate, ddir);
281 gettimeofday(&td->stat_sample_time[ddir], NULL);
282 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
285 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
287 unsigned long long b, rb;
290 if (!td->sequential) {
291 unsigned long long max_blocks = td->io_size / td->min_bs;
295 lrand48_r(&td->random_state, &r);
296 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
297 rb = b + (td->file_offset / td->min_bs);
299 } while (!random_map_free(td, rb) && loops);
302 if (get_next_free_block(td, &b))
306 b = td->last_pos / td->min_bs;
308 *offset = (b * td->min_bs) + td->file_offset;
309 if (*offset > td->real_file_size)
315 static unsigned int get_next_buflen(struct thread_data *td)
320 if (td->min_bs == td->max_bs)
323 lrand48_r(&td->bsrange_state, &r);
324 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
325 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
328 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
329 buflen = td->io_size - td->this_io_bytes[td->ddir];
335 * busy looping version for the last few usec
337 static void __usec_sleep(unsigned int usec)
339 struct timeval start;
341 gettimeofday(&start, NULL);
342 while (utime_since_now(&start) < usec)
346 static void usec_sleep(struct thread_data *td, unsigned long usec)
348 struct timespec req, rem;
350 req.tv_sec = usec / 1000000;
351 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
359 rem.tv_sec = rem.tv_nsec = 0;
360 if (nanosleep(&req, &rem) < 0)
363 if ((rem.tv_sec + rem.tv_nsec) == 0)
366 req.tv_nsec = rem.tv_nsec;
367 req.tv_sec = rem.tv_sec;
369 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
370 } while (!td->terminate);
373 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
376 unsigned long usec_cycle;
381 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
383 if (time_spent < usec_cycle) {
384 unsigned long s = usec_cycle - time_spent;
386 td->rate_pending_usleep += s;
387 if (td->rate_pending_usleep >= 100000) {
388 usec_sleep(td, td->rate_pending_usleep);
389 td->rate_pending_usleep = 0;
392 long overtime = time_spent - usec_cycle;
394 td->rate_pending_usleep -= overtime;
398 static int check_min_rate(struct thread_data *td, struct timeval *now)
405 * allow a 2 second settle period in the beginning
407 if (mtime_since(&td->start, now) < 2000)
411 * if rate blocks is set, sample is running
413 if (td->rate_bytes) {
414 spent = mtime_since(&td->lastrate, now);
415 if (spent < td->ratecycle)
418 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
419 if (rate < td->ratemin) {
420 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
422 terminate_threads(td->groupid);
427 td->rate_bytes = td->this_io_bytes[ddir];
428 memcpy(&td->lastrate, now, sizeof(*now));
432 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
436 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
442 static void fill_random_bytes(struct thread_data *td,
443 unsigned char *p, unsigned int len)
449 drand48_r(&td->verify_state, &r);
452 * lrand48_r seems to be broken and only fill the bottom
453 * 32-bits, even on 64-bit archs with 64-bit longs
466 static void hexdump(void *buffer, int len)
468 unsigned char *p = buffer;
471 for (i = 0; i < len; i++)
472 printf("%02x", p[i]);
476 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
478 unsigned char *p = (unsigned char *) io_u->buf;
483 c = crc32(p, hdr->len - sizeof(*hdr));
484 ret = c != hdr->crc32;
487 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
488 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
494 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
496 unsigned char *p = (unsigned char *) io_u->buf;
497 struct md5_ctx md5_ctx;
500 memset(&md5_ctx, 0, sizeof(md5_ctx));
502 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
504 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
506 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
507 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
508 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
514 static int verify_io_u(struct io_u *io_u)
516 struct verify_header *hdr = (struct verify_header *) io_u->buf;
519 if (hdr->fio_magic != FIO_HDR_MAGIC)
522 if (hdr->verify_type == VERIFY_MD5)
523 ret = verify_io_u_md5(hdr, io_u);
524 else if (hdr->verify_type == VERIFY_CRC32)
525 ret = verify_io_u_crc32(hdr, io_u);
527 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
534 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
536 hdr->crc32 = crc32(p, len);
539 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
541 struct md5_ctx md5_ctx;
543 memset(&md5_ctx, 0, sizeof(md5_ctx));
544 md5_update(&md5_ctx, p, len);
545 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
548 unsigned int hweight32(unsigned int w)
550 unsigned int res = w - ((w >> 1) & 0x55555555);
552 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
553 res = (res + (res >> 4)) & 0x0F0F0F0F;
554 res = res + (res >> 8);
556 return (res + (res >> 16)) & 0x000000FF;
559 unsigned long hweight64(unsigned long long w)
562 return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
563 #elif __WORDSIZE == 64
564 unsigned long long v = w - ((w >> 1) & 0x5555555555555555ul);
566 v = (v & 0x3333333333333333ul) + ((v >> 2) & 0x3333333333333333ul);
567 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
571 return (v + (v >> 32)) & 0x00000000000000FFul;
573 #error __WORDSIZE not defined
577 static int get_rw_ddir(struct thread_data *td)
580 * perhaps cheasy, but use the hamming weight of the position
581 * as a randomizer for data direction.
584 return hweight64(td->last_pos) & 1;
585 else if (td_read(td))
592 * fill body of io_u->buf with random data and add a header with the
593 * (eg) sha1sum of that data.
595 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
597 unsigned char *p = (unsigned char *) io_u->buf;
598 struct verify_header hdr;
600 hdr.fio_magic = FIO_HDR_MAGIC;
601 hdr.len = io_u->buflen;
603 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
605 if (td->verify == VERIFY_MD5) {
606 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
607 hdr.verify_type = VERIFY_MD5;
609 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
610 hdr.verify_type = VERIFY_CRC32;
613 memcpy(io_u->buf, &hdr, sizeof(hdr));
616 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
618 if (td->io_prep && td->io_prep(td, io_u))
624 void put_io_u(struct thread_data *td, struct io_u *io_u)
626 list_del(&io_u->list);
627 list_add(&io_u->list, &td->io_u_freelist);
631 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
634 * If using an iolog, grab next piece if any available.
637 struct io_piece *ipo;
639 if (list_empty(&td->io_log_list))
642 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
643 list_del(&ipo->list);
644 io_u->offset = ipo->offset;
645 io_u->buflen = ipo->len;
646 io_u->ddir = ipo->ddir;
652 * No log, let the seq/rand engine retrieve the next position.
654 if (!get_next_offset(td, &io_u->offset)) {
655 io_u->buflen = get_next_buflen(td);
658 io_u->ddir = get_rw_ddir(td);
666 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
668 struct io_u *__get_io_u(struct thread_data *td)
675 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
678 list_del(&io_u->list);
679 list_add(&io_u->list, &td->io_u_busylist);
684 static struct io_u *get_io_u(struct thread_data *td)
688 io_u = __get_io_u(td);
692 if (td->zone_bytes >= td->zone_size) {
694 td->last_pos += td->zone_skip;
697 if (fill_io_u(td, io_u)) {
702 if (io_u->buflen + io_u->offset > td->real_file_size)
703 io_u->buflen = td->real_file_size - io_u->offset;
710 if (!td->iolog && !td->sequential)
711 mark_random_map(td, io_u);
713 td->last_pos += io_u->buflen;
715 if (td->verify != VERIFY_NONE)
716 populate_io_u(td, io_u);
718 if (td_io_prep(td, io_u)) {
723 gettimeofday(&io_u->start_time, NULL);
727 static inline void td_set_runstate(struct thread_data *td, int runstate)
729 td->old_runstate = td->runstate;
730 td->runstate = runstate;
733 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
735 struct io_piece *ipo;
737 if (list_empty(&td->io_hist_list))
740 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
741 list_del(&ipo->list);
743 io_u->offset = ipo->offset;
744 io_u->buflen = ipo->len;
745 io_u->ddir = DDIR_READ;
750 static void prune_io_piece_log(struct thread_data *td)
752 struct io_piece *ipo;
754 while (!list_empty(&td->io_hist_list)) {
755 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
757 list_del(&ipo->list);
763 * log a succesful write, so we can unwind the log for verify
765 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
767 struct io_piece *ipo = malloc(sizeof(struct io_piece));
768 struct list_head *entry;
770 INIT_LIST_HEAD(&ipo->list);
771 ipo->offset = io_u->offset;
772 ipo->len = io_u->buflen;
775 * for random io where the writes extend the file, it will typically
776 * be laid out with the block scattered as written. it's faster to
777 * read them in in that order again, so don't sort
779 if (td->sequential || !td->overwrite) {
780 list_add_tail(&ipo->list, &td->io_hist_list);
785 * for random io, sort the list so verify will run faster
787 entry = &td->io_hist_list;
788 while ((entry = entry->prev) != &td->io_hist_list) {
789 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
791 if (__ipo->offset < ipo->offset)
795 list_add(&ipo->list, entry);
798 static int init_iolog(struct thread_data *td)
800 unsigned long long offset;
804 int rw, i, reads, writes;
809 f = fopen(td->iolog_file, "r");
811 perror("fopen iolog");
816 reads = writes = i = 0;
817 while ((p = fgets(str, 4096, f)) != NULL) {
818 struct io_piece *ipo;
820 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
821 fprintf(stderr, "bad iolog: %s\n", p);
826 else if (rw == DDIR_WRITE)
829 fprintf(stderr, "bad ddir: %d\n", rw);
833 ipo = malloc(sizeof(*ipo));
834 INIT_LIST_HEAD(&ipo->list);
835 ipo->offset = offset;
837 if (bytes > td->max_bs)
840 list_add_tail(&ipo->list, &td->io_log_list);
850 if (reads && !writes)
851 td->ddir = DDIR_READ;
852 else if (!reads && writes)
853 td->ddir = DDIR_READ;
860 static int sync_td(struct thread_data *td)
863 return td->io_sync(td);
868 static int io_u_getevents(struct thread_data *td, int min, int max,
871 return td->io_getevents(td, min, max, t);
874 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
876 gettimeofday(&io_u->issue_time, NULL);
878 return td->io_queue(td, io_u);
881 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
883 static void io_completed(struct thread_data *td, struct io_u *io_u,
884 struct io_completion_data *icd)
889 gettimeofday(&e, NULL);
892 unsigned int bytes = io_u->buflen - io_u->resid;
893 const int idx = io_u->ddir;
895 td->io_blocks[idx]++;
896 td->io_bytes[idx] += bytes;
897 td->zone_bytes += bytes;
898 td->this_io_bytes[idx] += bytes;
900 msec = mtime_since(&io_u->issue_time, &e);
902 add_clat_sample(td, idx, msec);
903 add_bw_sample(td, idx);
905 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
906 log_io_piece(td, io_u);
908 icd->bytes_done[idx] += bytes;
910 icd->error = io_u->error;
913 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
919 icd->bytes_done[0] = icd->bytes_done[1] = 0;
921 for (i = 0; i < icd->nr; i++) {
922 io_u = td->io_event(td, i);
924 io_completed(td, io_u, icd);
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;
933 struct io_completion_data icd;
938 * get immediately available events, if any
940 r = io_u_getevents(td, 0, td->cur_depth, &ts);
943 ios_completed(td, &icd);
947 * now cancel remaining active events
950 list_for_each_safe(entry, n, &td->io_u_busylist) {
951 io_u = list_entry(entry, struct io_u, list);
953 r = td->io_cancel(td, io_u);
960 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
963 ios_completed(td, &icd);
968 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
970 struct io_u *v_io_u = *io_u;
974 ret = verify_io_u(v_io_u);
975 put_io_u(td, v_io_u);
982 static void do_verify(struct thread_data *td)
985 struct io_u *io_u, *v_io_u = NULL;
986 struct io_completion_data icd;
989 td_set_runstate(td, TD_VERIFYING);
995 gettimeofday(&t, NULL);
996 if (runtime_exceeded(td, &t))
999 io_u = __get_io_u(td);
1003 if (get_next_verify(td, io_u)) {
1008 if (td_io_prep(td, io_u)) {
1013 ret = io_u_queue(td, io_u);
1021 * we have one pending to verify, do that while
1022 * we are doing io on the next one
1024 if (do_io_u_verify(td, &v_io_u))
1027 ret = io_u_getevents(td, 1, 1, NULL);
1034 v_io_u = td->io_event(td, 0);
1037 io_completed(td, v_io_u, &icd);
1040 td_verror(td, icd.error);
1041 put_io_u(td, v_io_u);
1047 * if we can't submit more io, we need to verify now
1049 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
1054 do_io_u_verify(td, &v_io_u);
1057 cleanup_pending_aio(td);
1059 td_set_runstate(td, TD_RUNNING);
1062 static void do_io(struct thread_data *td)
1064 struct io_completion_data icd;
1065 struct timeval s, e;
1068 while (td->this_io_bytes[td->ddir] < td->io_size) {
1069 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1070 struct timespec *timeout;
1071 int ret, min_evts = 0;
1077 io_u = get_io_u(td);
1081 memcpy(&s, &io_u->start_time, sizeof(s));
1083 ret = io_u_queue(td, io_u);
1090 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
1092 if (td->cur_depth < td->iodepth) {
1100 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1108 ios_completed(td, &icd);
1110 td_verror(td, icd.error);
1115 * the rate is batched for now, it should work for batches
1116 * of completions except the very first one which may look
1119 gettimeofday(&e, NULL);
1120 usec = utime_since(&s, &e);
1122 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
1124 if (check_min_rate(td, &e)) {
1125 td_verror(td, ENOMEM);
1129 if (runtime_exceeded(td, &e))
1133 usec_sleep(td, td->thinktime);
1135 if (should_fsync(td) && td->fsync_blocks &&
1136 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
1141 cleanup_pending_aio(td);
1143 if (should_fsync(td) && td->end_fsync)
1147 static void cleanup_io(struct thread_data *td)
1153 static int init_io(struct thread_data *td)
1155 if (td->io_engine == FIO_SYNCIO)
1156 return fio_syncio_init(td);
1157 else if (td->io_engine == FIO_MMAPIO)
1158 return fio_mmapio_init(td);
1159 else if (td->io_engine == FIO_LIBAIO)
1160 return fio_libaio_init(td);
1161 else if (td->io_engine == FIO_POSIXAIO)
1162 return fio_posixaio_init(td);
1163 else if (td->io_engine == FIO_SGIO)
1164 return fio_sgio_init(td);
1165 else if (td->io_engine == FIO_SPLICEIO)
1166 return fio_spliceio_init(td);
1168 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1173 static void cleanup_io_u(struct thread_data *td)
1175 struct list_head *entry, *n;
1178 list_for_each_safe(entry, n, &td->io_u_freelist) {
1179 io_u = list_entry(entry, struct io_u, list);
1181 list_del(&io_u->list);
1185 if (td->mem_type == MEM_MALLOC)
1186 free(td->orig_buffer);
1187 else if (td->mem_type == MEM_SHM) {
1188 struct shmid_ds sbuf;
1190 shmdt(td->orig_buffer);
1191 shmctl(td->shm_id, IPC_RMID, &sbuf);
1192 } else if (td->mem_type == MEM_MMAP)
1193 munmap(td->orig_buffer, td->orig_buffer_size);
1195 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1197 td->orig_buffer = NULL;
1200 static int init_io_u(struct thread_data *td)
1206 if (td->io_engine & FIO_SYNCIO)
1209 max_units = td->iodepth;
1211 td->orig_buffer_size = td->max_bs * max_units + MASK;
1213 if (td->mem_type == MEM_MALLOC)
1214 td->orig_buffer = malloc(td->orig_buffer_size);
1215 else if (td->mem_type == MEM_SHM) {
1216 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1217 if (td->shm_id < 0) {
1218 td_verror(td, errno);
1223 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1224 if (td->orig_buffer == (void *) -1) {
1225 td_verror(td, errno);
1227 td->orig_buffer = NULL;
1230 } else if (td->mem_type == MEM_MMAP) {
1231 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1232 if (td->orig_buffer == MAP_FAILED) {
1233 td_verror(td, errno);
1235 td->orig_buffer = NULL;
1240 p = ALIGN(td->orig_buffer);
1241 for (i = 0; i < max_units; i++) {
1242 io_u = malloc(sizeof(*io_u));
1243 memset(io_u, 0, sizeof(*io_u));
1244 INIT_LIST_HEAD(&io_u->list);
1246 io_u->buf = p + td->max_bs * i;
1248 list_add(&io_u->list, &td->io_u_freelist);
1254 static int create_file(struct thread_data *td, unsigned long long size,
1257 unsigned long long left;
1263 * unless specifically asked for overwrite, let normal io extend it
1265 if (td_write(td) && !td->overwrite)
1269 fprintf(stderr, "Need size for create\n");
1270 td_verror(td, EINVAL);
1275 oflags = O_CREAT | O_TRUNC;
1276 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1279 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1282 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1284 td_verror(td, errno);
1288 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1289 td_verror(td, errno);
1293 td->io_size = td->file_size;
1294 b = malloc(td->max_bs);
1295 memset(b, 0, td->max_bs);
1298 while (left && !td->terminate) {
1303 r = write(td->fd, b, bs);
1305 if (r == (int) bs) {
1310 td_verror(td, errno);
1319 unlink(td->file_name);
1320 else if (td->create_fsync)
1329 static int file_size(struct thread_data *td)
1333 if (fstat(td->fd, &st) == -1) {
1334 td_verror(td, errno);
1338 td->real_file_size = st.st_size;
1340 if (!td->file_size || td->file_size > td->real_file_size)
1341 td->file_size = td->real_file_size;
1343 td->file_size -= td->file_offset;
1347 static int bdev_size(struct thread_data *td)
1349 unsigned long long bytes;
1352 r = blockdev_size(td->fd, &bytes);
1358 td->real_file_size = bytes;
1361 * no extend possibilities, so limit size to device size if too large
1363 if (!td->file_size || td->file_size > td->real_file_size)
1364 td->file_size = td->real_file_size;
1366 td->file_size -= td->file_offset;
1370 static int get_file_size(struct thread_data *td)
1374 if (td->filetype == FIO_TYPE_FILE)
1375 ret = file_size(td);
1376 else if (td->filetype == FIO_TYPE_BD)
1377 ret = bdev_size(td);
1379 td->real_file_size = -1;
1384 if (td->file_offset > td->real_file_size) {
1385 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
1389 td->io_size = td->file_size;
1390 if (td->io_size == 0) {
1391 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1392 td_verror(td, EINVAL);
1397 td->zone_size = td->io_size;
1399 td->total_io_size = td->io_size * td->loops;
1403 static int setup_file_mmap(struct thread_data *td)
1408 flags = PROT_READ | PROT_WRITE;
1409 else if (td_write(td)) {
1412 if (td->verify != VERIFY_NONE)
1417 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1418 if (td->mmap == MAP_FAILED) {
1420 td_verror(td, errno);
1424 if (td->invalidate_cache) {
1425 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1426 td_verror(td, errno);
1431 if (td->sequential) {
1432 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1433 td_verror(td, errno);
1437 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1438 td_verror(td, errno);
1446 static int setup_file_plain(struct thread_data *td)
1448 if (td->invalidate_cache) {
1449 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1450 td_verror(td, errno);
1455 if (td->sequential) {
1456 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1457 td_verror(td, errno);
1461 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1462 td_verror(td, errno);
1470 static int setup_file(struct thread_data *td)
1475 if (stat(td->file_name, &st) == -1) {
1476 if (errno != ENOENT) {
1477 td_verror(td, errno);
1480 if (!td->create_file) {
1481 td_verror(td, ENOENT);
1484 if (create_file(td, td->file_size, 0))
1486 } else if (td->filetype == FIO_TYPE_FILE) {
1487 if (st.st_size < (off_t) td->file_size) {
1488 if (create_file(td, td->file_size - st.st_size, 1))
1496 if (td_write(td) || td_rw(td)) {
1497 if (td->filetype == FIO_TYPE_FILE) {
1508 td->fd = open(td->file_name, flags, 0600);
1510 if (td->filetype == FIO_TYPE_CHAR)
1515 td->fd = open(td->file_name, flags);
1519 td_verror(td, errno);
1523 if (get_file_size(td))
1526 if (td->io_engine != FIO_MMAPIO)
1527 return setup_file_plain(td);
1529 return setup_file_mmap(td);
1532 static int check_dev_match(dev_t dev, char *path)
1534 unsigned int major, minor;
1538 f = fopen(path, "r");
1540 perror("open path");
1544 p = fgets(line, sizeof(line), f);
1550 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1555 if (((major << 8) | minor) == dev) {
1564 static int find_block_dir(dev_t dev, char *path)
1575 while ((dir = readdir(D)) != NULL) {
1576 char full_path[256];
1578 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1580 if (!strcmp(dir->d_name, "device"))
1583 sprintf(full_path, "%s/%s", path, dir->d_name);
1585 if (!strcmp(dir->d_name, "dev")) {
1586 if (!check_dev_match(dev, full_path)) {
1592 if (stat(full_path, &st) == -1) {
1597 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1600 found = find_block_dir(dev, full_path);
1602 strcpy(path, full_path);
1611 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1618 f = fopen(du->path, "r");
1622 p = fgets(line, sizeof(line), f);
1628 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) {
1637 static void update_io_tick_disk(struct disk_util *du)
1639 struct disk_util_stat __dus, *dus, *ldus;
1642 if (get_io_ticks(du, &__dus))
1646 ldus = &du->last_dus;
1648 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1649 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1650 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1651 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1652 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1653 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1654 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1655 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1656 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1657 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1659 gettimeofday(&t, NULL);
1660 du->msec += mtime_since(&du->time, &t);
1661 memcpy(&du->time, &t, sizeof(t));
1662 memcpy(ldus, &__dus, sizeof(__dus));
1665 static void update_io_ticks(void)
1667 struct list_head *entry;
1668 struct disk_util *du;
1670 list_for_each(entry, &disk_list) {
1671 du = list_entry(entry, struct disk_util, list);
1672 update_io_tick_disk(du);
1676 static int disk_util_exists(dev_t dev)
1678 struct list_head *entry;
1679 struct disk_util *du;
1681 list_for_each(entry, &disk_list) {
1682 du = list_entry(entry, struct disk_util, list);
1691 static void disk_util_add(dev_t dev, char *path)
1693 struct disk_util *du = malloc(sizeof(*du));
1695 memset(du, 0, sizeof(*du));
1696 INIT_LIST_HEAD(&du->list);
1697 sprintf(du->path, "%s/stat", path);
1698 du->name = strdup(basename(path));
1701 gettimeofday(&du->time, NULL);
1702 get_io_ticks(du, &du->last_dus);
1704 list_add_tail(&du->list, &disk_list);
1707 static void init_disk_util(struct thread_data *td)
1710 char foo[256], tmp[256];
1714 if (!td->do_disk_util)
1717 if (!stat(td->file_name, &st)) {
1718 if (S_ISBLK(st.st_mode))
1724 * must be a file, open "." in that path
1726 strcpy(foo, td->file_name);
1729 perror("disk util stat");
1736 if (disk_util_exists(dev))
1739 sprintf(foo, "/sys/block");
1740 if (!find_block_dir(dev, foo))
1744 * If there's a ../queue/ directory there, we are inside a partition.
1745 * Check if that is the case and jump back. For loop/md/dm etc we
1746 * are already in the right spot.
1748 sprintf(tmp, "%s/../queue", foo);
1749 if (!stat(tmp, &st)) {
1751 sprintf(tmp, "%s/queue", p);
1752 if (stat(tmp, &st)) {
1753 fprintf(stderr, "unknown sysfs layout\n");
1756 sprintf(foo, "%s", p);
1759 disk_util_add(dev, foo);
1762 static void disk_util_timer_arm(void)
1764 itimer.it_value.tv_sec = 0;
1765 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1766 setitimer(ITIMER_REAL, &itimer, NULL);
1769 static void clear_io_state(struct thread_data *td)
1771 if (td->io_engine == FIO_SYNCIO)
1772 lseek(td->fd, SEEK_SET, 0);
1775 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1776 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1780 memset(td->file_map, 0, td->num_maps * sizeof(long));
1783 static void update_rusage_stat(struct thread_data *td)
1785 if (!(td->runtime[0] + td->runtime[1]))
1788 getrusage(RUSAGE_SELF, &td->ru_end);
1790 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1791 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1792 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1795 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1798 static void *thread_main(void *data)
1800 struct thread_data *td = data;
1803 if (!td->use_thread)
1808 INIT_LIST_HEAD(&td->io_u_freelist);
1809 INIT_LIST_HEAD(&td->io_u_busylist);
1810 INIT_LIST_HEAD(&td->io_hist_list);
1811 INIT_LIST_HEAD(&td->io_log_list);
1816 if (fio_setaffinity(td) == -1) {
1817 td_verror(td, errno);
1828 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1829 td_verror(td, errno);
1834 if (init_random_state(td))
1837 td_set_runstate(td, TD_INITIALIZED);
1838 sem_post(&startup_sem);
1839 sem_wait(&td->mutex);
1841 if (!td->create_serialize && setup_file(td))
1844 gettimeofday(&td->epoch, NULL);
1846 while (td->loops--) {
1847 getrusage(RUSAGE_SELF, &td->ru_start);
1848 gettimeofday(&td->start, NULL);
1849 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1852 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1855 prune_io_piece_log(td);
1859 td->runtime[td->ddir] += mtime_since_now(&td->start);
1860 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1861 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1863 update_rusage_stat(td);
1865 if (td->error || td->terminate)
1868 if (td->verify == VERIFY_NONE)
1872 gettimeofday(&td->start, NULL);
1876 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1878 if (td->error || td->terminate)
1885 finish_log(td, td->bw_log, "bw");
1887 finish_log(td, td->slat_log, "slat");
1889 finish_log(td, td->clat_log, "clat");
1891 if (exitall_on_terminate)
1892 terminate_threads(td->groupid);
1900 munmap(td->mmap, td->file_size);
1904 sem_post(&startup_sem);
1905 sem_wait(&td->mutex);
1907 td_set_runstate(td, TD_EXITED);
1912 static void *fork_main(int shmid, int offset)
1914 struct thread_data *td;
1917 data = shmat(shmid, NULL, 0);
1918 if (data == (void *) -1) {
1923 td = data + offset * sizeof(struct thread_data);
1929 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1930 double *mean, double *dev)
1934 if (is->samples == 0)
1940 n = (double) is->samples;
1941 *mean = (double) is->val / n;
1942 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1943 if (!(*min + *max) && !(*mean + *dev))
1949 static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
1952 char *ddir_str[] = { "read ", "write" };
1953 unsigned long min, max;
1954 unsigned long long bw;
1957 if (!td->runtime[ddir])
1960 bw = td->io_bytes[ddir] / td->runtime[ddir];
1961 printf(" %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
1963 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
1964 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1966 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
1967 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1969 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
1972 p_of_agg = mean * 100 / (double) rs->agg[ddir];
1973 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);
1977 static void show_thread_status(struct thread_data *td,
1978 struct group_run_stats *rs)
1980 double usr_cpu, sys_cpu;
1982 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
1985 printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error);
1987 show_ddir_status(td, rs, td->ddir);
1988 if (td->io_bytes[td->ddir ^ 1])
1989 show_ddir_status(td, rs, td->ddir ^ 1);
1991 if (td->runtime[0] + td->runtime[1]) {
1992 double runt = td->runtime[0] + td->runtime[1];
1994 usr_cpu = (double) td->usr_time * 100 / runt;
1995 sys_cpu = (double) td->sys_time * 100 / runt;
2001 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
2004 static void check_str_update(struct thread_data *td)
2006 char c = run_str[td->thread_number - 1];
2008 if (td->runstate == td->old_runstate)
2011 switch (td->runstate) {
2024 } else if (td_read(td)) {
2042 case TD_INITIALIZED:
2045 case TD_NOT_CREATED:
2049 printf("state %d\n", td->runstate);
2052 run_str[td->thread_number - 1] = c;
2053 td->old_runstate = td->runstate;
2056 static void eta_to_str(char *str, int eta_sec)
2058 unsigned int d, h, m, s;
2059 static int always_d, always_h;
2071 if (d || always_d) {
2073 str += sprintf(str, "%02dd:", d);
2075 if (h || always_h) {
2077 str += sprintf(str, "%02dh:", h);
2080 str += sprintf(str, "%02dm:", m);
2081 str += sprintf(str, "%02ds", s);
2084 static int thread_eta(struct thread_data *td, unsigned long elapsed)
2086 unsigned long long bytes_total, bytes_done;
2087 unsigned int eta_sec = 0;
2089 bytes_total = td->total_io_size;
2092 * if writing, bytes_total will be twice the size. If mixing,
2093 * assume a 50/50 split and thus bytes_total will be 50% larger.
2097 bytes_total = bytes_total * 3 / 2;
2101 if (td->zone_size && td->zone_skip)
2102 bytes_total /= (td->zone_skip / td->zone_size);
2104 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
2107 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
2108 perc = (double) bytes_done / (double) bytes_total;
2112 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
2114 if (td->timeout && eta_sec > (td->timeout - elapsed))
2115 eta_sec = td->timeout - elapsed;
2116 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
2117 || td->runstate == TD_INITIALIZED) {
2118 int t_eta = 0, r_eta = 0;
2121 * We can only guess - assume it'll run the full timeout
2122 * if given, otherwise assume it'll run at the specified rate.
2125 t_eta = td->timeout + td->start_delay - elapsed;
2127 r_eta = (bytes_total / 1024) / td->rate;
2128 r_eta += td->start_delay - elapsed;
2132 eta_sec = min(r_eta, t_eta);
2141 * thread is already done
2149 static void print_thread_status(void)
2151 unsigned long elapsed = time_since_now(&genesis);
2152 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
2156 eta_secs = malloc(thread_number * sizeof(int));
2157 memset(eta_secs, 0, thread_number * sizeof(int));
2159 nr_running = t_rate = m_rate = 0;
2160 for (i = 0; i < thread_number; i++) {
2161 struct thread_data *td = &threads[i];
2163 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
2166 m_rate += td->ratemin;
2170 eta_secs[i] = thread_eta(td, elapsed);
2172 eta_secs[i] = INT_MAX;
2174 check_str_update(td);
2177 if (exitall_on_terminate)
2182 for (i = 0; i < thread_number; i++) {
2183 if (exitall_on_terminate) {
2184 if (eta_secs[i] < eta_sec)
2185 eta_sec = eta_secs[i];
2187 if (eta_secs[i] > eta_sec)
2188 eta_sec = eta_secs[i];
2192 if (eta_sec != INT_MAX && elapsed) {
2193 perc = (double) elapsed / (double) (elapsed + eta_sec);
2194 eta_to_str(eta_str, eta_sec);
2197 printf("Threads now running (%d)", nr_running);
2198 if (m_rate || t_rate)
2199 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
2200 if (eta_sec != INT_MAX) {
2202 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
2209 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2214 * reap exited threads (TD_EXITED -> TD_REAPED)
2216 for (i = 0; i < thread_number; i++) {
2217 struct thread_data *td = &threads[i];
2219 if (td->runstate != TD_EXITED)
2222 td_set_runstate(td, TD_REAPED);
2224 if (td->use_thread) {
2227 if (pthread_join(td->thread, (void *) &ret))
2228 perror("thread_join");
2230 waitpid(td->pid, NULL, 0);
2233 (*m_rate) -= td->ratemin;
2234 (*t_rate) -= td->rate;
2238 static void fio_unpin_memory(void *pinned)
2241 if (munlock(pinned, mlock_size) < 0)
2243 munmap(pinned, mlock_size);
2247 static void *fio_pin_memory(void)
2249 long pagesize, pages;
2256 * Don't allow mlock of more than real_mem-128MB
2258 pagesize = sysconf(_SC_PAGESIZE);
2259 pages = sysconf(_SC_PHYS_PAGES);
2260 if (pages != -1 && pagesize != -1) {
2261 unsigned long long real_mem = pages * pagesize;
2263 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
2264 mlock_size = real_mem - 128 * 1024 * 1024;
2265 printf("fio: limiting mlocked memory to %lluMiB\n",
2270 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
2272 perror("malloc locked mem");
2275 if (mlock(ptr, mlock_size) < 0) {
2276 munmap(ptr, mlock_size);
2284 static void run_threads(void)
2286 struct thread_data *td;
2287 unsigned long spent;
2288 int i, todo, nr_running, m_rate, t_rate, nr_started;
2291 mlocked_mem = fio_pin_memory();
2293 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
2296 signal(SIGINT, sig_handler);
2297 signal(SIGALRM, sig_handler);
2299 todo = thread_number;
2302 m_rate = t_rate = 0;
2304 for (i = 0; i < thread_number; i++) {
2307 run_str[td->thread_number - 1] = 'P';
2311 if (!td->create_serialize)
2315 * do file setup here so it happens sequentially,
2316 * we don't want X number of threads getting their
2317 * client data interspersed on disk
2319 if (setup_file(td)) {
2320 td_set_runstate(td, TD_REAPED);
2325 gettimeofday(&genesis, NULL);
2328 struct thread_data *map[MAX_JOBS];
2329 struct timeval this_start;
2330 int this_jobs = 0, left;
2333 * create threads (TD_NOT_CREATED -> TD_CREATED)
2335 for (i = 0; i < thread_number; i++) {
2338 if (td->runstate != TD_NOT_CREATED)
2342 * never got a chance to start, killed by other
2343 * thread for some reason
2345 if (td->terminate) {
2350 if (td->start_delay) {
2351 spent = mtime_since_now(&genesis);
2353 if (td->start_delay * 1000 > spent)
2357 if (td->stonewall && (nr_started || nr_running))
2361 * Set state to created. Thread will transition
2362 * to TD_INITIALIZED when it's done setting up.
2364 td_set_runstate(td, TD_CREATED);
2365 map[this_jobs++] = td;
2366 sem_init(&startup_sem, 0, 1);
2369 if (td->use_thread) {
2370 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2371 perror("thread_create");
2376 sem_wait(&startup_sem);
2378 fork_main(shm_id, i);
2385 * Wait for the started threads to transition to
2388 printf("fio: Waiting for threads to initialize...\n");
2389 gettimeofday(&this_start, NULL);
2392 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2397 for (i = 0; i < this_jobs; i++) {
2401 if (td->runstate == TD_INITIALIZED ||
2402 td->runstate >= TD_EXITED) {
2411 fprintf(stderr, "fio: %d jobs failed to start\n", left);
2412 for (i = 0; i < this_jobs; i++) {
2416 kill(td->pid, SIGTERM);
2422 * start created threads (TD_INITIALIZED -> TD_RUNNING)
2424 printf("fio: Go for launch\n");
2425 for (i = 0; i < thread_number; i++) {
2428 if (td->runstate != TD_INITIALIZED)
2431 td_set_runstate(td, TD_RUNNING);
2434 m_rate += td->ratemin;
2437 sem_post(&td->mutex);
2440 reap_threads(&nr_running, &t_rate, &m_rate);
2446 while (nr_running) {
2447 reap_threads(&nr_running, &t_rate, &m_rate);
2452 fio_unpin_memory(mlocked_mem);
2455 static void show_group_stats(struct group_run_stats *rs, int id)
2457 printf("\nRun status group %d (all jobs):\n", id);
2459 if (rs->max_run[DDIR_READ])
2460 printf(" READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]);
2461 if (rs->max_run[DDIR_WRITE])
2462 printf(" WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
2465 static void show_disk_util(void)
2467 struct disk_util_stat *dus;
2468 struct list_head *entry;
2469 struct disk_util *du;
2472 printf("\nDisk stats (read/write):\n");
2474 list_for_each(entry, &disk_list) {
2475 du = list_entry(entry, struct disk_util, list);
2478 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2482 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);
2486 static void show_run_stats(void)
2488 struct group_run_stats *runstats, *rs;
2489 struct thread_data *td;
2492 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2494 for (i = 0; i < groupid + 1; i++) {
2497 memset(rs, 0, sizeof(*rs));
2498 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2499 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2502 for (i = 0; i < thread_number; i++) {
2503 unsigned long long rbw, wbw;
2508 printf("Client%d: %s\n", td->thread_number, td->verror);
2512 rs = &runstats[td->groupid];
2514 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
2515 rs->min_run[0] = td->runtime[0];
2516 if (td->runtime[0] > rs->max_run[0])
2517 rs->max_run[0] = td->runtime[0];
2518 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
2519 rs->min_run[1] = td->runtime[1];
2520 if (td->runtime[1] > rs->max_run[1])
2521 rs->max_run[1] = td->runtime[1];
2525 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
2527 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
2529 if (rbw < rs->min_bw[0])
2530 rs->min_bw[0] = rbw;
2531 if (wbw < rs->min_bw[1])
2532 rs->min_bw[1] = wbw;
2533 if (rbw > rs->max_bw[0])
2534 rs->max_bw[0] = rbw;
2535 if (wbw > rs->max_bw[1])
2536 rs->max_bw[1] = wbw;
2538 rs->io_mb[0] += td->io_bytes[0] >> 20;
2539 rs->io_mb[1] += td->io_bytes[1] >> 20;
2542 for (i = 0; i < groupid + 1; i++) {
2546 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2548 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2552 * don't overwrite last signal output
2556 for (i = 0; i < thread_number; i++) {
2558 rs = &runstats[td->groupid];
2560 show_thread_status(td, rs);
2563 for (i = 0; i < groupid + 1; i++)
2564 show_group_stats(&runstats[i], i);
2569 int main(int argc, char *argv[])
2571 if (parse_options(argc, argv))
2574 if (!thread_number) {
2575 printf("Nothing to do\n");
2579 disk_util_timer_arm();