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);
72 #define should_fsync(td) (td_write(td) && (!(td)->odirect || (td)->override_sync))
74 static sem_t startup_sem;
76 #define TERMINATE_ALL (-1)
78 static void terminate_threads(int group_id)
82 for (i = 0; i < thread_number; i++) {
83 struct thread_data *td = &threads[i];
85 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
92 static void sig_handler(int sig)
97 disk_util_timer_arm();
98 print_thread_status();
101 printf("\nfio: terminating on signal\n");
103 terminate_threads(TERMINATE_ALL);
108 static unsigned long utime_since(struct timeval *s, struct timeval *e)
112 sec = e->tv_sec - s->tv_sec;
113 usec = e->tv_usec - s->tv_usec;
114 if (sec > 0 && usec < 0) {
119 sec *= (double) 1000000;
124 static unsigned long utime_since_now(struct timeval *s)
128 gettimeofday(&t, NULL);
129 return utime_since(s, &t);
132 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
136 sec = e->tv_sec - s->tv_sec;
137 usec = e->tv_usec - s->tv_usec;
138 if (sec > 0 && usec < 0) {
143 sec *= (double) 1000;
144 usec /= (double) 1000;
149 static unsigned long mtime_since_now(struct timeval *s)
153 gettimeofday(&t, NULL);
154 return mtime_since(s, &t);
157 static inline unsigned long msec_now(struct timeval *s)
159 return s->tv_sec * 1000 + s->tv_usec / 1000;
162 static unsigned long time_since_now(struct timeval *s)
164 return mtime_since_now(s) / 1000;
167 static int random_map_free(struct thread_data *td, unsigned long long block)
169 unsigned int idx = RAND_MAP_IDX(td, block);
170 unsigned int bit = RAND_MAP_BIT(td, block);
172 return (td->file_map[idx] & (1UL << bit)) == 0;
175 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
181 while ((*b) * td->min_bs < td->io_size) {
182 if (td->file_map[i] != -1UL) {
183 *b += ffz(td->file_map[i]);
187 *b += BLOCKS_PER_MAP;
194 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
196 unsigned long block = io_u->offset / td->min_bs;
197 unsigned int blocks = 0;
199 while (blocks < (io_u->buflen / td->min_bs)) {
200 unsigned int idx, bit;
202 if (!random_map_free(td, block))
205 idx = RAND_MAP_IDX(td, block);
206 bit = RAND_MAP_BIT(td, block);
208 assert(idx < td->num_maps);
210 td->file_map[idx] |= (1UL << bit);
215 if ((blocks * td->min_bs) < io_u->buflen)
216 io_u->buflen = blocks * td->min_bs;
219 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
221 unsigned long long b, rb;
224 if (!td->sequential) {
225 unsigned long max_blocks = td->io_size / td->min_bs;
229 lrand48_r(&td->random_state, &r);
230 b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
231 rb = b + (td->file_offset / td->min_bs);
233 } while (!random_map_free(td, rb) && loops);
236 if (get_next_free_block(td, &b))
240 b = td->last_bytes / td->min_bs;
242 *offset = (b * td->min_bs) + td->file_offset;
243 if (*offset > td->file_size)
249 static unsigned int get_next_buflen(struct thread_data *td)
254 if (td->min_bs == td->max_bs)
257 lrand48_r(&td->bsrange_state, &r);
258 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
259 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
262 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
263 buflen = td->io_size - td->this_io_bytes[td->ddir];
268 static inline void add_stat_sample(struct io_stat *is, unsigned long val)
270 if (val > is->max_val)
272 if (val < is->min_val)
276 is->val_sq += val * val;
280 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
281 unsigned long val, int ddir)
283 if (iolog->nr_samples == iolog->max_samples) {
284 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
286 iolog->log = realloc(iolog->log, new_size);
287 iolog->max_samples <<= 1;
290 iolog->log[iolog->nr_samples].val = val;
291 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
292 iolog->log[iolog->nr_samples].ddir = ddir;
296 static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec)
298 add_stat_sample(&td->clat_stat[ddir], msec);
301 add_log_sample(td, td->clat_log, msec, ddir);
304 static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec)
306 add_stat_sample(&td->slat_stat[ddir], msec);
309 add_log_sample(td, td->slat_log, msec, ddir);
312 static void add_bw_sample(struct thread_data *td, int ddir)
314 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
317 if (spent < td->bw_avg_time)
320 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
321 add_stat_sample(&td->bw_stat[ddir], rate);
324 add_log_sample(td, td->bw_log, rate, ddir);
326 gettimeofday(&td->stat_sample_time[ddir], NULL);
327 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
331 * busy looping version for the last few usec
333 static void __usec_sleep(unsigned int usec)
335 struct timeval start;
337 gettimeofday(&start, NULL);
338 while (utime_since_now(&start) < usec)
342 static void usec_sleep(struct thread_data *td, unsigned long usec)
344 struct timespec req, rem;
346 req.tv_sec = usec / 1000000;
347 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
355 rem.tv_sec = rem.tv_nsec = 0;
356 if (nanosleep(&req, &rem) < 0)
359 if ((rem.tv_sec + rem.tv_nsec) == 0)
362 req.tv_nsec = rem.tv_nsec;
363 req.tv_sec = rem.tv_sec;
365 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
366 } while (!td->terminate);
369 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
372 unsigned long usec_cycle;
377 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
379 if (time_spent < usec_cycle) {
380 unsigned long s = usec_cycle - time_spent;
382 td->rate_pending_usleep += s;
383 if (td->rate_pending_usleep >= 100000) {
384 usec_sleep(td, td->rate_pending_usleep);
385 td->rate_pending_usleep = 0;
388 long overtime = time_spent - usec_cycle;
390 td->rate_pending_usleep -= overtime;
394 static int check_min_rate(struct thread_data *td, struct timeval *now)
401 * allow a 2 second settle period in the beginning
403 if (mtime_since(&td->start, now) < 2000)
407 * if rate blocks is set, sample is running
409 if (td->rate_bytes) {
410 spent = mtime_since(&td->lastrate, now);
411 if (spent < td->ratecycle)
414 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
415 if (rate < td->ratemin) {
416 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
418 terminate_threads(td->groupid);
423 td->rate_bytes = td->this_io_bytes[ddir];
424 memcpy(&td->lastrate, now, sizeof(*now));
428 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
432 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
438 static void fill_random_bytes(struct thread_data *td,
439 unsigned char *p, unsigned int len)
445 drand48_r(&td->verify_state, &r);
448 * lrand48_r seems to be broken and only fill the bottom
449 * 32-bits, even on 64-bit archs with 64-bit longs
462 static void hexdump(void *buffer, int len)
464 unsigned char *p = buffer;
467 for (i = 0; i < len; i++)
468 printf("%02x", p[i]);
472 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
474 unsigned char *p = (unsigned char *) io_u->buf;
479 c = crc32(p, hdr->len - sizeof(*hdr));
480 ret = c != hdr->crc32;
483 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
484 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
490 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
492 unsigned char *p = (unsigned char *) io_u->buf;
493 struct md5_ctx md5_ctx;
496 memset(&md5_ctx, 0, sizeof(md5_ctx));
498 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
500 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
502 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
503 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
504 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
510 static int verify_io_u(struct io_u *io_u)
512 struct verify_header *hdr = (struct verify_header *) io_u->buf;
515 if (hdr->fio_magic != FIO_HDR_MAGIC)
518 if (hdr->verify_type == VERIFY_MD5)
519 ret = verify_io_u_md5(hdr, io_u);
520 else if (hdr->verify_type == VERIFY_CRC32)
521 ret = verify_io_u_crc32(hdr, io_u);
523 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
530 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
532 hdr->crc32 = crc32(p, len);
535 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
537 struct md5_ctx md5_ctx;
539 memset(&md5_ctx, 0, sizeof(md5_ctx));
540 md5_update(&md5_ctx, p, len);
541 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
545 * fill body of io_u->buf with random data and add a header with the
546 * (eg) sha1sum of that data.
548 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
550 unsigned char *p = (unsigned char *) io_u->buf;
551 struct verify_header hdr;
553 hdr.fio_magic = FIO_HDR_MAGIC;
554 hdr.len = io_u->buflen;
556 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
558 if (td->verify == VERIFY_MD5) {
559 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
560 hdr.verify_type = VERIFY_MD5;
562 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
563 hdr.verify_type = VERIFY_CRC32;
566 memcpy(io_u->buf, &hdr, sizeof(hdr));
569 static void put_io_u(struct thread_data *td, struct io_u *io_u)
571 list_del(&io_u->list);
572 list_add(&io_u->list, &td->io_u_freelist);
576 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
578 static struct io_u *__get_io_u(struct thread_data *td)
585 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
588 list_del(&io_u->list);
589 list_add(&io_u->list, &td->io_u_busylist);
594 static int td_io_prep(struct thread_data *td, struct io_u *io_u, int read)
597 io_u->ddir = DDIR_READ;
599 io_u->ddir = DDIR_WRITE;
601 if (td->io_prep && td->io_prep(td, io_u))
607 static struct io_u *get_io_u(struct thread_data *td)
611 io_u = __get_io_u(td);
615 if (get_next_offset(td, &io_u->offset)) {
620 io_u->buflen = get_next_buflen(td);
626 if (io_u->buflen + io_u->offset > td->file_size)
627 io_u->buflen = td->file_size - io_u->offset;
635 mark_random_map(td, io_u);
637 td->last_bytes += io_u->buflen;
639 if (td->verify != VERIFY_NONE)
640 populate_io_u(td, io_u);
642 if (td_io_prep(td, io_u, td_read(td))) {
647 gettimeofday(&io_u->start_time, NULL);
651 static inline void td_set_runstate(struct thread_data *td, int runstate)
653 td->old_runstate = td->runstate;
654 td->runstate = runstate;
657 static int get_next_verify(struct thread_data *td,
658 unsigned long long *offset, unsigned int *len)
660 struct io_piece *ipo;
662 if (list_empty(&td->io_hist_list))
665 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
666 list_del(&ipo->list);
668 *offset = ipo->offset;
674 static void prune_io_piece_log(struct thread_data *td)
676 struct io_piece *ipo;
678 while (!list_empty(&td->io_hist_list)) {
679 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
681 list_del(&ipo->list);
687 * log a succesful write, so we can unwind the log for verify
689 static void log_io_piece(struct thread_data *td, struct io_u *io_u)
691 struct io_piece *ipo = malloc(sizeof(struct io_piece));
692 struct list_head *entry;
694 INIT_LIST_HEAD(&ipo->list);
695 ipo->offset = io_u->offset;
696 ipo->len = io_u->buflen;
699 * for random io where the writes extend the file, it will typically
700 * be laid out with the block scattered as written. it's faster to
701 * read them in in that order again, so don't sort
703 if (td->sequential || !td->overwrite) {
704 list_add_tail(&ipo->list, &td->io_hist_list);
709 * for random io, sort the list so verify will run faster
711 entry = &td->io_hist_list;
712 while ((entry = entry->prev) != &td->io_hist_list) {
713 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
715 if (__ipo->offset < ipo->offset)
719 list_add(&ipo->list, entry);
722 static int sync_td(struct thread_data *td)
725 return td->io_sync(td);
730 static int io_u_getevents(struct thread_data *td, int min, int max,
733 return td->io_getevents(td, min, max, t);
736 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
738 gettimeofday(&io_u->issue_time, NULL);
740 return td->io_queue(td, io_u);
743 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
745 static void io_completed(struct thread_data *td, struct io_u *io_u,
746 struct io_completion_data *icd)
751 gettimeofday(&e, NULL);
754 int idx = io_u->ddir;
756 td->io_blocks[idx]++;
757 td->io_bytes[idx] += (io_u->buflen - io_u->resid);
758 td->this_io_bytes[idx] += (io_u->buflen - io_u->resid);
760 msec = mtime_since(&io_u->issue_time, &e);
762 add_clat_sample(td, io_u->ddir, msec);
763 add_bw_sample(td, io_u->ddir);
765 if (td_write(td) && io_u->ddir == DDIR_WRITE)
766 log_io_piece(td, io_u);
768 icd->bytes_done[idx] += (io_u->buflen - io_u->resid);
770 icd->error = io_u->error;
773 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
779 icd->bytes_done[0] = icd->bytes_done[1] = 0;
781 for (i = 0; i < icd->nr; i++) {
782 io_u = td->io_event(td, i);
784 io_completed(td, io_u, icd);
789 static void cleanup_pending_aio(struct thread_data *td)
791 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
792 struct list_head *entry, *n;
793 struct io_completion_data icd;
798 * get immediately available events, if any
800 r = io_u_getevents(td, 0, td->cur_depth, &ts);
803 ios_completed(td, &icd);
807 * now cancel remaining active events
810 list_for_each_safe(entry, n, &td->io_u_busylist) {
811 io_u = list_entry(entry, struct io_u, list);
813 r = td->io_cancel(td, io_u);
820 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
823 ios_completed(td, &icd);
828 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
830 struct io_u *v_io_u = *io_u;
834 ret = verify_io_u(v_io_u);
835 put_io_u(td, v_io_u);
842 static void do_verify(struct thread_data *td)
845 struct io_u *io_u, *v_io_u = NULL;
846 struct io_completion_data icd;
849 td_set_runstate(td, TD_VERIFYING);
855 gettimeofday(&t, NULL);
856 if (runtime_exceeded(td, &t))
859 io_u = __get_io_u(td);
863 if (get_next_verify(td, &io_u->offset, &io_u->buflen)) {
868 if (td_io_prep(td, io_u, 1)) {
873 ret = io_u_queue(td, io_u);
881 * we have one pending to verify, do that while
882 * we are doing io on the next one
884 if (do_io_u_verify(td, &v_io_u))
887 ret = io_u_getevents(td, 1, 1, NULL);
894 v_io_u = td->io_event(td, 0);
897 io_completed(td, v_io_u, &icd);
900 td_verror(td, icd.error);
901 put_io_u(td, v_io_u);
907 * if we can't submit more io, we need to verify now
909 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
914 do_io_u_verify(td, &v_io_u);
917 cleanup_pending_aio(td);
919 td_set_runstate(td, TD_RUNNING);
922 static void do_io(struct thread_data *td)
924 struct io_completion_data icd;
928 while (td->this_io_bytes[td->ddir] < td->io_size) {
929 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
930 struct timespec *timeout;
931 int ret, min_evts = 0;
941 memcpy(&s, &io_u->start_time, sizeof(s));
943 ret = io_u_queue(td, io_u);
950 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
952 if (td->cur_depth < td->iodepth) {
960 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
968 ios_completed(td, &icd);
970 td_verror(td, icd.error);
975 * the rate is batched for now, it should work for batches
976 * of completions except the very first one which may look
979 gettimeofday(&e, NULL);
980 usec = utime_since(&s, &e);
982 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
984 if (check_min_rate(td, &e)) {
985 td_verror(td, ENOMEM);
989 if (runtime_exceeded(td, &e))
993 usec_sleep(td, td->thinktime);
995 if (should_fsync(td) && td->fsync_blocks &&
996 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
1001 cleanup_pending_aio(td);
1003 if (should_fsync(td))
1007 static void cleanup_io(struct thread_data *td)
1013 static int init_io(struct thread_data *td)
1015 if (td->io_engine == FIO_SYNCIO)
1016 return fio_syncio_init(td);
1017 else if (td->io_engine == FIO_MMAPIO)
1018 return fio_mmapio_init(td);
1019 else if (td->io_engine == FIO_LIBAIO)
1020 return fio_libaio_init(td);
1021 else if (td->io_engine == FIO_POSIXAIO)
1022 return fio_posixaio_init(td);
1023 else if (td->io_engine == FIO_SGIO)
1024 return fio_sgio_init(td);
1026 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1031 static void cleanup_io_u(struct thread_data *td)
1033 struct list_head *entry, *n;
1036 list_for_each_safe(entry, n, &td->io_u_freelist) {
1037 io_u = list_entry(entry, struct io_u, list);
1039 list_del(&io_u->list);
1043 if (td->mem_type == MEM_MALLOC)
1044 free(td->orig_buffer);
1045 else if (td->mem_type == MEM_SHM) {
1046 struct shmid_ds sbuf;
1048 shmdt(td->orig_buffer);
1049 shmctl(td->shm_id, IPC_RMID, &sbuf);
1050 } else if (td->mem_type == MEM_MMAP)
1051 munmap(td->orig_buffer, td->orig_buffer_size);
1053 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1055 td->orig_buffer = NULL;
1058 static int init_io_u(struct thread_data *td)
1064 if (td->io_engine & FIO_SYNCIO)
1067 max_units = td->iodepth;
1069 td->orig_buffer_size = td->max_bs * max_units + MASK;
1071 if (td->mem_type == MEM_MALLOC)
1072 td->orig_buffer = malloc(td->orig_buffer_size);
1073 else if (td->mem_type == MEM_SHM) {
1074 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1075 if (td->shm_id < 0) {
1076 td_verror(td, errno);
1081 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1082 if (td->orig_buffer == (void *) -1) {
1083 td_verror(td, errno);
1085 td->orig_buffer = NULL;
1088 } else if (td->mem_type == MEM_MMAP) {
1089 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1090 if (td->orig_buffer == MAP_FAILED) {
1091 td_verror(td, errno);
1093 td->orig_buffer = NULL;
1098 INIT_LIST_HEAD(&td->io_u_freelist);
1099 INIT_LIST_HEAD(&td->io_u_busylist);
1100 INIT_LIST_HEAD(&td->io_hist_list);
1102 p = ALIGN(td->orig_buffer);
1103 for (i = 0; i < max_units; i++) {
1104 io_u = malloc(sizeof(*io_u));
1105 memset(io_u, 0, sizeof(*io_u));
1106 INIT_LIST_HEAD(&io_u->list);
1108 io_u->buf = p + td->max_bs * i;
1109 list_add(&io_u->list, &td->io_u_freelist);
1115 static int create_file(struct thread_data *td, unsigned long long size,
1118 unsigned long long left;
1124 * unless specifically asked for overwrite, let normal io extend it
1126 if (td_write(td) && !td->overwrite)
1130 fprintf(stderr, "Need size for create\n");
1131 td_verror(td, EINVAL);
1136 oflags = O_CREAT | O_TRUNC;
1137 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1140 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1143 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1145 td_verror(td, errno);
1149 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1150 td_verror(td, errno);
1154 td->io_size = td->file_size;
1155 b = malloc(td->max_bs);
1156 memset(b, 0, td->max_bs);
1159 while (left && !td->terminate) {
1164 r = write(td->fd, b, bs);
1166 if (r == (int) bs) {
1171 td_verror(td, errno);
1180 unlink(td->file_name);
1181 else if (td->create_fsync)
1190 static int file_size(struct thread_data *td)
1194 if (fstat(td->fd, &st) == -1) {
1195 td_verror(td, errno);
1200 td->file_size = st.st_size;
1205 static int bdev_size(struct thread_data *td)
1210 r = blockdev_size(td->fd, &bytes);
1217 * no extend possibilities, so limit size to device size if too large
1219 if (!td->file_size || td->file_size > bytes)
1220 td->file_size = bytes;
1225 static int get_file_size(struct thread_data *td)
1229 if (td->filetype == FIO_TYPE_FILE)
1230 ret = file_size(td);
1232 ret = bdev_size(td);
1237 if (td->file_offset > td->file_size) {
1238 fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
1242 td->io_size = td->file_size - td->file_offset;
1243 if (td->io_size == 0) {
1244 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1245 td_verror(td, EINVAL);
1249 td->total_io_size = td->io_size * td->loops;
1253 static int setup_file_mmap(struct thread_data *td)
1262 if (td->verify != VERIFY_NONE)
1266 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1267 if (td->mmap == MAP_FAILED) {
1269 td_verror(td, errno);
1273 if (td->invalidate_cache) {
1274 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1275 td_verror(td, errno);
1280 if (td->sequential) {
1281 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1282 td_verror(td, errno);
1286 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1287 td_verror(td, errno);
1295 static int setup_file_plain(struct thread_data *td)
1297 if (td->invalidate_cache) {
1298 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1299 td_verror(td, errno);
1304 if (td->sequential) {
1305 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1306 td_verror(td, errno);
1310 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1311 td_verror(td, errno);
1319 static int setup_file(struct thread_data *td)
1324 if (stat(td->file_name, &st) == -1) {
1325 if (errno != ENOENT) {
1326 td_verror(td, errno);
1329 if (!td->create_file) {
1330 td_verror(td, ENOENT);
1333 if (create_file(td, td->file_size, 0))
1335 } else if (td->filetype == FIO_TYPE_FILE) {
1336 if (st.st_size < td->file_size) {
1337 if (create_file(td, td->file_size - st.st_size, 1))
1346 td->fd = open(td->file_name, flags | O_RDONLY);
1348 if (td->filetype == FIO_TYPE_FILE) {
1359 td->fd = open(td->file_name, flags, 0600);
1363 td_verror(td, errno);
1367 if (get_file_size(td))
1370 if (td->io_engine != FIO_MMAPIO)
1371 return setup_file_plain(td);
1373 return setup_file_mmap(td);
1376 static int check_dev_match(dev_t dev, char *path)
1378 unsigned int major, minor;
1382 f = fopen(path, "r");
1384 perror("open path");
1388 p = fgets(line, sizeof(line), f);
1394 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1399 if (((major << 8) | minor) == dev) {
1408 static int find_block_dir(dev_t dev, char *path)
1419 while ((dir = readdir(D)) != NULL) {
1420 char full_path[256];
1422 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1424 if (!strcmp(dir->d_name, "device"))
1427 sprintf(full_path, "%s/%s", path, dir->d_name);
1429 if (!strcmp(dir->d_name, "dev")) {
1430 if (!check_dev_match(dev, full_path)) {
1436 if (stat(full_path, &st) == -1) {
1441 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1444 found = find_block_dir(dev, full_path);
1446 strcpy(path, full_path);
1455 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1462 f = fopen(du->path, "r");
1466 p = fgets(line, sizeof(line), f);
1472 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) {
1481 static void update_io_tick_disk(struct disk_util *du)
1483 struct disk_util_stat __dus, *dus, *ldus;
1486 if (get_io_ticks(du, &__dus))
1490 ldus = &du->last_dus;
1492 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1493 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1494 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1495 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1496 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1497 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1498 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1499 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1500 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1501 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1503 gettimeofday(&t, NULL);
1504 du->msec += mtime_since(&du->time, &t);
1505 memcpy(&du->time, &t, sizeof(t));
1506 memcpy(ldus, &__dus, sizeof(__dus));
1509 static void update_io_ticks(void)
1511 struct list_head *entry;
1512 struct disk_util *du;
1514 list_for_each(entry, &disk_list) {
1515 du = list_entry(entry, struct disk_util, list);
1516 update_io_tick_disk(du);
1520 static int disk_util_exists(dev_t dev)
1522 struct list_head *entry;
1523 struct disk_util *du;
1525 list_for_each(entry, &disk_list) {
1526 du = list_entry(entry, struct disk_util, list);
1535 static void disk_util_add(dev_t dev, char *path)
1537 struct disk_util *du = malloc(sizeof(*du));
1539 memset(du, 0, sizeof(*du));
1540 INIT_LIST_HEAD(&du->list);
1541 sprintf(du->path, "%s/stat", path);
1542 du->name = strdup(basename(path));
1545 gettimeofday(&du->time, NULL);
1546 get_io_ticks(du, &du->last_dus);
1548 list_add_tail(&du->list, &disk_list);
1551 static void init_disk_util(struct thread_data *td)
1554 char foo[256], tmp[256];
1558 if (!td->do_disk_util)
1561 if (!stat(td->file_name, &st)) {
1562 if (S_ISBLK(st.st_mode))
1568 * must be a file, open "." in that path
1570 strcpy(foo, td->file_name);
1573 perror("disk util stat");
1580 if (disk_util_exists(dev))
1583 sprintf(foo, "/sys/block");
1584 if (!find_block_dir(dev, foo))
1588 * if this is inside a partition dir, jump back to parent
1590 sprintf(tmp, "%s/queue", foo);
1591 if (stat(tmp, &st)) {
1593 sprintf(tmp, "%s/queue", p);
1594 if (stat(tmp, &st)) {
1595 fprintf(stderr, "unknown sysfs layout\n");
1598 sprintf(foo, "%s", p);
1601 disk_util_add(dev, foo);
1604 static void disk_util_timer_arm(void)
1606 itimer.it_value.tv_sec = 0;
1607 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1608 setitimer(ITIMER_REAL, &itimer, NULL);
1611 static void clear_io_state(struct thread_data *td)
1613 if (td->io_engine == FIO_SYNCIO)
1614 lseek(td->fd, SEEK_SET, 0);
1617 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1618 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1621 memset(td->file_map, 0, td->num_maps * sizeof(long));
1624 static void update_rusage_stat(struct thread_data *td)
1626 if (!(td->runtime[0] + td->runtime[1]))
1629 getrusage(RUSAGE_SELF, &td->ru_end);
1631 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1632 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1633 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1636 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1639 static void *thread_main(void *data)
1641 struct thread_data *td = data;
1644 if (!td->use_thread)
1652 if (fio_setaffinity(td) == -1) {
1653 td_verror(td, errno);
1661 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1662 td_verror(td, errno);
1667 sem_post(&startup_sem);
1668 sem_wait(&td->mutex);
1670 if (!td->create_serialize && setup_file(td))
1673 if (init_random_state(td))
1676 gettimeofday(&td->epoch, NULL);
1678 while (td->loops--) {
1679 getrusage(RUSAGE_SELF, &td->ru_start);
1680 gettimeofday(&td->start, NULL);
1681 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1684 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1687 prune_io_piece_log(td);
1691 td->runtime[td->ddir] += mtime_since_now(&td->start);
1692 update_rusage_stat(td);
1694 if (td->error || td->terminate)
1697 if (td->verify == VERIFY_NONE)
1701 gettimeofday(&td->start, NULL);
1705 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1707 if (td->error || td->terminate)
1714 finish_log(td, td->bw_log, "bw");
1716 finish_log(td, td->slat_log, "slat");
1718 finish_log(td, td->clat_log, "clat");
1720 if (exitall_on_terminate)
1721 terminate_threads(td->groupid);
1729 munmap(td->mmap, td->file_size);
1733 sem_post(&startup_sem);
1734 sem_wait(&td->mutex);
1736 td_set_runstate(td, TD_EXITED);
1741 static void *fork_main(int shmid, int offset)
1743 struct thread_data *td;
1746 data = shmat(shmid, NULL, 0);
1747 if (data == (void *) -1) {
1752 td = data + offset * sizeof(struct thread_data);
1758 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1759 double *mean, double *dev)
1763 if (is->samples == 0)
1769 n = (double) is->samples;
1770 *mean = (double) is->val / n;
1771 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1772 if (!(*min + *max) && !(*mean + *dev))
1778 static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
1781 char *ddir_str[] = { "read ", "write" };
1782 unsigned long min, max, bw;
1785 if (!td->runtime[ddir])
1788 bw = td->io_bytes[ddir] / td->runtime[ddir];
1789 printf(" %s: io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
1791 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
1792 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1794 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
1795 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1797 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
1800 p_of_agg = mean * 100 / (double) rs->agg[ddir];
1801 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);
1805 static void show_thread_status(struct thread_data *td,
1806 struct group_run_stats *rs)
1808 double usr_cpu, sys_cpu;
1810 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
1813 printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error);
1815 show_ddir_status(td, rs, td->ddir);
1816 show_ddir_status(td, rs, td->ddir ^ 1);
1818 if (td->runtime[0] + td->runtime[1]) {
1819 double runt = td->runtime[0] + td->runtime[1];
1821 usr_cpu = (double) td->usr_time * 100 / runt;
1822 sys_cpu = (double) td->sys_time * 100 / runt;
1828 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
1831 static void check_str_update(struct thread_data *td)
1833 char c = run_str[td->thread_number - 1];
1835 if (td->runstate == td->old_runstate)
1838 switch (td->runstate) {
1864 case TD_NOT_CREATED:
1868 printf("state %d\n", td->runstate);
1871 run_str[td->thread_number - 1] = c;
1872 td->old_runstate = td->runstate;
1875 static void eta_to_str(char *str, int eta_sec)
1877 unsigned int d, h, m, s;
1878 static int always_d, always_h;
1890 if (d || always_d) {
1892 str += sprintf(str, "%02dd:", d);
1894 if (h || always_h) {
1896 str += sprintf(str, "%02dh:", h);
1899 str += sprintf(str, "%02dm:", m);
1900 str += sprintf(str, "%02ds", s);
1903 static void print_thread_status(void)
1905 unsigned long long bytes_done, bytes_total;
1906 int i, nr_running, t_rate, m_rate, eta_sec;
1910 bytes_done = bytes_total = 0;
1911 nr_running = t_rate = m_rate = 0;
1912 for (i = 0; i < thread_number; i++) {
1913 struct thread_data *td = &threads[i];
1915 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1918 m_rate += td->ratemin;
1921 bytes_total += td->total_io_size;
1923 bytes_total += td->total_io_size;
1925 bytes_done += td->io_bytes[DDIR_READ] +td->io_bytes[DDIR_WRITE];
1927 check_str_update(td);
1932 if (bytes_total && bytes_done) {
1933 unsigned long runtime;
1935 perc = (double) bytes_done / (double) bytes_total;
1939 runtime = time_since_now(&genesis);
1941 memset(eta_str, 0, sizeof(eta_str));
1942 eta_sec = (runtime * (1.0 / perc)) - runtime;
1943 eta_to_str(eta_str, eta_sec);
1949 printf("Threads now running (%d)", nr_running);
1950 if (m_rate || t_rate)
1951 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1952 printf(": [%s] [%3.2f%% done]", run_str, perc);
1954 printf(" [eta %s]", eta_str);
1959 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1964 * reap exited threads (TD_EXITED -> TD_REAPED)
1966 for (i = 0; i < thread_number; i++) {
1967 struct thread_data *td = &threads[i];
1969 if (td->runstate != TD_EXITED)
1972 td_set_runstate(td, TD_REAPED);
1974 if (td->use_thread) {
1977 if (pthread_join(td->thread, (void *) &ret))
1978 perror("thread_join");
1980 waitpid(td->pid, NULL, 0);
1983 (*m_rate) -= td->ratemin;
1984 (*t_rate) -= td->rate;
1988 static void run_threads(void)
1990 struct thread_data *td;
1991 unsigned long spent;
1992 int i, todo, nr_running, m_rate, t_rate, nr_started;
1994 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1997 signal(SIGINT, sig_handler);
1998 signal(SIGALRM, sig_handler);
2000 todo = thread_number;
2003 m_rate = t_rate = 0;
2005 for (i = 0; i < thread_number; i++) {
2008 run_str[td->thread_number - 1] = 'P';
2012 if (!td->create_serialize)
2016 * do file setup here so it happens sequentially,
2017 * we don't want X number of threads getting their
2018 * client data interspersed on disk
2020 if (setup_file(td)) {
2021 td_set_runstate(td, TD_REAPED);
2026 gettimeofday(&genesis, NULL);
2030 * create threads (TD_NOT_CREATED -> TD_CREATED)
2032 for (i = 0; i < thread_number; i++) {
2035 if (td->runstate != TD_NOT_CREATED)
2039 * never got a chance to start, killed by other
2040 * thread for some reason
2042 if (td->terminate) {
2047 if (td->start_delay) {
2048 spent = mtime_since_now(&genesis);
2050 if (td->start_delay * 1000 > spent)
2054 if (td->stonewall && (nr_started || nr_running))
2057 td_set_runstate(td, TD_CREATED);
2058 sem_init(&startup_sem, 0, 1);
2062 if (td->use_thread) {
2063 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2064 perror("thread_create");
2069 sem_wait(&startup_sem);
2071 fork_main(shm_id, i);
2078 * start created threads (TD_CREATED -> TD_RUNNING)
2080 for (i = 0; i < thread_number; i++) {
2083 if (td->runstate != TD_CREATED)
2086 td_set_runstate(td, TD_RUNNING);
2089 m_rate += td->ratemin;
2091 sem_post(&td->mutex);
2094 reap_threads(&nr_running, &t_rate, &m_rate);
2100 while (nr_running) {
2101 reap_threads(&nr_running, &t_rate, &m_rate);
2108 static void show_group_stats(struct group_run_stats *rs, int id)
2110 printf("\nRun status group %d (all jobs):\n", id);
2112 if (rs->max_run[DDIR_READ])
2113 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]);
2114 if (rs->max_run[DDIR_WRITE])
2115 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]);
2118 static void show_disk_util(void)
2120 struct disk_util_stat *dus;
2121 struct list_head *entry;
2122 struct disk_util *du;
2125 printf("\nDisk stats (read/write):\n");
2127 list_for_each(entry, &disk_list) {
2128 du = list_entry(entry, struct disk_util, list);
2131 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2135 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);
2139 static void show_run_stats(void)
2141 struct group_run_stats *runstats, *rs;
2142 struct thread_data *td;
2145 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2147 for (i = 0; i < groupid + 1; i++) {
2150 memset(rs, 0, sizeof(*rs));
2151 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2152 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2155 for (i = 0; i < thread_number; i++) {
2156 unsigned long rbw, wbw;
2161 printf("Client%d: %s\n", td->thread_number, td->verror);
2165 rs = &runstats[td->groupid];
2167 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
2168 rs->min_run[0] = td->runtime[0];
2169 if (td->runtime[0] > rs->max_run[0])
2170 rs->max_run[0] = td->runtime[0];
2171 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
2172 rs->min_run[1] = td->runtime[1];
2173 if (td->runtime[1] > rs->max_run[1])
2174 rs->max_run[1] = td->runtime[1];
2178 rbw = td->io_bytes[0] / td->runtime[0];
2180 wbw = td->io_bytes[1] / td->runtime[1];
2182 if (rbw < rs->min_bw[0])
2183 rs->min_bw[0] = rbw;
2184 if (wbw < rs->min_bw[1])
2185 rs->min_bw[1] = wbw;
2186 if (rbw > rs->max_bw[0])
2187 rs->max_bw[0] = rbw;
2188 if (wbw > rs->max_bw[1])
2189 rs->max_bw[1] = wbw;
2191 rs->io_mb[0] += td->io_bytes[0] >> 20;
2192 rs->io_mb[1] += td->io_bytes[1] >> 20;
2195 for (i = 0; i < groupid + 1; i++) {
2199 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2201 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2205 * don't overwrite last signal output
2209 for (i = 0; i < thread_number; i++) {
2211 rs = &runstats[td->groupid];
2213 show_thread_status(td, rs);
2216 for (i = 0; i < groupid + 1; i++)
2217 show_group_stats(&runstats[i], i);
2222 int main(int argc, char *argv[])
2224 if (parse_options(argc, argv))
2227 if (!thread_number) {
2228 printf("Nothing to do\n");
2232 disk_util_timer_arm();