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
41 #ifndef FIO_NO_HAVE_SHM_H
52 #include "lib/memalign.h"
54 #include "lib/getrusage.h"
57 #include "workqueue.h"
58 #include "lib/mountcheck.h"
59 #include "rate-submit.h"
60 #include "helper_thread.h"
62 static struct fio_mutex *startup_mutex;
63 static struct flist_head *cgroup_list;
64 static char *cgroup_mnt;
65 static int exit_value;
66 static volatile int fio_abort;
67 static unsigned int nr_process = 0;
68 static unsigned int nr_thread = 0;
70 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
73 unsigned int thread_number = 0;
74 unsigned int stat_number = 0;
77 unsigned long done_secs = 0;
79 #define PAGE_ALIGN(buf) \
80 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
82 #define JOB_START_TIMEOUT (5 * 1000)
84 static void sig_int(int sig)
88 fio_server_got_signal(sig);
90 log_info("\nfio: terminating on signal %d\n", sig);
95 fio_terminate_threads(TERMINATE_ALL);
99 void sig_show_status(int sig)
101 show_running_run_stats();
104 static void set_sig_handlers(void)
106 struct sigaction act;
108 memset(&act, 0, sizeof(act));
109 act.sa_handler = sig_int;
110 act.sa_flags = SA_RESTART;
111 sigaction(SIGINT, &act, NULL);
113 memset(&act, 0, sizeof(act));
114 act.sa_handler = sig_int;
115 act.sa_flags = SA_RESTART;
116 sigaction(SIGTERM, &act, NULL);
118 /* Windows uses SIGBREAK as a quit signal from other applications */
120 memset(&act, 0, sizeof(act));
121 act.sa_handler = sig_int;
122 act.sa_flags = SA_RESTART;
123 sigaction(SIGBREAK, &act, NULL);
126 memset(&act, 0, sizeof(act));
127 act.sa_handler = sig_show_status;
128 act.sa_flags = SA_RESTART;
129 sigaction(SIGUSR1, &act, NULL);
132 memset(&act, 0, sizeof(act));
133 act.sa_handler = sig_int;
134 act.sa_flags = SA_RESTART;
135 sigaction(SIGPIPE, &act, NULL);
140 * Check if we are above the minimum rate given.
142 static bool __check_min_rate(struct thread_data *td, struct timeval *now,
145 unsigned long long bytes = 0;
146 unsigned long iops = 0;
149 unsigned int ratemin = 0;
150 unsigned int rate_iops = 0;
151 unsigned int rate_iops_min = 0;
153 assert(ddir_rw(ddir));
155 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
159 * allow a 2 second settle period in the beginning
161 if (mtime_since(&td->start, now) < 2000)
164 iops += td->this_io_blocks[ddir];
165 bytes += td->this_io_bytes[ddir];
166 ratemin += td->o.ratemin[ddir];
167 rate_iops += td->o.rate_iops[ddir];
168 rate_iops_min += td->o.rate_iops_min[ddir];
171 * if rate blocks is set, sample is running
173 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
174 spent = mtime_since(&td->lastrate[ddir], now);
175 if (spent < td->o.ratecycle)
178 if (td->o.rate[ddir] || td->o.ratemin[ddir]) {
180 * check bandwidth specified rate
182 if (bytes < td->rate_bytes[ddir]) {
183 log_err("%s: rate_min=%uB/s not met, only transferred %lluB\n",
184 td->o.name, ratemin, bytes);
188 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
192 if (rate < ratemin ||
193 bytes < td->rate_bytes[ddir]) {
194 log_err("%s: rate_min=%uB/s not met, got %luB/s\n",
195 td->o.name, ratemin, rate);
201 * checks iops specified rate
203 if (iops < rate_iops) {
204 log_err("%s: rate_iops_min=%u not met, only performed %lu IOs\n",
205 td->o.name, rate_iops, iops);
209 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
213 if (rate < rate_iops_min ||
214 iops < td->rate_blocks[ddir]) {
215 log_err("%s: rate_iops_min=%u not met, got %lu IOPS\n",
216 td->o.name, rate_iops_min, rate);
223 td->rate_bytes[ddir] = bytes;
224 td->rate_blocks[ddir] = iops;
225 memcpy(&td->lastrate[ddir], now, sizeof(*now));
229 static bool check_min_rate(struct thread_data *td, struct timeval *now)
233 if (td->bytes_done[DDIR_READ])
234 ret |= __check_min_rate(td, now, DDIR_READ);
235 if (td->bytes_done[DDIR_WRITE])
236 ret |= __check_min_rate(td, now, DDIR_WRITE);
237 if (td->bytes_done[DDIR_TRIM])
238 ret |= __check_min_rate(td, now, DDIR_TRIM);
244 * When job exits, we can cancel the in-flight IO if we are using async
245 * io. Attempt to do so.
247 static void cleanup_pending_aio(struct thread_data *td)
252 * get immediately available events, if any
254 r = io_u_queued_complete(td, 0);
259 * now cancel remaining active events
261 if (td->io_ops->cancel) {
265 io_u_qiter(&td->io_u_all, io_u, i) {
266 if (io_u->flags & IO_U_F_FLIGHT) {
267 r = td->io_ops->cancel(td, io_u);
275 r = io_u_queued_complete(td, td->cur_depth);
279 * Helper to handle the final sync of a file. Works just like the normal
280 * io path, just does everything sync.
282 static bool 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 (td_io_commit(td))
307 if (io_u_queued_complete(td, 1) < 0)
309 } else if (ret == FIO_Q_COMPLETED) {
311 td_verror(td, io_u->error, "td_io_queue");
315 if (io_u_sync_complete(td, io_u) < 0)
317 } else if (ret == FIO_Q_BUSY) {
318 if (td_io_commit(td))
326 static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
330 if (fio_file_open(f))
331 return fio_io_sync(td, f);
333 if (td_io_open_file(td, f))
336 ret = fio_io_sync(td, f);
337 td_io_close_file(td, f);
341 static inline void __update_tv_cache(struct thread_data *td)
343 fio_gettime(&td->tv_cache, NULL);
346 static inline void update_tv_cache(struct thread_data *td)
348 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
349 __update_tv_cache(td);
352 static inline bool runtime_exceeded(struct thread_data *td, struct timeval *t)
354 if (in_ramp_time(td))
358 if (utime_since(&td->epoch, t) >= td->o.timeout)
365 * We need to update the runtime consistently in ms, but keep a running
366 * tally of the current elapsed time in microseconds for sub millisecond
369 static inline void update_runtime(struct thread_data *td,
370 unsigned long long *elapsed_us,
371 const enum fio_ddir ddir)
373 if (ddir == DDIR_WRITE && td_write(td) && td->o.verify_only)
376 td->ts.runtime[ddir] -= (elapsed_us[ddir] + 999) / 1000;
377 elapsed_us[ddir] += utime_since_now(&td->start);
378 td->ts.runtime[ddir] += (elapsed_us[ddir] + 999) / 1000;
381 static bool break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
386 if (ret < 0 || td->error) {
388 enum error_type_bit eb;
393 eb = td_error_type(ddir, err);
394 if (!(td->o.continue_on_error & (1 << eb)))
397 if (td_non_fatal_error(td, eb, err)) {
399 * Continue with the I/Os in case of
402 update_error_count(td, err);
406 } else if (td->o.fill_device && err == ENOSPC) {
408 * We expect to hit this error if
409 * fill_device option is set.
412 fio_mark_td_terminate(td);
416 * Stop the I/O in case of a fatal
419 update_error_count(td, err);
427 static void check_update_rusage(struct thread_data *td)
429 if (td->update_rusage) {
430 td->update_rusage = 0;
431 update_rusage_stat(td);
432 fio_mutex_up(td->rusage_sem);
436 static int wait_for_completions(struct thread_data *td, struct timeval *time)
438 const int full = queue_full(td);
442 if (td->flags & TD_F_REGROW_LOGS)
443 return io_u_quiesce(td);
446 * if the queue is full, we MUST reap at least 1 event
448 min_evts = min(td->o.iodepth_batch_complete_min, td->cur_depth);
449 if ((full && !min_evts) || !td->o.iodepth_batch_complete_min)
452 if (time && (__should_check_rate(td, DDIR_READ) ||
453 __should_check_rate(td, DDIR_WRITE) ||
454 __should_check_rate(td, DDIR_TRIM)))
455 fio_gettime(time, NULL);
458 ret = io_u_queued_complete(td, min_evts);
461 } while (full && (td->cur_depth > td->o.iodepth_low));
466 int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
467 enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
468 struct timeval *comp_time)
473 case FIO_Q_COMPLETED:
476 clear_io_u(td, io_u);
477 } else if (io_u->resid) {
478 int bytes = io_u->xfer_buflen - io_u->resid;
479 struct fio_file *f = io_u->file;
482 *bytes_issued += bytes;
485 trim_io_piece(td, io_u);
492 unlog_io_piece(td, io_u);
493 td_verror(td, EIO, "full resid");
498 io_u->xfer_buflen = io_u->resid;
499 io_u->xfer_buf += bytes;
500 io_u->offset += bytes;
502 if (ddir_rw(io_u->ddir))
503 td->ts.short_io_u[io_u->ddir]++;
506 if (io_u->offset == f->real_file_size)
509 requeue_io_u(td, &io_u);
512 if (comp_time && (__should_check_rate(td, DDIR_READ) ||
513 __should_check_rate(td, DDIR_WRITE) ||
514 __should_check_rate(td, DDIR_TRIM)))
515 fio_gettime(comp_time, NULL);
517 *ret = io_u_sync_complete(td, io_u);
522 if (td->flags & TD_F_REGROW_LOGS)
526 * when doing I/O (not when verifying),
527 * check for any errors that are to be ignored
535 * if the engine doesn't have a commit hook,
536 * the io_u is really queued. if it does have such
537 * a hook, it has to call io_u_queued() itself.
539 if (td->io_ops->commit == NULL)
540 io_u_queued(td, io_u);
542 *bytes_issued += io_u->xfer_buflen;
546 unlog_io_piece(td, io_u);
547 requeue_io_u(td, &io_u);
548 ret2 = td_io_commit(td);
554 td_verror(td, -(*ret), "td_io_queue");
558 if (break_on_this_error(td, ddir, ret))
564 static inline bool io_in_polling(struct thread_data *td)
566 return !td->o.iodepth_batch_complete_min &&
567 !td->o.iodepth_batch_complete_max;
570 * Unlinks files from thread data fio_file structure
572 static int unlink_all_files(struct thread_data *td)
578 for_each_file(td, f, i) {
579 if (f->filetype != FIO_TYPE_FILE)
581 ret = td_io_unlink_file(td, f);
587 td_verror(td, ret, "unlink_all_files");
593 * The main verify engine. Runs over the writes we previously submitted,
594 * reads the blocks back in, and checks the crc/md5 of the data.
596 static void do_verify(struct thread_data *td, uint64_t verify_bytes)
603 dprint(FD_VERIFY, "starting loop\n");
606 * sync io first and invalidate cache, to make sure we really
609 for_each_file(td, f, i) {
610 if (!fio_file_open(f))
612 if (fio_io_sync(td, f))
614 if (file_invalidate_cache(td, f))
618 check_update_rusage(td);
624 * verify_state needs to be reset before verification
625 * proceeds so that expected random seeds match actual
626 * random seeds in headers. The main loop will reset
627 * all random number generators if randrepeat is set.
629 if (!td->o.rand_repeatable)
630 td_fill_verify_state_seed(td);
632 td_set_runstate(td, TD_VERIFYING);
635 while (!td->terminate) {
640 check_update_rusage(td);
642 if (runtime_exceeded(td, &td->tv_cache)) {
643 __update_tv_cache(td);
644 if (runtime_exceeded(td, &td->tv_cache)) {
645 fio_mark_td_terminate(td);
650 if (flow_threshold_exceeded(td))
653 if (!td->o.experimental_verify) {
654 io_u = __get_io_u(td);
658 if (get_next_verify(td, io_u)) {
663 if (td_io_prep(td, io_u)) {
668 if (ddir_rw_sum(td->bytes_done) + td->o.rw_min_bs > verify_bytes)
671 while ((io_u = get_io_u(td)) != NULL) {
672 if (IS_ERR_OR_NULL(io_u)) {
679 * We are only interested in the places where
680 * we wrote or trimmed IOs. Turn those into
681 * reads for verification purposes.
683 if (io_u->ddir == DDIR_READ) {
685 * Pretend we issued it for rwmix
688 td->io_issues[DDIR_READ]++;
691 } else if (io_u->ddir == DDIR_TRIM) {
692 io_u->ddir = DDIR_READ;
693 io_u_set(td, io_u, IO_U_F_TRIMMED);
695 } else if (io_u->ddir == DDIR_WRITE) {
696 io_u->ddir = DDIR_READ;
708 if (verify_state_should_stop(td, io_u)) {
713 if (td->o.verify_async)
714 io_u->end_io = verify_io_u_async;
716 io_u->end_io = verify_io_u;
719 if (!td->o.disable_slat)
720 fio_gettime(&io_u->start_time, NULL);
722 ret = td_io_queue(td, io_u);
724 if (io_queue_event(td, io_u, &ret, ddir, NULL, 1, NULL))
728 * if we can queue more, do so. but check if there are
729 * completed io_u's first. Note that we can get BUSY even
730 * without IO queued, if the system is resource starved.
733 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
734 if (full || io_in_polling(td))
735 ret = wait_for_completions(td, NULL);
741 check_update_rusage(td);
744 min_events = td->cur_depth;
747 ret = io_u_queued_complete(td, min_events);
749 cleanup_pending_aio(td);
751 td_set_runstate(td, TD_RUNNING);
753 dprint(FD_VERIFY, "exiting loop\n");
756 static bool exceeds_number_ios(struct thread_data *td)
758 unsigned long long number_ios;
760 if (!td->o.number_ios)
763 number_ios = ddir_rw_sum(td->io_blocks);
764 number_ios += td->io_u_queued + td->io_u_in_flight;
766 return number_ios >= (td->o.number_ios * td->loops);
769 static bool io_bytes_exceeded(struct thread_data *td, uint64_t *this_bytes)
771 unsigned long long bytes, limit;
774 bytes = this_bytes[DDIR_READ] + this_bytes[DDIR_WRITE];
775 else if (td_write(td))
776 bytes = this_bytes[DDIR_WRITE];
777 else if (td_read(td))
778 bytes = this_bytes[DDIR_READ];
780 bytes = this_bytes[DDIR_TRIM];
783 limit = td->o.io_limit;
788 return bytes >= limit || exceeds_number_ios(td);
791 static bool io_issue_bytes_exceeded(struct thread_data *td)
793 return io_bytes_exceeded(td, td->io_issue_bytes);
796 static bool io_complete_bytes_exceeded(struct thread_data *td)
798 return io_bytes_exceeded(td, td->this_io_bytes);
802 * used to calculate the next io time for rate control
805 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
807 uint64_t secs, remainder, bps, bytes, iops;
809 assert(!(td->flags & TD_F_CHILD));
810 bytes = td->rate_io_issue_bytes[ddir];
811 bps = td->rate_bps[ddir];
813 if (td->o.rate_process == RATE_PROCESS_POISSON) {
815 iops = bps / td->o.bs[ddir];
816 val = (int64_t) (1000000 / iops) *
817 -logf(__rand_0_1(&td->poisson_state));
819 dprint(FD_RATE, "poisson rate iops=%llu\n",
820 (unsigned long long) 1000000 / val);
822 td->last_usec += val;
823 return td->last_usec;
826 remainder = bytes % bps;
827 return remainder * 1000000 / bps + secs * 1000000;
834 * Main IO worker function. It retrieves io_u's to process and queues
835 * and reaps them, checking for rate and errors along the way.
837 * Returns number of bytes written and trimmed.
839 static void do_io(struct thread_data *td, uint64_t *bytes_done)
843 uint64_t total_bytes, bytes_issued = 0;
845 for (i = 0; i < DDIR_RWDIR_CNT; i++)
846 bytes_done[i] = td->bytes_done[i];
848 if (in_ramp_time(td))
849 td_set_runstate(td, TD_RAMP);
851 td_set_runstate(td, TD_RUNNING);
855 total_bytes = td->o.size;
857 * Allow random overwrite workloads to write up to io_limit
858 * before starting verification phase as 'size' doesn't apply.
860 if (td_write(td) && td_random(td) && td->o.norandommap)
861 total_bytes = max(total_bytes, (uint64_t) td->o.io_limit);
863 * If verify_backlog is enabled, we'll run the verify in this
864 * handler as well. For that case, we may need up to twice the
867 if (td->o.verify != VERIFY_NONE &&
868 (td_write(td) && td->o.verify_backlog))
869 total_bytes += td->o.size;
871 /* In trimwrite mode, each byte is trimmed and then written, so
872 * allow total_bytes to be twice as big */
873 if (td_trimwrite(td))
874 total_bytes += td->total_io_size;
876 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
877 (!flist_empty(&td->trim_list)) || !io_issue_bytes_exceeded(td) ||
879 struct timeval comp_time;
884 check_update_rusage(td);
886 if (td->terminate || td->done)
891 if (runtime_exceeded(td, &td->tv_cache)) {
892 __update_tv_cache(td);
893 if (runtime_exceeded(td, &td->tv_cache)) {
894 fio_mark_td_terminate(td);
899 if (flow_threshold_exceeded(td))
903 * Break if we exceeded the bytes. The exception is time
904 * based runs, but we still need to break out of the loop
905 * for those to run verification, if enabled.
907 if (bytes_issued >= total_bytes &&
908 (!td->o.time_based ||
909 (td->o.time_based && td->o.verify != VERIFY_NONE)))
913 if (IS_ERR_OR_NULL(io_u)) {
914 int err = PTR_ERR(io_u);
921 if (td->o.latency_target)
929 * Add verification end_io handler if:
930 * - Asked to verify (!td_rw(td))
931 * - Or the io_u is from our verify list (mixed write/ver)
933 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
934 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
936 if (!td->o.verify_pattern_bytes) {
937 io_u->rand_seed = __rand(&td->verify_state);
938 if (sizeof(int) != sizeof(long *))
939 io_u->rand_seed *= __rand(&td->verify_state);
942 if (verify_state_should_stop(td, io_u)) {
947 if (td->o.verify_async)
948 io_u->end_io = verify_io_u_async;
950 io_u->end_io = verify_io_u;
951 td_set_runstate(td, TD_VERIFYING);
952 } else if (in_ramp_time(td))
953 td_set_runstate(td, TD_RAMP);
955 td_set_runstate(td, TD_RUNNING);
958 * Always log IO before it's issued, so we know the specific
959 * order of it. The logged unit will track when the IO has
962 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
964 td->o.verify != VERIFY_NONE &&
965 !td->o.experimental_verify)
966 log_io_piece(td, io_u);
968 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
969 const unsigned long blen = io_u->xfer_buflen;
970 const enum fio_ddir ddir = acct_ddir(io_u);
975 workqueue_enqueue(&td->io_wq, &io_u->work);
979 td->io_issues[ddir]++;
980 td->io_issue_bytes[ddir] += blen;
981 td->rate_io_issue_bytes[ddir] += blen;
984 if (should_check_rate(td))
985 td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
988 ret = td_io_queue(td, io_u);
990 if (should_check_rate(td))
991 td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
993 if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 0, &comp_time))
997 * See if we need to complete some commands. Note that
998 * we can get BUSY even without IO queued, if the
999 * system is resource starved.
1002 full = queue_full(td) ||
1003 (ret == FIO_Q_BUSY && td->cur_depth);
1004 if (full || io_in_polling(td))
1005 ret = wait_for_completions(td, &comp_time);
1009 if (!ddir_rw_sum(td->bytes_done) &&
1010 !td_ioengine_flagged(td, FIO_NOIO))
1013 if (!in_ramp_time(td) && should_check_rate(td)) {
1014 if (check_min_rate(td, &comp_time)) {
1015 if (exitall_on_terminate || td->o.exitall_error)
1016 fio_terminate_threads(td->groupid);
1017 td_verror(td, EIO, "check_min_rate");
1021 if (!in_ramp_time(td) && td->o.latency_target)
1022 lat_target_check(td);
1024 if (td->o.thinktime) {
1025 unsigned long long b;
1027 b = ddir_rw_sum(td->io_blocks);
1028 if (!(b % td->o.thinktime_blocks)) {
1033 if (td->o.thinktime_spin)
1034 usec_spin(td->o.thinktime_spin);
1036 left = td->o.thinktime - td->o.thinktime_spin;
1038 usec_sleep(td, left);
1043 check_update_rusage(td);
1045 if (td->trim_entries)
1046 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
1048 if (td->o.fill_device && td->error == ENOSPC) {
1050 fio_mark_td_terminate(td);
1055 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
1056 workqueue_flush(&td->io_wq);
1062 ret = io_u_queued_complete(td, i);
1063 if (td->o.fill_device && td->error == ENOSPC)
1067 if (should_fsync(td) && td->o.end_fsync) {
1068 td_set_runstate(td, TD_FSYNCING);
1070 for_each_file(td, f, i) {
1071 if (!fio_file_fsync(td, f))
1074 log_err("fio: end_fsync failed for file %s\n",
1079 cleanup_pending_aio(td);
1082 * stop job if we failed doing any IO
1084 if (!ddir_rw_sum(td->this_io_bytes))
1087 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1088 bytes_done[i] = td->bytes_done[i] - bytes_done[i];
1091 static void free_file_completion_logging(struct thread_data *td)
1096 for_each_file(td, f, i) {
1097 if (!f->last_write_comp)
1099 sfree(f->last_write_comp);
1103 static int init_file_completion_logging(struct thread_data *td,
1109 if (td->o.verify == VERIFY_NONE || !td->o.verify_state_save)
1112 for_each_file(td, f, i) {
1113 f->last_write_comp = scalloc(depth, sizeof(uint64_t));
1114 if (!f->last_write_comp)
1121 free_file_completion_logging(td);
1122 log_err("fio: failed to alloc write comp data\n");
1126 static void cleanup_io_u(struct thread_data *td)
1130 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
1132 if (td->io_ops->io_u_free)
1133 td->io_ops->io_u_free(td, io_u);
1135 fio_memfree(io_u, sizeof(*io_u));
1140 io_u_rexit(&td->io_u_requeues);
1141 io_u_qexit(&td->io_u_freelist);
1142 io_u_qexit(&td->io_u_all);
1144 free_file_completion_logging(td);
1147 static int init_io_u(struct thread_data *td)
1150 unsigned int max_bs, min_write;
1151 int cl_align, i, max_units;
1152 int data_xfer = 1, err;
1155 max_units = td->o.iodepth;
1156 max_bs = td_max_bs(td);
1157 min_write = td->o.min_bs[DDIR_WRITE];
1158 td->orig_buffer_size = (unsigned long long) max_bs
1159 * (unsigned long long) max_units;
1161 if (td_ioengine_flagged(td, FIO_NOIO) || !(td_read(td) || td_write(td)))
1165 err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
1166 err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
1167 err += io_u_qinit(&td->io_u_all, td->o.iodepth);
1170 log_err("fio: failed setting up IO queues\n");
1175 * if we may later need to do address alignment, then add any
1176 * possible adjustment here so that we don't cause a buffer
1177 * overflow later. this adjustment may be too much if we get
1178 * lucky and the allocator gives us an aligned address.
1180 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1181 td_ioengine_flagged(td, FIO_RAWIO))
1182 td->orig_buffer_size += page_mask + td->o.mem_align;
1184 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
1187 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
1188 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
1191 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
1192 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
1196 if (data_xfer && allocate_io_mem(td))
1199 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1200 td_ioengine_flagged(td, FIO_RAWIO))
1201 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
1203 p = td->orig_buffer;
1205 cl_align = os_cache_line_size();
1207 for (i = 0; i < max_units; i++) {
1213 ptr = fio_memalign(cl_align, sizeof(*io_u));
1215 log_err("fio: unable to allocate aligned memory\n");
1220 memset(io_u, 0, sizeof(*io_u));
1221 INIT_FLIST_HEAD(&io_u->verify_list);
1222 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1226 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
1229 io_u_fill_buffer(td, io_u, min_write, max_bs);
1230 if (td_write(td) && td->o.verify_pattern_bytes) {
1232 * Fill the buffer with the pattern if we are
1233 * going to be doing writes.
1235 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
1240 io_u->flags = IO_U_F_FREE;
1241 io_u_qpush(&td->io_u_freelist, io_u);
1244 * io_u never leaves this stack, used for iteration of all
1247 io_u_qpush(&td->io_u_all, io_u);
1249 if (td->io_ops->io_u_init) {
1250 int ret = td->io_ops->io_u_init(td, io_u);
1253 log_err("fio: failed to init engine data: %d\n", ret);
1261 if (init_file_completion_logging(td, max_units))
1267 static int switch_ioscheduler(struct thread_data *td)
1269 #ifdef FIO_HAVE_IOSCHED_SWITCH
1270 char tmp[256], tmp2[128];
1274 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
1277 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1279 f = fopen(tmp, "r+");
1281 if (errno == ENOENT) {
1282 log_err("fio: os or kernel doesn't support IO scheduler"
1286 td_verror(td, errno, "fopen iosched");
1293 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
1294 if (ferror(f) || ret != 1) {
1295 td_verror(td, errno, "fwrite");
1303 * Read back and check that the selected scheduler is now the default.
1305 memset(tmp, 0, sizeof(tmp));
1306 ret = fread(tmp, sizeof(tmp), 1, f);
1307 if (ferror(f) || ret < 0) {
1308 td_verror(td, errno, "fread");
1313 * either a list of io schedulers or "none\n" is expected.
1315 tmp[strlen(tmp) - 1] = '\0';
1318 * Write to "none" entry doesn't fail, so check the result here.
1320 if (!strcmp(tmp, "none")) {
1321 log_err("fio: io scheduler is not tunable\n");
1326 sprintf(tmp2, "[%s]", td->o.ioscheduler);
1327 if (!strstr(tmp, tmp2)) {
1328 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1329 td_verror(td, EINVAL, "iosched_switch");
1341 static bool keep_running(struct thread_data *td)
1343 unsigned long long limit;
1347 if (td->o.time_based)
1353 if (exceeds_number_ios(td))
1357 limit = td->o.io_limit;
1361 if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
1365 * If the difference is less than the minimum IO size, we
1368 diff = limit - ddir_rw_sum(td->io_bytes);
1369 if (diff < td_max_bs(td))
1372 if (fio_files_done(td) && !td->o.io_limit)
1381 static int exec_string(struct thread_options *o, const char *string, const char *mode)
1383 size_t newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
1387 str = malloc(newlen);
1388 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
1390 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
1393 log_err("fio: exec of cmd <%s> failed\n", str);
1400 * Dry run to compute correct state of numberio for verification.
1402 static uint64_t do_dry_run(struct thread_data *td)
1404 td_set_runstate(td, TD_RUNNING);
1406 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1407 (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) {
1411 if (td->terminate || td->done)
1414 io_u = get_io_u(td);
1415 if (IS_ERR_OR_NULL(io_u))
1418 io_u_set(td, io_u, IO_U_F_FLIGHT);
1421 if (ddir_rw(acct_ddir(io_u)))
1422 td->io_issues[acct_ddir(io_u)]++;
1423 if (ddir_rw(io_u->ddir)) {
1424 io_u_mark_depth(td, 1);
1425 td->ts.total_io_u[io_u->ddir]++;
1428 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1430 td->o.verify != VERIFY_NONE &&
1431 !td->o.experimental_verify)
1432 log_io_piece(td, io_u);
1434 ret = io_u_sync_complete(td, io_u);
1438 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
1442 struct thread_data *td;
1443 struct sk_out *sk_out;
1447 * Entry point for the thread based jobs. The process based jobs end up
1448 * here as well, after a little setup.
1450 static void *thread_main(void *data)
1452 struct fork_data *fd = data;
1453 unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, };
1454 struct thread_data *td = fd->td;
1455 struct thread_options *o = &td->o;
1456 struct sk_out *sk_out = fd->sk_out;
1457 int deadlock_loop_cnt;
1461 sk_out_assign(sk_out);
1464 if (!o->use_thread) {
1470 fio_local_clock_init(o->use_thread);
1472 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1475 fio_server_send_start(td);
1477 INIT_FLIST_HEAD(&td->io_log_list);
1478 INIT_FLIST_HEAD(&td->io_hist_list);
1479 INIT_FLIST_HEAD(&td->verify_list);
1480 INIT_FLIST_HEAD(&td->trim_list);
1481 INIT_FLIST_HEAD(&td->next_rand_list);
1482 td->io_hist_tree = RB_ROOT;
1484 ret = mutex_cond_init_pshared(&td->io_u_lock, &td->free_cond);
1486 td_verror(td, ret, "mutex_cond_init_pshared");
1489 ret = cond_init_pshared(&td->verify_cond);
1491 td_verror(td, ret, "mutex_cond_pshared");
1495 td_set_runstate(td, TD_INITIALIZED);
1496 dprint(FD_MUTEX, "up startup_mutex\n");
1497 fio_mutex_up(startup_mutex);
1498 dprint(FD_MUTEX, "wait on td->mutex\n");
1499 fio_mutex_down(td->mutex);
1500 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1503 * A new gid requires privilege, so we need to do this before setting
1506 if (o->gid != -1U && setgid(o->gid)) {
1507 td_verror(td, errno, "setgid");
1510 if (o->uid != -1U && setuid(o->uid)) {
1511 td_verror(td, errno, "setuid");
1516 * Do this early, we don't want the compress threads to be limited
1517 * to the same CPUs as the IO workers. So do this before we set
1518 * any potential CPU affinity
1520 if (iolog_compress_init(td, sk_out))
1524 * If we have a gettimeofday() thread, make sure we exclude that
1525 * thread from this job
1528 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1531 * Set affinity first, in case it has an impact on the memory
1534 if (fio_option_is_set(o, cpumask)) {
1535 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
1536 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
1538 log_err("fio: no CPUs set\n");
1539 log_err("fio: Try increasing number of available CPUs\n");
1540 td_verror(td, EINVAL, "cpus_split");
1544 ret = fio_setaffinity(td->pid, o->cpumask);
1546 td_verror(td, errno, "cpu_set_affinity");
1551 #ifdef CONFIG_LIBNUMA
1552 /* numa node setup */
1553 if (fio_option_is_set(o, numa_cpunodes) ||
1554 fio_option_is_set(o, numa_memnodes)) {
1555 struct bitmask *mask;
1557 if (numa_available() < 0) {
1558 td_verror(td, errno, "Does not support NUMA API\n");
1562 if (fio_option_is_set(o, numa_cpunodes)) {
1563 mask = numa_parse_nodestring(o->numa_cpunodes);
1564 ret = numa_run_on_node_mask(mask);
1565 numa_free_nodemask(mask);
1567 td_verror(td, errno, \
1568 "numa_run_on_node_mask failed\n");
1573 if (fio_option_is_set(o, numa_memnodes)) {
1575 if (o->numa_memnodes)
1576 mask = numa_parse_nodestring(o->numa_memnodes);
1578 switch (o->numa_mem_mode) {
1579 case MPOL_INTERLEAVE:
1580 numa_set_interleave_mask(mask);
1583 numa_set_membind(mask);
1586 numa_set_localalloc();
1588 case MPOL_PREFERRED:
1589 numa_set_preferred(o->numa_mem_prefer_node);
1597 numa_free_nodemask(mask);
1603 if (fio_pin_memory(td))
1607 * May alter parameters that init_io_u() will use, so we need to
1616 if (o->verify_async && verify_async_init(td))
1619 if (fio_option_is_set(o, ioprio) ||
1620 fio_option_is_set(o, ioprio_class)) {
1621 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1623 td_verror(td, errno, "ioprio_set");
1628 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1632 if (nice(o->nice) == -1 && errno != 0) {
1633 td_verror(td, errno, "nice");
1637 if (o->ioscheduler && switch_ioscheduler(td))
1640 if (!o->create_serialize && setup_files(td))
1646 if (init_random_map(td))
1649 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
1653 if (pre_read_files(td) < 0)
1657 fio_verify_init(td);
1659 if (rate_submit_init(td, sk_out))
1662 set_epoch_time(td, o->log_unix_epoch);
1663 fio_getrusage(&td->ru_start);
1664 memcpy(&td->bw_sample_time, &td->epoch, sizeof(td->epoch));
1665 memcpy(&td->iops_sample_time, &td->epoch, sizeof(td->epoch));
1666 memcpy(&td->ss.prev_time, &td->epoch, sizeof(td->epoch));
1668 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1669 o->ratemin[DDIR_TRIM]) {
1670 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1671 sizeof(td->bw_sample_time));
1672 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1673 sizeof(td->bw_sample_time));
1674 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1675 sizeof(td->bw_sample_time));
1679 while (keep_running(td)) {
1680 uint64_t verify_bytes;
1682 fio_gettime(&td->start, NULL);
1683 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1686 clear_io_state(td, 0);
1688 if (o->unlink_each_loop && unlink_all_files(td))
1692 prune_io_piece_log(td);
1694 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1695 verify_bytes = do_dry_run(td);
1697 uint64_t bytes_done[DDIR_RWDIR_CNT];
1699 do_io(td, bytes_done);
1701 if (!ddir_rw_sum(bytes_done)) {
1702 fio_mark_td_terminate(td);
1705 verify_bytes = bytes_done[DDIR_WRITE] +
1706 bytes_done[DDIR_TRIM];
1711 * If we took too long to shut down, the main thread could
1712 * already consider us reaped/exited. If that happens, break
1715 if (td->runstate >= TD_EXITED)
1721 * Make sure we've successfully updated the rusage stats
1722 * before waiting on the stat mutex. Otherwise we could have
1723 * the stat thread holding stat mutex and waiting for
1724 * the rusage_sem, which would never get upped because
1725 * this thread is waiting for the stat mutex.
1727 deadlock_loop_cnt = 0;
1729 check_update_rusage(td);
1730 if (!fio_mutex_down_trylock(stat_mutex))
1733 if (deadlock_loop_cnt++ > 5000) {
1734 log_err("fio seems to be stuck grabbing stat_mutex, forcibly exiting\n");
1735 td->error = EDEADLK;
1740 if (td_read(td) && td->io_bytes[DDIR_READ])
1741 update_runtime(td, elapsed_us, DDIR_READ);
1742 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1743 update_runtime(td, elapsed_us, DDIR_WRITE);
1744 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1745 update_runtime(td, elapsed_us, DDIR_TRIM);
1746 fio_gettime(&td->start, NULL);
1747 fio_mutex_up(stat_mutex);
1749 if (td->error || td->terminate)
1752 if (!o->do_verify ||
1753 o->verify == VERIFY_NONE ||
1754 td_ioengine_flagged(td, FIO_UNIDIR))
1757 clear_io_state(td, 0);
1759 fio_gettime(&td->start, NULL);
1761 do_verify(td, verify_bytes);
1764 * See comment further up for why this is done here.
1766 check_update_rusage(td);
1768 fio_mutex_down(stat_mutex);
1769 update_runtime(td, elapsed_us, DDIR_READ);
1770 fio_gettime(&td->start, NULL);
1771 fio_mutex_up(stat_mutex);
1773 if (td->error || td->terminate)
1777 td_set_runstate(td, TD_FINISHING);
1779 update_rusage_stat(td);
1780 td->ts.total_run_time = mtime_since_now(&td->epoch);
1781 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1782 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1783 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1785 if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) &&
1786 (td->o.verify != VERIFY_NONE && td_write(td)))
1787 verify_save_state(td->thread_number);
1789 fio_unpin_memory(td);
1791 td_writeout_logs(td, true);
1793 iolog_compress_exit(td);
1794 rate_submit_exit(td);
1796 if (o->exec_postrun)
1797 exec_string(o, o->exec_postrun, (const char *)"postrun");
1799 if (exitall_on_terminate || (o->exitall_error && td->error))
1800 fio_terminate_threads(td->groupid);
1804 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1807 if (o->verify_async)
1808 verify_async_exit(td);
1810 close_and_free_files(td);
1813 cgroup_shutdown(td, &cgroup_mnt);
1814 verify_free_state(td);
1816 if (td->zone_state_index) {
1819 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1820 free(td->zone_state_index[i]);
1821 free(td->zone_state_index);
1822 td->zone_state_index = NULL;
1825 if (fio_option_is_set(o, cpumask)) {
1826 ret = fio_cpuset_exit(&o->cpumask);
1828 td_verror(td, ret, "fio_cpuset_exit");
1832 * do this very late, it will log file closing as well
1834 if (o->write_iolog_file)
1835 write_iolog_close(td);
1837 fio_mutex_remove(td->mutex);
1840 td_set_runstate(td, TD_EXITED);
1843 * Do this last after setting our runstate to exited, so we
1844 * know that the stat thread is signaled.
1846 check_update_rusage(td);
1849 return (void *) (uintptr_t) td->error;
1852 static void dump_td_info(struct thread_data *td)
1854 log_err("fio: job '%s' (state=%d) hasn't exited in %lu seconds, it "
1855 "appears to be stuck. Doing forceful exit of this job.\n",
1856 td->o.name, td->runstate,
1857 (unsigned long) time_since_now(&td->terminate_time));
1861 * Run over the job map and reap the threads that have exited, if any.
1863 static void reap_threads(unsigned int *nr_running, uint64_t *t_rate,
1866 struct thread_data *td;
1867 unsigned int cputhreads, realthreads, pending;
1871 * reap exited threads (TD_EXITED -> TD_REAPED)
1873 realthreads = pending = cputhreads = 0;
1874 for_each_td(td, i) {
1878 * ->io_ops is NULL for a thread that has closed its
1881 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1890 if (td->runstate == TD_REAPED)
1892 if (td->o.use_thread) {
1893 if (td->runstate == TD_EXITED) {
1894 td_set_runstate(td, TD_REAPED);
1901 if (td->runstate == TD_EXITED)
1905 * check if someone quit or got killed in an unusual way
1907 ret = waitpid(td->pid, &status, flags);
1909 if (errno == ECHILD) {
1910 log_err("fio: pid=%d disappeared %d\n",
1911 (int) td->pid, td->runstate);
1913 td_set_runstate(td, TD_REAPED);
1917 } else if (ret == td->pid) {
1918 if (WIFSIGNALED(status)) {
1919 int sig = WTERMSIG(status);
1921 if (sig != SIGTERM && sig != SIGUSR2)
1922 log_err("fio: pid=%d, got signal=%d\n",
1923 (int) td->pid, sig);
1925 td_set_runstate(td, TD_REAPED);
1928 if (WIFEXITED(status)) {
1929 if (WEXITSTATUS(status) && !td->error)
1930 td->error = WEXITSTATUS(status);
1932 td_set_runstate(td, TD_REAPED);
1938 * If the job is stuck, do a forceful timeout of it and
1941 if (td->terminate &&
1942 td->runstate < TD_FSYNCING &&
1943 time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1945 td_set_runstate(td, TD_REAPED);
1950 * thread is not dead, continue
1956 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1957 (*t_rate) -= ddir_rw_sum(td->o.rate);
1964 done_secs += mtime_since_now(&td->epoch) / 1000;
1965 profile_td_exit(td);
1968 if (*nr_running == cputhreads && !pending && realthreads)
1969 fio_terminate_threads(TERMINATE_ALL);
1972 static bool __check_trigger_file(void)
1979 if (stat(trigger_file, &sb))
1982 if (unlink(trigger_file) < 0)
1983 log_err("fio: failed to unlink %s: %s\n", trigger_file,
1989 static bool trigger_timedout(void)
1991 if (trigger_timeout)
1992 return time_since_genesis() >= trigger_timeout;
1997 void exec_trigger(const char *cmd)
2006 log_err("fio: failed executing %s trigger\n", cmd);
2009 void check_trigger_file(void)
2011 if (__check_trigger_file() || trigger_timedout()) {
2013 fio_clients_send_trigger(trigger_remote_cmd);
2015 verify_save_state(IO_LIST_ALL);
2016 fio_terminate_threads(TERMINATE_ALL);
2017 exec_trigger(trigger_cmd);
2022 static int fio_verify_load_state(struct thread_data *td)
2026 if (!td->o.verify_state)
2032 ret = fio_server_get_verify_state(td->o.name,
2033 td->thread_number - 1, &data);
2035 verify_assign_state(td, data);
2037 ret = verify_load_state(td, "local");
2042 static void do_usleep(unsigned int usecs)
2044 check_for_running_stats();
2045 check_trigger_file();
2049 static bool check_mount_writes(struct thread_data *td)
2054 if (!td_write(td) || td->o.allow_mounted_write)
2057 for_each_file(td, f, i) {
2058 if (f->filetype != FIO_TYPE_BD)
2060 if (device_is_mounted(f->file_name))
2066 log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.\n", f->file_name);
2070 static bool waitee_running(struct thread_data *me)
2072 const char *waitee = me->o.wait_for;
2073 const char *self = me->o.name;
2074 struct thread_data *td;
2080 for_each_td(td, i) {
2081 if (!strcmp(td->o.name, self) || strcmp(td->o.name, waitee))
2084 if (td->runstate < TD_EXITED) {
2085 dprint(FD_PROCESS, "%s fenced by %s(%s)\n",
2087 runstate_to_name(td->runstate));
2092 dprint(FD_PROCESS, "%s: %s completed, can run\n", self, waitee);
2097 * Main function for kicking off and reaping jobs, as needed.
2099 static void run_threads(struct sk_out *sk_out)
2101 struct thread_data *td;
2102 unsigned int i, todo, nr_running, nr_started;
2103 uint64_t m_rate, t_rate;
2106 if (fio_gtod_offload && fio_start_gtod_thread())
2109 fio_idle_prof_init();
2113 nr_thread = nr_process = 0;
2114 for_each_td(td, i) {
2115 if (check_mount_writes(td))
2117 if (td->o.use_thread)
2123 if (output_format & FIO_OUTPUT_NORMAL) {
2124 log_info("Starting ");
2126 log_info("%d thread%s", nr_thread,
2127 nr_thread > 1 ? "s" : "");
2131 log_info("%d process%s", nr_process,
2132 nr_process > 1 ? "es" : "");
2138 todo = thread_number;
2141 m_rate = t_rate = 0;
2143 for_each_td(td, i) {
2144 print_status_init(td->thread_number - 1);
2146 if (!td->o.create_serialize)
2149 if (fio_verify_load_state(td))
2153 * do file setup here so it happens sequentially,
2154 * we don't want X number of threads getting their
2155 * client data interspersed on disk
2157 if (setup_files(td)) {
2161 log_err("fio: pid=%d, err=%d/%s\n",
2162 (int) td->pid, td->error, td->verror);
2163 td_set_runstate(td, TD_REAPED);
2170 * for sharing to work, each job must always open
2171 * its own files. so close them, if we opened them
2174 for_each_file(td, f, j) {
2175 if (fio_file_open(f))
2176 td_io_close_file(td, f);
2181 /* start idle threads before io threads start to run */
2182 fio_idle_prof_start();
2187 struct thread_data *map[REAL_MAX_JOBS];
2188 struct timeval this_start;
2189 int this_jobs = 0, left;
2190 struct fork_data *fd;
2193 * create threads (TD_NOT_CREATED -> TD_CREATED)
2195 for_each_td(td, i) {
2196 if (td->runstate != TD_NOT_CREATED)
2200 * never got a chance to start, killed by other
2201 * thread for some reason
2203 if (td->terminate) {
2208 if (td->o.start_delay) {
2209 spent = utime_since_genesis();
2211 if (td->o.start_delay > spent)
2215 if (td->o.stonewall && (nr_started || nr_running)) {
2216 dprint(FD_PROCESS, "%s: stonewall wait\n",
2221 if (waitee_running(td)) {
2222 dprint(FD_PROCESS, "%s: waiting for %s\n",
2223 td->o.name, td->o.wait_for);
2229 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
2230 td->update_rusage = 0;
2233 * Set state to created. Thread will transition
2234 * to TD_INITIALIZED when it's done setting up.
2236 td_set_runstate(td, TD_CREATED);
2237 map[this_jobs++] = td;
2240 fd = calloc(1, sizeof(*fd));
2242 fd->sk_out = sk_out;
2244 if (td->o.use_thread) {
2247 dprint(FD_PROCESS, "will pthread_create\n");
2248 ret = pthread_create(&td->thread, NULL,
2251 log_err("pthread_create: %s\n",
2257 ret = pthread_detach(td->thread);
2259 log_err("pthread_detach: %s",
2263 dprint(FD_PROCESS, "will fork\n");
2268 ret = (int)(uintptr_t)thread_main(fd);
2270 } else if (i == fio_debug_jobno)
2271 *fio_debug_jobp = pid;
2273 dprint(FD_MUTEX, "wait on startup_mutex\n");
2274 if (fio_mutex_down_timeout(startup_mutex, 10000)) {
2275 log_err("fio: job startup hung? exiting.\n");
2276 fio_terminate_threads(TERMINATE_ALL);
2281 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2285 * Wait for the started threads to transition to
2288 fio_gettime(&this_start, NULL);
2290 while (left && !fio_abort) {
2291 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2296 for (i = 0; i < this_jobs; i++) {
2300 if (td->runstate == TD_INITIALIZED) {
2303 } else if (td->runstate >= TD_EXITED) {
2307 nr_running++; /* work-around... */
2313 log_err("fio: %d job%s failed to start\n", left,
2314 left > 1 ? "s" : "");
2315 for (i = 0; i < this_jobs; i++) {
2319 kill(td->pid, SIGTERM);
2325 * start created threads (TD_INITIALIZED -> TD_RUNNING).
2327 for_each_td(td, i) {
2328 if (td->runstate != TD_INITIALIZED)
2331 if (in_ramp_time(td))
2332 td_set_runstate(td, TD_RAMP);
2334 td_set_runstate(td, TD_RUNNING);
2337 m_rate += ddir_rw_sum(td->o.ratemin);
2338 t_rate += ddir_rw_sum(td->o.rate);
2340 fio_mutex_up(td->mutex);
2343 reap_threads(&nr_running, &t_rate, &m_rate);
2349 while (nr_running) {
2350 reap_threads(&nr_running, &t_rate, &m_rate);
2354 fio_idle_prof_stop();
2359 static void free_disk_util(void)
2361 disk_util_prune_entries();
2362 helper_thread_destroy();
2365 int fio_backend(struct sk_out *sk_out)
2367 struct thread_data *td;
2371 if (load_profile(exec_profile))
2374 exec_profile = NULL;
2380 struct log_params p = {
2381 .log_type = IO_LOG_TYPE_BW,
2384 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2385 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2386 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
2389 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2390 if (startup_mutex == NULL)
2395 helper_thread_create(startup_mutex, sk_out);
2397 cgroup_list = smalloc(sizeof(*cgroup_list));
2398 INIT_FLIST_HEAD(cgroup_list);
2400 run_threads(sk_out);
2402 helper_thread_exit();
2407 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2408 struct io_log *log = agg_io_log[i];
2410 flush_log(log, false);
2416 for_each_td(td, i) {
2418 if (td->ss.iops_data != NULL) {
2419 free(td->ss.iops_data);
2420 free(td->ss.bw_data);
2423 fio_options_free(td);
2424 if (td->rusage_sem) {
2425 fio_mutex_remove(td->rusage_sem);
2426 td->rusage_sem = NULL;
2431 cgroup_kill(cgroup_list);
2435 fio_mutex_remove(startup_mutex);