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