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