2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 unsigned long page_mask;
40 unsigned long page_size;
42 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
45 int thread_number = 0;
51 static struct fio_sem *startup_sem;
52 static volatile int fio_abort;
53 static int exit_value;
55 struct io_log *agg_io_log[2];
57 #define TERMINATE_ALL (-1)
58 #define JOB_START_TIMEOUT (5 * 1000)
60 static inline void td_set_runstate(struct thread_data *td, int runstate)
62 td->runstate = runstate;
65 static void terminate_threads(int group_id)
67 struct thread_data *td;
71 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
72 if (td->runstate <= TD_RUNNING)
73 kill(td->pid, SIGQUIT);
75 td->o.start_delay = 0;
80 static void sig_handler(int sig)
85 disk_util_timer_arm();
86 print_thread_status();
89 printf("\nfio: terminating on signal %d\n", sig);
91 terminate_threads(TERMINATE_ALL);
97 * Check if we are above the minimum rate given.
99 static int check_min_rate(struct thread_data *td, struct timeval *now)
101 unsigned long long bytes = 0;
102 unsigned long iops = 0;
107 * No minimum rate set, always ok
109 if (!td->o.ratemin && !td->o.rate_iops_min)
113 * allow a 2 second settle period in the beginning
115 if (mtime_since(&td->start, now) < 2000)
119 iops += td->io_blocks[DDIR_READ];
120 bytes += td->this_io_bytes[DDIR_READ];
123 iops += td->io_blocks[DDIR_WRITE];
124 bytes += td->this_io_bytes[DDIR_WRITE];
128 * if rate blocks is set, sample is running
130 if (td->rate_bytes || td->rate_blocks) {
131 spent = mtime_since(&td->lastrate, now);
132 if (spent < td->o.ratecycle)
137 * check bandwidth specified rate
139 if (bytes < td->rate_bytes) {
140 log_err("%s: min rate %u not met\n", td->o.name, td->o.ratemin);
143 rate = (bytes - td->rate_bytes) / spent;
144 if (rate < td->o.ratemin || bytes < td->rate_bytes) {
145 log_err("%s: min rate %u not met, got %luKiB/sec\n", td->o.name, td->o.ratemin, rate);
151 * checks iops specified rate
153 if (iops < td->o.rate_iops) {
154 log_err("%s: min iops rate %u not met\n", td->o.name, td->o.rate_iops);
157 rate = (iops - td->rate_blocks) / spent;
158 if (rate < td->o.rate_iops_min || iops < td->rate_blocks) {
159 log_err("%s: min iops rate %u not met, got %lu\n", td->o.name, td->o.rate_iops_min, rate);
165 td->rate_bytes = bytes;
166 td->rate_blocks = iops;
167 memcpy(&td->lastrate, now, sizeof(*now));
171 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
175 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
182 * When job exits, we can cancel the in-flight IO if we are using async
183 * io. Attempt to do so.
185 static void cleanup_pending_aio(struct thread_data *td)
187 struct list_head *entry, *n;
192 * get immediately available events, if any
194 r = io_u_queued_complete(td, 0);
199 * now cancel remaining active events
201 if (td->io_ops->cancel) {
202 list_for_each_safe(entry, n, &td->io_u_busylist) {
203 io_u = list_entry(entry, struct io_u, list);
206 * if the io_u isn't in flight, then that generally
207 * means someone leaked an io_u. complain but fix
208 * it up, so we don't stall here.
210 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
211 log_err("fio: non-busy IO on busy list\n");
214 r = td->io_ops->cancel(td, io_u);
222 r = io_u_queued_complete(td, td->cur_depth);
226 * Helper to handle the final sync of a file. Works just like the normal
227 * io path, just does everything sync.
229 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
231 struct io_u *io_u = __get_io_u(td);
237 io_u->ddir = DDIR_SYNC;
240 if (td_io_prep(td, io_u)) {
246 ret = td_io_queue(td, io_u);
248 td_verror(td, io_u->error, "td_io_queue");
251 } else if (ret == FIO_Q_QUEUED) {
252 if (io_u_queued_complete(td, 1) < 0)
254 } else if (ret == FIO_Q_COMPLETED) {
256 td_verror(td, io_u->error, "td_io_queue");
260 if (io_u_sync_complete(td, io_u) < 0)
262 } else if (ret == FIO_Q_BUSY) {
263 if (td_io_commit(td))
272 * The main verify engine. Runs over the writes we previously submitted,
273 * reads the blocks back in, and checks the crc/md5 of the data.
275 static void do_verify(struct thread_data *td)
283 * sync io first and invalidate cache, to make sure we really
286 for_each_file(td, f, i) {
287 if (!(f->flags & FIO_FILE_OPEN))
289 if (fio_io_sync(td, f))
291 if (file_invalidate_cache(td, f))
298 td_set_runstate(td, TD_VERIFYING);
301 while (!td->terminate) {
304 io_u = __get_io_u(td);
308 if (runtime_exceeded(td, &io_u->start_time)) {
313 if (get_next_verify(td, io_u)) {
318 if (td_io_prep(td, io_u)) {
323 io_u->end_io = verify_io_u;
325 ret = td_io_queue(td, io_u);
327 case FIO_Q_COMPLETED:
330 else if (io_u->resid) {
331 int bytes = io_u->xfer_buflen - io_u->resid;
332 struct fio_file *f = io_u->file;
338 td_verror(td, ENODATA, "full resid");
343 io_u->xfer_buflen = io_u->resid;
344 io_u->xfer_buf += bytes;
345 io_u->offset += bytes;
346 f->last_completed_pos = io_u->offset;
348 td->ts.short_io_u[io_u->ddir]++;
350 if (io_u->offset == f->real_file_size)
353 requeue_io_u(td, &io_u);
356 ret = io_u_sync_complete(td, io_u);
364 requeue_io_u(td, &io_u);
365 ret2 = td_io_commit(td);
371 td_verror(td, -ret, "td_io_queue");
375 if (ret < 0 || td->error)
379 * if we can queue more, do so. but check if there are
380 * completed io_u's first.
383 if (queue_full(td) || ret == FIO_Q_BUSY) {
386 if (td->cur_depth > td->o.iodepth_low)
387 min_events = td->cur_depth - td->o.iodepth_low;
391 * Reap required number of io units, if any, and do the
392 * verification on them through the callback handler
394 if (io_u_queued_complete(td, min_events) < 0)
399 min_events = td->cur_depth;
402 ret = io_u_queued_complete(td, min_events);
404 cleanup_pending_aio(td);
406 td_set_runstate(td, TD_RUNNING);
410 * Main IO worker function. It retrieves io_u's to process and queues
411 * and reaps them, checking for rate and errors along the way.
413 static void do_io(struct thread_data *td)
420 td_set_runstate(td, TD_RUNNING);
422 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
423 struct timeval comp_time;
436 memcpy(&s, &io_u->start_time, sizeof(s));
438 if (runtime_exceeded(td, &s)) {
443 ret = td_io_queue(td, io_u);
445 case FIO_Q_COMPLETED:
448 else if (io_u->resid) {
449 int bytes = io_u->xfer_buflen - io_u->resid;
450 struct fio_file *f = io_u->file;
456 td_verror(td, ENODATA, "full resid");
461 io_u->xfer_buflen = io_u->resid;
462 io_u->xfer_buf += bytes;
463 io_u->offset += bytes;
464 f->last_completed_pos = io_u->offset;
466 td->ts.short_io_u[io_u->ddir]++;
468 if (io_u->offset == f->real_file_size)
471 requeue_io_u(td, &io_u);
474 fio_gettime(&comp_time, NULL);
475 bytes_done = io_u_sync_complete(td, io_u);
482 * if the engine doesn't have a commit hook,
483 * the io_u is really queued. if it does have such
484 * a hook, it has to call io_u_queued() itself.
486 if (td->io_ops->commit == NULL)
487 io_u_queued(td, io_u);
490 requeue_io_u(td, &io_u);
491 ret2 = td_io_commit(td);
501 if (ret < 0 || td->error)
505 * See if we need to complete some commands
507 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
509 if (queue_full(td) || ret == FIO_Q_BUSY) {
512 if (td->cur_depth > td->o.iodepth_low)
513 min_evts = td->cur_depth - td->o.iodepth_low;
516 fio_gettime(&comp_time, NULL);
517 bytes_done = io_u_queued_complete(td, min_evts);
526 * the rate is batched for now, it should work for batches
527 * of completions except the very first one which may look
530 usec = utime_since(&s, &comp_time);
532 rate_throttle(td, usec, bytes_done);
534 if (check_min_rate(td, &comp_time)) {
535 if (exitall_on_terminate)
536 terminate_threads(td->groupid);
537 td_verror(td, ENODATA, "check_min_rate");
541 if (td->o.thinktime) {
542 unsigned long long b;
544 b = td->io_blocks[0] + td->io_blocks[1];
545 if (!(b % td->o.thinktime_blocks)) {
548 if (td->o.thinktime_spin)
549 __usec_sleep(td->o.thinktime_spin);
551 left = td->o.thinktime - td->o.thinktime_spin;
553 usec_sleep(td, left);
563 ret = io_u_queued_complete(td, i);
565 if (should_fsync(td) && td->o.end_fsync) {
566 td_set_runstate(td, TD_FSYNCING);
568 for_each_file(td, f, i) {
569 if (!(f->flags & FIO_FILE_OPEN))
575 cleanup_pending_aio(td);
578 static void cleanup_io_u(struct thread_data *td)
580 struct list_head *entry, *n;
583 list_for_each_safe(entry, n, &td->io_u_freelist) {
584 io_u = list_entry(entry, struct io_u, list);
586 list_del(&io_u->list);
594 * "randomly" fill the buffer contents
596 static void fill_rand_buf(struct io_u *io_u, int max_bs)
598 int *ptr = io_u->buf;
600 while ((void *) ptr - io_u->buf < max_bs) {
601 *ptr = rand() * 0x9e370001;
606 static int init_io_u(struct thread_data *td)
608 unsigned long long buf_size;
614 if (td->io_ops->flags & FIO_SYNCIO)
617 max_units = td->o.iodepth;
619 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
620 buf_size = (unsigned long long) max_bs * (unsigned long long) max_units;
621 buf_size += page_mask;
622 if (buf_size != (size_t) buf_size) {
623 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
627 td->orig_buffer_size = buf_size;
629 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE)
630 td->orig_buffer_size = (td->orig_buffer_size + td->o.hugepage_size - 1) & ~(td->o.hugepage_size - 1);
631 else if (td->orig_buffer_size & page_mask)
632 td->orig_buffer_size = (td->orig_buffer_size + page_mask) & ~page_mask;
634 if (allocate_io_mem(td))
637 p = ALIGN(td->orig_buffer);
638 for (i = 0; i < max_units; i++) {
639 io_u = malloc(sizeof(*io_u));
640 memset(io_u, 0, sizeof(*io_u));
641 INIT_LIST_HEAD(&io_u->list);
643 io_u->buf = p + max_bs * i;
644 if (td_write(td) || td_rw(td))
645 fill_rand_buf(io_u, max_bs);
648 io_u->flags = IO_U_F_FREE;
649 list_add(&io_u->list, &td->io_u_freelist);
657 static int switch_ioscheduler(struct thread_data *td)
659 char tmp[256], tmp2[128];
663 if (td->io_ops->flags & FIO_DISKLESSIO)
666 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
668 f = fopen(tmp, "r+");
670 if (errno == ENOENT) {
671 log_err("fio: os or kernel doesn't support IO scheduler switching\n");
674 td_verror(td, errno, "fopen iosched");
681 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
682 if (ferror(f) || ret != 1) {
683 td_verror(td, errno, "fwrite");
691 * Read back and check that the selected scheduler is now the default.
693 ret = fread(tmp, 1, sizeof(tmp), f);
694 if (ferror(f) || ret < 0) {
695 td_verror(td, errno, "fread");
700 sprintf(tmp2, "[%s]", td->o.ioscheduler);
701 if (!strstr(tmp, tmp2)) {
702 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
703 td_verror(td, EINVAL, "iosched_switch");
712 static int clear_io_state(struct thread_data *td)
718 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
719 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
723 td->rw_end_set[0] = td->rw_end_set[1] = 0;
725 td->last_was_sync = 0;
727 for_each_file(td, f, i)
728 td_io_close_file(td, f);
731 for_each_file(td, f, i) {
732 ret = td_io_open_file(td, f);
741 * Entry point for the thread based jobs. The process based jobs end up
742 * here as well, after a little setup.
744 static void *thread_main(void *data)
746 unsigned long long runtime[2];
747 struct thread_data *td = data;
748 unsigned long elapsed;
751 if (!td->o.use_thread)
756 INIT_LIST_HEAD(&td->io_u_freelist);
757 INIT_LIST_HEAD(&td->io_u_busylist);
758 INIT_LIST_HEAD(&td->io_u_requeues);
759 INIT_LIST_HEAD(&td->io_log_list);
760 INIT_LIST_HEAD(&td->io_hist_list);
761 td->io_hist_tree = RB_ROOT;
766 if (fio_setaffinity(td) == -1) {
767 td_verror(td, errno, "cpu_set_affinity");
775 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
776 td_verror(td, errno, "ioprio_set");
781 if (nice(td->o.nice) == -1) {
782 td_verror(td, errno, "nice");
786 if (td->o.ioscheduler && switch_ioscheduler(td))
789 td_set_runstate(td, TD_INITIALIZED);
790 fio_sem_up(startup_sem);
791 fio_sem_down(td->mutex);
794 * the ->mutex semaphore is now no longer used, close it to avoid
795 * eating a file descriptor
797 fio_sem_remove(td->mutex);
799 if (!td->o.create_serialize && setup_files(td))
808 if (init_random_map(td))
811 if (td->o.exec_prerun) {
812 if (system(td->o.exec_prerun) < 0)
816 fio_gettime(&td->epoch, NULL);
817 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
818 getrusage(RUSAGE_SELF, &td->ts.ru_start);
820 runtime[0] = runtime[1] = 0;
822 while (td->o.loops--) {
823 fio_gettime(&td->start, NULL);
824 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
827 memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
829 if (clear_state && clear_io_state(td))
832 prune_io_piece_log(td);
838 if (td_read(td) && td->io_bytes[DDIR_READ]) {
839 if (td->rw_end_set[DDIR_READ])
840 elapsed = utime_since(&td->start, &td->rw_end[DDIR_READ]);
842 elapsed = utime_since_now(&td->start);
844 runtime[DDIR_READ] += elapsed;
846 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
847 if (td->rw_end_set[DDIR_WRITE])
848 elapsed = utime_since(&td->start, &td->rw_end[DDIR_WRITE]);
850 elapsed = utime_since_now(&td->start);
852 runtime[DDIR_WRITE] += elapsed;
855 if (td->error || td->terminate)
858 if (td->o.verify == VERIFY_NONE)
861 if (clear_io_state(td))
864 fio_gettime(&td->start, NULL);
868 runtime[DDIR_READ] += utime_since_now(&td->start);
870 if (td->error || td->terminate)
874 update_rusage_stat(td);
875 td->ts.runtime[0] = runtime[0] / 1000;
876 td->ts.runtime[1] = runtime[1] / 1000;
877 td->ts.total_run_time = mtime_since_now(&td->epoch);
878 td->ts.io_bytes[0] = td->io_bytes[0];
879 td->ts.io_bytes[1] = td->io_bytes[1];
882 finish_log(td, td->ts.bw_log, "bw");
884 finish_log(td, td->ts.slat_log, "slat");
886 finish_log(td, td->ts.clat_log, "clat");
887 if (td->o.write_iolog_file)
888 write_iolog_close(td);
889 if (td->o.exec_postrun) {
890 if (system(td->o.exec_postrun) < 0)
891 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
894 if (exitall_on_terminate)
895 terminate_threads(td->groupid);
899 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
903 options_mem_free(td);
904 td_set_runstate(td, TD_EXITED);
905 return (void *) (unsigned long) td->error;
907 fio_sem_up(startup_sem);
912 * We cannot pass the td data into a forked process, so attach the td and
913 * pass it to the thread worker.
915 static int fork_main(int shmid, int offset)
917 struct thread_data *td;
920 data = shmat(shmid, NULL, 0);
921 if (data == (void *) -1) {
928 td = data + offset * sizeof(struct thread_data);
929 ret = thread_main(td);
931 return (int) (unsigned long) ret;
935 * Run over the job map and reap the threads that have exited, if any.
937 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
939 struct thread_data *td;
940 int i, cputhreads, pending, status, ret;
943 * reap exited threads (TD_EXITED -> TD_REAPED)
945 pending = cputhreads = 0;
950 * ->io_ops is NULL for a thread that has closed its
953 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
956 if (!td->pid || td->runstate == TD_REAPED)
958 if (td->o.use_thread) {
959 if (td->runstate == TD_EXITED) {
960 td_set_runstate(td, TD_REAPED);
967 if (td->runstate == TD_EXITED)
971 * check if someone quit or got killed in an unusual way
973 ret = waitpid(td->pid, &status, flags);
975 if (errno == ECHILD) {
976 log_err("fio: pid=%d disappeared %d\n", td->pid, td->runstate);
977 td_set_runstate(td, TD_REAPED);
981 } else if (ret == td->pid) {
982 if (WIFSIGNALED(status)) {
983 int sig = WTERMSIG(status);
986 log_err("fio: pid=%d, got signal=%d\n", td->pid, sig);
987 td_set_runstate(td, TD_REAPED);
990 if (WIFEXITED(status)) {
991 if (WEXITSTATUS(status) && !td->error)
992 td->error = WEXITSTATUS(status);
994 td_set_runstate(td, TD_REAPED);
1000 * thread is not dead, continue
1005 if (td->o.use_thread) {
1008 if (pthread_join(td->thread, (void *) &ret))
1009 perror("pthread_join");
1013 (*m_rate) -= td->o.ratemin;
1014 (*t_rate) -= td->o.rate;
1021 if (*nr_running == cputhreads && !pending)
1022 terminate_threads(TERMINATE_ALL);
1026 * Main function for kicking off and reaping jobs, as needed.
1028 static void run_threads(void)
1030 struct thread_data *td;
1031 unsigned long spent;
1032 int i, todo, nr_running, m_rate, t_rate, nr_started;
1034 if (fio_pin_memory())
1037 if (!terse_output) {
1038 printf("Starting ");
1040 printf("%d thread%s", nr_thread, nr_thread > 1 ? "s" : "");
1044 printf("%d process%s", nr_process, nr_process > 1 ? "es" : "");
1050 signal(SIGINT, sig_handler);
1051 signal(SIGALRM, sig_handler);
1053 todo = thread_number;
1056 m_rate = t_rate = 0;
1058 for_each_td(td, i) {
1059 print_status_init(td->thread_number - 1);
1061 if (!td->o.create_serialize) {
1067 * do file setup here so it happens sequentially,
1068 * we don't want X number of threads getting their
1069 * client data interspersed on disk
1071 if (setup_files(td)) {
1074 log_err("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
1075 td_set_runstate(td, TD_REAPED);
1085 struct thread_data *map[MAX_JOBS];
1086 struct timeval this_start;
1087 int this_jobs = 0, left;
1090 * create threads (TD_NOT_CREATED -> TD_CREATED)
1092 for_each_td(td, i) {
1093 if (td->runstate != TD_NOT_CREATED)
1097 * never got a chance to start, killed by other
1098 * thread for some reason
1100 if (td->terminate) {
1105 if (td->o.start_delay) {
1106 spent = mtime_since_genesis();
1108 if (td->o.start_delay * 1000 > spent)
1112 if (td->o.stonewall && (nr_started || nr_running))
1116 * Set state to created. Thread will transition
1117 * to TD_INITIALIZED when it's done setting up.
1119 td_set_runstate(td, TD_CREATED);
1120 map[this_jobs++] = td;
1123 if (td->o.use_thread) {
1124 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1125 perror("thread_create");
1131 int ret = fork_main(shm_id, i);
1136 fio_sem_down(startup_sem);
1140 * Wait for the started threads to transition to
1143 fio_gettime(&this_start, NULL);
1145 while (left && !fio_abort) {
1146 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1151 for (i = 0; i < this_jobs; i++) {
1155 if (td->runstate == TD_INITIALIZED) {
1158 } else if (td->runstate >= TD_EXITED) {
1162 nr_running++; /* work-around... */
1168 log_err("fio: %d jobs failed to start\n", left);
1169 for (i = 0; i < this_jobs; i++) {
1173 kill(td->pid, SIGTERM);
1179 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1181 for_each_td(td, i) {
1182 if (td->runstate != TD_INITIALIZED)
1185 td_set_runstate(td, TD_RUNNING);
1188 m_rate += td->o.ratemin;
1189 t_rate += td->o.rate;
1191 fio_sem_up(td->mutex);
1194 reap_threads(&nr_running, &t_rate, &m_rate);
1200 while (nr_running) {
1201 reap_threads(&nr_running, &t_rate, &m_rate);
1209 int main(int argc, char *argv[])
1214 * We need locale for number printing, if it isn't set then just
1215 * go with the US format.
1217 if (!getenv("LC_NUMERIC"))
1218 setlocale(LC_NUMERIC, "en_US");
1220 if (parse_options(argc, argv))
1226 ps = sysconf(_SC_PAGESIZE);
1228 log_err("Failed to get page size\n");
1236 setup_log(&agg_io_log[DDIR_READ]);
1237 setup_log(&agg_io_log[DDIR_WRITE]);
1240 startup_sem = fio_sem_init(0);
1244 disk_util_timer_arm();
1251 __finish_log(agg_io_log[DDIR_READ],"agg-read_bw.log");
1252 __finish_log(agg_io_log[DDIR_WRITE],"agg-write_bw.log");
1256 fio_sem_remove(startup_sem);