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
37 #ifndef FIO_NO_HAVE_SHM_H
53 #include "lib/getrusage.h"
55 static pthread_t disk_util_thread;
56 static struct fio_mutex *disk_thread_mutex;
57 static struct fio_mutex *startup_mutex;
58 static struct fio_mutex *writeout_mutex;
59 static struct flist_head *cgroup_list;
60 static char *cgroup_mnt;
61 static int exit_value;
62 static volatile int fio_abort;
64 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
67 unsigned int thread_number = 0;
68 unsigned int stat_number = 0;
69 unsigned int nr_process = 0;
70 unsigned int nr_thread = 0;
73 unsigned long done_secs = 0;
74 volatile int disk_util_exit = 0;
76 #define PAGE_ALIGN(buf) \
77 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
79 #define JOB_START_TIMEOUT (5 * 1000)
81 static void sig_int(int sig)
85 fio_server_got_signal(sig);
87 log_info("\nfio: terminating on signal %d\n", sig);
92 fio_terminate_threads(TERMINATE_ALL);
96 static void sig_show_status(int sig)
98 show_running_run_stats();
101 static void set_sig_handlers(void)
103 struct sigaction act;
105 memset(&act, 0, sizeof(act));
106 act.sa_handler = sig_int;
107 act.sa_flags = SA_RESTART;
108 sigaction(SIGINT, &act, NULL);
110 memset(&act, 0, sizeof(act));
111 act.sa_handler = sig_int;
112 act.sa_flags = SA_RESTART;
113 sigaction(SIGTERM, &act, NULL);
115 /* Windows uses SIGBREAK as a quit signal from other applications */
117 memset(&act, 0, sizeof(act));
118 act.sa_handler = sig_int;
119 act.sa_flags = SA_RESTART;
120 sigaction(SIGBREAK, &act, NULL);
123 memset(&act, 0, sizeof(act));
124 act.sa_handler = sig_show_status;
125 act.sa_flags = SA_RESTART;
126 sigaction(SIGUSR1, &act, NULL);
129 memset(&act, 0, sizeof(act));
130 act.sa_handler = sig_int;
131 act.sa_flags = SA_RESTART;
132 sigaction(SIGPIPE, &act, NULL);
137 * Check if we are above the minimum rate given.
139 static int __check_min_rate(struct thread_data *td, struct timeval *now,
142 unsigned long long bytes = 0;
143 unsigned long iops = 0;
146 unsigned int ratemin = 0;
147 unsigned int rate_iops = 0;
148 unsigned int rate_iops_min = 0;
150 assert(ddir_rw(ddir));
152 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
156 * allow a 2 second settle period in the beginning
158 if (mtime_since(&td->start, now) < 2000)
161 iops += td->this_io_blocks[ddir];
162 bytes += td->this_io_bytes[ddir];
163 ratemin += td->o.ratemin[ddir];
164 rate_iops += td->o.rate_iops[ddir];
165 rate_iops_min += td->o.rate_iops_min[ddir];
168 * if rate blocks is set, sample is running
170 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
171 spent = mtime_since(&td->lastrate[ddir], now);
172 if (spent < td->o.ratecycle)
175 if (td->o.rate[ddir]) {
177 * check bandwidth specified rate
179 if (bytes < td->rate_bytes[ddir]) {
180 log_err("%s: min rate %u not met\n", td->o.name,
184 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
185 if (rate < ratemin ||
186 bytes < td->rate_bytes[ddir]) {
187 log_err("%s: min rate %u not met, got"
188 " %luKB/sec\n", td->o.name,
195 * checks iops specified rate
197 if (iops < rate_iops) {
198 log_err("%s: min iops rate %u not met\n",
199 td->o.name, rate_iops);
202 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
203 if (rate < rate_iops_min ||
204 iops < td->rate_blocks[ddir]) {
205 log_err("%s: min iops rate %u not met,"
206 " got %lu\n", td->o.name,
207 rate_iops_min, rate);
213 td->rate_bytes[ddir] = bytes;
214 td->rate_blocks[ddir] = iops;
215 memcpy(&td->lastrate[ddir], now, sizeof(*now));
219 static int check_min_rate(struct thread_data *td, struct timeval *now,
220 uint64_t *bytes_done)
224 if (bytes_done[DDIR_READ])
225 ret |= __check_min_rate(td, now, DDIR_READ);
226 if (bytes_done[DDIR_WRITE])
227 ret |= __check_min_rate(td, now, DDIR_WRITE);
228 if (bytes_done[DDIR_TRIM])
229 ret |= __check_min_rate(td, now, DDIR_TRIM);
235 * When job exits, we can cancel the in-flight IO if we are using async
236 * io. Attempt to do so.
238 static void cleanup_pending_aio(struct thread_data *td)
240 struct flist_head *entry, *n;
245 * get immediately available events, if any
247 r = io_u_queued_complete(td, 0, NULL);
252 * now cancel remaining active events
254 if (td->io_ops->cancel) {
255 flist_for_each_safe(entry, n, &td->io_u_busylist) {
256 io_u = flist_entry(entry, struct io_u, list);
259 * if the io_u isn't in flight, then that generally
260 * means someone leaked an io_u. complain but fix
261 * it up, so we don't stall here.
263 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
264 log_err("fio: non-busy IO on busy list\n");
267 r = td->io_ops->cancel(td, io_u);
275 r = io_u_queued_complete(td, td->cur_depth, NULL);
279 * Helper to handle the final sync of a file. Works just like the normal
280 * io path, just does everything sync.
282 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
284 struct io_u *io_u = __get_io_u(td);
290 io_u->ddir = DDIR_SYNC;
293 if (td_io_prep(td, io_u)) {
299 ret = td_io_queue(td, io_u);
301 td_verror(td, io_u->error, "td_io_queue");
304 } else if (ret == FIO_Q_QUEUED) {
305 if (io_u_queued_complete(td, 1, NULL) < 0)
307 } else if (ret == FIO_Q_COMPLETED) {
309 td_verror(td, io_u->error, "td_io_queue");
313 if (io_u_sync_complete(td, io_u, NULL) < 0)
315 } else if (ret == FIO_Q_BUSY) {
316 if (td_io_commit(td))
324 static inline void __update_tv_cache(struct thread_data *td)
326 fio_gettime(&td->tv_cache, NULL);
329 static inline void update_tv_cache(struct thread_data *td)
331 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
332 __update_tv_cache(td);
335 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
337 if (in_ramp_time(td))
341 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
347 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
352 if (ret < 0 || td->error) {
354 enum error_type_bit eb;
359 eb = td_error_type(ddir, err);
360 if (!(td->o.continue_on_error & (1 << eb)))
363 if (td_non_fatal_error(td, eb, err)) {
365 * Continue with the I/Os in case of
368 update_error_count(td, err);
372 } else if (td->o.fill_device && err == ENOSPC) {
374 * We expect to hit this error if
375 * fill_device option is set.
382 * Stop the I/O in case of a fatal
385 update_error_count(td, err);
394 * The main verify engine. Runs over the writes we previously submitted,
395 * reads the blocks back in, and checks the crc/md5 of the data.
397 static void do_verify(struct thread_data *td, uint64_t verify_bytes)
399 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
405 dprint(FD_VERIFY, "starting loop\n");
408 * sync io first and invalidate cache, to make sure we really
411 for_each_file(td, f, i) {
412 if (!fio_file_open(f))
414 if (fio_io_sync(td, f))
416 if (file_invalidate_cache(td, f))
423 td_set_runstate(td, TD_VERIFYING);
426 while (!td->terminate) {
432 if (runtime_exceeded(td, &td->tv_cache)) {
433 __update_tv_cache(td);
434 if (runtime_exceeded(td, &td->tv_cache)) {
440 if (flow_threshold_exceeded(td))
443 if (!td->o.experimental_verify) {
444 io_u = __get_io_u(td);
448 if (get_next_verify(td, io_u)) {
453 if (td_io_prep(td, io_u)) {
458 if (ddir_rw_sum(bytes_done) + td->o.rw_min_bs > verify_bytes)
461 while ((io_u = get_io_u(td)) != NULL) {
463 * We are only interested in the places where
464 * we wrote or trimmed IOs. Turn those into
465 * reads for verification purposes.
467 if (io_u->ddir == DDIR_READ) {
469 * Pretend we issued it for rwmix
472 td->io_issues[DDIR_READ]++;
475 } else if (io_u->ddir == DDIR_TRIM) {
476 io_u->ddir = DDIR_READ;
477 io_u->flags |= IO_U_F_TRIMMED;
479 } else if (io_u->ddir == DDIR_WRITE) {
480 io_u->ddir = DDIR_READ;
492 if (td->o.verify_async)
493 io_u->end_io = verify_io_u_async;
495 io_u->end_io = verify_io_u;
499 ret = td_io_queue(td, io_u);
501 case FIO_Q_COMPLETED:
504 clear_io_u(td, io_u);
505 } else if (io_u->resid) {
506 int bytes = io_u->xfer_buflen - io_u->resid;
512 td_verror(td, EIO, "full resid");
517 io_u->xfer_buflen = io_u->resid;
518 io_u->xfer_buf += bytes;
519 io_u->offset += bytes;
521 if (ddir_rw(io_u->ddir))
522 td->ts.short_io_u[io_u->ddir]++;
525 if (io_u->offset == f->real_file_size)
528 requeue_io_u(td, &io_u);
531 ret = io_u_sync_complete(td, io_u, bytes_done);
539 requeue_io_u(td, &io_u);
540 ret2 = td_io_commit(td);
546 td_verror(td, -ret, "td_io_queue");
550 if (break_on_this_error(td, ddir, &ret))
554 * if we can queue more, do so. but check if there are
555 * completed io_u's first. Note that we can get BUSY even
556 * without IO queued, if the system is resource starved.
558 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
559 if (full || !td->o.iodepth_batch_complete) {
560 min_events = min(td->o.iodepth_batch_complete,
563 * if the queue is full, we MUST reap at least 1 event
565 if (full && !min_events)
570 * Reap required number of io units, if any,
571 * and do the verification on them through
572 * the callback handler
574 if (io_u_queued_complete(td, min_events, bytes_done) < 0) {
578 } while (full && (td->cur_depth > td->o.iodepth_low));
585 min_events = td->cur_depth;
588 ret = io_u_queued_complete(td, min_events, NULL);
590 cleanup_pending_aio(td);
592 td_set_runstate(td, TD_RUNNING);
594 dprint(FD_VERIFY, "exiting loop\n");
597 static int io_bytes_exceeded(struct thread_data *td)
599 unsigned long long bytes;
602 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
603 else if (td_write(td))
604 bytes = td->this_io_bytes[DDIR_WRITE];
605 else if (td_read(td))
606 bytes = td->this_io_bytes[DDIR_READ];
608 bytes = td->this_io_bytes[DDIR_TRIM];
610 return bytes >= td->o.size;
614 * Main IO worker function. It retrieves io_u's to process and queues
615 * and reaps them, checking for rate and errors along the way.
617 * Returns number of bytes written and trimmed.
619 static uint64_t do_io(struct thread_data *td)
621 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
625 if (in_ramp_time(td))
626 td_set_runstate(td, TD_RAMP);
628 td_set_runstate(td, TD_RUNNING);
630 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
631 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
633 struct timeval comp_time;
639 if (td->terminate || td->done)
644 if (runtime_exceeded(td, &td->tv_cache)) {
645 __update_tv_cache(td);
646 if (runtime_exceeded(td, &td->tv_cache)) {
652 if (flow_threshold_exceeded(td))
662 * Add verification end_io handler if:
663 * - Asked to verify (!td_rw(td))
664 * - Or the io_u is from our verify list (mixed write/ver)
666 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
667 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
668 if (td->o.verify_async)
669 io_u->end_io = verify_io_u_async;
671 io_u->end_io = verify_io_u;
672 td_set_runstate(td, TD_VERIFYING);
673 } else if (in_ramp_time(td))
674 td_set_runstate(td, TD_RAMP);
676 td_set_runstate(td, TD_RUNNING);
678 ret = td_io_queue(td, io_u);
680 case FIO_Q_COMPLETED:
683 clear_io_u(td, io_u);
684 } else if (io_u->resid) {
685 int bytes = io_u->xfer_buflen - io_u->resid;
686 struct fio_file *f = io_u->file;
692 td_verror(td, EIO, "full resid");
697 io_u->xfer_buflen = io_u->resid;
698 io_u->xfer_buf += bytes;
699 io_u->offset += bytes;
701 if (ddir_rw(io_u->ddir))
702 td->ts.short_io_u[io_u->ddir]++;
704 if (io_u->offset == f->real_file_size)
707 requeue_io_u(td, &io_u);
710 if (__should_check_rate(td, DDIR_READ) ||
711 __should_check_rate(td, DDIR_WRITE) ||
712 __should_check_rate(td, DDIR_TRIM))
713 fio_gettime(&comp_time, NULL);
715 ret = io_u_sync_complete(td, io_u, bytes_done);
722 * if the engine doesn't have a commit hook,
723 * the io_u is really queued. if it does have such
724 * a hook, it has to call io_u_queued() itself.
726 if (td->io_ops->commit == NULL)
727 io_u_queued(td, io_u);
730 requeue_io_u(td, &io_u);
731 ret2 = td_io_commit(td);
741 if (break_on_this_error(td, ddir, &ret))
745 * See if we need to complete some commands. Note that we
746 * can get BUSY even without IO queued, if the system is
749 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
750 if (full || !td->o.iodepth_batch_complete) {
751 min_evts = min(td->o.iodepth_batch_complete,
754 * if the queue is full, we MUST reap at least 1 event
756 if (full && !min_evts)
759 if (__should_check_rate(td, DDIR_READ) ||
760 __should_check_rate(td, DDIR_WRITE) ||
761 __should_check_rate(td, DDIR_TRIM))
762 fio_gettime(&comp_time, NULL);
765 ret = io_u_queued_complete(td, min_evts, bytes_done);
769 } while (full && (td->cur_depth > td->o.iodepth_low));
774 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
777 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
778 if (check_min_rate(td, &comp_time, bytes_done)) {
779 if (exitall_on_terminate)
780 fio_terminate_threads(td->groupid);
781 td_verror(td, EIO, "check_min_rate");
786 if (td->o.thinktime) {
787 unsigned long long b;
789 b = ddir_rw_sum(td->io_blocks);
790 if (!(b % td->o.thinktime_blocks)) {
793 if (td->o.thinktime_spin)
794 usec_spin(td->o.thinktime_spin);
796 left = td->o.thinktime - td->o.thinktime_spin;
798 usec_sleep(td, left);
803 if (td->trim_entries)
804 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
806 if (td->o.fill_device && td->error == ENOSPC) {
815 ret = io_u_queued_complete(td, i, NULL);
816 if (td->o.fill_device && td->error == ENOSPC)
820 if (should_fsync(td) && td->o.end_fsync) {
821 td_set_runstate(td, TD_FSYNCING);
823 for_each_file(td, f, i) {
824 if (!fio_file_open(f))
830 cleanup_pending_aio(td);
833 * stop job if we failed doing any IO
835 if (!ddir_rw_sum(td->this_io_bytes))
838 return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
841 static void cleanup_io_u(struct thread_data *td)
843 struct flist_head *entry, *n;
846 flist_for_each_safe(entry, n, &td->io_u_freelist) {
847 io_u = flist_entry(entry, struct io_u, list);
849 flist_del(&io_u->list);
851 if (td->io_ops->io_u_free)
852 td->io_ops->io_u_free(td, io_u);
854 fio_memfree(io_u, sizeof(*io_u));
860 static int init_io_u(struct thread_data *td)
863 unsigned int max_bs, min_write;
864 int cl_align, i, max_units;
868 max_units = td->o.iodepth;
869 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
870 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
871 min_write = td->o.min_bs[DDIR_WRITE];
872 td->orig_buffer_size = (unsigned long long) max_bs
873 * (unsigned long long) max_units;
875 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
878 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
881 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
882 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
885 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
886 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
890 if (data_xfer && allocate_io_mem(td))
893 if (td->o.odirect || td->o.mem_align ||
894 (td->io_ops->flags & FIO_RAWIO))
895 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
899 cl_align = os_cache_line_size();
901 for (i = 0; i < max_units; i++) {
907 ptr = fio_memalign(cl_align, sizeof(*io_u));
909 log_err("fio: unable to allocate aligned memory\n");
914 memset(io_u, 0, sizeof(*io_u));
915 INIT_FLIST_HEAD(&io_u->list);
916 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
920 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
923 io_u_fill_buffer(td, io_u, min_write, max_bs);
924 if (td_write(td) && td->o.verify_pattern_bytes) {
926 * Fill the buffer with the pattern if we are
927 * going to be doing writes.
929 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
934 io_u->flags = IO_U_F_FREE;
935 flist_add(&io_u->list, &td->io_u_freelist);
937 if (td->io_ops->io_u_init) {
938 int ret = td->io_ops->io_u_init(td, io_u);
941 log_err("fio: failed to init engine data: %d\n", ret);
952 static int switch_ioscheduler(struct thread_data *td)
954 char tmp[256], tmp2[128];
958 if (td->io_ops->flags & FIO_DISKLESSIO)
961 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
963 f = fopen(tmp, "r+");
965 if (errno == ENOENT) {
966 log_err("fio: os or kernel doesn't support IO scheduler"
970 td_verror(td, errno, "fopen iosched");
977 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
978 if (ferror(f) || ret != 1) {
979 td_verror(td, errno, "fwrite");
987 * Read back and check that the selected scheduler is now the default.
989 ret = fread(tmp, 1, sizeof(tmp), f);
990 if (ferror(f) || ret < 0) {
991 td_verror(td, errno, "fread");
996 sprintf(tmp2, "[%s]", td->o.ioscheduler);
997 if (!strstr(tmp, tmp2)) {
998 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
999 td_verror(td, EINVAL, "iosched_switch");
1008 static int keep_running(struct thread_data *td)
1012 if (td->o.time_based)
1019 if (ddir_rw_sum(td->io_bytes) < td->o.size)
1025 static int exec_string(const char *string)
1027 int ret, newlen = strlen(string) + 1 + 8;
1030 str = malloc(newlen);
1031 sprintf(str, "sh -c %s", string);
1035 log_err("fio: exec of cmd <%s> failed\n", str);
1042 * Entry point for the thread based jobs. The process based jobs end up
1043 * here as well, after a little setup.
1045 static void *thread_main(void *data)
1047 unsigned long long elapsed;
1048 struct thread_data *td = data;
1049 pthread_condattr_t attr;
1052 if (!td->o.use_thread) {
1058 fio_local_clock_init(td->o.use_thread);
1060 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1062 INIT_FLIST_HEAD(&td->io_u_freelist);
1063 INIT_FLIST_HEAD(&td->io_u_busylist);
1064 INIT_FLIST_HEAD(&td->io_u_requeues);
1065 INIT_FLIST_HEAD(&td->io_log_list);
1066 INIT_FLIST_HEAD(&td->io_hist_list);
1067 INIT_FLIST_HEAD(&td->verify_list);
1068 INIT_FLIST_HEAD(&td->trim_list);
1069 INIT_FLIST_HEAD(&td->next_rand_list);
1070 pthread_mutex_init(&td->io_u_lock, NULL);
1071 td->io_hist_tree = RB_ROOT;
1073 pthread_condattr_init(&attr);
1074 pthread_cond_init(&td->verify_cond, &attr);
1075 pthread_cond_init(&td->free_cond, &attr);
1077 td_set_runstate(td, TD_INITIALIZED);
1078 dprint(FD_MUTEX, "up startup_mutex\n");
1079 fio_mutex_up(startup_mutex);
1080 dprint(FD_MUTEX, "wait on td->mutex\n");
1081 fio_mutex_down(td->mutex);
1082 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1085 * the ->mutex mutex is now no longer used, close it to avoid
1086 * eating a file descriptor
1088 fio_mutex_remove(td->mutex);
1091 * A new gid requires privilege, so we need to do this before setting
1094 if (td->o.gid != -1U && setgid(td->o.gid)) {
1095 td_verror(td, errno, "setgid");
1098 if (td->o.uid != -1U && setuid(td->o.uid)) {
1099 td_verror(td, errno, "setuid");
1104 * If we have a gettimeofday() thread, make sure we exclude that
1105 * thread from this job
1108 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1111 * Set affinity first, in case it has an impact on the memory
1114 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1115 td_verror(td, errno, "cpu_set_affinity");
1119 #ifdef CONFIG_LIBNUMA
1120 /* numa node setup */
1121 if (td->o.numa_cpumask_set || td->o.numa_memmask_set) {
1124 if (numa_available() < 0) {
1125 td_verror(td, errno, "Does not support NUMA API\n");
1129 if (td->o.numa_cpumask_set) {
1130 ret = numa_run_on_node_mask(td->o.numa_cpunodesmask);
1132 td_verror(td, errno, \
1133 "numa_run_on_node_mask failed\n");
1138 if (td->o.numa_memmask_set) {
1140 switch (td->o.numa_mem_mode) {
1141 case MPOL_INTERLEAVE:
1142 numa_set_interleave_mask(td->o.numa_memnodesmask);
1145 numa_set_membind(td->o.numa_memnodesmask);
1148 numa_set_localalloc();
1150 case MPOL_PREFERRED:
1151 numa_set_preferred(td->o.numa_mem_prefer_node);
1163 * May alter parameters that init_io_u() will use, so we need to
1172 if (td->o.verify_async && verify_async_init(td))
1175 if (td->ioprio_set) {
1176 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1177 td_verror(td, errno, "ioprio_set");
1182 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1186 if (nice(td->o.nice) == -1 && errno != 0) {
1187 td_verror(td, errno, "nice");
1191 if (td->o.ioscheduler && switch_ioscheduler(td))
1194 if (!td->o.create_serialize && setup_files(td))
1200 if (init_random_map(td))
1203 if (td->o.exec_prerun) {
1204 if (exec_string(td->o.exec_prerun))
1208 if (td->o.pre_read) {
1209 if (pre_read_files(td) < 0)
1213 fio_gettime(&td->epoch, NULL);
1214 fio_getrusage(&td->ru_start);
1216 while (keep_running(td)) {
1217 uint64_t verify_bytes;
1219 fio_gettime(&td->start, NULL);
1220 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1221 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1222 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1224 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1225 td->o.ratemin[DDIR_TRIM]) {
1226 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1227 sizeof(td->bw_sample_time));
1228 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1229 sizeof(td->bw_sample_time));
1230 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1231 sizeof(td->bw_sample_time));
1237 prune_io_piece_log(td);
1239 verify_bytes = do_io(td);
1243 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1244 elapsed = utime_since_now(&td->start);
1245 td->ts.runtime[DDIR_READ] += elapsed;
1247 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1248 elapsed = utime_since_now(&td->start);
1249 td->ts.runtime[DDIR_WRITE] += elapsed;
1251 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1252 elapsed = utime_since_now(&td->start);
1253 td->ts.runtime[DDIR_TRIM] += elapsed;
1256 if (td->error || td->terminate)
1259 if (!td->o.do_verify ||
1260 td->o.verify == VERIFY_NONE ||
1261 (td->io_ops->flags & FIO_UNIDIR))
1266 fio_gettime(&td->start, NULL);
1268 do_verify(td, verify_bytes);
1270 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1272 if (td->error || td->terminate)
1276 update_rusage_stat(td);
1277 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1278 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1279 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1280 td->ts.total_run_time = mtime_since_now(&td->epoch);
1281 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1282 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1283 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1285 fio_mutex_down(writeout_mutex);
1287 if (td->o.bw_log_file) {
1288 finish_log_named(td, td->bw_log,
1289 td->o.bw_log_file, "bw");
1291 finish_log(td, td->bw_log, "bw");
1294 if (td->o.lat_log_file) {
1295 finish_log_named(td, td->lat_log,
1296 td->o.lat_log_file, "lat");
1298 finish_log(td, td->lat_log, "lat");
1301 if (td->o.lat_log_file) {
1302 finish_log_named(td, td->slat_log,
1303 td->o.lat_log_file, "slat");
1305 finish_log(td, td->slat_log, "slat");
1308 if (td->o.lat_log_file) {
1309 finish_log_named(td, td->clat_log,
1310 td->o.lat_log_file, "clat");
1312 finish_log(td, td->clat_log, "clat");
1315 if (td->o.iops_log_file) {
1316 finish_log_named(td, td->iops_log,
1317 td->o.iops_log_file, "iops");
1319 finish_log(td, td->iops_log, "iops");
1322 fio_mutex_up(writeout_mutex);
1323 if (td->o.exec_postrun)
1324 exec_string(td->o.exec_postrun);
1326 if (exitall_on_terminate)
1327 fio_terminate_threads(td->groupid);
1331 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1334 if (td->o.verify_async)
1335 verify_async_exit(td);
1337 close_and_free_files(td);
1340 cgroup_shutdown(td, &cgroup_mnt);
1342 if (td->o.cpumask_set) {
1343 int ret = fio_cpuset_exit(&td->o.cpumask);
1345 td_verror(td, ret, "fio_cpuset_exit");
1349 * do this very late, it will log file closing as well
1351 if (td->o.write_iolog_file)
1352 write_iolog_close(td);
1354 td_set_runstate(td, TD_EXITED);
1355 return (void *) (uintptr_t) td->error;
1360 * We cannot pass the td data into a forked process, so attach the td and
1361 * pass it to the thread worker.
1363 static int fork_main(int shmid, int offset)
1365 struct thread_data *td;
1369 data = shmat(shmid, NULL, 0);
1370 if (data == (void *) -1) {
1378 * HP-UX inherits shm mappings?
1383 td = data + offset * sizeof(struct thread_data);
1384 ret = thread_main(td);
1386 return (int) (uintptr_t) ret;
1390 * Run over the job map and reap the threads that have exited, if any.
1392 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1393 unsigned int *m_rate)
1395 struct thread_data *td;
1396 unsigned int cputhreads, realthreads, pending;
1400 * reap exited threads (TD_EXITED -> TD_REAPED)
1402 realthreads = pending = cputhreads = 0;
1403 for_each_td(td, i) {
1407 * ->io_ops is NULL for a thread that has closed its
1410 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1419 if (td->runstate == TD_REAPED)
1421 if (td->o.use_thread) {
1422 if (td->runstate == TD_EXITED) {
1423 td_set_runstate(td, TD_REAPED);
1430 if (td->runstate == TD_EXITED)
1434 * check if someone quit or got killed in an unusual way
1436 ret = waitpid(td->pid, &status, flags);
1438 if (errno == ECHILD) {
1439 log_err("fio: pid=%d disappeared %d\n",
1440 (int) td->pid, td->runstate);
1442 td_set_runstate(td, TD_REAPED);
1446 } else if (ret == td->pid) {
1447 if (WIFSIGNALED(status)) {
1448 int sig = WTERMSIG(status);
1450 if (sig != SIGTERM && sig != SIGUSR2)
1451 log_err("fio: pid=%d, got signal=%d\n",
1452 (int) td->pid, sig);
1454 td_set_runstate(td, TD_REAPED);
1457 if (WIFEXITED(status)) {
1458 if (WEXITSTATUS(status) && !td->error)
1459 td->error = WEXITSTATUS(status);
1461 td_set_runstate(td, TD_REAPED);
1467 * thread is not dead, continue
1473 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1474 (*t_rate) -= ddir_rw_sum(td->o.rate);
1481 done_secs += mtime_since_now(&td->epoch) / 1000;
1484 if (*nr_running == cputhreads && !pending && realthreads)
1485 fio_terminate_threads(TERMINATE_ALL);
1489 * Main function for kicking off and reaping jobs, as needed.
1491 static void run_threads(void)
1493 struct thread_data *td;
1494 unsigned long spent;
1495 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1497 if (fio_pin_memory())
1500 if (fio_gtod_offload && fio_start_gtod_thread())
1505 if (output_format == FIO_OUTPUT_NORMAL) {
1506 log_info("Starting ");
1508 log_info("%d thread%s", nr_thread,
1509 nr_thread > 1 ? "s" : "");
1513 log_info("%d process%s", nr_process,
1514 nr_process > 1 ? "es" : "");
1520 todo = thread_number;
1523 m_rate = t_rate = 0;
1525 for_each_td(td, i) {
1526 print_status_init(td->thread_number - 1);
1528 if (!td->o.create_serialize)
1532 * do file setup here so it happens sequentially,
1533 * we don't want X number of threads getting their
1534 * client data interspersed on disk
1536 if (setup_files(td)) {
1539 log_err("fio: pid=%d, err=%d/%s\n",
1540 (int) td->pid, td->error, td->verror);
1541 td_set_runstate(td, TD_REAPED);
1548 * for sharing to work, each job must always open
1549 * its own files. so close them, if we opened them
1552 for_each_file(td, f, j) {
1553 if (fio_file_open(f))
1554 td_io_close_file(td, f);
1562 struct thread_data *map[REAL_MAX_JOBS];
1563 struct timeval this_start;
1564 int this_jobs = 0, left;
1567 * create threads (TD_NOT_CREATED -> TD_CREATED)
1569 for_each_td(td, i) {
1570 if (td->runstate != TD_NOT_CREATED)
1574 * never got a chance to start, killed by other
1575 * thread for some reason
1577 if (td->terminate) {
1582 if (td->o.start_delay) {
1583 spent = mtime_since_genesis();
1585 if (td->o.start_delay * 1000 > spent)
1589 if (td->o.stonewall && (nr_started || nr_running)) {
1590 dprint(FD_PROCESS, "%s: stonewall wait\n",
1598 * Set state to created. Thread will transition
1599 * to TD_INITIALIZED when it's done setting up.
1601 td_set_runstate(td, TD_CREATED);
1602 map[this_jobs++] = td;
1605 if (td->o.use_thread) {
1608 dprint(FD_PROCESS, "will pthread_create\n");
1609 ret = pthread_create(&td->thread, NULL,
1612 log_err("pthread_create: %s\n",
1617 ret = pthread_detach(td->thread);
1619 log_err("pthread_detach: %s",
1623 dprint(FD_PROCESS, "will fork\n");
1626 int ret = fork_main(shm_id, i);
1629 } else if (i == fio_debug_jobno)
1630 *fio_debug_jobp = pid;
1632 dprint(FD_MUTEX, "wait on startup_mutex\n");
1633 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1634 log_err("fio: job startup hung? exiting.\n");
1635 fio_terminate_threads(TERMINATE_ALL);
1640 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1644 * Wait for the started threads to transition to
1647 fio_gettime(&this_start, NULL);
1649 while (left && !fio_abort) {
1650 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1655 for (i = 0; i < this_jobs; i++) {
1659 if (td->runstate == TD_INITIALIZED) {
1662 } else if (td->runstate >= TD_EXITED) {
1666 nr_running++; /* work-around... */
1672 log_err("fio: %d job%s failed to start\n", left,
1673 left > 1 ? "s" : "");
1674 for (i = 0; i < this_jobs; i++) {
1678 kill(td->pid, SIGTERM);
1684 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1686 for_each_td(td, i) {
1687 if (td->runstate != TD_INITIALIZED)
1690 if (in_ramp_time(td))
1691 td_set_runstate(td, TD_RAMP);
1693 td_set_runstate(td, TD_RUNNING);
1696 m_rate += ddir_rw_sum(td->o.ratemin);
1697 t_rate += ddir_rw_sum(td->o.rate);
1699 fio_mutex_up(td->mutex);
1702 reap_threads(&nr_running, &t_rate, &m_rate);
1706 fio_server_idle_loop();
1712 while (nr_running) {
1713 reap_threads(&nr_running, &t_rate, &m_rate);
1716 fio_server_idle_loop();
1725 void wait_for_disk_thread_exit(void)
1727 fio_mutex_down(disk_thread_mutex);
1730 static void free_disk_util(void)
1732 disk_util_start_exit();
1733 wait_for_disk_thread_exit();
1734 disk_util_prune_entries();
1737 static void *disk_thread_main(void *data)
1741 fio_mutex_up(startup_mutex);
1743 while (threads && !ret) {
1744 usleep(DISK_UTIL_MSEC * 1000);
1747 ret = update_io_ticks();
1750 print_thread_status();
1753 fio_mutex_up(disk_thread_mutex);
1757 static int create_disk_util_thread(void)
1763 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1765 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1767 fio_mutex_remove(disk_thread_mutex);
1768 log_err("Can't create disk util thread: %s\n", strerror(ret));
1772 ret = pthread_detach(disk_util_thread);
1774 fio_mutex_remove(disk_thread_mutex);
1775 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1779 dprint(FD_MUTEX, "wait on startup_mutex\n");
1780 fio_mutex_down(startup_mutex);
1781 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1785 int fio_backend(void)
1787 struct thread_data *td;
1791 if (load_profile(exec_profile))
1794 exec_profile = NULL;
1800 setup_log(&agg_io_log[DDIR_READ], 0);
1801 setup_log(&agg_io_log[DDIR_WRITE], 0);
1802 setup_log(&agg_io_log[DDIR_TRIM], 0);
1805 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1806 if (startup_mutex == NULL)
1808 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1809 if (writeout_mutex == NULL)
1813 create_disk_util_thread();
1815 cgroup_list = smalloc(sizeof(*cgroup_list));
1816 INIT_FLIST_HEAD(cgroup_list);
1823 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1824 __finish_log(agg_io_log[DDIR_WRITE],
1825 "agg-write_bw.log");
1826 __finish_log(agg_io_log[DDIR_TRIM],
1827 "agg-write_bw.log");
1832 fio_options_free(td);
1835 cgroup_kill(cgroup_list);
1839 fio_mutex_remove(startup_mutex);
1840 fio_mutex_remove(writeout_mutex);
1841 fio_mutex_remove(disk_thread_mutex);