2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 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 unsigned long page_mask;
42 unsigned long page_size;
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
47 int thread_number = 0;
52 unsigned long done_secs = 0;
54 static struct fio_mutex *startup_mutex;
55 static volatile int fio_abort;
56 static int exit_value;
57 static struct itimerval itimer;
58 static pthread_t gtod_thread;
60 struct io_log *agg_io_log[2];
62 #define TERMINATE_ALL (-1)
63 #define JOB_START_TIMEOUT (5 * 1000)
65 void td_set_runstate(struct thread_data *td, int runstate)
67 if (td->runstate == runstate)
70 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
71 td->runstate, runstate);
72 td->runstate = runstate;
75 static void terminate_threads(int group_id)
77 struct thread_data *td;
80 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
84 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
85 td->o.name, (int) td->pid);
87 td->o.start_delay = 0;
90 * if the thread is running, just let it exit
92 if (td->runstate < TD_RUNNING)
93 kill(td->pid, SIGQUIT);
95 struct ioengine_ops *ops = td->io_ops;
97 if (ops && (ops->flags & FIO_SIGQUIT))
98 kill(td->pid, SIGQUIT);
104 static void status_timer_arm(void)
106 itimer.it_value.tv_sec = 0;
107 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
108 setitimer(ITIMER_REAL, &itimer, NULL);
111 static void sig_alrm(int fio_unused sig)
115 print_thread_status();
121 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
123 static void sig_quit(int sig)
127 static void sig_int(int sig)
130 printf("\nfio: terminating on signal %d\n", sig);
132 terminate_threads(TERMINATE_ALL);
136 static void sig_ill(int fio_unused sig)
141 log_err("fio: illegal instruction. your cpu does not support "
142 "the sse4.2 instruction for crc32c\n");
143 terminate_threads(TERMINATE_ALL);
147 static void set_sig_handlers(void)
149 struct sigaction act;
151 memset(&act, 0, sizeof(act));
152 act.sa_handler = sig_alrm;
153 act.sa_flags = SA_RESTART;
154 sigaction(SIGALRM, &act, NULL);
156 memset(&act, 0, sizeof(act));
157 act.sa_handler = sig_int;
158 act.sa_flags = SA_RESTART;
159 sigaction(SIGINT, &act, NULL);
161 memset(&act, 0, sizeof(act));
162 act.sa_handler = sig_ill;
163 act.sa_flags = SA_RESTART;
164 sigaction(SIGILL, &act, NULL);
166 memset(&act, 0, sizeof(act));
167 act.sa_handler = sig_quit;
168 act.sa_flags = SA_RESTART;
169 sigaction(SIGQUIT, &act, NULL);
172 static inline int should_check_rate(struct thread_data *td)
174 struct thread_options *o = &td->o;
177 * If some rate setting was given, we need to check it
179 if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min)
186 * Check if we are above the minimum rate given.
188 static int check_min_rate(struct thread_data *td, struct timeval *now)
190 unsigned long long bytes = 0;
191 unsigned long iops = 0;
196 * allow a 2 second settle period in the beginning
198 if (mtime_since(&td->start, now) < 2000)
202 iops += td->io_blocks[DDIR_READ];
203 bytes += td->this_io_bytes[DDIR_READ];
206 iops += td->io_blocks[DDIR_WRITE];
207 bytes += td->this_io_bytes[DDIR_WRITE];
211 * if rate blocks is set, sample is running
213 if (td->rate_bytes || td->rate_blocks) {
214 spent = mtime_since(&td->lastrate, now);
215 if (spent < td->o.ratecycle)
220 * check bandwidth specified rate
222 if (bytes < td->rate_bytes) {
223 log_err("%s: min rate %u not met\n", td->o.name,
227 rate = (bytes - td->rate_bytes) / spent;
228 if (rate < td->o.ratemin ||
229 bytes < td->rate_bytes) {
230 log_err("%s: min rate %u not met, got"
231 " %luKiB/sec\n", td->o.name,
232 td->o.ratemin, rate);
238 * checks iops specified rate
240 if (iops < td->o.rate_iops) {
241 log_err("%s: min iops rate %u not met\n",
242 td->o.name, td->o.rate_iops);
245 rate = (iops - td->rate_blocks) / spent;
246 if (rate < td->o.rate_iops_min ||
247 iops < td->rate_blocks) {
248 log_err("%s: min iops rate %u not met,"
249 " got %lu\n", td->o.name,
257 td->rate_bytes = bytes;
258 td->rate_blocks = iops;
259 memcpy(&td->lastrate, now, sizeof(*now));
263 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
267 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
274 * When job exits, we can cancel the in-flight IO if we are using async
275 * io. Attempt to do so.
277 static void cleanup_pending_aio(struct thread_data *td)
279 struct flist_head *entry, *n;
284 * get immediately available events, if any
286 r = io_u_queued_complete(td, 0);
291 * now cancel remaining active events
293 if (td->io_ops->cancel) {
294 flist_for_each_safe(entry, n, &td->io_u_busylist) {
295 io_u = flist_entry(entry, struct io_u, list);
298 * if the io_u isn't in flight, then that generally
299 * means someone leaked an io_u. complain but fix
300 * it up, so we don't stall here.
302 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
303 log_err("fio: non-busy IO on busy list\n");
306 r = td->io_ops->cancel(td, io_u);
314 r = io_u_queued_complete(td, td->cur_depth);
318 * Helper to handle the final sync of a file. Works just like the normal
319 * io path, just does everything sync.
321 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
323 struct io_u *io_u = __get_io_u(td);
329 io_u->ddir = DDIR_SYNC;
332 if (td_io_prep(td, io_u)) {
338 ret = td_io_queue(td, io_u);
340 td_verror(td, io_u->error, "td_io_queue");
343 } else if (ret == FIO_Q_QUEUED) {
344 if (io_u_queued_complete(td, 1) < 0)
346 } else if (ret == FIO_Q_COMPLETED) {
348 td_verror(td, io_u->error, "td_io_queue");
352 if (io_u_sync_complete(td, io_u) < 0)
354 } else if (ret == FIO_Q_BUSY) {
355 if (td_io_commit(td))
363 static inline void update_tv_cache(struct thread_data *td)
365 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
366 fio_gettime(&td->tv_cache, NULL);
370 * The main verify engine. Runs over the writes we previously submitted,
371 * reads the blocks back in, and checks the crc/md5 of the data.
373 static void do_verify(struct thread_data *td)
381 * sync io first and invalidate cache, to make sure we really
384 for_each_file(td, f, i) {
385 if (!(f->flags & FIO_FILE_OPEN))
387 if (fio_io_sync(td, f))
389 if (file_invalidate_cache(td, f))
396 td_set_runstate(td, TD_VERIFYING);
399 while (!td->terminate) {
402 io_u = __get_io_u(td);
408 if (runtime_exceeded(td, &td->tv_cache)) {
414 if (get_next_verify(td, io_u)) {
419 if (td_io_prep(td, io_u)) {
424 io_u->end_io = verify_io_u;
426 ret = td_io_queue(td, io_u);
428 case FIO_Q_COMPLETED:
431 else if (io_u->resid) {
432 int bytes = io_u->xfer_buflen - io_u->resid;
433 struct fio_file *f = io_u->file;
439 td_verror(td, EIO, "full resid");
444 io_u->xfer_buflen = io_u->resid;
445 io_u->xfer_buf += bytes;
446 io_u->offset += bytes;
448 td->ts.short_io_u[io_u->ddir]++;
450 if (io_u->offset == f->real_file_size)
453 requeue_io_u(td, &io_u);
456 ret = io_u_sync_complete(td, io_u);
464 requeue_io_u(td, &io_u);
465 ret2 = td_io_commit(td);
471 td_verror(td, -ret, "td_io_queue");
475 if (ret < 0 || td->error)
479 * if we can queue more, do so. but check if there are
480 * completed io_u's first.
482 full = queue_full(td) || ret == FIO_Q_BUSY;
483 if (full || !td->o.iodepth_batch_complete) {
484 min_events = td->o.iodepth_batch_complete;
485 if (full && !min_events)
490 * Reap required number of io units, if any,
491 * and do the verification on them through
492 * the callback handler
494 if (io_u_queued_complete(td, min_events) < 0) {
498 } while (full && (td->cur_depth > td->o.iodepth_low));
505 min_events = td->cur_depth;
508 ret = io_u_queued_complete(td, min_events);
510 cleanup_pending_aio(td);
512 td_set_runstate(td, TD_RUNNING);
516 * Main IO worker function. It retrieves io_u's to process and queues
517 * and reaps them, checking for rate and errors along the way.
519 static void do_io(struct thread_data *td)
525 if (in_ramp_time(td))
526 td_set_runstate(td, TD_RAMP);
528 td_set_runstate(td, TD_RUNNING);
530 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
531 struct timeval comp_time;
546 if (runtime_exceeded(td, &td->tv_cache)) {
553 * Add verification end_io handler, if asked to verify
554 * a previously written file.
556 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
557 io_u->end_io = verify_io_u;
558 td_set_runstate(td, TD_VERIFYING);
559 } else if (in_ramp_time(td))
560 td_set_runstate(td, TD_RAMP);
562 td_set_runstate(td, TD_RUNNING);
564 ret = td_io_queue(td, io_u);
566 case FIO_Q_COMPLETED:
569 else if (io_u->resid) {
570 int bytes = io_u->xfer_buflen - io_u->resid;
571 struct fio_file *f = io_u->file;
577 td_verror(td, EIO, "full resid");
582 io_u->xfer_buflen = io_u->resid;
583 io_u->xfer_buf += bytes;
584 io_u->offset += bytes;
586 td->ts.short_io_u[io_u->ddir]++;
588 if (io_u->offset == f->real_file_size)
591 requeue_io_u(td, &io_u);
594 if (should_check_rate(td))
595 fio_gettime(&comp_time, NULL);
597 bytes_done = io_u_sync_complete(td, io_u);
604 * if the engine doesn't have a commit hook,
605 * the io_u is really queued. if it does have such
606 * a hook, it has to call io_u_queued() itself.
608 if (td->io_ops->commit == NULL)
609 io_u_queued(td, io_u);
612 requeue_io_u(td, &io_u);
613 ret2 = td_io_commit(td);
623 if (ret < 0 || td->error)
627 * See if we need to complete some commands
629 full = queue_full(td) || ret == FIO_Q_BUSY;
630 if (full || !td->o.iodepth_batch_complete) {
631 min_evts = td->o.iodepth_batch_complete;
632 if (full && !min_evts)
635 if (should_check_rate(td))
636 fio_gettime(&comp_time, NULL);
639 ret = io_u_queued_complete(td, min_evts);
644 } while (full && (td->cur_depth > td->o.iodepth_low));
653 * the rate is batched for now, it should work for batches
654 * of completions except the very first one which may look
657 if (!in_ramp_time(td) && should_check_rate(td)) {
658 usec = utime_since(&td->tv_cache, &comp_time);
660 rate_throttle(td, usec, bytes_done);
662 if (check_min_rate(td, &comp_time)) {
663 if (exitall_on_terminate)
664 terminate_threads(td->groupid);
665 td_verror(td, EIO, "check_min_rate");
670 if (td->o.thinktime) {
671 unsigned long long b;
673 b = td->io_blocks[0] + td->io_blocks[1];
674 if (!(b % td->o.thinktime_blocks)) {
677 if (td->o.thinktime_spin)
678 usec_spin(td->o.thinktime_spin);
680 left = td->o.thinktime - td->o.thinktime_spin;
682 usec_sleep(td, left);
687 if (td->o.fill_device && td->error == ENOSPC) {
696 ret = io_u_queued_complete(td, i);
698 if (should_fsync(td) && td->o.end_fsync) {
699 td_set_runstate(td, TD_FSYNCING);
701 for_each_file(td, f, i) {
702 if (!(f->flags & FIO_FILE_OPEN))
708 cleanup_pending_aio(td);
711 * stop job if we failed doing any IO
713 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
717 static void cleanup_io_u(struct thread_data *td)
719 struct flist_head *entry, *n;
722 flist_for_each_safe(entry, n, &td->io_u_freelist) {
723 io_u = flist_entry(entry, struct io_u, list);
725 flist_del(&io_u->list);
732 static int init_io_u(struct thread_data *td)
736 int cl_align, i, max_units;
739 max_units = td->o.iodepth;
740 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
741 td->orig_buffer_size = (unsigned long long) max_bs
742 * (unsigned long long) max_units;
744 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
747 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
748 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
751 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
752 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
756 if (allocate_io_mem(td))
760 p = ALIGN(td->orig_buffer);
764 cl_align = os_cache_line_size();
766 for (i = 0; i < max_units; i++) {
772 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
773 log_err("fio: posix_memalign=%s\n", strerror(errno));
778 memset(io_u, 0, sizeof(*io_u));
779 INIT_FLIST_HEAD(&io_u->list);
781 if (!(td->io_ops->flags & FIO_NOIO)) {
782 io_u->buf = p + max_bs * i;
784 if (td_write(td) && !td->o.refill_buffers)
785 io_u_fill_buffer(td, io_u, max_bs);
789 io_u->flags = IO_U_F_FREE;
790 flist_add(&io_u->list, &td->io_u_freelist);
796 static int switch_ioscheduler(struct thread_data *td)
798 char tmp[256], tmp2[128];
802 if (td->io_ops->flags & FIO_DISKLESSIO)
805 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
807 f = fopen(tmp, "r+");
809 if (errno == ENOENT) {
810 log_err("fio: os or kernel doesn't support IO scheduler"
814 td_verror(td, errno, "fopen iosched");
821 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
822 if (ferror(f) || ret != 1) {
823 td_verror(td, errno, "fwrite");
831 * Read back and check that the selected scheduler is now the default.
833 ret = fread(tmp, 1, sizeof(tmp), f);
834 if (ferror(f) || ret < 0) {
835 td_verror(td, errno, "fread");
840 sprintf(tmp2, "[%s]", td->o.ioscheduler);
841 if (!strstr(tmp, tmp2)) {
842 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
843 td_verror(td, EINVAL, "iosched_switch");
852 static int keep_running(struct thread_data *td)
854 unsigned long long io_done;
858 if (td->o.time_based)
865 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
867 if (io_done < td->o.size)
873 static void reset_io_counters(struct thread_data *td)
875 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
876 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
880 td->rw_end_set[0] = td->rw_end_set[1] = 0;
882 td->last_was_sync = 0;
885 * reset file done count if we are to start over
887 if (td->o.time_based || td->o.loops)
888 td->nr_done_files = 0;
891 * Set the same seed to get repeatable runs
893 td_fill_rand_seeds(td);
896 void reset_all_stats(struct thread_data *td)
901 reset_io_counters(td);
903 for (i = 0; i < 2; i++) {
905 td->io_blocks[i] = 0;
906 td->io_issues[i] = 0;
907 td->ts.total_io_u[i] = 0;
910 fio_gettime(&tv, NULL);
911 memcpy(&td->epoch, &tv, sizeof(tv));
912 memcpy(&td->start, &tv, sizeof(tv));
915 static void clear_io_state(struct thread_data *td)
920 reset_io_counters(td);
923 for_each_file(td, f, i)
924 f->flags &= ~FIO_FILE_DONE;
928 * Entry point for the thread based jobs. The process based jobs end up
929 * here as well, after a little setup.
931 static void *thread_main(void *data)
933 unsigned long long runtime[2], elapsed;
934 struct thread_data *td = data;
937 if (!td->o.use_thread)
942 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
944 INIT_FLIST_HEAD(&td->io_u_freelist);
945 INIT_FLIST_HEAD(&td->io_u_busylist);
946 INIT_FLIST_HEAD(&td->io_u_requeues);
947 INIT_FLIST_HEAD(&td->io_log_list);
948 INIT_FLIST_HEAD(&td->io_hist_list);
949 td->io_hist_tree = RB_ROOT;
951 td_set_runstate(td, TD_INITIALIZED);
952 dprint(FD_MUTEX, "up startup_mutex\n");
953 fio_mutex_up(startup_mutex);
954 dprint(FD_MUTEX, "wait on td->mutex\n");
955 fio_mutex_down(td->mutex);
956 dprint(FD_MUTEX, "done waiting on td->mutex\n");
959 * the ->mutex mutex is now no longer used, close it to avoid
960 * eating a file descriptor
962 fio_mutex_remove(td->mutex);
965 * May alter parameters that init_io_u() will use, so we need to
974 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
975 td_verror(td, errno, "cpu_set_affinity");
980 * If we have a gettimeofday() thread, make sure we exclude that
981 * thread from this job
983 if (td->o.gtod_cpu) {
984 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
985 if (fio_setaffinity(td) == -1) {
986 td_verror(td, errno, "cpu_set_affinity");
991 if (td->ioprio_set) {
992 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
993 td_verror(td, errno, "ioprio_set");
998 if (nice(td->o.nice) == -1) {
999 td_verror(td, errno, "nice");
1003 if (td->o.ioscheduler && switch_ioscheduler(td))
1006 if (!td->o.create_serialize && setup_files(td))
1012 if (init_random_map(td))
1015 if (td->o.exec_prerun) {
1016 if (system(td->o.exec_prerun) < 0)
1020 fio_gettime(&td->epoch, NULL);
1021 getrusage(RUSAGE_SELF, &td->ts.ru_start);
1023 runtime[0] = runtime[1] = 0;
1025 while (keep_running(td)) {
1026 fio_gettime(&td->start, NULL);
1027 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
1028 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1031 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1032 sizeof(td->lastrate));
1037 prune_io_piece_log(td);
1043 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1044 if (td->rw_end_set[DDIR_READ])
1045 elapsed = utime_since(&td->start,
1046 &td->rw_end[DDIR_READ]);
1048 elapsed = utime_since_now(&td->start);
1050 runtime[DDIR_READ] += elapsed;
1052 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1053 if (td->rw_end_set[DDIR_WRITE])
1054 elapsed = utime_since(&td->start,
1055 &td->rw_end[DDIR_WRITE]);
1057 elapsed = utime_since_now(&td->start);
1059 runtime[DDIR_WRITE] += elapsed;
1062 if (td->error || td->terminate)
1065 if (!td->o.do_verify ||
1066 td->o.verify == VERIFY_NONE ||
1067 (td->io_ops->flags & FIO_UNIDIR))
1072 fio_gettime(&td->start, NULL);
1076 runtime[DDIR_READ] += utime_since_now(&td->start);
1078 if (td->error || td->terminate)
1082 update_rusage_stat(td);
1083 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1084 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1085 td->ts.total_run_time = mtime_since_now(&td->epoch);
1086 td->ts.io_bytes[0] = td->io_bytes[0];
1087 td->ts.io_bytes[1] = td->io_bytes[1];
1089 if (td->ts.bw_log) {
1090 if (td->o.bw_log_file) {
1091 finish_log_named(td, td->ts.bw_log,
1092 td->o.bw_log_file, "bw");
1094 finish_log(td, td->ts.bw_log, "bw");
1096 if (td->ts.slat_log) {
1097 if (td->o.lat_log_file) {
1098 finish_log_named(td, td->ts.slat_log,
1099 td->o.lat_log_file, "slat");
1101 finish_log(td, td->ts.slat_log, "slat");
1103 if (td->ts.clat_log) {
1104 if (td->o.lat_log_file) {
1105 finish_log_named(td, td->ts.clat_log,
1106 td->o.lat_log_file, "clat");
1108 finish_log(td, td->ts.clat_log, "clat");
1110 if (td->o.exec_postrun) {
1111 if (system(td->o.exec_postrun) < 0)
1112 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
1115 if (exitall_on_terminate)
1116 terminate_threads(td->groupid);
1120 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1122 close_and_free_files(td);
1126 if (td->o.cpumask_set) {
1127 int ret = fio_cpuset_exit(&td->o.cpumask);
1129 td_verror(td, ret, "fio_cpuset_exit");
1133 * do this very late, it will log file closing as well
1135 if (td->o.write_iolog_file)
1136 write_iolog_close(td);
1138 options_mem_free(td);
1139 td_set_runstate(td, TD_EXITED);
1140 return (void *) (unsigned long) td->error;
1144 * We cannot pass the td data into a forked process, so attach the td and
1145 * pass it to the thread worker.
1147 static int fork_main(int shmid, int offset)
1149 struct thread_data *td;
1152 data = shmat(shmid, NULL, 0);
1153 if (data == (void *) -1) {
1160 td = data + offset * sizeof(struct thread_data);
1161 ret = thread_main(td);
1163 return (int) (unsigned long) ret;
1167 * Run over the job map and reap the threads that have exited, if any.
1169 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1171 struct thread_data *td;
1172 int i, cputhreads, realthreads, pending, status, ret;
1175 * reap exited threads (TD_EXITED -> TD_REAPED)
1177 realthreads = pending = cputhreads = 0;
1178 for_each_td(td, i) {
1182 * ->io_ops is NULL for a thread that has closed its
1185 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1194 if (td->runstate == TD_REAPED)
1196 if (td->o.use_thread) {
1197 if (td->runstate == TD_EXITED) {
1198 td_set_runstate(td, TD_REAPED);
1205 if (td->runstate == TD_EXITED)
1209 * check if someone quit or got killed in an unusual way
1211 ret = waitpid(td->pid, &status, flags);
1213 if (errno == ECHILD) {
1214 log_err("fio: pid=%d disappeared %d\n",
1215 (int) td->pid, td->runstate);
1216 td_set_runstate(td, TD_REAPED);
1220 } else if (ret == td->pid) {
1221 if (WIFSIGNALED(status)) {
1222 int sig = WTERMSIG(status);
1225 log_err("fio: pid=%d, got signal=%d\n",
1226 (int) td->pid, sig);
1227 td_set_runstate(td, TD_REAPED);
1230 if (WIFEXITED(status)) {
1231 if (WEXITSTATUS(status) && !td->error)
1232 td->error = WEXITSTATUS(status);
1234 td_set_runstate(td, TD_REAPED);
1240 * thread is not dead, continue
1246 (*m_rate) -= td->o.ratemin;
1247 (*t_rate) -= td->o.rate;
1254 done_secs += mtime_since_now(&td->epoch) / 1000;
1257 if (*nr_running == cputhreads && !pending && realthreads)
1258 terminate_threads(TERMINATE_ALL);
1261 static void *gtod_thread_main(void *data)
1263 fio_mutex_up(startup_mutex);
1266 * As long as we have jobs around, update the clock. It would be nice
1267 * to have some way of NOT hammering that CPU with gettimeofday(),
1268 * but I'm not sure what to use outside of a simple CPU nop to relax
1269 * it - we don't want to lose precision.
1279 static int fio_start_gtod_thread(void)
1283 ret = pthread_create(>od_thread, NULL, gtod_thread_main, NULL);
1285 log_err("Can't create gtod thread: %s\n", strerror(ret));
1289 ret = pthread_detach(gtod_thread);
1291 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
1295 dprint(FD_MUTEX, "wait on startup_mutex\n");
1296 fio_mutex_down(startup_mutex);
1297 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1302 * Main function for kicking off and reaping jobs, as needed.
1304 static void run_threads(void)
1306 struct thread_data *td;
1307 unsigned long spent;
1308 int i, todo, nr_running, m_rate, t_rate, nr_started;
1310 if (fio_pin_memory())
1313 if (fio_gtod_offload && fio_start_gtod_thread())
1316 if (!terse_output) {
1317 printf("Starting ");
1319 printf("%d thread%s", nr_thread,
1320 nr_thread > 1 ? "s" : "");
1324 printf("%d process%s", nr_process,
1325 nr_process > 1 ? "es" : "");
1333 todo = thread_number;
1336 m_rate = t_rate = 0;
1338 for_each_td(td, i) {
1339 print_status_init(td->thread_number - 1);
1341 if (!td->o.create_serialize) {
1347 * do file setup here so it happens sequentially,
1348 * we don't want X number of threads getting their
1349 * client data interspersed on disk
1351 if (setup_files(td)) {
1354 log_err("fio: pid=%d, err=%d/%s\n",
1355 (int) td->pid, td->error, td->verror);
1356 td_set_runstate(td, TD_REAPED);
1363 * for sharing to work, each job must always open
1364 * its own files. so close them, if we opened them
1367 for_each_file(td, f, i)
1368 td_io_close_file(td, f);
1377 struct thread_data *map[MAX_JOBS];
1378 struct timeval this_start;
1379 int this_jobs = 0, left;
1382 * create threads (TD_NOT_CREATED -> TD_CREATED)
1384 for_each_td(td, i) {
1385 if (td->runstate != TD_NOT_CREATED)
1389 * never got a chance to start, killed by other
1390 * thread for some reason
1392 if (td->terminate) {
1397 if (td->o.start_delay) {
1398 spent = mtime_since_genesis();
1400 if (td->o.start_delay * 1000 > spent)
1404 if (td->o.stonewall && (nr_started || nr_running)) {
1405 dprint(FD_PROCESS, "%s: stonewall wait\n",
1411 * Set state to created. Thread will transition
1412 * to TD_INITIALIZED when it's done setting up.
1414 td_set_runstate(td, TD_CREATED);
1415 map[this_jobs++] = td;
1418 if (td->o.use_thread) {
1421 dprint(FD_PROCESS, "will pthread_create\n");
1422 ret = pthread_create(&td->thread, NULL,
1425 log_err("pthread_create: %s\n",
1430 ret = pthread_detach(td->thread);
1432 log_err("pthread_detach: %s",
1436 dprint(FD_PROCESS, "will fork\n");
1439 int ret = fork_main(shm_id, i);
1442 } else if (i == fio_debug_jobno)
1443 *fio_debug_jobp = pid;
1445 dprint(FD_MUTEX, "wait on startup_mutex\n");
1446 fio_mutex_down(startup_mutex);
1447 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1451 * Wait for the started threads to transition to
1454 fio_gettime(&this_start, NULL);
1456 while (left && !fio_abort) {
1457 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1462 for (i = 0; i < this_jobs; i++) {
1466 if (td->runstate == TD_INITIALIZED) {
1469 } else if (td->runstate >= TD_EXITED) {
1473 nr_running++; /* work-around... */
1479 log_err("fio: %d jobs failed to start\n", left);
1480 for (i = 0; i < this_jobs; i++) {
1484 kill(td->pid, SIGTERM);
1490 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1492 for_each_td(td, i) {
1493 if (td->runstate != TD_INITIALIZED)
1496 if (in_ramp_time(td))
1497 td_set_runstate(td, TD_RAMP);
1499 td_set_runstate(td, TD_RUNNING);
1502 m_rate += td->o.ratemin;
1503 t_rate += td->o.rate;
1505 fio_mutex_up(td->mutex);
1508 reap_threads(&nr_running, &t_rate, &m_rate);
1514 while (nr_running) {
1515 reap_threads(&nr_running, &t_rate, &m_rate);
1523 int main(int argc, char *argv[])
1530 * We need locale for number printing, if it isn't set then just
1531 * go with the US format.
1533 if (!getenv("LC_NUMERIC"))
1534 setlocale(LC_NUMERIC, "en_US");
1536 if (parse_options(argc, argv))
1542 ps = sysconf(_SC_PAGESIZE);
1544 log_err("Failed to get page size\n");
1552 setup_log(&agg_io_log[DDIR_READ]);
1553 setup_log(&agg_io_log[DDIR_WRITE]);
1556 startup_mutex = fio_mutex_init(0);
1567 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1568 __finish_log(agg_io_log[DDIR_WRITE],
1569 "agg-write_bw.log");
1573 fio_mutex_remove(startup_mutex);