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 *disk_thread_mutex;
54 static struct fio_mutex *startup_mutex;
55 static struct fio_mutex *writeout_mutex;
56 static struct flist_head *cgroup_list;
57 static char *cgroup_mnt;
58 static int exit_value;
59 static volatile int fio_abort;
60 static unsigned int nr_process = 0;
61 static unsigned int nr_thread = 0;
63 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
66 unsigned int thread_number = 0;
69 unsigned long done_secs = 0;
70 volatile int disk_util_exit = 0;
72 #define PAGE_ALIGN(buf) \
73 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
75 #define JOB_START_TIMEOUT (5 * 1000)
77 static void sig_int(int sig)
81 fio_server_got_signal(sig);
83 log_info("\nfio: terminating on signal %d\n", sig);
88 fio_terminate_threads(TERMINATE_ALL);
92 static void sig_show_status(int sig)
94 show_running_run_stats();
97 static void set_sig_handlers(void)
101 memset(&act, 0, sizeof(act));
102 act.sa_handler = sig_int;
103 act.sa_flags = SA_RESTART;
104 sigaction(SIGINT, &act, NULL);
106 memset(&act, 0, sizeof(act));
107 act.sa_handler = sig_int;
108 act.sa_flags = SA_RESTART;
109 sigaction(SIGTERM, &act, NULL);
111 /* Windows uses SIGBREAK as a quit signal from other applications */
113 memset(&act, 0, sizeof(act));
114 act.sa_handler = sig_int;
115 act.sa_flags = SA_RESTART;
116 sigaction(SIGBREAK, &act, NULL);
119 memset(&act, 0, sizeof(act));
120 act.sa_handler = sig_show_status;
121 act.sa_flags = SA_RESTART;
122 sigaction(SIGUSR1, &act, NULL);
125 memset(&act, 0, sizeof(act));
126 act.sa_handler = sig_int;
127 act.sa_flags = SA_RESTART;
128 sigaction(SIGPIPE, &act, NULL);
133 * Check if we are above the minimum rate given.
135 static int __check_min_rate(struct thread_data *td, struct timeval *now,
138 unsigned long long bytes = 0;
139 unsigned long iops = 0;
142 unsigned int ratemin = 0;
143 unsigned int rate_iops = 0;
144 unsigned int rate_iops_min = 0;
146 assert(ddir_rw(ddir));
148 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
152 * allow a 2 second settle period in the beginning
154 if (mtime_since(&td->start, now) < 2000)
157 iops += td->this_io_blocks[ddir];
158 bytes += td->this_io_bytes[ddir];
159 ratemin += td->o.ratemin[ddir];
160 rate_iops += td->o.rate_iops[ddir];
161 rate_iops_min += td->o.rate_iops_min[ddir];
164 * if rate blocks is set, sample is running
166 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
167 spent = mtime_since(&td->lastrate[ddir], now);
168 if (spent < td->o.ratecycle)
171 if (td->o.rate[ddir]) {
173 * check bandwidth specified rate
175 if (bytes < td->rate_bytes[ddir]) {
176 log_err("%s: min rate %u not met\n", td->o.name,
180 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
181 if (rate < ratemin ||
182 bytes < td->rate_bytes[ddir]) {
183 log_err("%s: min rate %u not met, got"
184 " %luKB/sec\n", td->o.name,
191 * checks iops specified rate
193 if (iops < rate_iops) {
194 log_err("%s: min iops rate %u not met\n",
195 td->o.name, rate_iops);
198 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
199 if (rate < rate_iops_min ||
200 iops < td->rate_blocks[ddir]) {
201 log_err("%s: min iops rate %u not met,"
202 " got %lu\n", td->o.name,
203 rate_iops_min, rate);
209 td->rate_bytes[ddir] = bytes;
210 td->rate_blocks[ddir] = iops;
211 memcpy(&td->lastrate[ddir], now, sizeof(*now));
215 static int check_min_rate(struct thread_data *td, struct timeval *now,
216 unsigned long *bytes_done)
220 if (bytes_done[DDIR_READ])
221 ret |= __check_min_rate(td, now, DDIR_READ);
222 if (bytes_done[DDIR_WRITE])
223 ret |= __check_min_rate(td, now, DDIR_WRITE);
224 if (bytes_done[DDIR_TRIM])
225 ret |= __check_min_rate(td, now, DDIR_TRIM);
231 * When job exits, we can cancel the in-flight IO if we are using async
232 * io. Attempt to do so.
234 static void cleanup_pending_aio(struct thread_data *td)
236 struct flist_head *entry, *n;
241 * get immediately available events, if any
243 r = io_u_queued_complete(td, 0, NULL);
248 * now cancel remaining active events
250 if (td->io_ops->cancel) {
251 flist_for_each_safe(entry, n, &td->io_u_busylist) {
252 io_u = flist_entry(entry, struct io_u, list);
255 * if the io_u isn't in flight, then that generally
256 * means someone leaked an io_u. complain but fix
257 * it up, so we don't stall here.
259 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
260 log_err("fio: non-busy IO on busy list\n");
263 r = td->io_ops->cancel(td, io_u);
271 r = io_u_queued_complete(td, td->cur_depth, NULL);
275 * Helper to handle the final sync of a file. Works just like the normal
276 * io path, just does everything sync.
278 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
280 struct io_u *io_u = __get_io_u(td);
286 io_u->ddir = DDIR_SYNC;
289 if (td_io_prep(td, io_u)) {
295 ret = td_io_queue(td, io_u);
297 td_verror(td, io_u->error, "td_io_queue");
300 } else if (ret == FIO_Q_QUEUED) {
301 if (io_u_queued_complete(td, 1, NULL) < 0)
303 } else if (ret == FIO_Q_COMPLETED) {
305 td_verror(td, io_u->error, "td_io_queue");
309 if (io_u_sync_complete(td, io_u, NULL) < 0)
311 } else if (ret == FIO_Q_BUSY) {
312 if (td_io_commit(td))
320 static inline void __update_tv_cache(struct thread_data *td)
322 fio_gettime(&td->tv_cache, NULL);
325 static inline void update_tv_cache(struct thread_data *td)
327 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
328 __update_tv_cache(td);
331 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
333 if (in_ramp_time(td))
337 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
343 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
348 if (ret < 0 || td->error) {
350 enum error_type_bit eb;
355 eb = td_error_type(ddir, err);
356 if (!(td->o.continue_on_error & (1 << eb)))
359 if (td_non_fatal_error(td, eb, err)) {
361 * Continue with the I/Os in case of
364 update_error_count(td, err);
368 } else if (td->o.fill_device && err == ENOSPC) {
370 * We expect to hit this error if
371 * fill_device option is set.
378 * Stop the I/O in case of a fatal
381 update_error_count(td, err);
390 * The main verify engine. Runs over the writes we previously submitted,
391 * reads the blocks back in, and checks the crc/md5 of the data.
393 static void do_verify(struct thread_data *td)
400 dprint(FD_VERIFY, "starting loop\n");
403 * sync io first and invalidate cache, to make sure we really
406 for_each_file(td, f, i) {
407 if (!fio_file_open(f))
409 if (fio_io_sync(td, f))
411 if (file_invalidate_cache(td, f))
418 td_set_runstate(td, TD_VERIFYING);
421 while (!td->terminate) {
426 if (runtime_exceeded(td, &td->tv_cache)) {
427 __update_tv_cache(td);
428 if (runtime_exceeded(td, &td->tv_cache)) {
434 if (flow_threshold_exceeded(td))
437 io_u = __get_io_u(td);
441 if (get_next_verify(td, io_u)) {
446 if (td_io_prep(td, io_u)) {
451 if (td->o.verify_async)
452 io_u->end_io = verify_io_u_async;
454 io_u->end_io = verify_io_u;
456 ret = td_io_queue(td, io_u);
458 case FIO_Q_COMPLETED:
461 clear_io_u(td, io_u);
462 } else if (io_u->resid) {
463 int bytes = io_u->xfer_buflen - io_u->resid;
469 td_verror(td, EIO, "full resid");
474 io_u->xfer_buflen = io_u->resid;
475 io_u->xfer_buf += bytes;
476 io_u->offset += bytes;
478 if (ddir_rw(io_u->ddir))
479 td->ts.short_io_u[io_u->ddir]++;
482 if (io_u->offset == f->real_file_size)
485 requeue_io_u(td, &io_u);
488 ret = io_u_sync_complete(td, io_u, NULL);
496 requeue_io_u(td, &io_u);
497 ret2 = td_io_commit(td);
503 td_verror(td, -ret, "td_io_queue");
507 if (break_on_this_error(td, io_u->ddir, &ret))
511 * if we can queue more, do so. but check if there are
512 * completed io_u's first. Note that we can get BUSY even
513 * without IO queued, if the system is resource starved.
515 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
516 if (full || !td->o.iodepth_batch_complete) {
517 min_events = min(td->o.iodepth_batch_complete,
520 * if the queue is full, we MUST reap at least 1 event
522 if (full && !min_events)
527 * Reap required number of io units, if any,
528 * and do the verification on them through
529 * the callback handler
531 if (io_u_queued_complete(td, min_events, NULL) < 0) {
535 } while (full && (td->cur_depth > td->o.iodepth_low));
542 min_events = td->cur_depth;
545 ret = io_u_queued_complete(td, min_events, NULL);
547 cleanup_pending_aio(td);
549 td_set_runstate(td, TD_RUNNING);
551 dprint(FD_VERIFY, "exiting loop\n");
554 static int io_bytes_exceeded(struct thread_data *td)
556 unsigned long long bytes;
559 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
560 else if (td_write(td))
561 bytes = td->this_io_bytes[DDIR_WRITE];
562 else if (td_read(td))
563 bytes = td->this_io_bytes[DDIR_READ];
565 bytes = td->this_io_bytes[DDIR_TRIM];
567 return bytes >= td->o.size;
571 * Main IO worker function. It retrieves io_u's to process and queues
572 * and reaps them, checking for rate and errors along the way.
574 static void do_io(struct thread_data *td)
579 if (in_ramp_time(td))
580 td_set_runstate(td, TD_RAMP);
582 td_set_runstate(td, TD_RUNNING);
584 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
585 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
587 struct timeval comp_time;
588 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
599 if (runtime_exceeded(td, &td->tv_cache)) {
600 __update_tv_cache(td);
601 if (runtime_exceeded(td, &td->tv_cache)) {
607 if (flow_threshold_exceeded(td))
617 * Add verification end_io handler if:
618 * - Asked to verify (!td_rw(td))
619 * - Or the io_u is from our verify list (mixed write/ver)
621 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
622 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
623 if (td->o.verify_async)
624 io_u->end_io = verify_io_u_async;
626 io_u->end_io = verify_io_u;
627 td_set_runstate(td, TD_VERIFYING);
628 } else if (in_ramp_time(td))
629 td_set_runstate(td, TD_RAMP);
631 td_set_runstate(td, TD_RUNNING);
633 ret = td_io_queue(td, io_u);
635 case FIO_Q_COMPLETED:
638 clear_io_u(td, io_u);
639 } else if (io_u->resid) {
640 int bytes = io_u->xfer_buflen - io_u->resid;
641 struct fio_file *f = io_u->file;
647 td_verror(td, EIO, "full resid");
652 io_u->xfer_buflen = io_u->resid;
653 io_u->xfer_buf += bytes;
654 io_u->offset += bytes;
656 if (ddir_rw(io_u->ddir))
657 td->ts.short_io_u[io_u->ddir]++;
659 if (io_u->offset == f->real_file_size)
662 requeue_io_u(td, &io_u);
665 if (__should_check_rate(td, DDIR_READ) ||
666 __should_check_rate(td, DDIR_WRITE) ||
667 __should_check_rate(td, DDIR_TRIM))
668 fio_gettime(&comp_time, NULL);
670 ret = io_u_sync_complete(td, io_u, bytes_done);
677 * if the engine doesn't have a commit hook,
678 * the io_u is really queued. if it does have such
679 * a hook, it has to call io_u_queued() itself.
681 if (td->io_ops->commit == NULL)
682 io_u_queued(td, io_u);
685 requeue_io_u(td, &io_u);
686 ret2 = td_io_commit(td);
696 if (break_on_this_error(td, ddir, &ret))
700 * See if we need to complete some commands. Note that we
701 * can get BUSY even without IO queued, if the system is
704 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
705 if (full || !td->o.iodepth_batch_complete) {
706 min_evts = min(td->o.iodepth_batch_complete,
709 * if the queue is full, we MUST reap at least 1 event
711 if (full && !min_evts)
714 if (__should_check_rate(td, DDIR_READ) ||
715 __should_check_rate(td, DDIR_WRITE) ||
716 __should_check_rate(td, DDIR_TRIM))
717 fio_gettime(&comp_time, NULL);
720 ret = io_u_queued_complete(td, min_evts, bytes_done);
724 } while (full && (td->cur_depth > td->o.iodepth_low));
729 if (!ddir_rw_sum(bytes_done))
732 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
733 if (check_min_rate(td, &comp_time, bytes_done)) {
734 if (exitall_on_terminate)
735 fio_terminate_threads(td->groupid);
736 td_verror(td, EIO, "check_min_rate");
741 if (td->o.thinktime) {
742 unsigned long long b;
744 b = ddir_rw_sum(td->io_blocks);
745 if (!(b % td->o.thinktime_blocks)) {
748 if (td->o.thinktime_spin)
749 usec_spin(td->o.thinktime_spin);
751 left = td->o.thinktime - td->o.thinktime_spin;
753 usec_sleep(td, left);
758 if (td->trim_entries)
759 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
761 if (td->o.fill_device && td->error == ENOSPC) {
770 ret = io_u_queued_complete(td, i, NULL);
771 if (td->o.fill_device && td->error == ENOSPC)
775 if (should_fsync(td) && td->o.end_fsync) {
776 td_set_runstate(td, TD_FSYNCING);
778 for_each_file(td, f, i) {
779 if (!fio_file_open(f))
785 cleanup_pending_aio(td);
788 * stop job if we failed doing any IO
790 if (!ddir_rw_sum(td->this_io_bytes))
794 static void cleanup_io_u(struct thread_data *td)
796 struct flist_head *entry, *n;
799 flist_for_each_safe(entry, n, &td->io_u_freelist) {
800 io_u = flist_entry(entry, struct io_u, list);
802 flist_del(&io_u->list);
803 fio_memfree(io_u, sizeof(*io_u));
809 static int init_io_u(struct thread_data *td)
812 unsigned int max_bs, min_write;
813 int cl_align, i, max_units;
817 max_units = td->o.iodepth;
818 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
819 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
820 min_write = td->o.min_bs[DDIR_WRITE];
821 td->orig_buffer_size = (unsigned long long) max_bs
822 * (unsigned long long) max_units;
824 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
827 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
830 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
831 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
834 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
835 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
839 if (data_xfer && allocate_io_mem(td))
842 if (td->o.odirect || td->o.mem_align ||
843 (td->io_ops->flags & FIO_RAWIO))
844 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
848 cl_align = os_cache_line_size();
850 for (i = 0; i < max_units; i++) {
856 ptr = fio_memalign(cl_align, sizeof(*io_u));
858 log_err("fio: unable to allocate aligned memory\n");
863 memset(io_u, 0, sizeof(*io_u));
864 INIT_FLIST_HEAD(&io_u->list);
865 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
869 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
872 io_u_fill_buffer(td, io_u, min_write, max_bs);
873 if (td_write(td) && td->o.verify_pattern_bytes) {
875 * Fill the buffer with the pattern if we are
876 * going to be doing writes.
878 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
883 io_u->flags = IO_U_F_FREE;
884 flist_add(&io_u->list, &td->io_u_freelist);
891 static int switch_ioscheduler(struct thread_data *td)
893 char tmp[256], tmp2[128];
897 if (td->io_ops->flags & FIO_DISKLESSIO)
900 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
902 f = fopen(tmp, "r+");
904 if (errno == ENOENT) {
905 log_err("fio: os or kernel doesn't support IO scheduler"
909 td_verror(td, errno, "fopen iosched");
916 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
917 if (ferror(f) || ret != 1) {
918 td_verror(td, errno, "fwrite");
926 * Read back and check that the selected scheduler is now the default.
928 ret = fread(tmp, 1, sizeof(tmp), f);
929 if (ferror(f) || ret < 0) {
930 td_verror(td, errno, "fread");
935 sprintf(tmp2, "[%s]", td->o.ioscheduler);
936 if (!strstr(tmp, tmp2)) {
937 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
938 td_verror(td, EINVAL, "iosched_switch");
947 static int keep_running(struct thread_data *td)
951 if (td->o.time_based)
958 if (ddir_rw_sum(td->io_bytes) < td->o.size)
964 static int exec_string(const char *string)
966 int ret, newlen = strlen(string) + 1 + 8;
969 str = malloc(newlen);
970 sprintf(str, "sh -c %s", string);
974 log_err("fio: exec of cmd <%s> failed\n", str);
981 * Entry point for the thread based jobs. The process based jobs end up
982 * here as well, after a little setup.
984 static void *thread_main(void *data)
986 unsigned long long elapsed;
987 struct thread_data *td = data;
988 struct thread_options *o = &td->o;
989 pthread_condattr_t attr;
993 if (!o->use_thread) {
999 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1002 fio_server_send_start(td);
1004 INIT_FLIST_HEAD(&td->io_u_freelist);
1005 INIT_FLIST_HEAD(&td->io_u_busylist);
1006 INIT_FLIST_HEAD(&td->io_u_requeues);
1007 INIT_FLIST_HEAD(&td->io_log_list);
1008 INIT_FLIST_HEAD(&td->io_hist_list);
1009 INIT_FLIST_HEAD(&td->verify_list);
1010 INIT_FLIST_HEAD(&td->trim_list);
1011 pthread_mutex_init(&td->io_u_lock, NULL);
1012 td->io_hist_tree = RB_ROOT;
1014 pthread_condattr_init(&attr);
1015 pthread_cond_init(&td->verify_cond, &attr);
1016 pthread_cond_init(&td->free_cond, &attr);
1018 td_set_runstate(td, TD_INITIALIZED);
1019 dprint(FD_MUTEX, "up startup_mutex\n");
1020 fio_mutex_up(startup_mutex);
1021 dprint(FD_MUTEX, "wait on td->mutex\n");
1022 fio_mutex_down(td->mutex);
1023 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1026 * the ->mutex mutex is now no longer used, close it to avoid
1027 * eating a file descriptor
1029 fio_mutex_remove(td->mutex);
1033 * A new gid requires privilege, so we need to do this before setting
1036 if (o->gid != -1U && setgid(o->gid)) {
1037 td_verror(td, errno, "setgid");
1040 if (o->uid != -1U && setuid(o->uid)) {
1041 td_verror(td, errno, "setuid");
1046 * If we have a gettimeofday() thread, make sure we exclude that
1047 * thread from this job
1050 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1053 * Set affinity first, in case it has an impact on the memory
1056 if (o->cpumask_set) {
1057 ret = fio_setaffinity(td->pid, o->cpumask);
1059 td_verror(td, errno, "cpu_set_affinity");
1064 if (fio_pin_memory(td))
1068 * May alter parameters that init_io_u() will use, so we need to
1077 if (o->verify_async && verify_async_init(td))
1081 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1083 td_verror(td, errno, "ioprio_set");
1088 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1092 if (nice(o->nice) == -1 && errno != 0) {
1093 td_verror(td, errno, "nice");
1097 if (o->ioscheduler && switch_ioscheduler(td))
1100 if (!o->create_serialize && setup_files(td))
1106 if (init_random_map(td))
1109 if (o->exec_prerun && exec_string(o->exec_prerun))
1113 if (pre_read_files(td) < 0)
1117 fio_verify_init(td);
1119 fio_gettime(&td->epoch, NULL);
1120 getrusage(RUSAGE_SELF, &td->ru_start);
1123 while (keep_running(td)) {
1124 fio_gettime(&td->start, NULL);
1125 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1126 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1127 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1129 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1130 td->o.ratemin[DDIR_TRIM]) {
1131 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1132 sizeof(td->bw_sample_time));
1133 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1134 sizeof(td->bw_sample_time));
1135 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1136 sizeof(td->bw_sample_time));
1142 prune_io_piece_log(td);
1148 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1149 elapsed = utime_since_now(&td->start);
1150 td->ts.runtime[DDIR_READ] += elapsed;
1152 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1153 elapsed = utime_since_now(&td->start);
1154 td->ts.runtime[DDIR_WRITE] += elapsed;
1156 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1157 elapsed = utime_since_now(&td->start);
1158 td->ts.runtime[DDIR_TRIM] += elapsed;
1161 if (td->error || td->terminate)
1164 if (!td->o.do_verify ||
1165 td->o.verify == VERIFY_NONE ||
1166 (td->io_ops->flags & FIO_UNIDIR))
1171 fio_gettime(&td->start, NULL);
1175 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1177 if (td->error || td->terminate)
1181 update_rusage_stat(td);
1182 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1183 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1184 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1185 td->ts.total_run_time = mtime_since_now(&td->epoch);
1186 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1187 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1188 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1190 fio_unpin_memory(td);
1192 fio_mutex_down(writeout_mutex);
1194 if (td->o.bw_log_file) {
1195 finish_log_named(td, td->bw_log,
1196 td->o.bw_log_file, "bw");
1198 finish_log(td, td->bw_log, "bw");
1201 if (td->o.lat_log_file) {
1202 finish_log_named(td, td->lat_log,
1203 td->o.lat_log_file, "lat");
1205 finish_log(td, td->lat_log, "lat");
1208 if (td->o.lat_log_file) {
1209 finish_log_named(td, td->slat_log,
1210 td->o.lat_log_file, "slat");
1212 finish_log(td, td->slat_log, "slat");
1215 if (td->o.lat_log_file) {
1216 finish_log_named(td, td->clat_log,
1217 td->o.lat_log_file, "clat");
1219 finish_log(td, td->clat_log, "clat");
1222 if (td->o.iops_log_file) {
1223 finish_log_named(td, td->iops_log,
1224 td->o.iops_log_file, "iops");
1226 finish_log(td, td->iops_log, "iops");
1229 fio_mutex_up(writeout_mutex);
1230 if (td->o.exec_postrun)
1231 exec_string(td->o.exec_postrun);
1233 if (exitall_on_terminate)
1234 fio_terminate_threads(td->groupid);
1238 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1241 if (td->o.verify_async)
1242 verify_async_exit(td);
1244 close_and_free_files(td);
1247 cgroup_shutdown(td, &cgroup_mnt);
1249 if (o->cpumask_set) {
1250 int ret = fio_cpuset_exit(&o->cpumask);
1252 td_verror(td, ret, "fio_cpuset_exit");
1256 * do this very late, it will log file closing as well
1258 if (td->o.write_iolog_file)
1259 write_iolog_close(td);
1261 td_set_runstate(td, TD_EXITED);
1262 return (void *) (uintptr_t) td->error;
1267 * We cannot pass the td data into a forked process, so attach the td and
1268 * pass it to the thread worker.
1270 static int fork_main(int shmid, int offset)
1272 struct thread_data *td;
1276 data = shmat(shmid, NULL, 0);
1277 if (data == (void *) -1) {
1285 * HP-UX inherits shm mappings?
1290 td = data + offset * sizeof(struct thread_data);
1291 ret = thread_main(td);
1293 return (int) (uintptr_t) ret;
1297 * Run over the job map and reap the threads that have exited, if any.
1299 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1300 unsigned int *m_rate)
1302 struct thread_data *td;
1303 unsigned int cputhreads, realthreads, pending;
1307 * reap exited threads (TD_EXITED -> TD_REAPED)
1309 realthreads = pending = cputhreads = 0;
1310 for_each_td(td, i) {
1314 * ->io_ops is NULL for a thread that has closed its
1317 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1326 if (td->runstate == TD_REAPED)
1328 if (td->o.use_thread) {
1329 if (td->runstate == TD_EXITED) {
1330 td_set_runstate(td, TD_REAPED);
1337 if (td->runstate == TD_EXITED)
1341 * check if someone quit or got killed in an unusual way
1343 ret = waitpid(td->pid, &status, flags);
1345 if (errno == ECHILD) {
1346 log_err("fio: pid=%d disappeared %d\n",
1347 (int) td->pid, td->runstate);
1349 td_set_runstate(td, TD_REAPED);
1353 } else if (ret == td->pid) {
1354 if (WIFSIGNALED(status)) {
1355 int sig = WTERMSIG(status);
1358 log_err("fio: pid=%d, got signal=%d\n",
1359 (int) td->pid, sig);
1361 td_set_runstate(td, TD_REAPED);
1364 if (WIFEXITED(status)) {
1365 if (WEXITSTATUS(status) && !td->error)
1366 td->error = WEXITSTATUS(status);
1368 td_set_runstate(td, TD_REAPED);
1374 * thread is not dead, continue
1380 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1381 (*t_rate) -= ddir_rw_sum(td->o.rate);
1388 done_secs += mtime_since_now(&td->epoch) / 1000;
1391 if (*nr_running == cputhreads && !pending && realthreads)
1392 fio_terminate_threads(TERMINATE_ALL);
1396 * Main function for kicking off and reaping jobs, as needed.
1398 static void run_threads(void)
1400 struct thread_data *td;
1401 unsigned long spent;
1402 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1404 if (fio_gtod_offload && fio_start_gtod_thread())
1409 nr_thread = nr_process = 0;
1410 for_each_td(td, i) {
1411 if (td->o.use_thread)
1417 if (output_format == FIO_OUTPUT_NORMAL) {
1418 log_info("Starting ");
1420 log_info("%d thread%s", nr_thread,
1421 nr_thread > 1 ? "s" : "");
1425 log_info("%d process%s", nr_process,
1426 nr_process > 1 ? "es" : "");
1432 todo = thread_number;
1435 m_rate = t_rate = 0;
1437 for_each_td(td, i) {
1438 print_status_init(td->thread_number - 1);
1440 if (!td->o.create_serialize)
1444 * do file setup here so it happens sequentially,
1445 * we don't want X number of threads getting their
1446 * client data interspersed on disk
1448 if (setup_files(td)) {
1451 log_err("fio: pid=%d, err=%d/%s\n",
1452 (int) td->pid, td->error, td->verror);
1453 td_set_runstate(td, TD_REAPED);
1460 * for sharing to work, each job must always open
1461 * its own files. so close them, if we opened them
1464 for_each_file(td, f, j) {
1465 if (fio_file_open(f))
1466 td_io_close_file(td, f);
1474 struct thread_data *map[REAL_MAX_JOBS];
1475 struct timeval this_start;
1476 int this_jobs = 0, left;
1479 * create threads (TD_NOT_CREATED -> TD_CREATED)
1481 for_each_td(td, i) {
1482 if (td->runstate != TD_NOT_CREATED)
1486 * never got a chance to start, killed by other
1487 * thread for some reason
1489 if (td->terminate) {
1494 if (td->o.start_delay) {
1495 spent = mtime_since_genesis();
1497 if (td->o.start_delay * 1000 > spent)
1501 if (td->o.stonewall && (nr_started || nr_running)) {
1502 dprint(FD_PROCESS, "%s: stonewall wait\n",
1510 * Set state to created. Thread will transition
1511 * to TD_INITIALIZED when it's done setting up.
1513 td_set_runstate(td, TD_CREATED);
1514 map[this_jobs++] = td;
1517 if (td->o.use_thread) {
1520 dprint(FD_PROCESS, "will pthread_create\n");
1521 ret = pthread_create(&td->thread, NULL,
1524 log_err("pthread_create: %s\n",
1529 ret = pthread_detach(td->thread);
1531 log_err("pthread_detach: %s",
1535 dprint(FD_PROCESS, "will fork\n");
1538 int ret = fork_main(shm_id, i);
1541 } else if (i == fio_debug_jobno)
1542 *fio_debug_jobp = pid;
1544 dprint(FD_MUTEX, "wait on startup_mutex\n");
1545 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1546 log_err("fio: job startup hung? exiting.\n");
1547 fio_terminate_threads(TERMINATE_ALL);
1552 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1556 * Wait for the started threads to transition to
1559 fio_gettime(&this_start, NULL);
1561 while (left && !fio_abort) {
1562 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1567 for (i = 0; i < this_jobs; i++) {
1571 if (td->runstate == TD_INITIALIZED) {
1574 } else if (td->runstate >= TD_EXITED) {
1578 nr_running++; /* work-around... */
1584 log_err("fio: %d job%s failed to start\n", left,
1585 left > 1 ? "s" : "");
1586 for (i = 0; i < this_jobs; i++) {
1590 kill(td->pid, SIGTERM);
1596 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1598 for_each_td(td, i) {
1599 if (td->runstate != TD_INITIALIZED)
1602 if (in_ramp_time(td))
1603 td_set_runstate(td, TD_RAMP);
1605 td_set_runstate(td, TD_RUNNING);
1608 m_rate += ddir_rw_sum(td->o.ratemin);
1609 t_rate += ddir_rw_sum(td->o.rate);
1611 fio_mutex_up(td->mutex);
1614 reap_threads(&nr_running, &t_rate, &m_rate);
1620 while (nr_running) {
1621 reap_threads(&nr_running, &t_rate, &m_rate);
1628 void wait_for_disk_thread_exit(void)
1630 fio_mutex_down(disk_thread_mutex);
1633 static void free_disk_util(void)
1635 disk_util_start_exit();
1636 wait_for_disk_thread_exit();
1637 disk_util_prune_entries();
1640 static void *disk_thread_main(void *data)
1644 fio_mutex_up(startup_mutex);
1646 while (threads && !ret) {
1647 usleep(DISK_UTIL_MSEC * 1000);
1650 ret = update_io_ticks();
1653 print_thread_status();
1656 fio_mutex_up(disk_thread_mutex);
1660 static int create_disk_util_thread(void)
1666 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1668 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1670 fio_mutex_remove(disk_thread_mutex);
1671 log_err("Can't create disk util thread: %s\n", strerror(ret));
1675 ret = pthread_detach(disk_util_thread);
1677 fio_mutex_remove(disk_thread_mutex);
1678 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1682 dprint(FD_MUTEX, "wait on startup_mutex\n");
1683 fio_mutex_down(startup_mutex);
1684 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1688 int fio_backend(void)
1690 struct thread_data *td;
1694 if (load_profile(exec_profile))
1697 exec_profile = NULL;
1703 setup_log(&agg_io_log[DDIR_READ], 0, IO_LOG_TYPE_BW);
1704 setup_log(&agg_io_log[DDIR_WRITE], 0, IO_LOG_TYPE_BW);
1705 setup_log(&agg_io_log[DDIR_TRIM], 0, IO_LOG_TYPE_BW);
1708 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1709 if (startup_mutex == NULL)
1711 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1712 if (writeout_mutex == NULL)
1716 create_disk_util_thread();
1718 cgroup_list = smalloc(sizeof(*cgroup_list));
1719 INIT_FLIST_HEAD(cgroup_list);
1726 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1727 __finish_log(agg_io_log[DDIR_WRITE],
1728 "agg-write_bw.log");
1729 __finish_log(agg_io_log[DDIR_TRIM],
1730 "agg-write_bw.log");
1735 fio_options_free(td);
1738 cgroup_kill(cgroup_list);
1742 fio_mutex_remove(startup_mutex);
1743 fio_mutex_remove(writeout_mutex);
1744 fio_mutex_remove(disk_thread_mutex);