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 struct fio_mutex *writeout_mutex;
56 static volatile int fio_abort;
57 static int exit_value;
58 static struct itimerval itimer;
59 static pthread_t gtod_thread;
61 struct io_log *agg_io_log[2];
63 #define TERMINATE_ALL (-1)
64 #define JOB_START_TIMEOUT (5 * 1000)
66 void td_set_runstate(struct thread_data *td, int runstate)
68 if (td->runstate == runstate)
71 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
72 td->runstate, runstate);
73 td->runstate = runstate;
76 static void terminate_threads(int group_id)
78 struct thread_data *td;
81 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
84 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
85 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
86 td->o.name, (int) td->pid);
88 td->o.start_delay = 0;
91 * if the thread is running, just let it exit
93 if (td->runstate < TD_RUNNING)
94 kill(td->pid, SIGQUIT);
96 struct ioengine_ops *ops = td->io_ops;
98 if (ops && (ops->flags & FIO_SIGQUIT))
99 kill(td->pid, SIGQUIT);
105 static void status_timer_arm(void)
107 itimer.it_value.tv_sec = 0;
108 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
109 setitimer(ITIMER_REAL, &itimer, NULL);
112 static void sig_alrm(int fio_unused sig)
116 print_thread_status();
122 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
124 static void sig_quit(int sig)
128 static void sig_int(int sig)
131 printf("\nfio: terminating on signal %d\n", sig);
133 terminate_threads(TERMINATE_ALL);
137 static void sig_ill(int fio_unused sig)
142 log_err("fio: illegal instruction. your cpu does not support "
143 "the sse4.2 instruction for crc32c\n");
144 terminate_threads(TERMINATE_ALL);
148 static void set_sig_handlers(void)
150 struct sigaction act;
152 memset(&act, 0, sizeof(act));
153 act.sa_handler = sig_alrm;
154 act.sa_flags = SA_RESTART;
155 sigaction(SIGALRM, &act, NULL);
157 memset(&act, 0, sizeof(act));
158 act.sa_handler = sig_int;
159 act.sa_flags = SA_RESTART;
160 sigaction(SIGINT, &act, NULL);
162 memset(&act, 0, sizeof(act));
163 act.sa_handler = sig_ill;
164 act.sa_flags = SA_RESTART;
165 sigaction(SIGILL, &act, NULL);
167 memset(&act, 0, sizeof(act));
168 act.sa_handler = sig_quit;
169 act.sa_flags = SA_RESTART;
170 sigaction(SIGQUIT, &act, NULL);
173 static inline int should_check_rate(struct thread_data *td)
175 struct thread_options *o = &td->o;
178 * If some rate setting was given, we need to check it
180 if (o->rate || o->ratemin || o->rate_iops || o->rate_iops_min)
187 * Check if we are above the minimum rate given.
189 static int check_min_rate(struct thread_data *td, struct timeval *now)
191 unsigned long long bytes = 0;
192 unsigned long iops = 0;
197 * allow a 2 second settle period in the beginning
199 if (mtime_since(&td->start, now) < 2000)
203 iops += td->io_blocks[DDIR_READ];
204 bytes += td->this_io_bytes[DDIR_READ];
207 iops += td->io_blocks[DDIR_WRITE];
208 bytes += td->this_io_bytes[DDIR_WRITE];
212 * if rate blocks is set, sample is running
214 if (td->rate_bytes || td->rate_blocks) {
215 spent = mtime_since(&td->lastrate, now);
216 if (spent < td->o.ratecycle)
221 * check bandwidth specified rate
223 if (bytes < td->rate_bytes) {
224 log_err("%s: min rate %u not met\n", td->o.name,
228 rate = (bytes - td->rate_bytes) / spent;
229 if (rate < td->o.ratemin ||
230 bytes < td->rate_bytes) {
231 log_err("%s: min rate %u not met, got"
232 " %luKiB/sec\n", td->o.name,
233 td->o.ratemin, rate);
239 * checks iops specified rate
241 if (iops < td->o.rate_iops) {
242 log_err("%s: min iops rate %u not met\n",
243 td->o.name, td->o.rate_iops);
246 rate = (iops - td->rate_blocks) / spent;
247 if (rate < td->o.rate_iops_min ||
248 iops < td->rate_blocks) {
249 log_err("%s: min iops rate %u not met,"
250 " got %lu\n", td->o.name,
258 td->rate_bytes = bytes;
259 td->rate_blocks = iops;
260 memcpy(&td->lastrate, now, sizeof(*now));
264 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
268 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
275 * When job exits, we can cancel the in-flight IO if we are using async
276 * io. Attempt to do so.
278 static void cleanup_pending_aio(struct thread_data *td)
280 struct flist_head *entry, *n;
285 * get immediately available events, if any
287 r = io_u_queued_complete(td, 0);
292 * now cancel remaining active events
294 if (td->io_ops->cancel) {
295 flist_for_each_safe(entry, n, &td->io_u_busylist) {
296 io_u = flist_entry(entry, struct io_u, list);
299 * if the io_u isn't in flight, then that generally
300 * means someone leaked an io_u. complain but fix
301 * it up, so we don't stall here.
303 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
304 log_err("fio: non-busy IO on busy list\n");
307 r = td->io_ops->cancel(td, io_u);
315 r = io_u_queued_complete(td, td->cur_depth);
319 * Helper to handle the final sync of a file. Works just like the normal
320 * io path, just does everything sync.
322 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
324 struct io_u *io_u = __get_io_u(td);
330 io_u->ddir = DDIR_SYNC;
333 if (td_io_prep(td, io_u)) {
339 ret = td_io_queue(td, io_u);
341 td_verror(td, io_u->error, "td_io_queue");
344 } else if (ret == FIO_Q_QUEUED) {
345 if (io_u_queued_complete(td, 1) < 0)
347 } else if (ret == FIO_Q_COMPLETED) {
349 td_verror(td, io_u->error, "td_io_queue");
353 if (io_u_sync_complete(td, io_u) < 0)
355 } else if (ret == FIO_Q_BUSY) {
356 if (td_io_commit(td))
364 static inline void update_tv_cache(struct thread_data *td)
366 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
367 fio_gettime(&td->tv_cache, NULL);
371 * The main verify engine. Runs over the writes we previously submitted,
372 * reads the blocks back in, and checks the crc/md5 of the data.
374 static void do_verify(struct thread_data *td)
382 * sync io first and invalidate cache, to make sure we really
385 for_each_file(td, f, i) {
386 if (!(f->flags & FIO_FILE_OPEN))
388 if (fio_io_sync(td, f))
390 if (file_invalidate_cache(td, f))
397 td_set_runstate(td, TD_VERIFYING);
400 while (!td->terminate) {
403 io_u = __get_io_u(td);
409 if (runtime_exceeded(td, &td->tv_cache)) {
415 if (get_next_verify(td, io_u)) {
420 if (td_io_prep(td, io_u)) {
425 io_u->end_io = verify_io_u;
427 ret = td_io_queue(td, io_u);
429 case FIO_Q_COMPLETED:
432 else if (io_u->resid) {
433 int bytes = io_u->xfer_buflen - io_u->resid;
434 struct fio_file *f = io_u->file;
440 td_verror(td, EIO, "full resid");
445 io_u->xfer_buflen = io_u->resid;
446 io_u->xfer_buf += bytes;
447 io_u->offset += bytes;
449 td->ts.short_io_u[io_u->ddir]++;
451 if (io_u->offset == f->real_file_size)
454 requeue_io_u(td, &io_u);
457 ret = io_u_sync_complete(td, io_u);
465 requeue_io_u(td, &io_u);
466 ret2 = td_io_commit(td);
472 td_verror(td, -ret, "td_io_queue");
476 if (ret < 0 || td->error)
480 * if we can queue more, do so. but check if there are
481 * completed io_u's first.
483 full = queue_full(td) || ret == FIO_Q_BUSY;
484 if (full || !td->o.iodepth_batch_complete) {
485 min_events = td->o.iodepth_batch_complete;
486 if (full && !min_events)
491 * Reap required number of io units, if any,
492 * and do the verification on them through
493 * the callback handler
495 if (io_u_queued_complete(td, min_events) < 0) {
499 } while (full && (td->cur_depth > td->o.iodepth_low));
506 min_events = td->cur_depth;
509 ret = io_u_queued_complete(td, min_events);
511 cleanup_pending_aio(td);
513 td_set_runstate(td, TD_RUNNING);
517 * Main IO worker function. It retrieves io_u's to process and queues
518 * and reaps them, checking for rate and errors along the way.
520 static void do_io(struct thread_data *td)
526 if (in_ramp_time(td))
527 td_set_runstate(td, TD_RAMP);
529 td_set_runstate(td, TD_RUNNING);
531 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
532 struct timeval comp_time;
547 if (runtime_exceeded(td, &td->tv_cache)) {
554 * Add verification end_io handler, if asked to verify
555 * a previously written file.
557 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
558 io_u->end_io = verify_io_u;
559 td_set_runstate(td, TD_VERIFYING);
560 } else if (in_ramp_time(td))
561 td_set_runstate(td, TD_RAMP);
563 td_set_runstate(td, TD_RUNNING);
565 ret = td_io_queue(td, io_u);
567 case FIO_Q_COMPLETED:
570 else if (io_u->resid) {
571 int bytes = io_u->xfer_buflen - io_u->resid;
572 struct fio_file *f = io_u->file;
578 td_verror(td, EIO, "full resid");
583 io_u->xfer_buflen = io_u->resid;
584 io_u->xfer_buf += bytes;
585 io_u->offset += bytes;
587 td->ts.short_io_u[io_u->ddir]++;
589 if (io_u->offset == f->real_file_size)
592 requeue_io_u(td, &io_u);
595 if (should_check_rate(td))
596 fio_gettime(&comp_time, NULL);
598 bytes_done = io_u_sync_complete(td, io_u);
605 * if the engine doesn't have a commit hook,
606 * the io_u is really queued. if it does have such
607 * a hook, it has to call io_u_queued() itself.
609 if (td->io_ops->commit == NULL)
610 io_u_queued(td, io_u);
613 requeue_io_u(td, &io_u);
614 ret2 = td_io_commit(td);
624 if (ret < 0 || td->error)
628 * See if we need to complete some commands
630 full = queue_full(td) || ret == FIO_Q_BUSY;
631 if (full || !td->o.iodepth_batch_complete) {
632 min_evts = td->o.iodepth_batch_complete;
633 if (full && !min_evts)
636 if (should_check_rate(td))
637 fio_gettime(&comp_time, NULL);
640 ret = io_u_queued_complete(td, min_evts);
645 } while (full && (td->cur_depth > td->o.iodepth_low));
654 * the rate is batched for now, it should work for batches
655 * of completions except the very first one which may look
658 if (!in_ramp_time(td) && should_check_rate(td)) {
659 usec = utime_since(&td->tv_cache, &comp_time);
661 rate_throttle(td, usec, bytes_done);
663 if (check_min_rate(td, &comp_time)) {
664 if (exitall_on_terminate)
665 terminate_threads(td->groupid);
666 td_verror(td, EIO, "check_min_rate");
671 if (td->o.thinktime) {
672 unsigned long long b;
674 b = td->io_blocks[0] + td->io_blocks[1];
675 if (!(b % td->o.thinktime_blocks)) {
678 if (td->o.thinktime_spin)
679 usec_spin(td->o.thinktime_spin);
681 left = td->o.thinktime - td->o.thinktime_spin;
683 usec_sleep(td, left);
688 if (td->o.fill_device && td->error == ENOSPC) {
697 ret = io_u_queued_complete(td, i);
699 if (should_fsync(td) && td->o.end_fsync) {
700 td_set_runstate(td, TD_FSYNCING);
702 for_each_file(td, f, i) {
703 if (!(f->flags & FIO_FILE_OPEN))
709 cleanup_pending_aio(td);
712 * stop job if we failed doing any IO
714 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
718 static void cleanup_io_u(struct thread_data *td)
720 struct flist_head *entry, *n;
723 flist_for_each_safe(entry, n, &td->io_u_freelist) {
724 io_u = flist_entry(entry, struct io_u, list);
726 flist_del(&io_u->list);
733 static int init_io_u(struct thread_data *td)
737 int cl_align, i, max_units;
740 max_units = td->o.iodepth;
741 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
742 td->orig_buffer_size = (unsigned long long) max_bs
743 * (unsigned long long) max_units;
745 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
748 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
749 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
752 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
753 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
757 if (allocate_io_mem(td))
761 p = ALIGN(td->orig_buffer);
765 cl_align = os_cache_line_size();
767 for (i = 0; i < max_units; i++) {
773 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
774 log_err("fio: posix_memalign=%s\n", strerror(errno));
779 memset(io_u, 0, sizeof(*io_u));
780 INIT_FLIST_HEAD(&io_u->list);
782 if (!(td->io_ops->flags & FIO_NOIO)) {
783 io_u->buf = p + max_bs * i;
785 if (td_write(td) && !td->o.refill_buffers)
786 io_u_fill_buffer(td, io_u, max_bs);
790 io_u->flags = IO_U_F_FREE;
791 flist_add(&io_u->list, &td->io_u_freelist);
797 static int switch_ioscheduler(struct thread_data *td)
799 char tmp[256], tmp2[128];
803 if (td->io_ops->flags & FIO_DISKLESSIO)
806 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
808 f = fopen(tmp, "r+");
810 if (errno == ENOENT) {
811 log_err("fio: os or kernel doesn't support IO scheduler"
815 td_verror(td, errno, "fopen iosched");
822 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
823 if (ferror(f) || ret != 1) {
824 td_verror(td, errno, "fwrite");
832 * Read back and check that the selected scheduler is now the default.
834 ret = fread(tmp, 1, sizeof(tmp), f);
835 if (ferror(f) || ret < 0) {
836 td_verror(td, errno, "fread");
841 sprintf(tmp2, "[%s]", td->o.ioscheduler);
842 if (!strstr(tmp, tmp2)) {
843 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
844 td_verror(td, EINVAL, "iosched_switch");
853 static int keep_running(struct thread_data *td)
855 unsigned long long io_done;
859 if (td->o.time_based)
866 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
868 if (io_done < td->o.size)
874 static void reset_io_counters(struct thread_data *td)
876 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
877 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
881 td->rw_end_set[0] = td->rw_end_set[1] = 0;
883 td->last_was_sync = 0;
886 * reset file done count if we are to start over
888 if (td->o.time_based || td->o.loops)
889 td->nr_done_files = 0;
892 * Set the same seed to get repeatable runs
894 td_fill_rand_seeds(td);
897 void reset_all_stats(struct thread_data *td)
902 reset_io_counters(td);
904 for (i = 0; i < 2; i++) {
906 td->io_blocks[i] = 0;
907 td->io_issues[i] = 0;
908 td->ts.total_io_u[i] = 0;
911 fio_gettime(&tv, NULL);
912 memcpy(&td->epoch, &tv, sizeof(tv));
913 memcpy(&td->start, &tv, sizeof(tv));
916 static void clear_io_state(struct thread_data *td)
921 reset_io_counters(td);
924 for_each_file(td, f, i)
925 f->flags &= ~FIO_FILE_DONE;
928 static int exec_string(const char *string)
930 int ret, newlen = strlen(string) + 1 + 8;
933 str = malloc(newlen);
934 sprintf(str, "sh -c %s", string);
938 log_err("fio: exec of cmd <%s> failed\n", str);
945 * Entry point for the thread based jobs. The process based jobs end up
946 * here as well, after a little setup.
948 static void *thread_main(void *data)
950 unsigned long long runtime[2], elapsed;
951 struct thread_data *td = data;
954 if (!td->o.use_thread)
959 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
961 INIT_FLIST_HEAD(&td->io_u_freelist);
962 INIT_FLIST_HEAD(&td->io_u_busylist);
963 INIT_FLIST_HEAD(&td->io_u_requeues);
964 INIT_FLIST_HEAD(&td->io_log_list);
965 INIT_FLIST_HEAD(&td->io_hist_list);
966 td->io_hist_tree = RB_ROOT;
968 td_set_runstate(td, TD_INITIALIZED);
969 dprint(FD_MUTEX, "up startup_mutex\n");
970 fio_mutex_up(startup_mutex);
971 dprint(FD_MUTEX, "wait on td->mutex\n");
972 fio_mutex_down(td->mutex);
973 dprint(FD_MUTEX, "done waiting on td->mutex\n");
976 * the ->mutex mutex is now no longer used, close it to avoid
977 * eating a file descriptor
979 fio_mutex_remove(td->mutex);
982 * May alter parameters that init_io_u() will use, so we need to
991 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
992 td_verror(td, errno, "cpu_set_affinity");
997 * If we have a gettimeofday() thread, make sure we exclude that
998 * thread from this job
1000 if (td->o.gtod_cpu) {
1001 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1002 if (fio_setaffinity(td) == -1) {
1003 td_verror(td, errno, "cpu_set_affinity");
1008 if (td->ioprio_set) {
1009 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1010 td_verror(td, errno, "ioprio_set");
1015 if (nice(td->o.nice) == -1) {
1016 td_verror(td, errno, "nice");
1020 if (td->o.ioscheduler && switch_ioscheduler(td))
1023 if (!td->o.create_serialize && setup_files(td))
1029 if (init_random_map(td))
1032 if (td->o.exec_prerun) {
1033 if (exec_string(td->o.exec_prerun))
1037 if (td->o.pre_read) {
1038 if (pre_read_files(td) < 0)
1042 fio_gettime(&td->epoch, NULL);
1043 getrusage(RUSAGE_SELF, &td->ts.ru_start);
1045 runtime[0] = runtime[1] = 0;
1047 while (keep_running(td)) {
1048 fio_gettime(&td->start, NULL);
1049 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
1050 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1053 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1054 sizeof(td->lastrate));
1059 prune_io_piece_log(td);
1065 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1066 if (td->rw_end_set[DDIR_READ])
1067 elapsed = utime_since(&td->start,
1068 &td->rw_end[DDIR_READ]);
1070 elapsed = utime_since_now(&td->start);
1072 runtime[DDIR_READ] += elapsed;
1074 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1075 if (td->rw_end_set[DDIR_WRITE])
1076 elapsed = utime_since(&td->start,
1077 &td->rw_end[DDIR_WRITE]);
1079 elapsed = utime_since_now(&td->start);
1081 runtime[DDIR_WRITE] += elapsed;
1084 if (td->error || td->terminate)
1087 if (!td->o.do_verify ||
1088 td->o.verify == VERIFY_NONE ||
1089 (td->io_ops->flags & FIO_UNIDIR))
1094 fio_gettime(&td->start, NULL);
1098 runtime[DDIR_READ] += utime_since_now(&td->start);
1100 if (td->error || td->terminate)
1104 update_rusage_stat(td);
1105 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1106 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1107 td->ts.total_run_time = mtime_since_now(&td->epoch);
1108 td->ts.io_bytes[0] = td->io_bytes[0];
1109 td->ts.io_bytes[1] = td->io_bytes[1];
1111 fio_mutex_down(writeout_mutex);
1112 if (td->ts.bw_log) {
1113 if (td->o.bw_log_file) {
1114 finish_log_named(td, td->ts.bw_log,
1115 td->o.bw_log_file, "bw");
1117 finish_log(td, td->ts.bw_log, "bw");
1119 if (td->ts.slat_log) {
1120 if (td->o.lat_log_file) {
1121 finish_log_named(td, td->ts.slat_log,
1122 td->o.lat_log_file, "slat");
1124 finish_log(td, td->ts.slat_log, "slat");
1126 if (td->ts.clat_log) {
1127 if (td->o.lat_log_file) {
1128 finish_log_named(td, td->ts.clat_log,
1129 td->o.lat_log_file, "clat");
1131 finish_log(td, td->ts.clat_log, "clat");
1133 fio_mutex_up(writeout_mutex);
1134 if (td->o.exec_postrun)
1135 exec_string(td->o.exec_postrun);
1137 if (exitall_on_terminate)
1138 terminate_threads(td->groupid);
1142 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1144 close_and_free_files(td);
1148 if (td->o.cpumask_set) {
1149 int ret = fio_cpuset_exit(&td->o.cpumask);
1151 td_verror(td, ret, "fio_cpuset_exit");
1155 * do this very late, it will log file closing as well
1157 if (td->o.write_iolog_file)
1158 write_iolog_close(td);
1160 options_mem_free(td);
1161 td_set_runstate(td, TD_EXITED);
1162 return (void *) (unsigned long) td->error;
1166 * We cannot pass the td data into a forked process, so attach the td and
1167 * pass it to the thread worker.
1169 static int fork_main(int shmid, int offset)
1171 struct thread_data *td;
1174 data = shmat(shmid, NULL, 0);
1175 if (data == (void *) -1) {
1182 td = data + offset * sizeof(struct thread_data);
1183 ret = thread_main(td);
1185 return (int) (unsigned long) ret;
1189 * Run over the job map and reap the threads that have exited, if any.
1191 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1193 struct thread_data *td;
1194 int i, cputhreads, realthreads, pending, status, ret;
1197 * reap exited threads (TD_EXITED -> TD_REAPED)
1199 realthreads = pending = cputhreads = 0;
1200 for_each_td(td, i) {
1204 * ->io_ops is NULL for a thread that has closed its
1207 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1216 if (td->runstate == TD_REAPED)
1218 if (td->o.use_thread) {
1219 if (td->runstate == TD_EXITED) {
1220 td_set_runstate(td, TD_REAPED);
1227 if (td->runstate == TD_EXITED)
1231 * check if someone quit or got killed in an unusual way
1233 ret = waitpid(td->pid, &status, flags);
1235 if (errno == ECHILD) {
1236 log_err("fio: pid=%d disappeared %d\n",
1237 (int) td->pid, td->runstate);
1238 td_set_runstate(td, TD_REAPED);
1242 } else if (ret == td->pid) {
1243 if (WIFSIGNALED(status)) {
1244 int sig = WTERMSIG(status);
1247 log_err("fio: pid=%d, got signal=%d\n",
1248 (int) td->pid, sig);
1249 td_set_runstate(td, TD_REAPED);
1252 if (WIFEXITED(status)) {
1253 if (WEXITSTATUS(status) && !td->error)
1254 td->error = WEXITSTATUS(status);
1256 td_set_runstate(td, TD_REAPED);
1262 * thread is not dead, continue
1268 (*m_rate) -= td->o.ratemin;
1269 (*t_rate) -= td->o.rate;
1276 done_secs += mtime_since_now(&td->epoch) / 1000;
1279 if (*nr_running == cputhreads && !pending && realthreads)
1280 terminate_threads(TERMINATE_ALL);
1283 static void *gtod_thread_main(void *data)
1285 fio_mutex_up(startup_mutex);
1288 * As long as we have jobs around, update the clock. It would be nice
1289 * to have some way of NOT hammering that CPU with gettimeofday(),
1290 * but I'm not sure what to use outside of a simple CPU nop to relax
1291 * it - we don't want to lose precision.
1301 static int fio_start_gtod_thread(void)
1305 ret = pthread_create(>od_thread, NULL, gtod_thread_main, NULL);
1307 log_err("Can't create gtod thread: %s\n", strerror(ret));
1311 ret = pthread_detach(gtod_thread);
1313 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
1317 dprint(FD_MUTEX, "wait on startup_mutex\n");
1318 fio_mutex_down(startup_mutex);
1319 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1324 * Main function for kicking off and reaping jobs, as needed.
1326 static void run_threads(void)
1328 struct thread_data *td;
1329 unsigned long spent;
1330 int i, todo, nr_running, m_rate, t_rate, nr_started;
1332 if (fio_pin_memory())
1335 if (fio_gtod_offload && fio_start_gtod_thread())
1338 if (!terse_output) {
1339 printf("Starting ");
1341 printf("%d thread%s", nr_thread,
1342 nr_thread > 1 ? "s" : "");
1346 printf("%d process%s", nr_process,
1347 nr_process > 1 ? "es" : "");
1355 todo = thread_number;
1358 m_rate = t_rate = 0;
1360 for_each_td(td, i) {
1361 print_status_init(td->thread_number - 1);
1363 if (!td->o.create_serialize) {
1369 * do file setup here so it happens sequentially,
1370 * we don't want X number of threads getting their
1371 * client data interspersed on disk
1373 if (setup_files(td)) {
1376 log_err("fio: pid=%d, err=%d/%s\n",
1377 (int) td->pid, td->error, td->verror);
1378 td_set_runstate(td, TD_REAPED);
1385 * for sharing to work, each job must always open
1386 * its own files. so close them, if we opened them
1389 for_each_file(td, f, i)
1390 td_io_close_file(td, f);
1399 struct thread_data *map[MAX_JOBS];
1400 struct timeval this_start;
1401 int this_jobs = 0, left;
1404 * create threads (TD_NOT_CREATED -> TD_CREATED)
1406 for_each_td(td, i) {
1407 if (td->runstate != TD_NOT_CREATED)
1411 * never got a chance to start, killed by other
1412 * thread for some reason
1414 if (td->terminate) {
1419 if (td->o.start_delay) {
1420 spent = mtime_since_genesis();
1422 if (td->o.start_delay * 1000 > spent)
1426 if (td->o.stonewall && (nr_started || nr_running)) {
1427 dprint(FD_PROCESS, "%s: stonewall wait\n",
1433 * Set state to created. Thread will transition
1434 * to TD_INITIALIZED when it's done setting up.
1436 td_set_runstate(td, TD_CREATED);
1437 map[this_jobs++] = td;
1440 if (td->o.use_thread) {
1443 dprint(FD_PROCESS, "will pthread_create\n");
1444 ret = pthread_create(&td->thread, NULL,
1447 log_err("pthread_create: %s\n",
1452 ret = pthread_detach(td->thread);
1454 log_err("pthread_detach: %s",
1458 dprint(FD_PROCESS, "will fork\n");
1461 int ret = fork_main(shm_id, i);
1464 } else if (i == fio_debug_jobno)
1465 *fio_debug_jobp = pid;
1467 dprint(FD_MUTEX, "wait on startup_mutex\n");
1468 fio_mutex_down(startup_mutex);
1469 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1473 * Wait for the started threads to transition to
1476 fio_gettime(&this_start, NULL);
1478 while (left && !fio_abort) {
1479 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1484 for (i = 0; i < this_jobs; i++) {
1488 if (td->runstate == TD_INITIALIZED) {
1491 } else if (td->runstate >= TD_EXITED) {
1495 nr_running++; /* work-around... */
1501 log_err("fio: %d jobs failed to start\n", left);
1502 for (i = 0; i < this_jobs; i++) {
1506 kill(td->pid, SIGTERM);
1512 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1514 for_each_td(td, i) {
1515 if (td->runstate != TD_INITIALIZED)
1518 if (in_ramp_time(td))
1519 td_set_runstate(td, TD_RAMP);
1521 td_set_runstate(td, TD_RUNNING);
1524 m_rate += td->o.ratemin;
1525 t_rate += td->o.rate;
1527 fio_mutex_up(td->mutex);
1530 reap_threads(&nr_running, &t_rate, &m_rate);
1536 while (nr_running) {
1537 reap_threads(&nr_running, &t_rate, &m_rate);
1545 int main(int argc, char *argv[])
1552 * We need locale for number printing, if it isn't set then just
1553 * go with the US format.
1555 if (!getenv("LC_NUMERIC"))
1556 setlocale(LC_NUMERIC, "en_US");
1558 if (parse_options(argc, argv))
1564 ps = sysconf(_SC_PAGESIZE);
1566 log_err("Failed to get page size\n");
1574 setup_log(&agg_io_log[DDIR_READ]);
1575 setup_log(&agg_io_log[DDIR_WRITE]);
1578 startup_mutex = fio_mutex_init(0);
1579 writeout_mutex = fio_mutex_init(1);
1590 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1591 __finish_log(agg_io_log[DDIR_WRITE],
1592 "agg-write_bw.log");
1596 fio_mutex_remove(startup_mutex);
1597 fio_mutex_remove(writeout_mutex);