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 * The main verify engine. Runs over the writes we previusly submitted,
198 * reads the blocks back in, and checks the crc/md5 of the data.
200 void do_verify(struct thread_data *td)
203 struct io_u *io_u, *v_io_u = NULL;
204 struct io_completion_data icd;
209 * sync io first and invalidate cache, to make sure we really
212 for_each_file(td, f, i) {
214 file_invalidate_cache(td, f);
217 td_set_runstate(td, TD_VERIFYING);
223 gettimeofday(&t, NULL);
224 if (runtime_exceeded(td, &t))
227 io_u = __get_io_u(td);
231 if (get_next_verify(td, io_u)) {
236 f = get_next_file(td);
242 if (td_io_prep(td, io_u)) {
247 ret = td_io_queue(td, io_u);
255 * we have one pending to verify, do that while
256 * we are doing io on the next one
258 if (do_io_u_verify(td, &v_io_u))
261 ret = td_io_getevents(td, 1, 1, NULL);
268 v_io_u = td->io_ops->event(td, 0);
271 io_completed(td, v_io_u, &icd);
274 td_verror(td, icd.error);
275 put_io_u(td, v_io_u);
281 * if we can't submit more io, we need to verify now
283 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
288 do_io_u_verify(td, &v_io_u);
291 cleanup_pending_aio(td);
293 td_set_runstate(td, TD_RUNNING);
297 * Not really an io thread, all it does is burn CPU cycles in the specified
300 static void do_cpuio(struct thread_data *td)
303 int split = 100 / td->cpuload;
306 while (!td->terminate) {
307 gettimeofday(&e, NULL);
309 if (runtime_exceeded(td, &e))
315 usec_sleep(td, 10000);
322 * Main IO worker function. It retrieves io_u's to process and queues
323 * and reaps them, checking for rate and errors along the way.
325 static void do_io(struct thread_data *td)
327 struct io_completion_data icd;
333 td_set_runstate(td, TD_RUNNING);
335 while (td->this_io_bytes[td->ddir] < td->io_size) {
336 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
337 struct timespec *timeout;
344 f = get_next_file(td);
348 io_u = get_io_u(td, f);
352 memcpy(&s, &io_u->start_time, sizeof(s));
354 ret = td_io_queue(td, io_u);
361 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
363 if (td->cur_depth < td->iodepth) {
371 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
379 ios_completed(td, &icd);
381 td_verror(td, icd.error);
386 * the rate is batched for now, it should work for batches
387 * of completions except the very first one which may look
390 gettimeofday(&e, NULL);
391 usec = utime_since(&s, &e);
393 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
395 if (check_min_rate(td, &e)) {
397 terminate_threads(td->groupid);
398 td_verror(td, ENOMEM);
402 if (runtime_exceeded(td, &e))
406 usec_sleep(td, td->thinktime);
411 cleanup_pending_aio(td);
413 if (should_fsync(td) && td->end_fsync) {
414 td_set_runstate(td, TD_FSYNCING);
415 for_each_file(td, f, i)
421 static void cleanup_io_u(struct thread_data *td)
423 struct list_head *entry, *n;
426 list_for_each_safe(entry, n, &td->io_u_freelist) {
427 io_u = list_entry(entry, struct io_u, list);
429 list_del(&io_u->list);
436 static int init_io_u(struct thread_data *td)
442 if (td->io_ops->flags & FIO_CPUIO)
445 if (td->io_ops->flags & FIO_SYNCIO)
448 max_units = td->iodepth;
450 td->orig_buffer_size = td->max_bs * max_units + MASK;
452 if (allocate_io_mem(td))
455 p = ALIGN(td->orig_buffer);
456 for (i = 0; i < max_units; i++) {
457 io_u = malloc(sizeof(*io_u));
458 memset(io_u, 0, sizeof(*io_u));
459 INIT_LIST_HEAD(&io_u->list);
461 io_u->buf = p + td->max_bs * i;
463 list_add(&io_u->list, &td->io_u_freelist);
469 static int switch_ioscheduler(struct thread_data *td)
471 char tmp[256], tmp2[128];
475 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
477 f = fopen(tmp, "r+");
479 td_verror(td, errno);
486 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
487 if (ferror(f) || ret != 1) {
488 td_verror(td, errno);
496 * Read back and check that the selected scheduler is now the default.
498 ret = fread(tmp, 1, sizeof(tmp), f);
499 if (ferror(f) || ret < 0) {
500 td_verror(td, errno);
505 sprintf(tmp2, "[%s]", td->ioscheduler);
506 if (!strstr(tmp, tmp2)) {
507 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
508 td_verror(td, EINVAL);
517 static void clear_io_state(struct thread_data *td)
522 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
523 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
526 for_each_file(td, f, i) {
528 if (td->io_ops->flags & FIO_SYNCIO)
529 lseek(f->fd, SEEK_SET, 0);
532 memset(f->file_map, 0, f->num_maps * sizeof(long));
537 * Entry point for the thread based jobs. The process based jobs end up
538 * here as well, after a little setup.
540 static void *thread_main(void *data)
542 struct thread_data *td = data;
549 INIT_LIST_HEAD(&td->io_u_freelist);
550 INIT_LIST_HEAD(&td->io_u_busylist);
551 INIT_LIST_HEAD(&td->io_hist_list);
552 INIT_LIST_HEAD(&td->io_log_list);
557 if (fio_setaffinity(td) == -1) {
558 td_verror(td, errno);
569 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
570 td_verror(td, errno);
575 if (nice(td->nice) == -1) {
576 td_verror(td, errno);
580 if (init_random_state(td))
583 if (td->ioscheduler && switch_ioscheduler(td))
586 td_set_runstate(td, TD_INITIALIZED);
587 fio_sem_up(&startup_sem);
588 fio_sem_down(&td->mutex);
590 if (!td->create_serialize && setup_files(td))
593 gettimeofday(&td->epoch, NULL);
596 system(td->exec_prerun);
598 while (td->loops--) {
599 getrusage(RUSAGE_SELF, &td->ru_start);
600 gettimeofday(&td->start, NULL);
601 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
604 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
607 prune_io_piece_log(td);
609 if (td->io_ops->flags & FIO_CPUIO)
614 td->runtime[td->ddir] += mtime_since_now(&td->start);
615 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
616 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
618 update_rusage_stat(td);
620 if (td->error || td->terminate)
623 if (td->verify == VERIFY_NONE)
627 gettimeofday(&td->start, NULL);
631 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
633 if (td->error || td->terminate)
638 finish_log(td, td->bw_log, "bw");
640 finish_log(td, td->slat_log, "slat");
642 finish_log(td, td->clat_log, "clat");
644 write_iolog_close(td);
645 if (td->exec_postrun)
646 system(td->exec_postrun);
648 if (exitall_on_terminate)
649 terminate_threads(td->groupid);
655 td_set_runstate(td, TD_EXITED);
661 * We cannot pass the td data into a forked process, so attach the td and
662 * pass it to the thread worker.
664 static void *fork_main(int shmid, int offset)
666 struct thread_data *td;
669 data = shmat(shmid, NULL, 0);
670 if (data == (void *) -1) {
675 td = data + offset * sizeof(struct thread_data);
682 * Run over the job map and reap the threads that have exited, if any.
684 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
686 struct thread_data *td;
690 * reap exited threads (TD_EXITED -> TD_REAPED)
695 * ->io_ops is NULL for a thread that has closed its
698 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
701 if (td->runstate != TD_EXITED)
704 td_set_runstate(td, TD_REAPED);
706 if (td->use_thread) {
709 if (pthread_join(td->thread, (void *) &ret))
710 perror("thread_join");
712 waitpid(td->pid, NULL, 0);
715 (*m_rate) -= td->ratemin;
716 (*t_rate) -= td->rate;
719 if (*nr_running == cputhreads)
720 terminate_threads(TERMINATE_ALL);
724 * Main function for kicking off and reaping jobs, as needed.
726 static void run_threads(void)
728 struct thread_data *td;
730 int i, todo, nr_running, m_rate, t_rate, nr_started;
732 if (fio_pin_memory())
736 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
740 signal(SIGINT, sig_handler);
741 signal(SIGALRM, sig_handler);
743 todo = thread_number;
749 print_status_init(td->thread_number - 1);
753 if (!td->create_serialize)
757 * do file setup here so it happens sequentially,
758 * we don't want X number of threads getting their
759 * client data interspersed on disk
761 if (setup_files(td)) {
762 td_set_runstate(td, TD_REAPED);
770 struct thread_data *map[MAX_JOBS];
771 struct timeval this_start;
772 int this_jobs = 0, left;
775 * create threads (TD_NOT_CREATED -> TD_CREATED)
778 if (td->runstate != TD_NOT_CREATED)
782 * never got a chance to start, killed by other
783 * thread for some reason
790 if (td->start_delay) {
791 spent = mtime_since_genesis();
793 if (td->start_delay * 1000 > spent)
797 if (td->stonewall && (nr_started || nr_running))
801 * Set state to created. Thread will transition
802 * to TD_INITIALIZED when it's done setting up.
804 td_set_runstate(td, TD_CREATED);
805 map[this_jobs++] = td;
806 fio_sem_init(&startup_sem, 1);
809 if (td->use_thread) {
810 if (pthread_create(&td->thread, NULL, thread_main, td)) {
811 perror("thread_create");
816 fio_sem_down(&startup_sem);
818 fork_main(shm_id, i);
825 * Wait for the started threads to transition to
828 gettimeofday(&this_start, NULL);
831 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
836 for (i = 0; i < this_jobs; i++) {
840 if (td->runstate == TD_INITIALIZED) {
843 } else if (td->runstate >= TD_EXITED) {
847 nr_running++; /* work-around... */
853 log_err("fio: %d jobs failed to start\n", left);
854 for (i = 0; i < this_jobs; i++) {
858 kill(td->pid, SIGTERM);
864 * start created threads (TD_INITIALIZED -> TD_RUNNING).
867 if (td->runstate != TD_INITIALIZED)
870 td_set_runstate(td, TD_RUNNING);
873 m_rate += td->ratemin;
876 fio_sem_up(&td->mutex);
879 reap_threads(&nr_running, &t_rate, &m_rate);
886 reap_threads(&nr_running, &t_rate, &m_rate);
894 int main(int argc, char *argv[])
896 if (parse_options(argc, argv))
899 if (!thread_number) {
900 log_err("Nothing to do\n");
904 disk_util_timer_arm();