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;
61 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
64 unsigned int thread_number = 0;
65 unsigned int stat_number = 0;
66 unsigned int nr_process = 0;
67 unsigned int nr_thread = 0;
70 unsigned long done_secs = 0;
71 volatile int disk_util_exit = 0;
73 #define PAGE_ALIGN(buf) \
74 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
76 #define JOB_START_TIMEOUT (5 * 1000)
78 static void sig_int(int sig)
82 fio_server_got_signal(sig);
84 log_info("\nfio: terminating on signal %d\n", sig);
89 fio_terminate_threads(TERMINATE_ALL);
93 static void sig_show_status(int sig)
95 show_running_run_stats();
98 static void set_sig_handlers(void)
100 struct sigaction act;
102 memset(&act, 0, sizeof(act));
103 act.sa_handler = sig_int;
104 act.sa_flags = SA_RESTART;
105 sigaction(SIGINT, &act, NULL);
107 memset(&act, 0, sizeof(act));
108 act.sa_handler = sig_int;
109 act.sa_flags = SA_RESTART;
110 sigaction(SIGTERM, &act, NULL);
112 /* Windows uses SIGBREAK as a quit signal from other applications */
114 memset(&act, 0, sizeof(act));
115 act.sa_handler = sig_int;
116 act.sa_flags = SA_RESTART;
117 sigaction(SIGBREAK, &act, NULL);
120 memset(&act, 0, sizeof(act));
121 act.sa_handler = sig_show_status;
122 act.sa_flags = SA_RESTART;
123 sigaction(SIGUSR1, &act, NULL);
126 memset(&act, 0, sizeof(act));
127 act.sa_handler = sig_int;
128 act.sa_flags = SA_RESTART;
129 sigaction(SIGPIPE, &act, NULL);
134 * Check if we are above the minimum rate given.
136 static int __check_min_rate(struct thread_data *td, struct timeval *now,
139 unsigned long long bytes = 0;
140 unsigned long iops = 0;
143 unsigned int ratemin = 0;
144 unsigned int rate_iops = 0;
145 unsigned int rate_iops_min = 0;
147 assert(ddir_rw(ddir));
149 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
153 * allow a 2 second settle period in the beginning
155 if (mtime_since(&td->start, now) < 2000)
158 iops += td->this_io_blocks[ddir];
159 bytes += td->this_io_bytes[ddir];
160 ratemin += td->o.ratemin[ddir];
161 rate_iops += td->o.rate_iops[ddir];
162 rate_iops_min += td->o.rate_iops_min[ddir];
165 * if rate blocks is set, sample is running
167 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
168 spent = mtime_since(&td->lastrate[ddir], now);
169 if (spent < td->o.ratecycle)
172 if (td->o.rate[ddir]) {
174 * check bandwidth specified rate
176 if (bytes < td->rate_bytes[ddir]) {
177 log_err("%s: min rate %u not met\n", td->o.name,
181 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
182 if (rate < ratemin ||
183 bytes < td->rate_bytes[ddir]) {
184 log_err("%s: min rate %u not met, got"
185 " %luKB/sec\n", td->o.name,
192 * checks iops specified rate
194 if (iops < rate_iops) {
195 log_err("%s: min iops rate %u not met\n",
196 td->o.name, rate_iops);
199 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
200 if (rate < rate_iops_min ||
201 iops < td->rate_blocks[ddir]) {
202 log_err("%s: min iops rate %u not met,"
203 " got %lu\n", td->o.name,
204 rate_iops_min, rate);
210 td->rate_bytes[ddir] = bytes;
211 td->rate_blocks[ddir] = iops;
212 memcpy(&td->lastrate[ddir], now, sizeof(*now));
216 static int check_min_rate(struct thread_data *td, struct timeval *now,
217 unsigned long *bytes_done)
221 if (bytes_done[DDIR_READ])
222 ret |= __check_min_rate(td, now, DDIR_READ);
223 if (bytes_done[DDIR_WRITE])
224 ret |= __check_min_rate(td, now, DDIR_WRITE);
225 if (bytes_done[DDIR_TRIM])
226 ret |= __check_min_rate(td, now, DDIR_TRIM);
232 * When job exits, we can cancel the in-flight IO if we are using async
233 * io. Attempt to do so.
235 static void cleanup_pending_aio(struct thread_data *td)
237 struct flist_head *entry, *n;
242 * get immediately available events, if any
244 r = io_u_queued_complete(td, 0, NULL);
249 * now cancel remaining active events
251 if (td->io_ops->cancel) {
252 flist_for_each_safe(entry, n, &td->io_u_busylist) {
253 io_u = flist_entry(entry, struct io_u, list);
256 * if the io_u isn't in flight, then that generally
257 * means someone leaked an io_u. complain but fix
258 * it up, so we don't stall here.
260 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
261 log_err("fio: non-busy IO on busy list\n");
264 r = td->io_ops->cancel(td, io_u);
272 r = io_u_queued_complete(td, td->cur_depth, NULL);
276 * Helper to handle the final sync of a file. Works just like the normal
277 * io path, just does everything sync.
279 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
281 struct io_u *io_u = __get_io_u(td);
287 io_u->ddir = DDIR_SYNC;
290 if (td_io_prep(td, io_u)) {
296 ret = td_io_queue(td, io_u);
298 td_verror(td, io_u->error, "td_io_queue");
301 } else if (ret == FIO_Q_QUEUED) {
302 if (io_u_queued_complete(td, 1, NULL) < 0)
304 } else if (ret == FIO_Q_COMPLETED) {
306 td_verror(td, io_u->error, "td_io_queue");
310 if (io_u_sync_complete(td, io_u, NULL) < 0)
312 } else if (ret == FIO_Q_BUSY) {
313 if (td_io_commit(td))
321 static inline void __update_tv_cache(struct thread_data *td)
323 fio_gettime(&td->tv_cache, NULL);
326 static inline void update_tv_cache(struct thread_data *td)
328 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
329 __update_tv_cache(td);
332 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
334 if (in_ramp_time(td))
338 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
344 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
349 if (ret < 0 || td->error) {
351 enum error_type_bit eb;
356 eb = td_error_type(ddir, err);
357 if (!(td->o.continue_on_error & (1 << eb)))
360 if (td_non_fatal_error(td, eb, err)) {
362 * Continue with the I/Os in case of
365 update_error_count(td, err);
369 } else if (td->o.fill_device && err == ENOSPC) {
371 * We expect to hit this error if
372 * fill_device option is set.
379 * Stop the I/O in case of a fatal
382 update_error_count(td, err);
391 * The main verify engine. Runs over the writes we previously submitted,
392 * reads the blocks back in, and checks the crc/md5 of the data.
394 static void do_verify(struct thread_data *td)
401 dprint(FD_VERIFY, "starting loop\n");
404 * sync io first and invalidate cache, to make sure we really
407 for_each_file(td, f, i) {
408 if (!fio_file_open(f))
410 if (fio_io_sync(td, f))
412 if (file_invalidate_cache(td, f))
419 td_set_runstate(td, TD_VERIFYING);
422 while (!td->terminate) {
427 if (runtime_exceeded(td, &td->tv_cache)) {
428 __update_tv_cache(td);
429 if (runtime_exceeded(td, &td->tv_cache)) {
435 if (flow_threshold_exceeded(td))
438 io_u = __get_io_u(td);
442 if (get_next_verify(td, io_u)) {
447 if (td_io_prep(td, io_u)) {
452 if (td->o.verify_async)
453 io_u->end_io = verify_io_u_async;
455 io_u->end_io = verify_io_u;
457 ret = td_io_queue(td, io_u);
459 case FIO_Q_COMPLETED:
462 clear_io_u(td, io_u);
463 } else if (io_u->resid) {
464 int bytes = io_u->xfer_buflen - io_u->resid;
470 td_verror(td, EIO, "full resid");
475 io_u->xfer_buflen = io_u->resid;
476 io_u->xfer_buf += bytes;
477 io_u->offset += bytes;
479 if (ddir_rw(io_u->ddir))
480 td->ts.short_io_u[io_u->ddir]++;
483 if (io_u->offset == f->real_file_size)
486 requeue_io_u(td, &io_u);
489 ret = io_u_sync_complete(td, io_u, NULL);
497 requeue_io_u(td, &io_u);
498 ret2 = td_io_commit(td);
504 td_verror(td, -ret, "td_io_queue");
508 if (break_on_this_error(td, io_u->ddir, &ret))
512 * if we can queue more, do so. but check if there are
513 * completed io_u's first. Note that we can get BUSY even
514 * without IO queued, if the system is resource starved.
516 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
517 if (full || !td->o.iodepth_batch_complete) {
518 min_events = min(td->o.iodepth_batch_complete,
521 * if the queue is full, we MUST reap at least 1 event
523 if (full && !min_events)
528 * Reap required number of io units, if any,
529 * and do the verification on them through
530 * the callback handler
532 if (io_u_queued_complete(td, min_events, NULL) < 0) {
536 } while (full && (td->cur_depth > td->o.iodepth_low));
543 min_events = td->cur_depth;
546 ret = io_u_queued_complete(td, min_events, NULL);
548 cleanup_pending_aio(td);
550 td_set_runstate(td, TD_RUNNING);
552 dprint(FD_VERIFY, "exiting loop\n");
555 static int io_bytes_exceeded(struct thread_data *td)
557 unsigned long long bytes;
560 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
561 else if (td_write(td))
562 bytes = td->this_io_bytes[DDIR_WRITE];
563 else if (td_read(td))
564 bytes = td->this_io_bytes[DDIR_READ];
566 bytes = td->this_io_bytes[DDIR_TRIM];
568 return bytes >= td->o.size;
572 * Main IO worker function. It retrieves io_u's to process and queues
573 * and reaps them, checking for rate and errors along the way.
575 static void do_io(struct thread_data *td)
580 if (in_ramp_time(td))
581 td_set_runstate(td, TD_RAMP);
583 td_set_runstate(td, TD_RUNNING);
585 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
586 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
588 struct timeval comp_time;
589 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
595 if (td->terminate || td->done)
600 if (runtime_exceeded(td, &td->tv_cache)) {
601 __update_tv_cache(td);
602 if (runtime_exceeded(td, &td->tv_cache)) {
608 if (flow_threshold_exceeded(td))
618 * Add verification end_io handler if:
619 * - Asked to verify (!td_rw(td))
620 * - Or the io_u is from our verify list (mixed write/ver)
622 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
623 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
624 if (td->o.verify_async)
625 io_u->end_io = verify_io_u_async;
627 io_u->end_io = verify_io_u;
628 td_set_runstate(td, TD_VERIFYING);
629 } else if (in_ramp_time(td))
630 td_set_runstate(td, TD_RAMP);
632 td_set_runstate(td, TD_RUNNING);
634 ret = td_io_queue(td, io_u);
636 case FIO_Q_COMPLETED:
639 clear_io_u(td, io_u);
640 } else if (io_u->resid) {
641 int bytes = io_u->xfer_buflen - io_u->resid;
642 struct fio_file *f = io_u->file;
648 td_verror(td, EIO, "full resid");
653 io_u->xfer_buflen = io_u->resid;
654 io_u->xfer_buf += bytes;
655 io_u->offset += bytes;
657 if (ddir_rw(io_u->ddir))
658 td->ts.short_io_u[io_u->ddir]++;
660 if (io_u->offset == f->real_file_size)
663 requeue_io_u(td, &io_u);
666 if (__should_check_rate(td, DDIR_READ) ||
667 __should_check_rate(td, DDIR_WRITE) ||
668 __should_check_rate(td, DDIR_TRIM))
669 fio_gettime(&comp_time, NULL);
671 ret = io_u_sync_complete(td, io_u, bytes_done);
678 * if the engine doesn't have a commit hook,
679 * the io_u is really queued. if it does have such
680 * a hook, it has to call io_u_queued() itself.
682 if (td->io_ops->commit == NULL)
683 io_u_queued(td, io_u);
686 requeue_io_u(td, &io_u);
687 ret2 = td_io_commit(td);
697 if (break_on_this_error(td, ddir, &ret))
701 * See if we need to complete some commands. Note that we
702 * can get BUSY even without IO queued, if the system is
705 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
706 if (full || !td->o.iodepth_batch_complete) {
707 min_evts = min(td->o.iodepth_batch_complete,
710 * if the queue is full, we MUST reap at least 1 event
712 if (full && !min_evts)
715 if (__should_check_rate(td, DDIR_READ) ||
716 __should_check_rate(td, DDIR_WRITE) ||
717 __should_check_rate(td, DDIR_TRIM))
718 fio_gettime(&comp_time, NULL);
721 ret = io_u_queued_complete(td, min_evts, bytes_done);
725 } while (full && (td->cur_depth > td->o.iodepth_low));
730 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
733 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
734 if (check_min_rate(td, &comp_time, bytes_done)) {
735 if (exitall_on_terminate)
736 fio_terminate_threads(td->groupid);
737 td_verror(td, EIO, "check_min_rate");
742 if (td->o.thinktime) {
743 unsigned long long b;
745 b = ddir_rw_sum(td->io_blocks);
746 if (!(b % td->o.thinktime_blocks)) {
749 if (td->o.thinktime_spin)
750 usec_spin(td->o.thinktime_spin);
752 left = td->o.thinktime - td->o.thinktime_spin;
754 usec_sleep(td, left);
759 if (td->trim_entries)
760 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
762 if (td->o.fill_device && td->error == ENOSPC) {
771 ret = io_u_queued_complete(td, i, NULL);
772 if (td->o.fill_device && td->error == ENOSPC)
776 if (should_fsync(td) && td->o.end_fsync) {
777 td_set_runstate(td, TD_FSYNCING);
779 for_each_file(td, f, i) {
780 if (!fio_file_open(f))
786 cleanup_pending_aio(td);
789 * stop job if we failed doing any IO
791 if (!ddir_rw_sum(td->this_io_bytes))
795 static void cleanup_io_u(struct thread_data *td)
797 struct flist_head *entry, *n;
800 flist_for_each_safe(entry, n, &td->io_u_freelist) {
801 io_u = flist_entry(entry, struct io_u, list);
803 flist_del(&io_u->list);
804 fio_memfree(io_u, sizeof(*io_u));
810 static int init_io_u(struct thread_data *td)
813 unsigned int max_bs, min_write;
814 int cl_align, i, max_units;
818 max_units = td->o.iodepth;
819 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
820 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
821 min_write = td->o.min_bs[DDIR_WRITE];
822 td->orig_buffer_size = (unsigned long long) max_bs
823 * (unsigned long long) max_units;
825 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
828 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
831 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
832 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
835 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
836 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
840 if (data_xfer && allocate_io_mem(td))
843 if (td->o.odirect || td->o.mem_align ||
844 (td->io_ops->flags & FIO_RAWIO))
845 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
849 cl_align = os_cache_line_size();
851 for (i = 0; i < max_units; i++) {
857 ptr = fio_memalign(cl_align, sizeof(*io_u));
859 log_err("fio: unable to allocate aligned memory\n");
864 memset(io_u, 0, sizeof(*io_u));
865 INIT_FLIST_HEAD(&io_u->list);
866 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
870 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
873 io_u_fill_buffer(td, io_u, min_write, max_bs);
874 if (td_write(td) && td->o.verify_pattern_bytes) {
876 * Fill the buffer with the pattern if we are
877 * going to be doing writes.
879 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
884 io_u->flags = IO_U_F_FREE;
885 flist_add(&io_u->list, &td->io_u_freelist);
892 static int switch_ioscheduler(struct thread_data *td)
894 char tmp[256], tmp2[128];
898 if (td->io_ops->flags & FIO_DISKLESSIO)
901 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
903 f = fopen(tmp, "r+");
905 if (errno == ENOENT) {
906 log_err("fio: os or kernel doesn't support IO scheduler"
910 td_verror(td, errno, "fopen iosched");
917 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
918 if (ferror(f) || ret != 1) {
919 td_verror(td, errno, "fwrite");
927 * Read back and check that the selected scheduler is now the default.
929 ret = fread(tmp, 1, sizeof(tmp), f);
930 if (ferror(f) || ret < 0) {
931 td_verror(td, errno, "fread");
936 sprintf(tmp2, "[%s]", td->o.ioscheduler);
937 if (!strstr(tmp, tmp2)) {
938 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
939 td_verror(td, EINVAL, "iosched_switch");
948 static int keep_running(struct thread_data *td)
952 if (td->o.time_based)
959 if (ddir_rw_sum(td->io_bytes) < td->o.size)
965 static int exec_string(const char *string)
967 int ret, newlen = strlen(string) + 1 + 8;
970 str = malloc(newlen);
971 sprintf(str, "sh -c %s", string);
975 log_err("fio: exec of cmd <%s> failed\n", str);
982 * Entry point for the thread based jobs. The process based jobs end up
983 * here as well, after a little setup.
985 static void *thread_main(void *data)
987 unsigned long long elapsed;
988 struct thread_data *td = data;
989 pthread_condattr_t attr;
992 if (!td->o.use_thread) {
998 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1000 INIT_FLIST_HEAD(&td->io_u_freelist);
1001 INIT_FLIST_HEAD(&td->io_u_busylist);
1002 INIT_FLIST_HEAD(&td->io_u_requeues);
1003 INIT_FLIST_HEAD(&td->io_log_list);
1004 INIT_FLIST_HEAD(&td->io_hist_list);
1005 INIT_FLIST_HEAD(&td->verify_list);
1006 INIT_FLIST_HEAD(&td->trim_list);
1007 pthread_mutex_init(&td->io_u_lock, NULL);
1008 td->io_hist_tree = RB_ROOT;
1010 pthread_condattr_init(&attr);
1011 pthread_cond_init(&td->verify_cond, &attr);
1012 pthread_cond_init(&td->free_cond, &attr);
1014 td_set_runstate(td, TD_INITIALIZED);
1015 dprint(FD_MUTEX, "up startup_mutex\n");
1016 fio_mutex_up(startup_mutex);
1017 dprint(FD_MUTEX, "wait on td->mutex\n");
1018 fio_mutex_down(td->mutex);
1019 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1022 * the ->mutex mutex is now no longer used, close it to avoid
1023 * eating a file descriptor
1025 fio_mutex_remove(td->mutex);
1028 * A new gid requires privilege, so we need to do this before setting
1031 if (td->o.gid != -1U && setgid(td->o.gid)) {
1032 td_verror(td, errno, "setgid");
1035 if (td->o.uid != -1U && setuid(td->o.uid)) {
1036 td_verror(td, errno, "setuid");
1041 * If we have a gettimeofday() thread, make sure we exclude that
1042 * thread from this job
1045 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1048 * Set affinity first, in case it has an impact on the memory
1051 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1052 td_verror(td, errno, "cpu_set_affinity");
1056 #ifdef FIO_HAVE_LIBNUMA
1057 /* numa node setup */
1058 if (td->o.numa_cpumask_set || td->o.numa_memmask_set) {
1061 if (numa_available() < 0) {
1062 td_verror(td, errno, "Does not support NUMA API\n");
1066 if (td->o.numa_cpumask_set) {
1067 ret = numa_run_on_node_mask(td->o.numa_cpunodesmask);
1069 td_verror(td, errno, \
1070 "numa_run_on_node_mask failed\n");
1075 if (td->o.numa_memmask_set) {
1077 switch (td->o.numa_mem_mode) {
1078 case MPOL_INTERLEAVE:
1079 numa_set_interleave_mask(td->o.numa_memnodesmask);
1082 numa_set_membind(td->o.numa_memnodesmask);
1085 numa_set_localalloc();
1087 case MPOL_PREFERRED:
1088 numa_set_preferred(td->o.numa_mem_prefer_node);
1100 * May alter parameters that init_io_u() will use, so we need to
1109 if (td->o.verify_async && verify_async_init(td))
1112 if (td->ioprio_set) {
1113 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1114 td_verror(td, errno, "ioprio_set");
1119 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1123 if (nice(td->o.nice) == -1 && errno != 0) {
1124 td_verror(td, errno, "nice");
1128 if (td->o.ioscheduler && switch_ioscheduler(td))
1131 if (!td->o.create_serialize && setup_files(td))
1137 if (init_random_map(td))
1140 if (td->o.exec_prerun) {
1141 if (exec_string(td->o.exec_prerun))
1145 if (td->o.pre_read) {
1146 if (pre_read_files(td) < 0)
1150 fio_gettime(&td->epoch, NULL);
1151 getrusage(RUSAGE_SELF, &td->ru_start);
1154 while (keep_running(td)) {
1155 fio_gettime(&td->start, NULL);
1156 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1157 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1158 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1160 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1161 td->o.ratemin[DDIR_TRIM]) {
1162 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1163 sizeof(td->bw_sample_time));
1164 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1165 sizeof(td->bw_sample_time));
1166 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1167 sizeof(td->bw_sample_time));
1173 prune_io_piece_log(td);
1179 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1180 elapsed = utime_since_now(&td->start);
1181 td->ts.runtime[DDIR_READ] += elapsed;
1183 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1184 elapsed = utime_since_now(&td->start);
1185 td->ts.runtime[DDIR_WRITE] += elapsed;
1187 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1188 elapsed = utime_since_now(&td->start);
1189 td->ts.runtime[DDIR_TRIM] += elapsed;
1192 if (td->error || td->terminate)
1195 if (!td->o.do_verify ||
1196 td->o.verify == VERIFY_NONE ||
1197 (td->io_ops->flags & FIO_UNIDIR))
1202 fio_gettime(&td->start, NULL);
1206 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1208 if (td->error || td->terminate)
1212 update_rusage_stat(td);
1213 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1214 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1215 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1216 td->ts.total_run_time = mtime_since_now(&td->epoch);
1217 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1218 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1219 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1221 fio_mutex_down(writeout_mutex);
1223 if (td->o.bw_log_file) {
1224 finish_log_named(td, td->bw_log,
1225 td->o.bw_log_file, "bw");
1227 finish_log(td, td->bw_log, "bw");
1230 if (td->o.lat_log_file) {
1231 finish_log_named(td, td->lat_log,
1232 td->o.lat_log_file, "lat");
1234 finish_log(td, td->lat_log, "lat");
1237 if (td->o.lat_log_file) {
1238 finish_log_named(td, td->slat_log,
1239 td->o.lat_log_file, "slat");
1241 finish_log(td, td->slat_log, "slat");
1244 if (td->o.lat_log_file) {
1245 finish_log_named(td, td->clat_log,
1246 td->o.lat_log_file, "clat");
1248 finish_log(td, td->clat_log, "clat");
1251 if (td->o.iops_log_file) {
1252 finish_log_named(td, td->iops_log,
1253 td->o.iops_log_file, "iops");
1255 finish_log(td, td->iops_log, "iops");
1258 fio_mutex_up(writeout_mutex);
1259 if (td->o.exec_postrun)
1260 exec_string(td->o.exec_postrun);
1262 if (exitall_on_terminate)
1263 fio_terminate_threads(td->groupid);
1267 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1270 if (td->o.verify_async)
1271 verify_async_exit(td);
1273 close_and_free_files(td);
1276 cgroup_shutdown(td, &cgroup_mnt);
1278 if (td->o.cpumask_set) {
1279 int ret = fio_cpuset_exit(&td->o.cpumask);
1281 td_verror(td, ret, "fio_cpuset_exit");
1285 * do this very late, it will log file closing as well
1287 if (td->o.write_iolog_file)
1288 write_iolog_close(td);
1290 td_set_runstate(td, TD_EXITED);
1291 return (void *) (uintptr_t) td->error;
1296 * We cannot pass the td data into a forked process, so attach the td and
1297 * pass it to the thread worker.
1299 static int fork_main(int shmid, int offset)
1301 struct thread_data *td;
1305 data = shmat(shmid, NULL, 0);
1306 if (data == (void *) -1) {
1314 * HP-UX inherits shm mappings?
1319 td = data + offset * sizeof(struct thread_data);
1320 ret = thread_main(td);
1322 return (int) (uintptr_t) ret;
1326 * Run over the job map and reap the threads that have exited, if any.
1328 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1329 unsigned int *m_rate)
1331 struct thread_data *td;
1332 unsigned int cputhreads, realthreads, pending;
1336 * reap exited threads (TD_EXITED -> TD_REAPED)
1338 realthreads = pending = cputhreads = 0;
1339 for_each_td(td, i) {
1343 * ->io_ops is NULL for a thread that has closed its
1346 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1355 if (td->runstate == TD_REAPED)
1357 if (td->o.use_thread) {
1358 if (td->runstate == TD_EXITED) {
1359 td_set_runstate(td, TD_REAPED);
1366 if (td->runstate == TD_EXITED)
1370 * check if someone quit or got killed in an unusual way
1372 ret = waitpid(td->pid, &status, flags);
1374 if (errno == ECHILD) {
1375 log_err("fio: pid=%d disappeared %d\n",
1376 (int) td->pid, td->runstate);
1378 td_set_runstate(td, TD_REAPED);
1382 } else if (ret == td->pid) {
1383 if (WIFSIGNALED(status)) {
1384 int sig = WTERMSIG(status);
1387 log_err("fio: pid=%d, got signal=%d\n",
1388 (int) td->pid, sig);
1390 td_set_runstate(td, TD_REAPED);
1393 if (WIFEXITED(status)) {
1394 if (WEXITSTATUS(status) && !td->error)
1395 td->error = WEXITSTATUS(status);
1397 td_set_runstate(td, TD_REAPED);
1403 * thread is not dead, continue
1409 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1410 (*t_rate) -= ddir_rw_sum(td->o.rate);
1417 done_secs += mtime_since_now(&td->epoch) / 1000;
1420 if (*nr_running == cputhreads && !pending && realthreads)
1421 fio_terminate_threads(TERMINATE_ALL);
1425 * Main function for kicking off and reaping jobs, as needed.
1427 static void run_threads(void)
1429 struct thread_data *td;
1430 unsigned long spent;
1431 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1433 if (fio_pin_memory())
1436 if (fio_gtod_offload && fio_start_gtod_thread())
1441 if (output_format == FIO_OUTPUT_NORMAL) {
1442 log_info("Starting ");
1444 log_info("%d thread%s", nr_thread,
1445 nr_thread > 1 ? "s" : "");
1449 log_info("%d process%s", nr_process,
1450 nr_process > 1 ? "es" : "");
1456 todo = thread_number;
1459 m_rate = t_rate = 0;
1461 for_each_td(td, i) {
1462 print_status_init(td->thread_number - 1);
1464 if (!td->o.create_serialize)
1468 * do file setup here so it happens sequentially,
1469 * we don't want X number of threads getting their
1470 * client data interspersed on disk
1472 if (setup_files(td)) {
1475 log_err("fio: pid=%d, err=%d/%s\n",
1476 (int) td->pid, td->error, td->verror);
1477 td_set_runstate(td, TD_REAPED);
1484 * for sharing to work, each job must always open
1485 * its own files. so close them, if we opened them
1488 for_each_file(td, f, j) {
1489 if (fio_file_open(f))
1490 td_io_close_file(td, f);
1498 struct thread_data *map[REAL_MAX_JOBS];
1499 struct timeval this_start;
1500 int this_jobs = 0, left;
1503 * create threads (TD_NOT_CREATED -> TD_CREATED)
1505 for_each_td(td, i) {
1506 if (td->runstate != TD_NOT_CREATED)
1510 * never got a chance to start, killed by other
1511 * thread for some reason
1513 if (td->terminate) {
1518 if (td->o.start_delay) {
1519 spent = mtime_since_genesis();
1521 if (td->o.start_delay * 1000 > spent)
1525 if (td->o.stonewall && (nr_started || nr_running)) {
1526 dprint(FD_PROCESS, "%s: stonewall wait\n",
1534 * Set state to created. Thread will transition
1535 * to TD_INITIALIZED when it's done setting up.
1537 td_set_runstate(td, TD_CREATED);
1538 map[this_jobs++] = td;
1541 if (td->o.use_thread) {
1544 dprint(FD_PROCESS, "will pthread_create\n");
1545 ret = pthread_create(&td->thread, NULL,
1548 log_err("pthread_create: %s\n",
1553 ret = pthread_detach(td->thread);
1555 log_err("pthread_detach: %s",
1559 dprint(FD_PROCESS, "will fork\n");
1562 int ret = fork_main(shm_id, i);
1565 } else if (i == fio_debug_jobno)
1566 *fio_debug_jobp = pid;
1568 dprint(FD_MUTEX, "wait on startup_mutex\n");
1569 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1570 log_err("fio: job startup hung? exiting.\n");
1571 fio_terminate_threads(TERMINATE_ALL);
1576 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1580 * Wait for the started threads to transition to
1583 fio_gettime(&this_start, NULL);
1585 while (left && !fio_abort) {
1586 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1591 for (i = 0; i < this_jobs; i++) {
1595 if (td->runstate == TD_INITIALIZED) {
1598 } else if (td->runstate >= TD_EXITED) {
1602 nr_running++; /* work-around... */
1608 log_err("fio: %d job%s failed to start\n", left,
1609 left > 1 ? "s" : "");
1610 for (i = 0; i < this_jobs; i++) {
1614 kill(td->pid, SIGTERM);
1620 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1622 for_each_td(td, i) {
1623 if (td->runstate != TD_INITIALIZED)
1626 if (in_ramp_time(td))
1627 td_set_runstate(td, TD_RAMP);
1629 td_set_runstate(td, TD_RUNNING);
1632 m_rate += ddir_rw_sum(td->o.ratemin);
1633 t_rate += ddir_rw_sum(td->o.rate);
1635 fio_mutex_up(td->mutex);
1638 reap_threads(&nr_running, &t_rate, &m_rate);
1642 fio_server_idle_loop();
1648 while (nr_running) {
1649 reap_threads(&nr_running, &t_rate, &m_rate);
1652 fio_server_idle_loop();
1661 void wait_for_disk_thread_exit(void)
1663 fio_mutex_down(disk_thread_mutex);
1666 static void free_disk_util(void)
1668 disk_util_start_exit();
1669 wait_for_disk_thread_exit();
1670 disk_util_prune_entries();
1673 static void *disk_thread_main(void *data)
1677 fio_mutex_up(startup_mutex);
1679 while (threads && !ret) {
1680 usleep(DISK_UTIL_MSEC * 1000);
1683 ret = update_io_ticks();
1686 print_thread_status();
1689 fio_mutex_up(disk_thread_mutex);
1693 static int create_disk_util_thread(void)
1699 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1701 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1703 fio_mutex_remove(disk_thread_mutex);
1704 log_err("Can't create disk util thread: %s\n", strerror(ret));
1708 ret = pthread_detach(disk_util_thread);
1710 fio_mutex_remove(disk_thread_mutex);
1711 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1715 dprint(FD_MUTEX, "wait on startup_mutex\n");
1716 fio_mutex_down(startup_mutex);
1717 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1721 int fio_backend(void)
1723 struct thread_data *td;
1727 if (load_profile(exec_profile))
1730 exec_profile = NULL;
1736 setup_log(&agg_io_log[DDIR_READ], 0);
1737 setup_log(&agg_io_log[DDIR_WRITE], 0);
1738 setup_log(&agg_io_log[DDIR_TRIM], 0);
1741 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1742 if (startup_mutex == NULL)
1744 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1745 if (writeout_mutex == NULL)
1749 create_disk_util_thread();
1751 cgroup_list = smalloc(sizeof(*cgroup_list));
1752 INIT_FLIST_HEAD(cgroup_list);
1759 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1760 __finish_log(agg_io_log[DDIR_WRITE],
1761 "agg-write_bw.log");
1762 __finish_log(agg_io_log[DDIR_TRIM],
1763 "agg-write_bw.log");
1768 fio_options_free(td);
1771 cgroup_kill(cgroup_list);
1775 fio_mutex_remove(startup_mutex);
1776 fio_mutex_remove(writeout_mutex);
1777 fio_mutex_remove(disk_thread_mutex);