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"
58 static pthread_t disk_util_thread;
59 static struct fio_mutex *disk_thread_mutex;
60 static struct fio_mutex *startup_mutex;
61 static struct flist_head *cgroup_list;
62 static char *cgroup_mnt;
63 static int exit_value;
64 static volatile int fio_abort;
65 static unsigned int nr_process = 0;
66 static unsigned int nr_thread = 0;
68 struct io_log *agg_io_log[DDIR_RWDIR_CNT];
71 unsigned int thread_number = 0;
72 unsigned int stat_number = 0;
75 unsigned long done_secs = 0;
76 volatile int disk_util_exit = 0;
78 #define PAGE_ALIGN(buf) \
79 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
81 #define JOB_START_TIMEOUT (5 * 1000)
83 static void sig_int(int sig)
87 fio_server_got_signal(sig);
89 log_info("\nfio: terminating on signal %d\n", sig);
94 fio_terminate_threads(TERMINATE_ALL);
98 static void sig_show_status(int sig)
100 show_running_run_stats();
103 static void set_sig_handlers(void)
105 struct sigaction act;
107 memset(&act, 0, sizeof(act));
108 act.sa_handler = sig_int;
109 act.sa_flags = SA_RESTART;
110 sigaction(SIGINT, &act, NULL);
112 memset(&act, 0, sizeof(act));
113 act.sa_handler = sig_int;
114 act.sa_flags = SA_RESTART;
115 sigaction(SIGTERM, &act, NULL);
117 /* Windows uses SIGBREAK as a quit signal from other applications */
119 memset(&act, 0, sizeof(act));
120 act.sa_handler = sig_int;
121 act.sa_flags = SA_RESTART;
122 sigaction(SIGBREAK, &act, NULL);
125 memset(&act, 0, sizeof(act));
126 act.sa_handler = sig_show_status;
127 act.sa_flags = SA_RESTART;
128 sigaction(SIGUSR1, &act, NULL);
131 memset(&act, 0, sizeof(act));
132 act.sa_handler = sig_int;
133 act.sa_flags = SA_RESTART;
134 sigaction(SIGPIPE, &act, NULL);
139 * Check if we are above the minimum rate given.
141 static int __check_min_rate(struct thread_data *td, struct timeval *now,
144 unsigned long long bytes = 0;
145 unsigned long iops = 0;
148 unsigned int ratemin = 0;
149 unsigned int rate_iops = 0;
150 unsigned int rate_iops_min = 0;
152 assert(ddir_rw(ddir));
154 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
158 * allow a 2 second settle period in the beginning
160 if (mtime_since(&td->start, now) < 2000)
163 iops += td->this_io_blocks[ddir];
164 bytes += td->this_io_bytes[ddir];
165 ratemin += td->o.ratemin[ddir];
166 rate_iops += td->o.rate_iops[ddir];
167 rate_iops_min += td->o.rate_iops_min[ddir];
170 * if rate blocks is set, sample is running
172 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
173 spent = mtime_since(&td->lastrate[ddir], now);
174 if (spent < td->o.ratecycle)
177 if (td->o.rate[ddir]) {
179 * check bandwidth specified rate
181 if (bytes < td->rate_bytes[ddir]) {
182 log_err("%s: min rate %u not met\n", td->o.name,
187 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
191 if (rate < ratemin ||
192 bytes < td->rate_bytes[ddir]) {
193 log_err("%s: min rate %u not met, got"
194 " %luKB/sec\n", td->o.name,
201 * checks iops specified rate
203 if (iops < rate_iops) {
204 log_err("%s: min iops rate %u not met\n",
205 td->o.name, rate_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: min iops rate %u not met,"
216 " got %lu\n", td->o.name,
217 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 int check_min_rate(struct thread_data *td, struct timeval *now,
230 uint64_t *bytes_done)
234 if (bytes_done[DDIR_READ])
235 ret |= __check_min_rate(td, now, DDIR_READ);
236 if (bytes_done[DDIR_WRITE])
237 ret |= __check_min_rate(td, now, DDIR_WRITE);
238 if (bytes_done[DDIR_TRIM])
239 ret |= __check_min_rate(td, now, DDIR_TRIM);
245 * When job exits, we can cancel the in-flight IO if we are using async
246 * io. Attempt to do so.
248 static void cleanup_pending_aio(struct thread_data *td)
253 * get immediately available events, if any
255 r = io_u_queued_complete(td, 0, NULL);
260 * now cancel remaining active events
262 if (td->io_ops->cancel) {
266 io_u_qiter(&td->io_u_all, io_u, i) {
267 if (io_u->flags & IO_U_F_FLIGHT) {
268 r = td->io_ops->cancel(td, io_u);
276 r = io_u_queued_complete(td, td->cur_depth, NULL);
280 * Helper to handle the final sync of a file. Works just like the normal
281 * io path, just does everything sync.
283 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
285 struct io_u *io_u = __get_io_u(td);
291 io_u->ddir = DDIR_SYNC;
294 if (td_io_prep(td, io_u)) {
300 ret = td_io_queue(td, io_u);
302 td_verror(td, io_u->error, "td_io_queue");
305 } else if (ret == FIO_Q_QUEUED) {
306 if (io_u_queued_complete(td, 1, NULL) < 0)
308 } else if (ret == FIO_Q_COMPLETED) {
310 td_verror(td, io_u->error, "td_io_queue");
314 if (io_u_sync_complete(td, io_u, NULL) < 0)
316 } else if (ret == FIO_Q_BUSY) {
317 if (td_io_commit(td))
325 static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
329 if (fio_file_open(f))
330 return fio_io_sync(td, f);
332 if (td_io_open_file(td, f))
335 ret = fio_io_sync(td, f);
336 td_io_close_file(td, f);
340 static inline void __update_tv_cache(struct thread_data *td)
342 fio_gettime(&td->tv_cache, NULL);
345 static inline void update_tv_cache(struct thread_data *td)
347 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
348 __update_tv_cache(td);
351 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
353 if (in_ramp_time(td))
357 if (utime_since(&td->epoch, t) >= td->o.timeout)
363 static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
368 if (ret < 0 || td->error) {
370 enum error_type_bit eb;
375 eb = td_error_type(ddir, err);
376 if (!(td->o.continue_on_error & (1 << eb)))
379 if (td_non_fatal_error(td, eb, err)) {
381 * Continue with the I/Os in case of
384 update_error_count(td, err);
388 } else if (td->o.fill_device && err == ENOSPC) {
390 * We expect to hit this error if
391 * fill_device option is set.
394 fio_mark_td_terminate(td);
398 * Stop the I/O in case of a fatal
401 update_error_count(td, err);
409 static void check_update_rusage(struct thread_data *td)
411 if (td->update_rusage) {
412 td->update_rusage = 0;
413 update_rusage_stat(td);
414 fio_mutex_up(td->rusage_sem);
419 * The main verify engine. Runs over the writes we previously submitted,
420 * reads the blocks back in, and checks the crc/md5 of the data.
422 static void do_verify(struct thread_data *td, uint64_t verify_bytes)
424 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
430 dprint(FD_VERIFY, "starting loop\n");
433 * sync io first and invalidate cache, to make sure we really
436 for_each_file(td, f, i) {
437 if (!fio_file_open(f))
439 if (fio_io_sync(td, f))
441 if (file_invalidate_cache(td, f))
445 check_update_rusage(td);
450 td_set_runstate(td, TD_VERIFYING);
453 while (!td->terminate) {
458 check_update_rusage(td);
460 if (runtime_exceeded(td, &td->tv_cache)) {
461 __update_tv_cache(td);
462 if (runtime_exceeded(td, &td->tv_cache)) {
463 fio_mark_td_terminate(td);
468 if (flow_threshold_exceeded(td))
471 if (!td->o.experimental_verify) {
472 io_u = __get_io_u(td);
476 if (get_next_verify(td, io_u)) {
481 if (td_io_prep(td, io_u)) {
486 if (ddir_rw_sum(bytes_done) + td->o.rw_min_bs > verify_bytes)
489 while ((io_u = get_io_u(td)) != NULL) {
497 * We are only interested in the places where
498 * we wrote or trimmed IOs. Turn those into
499 * reads for verification purposes.
501 if (io_u->ddir == DDIR_READ) {
503 * Pretend we issued it for rwmix
506 td->io_issues[DDIR_READ]++;
509 } else if (io_u->ddir == DDIR_TRIM) {
510 io_u->ddir = DDIR_READ;
511 io_u->flags |= IO_U_F_TRIMMED;
513 } else if (io_u->ddir == DDIR_WRITE) {
514 io_u->ddir = DDIR_READ;
526 if (td->o.verify_async)
527 io_u->end_io = verify_io_u_async;
529 io_u->end_io = verify_io_u;
533 ret = td_io_queue(td, io_u);
535 case FIO_Q_COMPLETED:
538 clear_io_u(td, io_u);
539 } else if (io_u->resid) {
540 int bytes = io_u->xfer_buflen - io_u->resid;
546 td_verror(td, EIO, "full resid");
551 io_u->xfer_buflen = io_u->resid;
552 io_u->xfer_buf += bytes;
553 io_u->offset += bytes;
555 if (ddir_rw(io_u->ddir))
556 td->ts.short_io_u[io_u->ddir]++;
559 if (io_u->offset == f->real_file_size)
562 requeue_io_u(td, &io_u);
565 ret = io_u_sync_complete(td, io_u, bytes_done);
573 requeue_io_u(td, &io_u);
574 ret2 = td_io_commit(td);
580 td_verror(td, -ret, "td_io_queue");
584 if (break_on_this_error(td, ddir, &ret))
588 * if we can queue more, do so. but check if there are
589 * completed io_u's first. Note that we can get BUSY even
590 * without IO queued, if the system is resource starved.
593 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
594 if (full || !td->o.iodepth_batch_complete) {
595 min_events = min(td->o.iodepth_batch_complete,
598 * if the queue is full, we MUST reap at least 1 event
600 if (full && !min_events)
605 * Reap required number of io units, if any,
606 * and do the verification on them through
607 * the callback handler
609 if (io_u_queued_complete(td, min_events, bytes_done) < 0) {
613 } while (full && (td->cur_depth > td->o.iodepth_low));
619 check_update_rusage(td);
622 min_events = td->cur_depth;
625 ret = io_u_queued_complete(td, min_events, NULL);
627 cleanup_pending_aio(td);
629 td_set_runstate(td, TD_RUNNING);
631 dprint(FD_VERIFY, "exiting loop\n");
634 static unsigned int exceeds_number_ios(struct thread_data *td)
636 unsigned long long number_ios;
638 if (!td->o.number_ios)
641 number_ios = ddir_rw_sum(td->this_io_blocks);
642 number_ios += td->io_u_queued + td->io_u_in_flight;
644 return number_ios >= td->o.number_ios;
647 static int io_bytes_exceeded(struct thread_data *td)
649 unsigned long long bytes, limit;
652 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
653 else if (td_write(td))
654 bytes = td->this_io_bytes[DDIR_WRITE];
655 else if (td_read(td))
656 bytes = td->this_io_bytes[DDIR_READ];
658 bytes = td->this_io_bytes[DDIR_TRIM];
661 limit = td->o.io_limit;
665 return bytes >= limit || exceeds_number_ios(td);
669 * Main IO worker function. It retrieves io_u's to process and queues
670 * and reaps them, checking for rate and errors along the way.
672 * Returns number of bytes written and trimmed.
674 static uint64_t do_io(struct thread_data *td)
676 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
679 uint64_t total_bytes, bytes_issued = 0;
681 if (in_ramp_time(td))
682 td_set_runstate(td, TD_RAMP);
684 td_set_runstate(td, TD_RUNNING);
689 * If verify_backlog is enabled, we'll run the verify in this
690 * handler as well. For that case, we may need up to twice the
693 total_bytes = td->o.size;
694 if (td->o.verify != VERIFY_NONE &&
695 (td_write(td) && td->o.verify_backlog))
696 total_bytes += td->o.size;
698 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
699 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
701 struct timeval comp_time;
707 check_update_rusage(td);
709 if (td->terminate || td->done)
714 if (runtime_exceeded(td, &td->tv_cache)) {
715 __update_tv_cache(td);
716 if (runtime_exceeded(td, &td->tv_cache)) {
717 fio_mark_td_terminate(td);
722 if (flow_threshold_exceeded(td))
725 if (bytes_issued >= total_bytes)
729 if (IS_ERR_OR_NULL(io_u)) {
730 int err = PTR_ERR(io_u);
737 if (td->o.latency_target)
745 * Add verification end_io handler if:
746 * - Asked to verify (!td_rw(td))
747 * - Or the io_u is from our verify list (mixed write/ver)
749 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
750 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
752 if (!td->o.verify_pattern_bytes) {
753 io_u->rand_seed = __rand(&td->__verify_state);
754 if (sizeof(int) != sizeof(long *))
755 io_u->rand_seed *= __rand(&td->__verify_state);
758 if (td->o.verify_async)
759 io_u->end_io = verify_io_u_async;
761 io_u->end_io = verify_io_u;
762 td_set_runstate(td, TD_VERIFYING);
763 } else if (in_ramp_time(td))
764 td_set_runstate(td, TD_RAMP);
766 td_set_runstate(td, TD_RUNNING);
769 * Always log IO before it's issued, so we know the specific
770 * order of it. The logged unit will track when the IO has
773 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
775 td->o.verify != VERIFY_NONE &&
776 !td->o.experimental_verify)
777 log_io_piece(td, io_u);
779 ret = td_io_queue(td, io_u);
781 case FIO_Q_COMPLETED:
784 unlog_io_piece(td, io_u);
785 clear_io_u(td, io_u);
786 } else if (io_u->resid) {
787 int bytes = io_u->xfer_buflen - io_u->resid;
788 struct fio_file *f = io_u->file;
790 bytes_issued += bytes;
792 trim_io_piece(td, io_u);
798 unlog_io_piece(td, io_u);
799 td_verror(td, EIO, "full resid");
804 io_u->xfer_buflen = io_u->resid;
805 io_u->xfer_buf += bytes;
806 io_u->offset += bytes;
808 if (ddir_rw(io_u->ddir))
809 td->ts.short_io_u[io_u->ddir]++;
811 if (io_u->offset == f->real_file_size)
814 requeue_io_u(td, &io_u);
817 if (__should_check_rate(td, DDIR_READ) ||
818 __should_check_rate(td, DDIR_WRITE) ||
819 __should_check_rate(td, DDIR_TRIM))
820 fio_gettime(&comp_time, NULL);
822 ret = io_u_sync_complete(td, io_u, bytes_done);
825 bytes_issued += io_u->xfer_buflen;
830 * if the engine doesn't have a commit hook,
831 * the io_u is really queued. if it does have such
832 * a hook, it has to call io_u_queued() itself.
834 if (td->io_ops->commit == NULL)
835 io_u_queued(td, io_u);
836 bytes_issued += io_u->xfer_buflen;
839 unlog_io_piece(td, io_u);
840 requeue_io_u(td, &io_u);
841 ret2 = td_io_commit(td);
851 if (break_on_this_error(td, ddir, &ret))
855 * See if we need to complete some commands. Note that we
856 * can get BUSY even without IO queued, if the system is
860 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
861 if (full || !td->o.iodepth_batch_complete) {
862 min_evts = min(td->o.iodepth_batch_complete,
865 * if the queue is full, we MUST reap at least 1 event
867 if (full && !min_evts)
870 if (__should_check_rate(td, DDIR_READ) ||
871 __should_check_rate(td, DDIR_WRITE) ||
872 __should_check_rate(td, DDIR_TRIM))
873 fio_gettime(&comp_time, NULL);
876 ret = io_u_queued_complete(td, min_evts, bytes_done);
880 } while (full && (td->cur_depth > td->o.iodepth_low));
885 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
888 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
889 if (check_min_rate(td, &comp_time, bytes_done)) {
890 if (exitall_on_terminate)
891 fio_terminate_threads(td->groupid);
892 td_verror(td, EIO, "check_min_rate");
896 if (!in_ramp_time(td) && td->o.latency_target)
897 lat_target_check(td);
899 if (td->o.thinktime) {
900 unsigned long long b;
902 b = ddir_rw_sum(td->io_blocks);
903 if (!(b % td->o.thinktime_blocks)) {
908 if (td->o.thinktime_spin)
909 usec_spin(td->o.thinktime_spin);
911 left = td->o.thinktime - td->o.thinktime_spin;
913 usec_sleep(td, left);
918 check_update_rusage(td);
920 if (td->trim_entries)
921 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
923 if (td->o.fill_device && td->error == ENOSPC) {
925 fio_mark_td_terminate(td);
932 ret = io_u_queued_complete(td, i, bytes_done);
933 if (td->o.fill_device && td->error == ENOSPC)
937 if (should_fsync(td) && td->o.end_fsync) {
938 td_set_runstate(td, TD_FSYNCING);
940 for_each_file(td, f, i) {
941 if (!fio_file_fsync(td, f))
944 log_err("fio: end_fsync failed for file %s\n",
949 cleanup_pending_aio(td);
952 * stop job if we failed doing any IO
954 if (!ddir_rw_sum(td->this_io_bytes))
957 return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
960 static void cleanup_io_u(struct thread_data *td)
964 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
966 if (td->io_ops->io_u_free)
967 td->io_ops->io_u_free(td, io_u);
969 fio_memfree(io_u, sizeof(*io_u));
974 io_u_rexit(&td->io_u_requeues);
975 io_u_qexit(&td->io_u_freelist);
976 io_u_qexit(&td->io_u_all);
979 static int init_io_u(struct thread_data *td)
982 unsigned int max_bs, min_write;
983 int cl_align, i, max_units;
984 int data_xfer = 1, err;
987 max_units = td->o.iodepth;
988 max_bs = td_max_bs(td);
989 min_write = td->o.min_bs[DDIR_WRITE];
990 td->orig_buffer_size = (unsigned long long) max_bs
991 * (unsigned long long) max_units;
993 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
997 err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
998 err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
999 err += io_u_qinit(&td->io_u_all, td->o.iodepth);
1002 log_err("fio: failed setting up IO queues\n");
1007 * if we may later need to do address alignment, then add any
1008 * possible adjustment here so that we don't cause a buffer
1009 * overflow later. this adjustment may be too much if we get
1010 * lucky and the allocator gives us an aligned address.
1012 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1013 (td->io_ops->flags & FIO_RAWIO))
1014 td->orig_buffer_size += page_mask + td->o.mem_align;
1016 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
1019 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
1020 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
1023 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
1024 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
1028 if (data_xfer && allocate_io_mem(td))
1031 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1032 (td->io_ops->flags & FIO_RAWIO))
1033 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
1035 p = td->orig_buffer;
1037 cl_align = os_cache_line_size();
1039 for (i = 0; i < max_units; i++) {
1045 ptr = fio_memalign(cl_align, sizeof(*io_u));
1047 log_err("fio: unable to allocate aligned memory\n");
1052 memset(io_u, 0, sizeof(*io_u));
1053 INIT_FLIST_HEAD(&io_u->verify_list);
1054 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1058 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
1061 io_u_fill_buffer(td, io_u, min_write, max_bs);
1062 if (td_write(td) && td->o.verify_pattern_bytes) {
1064 * Fill the buffer with the pattern if we are
1065 * going to be doing writes.
1067 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
1072 io_u->flags = IO_U_F_FREE;
1073 io_u_qpush(&td->io_u_freelist, io_u);
1076 * io_u never leaves this stack, used for iteration of all
1079 io_u_qpush(&td->io_u_all, io_u);
1081 if (td->io_ops->io_u_init) {
1082 int ret = td->io_ops->io_u_init(td, io_u);
1085 log_err("fio: failed to init engine data: %d\n", ret);
1096 static int switch_ioscheduler(struct thread_data *td)
1098 char tmp[256], tmp2[128];
1102 if (td->io_ops->flags & FIO_DISKLESSIO)
1105 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1107 f = fopen(tmp, "r+");
1109 if (errno == ENOENT) {
1110 log_err("fio: os or kernel doesn't support IO scheduler"
1114 td_verror(td, errno, "fopen iosched");
1121 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
1122 if (ferror(f) || ret != 1) {
1123 td_verror(td, errno, "fwrite");
1131 * Read back and check that the selected scheduler is now the default.
1133 ret = fread(tmp, sizeof(tmp), 1, f);
1134 if (ferror(f) || ret < 0) {
1135 td_verror(td, errno, "fread");
1139 tmp[sizeof(tmp) - 1] = '\0';
1142 sprintf(tmp2, "[%s]", td->o.ioscheduler);
1143 if (!strstr(tmp, tmp2)) {
1144 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1145 td_verror(td, EINVAL, "iosched_switch");
1154 static int keep_running(struct thread_data *td)
1156 unsigned long long limit;
1160 if (td->o.time_based)
1166 if (exceeds_number_ios(td))
1170 limit = td->o.io_limit;
1174 if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
1178 * If the difference is less than the minimum IO size, we
1181 diff = limit - ddir_rw_sum(td->io_bytes);
1182 if (diff < td_max_bs(td))
1185 if (fio_files_done(td))
1194 static int exec_string(struct thread_options *o, const char *string, const char *mode)
1196 int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
1199 str = malloc(newlen);
1200 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
1202 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
1205 log_err("fio: exec of cmd <%s> failed\n", str);
1212 * Dry run to compute correct state of numberio for verification.
1214 static uint64_t do_dry_run(struct thread_data *td)
1216 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
1218 td_set_runstate(td, TD_RUNNING);
1220 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1221 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td)) {
1225 if (td->terminate || td->done)
1228 io_u = get_io_u(td);
1232 io_u->flags |= IO_U_F_FLIGHT;
1235 if (ddir_rw(acct_ddir(io_u)))
1236 td->io_issues[acct_ddir(io_u)]++;
1237 if (ddir_rw(io_u->ddir)) {
1238 io_u_mark_depth(td, 1);
1239 td->ts.total_io_u[io_u->ddir]++;
1242 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1244 td->o.verify != VERIFY_NONE &&
1245 !td->o.experimental_verify)
1246 log_io_piece(td, io_u);
1248 ret = io_u_sync_complete(td, io_u, bytes_done);
1252 return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
1256 * Entry point for the thread based jobs. The process based jobs end up
1257 * here as well, after a little setup.
1259 static void *thread_main(void *data)
1261 unsigned long long elapsed;
1262 struct thread_data *td = data;
1263 struct thread_options *o = &td->o;
1264 pthread_condattr_t attr;
1268 if (!o->use_thread) {
1274 fio_local_clock_init(o->use_thread);
1276 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1279 fio_server_send_start(td);
1281 INIT_FLIST_HEAD(&td->io_log_list);
1282 INIT_FLIST_HEAD(&td->io_hist_list);
1283 INIT_FLIST_HEAD(&td->verify_list);
1284 INIT_FLIST_HEAD(&td->trim_list);
1285 INIT_FLIST_HEAD(&td->next_rand_list);
1286 pthread_mutex_init(&td->io_u_lock, NULL);
1287 td->io_hist_tree = RB_ROOT;
1289 pthread_condattr_init(&attr);
1290 pthread_cond_init(&td->verify_cond, &attr);
1291 pthread_cond_init(&td->free_cond, &attr);
1293 td_set_runstate(td, TD_INITIALIZED);
1294 dprint(FD_MUTEX, "up startup_mutex\n");
1295 fio_mutex_up(startup_mutex);
1296 dprint(FD_MUTEX, "wait on td->mutex\n");
1297 fio_mutex_down(td->mutex);
1298 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1301 * A new gid requires privilege, so we need to do this before setting
1304 if (o->gid != -1U && setgid(o->gid)) {
1305 td_verror(td, errno, "setgid");
1308 if (o->uid != -1U && setuid(o->uid)) {
1309 td_verror(td, errno, "setuid");
1314 * If we have a gettimeofday() thread, make sure we exclude that
1315 * thread from this job
1318 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1321 * Set affinity first, in case it has an impact on the memory
1324 if (o->cpumask_set) {
1325 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
1326 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
1328 log_err("fio: no CPUs set\n");
1329 log_err("fio: Try increasing number of available CPUs\n");
1330 td_verror(td, EINVAL, "cpus_split");
1334 ret = fio_setaffinity(td->pid, o->cpumask);
1336 td_verror(td, errno, "cpu_set_affinity");
1341 #ifdef CONFIG_LIBNUMA
1342 /* numa node setup */
1343 if (o->numa_cpumask_set || o->numa_memmask_set) {
1344 struct bitmask *mask;
1347 if (numa_available() < 0) {
1348 td_verror(td, errno, "Does not support NUMA API\n");
1352 if (o->numa_cpumask_set) {
1353 mask = numa_parse_nodestring(o->numa_cpunodes);
1354 ret = numa_run_on_node_mask(mask);
1355 numa_free_nodemask(mask);
1357 td_verror(td, errno, \
1358 "numa_run_on_node_mask failed\n");
1363 if (o->numa_memmask_set) {
1366 if (o->numa_memnodes)
1367 mask = numa_parse_nodestring(o->numa_memnodes);
1369 switch (o->numa_mem_mode) {
1370 case MPOL_INTERLEAVE:
1371 numa_set_interleave_mask(mask);
1374 numa_set_membind(mask);
1377 numa_set_localalloc();
1379 case MPOL_PREFERRED:
1380 numa_set_preferred(o->numa_mem_prefer_node);
1388 numa_free_nodemask(mask);
1394 if (fio_pin_memory(td))
1398 * May alter parameters that init_io_u() will use, so we need to
1407 if (o->verify_async && verify_async_init(td))
1411 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1413 td_verror(td, errno, "ioprio_set");
1418 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1422 if (nice(o->nice) == -1 && errno != 0) {
1423 td_verror(td, errno, "nice");
1427 if (o->ioscheduler && switch_ioscheduler(td))
1430 if (!o->create_serialize && setup_files(td))
1436 if (init_random_map(td))
1439 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
1443 if (pre_read_files(td) < 0)
1447 if (td->flags & TD_F_COMPRESS_LOG)
1448 tp_init(&td->tp_data);
1450 fio_verify_init(td);
1452 fio_gettime(&td->epoch, NULL);
1453 fio_getrusage(&td->ru_start);
1455 while (keep_running(td)) {
1456 uint64_t verify_bytes;
1458 fio_gettime(&td->start, NULL);
1459 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1460 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1461 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1463 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1464 o->ratemin[DDIR_TRIM]) {
1465 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1466 sizeof(td->bw_sample_time));
1467 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1468 sizeof(td->bw_sample_time));
1469 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1470 sizeof(td->bw_sample_time));
1476 prune_io_piece_log(td);
1478 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1479 verify_bytes = do_dry_run(td);
1481 verify_bytes = do_io(td);
1485 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1486 elapsed = utime_since_now(&td->start);
1487 td->ts.runtime[DDIR_READ] += elapsed;
1489 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1490 elapsed = utime_since_now(&td->start);
1491 td->ts.runtime[DDIR_WRITE] += elapsed;
1493 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1494 elapsed = utime_since_now(&td->start);
1495 td->ts.runtime[DDIR_TRIM] += elapsed;
1498 if (td->error || td->terminate)
1501 if (!o->do_verify ||
1502 o->verify == VERIFY_NONE ||
1503 (td->io_ops->flags & FIO_UNIDIR))
1508 fio_gettime(&td->start, NULL);
1510 do_verify(td, verify_bytes);
1512 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1514 if (td->error || td->terminate)
1518 update_rusage_stat(td);
1519 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1520 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1521 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
1522 td->ts.total_run_time = mtime_since_now(&td->epoch);
1523 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1524 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1525 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1527 fio_unpin_memory(td);
1529 fio_writeout_logs(td);
1531 if (td->flags & TD_F_COMPRESS_LOG)
1532 tp_exit(&td->tp_data);
1534 if (o->exec_postrun)
1535 exec_string(o, o->exec_postrun, (const char *)"postrun");
1537 if (exitall_on_terminate)
1538 fio_terminate_threads(td->groupid);
1542 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1545 if (o->verify_async)
1546 verify_async_exit(td);
1548 close_and_free_files(td);
1551 cgroup_shutdown(td, &cgroup_mnt);
1553 if (o->cpumask_set) {
1554 int ret = fio_cpuset_exit(&o->cpumask);
1556 td_verror(td, ret, "fio_cpuset_exit");
1560 * do this very late, it will log file closing as well
1562 if (o->write_iolog_file)
1563 write_iolog_close(td);
1565 fio_mutex_remove(td->rusage_sem);
1566 td->rusage_sem = NULL;
1568 fio_mutex_remove(td->mutex);
1571 td_set_runstate(td, TD_EXITED);
1572 return (void *) (uintptr_t) td->error;
1577 * We cannot pass the td data into a forked process, so attach the td and
1578 * pass it to the thread worker.
1580 static int fork_main(int shmid, int offset)
1582 struct thread_data *td;
1585 #if !defined(__hpux) && !defined(CONFIG_NO_SHM)
1586 data = shmat(shmid, NULL, 0);
1587 if (data == (void *) -1) {
1595 * HP-UX inherits shm mappings?
1600 td = data + offset * sizeof(struct thread_data);
1601 ret = thread_main(td);
1603 return (int) (uintptr_t) ret;
1606 static void dump_td_info(struct thread_data *td)
1608 log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to "
1609 "be stuck. Doing forceful exit of this job.\n", td->o.name,
1610 (unsigned long) time_since_now(&td->terminate_time));
1614 * Run over the job map and reap the threads that have exited, if any.
1616 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1617 unsigned int *m_rate)
1619 struct thread_data *td;
1620 unsigned int cputhreads, realthreads, pending;
1624 * reap exited threads (TD_EXITED -> TD_REAPED)
1626 realthreads = pending = cputhreads = 0;
1627 for_each_td(td, i) {
1631 * ->io_ops is NULL for a thread that has closed its
1634 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1643 if (td->runstate == TD_REAPED)
1645 if (td->o.use_thread) {
1646 if (td->runstate == TD_EXITED) {
1647 td_set_runstate(td, TD_REAPED);
1654 if (td->runstate == TD_EXITED)
1658 * check if someone quit or got killed in an unusual way
1660 ret = waitpid(td->pid, &status, flags);
1662 if (errno == ECHILD) {
1663 log_err("fio: pid=%d disappeared %d\n",
1664 (int) td->pid, td->runstate);
1666 td_set_runstate(td, TD_REAPED);
1670 } else if (ret == td->pid) {
1671 if (WIFSIGNALED(status)) {
1672 int sig = WTERMSIG(status);
1674 if (sig != SIGTERM && sig != SIGUSR2)
1675 log_err("fio: pid=%d, got signal=%d\n",
1676 (int) td->pid, sig);
1678 td_set_runstate(td, TD_REAPED);
1681 if (WIFEXITED(status)) {
1682 if (WEXITSTATUS(status) && !td->error)
1683 td->error = WEXITSTATUS(status);
1685 td_set_runstate(td, TD_REAPED);
1691 * If the job is stuck, do a forceful timeout of it and
1694 if (td->terminate &&
1695 time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1697 td_set_runstate(td, TD_REAPED);
1702 * thread is not dead, continue
1708 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1709 (*t_rate) -= ddir_rw_sum(td->o.rate);
1716 done_secs += mtime_since_now(&td->epoch) / 1000;
1717 profile_td_exit(td);
1720 if (*nr_running == cputhreads && !pending && realthreads)
1721 fio_terminate_threads(TERMINATE_ALL);
1724 static void do_usleep(unsigned int usecs)
1726 check_for_running_stats();
1731 * Main function for kicking off and reaping jobs, as needed.
1733 static void run_threads(void)
1735 struct thread_data *td;
1736 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1739 if (fio_gtod_offload && fio_start_gtod_thread())
1742 fio_idle_prof_init();
1746 nr_thread = nr_process = 0;
1747 for_each_td(td, i) {
1748 if (td->o.use_thread)
1754 if (output_format == FIO_OUTPUT_NORMAL) {
1755 log_info("Starting ");
1757 log_info("%d thread%s", nr_thread,
1758 nr_thread > 1 ? "s" : "");
1762 log_info("%d process%s", nr_process,
1763 nr_process > 1 ? "es" : "");
1769 todo = thread_number;
1772 m_rate = t_rate = 0;
1774 for_each_td(td, i) {
1775 print_status_init(td->thread_number - 1);
1777 if (!td->o.create_serialize)
1781 * do file setup here so it happens sequentially,
1782 * we don't want X number of threads getting their
1783 * client data interspersed on disk
1785 if (setup_files(td)) {
1788 log_err("fio: pid=%d, err=%d/%s\n",
1789 (int) td->pid, td->error, td->verror);
1790 td_set_runstate(td, TD_REAPED);
1797 * for sharing to work, each job must always open
1798 * its own files. so close them, if we opened them
1801 for_each_file(td, f, j) {
1802 if (fio_file_open(f))
1803 td_io_close_file(td, f);
1808 /* start idle threads before io threads start to run */
1809 fio_idle_prof_start();
1814 struct thread_data *map[REAL_MAX_JOBS];
1815 struct timeval this_start;
1816 int this_jobs = 0, left;
1819 * create threads (TD_NOT_CREATED -> TD_CREATED)
1821 for_each_td(td, i) {
1822 if (td->runstate != TD_NOT_CREATED)
1826 * never got a chance to start, killed by other
1827 * thread for some reason
1829 if (td->terminate) {
1834 if (td->o.start_delay) {
1835 spent = utime_since_genesis();
1837 if (td->o.start_delay > spent)
1841 if (td->o.stonewall && (nr_started || nr_running)) {
1842 dprint(FD_PROCESS, "%s: stonewall wait\n",
1849 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
1850 td->update_rusage = 0;
1853 * Set state to created. Thread will transition
1854 * to TD_INITIALIZED when it's done setting up.
1856 td_set_runstate(td, TD_CREATED);
1857 map[this_jobs++] = td;
1860 if (td->o.use_thread) {
1863 dprint(FD_PROCESS, "will pthread_create\n");
1864 ret = pthread_create(&td->thread, NULL,
1867 log_err("pthread_create: %s\n",
1872 ret = pthread_detach(td->thread);
1874 log_err("pthread_detach: %s",
1878 dprint(FD_PROCESS, "will fork\n");
1881 int ret = fork_main(shm_id, i);
1884 } else if (i == fio_debug_jobno)
1885 *fio_debug_jobp = pid;
1887 dprint(FD_MUTEX, "wait on startup_mutex\n");
1888 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1889 log_err("fio: job startup hung? exiting.\n");
1890 fio_terminate_threads(TERMINATE_ALL);
1895 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1899 * Wait for the started threads to transition to
1902 fio_gettime(&this_start, NULL);
1904 while (left && !fio_abort) {
1905 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1910 for (i = 0; i < this_jobs; i++) {
1914 if (td->runstate == TD_INITIALIZED) {
1917 } else if (td->runstate >= TD_EXITED) {
1921 nr_running++; /* work-around... */
1927 log_err("fio: %d job%s failed to start\n", left,
1928 left > 1 ? "s" : "");
1929 for (i = 0; i < this_jobs; i++) {
1933 kill(td->pid, SIGTERM);
1939 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1941 for_each_td(td, i) {
1942 if (td->runstate != TD_INITIALIZED)
1945 if (in_ramp_time(td))
1946 td_set_runstate(td, TD_RAMP);
1948 td_set_runstate(td, TD_RUNNING);
1951 m_rate += ddir_rw_sum(td->o.ratemin);
1952 t_rate += ddir_rw_sum(td->o.rate);
1954 fio_mutex_up(td->mutex);
1957 reap_threads(&nr_running, &t_rate, &m_rate);
1963 while (nr_running) {
1964 reap_threads(&nr_running, &t_rate, &m_rate);
1968 fio_idle_prof_stop();
1973 void wait_for_disk_thread_exit(void)
1975 fio_mutex_down(disk_thread_mutex);
1978 static void free_disk_util(void)
1980 disk_util_start_exit();
1981 wait_for_disk_thread_exit();
1982 disk_util_prune_entries();
1985 static void *disk_thread_main(void *data)
1989 fio_mutex_up(startup_mutex);
1991 while (threads && !ret) {
1992 usleep(DISK_UTIL_MSEC * 1000);
1995 ret = update_io_ticks();
1998 print_thread_status();
2001 fio_mutex_up(disk_thread_mutex);
2005 static int create_disk_util_thread(void)
2011 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2013 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
2015 fio_mutex_remove(disk_thread_mutex);
2016 log_err("Can't create disk util thread: %s\n", strerror(ret));
2020 ret = pthread_detach(disk_util_thread);
2022 fio_mutex_remove(disk_thread_mutex);
2023 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
2027 dprint(FD_MUTEX, "wait on startup_mutex\n");
2028 fio_mutex_down(startup_mutex);
2029 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2033 int fio_backend(void)
2035 struct thread_data *td;
2039 if (load_profile(exec_profile))
2042 exec_profile = NULL;
2048 struct log_params p = {
2049 .log_type = IO_LOG_TYPE_BW,
2052 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2053 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2054 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
2057 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2058 if (startup_mutex == NULL)
2063 create_disk_util_thread();
2065 cgroup_list = smalloc(sizeof(*cgroup_list));
2066 INIT_FLIST_HEAD(cgroup_list);
2075 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2076 struct io_log *log = agg_io_log[i];
2085 fio_options_free(td);
2088 cgroup_kill(cgroup_list);
2092 fio_mutex_remove(startup_mutex);
2093 fio_mutex_remove(disk_thread_mutex);