fio: fix ignore_error regression
[fio.git] / backend.c
... / ...
CommitLineData
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006-2012 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 <inttypes.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
36#include <sys/ipc.h>
37#include <sys/mman.h>
38#include <math.h>
39
40#include "fio.h"
41#ifndef FIO_NO_HAVE_SHM_H
42#include <sys/shm.h>
43#endif
44#include "hash.h"
45#include "smalloc.h"
46#include "verify.h"
47#include "trim.h"
48#include "diskutil.h"
49#include "cgroup.h"
50#include "profile.h"
51#include "lib/rand.h"
52#include "lib/memalign.h"
53#include "server.h"
54#include "lib/getrusage.h"
55#include "idletime.h"
56#include "err.h"
57#include "workqueue.h"
58#include "lib/mountcheck.h"
59#include "rate-submit.h"
60
61static pthread_t helper_thread;
62static pthread_mutex_t helper_lock;
63pthread_cond_t helper_cond;
64int helper_do_stat = 0;
65
66static struct fio_mutex *startup_mutex;
67static struct flist_head *cgroup_list;
68static char *cgroup_mnt;
69static int exit_value;
70static volatile int fio_abort;
71static unsigned int nr_process = 0;
72static unsigned int nr_thread = 0;
73
74struct io_log *agg_io_log[DDIR_RWDIR_CNT];
75
76int groupid = 0;
77unsigned int thread_number = 0;
78unsigned int stat_number = 0;
79int shm_id = 0;
80int temp_stall_ts;
81unsigned long done_secs = 0;
82volatile int helper_exit = 0;
83
84#define PAGE_ALIGN(buf) \
85 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
86
87#define JOB_START_TIMEOUT (5 * 1000)
88
89static void sig_int(int sig)
90{
91 if (threads) {
92 if (is_backend)
93 fio_server_got_signal(sig);
94 else {
95 log_info("\nfio: terminating on signal %d\n", sig);
96 log_info_flush();
97 exit_value = 128;
98 }
99
100 fio_terminate_threads(TERMINATE_ALL);
101 }
102}
103
104void sig_show_status(int sig)
105{
106 show_running_run_stats();
107}
108
109static void set_sig_handlers(void)
110{
111 struct sigaction act;
112
113 memset(&act, 0, sizeof(act));
114 act.sa_handler = sig_int;
115 act.sa_flags = SA_RESTART;
116 sigaction(SIGINT, &act, NULL);
117
118 memset(&act, 0, sizeof(act));
119 act.sa_handler = sig_int;
120 act.sa_flags = SA_RESTART;
121 sigaction(SIGTERM, &act, NULL);
122
123/* Windows uses SIGBREAK as a quit signal from other applications */
124#ifdef WIN32
125 memset(&act, 0, sizeof(act));
126 act.sa_handler = sig_int;
127 act.sa_flags = SA_RESTART;
128 sigaction(SIGBREAK, &act, NULL);
129#endif
130
131 memset(&act, 0, sizeof(act));
132 act.sa_handler = sig_show_status;
133 act.sa_flags = SA_RESTART;
134 sigaction(SIGUSR1, &act, NULL);
135
136 if (is_backend) {
137 memset(&act, 0, sizeof(act));
138 act.sa_handler = sig_int;
139 act.sa_flags = SA_RESTART;
140 sigaction(SIGPIPE, &act, NULL);
141 }
142}
143
144/*
145 * Check if we are above the minimum rate given.
146 */
147static bool __check_min_rate(struct thread_data *td, struct timeval *now,
148 enum fio_ddir ddir)
149{
150 unsigned long long bytes = 0;
151 unsigned long iops = 0;
152 unsigned long spent;
153 unsigned long rate;
154 unsigned int ratemin = 0;
155 unsigned int rate_iops = 0;
156 unsigned int rate_iops_min = 0;
157
158 assert(ddir_rw(ddir));
159
160 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
161 return false;
162
163 /*
164 * allow a 2 second settle period in the beginning
165 */
166 if (mtime_since(&td->start, now) < 2000)
167 return false;
168
169 iops += td->this_io_blocks[ddir];
170 bytes += td->this_io_bytes[ddir];
171 ratemin += td->o.ratemin[ddir];
172 rate_iops += td->o.rate_iops[ddir];
173 rate_iops_min += td->o.rate_iops_min[ddir];
174
175 /*
176 * if rate blocks is set, sample is running
177 */
178 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
179 spent = mtime_since(&td->lastrate[ddir], now);
180 if (spent < td->o.ratecycle)
181 return false;
182
183 if (td->o.rate[ddir] || td->o.ratemin[ddir]) {
184 /*
185 * check bandwidth specified rate
186 */
187 if (bytes < td->rate_bytes[ddir]) {
188 log_err("%s: min rate %u not met\n", td->o.name,
189 ratemin);
190 return true;
191 } else {
192 if (spent)
193 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
194 else
195 rate = 0;
196
197 if (rate < ratemin ||
198 bytes < td->rate_bytes[ddir]) {
199 log_err("%s: min rate %u not met, got"
200 " %luKB/sec\n", td->o.name,
201 ratemin, rate);
202 return true;
203 }
204 }
205 } else {
206 /*
207 * checks iops specified rate
208 */
209 if (iops < rate_iops) {
210 log_err("%s: min iops rate %u not met\n",
211 td->o.name, rate_iops);
212 return true;
213 } else {
214 if (spent)
215 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
216 else
217 rate = 0;
218
219 if (rate < rate_iops_min ||
220 iops < td->rate_blocks[ddir]) {
221 log_err("%s: min iops rate %u not met,"
222 " got %lu\n", td->o.name,
223 rate_iops_min, rate);
224 return true;
225 }
226 }
227 }
228 }
229
230 td->rate_bytes[ddir] = bytes;
231 td->rate_blocks[ddir] = iops;
232 memcpy(&td->lastrate[ddir], now, sizeof(*now));
233 return false;
234}
235
236static bool check_min_rate(struct thread_data *td, struct timeval *now)
237{
238 bool ret = false;
239
240 if (td->bytes_done[DDIR_READ])
241 ret |= __check_min_rate(td, now, DDIR_READ);
242 if (td->bytes_done[DDIR_WRITE])
243 ret |= __check_min_rate(td, now, DDIR_WRITE);
244 if (td->bytes_done[DDIR_TRIM])
245 ret |= __check_min_rate(td, now, DDIR_TRIM);
246
247 return ret;
248}
249
250/*
251 * When job exits, we can cancel the in-flight IO if we are using async
252 * io. Attempt to do so.
253 */
254static void cleanup_pending_aio(struct thread_data *td)
255{
256 int r;
257
258 /*
259 * get immediately available events, if any
260 */
261 r = io_u_queued_complete(td, 0);
262 if (r < 0)
263 return;
264
265 /*
266 * now cancel remaining active events
267 */
268 if (td->io_ops->cancel) {
269 struct io_u *io_u;
270 int i;
271
272 io_u_qiter(&td->io_u_all, io_u, i) {
273 if (io_u->flags & IO_U_F_FLIGHT) {
274 r = td->io_ops->cancel(td, io_u);
275 if (!r)
276 put_io_u(td, io_u);
277 }
278 }
279 }
280
281 if (td->cur_depth)
282 r = io_u_queued_complete(td, td->cur_depth);
283}
284
285/*
286 * Helper to handle the final sync of a file. Works just like the normal
287 * io path, just does everything sync.
288 */
289static bool fio_io_sync(struct thread_data *td, struct fio_file *f)
290{
291 struct io_u *io_u = __get_io_u(td);
292 int ret;
293
294 if (!io_u)
295 return true;
296
297 io_u->ddir = DDIR_SYNC;
298 io_u->file = f;
299
300 if (td_io_prep(td, io_u)) {
301 put_io_u(td, io_u);
302 return true;
303 }
304
305requeue:
306 ret = td_io_queue(td, io_u);
307 if (ret < 0) {
308 td_verror(td, io_u->error, "td_io_queue");
309 put_io_u(td, io_u);
310 return true;
311 } else if (ret == FIO_Q_QUEUED) {
312 if (io_u_queued_complete(td, 1) < 0)
313 return true;
314 } else if (ret == FIO_Q_COMPLETED) {
315 if (io_u->error) {
316 td_verror(td, io_u->error, "td_io_queue");
317 return true;
318 }
319
320 if (io_u_sync_complete(td, io_u) < 0)
321 return true;
322 } else if (ret == FIO_Q_BUSY) {
323 if (td_io_commit(td))
324 return true;
325 goto requeue;
326 }
327
328 return false;
329}
330
331static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
332{
333 int ret;
334
335 if (fio_file_open(f))
336 return fio_io_sync(td, f);
337
338 if (td_io_open_file(td, f))
339 return 1;
340
341 ret = fio_io_sync(td, f);
342 td_io_close_file(td, f);
343 return ret;
344}
345
346static inline void __update_tv_cache(struct thread_data *td)
347{
348 fio_gettime(&td->tv_cache, NULL);
349}
350
351static inline void update_tv_cache(struct thread_data *td)
352{
353 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
354 __update_tv_cache(td);
355}
356
357static inline bool runtime_exceeded(struct thread_data *td, struct timeval *t)
358{
359 if (in_ramp_time(td))
360 return false;
361 if (!td->o.timeout)
362 return false;
363 if (utime_since(&td->epoch, t) >= td->o.timeout)
364 return true;
365
366 return false;
367}
368
369/*
370 * We need to update the runtime consistently in ms, but keep a running
371 * tally of the current elapsed time in microseconds for sub millisecond
372 * updates.
373 */
374static inline void update_runtime(struct thread_data *td,
375 unsigned long long *elapsed_us,
376 const enum fio_ddir ddir)
377{
378 if (ddir == DDIR_WRITE && td_write(td) && td->o.verify_only)
379 return;
380
381 td->ts.runtime[ddir] -= (elapsed_us[ddir] + 999) / 1000;
382 elapsed_us[ddir] += utime_since_now(&td->start);
383 td->ts.runtime[ddir] += (elapsed_us[ddir] + 999) / 1000;
384}
385
386static bool break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
387 int *retptr)
388{
389 int ret = *retptr;
390
391 if (ret < 0 || td->error) {
392 int err = td->error;
393 enum error_type_bit eb;
394
395 if (ret < 0)
396 err = -ret;
397
398 eb = td_error_type(ddir, err);
399 if (!(td->o.continue_on_error & (1 << eb)))
400 return true;
401
402 if (td_non_fatal_error(td, eb, err)) {
403 /*
404 * Continue with the I/Os in case of
405 * a non fatal error.
406 */
407 update_error_count(td, err);
408 td_clear_error(td);
409 *retptr = 0;
410 return false;
411 } else if (td->o.fill_device && err == ENOSPC) {
412 /*
413 * We expect to hit this error if
414 * fill_device option is set.
415 */
416 td_clear_error(td);
417 fio_mark_td_terminate(td);
418 return true;
419 } else {
420 /*
421 * Stop the I/O in case of a fatal
422 * error.
423 */
424 update_error_count(td, err);
425 return true;
426 }
427 }
428
429 return false;
430}
431
432static void check_update_rusage(struct thread_data *td)
433{
434 if (td->update_rusage) {
435 td->update_rusage = 0;
436 update_rusage_stat(td);
437 fio_mutex_up(td->rusage_sem);
438 }
439}
440
441static int wait_for_completions(struct thread_data *td, struct timeval *time)
442{
443 const int full = queue_full(td);
444 int min_evts = 0;
445 int ret;
446
447 /*
448 * if the queue is full, we MUST reap at least 1 event
449 */
450 min_evts = min(td->o.iodepth_batch_complete_min, td->cur_depth);
451 if ((full && !min_evts) || !td->o.iodepth_batch_complete_min)
452 min_evts = 1;
453
454 if (time && (__should_check_rate(td, DDIR_READ) ||
455 __should_check_rate(td, DDIR_WRITE) ||
456 __should_check_rate(td, DDIR_TRIM)))
457 fio_gettime(time, NULL);
458
459 do {
460 ret = io_u_queued_complete(td, min_evts);
461 if (ret < 0)
462 break;
463 } while (full && (td->cur_depth > td->o.iodepth_low));
464
465 return ret;
466}
467
468int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
469 enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
470 struct timeval *comp_time)
471{
472 int ret2;
473
474 switch (*ret) {
475 case FIO_Q_COMPLETED:
476 if (io_u->error) {
477 *ret = -io_u->error;
478 clear_io_u(td, io_u);
479 } else if (io_u->resid) {
480 int bytes = io_u->xfer_buflen - io_u->resid;
481 struct fio_file *f = io_u->file;
482
483 if (bytes_issued)
484 *bytes_issued += bytes;
485
486 if (!from_verify)
487 trim_io_piece(td, io_u);
488
489 /*
490 * zero read, fail
491 */
492 if (!bytes) {
493 if (!from_verify)
494 unlog_io_piece(td, io_u);
495 td_verror(td, EIO, "full resid");
496 put_io_u(td, io_u);
497 break;
498 }
499
500 io_u->xfer_buflen = io_u->resid;
501 io_u->xfer_buf += bytes;
502 io_u->offset += bytes;
503
504 if (ddir_rw(io_u->ddir))
505 td->ts.short_io_u[io_u->ddir]++;
506
507 f = io_u->file;
508 if (io_u->offset == f->real_file_size)
509 goto sync_done;
510
511 requeue_io_u(td, &io_u);
512 } else {
513sync_done:
514 if (comp_time && (__should_check_rate(td, DDIR_READ) ||
515 __should_check_rate(td, DDIR_WRITE) ||
516 __should_check_rate(td, DDIR_TRIM)))
517 fio_gettime(comp_time, NULL);
518
519 *ret = io_u_sync_complete(td, io_u);
520 if (*ret < 0)
521 break;
522 }
523
524 /*
525 * when doing I/O (not when verifying),
526 * check for any errors that are to be ignored
527 */
528 if (!from_verify)
529 break;
530
531 return 0;
532 case FIO_Q_QUEUED:
533 /*
534 * if the engine doesn't have a commit hook,
535 * the io_u is really queued. if it does have such
536 * a hook, it has to call io_u_queued() itself.
537 */
538 if (td->io_ops->commit == NULL)
539 io_u_queued(td, io_u);
540 if (bytes_issued)
541 *bytes_issued += io_u->xfer_buflen;
542 break;
543 case FIO_Q_BUSY:
544 if (!from_verify)
545 unlog_io_piece(td, io_u);
546 requeue_io_u(td, &io_u);
547 ret2 = td_io_commit(td);
548 if (ret2 < 0)
549 *ret = ret2;
550 break;
551 default:
552 assert(*ret < 0);
553 td_verror(td, -(*ret), "td_io_queue");
554 break;
555 }
556
557 if (break_on_this_error(td, ddir, ret))
558 return 1;
559
560 return 0;
561}
562
563static inline bool io_in_polling(struct thread_data *td)
564{
565 return !td->o.iodepth_batch_complete_min &&
566 !td->o.iodepth_batch_complete_max;
567}
568
569/*
570 * The main verify engine. Runs over the writes we previously submitted,
571 * reads the blocks back in, and checks the crc/md5 of the data.
572 */
573static void do_verify(struct thread_data *td, uint64_t verify_bytes)
574{
575 struct fio_file *f;
576 struct io_u *io_u;
577 int ret, min_events;
578 unsigned int i;
579
580 dprint(FD_VERIFY, "starting loop\n");
581
582 /*
583 * sync io first and invalidate cache, to make sure we really
584 * read from disk.
585 */
586 for_each_file(td, f, i) {
587 if (!fio_file_open(f))
588 continue;
589 if (fio_io_sync(td, f))
590 break;
591 if (file_invalidate_cache(td, f))
592 break;
593 }
594
595 check_update_rusage(td);
596
597 if (td->error)
598 return;
599
600 td_set_runstate(td, TD_VERIFYING);
601
602 io_u = NULL;
603 while (!td->terminate) {
604 enum fio_ddir ddir;
605 int full;
606
607 update_tv_cache(td);
608 check_update_rusage(td);
609
610 if (runtime_exceeded(td, &td->tv_cache)) {
611 __update_tv_cache(td);
612 if (runtime_exceeded(td, &td->tv_cache)) {
613 fio_mark_td_terminate(td);
614 break;
615 }
616 }
617
618 if (flow_threshold_exceeded(td))
619 continue;
620
621 if (!td->o.experimental_verify) {
622 io_u = __get_io_u(td);
623 if (!io_u)
624 break;
625
626 if (get_next_verify(td, io_u)) {
627 put_io_u(td, io_u);
628 break;
629 }
630
631 if (td_io_prep(td, io_u)) {
632 put_io_u(td, io_u);
633 break;
634 }
635 } else {
636 if (ddir_rw_sum(td->bytes_done) + td->o.rw_min_bs > verify_bytes)
637 break;
638
639 while ((io_u = get_io_u(td)) != NULL) {
640 if (IS_ERR(io_u)) {
641 io_u = NULL;
642 ret = FIO_Q_BUSY;
643 goto reap;
644 }
645
646 /*
647 * We are only interested in the places where
648 * we wrote or trimmed IOs. Turn those into
649 * reads for verification purposes.
650 */
651 if (io_u->ddir == DDIR_READ) {
652 /*
653 * Pretend we issued it for rwmix
654 * accounting
655 */
656 td->io_issues[DDIR_READ]++;
657 put_io_u(td, io_u);
658 continue;
659 } else if (io_u->ddir == DDIR_TRIM) {
660 io_u->ddir = DDIR_READ;
661 io_u_set(io_u, IO_U_F_TRIMMED);
662 break;
663 } else if (io_u->ddir == DDIR_WRITE) {
664 io_u->ddir = DDIR_READ;
665 break;
666 } else {
667 put_io_u(td, io_u);
668 continue;
669 }
670 }
671
672 if (!io_u)
673 break;
674 }
675
676 if (verify_state_should_stop(td, io_u)) {
677 put_io_u(td, io_u);
678 break;
679 }
680
681 if (td->o.verify_async)
682 io_u->end_io = verify_io_u_async;
683 else
684 io_u->end_io = verify_io_u;
685
686 ddir = io_u->ddir;
687 if (!td->o.disable_slat)
688 fio_gettime(&io_u->start_time, NULL);
689
690 ret = td_io_queue(td, io_u);
691
692 if (io_queue_event(td, io_u, &ret, ddir, NULL, 1, NULL))
693 break;
694
695 /*
696 * if we can queue more, do so. but check if there are
697 * completed io_u's first. Note that we can get BUSY even
698 * without IO queued, if the system is resource starved.
699 */
700reap:
701 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
702 if (full || io_in_polling(td))
703 ret = wait_for_completions(td, NULL);
704
705 if (ret < 0)
706 break;
707 }
708
709 check_update_rusage(td);
710
711 if (!td->error) {
712 min_events = td->cur_depth;
713
714 if (min_events)
715 ret = io_u_queued_complete(td, min_events);
716 } else
717 cleanup_pending_aio(td);
718
719 td_set_runstate(td, TD_RUNNING);
720
721 dprint(FD_VERIFY, "exiting loop\n");
722}
723
724static bool exceeds_number_ios(struct thread_data *td)
725{
726 unsigned long long number_ios;
727
728 if (!td->o.number_ios)
729 return false;
730
731 number_ios = ddir_rw_sum(td->io_blocks);
732 number_ios += td->io_u_queued + td->io_u_in_flight;
733
734 return number_ios >= (td->o.number_ios * td->loops);
735}
736
737static bool io_issue_bytes_exceeded(struct thread_data *td)
738{
739 unsigned long long bytes, limit;
740
741 if (td_rw(td))
742 bytes = td->io_issue_bytes[DDIR_READ] + td->io_issue_bytes[DDIR_WRITE];
743 else if (td_write(td))
744 bytes = td->io_issue_bytes[DDIR_WRITE];
745 else if (td_read(td))
746 bytes = td->io_issue_bytes[DDIR_READ];
747 else
748 bytes = td->io_issue_bytes[DDIR_TRIM];
749
750 if (td->o.io_limit)
751 limit = td->o.io_limit;
752 else
753 limit = td->o.size;
754
755 limit *= td->loops;
756 return bytes >= limit || exceeds_number_ios(td);
757}
758
759static bool io_complete_bytes_exceeded(struct thread_data *td)
760{
761 unsigned long long bytes, limit;
762
763 if (td_rw(td))
764 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
765 else if (td_write(td))
766 bytes = td->this_io_bytes[DDIR_WRITE];
767 else if (td_read(td))
768 bytes = td->this_io_bytes[DDIR_READ];
769 else
770 bytes = td->this_io_bytes[DDIR_TRIM];
771
772 if (td->o.io_limit)
773 limit = td->o.io_limit;
774 else
775 limit = td->o.size;
776
777 limit *= td->loops;
778 return bytes >= limit || exceeds_number_ios(td);
779}
780
781/*
782 * used to calculate the next io time for rate control
783 *
784 */
785static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
786{
787 uint64_t secs, remainder, bps, bytes, iops;
788
789 assert(!(td->flags & TD_F_CHILD));
790 bytes = td->rate_io_issue_bytes[ddir];
791 bps = td->rate_bps[ddir];
792
793 if (td->o.rate_process == RATE_PROCESS_POISSON) {
794 uint64_t val;
795 iops = bps / td->o.bs[ddir];
796 val = (int64_t) (1000000 / iops) *
797 -logf(__rand_0_1(&td->poisson_state));
798 if (val) {
799 dprint(FD_RATE, "poisson rate iops=%llu\n",
800 (unsigned long long) 1000000 / val);
801 }
802 td->last_usec += val;
803 return td->last_usec;
804 } else if (bps) {
805 secs = bytes / bps;
806 remainder = bytes % bps;
807 return remainder * 1000000 / bps + secs * 1000000;
808 }
809
810 return 0;
811}
812
813/*
814 * Main IO worker function. It retrieves io_u's to process and queues
815 * and reaps them, checking for rate and errors along the way.
816 *
817 * Returns number of bytes written and trimmed.
818 */
819static void do_io(struct thread_data *td, uint64_t *bytes_done)
820{
821 unsigned int i;
822 int ret = 0;
823 uint64_t total_bytes, bytes_issued = 0;
824
825 for (i = 0; i < DDIR_RWDIR_CNT; i++)
826 bytes_done[i] = td->bytes_done[i];
827
828 if (in_ramp_time(td))
829 td_set_runstate(td, TD_RAMP);
830 else
831 td_set_runstate(td, TD_RUNNING);
832
833 lat_target_init(td);
834
835 total_bytes = td->o.size;
836 /*
837 * Allow random overwrite workloads to write up to io_limit
838 * before starting verification phase as 'size' doesn't apply.
839 */
840 if (td_write(td) && td_random(td) && td->o.norandommap)
841 total_bytes = max(total_bytes, (uint64_t) td->o.io_limit);
842 /*
843 * If verify_backlog is enabled, we'll run the verify in this
844 * handler as well. For that case, we may need up to twice the
845 * amount of bytes.
846 */
847 if (td->o.verify != VERIFY_NONE &&
848 (td_write(td) && td->o.verify_backlog))
849 total_bytes += td->o.size;
850
851 /* In trimwrite mode, each byte is trimmed and then written, so
852 * allow total_bytes to be twice as big */
853 if (td_trimwrite(td))
854 total_bytes += td->total_io_size;
855
856 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
857 (!flist_empty(&td->trim_list)) || !io_issue_bytes_exceeded(td) ||
858 td->o.time_based) {
859 struct timeval comp_time;
860 struct io_u *io_u;
861 int full;
862 enum fio_ddir ddir;
863
864 check_update_rusage(td);
865
866 if (td->terminate || td->done)
867 break;
868
869 update_tv_cache(td);
870
871 if (runtime_exceeded(td, &td->tv_cache)) {
872 __update_tv_cache(td);
873 if (runtime_exceeded(td, &td->tv_cache)) {
874 fio_mark_td_terminate(td);
875 break;
876 }
877 }
878
879 if (flow_threshold_exceeded(td))
880 continue;
881
882 if (!td->o.time_based && bytes_issued >= total_bytes)
883 break;
884
885 io_u = get_io_u(td);
886 if (IS_ERR_OR_NULL(io_u)) {
887 int err = PTR_ERR(io_u);
888
889 io_u = NULL;
890 if (err == -EBUSY) {
891 ret = FIO_Q_BUSY;
892 goto reap;
893 }
894 if (td->o.latency_target)
895 goto reap;
896 break;
897 }
898
899 ddir = io_u->ddir;
900
901 /*
902 * Add verification end_io handler if:
903 * - Asked to verify (!td_rw(td))
904 * - Or the io_u is from our verify list (mixed write/ver)
905 */
906 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
907 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
908
909 if (!td->o.verify_pattern_bytes) {
910 io_u->rand_seed = __rand(&td->verify_state);
911 if (sizeof(int) != sizeof(long *))
912 io_u->rand_seed *= __rand(&td->verify_state);
913 }
914
915 if (verify_state_should_stop(td, io_u)) {
916 put_io_u(td, io_u);
917 break;
918 }
919
920 if (td->o.verify_async)
921 io_u->end_io = verify_io_u_async;
922 else
923 io_u->end_io = verify_io_u;
924 td_set_runstate(td, TD_VERIFYING);
925 } else if (in_ramp_time(td))
926 td_set_runstate(td, TD_RAMP);
927 else
928 td_set_runstate(td, TD_RUNNING);
929
930 /*
931 * Always log IO before it's issued, so we know the specific
932 * order of it. The logged unit will track when the IO has
933 * completed.
934 */
935 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
936 td->o.do_verify &&
937 td->o.verify != VERIFY_NONE &&
938 !td->o.experimental_verify)
939 log_io_piece(td, io_u);
940
941 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
942 const unsigned long blen = io_u->xfer_buflen;
943 const enum fio_ddir ddir = acct_ddir(io_u);
944
945 if (td->error)
946 break;
947
948 workqueue_enqueue(&td->io_wq, &io_u->work);
949 ret = FIO_Q_QUEUED;
950
951 if (ddir_rw(ddir)) {
952 td->io_issues[ddir]++;
953 td->io_issue_bytes[ddir] += blen;
954 td->rate_io_issue_bytes[ddir] += blen;
955 }
956
957 if (should_check_rate(td))
958 td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
959
960 } else {
961 ret = td_io_queue(td, io_u);
962
963 if (should_check_rate(td))
964 td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
965
966 if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 0, &comp_time))
967 break;
968
969 /*
970 * See if we need to complete some commands. Note that
971 * we can get BUSY even without IO queued, if the
972 * system is resource starved.
973 */
974reap:
975 full = queue_full(td) ||
976 (ret == FIO_Q_BUSY && td->cur_depth);
977 if (full || io_in_polling(td))
978 ret = wait_for_completions(td, &comp_time);
979 }
980 if (ret < 0)
981 break;
982 if (!ddir_rw_sum(td->bytes_done) &&
983 !(td->io_ops->flags & FIO_NOIO))
984 continue;
985
986 if (!in_ramp_time(td) && should_check_rate(td)) {
987 if (check_min_rate(td, &comp_time)) {
988 if (exitall_on_terminate || td->o.exitall_error)
989 fio_terminate_threads(td->groupid);
990 td_verror(td, EIO, "check_min_rate");
991 break;
992 }
993 }
994 if (!in_ramp_time(td) && td->o.latency_target)
995 lat_target_check(td);
996
997 if (td->o.thinktime) {
998 unsigned long long b;
999
1000 b = ddir_rw_sum(td->io_blocks);
1001 if (!(b % td->o.thinktime_blocks)) {
1002 int left;
1003
1004 io_u_quiesce(td);
1005
1006 if (td->o.thinktime_spin)
1007 usec_spin(td->o.thinktime_spin);
1008
1009 left = td->o.thinktime - td->o.thinktime_spin;
1010 if (left)
1011 usec_sleep(td, left);
1012 }
1013 }
1014 }
1015
1016 check_update_rusage(td);
1017
1018 if (td->trim_entries)
1019 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
1020
1021 if (td->o.fill_device && td->error == ENOSPC) {
1022 td->error = 0;
1023 fio_mark_td_terminate(td);
1024 }
1025 if (!td->error) {
1026 struct fio_file *f;
1027
1028 if (td->o.io_submit_mode == IO_MODE_OFFLOAD) {
1029 workqueue_flush(&td->io_wq);
1030 i = 0;
1031 } else
1032 i = td->cur_depth;
1033
1034 if (i) {
1035 ret = io_u_queued_complete(td, i);
1036 if (td->o.fill_device && td->error == ENOSPC)
1037 td->error = 0;
1038 }
1039
1040 if (should_fsync(td) && td->o.end_fsync) {
1041 td_set_runstate(td, TD_FSYNCING);
1042
1043 for_each_file(td, f, i) {
1044 if (!fio_file_fsync(td, f))
1045 continue;
1046
1047 log_err("fio: end_fsync failed for file %s\n",
1048 f->file_name);
1049 }
1050 }
1051 } else
1052 cleanup_pending_aio(td);
1053
1054 /*
1055 * stop job if we failed doing any IO
1056 */
1057 if (!ddir_rw_sum(td->this_io_bytes))
1058 td->done = 1;
1059
1060 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1061 bytes_done[i] = td->bytes_done[i] - bytes_done[i];
1062}
1063
1064static void cleanup_io_u(struct thread_data *td)
1065{
1066 struct io_u *io_u;
1067
1068 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
1069
1070 if (td->io_ops->io_u_free)
1071 td->io_ops->io_u_free(td, io_u);
1072
1073 fio_memfree(io_u, sizeof(*io_u));
1074 }
1075
1076 free_io_mem(td);
1077
1078 io_u_rexit(&td->io_u_requeues);
1079 io_u_qexit(&td->io_u_freelist);
1080 io_u_qexit(&td->io_u_all);
1081
1082 if (td->last_write_comp)
1083 sfree(td->last_write_comp);
1084}
1085
1086static int init_io_u(struct thread_data *td)
1087{
1088 struct io_u *io_u;
1089 unsigned int max_bs, min_write;
1090 int cl_align, i, max_units;
1091 int data_xfer = 1, err;
1092 char *p;
1093
1094 max_units = td->o.iodepth;
1095 max_bs = td_max_bs(td);
1096 min_write = td->o.min_bs[DDIR_WRITE];
1097 td->orig_buffer_size = (unsigned long long) max_bs
1098 * (unsigned long long) max_units;
1099
1100 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
1101 data_xfer = 0;
1102
1103 err = 0;
1104 err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
1105 err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
1106 err += io_u_qinit(&td->io_u_all, td->o.iodepth);
1107
1108 if (err) {
1109 log_err("fio: failed setting up IO queues\n");
1110 return 1;
1111 }
1112
1113 /*
1114 * if we may later need to do address alignment, then add any
1115 * possible adjustment here so that we don't cause a buffer
1116 * overflow later. this adjustment may be too much if we get
1117 * lucky and the allocator gives us an aligned address.
1118 */
1119 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1120 (td->io_ops->flags & FIO_RAWIO))
1121 td->orig_buffer_size += page_mask + td->o.mem_align;
1122
1123 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
1124 unsigned long bs;
1125
1126 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
1127 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
1128 }
1129
1130 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
1131 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
1132 return 1;
1133 }
1134
1135 if (data_xfer && allocate_io_mem(td))
1136 return 1;
1137
1138 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1139 (td->io_ops->flags & FIO_RAWIO))
1140 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
1141 else
1142 p = td->orig_buffer;
1143
1144 cl_align = os_cache_line_size();
1145
1146 for (i = 0; i < max_units; i++) {
1147 void *ptr;
1148
1149 if (td->terminate)
1150 return 1;
1151
1152 ptr = fio_memalign(cl_align, sizeof(*io_u));
1153 if (!ptr) {
1154 log_err("fio: unable to allocate aligned memory\n");
1155 break;
1156 }
1157
1158 io_u = ptr;
1159 memset(io_u, 0, sizeof(*io_u));
1160 INIT_FLIST_HEAD(&io_u->verify_list);
1161 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1162
1163 if (data_xfer) {
1164 io_u->buf = p;
1165 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
1166
1167 if (td_write(td))
1168 io_u_fill_buffer(td, io_u, min_write, max_bs);
1169 if (td_write(td) && td->o.verify_pattern_bytes) {
1170 /*
1171 * Fill the buffer with the pattern if we are
1172 * going to be doing writes.
1173 */
1174 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
1175 }
1176 }
1177
1178 io_u->index = i;
1179 io_u->flags = IO_U_F_FREE;
1180 io_u_qpush(&td->io_u_freelist, io_u);
1181
1182 /*
1183 * io_u never leaves this stack, used for iteration of all
1184 * io_u buffers.
1185 */
1186 io_u_qpush(&td->io_u_all, io_u);
1187
1188 if (td->io_ops->io_u_init) {
1189 int ret = td->io_ops->io_u_init(td, io_u);
1190
1191 if (ret) {
1192 log_err("fio: failed to init engine data: %d\n", ret);
1193 return 1;
1194 }
1195 }
1196
1197 p += max_bs;
1198 }
1199
1200 if (td->o.verify != VERIFY_NONE) {
1201 td->last_write_comp = scalloc(max_units, sizeof(uint64_t));
1202 if (!td->last_write_comp) {
1203 log_err("fio: failed to alloc write comp data\n");
1204 return 1;
1205 }
1206 }
1207
1208 return 0;
1209}
1210
1211static int switch_ioscheduler(struct thread_data *td)
1212{
1213 char tmp[256], tmp2[128];
1214 FILE *f;
1215 int ret;
1216
1217 if (td->io_ops->flags & FIO_DISKLESSIO)
1218 return 0;
1219
1220 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1221
1222 f = fopen(tmp, "r+");
1223 if (!f) {
1224 if (errno == ENOENT) {
1225 log_err("fio: os or kernel doesn't support IO scheduler"
1226 " switching\n");
1227 return 0;
1228 }
1229 td_verror(td, errno, "fopen iosched");
1230 return 1;
1231 }
1232
1233 /*
1234 * Set io scheduler.
1235 */
1236 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
1237 if (ferror(f) || ret != 1) {
1238 td_verror(td, errno, "fwrite");
1239 fclose(f);
1240 return 1;
1241 }
1242
1243 rewind(f);
1244
1245 /*
1246 * Read back and check that the selected scheduler is now the default.
1247 */
1248 memset(tmp, 0, sizeof(tmp));
1249 ret = fread(tmp, sizeof(tmp), 1, f);
1250 if (ferror(f) || ret < 0) {
1251 td_verror(td, errno, "fread");
1252 fclose(f);
1253 return 1;
1254 }
1255 /*
1256 * either a list of io schedulers or "none\n" is expected.
1257 */
1258 tmp[strlen(tmp) - 1] = '\0';
1259
1260
1261 sprintf(tmp2, "[%s]", td->o.ioscheduler);
1262 if (!strstr(tmp, tmp2)) {
1263 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1264 td_verror(td, EINVAL, "iosched_switch");
1265 fclose(f);
1266 return 1;
1267 }
1268
1269 fclose(f);
1270 return 0;
1271}
1272
1273static bool keep_running(struct thread_data *td)
1274{
1275 unsigned long long limit;
1276
1277 if (td->done)
1278 return false;
1279 if (td->o.time_based)
1280 return true;
1281 if (td->o.loops) {
1282 td->o.loops--;
1283 return true;
1284 }
1285 if (exceeds_number_ios(td))
1286 return false;
1287
1288 if (td->o.io_limit)
1289 limit = td->o.io_limit;
1290 else
1291 limit = td->o.size;
1292
1293 if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
1294 uint64_t diff;
1295
1296 /*
1297 * If the difference is less than the minimum IO size, we
1298 * are done.
1299 */
1300 diff = limit - ddir_rw_sum(td->io_bytes);
1301 if (diff < td_max_bs(td))
1302 return false;
1303
1304 if (fio_files_done(td) && !td->o.io_limit)
1305 return false;
1306
1307 return true;
1308 }
1309
1310 return false;
1311}
1312
1313static int exec_string(struct thread_options *o, const char *string, const char *mode)
1314{
1315 size_t newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
1316 int ret;
1317 char *str;
1318
1319 str = malloc(newlen);
1320 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
1321
1322 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
1323 ret = system(str);
1324 if (ret == -1)
1325 log_err("fio: exec of cmd <%s> failed\n", str);
1326
1327 free(str);
1328 return ret;
1329}
1330
1331/*
1332 * Dry run to compute correct state of numberio for verification.
1333 */
1334static uint64_t do_dry_run(struct thread_data *td)
1335{
1336 td_set_runstate(td, TD_RUNNING);
1337
1338 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1339 (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) {
1340 struct io_u *io_u;
1341 int ret;
1342
1343 if (td->terminate || td->done)
1344 break;
1345
1346 io_u = get_io_u(td);
1347 if (!io_u)
1348 break;
1349
1350 io_u_set(io_u, IO_U_F_FLIGHT);
1351 io_u->error = 0;
1352 io_u->resid = 0;
1353 if (ddir_rw(acct_ddir(io_u)))
1354 td->io_issues[acct_ddir(io_u)]++;
1355 if (ddir_rw(io_u->ddir)) {
1356 io_u_mark_depth(td, 1);
1357 td->ts.total_io_u[io_u->ddir]++;
1358 }
1359
1360 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1361 td->o.do_verify &&
1362 td->o.verify != VERIFY_NONE &&
1363 !td->o.experimental_verify)
1364 log_io_piece(td, io_u);
1365
1366 ret = io_u_sync_complete(td, io_u);
1367 (void) ret;
1368 }
1369
1370 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
1371}
1372
1373struct fork_data {
1374 struct thread_data *td;
1375 struct sk_out *sk_out;
1376};
1377
1378/*
1379 * Entry point for the thread based jobs. The process based jobs end up
1380 * here as well, after a little setup.
1381 */
1382static void *thread_main(void *data)
1383{
1384 struct fork_data *fd = data;
1385 unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, };
1386 struct thread_data *td = fd->td;
1387 struct thread_options *o = &td->o;
1388 struct sk_out *sk_out = fd->sk_out;
1389 pthread_condattr_t attr;
1390 int clear_state;
1391 int ret;
1392
1393 sk_out_assign(sk_out);
1394 free(fd);
1395
1396 if (!o->use_thread) {
1397 setsid();
1398 td->pid = getpid();
1399 } else
1400 td->pid = gettid();
1401
1402 fio_local_clock_init(o->use_thread);
1403
1404 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1405
1406 if (is_backend)
1407 fio_server_send_start(td);
1408
1409 INIT_FLIST_HEAD(&td->io_log_list);
1410 INIT_FLIST_HEAD(&td->io_hist_list);
1411 INIT_FLIST_HEAD(&td->verify_list);
1412 INIT_FLIST_HEAD(&td->trim_list);
1413 INIT_FLIST_HEAD(&td->next_rand_list);
1414 pthread_mutex_init(&td->io_u_lock, NULL);
1415 td->io_hist_tree = RB_ROOT;
1416
1417 pthread_condattr_init(&attr);
1418 pthread_cond_init(&td->verify_cond, &attr);
1419 pthread_cond_init(&td->free_cond, &attr);
1420
1421 td_set_runstate(td, TD_INITIALIZED);
1422 dprint(FD_MUTEX, "up startup_mutex\n");
1423 fio_mutex_up(startup_mutex);
1424 dprint(FD_MUTEX, "wait on td->mutex\n");
1425 fio_mutex_down(td->mutex);
1426 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1427
1428 /*
1429 * A new gid requires privilege, so we need to do this before setting
1430 * the uid.
1431 */
1432 if (o->gid != -1U && setgid(o->gid)) {
1433 td_verror(td, errno, "setgid");
1434 goto err;
1435 }
1436 if (o->uid != -1U && setuid(o->uid)) {
1437 td_verror(td, errno, "setuid");
1438 goto err;
1439 }
1440
1441 /*
1442 * If we have a gettimeofday() thread, make sure we exclude that
1443 * thread from this job
1444 */
1445 if (o->gtod_cpu)
1446 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1447
1448 /*
1449 * Set affinity first, in case it has an impact on the memory
1450 * allocations.
1451 */
1452 if (fio_option_is_set(o, cpumask)) {
1453 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
1454 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
1455 if (!ret) {
1456 log_err("fio: no CPUs set\n");
1457 log_err("fio: Try increasing number of available CPUs\n");
1458 td_verror(td, EINVAL, "cpus_split");
1459 goto err;
1460 }
1461 }
1462 ret = fio_setaffinity(td->pid, o->cpumask);
1463 if (ret == -1) {
1464 td_verror(td, errno, "cpu_set_affinity");
1465 goto err;
1466 }
1467 }
1468
1469#ifdef CONFIG_LIBNUMA
1470 /* numa node setup */
1471 if (fio_option_is_set(o, numa_cpunodes) ||
1472 fio_option_is_set(o, numa_memnodes)) {
1473 struct bitmask *mask;
1474
1475 if (numa_available() < 0) {
1476 td_verror(td, errno, "Does not support NUMA API\n");
1477 goto err;
1478 }
1479
1480 if (fio_option_is_set(o, numa_cpunodes)) {
1481 mask = numa_parse_nodestring(o->numa_cpunodes);
1482 ret = numa_run_on_node_mask(mask);
1483 numa_free_nodemask(mask);
1484 if (ret == -1) {
1485 td_verror(td, errno, \
1486 "numa_run_on_node_mask failed\n");
1487 goto err;
1488 }
1489 }
1490
1491 if (fio_option_is_set(o, numa_memnodes)) {
1492 mask = NULL;
1493 if (o->numa_memnodes)
1494 mask = numa_parse_nodestring(o->numa_memnodes);
1495
1496 switch (o->numa_mem_mode) {
1497 case MPOL_INTERLEAVE:
1498 numa_set_interleave_mask(mask);
1499 break;
1500 case MPOL_BIND:
1501 numa_set_membind(mask);
1502 break;
1503 case MPOL_LOCAL:
1504 numa_set_localalloc();
1505 break;
1506 case MPOL_PREFERRED:
1507 numa_set_preferred(o->numa_mem_prefer_node);
1508 break;
1509 case MPOL_DEFAULT:
1510 default:
1511 break;
1512 }
1513
1514 if (mask)
1515 numa_free_nodemask(mask);
1516
1517 }
1518 }
1519#endif
1520
1521 if (fio_pin_memory(td))
1522 goto err;
1523
1524 /*
1525 * May alter parameters that init_io_u() will use, so we need to
1526 * do this first.
1527 */
1528 if (init_iolog(td))
1529 goto err;
1530
1531 if (init_io_u(td))
1532 goto err;
1533
1534 if (o->verify_async && verify_async_init(td))
1535 goto err;
1536
1537 if (fio_option_is_set(o, ioprio) ||
1538 fio_option_is_set(o, ioprio_class)) {
1539 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1540 if (ret == -1) {
1541 td_verror(td, errno, "ioprio_set");
1542 goto err;
1543 }
1544 }
1545
1546 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1547 goto err;
1548
1549 errno = 0;
1550 if (nice(o->nice) == -1 && errno != 0) {
1551 td_verror(td, errno, "nice");
1552 goto err;
1553 }
1554
1555 if (o->ioscheduler && switch_ioscheduler(td))
1556 goto err;
1557
1558 if (!o->create_serialize && setup_files(td))
1559 goto err;
1560
1561 if (td_io_init(td))
1562 goto err;
1563
1564 if (init_random_map(td))
1565 goto err;
1566
1567 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
1568 goto err;
1569
1570 if (o->pre_read) {
1571 if (pre_read_files(td) < 0)
1572 goto err;
1573 }
1574
1575 if (iolog_compress_init(td, sk_out))
1576 goto err;
1577
1578 fio_verify_init(td);
1579
1580 if (rate_submit_init(td, sk_out))
1581 goto err;
1582
1583 fio_gettime(&td->epoch, NULL);
1584 fio_getrusage(&td->ru_start);
1585 memcpy(&td->bw_sample_time, &td->epoch, sizeof(td->epoch));
1586 memcpy(&td->iops_sample_time, &td->epoch, sizeof(td->epoch));
1587
1588 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1589 o->ratemin[DDIR_TRIM]) {
1590 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1591 sizeof(td->bw_sample_time));
1592 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1593 sizeof(td->bw_sample_time));
1594 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1595 sizeof(td->bw_sample_time));
1596 }
1597
1598 clear_state = 0;
1599 while (keep_running(td)) {
1600 uint64_t verify_bytes;
1601
1602 fio_gettime(&td->start, NULL);
1603 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1604
1605 if (clear_state)
1606 clear_io_state(td, 0);
1607
1608 prune_io_piece_log(td);
1609
1610 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1611 verify_bytes = do_dry_run(td);
1612 else {
1613 uint64_t bytes_done[DDIR_RWDIR_CNT];
1614
1615 do_io(td, bytes_done);
1616
1617 if (!ddir_rw_sum(bytes_done)) {
1618 fio_mark_td_terminate(td);
1619 verify_bytes = 0;
1620 } else {
1621 verify_bytes = bytes_done[DDIR_WRITE] +
1622 bytes_done[DDIR_TRIM];
1623 }
1624 }
1625
1626 clear_state = 1;
1627
1628 /*
1629 * Make sure we've successfully updated the rusage stats
1630 * before waiting on the stat mutex. Otherwise we could have
1631 * the stat thread holding stat mutex and waiting for
1632 * the rusage_sem, which would never get upped because
1633 * this thread is waiting for the stat mutex.
1634 */
1635 check_update_rusage(td);
1636
1637 fio_mutex_down(stat_mutex);
1638 if (td_read(td) && td->io_bytes[DDIR_READ])
1639 update_runtime(td, elapsed_us, DDIR_READ);
1640 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1641 update_runtime(td, elapsed_us, DDIR_WRITE);
1642 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1643 update_runtime(td, elapsed_us, DDIR_TRIM);
1644 fio_gettime(&td->start, NULL);
1645 fio_mutex_up(stat_mutex);
1646
1647 if (td->error || td->terminate)
1648 break;
1649
1650 if (!o->do_verify ||
1651 o->verify == VERIFY_NONE ||
1652 (td->io_ops->flags & FIO_UNIDIR))
1653 continue;
1654
1655 clear_io_state(td, 0);
1656
1657 fio_gettime(&td->start, NULL);
1658
1659 do_verify(td, verify_bytes);
1660
1661 /*
1662 * See comment further up for why this is done here.
1663 */
1664 check_update_rusage(td);
1665
1666 fio_mutex_down(stat_mutex);
1667 update_runtime(td, elapsed_us, DDIR_READ);
1668 fio_gettime(&td->start, NULL);
1669 fio_mutex_up(stat_mutex);
1670
1671 if (td->error || td->terminate)
1672 break;
1673 }
1674
1675 update_rusage_stat(td);
1676 td->ts.total_run_time = mtime_since_now(&td->epoch);
1677 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1678 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1679 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1680
1681 if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) &&
1682 (td->o.verify != VERIFY_NONE && td_write(td)))
1683 verify_save_state(td->thread_number);
1684
1685 fio_unpin_memory(td);
1686
1687 fio_writeout_logs(td);
1688
1689 iolog_compress_exit(td);
1690 rate_submit_exit(td);
1691
1692 if (o->exec_postrun)
1693 exec_string(o, o->exec_postrun, (const char *)"postrun");
1694
1695 if (exitall_on_terminate || (o->exitall_error && td->error))
1696 fio_terminate_threads(td->groupid);
1697
1698err:
1699 if (td->error)
1700 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1701 td->verror);
1702
1703 if (o->verify_async)
1704 verify_async_exit(td);
1705
1706 close_and_free_files(td);
1707 cleanup_io_u(td);
1708 close_ioengine(td);
1709 cgroup_shutdown(td, &cgroup_mnt);
1710 verify_free_state(td);
1711
1712 if (fio_option_is_set(o, cpumask)) {
1713 ret = fio_cpuset_exit(&o->cpumask);
1714 if (ret)
1715 td_verror(td, ret, "fio_cpuset_exit");
1716 }
1717
1718 /*
1719 * do this very late, it will log file closing as well
1720 */
1721 if (o->write_iolog_file)
1722 write_iolog_close(td);
1723
1724 fio_mutex_remove(td->mutex);
1725 td->mutex = NULL;
1726
1727 td_set_runstate(td, TD_EXITED);
1728
1729 /*
1730 * Do this last after setting our runstate to exited, so we
1731 * know that the stat thread is signaled.
1732 */
1733 check_update_rusage(td);
1734
1735 sk_out_drop();
1736 return (void *) (uintptr_t) td->error;
1737}
1738
1739
1740/*
1741 * We cannot pass the td data into a forked process, so attach the td and
1742 * pass it to the thread worker.
1743 */
1744static int fork_main(struct sk_out *sk_out, int shmid, int offset)
1745{
1746 struct fork_data *fd;
1747 void *data, *ret;
1748
1749#if !defined(__hpux) && !defined(CONFIG_NO_SHM)
1750 data = shmat(shmid, NULL, 0);
1751 if (data == (void *) -1) {
1752 int __err = errno;
1753
1754 perror("shmat");
1755 return __err;
1756 }
1757#else
1758 /*
1759 * HP-UX inherits shm mappings?
1760 */
1761 data = threads;
1762#endif
1763
1764 fd = calloc(1, sizeof(*fd));
1765 fd->td = data + offset * sizeof(struct thread_data);
1766 fd->sk_out = sk_out;
1767 ret = thread_main(fd);
1768 shmdt(data);
1769 return (int) (uintptr_t) ret;
1770}
1771
1772static void dump_td_info(struct thread_data *td)
1773{
1774 log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to "
1775 "be stuck. Doing forceful exit of this job.\n", td->o.name,
1776 (unsigned long) time_since_now(&td->terminate_time));
1777}
1778
1779/*
1780 * Run over the job map and reap the threads that have exited, if any.
1781 */
1782static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1783 unsigned int *m_rate)
1784{
1785 struct thread_data *td;
1786 unsigned int cputhreads, realthreads, pending;
1787 int i, status, ret;
1788
1789 /*
1790 * reap exited threads (TD_EXITED -> TD_REAPED)
1791 */
1792 realthreads = pending = cputhreads = 0;
1793 for_each_td(td, i) {
1794 int flags = 0;
1795
1796 /*
1797 * ->io_ops is NULL for a thread that has closed its
1798 * io engine
1799 */
1800 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1801 cputhreads++;
1802 else
1803 realthreads++;
1804
1805 if (!td->pid) {
1806 pending++;
1807 continue;
1808 }
1809 if (td->runstate == TD_REAPED)
1810 continue;
1811 if (td->o.use_thread) {
1812 if (td->runstate == TD_EXITED) {
1813 td_set_runstate(td, TD_REAPED);
1814 goto reaped;
1815 }
1816 continue;
1817 }
1818
1819 flags = WNOHANG;
1820 if (td->runstate == TD_EXITED)
1821 flags = 0;
1822
1823 /*
1824 * check if someone quit or got killed in an unusual way
1825 */
1826 ret = waitpid(td->pid, &status, flags);
1827 if (ret < 0) {
1828 if (errno == ECHILD) {
1829 log_err("fio: pid=%d disappeared %d\n",
1830 (int) td->pid, td->runstate);
1831 td->sig = ECHILD;
1832 td_set_runstate(td, TD_REAPED);
1833 goto reaped;
1834 }
1835 perror("waitpid");
1836 } else if (ret == td->pid) {
1837 if (WIFSIGNALED(status)) {
1838 int sig = WTERMSIG(status);
1839
1840 if (sig != SIGTERM && sig != SIGUSR2)
1841 log_err("fio: pid=%d, got signal=%d\n",
1842 (int) td->pid, sig);
1843 td->sig = sig;
1844 td_set_runstate(td, TD_REAPED);
1845 goto reaped;
1846 }
1847 if (WIFEXITED(status)) {
1848 if (WEXITSTATUS(status) && !td->error)
1849 td->error = WEXITSTATUS(status);
1850
1851 td_set_runstate(td, TD_REAPED);
1852 goto reaped;
1853 }
1854 }
1855
1856 /*
1857 * If the job is stuck, do a forceful timeout of it and
1858 * move on.
1859 */
1860 if (td->terminate &&
1861 time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1862 dump_td_info(td);
1863 td_set_runstate(td, TD_REAPED);
1864 goto reaped;
1865 }
1866
1867 /*
1868 * thread is not dead, continue
1869 */
1870 pending++;
1871 continue;
1872reaped:
1873 (*nr_running)--;
1874 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1875 (*t_rate) -= ddir_rw_sum(td->o.rate);
1876 if (!td->pid)
1877 pending--;
1878
1879 if (td->error)
1880 exit_value++;
1881
1882 done_secs += mtime_since_now(&td->epoch) / 1000;
1883 profile_td_exit(td);
1884 }
1885
1886 if (*nr_running == cputhreads && !pending && realthreads)
1887 fio_terminate_threads(TERMINATE_ALL);
1888}
1889
1890static bool __check_trigger_file(void)
1891{
1892 struct stat sb;
1893
1894 if (!trigger_file)
1895 return false;
1896
1897 if (stat(trigger_file, &sb))
1898 return false;
1899
1900 if (unlink(trigger_file) < 0)
1901 log_err("fio: failed to unlink %s: %s\n", trigger_file,
1902 strerror(errno));
1903
1904 return true;
1905}
1906
1907static bool trigger_timedout(void)
1908{
1909 if (trigger_timeout)
1910 return time_since_genesis() >= trigger_timeout;
1911
1912 return false;
1913}
1914
1915void exec_trigger(const char *cmd)
1916{
1917 int ret;
1918
1919 if (!cmd)
1920 return;
1921
1922 ret = system(cmd);
1923 if (ret == -1)
1924 log_err("fio: failed executing %s trigger\n", cmd);
1925}
1926
1927void check_trigger_file(void)
1928{
1929 if (__check_trigger_file() || trigger_timedout()) {
1930 if (nr_clients)
1931 fio_clients_send_trigger(trigger_remote_cmd);
1932 else {
1933 verify_save_state(IO_LIST_ALL);
1934 fio_terminate_threads(TERMINATE_ALL);
1935 exec_trigger(trigger_cmd);
1936 }
1937 }
1938}
1939
1940static int fio_verify_load_state(struct thread_data *td)
1941{
1942 int ret;
1943
1944 if (!td->o.verify_state)
1945 return 0;
1946
1947 if (is_backend) {
1948 void *data;
1949 int ver;
1950
1951 ret = fio_server_get_verify_state(td->o.name,
1952 td->thread_number - 1, &data, &ver);
1953 if (!ret)
1954 verify_convert_assign_state(td, data, ver);
1955 } else
1956 ret = verify_load_state(td, "local");
1957
1958 return ret;
1959}
1960
1961static void do_usleep(unsigned int usecs)
1962{
1963 check_for_running_stats();
1964 check_trigger_file();
1965 usleep(usecs);
1966}
1967
1968static bool check_mount_writes(struct thread_data *td)
1969{
1970 struct fio_file *f;
1971 unsigned int i;
1972
1973 if (!td_write(td) || td->o.allow_mounted_write)
1974 return false;
1975
1976 for_each_file(td, f, i) {
1977 if (f->filetype != FIO_TYPE_BD)
1978 continue;
1979 if (device_is_mounted(f->file_name))
1980 goto mounted;
1981 }
1982
1983 return false;
1984mounted:
1985 log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.", f->file_name);
1986 return true;
1987}
1988
1989static bool waitee_running(struct thread_data *me)
1990{
1991 const char *waitee = me->o.wait_for;
1992 const char *self = me->o.name;
1993 struct thread_data *td;
1994 int i;
1995
1996 if (!waitee)
1997 return false;
1998
1999 for_each_td(td, i) {
2000 if (!strcmp(td->o.name, self) || strcmp(td->o.name, waitee))
2001 continue;
2002
2003 if (td->runstate < TD_EXITED) {
2004 dprint(FD_PROCESS, "%s fenced by %s(%s)\n",
2005 self, td->o.name,
2006 runstate_to_name(td->runstate));
2007 return true;
2008 }
2009 }
2010
2011 dprint(FD_PROCESS, "%s: %s completed, can run\n", self, waitee);
2012 return false;
2013}
2014
2015/*
2016 * Main function for kicking off and reaping jobs, as needed.
2017 */
2018static void run_threads(struct sk_out *sk_out)
2019{
2020 struct thread_data *td;
2021 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
2022 uint64_t spent;
2023
2024 if (fio_gtod_offload && fio_start_gtod_thread())
2025 return;
2026
2027 fio_idle_prof_init();
2028
2029 set_sig_handlers();
2030
2031 nr_thread = nr_process = 0;
2032 for_each_td(td, i) {
2033 if (check_mount_writes(td))
2034 return;
2035 if (td->o.use_thread)
2036 nr_thread++;
2037 else
2038 nr_process++;
2039 }
2040
2041 if (output_format & FIO_OUTPUT_NORMAL) {
2042 log_info("Starting ");
2043 if (nr_thread)
2044 log_info("%d thread%s", nr_thread,
2045 nr_thread > 1 ? "s" : "");
2046 if (nr_process) {
2047 if (nr_thread)
2048 log_info(" and ");
2049 log_info("%d process%s", nr_process,
2050 nr_process > 1 ? "es" : "");
2051 }
2052 log_info("\n");
2053 log_info_flush();
2054 }
2055
2056 todo = thread_number;
2057 nr_running = 0;
2058 nr_started = 0;
2059 m_rate = t_rate = 0;
2060
2061 for_each_td(td, i) {
2062 print_status_init(td->thread_number - 1);
2063
2064 if (!td->o.create_serialize)
2065 continue;
2066
2067 if (fio_verify_load_state(td))
2068 goto reap;
2069
2070 /*
2071 * do file setup here so it happens sequentially,
2072 * we don't want X number of threads getting their
2073 * client data interspersed on disk
2074 */
2075 if (setup_files(td)) {
2076reap:
2077 exit_value++;
2078 if (td->error)
2079 log_err("fio: pid=%d, err=%d/%s\n",
2080 (int) td->pid, td->error, td->verror);
2081 td_set_runstate(td, TD_REAPED);
2082 todo--;
2083 } else {
2084 struct fio_file *f;
2085 unsigned int j;
2086
2087 /*
2088 * for sharing to work, each job must always open
2089 * its own files. so close them, if we opened them
2090 * for creation
2091 */
2092 for_each_file(td, f, j) {
2093 if (fio_file_open(f))
2094 td_io_close_file(td, f);
2095 }
2096 }
2097 }
2098
2099 /* start idle threads before io threads start to run */
2100 fio_idle_prof_start();
2101
2102 set_genesis_time();
2103
2104 while (todo) {
2105 struct thread_data *map[REAL_MAX_JOBS];
2106 struct timeval this_start;
2107 int this_jobs = 0, left;
2108
2109 /*
2110 * create threads (TD_NOT_CREATED -> TD_CREATED)
2111 */
2112 for_each_td(td, i) {
2113 if (td->runstate != TD_NOT_CREATED)
2114 continue;
2115
2116 /*
2117 * never got a chance to start, killed by other
2118 * thread for some reason
2119 */
2120 if (td->terminate) {
2121 todo--;
2122 continue;
2123 }
2124
2125 if (td->o.start_delay) {
2126 spent = utime_since_genesis();
2127
2128 if (td->o.start_delay > spent)
2129 continue;
2130 }
2131
2132 if (td->o.stonewall && (nr_started || nr_running)) {
2133 dprint(FD_PROCESS, "%s: stonewall wait\n",
2134 td->o.name);
2135 break;
2136 }
2137
2138 if (waitee_running(td)) {
2139 dprint(FD_PROCESS, "%s: waiting for %s\n",
2140 td->o.name, td->o.wait_for);
2141 continue;
2142 }
2143
2144 init_disk_util(td);
2145
2146 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
2147 td->update_rusage = 0;
2148
2149 /*
2150 * Set state to created. Thread will transition
2151 * to TD_INITIALIZED when it's done setting up.
2152 */
2153 td_set_runstate(td, TD_CREATED);
2154 map[this_jobs++] = td;
2155 nr_started++;
2156
2157 if (td->o.use_thread) {
2158 struct fork_data *fd;
2159 int ret;
2160
2161 fd = calloc(1, sizeof(*fd));
2162 fd->td = td;
2163 fd->sk_out = sk_out;
2164
2165 dprint(FD_PROCESS, "will pthread_create\n");
2166 ret = pthread_create(&td->thread, NULL,
2167 thread_main, fd);
2168 if (ret) {
2169 log_err("pthread_create: %s\n",
2170 strerror(ret));
2171 free(fd);
2172 nr_started--;
2173 break;
2174 }
2175 ret = pthread_detach(td->thread);
2176 if (ret)
2177 log_err("pthread_detach: %s",
2178 strerror(ret));
2179 } else {
2180 pid_t pid;
2181 dprint(FD_PROCESS, "will fork\n");
2182 pid = fork();
2183 if (!pid) {
2184 int ret = fork_main(sk_out, shm_id, i);
2185
2186 _exit(ret);
2187 } else if (i == fio_debug_jobno)
2188 *fio_debug_jobp = pid;
2189 }
2190 dprint(FD_MUTEX, "wait on startup_mutex\n");
2191 if (fio_mutex_down_timeout(startup_mutex, 10000)) {
2192 log_err("fio: job startup hung? exiting.\n");
2193 fio_terminate_threads(TERMINATE_ALL);
2194 fio_abort = 1;
2195 nr_started--;
2196 break;
2197 }
2198 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2199 }
2200
2201 /*
2202 * Wait for the started threads to transition to
2203 * TD_INITIALIZED.
2204 */
2205 fio_gettime(&this_start, NULL);
2206 left = this_jobs;
2207 while (left && !fio_abort) {
2208 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2209 break;
2210
2211 do_usleep(100000);
2212
2213 for (i = 0; i < this_jobs; i++) {
2214 td = map[i];
2215 if (!td)
2216 continue;
2217 if (td->runstate == TD_INITIALIZED) {
2218 map[i] = NULL;
2219 left--;
2220 } else if (td->runstate >= TD_EXITED) {
2221 map[i] = NULL;
2222 left--;
2223 todo--;
2224 nr_running++; /* work-around... */
2225 }
2226 }
2227 }
2228
2229 if (left) {
2230 log_err("fio: %d job%s failed to start\n", left,
2231 left > 1 ? "s" : "");
2232 for (i = 0; i < this_jobs; i++) {
2233 td = map[i];
2234 if (!td)
2235 continue;
2236 kill(td->pid, SIGTERM);
2237 }
2238 break;
2239 }
2240
2241 /*
2242 * start created threads (TD_INITIALIZED -> TD_RUNNING).
2243 */
2244 for_each_td(td, i) {
2245 if (td->runstate != TD_INITIALIZED)
2246 continue;
2247
2248 if (in_ramp_time(td))
2249 td_set_runstate(td, TD_RAMP);
2250 else
2251 td_set_runstate(td, TD_RUNNING);
2252 nr_running++;
2253 nr_started--;
2254 m_rate += ddir_rw_sum(td->o.ratemin);
2255 t_rate += ddir_rw_sum(td->o.rate);
2256 todo--;
2257 fio_mutex_up(td->mutex);
2258 }
2259
2260 reap_threads(&nr_running, &t_rate, &m_rate);
2261
2262 if (todo)
2263 do_usleep(100000);
2264 }
2265
2266 while (nr_running) {
2267 reap_threads(&nr_running, &t_rate, &m_rate);
2268 do_usleep(10000);
2269 }
2270
2271 fio_idle_prof_stop();
2272
2273 update_io_ticks();
2274}
2275
2276static void wait_for_helper_thread_exit(void)
2277{
2278 void *ret;
2279
2280 helper_exit = 1;
2281 pthread_cond_signal(&helper_cond);
2282 pthread_join(helper_thread, &ret);
2283}
2284
2285static void free_disk_util(void)
2286{
2287 disk_util_prune_entries();
2288
2289 pthread_cond_destroy(&helper_cond);
2290}
2291
2292static void *helper_thread_main(void *data)
2293{
2294 struct sk_out *sk_out = data;
2295 int ret = 0;
2296
2297 sk_out_assign(sk_out);
2298
2299 fio_mutex_up(startup_mutex);
2300
2301 while (!ret) {
2302 uint64_t sec = DISK_UTIL_MSEC / 1000;
2303 uint64_t nsec = (DISK_UTIL_MSEC % 1000) * 1000000;
2304 struct timespec ts;
2305 struct timeval tv;
2306
2307 gettimeofday(&tv, NULL);
2308 ts.tv_sec = tv.tv_sec + sec;
2309 ts.tv_nsec = (tv.tv_usec * 1000) + nsec;
2310
2311 if (ts.tv_nsec >= 1000000000ULL) {
2312 ts.tv_nsec -= 1000000000ULL;
2313 ts.tv_sec++;
2314 }
2315
2316 pthread_cond_timedwait(&helper_cond, &helper_lock, &ts);
2317
2318 ret = update_io_ticks();
2319
2320 if (helper_do_stat) {
2321 helper_do_stat = 0;
2322 __show_running_run_stats();
2323 }
2324
2325 if (!is_backend)
2326 print_thread_status();
2327 }
2328
2329 sk_out_drop();
2330 return NULL;
2331}
2332
2333static int create_helper_thread(struct sk_out *sk_out)
2334{
2335 int ret;
2336
2337 setup_disk_util();
2338
2339 pthread_cond_init(&helper_cond, NULL);
2340 pthread_mutex_init(&helper_lock, NULL);
2341
2342 ret = pthread_create(&helper_thread, NULL, helper_thread_main, sk_out);
2343 if (ret) {
2344 log_err("Can't create helper thread: %s\n", strerror(ret));
2345 return 1;
2346 }
2347
2348 dprint(FD_MUTEX, "wait on startup_mutex\n");
2349 fio_mutex_down(startup_mutex);
2350 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2351 return 0;
2352}
2353
2354int fio_backend(struct sk_out *sk_out)
2355{
2356 struct thread_data *td;
2357 int i;
2358
2359 if (exec_profile) {
2360 if (load_profile(exec_profile))
2361 return 1;
2362 free(exec_profile);
2363 exec_profile = NULL;
2364 }
2365 if (!thread_number)
2366 return 0;
2367
2368 if (write_bw_log) {
2369 struct log_params p = {
2370 .log_type = IO_LOG_TYPE_BW,
2371 };
2372
2373 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2374 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2375 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
2376 }
2377
2378 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2379 if (startup_mutex == NULL)
2380 return 1;
2381
2382 set_genesis_time();
2383 stat_init();
2384 create_helper_thread(sk_out);
2385
2386 cgroup_list = smalloc(sizeof(*cgroup_list));
2387 INIT_FLIST_HEAD(cgroup_list);
2388
2389 run_threads(sk_out);
2390
2391 wait_for_helper_thread_exit();
2392
2393 if (!fio_abort) {
2394 __show_run_stats();
2395 if (write_bw_log) {
2396 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2397 struct io_log *log = agg_io_log[i];
2398
2399 flush_log(log, 0);
2400 free_log(log);
2401 }
2402 }
2403 }
2404
2405 for_each_td(td, i) {
2406 fio_options_free(td);
2407 if (td->rusage_sem) {
2408 fio_mutex_remove(td->rusage_sem);
2409 td->rusage_sem = NULL;
2410 }
2411 }
2412
2413 free_disk_util();
2414 cgroup_kill(cgroup_list);
2415 sfree(cgroup_list);
2416 sfree(cgroup_mnt);
2417
2418 fio_mutex_remove(startup_mutex);
2419 stat_exit();
2420 return exit_value;
2421}