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;
59 struct io_log *agg_io_log[2];
61 #define TERMINATE_ALL (-1)
62 #define JOB_START_TIMEOUT (5 * 1000)
64 void td_set_runstate(struct thread_data *td, int runstate)
66 if (td->runstate == runstate)
69 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
70 td->runstate, runstate);
71 td->runstate = runstate;
74 static void terminate_threads(int group_id)
76 struct thread_data *td;
79 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
82 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
83 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
84 td->o.name, (int) td->pid);
86 td->o.start_delay = 0;
89 * if the thread is running, just let it exit
91 if (td->runstate < TD_RUNNING)
92 kill(td->pid, SIGQUIT);
94 struct ioengine_ops *ops = td->io_ops;
96 if (ops && (ops->flags & FIO_SIGQUIT))
97 kill(td->pid, SIGQUIT);
103 static void status_timer_arm(void)
105 itimer.it_value.tv_sec = 0;
106 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
107 setitimer(ITIMER_REAL, &itimer, NULL);
110 static void sig_alrm(int fio_unused sig)
114 print_thread_status();
120 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
122 static void sig_quit(int sig)
126 static void sig_int(int sig)
129 printf("\nfio: terminating on signal %d\n", sig);
131 terminate_threads(TERMINATE_ALL);
135 static void sig_ill(int fio_unused sig)
140 log_err("fio: illegal instruction. your cpu does not support "
141 "the sse4.2 instruction for crc32c\n");
142 terminate_threads(TERMINATE_ALL);
146 static void set_sig_handlers(void)
148 struct sigaction act;
150 memset(&act, 0, sizeof(act));
151 act.sa_handler = sig_alrm;
152 act.sa_flags = SA_RESTART;
153 sigaction(SIGALRM, &act, NULL);
155 memset(&act, 0, sizeof(act));
156 act.sa_handler = sig_int;
157 act.sa_flags = SA_RESTART;
158 sigaction(SIGINT, &act, NULL);
160 memset(&act, 0, sizeof(act));
161 act.sa_handler = sig_ill;
162 act.sa_flags = SA_RESTART;
163 sigaction(SIGILL, &act, NULL);
165 memset(&act, 0, sizeof(act));
166 act.sa_handler = sig_quit;
167 act.sa_flags = SA_RESTART;
168 sigaction(SIGQUIT, &act, NULL);
171 static inline int should_check_rate(struct thread_data *td)
174 * No minimum rate set, always ok
176 if (!td->o.ratemin && !td->o.rate_iops_min)
183 * Check if we are above the minimum rate given.
185 static int check_min_rate(struct thread_data *td, struct timeval *now)
187 unsigned long long bytes = 0;
188 unsigned long iops = 0;
193 * allow a 2 second settle period in the beginning
195 if (mtime_since(&td->start, now) < 2000)
199 iops += td->io_blocks[DDIR_READ];
200 bytes += td->this_io_bytes[DDIR_READ];
203 iops += td->io_blocks[DDIR_WRITE];
204 bytes += td->this_io_bytes[DDIR_WRITE];
208 * if rate blocks is set, sample is running
210 if (td->rate_bytes || td->rate_blocks) {
211 spent = mtime_since(&td->lastrate, now);
212 if (spent < td->o.ratecycle)
217 * check bandwidth specified rate
219 if (bytes < td->rate_bytes) {
220 log_err("%s: min rate %u not met\n", td->o.name,
224 rate = (bytes - td->rate_bytes) / spent;
225 if (rate < td->o.ratemin ||
226 bytes < td->rate_bytes) {
227 log_err("%s: min rate %u not met, got"
228 " %luKiB/sec\n", td->o.name,
229 td->o.ratemin, rate);
235 * checks iops specified rate
237 if (iops < td->o.rate_iops) {
238 log_err("%s: min iops rate %u not met\n",
239 td->o.name, td->o.rate_iops);
242 rate = (iops - td->rate_blocks) / spent;
243 if (rate < td->o.rate_iops_min ||
244 iops < td->rate_blocks) {
245 log_err("%s: min iops rate %u not met,"
246 " got %lu\n", td->o.name,
254 td->rate_bytes = bytes;
255 td->rate_blocks = iops;
256 memcpy(&td->lastrate, now, sizeof(*now));
260 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
264 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
271 * When job exits, we can cancel the in-flight IO if we are using async
272 * io. Attempt to do so.
274 static void cleanup_pending_aio(struct thread_data *td)
276 struct flist_head *entry, *n;
281 * get immediately available events, if any
283 r = io_u_queued_complete(td, 0);
288 * now cancel remaining active events
290 if (td->io_ops->cancel) {
291 flist_for_each_safe(entry, n, &td->io_u_busylist) {
292 io_u = flist_entry(entry, struct io_u, list);
295 * if the io_u isn't in flight, then that generally
296 * means someone leaked an io_u. complain but fix
297 * it up, so we don't stall here.
299 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
300 log_err("fio: non-busy IO on busy list\n");
303 r = td->io_ops->cancel(td, io_u);
311 r = io_u_queued_complete(td, td->cur_depth);
315 * Helper to handle the final sync of a file. Works just like the normal
316 * io path, just does everything sync.
318 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
320 struct io_u *io_u = __get_io_u(td);
326 io_u->ddir = DDIR_SYNC;
329 if (td_io_prep(td, io_u)) {
335 ret = td_io_queue(td, io_u);
337 td_verror(td, io_u->error, "td_io_queue");
340 } else if (ret == FIO_Q_QUEUED) {
341 if (io_u_queued_complete(td, 1) < 0)
343 } else if (ret == FIO_Q_COMPLETED) {
345 td_verror(td, io_u->error, "td_io_queue");
349 if (io_u_sync_complete(td, io_u) < 0)
351 } else if (ret == FIO_Q_BUSY) {
352 if (td_io_commit(td))
360 static inline void update_tv_cache(struct thread_data *td)
362 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
363 fio_gettime(&td->tv_cache, NULL);
367 * The main verify engine. Runs over the writes we previously submitted,
368 * reads the blocks back in, and checks the crc/md5 of the data.
370 static void do_verify(struct thread_data *td)
378 * sync io first and invalidate cache, to make sure we really
381 for_each_file(td, f, i) {
382 if (!(f->flags & FIO_FILE_OPEN))
384 if (fio_io_sync(td, f))
386 if (file_invalidate_cache(td, f))
393 td_set_runstate(td, TD_VERIFYING);
396 while (!td->terminate) {
399 io_u = __get_io_u(td);
405 if (runtime_exceeded(td, &td->tv_cache)) {
411 if (get_next_verify(td, io_u)) {
416 if (td_io_prep(td, io_u)) {
421 io_u->end_io = verify_io_u;
423 ret = td_io_queue(td, io_u);
425 case FIO_Q_COMPLETED:
428 else if (io_u->resid) {
429 int bytes = io_u->xfer_buflen - io_u->resid;
430 struct fio_file *f = io_u->file;
436 td_verror(td, EIO, "full resid");
441 io_u->xfer_buflen = io_u->resid;
442 io_u->xfer_buf += bytes;
443 io_u->offset += bytes;
445 td->ts.short_io_u[io_u->ddir]++;
447 if (io_u->offset == f->real_file_size)
450 requeue_io_u(td, &io_u);
453 ret = io_u_sync_complete(td, io_u);
461 requeue_io_u(td, &io_u);
462 ret2 = td_io_commit(td);
468 td_verror(td, -ret, "td_io_queue");
472 if (ret < 0 || td->error)
476 * if we can queue more, do so. but check if there are
477 * completed io_u's first.
479 full = queue_full(td) || ret == FIO_Q_BUSY;
480 if (full || !td->o.iodepth_batch_complete) {
481 min_events = td->o.iodepth_batch_complete;
482 if (full && !min_events)
487 * Reap required number of io units, if any,
488 * and do the verification on them through
489 * the callback handler
491 if (io_u_queued_complete(td, min_events) < 0) {
495 } while (full && (td->cur_depth > td->o.iodepth_low));
502 min_events = td->cur_depth;
505 ret = io_u_queued_complete(td, min_events);
507 cleanup_pending_aio(td);
509 td_set_runstate(td, TD_RUNNING);
513 * Main IO worker function. It retrieves io_u's to process and queues
514 * and reaps them, checking for rate and errors along the way.
516 static void do_io(struct thread_data *td)
522 if (in_ramp_time(td))
523 td_set_runstate(td, TD_RAMP);
525 td_set_runstate(td, TD_RUNNING);
527 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
528 struct timeval comp_time;
543 if (runtime_exceeded(td, &td->tv_cache)) {
550 * Add verification end_io handler, if asked to verify
551 * a previously written file.
553 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
554 io_u->end_io = verify_io_u;
555 td_set_runstate(td, TD_VERIFYING);
556 } else if (in_ramp_time(td))
557 td_set_runstate(td, TD_RAMP);
559 td_set_runstate(td, TD_RUNNING);
561 ret = td_io_queue(td, io_u);
563 case FIO_Q_COMPLETED:
566 else if (io_u->resid) {
567 int bytes = io_u->xfer_buflen - io_u->resid;
568 struct fio_file *f = io_u->file;
574 td_verror(td, EIO, "full resid");
579 io_u->xfer_buflen = io_u->resid;
580 io_u->xfer_buf += bytes;
581 io_u->offset += bytes;
583 td->ts.short_io_u[io_u->ddir]++;
585 if (io_u->offset == f->real_file_size)
588 requeue_io_u(td, &io_u);
591 if (should_check_rate(td))
592 fio_gettime(&comp_time, NULL);
594 bytes_done = io_u_sync_complete(td, io_u);
601 * if the engine doesn't have a commit hook,
602 * the io_u is really queued. if it does have such
603 * a hook, it has to call io_u_queued() itself.
605 if (td->io_ops->commit == NULL)
606 io_u_queued(td, io_u);
609 requeue_io_u(td, &io_u);
610 ret2 = td_io_commit(td);
620 if (ret < 0 || td->error)
624 * See if we need to complete some commands
626 full = queue_full(td) || ret == FIO_Q_BUSY;
627 if (full || !td->o.iodepth_batch_complete) {
628 min_evts = td->o.iodepth_batch_complete;
629 if (full && !min_evts)
632 if (should_check_rate(td))
633 fio_gettime(&comp_time, NULL);
636 ret = io_u_queued_complete(td, min_evts);
641 } while (full && (td->cur_depth > td->o.iodepth_low));
650 * the rate is batched for now, it should work for batches
651 * of completions except the very first one which may look
654 if (!in_ramp_time(td) && should_check_rate(td)) {
655 usec = utime_since(&td->tv_cache, &comp_time);
657 rate_throttle(td, usec, bytes_done);
659 if (check_min_rate(td, &comp_time)) {
660 if (exitall_on_terminate)
661 terminate_threads(td->groupid);
662 td_verror(td, EIO, "check_min_rate");
667 if (td->o.thinktime) {
668 unsigned long long b;
670 b = td->io_blocks[0] + td->io_blocks[1];
671 if (!(b % td->o.thinktime_blocks)) {
674 if (td->o.thinktime_spin)
675 __usec_sleep(td->o.thinktime_spin);
677 left = td->o.thinktime - td->o.thinktime_spin;
679 usec_sleep(td, left);
684 if (td->o.fill_device && td->error == ENOSPC) {
693 ret = io_u_queued_complete(td, i);
695 if (should_fsync(td) && td->o.end_fsync) {
696 td_set_runstate(td, TD_FSYNCING);
698 for_each_file(td, f, i) {
699 if (!(f->flags & FIO_FILE_OPEN))
705 cleanup_pending_aio(td);
708 * stop job if we failed doing any IO
710 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
714 static void cleanup_io_u(struct thread_data *td)
716 struct flist_head *entry, *n;
719 flist_for_each_safe(entry, n, &td->io_u_freelist) {
720 io_u = flist_entry(entry, struct io_u, list);
722 flist_del(&io_u->list);
729 static int init_io_u(struct thread_data *td)
736 max_units = td->o.iodepth;
737 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
738 td->orig_buffer_size = (unsigned long long) max_bs
739 * (unsigned long long) max_units;
741 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
744 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
745 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
748 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
749 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
753 if (allocate_io_mem(td))
757 p = ALIGN(td->orig_buffer);
761 for (i = 0; i < max_units; i++) {
764 io_u = malloc(sizeof(*io_u));
765 memset(io_u, 0, sizeof(*io_u));
766 INIT_FLIST_HEAD(&io_u->list);
768 if (!(td->io_ops->flags & FIO_NOIO)) {
769 io_u->buf = p + max_bs * i;
771 if (td_write(td) && !td->o.refill_buffers)
772 io_u_fill_buffer(td, io_u, max_bs);
776 io_u->flags = IO_U_F_FREE;
777 flist_add(&io_u->list, &td->io_u_freelist);
783 static int switch_ioscheduler(struct thread_data *td)
785 char tmp[256], tmp2[128];
789 if (td->io_ops->flags & FIO_DISKLESSIO)
792 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
794 f = fopen(tmp, "r+");
796 if (errno == ENOENT) {
797 log_err("fio: os or kernel doesn't support IO scheduler"
801 td_verror(td, errno, "fopen iosched");
808 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
809 if (ferror(f) || ret != 1) {
810 td_verror(td, errno, "fwrite");
818 * Read back and check that the selected scheduler is now the default.
820 ret = fread(tmp, 1, sizeof(tmp), f);
821 if (ferror(f) || ret < 0) {
822 td_verror(td, errno, "fread");
827 sprintf(tmp2, "[%s]", td->o.ioscheduler);
828 if (!strstr(tmp, tmp2)) {
829 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
830 td_verror(td, EINVAL, "iosched_switch");
839 static int keep_running(struct thread_data *td)
841 unsigned long long io_done;
845 if (td->o.time_based)
852 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
854 if (io_done < td->o.size)
860 static void reset_io_counters(struct thread_data *td)
862 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
863 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
867 td->rw_end_set[0] = td->rw_end_set[1] = 0;
869 td->last_was_sync = 0;
872 * reset file done count if we are to start over
874 if (td->o.time_based || td->o.loops)
875 td->nr_done_files = 0;
878 void reset_all_stats(struct thread_data *td)
883 reset_io_counters(td);
885 for (i = 0; i < 2; i++) {
887 td->io_blocks[i] = 0;
888 td->io_issues[i] = 0;
889 td->ts.total_io_u[i] = 0;
892 fio_gettime(&tv, NULL);
893 memcpy(&td->epoch, &tv, sizeof(tv));
894 memcpy(&td->start, &tv, sizeof(tv));
897 static int clear_io_state(struct thread_data *td)
903 reset_io_counters(td);
908 for_each_file(td, f, i) {
909 f->flags &= ~FIO_FILE_DONE;
910 ret = td_io_open_file(td, f);
919 * Entry point for the thread based jobs. The process based jobs end up
920 * here as well, after a little setup.
922 static void *thread_main(void *data)
924 unsigned long long runtime[2], elapsed;
925 struct thread_data *td = data;
928 if (!td->o.use_thread)
933 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
935 INIT_FLIST_HEAD(&td->io_u_freelist);
936 INIT_FLIST_HEAD(&td->io_u_busylist);
937 INIT_FLIST_HEAD(&td->io_u_requeues);
938 INIT_FLIST_HEAD(&td->io_log_list);
939 INIT_FLIST_HEAD(&td->io_hist_list);
940 td->io_hist_tree = RB_ROOT;
942 td_set_runstate(td, TD_INITIALIZED);
943 fio_mutex_up(startup_mutex);
944 fio_mutex_down(td->mutex);
947 * the ->mutex mutex is now no longer used, close it to avoid
948 * eating a file descriptor
950 fio_mutex_remove(td->mutex);
953 * May alter parameters that init_io_u() will use, so we need to
962 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
963 td_verror(td, errno, "cpu_set_affinity");
967 if (td->ioprio_set) {
968 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
969 td_verror(td, errno, "ioprio_set");
974 if (nice(td->o.nice) == -1) {
975 td_verror(td, errno, "nice");
979 if (td->o.ioscheduler && switch_ioscheduler(td))
982 if (!td->o.create_serialize && setup_files(td))
991 if (init_random_map(td))
994 if (td->o.exec_prerun) {
995 if (system(td->o.exec_prerun) < 0)
999 fio_gettime(&td->epoch, NULL);
1000 getrusage(RUSAGE_SELF, &td->ts.ru_start);
1002 runtime[0] = runtime[1] = 0;
1004 while (keep_running(td)) {
1005 fio_gettime(&td->start, NULL);
1006 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
1007 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1010 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1011 sizeof(td->lastrate));
1013 if (clear_state && clear_io_state(td))
1016 prune_io_piece_log(td);
1022 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1023 if (td->rw_end_set[DDIR_READ])
1024 elapsed = utime_since(&td->start,
1025 &td->rw_end[DDIR_READ]);
1027 elapsed = utime_since_now(&td->start);
1029 runtime[DDIR_READ] += elapsed;
1031 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1032 if (td->rw_end_set[DDIR_WRITE])
1033 elapsed = utime_since(&td->start,
1034 &td->rw_end[DDIR_WRITE]);
1036 elapsed = utime_since_now(&td->start);
1038 runtime[DDIR_WRITE] += elapsed;
1041 if (td->error || td->terminate)
1044 if (!td->o.do_verify ||
1045 td->o.verify == VERIFY_NONE ||
1046 (td->io_ops->flags & FIO_UNIDIR))
1049 if (clear_io_state(td))
1052 fio_gettime(&td->start, NULL);
1056 runtime[DDIR_READ] += utime_since_now(&td->start);
1058 if (td->error || td->terminate)
1062 update_rusage_stat(td);
1063 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1064 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1065 td->ts.total_run_time = mtime_since_now(&td->epoch);
1066 td->ts.io_bytes[0] = td->io_bytes[0];
1067 td->ts.io_bytes[1] = td->io_bytes[1];
1069 if (td->ts.bw_log) {
1070 if (td->o.bw_log_file)
1071 finish_log_named(td, td->ts.bw_log, td->o.bw_log_file, "bw");
1073 finish_log(td, td->ts.bw_log, "bw");
1075 if (td->ts.slat_log) {
1076 if (td->o.lat_log_file)
1077 finish_log_named(td, td->ts.slat_log, td->o.lat_log_file, "clat");
1079 finish_log(td, td->ts.slat_log, "slat");
1081 if (td->ts.clat_log) {
1082 if (td->o.lat_log_file)
1083 finish_log_named(td, td->ts.clat_log, td->o.lat_log_file, "clat");
1085 finish_log(td, td->ts.clat_log, "clat");
1087 if (td->o.exec_postrun) {
1088 if (system(td->o.exec_postrun) < 0)
1089 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
1092 if (exitall_on_terminate)
1093 terminate_threads(td->groupid);
1097 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1099 close_and_free_files(td);
1104 * do this very late, it will log file closing as well
1106 if (td->o.write_iolog_file)
1107 write_iolog_close(td);
1109 options_mem_free(td);
1110 td_set_runstate(td, TD_EXITED);
1111 return (void *) (unsigned long) td->error;
1115 * We cannot pass the td data into a forked process, so attach the td and
1116 * pass it to the thread worker.
1118 static int fork_main(int shmid, int offset)
1120 struct thread_data *td;
1123 data = shmat(shmid, NULL, 0);
1124 if (data == (void *) -1) {
1131 td = data + offset * sizeof(struct thread_data);
1132 ret = thread_main(td);
1134 return (int) (unsigned long) ret;
1138 * Run over the job map and reap the threads that have exited, if any.
1140 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1142 struct thread_data *td;
1143 int i, cputhreads, realthreads, pending, status, ret;
1146 * reap exited threads (TD_EXITED -> TD_REAPED)
1148 realthreads = pending = cputhreads = 0;
1149 for_each_td(td, i) {
1153 * ->io_ops is NULL for a thread that has closed its
1156 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1165 if (td->runstate == TD_REAPED)
1167 if (td->o.use_thread) {
1168 if (td->runstate == TD_EXITED) {
1169 td_set_runstate(td, TD_REAPED);
1176 if (td->runstate == TD_EXITED)
1180 * check if someone quit or got killed in an unusual way
1182 ret = waitpid(td->pid, &status, flags);
1184 if (errno == ECHILD) {
1185 log_err("fio: pid=%d disappeared %d\n",
1186 (int) td->pid, td->runstate);
1187 td_set_runstate(td, TD_REAPED);
1191 } else if (ret == td->pid) {
1192 if (WIFSIGNALED(status)) {
1193 int sig = WTERMSIG(status);
1196 log_err("fio: pid=%d, got signal=%d\n",
1197 (int) td->pid, sig);
1198 td_set_runstate(td, TD_REAPED);
1201 if (WIFEXITED(status)) {
1202 if (WEXITSTATUS(status) && !td->error)
1203 td->error = WEXITSTATUS(status);
1205 td_set_runstate(td, TD_REAPED);
1211 * thread is not dead, continue
1217 (*m_rate) -= td->o.ratemin;
1218 (*t_rate) -= td->o.rate;
1225 done_secs += mtime_since_now(&td->epoch) / 1000;
1228 if (*nr_running == cputhreads && !pending && realthreads)
1229 terminate_threads(TERMINATE_ALL);
1233 * Main function for kicking off and reaping jobs, as needed.
1235 static void run_threads(void)
1237 struct thread_data *td;
1238 unsigned long spent;
1239 int i, todo, nr_running, m_rate, t_rate, nr_started;
1241 if (fio_pin_memory())
1244 if (!terse_output) {
1245 printf("Starting ");
1247 printf("%d thread%s", nr_thread,
1248 nr_thread > 1 ? "s" : "");
1252 printf("%d process%s", nr_process,
1253 nr_process > 1 ? "es" : "");
1261 todo = thread_number;
1264 m_rate = t_rate = 0;
1266 for_each_td(td, i) {
1267 print_status_init(td->thread_number - 1);
1269 if (!td->o.create_serialize) {
1275 * do file setup here so it happens sequentially,
1276 * we don't want X number of threads getting their
1277 * client data interspersed on disk
1279 if (setup_files(td)) {
1282 log_err("fio: pid=%d, err=%d/%s\n",
1283 (int) td->pid, td->error, td->verror);
1284 td_set_runstate(td, TD_REAPED);
1291 * for sharing to work, each job must always open
1292 * its own files. so close them, if we opened them
1295 for_each_file(td, f, i)
1296 td_io_close_file(td, f);
1305 struct thread_data *map[MAX_JOBS];
1306 struct timeval this_start;
1307 int this_jobs = 0, left;
1310 * create threads (TD_NOT_CREATED -> TD_CREATED)
1312 for_each_td(td, i) {
1313 if (td->runstate != TD_NOT_CREATED)
1317 * never got a chance to start, killed by other
1318 * thread for some reason
1320 if (td->terminate) {
1325 if (td->o.start_delay) {
1326 spent = mtime_since_genesis();
1328 if (td->o.start_delay * 1000 > spent)
1332 if (td->o.stonewall && (nr_started || nr_running)) {
1333 dprint(FD_PROCESS, "%s: stonewall wait\n",
1339 * Set state to created. Thread will transition
1340 * to TD_INITIALIZED when it's done setting up.
1342 td_set_runstate(td, TD_CREATED);
1343 map[this_jobs++] = td;
1346 if (td->o.use_thread) {
1347 dprint(FD_PROCESS, "will pthread_create\n");
1348 if (pthread_create(&td->thread, NULL,
1350 perror("pthread_create");
1354 if (pthread_detach(td->thread) < 0)
1355 perror("pthread_detach");
1358 dprint(FD_PROCESS, "will fork\n");
1361 int ret = fork_main(shm_id, i);
1364 } else if (i == fio_debug_jobno)
1365 *fio_debug_jobp = pid;
1367 fio_mutex_down(startup_mutex);
1371 * Wait for the started threads to transition to
1374 fio_gettime(&this_start, NULL);
1376 while (left && !fio_abort) {
1377 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1382 for (i = 0; i < this_jobs; i++) {
1386 if (td->runstate == TD_INITIALIZED) {
1389 } else if (td->runstate >= TD_EXITED) {
1393 nr_running++; /* work-around... */
1399 log_err("fio: %d jobs failed to start\n", left);
1400 for (i = 0; i < this_jobs; i++) {
1404 kill(td->pid, SIGTERM);
1410 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1412 for_each_td(td, i) {
1413 if (td->runstate != TD_INITIALIZED)
1416 if (in_ramp_time(td))
1417 td_set_runstate(td, TD_RAMP);
1419 td_set_runstate(td, TD_RUNNING);
1422 m_rate += td->o.ratemin;
1423 t_rate += td->o.rate;
1425 fio_mutex_up(td->mutex);
1428 reap_threads(&nr_running, &t_rate, &m_rate);
1434 while (nr_running) {
1435 reap_threads(&nr_running, &t_rate, &m_rate);
1443 int main(int argc, char *argv[])
1450 * We need locale for number printing, if it isn't set then just
1451 * go with the US format.
1453 if (!getenv("LC_NUMERIC"))
1454 setlocale(LC_NUMERIC, "en_US");
1456 if (parse_options(argc, argv))
1462 ps = sysconf(_SC_PAGESIZE);
1464 log_err("Failed to get page size\n");
1472 setup_log(&agg_io_log[DDIR_READ]);
1473 setup_log(&agg_io_log[DDIR_WRITE]);
1476 startup_mutex = fio_mutex_init(0);
1487 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1488 __finish_log(agg_io_log[DDIR_WRITE],
1489 "agg-write_bw.log");
1493 fio_mutex_remove(startup_mutex);