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 memset(&act, 0, sizeof(act));
112 act.sa_handler = sig_show_status;
113 act.sa_flags = SA_RESTART;
114 sigaction(SIGUSR1, &act, NULL);
117 memset(&act, 0, sizeof(act));
118 act.sa_handler = sig_int;
119 act.sa_flags = SA_RESTART;
120 sigaction(SIGPIPE, &act, NULL);
125 * Check if we are above the minimum rate given.
127 static int __check_min_rate(struct thread_data *td, struct timeval *now,
130 unsigned long long bytes = 0;
131 unsigned long iops = 0;
134 unsigned int ratemin = 0;
135 unsigned int rate_iops = 0;
136 unsigned int rate_iops_min = 0;
138 assert(ddir_rw(ddir));
140 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
144 * allow a 2 second settle period in the beginning
146 if (mtime_since(&td->start, now) < 2000)
149 iops += td->this_io_blocks[ddir];
150 bytes += td->this_io_bytes[ddir];
151 ratemin += td->o.ratemin[ddir];
152 rate_iops += td->o.rate_iops[ddir];
153 rate_iops_min += td->o.rate_iops_min[ddir];
156 * if rate blocks is set, sample is running
158 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
159 spent = mtime_since(&td->lastrate[ddir], now);
160 if (spent < td->o.ratecycle)
163 if (td->o.rate[ddir]) {
165 * check bandwidth specified rate
167 if (bytes < td->rate_bytes[ddir]) {
168 log_err("%s: min rate %u not met\n", td->o.name,
172 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
173 if (rate < ratemin ||
174 bytes < td->rate_bytes[ddir]) {
175 log_err("%s: min rate %u not met, got"
176 " %luKB/sec\n", td->o.name,
183 * checks iops specified rate
185 if (iops < rate_iops) {
186 log_err("%s: min iops rate %u not met\n",
187 td->o.name, rate_iops);
190 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
191 if (rate < rate_iops_min ||
192 iops < td->rate_blocks[ddir]) {
193 log_err("%s: min iops rate %u not met,"
194 " got %lu\n", td->o.name,
195 rate_iops_min, rate);
201 td->rate_bytes[ddir] = bytes;
202 td->rate_blocks[ddir] = iops;
203 memcpy(&td->lastrate[ddir], now, sizeof(*now));
207 static int check_min_rate(struct thread_data *td, struct timeval *now,
208 unsigned long *bytes_done)
212 if (bytes_done[DDIR_READ])
213 ret |= __check_min_rate(td, now, DDIR_READ);
214 if (bytes_done[DDIR_WRITE])
215 ret |= __check_min_rate(td, now, DDIR_WRITE);
216 if (bytes_done[DDIR_TRIM])
217 ret |= __check_min_rate(td, now, DDIR_TRIM);
223 * When job exits, we can cancel the in-flight IO if we are using async
224 * io. Attempt to do so.
226 static void cleanup_pending_aio(struct thread_data *td)
228 struct flist_head *entry, *n;
233 * get immediately available events, if any
235 r = io_u_queued_complete(td, 0, NULL);
240 * now cancel remaining active events
242 if (td->io_ops->cancel) {
243 flist_for_each_safe(entry, n, &td->io_u_busylist) {
244 io_u = flist_entry(entry, struct io_u, list);
247 * if the io_u isn't in flight, then that generally
248 * means someone leaked an io_u. complain but fix
249 * it up, so we don't stall here.
251 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
252 log_err("fio: non-busy IO on busy list\n");
255 r = td->io_ops->cancel(td, io_u);
263 r = io_u_queued_complete(td, td->cur_depth, NULL);
267 * Helper to handle the final sync of a file. Works just like the normal
268 * io path, just does everything sync.
270 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
272 struct io_u *io_u = __get_io_u(td);
278 io_u->ddir = DDIR_SYNC;
281 if (td_io_prep(td, io_u)) {
287 ret = td_io_queue(td, io_u);
289 td_verror(td, io_u->error, "td_io_queue");
292 } else if (ret == FIO_Q_QUEUED) {
293 if (io_u_queued_complete(td, 1, NULL) < 0)
295 } else if (ret == FIO_Q_COMPLETED) {
297 td_verror(td, io_u->error, "td_io_queue");
301 if (io_u_sync_complete(td, io_u, NULL) < 0)
303 } else if (ret == FIO_Q_BUSY) {
304 if (td_io_commit(td))
312 static inline void __update_tv_cache(struct thread_data *td)
314 fio_gettime(&td->tv_cache, NULL);
317 static inline void update_tv_cache(struct thread_data *td)
319 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
320 __update_tv_cache(td);
323 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
325 if (in_ramp_time(td))
329 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
335 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
340 if (ret < 0 || td->error) {
342 enum error_type_bit eb;
347 eb = td_error_type(ddir, err);
348 if (!(td->o.continue_on_error & (1 << eb)))
351 if (td_non_fatal_error(td, eb, err)) {
353 * Continue with the I/Os in case of
356 update_error_count(td, err);
360 } else if (td->o.fill_device && err == ENOSPC) {
362 * We expect to hit this error if
363 * fill_device option is set.
370 * Stop the I/O in case of a fatal
373 update_error_count(td, err);
382 * The main verify engine. Runs over the writes we previously submitted,
383 * reads the blocks back in, and checks the crc/md5 of the data.
385 static void do_verify(struct thread_data *td)
392 dprint(FD_VERIFY, "starting loop\n");
395 * sync io first and invalidate cache, to make sure we really
398 for_each_file(td, f, i) {
399 if (!fio_file_open(f))
401 if (fio_io_sync(td, f))
403 if (file_invalidate_cache(td, f))
410 td_set_runstate(td, TD_VERIFYING);
413 while (!td->terminate) {
418 if (runtime_exceeded(td, &td->tv_cache)) {
419 __update_tv_cache(td);
420 if (runtime_exceeded(td, &td->tv_cache)) {
426 if (flow_threshold_exceeded(td))
429 io_u = __get_io_u(td);
433 if (get_next_verify(td, io_u)) {
438 if (td_io_prep(td, io_u)) {
443 if (td->o.verify_async)
444 io_u->end_io = verify_io_u_async;
446 io_u->end_io = verify_io_u;
448 ret = td_io_queue(td, io_u);
450 case FIO_Q_COMPLETED:
453 clear_io_u(td, io_u);
454 } else if (io_u->resid) {
455 int bytes = io_u->xfer_buflen - io_u->resid;
461 td_verror(td, EIO, "full resid");
466 io_u->xfer_buflen = io_u->resid;
467 io_u->xfer_buf += bytes;
468 io_u->offset += bytes;
470 if (ddir_rw(io_u->ddir))
471 td->ts.short_io_u[io_u->ddir]++;
474 if (io_u->offset == f->real_file_size)
477 requeue_io_u(td, &io_u);
480 ret = io_u_sync_complete(td, io_u, NULL);
488 requeue_io_u(td, &io_u);
489 ret2 = td_io_commit(td);
495 td_verror(td, -ret, "td_io_queue");
499 if (break_on_this_error(td, io_u->ddir, &ret))
503 * if we can queue more, do so. but check if there are
504 * completed io_u's first. Note that we can get BUSY even
505 * without IO queued, if the system is resource starved.
507 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
508 if (full || !td->o.iodepth_batch_complete) {
509 min_events = min(td->o.iodepth_batch_complete,
512 * if the queue is full, we MUST reap at least 1 event
514 if (full && !min_events)
519 * Reap required number of io units, if any,
520 * and do the verification on them through
521 * the callback handler
523 if (io_u_queued_complete(td, min_events, NULL) < 0) {
527 } while (full && (td->cur_depth > td->o.iodepth_low));
534 min_events = td->cur_depth;
537 ret = io_u_queued_complete(td, min_events, NULL);
539 cleanup_pending_aio(td);
541 td_set_runstate(td, TD_RUNNING);
543 dprint(FD_VERIFY, "exiting loop\n");
546 static int io_bytes_exceeded(struct thread_data *td)
548 unsigned long long bytes;
551 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
552 else if (td_write(td))
553 bytes = td->this_io_bytes[DDIR_WRITE];
554 else if (td_read(td))
555 bytes = td->this_io_bytes[DDIR_READ];
557 bytes = td->this_io_bytes[DDIR_TRIM];
559 return bytes >= td->o.size;
563 * Main IO worker function. It retrieves io_u's to process and queues
564 * and reaps them, checking for rate and errors along the way.
566 static void do_io(struct thread_data *td)
571 if (in_ramp_time(td))
572 td_set_runstate(td, TD_RAMP);
574 td_set_runstate(td, TD_RUNNING);
576 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
577 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
579 struct timeval comp_time;
580 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
591 if (runtime_exceeded(td, &td->tv_cache)) {
592 __update_tv_cache(td);
593 if (runtime_exceeded(td, &td->tv_cache)) {
599 if (flow_threshold_exceeded(td))
609 * Add verification end_io handler if:
610 * - Asked to verify (!td_rw(td))
611 * - Or the io_u is from our verify list (mixed write/ver)
613 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
614 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
615 if (td->o.verify_async)
616 io_u->end_io = verify_io_u_async;
618 io_u->end_io = verify_io_u;
619 td_set_runstate(td, TD_VERIFYING);
620 } else if (in_ramp_time(td))
621 td_set_runstate(td, TD_RAMP);
623 td_set_runstate(td, TD_RUNNING);
625 ret = td_io_queue(td, io_u);
627 case FIO_Q_COMPLETED:
630 clear_io_u(td, io_u);
631 } else if (io_u->resid) {
632 int bytes = io_u->xfer_buflen - io_u->resid;
633 struct fio_file *f = io_u->file;
639 td_verror(td, EIO, "full resid");
644 io_u->xfer_buflen = io_u->resid;
645 io_u->xfer_buf += bytes;
646 io_u->offset += bytes;
648 if (ddir_rw(io_u->ddir))
649 td->ts.short_io_u[io_u->ddir]++;
651 if (io_u->offset == f->real_file_size)
654 requeue_io_u(td, &io_u);
657 if (__should_check_rate(td, DDIR_READ) ||
658 __should_check_rate(td, DDIR_WRITE) ||
659 __should_check_rate(td, DDIR_TRIM))
660 fio_gettime(&comp_time, NULL);
662 ret = io_u_sync_complete(td, io_u, bytes_done);
669 * if the engine doesn't have a commit hook,
670 * the io_u is really queued. if it does have such
671 * a hook, it has to call io_u_queued() itself.
673 if (td->io_ops->commit == NULL)
674 io_u_queued(td, io_u);
677 requeue_io_u(td, &io_u);
678 ret2 = td_io_commit(td);
688 if (break_on_this_error(td, ddir, &ret))
692 * See if we need to complete some commands. Note that we
693 * can get BUSY even without IO queued, if the system is
696 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
697 if (full || !td->o.iodepth_batch_complete) {
698 min_evts = min(td->o.iodepth_batch_complete,
701 * if the queue is full, we MUST reap at least 1 event
703 if (full && !min_evts)
706 if (__should_check_rate(td, DDIR_READ) ||
707 __should_check_rate(td, DDIR_WRITE) ||
708 __should_check_rate(td, DDIR_TRIM))
709 fio_gettime(&comp_time, NULL);
712 ret = io_u_queued_complete(td, min_evts, bytes_done);
716 } while (full && (td->cur_depth > td->o.iodepth_low));
721 if (!ddir_rw_sum(bytes_done))
724 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
725 if (check_min_rate(td, &comp_time, bytes_done)) {
726 if (exitall_on_terminate)
727 fio_terminate_threads(td->groupid);
728 td_verror(td, EIO, "check_min_rate");
733 if (td->o.thinktime) {
734 unsigned long long b;
736 b = ddir_rw_sum(td->io_blocks);
737 if (!(b % td->o.thinktime_blocks)) {
740 if (td->o.thinktime_spin)
741 usec_spin(td->o.thinktime_spin);
743 left = td->o.thinktime - td->o.thinktime_spin;
745 usec_sleep(td, left);
750 if (td->trim_entries)
751 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
753 if (td->o.fill_device && td->error == ENOSPC) {
762 ret = io_u_queued_complete(td, i, NULL);
763 if (td->o.fill_device && td->error == ENOSPC)
767 if (should_fsync(td) && td->o.end_fsync) {
768 td_set_runstate(td, TD_FSYNCING);
770 for_each_file(td, f, i) {
771 if (!fio_file_open(f))
777 cleanup_pending_aio(td);
780 * stop job if we failed doing any IO
782 if (!ddir_rw_sum(td->this_io_bytes))
786 static void cleanup_io_u(struct thread_data *td)
788 struct flist_head *entry, *n;
791 flist_for_each_safe(entry, n, &td->io_u_freelist) {
792 io_u = flist_entry(entry, struct io_u, list);
794 flist_del(&io_u->list);
795 fio_memfree(io_u, sizeof(*io_u));
801 static int init_io_u(struct thread_data *td)
804 unsigned int max_bs, min_write;
805 int cl_align, i, max_units;
809 max_units = td->o.iodepth;
810 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
811 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
812 min_write = td->o.min_bs[DDIR_WRITE];
813 td->orig_buffer_size = (unsigned long long) max_bs
814 * (unsigned long long) max_units;
816 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
819 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
822 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
823 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
826 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
827 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
831 if (data_xfer && allocate_io_mem(td))
834 if (td->o.odirect || td->o.mem_align ||
835 (td->io_ops->flags & FIO_RAWIO))
836 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
840 cl_align = os_cache_line_size();
842 for (i = 0; i < max_units; i++) {
848 ptr = fio_memalign(cl_align, sizeof(*io_u));
850 log_err("fio: unable to allocate aligned memory\n");
855 memset(io_u, 0, sizeof(*io_u));
856 INIT_FLIST_HEAD(&io_u->list);
857 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
861 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
864 io_u_fill_buffer(td, io_u, min_write, max_bs);
865 if (td_write(td) && td->o.verify_pattern_bytes) {
867 * Fill the buffer with the pattern if we are
868 * going to be doing writes.
870 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
875 io_u->flags = IO_U_F_FREE;
876 flist_add(&io_u->list, &td->io_u_freelist);
883 static int switch_ioscheduler(struct thread_data *td)
885 char tmp[256], tmp2[128];
889 if (td->io_ops->flags & FIO_DISKLESSIO)
892 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
894 f = fopen(tmp, "r+");
896 if (errno == ENOENT) {
897 log_err("fio: os or kernel doesn't support IO scheduler"
901 td_verror(td, errno, "fopen iosched");
908 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
909 if (ferror(f) || ret != 1) {
910 td_verror(td, errno, "fwrite");
918 * Read back and check that the selected scheduler is now the default.
920 ret = fread(tmp, 1, sizeof(tmp), f);
921 if (ferror(f) || ret < 0) {
922 td_verror(td, errno, "fread");
927 sprintf(tmp2, "[%s]", td->o.ioscheduler);
928 if (!strstr(tmp, tmp2)) {
929 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
930 td_verror(td, EINVAL, "iosched_switch");
939 static int keep_running(struct thread_data *td)
943 if (td->o.time_based)
950 if (ddir_rw_sum(td->io_bytes) < td->o.size)
956 static int exec_string(const char *string)
958 int ret, newlen = strlen(string) + 1 + 8;
961 str = malloc(newlen);
962 sprintf(str, "sh -c %s", string);
966 log_err("fio: exec of cmd <%s> failed\n", str);
973 * Entry point for the thread based jobs. The process based jobs end up
974 * here as well, after a little setup.
976 static void *thread_main(void *data)
978 unsigned long long elapsed;
979 struct thread_data *td = data;
980 struct thread_options *o = &td->o;
981 pthread_condattr_t attr;
985 if (!o->use_thread) {
991 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
994 fio_server_send_start(td);
996 INIT_FLIST_HEAD(&td->io_u_freelist);
997 INIT_FLIST_HEAD(&td->io_u_busylist);
998 INIT_FLIST_HEAD(&td->io_u_requeues);
999 INIT_FLIST_HEAD(&td->io_log_list);
1000 INIT_FLIST_HEAD(&td->io_hist_list);
1001 INIT_FLIST_HEAD(&td->verify_list);
1002 INIT_FLIST_HEAD(&td->trim_list);
1003 pthread_mutex_init(&td->io_u_lock, NULL);
1004 td->io_hist_tree = RB_ROOT;
1006 pthread_condattr_init(&attr);
1007 pthread_cond_init(&td->verify_cond, &attr);
1008 pthread_cond_init(&td->free_cond, &attr);
1010 td_set_runstate(td, TD_INITIALIZED);
1011 dprint(FD_MUTEX, "up startup_mutex\n");
1012 fio_mutex_up(startup_mutex);
1013 dprint(FD_MUTEX, "wait on td->mutex\n");
1014 fio_mutex_down(td->mutex);
1015 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1018 * the ->mutex mutex is now no longer used, close it to avoid
1019 * eating a file descriptor
1021 fio_mutex_remove(td->mutex);
1025 * A new gid requires privilege, so we need to do this before setting
1028 if (o->gid != -1U && setgid(o->gid)) {
1029 td_verror(td, errno, "setgid");
1032 if (o->uid != -1U && setuid(o->uid)) {
1033 td_verror(td, errno, "setuid");
1038 * If we have a gettimeofday() thread, make sure we exclude that
1039 * thread from this job
1042 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1045 * Set affinity first, in case it has an impact on the memory
1048 if (o->cpumask_set) {
1049 ret = fio_setaffinity(td->pid, o->cpumask);
1051 td_verror(td, errno, "cpu_set_affinity");
1056 if (fio_pin_memory(td))
1060 * May alter parameters that init_io_u() will use, so we need to
1069 if (o->verify_async && verify_async_init(td))
1073 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1075 td_verror(td, errno, "ioprio_set");
1080 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1084 if (nice(o->nice) == -1 && errno != 0) {
1085 td_verror(td, errno, "nice");
1089 if (o->ioscheduler && switch_ioscheduler(td))
1092 if (!o->create_serialize && setup_files(td))
1098 if (init_random_map(td))
1101 if (o->exec_prerun && exec_string(o->exec_prerun))
1105 if (pre_read_files(td) < 0)
1109 fio_verify_init(td);
1111 fio_gettime(&td->epoch, NULL);
1112 getrusage(RUSAGE_SELF, &td->ru_start);
1115 while (keep_running(td)) {
1116 fio_gettime(&td->start, NULL);
1117 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1118 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1119 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1121 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1122 td->o.ratemin[DDIR_TRIM]) {
1123 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1124 sizeof(td->bw_sample_time));
1125 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1126 sizeof(td->bw_sample_time));
1127 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1128 sizeof(td->bw_sample_time));
1134 prune_io_piece_log(td);
1140 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1141 elapsed = utime_since_now(&td->start);
1142 td->ts.runtime[DDIR_READ] += elapsed;
1144 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1145 elapsed = utime_since_now(&td->start);
1146 td->ts.runtime[DDIR_WRITE] += elapsed;
1148 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1149 elapsed = utime_since_now(&td->start);
1150 td->ts.runtime[DDIR_TRIM] += elapsed;
1153 if (td->error || td->terminate)
1156 if (!td->o.do_verify ||
1157 td->o.verify == VERIFY_NONE ||
1158 (td->io_ops->flags & FIO_UNIDIR))
1163 fio_gettime(&td->start, NULL);
1167 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1169 if (td->error || td->terminate)
1173 update_rusage_stat(td);
1174 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1175 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1176 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1177 td->ts.total_run_time = mtime_since_now(&td->epoch);
1178 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1179 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1180 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1182 fio_unpin_memory(td);
1184 fio_mutex_down(writeout_mutex);
1186 if (td->o.bw_log_file) {
1187 finish_log_named(td, td->bw_log,
1188 td->o.bw_log_file, "bw");
1190 finish_log(td, td->bw_log, "bw");
1193 if (td->o.lat_log_file) {
1194 finish_log_named(td, td->lat_log,
1195 td->o.lat_log_file, "lat");
1197 finish_log(td, td->lat_log, "lat");
1200 if (td->o.lat_log_file) {
1201 finish_log_named(td, td->slat_log,
1202 td->o.lat_log_file, "slat");
1204 finish_log(td, td->slat_log, "slat");
1207 if (td->o.lat_log_file) {
1208 finish_log_named(td, td->clat_log,
1209 td->o.lat_log_file, "clat");
1211 finish_log(td, td->clat_log, "clat");
1214 if (td->o.iops_log_file) {
1215 finish_log_named(td, td->iops_log,
1216 td->o.iops_log_file, "iops");
1218 finish_log(td, td->iops_log, "iops");
1221 fio_mutex_up(writeout_mutex);
1222 if (td->o.exec_postrun)
1223 exec_string(td->o.exec_postrun);
1225 if (exitall_on_terminate)
1226 fio_terminate_threads(td->groupid);
1230 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1233 if (td->o.verify_async)
1234 verify_async_exit(td);
1236 close_and_free_files(td);
1239 cgroup_shutdown(td, &cgroup_mnt);
1241 if (o->cpumask_set) {
1242 int ret = fio_cpuset_exit(&o->cpumask);
1244 td_verror(td, ret, "fio_cpuset_exit");
1248 * do this very late, it will log file closing as well
1250 if (td->o.write_iolog_file)
1251 write_iolog_close(td);
1253 td_set_runstate(td, TD_EXITED);
1254 return (void *) (uintptr_t) td->error;
1259 * We cannot pass the td data into a forked process, so attach the td and
1260 * pass it to the thread worker.
1262 static int fork_main(int shmid, int offset)
1264 struct thread_data *td;
1268 data = shmat(shmid, NULL, 0);
1269 if (data == (void *) -1) {
1277 * HP-UX inherits shm mappings?
1282 td = data + offset * sizeof(struct thread_data);
1283 ret = thread_main(td);
1285 return (int) (uintptr_t) ret;
1289 * Run over the job map and reap the threads that have exited, if any.
1291 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1292 unsigned int *m_rate)
1294 struct thread_data *td;
1295 unsigned int cputhreads, realthreads, pending;
1299 * reap exited threads (TD_EXITED -> TD_REAPED)
1301 realthreads = pending = cputhreads = 0;
1302 for_each_td(td, i) {
1306 * ->io_ops is NULL for a thread that has closed its
1309 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1318 if (td->runstate == TD_REAPED)
1320 if (td->o.use_thread) {
1321 if (td->runstate == TD_EXITED) {
1322 td_set_runstate(td, TD_REAPED);
1329 if (td->runstate == TD_EXITED)
1333 * check if someone quit or got killed in an unusual way
1335 ret = waitpid(td->pid, &status, flags);
1337 if (errno == ECHILD) {
1338 log_err("fio: pid=%d disappeared %d\n",
1339 (int) td->pid, td->runstate);
1341 td_set_runstate(td, TD_REAPED);
1345 } else if (ret == td->pid) {
1346 if (WIFSIGNALED(status)) {
1347 int sig = WTERMSIG(status);
1350 log_err("fio: pid=%d, got signal=%d\n",
1351 (int) td->pid, sig);
1353 td_set_runstate(td, TD_REAPED);
1356 if (WIFEXITED(status)) {
1357 if (WEXITSTATUS(status) && !td->error)
1358 td->error = WEXITSTATUS(status);
1360 td_set_runstate(td, TD_REAPED);
1366 * thread is not dead, continue
1372 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1373 (*t_rate) -= ddir_rw_sum(td->o.rate);
1380 done_secs += mtime_since_now(&td->epoch) / 1000;
1383 if (*nr_running == cputhreads && !pending && realthreads)
1384 fio_terminate_threads(TERMINATE_ALL);
1388 * Main function for kicking off and reaping jobs, as needed.
1390 static void run_threads(void)
1392 struct thread_data *td;
1393 unsigned long spent;
1394 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1396 if (fio_gtod_offload && fio_start_gtod_thread())
1401 nr_thread = nr_process = 0;
1402 for_each_td(td, i) {
1403 if (td->o.use_thread)
1409 if (output_format == FIO_OUTPUT_NORMAL) {
1410 log_info("Starting ");
1412 log_info("%d thread%s", nr_thread,
1413 nr_thread > 1 ? "s" : "");
1417 log_info("%d process%s", nr_process,
1418 nr_process > 1 ? "es" : "");
1424 todo = thread_number;
1427 m_rate = t_rate = 0;
1429 for_each_td(td, i) {
1430 print_status_init(td->thread_number - 1);
1432 if (!td->o.create_serialize)
1436 * do file setup here so it happens sequentially,
1437 * we don't want X number of threads getting their
1438 * client data interspersed on disk
1440 if (setup_files(td)) {
1443 log_err("fio: pid=%d, err=%d/%s\n",
1444 (int) td->pid, td->error, td->verror);
1445 td_set_runstate(td, TD_REAPED);
1452 * for sharing to work, each job must always open
1453 * its own files. so close them, if we opened them
1456 for_each_file(td, f, j) {
1457 if (fio_file_open(f))
1458 td_io_close_file(td, f);
1466 struct thread_data *map[REAL_MAX_JOBS];
1467 struct timeval this_start;
1468 int this_jobs = 0, left;
1471 * create threads (TD_NOT_CREATED -> TD_CREATED)
1473 for_each_td(td, i) {
1474 if (td->runstate != TD_NOT_CREATED)
1478 * never got a chance to start, killed by other
1479 * thread for some reason
1481 if (td->terminate) {
1486 if (td->o.start_delay) {
1487 spent = mtime_since_genesis();
1489 if (td->o.start_delay * 1000 > spent)
1493 if (td->o.stonewall && (nr_started || nr_running)) {
1494 dprint(FD_PROCESS, "%s: stonewall wait\n",
1502 * Set state to created. Thread will transition
1503 * to TD_INITIALIZED when it's done setting up.
1505 td_set_runstate(td, TD_CREATED);
1506 map[this_jobs++] = td;
1509 if (td->o.use_thread) {
1512 dprint(FD_PROCESS, "will pthread_create\n");
1513 ret = pthread_create(&td->thread, NULL,
1516 log_err("pthread_create: %s\n",
1521 ret = pthread_detach(td->thread);
1523 log_err("pthread_detach: %s",
1527 dprint(FD_PROCESS, "will fork\n");
1530 int ret = fork_main(shm_id, i);
1533 } else if (i == fio_debug_jobno)
1534 *fio_debug_jobp = pid;
1536 dprint(FD_MUTEX, "wait on startup_mutex\n");
1537 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1538 log_err("fio: job startup hung? exiting.\n");
1539 fio_terminate_threads(TERMINATE_ALL);
1544 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1548 * Wait for the started threads to transition to
1551 fio_gettime(&this_start, NULL);
1553 while (left && !fio_abort) {
1554 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1559 for (i = 0; i < this_jobs; i++) {
1563 if (td->runstate == TD_INITIALIZED) {
1566 } else if (td->runstate >= TD_EXITED) {
1570 nr_running++; /* work-around... */
1576 log_err("fio: %d job%s failed to start\n", left,
1577 left > 1 ? "s" : "");
1578 for (i = 0; i < this_jobs; i++) {
1582 kill(td->pid, SIGTERM);
1588 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1590 for_each_td(td, i) {
1591 if (td->runstate != TD_INITIALIZED)
1594 if (in_ramp_time(td))
1595 td_set_runstate(td, TD_RAMP);
1597 td_set_runstate(td, TD_RUNNING);
1600 m_rate += ddir_rw_sum(td->o.ratemin);
1601 t_rate += ddir_rw_sum(td->o.rate);
1603 fio_mutex_up(td->mutex);
1606 reap_threads(&nr_running, &t_rate, &m_rate);
1612 while (nr_running) {
1613 reap_threads(&nr_running, &t_rate, &m_rate);
1620 void wait_for_disk_thread_exit(void)
1622 fio_mutex_down(disk_thread_mutex);
1625 static void free_disk_util(void)
1627 disk_util_start_exit();
1628 wait_for_disk_thread_exit();
1629 disk_util_prune_entries();
1632 static void *disk_thread_main(void *data)
1636 fio_mutex_up(startup_mutex);
1638 while (threads && !ret) {
1639 usleep(DISK_UTIL_MSEC * 1000);
1642 ret = update_io_ticks();
1645 print_thread_status();
1648 fio_mutex_up(disk_thread_mutex);
1652 static int create_disk_util_thread(void)
1658 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1660 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1662 fio_mutex_remove(disk_thread_mutex);
1663 log_err("Can't create disk util thread: %s\n", strerror(ret));
1667 ret = pthread_detach(disk_util_thread);
1669 fio_mutex_remove(disk_thread_mutex);
1670 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1674 dprint(FD_MUTEX, "wait on startup_mutex\n");
1675 fio_mutex_down(startup_mutex);
1676 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1680 int fio_backend(void)
1682 struct thread_data *td;
1686 if (load_profile(exec_profile))
1689 exec_profile = NULL;
1695 setup_log(&agg_io_log[DDIR_READ], 0, IO_LOG_TYPE_BW);
1696 setup_log(&agg_io_log[DDIR_WRITE], 0, IO_LOG_TYPE_BW);
1697 setup_log(&agg_io_log[DDIR_TRIM], 0, IO_LOG_TYPE_BW);
1700 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1701 if (startup_mutex == NULL)
1703 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1704 if (writeout_mutex == NULL)
1708 create_disk_util_thread();
1710 cgroup_list = smalloc(sizeof(*cgroup_list));
1711 INIT_FLIST_HEAD(cgroup_list);
1718 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1719 __finish_log(agg_io_log[DDIR_WRITE],
1720 "agg-write_bw.log");
1721 __finish_log(agg_io_log[DDIR_TRIM],
1722 "agg-write_bw.log");
1727 fio_options_free(td);
1730 cgroup_kill(cgroup_list);
1734 fio_mutex_remove(startup_mutex);
1735 fio_mutex_remove(writeout_mutex);
1736 fio_mutex_remove(disk_thread_mutex);