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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/ioctl.h>
40 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43 int thread_number = 0;
46 char *fio_inst_prefix = _INST_PREFIX;
48 extern unsigned long long mlock_size;
50 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
52 static volatile int startup_sem;
54 #define TERMINATE_ALL (-1)
55 #define JOB_START_TIMEOUT (5 * 1000)
57 static void terminate_threads(int group_id)
61 for (i = 0; i < thread_number; i++) {
62 struct thread_data *td = &threads[i];
64 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
71 static void sig_handler(int sig)
76 disk_util_timer_arm();
77 print_thread_status();
80 printf("\nfio: terminating on signal\n");
82 terminate_threads(TERMINATE_ALL);
88 * The ->file_map[] contains a map of blocks we have or have not done io
89 * to yet. Used to make sure we cover the entire range in a fair fashion.
91 static int random_map_free(struct thread_data *td, struct fio_file *f,
92 unsigned long long block)
94 unsigned int idx = RAND_MAP_IDX(td, f, block);
95 unsigned int bit = RAND_MAP_BIT(td, f, block);
97 return (f->file_map[idx] & (1UL << bit)) == 0;
101 * Return the next free block in the map.
103 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
104 unsigned long long *b)
110 while ((*b) * td->min_bs < f->file_size) {
111 if (f->file_map[i] != -1UL) {
112 *b += ffz(f->file_map[i]);
116 *b += BLOCKS_PER_MAP;
124 * Mark a given offset as used in the map.
126 static void mark_random_map(struct thread_data *td, struct fio_file *f,
129 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
130 unsigned int blocks = 0;
132 while (blocks < (io_u->buflen / td->min_bs)) {
133 unsigned int idx, bit;
135 if (!random_map_free(td, f, block))
138 idx = RAND_MAP_IDX(td, f, block);
139 bit = RAND_MAP_BIT(td, f, block);
141 assert(idx < f->num_maps);
143 f->file_map[idx] |= (1UL << bit);
148 if ((blocks * td->min_bs) < io_u->buflen)
149 io_u->buflen = blocks * td->min_bs;
153 * For random io, generate a random new block and see if it's used. Repeat
154 * until we find a free one. For sequential io, just return the end of
155 * the last io issued.
157 static int get_next_offset(struct thread_data *td, struct fio_file *f,
158 unsigned long long *offset)
160 unsigned long long b, rb;
163 if (!td->sequential) {
164 unsigned long long max_blocks = td->io_size / td->min_bs;
168 r = os_random_long(&td->random_state);
169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
170 rb = b + (f->file_offset / td->min_bs);
172 } while (!random_map_free(td, f, rb) && loops);
175 if (get_next_free_block(td, f, &b))
179 b = f->last_pos / td->min_bs;
181 *offset = (b * td->min_bs) + f->file_offset;
182 if (*offset > f->file_size)
188 static unsigned int get_next_buflen(struct thread_data *td)
193 if (td->min_bs == td->max_bs)
196 r = os_random_long(&td->bsrange_state);
197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
203 * if using direct/raw io, we may not be able to
204 * shrink the size. so just fail it.
206 if (td->io_ops->flags & FIO_RAWIO)
209 buflen = td->io_size - td->this_io_bytes[td->ddir];
216 * Check if we are above the minimum rate given.
218 static int check_min_rate(struct thread_data *td, struct timeval *now)
225 * allow a 2 second settle period in the beginning
227 if (mtime_since(&td->start, now) < 2000)
231 * if rate blocks is set, sample is running
233 if (td->rate_bytes) {
234 spent = mtime_since(&td->lastrate, now);
235 if (spent < td->ratecycle)
238 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
239 if (rate < td->ratemin) {
240 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
242 terminate_threads(td->groupid);
247 td->rate_bytes = td->this_io_bytes[ddir];
248 memcpy(&td->lastrate, now, sizeof(*now));
252 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
256 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
263 * Return the data direction for the next io_u. If the job is a
264 * mixed read/write workload, check the rwmix cycle and switch if
267 static int get_rw_ddir(struct thread_data *td)
271 unsigned long elapsed;
273 gettimeofday(&now, NULL);
274 elapsed = mtime_since_now(&td->rwmix_switch);
277 * Check if it's time to seed a new data direction.
279 if (elapsed >= td->rwmixcycle) {
283 r = os_random_long(&td->rwmix_state);
284 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
285 if (v < td->rwmixread)
286 td->rwmix_ddir = DDIR_READ;
288 td->rwmix_ddir = DDIR_WRITE;
289 memcpy(&td->rwmix_switch, &now, sizeof(now));
291 return td->rwmix_ddir;
292 } else if (td_read(td))
298 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
300 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
306 void put_io_u(struct thread_data *td, struct io_u *io_u)
309 list_del(&io_u->list);
310 list_add(&io_u->list, &td->io_u_freelist);
314 static int fill_io_u(struct thread_data *td, struct fio_file *f,
318 * If using an iolog, grab next piece if any available.
321 return read_iolog_get(td, io_u);
324 * No log, let the seq/rand engine retrieve the next position.
326 if (!get_next_offset(td, f, &io_u->offset)) {
327 io_u->buflen = get_next_buflen(td);
330 io_u->ddir = get_rw_ddir(td);
333 * If using a write iolog, store this entry.
336 write_iolog_put(td, io_u);
346 #define queue_full(td) list_empty(&(td)->io_u_freelist)
348 struct io_u *__get_io_u(struct thread_data *td)
350 struct io_u *io_u = NULL;
352 if (!queue_full(td)) {
353 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
357 list_del(&io_u->list);
358 list_add(&io_u->list, &td->io_u_busylist);
366 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
367 * etc. The returned io_u is fully ready to be prepped and submitted.
369 static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
373 io_u = __get_io_u(td);
377 if (td->zone_bytes >= td->zone_size) {
379 f->last_pos += td->zone_skip;
382 if (fill_io_u(td, f, io_u)) {
387 if (io_u->buflen + io_u->offset > f->file_size) {
388 if (td->io_ops->flags & FIO_RAWIO) {
393 io_u->buflen = f->file_size - io_u->offset;
401 if (!td->read_iolog && !td->sequential)
402 mark_random_map(td, f, io_u);
404 f->last_pos += io_u->buflen;
406 if (td->verify != VERIFY_NONE)
407 populate_verify_io_u(td, io_u);
409 if (td_io_prep(td, io_u)) {
414 gettimeofday(&io_u->start_time, NULL);
418 static inline void td_set_runstate(struct thread_data *td, int runstate)
420 td->runstate = runstate;
423 static struct fio_file *get_next_file(struct thread_data *td)
425 int old_next_file = td->next_file;
429 f = &td->files[td->next_file];
432 if (td->next_file >= td->nr_files)
439 } while (td->next_file != old_next_file);
444 static int td_io_sync(struct thread_data *td, struct fio_file *f)
446 if (td->io_ops->sync)
447 return td->io_ops->sync(td, f);
452 static int td_io_getevents(struct thread_data *td, int min, int max,
455 return td->io_ops->getevents(td, min, max, t);
458 static int td_io_queue(struct thread_data *td, struct io_u *io_u)
460 gettimeofday(&io_u->issue_time, NULL);
462 return td->io_ops->queue(td, io_u);
465 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
467 static void io_completed(struct thread_data *td, struct io_u *io_u,
468 struct io_completion_data *icd)
473 gettimeofday(&e, NULL);
476 unsigned int bytes = io_u->buflen - io_u->resid;
477 const int idx = io_u->ddir;
479 td->io_blocks[idx]++;
480 td->io_bytes[idx] += bytes;
481 td->zone_bytes += bytes;
482 td->this_io_bytes[idx] += bytes;
484 msec = mtime_since(&io_u->issue_time, &e);
486 add_clat_sample(td, idx, msec);
487 add_bw_sample(td, idx);
489 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
490 log_io_piece(td, io_u);
492 icd->bytes_done[idx] += bytes;
494 icd->error = io_u->error;
497 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
503 icd->bytes_done[0] = icd->bytes_done[1] = 0;
505 for (i = 0; i < icd->nr; i++) {
506 io_u = td->io_ops->event(td, i);
508 io_completed(td, io_u, icd);
514 * When job exits, we can cancel the in-flight IO if we are using async
515 * io. Attempt to do so.
517 static void cleanup_pending_aio(struct thread_data *td)
519 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
520 struct list_head *entry, *n;
521 struct io_completion_data icd;
526 * get immediately available events, if any
528 r = td_io_getevents(td, 0, td->cur_depth, &ts);
531 ios_completed(td, &icd);
535 * now cancel remaining active events
537 if (td->io_ops->cancel) {
538 list_for_each_safe(entry, n, &td->io_u_busylist) {
539 io_u = list_entry(entry, struct io_u, list);
541 r = td->io_ops->cancel(td, io_u);
548 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
551 ios_completed(td, &icd);
556 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
558 struct io_u *v_io_u = *io_u;
562 ret = verify_io_u(v_io_u);
563 put_io_u(td, v_io_u);
571 * The main verify engine. Runs over the writes we previusly submitted,
572 * reads the blocks back in, and checks the crc/md5 of the data.
574 static void do_verify(struct thread_data *td)
577 struct io_u *io_u, *v_io_u = NULL;
578 struct io_completion_data icd;
582 td_set_runstate(td, TD_VERIFYING);
588 gettimeofday(&t, NULL);
589 if (runtime_exceeded(td, &t))
592 io_u = __get_io_u(td);
596 if (get_next_verify(td, io_u)) {
601 f = get_next_file(td);
607 if (td_io_prep(td, io_u)) {
612 ret = td_io_queue(td, io_u);
620 * we have one pending to verify, do that while
621 * we are doing io on the next one
623 if (do_io_u_verify(td, &v_io_u))
626 ret = td_io_getevents(td, 1, 1, NULL);
633 v_io_u = td->io_ops->event(td, 0);
636 io_completed(td, v_io_u, &icd);
639 td_verror(td, icd.error);
640 put_io_u(td, v_io_u);
646 * if we can't submit more io, we need to verify now
648 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
653 do_io_u_verify(td, &v_io_u);
656 cleanup_pending_aio(td);
658 td_set_runstate(td, TD_RUNNING);
662 * Not really an io thread, all it does is burn CPU cycles in the specified
665 static void do_cpuio(struct thread_data *td)
668 int split = 100 / td->cpuload;
671 while (!td->terminate) {
672 gettimeofday(&e, NULL);
674 if (runtime_exceeded(td, &e))
680 usec_sleep(td, 10000);
687 * Main IO worker function. It retrieves io_u's to process and queues
688 * and reaps them, checking for rate and errors along the way.
690 static void do_io(struct thread_data *td)
692 struct io_completion_data icd;
698 td_set_runstate(td, TD_RUNNING);
700 while (td->this_io_bytes[td->ddir] < td->io_size) {
701 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
702 struct timespec *timeout;
703 int ret, min_evts = 0;
709 f = get_next_file(td);
713 io_u = get_io_u(td, f);
717 memcpy(&s, &io_u->start_time, sizeof(s));
719 ret = td_io_queue(td, io_u);
726 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
728 if (td->cur_depth < td->iodepth) {
736 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
744 ios_completed(td, &icd);
746 td_verror(td, icd.error);
751 * the rate is batched for now, it should work for batches
752 * of completions except the very first one which may look
755 gettimeofday(&e, NULL);
756 usec = utime_since(&s, &e);
758 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
760 if (check_min_rate(td, &e)) {
761 td_verror(td, ENOMEM);
765 if (runtime_exceeded(td, &e))
769 usec_sleep(td, td->thinktime);
771 if (should_fsync(td) && td->fsync_blocks &&
772 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
777 cleanup_pending_aio(td);
779 if (should_fsync(td) && td->end_fsync) {
780 td_set_runstate(td, TD_FSYNCING);
781 for_each_file(td, f, i)
786 static int td_io_init(struct thread_data *td)
788 if (td->io_ops->init)
789 return td->io_ops->init(td);
794 static void cleanup_io_u(struct thread_data *td)
796 struct list_head *entry, *n;
799 list_for_each_safe(entry, n, &td->io_u_freelist) {
800 io_u = list_entry(entry, struct io_u, list);
802 list_del(&io_u->list);
806 if (td->mem_type == MEM_MALLOC)
807 free(td->orig_buffer);
808 else if (td->mem_type == MEM_SHM) {
809 struct shmid_ds sbuf;
811 shmdt(td->orig_buffer);
812 shmctl(td->shm_id, IPC_RMID, &sbuf);
813 } else if (td->mem_type == MEM_MMAP)
814 munmap(td->orig_buffer, td->orig_buffer_size);
816 log_err("Bad memory type %d\n", td->mem_type);
818 td->orig_buffer = NULL;
821 static int init_io_u(struct thread_data *td)
827 if (td->io_ops->flags & FIO_CPUIO)
830 if (td->io_ops->flags & FIO_SYNCIO)
833 max_units = td->iodepth;
835 td->orig_buffer_size = td->max_bs * max_units + MASK;
837 if (td->mem_type == MEM_MALLOC)
838 td->orig_buffer = malloc(td->orig_buffer_size);
839 else if (td->mem_type == MEM_SHM) {
840 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
841 if (td->shm_id < 0) {
842 td_verror(td, errno);
847 td->orig_buffer = shmat(td->shm_id, NULL, 0);
848 if (td->orig_buffer == (void *) -1) {
849 td_verror(td, errno);
851 td->orig_buffer = NULL;
854 } else if (td->mem_type == MEM_MMAP) {
855 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
856 if (td->orig_buffer == MAP_FAILED) {
857 td_verror(td, errno);
859 td->orig_buffer = NULL;
864 p = ALIGN(td->orig_buffer);
865 for (i = 0; i < max_units; i++) {
866 io_u = malloc(sizeof(*io_u));
867 memset(io_u, 0, sizeof(*io_u));
868 INIT_LIST_HEAD(&io_u->list);
870 io_u->buf = p + td->max_bs * i;
872 list_add(&io_u->list, &td->io_u_freelist);
878 static int switch_ioscheduler(struct thread_data *td)
880 char tmp[256], tmp2[128];
884 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
886 f = fopen(tmp, "r+");
888 td_verror(td, errno);
895 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
896 if (ferror(f) || ret != 1) {
897 td_verror(td, errno);
905 * Read back and check that the selected scheduler is now the default.
907 ret = fread(tmp, 1, sizeof(tmp), f);
908 if (ferror(f) || ret < 0) {
909 td_verror(td, errno);
914 sprintf(tmp2, "[%s]", td->ioscheduler);
915 if (!strstr(tmp, tmp2)) {
916 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
917 td_verror(td, EINVAL);
926 static void clear_io_state(struct thread_data *td)
931 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
932 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
935 for_each_file(td, f, i) {
937 if (td->io_ops->flags & FIO_SYNCIO)
938 lseek(f->fd, SEEK_SET, 0);
941 memset(f->file_map, 0, f->num_maps * sizeof(long));
946 * Entry point for the thread based jobs. The process based jobs end up
947 * here as well, after a little setup.
949 static void *thread_main(void *data)
951 struct thread_data *td = data;
958 INIT_LIST_HEAD(&td->io_u_freelist);
959 INIT_LIST_HEAD(&td->io_u_busylist);
960 INIT_LIST_HEAD(&td->io_hist_list);
961 INIT_LIST_HEAD(&td->io_log_list);
966 if (fio_setaffinity(td) == -1) {
967 td_verror(td, errno);
978 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
979 td_verror(td, errno);
984 if (nice(td->nice) == -1) {
985 td_verror(td, errno);
989 if (init_random_state(td))
992 if (td->ioscheduler && switch_ioscheduler(td))
995 td_set_runstate(td, TD_INITIALIZED);
996 fio_sem_up(&startup_sem);
997 fio_sem_down(&td->mutex);
999 if (!td->create_serialize && setup_files(td))
1002 gettimeofday(&td->epoch, NULL);
1004 if (td->exec_prerun)
1005 system(td->exec_prerun);
1007 while (td->loops--) {
1008 getrusage(RUSAGE_SELF, &td->ru_start);
1009 gettimeofday(&td->start, NULL);
1010 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1013 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1016 prune_io_piece_log(td);
1018 if (td->io_ops->flags & FIO_CPUIO)
1023 td->runtime[td->ddir] += mtime_since_now(&td->start);
1024 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1025 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1027 update_rusage_stat(td);
1029 if (td->error || td->terminate)
1032 if (td->verify == VERIFY_NONE)
1036 gettimeofday(&td->start, NULL);
1040 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1042 if (td->error || td->terminate)
1047 finish_log(td, td->bw_log, "bw");
1049 finish_log(td, td->slat_log, "slat");
1051 finish_log(td, td->clat_log, "clat");
1052 if (td->write_iolog)
1053 write_iolog_close(td);
1054 if (td->exec_postrun)
1055 system(td->exec_postrun);
1057 if (exitall_on_terminate)
1058 terminate_threads(td->groupid);
1064 td_set_runstate(td, TD_EXITED);
1070 * We cannot pass the td data into a forked process, so attach the td and
1071 * pass it to the thread worker.
1073 static void *fork_main(int shmid, int offset)
1075 struct thread_data *td;
1078 data = shmat(shmid, NULL, 0);
1079 if (data == (void *) -1) {
1084 td = data + offset * sizeof(struct thread_data);
1091 * Run over the job map and reap the threads that have exited, if any.
1093 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1098 * reap exited threads (TD_EXITED -> TD_REAPED)
1100 for (i = 0, cputhreads = 0; i < thread_number; i++) {
1101 struct thread_data *td = &threads[i];
1103 if (td->io_ops->flags & FIO_CPUIO)
1106 if (td->runstate != TD_EXITED)
1109 td_set_runstate(td, TD_REAPED);
1111 if (td->use_thread) {
1114 if (pthread_join(td->thread, (void *) &ret))
1115 perror("thread_join");
1117 waitpid(td->pid, NULL, 0);
1120 (*m_rate) -= td->ratemin;
1121 (*t_rate) -= td->rate;
1124 if (*nr_running == cputhreads)
1125 terminate_threads(TERMINATE_ALL);
1128 static void fio_unpin_memory(void *pinned)
1131 if (munlock(pinned, mlock_size) < 0)
1133 munmap(pinned, mlock_size);
1137 static void *fio_pin_memory(void)
1139 unsigned long long phys_mem;
1146 * Don't allow mlock of more than real_mem-128MB
1148 phys_mem = os_phys_mem();
1150 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1151 mlock_size = phys_mem - 128 * 1024 * 1024;
1152 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1156 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1158 perror("malloc locked mem");
1161 if (mlock(ptr, mlock_size) < 0) {
1162 munmap(ptr, mlock_size);
1171 * Main function for kicking off and reaping jobs, as needed.
1173 static void run_threads(void)
1175 struct thread_data *td;
1176 unsigned long spent;
1177 int i, todo, nr_running, m_rate, t_rate, nr_started;
1180 mlocked_mem = fio_pin_memory();
1182 if (!terse_output) {
1183 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1187 signal(SIGINT, sig_handler);
1188 signal(SIGALRM, sig_handler);
1190 todo = thread_number;
1193 m_rate = t_rate = 0;
1195 for (i = 0; i < thread_number; i++) {
1198 print_status_init(td->thread_number - 1);
1202 if (!td->create_serialize)
1206 * do file setup here so it happens sequentially,
1207 * we don't want X number of threads getting their
1208 * client data interspersed on disk
1210 if (setup_files(td)) {
1211 td_set_runstate(td, TD_REAPED);
1219 struct thread_data *map[MAX_JOBS];
1220 struct timeval this_start;
1221 int this_jobs = 0, left;
1224 * create threads (TD_NOT_CREATED -> TD_CREATED)
1226 for (i = 0; i < thread_number; i++) {
1229 if (td->runstate != TD_NOT_CREATED)
1233 * never got a chance to start, killed by other
1234 * thread for some reason
1236 if (td->terminate) {
1241 if (td->start_delay) {
1242 spent = mtime_since_genesis();
1244 if (td->start_delay * 1000 > spent)
1248 if (td->stonewall && (nr_started || nr_running))
1252 * Set state to created. Thread will transition
1253 * to TD_INITIALIZED when it's done setting up.
1255 td_set_runstate(td, TD_CREATED);
1256 map[this_jobs++] = td;
1257 fio_sem_init(&startup_sem, 1);
1260 if (td->use_thread) {
1261 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1262 perror("thread_create");
1267 fio_sem_down(&startup_sem);
1269 fork_main(shm_id, i);
1276 * Wait for the started threads to transition to
1279 gettimeofday(&this_start, NULL);
1282 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1287 for (i = 0; i < this_jobs; i++) {
1291 if (td->runstate == TD_INITIALIZED) {
1294 } else if (td->runstate >= TD_EXITED) {
1298 nr_running++; /* work-around... */
1304 log_err("fio: %d jobs failed to start\n", left);
1305 for (i = 0; i < this_jobs; i++) {
1309 kill(td->pid, SIGTERM);
1315 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1317 for (i = 0; i < thread_number; i++) {
1320 if (td->runstate != TD_INITIALIZED)
1323 td_set_runstate(td, TD_RUNNING);
1326 m_rate += td->ratemin;
1329 fio_sem_up(&td->mutex);
1332 reap_threads(&nr_running, &t_rate, &m_rate);
1338 while (nr_running) {
1339 reap_threads(&nr_running, &t_rate, &m_rate);
1344 fio_unpin_memory(mlocked_mem);
1347 int main(int argc, char *argv[])
1349 if (parse_options(argc, argv))
1352 if (!thread_number) {
1353 log_err("Nothing to do\n");
1357 disk_util_timer_arm();