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();
119 static void sig_int(int sig)
122 printf("\nfio: terminating on signal %d\n", sig);
124 terminate_threads(TERMINATE_ALL);
128 static void sig_ill(int fio_unused sig)
133 log_err("fio: illegal instruction. your cpu does not support "
134 "the sse4.2 instruction for crc32c\n");
135 terminate_threads(TERMINATE_ALL);
139 static void set_sig_handlers(void)
141 struct sigaction act;
143 memset(&act, 0, sizeof(act));
144 act.sa_handler = sig_alrm;
145 act.sa_flags = SA_RESTART;
146 sigaction(SIGALRM, &act, NULL);
148 memset(&act, 0, sizeof(act));
149 act.sa_handler = sig_int;
150 act.sa_flags = SA_RESTART;
151 sigaction(SIGINT, &act, NULL);
153 memset(&act, 0, sizeof(act));
154 act.sa_handler = sig_ill;
155 act.sa_flags = SA_RESTART;
156 sigaction(SIGILL, &act, NULL);
160 * Check if we are above the minimum rate given.
162 static int check_min_rate(struct thread_data *td, struct timeval *now)
164 unsigned long long bytes = 0;
165 unsigned long iops = 0;
170 * No minimum rate set, always ok
172 if (!td->o.ratemin && !td->o.rate_iops_min)
176 * allow a 2 second settle period in the beginning
178 if (mtime_since(&td->start, now) < 2000)
182 iops += td->io_blocks[DDIR_READ];
183 bytes += td->this_io_bytes[DDIR_READ];
186 iops += td->io_blocks[DDIR_WRITE];
187 bytes += td->this_io_bytes[DDIR_WRITE];
191 * if rate blocks is set, sample is running
193 if (td->rate_bytes || td->rate_blocks) {
194 spent = mtime_since(&td->lastrate, now);
195 if (spent < td->o.ratecycle)
200 * check bandwidth specified rate
202 if (bytes < td->rate_bytes) {
203 log_err("%s: min rate %u not met\n", td->o.name,
207 rate = (bytes - td->rate_bytes) / spent;
208 if (rate < td->o.ratemin ||
209 bytes < td->rate_bytes) {
210 log_err("%s: min rate %u not met, got"
211 " %luKiB/sec\n", td->o.name,
212 td->o.ratemin, rate);
218 * checks iops specified rate
220 if (iops < td->o.rate_iops) {
221 log_err("%s: min iops rate %u not met\n",
222 td->o.name, td->o.rate_iops);
225 rate = (iops - td->rate_blocks) / spent;
226 if (rate < td->o.rate_iops_min ||
227 iops < td->rate_blocks) {
228 log_err("%s: min iops rate %u not met,"
229 " got %lu\n", td->o.name,
237 td->rate_bytes = bytes;
238 td->rate_blocks = iops;
239 memcpy(&td->lastrate, now, sizeof(*now));
243 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
247 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
254 * When job exits, we can cancel the in-flight IO if we are using async
255 * io. Attempt to do so.
257 static void cleanup_pending_aio(struct thread_data *td)
259 struct flist_head *entry, *n;
264 * get immediately available events, if any
266 r = io_u_queued_complete(td, 0);
271 * now cancel remaining active events
273 if (td->io_ops->cancel) {
274 flist_for_each_safe(entry, n, &td->io_u_busylist) {
275 io_u = flist_entry(entry, struct io_u, list);
278 * if the io_u isn't in flight, then that generally
279 * means someone leaked an io_u. complain but fix
280 * it up, so we don't stall here.
282 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
283 log_err("fio: non-busy IO on busy list\n");
286 r = td->io_ops->cancel(td, io_u);
294 r = io_u_queued_complete(td, td->cur_depth);
298 * Helper to handle the final sync of a file. Works just like the normal
299 * io path, just does everything sync.
301 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
303 struct io_u *io_u = __get_io_u(td);
309 io_u->ddir = DDIR_SYNC;
312 if (td_io_prep(td, io_u)) {
318 ret = td_io_queue(td, io_u);
320 td_verror(td, io_u->error, "td_io_queue");
323 } else if (ret == FIO_Q_QUEUED) {
324 if (io_u_queued_complete(td, 1) < 0)
326 } else if (ret == FIO_Q_COMPLETED) {
328 td_verror(td, io_u->error, "td_io_queue");
332 if (io_u_sync_complete(td, io_u) < 0)
334 } else if (ret == FIO_Q_BUSY) {
335 if (td_io_commit(td))
344 * The main verify engine. Runs over the writes we previously submitted,
345 * reads the blocks back in, and checks the crc/md5 of the data.
347 static void do_verify(struct thread_data *td)
355 * sync io first and invalidate cache, to make sure we really
358 for_each_file(td, f, i) {
359 if (!(f->flags & FIO_FILE_OPEN))
361 if (fio_io_sync(td, f))
363 if (file_invalidate_cache(td, f))
370 td_set_runstate(td, TD_VERIFYING);
373 while (!td->terminate) {
376 io_u = __get_io_u(td);
380 if (runtime_exceeded(td, &io_u->start_time)) {
386 if (get_next_verify(td, io_u)) {
391 if (td_io_prep(td, io_u)) {
396 io_u->end_io = verify_io_u;
398 ret = td_io_queue(td, io_u);
400 case FIO_Q_COMPLETED:
403 else if (io_u->resid) {
404 int bytes = io_u->xfer_buflen - io_u->resid;
405 struct fio_file *f = io_u->file;
411 td_verror(td, EIO, "full resid");
416 io_u->xfer_buflen = io_u->resid;
417 io_u->xfer_buf += bytes;
418 io_u->offset += bytes;
420 td->ts.short_io_u[io_u->ddir]++;
422 if (io_u->offset == f->real_file_size)
425 requeue_io_u(td, &io_u);
428 ret = io_u_sync_complete(td, io_u);
436 requeue_io_u(td, &io_u);
437 ret2 = td_io_commit(td);
443 td_verror(td, -ret, "td_io_queue");
447 if (ret < 0 || td->error)
451 * if we can queue more, do so. but check if there are
452 * completed io_u's first.
454 full = queue_full(td) || ret == FIO_Q_BUSY;
455 if (full || !td->o.iodepth_batch_complete) {
456 min_events = td->o.iodepth_batch_complete;
457 if (full && !min_events)
462 * Reap required number of io units, if any,
463 * and do the verification on them through
464 * the callback handler
466 if (io_u_queued_complete(td, min_events) < 0) {
470 } while (full && (td->cur_depth > td->o.iodepth_low));
477 min_events = td->cur_depth;
480 ret = io_u_queued_complete(td, min_events);
482 cleanup_pending_aio(td);
484 td_set_runstate(td, TD_RUNNING);
488 * Main IO worker function. It retrieves io_u's to process and queues
489 * and reaps them, checking for rate and errors along the way.
491 static void do_io(struct thread_data *td)
498 if (in_ramp_time(td))
499 td_set_runstate(td, TD_RAMP);
501 td_set_runstate(td, TD_RUNNING);
503 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
504 struct timeval comp_time;
517 memcpy(&s, &io_u->start_time, sizeof(s));
519 if (runtime_exceeded(td, &s)) {
526 * Add verification end_io handler, if asked to verify
527 * a previously written file.
529 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
530 io_u->end_io = verify_io_u;
531 td_set_runstate(td, TD_VERIFYING);
532 } else if (in_ramp_time(td))
533 td_set_runstate(td, TD_RAMP);
535 td_set_runstate(td, TD_RUNNING);
537 ret = td_io_queue(td, io_u);
539 case FIO_Q_COMPLETED:
542 else if (io_u->resid) {
543 int bytes = io_u->xfer_buflen - io_u->resid;
544 struct fio_file *f = io_u->file;
550 td_verror(td, EIO, "full resid");
555 io_u->xfer_buflen = io_u->resid;
556 io_u->xfer_buf += bytes;
557 io_u->offset += bytes;
559 td->ts.short_io_u[io_u->ddir]++;
561 if (io_u->offset == f->real_file_size)
564 requeue_io_u(td, &io_u);
567 fio_gettime(&comp_time, NULL);
568 bytes_done = io_u_sync_complete(td, io_u);
575 * if the engine doesn't have a commit hook,
576 * the io_u is really queued. if it does have such
577 * a hook, it has to call io_u_queued() itself.
579 if (td->io_ops->commit == NULL)
580 io_u_queued(td, io_u);
583 requeue_io_u(td, &io_u);
584 ret2 = td_io_commit(td);
594 if (ret < 0 || td->error)
598 * See if we need to complete some commands
600 full = queue_full(td) || ret == FIO_Q_BUSY;
601 if (full || !td->o.iodepth_batch_complete) {
602 min_evts = td->o.iodepth_batch_complete;
603 if (full && !min_evts)
606 fio_gettime(&comp_time, NULL);
609 ret = io_u_queued_complete(td, min_evts);
614 } while (full && (td->cur_depth > td->o.iodepth_low));
623 * the rate is batched for now, it should work for batches
624 * of completions except the very first one which may look
627 if (!in_ramp_time(td)) {
628 usec = utime_since(&s, &comp_time);
630 rate_throttle(td, usec, bytes_done);
632 if (check_min_rate(td, &comp_time)) {
633 if (exitall_on_terminate)
634 terminate_threads(td->groupid);
635 td_verror(td, EIO, "check_min_rate");
640 if (td->o.thinktime) {
641 unsigned long long b;
643 b = td->io_blocks[0] + td->io_blocks[1];
644 if (!(b % td->o.thinktime_blocks)) {
647 if (td->o.thinktime_spin)
648 __usec_sleep(td->o.thinktime_spin);
650 left = td->o.thinktime - td->o.thinktime_spin;
652 usec_sleep(td, left);
657 if (td->o.fill_device && td->error == ENOSPC) {
666 ret = io_u_queued_complete(td, i);
668 if (should_fsync(td) && td->o.end_fsync) {
669 td_set_runstate(td, TD_FSYNCING);
671 for_each_file(td, f, i) {
672 if (!(f->flags & FIO_FILE_OPEN))
678 cleanup_pending_aio(td);
681 * stop job if we failed doing any IO
683 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
687 static void cleanup_io_u(struct thread_data *td)
689 struct flist_head *entry, *n;
692 flist_for_each_safe(entry, n, &td->io_u_freelist) {
693 io_u = flist_entry(entry, struct io_u, list);
695 flist_del(&io_u->list);
702 static int init_io_u(struct thread_data *td)
709 max_units = td->o.iodepth;
710 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
711 td->orig_buffer_size = (unsigned long long) max_bs
712 * (unsigned long long) max_units;
714 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
717 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
718 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
721 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
722 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
726 if (allocate_io_mem(td))
730 p = ALIGN(td->orig_buffer);
734 for (i = 0; i < max_units; i++) {
737 io_u = malloc(sizeof(*io_u));
738 memset(io_u, 0, sizeof(*io_u));
739 INIT_FLIST_HEAD(&io_u->list);
741 if (!(td->io_ops->flags & FIO_NOIO)) {
742 io_u->buf = p + max_bs * i;
744 if (td_write(td) && !td->o.refill_buffers)
745 io_u_fill_buffer(td, io_u, max_bs);
749 io_u->flags = IO_U_F_FREE;
750 flist_add(&io_u->list, &td->io_u_freelist);
756 static int switch_ioscheduler(struct thread_data *td)
758 char tmp[256], tmp2[128];
762 if (td->io_ops->flags & FIO_DISKLESSIO)
765 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
767 f = fopen(tmp, "r+");
769 if (errno == ENOENT) {
770 log_err("fio: os or kernel doesn't support IO scheduler"
774 td_verror(td, errno, "fopen iosched");
781 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
782 if (ferror(f) || ret != 1) {
783 td_verror(td, errno, "fwrite");
791 * Read back and check that the selected scheduler is now the default.
793 ret = fread(tmp, 1, sizeof(tmp), f);
794 if (ferror(f) || ret < 0) {
795 td_verror(td, errno, "fread");
800 sprintf(tmp2, "[%s]", td->o.ioscheduler);
801 if (!strstr(tmp, tmp2)) {
802 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
803 td_verror(td, EINVAL, "iosched_switch");
812 static int keep_running(struct thread_data *td)
814 unsigned long long io_done;
818 if (td->o.time_based)
825 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
827 if (io_done < td->o.size)
833 static void reset_io_counters(struct thread_data *td)
835 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
836 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
840 td->rw_end_set[0] = td->rw_end_set[1] = 0;
842 td->last_was_sync = 0;
845 * reset file done count if we are to start over
847 if (td->o.time_based || td->o.loops)
848 td->nr_done_files = 0;
851 void reset_all_stats(struct thread_data *td)
856 reset_io_counters(td);
858 for (i = 0; i < 2; i++) {
860 td->io_blocks[i] = 0;
861 td->io_issues[i] = 0;
862 td->ts.total_io_u[i] = 0;
865 fio_gettime(&tv, NULL);
866 memcpy(&td->epoch, &tv, sizeof(tv));
867 memcpy(&td->start, &tv, sizeof(tv));
870 static int clear_io_state(struct thread_data *td)
876 reset_io_counters(td);
881 for_each_file(td, f, i) {
882 f->flags &= ~FIO_FILE_DONE;
883 ret = td_io_open_file(td, f);
892 * Entry point for the thread based jobs. The process based jobs end up
893 * here as well, after a little setup.
895 static void *thread_main(void *data)
897 unsigned long long runtime[2], elapsed;
898 struct thread_data *td = data;
901 if (!td->o.use_thread)
906 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
908 INIT_FLIST_HEAD(&td->io_u_freelist);
909 INIT_FLIST_HEAD(&td->io_u_busylist);
910 INIT_FLIST_HEAD(&td->io_u_requeues);
911 INIT_FLIST_HEAD(&td->io_log_list);
912 INIT_FLIST_HEAD(&td->io_hist_list);
913 td->io_hist_tree = RB_ROOT;
915 td_set_runstate(td, TD_INITIALIZED);
916 fio_mutex_up(startup_mutex);
917 fio_mutex_down(td->mutex);
920 * the ->mutex mutex is now no longer used, close it to avoid
921 * eating a file descriptor
923 fio_mutex_remove(td->mutex);
926 * May alter parameters that init_io_u() will use, so we need to
935 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
936 td_verror(td, errno, "cpu_set_affinity");
940 if (td->ioprio_set) {
941 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
942 td_verror(td, errno, "ioprio_set");
947 if (nice(td->o.nice) == -1) {
948 td_verror(td, errno, "nice");
952 if (td->o.ioscheduler && switch_ioscheduler(td))
955 if (!td->o.create_serialize && setup_files(td))
964 if (init_random_map(td))
967 if (td->o.exec_prerun) {
968 if (system(td->o.exec_prerun) < 0)
972 fio_gettime(&td->epoch, NULL);
973 getrusage(RUSAGE_SELF, &td->ts.ru_start);
975 runtime[0] = runtime[1] = 0;
977 while (keep_running(td)) {
978 fio_gettime(&td->start, NULL);
979 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
982 memcpy(&td->lastrate, &td->ts.stat_sample_time,
983 sizeof(td->lastrate));
985 if (clear_state && clear_io_state(td))
988 prune_io_piece_log(td);
994 if (td_read(td) && td->io_bytes[DDIR_READ]) {
995 if (td->rw_end_set[DDIR_READ])
996 elapsed = utime_since(&td->start,
997 &td->rw_end[DDIR_READ]);
999 elapsed = utime_since_now(&td->start);
1001 runtime[DDIR_READ] += elapsed;
1003 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1004 if (td->rw_end_set[DDIR_WRITE])
1005 elapsed = utime_since(&td->start,
1006 &td->rw_end[DDIR_WRITE]);
1008 elapsed = utime_since_now(&td->start);
1010 runtime[DDIR_WRITE] += elapsed;
1013 if (td->error || td->terminate)
1016 if (!td->o.do_verify ||
1017 td->o.verify == VERIFY_NONE ||
1018 (td->io_ops->flags & FIO_UNIDIR))
1021 if (clear_io_state(td))
1024 fio_gettime(&td->start, NULL);
1028 runtime[DDIR_READ] += utime_since_now(&td->start);
1030 if (td->error || td->terminate)
1034 update_rusage_stat(td);
1035 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1036 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1037 td->ts.total_run_time = mtime_since_now(&td->epoch);
1038 td->ts.io_bytes[0] = td->io_bytes[0];
1039 td->ts.io_bytes[1] = td->io_bytes[1];
1042 finish_log(td, td->ts.bw_log, "bw");
1043 if (td->ts.slat_log)
1044 finish_log(td, td->ts.slat_log, "slat");
1045 if (td->ts.clat_log)
1046 finish_log(td, td->ts.clat_log, "clat");
1047 if (td->o.exec_postrun) {
1048 if (system(td->o.exec_postrun) < 0)
1049 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
1052 if (exitall_on_terminate)
1053 terminate_threads(td->groupid);
1057 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1059 close_and_free_files(td);
1064 * do this very late, it will log file closing as well
1066 if (td->o.write_iolog_file)
1067 write_iolog_close(td);
1069 options_mem_free(td);
1070 td_set_runstate(td, TD_EXITED);
1071 return (void *) (unsigned long) td->error;
1075 * We cannot pass the td data into a forked process, so attach the td and
1076 * pass it to the thread worker.
1078 static int fork_main(int shmid, int offset)
1080 struct thread_data *td;
1083 data = shmat(shmid, NULL, 0);
1084 if (data == (void *) -1) {
1091 td = data + offset * sizeof(struct thread_data);
1092 ret = thread_main(td);
1094 return (int) (unsigned long) ret;
1098 * Run over the job map and reap the threads that have exited, if any.
1100 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1102 struct thread_data *td;
1103 int i, cputhreads, realthreads, pending, status, ret;
1106 * reap exited threads (TD_EXITED -> TD_REAPED)
1108 realthreads = pending = cputhreads = 0;
1109 for_each_td(td, i) {
1113 * ->io_ops is NULL for a thread that has closed its
1116 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1125 if (td->runstate == TD_REAPED)
1127 if (td->o.use_thread) {
1128 if (td->runstate == TD_EXITED) {
1129 td_set_runstate(td, TD_REAPED);
1136 if (td->runstate == TD_EXITED)
1140 * check if someone quit or got killed in an unusual way
1142 ret = waitpid(td->pid, &status, flags);
1144 if (errno == ECHILD) {
1145 log_err("fio: pid=%d disappeared %d\n",
1146 (int) td->pid, td->runstate);
1147 td_set_runstate(td, TD_REAPED);
1151 } else if (ret == td->pid) {
1152 if (WIFSIGNALED(status)) {
1153 int sig = WTERMSIG(status);
1156 log_err("fio: pid=%d, got signal=%d\n",
1157 (int) td->pid, sig);
1158 td_set_runstate(td, TD_REAPED);
1161 if (WIFEXITED(status)) {
1162 if (WEXITSTATUS(status) && !td->error)
1163 td->error = WEXITSTATUS(status);
1165 td_set_runstate(td, TD_REAPED);
1171 * thread is not dead, continue
1177 (*m_rate) -= td->o.ratemin;
1178 (*t_rate) -= td->o.rate;
1185 done_secs += mtime_since_now(&td->epoch) / 1000;
1188 if (*nr_running == cputhreads && !pending && realthreads)
1189 terminate_threads(TERMINATE_ALL);
1193 * Main function for kicking off and reaping jobs, as needed.
1195 static void run_threads(void)
1197 struct thread_data *td;
1198 unsigned long spent;
1199 int i, todo, nr_running, m_rate, t_rate, nr_started;
1201 if (fio_pin_memory())
1204 if (!terse_output) {
1205 printf("Starting ");
1207 printf("%d thread%s", nr_thread,
1208 nr_thread > 1 ? "s" : "");
1212 printf("%d process%s", nr_process,
1213 nr_process > 1 ? "es" : "");
1221 todo = thread_number;
1224 m_rate = t_rate = 0;
1226 for_each_td(td, i) {
1227 print_status_init(td->thread_number - 1);
1229 if (!td->o.create_serialize) {
1235 * do file setup here so it happens sequentially,
1236 * we don't want X number of threads getting their
1237 * client data interspersed on disk
1239 if (setup_files(td)) {
1242 log_err("fio: pid=%d, err=%d/%s\n",
1243 (int) td->pid, td->error, td->verror);
1244 td_set_runstate(td, TD_REAPED);
1251 * for sharing to work, each job must always open
1252 * its own files. so close them, if we opened them
1255 for_each_file(td, f, i)
1256 td_io_close_file(td, f);
1265 struct thread_data *map[MAX_JOBS];
1266 struct timeval this_start;
1267 int this_jobs = 0, left;
1270 * create threads (TD_NOT_CREATED -> TD_CREATED)
1272 for_each_td(td, i) {
1273 if (td->runstate != TD_NOT_CREATED)
1277 * never got a chance to start, killed by other
1278 * thread for some reason
1280 if (td->terminate) {
1285 if (td->o.start_delay) {
1286 spent = mtime_since_genesis();
1288 if (td->o.start_delay * 1000 > spent)
1292 if (td->o.stonewall && (nr_started || nr_running)) {
1293 dprint(FD_PROCESS, "%s: stonewall wait\n",
1299 * Set state to created. Thread will transition
1300 * to TD_INITIALIZED when it's done setting up.
1302 td_set_runstate(td, TD_CREATED);
1303 map[this_jobs++] = td;
1306 if (td->o.use_thread) {
1307 dprint(FD_PROCESS, "will pthread_create\n");
1308 if (pthread_create(&td->thread, NULL,
1310 perror("pthread_create");
1314 if (pthread_detach(td->thread) < 0)
1315 perror("pthread_detach");
1318 dprint(FD_PROCESS, "will fork\n");
1321 int ret = fork_main(shm_id, i);
1324 } else if (i == fio_debug_jobno)
1325 *fio_debug_jobp = pid;
1327 fio_mutex_down(startup_mutex);
1331 * Wait for the started threads to transition to
1334 fio_gettime(&this_start, NULL);
1336 while (left && !fio_abort) {
1337 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1342 for (i = 0; i < this_jobs; i++) {
1346 if (td->runstate == TD_INITIALIZED) {
1349 } else if (td->runstate >= TD_EXITED) {
1353 nr_running++; /* work-around... */
1359 log_err("fio: %d jobs failed to start\n", left);
1360 for (i = 0; i < this_jobs; i++) {
1364 kill(td->pid, SIGTERM);
1370 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1372 for_each_td(td, i) {
1373 if (td->runstate != TD_INITIALIZED)
1376 if (in_ramp_time(td))
1377 td_set_runstate(td, TD_RAMP);
1379 td_set_runstate(td, TD_RUNNING);
1382 m_rate += td->o.ratemin;
1383 t_rate += td->o.rate;
1385 fio_mutex_up(td->mutex);
1388 reap_threads(&nr_running, &t_rate, &m_rate);
1394 while (nr_running) {
1395 reap_threads(&nr_running, &t_rate, &m_rate);
1403 int main(int argc, char *argv[])
1410 * We need locale for number printing, if it isn't set then just
1411 * go with the US format.
1413 if (!getenv("LC_NUMERIC"))
1414 setlocale(LC_NUMERIC, "en_US");
1416 if (parse_options(argc, argv))
1422 ps = sysconf(_SC_PAGESIZE);
1424 log_err("Failed to get page size\n");
1432 setup_log(&agg_io_log[DDIR_READ]);
1433 setup_log(&agg_io_log[DDIR_WRITE]);
1436 startup_mutex = fio_mutex_init(0);
1447 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1448 __finish_log(agg_io_log[DDIR_WRITE],
1449 "agg-write_bw.log");
1453 fio_mutex_remove(startup_mutex);