client: don't clear client->addr after it's been set
[fio.git] / fio.c
... / ...
CommitLineData
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 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
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 */
24#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
27#include <limits.h>
28#include <signal.h>
29#include <time.h>
30#include <locale.h>
31#include <assert.h>
32#include <time.h>
33#include <sys/stat.h>
34#include <sys/wait.h>
35#include <sys/ipc.h>
36#include <sys/shm.h>
37#include <sys/mman.h>
38
39#include "fio.h"
40#include "hash.h"
41#include "smalloc.h"
42#include "verify.h"
43#include "trim.h"
44#include "diskutil.h"
45#include "cgroup.h"
46#include "profile.h"
47#include "lib/rand.h"
48#include "memalign.h"
49#include "server.h"
50
51unsigned long page_mask;
52unsigned long page_size;
53
54#define PAGE_ALIGN(buf) \
55 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
56
57int groupid = 0;
58int thread_number = 0;
59int nr_process = 0;
60int nr_thread = 0;
61int shm_id = 0;
62int temp_stall_ts;
63unsigned long done_secs = 0;
64
65static struct fio_mutex *startup_mutex;
66static struct fio_mutex *writeout_mutex;
67static volatile int fio_abort;
68static int exit_value;
69static pthread_t gtod_thread;
70static pthread_t disk_util_thread;
71static struct flist_head *cgroup_list;
72static char *cgroup_mnt;
73
74unsigned long arch_flags = 0;
75
76struct io_log *agg_io_log[2];
77
78#define JOB_START_TIMEOUT (5 * 1000)
79
80static const char *fio_os_strings[os_nr] = {
81 "Invalid",
82 "Linux",
83 "AIX",
84 "FreeBSD",
85 "HP-UX",
86 "OSX",
87 "NetBSD",
88 "Solaris",
89 "Windows"
90};
91
92static const char *fio_arch_strings[arch_nr] = {
93 "Invalid",
94 "x86-64",
95 "x86",
96 "ppc",
97 "ia64",
98 "s390",
99 "alpha",
100 "sparc",
101 "sparc64",
102 "arm",
103 "sh",
104 "hppa",
105 "generic"
106};
107
108const char *fio_get_os_string(int nr)
109{
110 if (nr < os_nr)
111 return fio_os_strings[nr];
112
113 return NULL;
114}
115
116const char *fio_get_arch_string(int nr)
117{
118 if (nr < arch_nr)
119 return fio_arch_strings[nr];
120
121 return NULL;
122}
123
124void td_set_runstate(struct thread_data *td, int runstate)
125{
126 if (td->runstate == runstate)
127 return;
128
129 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
130 td->runstate, runstate);
131 td->runstate = runstate;
132}
133
134void fio_terminate_threads(int group_id)
135{
136 struct thread_data *td;
137 int i;
138
139 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
140
141 for_each_td(td, i) {
142 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
143 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
144 td->o.name, (int) td->pid);
145 td->terminate = 1;
146 td->o.start_delay = 0;
147
148 /*
149 * if the thread is running, just let it exit
150 */
151 if (!td->pid)
152 continue;
153 else if (td->runstate < TD_RAMP)
154 kill(td->pid, SIGTERM);
155 else {
156 struct ioengine_ops *ops = td->io_ops;
157
158 if (ops && (ops->flags & FIO_SIGTERM))
159 kill(td->pid, SIGTERM);
160 }
161 }
162 }
163}
164
165static void sig_int(int sig)
166{
167 if (threads) {
168 log_info("\nfio: terminating on signal %d\n", sig);
169 exit_backend = 1;
170 fflush(stdout);
171 exit_value = 128;
172 fio_terminate_threads(TERMINATE_ALL);
173 }
174}
175
176static void *disk_thread_main(void *data)
177{
178 fio_mutex_up(startup_mutex);
179
180 while (threads) {
181 usleep(DISK_UTIL_MSEC * 1000);
182 if (!threads)
183 break;
184 update_io_ticks();
185
186 if (is_backend)
187 fio_server_send_status();
188 else
189 print_thread_status();
190 }
191
192 return NULL;
193}
194
195static int create_disk_util_thread(void)
196{
197 int ret;
198
199 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
200 if (ret) {
201 log_err("Can't create disk util thread: %s\n", strerror(ret));
202 return 1;
203 }
204
205 ret = pthread_detach(disk_util_thread);
206 if (ret) {
207 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
208 return 1;
209 }
210
211 dprint(FD_MUTEX, "wait on startup_mutex\n");
212 fio_mutex_down(startup_mutex);
213 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
214 return 0;
215}
216
217static void set_sig_handlers(void)
218{
219 struct sigaction act;
220
221 memset(&act, 0, sizeof(act));
222 act.sa_handler = sig_int;
223 act.sa_flags = SA_RESTART;
224 sigaction(SIGINT, &act, NULL);
225
226 memset(&act, 0, sizeof(act));
227 act.sa_handler = sig_int;
228 act.sa_flags = SA_RESTART;
229 sigaction(SIGTERM, &act, NULL);
230
231 if (is_backend) {
232 memset(&act, 0, sizeof(act));
233 act.sa_handler = sig_int;
234 act.sa_flags = SA_RESTART;
235 sigaction(SIGPIPE, &act, NULL);
236 }
237}
238
239/*
240 * Check if we are above the minimum rate given.
241 */
242static int __check_min_rate(struct thread_data *td, struct timeval *now,
243 enum fio_ddir ddir)
244{
245 unsigned long long bytes = 0;
246 unsigned long iops = 0;
247 unsigned long spent;
248 unsigned long rate;
249 unsigned int ratemin = 0;
250 unsigned int rate_iops = 0;
251 unsigned int rate_iops_min = 0;
252
253 assert(ddir_rw(ddir));
254
255 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
256 return 0;
257
258 /*
259 * allow a 2 second settle period in the beginning
260 */
261 if (mtime_since(&td->start, now) < 2000)
262 return 0;
263
264 iops += td->this_io_blocks[ddir];
265 bytes += td->this_io_bytes[ddir];
266 ratemin += td->o.ratemin[ddir];
267 rate_iops += td->o.rate_iops[ddir];
268 rate_iops_min += td->o.rate_iops_min[ddir];
269
270 /*
271 * if rate blocks is set, sample is running
272 */
273 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
274 spent = mtime_since(&td->lastrate[ddir], now);
275 if (spent < td->o.ratecycle)
276 return 0;
277
278 if (td->o.rate[ddir]) {
279 /*
280 * check bandwidth specified rate
281 */
282 if (bytes < td->rate_bytes[ddir]) {
283 log_err("%s: min rate %u not met\n", td->o.name,
284 ratemin);
285 return 1;
286 } else {
287 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
288 if (rate < ratemin ||
289 bytes < td->rate_bytes[ddir]) {
290 log_err("%s: min rate %u not met, got"
291 " %luKB/sec\n", td->o.name,
292 ratemin, rate);
293 return 1;
294 }
295 }
296 } else {
297 /*
298 * checks iops specified rate
299 */
300 if (iops < rate_iops) {
301 log_err("%s: min iops rate %u not met\n",
302 td->o.name, rate_iops);
303 return 1;
304 } else {
305 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
306 if (rate < rate_iops_min ||
307 iops < td->rate_blocks[ddir]) {
308 log_err("%s: min iops rate %u not met,"
309 " got %lu\n", td->o.name,
310 rate_iops_min, rate);
311 }
312 }
313 }
314 }
315
316 td->rate_bytes[ddir] = bytes;
317 td->rate_blocks[ddir] = iops;
318 memcpy(&td->lastrate[ddir], now, sizeof(*now));
319 return 0;
320}
321
322static int check_min_rate(struct thread_data *td, struct timeval *now,
323 unsigned long *bytes_done)
324{
325 int ret = 0;
326
327 if (bytes_done[0])
328 ret |= __check_min_rate(td, now, 0);
329 if (bytes_done[1])
330 ret |= __check_min_rate(td, now, 1);
331
332 return ret;
333}
334
335static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
336{
337 if (!td->o.timeout)
338 return 0;
339 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
340 return 1;
341
342 return 0;
343}
344
345/*
346 * When job exits, we can cancel the in-flight IO if we are using async
347 * io. Attempt to do so.
348 */
349static void cleanup_pending_aio(struct thread_data *td)
350{
351 struct flist_head *entry, *n;
352 struct io_u *io_u;
353 int r;
354
355 /*
356 * get immediately available events, if any
357 */
358 r = io_u_queued_complete(td, 0, NULL);
359 if (r < 0)
360 return;
361
362 /*
363 * now cancel remaining active events
364 */
365 if (td->io_ops->cancel) {
366 flist_for_each_safe(entry, n, &td->io_u_busylist) {
367 io_u = flist_entry(entry, struct io_u, list);
368
369 /*
370 * if the io_u isn't in flight, then that generally
371 * means someone leaked an io_u. complain but fix
372 * it up, so we don't stall here.
373 */
374 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
375 log_err("fio: non-busy IO on busy list\n");
376 put_io_u(td, io_u);
377 } else {
378 r = td->io_ops->cancel(td, io_u);
379 if (!r)
380 put_io_u(td, io_u);
381 }
382 }
383 }
384
385 if (td->cur_depth)
386 r = io_u_queued_complete(td, td->cur_depth, NULL);
387}
388
389/*
390 * Helper to handle the final sync of a file. Works just like the normal
391 * io path, just does everything sync.
392 */
393static int fio_io_sync(struct thread_data *td, struct fio_file *f)
394{
395 struct io_u *io_u = __get_io_u(td);
396 int ret;
397
398 if (!io_u)
399 return 1;
400
401 io_u->ddir = DDIR_SYNC;
402 io_u->file = f;
403
404 if (td_io_prep(td, io_u)) {
405 put_io_u(td, io_u);
406 return 1;
407 }
408
409requeue:
410 ret = td_io_queue(td, io_u);
411 if (ret < 0) {
412 td_verror(td, io_u->error, "td_io_queue");
413 put_io_u(td, io_u);
414 return 1;
415 } else if (ret == FIO_Q_QUEUED) {
416 if (io_u_queued_complete(td, 1, NULL) < 0)
417 return 1;
418 } else if (ret == FIO_Q_COMPLETED) {
419 if (io_u->error) {
420 td_verror(td, io_u->error, "td_io_queue");
421 return 1;
422 }
423
424 if (io_u_sync_complete(td, io_u, NULL) < 0)
425 return 1;
426 } else if (ret == FIO_Q_BUSY) {
427 if (td_io_commit(td))
428 return 1;
429 goto requeue;
430 }
431
432 return 0;
433}
434
435static inline void __update_tv_cache(struct thread_data *td)
436{
437 fio_gettime(&td->tv_cache, NULL);
438}
439
440static inline void update_tv_cache(struct thread_data *td)
441{
442 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
443 __update_tv_cache(td);
444}
445
446static int break_on_this_error(struct thread_data *td, int *retptr)
447{
448 int ret = *retptr;
449
450 if (ret < 0 || td->error) {
451 int err;
452
453 if (!td->o.continue_on_error)
454 return 1;
455
456 if (ret < 0)
457 err = -ret;
458 else
459 err = td->error;
460
461 if (td_non_fatal_error(err)) {
462 /*
463 * Continue with the I/Os in case of
464 * a non fatal error.
465 */
466 update_error_count(td, err);
467 td_clear_error(td);
468 *retptr = 0;
469 return 0;
470 } else if (td->o.fill_device && err == ENOSPC) {
471 /*
472 * We expect to hit this error if
473 * fill_device option is set.
474 */
475 td_clear_error(td);
476 td->terminate = 1;
477 return 1;
478 } else {
479 /*
480 * Stop the I/O in case of a fatal
481 * error.
482 */
483 update_error_count(td, err);
484 return 1;
485 }
486 }
487
488 return 0;
489}
490
491/*
492 * The main verify engine. Runs over the writes we previously submitted,
493 * reads the blocks back in, and checks the crc/md5 of the data.
494 */
495static void do_verify(struct thread_data *td)
496{
497 struct fio_file *f;
498 struct io_u *io_u;
499 int ret, min_events;
500 unsigned int i;
501
502 dprint(FD_VERIFY, "starting loop\n");
503
504 /*
505 * sync io first and invalidate cache, to make sure we really
506 * read from disk.
507 */
508 for_each_file(td, f, i) {
509 if (!fio_file_open(f))
510 continue;
511 if (fio_io_sync(td, f))
512 break;
513 if (file_invalidate_cache(td, f))
514 break;
515 }
516
517 if (td->error)
518 return;
519
520 td_set_runstate(td, TD_VERIFYING);
521
522 io_u = NULL;
523 while (!td->terminate) {
524 int ret2, full;
525
526 update_tv_cache(td);
527
528 if (runtime_exceeded(td, &td->tv_cache)) {
529 __update_tv_cache(td);
530 if (runtime_exceeded(td, &td->tv_cache)) {
531 td->terminate = 1;
532 break;
533 }
534 }
535
536 io_u = __get_io_u(td);
537 if (!io_u)
538 break;
539
540 if (get_next_verify(td, io_u)) {
541 put_io_u(td, io_u);
542 break;
543 }
544
545 if (td_io_prep(td, io_u)) {
546 put_io_u(td, io_u);
547 break;
548 }
549
550 if (td->o.verify_async)
551 io_u->end_io = verify_io_u_async;
552 else
553 io_u->end_io = verify_io_u;
554
555 ret = td_io_queue(td, io_u);
556 switch (ret) {
557 case FIO_Q_COMPLETED:
558 if (io_u->error) {
559 ret = -io_u->error;
560 clear_io_u(td, io_u);
561 } else if (io_u->resid) {
562 int bytes = io_u->xfer_buflen - io_u->resid;
563
564 /*
565 * zero read, fail
566 */
567 if (!bytes) {
568 td_verror(td, EIO, "full resid");
569 put_io_u(td, io_u);
570 break;
571 }
572
573 io_u->xfer_buflen = io_u->resid;
574 io_u->xfer_buf += bytes;
575 io_u->offset += bytes;
576
577 if (ddir_rw(io_u->ddir))
578 td->ts.short_io_u[io_u->ddir]++;
579
580 f = io_u->file;
581 if (io_u->offset == f->real_file_size)
582 goto sync_done;
583
584 requeue_io_u(td, &io_u);
585 } else {
586sync_done:
587 ret = io_u_sync_complete(td, io_u, NULL);
588 if (ret < 0)
589 break;
590 }
591 continue;
592 case FIO_Q_QUEUED:
593 break;
594 case FIO_Q_BUSY:
595 requeue_io_u(td, &io_u);
596 ret2 = td_io_commit(td);
597 if (ret2 < 0)
598 ret = ret2;
599 break;
600 default:
601 assert(ret < 0);
602 td_verror(td, -ret, "td_io_queue");
603 break;
604 }
605
606 if (break_on_this_error(td, &ret))
607 break;
608
609 /*
610 * if we can queue more, do so. but check if there are
611 * completed io_u's first. Note that we can get BUSY even
612 * without IO queued, if the system is resource starved.
613 */
614 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
615 if (full || !td->o.iodepth_batch_complete) {
616 min_events = min(td->o.iodepth_batch_complete,
617 td->cur_depth);
618 if (full && !min_events && td->o.iodepth_batch_complete != 0)
619 min_events = 1;
620
621 do {
622 /*
623 * Reap required number of io units, if any,
624 * and do the verification on them through
625 * the callback handler
626 */
627 if (io_u_queued_complete(td, min_events, NULL) < 0) {
628 ret = -1;
629 break;
630 }
631 } while (full && (td->cur_depth > td->o.iodepth_low));
632 }
633 if (ret < 0)
634 break;
635 }
636
637 if (!td->error) {
638 min_events = td->cur_depth;
639
640 if (min_events)
641 ret = io_u_queued_complete(td, min_events, NULL);
642 } else
643 cleanup_pending_aio(td);
644
645 td_set_runstate(td, TD_RUNNING);
646
647 dprint(FD_VERIFY, "exiting loop\n");
648}
649
650/*
651 * Main IO worker function. It retrieves io_u's to process and queues
652 * and reaps them, checking for rate and errors along the way.
653 */
654static void do_io(struct thread_data *td)
655{
656 unsigned int i;
657 int ret = 0;
658
659 if (in_ramp_time(td))
660 td_set_runstate(td, TD_RAMP);
661 else
662 td_set_runstate(td, TD_RUNNING);
663
664 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
665 (!flist_empty(&td->trim_list)) ||
666 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
667 struct timeval comp_time;
668 unsigned long bytes_done[2] = { 0, 0 };
669 int min_evts = 0;
670 struct io_u *io_u;
671 int ret2, full;
672
673 if (td->terminate)
674 break;
675
676 update_tv_cache(td);
677
678 if (runtime_exceeded(td, &td->tv_cache)) {
679 __update_tv_cache(td);
680 if (runtime_exceeded(td, &td->tv_cache)) {
681 td->terminate = 1;
682 break;
683 }
684 }
685
686 io_u = get_io_u(td);
687 if (!io_u)
688 break;
689
690 /*
691 * Add verification end_io handler, if asked to verify
692 * a previously written file.
693 */
694 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
695 !td_rw(td)) {
696 if (td->o.verify_async)
697 io_u->end_io = verify_io_u_async;
698 else
699 io_u->end_io = verify_io_u;
700 td_set_runstate(td, TD_VERIFYING);
701 } else if (in_ramp_time(td))
702 td_set_runstate(td, TD_RAMP);
703 else
704 td_set_runstate(td, TD_RUNNING);
705
706 ret = td_io_queue(td, io_u);
707 switch (ret) {
708 case FIO_Q_COMPLETED:
709 if (io_u->error) {
710 ret = -io_u->error;
711 clear_io_u(td, io_u);
712 } else if (io_u->resid) {
713 int bytes = io_u->xfer_buflen - io_u->resid;
714 struct fio_file *f = io_u->file;
715
716 /*
717 * zero read, fail
718 */
719 if (!bytes) {
720 td_verror(td, EIO, "full resid");
721 put_io_u(td, io_u);
722 break;
723 }
724
725 io_u->xfer_buflen = io_u->resid;
726 io_u->xfer_buf += bytes;
727 io_u->offset += bytes;
728
729 if (ddir_rw(io_u->ddir))
730 td->ts.short_io_u[io_u->ddir]++;
731
732 if (io_u->offset == f->real_file_size)
733 goto sync_done;
734
735 requeue_io_u(td, &io_u);
736 } else {
737sync_done:
738 if (__should_check_rate(td, 0) ||
739 __should_check_rate(td, 1))
740 fio_gettime(&comp_time, NULL);
741
742 ret = io_u_sync_complete(td, io_u, bytes_done);
743 if (ret < 0)
744 break;
745 }
746 break;
747 case FIO_Q_QUEUED:
748 /*
749 * if the engine doesn't have a commit hook,
750 * the io_u is really queued. if it does have such
751 * a hook, it has to call io_u_queued() itself.
752 */
753 if (td->io_ops->commit == NULL)
754 io_u_queued(td, io_u);
755 break;
756 case FIO_Q_BUSY:
757 requeue_io_u(td, &io_u);
758 ret2 = td_io_commit(td);
759 if (ret2 < 0)
760 ret = ret2;
761 break;
762 default:
763 assert(ret < 0);
764 put_io_u(td, io_u);
765 break;
766 }
767
768 if (break_on_this_error(td, &ret))
769 break;
770
771 /*
772 * See if we need to complete some commands. Note that we
773 * can get BUSY even without IO queued, if the system is
774 * resource starved.
775 */
776 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
777 if (full || !td->o.iodepth_batch_complete) {
778 min_evts = min(td->o.iodepth_batch_complete,
779 td->cur_depth);
780 if (full && !min_evts && td->o.iodepth_batch_complete != 0)
781 min_evts = 1;
782
783 if (__should_check_rate(td, 0) ||
784 __should_check_rate(td, 1))
785 fio_gettime(&comp_time, NULL);
786
787 do {
788 ret = io_u_queued_complete(td, min_evts, bytes_done);
789 if (ret < 0)
790 break;
791
792 } while (full && (td->cur_depth > td->o.iodepth_low));
793 }
794
795 if (ret < 0)
796 break;
797 if (!(bytes_done[0] + bytes_done[1]))
798 continue;
799
800 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
801 if (check_min_rate(td, &comp_time, bytes_done)) {
802 if (exitall_on_terminate)
803 fio_terminate_threads(td->groupid);
804 td_verror(td, EIO, "check_min_rate");
805 break;
806 }
807 }
808
809 if (td->o.thinktime) {
810 unsigned long long b;
811
812 b = td->io_blocks[0] + td->io_blocks[1];
813 if (!(b % td->o.thinktime_blocks)) {
814 int left;
815
816 if (td->o.thinktime_spin)
817 usec_spin(td->o.thinktime_spin);
818
819 left = td->o.thinktime - td->o.thinktime_spin;
820 if (left)
821 usec_sleep(td, left);
822 }
823 }
824 }
825
826 if (td->trim_entries)
827 printf("trim entries %ld\n", td->trim_entries);
828
829 if (td->o.fill_device && td->error == ENOSPC) {
830 td->error = 0;
831 td->terminate = 1;
832 }
833 if (!td->error) {
834 struct fio_file *f;
835
836 i = td->cur_depth;
837 if (i) {
838 ret = io_u_queued_complete(td, i, NULL);
839 if (td->o.fill_device && td->error == ENOSPC)
840 td->error = 0;
841 }
842
843 if (should_fsync(td) && td->o.end_fsync) {
844 td_set_runstate(td, TD_FSYNCING);
845
846 for_each_file(td, f, i) {
847 if (!fio_file_open(f))
848 continue;
849 fio_io_sync(td, f);
850 }
851 }
852 } else
853 cleanup_pending_aio(td);
854
855 /*
856 * stop job if we failed doing any IO
857 */
858 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
859 td->done = 1;
860}
861
862static void cleanup_io_u(struct thread_data *td)
863{
864 struct flist_head *entry, *n;
865 struct io_u *io_u;
866
867 flist_for_each_safe(entry, n, &td->io_u_freelist) {
868 io_u = flist_entry(entry, struct io_u, list);
869
870 flist_del(&io_u->list);
871 fio_memfree(io_u, sizeof(*io_u));
872 }
873
874 free_io_mem(td);
875}
876
877static int init_io_u(struct thread_data *td)
878{
879 struct io_u *io_u;
880 unsigned int max_bs;
881 int cl_align, i, max_units;
882 char *p;
883
884 max_units = td->o.iodepth;
885 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
886 td->orig_buffer_size = (unsigned long long) max_bs
887 * (unsigned long long) max_units;
888
889 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
890 unsigned long bs;
891
892 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
893 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
894 }
895
896 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
897 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
898 return 1;
899 }
900
901 if (allocate_io_mem(td))
902 return 1;
903
904 if (td->o.odirect || td->o.mem_align ||
905 (td->io_ops->flags & FIO_RAWIO))
906 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
907 else
908 p = td->orig_buffer;
909
910 cl_align = os_cache_line_size();
911
912 for (i = 0; i < max_units; i++) {
913 void *ptr;
914
915 if (td->terminate)
916 return 1;
917
918 ptr = fio_memalign(cl_align, sizeof(*io_u));
919 if (!ptr) {
920 log_err("fio: unable to allocate aligned memory\n");
921 break;
922 }
923
924 io_u = ptr;
925 memset(io_u, 0, sizeof(*io_u));
926 INIT_FLIST_HEAD(&io_u->list);
927 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
928
929 if (!(td->io_ops->flags & FIO_NOIO)) {
930 io_u->buf = p + max_bs * i;
931 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
932
933 if (td_write(td))
934 io_u_fill_buffer(td, io_u, max_bs);
935 if (td_write(td) && td->o.verify_pattern_bytes) {
936 /*
937 * Fill the buffer with the pattern if we are
938 * going to be doing writes.
939 */
940 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
941 }
942 }
943
944 io_u->index = i;
945 io_u->flags = IO_U_F_FREE;
946 flist_add(&io_u->list, &td->io_u_freelist);
947 }
948
949 return 0;
950}
951
952static int switch_ioscheduler(struct thread_data *td)
953{
954 char tmp[256], tmp2[128];
955 FILE *f;
956 int ret;
957
958 if (td->io_ops->flags & FIO_DISKLESSIO)
959 return 0;
960
961 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
962
963 f = fopen(tmp, "r+");
964 if (!f) {
965 if (errno == ENOENT) {
966 log_err("fio: os or kernel doesn't support IO scheduler"
967 " switching\n");
968 return 0;
969 }
970 td_verror(td, errno, "fopen iosched");
971 return 1;
972 }
973
974 /*
975 * Set io scheduler.
976 */
977 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
978 if (ferror(f) || ret != 1) {
979 td_verror(td, errno, "fwrite");
980 fclose(f);
981 return 1;
982 }
983
984 rewind(f);
985
986 /*
987 * Read back and check that the selected scheduler is now the default.
988 */
989 ret = fread(tmp, 1, sizeof(tmp), f);
990 if (ferror(f) || ret < 0) {
991 td_verror(td, errno, "fread");
992 fclose(f);
993 return 1;
994 }
995
996 sprintf(tmp2, "[%s]", td->o.ioscheduler);
997 if (!strstr(tmp, tmp2)) {
998 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
999 td_verror(td, EINVAL, "iosched_switch");
1000 fclose(f);
1001 return 1;
1002 }
1003
1004 fclose(f);
1005 return 0;
1006}
1007
1008static int keep_running(struct thread_data *td)
1009{
1010 unsigned long long io_done;
1011
1012 if (td->done)
1013 return 0;
1014 if (td->o.time_based)
1015 return 1;
1016 if (td->o.loops) {
1017 td->o.loops--;
1018 return 1;
1019 }
1020
1021 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
1022 + td->io_skip_bytes;
1023 if (io_done < td->o.size)
1024 return 1;
1025
1026 return 0;
1027}
1028
1029static void reset_io_counters(struct thread_data *td)
1030{
1031 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1032 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1033 td->stat_io_blocks[0] = td->stat_io_blocks[1] = 0;
1034 td->this_io_blocks[0] = td->this_io_blocks[1] = 0;
1035 td->zone_bytes = 0;
1036 td->rate_bytes[0] = td->rate_bytes[1] = 0;
1037 td->rate_blocks[0] = td->rate_blocks[1] = 0;
1038
1039 td->last_was_sync = 0;
1040
1041 /*
1042 * reset file done count if we are to start over
1043 */
1044 if (td->o.time_based || td->o.loops)
1045 td->nr_done_files = 0;
1046}
1047
1048void reset_all_stats(struct thread_data *td)
1049{
1050 struct timeval tv;
1051 int i;
1052
1053 reset_io_counters(td);
1054
1055 for (i = 0; i < 2; i++) {
1056 td->io_bytes[i] = 0;
1057 td->io_blocks[i] = 0;
1058 td->io_issues[i] = 0;
1059 td->ts.total_io_u[i] = 0;
1060 }
1061
1062 fio_gettime(&tv, NULL);
1063 td->ts.runtime[0] = 0;
1064 td->ts.runtime[1] = 0;
1065 memcpy(&td->epoch, &tv, sizeof(tv));
1066 memcpy(&td->start, &tv, sizeof(tv));
1067}
1068
1069static void clear_io_state(struct thread_data *td)
1070{
1071 struct fio_file *f;
1072 unsigned int i;
1073
1074 reset_io_counters(td);
1075
1076 close_files(td);
1077 for_each_file(td, f, i)
1078 fio_file_clear_done(f);
1079
1080 /*
1081 * Set the same seed to get repeatable runs
1082 */
1083 td_fill_rand_seeds(td);
1084}
1085
1086static int exec_string(const char *string)
1087{
1088 int ret, newlen = strlen(string) + 1 + 8;
1089 char *str;
1090
1091 str = malloc(newlen);
1092 sprintf(str, "sh -c %s", string);
1093
1094 ret = system(str);
1095 if (ret == -1)
1096 log_err("fio: exec of cmd <%s> failed\n", str);
1097
1098 free(str);
1099 return ret;
1100}
1101
1102/*
1103 * Entry point for the thread based jobs. The process based jobs end up
1104 * here as well, after a little setup.
1105 */
1106static void *thread_main(void *data)
1107{
1108 unsigned long long elapsed;
1109 struct thread_data *td = data;
1110 pthread_condattr_t attr;
1111 int clear_state;
1112
1113 if (!td->o.use_thread) {
1114 setsid();
1115 td->pid = getpid();
1116 } else
1117 td->pid = gettid();
1118
1119 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1120
1121 INIT_FLIST_HEAD(&td->io_u_freelist);
1122 INIT_FLIST_HEAD(&td->io_u_busylist);
1123 INIT_FLIST_HEAD(&td->io_u_requeues);
1124 INIT_FLIST_HEAD(&td->io_log_list);
1125 INIT_FLIST_HEAD(&td->io_hist_list);
1126 INIT_FLIST_HEAD(&td->verify_list);
1127 INIT_FLIST_HEAD(&td->trim_list);
1128 pthread_mutex_init(&td->io_u_lock, NULL);
1129 td->io_hist_tree = RB_ROOT;
1130
1131 pthread_condattr_init(&attr);
1132 pthread_cond_init(&td->verify_cond, &attr);
1133 pthread_cond_init(&td->free_cond, &attr);
1134
1135 td_set_runstate(td, TD_INITIALIZED);
1136 dprint(FD_MUTEX, "up startup_mutex\n");
1137 fio_mutex_up(startup_mutex);
1138 dprint(FD_MUTEX, "wait on td->mutex\n");
1139 fio_mutex_down(td->mutex);
1140 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1141
1142 /*
1143 * the ->mutex mutex is now no longer used, close it to avoid
1144 * eating a file descriptor
1145 */
1146 fio_mutex_remove(td->mutex);
1147
1148 /*
1149 * A new gid requires privilege, so we need to do this before setting
1150 * the uid.
1151 */
1152 if (td->o.gid != -1U && setgid(td->o.gid)) {
1153 td_verror(td, errno, "setgid");
1154 goto err;
1155 }
1156 if (td->o.uid != -1U && setuid(td->o.uid)) {
1157 td_verror(td, errno, "setuid");
1158 goto err;
1159 }
1160
1161 /*
1162 * If we have a gettimeofday() thread, make sure we exclude that
1163 * thread from this job
1164 */
1165 if (td->o.gtod_cpu)
1166 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1167
1168 /*
1169 * Set affinity first, in case it has an impact on the memory
1170 * allocations.
1171 */
1172 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1173 td_verror(td, errno, "cpu_set_affinity");
1174 goto err;
1175 }
1176
1177 /*
1178 * May alter parameters that init_io_u() will use, so we need to
1179 * do this first.
1180 */
1181 if (init_iolog(td))
1182 goto err;
1183
1184 if (init_io_u(td))
1185 goto err;
1186
1187 if (td->o.verify_async && verify_async_init(td))
1188 goto err;
1189
1190 if (td->ioprio_set) {
1191 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1192 td_verror(td, errno, "ioprio_set");
1193 goto err;
1194 }
1195 }
1196
1197 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1198 goto err;
1199
1200 if (nice(td->o.nice) == -1) {
1201 td_verror(td, errno, "nice");
1202 goto err;
1203 }
1204
1205 if (td->o.ioscheduler && switch_ioscheduler(td))
1206 goto err;
1207
1208 if (!td->o.create_serialize && setup_files(td))
1209 goto err;
1210
1211 if (td_io_init(td))
1212 goto err;
1213
1214 if (init_random_map(td))
1215 goto err;
1216
1217 if (td->o.exec_prerun) {
1218 if (exec_string(td->o.exec_prerun))
1219 goto err;
1220 }
1221
1222 if (td->o.pre_read) {
1223 if (pre_read_files(td) < 0)
1224 goto err;
1225 }
1226
1227 fio_gettime(&td->epoch, NULL);
1228 getrusage(RUSAGE_SELF, &td->ru_start);
1229
1230 clear_state = 0;
1231 while (keep_running(td)) {
1232 fio_gettime(&td->start, NULL);
1233 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1234 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1235 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1236
1237 if (td->o.ratemin[0] || td->o.ratemin[1])
1238 memcpy(&td->lastrate, &td->bw_sample_time,
1239 sizeof(td->lastrate));
1240
1241 if (clear_state)
1242 clear_io_state(td);
1243
1244 prune_io_piece_log(td);
1245
1246 do_io(td);
1247
1248 clear_state = 1;
1249
1250 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1251 elapsed = utime_since_now(&td->start);
1252 td->ts.runtime[DDIR_READ] += elapsed;
1253 }
1254 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1255 elapsed = utime_since_now(&td->start);
1256 td->ts.runtime[DDIR_WRITE] += elapsed;
1257 }
1258
1259 if (td->error || td->terminate)
1260 break;
1261
1262 if (!td->o.do_verify ||
1263 td->o.verify == VERIFY_NONE ||
1264 (td->io_ops->flags & FIO_UNIDIR))
1265 continue;
1266
1267 clear_io_state(td);
1268
1269 fio_gettime(&td->start, NULL);
1270
1271 do_verify(td);
1272
1273 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1274
1275 if (td->error || td->terminate)
1276 break;
1277 }
1278
1279 update_rusage_stat(td);
1280 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1281 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
1282 td->ts.total_run_time = mtime_since_now(&td->epoch);
1283 td->ts.io_bytes[0] = td->io_bytes[0];
1284 td->ts.io_bytes[1] = td->io_bytes[1];
1285
1286 fio_mutex_down(writeout_mutex);
1287 if (td->bw_log) {
1288 if (td->o.bw_log_file) {
1289 finish_log_named(td, td->bw_log,
1290 td->o.bw_log_file, "bw");
1291 } else
1292 finish_log(td, td->bw_log, "bw");
1293 }
1294 if (td->lat_log) {
1295 if (td->o.lat_log_file) {
1296 finish_log_named(td, td->lat_log,
1297 td->o.lat_log_file, "lat");
1298 } else
1299 finish_log(td, td->lat_log, "lat");
1300 }
1301 if (td->slat_log) {
1302 if (td->o.lat_log_file) {
1303 finish_log_named(td, td->slat_log,
1304 td->o.lat_log_file, "slat");
1305 } else
1306 finish_log(td, td->slat_log, "slat");
1307 }
1308 if (td->clat_log) {
1309 if (td->o.lat_log_file) {
1310 finish_log_named(td, td->clat_log,
1311 td->o.lat_log_file, "clat");
1312 } else
1313 finish_log(td, td->clat_log, "clat");
1314 }
1315 if (td->iops_log) {
1316 if (td->o.iops_log_file) {
1317 finish_log_named(td, td->iops_log,
1318 td->o.iops_log_file, "iops");
1319 } else
1320 finish_log(td, td->iops_log, "iops");
1321 }
1322
1323 fio_mutex_up(writeout_mutex);
1324 if (td->o.exec_postrun)
1325 exec_string(td->o.exec_postrun);
1326
1327 if (exitall_on_terminate)
1328 fio_terminate_threads(td->groupid);
1329
1330err:
1331 if (td->error)
1332 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1333 td->verror);
1334
1335 if (td->o.verify_async)
1336 verify_async_exit(td);
1337
1338 close_and_free_files(td);
1339 close_ioengine(td);
1340 cleanup_io_u(td);
1341 cgroup_shutdown(td, &cgroup_mnt);
1342
1343 if (td->o.cpumask_set) {
1344 int ret = fio_cpuset_exit(&td->o.cpumask);
1345
1346 td_verror(td, ret, "fio_cpuset_exit");
1347 }
1348
1349 /*
1350 * do this very late, it will log file closing as well
1351 */
1352 if (td->o.write_iolog_file)
1353 write_iolog_close(td);
1354
1355 options_mem_free(td);
1356 td_set_runstate(td, TD_EXITED);
1357 return (void *) (unsigned long) td->error;
1358}
1359
1360/*
1361 * We cannot pass the td data into a forked process, so attach the td and
1362 * pass it to the thread worker.
1363 */
1364static int fork_main(int shmid, int offset)
1365{
1366 struct thread_data *td;
1367 void *data, *ret;
1368
1369#ifndef __hpux
1370 data = shmat(shmid, NULL, 0);
1371 if (data == (void *) -1) {
1372 int __err = errno;
1373
1374 perror("shmat");
1375 return __err;
1376 }
1377#else
1378 /*
1379 * HP-UX inherits shm mappings?
1380 */
1381 data = threads;
1382#endif
1383
1384 td = data + offset * sizeof(struct thread_data);
1385 ret = thread_main(td);
1386 shmdt(data);
1387 return (int) (unsigned long) ret;
1388}
1389
1390/*
1391 * Run over the job map and reap the threads that have exited, if any.
1392 */
1393static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1394{
1395 struct thread_data *td;
1396 int i, cputhreads, realthreads, pending, status, ret;
1397
1398 /*
1399 * reap exited threads (TD_EXITED -> TD_REAPED)
1400 */
1401 realthreads = pending = cputhreads = 0;
1402 for_each_td(td, i) {
1403 int flags = 0;
1404
1405 /*
1406 * ->io_ops is NULL for a thread that has closed its
1407 * io engine
1408 */
1409 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1410 cputhreads++;
1411 else
1412 realthreads++;
1413
1414 if (!td->pid) {
1415 pending++;
1416 continue;
1417 }
1418 if (td->runstate == TD_REAPED)
1419 continue;
1420 if (td->o.use_thread) {
1421 if (td->runstate == TD_EXITED) {
1422 td_set_runstate(td, TD_REAPED);
1423 goto reaped;
1424 }
1425 continue;
1426 }
1427
1428 flags = WNOHANG;
1429 if (td->runstate == TD_EXITED)
1430 flags = 0;
1431
1432 /*
1433 * check if someone quit or got killed in an unusual way
1434 */
1435 ret = waitpid(td->pid, &status, flags);
1436 if (ret < 0) {
1437 if (errno == ECHILD) {
1438 log_err("fio: pid=%d disappeared %d\n",
1439 (int) td->pid, td->runstate);
1440 td_set_runstate(td, TD_REAPED);
1441 goto reaped;
1442 }
1443 perror("waitpid");
1444 } else if (ret == td->pid) {
1445 if (WIFSIGNALED(status)) {
1446 int sig = WTERMSIG(status);
1447
1448 if (sig != SIGTERM)
1449 log_err("fio: pid=%d, got signal=%d\n",
1450 (int) td->pid, sig);
1451 td_set_runstate(td, TD_REAPED);
1452 goto reaped;
1453 }
1454 if (WIFEXITED(status)) {
1455 if (WEXITSTATUS(status) && !td->error)
1456 td->error = WEXITSTATUS(status);
1457
1458 td_set_runstate(td, TD_REAPED);
1459 goto reaped;
1460 }
1461 }
1462
1463 /*
1464 * thread is not dead, continue
1465 */
1466 pending++;
1467 continue;
1468reaped:
1469 (*nr_running)--;
1470 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1471 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
1472 if (!td->pid)
1473 pending--;
1474
1475 if (td->error)
1476 exit_value++;
1477
1478 done_secs += mtime_since_now(&td->epoch) / 1000;
1479 }
1480
1481 if (*nr_running == cputhreads && !pending && realthreads)
1482 fio_terminate_threads(TERMINATE_ALL);
1483}
1484
1485static void *gtod_thread_main(void *data)
1486{
1487 fio_mutex_up(startup_mutex);
1488
1489 /*
1490 * As long as we have jobs around, update the clock. It would be nice
1491 * to have some way of NOT hammering that CPU with gettimeofday(),
1492 * but I'm not sure what to use outside of a simple CPU nop to relax
1493 * it - we don't want to lose precision.
1494 */
1495 while (threads) {
1496 fio_gtod_update();
1497 nop;
1498 }
1499
1500 return NULL;
1501}
1502
1503static int fio_start_gtod_thread(void)
1504{
1505 pthread_attr_t attr;
1506 int ret;
1507
1508 pthread_attr_init(&attr);
1509 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1510 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, NULL);
1511 pthread_attr_destroy(&attr);
1512 if (ret) {
1513 log_err("Can't create gtod thread: %s\n", strerror(ret));
1514 return 1;
1515 }
1516
1517 ret = pthread_detach(gtod_thread);
1518 if (ret) {
1519 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
1520 return 1;
1521 }
1522
1523 dprint(FD_MUTEX, "wait on startup_mutex\n");
1524 fio_mutex_down(startup_mutex);
1525 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1526 return 0;
1527}
1528
1529/*
1530 * Main function for kicking off and reaping jobs, as needed.
1531 */
1532static void run_threads(void)
1533{
1534 struct thread_data *td;
1535 unsigned long spent;
1536 int i, todo, nr_running, m_rate, t_rate, nr_started;
1537
1538 if (fio_pin_memory())
1539 return;
1540
1541 if (fio_gtod_offload && fio_start_gtod_thread())
1542 return;
1543
1544 set_sig_handlers();
1545
1546 if (!terse_output) {
1547 log_info("Starting ");
1548 if (nr_thread)
1549 log_info("%d thread%s", nr_thread,
1550 nr_thread > 1 ? "s" : "");
1551 if (nr_process) {
1552 if (nr_thread)
1553 printf(" and ");
1554 log_info("%d process%s", nr_process,
1555 nr_process > 1 ? "es" : "");
1556 }
1557 log_info("\n");
1558 fflush(stdout);
1559 }
1560
1561 todo = thread_number;
1562 nr_running = 0;
1563 nr_started = 0;
1564 m_rate = t_rate = 0;
1565
1566 for_each_td(td, i) {
1567 print_status_init(td->thread_number - 1);
1568
1569 if (!td->o.create_serialize)
1570 continue;
1571
1572 /*
1573 * do file setup here so it happens sequentially,
1574 * we don't want X number of threads getting their
1575 * client data interspersed on disk
1576 */
1577 if (setup_files(td)) {
1578 exit_value++;
1579 if (td->error)
1580 log_err("fio: pid=%d, err=%d/%s\n",
1581 (int) td->pid, td->error, td->verror);
1582 td_set_runstate(td, TD_REAPED);
1583 todo--;
1584 } else {
1585 struct fio_file *f;
1586 unsigned int j;
1587
1588 /*
1589 * for sharing to work, each job must always open
1590 * its own files. so close them, if we opened them
1591 * for creation
1592 */
1593 for_each_file(td, f, j) {
1594 if (fio_file_open(f))
1595 td_io_close_file(td, f);
1596 }
1597 }
1598 }
1599
1600 set_genesis_time();
1601
1602 while (todo) {
1603 struct thread_data *map[REAL_MAX_JOBS];
1604 struct timeval this_start;
1605 int this_jobs = 0, left;
1606
1607 /*
1608 * create threads (TD_NOT_CREATED -> TD_CREATED)
1609 */
1610 for_each_td(td, i) {
1611 if (td->runstate != TD_NOT_CREATED)
1612 continue;
1613
1614 /*
1615 * never got a chance to start, killed by other
1616 * thread for some reason
1617 */
1618 if (td->terminate) {
1619 todo--;
1620 continue;
1621 }
1622
1623 if (td->o.start_delay) {
1624 spent = mtime_since_genesis();
1625
1626 if (td->o.start_delay * 1000 > spent)
1627 continue;
1628 }
1629
1630 if (td->o.stonewall && (nr_started || nr_running)) {
1631 dprint(FD_PROCESS, "%s: stonewall wait\n",
1632 td->o.name);
1633 break;
1634 }
1635
1636 init_disk_util(td);
1637
1638 /*
1639 * Set state to created. Thread will transition
1640 * to TD_INITIALIZED when it's done setting up.
1641 */
1642 td_set_runstate(td, TD_CREATED);
1643 map[this_jobs++] = td;
1644 nr_started++;
1645
1646 if (td->o.use_thread) {
1647 int ret;
1648
1649 dprint(FD_PROCESS, "will pthread_create\n");
1650 ret = pthread_create(&td->thread, NULL,
1651 thread_main, td);
1652 if (ret) {
1653 log_err("pthread_create: %s\n",
1654 strerror(ret));
1655 nr_started--;
1656 break;
1657 }
1658 ret = pthread_detach(td->thread);
1659 if (ret)
1660 log_err("pthread_detach: %s",
1661 strerror(ret));
1662 } else {
1663 pid_t pid;
1664 dprint(FD_PROCESS, "will fork\n");
1665 pid = fork();
1666 if (!pid) {
1667 int ret = fork_main(shm_id, i);
1668
1669 _exit(ret);
1670 } else if (i == fio_debug_jobno)
1671 *fio_debug_jobp = pid;
1672 }
1673 dprint(FD_MUTEX, "wait on startup_mutex\n");
1674 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1675 log_err("fio: job startup hung? exiting.\n");
1676 fio_terminate_threads(TERMINATE_ALL);
1677 fio_abort = 1;
1678 nr_started--;
1679 break;
1680 }
1681 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1682 }
1683
1684 /*
1685 * Wait for the started threads to transition to
1686 * TD_INITIALIZED.
1687 */
1688 fio_gettime(&this_start, NULL);
1689 left = this_jobs;
1690 while (left && !fio_abort) {
1691 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1692 break;
1693
1694 usleep(100000);
1695
1696 for (i = 0; i < this_jobs; i++) {
1697 td = map[i];
1698 if (!td)
1699 continue;
1700 if (td->runstate == TD_INITIALIZED) {
1701 map[i] = NULL;
1702 left--;
1703 } else if (td->runstate >= TD_EXITED) {
1704 map[i] = NULL;
1705 left--;
1706 todo--;
1707 nr_running++; /* work-around... */
1708 }
1709 }
1710 }
1711
1712 if (left) {
1713 log_err("fio: %d jobs failed to start\n", left);
1714 for (i = 0; i < this_jobs; i++) {
1715 td = map[i];
1716 if (!td)
1717 continue;
1718 kill(td->pid, SIGTERM);
1719 }
1720 break;
1721 }
1722
1723 /*
1724 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1725 */
1726 for_each_td(td, i) {
1727 if (td->runstate != TD_INITIALIZED)
1728 continue;
1729
1730 if (in_ramp_time(td))
1731 td_set_runstate(td, TD_RAMP);
1732 else
1733 td_set_runstate(td, TD_RUNNING);
1734 nr_running++;
1735 nr_started--;
1736 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1737 t_rate += td->o.rate[0] + td->o.rate[1];
1738 todo--;
1739 fio_mutex_up(td->mutex);
1740 }
1741
1742 reap_threads(&nr_running, &t_rate, &m_rate);
1743
1744 if (todo) {
1745 if (is_backend)
1746 fio_server_idle_loop();
1747 else
1748 usleep(100000);
1749 }
1750 }
1751
1752 while (nr_running) {
1753 reap_threads(&nr_running, &t_rate, &m_rate);
1754
1755 if (is_backend)
1756 fio_server_idle_loop();
1757 else
1758 usleep(10000);
1759 }
1760
1761 update_io_ticks();
1762 fio_unpin_memory();
1763}
1764
1765int exec_run(void)
1766{
1767 if (nr_clients)
1768 return fio_handle_clients();
1769 if (exec_profile && load_profile(exec_profile))
1770 return 1;
1771
1772 if (!thread_number)
1773 return 0;
1774
1775 if (write_bw_log) {
1776 setup_log(&agg_io_log[DDIR_READ]);
1777 setup_log(&agg_io_log[DDIR_WRITE]);
1778 }
1779
1780 startup_mutex = fio_mutex_init(0);
1781 if (startup_mutex == NULL)
1782 return 1;
1783 writeout_mutex = fio_mutex_init(1);
1784 if (writeout_mutex == NULL)
1785 return 1;
1786
1787 set_genesis_time();
1788 create_disk_util_thread();
1789
1790 cgroup_list = smalloc(sizeof(*cgroup_list));
1791 INIT_FLIST_HEAD(cgroup_list);
1792
1793 run_threads();
1794
1795 if (!fio_abort) {
1796 show_run_stats();
1797 if (write_bw_log) {
1798 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1799 __finish_log(agg_io_log[DDIR_WRITE],
1800 "agg-write_bw.log");
1801 }
1802 }
1803
1804 cgroup_kill(cgroup_list);
1805 sfree(cgroup_list);
1806 sfree(cgroup_mnt);
1807
1808 fio_mutex_remove(startup_mutex);
1809 fio_mutex_remove(writeout_mutex);
1810 return exit_value;
1811}
1812
1813void reset_fio_state(void)
1814{
1815 groupid = 0;
1816 thread_number = 0;
1817 nr_process = 0;
1818 nr_thread = 0;
1819 done_secs = 0;
1820}
1821
1822static int endian_check(void)
1823{
1824 union {
1825 uint8_t c[8];
1826 uint64_t v;
1827 } u;
1828 int le = 0, be = 0;
1829
1830 u.v = 0x12;
1831 if (u.c[7] == 0x12)
1832 be = 1;
1833 else if (u.c[0] == 0x12)
1834 le = 1;
1835
1836#if defined(FIO_LITTLE_ENDIAN)
1837 if (be)
1838 return 1;
1839#elif defined(FIO_BIG_ENDIAN)
1840 if (le)
1841 return 1;
1842#else
1843 return 1;
1844#endif
1845
1846 if (!le && !be)
1847 return 1;
1848
1849 return 0;
1850}
1851
1852int main(int argc, char *argv[], char *envp[])
1853{
1854 long ps;
1855
1856 if (endian_check()) {
1857 log_err("fio: endianness settings appear wrong.\n");
1858 log_err("fio: please report this to fio@vger.kernel.org\n");
1859 return 1;
1860 }
1861
1862 arch_init(envp);
1863
1864 sinit();
1865
1866 /*
1867 * We need locale for number printing, if it isn't set then just
1868 * go with the US format.
1869 */
1870 if (!getenv("LC_NUMERIC"))
1871 setlocale(LC_NUMERIC, "en_US");
1872
1873 ps = sysconf(_SC_PAGESIZE);
1874 if (ps < 0) {
1875 log_err("Failed to get page size\n");
1876 return 1;
1877 }
1878
1879 page_size = ps;
1880 page_mask = ps - 1;
1881
1882 fio_keywords_init();
1883
1884 if (parse_options(argc, argv))
1885 return 1;
1886
1887 return exec_run();
1888}