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
34 #include <sys/ioctl.h>
42 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
45 int thread_number = 0;
49 static volatile int startup_sem;
51 #define TERMINATE_ALL (-1)
52 #define JOB_START_TIMEOUT (5 * 1000)
54 static void terminate_threads(int group_id)
56 struct thread_data *td;
60 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
67 static void sig_handler(int sig)
72 disk_util_timer_arm();
73 print_thread_status();
76 printf("\nfio: terminating on signal\n");
78 terminate_threads(TERMINATE_ALL);
84 * Check if we are above the minimum rate given.
86 static int check_min_rate(struct thread_data *td, struct timeval *now)
93 * allow a 2 second settle period in the beginning
95 if (mtime_since(&td->start, now) < 2000)
99 * if rate blocks is set, sample is running
101 if (td->rate_bytes) {
102 spent = mtime_since(&td->lastrate, now);
103 if (spent < td->ratecycle)
106 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
107 if (rate < td->ratemin) {
108 fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate);
113 td->rate_bytes = td->this_io_bytes[ddir];
114 memcpy(&td->lastrate, now, sizeof(*now));
118 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
122 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
128 static inline void td_set_runstate(struct thread_data *td, int runstate)
130 td->runstate = runstate;
133 static struct fio_file *get_next_file(struct thread_data *td)
135 unsigned int old_next_file = td->next_file;
139 f = &td->files[td->next_file];
142 if (td->next_file >= td->nr_files)
149 } while (td->next_file != old_next_file);
155 * When job exits, we can cancel the in-flight IO if we are using async
156 * io. Attempt to do so.
158 static void cleanup_pending_aio(struct thread_data *td)
160 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
161 struct list_head *entry, *n;
162 struct io_completion_data icd;
167 * get immediately available events, if any
169 r = td_io_getevents(td, 0, td->cur_depth, &ts);
172 ios_completed(td, &icd);
176 * now cancel remaining active events
178 if (td->io_ops->cancel) {
179 list_for_each_safe(entry, n, &td->io_u_busylist) {
180 io_u = list_entry(entry, struct io_u, list);
182 r = td->io_ops->cancel(td, io_u);
189 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
192 ios_completed(td, &icd);
198 * Helper to handle the final sync of a file. Works just like the normal
199 * io path, just does everything sync.
201 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
203 struct io_u *io_u = __get_io_u(td);
204 struct io_completion_data icd;
210 io_u->ddir = DDIR_SYNC;
213 if (td_io_prep(td, io_u)) {
218 ret = td_io_queue(td, io_u);
220 td_verror(td, io_u->error);
225 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
232 ios_completed(td, &icd);
234 td_verror(td, icd.error);
242 * The main verify engine. Runs over the writes we previusly submitted,
243 * reads the blocks back in, and checks the crc/md5 of the data.
245 static 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 io_u = __get_io_u(td);
271 if (runtime_exceeded(td, &io_u->start_time)) {
276 if (get_next_verify(td, io_u)) {
281 f = get_next_file(td);
287 if (td_io_prep(td, io_u)) {
292 ret = td_io_queue(td, io_u);
294 td_verror(td, io_u->error);
300 * we have one pending to verify, do that while
301 * we are doing io on the next one
303 if (do_io_u_verify(td, &v_io_u))
306 ret = td_io_getevents(td, 1, 1, NULL);
313 v_io_u = td->io_ops->event(td, 0);
316 fio_gettime(&icd.time, NULL);
317 io_completed(td, v_io_u, &icd);
320 td_verror(td, icd.error);
321 put_io_u(td, v_io_u);
327 * if we can't submit more io, we need to verify now
329 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
334 do_io_u_verify(td, &v_io_u);
337 cleanup_pending_aio(td);
339 td_set_runstate(td, TD_RUNNING);
343 * Not really an io thread, all it does is burn CPU cycles in the specified
346 static void do_cpuio(struct thread_data *td)
349 int split = 100 / td->cpuload;
352 while (!td->terminate) {
353 fio_gettime(&e, NULL);
355 if (runtime_exceeded(td, &e))
361 usec_sleep(td, 10000);
368 * Main IO worker function. It retrieves io_u's to process and queues
369 * and reaps them, checking for rate and errors along the way.
371 static void do_io(struct thread_data *td)
373 struct io_completion_data icd;
379 td_set_runstate(td, TD_RUNNING);
381 while (td->this_io_bytes[td->ddir] < td->io_size) {
382 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
383 struct timespec *timeout;
390 f = get_next_file(td);
394 io_u = get_io_u(td, f);
398 memcpy(&s, &io_u->start_time, sizeof(s));
400 ret = td_io_queue(td, io_u);
402 td_verror(td, io_u->error);
407 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
409 if (td->cur_depth < td->iodepth) {
417 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
425 ios_completed(td, &icd);
427 td_verror(td, icd.error);
432 * the rate is batched for now, it should work for batches
433 * of completions except the very first one which may look
436 usec = utime_since(&s, &icd.time);
438 rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir);
440 if (check_min_rate(td, &icd.time)) {
441 if (exitall_on_terminate)
442 terminate_threads(td->groupid);
443 td_verror(td, ENOMEM);
447 if (runtime_exceeded(td, &icd.time))
451 unsigned long long b;
453 b = td->io_blocks[0] + td->io_blocks[1];
454 if (!(td->thinktime_blocks % b))
455 usec_sleep(td, td->thinktime);
461 cleanup_pending_aio(td);
463 if (should_fsync(td) && td->end_fsync) {
464 td_set_runstate(td, TD_FSYNCING);
465 for_each_file(td, f, i)
471 static void cleanup_io_u(struct thread_data *td)
473 struct list_head *entry, *n;
476 list_for_each_safe(entry, n, &td->io_u_freelist) {
477 io_u = list_entry(entry, struct io_u, list);
479 list_del(&io_u->list);
487 * "randomly" fill the buffer contents
489 static void fill_rand_buf(struct io_u *io_u, int max_bs)
491 int *ptr = io_u->buf;
493 while ((void *) ptr - io_u->buf < max_bs) {
494 *ptr = rand() * 0x9e370001;
499 static int init_io_u(struct thread_data *td)
506 if (td->io_ops->flags & FIO_CPUIO)
509 if (td->io_ops->flags & FIO_SYNCIO)
512 max_units = td->iodepth;
514 max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]);
515 td->orig_buffer_size = max_bs * max_units;
517 if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE)
518 td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1);
520 td->orig_buffer_size += MASK;
522 if (allocate_io_mem(td))
525 p = ALIGN(td->orig_buffer);
526 for (i = 0; i < max_units; i++) {
527 io_u = malloc(sizeof(*io_u));
528 memset(io_u, 0, sizeof(*io_u));
529 INIT_LIST_HEAD(&io_u->list);
531 io_u->buf = p + max_bs * i;
532 if (td_write(td) || td_rw(td))
533 fill_rand_buf(io_u, max_bs);
536 list_add(&io_u->list, &td->io_u_freelist);
542 static int switch_ioscheduler(struct thread_data *td)
544 char tmp[256], tmp2[128];
548 if (td->io_ops->flags & FIO_CPUIO)
551 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
553 f = fopen(tmp, "r+");
555 td_verror(td, errno);
562 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
563 if (ferror(f) || ret != 1) {
564 td_verror(td, errno);
572 * Read back and check that the selected scheduler is now the default.
574 ret = fread(tmp, 1, sizeof(tmp), f);
575 if (ferror(f) || ret < 0) {
576 td_verror(td, errno);
581 sprintf(tmp2, "[%s]", td->ioscheduler);
582 if (!strstr(tmp, tmp2)) {
583 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
584 td_verror(td, EINVAL);
593 static void clear_io_state(struct thread_data *td)
598 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
599 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
602 for_each_file(td, f, i) {
604 if (td->io_ops->flags & FIO_SYNCIO)
605 lseek(f->fd, SEEK_SET, 0);
608 memset(f->file_map, 0, f->num_maps * sizeof(long));
613 * Entry point for the thread based jobs. The process based jobs end up
614 * here as well, after a little setup.
616 static void *thread_main(void *data)
618 unsigned long long runtime[2];
619 struct thread_data *td = data;
626 INIT_LIST_HEAD(&td->io_u_freelist);
627 INIT_LIST_HEAD(&td->io_u_busylist);
628 INIT_LIST_HEAD(&td->io_hist_list);
629 INIT_LIST_HEAD(&td->io_log_list);
634 if (fio_setaffinity(td) == -1) {
635 td_verror(td, errno);
646 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
647 td_verror(td, errno);
652 if (nice(td->nice) == -1) {
653 td_verror(td, errno);
657 if (init_random_state(td))
660 if (td->ioscheduler && switch_ioscheduler(td))
663 td_set_runstate(td, TD_INITIALIZED);
664 fio_sem_up(&startup_sem);
665 fio_sem_down(&td->mutex);
667 if (!td->create_serialize && setup_files(td))
673 system(td->exec_prerun);
675 fio_gettime(&td->epoch, NULL);
676 getrusage(RUSAGE_SELF, &td->ru_start);
678 runtime[0] = runtime[1] = 0;
679 while (td->loops--) {
680 fio_gettime(&td->start, NULL);
681 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
684 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
687 prune_io_piece_log(td);
689 if (td->io_ops->flags & FIO_CPUIO)
694 runtime[td->ddir] += utime_since_now(&td->start);
695 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
696 runtime[td->ddir ^ 1] = runtime[td->ddir];
698 if (td->error || td->terminate)
701 if (td->verify == VERIFY_NONE)
705 fio_gettime(&td->start, NULL);
709 runtime[DDIR_READ] += utime_since_now(&td->start);
711 if (td->error || td->terminate)
715 update_rusage_stat(td);
716 fio_gettime(&td->end_time, NULL);
717 td->runtime[0] = runtime[0] / 1000;
718 td->runtime[1] = runtime[1] / 1000;
721 finish_log(td, td->bw_log, "bw");
723 finish_log(td, td->slat_log, "slat");
725 finish_log(td, td->clat_log, "clat");
726 if (td->write_iolog_file)
727 write_iolog_close(td);
728 if (td->exec_postrun)
729 system(td->exec_postrun);
731 if (exitall_on_terminate)
732 terminate_threads(td->groupid);
738 td_set_runstate(td, TD_EXITED);
744 * We cannot pass the td data into a forked process, so attach the td and
745 * pass it to the thread worker.
747 static void *fork_main(int shmid, int offset)
749 struct thread_data *td;
752 data = shmat(shmid, NULL, 0);
753 if (data == (void *) -1) {
758 td = data + offset * sizeof(struct thread_data);
765 * Run over the job map and reap the threads that have exited, if any.
767 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
769 struct thread_data *td;
770 int i, cputhreads, pending;
773 * reap exited threads (TD_EXITED -> TD_REAPED)
775 pending = cputhreads = 0;
778 * ->io_ops is NULL for a thread that has closed its
781 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
784 if (td->runstate != TD_EXITED) {
785 if (td->runstate < TD_RUNNING)
791 td_set_runstate(td, TD_REAPED);
793 if (td->use_thread) {
796 if (pthread_join(td->thread, (void *) &ret))
797 perror("thread_join");
799 waitpid(td->pid, NULL, 0);
802 (*m_rate) -= td->ratemin;
803 (*t_rate) -= td->rate;
806 if (*nr_running == cputhreads && !pending)
807 terminate_threads(TERMINATE_ALL);
811 * Main function for kicking off and reaping jobs, as needed.
813 static void run_threads(void)
815 struct thread_data *td;
817 int i, todo, nr_running, m_rate, t_rate, nr_started;
819 if (fio_pin_memory())
823 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
827 signal(SIGINT, sig_handler);
828 signal(SIGALRM, sig_handler);
830 todo = thread_number;
836 print_status_init(td->thread_number - 1);
840 if (!td->create_serialize)
844 * do file setup here so it happens sequentially,
845 * we don't want X number of threads getting their
846 * client data interspersed on disk
848 if (setup_files(td)) {
849 td_set_runstate(td, TD_REAPED);
855 struct thread_data *map[MAX_JOBS];
856 struct timeval this_start;
857 int this_jobs = 0, left;
860 * create threads (TD_NOT_CREATED -> TD_CREATED)
863 if (td->runstate != TD_NOT_CREATED)
867 * never got a chance to start, killed by other
868 * thread for some reason
875 if (td->start_delay) {
876 spent = mtime_since_genesis();
878 if (td->start_delay * 1000 > spent)
882 if (td->stonewall && (nr_started || nr_running))
886 * Set state to created. Thread will transition
887 * to TD_INITIALIZED when it's done setting up.
889 td_set_runstate(td, TD_CREATED);
890 map[this_jobs++] = td;
891 fio_sem_init(&startup_sem, 1);
894 if (td->use_thread) {
895 if (pthread_create(&td->thread, NULL, thread_main, td)) {
896 perror("thread_create");
901 fio_sem_down(&startup_sem);
903 fork_main(shm_id, i);
910 * Wait for the started threads to transition to
913 fio_gettime(&this_start, NULL);
916 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
921 for (i = 0; i < this_jobs; i++) {
925 if (td->runstate == TD_INITIALIZED) {
928 } else if (td->runstate >= TD_EXITED) {
932 nr_running++; /* work-around... */
938 log_err("fio: %d jobs failed to start\n", left);
939 for (i = 0; i < this_jobs; i++) {
943 kill(td->pid, SIGTERM);
949 * start created threads (TD_INITIALIZED -> TD_RUNNING).
952 if (td->runstate != TD_INITIALIZED)
955 td_set_runstate(td, TD_RUNNING);
958 m_rate += td->ratemin;
961 fio_sem_up(&td->mutex);
964 reap_threads(&nr_running, &t_rate, &m_rate);
971 reap_threads(&nr_running, &t_rate, &m_rate);
979 int main(int argc, char *argv[])
981 if (parse_options(argc, argv))
984 if (!thread_number) {
985 log_err("Nothing to do\n");
989 disk_util_timer_arm();