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
51 static pthread_t disk_util_thread;
52 static struct fio_mutex *startup_mutex;
53 static struct fio_mutex *writeout_mutex;
54 static struct flist_head *cgroup_list;
55 static char *cgroup_mnt;
56 static int exit_value;
57 static volatile int fio_abort;
59 struct io_log *agg_io_log[2];
62 unsigned int thread_number = 0;
63 unsigned int nr_process = 0;
64 unsigned int nr_thread = 0;
67 unsigned long done_secs = 0;
69 #define PAGE_ALIGN(buf) \
70 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
72 #define JOB_START_TIMEOUT (5 * 1000)
74 static void sig_int(int sig)
78 fio_server_got_signal(sig);
80 log_info("\nfio: terminating on signal %d\n", sig);
85 fio_terminate_threads(TERMINATE_ALL);
89 static void set_sig_handlers(void)
93 memset(&act, 0, sizeof(act));
94 act.sa_handler = sig_int;
95 act.sa_flags = SA_RESTART;
96 sigaction(SIGINT, &act, NULL);
98 memset(&act, 0, sizeof(act));
99 act.sa_handler = sig_int;
100 act.sa_flags = SA_RESTART;
101 sigaction(SIGTERM, &act, NULL);
104 memset(&act, 0, sizeof(act));
105 act.sa_handler = sig_int;
106 act.sa_flags = SA_RESTART;
107 sigaction(SIGPIPE, &act, NULL);
112 * Check if we are above the minimum rate given.
114 static int __check_min_rate(struct thread_data *td, struct timeval *now,
117 unsigned long long bytes = 0;
118 unsigned long iops = 0;
121 unsigned int ratemin = 0;
122 unsigned int rate_iops = 0;
123 unsigned int rate_iops_min = 0;
125 assert(ddir_rw(ddir));
127 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
131 * allow a 2 second settle period in the beginning
133 if (mtime_since(&td->start, now) < 2000)
136 iops += td->this_io_blocks[ddir];
137 bytes += td->this_io_bytes[ddir];
138 ratemin += td->o.ratemin[ddir];
139 rate_iops += td->o.rate_iops[ddir];
140 rate_iops_min += td->o.rate_iops_min[ddir];
143 * if rate blocks is set, sample is running
145 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
146 spent = mtime_since(&td->lastrate[ddir], now);
147 if (spent < td->o.ratecycle)
150 if (td->o.rate[ddir]) {
152 * check bandwidth specified rate
154 if (bytes < td->rate_bytes[ddir]) {
155 log_err("%s: min rate %u not met\n", td->o.name,
159 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
160 if (rate < ratemin ||
161 bytes < td->rate_bytes[ddir]) {
162 log_err("%s: min rate %u not met, got"
163 " %luKB/sec\n", td->o.name,
170 * checks iops specified rate
172 if (iops < rate_iops) {
173 log_err("%s: min iops rate %u not met\n",
174 td->o.name, rate_iops);
177 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
178 if (rate < rate_iops_min ||
179 iops < td->rate_blocks[ddir]) {
180 log_err("%s: min iops rate %u not met,"
181 " got %lu\n", td->o.name,
182 rate_iops_min, rate);
188 td->rate_bytes[ddir] = bytes;
189 td->rate_blocks[ddir] = iops;
190 memcpy(&td->lastrate[ddir], now, sizeof(*now));
194 static int check_min_rate(struct thread_data *td, struct timeval *now,
195 unsigned long *bytes_done)
200 ret |= __check_min_rate(td, now, 0);
202 ret |= __check_min_rate(td, now, 1);
208 * When job exits, we can cancel the in-flight IO if we are using async
209 * io. Attempt to do so.
211 static void cleanup_pending_aio(struct thread_data *td)
213 struct flist_head *entry, *n;
218 * get immediately available events, if any
220 r = io_u_queued_complete(td, 0, NULL);
225 * now cancel remaining active events
227 if (td->io_ops->cancel) {
228 flist_for_each_safe(entry, n, &td->io_u_busylist) {
229 io_u = flist_entry(entry, struct io_u, list);
232 * if the io_u isn't in flight, then that generally
233 * means someone leaked an io_u. complain but fix
234 * it up, so we don't stall here.
236 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
237 log_err("fio: non-busy IO on busy list\n");
240 r = td->io_ops->cancel(td, io_u);
248 r = io_u_queued_complete(td, td->cur_depth, NULL);
252 * Helper to handle the final sync of a file. Works just like the normal
253 * io path, just does everything sync.
255 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
257 struct io_u *io_u = __get_io_u(td);
263 io_u->ddir = DDIR_SYNC;
266 if (td_io_prep(td, io_u)) {
272 ret = td_io_queue(td, io_u);
274 td_verror(td, io_u->error, "td_io_queue");
277 } else if (ret == FIO_Q_QUEUED) {
278 if (io_u_queued_complete(td, 1, NULL) < 0)
280 } else if (ret == FIO_Q_COMPLETED) {
282 td_verror(td, io_u->error, "td_io_queue");
286 if (io_u_sync_complete(td, io_u, NULL) < 0)
288 } else if (ret == FIO_Q_BUSY) {
289 if (td_io_commit(td))
297 static inline void __update_tv_cache(struct thread_data *td)
299 fio_gettime(&td->tv_cache, NULL);
302 static inline void update_tv_cache(struct thread_data *td)
304 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
305 __update_tv_cache(td);
308 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
310 if (in_ramp_time(td))
314 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
320 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
325 if (ret < 0 || td->error) {
333 if (!(td->o.continue_on_error & td_error_type(ddir, err)))
336 if (td_non_fatal_error(err)) {
338 * Continue with the I/Os in case of
341 update_error_count(td, err);
345 } else if (td->o.fill_device && err == ENOSPC) {
347 * We expect to hit this error if
348 * fill_device option is set.
355 * Stop the I/O in case of a fatal
358 update_error_count(td, err);
367 * The main verify engine. Runs over the writes we previously submitted,
368 * reads the blocks back in, and checks the crc/md5 of the data.
370 static void do_verify(struct thread_data *td)
377 dprint(FD_VERIFY, "starting loop\n");
380 * sync io first and invalidate cache, to make sure we really
383 for_each_file(td, f, i) {
384 if (!fio_file_open(f))
386 if (fio_io_sync(td, f))
388 if (file_invalidate_cache(td, f))
395 td_set_runstate(td, TD_VERIFYING);
398 while (!td->terminate) {
403 if (runtime_exceeded(td, &td->tv_cache)) {
404 __update_tv_cache(td);
405 if (runtime_exceeded(td, &td->tv_cache)) {
411 io_u = __get_io_u(td);
415 if (get_next_verify(td, io_u)) {
420 if (td_io_prep(td, io_u)) {
425 if (td->o.verify_async)
426 io_u->end_io = verify_io_u_async;
428 io_u->end_io = verify_io_u;
430 ret = td_io_queue(td, io_u);
432 case FIO_Q_COMPLETED:
435 clear_io_u(td, io_u);
436 } else if (io_u->resid) {
437 int bytes = io_u->xfer_buflen - io_u->resid;
443 td_verror(td, EIO, "full resid");
448 io_u->xfer_buflen = io_u->resid;
449 io_u->xfer_buf += bytes;
450 io_u->offset += bytes;
452 if (ddir_rw(io_u->ddir))
453 td->ts.short_io_u[io_u->ddir]++;
456 if (io_u->offset == f->real_file_size)
459 requeue_io_u(td, &io_u);
462 ret = io_u_sync_complete(td, io_u, NULL);
470 requeue_io_u(td, &io_u);
471 ret2 = td_io_commit(td);
477 td_verror(td, -ret, "td_io_queue");
481 if (break_on_this_error(td, io_u->ddir, &ret))
485 * if we can queue more, do so. but check if there are
486 * completed io_u's first. Note that we can get BUSY even
487 * without IO queued, if the system is resource starved.
489 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
490 if (full || !td->o.iodepth_batch_complete) {
491 min_events = min(td->o.iodepth_batch_complete,
493 if (full && !min_events && td->o.iodepth_batch_complete != 0)
498 * Reap required number of io units, if any,
499 * and do the verification on them through
500 * the callback handler
502 if (io_u_queued_complete(td, min_events, NULL) < 0) {
506 } while (full && (td->cur_depth > td->o.iodepth_low));
513 min_events = td->cur_depth;
516 ret = io_u_queued_complete(td, min_events, NULL);
518 cleanup_pending_aio(td);
520 td_set_runstate(td, TD_RUNNING);
522 dprint(FD_VERIFY, "exiting loop\n");
526 * Main IO worker function. It retrieves io_u's to process and queues
527 * and reaps them, checking for rate and errors along the way.
529 static void do_io(struct thread_data *td)
534 if (in_ramp_time(td))
535 td_set_runstate(td, TD_RAMP);
537 td_set_runstate(td, TD_RUNNING);
539 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
540 (!flist_empty(&td->trim_list)) ||
541 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
542 struct timeval comp_time;
543 unsigned long bytes_done[2] = { 0, 0 };
554 if (runtime_exceeded(td, &td->tv_cache)) {
555 __update_tv_cache(td);
556 if (runtime_exceeded(td, &td->tv_cache)) {
569 * Add verification end_io handler, if asked to verify
570 * a previously written file.
572 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
574 if (td->o.verify_async)
575 io_u->end_io = verify_io_u_async;
577 io_u->end_io = verify_io_u;
578 td_set_runstate(td, TD_VERIFYING);
579 } else if (in_ramp_time(td))
580 td_set_runstate(td, TD_RAMP);
582 td_set_runstate(td, TD_RUNNING);
584 ret = td_io_queue(td, io_u);
586 case FIO_Q_COMPLETED:
589 clear_io_u(td, io_u);
590 } else if (io_u->resid) {
591 int bytes = io_u->xfer_buflen - io_u->resid;
592 struct fio_file *f = io_u->file;
598 td_verror(td, EIO, "full resid");
603 io_u->xfer_buflen = io_u->resid;
604 io_u->xfer_buf += bytes;
605 io_u->offset += bytes;
607 if (ddir_rw(io_u->ddir))
608 td->ts.short_io_u[io_u->ddir]++;
610 if (io_u->offset == f->real_file_size)
613 requeue_io_u(td, &io_u);
616 if (__should_check_rate(td, 0) ||
617 __should_check_rate(td, 1))
618 fio_gettime(&comp_time, NULL);
620 ret = io_u_sync_complete(td, io_u, bytes_done);
627 * if the engine doesn't have a commit hook,
628 * the io_u is really queued. if it does have such
629 * a hook, it has to call io_u_queued() itself.
631 if (td->io_ops->commit == NULL)
632 io_u_queued(td, io_u);
635 requeue_io_u(td, &io_u);
636 ret2 = td_io_commit(td);
646 if (break_on_this_error(td, ddir, &ret))
650 * See if we need to complete some commands. Note that we
651 * can get BUSY even without IO queued, if the system is
654 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
655 if (full || !td->o.iodepth_batch_complete) {
656 min_evts = min(td->o.iodepth_batch_complete,
658 if (full && !min_evts && td->o.iodepth_batch_complete != 0)
661 if (__should_check_rate(td, 0) ||
662 __should_check_rate(td, 1))
663 fio_gettime(&comp_time, NULL);
666 ret = io_u_queued_complete(td, min_evts, bytes_done);
670 } while (full && (td->cur_depth > td->o.iodepth_low));
675 if (!(bytes_done[0] + bytes_done[1]))
678 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
679 if (check_min_rate(td, &comp_time, bytes_done)) {
680 if (exitall_on_terminate)
681 fio_terminate_threads(td->groupid);
682 td_verror(td, EIO, "check_min_rate");
687 if (td->o.thinktime) {
688 unsigned long long b;
690 b = td->io_blocks[0] + td->io_blocks[1];
691 if (!(b % td->o.thinktime_blocks)) {
694 if (td->o.thinktime_spin)
695 usec_spin(td->o.thinktime_spin);
697 left = td->o.thinktime - td->o.thinktime_spin;
699 usec_sleep(td, left);
704 if (td->trim_entries)
705 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
707 if (td->o.fill_device && td->error == ENOSPC) {
716 ret = io_u_queued_complete(td, i, NULL);
717 if (td->o.fill_device && td->error == ENOSPC)
721 if (should_fsync(td) && td->o.end_fsync) {
722 td_set_runstate(td, TD_FSYNCING);
724 for_each_file(td, f, i) {
725 if (!fio_file_open(f))
731 cleanup_pending_aio(td);
734 * stop job if we failed doing any IO
736 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
740 static void cleanup_io_u(struct thread_data *td)
742 struct flist_head *entry, *n;
745 flist_for_each_safe(entry, n, &td->io_u_freelist) {
746 io_u = flist_entry(entry, struct io_u, list);
748 flist_del(&io_u->list);
749 fio_memfree(io_u, sizeof(*io_u));
755 static int init_io_u(struct thread_data *td)
759 int cl_align, i, max_units;
762 max_units = td->o.iodepth;
763 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
764 td->orig_buffer_size = (unsigned long long) max_bs
765 * (unsigned long long) max_units;
767 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
770 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
771 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
774 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
775 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
779 if (allocate_io_mem(td))
782 if (td->o.odirect || td->o.mem_align ||
783 (td->io_ops->flags & FIO_RAWIO))
784 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
788 cl_align = os_cache_line_size();
790 for (i = 0; i < max_units; i++) {
796 ptr = fio_memalign(cl_align, sizeof(*io_u));
798 log_err("fio: unable to allocate aligned memory\n");
803 memset(io_u, 0, sizeof(*io_u));
804 INIT_FLIST_HEAD(&io_u->list);
805 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
807 if (!(td->io_ops->flags & FIO_NOIO)) {
809 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
812 io_u_fill_buffer(td, io_u, max_bs);
813 if (td_write(td) && td->o.verify_pattern_bytes) {
815 * Fill the buffer with the pattern if we are
816 * going to be doing writes.
818 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
823 io_u->flags = IO_U_F_FREE;
824 flist_add(&io_u->list, &td->io_u_freelist);
831 static int switch_ioscheduler(struct thread_data *td)
833 char tmp[256], tmp2[128];
837 if (td->io_ops->flags & FIO_DISKLESSIO)
840 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
842 f = fopen(tmp, "r+");
844 if (errno == ENOENT) {
845 log_err("fio: os or kernel doesn't support IO scheduler"
849 td_verror(td, errno, "fopen iosched");
856 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
857 if (ferror(f) || ret != 1) {
858 td_verror(td, errno, "fwrite");
866 * Read back and check that the selected scheduler is now the default.
868 ret = fread(tmp, 1, sizeof(tmp), f);
869 if (ferror(f) || ret < 0) {
870 td_verror(td, errno, "fread");
875 sprintf(tmp2, "[%s]", td->o.ioscheduler);
876 if (!strstr(tmp, tmp2)) {
877 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
878 td_verror(td, EINVAL, "iosched_switch");
887 static int keep_running(struct thread_data *td)
889 unsigned long long io_done;
893 if (td->o.time_based)
900 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
902 if (io_done < td->o.size)
908 static int exec_string(const char *string)
910 int ret, newlen = strlen(string) + 1 + 8;
913 str = malloc(newlen);
914 sprintf(str, "sh -c %s", string);
918 log_err("fio: exec of cmd <%s> failed\n", str);
925 * Entry point for the thread based jobs. The process based jobs end up
926 * here as well, after a little setup.
928 static void *thread_main(void *data)
930 unsigned long long elapsed;
931 struct thread_data *td = data;
932 pthread_condattr_t attr;
935 if (!td->o.use_thread) {
941 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
943 INIT_FLIST_HEAD(&td->io_u_freelist);
944 INIT_FLIST_HEAD(&td->io_u_busylist);
945 INIT_FLIST_HEAD(&td->io_u_requeues);
946 INIT_FLIST_HEAD(&td->io_log_list);
947 INIT_FLIST_HEAD(&td->io_hist_list);
948 INIT_FLIST_HEAD(&td->verify_list);
949 INIT_FLIST_HEAD(&td->trim_list);
950 pthread_mutex_init(&td->io_u_lock, NULL);
951 td->io_hist_tree = RB_ROOT;
953 pthread_condattr_init(&attr);
954 pthread_cond_init(&td->verify_cond, &attr);
955 pthread_cond_init(&td->free_cond, &attr);
957 td_set_runstate(td, TD_INITIALIZED);
958 dprint(FD_MUTEX, "up startup_mutex\n");
959 fio_mutex_up(startup_mutex);
960 dprint(FD_MUTEX, "wait on td->mutex\n");
961 fio_mutex_down(td->mutex);
962 dprint(FD_MUTEX, "done waiting on td->mutex\n");
965 * the ->mutex mutex is now no longer used, close it to avoid
966 * eating a file descriptor
968 fio_mutex_remove(td->mutex);
971 * A new gid requires privilege, so we need to do this before setting
974 if (td->o.gid != -1U && setgid(td->o.gid)) {
975 td_verror(td, errno, "setgid");
978 if (td->o.uid != -1U && setuid(td->o.uid)) {
979 td_verror(td, errno, "setuid");
984 * If we have a gettimeofday() thread, make sure we exclude that
985 * thread from this job
988 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
991 * Set affinity first, in case it has an impact on the memory
994 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
995 td_verror(td, errno, "cpu_set_affinity");
1000 * May alter parameters that init_io_u() will use, so we need to
1009 if (td->o.verify_async && verify_async_init(td))
1012 if (td->ioprio_set) {
1013 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1014 td_verror(td, errno, "ioprio_set");
1019 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1022 if (nice(td->o.nice) == -1) {
1023 td_verror(td, errno, "nice");
1027 if (td->o.ioscheduler && switch_ioscheduler(td))
1030 if (!td->o.create_serialize && setup_files(td))
1036 if (init_random_map(td))
1039 if (td->o.exec_prerun) {
1040 if (exec_string(td->o.exec_prerun))
1044 if (td->o.pre_read) {
1045 if (pre_read_files(td) < 0)
1049 fio_gettime(&td->epoch, NULL);
1050 getrusage(RUSAGE_SELF, &td->ru_start);
1053 while (keep_running(td)) {
1054 fio_gettime(&td->start, NULL);
1055 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1056 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1057 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1059 if (td->o.ratemin[0] || td->o.ratemin[1]) {
1060 memcpy(&td->lastrate[0], &td->bw_sample_time,
1061 sizeof(td->bw_sample_time));
1062 memcpy(&td->lastrate[1], &td->bw_sample_time,
1063 sizeof(td->bw_sample_time));
1069 prune_io_piece_log(td);
1075 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1076 elapsed = utime_since_now(&td->start);
1077 td->ts.runtime[DDIR_READ] += elapsed;
1079 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1080 elapsed = utime_since_now(&td->start);
1081 td->ts.runtime[DDIR_WRITE] += elapsed;
1084 if (td->error || td->terminate)
1087 if (!td->o.do_verify ||
1088 td->o.verify == VERIFY_NONE ||
1089 (td->io_ops->flags & FIO_UNIDIR))
1094 fio_gettime(&td->start, NULL);
1098 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1100 if (td->error || td->terminate)
1104 update_rusage_stat(td);
1105 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1106 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
1107 td->ts.total_run_time = mtime_since_now(&td->epoch);
1108 td->ts.io_bytes[0] = td->io_bytes[0];
1109 td->ts.io_bytes[1] = td->io_bytes[1];
1111 fio_mutex_down(writeout_mutex);
1113 if (td->o.bw_log_file) {
1114 finish_log_named(td, td->bw_log,
1115 td->o.bw_log_file, "bw");
1117 finish_log(td, td->bw_log, "bw");
1120 if (td->o.lat_log_file) {
1121 finish_log_named(td, td->lat_log,
1122 td->o.lat_log_file, "lat");
1124 finish_log(td, td->lat_log, "lat");
1127 if (td->o.lat_log_file) {
1128 finish_log_named(td, td->slat_log,
1129 td->o.lat_log_file, "slat");
1131 finish_log(td, td->slat_log, "slat");
1134 if (td->o.lat_log_file) {
1135 finish_log_named(td, td->clat_log,
1136 td->o.lat_log_file, "clat");
1138 finish_log(td, td->clat_log, "clat");
1141 if (td->o.iops_log_file) {
1142 finish_log_named(td, td->iops_log,
1143 td->o.iops_log_file, "iops");
1145 finish_log(td, td->iops_log, "iops");
1148 fio_mutex_up(writeout_mutex);
1149 if (td->o.exec_postrun)
1150 exec_string(td->o.exec_postrun);
1152 if (exitall_on_terminate)
1153 fio_terminate_threads(td->groupid);
1157 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1160 if (td->o.verify_async)
1161 verify_async_exit(td);
1163 close_and_free_files(td);
1166 cgroup_shutdown(td, &cgroup_mnt);
1168 if (td->o.cpumask_set) {
1169 int ret = fio_cpuset_exit(&td->o.cpumask);
1171 td_verror(td, ret, "fio_cpuset_exit");
1175 * do this very late, it will log file closing as well
1177 if (td->o.write_iolog_file)
1178 write_iolog_close(td);
1180 td_set_runstate(td, TD_EXITED);
1181 return (void *) (unsigned long) td->error;
1186 * We cannot pass the td data into a forked process, so attach the td and
1187 * pass it to the thread worker.
1189 static int fork_main(int shmid, int offset)
1191 struct thread_data *td;
1195 data = shmat(shmid, NULL, 0);
1196 if (data == (void *) -1) {
1204 * HP-UX inherits shm mappings?
1209 td = data + offset * sizeof(struct thread_data);
1210 ret = thread_main(td);
1212 return (int) (unsigned long) ret;
1216 * Run over the job map and reap the threads that have exited, if any.
1218 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1219 unsigned int *m_rate)
1221 struct thread_data *td;
1222 unsigned int cputhreads, realthreads, pending;
1226 * reap exited threads (TD_EXITED -> TD_REAPED)
1228 realthreads = pending = cputhreads = 0;
1229 for_each_td(td, i) {
1233 * ->io_ops is NULL for a thread that has closed its
1236 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1245 if (td->runstate == TD_REAPED)
1247 if (td->o.use_thread) {
1248 if (td->runstate == TD_EXITED) {
1249 td_set_runstate(td, TD_REAPED);
1256 if (td->runstate == TD_EXITED)
1260 * check if someone quit or got killed in an unusual way
1262 ret = waitpid(td->pid, &status, flags);
1264 if (errno == ECHILD) {
1265 log_err("fio: pid=%d disappeared %d\n",
1266 (int) td->pid, td->runstate);
1267 td_set_runstate(td, TD_REAPED);
1271 } else if (ret == td->pid) {
1272 if (WIFSIGNALED(status)) {
1273 int sig = WTERMSIG(status);
1276 log_err("fio: pid=%d, got signal=%d\n",
1277 (int) td->pid, sig);
1278 td_set_runstate(td, TD_REAPED);
1281 if (WIFEXITED(status)) {
1282 if (WEXITSTATUS(status) && !td->error)
1283 td->error = WEXITSTATUS(status);
1285 td_set_runstate(td, TD_REAPED);
1291 * thread is not dead, continue
1297 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1298 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
1305 done_secs += mtime_since_now(&td->epoch) / 1000;
1308 if (*nr_running == cputhreads && !pending && realthreads)
1309 fio_terminate_threads(TERMINATE_ALL);
1313 * Main function for kicking off and reaping jobs, as needed.
1315 static void run_threads(void)
1317 struct thread_data *td;
1318 unsigned long spent;
1319 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1321 if (fio_pin_memory())
1324 if (fio_gtod_offload && fio_start_gtod_thread())
1329 if (!terse_output) {
1330 log_info("Starting ");
1332 log_info("%d thread%s", nr_thread,
1333 nr_thread > 1 ? "s" : "");
1337 log_info("%d process%s", nr_process,
1338 nr_process > 1 ? "es" : "");
1344 todo = thread_number;
1347 m_rate = t_rate = 0;
1349 for_each_td(td, i) {
1350 print_status_init(td->thread_number - 1);
1352 if (!td->o.create_serialize)
1356 * do file setup here so it happens sequentially,
1357 * we don't want X number of threads getting their
1358 * client data interspersed on disk
1360 if (setup_files(td)) {
1363 log_err("fio: pid=%d, err=%d/%s\n",
1364 (int) td->pid, td->error, td->verror);
1365 td_set_runstate(td, TD_REAPED);
1372 * for sharing to work, each job must always open
1373 * its own files. so close them, if we opened them
1376 for_each_file(td, f, j) {
1377 if (fio_file_open(f))
1378 td_io_close_file(td, f);
1386 struct thread_data *map[REAL_MAX_JOBS];
1387 struct timeval this_start;
1388 int this_jobs = 0, left;
1391 * create threads (TD_NOT_CREATED -> TD_CREATED)
1393 for_each_td(td, i) {
1394 if (td->runstate != TD_NOT_CREATED)
1398 * never got a chance to start, killed by other
1399 * thread for some reason
1401 if (td->terminate) {
1406 if (td->o.start_delay) {
1407 spent = mtime_since_genesis();
1409 if (td->o.start_delay * 1000 > spent)
1413 if (td->o.stonewall && (nr_started || nr_running)) {
1414 dprint(FD_PROCESS, "%s: stonewall wait\n",
1422 * Set state to created. Thread will transition
1423 * to TD_INITIALIZED when it's done setting up.
1425 td_set_runstate(td, TD_CREATED);
1426 map[this_jobs++] = td;
1429 if (td->o.use_thread) {
1432 dprint(FD_PROCESS, "will pthread_create\n");
1433 ret = pthread_create(&td->thread, NULL,
1436 log_err("pthread_create: %s\n",
1441 ret = pthread_detach(td->thread);
1443 log_err("pthread_detach: %s",
1447 dprint(FD_PROCESS, "will fork\n");
1450 int ret = fork_main(shm_id, i);
1453 } else if (i == fio_debug_jobno)
1454 *fio_debug_jobp = pid;
1456 dprint(FD_MUTEX, "wait on startup_mutex\n");
1457 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1458 log_err("fio: job startup hung? exiting.\n");
1459 fio_terminate_threads(TERMINATE_ALL);
1464 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1468 * Wait for the started threads to transition to
1471 fio_gettime(&this_start, NULL);
1473 while (left && !fio_abort) {
1474 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1479 for (i = 0; i < this_jobs; i++) {
1483 if (td->runstate == TD_INITIALIZED) {
1486 } else if (td->runstate >= TD_EXITED) {
1490 nr_running++; /* work-around... */
1496 log_err("fio: %d jobs failed to start\n", left);
1497 for (i = 0; i < this_jobs; i++) {
1501 kill(td->pid, SIGTERM);
1507 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1509 for_each_td(td, i) {
1510 if (td->runstate != TD_INITIALIZED)
1513 if (in_ramp_time(td))
1514 td_set_runstate(td, TD_RAMP);
1516 td_set_runstate(td, TD_RUNNING);
1519 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1520 t_rate += td->o.rate[0] + td->o.rate[1];
1522 fio_mutex_up(td->mutex);
1525 reap_threads(&nr_running, &t_rate, &m_rate);
1529 fio_server_idle_loop();
1535 while (nr_running) {
1536 reap_threads(&nr_running, &t_rate, &m_rate);
1539 fio_server_idle_loop();
1548 static void *disk_thread_main(void *data)
1550 fio_mutex_up(startup_mutex);
1553 usleep(DISK_UTIL_MSEC * 1000);
1559 print_thread_status();
1565 static int create_disk_util_thread(void)
1569 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1571 log_err("Can't create disk util thread: %s\n", strerror(ret));
1575 ret = pthread_detach(disk_util_thread);
1577 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1581 dprint(FD_MUTEX, "wait on startup_mutex\n");
1582 fio_mutex_down(startup_mutex);
1583 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1587 int fio_backend(void)
1589 struct thread_data *td;
1593 if (load_profile(exec_profile))
1596 exec_profile = NULL;
1602 setup_log(&agg_io_log[DDIR_READ], 0);
1603 setup_log(&agg_io_log[DDIR_WRITE], 0);
1606 startup_mutex = fio_mutex_init(0);
1607 if (startup_mutex == NULL)
1609 writeout_mutex = fio_mutex_init(1);
1610 if (writeout_mutex == NULL)
1614 create_disk_util_thread();
1616 cgroup_list = smalloc(sizeof(*cgroup_list));
1617 INIT_FLIST_HEAD(cgroup_list);
1624 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1625 __finish_log(agg_io_log[DDIR_WRITE],
1626 "agg-write_bw.log");
1631 fio_options_free(td);
1633 cgroup_kill(cgroup_list);
1637 fio_mutex_remove(startup_mutex);
1638 fio_mutex_remove(writeout_mutex);