[PATCH] Remove the time init check
[fio.git] / fio.c
1 /*
2  * fio - the flexible io tester
3  *
4  * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5  * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
6  *
7  * The license below covers all files distributed with fio unless otherwise
8  * noted in the file itself.
9  *
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.
13  *
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.
18  *
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
22  *
23  */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <time.h>
29 #include <assert.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <sys/ipc.h>
33 #include <sys/shm.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36
37 #include "fio.h"
38 #include "os.h"
39
40 #define MASK    (4095)
41
42 #define ALIGN(buf)      (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43
44 int groupid = 0;
45 int thread_number = 0;
46 int shm_id = 0;
47 int temp_stall_ts;
48
49 static volatile int startup_sem;
50 static volatile int fio_abort;
51
52 #define TERMINATE_ALL           (-1)
53 #define JOB_START_TIMEOUT       (5 * 1000)
54
55 static inline void td_set_runstate(struct thread_data *td, int runstate)
56 {
57         td->runstate = runstate;
58 }
59
60 static void terminate_threads(int group_id, int forced_kill)
61 {
62         struct thread_data *td;
63         int i;
64
65         for_each_td(td, i) {
66                 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
67                         td->terminate = 1;
68                         td->start_delay = 0;
69                         if (forced_kill)
70                                 td_set_runstate(td, TD_EXITED);
71                 }
72         }
73 }
74
75 static void sig_handler(int sig)
76 {
77         switch (sig) {
78                 case SIGALRM:
79                         update_io_ticks();
80                         disk_util_timer_arm();
81                         print_thread_status();
82                         break;
83                 case SIGSEGV:
84                         fprintf(stderr, "fio: got segfault, aborting\n");
85                         terminate_threads(TERMINATE_ALL, 1);
86                         fio_abort = 1;
87                         exit(0);
88                 default:
89                         printf("\nfio: terminating on signal %d\n", sig);
90                         fflush(stdout);
91                         terminate_threads(TERMINATE_ALL, 0);
92                         break;
93         }
94 }
95
96 /*
97  * Check if we are above the minimum rate given.
98  */
99 static int check_min_rate(struct thread_data *td, struct timeval *now)
100 {
101         unsigned long spent;
102         unsigned long rate;
103         int ddir = td->ddir;
104
105         /*
106          * allow a 2 second settle period in the beginning
107          */
108         if (mtime_since(&td->start, now) < 2000)
109                 return 0;
110
111         /*
112          * if rate blocks is set, sample is running
113          */
114         if (td->rate_bytes) {
115                 spent = mtime_since(&td->lastrate, now);
116                 if (spent < td->ratecycle)
117                         return 0;
118
119                 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
120                 if (rate < td->ratemin) {
121                         fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate);
122                         return 1;
123                 }
124         }
125
126         td->rate_bytes = td->this_io_bytes[ddir];
127         memcpy(&td->lastrate, now, sizeof(*now));
128         return 0;
129 }
130
131 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
132 {
133         if (!td->timeout)
134                 return 0;
135         if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
136                 return 1;
137
138         return 0;
139 }
140
141 static struct fio_file *get_next_file(struct thread_data *td)
142 {
143         unsigned int old_next_file = td->next_file;
144         struct fio_file *f;
145
146         do {
147                 f = &td->files[td->next_file];
148
149                 td->next_file++;
150                 if (td->next_file >= td->nr_files)
151                         td->next_file = 0;
152
153                 if (f->fd != -1)
154                         break;
155
156                 f = NULL;
157         } while (td->next_file != old_next_file);
158
159         return f;
160 }
161
162 /*
163  * When job exits, we can cancel the in-flight IO if we are using async
164  * io. Attempt to do so.
165  */
166 static void cleanup_pending_aio(struct thread_data *td)
167 {
168         struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
169         struct list_head *entry, *n;
170         struct io_completion_data icd;
171         struct io_u *io_u;
172         int r;
173
174         /*
175          * get immediately available events, if any
176          */
177         r = td_io_getevents(td, 0, td->cur_depth, &ts);
178         if (r > 0) {
179                 icd.nr = r;
180                 ios_completed(td, &icd);
181         }
182
183         /*
184          * now cancel remaining active events
185          */
186         if (td->io_ops->cancel) {
187                 list_for_each_safe(entry, n, &td->io_u_busylist) {
188                         io_u = list_entry(entry, struct io_u, list);
189
190                         r = td->io_ops->cancel(td, io_u);
191                         if (!r)
192                                 put_io_u(td, io_u);
193                 }
194         }
195
196         if (td->cur_depth) {
197                 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
198                 if (r > 0) {
199                         icd.nr = r;
200                         ios_completed(td, &icd);
201                 }
202         }
203 }
204
205 /*
206  * Helper to handle the final sync of a file. Works just like the normal
207  * io path, just does everything sync.
208  */
209 static int fio_io_sync(struct thread_data *td, struct fio_file *f)
210 {
211         struct io_u *io_u = __get_io_u(td);
212         struct io_completion_data icd;
213         int ret;
214
215         if (!io_u)
216                 return 1;
217
218         io_u->ddir = DDIR_SYNC;
219         io_u->file = f;
220
221         if (td_io_prep(td, io_u)) {
222                 put_io_u(td, io_u);
223                 return 1;
224         }
225
226         ret = td_io_queue(td, io_u);
227         if (ret) {
228                 td_verror(td, io_u->error);
229                 put_io_u(td, io_u);
230                 return 1;
231         }
232
233         ret = td_io_getevents(td, 1, td->cur_depth, NULL);
234         if (ret < 0) {
235                 td_verror(td, ret);
236                 return 1;
237         }
238
239         icd.nr = ret;
240         ios_completed(td, &icd);
241         if (icd.error) {
242                 td_verror(td, icd.error);
243                 return 1;
244         }
245
246         return 0;
247 }
248
249 /*
250  * The main verify engine. Runs over the writes we previusly submitted,
251  * reads the blocks back in, and checks the crc/md5 of the data.
252  */
253 static void do_verify(struct thread_data *td)
254 {
255         struct io_u *io_u, *v_io_u = NULL;
256         struct io_completion_data icd;
257         struct fio_file *f;
258         int ret, i;
259
260         /*
261          * sync io first and invalidate cache, to make sure we really
262          * read from disk.
263          */
264         for_each_file(td, f, i) {
265                 fio_io_sync(td, f);
266                 file_invalidate_cache(td, f);
267         }
268
269         td_set_runstate(td, TD_VERIFYING);
270
271         do {
272                 if (td->terminate)
273                         break;
274
275                 io_u = __get_io_u(td);
276                 if (!io_u)
277                         break;
278
279                 if (runtime_exceeded(td, &io_u->start_time)) {
280                         put_io_u(td, io_u);
281                         break;
282                 }
283
284                 if (get_next_verify(td, io_u)) {
285                         put_io_u(td, io_u);
286                         break;
287                 }
288
289                 f = get_next_file(td);
290                 if (!f)
291                         break;
292
293                 io_u->file = f;
294
295                 if (td_io_prep(td, io_u)) {
296                         put_io_u(td, io_u);
297                         break;
298                 }
299
300                 ret = td_io_queue(td, io_u);
301                 if (ret) {
302                         td_verror(td, io_u->error);
303                         put_io_u(td, io_u);
304                         break;
305                 }
306
307                 /*
308                  * we have one pending to verify, do that while
309                  * we are doing io on the next one
310                  */
311                 if (do_io_u_verify(td, &v_io_u))
312                         break;
313
314                 ret = td_io_getevents(td, 1, 1, NULL);
315                 if (ret != 1) {
316                         if (ret < 0)
317                                 td_verror(td, ret);
318                         break;
319                 }
320
321                 v_io_u = td->io_ops->event(td, 0);
322                 icd.nr = 1;
323                 icd.error = 0;
324                 fio_gettime(&icd.time, NULL);
325                 io_completed(td, v_io_u, &icd);
326
327                 if (icd.error) {
328                         td_verror(td, icd.error);
329                         put_io_u(td, v_io_u);
330                         v_io_u = NULL;
331                         break;
332                 }
333
334                 /*
335                  * if we can't submit more io, we need to verify now
336                  */
337                 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
338                         break;
339
340         } while (1);
341
342         do_io_u_verify(td, &v_io_u);
343
344         if (td->cur_depth)
345                 cleanup_pending_aio(td);
346
347         td_set_runstate(td, TD_RUNNING);
348 }
349
350 /*
351  * Not really an io thread, all it does is burn CPU cycles in the specified
352  * manner.
353  */
354 static void do_cpuio(struct thread_data *td)
355 {
356         struct timeval e;
357         int split = 100 / td->cpuload;
358         int i = 0;
359
360         while (!td->terminate) {
361                 fio_gettime(&e, NULL);
362
363                 if (runtime_exceeded(td, &e))
364                         break;
365
366                 if (!(i % split))
367                         __usec_sleep(10000);
368                 else
369                         usec_sleep(td, 10000);
370
371                 i++;
372         }
373 }
374
375 /*
376  * Main IO worker function. It retrieves io_u's to process and queues
377  * and reaps them, checking for rate and errors along the way.
378  */
379 static void do_io(struct thread_data *td)
380 {
381         struct io_completion_data icd;
382         struct timeval s;
383         unsigned long usec;
384         struct fio_file *f;
385         int i, ret = 0;
386
387         td_set_runstate(td, TD_RUNNING);
388
389         while (td->this_io_bytes[td->ddir] < td->io_size) {
390                 struct timespec *timeout;
391                 int min_evts = 0;
392                 struct io_u *io_u;
393
394                 if (td->terminate)
395                         break;
396
397                 f = get_next_file(td);
398                 if (!f)
399                         break;
400
401                 io_u = get_io_u(td, f);
402                 if (!io_u)
403                         break;
404
405                 memcpy(&s, &io_u->start_time, sizeof(s));
406
407                 ret = td_io_queue(td, io_u);
408                 if (ret) {
409                         td_verror(td, io_u->error);
410                         put_io_u(td, io_u);
411                         break;
412                 }
413
414                 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
415
416                 if (td->cur_depth < td->iodepth) {
417                         struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
418
419                         timeout = &ts;
420                         min_evts = 0;
421                 } else {
422                         timeout = NULL;
423                         min_evts = 1;
424                 }
425
426                 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
427                 if (ret < 0) {
428                         td_verror(td, ret);
429                         break;
430                 } else if (!ret)
431                         continue;
432
433                 icd.nr = ret;
434                 ios_completed(td, &icd);
435                 if (icd.error) {
436                         td_verror(td, icd.error);
437                         break;
438                 }
439
440                 /*
441                  * the rate is batched for now, it should work for batches
442                  * of completions except the very first one which may look
443                  * a little bursty
444                  */
445                 usec = utime_since(&s, &icd.time);
446
447                 rate_throttle(td, usec, icd.bytes_done[td->ddir], td->ddir);
448
449                 if (check_min_rate(td, &icd.time)) {
450                         if (exitall_on_terminate)
451                                 terminate_threads(td->groupid, 0);
452                         td_verror(td, ENODATA);
453                         break;
454                 }
455
456                 if (runtime_exceeded(td, &icd.time))
457                         break;
458
459                 if (td->thinktime) {
460                         unsigned long long b;
461
462                         b = td->io_blocks[0] + td->io_blocks[1];
463                         if (!(b % td->thinktime_blocks))
464                                 usec_sleep(td, td->thinktime);
465                 }
466         }
467
468         if (!td->error) {
469                 if (td->cur_depth)
470                         cleanup_pending_aio(td);
471
472                 if (should_fsync(td) && td->end_fsync) {
473                         td_set_runstate(td, TD_FSYNCING);
474                         for_each_file(td, f, i)
475                                 fio_io_sync(td, f);
476                 }
477         }
478 }
479
480 static void cleanup_io_u(struct thread_data *td)
481 {
482         struct list_head *entry, *n;
483         struct io_u *io_u;
484
485         list_for_each_safe(entry, n, &td->io_u_freelist) {
486                 io_u = list_entry(entry, struct io_u, list);
487
488                 list_del(&io_u->list);
489                 free(io_u);
490         }
491
492         free_io_mem(td);
493 }
494
495 /*
496  * "randomly" fill the buffer contents
497  */
498 static void fill_rand_buf(struct io_u *io_u, int max_bs)
499 {
500         int *ptr = io_u->buf;
501
502         while ((void *) ptr - io_u->buf < max_bs) {
503                 *ptr = rand() * 0x9e370001;
504                 ptr++;
505         }
506 }
507
508 static int init_io_u(struct thread_data *td)
509 {
510         struct io_u *io_u;
511         unsigned int max_bs;
512         int i, max_units;
513         char *p;
514
515         if (td->io_ops->flags & FIO_CPUIO)
516                 return 0;
517
518         if (td->io_ops->flags & FIO_SYNCIO)
519                 max_units = 1;
520         else
521                 max_units = td->iodepth;
522
523         max_bs = max(td->max_bs[DDIR_READ], td->max_bs[DDIR_WRITE]);
524         td->orig_buffer_size = max_bs * max_units;
525
526         if (td->mem_type == MEM_SHMHUGE || td->mem_type == MEM_MMAPHUGE)
527                 td->orig_buffer_size = (td->orig_buffer_size + td->hugepage_size - 1) & ~(td->hugepage_size - 1);
528         else
529                 td->orig_buffer_size += MASK;
530
531         if (allocate_io_mem(td))
532                 return 1;
533
534         p = ALIGN(td->orig_buffer);
535         for (i = 0; i < max_units; i++) {
536                 io_u = malloc(sizeof(*io_u));
537                 memset(io_u, 0, sizeof(*io_u));
538                 INIT_LIST_HEAD(&io_u->list);
539
540                 io_u->buf = p + max_bs * i;
541                 if (td_write(td) || td_rw(td))
542                         fill_rand_buf(io_u, max_bs);
543
544                 io_u->index = i;
545                 list_add(&io_u->list, &td->io_u_freelist);
546         }
547
548         return 0;
549 }
550
551 static int switch_ioscheduler(struct thread_data *td)
552 {
553         char tmp[256], tmp2[128];
554         FILE *f;
555         int ret;
556
557         if (td->io_ops->flags & FIO_CPUIO)
558                 return 0;
559
560         sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
561
562         f = fopen(tmp, "r+");
563         if (!f) {
564                 td_verror(td, errno);
565                 return 1;
566         }
567
568         /*
569          * Set io scheduler.
570          */
571         ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
572         if (ferror(f) || ret != 1) {
573                 td_verror(td, errno);
574                 fclose(f);
575                 return 1;
576         }
577
578         rewind(f);
579
580         /*
581          * Read back and check that the selected scheduler is now the default.
582          */
583         ret = fread(tmp, 1, sizeof(tmp), f);
584         if (ferror(f) || ret < 0) {
585                 td_verror(td, errno);
586                 fclose(f);
587                 return 1;
588         }
589
590         sprintf(tmp2, "[%s]", td->ioscheduler);
591         if (!strstr(tmp, tmp2)) {
592                 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
593                 td_verror(td, EINVAL);
594                 fclose(f);
595                 return 1;
596         }
597
598         fclose(f);
599         return 0;
600 }
601
602 static void clear_io_state(struct thread_data *td)
603 {
604         struct fio_file *f;
605         int i;
606
607         td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
608         td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
609         td->zone_bytes = 0;
610
611         for_each_file(td, f, i) {
612                 f->last_pos = 0;
613                 if (td->io_ops->flags & FIO_SYNCIO)
614                         lseek(f->fd, SEEK_SET, 0);
615
616                 if (f->file_map)
617                         memset(f->file_map, 0, f->num_maps * sizeof(long));
618         }
619 }
620
621 /*
622  * Entry point for the thread based jobs. The process based jobs end up
623  * here as well, after a little setup.
624  */
625 static void *thread_main(void *data)
626 {
627         unsigned long long runtime[2];
628         struct thread_data *td = data;
629
630         if (!td->use_thread)
631                 setsid();
632
633         td->pid = getpid();
634
635         INIT_LIST_HEAD(&td->io_u_freelist);
636         INIT_LIST_HEAD(&td->io_u_busylist);
637         INIT_LIST_HEAD(&td->io_hist_list);
638         INIT_LIST_HEAD(&td->io_log_list);
639
640         if (init_io_u(td))
641                 goto err;
642
643         if (fio_setaffinity(td) == -1) {
644                 td_verror(td, errno);
645                 goto err;
646         }
647
648         if (td_io_init(td))
649                 goto err;
650
651         if (init_iolog(td))
652                 goto err;
653
654         if (td->ioprio) {
655                 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
656                         td_verror(td, errno);
657                         goto err;
658                 }
659         }
660
661         if (nice(td->nice) == -1) {
662                 td_verror(td, errno);
663                 goto err;
664         }
665
666         if (init_random_state(td))
667                 goto err;
668
669         if (td->ioscheduler && switch_ioscheduler(td))
670                 goto err;
671
672         td_set_runstate(td, TD_INITIALIZED);
673         fio_sem_up(&startup_sem);
674         fio_sem_down(&td->mutex);
675
676         if (!td->create_serialize && setup_files(td))
677                 goto err;
678         if (open_files(td))
679                 goto err;
680
681         if (td->exec_prerun)
682                 system(td->exec_prerun);
683
684         fio_gettime(&td->epoch, NULL);
685         getrusage(RUSAGE_SELF, &td->ru_start);
686
687         runtime[0] = runtime[1] = 0;
688         while (td->loops--) {
689                 fio_gettime(&td->start, NULL);
690                 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
691
692                 if (td->ratemin)
693                         memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
694
695                 clear_io_state(td);
696                 prune_io_piece_log(td);
697
698                 if (td->io_ops->flags & FIO_CPUIO)
699                         do_cpuio(td);
700                 else
701                         do_io(td);
702
703                 runtime[td->ddir] += utime_since_now(&td->start);
704                 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
705                         runtime[td->ddir ^ 1] = runtime[td->ddir];
706
707                 if (td->error || td->terminate)
708                         break;
709
710                 if (td->verify == VERIFY_NONE)
711                         continue;
712
713                 clear_io_state(td);
714                 fio_gettime(&td->start, NULL);
715
716                 do_verify(td);
717
718                 runtime[DDIR_READ] += utime_since_now(&td->start);
719
720                 if (td->error || td->terminate)
721                         break;
722         }
723
724         update_rusage_stat(td);
725         fio_gettime(&td->end_time, NULL);
726         td->runtime[0] = runtime[0] / 1000;
727         td->runtime[1] = runtime[1] / 1000;
728
729         if (td->bw_log)
730                 finish_log(td, td->bw_log, "bw");
731         if (td->slat_log)
732                 finish_log(td, td->slat_log, "slat");
733         if (td->clat_log)
734                 finish_log(td, td->clat_log, "clat");
735         if (td->write_iolog_file)
736                 write_iolog_close(td);
737         if (td->exec_postrun)
738                 system(td->exec_postrun);
739
740         if (exitall_on_terminate)
741                 terminate_threads(td->groupid, 0);
742
743 err:
744         close_files(td);
745         close_ioengine(td);
746         cleanup_io_u(td);
747         td_set_runstate(td, TD_EXITED);
748         return NULL;
749
750 }
751
752 /*
753  * We cannot pass the td data into a forked process, so attach the td and
754  * pass it to the thread worker.
755  */
756 static void *fork_main(int shmid, int offset)
757 {
758         struct thread_data *td;
759         void *data;
760
761         data = shmat(shmid, NULL, 0);
762         if (data == (void *) -1) {
763                 perror("shmat");
764                 return NULL;
765         }
766
767         td = data + offset * sizeof(struct thread_data);
768         thread_main(td);
769         shmdt(data);
770         return NULL;
771 }
772
773 /*
774  * Run over the job map and reap the threads that have exited, if any.
775  */
776 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
777 {
778         struct thread_data *td;
779         int i, cputhreads, pending;
780
781         /*
782          * reap exited threads (TD_EXITED -> TD_REAPED)
783          */
784         pending = cputhreads = 0;
785         for_each_td(td, i) {
786                 /*
787                  * ->io_ops is NULL for a thread that has closed its
788                  * io engine
789                  */
790                 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
791                         cputhreads++;
792
793                 if (td->runstate != TD_EXITED) {
794                         if (td->runstate < TD_RUNNING)
795                                 pending++;
796
797                         continue;
798                 }
799
800                 td_set_runstate(td, TD_REAPED);
801
802                 if (td->use_thread) {
803                         long ret;
804
805                         if (pthread_join(td->thread, (void *) &ret))
806                                 perror("thread_join");
807                 } else
808                         waitpid(td->pid, NULL, 0);
809
810                 (*nr_running)--;
811                 (*m_rate) -= td->ratemin;
812                 (*t_rate) -= td->rate;
813         }
814
815         if (*nr_running == cputhreads && !pending)
816                 terminate_threads(TERMINATE_ALL, 0);
817 }
818
819 /*
820  * Main function for kicking off and reaping jobs, as needed.
821  */
822 static void run_threads(void)
823 {
824         struct thread_data *td;
825         unsigned long spent;
826         int i, todo, nr_running, m_rate, t_rate, nr_started;
827
828         if (fio_pin_memory())
829                 return;
830
831         if (!terse_output) {
832                 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
833                 fflush(stdout);
834         }
835
836         signal(SIGINT, sig_handler);
837         signal(SIGALRM, sig_handler);
838         signal(SIGSEGV, sig_handler);
839
840         todo = thread_number;
841         nr_running = 0;
842         nr_started = 0;
843         m_rate = t_rate = 0;
844
845         for_each_td(td, i) {
846                 print_status_init(td->thread_number - 1);
847
848                 if (!td->create_serialize) {
849                         init_disk_util(td);
850                         continue;
851                 }
852
853                 /*
854                  * do file setup here so it happens sequentially,
855                  * we don't want X number of threads getting their
856                  * client data interspersed on disk
857                  */
858                 if (setup_files(td)) {
859                         td_set_runstate(td, TD_REAPED);
860                         todo--;
861                 }
862
863                 init_disk_util(td);
864         }
865
866         while (todo) {
867                 struct thread_data *map[MAX_JOBS];
868                 struct timeval this_start;
869                 int this_jobs = 0, left;
870
871                 /*
872                  * create threads (TD_NOT_CREATED -> TD_CREATED)
873                  */
874                 for_each_td(td, i) {
875                         if (td->runstate != TD_NOT_CREATED)
876                                 continue;
877
878                         /*
879                          * never got a chance to start, killed by other
880                          * thread for some reason
881                          */
882                         if (td->terminate) {
883                                 todo--;
884                                 continue;
885                         }
886
887                         if (td->start_delay) {
888                                 spent = mtime_since_genesis();
889
890                                 if (td->start_delay * 1000 > spent)
891                                         continue;
892                         }
893
894                         if (td->stonewall && (nr_started || nr_running))
895                                 break;
896
897                         /*
898                          * Set state to created. Thread will transition
899                          * to TD_INITIALIZED when it's done setting up.
900                          */
901                         td_set_runstate(td, TD_CREATED);
902                         map[this_jobs++] = td;
903                         fio_sem_init(&startup_sem, 1);
904                         nr_started++;
905
906                         if (td->use_thread) {
907                                 if (pthread_create(&td->thread, NULL, thread_main, td)) {
908                                         perror("thread_create");
909                                         nr_started--;
910                                 }
911                         } else {
912                                 if (fork())
913                                         fio_sem_down(&startup_sem);
914                                 else {
915                                         fork_main(shm_id, i);
916                                         exit(0);
917                                 }
918                         }
919                 }
920
921                 /*
922                  * Wait for the started threads to transition to
923                  * TD_INITIALIZED.
924                  */
925                 fio_gettime(&this_start, NULL);
926                 left = this_jobs;
927                 while (left && !fio_abort) {
928                         if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
929                                 break;
930
931                         usleep(100000);
932
933                         for (i = 0; i < this_jobs; i++) {
934                                 td = map[i];
935                                 if (!td)
936                                         continue;
937                                 if (td->runstate == TD_INITIALIZED) {
938                                         map[i] = NULL;
939                                         left--;
940                                 } else if (td->runstate >= TD_EXITED) {
941                                         map[i] = NULL;
942                                         left--;
943                                         todo--;
944                                         nr_running++; /* work-around... */
945                                 }
946                         }
947                 }
948
949                 if (left) {
950                         log_err("fio: %d jobs failed to start\n", left);
951                         for (i = 0; i < this_jobs; i++) {
952                                 td = map[i];
953                                 if (!td)
954                                         continue;
955                                 kill(td->pid, SIGTERM);
956                         }
957                         break;
958                 }
959
960                 /*
961                  * start created threads (TD_INITIALIZED -> TD_RUNNING).
962                  */
963                 for_each_td(td, i) {
964                         if (td->runstate != TD_INITIALIZED)
965                                 continue;
966
967                         td_set_runstate(td, TD_RUNNING);
968                         nr_running++;
969                         nr_started--;
970                         m_rate += td->ratemin;
971                         t_rate += td->rate;
972                         todo--;
973                         fio_sem_up(&td->mutex);
974                 }
975
976                 reap_threads(&nr_running, &t_rate, &m_rate);
977
978                 if (todo)
979                         usleep(100000);
980         }
981
982         while (nr_running) {
983                 reap_threads(&nr_running, &t_rate, &m_rate);
984                 usleep(10000);
985         }
986
987         update_io_ticks();
988         fio_unpin_memory();
989 }
990
991 int main(int argc, char *argv[])
992 {
993         if (parse_options(argc, argv))
994                 return 1;
995
996         if (!thread_number) {
997                 log_err("Nothing to do\n");
998                 return 1;
999         }
1000
1001         disk_util_timer_arm();
1002
1003         run_threads();
1004
1005         if (!fio_abort)
1006                 show_run_stats();
1007
1008         return 0;
1009 }