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
40 #ifndef FIO_NO_HAVE_SHM_H
53 #include "lib/getrusage.h"
57 #include "workqueue.h"
58 #include "lib/mountcheck.h"
60 static pthread_t helper_thread;
61 static pthread_mutex_t helper_lock;
62 pthread_cond_t helper_cond;
63 int helper_do_stat = 0;
65 static struct fio_mutex *startup_mutex;
66 static struct flist_head *cgroup_list;
67 static char *cgroup_mnt;
68 static int exit_value;
69 static volatile int fio_abort;
70 static unsigned int nr_process = 0;
71 static unsigned int nr_thread = 0;
73 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
76 unsigned int thread_number = 0;
77 unsigned int stat_number = 0;
80 unsigned long done_secs = 0;
81 volatile int helper_exit = 0;
83 #define PAGE_ALIGN(buf) \
84 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
86 #define JOB_START_TIMEOUT (5 * 1000)
88 static void sig_int(int sig)
92 fio_server_got_signal(sig);
94 log_info("\nfio: terminating on signal %d\n", sig);
99 fio_terminate_threads(TERMINATE_ALL);
103 static void sig_show_status(int sig)
105 show_running_run_stats();
108 static void set_sig_handlers(void)
110 struct sigaction act;
112 memset(&act, 0, sizeof(act));
113 act.sa_handler = sig_int;
114 act.sa_flags = SA_RESTART;
115 sigaction(SIGINT, &act, NULL);
117 memset(&act, 0, sizeof(act));
118 act.sa_handler = sig_int;
119 act.sa_flags = SA_RESTART;
120 sigaction(SIGTERM, &act, NULL);
122 /* Windows uses SIGBREAK as a quit signal from other applications */
124 memset(&act, 0, sizeof(act));
125 act.sa_handler = sig_int;
126 act.sa_flags = SA_RESTART;
127 sigaction(SIGBREAK, &act, NULL);
130 memset(&act, 0, sizeof(act));
131 act.sa_handler = sig_show_status;
132 act.sa_flags = SA_RESTART;
133 sigaction(SIGUSR1, &act, NULL);
136 memset(&act, 0, sizeof(act));
137 act.sa_handler = sig_int;
138 act.sa_flags = SA_RESTART;
139 sigaction(SIGPIPE, &act, NULL);
144 * Check if we are above the minimum rate given.
146 static int __check_min_rate(struct thread_data *td, struct timeval *now,
149 unsigned long long bytes = 0;
150 unsigned long iops = 0;
153 unsigned int ratemin = 0;
154 unsigned int rate_iops = 0;
155 unsigned int rate_iops_min = 0;
157 assert(ddir_rw(ddir));
159 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
163 * allow a 2 second settle period in the beginning
165 if (mtime_since(&td->start, now) < 2000)
168 iops += td->this_io_blocks[ddir];
169 bytes += td->this_io_bytes[ddir];
170 ratemin += td->o.ratemin[ddir];
171 rate_iops += td->o.rate_iops[ddir];
172 rate_iops_min += td->o.rate_iops_min[ddir];
175 * if rate blocks is set, sample is running
177 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
178 spent = mtime_since(&td->lastrate[ddir], now);
179 if (spent < td->o.ratecycle)
182 if (td->o.rate[ddir]) {
184 * check bandwidth specified rate
186 if (bytes < td->rate_bytes[ddir]) {
187 log_err("%s: min rate %u not met\n", td->o.name,
192 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
196 if (rate < ratemin ||
197 bytes < td->rate_bytes[ddir]) {
198 log_err("%s: min rate %u not met, got"
199 " %luKB/sec\n", td->o.name,
206 * checks iops specified rate
208 if (iops < rate_iops) {
209 log_err("%s: min iops rate %u not met\n",
210 td->o.name, rate_iops);
214 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
218 if (rate < rate_iops_min ||
219 iops < td->rate_blocks[ddir]) {
220 log_err("%s: min iops rate %u not met,"
221 " got %lu\n", td->o.name,
222 rate_iops_min, rate);
228 td->rate_bytes[ddir] = bytes;
229 td->rate_blocks[ddir] = iops;
230 memcpy(&td->lastrate[ddir], now, sizeof(*now));
234 static int check_min_rate(struct thread_data *td, struct timeval *now)
238 if (td->bytes_done[DDIR_READ])
239 ret |= __check_min_rate(td, now, DDIR_READ);
240 if (td->bytes_done[DDIR_WRITE])
241 ret |= __check_min_rate(td, now, DDIR_WRITE);
242 if (td->bytes_done[DDIR_TRIM])
243 ret |= __check_min_rate(td, now, DDIR_TRIM);
249 * When job exits, we can cancel the in-flight IO if we are using async
250 * io. Attempt to do so.
252 static void cleanup_pending_aio(struct thread_data *td)
257 * get immediately available events, if any
259 r = io_u_queued_complete(td, 0);
264 * now cancel remaining active events
266 if (td->io_ops->cancel) {
270 io_u_qiter(&td->io_u_all, io_u, i) {
271 if (io_u->flags & IO_U_F_FLIGHT) {
272 r = td->io_ops->cancel(td, io_u);
280 r = io_u_queued_complete(td, td->cur_depth);
284 * Helper to handle the final sync of a file. Works just like the normal
285 * io path, just does everything sync.
287 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
289 struct io_u *io_u = __get_io_u(td);
295 io_u->ddir = DDIR_SYNC;
298 if (td_io_prep(td, io_u)) {
304 ret = td_io_queue(td, io_u);
306 td_verror(td, io_u->error, "td_io_queue");
309 } else if (ret == FIO_Q_QUEUED) {
310 if (io_u_queued_complete(td, 1) < 0)
312 } else if (ret == FIO_Q_COMPLETED) {
314 td_verror(td, io_u->error, "td_io_queue");
318 if (io_u_sync_complete(td, io_u) < 0)
320 } else if (ret == FIO_Q_BUSY) {
321 if (td_io_commit(td))
329 static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
333 if (fio_file_open(f))
334 return fio_io_sync(td, f);
336 if (td_io_open_file(td, f))
339 ret = fio_io_sync(td, f);
340 td_io_close_file(td, f);
344 static inline void __update_tv_cache(struct thread_data *td)
346 fio_gettime(&td->tv_cache, NULL);
349 static inline void update_tv_cache(struct thread_data *td)
351 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
352 __update_tv_cache(td);
355 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
357 if (in_ramp_time(td))
361 if (utime_since(&td->epoch, t) >= td->o.timeout)
368 * We need to update the runtime consistently in ms, but keep a running
369 * tally of the current elapsed time in microseconds for sub millisecond
372 static inline void update_runtime(struct thread_data *td,
373 unsigned long long *elapsed_us,
374 const enum fio_ddir ddir)
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 int 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);
443 * if the queue is full, we MUST reap at least 1 event
445 min_evts = min(td->o.iodepth_batch_complete, td->cur_depth);
446 if ((full && !min_evts) || !td->o.iodepth_batch_complete)
449 if (time && (__should_check_rate(td, DDIR_READ) ||
450 __should_check_rate(td, DDIR_WRITE) ||
451 __should_check_rate(td, DDIR_TRIM)))
452 fio_gettime(time, NULL);
455 ret = io_u_queued_complete(td, min_evts);
458 } while (full && (td->cur_depth > td->o.iodepth_low));
463 int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
464 enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
465 struct timeval *comp_time)
470 case FIO_Q_COMPLETED:
473 clear_io_u(td, io_u);
474 } else if (io_u->resid) {
475 int bytes = io_u->xfer_buflen - io_u->resid;
476 struct fio_file *f = io_u->file;
479 *bytes_issued += bytes;
482 trim_io_piece(td, io_u);
489 unlog_io_piece(td, io_u);
490 td_verror(td, EIO, "full resid");
495 io_u->xfer_buflen = io_u->resid;
496 io_u->xfer_buf += bytes;
497 io_u->offset += bytes;
499 if (ddir_rw(io_u->ddir))
500 td->ts.short_io_u[io_u->ddir]++;
503 if (io_u->offset == f->real_file_size)
506 requeue_io_u(td, &io_u);
509 if (comp_time && (__should_check_rate(td, DDIR_READ) ||
510 __should_check_rate(td, DDIR_WRITE) ||
511 __should_check_rate(td, DDIR_TRIM)))
512 fio_gettime(comp_time, NULL);
514 *ret = io_u_sync_complete(td, io_u);
521 * if the engine doesn't have a commit hook,
522 * the io_u is really queued. if it does have such
523 * a hook, it has to call io_u_queued() itself.
525 if (td->io_ops->commit == NULL)
526 io_u_queued(td, io_u);
528 *bytes_issued += io_u->xfer_buflen;
532 unlog_io_piece(td, io_u);
533 requeue_io_u(td, &io_u);
534 ret2 = td_io_commit(td);
540 td_verror(td, -(*ret), "td_io_queue");
544 if (break_on_this_error(td, ddir, ret))
551 * The main verify engine. Runs over the writes we previously submitted,
552 * reads the blocks back in, and checks the crc/md5 of the data.
554 static void do_verify(struct thread_data *td, uint64_t verify_bytes)
561 dprint(FD_VERIFY, "starting loop\n");
564 * sync io first and invalidate cache, to make sure we really
567 for_each_file(td, f, i) {
568 if (!fio_file_open(f))
570 if (fio_io_sync(td, f))
572 if (file_invalidate_cache(td, f))
576 check_update_rusage(td);
581 td_set_runstate(td, TD_VERIFYING);
584 while (!td->terminate) {
589 check_update_rusage(td);
591 if (runtime_exceeded(td, &td->tv_cache)) {
592 __update_tv_cache(td);
593 if (runtime_exceeded(td, &td->tv_cache)) {
594 fio_mark_td_terminate(td);
599 if (flow_threshold_exceeded(td))
602 if (!td->o.experimental_verify) {
603 io_u = __get_io_u(td);
607 if (get_next_verify(td, io_u)) {
612 if (td_io_prep(td, io_u)) {
617 if (ddir_rw_sum(td->bytes_done) + td->o.rw_min_bs > verify_bytes)
620 while ((io_u = get_io_u(td)) != NULL) {
628 * We are only interested in the places where
629 * we wrote or trimmed IOs. Turn those into
630 * reads for verification purposes.
632 if (io_u->ddir == DDIR_READ) {
634 * Pretend we issued it for rwmix
637 td->io_issues[DDIR_READ]++;
640 } else if (io_u->ddir == DDIR_TRIM) {
641 io_u->ddir = DDIR_READ;
642 io_u_set(io_u, IO_U_F_TRIMMED);
644 } else if (io_u->ddir == DDIR_WRITE) {
645 io_u->ddir = DDIR_READ;
657 if (verify_state_should_stop(td, io_u)) {
662 if (td->o.verify_async)
663 io_u->end_io = verify_io_u_async;
665 io_u->end_io = verify_io_u;
668 if (!td->o.disable_slat)
669 fio_gettime(&io_u->start_time, NULL);
671 ret = td_io_queue(td, io_u);
673 if (io_queue_event(td, io_u, &ret, ddir, NULL, 1, NULL))
677 * if we can queue more, do so. but check if there are
678 * completed io_u's first. Note that we can get BUSY even
679 * without IO queued, if the system is resource starved.
682 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
683 if (full || !td->o.iodepth_batch_complete)
684 ret = wait_for_completions(td, NULL);
690 check_update_rusage(td);
693 min_events = td->cur_depth;
696 ret = io_u_queued_complete(td, min_events);
698 cleanup_pending_aio(td);
700 td_set_runstate(td, TD_RUNNING);
702 dprint(FD_VERIFY, "exiting loop\n");
705 static unsigned int exceeds_number_ios(struct thread_data *td)
707 unsigned long long number_ios;
709 if (!td->o.number_ios)
712 number_ios = ddir_rw_sum(td->io_blocks);
713 number_ios += td->io_u_queued + td->io_u_in_flight;
715 return number_ios >= (td->o.number_ios * td->loops);
718 static int io_issue_bytes_exceeded(struct thread_data *td)
720 unsigned long long bytes, limit;
723 bytes = td->io_issue_bytes[DDIR_READ] + td->io_issue_bytes[DDIR_WRITE];
724 else if (td_write(td))
725 bytes = td->io_issue_bytes[DDIR_WRITE];
726 else if (td_read(td))
727 bytes = td->io_issue_bytes[DDIR_READ];
729 bytes = td->io_issue_bytes[DDIR_TRIM];
732 limit = td->o.io_limit;
737 return bytes >= limit || exceeds_number_ios(td);
740 static int io_complete_bytes_exceeded(struct thread_data *td)
742 unsigned long long bytes, limit;
745 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
746 else if (td_write(td))
747 bytes = td->this_io_bytes[DDIR_WRITE];
748 else if (td_read(td))
749 bytes = td->this_io_bytes[DDIR_READ];
751 bytes = td->this_io_bytes[DDIR_TRIM];
754 limit = td->o.io_limit;
759 return bytes >= limit || exceeds_number_ios(td);
763 * Main IO worker function. It retrieves io_u's to process and queues
764 * and reaps them, checking for rate and errors along the way.
766 * Returns number of bytes written and trimmed.
768 static uint64_t do_io(struct thread_data *td)
772 uint64_t total_bytes, bytes_issued = 0;
774 if (in_ramp_time(td))
775 td_set_runstate(td, TD_RAMP);
777 td_set_runstate(td, TD_RUNNING);
781 total_bytes = td->o.size;
783 * Allow random overwrite workloads to write up to io_limit
784 * before starting verification phase as 'size' doesn't apply.
786 if (td_write(td) && td_random(td) && td->o.norandommap)
787 total_bytes = max(total_bytes, (uint64_t) td->o.io_limit);
789 * If verify_backlog is enabled, we'll run the verify in this
790 * handler as well. For that case, we may need up to twice the
793 if (td->o.verify != VERIFY_NONE &&
794 (td_write(td) && td->o.verify_backlog))
795 total_bytes += td->o.size;
797 /* In trimwrite mode, each byte is trimmed and then written, so
798 * allow total_bytes to be twice as big */
799 if (td_trimwrite(td))
800 total_bytes += td->total_io_size;
802 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
803 (!flist_empty(&td->trim_list)) || !io_issue_bytes_exceeded(td) ||
805 struct timeval comp_time;
810 check_update_rusage(td);
812 if (td->terminate || td->done)
817 if (runtime_exceeded(td, &td->tv_cache)) {
818 __update_tv_cache(td);
819 if (runtime_exceeded(td, &td->tv_cache)) {
820 fio_mark_td_terminate(td);
825 if (flow_threshold_exceeded(td))
828 if (bytes_issued >= total_bytes)
832 if (IS_ERR_OR_NULL(io_u)) {
833 int err = PTR_ERR(io_u);
840 if (td->o.latency_target)
848 * Add verification end_io handler if:
849 * - Asked to verify (!td_rw(td))
850 * - Or the io_u is from our verify list (mixed write/ver)
852 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
853 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
855 if (!td->o.verify_pattern_bytes) {
856 io_u->rand_seed = __rand(&td->verify_state);
857 if (sizeof(int) != sizeof(long *))
858 io_u->rand_seed *= __rand(&td->verify_state);
861 if (verify_state_should_stop(td, io_u)) {
866 if (td->o.verify_async)
867 io_u->end_io = verify_io_u_async;
869 io_u->end_io = verify_io_u;
870 td_set_runstate(td, TD_VERIFYING);
871 } else if (in_ramp_time(td))
872 td_set_runstate(td, TD_RAMP);
874 td_set_runstate(td, TD_RUNNING);
877 * Always log IO before it's issued, so we know the specific
878 * order of it. The logged unit will track when the IO has
881 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
883 td->o.verify != VERIFY_NONE &&
884 !td->o.experimental_verify)
885 log_io_piece(td, io_u);
887 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
890 ret = workqueue_enqueue(&td->io_wq, io_u);
892 ret = td_io_queue(td, io_u);
894 if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 1, &comp_time))
898 * See if we need to complete some commands. Note that
899 * we can get BUSY even without IO queued, if the
900 * system is resource starved.
903 full = queue_full(td) ||
904 (ret == FIO_Q_BUSY && td->cur_depth);
905 if (full || !td->o.iodepth_batch_complete)
906 ret = wait_for_completions(td, &comp_time);
910 if (!ddir_rw_sum(td->bytes_done) &&
911 !(td->io_ops->flags & FIO_NOIO))
914 if (!in_ramp_time(td) && should_check_rate(td)) {
915 if (check_min_rate(td, &comp_time)) {
916 if (exitall_on_terminate)
917 fio_terminate_threads(td->groupid);
918 td_verror(td, EIO, "check_min_rate");
922 if (!in_ramp_time(td) && td->o.latency_target)
923 lat_target_check(td);
925 if (td->o.thinktime) {
926 unsigned long long b;
928 b = ddir_rw_sum(td->io_blocks);
929 if (!(b % td->o.thinktime_blocks)) {
934 if (td->o.thinktime_spin)
935 usec_spin(td->o.thinktime_spin);
937 left = td->o.thinktime - td->o.thinktime_spin;
939 usec_sleep(td, left);
944 check_update_rusage(td);
946 if (td->trim_entries)
947 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
949 if (td->o.fill_device && td->error == ENOSPC) {
951 fio_mark_td_terminate(td);
956 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
957 workqueue_flush(&td->io_wq);
963 ret = io_u_queued_complete(td, i);
964 if (td->o.fill_device && td->error == ENOSPC)
968 if (should_fsync(td) && td->o.end_fsync) {
969 td_set_runstate(td, TD_FSYNCING);
971 for_each_file(td, f, i) {
972 if (!fio_file_fsync(td, f))
975 log_err("fio: end_fsync failed for file %s\n",
980 cleanup_pending_aio(td);
983 * stop job if we failed doing any IO
985 if (!ddir_rw_sum(td->this_io_bytes))
988 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
991 static void cleanup_io_u(struct thread_data *td)
995 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
997 if (td->io_ops->io_u_free)
998 td->io_ops->io_u_free(td, io_u);
1000 fio_memfree(io_u, sizeof(*io_u));
1005 io_u_rexit(&td->io_u_requeues);
1006 io_u_qexit(&td->io_u_freelist);
1007 io_u_qexit(&td->io_u_all);
1009 if (td->last_write_comp)
1010 sfree(td->last_write_comp);
1013 static int init_io_u(struct thread_data *td)
1016 unsigned int max_bs, min_write;
1017 int cl_align, i, max_units;
1018 int data_xfer = 1, err;
1021 max_units = td->o.iodepth;
1022 max_bs = td_max_bs(td);
1023 min_write = td->o.min_bs[DDIR_WRITE];
1024 td->orig_buffer_size = (unsigned long long) max_bs
1025 * (unsigned long long) max_units;
1027 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
1031 err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
1032 err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
1033 err += io_u_qinit(&td->io_u_all, td->o.iodepth);
1036 log_err("fio: failed setting up IO queues\n");
1041 * if we may later need to do address alignment, then add any
1042 * possible adjustment here so that we don't cause a buffer
1043 * overflow later. this adjustment may be too much if we get
1044 * lucky and the allocator gives us an aligned address.
1046 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1047 (td->io_ops->flags & FIO_RAWIO))
1048 td->orig_buffer_size += page_mask + td->o.mem_align;
1050 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
1053 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
1054 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
1057 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
1058 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
1062 if (data_xfer && allocate_io_mem(td))
1065 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1066 (td->io_ops->flags & FIO_RAWIO))
1067 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
1069 p = td->orig_buffer;
1071 cl_align = os_cache_line_size();
1073 for (i = 0; i < max_units; i++) {
1079 ptr = fio_memalign(cl_align, sizeof(*io_u));
1081 log_err("fio: unable to allocate aligned memory\n");
1086 memset(io_u, 0, sizeof(*io_u));
1087 INIT_FLIST_HEAD(&io_u->verify_list);
1088 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1092 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
1095 io_u_fill_buffer(td, io_u, min_write, max_bs);
1096 if (td_write(td) && td->o.verify_pattern_bytes) {
1098 * Fill the buffer with the pattern if we are
1099 * going to be doing writes.
1101 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
1106 io_u->flags = IO_U_F_FREE;
1107 io_u_qpush(&td->io_u_freelist, io_u);
1110 * io_u never leaves this stack, used for iteration of all
1113 io_u_qpush(&td->io_u_all, io_u);
1115 if (td->io_ops->io_u_init) {
1116 int ret = td->io_ops->io_u_init(td, io_u);
1119 log_err("fio: failed to init engine data: %d\n", ret);
1127 if (td->o.verify != VERIFY_NONE) {
1128 td->last_write_comp = scalloc(max_units, sizeof(uint64_t));
1129 if (!td->last_write_comp) {
1130 log_err("fio: failed to alloc write comp data\n");
1138 static int switch_ioscheduler(struct thread_data *td)
1140 char tmp[256], tmp2[128];
1144 if (td->io_ops->flags & FIO_DISKLESSIO)
1147 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1149 f = fopen(tmp, "r+");
1151 if (errno == ENOENT) {
1152 log_err("fio: os or kernel doesn't support IO scheduler"
1156 td_verror(td, errno, "fopen iosched");
1163 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
1164 if (ferror(f) || ret != 1) {
1165 td_verror(td, errno, "fwrite");
1173 * Read back and check that the selected scheduler is now the default.
1175 ret = fread(tmp, sizeof(tmp), 1, f);
1176 if (ferror(f) || ret < 0) {
1177 td_verror(td, errno, "fread");
1181 tmp[sizeof(tmp) - 1] = '\0';
1184 sprintf(tmp2, "[%s]", td->o.ioscheduler);
1185 if (!strstr(tmp, tmp2)) {
1186 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1187 td_verror(td, EINVAL, "iosched_switch");
1196 static int keep_running(struct thread_data *td)
1198 unsigned long long limit;
1202 if (td->o.time_based)
1208 if (exceeds_number_ios(td))
1212 limit = td->o.io_limit;
1216 if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
1220 * If the difference is less than the minimum IO size, we
1223 diff = limit - ddir_rw_sum(td->io_bytes);
1224 if (diff < td_max_bs(td))
1227 if (fio_files_done(td))
1236 static int exec_string(struct thread_options *o, const char *string, const char *mode)
1238 int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
1241 str = malloc(newlen);
1242 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
1244 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
1247 log_err("fio: exec of cmd <%s> failed\n", str);
1254 * Dry run to compute correct state of numberio for verification.
1256 static uint64_t do_dry_run(struct thread_data *td)
1258 td_set_runstate(td, TD_RUNNING);
1260 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1261 (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) {
1265 if (td->terminate || td->done)
1268 io_u = get_io_u(td);
1272 io_u_set(io_u, IO_U_F_FLIGHT);
1275 if (ddir_rw(acct_ddir(io_u)))
1276 td->io_issues[acct_ddir(io_u)]++;
1277 if (ddir_rw(io_u->ddir)) {
1278 io_u_mark_depth(td, 1);
1279 td->ts.total_io_u[io_u->ddir]++;
1282 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1284 td->o.verify != VERIFY_NONE &&
1285 !td->o.experimental_verify)
1286 log_io_piece(td, io_u);
1288 ret = io_u_sync_complete(td, io_u);
1292 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
1295 static void io_workqueue_fn(struct thread_data *td, struct io_u *io_u)
1297 const enum fio_ddir ddir = io_u->ddir;
1300 dprint(FD_RATE, "io_u %p queued by %u\n", io_u, gettid());
1302 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1306 ret = td_io_queue(td, io_u);
1308 dprint(FD_RATE, "io_u %p ret %d by %u\n", io_u, ret, gettid());
1310 io_queue_event(td, io_u, &ret, ddir, NULL, 0, NULL);
1312 if (ret == FIO_Q_QUEUED)
1313 ret = io_u_queued_complete(td, 1);
1319 * Entry point for the thread based jobs. The process based jobs end up
1320 * here as well, after a little setup.
1322 static void *thread_main(void *data)
1324 unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, };
1325 struct thread_data *td = data;
1326 struct thread_options *o = &td->o;
1327 pthread_condattr_t attr;
1331 if (!o->use_thread) {
1337 fio_local_clock_init(o->use_thread);
1339 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1342 fio_server_send_start(td);
1344 INIT_FLIST_HEAD(&td->io_log_list);
1345 INIT_FLIST_HEAD(&td->io_hist_list);
1346 INIT_FLIST_HEAD(&td->verify_list);
1347 INIT_FLIST_HEAD(&td->trim_list);
1348 INIT_FLIST_HEAD(&td->next_rand_list);
1349 pthread_mutex_init(&td->io_u_lock, NULL);
1350 td->io_hist_tree = RB_ROOT;
1352 pthread_condattr_init(&attr);
1353 pthread_cond_init(&td->verify_cond, &attr);
1354 pthread_cond_init(&td->free_cond, &attr);
1356 td_set_runstate(td, TD_INITIALIZED);
1357 dprint(FD_MUTEX, "up startup_mutex\n");
1358 fio_mutex_up(startup_mutex);
1359 dprint(FD_MUTEX, "wait on td->mutex\n");
1360 fio_mutex_down(td->mutex);
1361 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1364 * A new gid requires privilege, so we need to do this before setting
1367 if (o->gid != -1U && setgid(o->gid)) {
1368 td_verror(td, errno, "setgid");
1371 if (o->uid != -1U && setuid(o->uid)) {
1372 td_verror(td, errno, "setuid");
1377 * If we have a gettimeofday() thread, make sure we exclude that
1378 * thread from this job
1381 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1384 * Set affinity first, in case it has an impact on the memory
1387 if (fio_option_is_set(o, cpumask)) {
1388 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
1389 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
1391 log_err("fio: no CPUs set\n");
1392 log_err("fio: Try increasing number of available CPUs\n");
1393 td_verror(td, EINVAL, "cpus_split");
1397 ret = fio_setaffinity(td->pid, o->cpumask);
1399 td_verror(td, errno, "cpu_set_affinity");
1404 #ifdef CONFIG_LIBNUMA
1405 /* numa node setup */
1406 if (fio_option_is_set(o, numa_cpunodes) ||
1407 fio_option_is_set(o, numa_memnodes)) {
1408 struct bitmask *mask;
1410 if (numa_available() < 0) {
1411 td_verror(td, errno, "Does not support NUMA API\n");
1415 if (fio_option_is_set(o, numa_cpunodes)) {
1416 mask = numa_parse_nodestring(o->numa_cpunodes);
1417 ret = numa_run_on_node_mask(mask);
1418 numa_free_nodemask(mask);
1420 td_verror(td, errno, \
1421 "numa_run_on_node_mask failed\n");
1426 if (fio_option_is_set(o, numa_memnodes)) {
1428 if (o->numa_memnodes)
1429 mask = numa_parse_nodestring(o->numa_memnodes);
1431 switch (o->numa_mem_mode) {
1432 case MPOL_INTERLEAVE:
1433 numa_set_interleave_mask(mask);
1436 numa_set_membind(mask);
1439 numa_set_localalloc();
1441 case MPOL_PREFERRED:
1442 numa_set_preferred(o->numa_mem_prefer_node);
1450 numa_free_nodemask(mask);
1456 if (fio_pin_memory(td))
1460 * May alter parameters that init_io_u() will use, so we need to
1469 if (o->verify_async && verify_async_init(td))
1472 if (fio_option_is_set(o, ioprio) ||
1473 fio_option_is_set(o, ioprio_class)) {
1474 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1476 td_verror(td, errno, "ioprio_set");
1481 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1485 if (nice(o->nice) == -1 && errno != 0) {
1486 td_verror(td, errno, "nice");
1490 if (o->ioscheduler && switch_ioscheduler(td))
1493 if (!o->create_serialize && setup_files(td))
1499 if (init_random_map(td))
1502 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
1506 if (pre_read_files(td) < 0)
1510 if (td->flags & TD_F_COMPRESS_LOG)
1511 tp_init(&td->tp_data);
1513 fio_verify_init(td);
1515 if ((o->io_submit_mode == IO_MODE_OFFLOAD) &&
1516 workqueue_init(td, &td->io_wq, io_workqueue_fn, td->o.iodepth))
1519 fio_gettime(&td->epoch, NULL);
1520 fio_getrusage(&td->ru_start);
1522 while (keep_running(td)) {
1523 uint64_t verify_bytes;
1525 fio_gettime(&td->start, NULL);
1526 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1527 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1528 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1530 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1531 o->ratemin[DDIR_TRIM]) {
1532 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1533 sizeof(td->bw_sample_time));
1534 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1535 sizeof(td->bw_sample_time));
1536 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1537 sizeof(td->bw_sample_time));
1543 prune_io_piece_log(td);
1545 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1546 verify_bytes = do_dry_run(td);
1548 verify_bytes = do_io(td);
1553 * Make sure we've successfully updated the rusage stats
1554 * before waiting on the stat mutex. Otherwise we could have
1555 * the stat thread holding stat mutex and waiting for
1556 * the rusage_sem, which would never get upped because
1557 * this thread is waiting for the stat mutex.
1559 check_update_rusage(td);
1561 fio_mutex_down(stat_mutex);
1562 if (td_read(td) && td->io_bytes[DDIR_READ])
1563 update_runtime(td, elapsed_us, DDIR_READ);
1564 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1565 update_runtime(td, elapsed_us, DDIR_WRITE);
1566 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1567 update_runtime(td, elapsed_us, DDIR_TRIM);
1568 fio_gettime(&td->start, NULL);
1569 fio_mutex_up(stat_mutex);
1571 if (td->error || td->terminate)
1574 if (!o->do_verify ||
1575 o->verify == VERIFY_NONE ||
1576 (td->io_ops->flags & FIO_UNIDIR))
1581 fio_gettime(&td->start, NULL);
1583 do_verify(td, verify_bytes);
1586 * See comment further up for why this is done here.
1588 check_update_rusage(td);
1590 fio_mutex_down(stat_mutex);
1591 update_runtime(td, elapsed_us, DDIR_READ);
1592 fio_gettime(&td->start, NULL);
1593 fio_mutex_up(stat_mutex);
1595 if (td->error || td->terminate)
1599 update_rusage_stat(td);
1600 td->ts.total_run_time = mtime_since_now(&td->epoch);
1601 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1602 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1603 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1605 if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) &&
1606 (td->o.verify != VERIFY_NONE && td_write(td))) {
1607 struct all_io_list *state;
1610 state = get_all_io_list(td->thread_number, &sz);
1612 __verify_save_state(state, "local");
1617 fio_unpin_memory(td);
1619 fio_writeout_logs(td);
1621 if (o->io_submit_mode == IO_MODE_OFFLOAD)
1622 workqueue_exit(&td->io_wq);
1624 if (td->flags & TD_F_COMPRESS_LOG)
1625 tp_exit(&td->tp_data);
1627 if (o->exec_postrun)
1628 exec_string(o, o->exec_postrun, (const char *)"postrun");
1630 if (exitall_on_terminate)
1631 fio_terminate_threads(td->groupid);
1635 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1638 if (o->verify_async)
1639 verify_async_exit(td);
1641 close_and_free_files(td);
1644 cgroup_shutdown(td, &cgroup_mnt);
1645 verify_free_state(td);
1647 if (fio_option_is_set(o, cpumask)) {
1648 ret = fio_cpuset_exit(&o->cpumask);
1650 td_verror(td, ret, "fio_cpuset_exit");
1654 * do this very late, it will log file closing as well
1656 if (o->write_iolog_file)
1657 write_iolog_close(td);
1659 fio_mutex_remove(td->mutex);
1662 td_set_runstate(td, TD_EXITED);
1665 * Do this last after setting our runstate to exited, so we
1666 * know that the stat thread is signaled.
1668 check_update_rusage(td);
1670 return (void *) (uintptr_t) td->error;
1675 * We cannot pass the td data into a forked process, so attach the td and
1676 * pass it to the thread worker.
1678 static int fork_main(int shmid, int offset)
1680 struct thread_data *td;
1683 #if !defined(__hpux) && !defined(CONFIG_NO_SHM)
1684 data = shmat(shmid, NULL, 0);
1685 if (data == (void *) -1) {
1693 * HP-UX inherits shm mappings?
1698 td = data + offset * sizeof(struct thread_data);
1699 ret = thread_main(td);
1701 return (int) (uintptr_t) ret;
1704 static void dump_td_info(struct thread_data *td)
1706 log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to "
1707 "be stuck. Doing forceful exit of this job.\n", td->o.name,
1708 (unsigned long) time_since_now(&td->terminate_time));
1712 * Run over the job map and reap the threads that have exited, if any.
1714 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1715 unsigned int *m_rate)
1717 struct thread_data *td;
1718 unsigned int cputhreads, realthreads, pending;
1722 * reap exited threads (TD_EXITED -> TD_REAPED)
1724 realthreads = pending = cputhreads = 0;
1725 for_each_td(td, i) {
1729 * ->io_ops is NULL for a thread that has closed its
1732 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1741 if (td->runstate == TD_REAPED)
1743 if (td->o.use_thread) {
1744 if (td->runstate == TD_EXITED) {
1745 td_set_runstate(td, TD_REAPED);
1752 if (td->runstate == TD_EXITED)
1756 * check if someone quit or got killed in an unusual way
1758 ret = waitpid(td->pid, &status, flags);
1760 if (errno == ECHILD) {
1761 log_err("fio: pid=%d disappeared %d\n",
1762 (int) td->pid, td->runstate);
1764 td_set_runstate(td, TD_REAPED);
1768 } else if (ret == td->pid) {
1769 if (WIFSIGNALED(status)) {
1770 int sig = WTERMSIG(status);
1772 if (sig != SIGTERM && sig != SIGUSR2)
1773 log_err("fio: pid=%d, got signal=%d\n",
1774 (int) td->pid, sig);
1776 td_set_runstate(td, TD_REAPED);
1779 if (WIFEXITED(status)) {
1780 if (WEXITSTATUS(status) && !td->error)
1781 td->error = WEXITSTATUS(status);
1783 td_set_runstate(td, TD_REAPED);
1789 * If the job is stuck, do a forceful timeout of it and
1792 if (td->terminate &&
1793 time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1795 td_set_runstate(td, TD_REAPED);
1800 * thread is not dead, continue
1806 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1807 (*t_rate) -= ddir_rw_sum(td->o.rate);
1814 done_secs += mtime_since_now(&td->epoch) / 1000;
1815 profile_td_exit(td);
1818 if (*nr_running == cputhreads && !pending && realthreads)
1819 fio_terminate_threads(TERMINATE_ALL);
1822 static int __check_trigger_file(void)
1829 if (stat(trigger_file, &sb))
1832 if (unlink(trigger_file) < 0)
1833 log_err("fio: failed to unlink %s: %s\n", trigger_file,
1839 static int trigger_timedout(void)
1841 if (trigger_timeout)
1842 return time_since_genesis() >= trigger_timeout;
1847 void exec_trigger(const char *cmd)
1856 log_err("fio: failed executing %s trigger\n", cmd);
1859 void check_trigger_file(void)
1861 if (__check_trigger_file() || trigger_timedout()) {
1863 fio_clients_send_trigger(trigger_remote_cmd);
1865 verify_save_state();
1866 fio_terminate_threads(TERMINATE_ALL);
1867 exec_trigger(trigger_cmd);
1872 static int fio_verify_load_state(struct thread_data *td)
1876 if (!td->o.verify_state)
1883 ret = fio_server_get_verify_state(td->o.name,
1884 td->thread_number - 1, &data, &ver);
1886 verify_convert_assign_state(td, data, ver);
1888 ret = verify_load_state(td, "local");
1893 static void do_usleep(unsigned int usecs)
1895 check_for_running_stats();
1896 check_trigger_file();
1900 static int check_mount_writes(struct thread_data *td)
1905 if (!td_write(td) || td->o.allow_mounted_write)
1908 for_each_file(td, f, i) {
1909 if (f->filetype != FIO_TYPE_BD)
1911 if (device_is_mounted(f->file_name))
1917 log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.", f->file_name);
1922 * Main function for kicking off and reaping jobs, as needed.
1924 static void run_threads(void)
1926 struct thread_data *td;
1927 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1930 if (fio_gtod_offload && fio_start_gtod_thread())
1933 fio_idle_prof_init();
1937 nr_thread = nr_process = 0;
1938 for_each_td(td, i) {
1939 if (check_mount_writes(td))
1941 if (td->o.use_thread)
1947 if (output_format == FIO_OUTPUT_NORMAL) {
1948 log_info("Starting ");
1950 log_info("%d thread%s", nr_thread,
1951 nr_thread > 1 ? "s" : "");
1955 log_info("%d process%s", nr_process,
1956 nr_process > 1 ? "es" : "");
1962 todo = thread_number;
1965 m_rate = t_rate = 0;
1967 for_each_td(td, i) {
1968 print_status_init(td->thread_number - 1);
1970 if (!td->o.create_serialize)
1973 if (fio_verify_load_state(td))
1977 * do file setup here so it happens sequentially,
1978 * we don't want X number of threads getting their
1979 * client data interspersed on disk
1981 if (setup_files(td)) {
1985 log_err("fio: pid=%d, err=%d/%s\n",
1986 (int) td->pid, td->error, td->verror);
1987 td_set_runstate(td, TD_REAPED);
1994 * for sharing to work, each job must always open
1995 * its own files. so close them, if we opened them
1998 for_each_file(td, f, j) {
1999 if (fio_file_open(f))
2000 td_io_close_file(td, f);
2005 /* start idle threads before io threads start to run */
2006 fio_idle_prof_start();
2011 struct thread_data *map[REAL_MAX_JOBS];
2012 struct timeval this_start;
2013 int this_jobs = 0, left;
2016 * create threads (TD_NOT_CREATED -> TD_CREATED)
2018 for_each_td(td, i) {
2019 if (td->runstate != TD_NOT_CREATED)
2023 * never got a chance to start, killed by other
2024 * thread for some reason
2026 if (td->terminate) {
2031 if (td->o.start_delay) {
2032 spent = utime_since_genesis();
2034 if (td->o.start_delay > spent)
2038 if (td->o.stonewall && (nr_started || nr_running)) {
2039 dprint(FD_PROCESS, "%s: stonewall wait\n",
2046 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
2047 td->update_rusage = 0;
2050 * Set state to created. Thread will transition
2051 * to TD_INITIALIZED when it's done setting up.
2053 td_set_runstate(td, TD_CREATED);
2054 map[this_jobs++] = td;
2057 if (td->o.use_thread) {
2060 dprint(FD_PROCESS, "will pthread_create\n");
2061 ret = pthread_create(&td->thread, NULL,
2064 log_err("pthread_create: %s\n",
2069 ret = pthread_detach(td->thread);
2071 log_err("pthread_detach: %s",
2075 dprint(FD_PROCESS, "will fork\n");
2078 int ret = fork_main(shm_id, i);
2081 } else if (i == fio_debug_jobno)
2082 *fio_debug_jobp = pid;
2084 dprint(FD_MUTEX, "wait on startup_mutex\n");
2085 if (fio_mutex_down_timeout(startup_mutex, 10)) {
2086 log_err("fio: job startup hung? exiting.\n");
2087 fio_terminate_threads(TERMINATE_ALL);
2092 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2096 * Wait for the started threads to transition to
2099 fio_gettime(&this_start, NULL);
2101 while (left && !fio_abort) {
2102 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2107 for (i = 0; i < this_jobs; i++) {
2111 if (td->runstate == TD_INITIALIZED) {
2114 } else if (td->runstate >= TD_EXITED) {
2118 nr_running++; /* work-around... */
2124 log_err("fio: %d job%s failed to start\n", left,
2125 left > 1 ? "s" : "");
2126 for (i = 0; i < this_jobs; i++) {
2130 kill(td->pid, SIGTERM);
2136 * start created threads (TD_INITIALIZED -> TD_RUNNING).
2138 for_each_td(td, i) {
2139 if (td->runstate != TD_INITIALIZED)
2142 if (in_ramp_time(td))
2143 td_set_runstate(td, TD_RAMP);
2145 td_set_runstate(td, TD_RUNNING);
2148 m_rate += ddir_rw_sum(td->o.ratemin);
2149 t_rate += ddir_rw_sum(td->o.rate);
2151 fio_mutex_up(td->mutex);
2154 reap_threads(&nr_running, &t_rate, &m_rate);
2160 while (nr_running) {
2161 reap_threads(&nr_running, &t_rate, &m_rate);
2165 fio_idle_prof_stop();
2170 static void wait_for_helper_thread_exit(void)
2175 pthread_cond_signal(&helper_cond);
2176 pthread_join(helper_thread, &ret);
2179 static void free_disk_util(void)
2181 disk_util_prune_entries();
2183 pthread_cond_destroy(&helper_cond);
2186 static void *helper_thread_main(void *data)
2190 fio_mutex_up(startup_mutex);
2193 uint64_t sec = DISK_UTIL_MSEC / 1000;
2194 uint64_t nsec = (DISK_UTIL_MSEC % 1000) * 1000000;
2198 gettimeofday(&tv, NULL);
2199 ts.tv_sec = tv.tv_sec + sec;
2200 ts.tv_nsec = (tv.tv_usec * 1000) + nsec;
2202 if (ts.tv_nsec >= 1000000000ULL) {
2203 ts.tv_nsec -= 1000000000ULL;
2207 pthread_cond_timedwait(&helper_cond, &helper_lock, &ts);
2209 ret = update_io_ticks();
2211 if (helper_do_stat) {
2213 __show_running_run_stats();
2217 print_thread_status();
2223 static int create_helper_thread(void)
2229 pthread_cond_init(&helper_cond, NULL);
2230 pthread_mutex_init(&helper_lock, NULL);
2232 ret = pthread_create(&helper_thread, NULL, helper_thread_main, NULL);
2234 log_err("Can't create helper thread: %s\n", strerror(ret));
2238 dprint(FD_MUTEX, "wait on startup_mutex\n");
2239 fio_mutex_down(startup_mutex);
2240 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2244 int fio_backend(void)
2246 struct thread_data *td;
2250 if (load_profile(exec_profile))
2253 exec_profile = NULL;
2259 struct log_params p = {
2260 .log_type = IO_LOG_TYPE_BW,
2263 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2264 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2265 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
2268 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2269 if (startup_mutex == NULL)
2274 create_helper_thread();
2276 cgroup_list = smalloc(sizeof(*cgroup_list));
2277 INIT_FLIST_HEAD(cgroup_list);
2281 wait_for_helper_thread_exit();
2286 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2287 struct io_log *log = agg_io_log[i];
2295 for_each_td(td, i) {
2296 fio_options_free(td);
2297 if (td->rusage_sem) {
2298 fio_mutex_remove(td->rusage_sem);
2299 td->rusage_sem = NULL;
2304 cgroup_kill(cgroup_list);
2308 fio_mutex_remove(startup_mutex);