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)
175 * No minimum rate set, always ok
177 if (!td->o.ratemin && !td->o.rate_iops_min)
184 * Check if we are above the minimum rate given.
186 static int check_min_rate(struct thread_data *td, struct timeval *now)
188 unsigned long long bytes = 0;
189 unsigned long iops = 0;
194 * allow a 2 second settle period in the beginning
196 if (mtime_since(&td->start, now) < 2000)
200 iops += td->io_blocks[DDIR_READ];
201 bytes += td->this_io_bytes[DDIR_READ];
204 iops += td->io_blocks[DDIR_WRITE];
205 bytes += td->this_io_bytes[DDIR_WRITE];
209 * if rate blocks is set, sample is running
211 if (td->rate_bytes || td->rate_blocks) {
212 spent = mtime_since(&td->lastrate, now);
213 if (spent < td->o.ratecycle)
218 * check bandwidth specified rate
220 if (bytes < td->rate_bytes) {
221 log_err("%s: min rate %u not met\n", td->o.name,
225 rate = (bytes - td->rate_bytes) / spent;
226 if (rate < td->o.ratemin ||
227 bytes < td->rate_bytes) {
228 log_err("%s: min rate %u not met, got"
229 " %luKiB/sec\n", td->o.name,
230 td->o.ratemin, rate);
236 * checks iops specified rate
238 if (iops < td->o.rate_iops) {
239 log_err("%s: min iops rate %u not met\n",
240 td->o.name, td->o.rate_iops);
243 rate = (iops - td->rate_blocks) / spent;
244 if (rate < td->o.rate_iops_min ||
245 iops < td->rate_blocks) {
246 log_err("%s: min iops rate %u not met,"
247 " got %lu\n", td->o.name,
255 td->rate_bytes = bytes;
256 td->rate_blocks = iops;
257 memcpy(&td->lastrate, now, sizeof(*now));
261 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
265 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
272 * When job exits, we can cancel the in-flight IO if we are using async
273 * io. Attempt to do so.
275 static void cleanup_pending_aio(struct thread_data *td)
277 struct flist_head *entry, *n;
282 * get immediately available events, if any
284 r = io_u_queued_complete(td, 0);
289 * now cancel remaining active events
291 if (td->io_ops->cancel) {
292 flist_for_each_safe(entry, n, &td->io_u_busylist) {
293 io_u = flist_entry(entry, struct io_u, list);
296 * if the io_u isn't in flight, then that generally
297 * means someone leaked an io_u. complain but fix
298 * it up, so we don't stall here.
300 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
301 log_err("fio: non-busy IO on busy list\n");
304 r = td->io_ops->cancel(td, io_u);
312 r = io_u_queued_complete(td, td->cur_depth);
316 * Helper to handle the final sync of a file. Works just like the normal
317 * io path, just does everything sync.
319 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
321 struct io_u *io_u = __get_io_u(td);
327 io_u->ddir = DDIR_SYNC;
330 if (td_io_prep(td, io_u)) {
336 ret = td_io_queue(td, io_u);
338 td_verror(td, io_u->error, "td_io_queue");
341 } else if (ret == FIO_Q_QUEUED) {
342 if (io_u_queued_complete(td, 1) < 0)
344 } else if (ret == FIO_Q_COMPLETED) {
346 td_verror(td, io_u->error, "td_io_queue");
350 if (io_u_sync_complete(td, io_u) < 0)
352 } else if (ret == FIO_Q_BUSY) {
353 if (td_io_commit(td))
361 static inline void update_tv_cache(struct thread_data *td)
363 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
364 fio_gettime(&td->tv_cache, NULL);
368 * The main verify engine. Runs over the writes we previously submitted,
369 * reads the blocks back in, and checks the crc/md5 of the data.
371 static void do_verify(struct thread_data *td)
379 * sync io first and invalidate cache, to make sure we really
382 for_each_file(td, f, i) {
383 if (!(f->flags & FIO_FILE_OPEN))
385 if (fio_io_sync(td, f))
387 if (file_invalidate_cache(td, f))
394 td_set_runstate(td, TD_VERIFYING);
397 while (!td->terminate) {
400 io_u = __get_io_u(td);
406 if (runtime_exceeded(td, &td->tv_cache)) {
412 if (get_next_verify(td, io_u)) {
417 if (td_io_prep(td, io_u)) {
422 io_u->end_io = verify_io_u;
424 ret = td_io_queue(td, io_u);
426 case FIO_Q_COMPLETED:
429 else if (io_u->resid) {
430 int bytes = io_u->xfer_buflen - io_u->resid;
431 struct fio_file *f = io_u->file;
437 td_verror(td, EIO, "full resid");
442 io_u->xfer_buflen = io_u->resid;
443 io_u->xfer_buf += bytes;
444 io_u->offset += bytes;
446 td->ts.short_io_u[io_u->ddir]++;
448 if (io_u->offset == f->real_file_size)
451 requeue_io_u(td, &io_u);
454 ret = io_u_sync_complete(td, io_u);
462 requeue_io_u(td, &io_u);
463 ret2 = td_io_commit(td);
469 td_verror(td, -ret, "td_io_queue");
473 if (ret < 0 || td->error)
477 * if we can queue more, do so. but check if there are
478 * completed io_u's first.
480 full = queue_full(td) || ret == FIO_Q_BUSY;
481 if (full || !td->o.iodepth_batch_complete) {
482 min_events = td->o.iodepth_batch_complete;
483 if (full && !min_events)
488 * Reap required number of io units, if any,
489 * and do the verification on them through
490 * the callback handler
492 if (io_u_queued_complete(td, min_events) < 0) {
496 } while (full && (td->cur_depth > td->o.iodepth_low));
503 min_events = td->cur_depth;
506 ret = io_u_queued_complete(td, min_events);
508 cleanup_pending_aio(td);
510 td_set_runstate(td, TD_RUNNING);
514 * Main IO worker function. It retrieves io_u's to process and queues
515 * and reaps them, checking for rate and errors along the way.
517 static void do_io(struct thread_data *td)
523 if (in_ramp_time(td))
524 td_set_runstate(td, TD_RAMP);
526 td_set_runstate(td, TD_RUNNING);
528 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
529 struct timeval comp_time;
544 if (runtime_exceeded(td, &td->tv_cache)) {
551 * Add verification end_io handler, if asked to verify
552 * a previously written file.
554 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
555 io_u->end_io = verify_io_u;
556 td_set_runstate(td, TD_VERIFYING);
557 } else if (in_ramp_time(td))
558 td_set_runstate(td, TD_RAMP);
560 td_set_runstate(td, TD_RUNNING);
562 ret = td_io_queue(td, io_u);
564 case FIO_Q_COMPLETED:
567 else if (io_u->resid) {
568 int bytes = io_u->xfer_buflen - io_u->resid;
569 struct fio_file *f = io_u->file;
575 td_verror(td, EIO, "full resid");
580 io_u->xfer_buflen = io_u->resid;
581 io_u->xfer_buf += bytes;
582 io_u->offset += bytes;
584 td->ts.short_io_u[io_u->ddir]++;
586 if (io_u->offset == f->real_file_size)
589 requeue_io_u(td, &io_u);
592 if (should_check_rate(td))
593 fio_gettime(&comp_time, NULL);
595 bytes_done = io_u_sync_complete(td, io_u);
602 * if the engine doesn't have a commit hook,
603 * the io_u is really queued. if it does have such
604 * a hook, it has to call io_u_queued() itself.
606 if (td->io_ops->commit == NULL)
607 io_u_queued(td, io_u);
610 requeue_io_u(td, &io_u);
611 ret2 = td_io_commit(td);
621 if (ret < 0 || td->error)
625 * See if we need to complete some commands
627 full = queue_full(td) || ret == FIO_Q_BUSY;
628 if (full || !td->o.iodepth_batch_complete) {
629 min_evts = td->o.iodepth_batch_complete;
630 if (full && !min_evts)
633 if (should_check_rate(td))
634 fio_gettime(&comp_time, NULL);
637 ret = io_u_queued_complete(td, min_evts);
642 } while (full && (td->cur_depth > td->o.iodepth_low));
651 * the rate is batched for now, it should work for batches
652 * of completions except the very first one which may look
655 if (!in_ramp_time(td) && should_check_rate(td)) {
656 usec = utime_since(&td->tv_cache, &comp_time);
658 rate_throttle(td, usec, bytes_done);
660 if (check_min_rate(td, &comp_time)) {
661 if (exitall_on_terminate)
662 terminate_threads(td->groupid);
663 td_verror(td, EIO, "check_min_rate");
668 if (td->o.thinktime) {
669 unsigned long long b;
671 b = td->io_blocks[0] + td->io_blocks[1];
672 if (!(b % td->o.thinktime_blocks)) {
675 if (td->o.thinktime_spin)
676 usec_spin(td->o.thinktime_spin);
678 left = td->o.thinktime - td->o.thinktime_spin;
680 usec_sleep(td, left);
685 if (td->o.fill_device && td->error == ENOSPC) {
694 ret = io_u_queued_complete(td, i);
696 if (should_fsync(td) && td->o.end_fsync) {
697 td_set_runstate(td, TD_FSYNCING);
699 for_each_file(td, f, i) {
700 if (!(f->flags & FIO_FILE_OPEN))
706 cleanup_pending_aio(td);
709 * stop job if we failed doing any IO
711 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
715 static void cleanup_io_u(struct thread_data *td)
717 struct flist_head *entry, *n;
720 flist_for_each_safe(entry, n, &td->io_u_freelist) {
721 io_u = flist_entry(entry, struct io_u, list);
723 flist_del(&io_u->list);
730 static int init_io_u(struct thread_data *td)
737 max_units = td->o.iodepth;
738 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
739 td->orig_buffer_size = (unsigned long long) max_bs
740 * (unsigned long long) max_units;
742 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
745 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
746 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
749 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
750 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
754 if (allocate_io_mem(td))
758 p = ALIGN(td->orig_buffer);
762 for (i = 0; i < max_units; i++) {
765 io_u = malloc(sizeof(*io_u));
766 memset(io_u, 0, sizeof(*io_u));
767 INIT_FLIST_HEAD(&io_u->list);
769 if (!(td->io_ops->flags & FIO_NOIO)) {
770 io_u->buf = p + max_bs * i;
772 if (td_write(td) && !td->o.refill_buffers)
773 io_u_fill_buffer(td, io_u, max_bs);
777 io_u->flags = IO_U_F_FREE;
778 flist_add(&io_u->list, &td->io_u_freelist);
784 static int switch_ioscheduler(struct thread_data *td)
786 char tmp[256], tmp2[128];
790 if (td->io_ops->flags & FIO_DISKLESSIO)
793 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
795 f = fopen(tmp, "r+");
797 if (errno == ENOENT) {
798 log_err("fio: os or kernel doesn't support IO scheduler"
802 td_verror(td, errno, "fopen iosched");
809 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
810 if (ferror(f) || ret != 1) {
811 td_verror(td, errno, "fwrite");
819 * Read back and check that the selected scheduler is now the default.
821 ret = fread(tmp, 1, sizeof(tmp), f);
822 if (ferror(f) || ret < 0) {
823 td_verror(td, errno, "fread");
828 sprintf(tmp2, "[%s]", td->o.ioscheduler);
829 if (!strstr(tmp, tmp2)) {
830 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
831 td_verror(td, EINVAL, "iosched_switch");
840 static int keep_running(struct thread_data *td)
842 unsigned long long io_done;
846 if (td->o.time_based)
853 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
855 if (io_done < td->o.size)
861 static void reset_io_counters(struct thread_data *td)
863 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
864 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
868 td->rw_end_set[0] = td->rw_end_set[1] = 0;
870 td->last_was_sync = 0;
873 * reset file done count if we are to start over
875 if (td->o.time_based || td->o.loops)
876 td->nr_done_files = 0;
879 void reset_all_stats(struct thread_data *td)
884 reset_io_counters(td);
886 for (i = 0; i < 2; i++) {
888 td->io_blocks[i] = 0;
889 td->io_issues[i] = 0;
890 td->ts.total_io_u[i] = 0;
893 fio_gettime(&tv, NULL);
894 memcpy(&td->epoch, &tv, sizeof(tv));
895 memcpy(&td->start, &tv, sizeof(tv));
898 static int clear_io_state(struct thread_data *td)
904 reset_io_counters(td);
909 for_each_file(td, f, i) {
910 f->flags &= ~FIO_FILE_DONE;
911 ret = td_io_open_file(td, f);
920 * Entry point for the thread based jobs. The process based jobs end up
921 * here as well, after a little setup.
923 static void *thread_main(void *data)
925 unsigned long long runtime[2], elapsed;
926 struct thread_data *td = data;
929 if (!td->o.use_thread)
934 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
936 INIT_FLIST_HEAD(&td->io_u_freelist);
937 INIT_FLIST_HEAD(&td->io_u_busylist);
938 INIT_FLIST_HEAD(&td->io_u_requeues);
939 INIT_FLIST_HEAD(&td->io_log_list);
940 INIT_FLIST_HEAD(&td->io_hist_list);
941 td->io_hist_tree = RB_ROOT;
943 td_set_runstate(td, TD_INITIALIZED);
944 dprint(FD_MUTEX, "up startup_mutex\n");
945 fio_mutex_up(startup_mutex);
946 dprint(FD_MUTEX, "wait on td->mutex\n");
947 fio_mutex_down(td->mutex);
948 dprint(FD_MUTEX, "done waiting on td->mutex\n");
951 * the ->mutex mutex is now no longer used, close it to avoid
952 * eating a file descriptor
954 fio_mutex_remove(td->mutex);
957 * May alter parameters that init_io_u() will use, so we need to
966 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
967 td_verror(td, errno, "cpu_set_affinity");
972 * If we have a gettimeofday() thread, make sure we exclude that
973 * thread from this job
975 if (td->o.gtod_cpu) {
976 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
977 if (fio_setaffinity(td) == -1) {
978 td_verror(td, errno, "cpu_set_affinity");
983 if (td->ioprio_set) {
984 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
985 td_verror(td, errno, "ioprio_set");
990 if (nice(td->o.nice) == -1) {
991 td_verror(td, errno, "nice");
995 if (td->o.ioscheduler && switch_ioscheduler(td))
998 if (!td->o.create_serialize && setup_files(td))
1007 if (init_random_map(td))
1010 if (td->o.exec_prerun) {
1011 if (system(td->o.exec_prerun) < 0)
1015 fio_gettime(&td->epoch, NULL);
1016 getrusage(RUSAGE_SELF, &td->ts.ru_start);
1018 runtime[0] = runtime[1] = 0;
1020 while (keep_running(td)) {
1021 fio_gettime(&td->start, NULL);
1022 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
1023 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1026 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1027 sizeof(td->lastrate));
1029 if (clear_state && clear_io_state(td))
1032 prune_io_piece_log(td);
1038 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1039 if (td->rw_end_set[DDIR_READ])
1040 elapsed = utime_since(&td->start,
1041 &td->rw_end[DDIR_READ]);
1043 elapsed = utime_since_now(&td->start);
1045 runtime[DDIR_READ] += elapsed;
1047 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1048 if (td->rw_end_set[DDIR_WRITE])
1049 elapsed = utime_since(&td->start,
1050 &td->rw_end[DDIR_WRITE]);
1052 elapsed = utime_since_now(&td->start);
1054 runtime[DDIR_WRITE] += elapsed;
1057 if (td->error || td->terminate)
1060 if (!td->o.do_verify ||
1061 td->o.verify == VERIFY_NONE ||
1062 (td->io_ops->flags & FIO_UNIDIR))
1065 if (clear_io_state(td))
1068 fio_gettime(&td->start, NULL);
1072 runtime[DDIR_READ] += utime_since_now(&td->start);
1074 if (td->error || td->terminate)
1078 update_rusage_stat(td);
1079 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1080 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1081 td->ts.total_run_time = mtime_since_now(&td->epoch);
1082 td->ts.io_bytes[0] = td->io_bytes[0];
1083 td->ts.io_bytes[1] = td->io_bytes[1];
1085 if (td->ts.bw_log) {
1086 if (td->o.bw_log_file) {
1087 finish_log_named(td, td->ts.bw_log,
1088 td->o.bw_log_file, "bw");
1090 finish_log(td, td->ts.bw_log, "bw");
1092 if (td->ts.slat_log) {
1093 if (td->o.lat_log_file) {
1094 finish_log_named(td, td->ts.slat_log,
1095 td->o.lat_log_file, "clat");
1097 finish_log(td, td->ts.slat_log, "slat");
1099 if (td->ts.clat_log) {
1100 if (td->o.lat_log_file) {
1101 finish_log_named(td, td->ts.clat_log,
1102 td->o.lat_log_file, "clat");
1104 finish_log(td, td->ts.clat_log, "clat");
1106 if (td->o.exec_postrun) {
1107 if (system(td->o.exec_postrun) < 0)
1108 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
1111 if (exitall_on_terminate)
1112 terminate_threads(td->groupid);
1116 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1118 close_and_free_files(td);
1122 if (td->o.cpumask_set) {
1123 int ret = fio_cpuset_exit(&td->o.cpumask);
1125 td_verror(td, ret, "fio_cpuset_exit");
1129 * do this very late, it will log file closing as well
1131 if (td->o.write_iolog_file)
1132 write_iolog_close(td);
1134 options_mem_free(td);
1135 td_set_runstate(td, TD_EXITED);
1136 return (void *) (unsigned long) td->error;
1140 * We cannot pass the td data into a forked process, so attach the td and
1141 * pass it to the thread worker.
1143 static int fork_main(int shmid, int offset)
1145 struct thread_data *td;
1148 data = shmat(shmid, NULL, 0);
1149 if (data == (void *) -1) {
1156 td = data + offset * sizeof(struct thread_data);
1157 ret = thread_main(td);
1159 return (int) (unsigned long) ret;
1163 * Run over the job map and reap the threads that have exited, if any.
1165 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1167 struct thread_data *td;
1168 int i, cputhreads, realthreads, pending, status, ret;
1171 * reap exited threads (TD_EXITED -> TD_REAPED)
1173 realthreads = pending = cputhreads = 0;
1174 for_each_td(td, i) {
1178 * ->io_ops is NULL for a thread that has closed its
1181 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1190 if (td->runstate == TD_REAPED)
1192 if (td->o.use_thread) {
1193 if (td->runstate == TD_EXITED) {
1194 td_set_runstate(td, TD_REAPED);
1201 if (td->runstate == TD_EXITED)
1205 * check if someone quit or got killed in an unusual way
1207 ret = waitpid(td->pid, &status, flags);
1209 if (errno == ECHILD) {
1210 log_err("fio: pid=%d disappeared %d\n",
1211 (int) td->pid, td->runstate);
1212 td_set_runstate(td, TD_REAPED);
1216 } else if (ret == td->pid) {
1217 if (WIFSIGNALED(status)) {
1218 int sig = WTERMSIG(status);
1221 log_err("fio: pid=%d, got signal=%d\n",
1222 (int) td->pid, sig);
1223 td_set_runstate(td, TD_REAPED);
1226 if (WIFEXITED(status)) {
1227 if (WEXITSTATUS(status) && !td->error)
1228 td->error = WEXITSTATUS(status);
1230 td_set_runstate(td, TD_REAPED);
1236 * thread is not dead, continue
1242 (*m_rate) -= td->o.ratemin;
1243 (*t_rate) -= td->o.rate;
1250 done_secs += mtime_since_now(&td->epoch) / 1000;
1253 if (*nr_running == cputhreads && !pending && realthreads)
1254 terminate_threads(TERMINATE_ALL);
1257 static void *gtod_thread_main(void *data)
1259 fio_mutex_up(startup_mutex);
1262 * As long as we have jobs around, update the clock. It would be nice
1263 * to have some way of NOT hammering that CPU with gettimeofday(),
1264 * but I'm not sure what to use outside of a simple CPU nop to relax
1265 * it - we don't want to lose precision.
1275 static int fio_start_gtod_thread(void)
1279 ret = pthread_create(>od_thread, NULL, gtod_thread_main, NULL);
1281 log_err("Can't create gtod thread: %s\n", strerror(ret));
1285 ret = pthread_detach(gtod_thread);
1287 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
1291 dprint(FD_MUTEX, "wait on startup_mutex\n");
1292 fio_mutex_down(startup_mutex);
1293 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1298 * Main function for kicking off and reaping jobs, as needed.
1300 static void run_threads(void)
1302 struct thread_data *td;
1303 unsigned long spent;
1304 int i, todo, nr_running, m_rate, t_rate, nr_started;
1306 if (fio_pin_memory())
1309 if (fio_gtod_offload && fio_start_gtod_thread())
1312 if (!terse_output) {
1313 printf("Starting ");
1315 printf("%d thread%s", nr_thread,
1316 nr_thread > 1 ? "s" : "");
1320 printf("%d process%s", nr_process,
1321 nr_process > 1 ? "es" : "");
1329 todo = thread_number;
1332 m_rate = t_rate = 0;
1334 for_each_td(td, i) {
1335 print_status_init(td->thread_number - 1);
1337 if (!td->o.create_serialize) {
1343 * do file setup here so it happens sequentially,
1344 * we don't want X number of threads getting their
1345 * client data interspersed on disk
1347 if (setup_files(td)) {
1350 log_err("fio: pid=%d, err=%d/%s\n",
1351 (int) td->pid, td->error, td->verror);
1352 td_set_runstate(td, TD_REAPED);
1359 * for sharing to work, each job must always open
1360 * its own files. so close them, if we opened them
1363 for_each_file(td, f, i)
1364 td_io_close_file(td, f);
1373 struct thread_data *map[MAX_JOBS];
1374 struct timeval this_start;
1375 int this_jobs = 0, left;
1378 * create threads (TD_NOT_CREATED -> TD_CREATED)
1380 for_each_td(td, i) {
1381 if (td->runstate != TD_NOT_CREATED)
1385 * never got a chance to start, killed by other
1386 * thread for some reason
1388 if (td->terminate) {
1393 if (td->o.start_delay) {
1394 spent = mtime_since_genesis();
1396 if (td->o.start_delay * 1000 > spent)
1400 if (td->o.stonewall && (nr_started || nr_running)) {
1401 dprint(FD_PROCESS, "%s: stonewall wait\n",
1407 * Set state to created. Thread will transition
1408 * to TD_INITIALIZED when it's done setting up.
1410 td_set_runstate(td, TD_CREATED);
1411 map[this_jobs++] = td;
1414 if (td->o.use_thread) {
1417 dprint(FD_PROCESS, "will pthread_create\n");
1418 ret = pthread_create(&td->thread, NULL,
1421 log_err("pthread_create: %s\n",
1426 ret = pthread_detach(td->thread);
1428 log_err("pthread_detach: %s",
1432 dprint(FD_PROCESS, "will fork\n");
1435 int ret = fork_main(shm_id, i);
1438 } else if (i == fio_debug_jobno)
1439 *fio_debug_jobp = pid;
1441 dprint(FD_MUTEX, "wait on startup_mutex\n");
1442 fio_mutex_down(startup_mutex);
1443 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1447 * Wait for the started threads to transition to
1450 fio_gettime(&this_start, NULL);
1452 while (left && !fio_abort) {
1453 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1458 for (i = 0; i < this_jobs; i++) {
1462 if (td->runstate == TD_INITIALIZED) {
1465 } else if (td->runstate >= TD_EXITED) {
1469 nr_running++; /* work-around... */
1475 log_err("fio: %d jobs failed to start\n", left);
1476 for (i = 0; i < this_jobs; i++) {
1480 kill(td->pid, SIGTERM);
1486 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1488 for_each_td(td, i) {
1489 if (td->runstate != TD_INITIALIZED)
1492 if (in_ramp_time(td))
1493 td_set_runstate(td, TD_RAMP);
1495 td_set_runstate(td, TD_RUNNING);
1498 m_rate += td->o.ratemin;
1499 t_rate += td->o.rate;
1501 fio_mutex_up(td->mutex);
1504 reap_threads(&nr_running, &t_rate, &m_rate);
1510 while (nr_running) {
1511 reap_threads(&nr_running, &t_rate, &m_rate);
1519 int main(int argc, char *argv[])
1526 * We need locale for number printing, if it isn't set then just
1527 * go with the US format.
1529 if (!getenv("LC_NUMERIC"))
1530 setlocale(LC_NUMERIC, "en_US");
1532 if (parse_options(argc, argv))
1538 ps = sysconf(_SC_PAGESIZE);
1540 log_err("Failed to get page size\n");
1548 setup_log(&agg_io_log[DDIR_READ]);
1549 setup_log(&agg_io_log[DDIR_WRITE]);
1552 startup_mutex = fio_mutex_init(0);
1563 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1564 __finish_log(agg_io_log[DDIR_WRITE],
1565 "agg-write_bw.log");
1569 fio_mutex_remove(startup_mutex);