[PATCH] Move td_io_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
858a3d47
JA
196/*
197 * Helper to handle the final sync of a file. Works just like the normal
198 * io path, just does everything sync.
199 */
200static 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
906c8d75
JA
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 */
a9619d44 244void do_verify(struct thread_data *td)
ebac4655
JA
245{
246 struct timeval t;
247 struct io_u *io_u, *v_io_u = NULL;
248 struct io_completion_data icd;
53cdc686 249 struct fio_file *f;
e5b401d4
JA
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) {
858a3d47 257 fio_io_sync(td, f);
e5b401d4
JA
258 file_invalidate_cache(td, f);
259 }
ebac4655
JA
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
aea47d44 275 if (get_next_verify(td, io_u)) {
ebac4655
JA
276 put_io_u(td, io_u);
277 break;
278 }
279
53cdc686
JA
280 f = get_next_file(td);
281 if (!f)
282 break;
283
284 io_u->file = f;
285
aea47d44 286 if (td_io_prep(td, io_u)) {
ebac4655
JA
287 put_io_u(td, io_u);
288 break;
289 }
290
45bee283 291 ret = td_io_queue(td, io_u);
ebac4655
JA
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
45bee283 305 ret = td_io_getevents(td, 1, 1, NULL);
ebac4655
JA
306 if (ret != 1) {
307 if (ret < 0)
308 td_verror(td, ret);
309 break;
310 }
311
2866c82d 312 v_io_u = td->io_ops->event(td, 0);
ebac4655
JA
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
ebac4655
JA
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
b990b5c0
JA
340/*
341 * Not really an io thread, all it does is burn CPU cycles in the specified
342 * manner.
343 */
344static 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
32cd46a0 365/*
906c8d75 366 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
367 * and reaps them, checking for rate and errors along the way.
368 */
ebac4655
JA
369static void do_io(struct thread_data *td)
370{
371 struct io_completion_data icd;
372 struct timeval s, e;
373 unsigned long usec;
53cdc686 374 struct fio_file *f;
84585003 375 int i, ret = 0;
ebac4655 376
5853e5a8
JA
377 td_set_runstate(td, TD_RUNNING);
378
ebac4655
JA
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;
84585003 382 int min_evts = 0;
ebac4655
JA
383 struct io_u *io_u;
384
385 if (td->terminate)
386 break;
387
53cdc686
JA
388 f = get_next_file(td);
389 if (!f)
390 break;
391
392 io_u = get_io_u(td, f);
ebac4655
JA
393 if (!io_u)
394 break;
395
396 memcpy(&s, &io_u->start_time, sizeof(s));
397
45bee283 398 ret = td_io_queue(td, io_u);
ebac4655
JA
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
45bee283 415 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
ebac4655 416 if (ret < 0) {
87dc1ab1 417 td_verror(td, ret);
ebac4655
JA
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)) {
2f9ade3c
JA
440 if (rate_quit)
441 terminate_threads(td->groupid);
ebac4655
JA
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);
ebac4655
JA
451 }
452
84585003
JA
453 if (!ret) {
454 if (td->cur_depth)
455 cleanup_pending_aio(td);
ebac4655 456
84585003
JA
457 if (should_fsync(td) && td->end_fsync) {
458 td_set_runstate(td, TD_FSYNCING);
459 for_each_file(td, f, i)
858a3d47 460 fio_io_sync(td, f);
84585003 461 }
5853e5a8 462 }
ebac4655
JA
463}
464
ebac4655
JA
465static 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
2f9ade3c 477 free_io_mem(td);
ebac4655
JA
478}
479
480static int init_io_u(struct thread_data *td)
481{
482 struct io_u *io_u;
483 int i, max_units;
484 char *p;
485
2866c82d 486 if (td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
487 return 0;
488
2866c82d 489 if (td->io_ops->flags & FIO_SYNCIO)
ebac4655
JA
490 max_units = 1;
491 else
492 max_units = td->iodepth;
493
494 td->orig_buffer_size = td->max_bs * max_units + MASK;
495
2f9ade3c
JA
496 if (allocate_io_mem(td))
497 return 1;
ebac4655 498
ebac4655
JA
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;
b1ff3403 506 io_u->index = i;
ebac4655
JA
507 list_add(&io_u->list, &td->io_u_freelist);
508 }
509
510 return 0;
511}
512
da86774e
JA
513static 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)) {
3b70d7e5 551 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
da86774e
JA
552 td_verror(td, EINVAL);
553 fclose(f);
554 return 1;
555 }
556
557 fclose(f);
558 return 0;
559}
560
ebac4655
JA
561static void clear_io_state(struct thread_data *td)
562{
53cdc686
JA
563 struct fio_file *f;
564 int i;
ebac4655 565
ebac4655
JA
566 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
567 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 568 td->zone_bytes = 0;
ebac4655 569
53cdc686
JA
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 }
ebac4655
JA
578}
579
906c8d75
JA
580/*
581 * Entry point for the thread based jobs. The process based jobs end up
582 * here as well, after a little setup.
583 */
ebac4655
JA
584static void *thread_main(void *data)
585{
586 struct thread_data *td = data;
ebac4655
JA
587
588 if (!td->use_thread)
589 setsid();
590
591 td->pid = getpid();
592
aea47d44
JA
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
ebac4655
JA
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
45bee283 606 if (td_io_init(td))
ebac4655
JA
607 goto err;
608
aea47d44
JA
609 if (init_iolog(td))
610 goto err;
611
ebac4655
JA
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
1056eaad 619 if (nice(td->nice) == -1) {
b6f4d880
JA
620 td_verror(td, errno);
621 goto err;
622 }
623
75154845
JA
624 if (init_random_state(td))
625 goto err;
626
56f9498d
JA
627 if (td->ioscheduler && switch_ioscheduler(td))
628 goto err;
da86774e 629
75154845 630 td_set_runstate(td, TD_INITIALIZED);
bbfd6b00
JA
631 fio_sem_up(&startup_sem);
632 fio_sem_down(&td->mutex);
ebac4655 633
53cdc686 634 if (!td->create_serialize && setup_files(td))
ebac4655
JA
635 goto err;
636
ebac4655
JA
637 gettimeofday(&td->epoch, NULL);
638
56f9498d 639 if (td->exec_prerun)
4e0ba8af
JA
640 system(td->exec_prerun);
641
ebac4655
JA
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
2866c82d 653 if (td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
654 do_cpuio(td);
655 else
656 do_io(td);
ebac4655
JA
657
658 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 659 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
660 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
661
ebac4655
JA
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
ebac4655
JA
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");
843a7413
JA
687 if (td->write_iolog)
688 write_iolog_close(td);
56f9498d 689 if (td->exec_postrun)
4e0ba8af 690 system(td->exec_postrun);
ebac4655
JA
691
692 if (exitall_on_terminate)
693 terminate_threads(td->groupid);
694
695err:
53cdc686 696 close_files(td);
2866c82d 697 close_ioengine(td);
ebac4655 698 cleanup_io_u(td);
ebac4655
JA
699 td_set_runstate(td, TD_EXITED);
700 return NULL;
701
702}
703
906c8d75
JA
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 */
ebac4655
JA
708static 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
906c8d75
JA
725/*
726 * Run over the job map and reap the threads that have exited, if any.
727 */
ebac4655
JA
728static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
729{
34572e28 730 struct thread_data *td;
b990b5c0 731 int i, cputhreads;
ebac4655
JA
732
733 /*
734 * reap exited threads (TD_EXITED -> TD_REAPED)
735 */
34572e28
JA
736 cputhreads = 0;
737 for_each_td(td, i) {
84585003
JA
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)
b990b5c0
JA
743 cputhreads++;
744
ebac4655
JA
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 }
b990b5c0
JA
762
763 if (*nr_running == cputhreads)
764 terminate_threads(TERMINATE_ALL);
ebac4655
JA
765}
766
906c8d75
JA
767/*
768 * Main function for kicking off and reaping jobs, as needed.
769 */
ebac4655
JA
770static void run_threads(void)
771{
ebac4655
JA
772 struct thread_data *td;
773 unsigned long spent;
774 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 775
2f9ade3c
JA
776 if (fio_pin_memory())
777 return;
ebac4655 778
c6ae0a5b
JA
779 if (!terse_output) {
780 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
781 fflush(stdout);
782 }
c04f7ec3 783
4efa970e
JA
784 signal(SIGINT, sig_handler);
785 signal(SIGALRM, sig_handler);
786
ebac4655
JA
787 todo = thread_number;
788 nr_running = 0;
789 nr_started = 0;
790 m_rate = t_rate = 0;
791
34572e28 792 for_each_td(td, i) {
263e529f 793 print_status_init(td->thread_number - 1);
ebac4655
JA
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 */
53cdc686 805 if (setup_files(td)) {
ebac4655
JA
806 td_set_runstate(td, TD_REAPED);
807 todo--;
808 }
809 }
810
263e529f 811 time_init();
ebac4655
JA
812
813 while (todo) {
75154845
JA
814 struct thread_data *map[MAX_JOBS];
815 struct timeval this_start;
816 int this_jobs = 0, left;
817
ebac4655
JA
818 /*
819 * create threads (TD_NOT_CREATED -> TD_CREATED)
820 */
34572e28 821 for_each_td(td, i) {
ebac4655
JA
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) {
263e529f 835 spent = mtime_since_genesis();
ebac4655
JA
836
837 if (td->start_delay * 1000 > spent)
838 continue;
839 }
840
841 if (td->stonewall && (nr_started || nr_running))
842 break;
843
75154845
JA
844 /*
845 * Set state to created. Thread will transition
846 * to TD_INITIALIZED when it's done setting up.
847 */
ebac4655 848 td_set_runstate(td, TD_CREATED);
75154845 849 map[this_jobs++] = td;
bbfd6b00 850 fio_sem_init(&startup_sem, 1);
ebac4655
JA
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())
bbfd6b00 860 fio_sem_down(&startup_sem);
ebac4655
JA
861 else {
862 fork_main(shm_id, i);
863 exit(0);
864 }
865 }
866 }
867
868 /*
75154845
JA
869 * Wait for the started threads to transition to
870 * TD_INITIALIZED.
ebac4655 871 */
75154845
JA
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;
b6f4d880 884 if (td->runstate == TD_INITIALIZED) {
75154845
JA
885 map[i] = NULL;
886 left--;
b6f4d880
JA
887 } else if (td->runstate >= TD_EXITED) {
888 map[i] = NULL;
889 left--;
890 todo--;
891 nr_running++; /* work-around... */
75154845
JA
892 }
893 }
894 }
895
896 if (left) {
3b70d7e5 897 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
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 /*
b6f4d880 908 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 909 */
34572e28 910 for_each_td(td, i) {
75154845 911 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
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;
75154845 919 todo--;
bbfd6b00 920 fio_sem_up(&td->mutex);
ebac4655
JA
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();
2f9ade3c 935 fio_unpin_memory();
ebac4655
JA
936}
937
ebac4655
JA
938int main(int argc, char *argv[])
939{
940 if (parse_options(argc, argv))
941 return 1;
942
943 if (!thread_number) {
3b70d7e5 944 log_err("Nothing to do\n");
ebac4655
JA
945 return 1;
946 }
947
948 disk_util_timer_arm();
949
950 run_threads();
951 show_run_stats();
952
953 return 0;
954}