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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/ioctl.h>
40 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43 int thread_number = 0;
46 char *fio_inst_prefix = _INST_PREFIX;
48 static volatile int startup_sem;
50 #define TERMINATE_ALL (-1)
51 #define JOB_START_TIMEOUT (5 * 1000)
53 static void terminate_threads(int group_id)
55 struct thread_data *td;
59 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
66 static void sig_handler(int sig)
71 disk_util_timer_arm();
72 print_thread_status();
75 printf("\nfio: terminating on signal\n");
77 terminate_threads(TERMINATE_ALL);
83 * Check if we are above the minimum rate given.
85 static int check_min_rate(struct thread_data *td, struct timeval *now)
92 * allow a 2 second settle period in the beginning
94 if (mtime_since(&td->start, now) < 2000)
98 * if rate blocks is set, sample is running
100 if (td->rate_bytes) {
101 spent = mtime_since(&td->lastrate, now);
102 if (spent < td->ratecycle)
105 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
106 if (rate < td->ratemin) {
107 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
112 td->rate_bytes = td->this_io_bytes[ddir];
113 memcpy(&td->lastrate, now, sizeof(*now));
117 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
121 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
127 static inline void td_set_runstate(struct thread_data *td, int runstate)
129 td->runstate = runstate;
132 static struct fio_file *get_next_file(struct thread_data *td)
134 unsigned int old_next_file = td->next_file;
138 f = &td->files[td->next_file];
141 if (td->next_file >= td->nr_files)
148 } while (td->next_file != old_next_file);
154 * When job exits, we can cancel the in-flight IO if we are using async
155 * io. Attempt to do so.
157 static void cleanup_pending_aio(struct thread_data *td)
159 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
160 struct list_head *entry, *n;
161 struct io_completion_data icd;
166 * get immediately available events, if any
168 r = td_io_getevents(td, 0, td->cur_depth, &ts);
171 ios_completed(td, &icd);
175 * now cancel remaining active events
177 if (td->io_ops->cancel) {
178 list_for_each_safe(entry, n, &td->io_u_busylist) {
179 io_u = list_entry(entry, struct io_u, list);
181 r = td->io_ops->cancel(td, io_u);
188 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
191 ios_completed(td, &icd);
197 * Helper to handle the final sync of a file. Works just like the normal
198 * io path, just does everything sync.
200 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
202 struct io_u *io_u = __get_io_u(td);
203 struct io_completion_data icd;
209 io_u->ddir = DDIR_SYNC;
212 if (td_io_prep(td, io_u)) {
217 ret = td_io_queue(td, io_u);
224 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
231 ios_completed(td, &icd);
233 td_verror(td, icd.error);
241 * The main verify engine. Runs over the writes we previusly submitted,
242 * reads the blocks back in, and checks the crc/md5 of the data.
244 void do_verify(struct thread_data *td)
247 struct io_u *io_u, *v_io_u = NULL;
248 struct io_completion_data icd;
253 * sync io first and invalidate cache, to make sure we really
256 for_each_file(td, f, i) {
258 file_invalidate_cache(td, f);
261 td_set_runstate(td, TD_VERIFYING);
267 gettimeofday(&t, NULL);
268 if (runtime_exceeded(td, &t))
271 io_u = __get_io_u(td);
275 if (get_next_verify(td, io_u)) {
280 f = get_next_file(td);
286 if (td_io_prep(td, io_u)) {
291 ret = td_io_queue(td, io_u);
299 * we have one pending to verify, do that while
300 * we are doing io on the next one
302 if (do_io_u_verify(td, &v_io_u))
305 ret = td_io_getevents(td, 1, 1, NULL);
312 v_io_u = td->io_ops->event(td, 0);
315 io_completed(td, v_io_u, &icd);
318 td_verror(td, icd.error);
319 put_io_u(td, v_io_u);
325 * if we can't submit more io, we need to verify now
327 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
332 do_io_u_verify(td, &v_io_u);
335 cleanup_pending_aio(td);
337 td_set_runstate(td, TD_RUNNING);
341 * Not really an io thread, all it does is burn CPU cycles in the specified
344 static void do_cpuio(struct thread_data *td)
347 int split = 100 / td->cpuload;
350 while (!td->terminate) {
351 gettimeofday(&e, NULL);
353 if (runtime_exceeded(td, &e))
359 usec_sleep(td, 10000);
366 * Main IO worker function. It retrieves io_u's to process and queues
367 * and reaps them, checking for rate and errors along the way.
369 static void do_io(struct thread_data *td)
371 struct io_completion_data icd;
377 td_set_runstate(td, TD_RUNNING);
379 while (td->this_io_bytes[td->ddir] < td->io_size) {
380 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
381 struct timespec *timeout;
388 f = get_next_file(td);
392 io_u = get_io_u(td, f);
396 memcpy(&s, &io_u->start_time, sizeof(s));
398 ret = td_io_queue(td, io_u);
405 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
407 if (td->cur_depth < td->iodepth) {
415 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
423 ios_completed(td, &icd);
425 td_verror(td, icd.error);
430 * the rate is batched for now, it should work for batches
431 * of completions except the very first one which may look
434 gettimeofday(&e, NULL);
435 usec = utime_since(&s, &e);
437 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
439 if (check_min_rate(td, &e)) {
441 terminate_threads(td->groupid);
442 td_verror(td, ENOMEM);
446 if (runtime_exceeded(td, &e))
450 usec_sleep(td, td->thinktime);
455 cleanup_pending_aio(td);
457 if (should_fsync(td) && td->end_fsync) {
458 td_set_runstate(td, TD_FSYNCING);
459 for_each_file(td, f, i)
465 static void cleanup_io_u(struct thread_data *td)
467 struct list_head *entry, *n;
470 list_for_each_safe(entry, n, &td->io_u_freelist) {
471 io_u = list_entry(entry, struct io_u, list);
473 list_del(&io_u->list);
480 static int init_io_u(struct thread_data *td)
486 if (td->io_ops->flags & FIO_CPUIO)
489 if (td->io_ops->flags & FIO_SYNCIO)
492 max_units = td->iodepth;
494 td->orig_buffer_size = td->max_bs * max_units + MASK;
496 if (allocate_io_mem(td))
499 p = ALIGN(td->orig_buffer);
500 for (i = 0; i < max_units; i++) {
501 io_u = malloc(sizeof(*io_u));
502 memset(io_u, 0, sizeof(*io_u));
503 INIT_LIST_HEAD(&io_u->list);
505 io_u->buf = p + td->max_bs * i;
507 list_add(&io_u->list, &td->io_u_freelist);
513 static int switch_ioscheduler(struct thread_data *td)
515 char tmp[256], tmp2[128];
519 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
521 f = fopen(tmp, "r+");
523 td_verror(td, errno);
530 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
531 if (ferror(f) || ret != 1) {
532 td_verror(td, errno);
540 * Read back and check that the selected scheduler is now the default.
542 ret = fread(tmp, 1, sizeof(tmp), f);
543 if (ferror(f) || ret < 0) {
544 td_verror(td, errno);
549 sprintf(tmp2, "[%s]", td->ioscheduler);
550 if (!strstr(tmp, tmp2)) {
551 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
552 td_verror(td, EINVAL);
561 static void clear_io_state(struct thread_data *td)
566 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
567 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
570 for_each_file(td, f, i) {
572 if (td->io_ops->flags & FIO_SYNCIO)
573 lseek(f->fd, SEEK_SET, 0);
576 memset(f->file_map, 0, f->num_maps * sizeof(long));
581 * Entry point for the thread based jobs. The process based jobs end up
582 * here as well, after a little setup.
584 static void *thread_main(void *data)
586 struct thread_data *td = data;
593 INIT_LIST_HEAD(&td->io_u_freelist);
594 INIT_LIST_HEAD(&td->io_u_busylist);
595 INIT_LIST_HEAD(&td->io_hist_list);
596 INIT_LIST_HEAD(&td->io_log_list);
601 if (fio_setaffinity(td) == -1) {
602 td_verror(td, errno);
613 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
614 td_verror(td, errno);
619 if (nice(td->nice) == -1) {
620 td_verror(td, errno);
624 if (init_random_state(td))
627 if (td->ioscheduler && switch_ioscheduler(td))
630 td_set_runstate(td, TD_INITIALIZED);
631 fio_sem_up(&startup_sem);
632 fio_sem_down(&td->mutex);
634 if (!td->create_serialize && setup_files(td))
637 gettimeofday(&td->epoch, NULL);
640 system(td->exec_prerun);
642 while (td->loops--) {
643 getrusage(RUSAGE_SELF, &td->ru_start);
644 gettimeofday(&td->start, NULL);
645 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
648 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
651 prune_io_piece_log(td);
653 if (td->io_ops->flags & FIO_CPUIO)
658 td->runtime[td->ddir] += mtime_since_now(&td->start);
659 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
660 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
662 update_rusage_stat(td);
664 if (td->error || td->terminate)
667 if (td->verify == VERIFY_NONE)
671 gettimeofday(&td->start, NULL);
675 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
677 if (td->error || td->terminate)
682 finish_log(td, td->bw_log, "bw");
684 finish_log(td, td->slat_log, "slat");
686 finish_log(td, td->clat_log, "clat");
688 write_iolog_close(td);
689 if (td->exec_postrun)
690 system(td->exec_postrun);
692 if (exitall_on_terminate)
693 terminate_threads(td->groupid);
699 td_set_runstate(td, TD_EXITED);
705 * We cannot pass the td data into a forked process, so attach the td and
706 * pass it to the thread worker.
708 static void *fork_main(int shmid, int offset)
710 struct thread_data *td;
713 data = shmat(shmid, NULL, 0);
714 if (data == (void *) -1) {
719 td = data + offset * sizeof(struct thread_data);
726 * Run over the job map and reap the threads that have exited, if any.
728 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
730 struct thread_data *td;
734 * reap exited threads (TD_EXITED -> TD_REAPED)
739 * ->io_ops is NULL for a thread that has closed its
742 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
745 if (td->runstate != TD_EXITED)
748 td_set_runstate(td, TD_REAPED);
750 if (td->use_thread) {
753 if (pthread_join(td->thread, (void *) &ret))
754 perror("thread_join");
756 waitpid(td->pid, NULL, 0);
759 (*m_rate) -= td->ratemin;
760 (*t_rate) -= td->rate;
763 if (*nr_running == cputhreads)
764 terminate_threads(TERMINATE_ALL);
768 * Main function for kicking off and reaping jobs, as needed.
770 static void run_threads(void)
772 struct thread_data *td;
774 int i, todo, nr_running, m_rate, t_rate, nr_started;
776 if (fio_pin_memory())
780 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
784 signal(SIGINT, sig_handler);
785 signal(SIGALRM, sig_handler);
787 todo = thread_number;
793 print_status_init(td->thread_number - 1);
797 if (!td->create_serialize)
801 * do file setup here so it happens sequentially,
802 * we don't want X number of threads getting their
803 * client data interspersed on disk
805 if (setup_files(td)) {
806 td_set_runstate(td, TD_REAPED);
814 struct thread_data *map[MAX_JOBS];
815 struct timeval this_start;
816 int this_jobs = 0, left;
819 * create threads (TD_NOT_CREATED -> TD_CREATED)
822 if (td->runstate != TD_NOT_CREATED)
826 * never got a chance to start, killed by other
827 * thread for some reason
834 if (td->start_delay) {
835 spent = mtime_since_genesis();
837 if (td->start_delay * 1000 > spent)
841 if (td->stonewall && (nr_started || nr_running))
845 * Set state to created. Thread will transition
846 * to TD_INITIALIZED when it's done setting up.
848 td_set_runstate(td, TD_CREATED);
849 map[this_jobs++] = td;
850 fio_sem_init(&startup_sem, 1);
853 if (td->use_thread) {
854 if (pthread_create(&td->thread, NULL, thread_main, td)) {
855 perror("thread_create");
860 fio_sem_down(&startup_sem);
862 fork_main(shm_id, i);
869 * Wait for the started threads to transition to
872 gettimeofday(&this_start, NULL);
875 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
880 for (i = 0; i < this_jobs; i++) {
884 if (td->runstate == TD_INITIALIZED) {
887 } else if (td->runstate >= TD_EXITED) {
891 nr_running++; /* work-around... */
897 log_err("fio: %d jobs failed to start\n", left);
898 for (i = 0; i < this_jobs; i++) {
902 kill(td->pid, SIGTERM);
908 * start created threads (TD_INITIALIZED -> TD_RUNNING).
911 if (td->runstate != TD_INITIALIZED)
914 td_set_runstate(td, TD_RUNNING);
917 m_rate += td->ratemin;
920 fio_sem_up(&td->mutex);
923 reap_threads(&nr_running, &t_rate, &m_rate);
930 reap_threads(&nr_running, &t_rate, &m_rate);
938 int main(int argc, char *argv[])
940 if (parse_options(argc, argv))
943 if (!thread_number) {
944 log_err("Nothing to do\n");
948 disk_util_timer_arm();