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;
59 static unsigned int nr_process = 0;
60 static unsigned int nr_thread = 0;
62 struct io_log *agg_io_log[2];
65 unsigned int thread_number = 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 set_sig_handlers(void)
94 memset(&act, 0, sizeof(act));
95 act.sa_handler = sig_int;
96 act.sa_flags = SA_RESTART;
97 sigaction(SIGINT, &act, NULL);
99 memset(&act, 0, sizeof(act));
100 act.sa_handler = sig_int;
101 act.sa_flags = SA_RESTART;
102 sigaction(SIGTERM, &act, NULL);
105 memset(&act, 0, sizeof(act));
106 act.sa_handler = sig_int;
107 act.sa_flags = SA_RESTART;
108 sigaction(SIGPIPE, &act, NULL);
113 * Check if we are above the minimum rate given.
115 static int __check_min_rate(struct thread_data *td, struct timeval *now,
118 unsigned long long bytes = 0;
119 unsigned long iops = 0;
122 unsigned int ratemin = 0;
123 unsigned int rate_iops = 0;
124 unsigned int rate_iops_min = 0;
126 assert(ddir_rw(ddir));
128 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
132 * allow a 2 second settle period in the beginning
134 if (mtime_since(&td->start, now) < 2000)
137 iops += td->this_io_blocks[ddir];
138 bytes += td->this_io_bytes[ddir];
139 ratemin += td->o.ratemin[ddir];
140 rate_iops += td->o.rate_iops[ddir];
141 rate_iops_min += td->o.rate_iops_min[ddir];
144 * if rate blocks is set, sample is running
146 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
147 spent = mtime_since(&td->lastrate[ddir], now);
148 if (spent < td->o.ratecycle)
151 if (td->o.rate[ddir]) {
153 * check bandwidth specified rate
155 if (bytes < td->rate_bytes[ddir]) {
156 log_err("%s: min rate %u not met\n", td->o.name,
160 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
161 if (rate < ratemin ||
162 bytes < td->rate_bytes[ddir]) {
163 log_err("%s: min rate %u not met, got"
164 " %luKB/sec\n", td->o.name,
171 * checks iops specified rate
173 if (iops < rate_iops) {
174 log_err("%s: min iops rate %u not met\n",
175 td->o.name, rate_iops);
178 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
179 if (rate < rate_iops_min ||
180 iops < td->rate_blocks[ddir]) {
181 log_err("%s: min iops rate %u not met,"
182 " got %lu\n", td->o.name,
183 rate_iops_min, rate);
189 td->rate_bytes[ddir] = bytes;
190 td->rate_blocks[ddir] = iops;
191 memcpy(&td->lastrate[ddir], now, sizeof(*now));
195 static int check_min_rate(struct thread_data *td, struct timeval *now,
196 unsigned long *bytes_done)
201 ret |= __check_min_rate(td, now, 0);
203 ret |= __check_min_rate(td, now, 1);
209 * When job exits, we can cancel the in-flight IO if we are using async
210 * io. Attempt to do so.
212 static void cleanup_pending_aio(struct thread_data *td)
214 struct flist_head *entry, *n;
219 * get immediately available events, if any
221 r = io_u_queued_complete(td, 0, NULL);
226 * now cancel remaining active events
228 if (td->io_ops->cancel) {
229 flist_for_each_safe(entry, n, &td->io_u_busylist) {
230 io_u = flist_entry(entry, struct io_u, list);
233 * if the io_u isn't in flight, then that generally
234 * means someone leaked an io_u. complain but fix
235 * it up, so we don't stall here.
237 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
238 log_err("fio: non-busy IO on busy list\n");
241 r = td->io_ops->cancel(td, io_u);
249 r = io_u_queued_complete(td, td->cur_depth, NULL);
253 * Helper to handle the final sync of a file. Works just like the normal
254 * io path, just does everything sync.
256 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
258 struct io_u *io_u = __get_io_u(td);
264 io_u->ddir = DDIR_SYNC;
267 if (td_io_prep(td, io_u)) {
273 ret = td_io_queue(td, io_u);
275 td_verror(td, io_u->error, "td_io_queue");
278 } else if (ret == FIO_Q_QUEUED) {
279 if (io_u_queued_complete(td, 1, NULL) < 0)
281 } else if (ret == FIO_Q_COMPLETED) {
283 td_verror(td, io_u->error, "td_io_queue");
287 if (io_u_sync_complete(td, io_u, NULL) < 0)
289 } else if (ret == FIO_Q_BUSY) {
290 if (td_io_commit(td))
298 static inline void __update_tv_cache(struct thread_data *td)
300 fio_gettime(&td->tv_cache, NULL);
303 static inline void update_tv_cache(struct thread_data *td)
305 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
306 __update_tv_cache(td);
309 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
311 if (in_ramp_time(td))
315 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
321 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
326 if (ret < 0 || td->error) {
334 if (!(td->o.continue_on_error & td_error_type(ddir, err)))
337 if (td_non_fatal_error(err)) {
339 * Continue with the I/Os in case of
342 update_error_count(td, err);
346 } else if (td->o.fill_device && err == ENOSPC) {
348 * We expect to hit this error if
349 * fill_device option is set.
356 * Stop the I/O in case of a fatal
359 update_error_count(td, err);
368 * The main verify engine. Runs over the writes we previously submitted,
369 * reads the blocks back in, and checks the crc/md5 of the data.
371 static void do_verify(struct thread_data *td)
378 dprint(FD_VERIFY, "starting loop\n");
381 * sync io first and invalidate cache, to make sure we really
384 for_each_file(td, f, i) {
385 if (!fio_file_open(f))
387 if (fio_io_sync(td, f))
389 if (file_invalidate_cache(td, f))
396 td_set_runstate(td, TD_VERIFYING);
399 while (!td->terminate) {
404 if (runtime_exceeded(td, &td->tv_cache)) {
405 __update_tv_cache(td);
406 if (runtime_exceeded(td, &td->tv_cache)) {
412 if (flow_threshold_exceeded(td))
415 io_u = __get_io_u(td);
419 if (get_next_verify(td, io_u)) {
424 if (td_io_prep(td, io_u)) {
429 if (td->o.verify_async)
430 io_u->end_io = verify_io_u_async;
432 io_u->end_io = verify_io_u;
434 ret = td_io_queue(td, io_u);
436 case FIO_Q_COMPLETED:
439 clear_io_u(td, io_u);
440 } else if (io_u->resid) {
441 int bytes = io_u->xfer_buflen - io_u->resid;
447 td_verror(td, EIO, "full resid");
452 io_u->xfer_buflen = io_u->resid;
453 io_u->xfer_buf += bytes;
454 io_u->offset += bytes;
456 if (ddir_rw(io_u->ddir))
457 td->ts.short_io_u[io_u->ddir]++;
460 if (io_u->offset == f->real_file_size)
463 requeue_io_u(td, &io_u);
466 ret = io_u_sync_complete(td, io_u, NULL);
474 requeue_io_u(td, &io_u);
475 ret2 = td_io_commit(td);
481 td_verror(td, -ret, "td_io_queue");
485 if (break_on_this_error(td, io_u->ddir, &ret))
489 * if we can queue more, do so. but check if there are
490 * completed io_u's first. Note that we can get BUSY even
491 * without IO queued, if the system is resource starved.
493 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
494 if (full || !td->o.iodepth_batch_complete) {
495 min_events = min(td->o.iodepth_batch_complete,
498 * if the queue is full, we MUST reap at least 1 event
500 if (full && !min_events)
505 * Reap required number of io units, if any,
506 * and do the verification on them through
507 * the callback handler
509 if (io_u_queued_complete(td, min_events, NULL) < 0) {
513 } while (full && (td->cur_depth > td->o.iodepth_low));
520 min_events = td->cur_depth;
523 ret = io_u_queued_complete(td, min_events, NULL);
525 cleanup_pending_aio(td);
527 td_set_runstate(td, TD_RUNNING);
529 dprint(FD_VERIFY, "exiting loop\n");
532 static int io_bytes_exceeded(struct thread_data *td)
534 unsigned long long bytes;
537 bytes = td->this_io_bytes[0] + td->this_io_bytes[1];
538 else if (td_write(td))
539 bytes = td->this_io_bytes[1];
541 bytes = td->this_io_bytes[0];
543 return bytes >= td->o.size;
547 * Main IO worker function. It retrieves io_u's to process and queues
548 * and reaps them, checking for rate and errors along the way.
550 static void do_io(struct thread_data *td)
555 if (in_ramp_time(td))
556 td_set_runstate(td, TD_RAMP);
558 td_set_runstate(td, TD_RUNNING);
560 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
561 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
563 struct timeval comp_time;
564 unsigned long bytes_done[2] = { 0, 0 };
575 if (runtime_exceeded(td, &td->tv_cache)) {
576 __update_tv_cache(td);
577 if (runtime_exceeded(td, &td->tv_cache)) {
583 if (flow_threshold_exceeded(td))
593 * Add verification end_io handler if:
594 * - Asked to verify (!td_rw(td))
595 * - Or the io_u is from our verify list (mixed write/ver)
597 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
598 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
599 if (td->o.verify_async)
600 io_u->end_io = verify_io_u_async;
602 io_u->end_io = verify_io_u;
603 td_set_runstate(td, TD_VERIFYING);
604 } else if (in_ramp_time(td))
605 td_set_runstate(td, TD_RAMP);
607 td_set_runstate(td, TD_RUNNING);
609 ret = td_io_queue(td, io_u);
611 case FIO_Q_COMPLETED:
614 clear_io_u(td, io_u);
615 } else if (io_u->resid) {
616 int bytes = io_u->xfer_buflen - io_u->resid;
617 struct fio_file *f = io_u->file;
623 td_verror(td, EIO, "full resid");
628 io_u->xfer_buflen = io_u->resid;
629 io_u->xfer_buf += bytes;
630 io_u->offset += bytes;
632 if (ddir_rw(io_u->ddir))
633 td->ts.short_io_u[io_u->ddir]++;
635 if (io_u->offset == f->real_file_size)
638 requeue_io_u(td, &io_u);
641 if (__should_check_rate(td, 0) ||
642 __should_check_rate(td, 1))
643 fio_gettime(&comp_time, NULL);
645 ret = io_u_sync_complete(td, io_u, bytes_done);
652 * if the engine doesn't have a commit hook,
653 * the io_u is really queued. if it does have such
654 * a hook, it has to call io_u_queued() itself.
656 if (td->io_ops->commit == NULL)
657 io_u_queued(td, io_u);
660 requeue_io_u(td, &io_u);
661 ret2 = td_io_commit(td);
671 if (break_on_this_error(td, ddir, &ret))
675 * See if we need to complete some commands. Note that we
676 * can get BUSY even without IO queued, if the system is
679 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
680 if (full || !td->o.iodepth_batch_complete) {
681 min_evts = min(td->o.iodepth_batch_complete,
684 * if the queue is full, we MUST reap at least 1 event
686 if (full && !min_evts)
689 if (__should_check_rate(td, 0) ||
690 __should_check_rate(td, 1))
691 fio_gettime(&comp_time, NULL);
694 ret = io_u_queued_complete(td, min_evts, bytes_done);
698 } while (full && (td->cur_depth > td->o.iodepth_low));
703 if (!(bytes_done[0] + bytes_done[1]))
706 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
707 if (check_min_rate(td, &comp_time, bytes_done)) {
708 if (exitall_on_terminate)
709 fio_terminate_threads(td->groupid);
710 td_verror(td, EIO, "check_min_rate");
715 if (td->o.thinktime) {
716 unsigned long long b;
718 b = td->io_blocks[0] + td->io_blocks[1];
719 if (!(b % td->o.thinktime_blocks)) {
722 if (td->o.thinktime_spin)
723 usec_spin(td->o.thinktime_spin);
725 left = td->o.thinktime - td->o.thinktime_spin;
727 usec_sleep(td, left);
732 if (td->trim_entries)
733 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
735 if (td->o.fill_device && td->error == ENOSPC) {
744 ret = io_u_queued_complete(td, i, NULL);
745 if (td->o.fill_device && td->error == ENOSPC)
749 if (should_fsync(td) && td->o.end_fsync) {
750 td_set_runstate(td, TD_FSYNCING);
752 for_each_file(td, f, i) {
753 if (!fio_file_open(f))
759 cleanup_pending_aio(td);
762 * stop job if we failed doing any IO
764 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
768 static void cleanup_io_u(struct thread_data *td)
770 struct flist_head *entry, *n;
773 flist_for_each_safe(entry, n, &td->io_u_freelist) {
774 io_u = flist_entry(entry, struct io_u, list);
776 flist_del(&io_u->list);
777 fio_memfree(io_u, sizeof(*io_u));
783 static int init_io_u(struct thread_data *td)
786 unsigned int max_bs, min_write;
787 int cl_align, i, max_units;
790 max_units = td->o.iodepth;
791 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
792 min_write = td->o.min_bs[DDIR_WRITE];
793 td->orig_buffer_size = (unsigned long long) max_bs
794 * (unsigned long long) max_units;
796 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
799 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
800 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
803 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
804 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
808 if (allocate_io_mem(td))
811 if (td->o.odirect || td->o.mem_align ||
812 (td->io_ops->flags & FIO_RAWIO))
813 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
817 cl_align = os_cache_line_size();
819 for (i = 0; i < max_units; i++) {
825 ptr = fio_memalign(cl_align, sizeof(*io_u));
827 log_err("fio: unable to allocate aligned memory\n");
832 memset(io_u, 0, sizeof(*io_u));
833 INIT_FLIST_HEAD(&io_u->list);
834 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
836 if (!(td->io_ops->flags & FIO_NOIO)) {
838 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
841 io_u_fill_buffer(td, io_u, min_write, max_bs);
842 if (td_write(td) && td->o.verify_pattern_bytes) {
844 * Fill the buffer with the pattern if we are
845 * going to be doing writes.
847 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
852 io_u->flags = IO_U_F_FREE;
853 flist_add(&io_u->list, &td->io_u_freelist);
860 static int switch_ioscheduler(struct thread_data *td)
862 char tmp[256], tmp2[128];
866 if (td->io_ops->flags & FIO_DISKLESSIO)
869 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
871 f = fopen(tmp, "r+");
873 if (errno == ENOENT) {
874 log_err("fio: os or kernel doesn't support IO scheduler"
878 td_verror(td, errno, "fopen iosched");
885 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
886 if (ferror(f) || ret != 1) {
887 td_verror(td, errno, "fwrite");
895 * Read back and check that the selected scheduler is now the default.
897 ret = fread(tmp, 1, sizeof(tmp), f);
898 if (ferror(f) || ret < 0) {
899 td_verror(td, errno, "fread");
904 sprintf(tmp2, "[%s]", td->o.ioscheduler);
905 if (!strstr(tmp, tmp2)) {
906 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
907 td_verror(td, EINVAL, "iosched_switch");
916 static int keep_running(struct thread_data *td)
918 unsigned long long io_done;
922 if (td->o.time_based)
929 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
931 if (io_done < td->o.size)
937 static int exec_string(const char *string)
939 int ret, newlen = strlen(string) + 1 + 8;
942 str = malloc(newlen);
943 sprintf(str, "sh -c %s", string);
947 log_err("fio: exec of cmd <%s> failed\n", str);
954 * Entry point for the thread based jobs. The process based jobs end up
955 * here as well, after a little setup.
957 static void *thread_main(void *data)
959 unsigned long long elapsed;
960 struct thread_data *td = data;
961 pthread_condattr_t attr;
964 if (!td->o.use_thread) {
970 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
973 fio_server_send_start(td);
975 INIT_FLIST_HEAD(&td->io_u_freelist);
976 INIT_FLIST_HEAD(&td->io_u_busylist);
977 INIT_FLIST_HEAD(&td->io_u_requeues);
978 INIT_FLIST_HEAD(&td->io_log_list);
979 INIT_FLIST_HEAD(&td->io_hist_list);
980 INIT_FLIST_HEAD(&td->verify_list);
981 INIT_FLIST_HEAD(&td->trim_list);
982 pthread_mutex_init(&td->io_u_lock, NULL);
983 td->io_hist_tree = RB_ROOT;
985 pthread_condattr_init(&attr);
986 pthread_cond_init(&td->verify_cond, &attr);
987 pthread_cond_init(&td->free_cond, &attr);
989 td_set_runstate(td, TD_INITIALIZED);
990 dprint(FD_MUTEX, "up startup_mutex\n");
991 fio_mutex_up(startup_mutex);
992 dprint(FD_MUTEX, "wait on td->mutex\n");
993 fio_mutex_down(td->mutex);
994 dprint(FD_MUTEX, "done waiting on td->mutex\n");
997 * the ->mutex mutex is now no longer used, close it to avoid
998 * eating a file descriptor
1000 fio_mutex_remove(td->mutex);
1004 * A new gid requires privilege, so we need to do this before setting
1007 if (td->o.gid != -1U && setgid(td->o.gid)) {
1008 td_verror(td, errno, "setgid");
1011 if (td->o.uid != -1U && setuid(td->o.uid)) {
1012 td_verror(td, errno, "setuid");
1017 * If we have a gettimeofday() thread, make sure we exclude that
1018 * thread from this job
1021 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1024 * Set affinity first, in case it has an impact on the memory
1027 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1028 td_verror(td, errno, "cpu_set_affinity");
1033 * May alter parameters that init_io_u() will use, so we need to
1042 if (td->o.verify_async && verify_async_init(td))
1045 if (td->ioprio_set) {
1046 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1047 td_verror(td, errno, "ioprio_set");
1052 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1056 if (nice(td->o.nice) == -1 && errno != 0) {
1057 td_verror(td, errno, "nice");
1061 if (td->o.ioscheduler && switch_ioscheduler(td))
1064 if (!td->o.create_serialize && setup_files(td))
1070 if (init_random_map(td))
1073 if (td->o.exec_prerun) {
1074 if (exec_string(td->o.exec_prerun))
1078 if (td->o.pre_read) {
1079 if (pre_read_files(td) < 0)
1083 fio_gettime(&td->epoch, NULL);
1084 getrusage(RUSAGE_SELF, &td->ru_start);
1087 while (keep_running(td)) {
1088 fio_gettime(&td->start, NULL);
1089 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1090 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1091 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1093 if (td->o.ratemin[0] || td->o.ratemin[1]) {
1094 memcpy(&td->lastrate[0], &td->bw_sample_time,
1095 sizeof(td->bw_sample_time));
1096 memcpy(&td->lastrate[1], &td->bw_sample_time,
1097 sizeof(td->bw_sample_time));
1103 prune_io_piece_log(td);
1109 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1110 elapsed = utime_since_now(&td->start);
1111 td->ts.runtime[DDIR_READ] += elapsed;
1113 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1114 elapsed = utime_since_now(&td->start);
1115 td->ts.runtime[DDIR_WRITE] += elapsed;
1118 if (td->error || td->terminate)
1121 if (!td->o.do_verify ||
1122 td->o.verify == VERIFY_NONE ||
1123 (td->io_ops->flags & FIO_UNIDIR))
1128 fio_gettime(&td->start, NULL);
1132 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1134 if (td->error || td->terminate)
1138 update_rusage_stat(td);
1139 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1140 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
1141 td->ts.total_run_time = mtime_since_now(&td->epoch);
1142 td->ts.io_bytes[0] = td->io_bytes[0];
1143 td->ts.io_bytes[1] = td->io_bytes[1];
1145 fio_mutex_down(writeout_mutex);
1147 if (td->o.bw_log_file) {
1148 finish_log_named(td, td->bw_log,
1149 td->o.bw_log_file, "bw");
1151 finish_log(td, td->bw_log, "bw");
1154 if (td->o.lat_log_file) {
1155 finish_log_named(td, td->lat_log,
1156 td->o.lat_log_file, "lat");
1158 finish_log(td, td->lat_log, "lat");
1161 if (td->o.lat_log_file) {
1162 finish_log_named(td, td->slat_log,
1163 td->o.lat_log_file, "slat");
1165 finish_log(td, td->slat_log, "slat");
1168 if (td->o.lat_log_file) {
1169 finish_log_named(td, td->clat_log,
1170 td->o.lat_log_file, "clat");
1172 finish_log(td, td->clat_log, "clat");
1175 if (td->o.iops_log_file) {
1176 finish_log_named(td, td->iops_log,
1177 td->o.iops_log_file, "iops");
1179 finish_log(td, td->iops_log, "iops");
1182 fio_mutex_up(writeout_mutex);
1183 if (td->o.exec_postrun)
1184 exec_string(td->o.exec_postrun);
1186 if (exitall_on_terminate)
1187 fio_terminate_threads(td->groupid);
1191 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1194 if (td->o.verify_async)
1195 verify_async_exit(td);
1197 close_and_free_files(td);
1200 cgroup_shutdown(td, &cgroup_mnt);
1202 if (td->o.cpumask_set) {
1203 int ret = fio_cpuset_exit(&td->o.cpumask);
1205 td_verror(td, ret, "fio_cpuset_exit");
1209 * do this very late, it will log file closing as well
1211 if (td->o.write_iolog_file)
1212 write_iolog_close(td);
1214 td_set_runstate(td, TD_EXITED);
1215 return (void *) (uintptr_t) td->error;
1220 * We cannot pass the td data into a forked process, so attach the td and
1221 * pass it to the thread worker.
1223 static int fork_main(int shmid, int offset)
1225 struct thread_data *td;
1229 data = shmat(shmid, NULL, 0);
1230 if (data == (void *) -1) {
1238 * HP-UX inherits shm mappings?
1243 td = data + offset * sizeof(struct thread_data);
1244 ret = thread_main(td);
1246 return (int) (uintptr_t) ret;
1250 * Run over the job map and reap the threads that have exited, if any.
1252 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1253 unsigned int *m_rate)
1255 struct thread_data *td;
1256 unsigned int cputhreads, realthreads, pending;
1260 * reap exited threads (TD_EXITED -> TD_REAPED)
1262 realthreads = pending = cputhreads = 0;
1263 for_each_td(td, i) {
1267 * ->io_ops is NULL for a thread that has closed its
1270 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1279 if (td->runstate == TD_REAPED)
1281 if (td->o.use_thread) {
1282 if (td->runstate == TD_EXITED) {
1283 td_set_runstate(td, TD_REAPED);
1290 if (td->runstate == TD_EXITED)
1294 * check if someone quit or got killed in an unusual way
1296 ret = waitpid(td->pid, &status, flags);
1298 if (errno == ECHILD) {
1299 log_err("fio: pid=%d disappeared %d\n",
1300 (int) td->pid, td->runstate);
1301 td_set_runstate(td, TD_REAPED);
1305 } else if (ret == td->pid) {
1306 if (WIFSIGNALED(status)) {
1307 int sig = WTERMSIG(status);
1310 log_err("fio: pid=%d, got signal=%d\n",
1311 (int) td->pid, sig);
1312 td_set_runstate(td, TD_REAPED);
1315 if (WIFEXITED(status)) {
1316 if (WEXITSTATUS(status) && !td->error)
1317 td->error = WEXITSTATUS(status);
1319 td_set_runstate(td, TD_REAPED);
1325 * thread is not dead, continue
1331 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1332 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
1339 done_secs += mtime_since_now(&td->epoch) / 1000;
1342 if (*nr_running == cputhreads && !pending && realthreads)
1343 fio_terminate_threads(TERMINATE_ALL);
1347 * Main function for kicking off and reaping jobs, as needed.
1349 static void run_threads(void)
1351 struct thread_data *td;
1352 unsigned long spent;
1353 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1355 if (fio_pin_memory())
1358 if (fio_gtod_offload && fio_start_gtod_thread())
1363 nr_thread = nr_process = 0;
1364 for_each_td(td, i) {
1365 if (td->o.use_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);
1574 while (nr_running) {
1575 reap_threads(&nr_running, &t_rate, &m_rate);
1583 static void *disk_thread_main(void *data)
1585 fio_mutex_up(startup_mutex);
1588 usleep(DISK_UTIL_MSEC * 1000);
1594 print_thread_status();
1600 static int create_disk_util_thread(void)
1604 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1606 log_err("Can't create disk util thread: %s\n", strerror(ret));
1610 ret = pthread_detach(disk_util_thread);
1612 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1616 dprint(FD_MUTEX, "wait on startup_mutex\n");
1617 fio_mutex_down(startup_mutex);
1618 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1622 int fio_backend(void)
1624 struct thread_data *td;
1628 if (load_profile(exec_profile))
1631 exec_profile = NULL;
1637 setup_log(&agg_io_log[DDIR_READ], 0, IO_LOG_TYPE_BW);
1638 setup_log(&agg_io_log[DDIR_WRITE], 0, IO_LOG_TYPE_BW);
1641 startup_mutex = fio_mutex_init(0);
1642 if (startup_mutex == NULL)
1644 writeout_mutex = fio_mutex_init(1);
1645 if (writeout_mutex == NULL)
1649 create_disk_util_thread();
1651 cgroup_list = smalloc(sizeof(*cgroup_list));
1652 INIT_FLIST_HEAD(cgroup_list);
1659 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1660 __finish_log(agg_io_log[DDIR_WRITE],
1661 "agg-write_bw.log");
1666 fio_options_free(td);
1668 cgroup_kill(cgroup_list);
1672 fio_mutex_remove(startup_mutex);
1673 fio_mutex_remove(writeout_mutex);