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 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
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 * The main verify engine. Runs over the writes we previusly submitted,
200 * reads the blocks back in, and checks the crc/md5 of the data.
202 void do_verify(struct thread_data *td)
205 struct io_u *io_u, *v_io_u = NULL;
206 struct io_completion_data icd;
211 * sync io first and invalidate cache, to make sure we really
214 for_each_file(td, f, i) {
216 file_invalidate_cache(td, f);
219 td_set_runstate(td, TD_VERIFYING);
225 gettimeofday(&t, NULL);
226 if (runtime_exceeded(td, &t))
229 io_u = __get_io_u(td);
233 if (get_next_verify(td, io_u)) {
238 f = get_next_file(td);
244 if (td_io_prep(td, io_u)) {
249 ret = td_io_queue(td, io_u);
257 * we have one pending to verify, do that while
258 * we are doing io on the next one
260 if (do_io_u_verify(td, &v_io_u))
263 ret = td_io_getevents(td, 1, 1, NULL);
270 v_io_u = td->io_ops->event(td, 0);
273 io_completed(td, v_io_u, &icd);
276 td_verror(td, icd.error);
277 put_io_u(td, v_io_u);
283 * if we can't submit more io, we need to verify now
285 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
290 do_io_u_verify(td, &v_io_u);
293 cleanup_pending_aio(td);
295 td_set_runstate(td, TD_RUNNING);
299 * Not really an io thread, all it does is burn CPU cycles in the specified
302 static void do_cpuio(struct thread_data *td)
305 int split = 100 / td->cpuload;
308 while (!td->terminate) {
309 gettimeofday(&e, NULL);
311 if (runtime_exceeded(td, &e))
317 usec_sleep(td, 10000);
324 * Main IO worker function. It retrieves io_u's to process and queues
325 * and reaps them, checking for rate and errors along the way.
327 static void do_io(struct thread_data *td)
329 struct io_completion_data icd;
335 td_set_runstate(td, TD_RUNNING);
337 while (td->this_io_bytes[td->ddir] < td->io_size) {
338 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
339 struct timespec *timeout;
346 f = get_next_file(td);
350 io_u = get_io_u(td, f);
354 memcpy(&s, &io_u->start_time, sizeof(s));
356 ret = td_io_queue(td, io_u);
363 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
365 if (td->cur_depth < td->iodepth) {
373 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
381 ios_completed(td, &icd);
383 td_verror(td, icd.error);
388 * the rate is batched for now, it should work for batches
389 * of completions except the very first one which may look
392 gettimeofday(&e, NULL);
393 usec = utime_since(&s, &e);
395 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
397 if (check_min_rate(td, &e)) {
399 terminate_threads(td->groupid);
400 td_verror(td, ENOMEM);
404 if (runtime_exceeded(td, &e))
408 usec_sleep(td, td->thinktime);
410 if (should_fsync(td) && td->fsync_blocks &&
411 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
417 cleanup_pending_aio(td);
419 if (should_fsync(td) && td->end_fsync) {
420 td_set_runstate(td, TD_FSYNCING);
421 for_each_file(td, f, i)
427 static void cleanup_io_u(struct thread_data *td)
429 struct list_head *entry, *n;
432 list_for_each_safe(entry, n, &td->io_u_freelist) {
433 io_u = list_entry(entry, struct io_u, list);
435 list_del(&io_u->list);
442 static int init_io_u(struct thread_data *td)
448 if (td->io_ops->flags & FIO_CPUIO)
451 if (td->io_ops->flags & FIO_SYNCIO)
454 max_units = td->iodepth;
456 td->orig_buffer_size = td->max_bs * max_units + MASK;
458 if (allocate_io_mem(td))
461 p = ALIGN(td->orig_buffer);
462 for (i = 0; i < max_units; i++) {
463 io_u = malloc(sizeof(*io_u));
464 memset(io_u, 0, sizeof(*io_u));
465 INIT_LIST_HEAD(&io_u->list);
467 io_u->buf = p + td->max_bs * i;
469 list_add(&io_u->list, &td->io_u_freelist);
475 static int switch_ioscheduler(struct thread_data *td)
477 char tmp[256], tmp2[128];
481 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
483 f = fopen(tmp, "r+");
485 td_verror(td, errno);
492 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
493 if (ferror(f) || ret != 1) {
494 td_verror(td, errno);
502 * Read back and check that the selected scheduler is now the default.
504 ret = fread(tmp, 1, sizeof(tmp), f);
505 if (ferror(f) || ret < 0) {
506 td_verror(td, errno);
511 sprintf(tmp2, "[%s]", td->ioscheduler);
512 if (!strstr(tmp, tmp2)) {
513 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
514 td_verror(td, EINVAL);
523 static void clear_io_state(struct thread_data *td)
528 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
529 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
532 for_each_file(td, f, i) {
534 if (td->io_ops->flags & FIO_SYNCIO)
535 lseek(f->fd, SEEK_SET, 0);
538 memset(f->file_map, 0, f->num_maps * sizeof(long));
543 * Entry point for the thread based jobs. The process based jobs end up
544 * here as well, after a little setup.
546 static void *thread_main(void *data)
548 struct thread_data *td = data;
555 INIT_LIST_HEAD(&td->io_u_freelist);
556 INIT_LIST_HEAD(&td->io_u_busylist);
557 INIT_LIST_HEAD(&td->io_hist_list);
558 INIT_LIST_HEAD(&td->io_log_list);
563 if (fio_setaffinity(td) == -1) {
564 td_verror(td, errno);
575 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
576 td_verror(td, errno);
581 if (nice(td->nice) == -1) {
582 td_verror(td, errno);
586 if (init_random_state(td))
589 if (td->ioscheduler) {
590 int ret = switch_ioscheduler(td);
592 free(td->ioscheduler);
593 free(td->sysfs_root);
598 td_set_runstate(td, TD_INITIALIZED);
599 fio_sem_up(&startup_sem);
600 fio_sem_down(&td->mutex);
602 if (!td->create_serialize && setup_files(td))
605 gettimeofday(&td->epoch, NULL);
607 if (td->exec_prerun) {
608 system(td->exec_prerun);
609 free(td->exec_prerun);
612 while (td->loops--) {
613 getrusage(RUSAGE_SELF, &td->ru_start);
614 gettimeofday(&td->start, NULL);
615 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
618 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
621 prune_io_piece_log(td);
623 if (td->io_ops->flags & FIO_CPUIO)
628 td->runtime[td->ddir] += mtime_since_now(&td->start);
629 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
630 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
632 update_rusage_stat(td);
634 if (td->error || td->terminate)
637 if (td->verify == VERIFY_NONE)
641 gettimeofday(&td->start, NULL);
645 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
647 if (td->error || td->terminate)
652 finish_log(td, td->bw_log, "bw");
654 finish_log(td, td->slat_log, "slat");
656 finish_log(td, td->clat_log, "clat");
658 write_iolog_close(td);
659 if (td->exec_postrun) {
660 system(td->exec_postrun);
661 free(td->exec_postrun);
664 if (exitall_on_terminate)
665 terminate_threads(td->groupid);
671 td_set_runstate(td, TD_EXITED);
677 * We cannot pass the td data into a forked process, so attach the td and
678 * pass it to the thread worker.
680 static void *fork_main(int shmid, int offset)
682 struct thread_data *td;
685 data = shmat(shmid, NULL, 0);
686 if (data == (void *) -1) {
691 td = data + offset * sizeof(struct thread_data);
698 * Run over the job map and reap the threads that have exited, if any.
700 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
702 struct thread_data *td;
706 * reap exited threads (TD_EXITED -> TD_REAPED)
711 * ->io_ops is NULL for a thread that has closed its
714 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
717 if (td->runstate != TD_EXITED)
720 td_set_runstate(td, TD_REAPED);
722 if (td->use_thread) {
725 if (pthread_join(td->thread, (void *) &ret))
726 perror("thread_join");
728 waitpid(td->pid, NULL, 0);
731 (*m_rate) -= td->ratemin;
732 (*t_rate) -= td->rate;
735 if (*nr_running == cputhreads)
736 terminate_threads(TERMINATE_ALL);
740 * Main function for kicking off and reaping jobs, as needed.
742 static void run_threads(void)
744 struct thread_data *td;
746 int i, todo, nr_running, m_rate, t_rate, nr_started;
748 if (fio_pin_memory())
752 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
756 signal(SIGINT, sig_handler);
757 signal(SIGALRM, sig_handler);
759 todo = thread_number;
765 print_status_init(td->thread_number - 1);
769 if (!td->create_serialize)
773 * do file setup here so it happens sequentially,
774 * we don't want X number of threads getting their
775 * client data interspersed on disk
777 if (setup_files(td)) {
778 td_set_runstate(td, TD_REAPED);
786 struct thread_data *map[MAX_JOBS];
787 struct timeval this_start;
788 int this_jobs = 0, left;
791 * create threads (TD_NOT_CREATED -> TD_CREATED)
794 if (td->runstate != TD_NOT_CREATED)
798 * never got a chance to start, killed by other
799 * thread for some reason
806 if (td->start_delay) {
807 spent = mtime_since_genesis();
809 if (td->start_delay * 1000 > spent)
813 if (td->stonewall && (nr_started || nr_running))
817 * Set state to created. Thread will transition
818 * to TD_INITIALIZED when it's done setting up.
820 td_set_runstate(td, TD_CREATED);
821 map[this_jobs++] = td;
822 fio_sem_init(&startup_sem, 1);
825 if (td->use_thread) {
826 if (pthread_create(&td->thread, NULL, thread_main, td)) {
827 perror("thread_create");
832 fio_sem_down(&startup_sem);
834 fork_main(shm_id, i);
841 * Wait for the started threads to transition to
844 gettimeofday(&this_start, NULL);
847 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
852 for (i = 0; i < this_jobs; i++) {
856 if (td->runstate == TD_INITIALIZED) {
859 } else if (td->runstate >= TD_EXITED) {
863 nr_running++; /* work-around... */
869 log_err("fio: %d jobs failed to start\n", left);
870 for (i = 0; i < this_jobs; i++) {
874 kill(td->pid, SIGTERM);
880 * start created threads (TD_INITIALIZED -> TD_RUNNING).
883 if (td->runstate != TD_INITIALIZED)
886 td_set_runstate(td, TD_RUNNING);
889 m_rate += td->ratemin;
892 fio_sem_up(&td->mutex);
895 reap_threads(&nr_running, &t_rate, &m_rate);
902 reap_threads(&nr_running, &t_rate, &m_rate);
910 int main(int argc, char *argv[])
912 if (parse_options(argc, argv))
915 if (!thread_number) {
916 log_err("Nothing to do\n");
920 disk_util_timer_arm();