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