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 void write_iolog_put(struct thread_data *td, struct io_u *io_u)
633 fprintf(td->iolog_f, "%d,%llu,%u\n", io_u->ddir, io_u->offset, io_u->buflen);
636 static int read_iolog_get(struct thread_data *td, struct io_u *io_u)
638 struct io_piece *ipo;
640 if (!list_empty(&td->io_log_list)) {
641 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
642 list_del(&ipo->list);
643 io_u->offset = ipo->offset;
644 io_u->buflen = ipo->len;
645 io_u->ddir = ipo->ddir;
653 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
656 * If using an iolog, grab next piece if any available.
659 return read_iolog_get(td, io_u);
662 * No log, let the seq/rand engine retrieve the next position.
664 if (!get_next_offset(td, &io_u->offset)) {
665 io_u->buflen = get_next_buflen(td);
668 io_u->ddir = get_rw_ddir(td);
671 * If using a write iolog, store this entry.
674 write_iolog_put(td, io_u);
683 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
685 struct io_u *__get_io_u(struct thread_data *td)
692 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
695 list_del(&io_u->list);
696 list_add(&io_u->list, &td->io_u_busylist);
701 static struct io_u *get_io_u(struct thread_data *td)
705 io_u = __get_io_u(td);
709 if (td->zone_bytes >= td->zone_size) {
711 td->last_pos += td->zone_skip;
714 if (fill_io_u(td, io_u)) {
719 if (io_u->buflen + io_u->offset > td->real_file_size)
720 io_u->buflen = td->real_file_size - io_u->offset;
727 if (!td->read_iolog && !td->sequential)
728 mark_random_map(td, io_u);
730 td->last_pos += io_u->buflen;
732 if (td->verify != VERIFY_NONE)
733 populate_io_u(td, io_u);
735 if (td_io_prep(td, io_u)) {
740 gettimeofday(&io_u->start_time, NULL);
744 static inline void td_set_runstate(struct thread_data *td, int runstate)
746 td->old_runstate = td->runstate;
747 td->runstate = runstate;
750 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
752 struct io_piece *ipo;
754 if (list_empty(&td->io_hist_list))
757 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
758 list_del(&ipo->list);
760 io_u->offset = ipo->offset;
761 io_u->buflen = ipo->len;
762 io_u->ddir = DDIR_READ;
767 static void prune_io_piece_log(struct thread_data *td)
769 struct io_piece *ipo;
771 while (!list_empty(&td->io_hist_list)) {
772 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
774 list_del(&ipo->list);
780 * log a succesful write, so we can unwind the log for verify
782 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
784 struct io_piece *ipo = malloc(sizeof(struct io_piece));
785 struct list_head *entry;
787 INIT_LIST_HEAD(&ipo->list);
788 ipo->offset = io_u->offset;
789 ipo->len = io_u->buflen;
792 * for random io where the writes extend the file, it will typically
793 * be laid out with the block scattered as written. it's faster to
794 * read them in in that order again, so don't sort
796 if (td->sequential || !td->overwrite) {
797 list_add_tail(&ipo->list, &td->io_hist_list);
802 * for random io, sort the list so verify will run faster
804 entry = &td->io_hist_list;
805 while ((entry = entry->prev) != &td->io_hist_list) {
806 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
808 if (__ipo->offset < ipo->offset)
812 list_add(&ipo->list, entry);
815 static void write_iolog_close(struct thread_data *td)
822 static int init_iolog(struct thread_data *td)
824 unsigned long long offset;
828 int rw, i, reads, writes;
830 if (!td->read_iolog && !td->write_iolog)
834 f = fopen(td->iolog_file, "r");
836 f = fopen(td->iolog_file, "w");
839 perror("fopen iolog");
840 printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog);
845 * That's it for writing, setup a log buffer and we're done.
847 if (td->write_iolog) {
849 td->iolog_buf = malloc(8192);
850 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
855 * Read in the read iolog and store it, reuse the infrastructure
856 * for doing verifications.
859 reads = writes = i = 0;
860 while ((p = fgets(str, 4096, f)) != NULL) {
861 struct io_piece *ipo;
863 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
864 fprintf(stderr, "bad iolog: %s\n", p);
869 else if (rw == DDIR_WRITE)
872 fprintf(stderr, "bad ddir: %d\n", rw);
876 ipo = malloc(sizeof(*ipo));
877 INIT_LIST_HEAD(&ipo->list);
878 ipo->offset = offset;
880 if (bytes > td->max_bs)
883 list_add_tail(&ipo->list, &td->io_log_list);
893 if (reads && !writes)
894 td->ddir = DDIR_READ;
895 else if (!reads && writes)
896 td->ddir = DDIR_READ;
903 static int sync_td(struct thread_data *td)
906 return td->io_sync(td);
911 static int io_u_getevents(struct thread_data *td, int min, int max,
914 return td->io_getevents(td, min, max, t);
917 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
919 gettimeofday(&io_u->issue_time, NULL);
921 return td->io_queue(td, io_u);
924 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
926 static void io_completed(struct thread_data *td, struct io_u *io_u,
927 struct io_completion_data *icd)
932 gettimeofday(&e, NULL);
935 unsigned int bytes = io_u->buflen - io_u->resid;
936 const int idx = io_u->ddir;
938 td->io_blocks[idx]++;
939 td->io_bytes[idx] += bytes;
940 td->zone_bytes += bytes;
941 td->this_io_bytes[idx] += bytes;
943 msec = mtime_since(&io_u->issue_time, &e);
945 add_clat_sample(td, idx, msec);
946 add_bw_sample(td, idx);
948 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
949 log_io_piece(td, io_u);
951 icd->bytes_done[idx] += bytes;
953 icd->error = io_u->error;
956 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
962 icd->bytes_done[0] = icd->bytes_done[1] = 0;
964 for (i = 0; i < icd->nr; i++) {
965 io_u = td->io_event(td, i);
967 io_completed(td, io_u, icd);
972 static void cleanup_pending_aio(struct thread_data *td)
974 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
975 struct list_head *entry, *n;
976 struct io_completion_data icd;
981 * get immediately available events, if any
983 r = io_u_getevents(td, 0, td->cur_depth, &ts);
986 ios_completed(td, &icd);
990 * now cancel remaining active events
993 list_for_each_safe(entry, n, &td->io_u_busylist) {
994 io_u = list_entry(entry, struct io_u, list);
996 r = td->io_cancel(td, io_u);
1002 if (td->cur_depth) {
1003 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
1006 ios_completed(td, &icd);
1011 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
1013 struct io_u *v_io_u = *io_u;
1017 ret = verify_io_u(v_io_u);
1018 put_io_u(td, v_io_u);
1025 static void do_verify(struct thread_data *td)
1028 struct io_u *io_u, *v_io_u = NULL;
1029 struct io_completion_data icd;
1032 td_set_runstate(td, TD_VERIFYING);
1038 gettimeofday(&t, NULL);
1039 if (runtime_exceeded(td, &t))
1042 io_u = __get_io_u(td);
1046 if (get_next_verify(td, io_u)) {
1051 if (td_io_prep(td, io_u)) {
1056 ret = io_u_queue(td, io_u);
1064 * we have one pending to verify, do that while
1065 * we are doing io on the next one
1067 if (do_io_u_verify(td, &v_io_u))
1070 ret = io_u_getevents(td, 1, 1, NULL);
1077 v_io_u = td->io_event(td, 0);
1080 io_completed(td, v_io_u, &icd);
1083 td_verror(td, icd.error);
1084 put_io_u(td, v_io_u);
1090 * if we can't submit more io, we need to verify now
1092 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
1097 do_io_u_verify(td, &v_io_u);
1100 cleanup_pending_aio(td);
1102 td_set_runstate(td, TD_RUNNING);
1105 static void do_io(struct thread_data *td)
1107 struct io_completion_data icd;
1108 struct timeval s, e;
1111 while (td->this_io_bytes[td->ddir] < td->io_size) {
1112 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1113 struct timespec *timeout;
1114 int ret, min_evts = 0;
1120 io_u = get_io_u(td);
1124 memcpy(&s, &io_u->start_time, sizeof(s));
1126 ret = io_u_queue(td, io_u);
1133 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
1135 if (td->cur_depth < td->iodepth) {
1143 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1151 ios_completed(td, &icd);
1153 td_verror(td, icd.error);
1158 * the rate is batched for now, it should work for batches
1159 * of completions except the very first one which may look
1162 gettimeofday(&e, NULL);
1163 usec = utime_since(&s, &e);
1165 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
1167 if (check_min_rate(td, &e)) {
1168 td_verror(td, ENOMEM);
1172 if (runtime_exceeded(td, &e))
1176 usec_sleep(td, td->thinktime);
1178 if (should_fsync(td) && td->fsync_blocks &&
1179 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
1184 cleanup_pending_aio(td);
1186 if (should_fsync(td) && td->end_fsync)
1190 static void cleanup_io(struct thread_data *td)
1196 static int init_io(struct thread_data *td)
1198 if (td->io_engine == FIO_SYNCIO)
1199 return fio_syncio_init(td);
1200 else if (td->io_engine == FIO_MMAPIO)
1201 return fio_mmapio_init(td);
1202 else if (td->io_engine == FIO_LIBAIO)
1203 return fio_libaio_init(td);
1204 else if (td->io_engine == FIO_POSIXAIO)
1205 return fio_posixaio_init(td);
1206 else if (td->io_engine == FIO_SGIO)
1207 return fio_sgio_init(td);
1208 else if (td->io_engine == FIO_SPLICEIO)
1209 return fio_spliceio_init(td);
1211 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1216 static void cleanup_io_u(struct thread_data *td)
1218 struct list_head *entry, *n;
1221 list_for_each_safe(entry, n, &td->io_u_freelist) {
1222 io_u = list_entry(entry, struct io_u, list);
1224 list_del(&io_u->list);
1228 if (td->mem_type == MEM_MALLOC)
1229 free(td->orig_buffer);
1230 else if (td->mem_type == MEM_SHM) {
1231 struct shmid_ds sbuf;
1233 shmdt(td->orig_buffer);
1234 shmctl(td->shm_id, IPC_RMID, &sbuf);
1235 } else if (td->mem_type == MEM_MMAP)
1236 munmap(td->orig_buffer, td->orig_buffer_size);
1238 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1240 td->orig_buffer = NULL;
1243 static int init_io_u(struct thread_data *td)
1249 if (td->io_engine & FIO_SYNCIO)
1252 max_units = td->iodepth;
1254 td->orig_buffer_size = td->max_bs * max_units + MASK;
1256 if (td->mem_type == MEM_MALLOC)
1257 td->orig_buffer = malloc(td->orig_buffer_size);
1258 else if (td->mem_type == MEM_SHM) {
1259 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1260 if (td->shm_id < 0) {
1261 td_verror(td, errno);
1266 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1267 if (td->orig_buffer == (void *) -1) {
1268 td_verror(td, errno);
1270 td->orig_buffer = NULL;
1273 } else if (td->mem_type == MEM_MMAP) {
1274 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1275 if (td->orig_buffer == MAP_FAILED) {
1276 td_verror(td, errno);
1278 td->orig_buffer = NULL;
1283 p = ALIGN(td->orig_buffer);
1284 for (i = 0; i < max_units; i++) {
1285 io_u = malloc(sizeof(*io_u));
1286 memset(io_u, 0, sizeof(*io_u));
1287 INIT_LIST_HEAD(&io_u->list);
1289 io_u->buf = p + td->max_bs * i;
1291 list_add(&io_u->list, &td->io_u_freelist);
1297 static int create_file(struct thread_data *td, unsigned long long size,
1300 unsigned long long left;
1306 * unless specifically asked for overwrite, let normal io extend it
1308 if (td_write(td) && !td->overwrite)
1312 fprintf(stderr, "Need size for create\n");
1313 td_verror(td, EINVAL);
1318 oflags = O_CREAT | O_TRUNC;
1319 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1322 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1325 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1327 td_verror(td, errno);
1331 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1332 td_verror(td, errno);
1336 td->io_size = td->file_size;
1337 b = malloc(td->max_bs);
1338 memset(b, 0, td->max_bs);
1341 while (left && !td->terminate) {
1346 r = write(td->fd, b, bs);
1348 if (r == (int) bs) {
1353 td_verror(td, errno);
1362 unlink(td->file_name);
1363 else if (td->create_fsync)
1372 static int file_size(struct thread_data *td)
1376 if (fstat(td->fd, &st) == -1) {
1377 td_verror(td, errno);
1381 td->real_file_size = st.st_size;
1383 if (!td->file_size || td->file_size > td->real_file_size)
1384 td->file_size = td->real_file_size;
1386 td->file_size -= td->file_offset;
1390 static int bdev_size(struct thread_data *td)
1392 unsigned long long bytes;
1395 r = blockdev_size(td->fd, &bytes);
1401 td->real_file_size = bytes;
1404 * no extend possibilities, so limit size to device size if too large
1406 if (!td->file_size || td->file_size > td->real_file_size)
1407 td->file_size = td->real_file_size;
1409 td->file_size -= td->file_offset;
1413 static int get_file_size(struct thread_data *td)
1417 if (td->filetype == FIO_TYPE_FILE)
1418 ret = file_size(td);
1419 else if (td->filetype == FIO_TYPE_BD)
1420 ret = bdev_size(td);
1422 td->real_file_size = -1;
1427 if (td->file_offset > td->real_file_size) {
1428 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
1432 td->io_size = td->file_size;
1433 if (td->io_size == 0) {
1434 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1435 td_verror(td, EINVAL);
1440 td->zone_size = td->io_size;
1442 td->total_io_size = td->io_size * td->loops;
1446 static int setup_file_mmap(struct thread_data *td)
1451 flags = PROT_READ | PROT_WRITE;
1452 else if (td_write(td)) {
1455 if (td->verify != VERIFY_NONE)
1460 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1461 if (td->mmap == MAP_FAILED) {
1463 td_verror(td, errno);
1467 if (td->invalidate_cache) {
1468 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1469 td_verror(td, errno);
1474 if (td->sequential) {
1475 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1476 td_verror(td, errno);
1480 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1481 td_verror(td, errno);
1489 static int setup_file_plain(struct thread_data *td)
1491 if (td->invalidate_cache) {
1492 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1493 td_verror(td, errno);
1498 if (td->sequential) {
1499 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1500 td_verror(td, errno);
1504 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1505 td_verror(td, errno);
1513 static int setup_file(struct thread_data *td)
1518 if (stat(td->file_name, &st) == -1) {
1519 if (errno != ENOENT) {
1520 td_verror(td, errno);
1523 if (!td->create_file) {
1524 td_verror(td, ENOENT);
1527 if (create_file(td, td->file_size, 0))
1529 } else if (td->filetype == FIO_TYPE_FILE) {
1530 if (st.st_size < (off_t) td->file_size) {
1531 if (create_file(td, td->file_size - st.st_size, 1))
1539 if (td_write(td) || td_rw(td)) {
1540 if (td->filetype == FIO_TYPE_FILE) {
1551 td->fd = open(td->file_name, flags, 0600);
1553 if (td->filetype == FIO_TYPE_CHAR)
1558 td->fd = open(td->file_name, flags);
1562 td_verror(td, errno);
1566 if (get_file_size(td))
1569 if (td->io_engine != FIO_MMAPIO)
1570 return setup_file_plain(td);
1572 return setup_file_mmap(td);
1575 static int check_dev_match(dev_t dev, char *path)
1577 unsigned int major, minor;
1581 f = fopen(path, "r");
1583 perror("open path");
1587 p = fgets(line, sizeof(line), f);
1593 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1598 if (((major << 8) | minor) == dev) {
1607 static int find_block_dir(dev_t dev, char *path)
1618 while ((dir = readdir(D)) != NULL) {
1619 char full_path[256];
1621 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1623 if (!strcmp(dir->d_name, "device"))
1626 sprintf(full_path, "%s/%s", path, dir->d_name);
1628 if (!strcmp(dir->d_name, "dev")) {
1629 if (!check_dev_match(dev, full_path)) {
1635 if (stat(full_path, &st) == -1) {
1640 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1643 found = find_block_dir(dev, full_path);
1645 strcpy(path, full_path);
1654 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1661 f = fopen(du->path, "r");
1665 p = fgets(line, sizeof(line), f);
1671 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) {
1680 static void update_io_tick_disk(struct disk_util *du)
1682 struct disk_util_stat __dus, *dus, *ldus;
1685 if (get_io_ticks(du, &__dus))
1689 ldus = &du->last_dus;
1691 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1692 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1693 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1694 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1695 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1696 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1697 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1698 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1699 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1700 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1702 gettimeofday(&t, NULL);
1703 du->msec += mtime_since(&du->time, &t);
1704 memcpy(&du->time, &t, sizeof(t));
1705 memcpy(ldus, &__dus, sizeof(__dus));
1708 static void update_io_ticks(void)
1710 struct list_head *entry;
1711 struct disk_util *du;
1713 list_for_each(entry, &disk_list) {
1714 du = list_entry(entry, struct disk_util, list);
1715 update_io_tick_disk(du);
1719 static int disk_util_exists(dev_t dev)
1721 struct list_head *entry;
1722 struct disk_util *du;
1724 list_for_each(entry, &disk_list) {
1725 du = list_entry(entry, struct disk_util, list);
1734 static void disk_util_add(dev_t dev, char *path)
1736 struct disk_util *du = malloc(sizeof(*du));
1738 memset(du, 0, sizeof(*du));
1739 INIT_LIST_HEAD(&du->list);
1740 sprintf(du->path, "%s/stat", path);
1741 du->name = strdup(basename(path));
1744 gettimeofday(&du->time, NULL);
1745 get_io_ticks(du, &du->last_dus);
1747 list_add_tail(&du->list, &disk_list);
1750 static void init_disk_util(struct thread_data *td)
1753 char foo[256], tmp[256];
1757 if (!td->do_disk_util)
1760 if (!stat(td->file_name, &st)) {
1761 if (S_ISBLK(st.st_mode))
1767 * must be a file, open "." in that path
1769 strcpy(foo, td->file_name);
1772 perror("disk util stat");
1779 if (disk_util_exists(dev))
1782 sprintf(foo, "/sys/block");
1783 if (!find_block_dir(dev, foo))
1787 * If there's a ../queue/ directory there, we are inside a partition.
1788 * Check if that is the case and jump back. For loop/md/dm etc we
1789 * are already in the right spot.
1791 sprintf(tmp, "%s/../queue", foo);
1792 if (!stat(tmp, &st)) {
1794 sprintf(tmp, "%s/queue", p);
1795 if (stat(tmp, &st)) {
1796 fprintf(stderr, "unknown sysfs layout\n");
1799 sprintf(foo, "%s", p);
1802 disk_util_add(dev, foo);
1805 static void disk_util_timer_arm(void)
1807 itimer.it_value.tv_sec = 0;
1808 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1809 setitimer(ITIMER_REAL, &itimer, NULL);
1812 static void clear_io_state(struct thread_data *td)
1814 if (td->io_engine == FIO_SYNCIO)
1815 lseek(td->fd, SEEK_SET, 0);
1818 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1819 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1823 memset(td->file_map, 0, td->num_maps * sizeof(long));
1826 static void update_rusage_stat(struct thread_data *td)
1828 if (!(td->runtime[0] + td->runtime[1]))
1831 getrusage(RUSAGE_SELF, &td->ru_end);
1833 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1834 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1835 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1838 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1841 static void *thread_main(void *data)
1843 struct thread_data *td = data;
1846 if (!td->use_thread)
1851 INIT_LIST_HEAD(&td->io_u_freelist);
1852 INIT_LIST_HEAD(&td->io_u_busylist);
1853 INIT_LIST_HEAD(&td->io_hist_list);
1854 INIT_LIST_HEAD(&td->io_log_list);
1859 if (fio_setaffinity(td) == -1) {
1860 td_verror(td, errno);
1871 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1872 td_verror(td, errno);
1877 if (init_random_state(td))
1880 td_set_runstate(td, TD_INITIALIZED);
1881 sem_post(&startup_sem);
1882 sem_wait(&td->mutex);
1885 if (!td->create_serialize && setup_file(td))
1888 gettimeofday(&td->epoch, NULL);
1890 while (td->loops--) {
1891 getrusage(RUSAGE_SELF, &td->ru_start);
1892 gettimeofday(&td->start, NULL);
1893 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1896 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1899 prune_io_piece_log(td);
1903 td->runtime[td->ddir] += mtime_since_now(&td->start);
1904 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1905 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1907 update_rusage_stat(td);
1909 if (td->error || td->terminate)
1912 if (td->verify == VERIFY_NONE)
1916 gettimeofday(&td->start, NULL);
1920 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1922 if (td->error || td->terminate)
1927 finish_log(td, td->bw_log, "bw");
1929 finish_log(td, td->slat_log, "slat");
1931 finish_log(td, td->clat_log, "clat");
1932 if (td->write_iolog)
1933 write_iolog_close(td);
1935 if (exitall_on_terminate)
1936 terminate_threads(td->groupid);
1944 munmap(td->mmap, td->file_size);
1948 sem_post(&startup_sem);
1949 sem_wait(&td->mutex);
1951 td_set_runstate(td, TD_EXITED);
1956 static void *fork_main(int shmid, int offset)
1958 struct thread_data *td;
1961 data = shmat(shmid, NULL, 0);
1962 if (data == (void *) -1) {
1967 td = data + offset * sizeof(struct thread_data);
1973 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1974 double *mean, double *dev)
1978 if (is->samples == 0)
1984 n = (double) is->samples;
1985 *mean = (double) is->val / n;
1986 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1987 if (!(*min + *max) && !(*mean + *dev))
1993 static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
1996 char *ddir_str[] = { "read ", "write" };
1997 unsigned long min, max;
1998 unsigned long long bw;
2001 if (!td->runtime[ddir])
2004 bw = td->io_bytes[ddir] / td->runtime[ddir];
2005 printf(" %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
2007 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
2008 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
2010 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
2011 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
2013 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
2016 p_of_agg = mean * 100 / (double) rs->agg[ddir];
2017 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);
2021 static void show_thread_status(struct thread_data *td,
2022 struct group_run_stats *rs)
2024 double usr_cpu, sys_cpu;
2026 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
2029 printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error);
2031 show_ddir_status(td, rs, td->ddir);
2032 if (td->io_bytes[td->ddir ^ 1])
2033 show_ddir_status(td, rs, td->ddir ^ 1);
2035 if (td->runtime[0] + td->runtime[1]) {
2036 double runt = td->runtime[0] + td->runtime[1];
2038 usr_cpu = (double) td->usr_time * 100 / runt;
2039 sys_cpu = (double) td->sys_time * 100 / runt;
2045 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
2048 static void check_str_update(struct thread_data *td)
2050 char c = run_str[td->thread_number - 1];
2052 if (td->runstate == td->old_runstate)
2055 switch (td->runstate) {
2068 } else if (td_read(td)) {
2086 case TD_INITIALIZED:
2089 case TD_NOT_CREATED:
2093 printf("state %d\n", td->runstate);
2096 run_str[td->thread_number - 1] = c;
2097 td->old_runstate = td->runstate;
2100 static void eta_to_str(char *str, int eta_sec)
2102 unsigned int d, h, m, s;
2103 static int always_d, always_h;
2115 if (d || always_d) {
2117 str += sprintf(str, "%02dd:", d);
2119 if (h || always_h) {
2121 str += sprintf(str, "%02dh:", h);
2124 str += sprintf(str, "%02dm:", m);
2125 str += sprintf(str, "%02ds", s);
2128 static int thread_eta(struct thread_data *td, unsigned long elapsed)
2130 unsigned long long bytes_total, bytes_done;
2131 unsigned int eta_sec = 0;
2133 bytes_total = td->total_io_size;
2136 * if writing, bytes_total will be twice the size. If mixing,
2137 * assume a 50/50 split and thus bytes_total will be 50% larger.
2141 bytes_total = bytes_total * 3 / 2;
2145 if (td->zone_size && td->zone_skip)
2146 bytes_total /= (td->zone_skip / td->zone_size);
2148 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
2151 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
2152 perc = (double) bytes_done / (double) bytes_total;
2156 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
2158 if (td->timeout && eta_sec > (td->timeout - elapsed))
2159 eta_sec = td->timeout - elapsed;
2160 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
2161 || td->runstate == TD_INITIALIZED) {
2162 int t_eta = 0, r_eta = 0;
2165 * We can only guess - assume it'll run the full timeout
2166 * if given, otherwise assume it'll run at the specified rate.
2169 t_eta = td->timeout + td->start_delay - elapsed;
2171 r_eta = (bytes_total / 1024) / td->rate;
2172 r_eta += td->start_delay - elapsed;
2176 eta_sec = min(r_eta, t_eta);
2185 * thread is already done
2193 static void print_thread_status(void)
2195 unsigned long elapsed = time_since_now(&genesis);
2196 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
2200 eta_secs = malloc(thread_number * sizeof(int));
2201 memset(eta_secs, 0, thread_number * sizeof(int));
2203 nr_running = t_rate = m_rate = 0;
2204 for (i = 0; i < thread_number; i++) {
2205 struct thread_data *td = &threads[i];
2207 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
2210 m_rate += td->ratemin;
2214 eta_secs[i] = thread_eta(td, elapsed);
2216 eta_secs[i] = INT_MAX;
2218 check_str_update(td);
2221 if (exitall_on_terminate)
2226 for (i = 0; i < thread_number; i++) {
2227 if (exitall_on_terminate) {
2228 if (eta_secs[i] < eta_sec)
2229 eta_sec = eta_secs[i];
2231 if (eta_secs[i] > eta_sec)
2232 eta_sec = eta_secs[i];
2236 if (eta_sec != INT_MAX && elapsed) {
2237 perc = (double) elapsed / (double) (elapsed + eta_sec);
2238 eta_to_str(eta_str, eta_sec);
2241 printf("Threads now running (%d)", nr_running);
2242 if (m_rate || t_rate)
2243 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
2244 if (eta_sec != INT_MAX) {
2246 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
2253 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2258 * reap exited threads (TD_EXITED -> TD_REAPED)
2260 for (i = 0; i < thread_number; i++) {
2261 struct thread_data *td = &threads[i];
2263 if (td->runstate != TD_EXITED)
2266 td_set_runstate(td, TD_REAPED);
2268 if (td->use_thread) {
2271 if (pthread_join(td->thread, (void *) &ret))
2272 perror("thread_join");
2274 waitpid(td->pid, NULL, 0);
2277 (*m_rate) -= td->ratemin;
2278 (*t_rate) -= td->rate;
2282 static void fio_unpin_memory(void *pinned)
2285 if (munlock(pinned, mlock_size) < 0)
2287 munmap(pinned, mlock_size);
2291 static void *fio_pin_memory(void)
2293 long pagesize, pages;
2300 * Don't allow mlock of more than real_mem-128MB
2302 pagesize = sysconf(_SC_PAGESIZE);
2303 pages = sysconf(_SC_PHYS_PAGES);
2304 if (pages != -1 && pagesize != -1) {
2305 unsigned long long real_mem = pages * pagesize;
2307 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
2308 mlock_size = real_mem - 128 * 1024 * 1024;
2309 printf("fio: limiting mlocked memory to %lluMiB\n",
2314 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
2316 perror("malloc locked mem");
2319 if (mlock(ptr, mlock_size) < 0) {
2320 munmap(ptr, mlock_size);
2328 static void run_threads(void)
2330 struct thread_data *td;
2331 unsigned long spent;
2332 int i, todo, nr_running, m_rate, t_rate, nr_started;
2335 mlocked_mem = fio_pin_memory();
2337 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
2340 signal(SIGINT, sig_handler);
2341 signal(SIGALRM, sig_handler);
2343 todo = thread_number;
2346 m_rate = t_rate = 0;
2348 for (i = 0; i < thread_number; i++) {
2351 run_str[td->thread_number - 1] = 'P';
2355 if (!td->create_serialize)
2359 * do file setup here so it happens sequentially,
2360 * we don't want X number of threads getting their
2361 * client data interspersed on disk
2363 if (setup_file(td)) {
2364 td_set_runstate(td, TD_REAPED);
2369 gettimeofday(&genesis, NULL);
2372 struct thread_data *map[MAX_JOBS];
2373 struct timeval this_start;
2374 int this_jobs = 0, left;
2377 * create threads (TD_NOT_CREATED -> TD_CREATED)
2379 for (i = 0; i < thread_number; i++) {
2382 if (td->runstate != TD_NOT_CREATED)
2386 * never got a chance to start, killed by other
2387 * thread for some reason
2389 if (td->terminate) {
2394 if (td->start_delay) {
2395 spent = mtime_since_now(&genesis);
2397 if (td->start_delay * 1000 > spent)
2401 if (td->stonewall && (nr_started || nr_running))
2405 * Set state to created. Thread will transition
2406 * to TD_INITIALIZED when it's done setting up.
2408 td_set_runstate(td, TD_CREATED);
2409 map[this_jobs++] = td;
2410 sem_init(&startup_sem, 0, 1);
2413 if (td->use_thread) {
2414 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2415 perror("thread_create");
2420 sem_wait(&startup_sem);
2422 fork_main(shm_id, i);
2429 * Wait for the started threads to transition to
2432 printf("fio: Waiting for threads to initialize...\n");
2433 gettimeofday(&this_start, NULL);
2436 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2441 for (i = 0; i < this_jobs; i++) {
2445 if (td->runstate == TD_INITIALIZED ||
2446 td->runstate >= TD_EXITED) {
2455 fprintf(stderr, "fio: %d jobs failed to start\n", left);
2456 for (i = 0; i < this_jobs; i++) {
2460 kill(td->pid, SIGTERM);
2466 * start created threads (TD_INITIALIZED -> TD_RUNNING)
2468 printf("fio: Go for launch\n");
2469 for (i = 0; i < thread_number; i++) {
2472 if (td->runstate != TD_INITIALIZED)
2475 td_set_runstate(td, TD_RUNNING);
2478 m_rate += td->ratemin;
2481 sem_post(&td->mutex);
2484 reap_threads(&nr_running, &t_rate, &m_rate);
2490 while (nr_running) {
2491 reap_threads(&nr_running, &t_rate, &m_rate);
2496 fio_unpin_memory(mlocked_mem);
2499 static void show_group_stats(struct group_run_stats *rs, int id)
2501 printf("\nRun status group %d (all jobs):\n", id);
2503 if (rs->max_run[DDIR_READ])
2504 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]);
2505 if (rs->max_run[DDIR_WRITE])
2506 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]);
2509 static void show_disk_util(void)
2511 struct disk_util_stat *dus;
2512 struct list_head *entry;
2513 struct disk_util *du;
2516 printf("\nDisk stats (read/write):\n");
2518 list_for_each(entry, &disk_list) {
2519 du = list_entry(entry, struct disk_util, list);
2522 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2526 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);
2530 static void show_run_stats(void)
2532 struct group_run_stats *runstats, *rs;
2533 struct thread_data *td;
2536 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2538 for (i = 0; i < groupid + 1; i++) {
2541 memset(rs, 0, sizeof(*rs));
2542 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2543 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2546 for (i = 0; i < thread_number; i++) {
2547 unsigned long long rbw, wbw;
2552 printf("Client%d: %s\n", td->thread_number, td->verror);
2556 rs = &runstats[td->groupid];
2558 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
2559 rs->min_run[0] = td->runtime[0];
2560 if (td->runtime[0] > rs->max_run[0])
2561 rs->max_run[0] = td->runtime[0];
2562 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
2563 rs->min_run[1] = td->runtime[1];
2564 if (td->runtime[1] > rs->max_run[1])
2565 rs->max_run[1] = td->runtime[1];
2569 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
2571 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
2573 if (rbw < rs->min_bw[0])
2574 rs->min_bw[0] = rbw;
2575 if (wbw < rs->min_bw[1])
2576 rs->min_bw[1] = wbw;
2577 if (rbw > rs->max_bw[0])
2578 rs->max_bw[0] = rbw;
2579 if (wbw > rs->max_bw[1])
2580 rs->max_bw[1] = wbw;
2582 rs->io_mb[0] += td->io_bytes[0] >> 20;
2583 rs->io_mb[1] += td->io_bytes[1] >> 20;
2586 for (i = 0; i < groupid + 1; i++) {
2590 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2592 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2596 * don't overwrite last signal output
2600 for (i = 0; i < thread_number; i++) {
2602 rs = &runstats[td->groupid];
2604 show_thread_status(td, rs);
2607 for (i = 0; i < groupid + 1; i++)
2608 show_group_stats(&runstats[i], i);
2613 int main(int argc, char *argv[])
2615 if (parse_options(argc, argv))
2618 if (!thread_number) {
2619 printf("Nothing to do\n");
2623 disk_util_timer_arm();