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
54 static pthread_t disk_util_thread;
55 static struct fio_mutex *disk_thread_mutex;
56 static struct fio_mutex *startup_mutex;
57 static struct fio_mutex *writeout_mutex;
58 static struct flist_head *cgroup_list;
59 static char *cgroup_mnt;
60 static int exit_value;
61 static volatile int fio_abort;
62 static unsigned int nr_process = 0;
63 static unsigned int nr_thread = 0;
65 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
68 unsigned int thread_number = 0;
69 unsigned int stat_number = 0;
72 unsigned long done_secs = 0;
73 volatile int disk_util_exit = 0;
75 #define PAGE_ALIGN(buf) \
76 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
78 #define JOB_START_TIMEOUT (5 * 1000)
80 static void sig_int(int sig)
84 fio_server_got_signal(sig);
86 log_info("\nfio: terminating on signal %d\n", sig);
91 fio_terminate_threads(TERMINATE_ALL);
95 static void sig_show_status(int sig)
97 show_running_run_stats();
100 static void set_sig_handlers(void)
102 struct sigaction act;
104 memset(&act, 0, sizeof(act));
105 act.sa_handler = sig_int;
106 act.sa_flags = SA_RESTART;
107 sigaction(SIGINT, &act, NULL);
109 memset(&act, 0, sizeof(act));
110 act.sa_handler = sig_int;
111 act.sa_flags = SA_RESTART;
112 sigaction(SIGTERM, &act, NULL);
114 /* Windows uses SIGBREAK as a quit signal from other applications */
116 memset(&act, 0, sizeof(act));
117 act.sa_handler = sig_int;
118 act.sa_flags = SA_RESTART;
119 sigaction(SIGBREAK, &act, NULL);
122 memset(&act, 0, sizeof(act));
123 act.sa_handler = sig_show_status;
124 act.sa_flags = SA_RESTART;
125 sigaction(SIGUSR1, &act, NULL);
128 memset(&act, 0, sizeof(act));
129 act.sa_handler = sig_int;
130 act.sa_flags = SA_RESTART;
131 sigaction(SIGPIPE, &act, NULL);
136 * Check if we are above the minimum rate given.
138 static int __check_min_rate(struct thread_data *td, struct timeval *now,
141 unsigned long long bytes = 0;
142 unsigned long iops = 0;
145 unsigned int ratemin = 0;
146 unsigned int rate_iops = 0;
147 unsigned int rate_iops_min = 0;
149 assert(ddir_rw(ddir));
151 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
155 * allow a 2 second settle period in the beginning
157 if (mtime_since(&td->start, now) < 2000)
160 iops += td->this_io_blocks[ddir];
161 bytes += td->this_io_bytes[ddir];
162 ratemin += td->o.ratemin[ddir];
163 rate_iops += td->o.rate_iops[ddir];
164 rate_iops_min += td->o.rate_iops_min[ddir];
167 * if rate blocks is set, sample is running
169 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
170 spent = mtime_since(&td->lastrate[ddir], now);
171 if (spent < td->o.ratecycle)
174 if (td->o.rate[ddir]) {
176 * check bandwidth specified rate
178 if (bytes < td->rate_bytes[ddir]) {
179 log_err("%s: min rate %u not met\n", td->o.name,
183 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
184 if (rate < ratemin ||
185 bytes < td->rate_bytes[ddir]) {
186 log_err("%s: min rate %u not met, got"
187 " %luKB/sec\n", td->o.name,
194 * checks iops specified rate
196 if (iops < rate_iops) {
197 log_err("%s: min iops rate %u not met\n",
198 td->o.name, rate_iops);
201 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
202 if (rate < rate_iops_min ||
203 iops < td->rate_blocks[ddir]) {
204 log_err("%s: min iops rate %u not met,"
205 " got %lu\n", td->o.name,
206 rate_iops_min, rate);
212 td->rate_bytes[ddir] = bytes;
213 td->rate_blocks[ddir] = iops;
214 memcpy(&td->lastrate[ddir], now, sizeof(*now));
218 static int check_min_rate(struct thread_data *td, struct timeval *now,
219 unsigned long *bytes_done)
223 if (bytes_done[DDIR_READ])
224 ret |= __check_min_rate(td, now, DDIR_READ);
225 if (bytes_done[DDIR_WRITE])
226 ret |= __check_min_rate(td, now, DDIR_WRITE);
227 if (bytes_done[DDIR_TRIM])
228 ret |= __check_min_rate(td, now, DDIR_TRIM);
234 * When job exits, we can cancel the in-flight IO if we are using async
235 * io. Attempt to do so.
237 static void cleanup_pending_aio(struct thread_data *td)
239 struct flist_head *entry, *n;
244 * get immediately available events, if any
246 r = io_u_queued_complete(td, 0, NULL);
251 * now cancel remaining active events
253 if (td->io_ops->cancel) {
254 flist_for_each_safe(entry, n, &td->io_u_busylist) {
255 io_u = flist_entry(entry, struct io_u, list);
258 * if the io_u isn't in flight, then that generally
259 * means someone leaked an io_u. complain but fix
260 * it up, so we don't stall here.
262 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
263 log_err("fio: non-busy IO on busy list\n");
266 r = td->io_ops->cancel(td, io_u);
274 r = io_u_queued_complete(td, td->cur_depth, NULL);
278 * Helper to handle the final sync of a file. Works just like the normal
279 * io path, just does everything sync.
281 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
283 struct io_u *io_u = __get_io_u(td);
289 io_u->ddir = DDIR_SYNC;
292 if (td_io_prep(td, io_u)) {
298 ret = td_io_queue(td, io_u);
300 td_verror(td, io_u->error, "td_io_queue");
303 } else if (ret == FIO_Q_QUEUED) {
304 if (io_u_queued_complete(td, 1, NULL) < 0)
306 } else if (ret == FIO_Q_COMPLETED) {
308 td_verror(td, io_u->error, "td_io_queue");
312 if (io_u_sync_complete(td, io_u, NULL) < 0)
314 } else if (ret == FIO_Q_BUSY) {
315 if (td_io_commit(td))
323 static inline void __update_tv_cache(struct thread_data *td)
325 fio_gettime(&td->tv_cache, NULL);
328 static inline void update_tv_cache(struct thread_data *td)
330 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
331 __update_tv_cache(td);
334 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
336 if (in_ramp_time(td))
340 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
346 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
351 if (ret < 0 || td->error) {
353 enum error_type_bit eb;
358 eb = td_error_type(ddir, err);
359 if (!(td->o.continue_on_error & (1 << eb)))
362 if (td_non_fatal_error(td, eb, err)) {
364 * Continue with the I/Os in case of
367 update_error_count(td, err);
371 } else if (td->o.fill_device && err == ENOSPC) {
373 * We expect to hit this error if
374 * fill_device option is set.
381 * Stop the I/O in case of a fatal
384 update_error_count(td, err);
393 * The main verify engine. Runs over the writes we previously submitted,
394 * reads the blocks back in, and checks the crc/md5 of the data.
396 static void do_verify(struct thread_data *td)
403 dprint(FD_VERIFY, "starting loop\n");
406 * sync io first and invalidate cache, to make sure we really
409 for_each_file(td, f, i) {
410 if (!fio_file_open(f))
412 if (fio_io_sync(td, f))
414 if (file_invalidate_cache(td, f))
421 td_set_runstate(td, TD_VERIFYING);
424 while (!td->terminate) {
430 if (runtime_exceeded(td, &td->tv_cache)) {
431 __update_tv_cache(td);
432 if (runtime_exceeded(td, &td->tv_cache)) {
438 if (flow_threshold_exceeded(td))
441 io_u = __get_io_u(td);
445 if (get_next_verify(td, io_u)) {
450 if (td_io_prep(td, io_u)) {
455 if (td->o.verify_async)
456 io_u->end_io = verify_io_u_async;
458 io_u->end_io = verify_io_u;
462 ret = td_io_queue(td, io_u);
464 case FIO_Q_COMPLETED:
467 clear_io_u(td, io_u);
468 } else if (io_u->resid) {
469 int bytes = io_u->xfer_buflen - io_u->resid;
475 td_verror(td, EIO, "full resid");
480 io_u->xfer_buflen = io_u->resid;
481 io_u->xfer_buf += bytes;
482 io_u->offset += bytes;
484 if (ddir_rw(io_u->ddir))
485 td->ts.short_io_u[io_u->ddir]++;
488 if (io_u->offset == f->real_file_size)
491 requeue_io_u(td, &io_u);
494 ret = io_u_sync_complete(td, io_u, NULL);
502 requeue_io_u(td, &io_u);
503 ret2 = td_io_commit(td);
509 td_verror(td, -ret, "td_io_queue");
513 if (break_on_this_error(td, ddir, &ret))
517 * if we can queue more, do so. but check if there are
518 * completed io_u's first. Note that we can get BUSY even
519 * without IO queued, if the system is resource starved.
521 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
522 if (full || !td->o.iodepth_batch_complete) {
523 min_events = min(td->o.iodepth_batch_complete,
526 * if the queue is full, we MUST reap at least 1 event
528 if (full && !min_events)
533 * Reap required number of io units, if any,
534 * and do the verification on them through
535 * the callback handler
537 if (io_u_queued_complete(td, min_events, NULL) < 0) {
541 } while (full && (td->cur_depth > td->o.iodepth_low));
548 min_events = td->cur_depth;
551 ret = io_u_queued_complete(td, min_events, NULL);
553 cleanup_pending_aio(td);
555 td_set_runstate(td, TD_RUNNING);
557 dprint(FD_VERIFY, "exiting loop\n");
560 static int io_bytes_exceeded(struct thread_data *td)
562 unsigned long long bytes;
565 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
566 else if (td_write(td))
567 bytes = td->this_io_bytes[DDIR_WRITE];
568 else if (td_read(td))
569 bytes = td->this_io_bytes[DDIR_READ];
571 bytes = td->this_io_bytes[DDIR_TRIM];
573 return bytes >= td->o.size;
577 * Main IO worker function. It retrieves io_u's to process and queues
578 * and reaps them, checking for rate and errors along the way.
580 static void do_io(struct thread_data *td)
585 if (in_ramp_time(td))
586 td_set_runstate(td, TD_RAMP);
588 td_set_runstate(td, TD_RUNNING);
590 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
591 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
593 struct timeval comp_time;
594 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
600 if (td->terminate || td->done)
605 if (runtime_exceeded(td, &td->tv_cache)) {
606 __update_tv_cache(td);
607 if (runtime_exceeded(td, &td->tv_cache)) {
613 if (flow_threshold_exceeded(td))
623 * Add verification end_io handler if:
624 * - Asked to verify (!td_rw(td))
625 * - Or the io_u is from our verify list (mixed write/ver)
627 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
628 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
629 if (td->o.verify_async)
630 io_u->end_io = verify_io_u_async;
632 io_u->end_io = verify_io_u;
633 td_set_runstate(td, TD_VERIFYING);
634 } else if (in_ramp_time(td))
635 td_set_runstate(td, TD_RAMP);
637 td_set_runstate(td, TD_RUNNING);
639 ret = td_io_queue(td, io_u);
641 case FIO_Q_COMPLETED:
644 clear_io_u(td, io_u);
645 } else if (io_u->resid) {
646 int bytes = io_u->xfer_buflen - io_u->resid;
647 struct fio_file *f = io_u->file;
653 td_verror(td, EIO, "full resid");
658 io_u->xfer_buflen = io_u->resid;
659 io_u->xfer_buf += bytes;
660 io_u->offset += bytes;
662 if (ddir_rw(io_u->ddir))
663 td->ts.short_io_u[io_u->ddir]++;
665 if (io_u->offset == f->real_file_size)
668 requeue_io_u(td, &io_u);
671 if (__should_check_rate(td, DDIR_READ) ||
672 __should_check_rate(td, DDIR_WRITE) ||
673 __should_check_rate(td, DDIR_TRIM))
674 fio_gettime(&comp_time, NULL);
676 ret = io_u_sync_complete(td, io_u, bytes_done);
683 * if the engine doesn't have a commit hook,
684 * the io_u is really queued. if it does have such
685 * a hook, it has to call io_u_queued() itself.
687 if (td->io_ops->commit == NULL)
688 io_u_queued(td, io_u);
691 requeue_io_u(td, &io_u);
692 ret2 = td_io_commit(td);
702 if (break_on_this_error(td, ddir, &ret))
706 * See if we need to complete some commands. Note that we
707 * can get BUSY even without IO queued, if the system is
710 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
711 if (full || !td->o.iodepth_batch_complete) {
712 min_evts = min(td->o.iodepth_batch_complete,
715 * if the queue is full, we MUST reap at least 1 event
717 if (full && !min_evts)
720 if (__should_check_rate(td, DDIR_READ) ||
721 __should_check_rate(td, DDIR_WRITE) ||
722 __should_check_rate(td, DDIR_TRIM))
723 fio_gettime(&comp_time, NULL);
726 ret = io_u_queued_complete(td, min_evts, bytes_done);
730 } while (full && (td->cur_depth > td->o.iodepth_low));
735 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
738 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
739 if (check_min_rate(td, &comp_time, bytes_done)) {
740 if (exitall_on_terminate)
741 fio_terminate_threads(td->groupid);
742 td_verror(td, EIO, "check_min_rate");
747 if (td->o.thinktime) {
748 unsigned long long b;
750 b = ddir_rw_sum(td->io_blocks);
751 if (!(b % td->o.thinktime_blocks)) {
754 if (td->o.thinktime_spin)
755 usec_spin(td->o.thinktime_spin);
757 left = td->o.thinktime - td->o.thinktime_spin;
759 usec_sleep(td, left);
764 if (td->trim_entries)
765 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
767 if (td->o.fill_device && td->error == ENOSPC) {
776 ret = io_u_queued_complete(td, i, NULL);
777 if (td->o.fill_device && td->error == ENOSPC)
781 if (should_fsync(td) && td->o.end_fsync) {
782 td_set_runstate(td, TD_FSYNCING);
784 for_each_file(td, f, i) {
785 if (!fio_file_open(f))
791 cleanup_pending_aio(td);
794 * stop job if we failed doing any IO
796 if (!ddir_rw_sum(td->this_io_bytes))
800 static void cleanup_io_u(struct thread_data *td)
802 struct flist_head *entry, *n;
805 flist_for_each_safe(entry, n, &td->io_u_freelist) {
806 io_u = flist_entry(entry, struct io_u, list);
808 flist_del(&io_u->list);
810 if (td->io_ops->io_u_free)
811 td->io_ops->io_u_free(td, io_u);
813 fio_memfree(io_u, sizeof(*io_u));
819 static int init_io_u(struct thread_data *td)
822 unsigned int max_bs, min_write;
823 int cl_align, i, max_units;
827 max_units = td->o.iodepth;
828 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
829 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
830 min_write = td->o.min_bs[DDIR_WRITE];
831 td->orig_buffer_size = (unsigned long long) max_bs
832 * (unsigned long long) max_units;
834 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
837 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
840 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
841 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
844 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
845 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
849 if (data_xfer && allocate_io_mem(td))
852 if (td->o.odirect || td->o.mem_align ||
853 (td->io_ops->flags & FIO_RAWIO))
854 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
858 cl_align = os_cache_line_size();
860 for (i = 0; i < max_units; i++) {
866 ptr = fio_memalign(cl_align, sizeof(*io_u));
868 log_err("fio: unable to allocate aligned memory\n");
873 memset(io_u, 0, sizeof(*io_u));
874 INIT_FLIST_HEAD(&io_u->list);
875 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
879 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
882 io_u_fill_buffer(td, io_u, min_write, max_bs);
883 if (td_write(td) && td->o.verify_pattern_bytes) {
885 * Fill the buffer with the pattern if we are
886 * going to be doing writes.
888 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
893 io_u->flags = IO_U_F_FREE;
894 flist_add(&io_u->list, &td->io_u_freelist);
896 if (td->io_ops->io_u_init) {
897 int ret = td->io_ops->io_u_init(td, io_u);
900 log_err("fio: failed to init engine data: %d\n", ret);
911 static int switch_ioscheduler(struct thread_data *td)
913 char tmp[256], tmp2[128];
917 if (td->io_ops->flags & FIO_DISKLESSIO)
920 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
922 f = fopen(tmp, "r+");
924 if (errno == ENOENT) {
925 log_err("fio: os or kernel doesn't support IO scheduler"
929 td_verror(td, errno, "fopen iosched");
936 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
937 if (ferror(f) || ret != 1) {
938 td_verror(td, errno, "fwrite");
946 * Read back and check that the selected scheduler is now the default.
948 ret = fread(tmp, 1, sizeof(tmp), f);
949 if (ferror(f) || ret < 0) {
950 td_verror(td, errno, "fread");
955 sprintf(tmp2, "[%s]", td->o.ioscheduler);
956 if (!strstr(tmp, tmp2)) {
957 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
958 td_verror(td, EINVAL, "iosched_switch");
967 static int keep_running(struct thread_data *td)
971 if (td->o.time_based)
978 if (ddir_rw_sum(td->io_bytes) < td->o.size)
984 static int exec_string(const char *string)
986 int ret, newlen = strlen(string) + 1 + 8;
989 str = malloc(newlen);
990 sprintf(str, "sh -c %s", string);
994 log_err("fio: exec of cmd <%s> failed\n", str);
1001 * Entry point for the thread based jobs. The process based jobs end up
1002 * here as well, after a little setup.
1004 static void *thread_main(void *data)
1006 unsigned long long elapsed;
1007 struct thread_data *td = data;
1008 struct thread_options *o = &td->o;
1009 pthread_condattr_t attr;
1013 if (!o->use_thread) {
1019 fio_local_clock_init(td->o.use_thread);
1021 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1024 fio_server_send_start(td);
1026 INIT_FLIST_HEAD(&td->io_u_freelist);
1027 INIT_FLIST_HEAD(&td->io_u_busylist);
1028 INIT_FLIST_HEAD(&td->io_u_requeues);
1029 INIT_FLIST_HEAD(&td->io_log_list);
1030 INIT_FLIST_HEAD(&td->io_hist_list);
1031 INIT_FLIST_HEAD(&td->verify_list);
1032 INIT_FLIST_HEAD(&td->trim_list);
1033 pthread_mutex_init(&td->io_u_lock, NULL);
1034 td->io_hist_tree = RB_ROOT;
1036 pthread_condattr_init(&attr);
1037 pthread_cond_init(&td->verify_cond, &attr);
1038 pthread_cond_init(&td->free_cond, &attr);
1040 td_set_runstate(td, TD_INITIALIZED);
1041 dprint(FD_MUTEX, "up startup_mutex\n");
1042 fio_mutex_up(startup_mutex);
1043 dprint(FD_MUTEX, "wait on td->mutex\n");
1044 fio_mutex_down(td->mutex);
1045 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1048 * the ->mutex mutex is now no longer used, close it to avoid
1049 * eating a file descriptor
1051 fio_mutex_remove(td->mutex);
1055 * A new gid requires privilege, so we need to do this before setting
1058 if (o->gid != -1U && setgid(o->gid)) {
1059 td_verror(td, errno, "setgid");
1062 if (o->uid != -1U && setuid(o->uid)) {
1063 td_verror(td, errno, "setuid");
1068 * If we have a gettimeofday() thread, make sure we exclude that
1069 * thread from this job
1072 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1075 * Set affinity first, in case it has an impact on the memory
1078 if (o->cpumask_set) {
1079 ret = fio_setaffinity(td->pid, o->cpumask);
1081 td_verror(td, errno, "cpu_set_affinity");
1086 if (fio_pin_memory(td))
1089 #ifdef FIO_HAVE_LIBNUMA
1090 /* numa node setup */
1091 if (td->o.numa_cpumask_set || td->o.numa_memmask_set) {
1094 if (numa_available() < 0) {
1095 td_verror(td, errno, "Does not support NUMA API\n");
1099 if (td->o.numa_cpumask_set) {
1100 ret = numa_run_on_node_mask(td->o.numa_cpunodesmask);
1102 td_verror(td, errno, \
1103 "numa_run_on_node_mask failed\n");
1108 if (td->o.numa_memmask_set) {
1110 switch (td->o.numa_mem_mode) {
1111 case MPOL_INTERLEAVE:
1112 numa_set_interleave_mask(td->o.numa_memnodesmask);
1115 numa_set_membind(td->o.numa_memnodesmask);
1118 numa_set_localalloc();
1120 case MPOL_PREFERRED:
1121 numa_set_preferred(td->o.numa_mem_prefer_node);
1133 * May alter parameters that init_io_u() will use, so we need to
1142 if (o->verify_async && verify_async_init(td))
1146 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1148 td_verror(td, errno, "ioprio_set");
1153 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1157 if (nice(o->nice) == -1 && errno != 0) {
1158 td_verror(td, errno, "nice");
1162 if (o->ioscheduler && switch_ioscheduler(td))
1165 if (!o->create_serialize && setup_files(td))
1171 if (init_random_map(td))
1174 if (o->exec_prerun && exec_string(o->exec_prerun))
1178 if (pre_read_files(td) < 0)
1182 fio_verify_init(td);
1184 fio_gettime(&td->epoch, NULL);
1185 getrusage(RUSAGE_SELF, &td->ru_start);
1188 while (keep_running(td)) {
1189 fio_gettime(&td->start, NULL);
1190 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1191 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1192 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1194 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1195 td->o.ratemin[DDIR_TRIM]) {
1196 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1197 sizeof(td->bw_sample_time));
1198 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1199 sizeof(td->bw_sample_time));
1200 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1201 sizeof(td->bw_sample_time));
1207 prune_io_piece_log(td);
1213 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1214 elapsed = utime_since_now(&td->start);
1215 td->ts.runtime[DDIR_READ] += elapsed;
1217 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1218 elapsed = utime_since_now(&td->start);
1219 td->ts.runtime[DDIR_WRITE] += elapsed;
1221 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1222 elapsed = utime_since_now(&td->start);
1223 td->ts.runtime[DDIR_TRIM] += elapsed;
1226 if (td->error || td->terminate)
1229 if (!td->o.do_verify ||
1230 td->o.verify == VERIFY_NONE ||
1231 (td->io_ops->flags & FIO_UNIDIR))
1236 fio_gettime(&td->start, NULL);
1240 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1242 if (td->error || td->terminate)
1246 update_rusage_stat(td);
1247 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1248 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1249 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1250 td->ts.total_run_time = mtime_since_now(&td->epoch);
1251 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1252 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1253 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1255 fio_unpin_memory(td);
1257 fio_mutex_down(writeout_mutex);
1259 if (td->o.bw_log_file) {
1260 finish_log_named(td, td->bw_log,
1261 td->o.bw_log_file, "bw");
1263 finish_log(td, td->bw_log, "bw");
1266 if (td->o.lat_log_file) {
1267 finish_log_named(td, td->lat_log,
1268 td->o.lat_log_file, "lat");
1270 finish_log(td, td->lat_log, "lat");
1273 if (td->o.lat_log_file) {
1274 finish_log_named(td, td->slat_log,
1275 td->o.lat_log_file, "slat");
1277 finish_log(td, td->slat_log, "slat");
1280 if (td->o.lat_log_file) {
1281 finish_log_named(td, td->clat_log,
1282 td->o.lat_log_file, "clat");
1284 finish_log(td, td->clat_log, "clat");
1287 if (td->o.iops_log_file) {
1288 finish_log_named(td, td->iops_log,
1289 td->o.iops_log_file, "iops");
1291 finish_log(td, td->iops_log, "iops");
1294 fio_mutex_up(writeout_mutex);
1295 if (td->o.exec_postrun)
1296 exec_string(td->o.exec_postrun);
1298 if (exitall_on_terminate)
1299 fio_terminate_threads(td->groupid);
1303 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1306 if (td->o.verify_async)
1307 verify_async_exit(td);
1309 close_and_free_files(td);
1312 cgroup_shutdown(td, &cgroup_mnt);
1314 if (o->cpumask_set) {
1315 int ret = fio_cpuset_exit(&o->cpumask);
1317 td_verror(td, ret, "fio_cpuset_exit");
1321 * do this very late, it will log file closing as well
1323 if (td->o.write_iolog_file)
1324 write_iolog_close(td);
1326 td_set_runstate(td, TD_EXITED);
1327 return (void *) (uintptr_t) td->error;
1332 * We cannot pass the td data into a forked process, so attach the td and
1333 * pass it to the thread worker.
1335 static int fork_main(int shmid, int offset)
1337 struct thread_data *td;
1341 data = shmat(shmid, NULL, 0);
1342 if (data == (void *) -1) {
1350 * HP-UX inherits shm mappings?
1355 td = data + offset * sizeof(struct thread_data);
1356 ret = thread_main(td);
1358 return (int) (uintptr_t) ret;
1362 * Run over the job map and reap the threads that have exited, if any.
1364 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1365 unsigned int *m_rate)
1367 struct thread_data *td;
1368 unsigned int cputhreads, realthreads, pending;
1372 * reap exited threads (TD_EXITED -> TD_REAPED)
1374 realthreads = pending = cputhreads = 0;
1375 for_each_td(td, i) {
1379 * ->io_ops is NULL for a thread that has closed its
1382 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1391 if (td->runstate == TD_REAPED)
1393 if (td->o.use_thread) {
1394 if (td->runstate == TD_EXITED) {
1395 td_set_runstate(td, TD_REAPED);
1402 if (td->runstate == TD_EXITED)
1406 * check if someone quit or got killed in an unusual way
1408 ret = waitpid(td->pid, &status, flags);
1410 if (errno == ECHILD) {
1411 log_err("fio: pid=%d disappeared %d\n",
1412 (int) td->pid, td->runstate);
1414 td_set_runstate(td, TD_REAPED);
1418 } else if (ret == td->pid) {
1419 if (WIFSIGNALED(status)) {
1420 int sig = WTERMSIG(status);
1422 if (sig != SIGTERM && sig != SIGUSR2)
1423 log_err("fio: pid=%d, got signal=%d\n",
1424 (int) td->pid, sig);
1426 td_set_runstate(td, TD_REAPED);
1429 if (WIFEXITED(status)) {
1430 if (WEXITSTATUS(status) && !td->error)
1431 td->error = WEXITSTATUS(status);
1433 td_set_runstate(td, TD_REAPED);
1439 * thread is not dead, continue
1445 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1446 (*t_rate) -= ddir_rw_sum(td->o.rate);
1453 done_secs += mtime_since_now(&td->epoch) / 1000;
1456 if (*nr_running == cputhreads && !pending && realthreads)
1457 fio_terminate_threads(TERMINATE_ALL);
1461 * Main function for kicking off and reaping jobs, as needed.
1463 static void run_threads(void)
1465 struct thread_data *td;
1466 unsigned long spent;
1467 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1469 if (fio_gtod_offload && fio_start_gtod_thread())
1474 nr_thread = nr_process = 0;
1475 for_each_td(td, i) {
1476 if (td->o.use_thread)
1482 if (output_format == FIO_OUTPUT_NORMAL) {
1483 log_info("Starting ");
1485 log_info("%d thread%s", nr_thread,
1486 nr_thread > 1 ? "s" : "");
1490 log_info("%d process%s", nr_process,
1491 nr_process > 1 ? "es" : "");
1497 todo = thread_number;
1500 m_rate = t_rate = 0;
1502 for_each_td(td, i) {
1503 print_status_init(td->thread_number - 1);
1505 if (!td->o.create_serialize)
1509 * do file setup here so it happens sequentially,
1510 * we don't want X number of threads getting their
1511 * client data interspersed on disk
1513 if (setup_files(td)) {
1516 log_err("fio: pid=%d, err=%d/%s\n",
1517 (int) td->pid, td->error, td->verror);
1518 td_set_runstate(td, TD_REAPED);
1525 * for sharing to work, each job must always open
1526 * its own files. so close them, if we opened them
1529 for_each_file(td, f, j) {
1530 if (fio_file_open(f))
1531 td_io_close_file(td, f);
1539 struct thread_data *map[REAL_MAX_JOBS];
1540 struct timeval this_start;
1541 int this_jobs = 0, left;
1544 * create threads (TD_NOT_CREATED -> TD_CREATED)
1546 for_each_td(td, i) {
1547 if (td->runstate != TD_NOT_CREATED)
1551 * never got a chance to start, killed by other
1552 * thread for some reason
1554 if (td->terminate) {
1559 if (td->o.start_delay) {
1560 spent = mtime_since_genesis();
1562 if (td->o.start_delay * 1000 > spent)
1566 if (td->o.stonewall && (nr_started || nr_running)) {
1567 dprint(FD_PROCESS, "%s: stonewall wait\n",
1575 * Set state to created. Thread will transition
1576 * to TD_INITIALIZED when it's done setting up.
1578 td_set_runstate(td, TD_CREATED);
1579 map[this_jobs++] = td;
1582 if (td->o.use_thread) {
1585 dprint(FD_PROCESS, "will pthread_create\n");
1586 ret = pthread_create(&td->thread, NULL,
1589 log_err("pthread_create: %s\n",
1594 ret = pthread_detach(td->thread);
1596 log_err("pthread_detach: %s",
1600 dprint(FD_PROCESS, "will fork\n");
1603 int ret = fork_main(shm_id, i);
1606 } else if (i == fio_debug_jobno)
1607 *fio_debug_jobp = pid;
1609 dprint(FD_MUTEX, "wait on startup_mutex\n");
1610 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1611 log_err("fio: job startup hung? exiting.\n");
1612 fio_terminate_threads(TERMINATE_ALL);
1617 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1621 * Wait for the started threads to transition to
1624 fio_gettime(&this_start, NULL);
1626 while (left && !fio_abort) {
1627 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1632 for (i = 0; i < this_jobs; i++) {
1636 if (td->runstate == TD_INITIALIZED) {
1639 } else if (td->runstate >= TD_EXITED) {
1643 nr_running++; /* work-around... */
1649 log_err("fio: %d job%s failed to start\n", left,
1650 left > 1 ? "s" : "");
1651 for (i = 0; i < this_jobs; i++) {
1655 kill(td->pid, SIGTERM);
1661 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1663 for_each_td(td, i) {
1664 if (td->runstate != TD_INITIALIZED)
1667 if (in_ramp_time(td))
1668 td_set_runstate(td, TD_RAMP);
1670 td_set_runstate(td, TD_RUNNING);
1673 m_rate += ddir_rw_sum(td->o.ratemin);
1674 t_rate += ddir_rw_sum(td->o.rate);
1676 fio_mutex_up(td->mutex);
1679 reap_threads(&nr_running, &t_rate, &m_rate);
1685 while (nr_running) {
1686 reap_threads(&nr_running, &t_rate, &m_rate);
1693 void wait_for_disk_thread_exit(void)
1695 fio_mutex_down(disk_thread_mutex);
1698 static void free_disk_util(void)
1700 disk_util_start_exit();
1701 wait_for_disk_thread_exit();
1702 disk_util_prune_entries();
1705 static void *disk_thread_main(void *data)
1709 fio_mutex_up(startup_mutex);
1711 while (threads && !ret) {
1712 usleep(DISK_UTIL_MSEC * 1000);
1715 ret = update_io_ticks();
1718 print_thread_status();
1721 fio_mutex_up(disk_thread_mutex);
1725 static int create_disk_util_thread(void)
1731 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1733 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1735 fio_mutex_remove(disk_thread_mutex);
1736 log_err("Can't create disk util thread: %s\n", strerror(ret));
1740 ret = pthread_detach(disk_util_thread);
1742 fio_mutex_remove(disk_thread_mutex);
1743 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1747 dprint(FD_MUTEX, "wait on startup_mutex\n");
1748 fio_mutex_down(startup_mutex);
1749 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1753 int fio_backend(void)
1755 struct thread_data *td;
1759 if (load_profile(exec_profile))
1762 exec_profile = NULL;
1768 setup_log(&agg_io_log[DDIR_READ], 0, IO_LOG_TYPE_BW);
1769 setup_log(&agg_io_log[DDIR_WRITE], 0, IO_LOG_TYPE_BW);
1770 setup_log(&agg_io_log[DDIR_TRIM], 0, IO_LOG_TYPE_BW);
1773 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1774 if (startup_mutex == NULL)
1776 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1777 if (writeout_mutex == NULL)
1781 create_disk_util_thread();
1783 cgroup_list = smalloc(sizeof(*cgroup_list));
1784 INIT_FLIST_HEAD(cgroup_list);
1791 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1792 __finish_log(agg_io_log[DDIR_WRITE],
1793 "agg-write_bw.log");
1794 __finish_log(agg_io_log[DDIR_TRIM],
1795 "agg-write_bw.log");
1800 fio_options_free(td);
1803 cgroup_kill(cgroup_list);
1807 fio_mutex_remove(startup_mutex);
1808 fio_mutex_remove(writeout_mutex);
1809 fio_mutex_remove(disk_thread_mutex);