2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006-2012 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
52 static pthread_t disk_util_thread;
53 static struct fio_mutex *startup_mutex;
54 static struct fio_mutex *writeout_mutex;
55 static struct flist_head *cgroup_list;
56 static char *cgroup_mnt;
57 static int exit_value;
58 static volatile int fio_abort;
60 struct io_log *agg_io_log[2];
63 unsigned int thread_number = 0;
64 unsigned int nr_process = 0;
65 unsigned int nr_thread = 0;
68 unsigned long done_secs = 0;
70 #define PAGE_ALIGN(buf) \
71 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
73 #define JOB_START_TIMEOUT (5 * 1000)
75 static void sig_int(int sig)
79 fio_server_got_signal(sig);
81 log_info("\nfio: terminating on signal %d\n", sig);
86 fio_terminate_threads(TERMINATE_ALL);
90 static void sig_show_status(int sig)
92 show_running_run_stats();
95 static void set_sig_handlers(void)
99 memset(&act, 0, sizeof(act));
100 act.sa_handler = sig_int;
101 act.sa_flags = SA_RESTART;
102 sigaction(SIGINT, &act, NULL);
104 memset(&act, 0, sizeof(act));
105 act.sa_handler = sig_int;
106 act.sa_flags = SA_RESTART;
107 sigaction(SIGTERM, &act, NULL);
109 memset(&act, 0, sizeof(act));
110 act.sa_handler = sig_show_status;
111 act.sa_flags = SA_RESTART;
112 sigaction(SIGUSR1, &act, NULL);
115 memset(&act, 0, sizeof(act));
116 act.sa_handler = sig_int;
117 act.sa_flags = SA_RESTART;
118 sigaction(SIGPIPE, &act, NULL);
123 * Check if we are above the minimum rate given.
125 static int __check_min_rate(struct thread_data *td, struct timeval *now,
128 unsigned long long bytes = 0;
129 unsigned long iops = 0;
132 unsigned int ratemin = 0;
133 unsigned int rate_iops = 0;
134 unsigned int rate_iops_min = 0;
136 assert(ddir_rw(ddir));
138 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
142 * allow a 2 second settle period in the beginning
144 if (mtime_since(&td->start, now) < 2000)
147 iops += td->this_io_blocks[ddir];
148 bytes += td->this_io_bytes[ddir];
149 ratemin += td->o.ratemin[ddir];
150 rate_iops += td->o.rate_iops[ddir];
151 rate_iops_min += td->o.rate_iops_min[ddir];
154 * if rate blocks is set, sample is running
156 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
157 spent = mtime_since(&td->lastrate[ddir], now);
158 if (spent < td->o.ratecycle)
161 if (td->o.rate[ddir]) {
163 * check bandwidth specified rate
165 if (bytes < td->rate_bytes[ddir]) {
166 log_err("%s: min rate %u not met\n", td->o.name,
170 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
171 if (rate < ratemin ||
172 bytes < td->rate_bytes[ddir]) {
173 log_err("%s: min rate %u not met, got"
174 " %luKB/sec\n", td->o.name,
181 * checks iops specified rate
183 if (iops < rate_iops) {
184 log_err("%s: min iops rate %u not met\n",
185 td->o.name, rate_iops);
188 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
189 if (rate < rate_iops_min ||
190 iops < td->rate_blocks[ddir]) {
191 log_err("%s: min iops rate %u not met,"
192 " got %lu\n", td->o.name,
193 rate_iops_min, rate);
199 td->rate_bytes[ddir] = bytes;
200 td->rate_blocks[ddir] = iops;
201 memcpy(&td->lastrate[ddir], now, sizeof(*now));
205 static int check_min_rate(struct thread_data *td, struct timeval *now,
206 unsigned long *bytes_done)
211 ret |= __check_min_rate(td, now, 0);
213 ret |= __check_min_rate(td, now, 1);
219 * When job exits, we can cancel the in-flight IO if we are using async
220 * io. Attempt to do so.
222 static void cleanup_pending_aio(struct thread_data *td)
224 struct flist_head *entry, *n;
229 * get immediately available events, if any
231 r = io_u_queued_complete(td, 0, NULL);
236 * now cancel remaining active events
238 if (td->io_ops->cancel) {
239 flist_for_each_safe(entry, n, &td->io_u_busylist) {
240 io_u = flist_entry(entry, struct io_u, list);
243 * if the io_u isn't in flight, then that generally
244 * means someone leaked an io_u. complain but fix
245 * it up, so we don't stall here.
247 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
248 log_err("fio: non-busy IO on busy list\n");
251 r = td->io_ops->cancel(td, io_u);
259 r = io_u_queued_complete(td, td->cur_depth, NULL);
263 * Helper to handle the final sync of a file. Works just like the normal
264 * io path, just does everything sync.
266 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
268 struct io_u *io_u = __get_io_u(td);
274 io_u->ddir = DDIR_SYNC;
277 if (td_io_prep(td, io_u)) {
283 ret = td_io_queue(td, io_u);
285 td_verror(td, io_u->error, "td_io_queue");
288 } else if (ret == FIO_Q_QUEUED) {
289 if (io_u_queued_complete(td, 1, NULL) < 0)
291 } else if (ret == FIO_Q_COMPLETED) {
293 td_verror(td, io_u->error, "td_io_queue");
297 if (io_u_sync_complete(td, io_u, NULL) < 0)
299 } else if (ret == FIO_Q_BUSY) {
300 if (td_io_commit(td))
308 static inline void __update_tv_cache(struct thread_data *td)
310 fio_gettime(&td->tv_cache, NULL);
313 static inline void update_tv_cache(struct thread_data *td)
315 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
316 __update_tv_cache(td);
319 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
321 if (in_ramp_time(td))
325 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
331 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
336 if (ret < 0 || td->error) {
344 if (!(td->o.continue_on_error & td_error_type(ddir, err)))
347 if (td_non_fatal_error(err)) {
349 * Continue with the I/Os in case of
352 update_error_count(td, err);
356 } else if (td->o.fill_device && err == ENOSPC) {
358 * We expect to hit this error if
359 * fill_device option is set.
366 * Stop the I/O in case of a fatal
369 update_error_count(td, err);
378 * The main verify engine. Runs over the writes we previously submitted,
379 * reads the blocks back in, and checks the crc/md5 of the data.
381 static void do_verify(struct thread_data *td)
388 dprint(FD_VERIFY, "starting loop\n");
391 * sync io first and invalidate cache, to make sure we really
394 for_each_file(td, f, i) {
395 if (!fio_file_open(f))
397 if (fio_io_sync(td, f))
399 if (file_invalidate_cache(td, f))
406 td_set_runstate(td, TD_VERIFYING);
409 while (!td->terminate) {
414 if (runtime_exceeded(td, &td->tv_cache)) {
415 __update_tv_cache(td);
416 if (runtime_exceeded(td, &td->tv_cache)) {
422 if (flow_threshold_exceeded(td))
425 io_u = __get_io_u(td);
429 if (get_next_verify(td, io_u)) {
434 if (td_io_prep(td, io_u)) {
439 if (td->o.verify_async)
440 io_u->end_io = verify_io_u_async;
442 io_u->end_io = verify_io_u;
444 ret = td_io_queue(td, io_u);
446 case FIO_Q_COMPLETED:
449 clear_io_u(td, io_u);
450 } else if (io_u->resid) {
451 int bytes = io_u->xfer_buflen - io_u->resid;
457 td_verror(td, EIO, "full resid");
462 io_u->xfer_buflen = io_u->resid;
463 io_u->xfer_buf += bytes;
464 io_u->offset += bytes;
466 if (ddir_rw(io_u->ddir))
467 td->ts.short_io_u[io_u->ddir]++;
470 if (io_u->offset == f->real_file_size)
473 requeue_io_u(td, &io_u);
476 ret = io_u_sync_complete(td, io_u, NULL);
484 requeue_io_u(td, &io_u);
485 ret2 = td_io_commit(td);
491 td_verror(td, -ret, "td_io_queue");
495 if (break_on_this_error(td, io_u->ddir, &ret))
499 * if we can queue more, do so. but check if there are
500 * completed io_u's first. Note that we can get BUSY even
501 * without IO queued, if the system is resource starved.
503 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
504 if (full || !td->o.iodepth_batch_complete) {
505 min_events = min(td->o.iodepth_batch_complete,
508 * if the queue is full, we MUST reap at least 1 event
510 if (full && !min_events)
515 * Reap required number of io units, if any,
516 * and do the verification on them through
517 * the callback handler
519 if (io_u_queued_complete(td, min_events, NULL) < 0) {
523 } while (full && (td->cur_depth > td->o.iodepth_low));
530 min_events = td->cur_depth;
533 ret = io_u_queued_complete(td, min_events, NULL);
535 cleanup_pending_aio(td);
537 td_set_runstate(td, TD_RUNNING);
539 dprint(FD_VERIFY, "exiting loop\n");
542 static int io_bytes_exceeded(struct thread_data *td)
544 unsigned long long bytes;
547 bytes = td->this_io_bytes[0] + td->this_io_bytes[1];
548 else if (td_write(td))
549 bytes = td->this_io_bytes[1];
551 bytes = td->this_io_bytes[0];
553 return bytes >= td->o.size;
557 * Main IO worker function. It retrieves io_u's to process and queues
558 * and reaps them, checking for rate and errors along the way.
560 static void do_io(struct thread_data *td)
565 if (in_ramp_time(td))
566 td_set_runstate(td, TD_RAMP);
568 td_set_runstate(td, TD_RUNNING);
570 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
571 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
573 struct timeval comp_time;
574 unsigned long bytes_done[2] = { 0, 0 };
585 if (runtime_exceeded(td, &td->tv_cache)) {
586 __update_tv_cache(td);
587 if (runtime_exceeded(td, &td->tv_cache)) {
593 if (flow_threshold_exceeded(td))
603 * Add verification end_io handler if:
604 * - Asked to verify (!td_rw(td))
605 * - Or the io_u is from our verify list (mixed write/ver)
607 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
608 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
609 if (td->o.verify_async)
610 io_u->end_io = verify_io_u_async;
612 io_u->end_io = verify_io_u;
613 td_set_runstate(td, TD_VERIFYING);
614 } else if (in_ramp_time(td))
615 td_set_runstate(td, TD_RAMP);
617 td_set_runstate(td, TD_RUNNING);
619 ret = td_io_queue(td, io_u);
621 case FIO_Q_COMPLETED:
624 clear_io_u(td, io_u);
625 } else if (io_u->resid) {
626 int bytes = io_u->xfer_buflen - io_u->resid;
627 struct fio_file *f = io_u->file;
633 td_verror(td, EIO, "full resid");
638 io_u->xfer_buflen = io_u->resid;
639 io_u->xfer_buf += bytes;
640 io_u->offset += bytes;
642 if (ddir_rw(io_u->ddir))
643 td->ts.short_io_u[io_u->ddir]++;
645 if (io_u->offset == f->real_file_size)
648 requeue_io_u(td, &io_u);
651 if (__should_check_rate(td, 0) ||
652 __should_check_rate(td, 1))
653 fio_gettime(&comp_time, NULL);
655 ret = io_u_sync_complete(td, io_u, bytes_done);
662 * if the engine doesn't have a commit hook,
663 * the io_u is really queued. if it does have such
664 * a hook, it has to call io_u_queued() itself.
666 if (td->io_ops->commit == NULL)
667 io_u_queued(td, io_u);
670 requeue_io_u(td, &io_u);
671 ret2 = td_io_commit(td);
681 if (break_on_this_error(td, ddir, &ret))
685 * See if we need to complete some commands. Note that we
686 * can get BUSY even without IO queued, if the system is
689 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
690 if (full || !td->o.iodepth_batch_complete) {
691 min_evts = min(td->o.iodepth_batch_complete,
694 * if the queue is full, we MUST reap at least 1 event
696 if (full && !min_evts)
699 if (__should_check_rate(td, 0) ||
700 __should_check_rate(td, 1))
701 fio_gettime(&comp_time, NULL);
704 ret = io_u_queued_complete(td, min_evts, bytes_done);
708 } while (full && (td->cur_depth > td->o.iodepth_low));
713 if (!(bytes_done[0] + bytes_done[1]))
716 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
717 if (check_min_rate(td, &comp_time, bytes_done)) {
718 if (exitall_on_terminate)
719 fio_terminate_threads(td->groupid);
720 td_verror(td, EIO, "check_min_rate");
725 if (td->o.thinktime) {
726 unsigned long long b;
728 b = td->io_blocks[0] + td->io_blocks[1];
729 if (!(b % td->o.thinktime_blocks)) {
732 if (td->o.thinktime_spin)
733 usec_spin(td->o.thinktime_spin);
735 left = td->o.thinktime - td->o.thinktime_spin;
737 usec_sleep(td, left);
742 if (td->trim_entries)
743 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
745 if (td->o.fill_device && td->error == ENOSPC) {
754 ret = io_u_queued_complete(td, i, NULL);
755 if (td->o.fill_device && td->error == ENOSPC)
759 if (should_fsync(td) && td->o.end_fsync) {
760 td_set_runstate(td, TD_FSYNCING);
762 for_each_file(td, f, i) {
763 if (!fio_file_open(f))
769 cleanup_pending_aio(td);
772 * stop job if we failed doing any IO
774 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
778 static void cleanup_io_u(struct thread_data *td)
780 struct flist_head *entry, *n;
783 flist_for_each_safe(entry, n, &td->io_u_freelist) {
784 io_u = flist_entry(entry, struct io_u, list);
786 flist_del(&io_u->list);
787 fio_memfree(io_u, sizeof(*io_u));
793 static int init_io_u(struct thread_data *td)
796 unsigned int max_bs, min_write;
797 int cl_align, i, max_units;
800 max_units = td->o.iodepth;
801 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
802 min_write = td->o.min_bs[DDIR_WRITE];
803 td->orig_buffer_size = (unsigned long long) max_bs
804 * (unsigned long long) max_units;
806 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
809 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
810 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
813 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
814 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
818 if (allocate_io_mem(td))
821 if (td->o.odirect || td->o.mem_align ||
822 (td->io_ops->flags & FIO_RAWIO))
823 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
827 cl_align = os_cache_line_size();
829 for (i = 0; i < max_units; i++) {
835 ptr = fio_memalign(cl_align, sizeof(*io_u));
837 log_err("fio: unable to allocate aligned memory\n");
842 memset(io_u, 0, sizeof(*io_u));
843 INIT_FLIST_HEAD(&io_u->list);
844 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
846 if (!(td->io_ops->flags & FIO_NOIO)) {
848 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
851 io_u_fill_buffer(td, io_u, min_write, max_bs);
852 if (td_write(td) && td->o.verify_pattern_bytes) {
854 * Fill the buffer with the pattern if we are
855 * going to be doing writes.
857 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
862 io_u->flags = IO_U_F_FREE;
863 flist_add(&io_u->list, &td->io_u_freelist);
870 static int switch_ioscheduler(struct thread_data *td)
872 char tmp[256], tmp2[128];
876 if (td->io_ops->flags & FIO_DISKLESSIO)
879 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
881 f = fopen(tmp, "r+");
883 if (errno == ENOENT) {
884 log_err("fio: os or kernel doesn't support IO scheduler"
888 td_verror(td, errno, "fopen iosched");
895 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
896 if (ferror(f) || ret != 1) {
897 td_verror(td, errno, "fwrite");
905 * Read back and check that the selected scheduler is now the default.
907 ret = fread(tmp, 1, sizeof(tmp), f);
908 if (ferror(f) || ret < 0) {
909 td_verror(td, errno, "fread");
914 sprintf(tmp2, "[%s]", td->o.ioscheduler);
915 if (!strstr(tmp, tmp2)) {
916 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
917 td_verror(td, EINVAL, "iosched_switch");
926 static int keep_running(struct thread_data *td)
928 unsigned long long io_done;
932 if (td->o.time_based)
939 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
941 if (io_done < td->o.size)
947 static int exec_string(const char *string)
949 int ret, newlen = strlen(string) + 1 + 8;
952 str = malloc(newlen);
953 sprintf(str, "sh -c %s", string);
957 log_err("fio: exec of cmd <%s> failed\n", str);
964 * Entry point for the thread based jobs. The process based jobs end up
965 * here as well, after a little setup.
967 static void *thread_main(void *data)
969 unsigned long long elapsed;
970 struct thread_data *td = data;
971 pthread_condattr_t attr;
974 if (!td->o.use_thread) {
980 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
982 INIT_FLIST_HEAD(&td->io_u_freelist);
983 INIT_FLIST_HEAD(&td->io_u_busylist);
984 INIT_FLIST_HEAD(&td->io_u_requeues);
985 INIT_FLIST_HEAD(&td->io_log_list);
986 INIT_FLIST_HEAD(&td->io_hist_list);
987 INIT_FLIST_HEAD(&td->verify_list);
988 INIT_FLIST_HEAD(&td->trim_list);
989 pthread_mutex_init(&td->io_u_lock, NULL);
990 td->io_hist_tree = RB_ROOT;
992 pthread_condattr_init(&attr);
993 pthread_cond_init(&td->verify_cond, &attr);
994 pthread_cond_init(&td->free_cond, &attr);
996 td_set_runstate(td, TD_INITIALIZED);
997 dprint(FD_MUTEX, "up startup_mutex\n");
998 fio_mutex_up(startup_mutex);
999 dprint(FD_MUTEX, "wait on td->mutex\n");
1000 fio_mutex_down(td->mutex);
1001 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1004 * the ->mutex mutex is now no longer used, close it to avoid
1005 * eating a file descriptor
1007 fio_mutex_remove(td->mutex);
1010 * A new gid requires privilege, so we need to do this before setting
1013 if (td->o.gid != -1U && setgid(td->o.gid)) {
1014 td_verror(td, errno, "setgid");
1017 if (td->o.uid != -1U && setuid(td->o.uid)) {
1018 td_verror(td, errno, "setuid");
1023 * If we have a gettimeofday() thread, make sure we exclude that
1024 * thread from this job
1027 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1030 * Set affinity first, in case it has an impact on the memory
1033 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1034 td_verror(td, errno, "cpu_set_affinity");
1039 * May alter parameters that init_io_u() will use, so we need to
1048 if (td->o.verify_async && verify_async_init(td))
1051 if (td->ioprio_set) {
1052 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1053 td_verror(td, errno, "ioprio_set");
1058 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1062 if (nice(td->o.nice) == -1 && errno != 0) {
1063 td_verror(td, errno, "nice");
1067 if (td->o.ioscheduler && switch_ioscheduler(td))
1070 if (!td->o.create_serialize && setup_files(td))
1076 if (init_random_map(td))
1079 if (td->o.exec_prerun) {
1080 if (exec_string(td->o.exec_prerun))
1084 if (td->o.pre_read) {
1085 if (pre_read_files(td) < 0)
1089 fio_gettime(&td->epoch, NULL);
1090 getrusage(RUSAGE_SELF, &td->ru_start);
1093 while (keep_running(td)) {
1094 fio_gettime(&td->start, NULL);
1095 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1096 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1097 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1099 if (td->o.ratemin[0] || td->o.ratemin[1]) {
1100 memcpy(&td->lastrate[0], &td->bw_sample_time,
1101 sizeof(td->bw_sample_time));
1102 memcpy(&td->lastrate[1], &td->bw_sample_time,
1103 sizeof(td->bw_sample_time));
1109 prune_io_piece_log(td);
1115 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1116 elapsed = utime_since_now(&td->start);
1117 td->ts.runtime[DDIR_READ] += elapsed;
1119 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1120 elapsed = utime_since_now(&td->start);
1121 td->ts.runtime[DDIR_WRITE] += elapsed;
1124 if (td->error || td->terminate)
1127 if (!td->o.do_verify ||
1128 td->o.verify == VERIFY_NONE ||
1129 (td->io_ops->flags & FIO_UNIDIR))
1134 fio_gettime(&td->start, NULL);
1138 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1140 if (td->error || td->terminate)
1144 update_rusage_stat(td);
1145 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1146 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
1147 td->ts.total_run_time = mtime_since_now(&td->epoch);
1148 td->ts.io_bytes[0] = td->io_bytes[0];
1149 td->ts.io_bytes[1] = td->io_bytes[1];
1151 fio_mutex_down(writeout_mutex);
1153 if (td->o.bw_log_file) {
1154 finish_log_named(td, td->bw_log,
1155 td->o.bw_log_file, "bw");
1157 finish_log(td, td->bw_log, "bw");
1160 if (td->o.lat_log_file) {
1161 finish_log_named(td, td->lat_log,
1162 td->o.lat_log_file, "lat");
1164 finish_log(td, td->lat_log, "lat");
1167 if (td->o.lat_log_file) {
1168 finish_log_named(td, td->slat_log,
1169 td->o.lat_log_file, "slat");
1171 finish_log(td, td->slat_log, "slat");
1174 if (td->o.lat_log_file) {
1175 finish_log_named(td, td->clat_log,
1176 td->o.lat_log_file, "clat");
1178 finish_log(td, td->clat_log, "clat");
1181 if (td->o.iops_log_file) {
1182 finish_log_named(td, td->iops_log,
1183 td->o.iops_log_file, "iops");
1185 finish_log(td, td->iops_log, "iops");
1188 fio_mutex_up(writeout_mutex);
1189 if (td->o.exec_postrun)
1190 exec_string(td->o.exec_postrun);
1192 if (exitall_on_terminate)
1193 fio_terminate_threads(td->groupid);
1197 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1200 if (td->o.verify_async)
1201 verify_async_exit(td);
1203 close_and_free_files(td);
1206 cgroup_shutdown(td, &cgroup_mnt);
1208 if (td->o.cpumask_set) {
1209 int ret = fio_cpuset_exit(&td->o.cpumask);
1211 td_verror(td, ret, "fio_cpuset_exit");
1215 * do this very late, it will log file closing as well
1217 if (td->o.write_iolog_file)
1218 write_iolog_close(td);
1220 td_set_runstate(td, TD_EXITED);
1221 return (void *) (uintptr_t) td->error;
1226 * We cannot pass the td data into a forked process, so attach the td and
1227 * pass it to the thread worker.
1229 static int fork_main(int shmid, int offset)
1231 struct thread_data *td;
1235 data = shmat(shmid, NULL, 0);
1236 if (data == (void *) -1) {
1244 * HP-UX inherits shm mappings?
1249 td = data + offset * sizeof(struct thread_data);
1250 ret = thread_main(td);
1252 return (int) (uintptr_t) ret;
1256 * Run over the job map and reap the threads that have exited, if any.
1258 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1259 unsigned int *m_rate)
1261 struct thread_data *td;
1262 unsigned int cputhreads, realthreads, pending;
1266 * reap exited threads (TD_EXITED -> TD_REAPED)
1268 realthreads = pending = cputhreads = 0;
1269 for_each_td(td, i) {
1273 * ->io_ops is NULL for a thread that has closed its
1276 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1285 if (td->runstate == TD_REAPED)
1287 if (td->o.use_thread) {
1288 if (td->runstate == TD_EXITED) {
1289 td_set_runstate(td, TD_REAPED);
1296 if (td->runstate == TD_EXITED)
1300 * check if someone quit or got killed in an unusual way
1302 ret = waitpid(td->pid, &status, flags);
1304 if (errno == ECHILD) {
1305 log_err("fio: pid=%d disappeared %d\n",
1306 (int) td->pid, td->runstate);
1308 td_set_runstate(td, TD_REAPED);
1312 } else if (ret == td->pid) {
1313 if (WIFSIGNALED(status)) {
1314 int sig = WTERMSIG(status);
1317 log_err("fio: pid=%d, got signal=%d\n",
1318 (int) td->pid, sig);
1320 td_set_runstate(td, TD_REAPED);
1323 if (WIFEXITED(status)) {
1324 if (WEXITSTATUS(status) && !td->error)
1325 td->error = WEXITSTATUS(status);
1327 td_set_runstate(td, TD_REAPED);
1333 * thread is not dead, continue
1339 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1340 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
1347 done_secs += mtime_since_now(&td->epoch) / 1000;
1350 if (*nr_running == cputhreads && !pending && realthreads)
1351 fio_terminate_threads(TERMINATE_ALL);
1355 * Main function for kicking off and reaping jobs, as needed.
1357 static void run_threads(void)
1359 struct thread_data *td;
1360 unsigned long spent;
1361 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1363 if (fio_pin_memory())
1366 if (fio_gtod_offload && fio_start_gtod_thread())
1371 if (!terse_output) {
1372 log_info("Starting ");
1374 log_info("%d thread%s", nr_thread,
1375 nr_thread > 1 ? "s" : "");
1379 log_info("%d process%s", nr_process,
1380 nr_process > 1 ? "es" : "");
1386 todo = thread_number;
1389 m_rate = t_rate = 0;
1391 for_each_td(td, i) {
1392 print_status_init(td->thread_number - 1);
1394 if (!td->o.create_serialize)
1398 * do file setup here so it happens sequentially,
1399 * we don't want X number of threads getting their
1400 * client data interspersed on disk
1402 if (setup_files(td)) {
1405 log_err("fio: pid=%d, err=%d/%s\n",
1406 (int) td->pid, td->error, td->verror);
1407 td_set_runstate(td, TD_REAPED);
1414 * for sharing to work, each job must always open
1415 * its own files. so close them, if we opened them
1418 for_each_file(td, f, j) {
1419 if (fio_file_open(f))
1420 td_io_close_file(td, f);
1428 struct thread_data *map[REAL_MAX_JOBS];
1429 struct timeval this_start;
1430 int this_jobs = 0, left;
1433 * create threads (TD_NOT_CREATED -> TD_CREATED)
1435 for_each_td(td, i) {
1436 if (td->runstate != TD_NOT_CREATED)
1440 * never got a chance to start, killed by other
1441 * thread for some reason
1443 if (td->terminate) {
1448 if (td->o.start_delay) {
1449 spent = mtime_since_genesis();
1451 if (td->o.start_delay * 1000 > spent)
1455 if (td->o.stonewall && (nr_started || nr_running)) {
1456 dprint(FD_PROCESS, "%s: stonewall wait\n",
1464 * Set state to created. Thread will transition
1465 * to TD_INITIALIZED when it's done setting up.
1467 td_set_runstate(td, TD_CREATED);
1468 map[this_jobs++] = td;
1471 if (td->o.use_thread) {
1474 dprint(FD_PROCESS, "will pthread_create\n");
1475 ret = pthread_create(&td->thread, NULL,
1478 log_err("pthread_create: %s\n",
1483 ret = pthread_detach(td->thread);
1485 log_err("pthread_detach: %s",
1489 dprint(FD_PROCESS, "will fork\n");
1492 int ret = fork_main(shm_id, i);
1495 } else if (i == fio_debug_jobno)
1496 *fio_debug_jobp = pid;
1498 dprint(FD_MUTEX, "wait on startup_mutex\n");
1499 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1500 log_err("fio: job startup hung? exiting.\n");
1501 fio_terminate_threads(TERMINATE_ALL);
1506 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1510 * Wait for the started threads to transition to
1513 fio_gettime(&this_start, NULL);
1515 while (left && !fio_abort) {
1516 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1521 for (i = 0; i < this_jobs; i++) {
1525 if (td->runstate == TD_INITIALIZED) {
1528 } else if (td->runstate >= TD_EXITED) {
1532 nr_running++; /* work-around... */
1538 log_err("fio: %d job%s failed to start\n", left,
1539 left > 1 ? "s" : "");
1540 for (i = 0; i < this_jobs; i++) {
1544 kill(td->pid, SIGTERM);
1550 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1552 for_each_td(td, i) {
1553 if (td->runstate != TD_INITIALIZED)
1556 if (in_ramp_time(td))
1557 td_set_runstate(td, TD_RAMP);
1559 td_set_runstate(td, TD_RUNNING);
1562 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1563 t_rate += td->o.rate[0] + td->o.rate[1];
1565 fio_mutex_up(td->mutex);
1568 reap_threads(&nr_running, &t_rate, &m_rate);
1572 fio_server_idle_loop();
1578 while (nr_running) {
1579 reap_threads(&nr_running, &t_rate, &m_rate);
1582 fio_server_idle_loop();
1591 static void *disk_thread_main(void *data)
1593 fio_mutex_up(startup_mutex);
1596 usleep(DISK_UTIL_MSEC * 1000);
1602 print_thread_status();
1608 static int create_disk_util_thread(void)
1612 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1614 log_err("Can't create disk util thread: %s\n", strerror(ret));
1618 ret = pthread_detach(disk_util_thread);
1620 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1624 dprint(FD_MUTEX, "wait on startup_mutex\n");
1625 fio_mutex_down(startup_mutex);
1626 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1630 int fio_backend(void)
1632 struct thread_data *td;
1636 if (load_profile(exec_profile))
1639 exec_profile = NULL;
1645 setup_log(&agg_io_log[DDIR_READ], 0);
1646 setup_log(&agg_io_log[DDIR_WRITE], 0);
1649 startup_mutex = fio_mutex_init(0);
1650 if (startup_mutex == NULL)
1652 writeout_mutex = fio_mutex_init(1);
1653 if (writeout_mutex == NULL)
1657 create_disk_util_thread();
1659 cgroup_list = smalloc(sizeof(*cgroup_list));
1660 INIT_FLIST_HEAD(cgroup_list);
1667 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1668 __finish_log(agg_io_log[DDIR_WRITE],
1669 "agg-write_bw.log");
1674 fio_options_free(td);
1677 cgroup_kill(cgroup_list);
1681 fio_mutex_remove(startup_mutex);
1682 fio_mutex_remove(writeout_mutex);