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