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;
48 char *fio_inst_prefix = _INST_PREFIX;
50 static volatile int startup_sem;
52 #define TERMINATE_ALL (-1)
53 #define JOB_START_TIMEOUT (5 * 1000)
55 static void terminate_threads(int group_id)
57 struct thread_data *td;
61 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
68 static void sig_handler(int sig)
73 disk_util_timer_arm();
74 print_thread_status();
77 printf("\nfio: terminating on signal\n");
79 terminate_threads(TERMINATE_ALL);
85 * Check if we are above the minimum rate given.
87 static int check_min_rate(struct thread_data *td, struct timeval *now)
94 * allow a 2 second settle period in the beginning
96 if (mtime_since(&td->start, now) < 2000)
100 * if rate blocks is set, sample is running
102 if (td->rate_bytes) {
103 spent = mtime_since(&td->lastrate, now);
104 if (spent < td->ratecycle)
107 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
108 if (rate < td->ratemin) {
109 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
114 td->rate_bytes = td->this_io_bytes[ddir];
115 memcpy(&td->lastrate, now, sizeof(*now));
119 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
123 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
129 static inline void td_set_runstate(struct thread_data *td, int runstate)
131 td->runstate = runstate;
134 static struct fio_file *get_next_file(struct thread_data *td)
136 unsigned int old_next_file = td->next_file;
140 f = &td->files[td->next_file];
143 if (td->next_file >= td->nr_files)
150 } while (td->next_file != old_next_file);
156 * When job exits, we can cancel the in-flight IO if we are using async
157 * io. Attempt to do so.
159 static void cleanup_pending_aio(struct thread_data *td)
161 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
162 struct list_head *entry, *n;
163 struct io_completion_data icd;
168 * get immediately available events, if any
170 r = td_io_getevents(td, 0, td->cur_depth, &ts);
173 ios_completed(td, &icd);
177 * now cancel remaining active events
179 if (td->io_ops->cancel) {
180 list_for_each_safe(entry, n, &td->io_u_busylist) {
181 io_u = list_entry(entry, struct io_u, list);
183 r = td->io_ops->cancel(td, io_u);
190 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
193 ios_completed(td, &icd);
199 * Helper to handle the final sync of a file. Works just like the normal
200 * io path, just does everything sync.
202 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
204 struct io_u *io_u = __get_io_u(td);
205 struct io_completion_data icd;
211 io_u->ddir = DDIR_SYNC;
214 if (td_io_prep(td, io_u)) {
219 ret = td_io_queue(td, io_u);
221 td_verror(td, io_u->error);
226 ret = td_io_getevents(td, 1, td->cur_depth, NULL);
233 ios_completed(td, &icd);
235 td_verror(td, icd.error);
243 * The main verify engine. Runs over the writes we previusly submitted,
244 * reads the blocks back in, and checks the crc/md5 of the data.
246 void do_verify(struct thread_data *td)
249 struct io_u *io_u, *v_io_u = NULL;
250 struct io_completion_data icd;
255 * sync io first and invalidate cache, to make sure we really
258 for_each_file(td, f, i) {
260 file_invalidate_cache(td, f);
263 td_set_runstate(td, TD_VERIFYING);
269 gettimeofday(&t, NULL);
270 if (runtime_exceeded(td, &t))
273 io_u = __get_io_u(td);
277 if (get_next_verify(td, io_u)) {
282 f = get_next_file(td);
288 if (td_io_prep(td, io_u)) {
293 ret = td_io_queue(td, io_u);
295 td_verror(td, io_u->error);
301 * we have one pending to verify, do that while
302 * we are doing io on the next one
304 if (do_io_u_verify(td, &v_io_u))
307 ret = td_io_getevents(td, 1, 1, NULL);
314 v_io_u = td->io_ops->event(td, 0);
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 gettimeofday(&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 gettimeofday(&e, NULL);
437 usec = utime_since(&s, &e);
439 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
441 if (check_min_rate(td, &e)) {
443 terminate_threads(td->groupid);
444 td_verror(td, ENOMEM);
448 if (runtime_exceeded(td, &e))
452 usec_sleep(td, td->thinktime);
457 cleanup_pending_aio(td);
459 if (should_fsync(td) && td->end_fsync) {
460 td_set_runstate(td, TD_FSYNCING);
461 for_each_file(td, f, i)
467 static void cleanup_io_u(struct thread_data *td)
469 struct list_head *entry, *n;
472 list_for_each_safe(entry, n, &td->io_u_freelist) {
473 io_u = list_entry(entry, struct io_u, list);
475 list_del(&io_u->list);
482 static int init_io_u(struct thread_data *td)
488 if (td->io_ops->flags & FIO_CPUIO)
491 if (td->io_ops->flags & FIO_SYNCIO)
494 max_units = td->iodepth;
496 td->orig_buffer_size = td->max_bs * max_units + MASK;
498 if (allocate_io_mem(td))
501 p = ALIGN(td->orig_buffer);
502 for (i = 0; i < max_units; i++) {
503 io_u = malloc(sizeof(*io_u));
504 memset(io_u, 0, sizeof(*io_u));
505 INIT_LIST_HEAD(&io_u->list);
507 io_u->buf = p + td->max_bs * i;
509 list_add(&io_u->list, &td->io_u_freelist);
515 static int switch_ioscheduler(struct thread_data *td)
517 char tmp[256], tmp2[128];
521 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
523 f = fopen(tmp, "r+");
525 td_verror(td, errno);
532 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
533 if (ferror(f) || ret != 1) {
534 td_verror(td, errno);
542 * Read back and check that the selected scheduler is now the default.
544 ret = fread(tmp, 1, sizeof(tmp), f);
545 if (ferror(f) || ret < 0) {
546 td_verror(td, errno);
551 sprintf(tmp2, "[%s]", td->ioscheduler);
552 if (!strstr(tmp, tmp2)) {
553 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
554 td_verror(td, EINVAL);
563 static void clear_io_state(struct thread_data *td)
568 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
569 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
572 for_each_file(td, f, i) {
574 if (td->io_ops->flags & FIO_SYNCIO)
575 lseek(f->fd, SEEK_SET, 0);
578 memset(f->file_map, 0, f->num_maps * sizeof(long));
583 * Entry point for the thread based jobs. The process based jobs end up
584 * here as well, after a little setup.
586 static void *thread_main(void *data)
588 struct thread_data *td = data;
595 INIT_LIST_HEAD(&td->io_u_freelist);
596 INIT_LIST_HEAD(&td->io_u_busylist);
597 INIT_LIST_HEAD(&td->io_hist_list);
598 INIT_LIST_HEAD(&td->io_log_list);
603 if (fio_setaffinity(td) == -1) {
604 td_verror(td, errno);
615 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
616 td_verror(td, errno);
621 if (nice(td->nice) == -1) {
622 td_verror(td, errno);
626 if (init_random_state(td))
629 if (td->ioscheduler && switch_ioscheduler(td))
632 td_set_runstate(td, TD_INITIALIZED);
633 fio_sem_up(&startup_sem);
634 fio_sem_down(&td->mutex);
636 if (!td->create_serialize && setup_files(td))
639 gettimeofday(&td->epoch, NULL);
642 system(td->exec_prerun);
644 while (td->loops--) {
645 getrusage(RUSAGE_SELF, &td->ru_start);
646 gettimeofday(&td->start, NULL);
647 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
650 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
653 prune_io_piece_log(td);
655 if (td->io_ops->flags & FIO_CPUIO)
660 td->runtime[td->ddir] += mtime_since_now(&td->start);
661 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
662 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
664 update_rusage_stat(td);
666 if (td->error || td->terminate)
669 if (td->verify == VERIFY_NONE)
673 gettimeofday(&td->start, NULL);
677 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
679 if (td->error || td->terminate)
684 finish_log(td, td->bw_log, "bw");
686 finish_log(td, td->slat_log, "slat");
688 finish_log(td, td->clat_log, "clat");
689 if (td->write_iolog_file)
690 write_iolog_close(td);
691 if (td->exec_postrun)
692 system(td->exec_postrun);
694 if (exitall_on_terminate)
695 terminate_threads(td->groupid);
701 td_set_runstate(td, TD_EXITED);
707 * We cannot pass the td data into a forked process, so attach the td and
708 * pass it to the thread worker.
710 static void *fork_main(int shmid, int offset)
712 struct thread_data *td;
715 data = shmat(shmid, NULL, 0);
716 if (data == (void *) -1) {
721 td = data + offset * sizeof(struct thread_data);
728 * Run over the job map and reap the threads that have exited, if any.
730 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
732 struct thread_data *td;
736 * reap exited threads (TD_EXITED -> TD_REAPED)
741 * ->io_ops is NULL for a thread that has closed its
744 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
747 if (td->runstate != TD_EXITED)
750 td_set_runstate(td, TD_REAPED);
752 if (td->use_thread) {
755 if (pthread_join(td->thread, (void *) &ret))
756 perror("thread_join");
758 waitpid(td->pid, NULL, 0);
761 (*m_rate) -= td->ratemin;
762 (*t_rate) -= td->rate;
765 if (*nr_running == cputhreads)
766 terminate_threads(TERMINATE_ALL);
770 * Main function for kicking off and reaping jobs, as needed.
772 static void run_threads(void)
774 struct thread_data *td;
776 int i, todo, nr_running, m_rate, t_rate, nr_started;
778 if (fio_pin_memory())
782 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
786 signal(SIGINT, sig_handler);
787 signal(SIGALRM, sig_handler);
789 todo = thread_number;
795 print_status_init(td->thread_number - 1);
799 if (!td->create_serialize)
803 * do file setup here so it happens sequentially,
804 * we don't want X number of threads getting their
805 * client data interspersed on disk
807 if (setup_files(td)) {
808 td_set_runstate(td, TD_REAPED);
816 struct thread_data *map[MAX_JOBS];
817 struct timeval this_start;
818 int this_jobs = 0, left;
821 * create threads (TD_NOT_CREATED -> TD_CREATED)
824 if (td->runstate != TD_NOT_CREATED)
828 * never got a chance to start, killed by other
829 * thread for some reason
836 if (td->start_delay) {
837 spent = mtime_since_genesis();
839 if (td->start_delay * 1000 > spent)
843 if (td->stonewall && (nr_started || nr_running))
847 * Set state to created. Thread will transition
848 * to TD_INITIALIZED when it's done setting up.
850 td_set_runstate(td, TD_CREATED);
851 map[this_jobs++] = td;
852 fio_sem_init(&startup_sem, 1);
855 if (td->use_thread) {
856 if (pthread_create(&td->thread, NULL, thread_main, td)) {
857 perror("thread_create");
862 fio_sem_down(&startup_sem);
864 fork_main(shm_id, i);
871 * Wait for the started threads to transition to
874 gettimeofday(&this_start, NULL);
877 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
882 for (i = 0; i < this_jobs; i++) {
886 if (td->runstate == TD_INITIALIZED) {
889 } else if (td->runstate >= TD_EXITED) {
893 nr_running++; /* work-around... */
899 log_err("fio: %d jobs failed to start\n", left);
900 for (i = 0; i < this_jobs; i++) {
904 kill(td->pid, SIGTERM);
910 * start created threads (TD_INITIALIZED -> TD_RUNNING).
913 if (td->runstate != TD_INITIALIZED)
916 td_set_runstate(td, TD_RUNNING);
919 m_rate += td->ratemin;
922 fio_sem_up(&td->mutex);
925 reap_threads(&nr_running, &t_rate, &m_rate);
932 reap_threads(&nr_running, &t_rate, &m_rate);
940 int main(int argc, char *argv[])
942 if (parse_options(argc, argv))
945 if (!thread_number) {
946 log_err("Nothing to do\n");
950 disk_util_timer_arm();