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);
159 static inline int should_check_rate(struct thread_data *td)
162 * No minimum rate set, always ok
164 if (!td->o.ratemin && !td->o.rate_iops_min)
171 * Check if we are above the minimum rate given.
173 static int check_min_rate(struct thread_data *td, struct timeval *now)
175 unsigned long long bytes = 0;
176 unsigned long iops = 0;
181 * allow a 2 second settle period in the beginning
183 if (mtime_since(&td->start, now) < 2000)
187 iops += td->io_blocks[DDIR_READ];
188 bytes += td->this_io_bytes[DDIR_READ];
191 iops += td->io_blocks[DDIR_WRITE];
192 bytes += td->this_io_bytes[DDIR_WRITE];
196 * if rate blocks is set, sample is running
198 if (td->rate_bytes || td->rate_blocks) {
199 spent = mtime_since(&td->lastrate, now);
200 if (spent < td->o.ratecycle)
205 * check bandwidth specified rate
207 if (bytes < td->rate_bytes) {
208 log_err("%s: min rate %u not met\n", td->o.name,
212 rate = (bytes - td->rate_bytes) / spent;
213 if (rate < td->o.ratemin ||
214 bytes < td->rate_bytes) {
215 log_err("%s: min rate %u not met, got"
216 " %luKiB/sec\n", td->o.name,
217 td->o.ratemin, rate);
223 * checks iops specified rate
225 if (iops < td->o.rate_iops) {
226 log_err("%s: min iops rate %u not met\n",
227 td->o.name, td->o.rate_iops);
230 rate = (iops - td->rate_blocks) / spent;
231 if (rate < td->o.rate_iops_min ||
232 iops < td->rate_blocks) {
233 log_err("%s: min iops rate %u not met,"
234 " got %lu\n", td->o.name,
242 td->rate_bytes = bytes;
243 td->rate_blocks = iops;
244 memcpy(&td->lastrate, now, sizeof(*now));
248 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
252 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
259 * When job exits, we can cancel the in-flight IO if we are using async
260 * io. Attempt to do so.
262 static void cleanup_pending_aio(struct thread_data *td)
264 struct flist_head *entry, *n;
269 * get immediately available events, if any
271 r = io_u_queued_complete(td, 0);
276 * now cancel remaining active events
278 if (td->io_ops->cancel) {
279 flist_for_each_safe(entry, n, &td->io_u_busylist) {
280 io_u = flist_entry(entry, struct io_u, list);
283 * if the io_u isn't in flight, then that generally
284 * means someone leaked an io_u. complain but fix
285 * it up, so we don't stall here.
287 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
288 log_err("fio: non-busy IO on busy list\n");
291 r = td->io_ops->cancel(td, io_u);
299 r = io_u_queued_complete(td, td->cur_depth);
303 * Helper to handle the final sync of a file. Works just like the normal
304 * io path, just does everything sync.
306 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
308 struct io_u *io_u = __get_io_u(td);
314 io_u->ddir = DDIR_SYNC;
317 if (td_io_prep(td, io_u)) {
323 ret = td_io_queue(td, io_u);
325 td_verror(td, io_u->error, "td_io_queue");
328 } else if (ret == FIO_Q_QUEUED) {
329 if (io_u_queued_complete(td, 1) < 0)
331 } else if (ret == FIO_Q_COMPLETED) {
333 td_verror(td, io_u->error, "td_io_queue");
337 if (io_u_sync_complete(td, io_u) < 0)
339 } else if (ret == FIO_Q_BUSY) {
340 if (td_io_commit(td))
348 static inline void update_tv_cache(struct thread_data *td)
350 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
351 fio_gettime(&td->tv_cache, NULL);
355 * The main verify engine. Runs over the writes we previously submitted,
356 * reads the blocks back in, and checks the crc/md5 of the data.
358 static void do_verify(struct thread_data *td)
366 * sync io first and invalidate cache, to make sure we really
369 for_each_file(td, f, i) {
370 if (!(f->flags & FIO_FILE_OPEN))
372 if (fio_io_sync(td, f))
374 if (file_invalidate_cache(td, f))
381 td_set_runstate(td, TD_VERIFYING);
384 while (!td->terminate) {
387 io_u = __get_io_u(td);
393 if (runtime_exceeded(td, &td->tv_cache)) {
399 if (get_next_verify(td, io_u)) {
404 if (td_io_prep(td, io_u)) {
409 io_u->end_io = verify_io_u;
411 ret = td_io_queue(td, io_u);
413 case FIO_Q_COMPLETED:
416 else if (io_u->resid) {
417 int bytes = io_u->xfer_buflen - io_u->resid;
418 struct fio_file *f = io_u->file;
424 td_verror(td, EIO, "full resid");
429 io_u->xfer_buflen = io_u->resid;
430 io_u->xfer_buf += bytes;
431 io_u->offset += bytes;
433 td->ts.short_io_u[io_u->ddir]++;
435 if (io_u->offset == f->real_file_size)
438 requeue_io_u(td, &io_u);
441 ret = io_u_sync_complete(td, io_u);
449 requeue_io_u(td, &io_u);
450 ret2 = td_io_commit(td);
456 td_verror(td, -ret, "td_io_queue");
460 if (ret < 0 || td->error)
464 * if we can queue more, do so. but check if there are
465 * completed io_u's first.
467 full = queue_full(td) || ret == FIO_Q_BUSY;
468 if (full || !td->o.iodepth_batch_complete) {
469 min_events = td->o.iodepth_batch_complete;
470 if (full && !min_events)
475 * Reap required number of io units, if any,
476 * and do the verification on them through
477 * the callback handler
479 if (io_u_queued_complete(td, min_events) < 0) {
483 } while (full && (td->cur_depth > td->o.iodepth_low));
490 min_events = td->cur_depth;
493 ret = io_u_queued_complete(td, min_events);
495 cleanup_pending_aio(td);
497 td_set_runstate(td, TD_RUNNING);
501 * Main IO worker function. It retrieves io_u's to process and queues
502 * and reaps them, checking for rate and errors along the way.
504 static void do_io(struct thread_data *td)
510 if (in_ramp_time(td))
511 td_set_runstate(td, TD_RAMP);
513 td_set_runstate(td, TD_RUNNING);
515 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
516 struct timeval comp_time;
531 if (runtime_exceeded(td, &td->tv_cache)) {
538 * Add verification end_io handler, if asked to verify
539 * a previously written file.
541 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
542 io_u->end_io = verify_io_u;
543 td_set_runstate(td, TD_VERIFYING);
544 } else if (in_ramp_time(td))
545 td_set_runstate(td, TD_RAMP);
547 td_set_runstate(td, TD_RUNNING);
549 ret = td_io_queue(td, io_u);
551 case FIO_Q_COMPLETED:
554 else if (io_u->resid) {
555 int bytes = io_u->xfer_buflen - io_u->resid;
556 struct fio_file *f = io_u->file;
562 td_verror(td, EIO, "full resid");
567 io_u->xfer_buflen = io_u->resid;
568 io_u->xfer_buf += bytes;
569 io_u->offset += bytes;
571 td->ts.short_io_u[io_u->ddir]++;
573 if (io_u->offset == f->real_file_size)
576 requeue_io_u(td, &io_u);
579 if (should_check_rate(td))
580 fio_gettime(&comp_time, NULL);
582 bytes_done = io_u_sync_complete(td, io_u);
589 * if the engine doesn't have a commit hook,
590 * the io_u is really queued. if it does have such
591 * a hook, it has to call io_u_queued() itself.
593 if (td->io_ops->commit == NULL)
594 io_u_queued(td, io_u);
597 requeue_io_u(td, &io_u);
598 ret2 = td_io_commit(td);
608 if (ret < 0 || td->error)
612 * See if we need to complete some commands
614 full = queue_full(td) || ret == FIO_Q_BUSY;
615 if (full || !td->o.iodepth_batch_complete) {
616 min_evts = td->o.iodepth_batch_complete;
617 if (full && !min_evts)
620 if (should_check_rate(td))
621 fio_gettime(&comp_time, NULL);
624 ret = io_u_queued_complete(td, min_evts);
629 } while (full && (td->cur_depth > td->o.iodepth_low));
638 * the rate is batched for now, it should work for batches
639 * of completions except the very first one which may look
642 if (!in_ramp_time(td) && should_check_rate(td)) {
643 usec = utime_since(&td->tv_cache, &comp_time);
645 rate_throttle(td, usec, bytes_done);
647 if (check_min_rate(td, &comp_time)) {
648 if (exitall_on_terminate)
649 terminate_threads(td->groupid);
650 td_verror(td, EIO, "check_min_rate");
655 if (td->o.thinktime) {
656 unsigned long long b;
658 b = td->io_blocks[0] + td->io_blocks[1];
659 if (!(b % td->o.thinktime_blocks)) {
662 if (td->o.thinktime_spin)
663 __usec_sleep(td->o.thinktime_spin);
665 left = td->o.thinktime - td->o.thinktime_spin;
667 usec_sleep(td, left);
672 if (td->o.fill_device && td->error == ENOSPC) {
681 ret = io_u_queued_complete(td, i);
683 if (should_fsync(td) && td->o.end_fsync) {
684 td_set_runstate(td, TD_FSYNCING);
686 for_each_file(td, f, i) {
687 if (!(f->flags & FIO_FILE_OPEN))
693 cleanup_pending_aio(td);
696 * stop job if we failed doing any IO
698 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
702 static void cleanup_io_u(struct thread_data *td)
704 struct flist_head *entry, *n;
707 flist_for_each_safe(entry, n, &td->io_u_freelist) {
708 io_u = flist_entry(entry, struct io_u, list);
710 flist_del(&io_u->list);
717 static int init_io_u(struct thread_data *td)
724 max_units = td->o.iodepth;
725 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
726 td->orig_buffer_size = (unsigned long long) max_bs
727 * (unsigned long long) max_units;
729 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
732 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
733 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
736 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
737 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
741 if (allocate_io_mem(td))
745 p = ALIGN(td->orig_buffer);
749 for (i = 0; i < max_units; i++) {
752 io_u = malloc(sizeof(*io_u));
753 memset(io_u, 0, sizeof(*io_u));
754 INIT_FLIST_HEAD(&io_u->list);
756 if (!(td->io_ops->flags & FIO_NOIO)) {
757 io_u->buf = p + max_bs * i;
759 if (td_write(td) && !td->o.refill_buffers)
760 io_u_fill_buffer(td, io_u, max_bs);
764 io_u->flags = IO_U_F_FREE;
765 flist_add(&io_u->list, &td->io_u_freelist);
771 static int switch_ioscheduler(struct thread_data *td)
773 char tmp[256], tmp2[128];
777 if (td->io_ops->flags & FIO_DISKLESSIO)
780 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
782 f = fopen(tmp, "r+");
784 if (errno == ENOENT) {
785 log_err("fio: os or kernel doesn't support IO scheduler"
789 td_verror(td, errno, "fopen iosched");
796 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
797 if (ferror(f) || ret != 1) {
798 td_verror(td, errno, "fwrite");
806 * Read back and check that the selected scheduler is now the default.
808 ret = fread(tmp, 1, sizeof(tmp), f);
809 if (ferror(f) || ret < 0) {
810 td_verror(td, errno, "fread");
815 sprintf(tmp2, "[%s]", td->o.ioscheduler);
816 if (!strstr(tmp, tmp2)) {
817 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
818 td_verror(td, EINVAL, "iosched_switch");
827 static int keep_running(struct thread_data *td)
829 unsigned long long io_done;
833 if (td->o.time_based)
840 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
842 if (io_done < td->o.size)
848 static void reset_io_counters(struct thread_data *td)
850 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
851 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
855 td->rw_end_set[0] = td->rw_end_set[1] = 0;
857 td->last_was_sync = 0;
860 * reset file done count if we are to start over
862 if (td->o.time_based || td->o.loops)
863 td->nr_done_files = 0;
866 void reset_all_stats(struct thread_data *td)
871 reset_io_counters(td);
873 for (i = 0; i < 2; i++) {
875 td->io_blocks[i] = 0;
876 td->io_issues[i] = 0;
877 td->ts.total_io_u[i] = 0;
880 fio_gettime(&tv, NULL);
881 memcpy(&td->epoch, &tv, sizeof(tv));
882 memcpy(&td->start, &tv, sizeof(tv));
885 static int clear_io_state(struct thread_data *td)
891 reset_io_counters(td);
896 for_each_file(td, f, i) {
897 f->flags &= ~FIO_FILE_DONE;
898 ret = td_io_open_file(td, f);
907 * Entry point for the thread based jobs. The process based jobs end up
908 * here as well, after a little setup.
910 static void *thread_main(void *data)
912 unsigned long long runtime[2], elapsed;
913 struct thread_data *td = data;
916 if (!td->o.use_thread)
921 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
923 INIT_FLIST_HEAD(&td->io_u_freelist);
924 INIT_FLIST_HEAD(&td->io_u_busylist);
925 INIT_FLIST_HEAD(&td->io_u_requeues);
926 INIT_FLIST_HEAD(&td->io_log_list);
927 INIT_FLIST_HEAD(&td->io_hist_list);
928 td->io_hist_tree = RB_ROOT;
930 td_set_runstate(td, TD_INITIALIZED);
931 fio_mutex_up(startup_mutex);
932 fio_mutex_down(td->mutex);
935 * the ->mutex mutex is now no longer used, close it to avoid
936 * eating a file descriptor
938 fio_mutex_remove(td->mutex);
941 * May alter parameters that init_io_u() will use, so we need to
950 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
951 td_verror(td, errno, "cpu_set_affinity");
955 if (td->ioprio_set) {
956 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
957 td_verror(td, errno, "ioprio_set");
962 if (nice(td->o.nice) == -1) {
963 td_verror(td, errno, "nice");
967 if (td->o.ioscheduler && switch_ioscheduler(td))
970 if (!td->o.create_serialize && setup_files(td))
979 if (init_random_map(td))
982 if (td->o.exec_prerun) {
983 if (system(td->o.exec_prerun) < 0)
987 fio_gettime(&td->epoch, NULL);
988 getrusage(RUSAGE_SELF, &td->ts.ru_start);
990 runtime[0] = runtime[1] = 0;
992 while (keep_running(td)) {
993 fio_gettime(&td->start, NULL);
994 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
995 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
998 memcpy(&td->lastrate, &td->ts.stat_sample_time,
999 sizeof(td->lastrate));
1001 if (clear_state && clear_io_state(td))
1004 prune_io_piece_log(td);
1010 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1011 if (td->rw_end_set[DDIR_READ])
1012 elapsed = utime_since(&td->start,
1013 &td->rw_end[DDIR_READ]);
1015 elapsed = utime_since_now(&td->start);
1017 runtime[DDIR_READ] += elapsed;
1019 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1020 if (td->rw_end_set[DDIR_WRITE])
1021 elapsed = utime_since(&td->start,
1022 &td->rw_end[DDIR_WRITE]);
1024 elapsed = utime_since_now(&td->start);
1026 runtime[DDIR_WRITE] += elapsed;
1029 if (td->error || td->terminate)
1032 if (!td->o.do_verify ||
1033 td->o.verify == VERIFY_NONE ||
1034 (td->io_ops->flags & FIO_UNIDIR))
1037 if (clear_io_state(td))
1040 fio_gettime(&td->start, NULL);
1044 runtime[DDIR_READ] += utime_since_now(&td->start);
1046 if (td->error || td->terminate)
1050 update_rusage_stat(td);
1051 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1052 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1053 td->ts.total_run_time = mtime_since_now(&td->epoch);
1054 td->ts.io_bytes[0] = td->io_bytes[0];
1055 td->ts.io_bytes[1] = td->io_bytes[1];
1058 finish_log(td, td->ts.bw_log, "bw");
1059 if (td->ts.slat_log)
1060 finish_log(td, td->ts.slat_log, "slat");
1061 if (td->ts.clat_log)
1062 finish_log(td, td->ts.clat_log, "clat");
1063 if (td->o.exec_postrun) {
1064 if (system(td->o.exec_postrun) < 0)
1065 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
1068 if (exitall_on_terminate)
1069 terminate_threads(td->groupid);
1073 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1075 close_and_free_files(td);
1080 * do this very late, it will log file closing as well
1082 if (td->o.write_iolog_file)
1083 write_iolog_close(td);
1085 options_mem_free(td);
1086 td_set_runstate(td, TD_EXITED);
1087 return (void *) (unsigned long) td->error;
1091 * We cannot pass the td data into a forked process, so attach the td and
1092 * pass it to the thread worker.
1094 static int fork_main(int shmid, int offset)
1096 struct thread_data *td;
1099 data = shmat(shmid, NULL, 0);
1100 if (data == (void *) -1) {
1107 td = data + offset * sizeof(struct thread_data);
1108 ret = thread_main(td);
1110 return (int) (unsigned long) ret;
1114 * Run over the job map and reap the threads that have exited, if any.
1116 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1118 struct thread_data *td;
1119 int i, cputhreads, realthreads, pending, status, ret;
1122 * reap exited threads (TD_EXITED -> TD_REAPED)
1124 realthreads = pending = cputhreads = 0;
1125 for_each_td(td, i) {
1129 * ->io_ops is NULL for a thread that has closed its
1132 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1141 if (td->runstate == TD_REAPED)
1143 if (td->o.use_thread) {
1144 if (td->runstate == TD_EXITED) {
1145 td_set_runstate(td, TD_REAPED);
1152 if (td->runstate == TD_EXITED)
1156 * check if someone quit or got killed in an unusual way
1158 ret = waitpid(td->pid, &status, flags);
1160 if (errno == ECHILD) {
1161 log_err("fio: pid=%d disappeared %d\n",
1162 (int) td->pid, td->runstate);
1163 td_set_runstate(td, TD_REAPED);
1167 } else if (ret == td->pid) {
1168 if (WIFSIGNALED(status)) {
1169 int sig = WTERMSIG(status);
1172 log_err("fio: pid=%d, got signal=%d\n",
1173 (int) td->pid, sig);
1174 td_set_runstate(td, TD_REAPED);
1177 if (WIFEXITED(status)) {
1178 if (WEXITSTATUS(status) && !td->error)
1179 td->error = WEXITSTATUS(status);
1181 td_set_runstate(td, TD_REAPED);
1187 * thread is not dead, continue
1193 (*m_rate) -= td->o.ratemin;
1194 (*t_rate) -= td->o.rate;
1201 done_secs += mtime_since_now(&td->epoch) / 1000;
1204 if (*nr_running == cputhreads && !pending && realthreads)
1205 terminate_threads(TERMINATE_ALL);
1209 * Main function for kicking off and reaping jobs, as needed.
1211 static void run_threads(void)
1213 struct thread_data *td;
1214 unsigned long spent;
1215 int i, todo, nr_running, m_rate, t_rate, nr_started;
1217 if (fio_pin_memory())
1220 if (!terse_output) {
1221 printf("Starting ");
1223 printf("%d thread%s", nr_thread,
1224 nr_thread > 1 ? "s" : "");
1228 printf("%d process%s", nr_process,
1229 nr_process > 1 ? "es" : "");
1237 todo = thread_number;
1240 m_rate = t_rate = 0;
1242 for_each_td(td, i) {
1243 print_status_init(td->thread_number - 1);
1245 if (!td->o.create_serialize) {
1251 * do file setup here so it happens sequentially,
1252 * we don't want X number of threads getting their
1253 * client data interspersed on disk
1255 if (setup_files(td)) {
1258 log_err("fio: pid=%d, err=%d/%s\n",
1259 (int) td->pid, td->error, td->verror);
1260 td_set_runstate(td, TD_REAPED);
1267 * for sharing to work, each job must always open
1268 * its own files. so close them, if we opened them
1271 for_each_file(td, f, i)
1272 td_io_close_file(td, f);
1281 struct thread_data *map[MAX_JOBS];
1282 struct timeval this_start;
1283 int this_jobs = 0, left;
1286 * create threads (TD_NOT_CREATED -> TD_CREATED)
1288 for_each_td(td, i) {
1289 if (td->runstate != TD_NOT_CREATED)
1293 * never got a chance to start, killed by other
1294 * thread for some reason
1296 if (td->terminate) {
1301 if (td->o.start_delay) {
1302 spent = mtime_since_genesis();
1304 if (td->o.start_delay * 1000 > spent)
1308 if (td->o.stonewall && (nr_started || nr_running)) {
1309 dprint(FD_PROCESS, "%s: stonewall wait\n",
1315 * Set state to created. Thread will transition
1316 * to TD_INITIALIZED when it's done setting up.
1318 td_set_runstate(td, TD_CREATED);
1319 map[this_jobs++] = td;
1322 if (td->o.use_thread) {
1323 dprint(FD_PROCESS, "will pthread_create\n");
1324 if (pthread_create(&td->thread, NULL,
1326 perror("pthread_create");
1330 if (pthread_detach(td->thread) < 0)
1331 perror("pthread_detach");
1334 dprint(FD_PROCESS, "will fork\n");
1337 int ret = fork_main(shm_id, i);
1340 } else if (i == fio_debug_jobno)
1341 *fio_debug_jobp = pid;
1343 fio_mutex_down(startup_mutex);
1347 * Wait for the started threads to transition to
1350 fio_gettime(&this_start, NULL);
1352 while (left && !fio_abort) {
1353 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1358 for (i = 0; i < this_jobs; i++) {
1362 if (td->runstate == TD_INITIALIZED) {
1365 } else if (td->runstate >= TD_EXITED) {
1369 nr_running++; /* work-around... */
1375 log_err("fio: %d jobs failed to start\n", left);
1376 for (i = 0; i < this_jobs; i++) {
1380 kill(td->pid, SIGTERM);
1386 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1388 for_each_td(td, i) {
1389 if (td->runstate != TD_INITIALIZED)
1392 if (in_ramp_time(td))
1393 td_set_runstate(td, TD_RAMP);
1395 td_set_runstate(td, TD_RUNNING);
1398 m_rate += td->o.ratemin;
1399 t_rate += td->o.rate;
1401 fio_mutex_up(td->mutex);
1404 reap_threads(&nr_running, &t_rate, &m_rate);
1410 while (nr_running) {
1411 reap_threads(&nr_running, &t_rate, &m_rate);
1419 int main(int argc, char *argv[])
1426 * We need locale for number printing, if it isn't set then just
1427 * go with the US format.
1429 if (!getenv("LC_NUMERIC"))
1430 setlocale(LC_NUMERIC, "en_US");
1432 if (parse_options(argc, argv))
1438 ps = sysconf(_SC_PAGESIZE);
1440 log_err("Failed to get page size\n");
1448 setup_log(&agg_io_log[DDIR_READ]);
1449 setup_log(&agg_io_log[DDIR_WRITE]);
1452 startup_mutex = fio_mutex_init(0);
1463 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1464 __finish_log(agg_io_log[DDIR_WRITE],
1465 "agg-write_bw.log");
1469 fio_mutex_remove(startup_mutex);