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)
59 struct thread_data *td;
63 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
70 static void sig_handler(int sig)
75 disk_util_timer_arm();
76 print_thread_status();
79 printf("\nfio: terminating on signal\n");
81 terminate_threads(TERMINATE_ALL);
87 * The ->file_map[] contains a map of blocks we have or have not done io
88 * to yet. Used to make sure we cover the entire range in a fair fashion.
90 static int random_map_free(struct thread_data *td, struct fio_file *f,
91 unsigned long long block)
93 unsigned int idx = RAND_MAP_IDX(td, f, block);
94 unsigned int bit = RAND_MAP_BIT(td, f, block);
96 return (f->file_map[idx] & (1UL << bit)) == 0;
100 * Return the next free block in the map.
102 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
103 unsigned long long *b)
109 while ((*b) * td->min_bs < f->file_size) {
110 if (f->file_map[i] != -1UL) {
111 *b += ffz(f->file_map[i]);
115 *b += BLOCKS_PER_MAP;
123 * Mark a given offset as used in the map.
125 static void mark_random_map(struct thread_data *td, struct fio_file *f,
128 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
129 unsigned int blocks = 0;
131 while (blocks < (io_u->buflen / td->min_bs)) {
132 unsigned int idx, bit;
134 if (!random_map_free(td, f, block))
137 idx = RAND_MAP_IDX(td, f, block);
138 bit = RAND_MAP_BIT(td, f, block);
140 assert(idx < f->num_maps);
142 f->file_map[idx] |= (1UL << bit);
147 if ((blocks * td->min_bs) < io_u->buflen)
148 io_u->buflen = blocks * td->min_bs;
152 * For random io, generate a random new block and see if it's used. Repeat
153 * until we find a free one. For sequential io, just return the end of
154 * the last io issued.
156 static int get_next_offset(struct thread_data *td, struct fio_file *f,
157 unsigned long long *offset)
159 unsigned long long b, rb;
162 if (!td->sequential) {
163 unsigned long long max_blocks = td->io_size / td->min_bs;
167 r = os_random_long(&td->random_state);
168 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
169 rb = b + (f->file_offset / td->min_bs);
171 } while (!random_map_free(td, f, rb) && loops);
174 if (get_next_free_block(td, f, &b))
178 b = f->last_pos / td->min_bs;
180 *offset = (b * td->min_bs) + f->file_offset;
181 if (*offset > f->file_size)
187 static unsigned int get_next_buflen(struct thread_data *td)
192 if (td->min_bs == td->max_bs)
195 r = os_random_long(&td->bsrange_state);
196 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
197 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
200 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
202 * if using direct/raw io, we may not be able to
203 * shrink the size. so just fail it.
205 if (td->io_ops->flags & FIO_RAWIO)
208 buflen = td->io_size - td->this_io_bytes[td->ddir];
215 * Check if we are above the minimum rate given.
217 static int check_min_rate(struct thread_data *td, struct timeval *now)
224 * allow a 2 second settle period in the beginning
226 if (mtime_since(&td->start, now) < 2000)
230 * if rate blocks is set, sample is running
232 if (td->rate_bytes) {
233 spent = mtime_since(&td->lastrate, now);
234 if (spent < td->ratecycle)
237 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
238 if (rate < td->ratemin) {
239 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
241 terminate_threads(td->groupid);
246 td->rate_bytes = td->this_io_bytes[ddir];
247 memcpy(&td->lastrate, now, sizeof(*now));
251 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
255 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
262 * Return the data direction for the next io_u. If the job is a
263 * mixed read/write workload, check the rwmix cycle and switch if
266 static int get_rw_ddir(struct thread_data *td)
270 unsigned long elapsed;
272 gettimeofday(&now, NULL);
273 elapsed = mtime_since_now(&td->rwmix_switch);
276 * Check if it's time to seed a new data direction.
278 if (elapsed >= td->rwmixcycle) {
282 r = os_random_long(&td->rwmix_state);
283 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
284 if (v < td->rwmixread)
285 td->rwmix_ddir = DDIR_READ;
287 td->rwmix_ddir = DDIR_WRITE;
288 memcpy(&td->rwmix_switch, &now, sizeof(now));
290 return td->rwmix_ddir;
291 } else if (td_read(td))
297 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
299 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
305 void put_io_u(struct thread_data *td, struct io_u *io_u)
308 list_del(&io_u->list);
309 list_add(&io_u->list, &td->io_u_freelist);
313 static int fill_io_u(struct thread_data *td, struct fio_file *f,
317 * If using an iolog, grab next piece if any available.
320 return read_iolog_get(td, io_u);
323 * No log, let the seq/rand engine retrieve the next position.
325 if (!get_next_offset(td, f, &io_u->offset)) {
326 io_u->buflen = get_next_buflen(td);
329 io_u->ddir = get_rw_ddir(td);
332 * If using a write iolog, store this entry.
335 write_iolog_put(td, io_u);
345 #define queue_full(td) list_empty(&(td)->io_u_freelist)
347 struct io_u *__get_io_u(struct thread_data *td)
349 struct io_u *io_u = NULL;
351 if (!queue_full(td)) {
352 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
356 list_del(&io_u->list);
357 list_add(&io_u->list, &td->io_u_busylist);
365 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
366 * etc. The returned io_u is fully ready to be prepped and submitted.
368 static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
372 io_u = __get_io_u(td);
376 if (td->zone_bytes >= td->zone_size) {
378 f->last_pos += td->zone_skip;
381 if (fill_io_u(td, f, io_u)) {
386 if (io_u->buflen + io_u->offset > f->file_size) {
387 if (td->io_ops->flags & FIO_RAWIO) {
392 io_u->buflen = f->file_size - io_u->offset;
400 if (!td->read_iolog && !td->sequential)
401 mark_random_map(td, f, io_u);
403 f->last_pos += io_u->buflen;
405 if (td->verify != VERIFY_NONE)
406 populate_verify_io_u(td, io_u);
408 if (td_io_prep(td, io_u)) {
413 gettimeofday(&io_u->start_time, NULL);
417 static inline void td_set_runstate(struct thread_data *td, int runstate)
419 td->runstate = runstate;
422 static struct fio_file *get_next_file(struct thread_data *td)
424 unsigned int old_next_file = td->next_file;
428 f = &td->files[td->next_file];
431 if (td->next_file >= td->nr_files)
438 } while (td->next_file != old_next_file);
443 static int td_io_sync(struct thread_data *td, struct fio_file *f)
445 if (td->io_ops->sync)
446 return td->io_ops->sync(td, f);
451 static int td_io_getevents(struct thread_data *td, int min, int max,
454 return td->io_ops->getevents(td, min, max, t);
457 static int td_io_queue(struct thread_data *td, struct io_u *io_u)
459 gettimeofday(&io_u->issue_time, NULL);
461 return td->io_ops->queue(td, io_u);
464 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
466 static void io_completed(struct thread_data *td, struct io_u *io_u,
467 struct io_completion_data *icd)
472 gettimeofday(&e, NULL);
475 unsigned int bytes = io_u->buflen - io_u->resid;
476 const int idx = io_u->ddir;
478 td->io_blocks[idx]++;
479 td->io_bytes[idx] += bytes;
480 td->zone_bytes += bytes;
481 td->this_io_bytes[idx] += bytes;
483 msec = mtime_since(&io_u->issue_time, &e);
485 add_clat_sample(td, idx, msec);
486 add_bw_sample(td, idx);
488 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
489 log_io_piece(td, io_u);
491 icd->bytes_done[idx] += bytes;
493 icd->error = io_u->error;
496 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
502 icd->bytes_done[0] = icd->bytes_done[1] = 0;
504 for (i = 0; i < icd->nr; i++) {
505 io_u = td->io_ops->event(td, i);
507 io_completed(td, io_u, icd);
513 * When job exits, we can cancel the in-flight IO if we are using async
514 * io. Attempt to do so.
516 static void cleanup_pending_aio(struct thread_data *td)
518 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
519 struct list_head *entry, *n;
520 struct io_completion_data icd;
525 * get immediately available events, if any
527 r = td_io_getevents(td, 0, td->cur_depth, &ts);
530 ios_completed(td, &icd);
534 * now cancel remaining active events
536 if (td->io_ops->cancel) {
537 list_for_each_safe(entry, n, &td->io_u_busylist) {
538 io_u = list_entry(entry, struct io_u, list);
540 r = td->io_ops->cancel(td, io_u);
547 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
550 ios_completed(td, &icd);
556 * The main verify engine. Runs over the writes we previusly submitted,
557 * reads the blocks back in, and checks the crc/md5 of the data.
559 void do_verify(struct thread_data *td)
562 struct io_u *io_u, *v_io_u = NULL;
563 struct io_completion_data icd;
568 * sync io first and invalidate cache, to make sure we really
571 for_each_file(td, f, i) {
573 file_invalidate_cache(td, f);
576 td_set_runstate(td, TD_VERIFYING);
582 gettimeofday(&t, NULL);
583 if (runtime_exceeded(td, &t))
586 io_u = __get_io_u(td);
590 if (get_next_verify(td, io_u)) {
595 f = get_next_file(td);
601 if (td_io_prep(td, io_u)) {
606 ret = td_io_queue(td, io_u);
614 * we have one pending to verify, do that while
615 * we are doing io on the next one
617 if (do_io_u_verify(td, &v_io_u))
620 ret = td_io_getevents(td, 1, 1, NULL);
627 v_io_u = td->io_ops->event(td, 0);
630 io_completed(td, v_io_u, &icd);
633 td_verror(td, icd.error);
634 put_io_u(td, v_io_u);
640 * if we can't submit more io, we need to verify now
642 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
647 do_io_u_verify(td, &v_io_u);
650 cleanup_pending_aio(td);
652 td_set_runstate(td, TD_RUNNING);
656 * Not really an io thread, all it does is burn CPU cycles in the specified
659 static void do_cpuio(struct thread_data *td)
662 int split = 100 / td->cpuload;
665 while (!td->terminate) {
666 gettimeofday(&e, NULL);
668 if (runtime_exceeded(td, &e))
674 usec_sleep(td, 10000);
681 * Main IO worker function. It retrieves io_u's to process and queues
682 * and reaps them, checking for rate and errors along the way.
684 static void do_io(struct thread_data *td)
686 struct io_completion_data icd;
692 td_set_runstate(td, TD_RUNNING);
694 while (td->this_io_bytes[td->ddir] < td->io_size) {
695 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
696 struct timespec *timeout;
703 f = get_next_file(td);
707 io_u = get_io_u(td, f);
711 memcpy(&s, &io_u->start_time, sizeof(s));
713 ret = td_io_queue(td, io_u);
720 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
722 if (td->cur_depth < td->iodepth) {
731 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
739 ios_completed(td, &icd);
741 td_verror(td, icd.error);
746 * the rate is batched for now, it should work for batches
747 * of completions except the very first one which may look
750 gettimeofday(&e, NULL);
751 usec = utime_since(&s, &e);
753 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
755 if (check_min_rate(td, &e)) {
756 td_verror(td, ENOMEM);
760 if (runtime_exceeded(td, &e))
764 usec_sleep(td, td->thinktime);
766 if (should_fsync(td) && td->fsync_blocks &&
767 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
773 cleanup_pending_aio(td);
775 if (should_fsync(td) && td->end_fsync) {
776 td_set_runstate(td, TD_FSYNCING);
777 for_each_file(td, f, i)
783 static int td_io_init(struct thread_data *td)
785 if (td->io_ops->init)
786 return td->io_ops->init(td);
791 static void cleanup_io_u(struct thread_data *td)
793 struct list_head *entry, *n;
796 list_for_each_safe(entry, n, &td->io_u_freelist) {
797 io_u = list_entry(entry, struct io_u, list);
799 list_del(&io_u->list);
803 if (td->mem_type == MEM_MALLOC)
804 free(td->orig_buffer);
805 else if (td->mem_type == MEM_SHM) {
806 struct shmid_ds sbuf;
808 shmdt(td->orig_buffer);
809 shmctl(td->shm_id, IPC_RMID, &sbuf);
810 } else if (td->mem_type == MEM_MMAP)
811 munmap(td->orig_buffer, td->orig_buffer_size);
813 log_err("Bad memory type %d\n", td->mem_type);
815 td->orig_buffer = NULL;
818 static int init_io_u(struct thread_data *td)
824 if (td->io_ops->flags & FIO_CPUIO)
827 if (td->io_ops->flags & FIO_SYNCIO)
830 max_units = td->iodepth;
832 td->orig_buffer_size = td->max_bs * max_units + MASK;
834 if (td->mem_type == MEM_MALLOC)
835 td->orig_buffer = malloc(td->orig_buffer_size);
836 else if (td->mem_type == MEM_SHM) {
837 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
838 if (td->shm_id < 0) {
839 td_verror(td, errno);
844 td->orig_buffer = shmat(td->shm_id, NULL, 0);
845 if (td->orig_buffer == (void *) -1) {
846 td_verror(td, errno);
848 td->orig_buffer = NULL;
851 } else if (td->mem_type == MEM_MMAP) {
852 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
853 if (td->orig_buffer == MAP_FAILED) {
854 td_verror(td, errno);
856 td->orig_buffer = NULL;
861 p = ALIGN(td->orig_buffer);
862 for (i = 0; i < max_units; i++) {
863 io_u = malloc(sizeof(*io_u));
864 memset(io_u, 0, sizeof(*io_u));
865 INIT_LIST_HEAD(&io_u->list);
867 io_u->buf = p + td->max_bs * i;
869 list_add(&io_u->list, &td->io_u_freelist);
875 static int switch_ioscheduler(struct thread_data *td)
877 char tmp[256], tmp2[128];
881 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
883 f = fopen(tmp, "r+");
885 td_verror(td, errno);
892 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
893 if (ferror(f) || ret != 1) {
894 td_verror(td, errno);
902 * Read back and check that the selected scheduler is now the default.
904 ret = fread(tmp, 1, sizeof(tmp), f);
905 if (ferror(f) || ret < 0) {
906 td_verror(td, errno);
911 sprintf(tmp2, "[%s]", td->ioscheduler);
912 if (!strstr(tmp, tmp2)) {
913 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
914 td_verror(td, EINVAL);
923 static void clear_io_state(struct thread_data *td)
928 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
929 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
932 for_each_file(td, f, i) {
934 if (td->io_ops->flags & FIO_SYNCIO)
935 lseek(f->fd, SEEK_SET, 0);
938 memset(f->file_map, 0, f->num_maps * sizeof(long));
943 * Entry point for the thread based jobs. The process based jobs end up
944 * here as well, after a little setup.
946 static void *thread_main(void *data)
948 struct thread_data *td = data;
955 INIT_LIST_HEAD(&td->io_u_freelist);
956 INIT_LIST_HEAD(&td->io_u_busylist);
957 INIT_LIST_HEAD(&td->io_hist_list);
958 INIT_LIST_HEAD(&td->io_log_list);
963 if (fio_setaffinity(td) == -1) {
964 td_verror(td, errno);
975 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
976 td_verror(td, errno);
981 if (nice(td->nice) == -1) {
982 td_verror(td, errno);
986 if (init_random_state(td))
989 if (td->ioscheduler && switch_ioscheduler(td))
992 td_set_runstate(td, TD_INITIALIZED);
993 fio_sem_up(&startup_sem);
994 fio_sem_down(&td->mutex);
996 if (!td->create_serialize && setup_files(td))
999 gettimeofday(&td->epoch, NULL);
1001 if (td->exec_prerun)
1002 system(td->exec_prerun);
1004 while (td->loops--) {
1005 getrusage(RUSAGE_SELF, &td->ru_start);
1006 gettimeofday(&td->start, NULL);
1007 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1010 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1013 prune_io_piece_log(td);
1015 if (td->io_ops->flags & FIO_CPUIO)
1020 td->runtime[td->ddir] += mtime_since_now(&td->start);
1021 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1022 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1024 update_rusage_stat(td);
1026 if (td->error || td->terminate)
1029 if (td->verify == VERIFY_NONE)
1033 gettimeofday(&td->start, NULL);
1037 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1039 if (td->error || td->terminate)
1044 finish_log(td, td->bw_log, "bw");
1046 finish_log(td, td->slat_log, "slat");
1048 finish_log(td, td->clat_log, "clat");
1049 if (td->write_iolog)
1050 write_iolog_close(td);
1051 if (td->exec_postrun)
1052 system(td->exec_postrun);
1054 if (exitall_on_terminate)
1055 terminate_threads(td->groupid);
1061 td_set_runstate(td, TD_EXITED);
1067 * We cannot pass the td data into a forked process, so attach the td and
1068 * pass it to the thread worker.
1070 static void *fork_main(int shmid, int offset)
1072 struct thread_data *td;
1075 data = shmat(shmid, NULL, 0);
1076 if (data == (void *) -1) {
1081 td = data + offset * sizeof(struct thread_data);
1088 * Run over the job map and reap the threads that have exited, if any.
1090 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1092 struct thread_data *td;
1096 * reap exited threads (TD_EXITED -> TD_REAPED)
1099 for_each_td(td, i) {
1101 * ->io_ops is NULL for a thread that has closed its
1104 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
1107 if (td->runstate != TD_EXITED)
1110 td_set_runstate(td, TD_REAPED);
1112 if (td->use_thread) {
1115 if (pthread_join(td->thread, (void *) &ret))
1116 perror("thread_join");
1118 waitpid(td->pid, NULL, 0);
1121 (*m_rate) -= td->ratemin;
1122 (*t_rate) -= td->rate;
1125 if (*nr_running == cputhreads)
1126 terminate_threads(TERMINATE_ALL);
1129 static void fio_unpin_memory(void *pinned)
1132 if (munlock(pinned, mlock_size) < 0)
1134 munmap(pinned, mlock_size);
1138 static void *fio_pin_memory(void)
1140 unsigned long long phys_mem;
1147 * Don't allow mlock of more than real_mem-128MB
1149 phys_mem = os_phys_mem();
1151 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1152 mlock_size = phys_mem - 128 * 1024 * 1024;
1153 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1157 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1159 perror("malloc locked mem");
1162 if (mlock(ptr, mlock_size) < 0) {
1163 munmap(ptr, mlock_size);
1172 * Main function for kicking off and reaping jobs, as needed.
1174 static void run_threads(void)
1176 struct thread_data *td;
1177 unsigned long spent;
1178 int i, todo, nr_running, m_rate, t_rate, nr_started;
1181 mlocked_mem = fio_pin_memory();
1183 if (!terse_output) {
1184 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1188 signal(SIGINT, sig_handler);
1189 signal(SIGALRM, sig_handler);
1191 todo = thread_number;
1194 m_rate = t_rate = 0;
1196 for_each_td(td, i) {
1197 print_status_init(td->thread_number - 1);
1201 if (!td->create_serialize)
1205 * do file setup here so it happens sequentially,
1206 * we don't want X number of threads getting their
1207 * client data interspersed on disk
1209 if (setup_files(td)) {
1210 td_set_runstate(td, TD_REAPED);
1218 struct thread_data *map[MAX_JOBS];
1219 struct timeval this_start;
1220 int this_jobs = 0, left;
1223 * create threads (TD_NOT_CREATED -> TD_CREATED)
1225 for_each_td(td, i) {
1226 if (td->runstate != TD_NOT_CREATED)
1230 * never got a chance to start, killed by other
1231 * thread for some reason
1233 if (td->terminate) {
1238 if (td->start_delay) {
1239 spent = mtime_since_genesis();
1241 if (td->start_delay * 1000 > spent)
1245 if (td->stonewall && (nr_started || nr_running))
1249 * Set state to created. Thread will transition
1250 * to TD_INITIALIZED when it's done setting up.
1252 td_set_runstate(td, TD_CREATED);
1253 map[this_jobs++] = td;
1254 fio_sem_init(&startup_sem, 1);
1257 if (td->use_thread) {
1258 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1259 perror("thread_create");
1264 fio_sem_down(&startup_sem);
1266 fork_main(shm_id, i);
1273 * Wait for the started threads to transition to
1276 gettimeofday(&this_start, NULL);
1279 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1284 for (i = 0; i < this_jobs; i++) {
1288 if (td->runstate == TD_INITIALIZED) {
1291 } else if (td->runstate >= TD_EXITED) {
1295 nr_running++; /* work-around... */
1301 log_err("fio: %d jobs failed to start\n", left);
1302 for (i = 0; i < this_jobs; i++) {
1306 kill(td->pid, SIGTERM);
1312 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1314 for_each_td(td, i) {
1315 if (td->runstate != TD_INITIALIZED)
1318 td_set_runstate(td, TD_RUNNING);
1321 m_rate += td->ratemin;
1324 fio_sem_up(&td->mutex);
1327 reap_threads(&nr_running, &t_rate, &m_rate);
1333 while (nr_running) {
1334 reap_threads(&nr_running, &t_rate, &m_rate);
1339 fio_unpin_memory(mlocked_mem);
1342 int main(int argc, char *argv[])
1344 if (parse_options(argc, argv))
1347 if (!thread_number) {
1348 log_err("Nothing to do\n");
1352 disk_util_timer_arm();