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