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