embed server address, not other end's address
[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         ret = fread(tmp, sizeof(tmp), 1, f);
1176         if (ferror(f) || ret < 0) {
1177                 td_verror(td, errno, "fread");
1178                 fclose(f);
1179                 return 1;
1180         }
1181         tmp[sizeof(tmp) - 1] = '\0';
1182
1183
1184         sprintf(tmp2, "[%s]", td->o.ioscheduler);
1185         if (!strstr(tmp, tmp2)) {
1186                 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1187                 td_verror(td, EINVAL, "iosched_switch");
1188                 fclose(f);
1189                 return 1;
1190         }
1191
1192         fclose(f);
1193         return 0;
1194 }
1195
1196 static int keep_running(struct thread_data *td)
1197 {
1198         unsigned long long limit;
1199
1200         if (td->done)
1201                 return 0;
1202         if (td->o.time_based)
1203                 return 1;
1204         if (td->o.loops) {
1205                 td->o.loops--;
1206                 return 1;
1207         }
1208         if (exceeds_number_ios(td))
1209                 return 0;
1210
1211         if (td->o.io_limit)
1212                 limit = td->o.io_limit;
1213         else
1214                 limit = td->o.size;
1215
1216         if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
1217                 uint64_t diff;
1218
1219                 /*
1220                  * If the difference is less than the minimum IO size, we
1221                  * are done.
1222                  */
1223                 diff = limit - ddir_rw_sum(td->io_bytes);
1224                 if (diff < td_max_bs(td))
1225                         return 0;
1226
1227                 if (fio_files_done(td))
1228                         return 0;
1229
1230                 return 1;
1231         }
1232
1233         return 0;
1234 }
1235
1236 static int exec_string(struct thread_options *o, const char *string, const char *mode)
1237 {
1238         int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
1239         char *str;
1240
1241         str = malloc(newlen);
1242         sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
1243
1244         log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
1245         ret = system(str);
1246         if (ret == -1)
1247                 log_err("fio: exec of cmd <%s> failed\n", str);
1248
1249         free(str);
1250         return ret;
1251 }
1252
1253 /*
1254  * Dry run to compute correct state of numberio for verification.
1255  */
1256 static uint64_t do_dry_run(struct thread_data *td)
1257 {
1258         td_set_runstate(td, TD_RUNNING);
1259
1260         while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1261                 (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) {
1262                 struct io_u *io_u;
1263                 int ret;
1264
1265                 if (td->terminate || td->done)
1266                         break;
1267
1268                 io_u = get_io_u(td);
1269                 if (!io_u)
1270                         break;
1271
1272                 io_u_set(io_u, IO_U_F_FLIGHT);
1273                 io_u->error = 0;
1274                 io_u->resid = 0;
1275                 if (ddir_rw(acct_ddir(io_u)))
1276                         td->io_issues[acct_ddir(io_u)]++;
1277                 if (ddir_rw(io_u->ddir)) {
1278                         io_u_mark_depth(td, 1);
1279                         td->ts.total_io_u[io_u->ddir]++;
1280                 }
1281
1282                 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1283                     td->o.do_verify &&
1284                     td->o.verify != VERIFY_NONE &&
1285                     !td->o.experimental_verify)
1286                         log_io_piece(td, io_u);
1287
1288                 ret = io_u_sync_complete(td, io_u);
1289                 (void) ret;
1290         }
1291
1292         return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
1293 }
1294
1295 static void io_workqueue_fn(struct thread_data *td, struct io_u *io_u)
1296 {
1297         const enum fio_ddir ddir = io_u->ddir;
1298         int ret;
1299
1300         dprint(FD_RATE, "io_u %p queued by %u\n", io_u, gettid());
1301
1302         io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1303
1304         td->cur_depth++;
1305
1306         ret = td_io_queue(td, io_u);
1307
1308         dprint(FD_RATE, "io_u %p ret %d by %u\n", io_u, ret, gettid());
1309
1310         io_queue_event(td, io_u, &ret, ddir, NULL, 0, NULL);
1311
1312         if (ret == FIO_Q_QUEUED)
1313                 ret = io_u_queued_complete(td, 1);
1314
1315         td->cur_depth--;
1316 }
1317
1318 /*
1319  * Entry point for the thread based jobs. The process based jobs end up
1320  * here as well, after a little setup.
1321  */
1322 static void *thread_main(void *data)
1323 {
1324         unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, };
1325         struct thread_data *td = data;
1326         struct thread_options *o = &td->o;
1327         pthread_condattr_t attr;
1328         int clear_state;
1329         int ret;
1330
1331         if (!o->use_thread) {
1332                 setsid();
1333                 td->pid = getpid();
1334         } else
1335                 td->pid = gettid();
1336
1337         fio_local_clock_init(o->use_thread);
1338
1339         dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1340
1341         if (is_backend)
1342                 fio_server_send_start(td);
1343
1344         INIT_FLIST_HEAD(&td->io_log_list);
1345         INIT_FLIST_HEAD(&td->io_hist_list);
1346         INIT_FLIST_HEAD(&td->verify_list);
1347         INIT_FLIST_HEAD(&td->trim_list);
1348         INIT_FLIST_HEAD(&td->next_rand_list);
1349         pthread_mutex_init(&td->io_u_lock, NULL);
1350         td->io_hist_tree = RB_ROOT;
1351
1352         pthread_condattr_init(&attr);
1353         pthread_cond_init(&td->verify_cond, &attr);
1354         pthread_cond_init(&td->free_cond, &attr);
1355
1356         td_set_runstate(td, TD_INITIALIZED);
1357         dprint(FD_MUTEX, "up startup_mutex\n");
1358         fio_mutex_up(startup_mutex);
1359         dprint(FD_MUTEX, "wait on td->mutex\n");
1360         fio_mutex_down(td->mutex);
1361         dprint(FD_MUTEX, "done waiting on td->mutex\n");
1362
1363         /*
1364          * A new gid requires privilege, so we need to do this before setting
1365          * the uid.
1366          */
1367         if (o->gid != -1U && setgid(o->gid)) {
1368                 td_verror(td, errno, "setgid");
1369                 goto err;
1370         }
1371         if (o->uid != -1U && setuid(o->uid)) {
1372                 td_verror(td, errno, "setuid");
1373                 goto err;
1374         }
1375
1376         /*
1377          * If we have a gettimeofday() thread, make sure we exclude that
1378          * thread from this job
1379          */
1380         if (o->gtod_cpu)
1381                 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
1382
1383         /*
1384          * Set affinity first, in case it has an impact on the memory
1385          * allocations.
1386          */
1387         if (fio_option_is_set(o, cpumask)) {
1388                 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
1389                         ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
1390                         if (!ret) {
1391                                 log_err("fio: no CPUs set\n");
1392                                 log_err("fio: Try increasing number of available CPUs\n");
1393                                 td_verror(td, EINVAL, "cpus_split");
1394                                 goto err;
1395                         }
1396                 }
1397                 ret = fio_setaffinity(td->pid, o->cpumask);
1398                 if (ret == -1) {
1399                         td_verror(td, errno, "cpu_set_affinity");
1400                         goto err;
1401                 }
1402         }
1403
1404 #ifdef CONFIG_LIBNUMA
1405         /* numa node setup */
1406         if (fio_option_is_set(o, numa_cpunodes) ||
1407             fio_option_is_set(o, numa_memnodes)) {
1408                 struct bitmask *mask;
1409
1410                 if (numa_available() < 0) {
1411                         td_verror(td, errno, "Does not support NUMA API\n");
1412                         goto err;
1413                 }
1414
1415                 if (fio_option_is_set(o, numa_cpunodes)) {
1416                         mask = numa_parse_nodestring(o->numa_cpunodes);
1417                         ret = numa_run_on_node_mask(mask);
1418                         numa_free_nodemask(mask);
1419                         if (ret == -1) {
1420                                 td_verror(td, errno, \
1421                                         "numa_run_on_node_mask failed\n");
1422                                 goto err;
1423                         }
1424                 }
1425
1426                 if (fio_option_is_set(o, numa_memnodes)) {
1427                         mask = NULL;
1428                         if (o->numa_memnodes)
1429                                 mask = numa_parse_nodestring(o->numa_memnodes);
1430
1431                         switch (o->numa_mem_mode) {
1432                         case MPOL_INTERLEAVE:
1433                                 numa_set_interleave_mask(mask);
1434                                 break;
1435                         case MPOL_BIND:
1436                                 numa_set_membind(mask);
1437                                 break;
1438                         case MPOL_LOCAL:
1439                                 numa_set_localalloc();
1440                                 break;
1441                         case MPOL_PREFERRED:
1442                                 numa_set_preferred(o->numa_mem_prefer_node);
1443                                 break;
1444                         case MPOL_DEFAULT:
1445                         default:
1446                                 break;
1447                         }
1448
1449                         if (mask)
1450                                 numa_free_nodemask(mask);
1451
1452                 }
1453         }
1454 #endif
1455
1456         if (fio_pin_memory(td))
1457                 goto err;
1458
1459         /*
1460          * May alter parameters that init_io_u() will use, so we need to
1461          * do this first.
1462          */
1463         if (init_iolog(td))
1464                 goto err;
1465
1466         if (init_io_u(td))
1467                 goto err;
1468
1469         if (o->verify_async && verify_async_init(td))
1470                 goto err;
1471
1472         if (fio_option_is_set(o, ioprio) ||
1473             fio_option_is_set(o, ioprio_class)) {
1474                 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1475                 if (ret == -1) {
1476                         td_verror(td, errno, "ioprio_set");
1477                         goto err;
1478                 }
1479         }
1480
1481         if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
1482                 goto err;
1483
1484         errno = 0;
1485         if (nice(o->nice) == -1 && errno != 0) {
1486                 td_verror(td, errno, "nice");
1487                 goto err;
1488         }
1489
1490         if (o->ioscheduler && switch_ioscheduler(td))
1491                 goto err;
1492
1493         if (!o->create_serialize && setup_files(td))
1494                 goto err;
1495
1496         if (td_io_init(td))
1497                 goto err;
1498
1499         if (init_random_map(td))
1500                 goto err;
1501
1502         if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
1503                 goto err;
1504
1505         if (o->pre_read) {
1506                 if (pre_read_files(td) < 0)
1507                         goto err;
1508         }
1509
1510         if (td->flags & TD_F_COMPRESS_LOG)
1511                 tp_init(&td->tp_data);
1512
1513         fio_verify_init(td);
1514
1515         if ((o->io_submit_mode == IO_MODE_OFFLOAD) &&
1516             workqueue_init(td, &td->io_wq, io_workqueue_fn, td->o.iodepth))
1517                 goto err;
1518
1519         fio_gettime(&td->epoch, NULL);
1520         fio_getrusage(&td->ru_start);
1521         clear_state = 0;
1522         while (keep_running(td)) {
1523                 uint64_t verify_bytes;
1524
1525                 fio_gettime(&td->start, NULL);
1526                 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1527                 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1528                 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1529
1530                 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1531                                 o->ratemin[DDIR_TRIM]) {
1532                         memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
1533                                                 sizeof(td->bw_sample_time));
1534                         memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1535                                                 sizeof(td->bw_sample_time));
1536                         memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
1537                                                 sizeof(td->bw_sample_time));
1538                 }
1539
1540                 if (clear_state)
1541                         clear_io_state(td);
1542
1543                 prune_io_piece_log(td);
1544
1545                 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1546                         verify_bytes = do_dry_run(td);
1547                 else
1548                         verify_bytes = do_io(td);
1549
1550                 clear_state = 1;
1551
1552                 /*
1553                  * Make sure we've successfully updated the rusage stats
1554                  * before waiting on the stat mutex. Otherwise we could have
1555                  * the stat thread holding stat mutex and waiting for
1556                  * the rusage_sem, which would never get upped because
1557                  * this thread is waiting for the stat mutex.
1558                  */
1559                 check_update_rusage(td);
1560
1561                 fio_mutex_down(stat_mutex);
1562                 if (td_read(td) && td->io_bytes[DDIR_READ])
1563                         update_runtime(td, elapsed_us, DDIR_READ);
1564                 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1565                         update_runtime(td, elapsed_us, DDIR_WRITE);
1566                 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1567                         update_runtime(td, elapsed_us, DDIR_TRIM);
1568                 fio_gettime(&td->start, NULL);
1569                 fio_mutex_up(stat_mutex);
1570
1571                 if (td->error || td->terminate)
1572                         break;
1573
1574                 if (!o->do_verify ||
1575                     o->verify == VERIFY_NONE ||
1576                     (td->io_ops->flags & FIO_UNIDIR))
1577                         continue;
1578
1579                 clear_io_state(td);
1580
1581                 fio_gettime(&td->start, NULL);
1582
1583                 do_verify(td, verify_bytes);
1584
1585                 /*
1586                  * See comment further up for why this is done here.
1587                  */
1588                 check_update_rusage(td);
1589
1590                 fio_mutex_down(stat_mutex);
1591                 update_runtime(td, elapsed_us, DDIR_READ);
1592                 fio_gettime(&td->start, NULL);
1593                 fio_mutex_up(stat_mutex);
1594
1595                 if (td->error || td->terminate)
1596                         break;
1597         }
1598
1599         update_rusage_stat(td);
1600         td->ts.total_run_time = mtime_since_now(&td->epoch);
1601         td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1602         td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1603         td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1604
1605         if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) &&
1606             (td->o.verify != VERIFY_NONE && td_write(td))) {
1607                 struct all_io_list *state;
1608                 size_t sz;
1609
1610                 state = get_all_io_list(td->thread_number, &sz);
1611                 if (state) {
1612                         __verify_save_state(state, "local");
1613                         free(state);
1614                 }
1615         }
1616
1617         fio_unpin_memory(td);
1618
1619         fio_writeout_logs(td);
1620
1621         if (o->io_submit_mode == IO_MODE_OFFLOAD)
1622                 workqueue_exit(&td->io_wq);
1623
1624         if (td->flags & TD_F_COMPRESS_LOG)
1625                 tp_exit(&td->tp_data);
1626
1627         if (o->exec_postrun)
1628                 exec_string(o, o->exec_postrun, (const char *)"postrun");
1629
1630         if (exitall_on_terminate)
1631                 fio_terminate_threads(td->groupid);
1632
1633 err:
1634         if (td->error)
1635                 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1636                                                         td->verror);
1637
1638         if (o->verify_async)
1639                 verify_async_exit(td);
1640
1641         close_and_free_files(td);
1642         cleanup_io_u(td);
1643         close_ioengine(td);
1644         cgroup_shutdown(td, &cgroup_mnt);
1645         verify_free_state(td);
1646
1647         if (fio_option_is_set(o, cpumask)) {
1648                 ret = fio_cpuset_exit(&o->cpumask);
1649                 if (ret)
1650                         td_verror(td, ret, "fio_cpuset_exit");
1651         }
1652
1653         /*
1654          * do this very late, it will log file closing as well
1655          */
1656         if (o->write_iolog_file)
1657                 write_iolog_close(td);
1658
1659         fio_mutex_remove(td->mutex);
1660         td->mutex = NULL;
1661
1662         td_set_runstate(td, TD_EXITED);
1663
1664         /*
1665          * Do this last after setting our runstate to exited, so we
1666          * know that the stat thread is signaled.
1667          */
1668         check_update_rusage(td);
1669
1670         return (void *) (uintptr_t) td->error;
1671 }
1672
1673
1674 /*
1675  * We cannot pass the td data into a forked process, so attach the td and
1676  * pass it to the thread worker.
1677  */
1678 static int fork_main(int shmid, int offset)
1679 {
1680         struct thread_data *td;
1681         void *data, *ret;
1682
1683 #if !defined(__hpux) && !defined(CONFIG_NO_SHM)
1684         data = shmat(shmid, NULL, 0);
1685         if (data == (void *) -1) {
1686                 int __err = errno;
1687
1688                 perror("shmat");
1689                 return __err;
1690         }
1691 #else
1692         /*
1693          * HP-UX inherits shm mappings?
1694          */
1695         data = threads;
1696 #endif
1697
1698         td = data + offset * sizeof(struct thread_data);
1699         ret = thread_main(td);
1700         shmdt(data);
1701         return (int) (uintptr_t) ret;
1702 }
1703
1704 static void dump_td_info(struct thread_data *td)
1705 {
1706         log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to "
1707                 "be stuck. Doing forceful exit of this job.\n", td->o.name,
1708                         (unsigned long) time_since_now(&td->terminate_time));
1709 }
1710
1711 /*
1712  * Run over the job map and reap the threads that have exited, if any.
1713  */
1714 static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1715                          unsigned int *m_rate)
1716 {
1717         struct thread_data *td;
1718         unsigned int cputhreads, realthreads, pending;
1719         int i, status, ret;
1720
1721         /*
1722          * reap exited threads (TD_EXITED -> TD_REAPED)
1723          */
1724         realthreads = pending = cputhreads = 0;
1725         for_each_td(td, i) {
1726                 int flags = 0;
1727
1728                 /*
1729                  * ->io_ops is NULL for a thread that has closed its
1730                  * io engine
1731                  */
1732                 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1733                         cputhreads++;
1734                 else
1735                         realthreads++;
1736
1737                 if (!td->pid) {
1738                         pending++;
1739                         continue;
1740                 }
1741                 if (td->runstate == TD_REAPED)
1742                         continue;
1743                 if (td->o.use_thread) {
1744                         if (td->runstate == TD_EXITED) {
1745                                 td_set_runstate(td, TD_REAPED);
1746                                 goto reaped;
1747                         }
1748                         continue;
1749                 }
1750
1751                 flags = WNOHANG;
1752                 if (td->runstate == TD_EXITED)
1753                         flags = 0;
1754
1755                 /*
1756                  * check if someone quit or got killed in an unusual way
1757                  */
1758                 ret = waitpid(td->pid, &status, flags);
1759                 if (ret < 0) {
1760                         if (errno == ECHILD) {
1761                                 log_err("fio: pid=%d disappeared %d\n",
1762                                                 (int) td->pid, td->runstate);
1763                                 td->sig = ECHILD;
1764                                 td_set_runstate(td, TD_REAPED);
1765                                 goto reaped;
1766                         }
1767                         perror("waitpid");
1768                 } else if (ret == td->pid) {
1769                         if (WIFSIGNALED(status)) {
1770                                 int sig = WTERMSIG(status);
1771
1772                                 if (sig != SIGTERM && sig != SIGUSR2)
1773                                         log_err("fio: pid=%d, got signal=%d\n",
1774                                                         (int) td->pid, sig);
1775                                 td->sig = sig;
1776                                 td_set_runstate(td, TD_REAPED);
1777                                 goto reaped;
1778                         }
1779                         if (WIFEXITED(status)) {
1780                                 if (WEXITSTATUS(status) && !td->error)
1781                                         td->error = WEXITSTATUS(status);
1782
1783                                 td_set_runstate(td, TD_REAPED);
1784                                 goto reaped;
1785                         }
1786                 }
1787
1788                 /*
1789                  * If the job is stuck, do a forceful timeout of it and
1790                  * move on.
1791                  */
1792                 if (td->terminate &&
1793                     time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1794                         dump_td_info(td);
1795                         td_set_runstate(td, TD_REAPED);
1796                         goto reaped;
1797                 }
1798
1799                 /*
1800                  * thread is not dead, continue
1801                  */
1802                 pending++;
1803                 continue;
1804 reaped:
1805                 (*nr_running)--;
1806                 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1807                 (*t_rate) -= ddir_rw_sum(td->o.rate);
1808                 if (!td->pid)
1809                         pending--;
1810
1811                 if (td->error)
1812                         exit_value++;
1813
1814                 done_secs += mtime_since_now(&td->epoch) / 1000;
1815                 profile_td_exit(td);
1816         }
1817
1818         if (*nr_running == cputhreads && !pending && realthreads)
1819                 fio_terminate_threads(TERMINATE_ALL);
1820 }
1821
1822 static int __check_trigger_file(void)
1823 {
1824         struct stat sb;
1825
1826         if (!trigger_file)
1827                 return 0;
1828
1829         if (stat(trigger_file, &sb))
1830                 return 0;
1831
1832         if (unlink(trigger_file) < 0)
1833                 log_err("fio: failed to unlink %s: %s\n", trigger_file,
1834                                                         strerror(errno));
1835
1836         return 1;
1837 }
1838
1839 static int trigger_timedout(void)
1840 {
1841         if (trigger_timeout)
1842                 return time_since_genesis() >= trigger_timeout;
1843
1844         return 0;
1845 }
1846
1847 void exec_trigger(const char *cmd)
1848 {
1849         int ret;
1850
1851         if (!cmd)
1852                 return;
1853
1854         ret = system(cmd);
1855         if (ret == -1)
1856                 log_err("fio: failed executing %s trigger\n", cmd);
1857 }
1858
1859 void check_trigger_file(void)
1860 {
1861         if (__check_trigger_file() || trigger_timedout()) {
1862                 if (nr_clients)
1863                         fio_clients_send_trigger(trigger_remote_cmd);
1864                 else {
1865                         verify_save_state();
1866                         fio_terminate_threads(TERMINATE_ALL);
1867                         exec_trigger(trigger_cmd);
1868                 }
1869         }
1870 }
1871
1872 static int fio_verify_load_state(struct thread_data *td)
1873 {
1874         int ret;
1875
1876         if (!td->o.verify_state)
1877                 return 0;
1878
1879         if (is_backend) {
1880                 void *data;
1881
1882                 ret = fio_server_get_verify_state(td->o.name,
1883                                         td->thread_number - 1, &data);
1884                 if (!ret)
1885                         verify_convert_assign_state(td, data);
1886         } else
1887                 ret = verify_load_state(td, "local");
1888
1889         return ret;
1890 }
1891
1892 static void do_usleep(unsigned int usecs)
1893 {
1894         check_for_running_stats();
1895         check_trigger_file();
1896         usleep(usecs);
1897 }
1898
1899 static int check_mount_writes(struct thread_data *td)
1900 {
1901         struct fio_file *f;
1902         unsigned int i;
1903
1904         if (!td_write(td) || td->o.allow_mounted_write)
1905                 return 0;
1906
1907         for_each_file(td, f, i) {
1908                 if (f->filetype != FIO_TYPE_BD)
1909                         continue;
1910                 if (device_is_mounted(f->file_name))
1911                         goto mounted;
1912         }
1913
1914         return 0;
1915 mounted:
1916         log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.", f->file_name);
1917         return 1;
1918 }
1919
1920 /*
1921  * Main function for kicking off and reaping jobs, as needed.
1922  */
1923 static void run_threads(void)
1924 {
1925         struct thread_data *td;
1926         unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1927         uint64_t spent;
1928
1929         if (fio_gtod_offload && fio_start_gtod_thread())
1930                 return;
1931
1932         fio_idle_prof_init();
1933
1934         set_sig_handlers();
1935
1936         nr_thread = nr_process = 0;
1937         for_each_td(td, i) {
1938                 if (check_mount_writes(td))
1939                         return;
1940                 if (td->o.use_thread)
1941                         nr_thread++;
1942                 else
1943                         nr_process++;
1944         }
1945
1946         if (output_format == FIO_OUTPUT_NORMAL) {
1947                 log_info("Starting ");
1948                 if (nr_thread)
1949                         log_info("%d thread%s", nr_thread,
1950                                                 nr_thread > 1 ? "s" : "");
1951                 if (nr_process) {
1952                         if (nr_thread)
1953                                 log_info(" and ");
1954                         log_info("%d process%s", nr_process,
1955                                                 nr_process > 1 ? "es" : "");
1956                 }
1957                 log_info("\n");
1958                 log_info_flush();
1959         }
1960
1961         todo = thread_number;
1962         nr_running = 0;
1963         nr_started = 0;
1964         m_rate = t_rate = 0;
1965
1966         for_each_td(td, i) {
1967                 print_status_init(td->thread_number - 1);
1968
1969                 if (!td->o.create_serialize)
1970                         continue;
1971
1972                 if (fio_verify_load_state(td))
1973                         goto reap;
1974
1975                 /*
1976                  * do file setup here so it happens sequentially,
1977                  * we don't want X number of threads getting their
1978                  * client data interspersed on disk
1979                  */
1980                 if (setup_files(td)) {
1981 reap:
1982                         exit_value++;
1983                         if (td->error)
1984                                 log_err("fio: pid=%d, err=%d/%s\n",
1985                                         (int) td->pid, td->error, td->verror);
1986                         td_set_runstate(td, TD_REAPED);
1987                         todo--;
1988                 } else {
1989                         struct fio_file *f;
1990                         unsigned int j;
1991
1992                         /*
1993                          * for sharing to work, each job must always open
1994                          * its own files. so close them, if we opened them
1995                          * for creation
1996                          */
1997                         for_each_file(td, f, j) {
1998                                 if (fio_file_open(f))
1999                                         td_io_close_file(td, f);
2000                         }
2001                 }
2002         }
2003
2004         /* start idle threads before io threads start to run */
2005         fio_idle_prof_start();
2006
2007         set_genesis_time();
2008
2009         while (todo) {
2010                 struct thread_data *map[REAL_MAX_JOBS];
2011                 struct timeval this_start;
2012                 int this_jobs = 0, left;
2013
2014                 /*
2015                  * create threads (TD_NOT_CREATED -> TD_CREATED)
2016                  */
2017                 for_each_td(td, i) {
2018                         if (td->runstate != TD_NOT_CREATED)
2019                                 continue;
2020
2021                         /*
2022                          * never got a chance to start, killed by other
2023                          * thread for some reason
2024                          */
2025                         if (td->terminate) {
2026                                 todo--;
2027                                 continue;
2028                         }
2029
2030                         if (td->o.start_delay) {
2031                                 spent = utime_since_genesis();
2032
2033                                 if (td->o.start_delay > spent)
2034                                         continue;
2035                         }
2036
2037                         if (td->o.stonewall && (nr_started || nr_running)) {
2038                                 dprint(FD_PROCESS, "%s: stonewall wait\n",
2039                                                         td->o.name);
2040                                 break;
2041                         }
2042
2043                         init_disk_util(td);
2044
2045                         td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
2046                         td->update_rusage = 0;
2047
2048                         /*
2049                          * Set state to created. Thread will transition
2050                          * to TD_INITIALIZED when it's done setting up.
2051                          */
2052                         td_set_runstate(td, TD_CREATED);
2053                         map[this_jobs++] = td;
2054                         nr_started++;
2055
2056                         if (td->o.use_thread) {
2057                                 int ret;
2058
2059                                 dprint(FD_PROCESS, "will pthread_create\n");
2060                                 ret = pthread_create(&td->thread, NULL,
2061                                                         thread_main, td);
2062                                 if (ret) {
2063                                         log_err("pthread_create: %s\n",
2064                                                         strerror(ret));
2065                                         nr_started--;
2066                                         break;
2067                                 }
2068                                 ret = pthread_detach(td->thread);
2069                                 if (ret)
2070                                         log_err("pthread_detach: %s",
2071                                                         strerror(ret));
2072                         } else {
2073                                 pid_t pid;
2074                                 dprint(FD_PROCESS, "will fork\n");
2075                                 pid = fork();
2076                                 if (!pid) {
2077                                         int ret = fork_main(shm_id, i);
2078
2079                                         _exit(ret);
2080                                 } else if (i == fio_debug_jobno)
2081                                         *fio_debug_jobp = pid;
2082                         }
2083                         dprint(FD_MUTEX, "wait on startup_mutex\n");
2084                         if (fio_mutex_down_timeout(startup_mutex, 10)) {
2085                                 log_err("fio: job startup hung? exiting.\n");
2086                                 fio_terminate_threads(TERMINATE_ALL);
2087                                 fio_abort = 1;
2088                                 nr_started--;
2089                                 break;
2090                         }
2091                         dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2092                 }
2093
2094                 /*
2095                  * Wait for the started threads to transition to
2096                  * TD_INITIALIZED.
2097                  */
2098                 fio_gettime(&this_start, NULL);
2099                 left = this_jobs;
2100                 while (left && !fio_abort) {
2101                         if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2102                                 break;
2103
2104                         do_usleep(100000);
2105
2106                         for (i = 0; i < this_jobs; i++) {
2107                                 td = map[i];
2108                                 if (!td)
2109                                         continue;
2110                                 if (td->runstate == TD_INITIALIZED) {
2111                                         map[i] = NULL;
2112                                         left--;
2113                                 } else if (td->runstate >= TD_EXITED) {
2114                                         map[i] = NULL;
2115                                         left--;
2116                                         todo--;
2117                                         nr_running++; /* work-around... */
2118                                 }
2119                         }
2120                 }
2121
2122                 if (left) {
2123                         log_err("fio: %d job%s failed to start\n", left,
2124                                         left > 1 ? "s" : "");
2125                         for (i = 0; i < this_jobs; i++) {
2126                                 td = map[i];
2127                                 if (!td)
2128                                         continue;
2129                                 kill(td->pid, SIGTERM);
2130                         }
2131                         break;
2132                 }
2133
2134                 /*
2135                  * start created threads (TD_INITIALIZED -> TD_RUNNING).
2136                  */
2137                 for_each_td(td, i) {
2138                         if (td->runstate != TD_INITIALIZED)
2139                                 continue;
2140
2141                         if (in_ramp_time(td))
2142                                 td_set_runstate(td, TD_RAMP);
2143                         else
2144                                 td_set_runstate(td, TD_RUNNING);
2145                         nr_running++;
2146                         nr_started--;
2147                         m_rate += ddir_rw_sum(td->o.ratemin);
2148                         t_rate += ddir_rw_sum(td->o.rate);
2149                         todo--;
2150                         fio_mutex_up(td->mutex);
2151                 }
2152
2153                 reap_threads(&nr_running, &t_rate, &m_rate);
2154
2155                 if (todo)
2156                         do_usleep(100000);
2157         }
2158
2159         while (nr_running) {
2160                 reap_threads(&nr_running, &t_rate, &m_rate);
2161                 do_usleep(10000);
2162         }
2163
2164         fio_idle_prof_stop();
2165
2166         update_io_ticks();
2167 }
2168
2169 static void wait_for_helper_thread_exit(void)
2170 {
2171         void *ret;
2172
2173         helper_exit = 1;
2174         pthread_cond_signal(&helper_cond);
2175         pthread_join(helper_thread, &ret);
2176 }
2177
2178 static void free_disk_util(void)
2179 {
2180         disk_util_prune_entries();
2181
2182         pthread_cond_destroy(&helper_cond);
2183 }
2184
2185 static void *helper_thread_main(void *data)
2186 {
2187         int ret = 0;
2188
2189         fio_mutex_up(startup_mutex);
2190
2191         while (!ret) {
2192                 uint64_t sec = DISK_UTIL_MSEC / 1000;
2193                 uint64_t nsec = (DISK_UTIL_MSEC % 1000) * 1000000;
2194                 struct timespec ts;
2195                 struct timeval tv;
2196
2197                 gettimeofday(&tv, NULL);
2198                 ts.tv_sec = tv.tv_sec + sec;
2199                 ts.tv_nsec = (tv.tv_usec * 1000) + nsec;
2200
2201                 if (ts.tv_nsec >= 1000000000ULL) {
2202                         ts.tv_nsec -= 1000000000ULL;
2203                         ts.tv_sec++;
2204                 }
2205
2206                 pthread_cond_timedwait(&helper_cond, &helper_lock, &ts);
2207
2208                 ret = update_io_ticks();
2209
2210                 if (helper_do_stat) {
2211                         helper_do_stat = 0;
2212                         __show_running_run_stats();
2213                 }
2214
2215                 if (!is_backend)
2216                         print_thread_status();
2217         }
2218
2219         return NULL;
2220 }
2221
2222 static int create_helper_thread(void)
2223 {
2224         int ret;
2225
2226         setup_disk_util();
2227
2228         pthread_cond_init(&helper_cond, NULL);
2229         pthread_mutex_init(&helper_lock, NULL);
2230
2231         ret = pthread_create(&helper_thread, NULL, helper_thread_main, NULL);
2232         if (ret) {
2233                 log_err("Can't create helper thread: %s\n", strerror(ret));
2234                 return 1;
2235         }
2236
2237         dprint(FD_MUTEX, "wait on startup_mutex\n");
2238         fio_mutex_down(startup_mutex);
2239         dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2240         return 0;
2241 }
2242
2243 int fio_backend(void)
2244 {
2245         struct thread_data *td;
2246         int i;
2247
2248         if (exec_profile) {
2249                 if (load_profile(exec_profile))
2250                         return 1;
2251                 free(exec_profile);
2252                 exec_profile = NULL;
2253         }
2254         if (!thread_number)
2255                 return 0;
2256
2257         if (write_bw_log) {
2258                 struct log_params p = {
2259                         .log_type = IO_LOG_TYPE_BW,
2260                 };
2261
2262                 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2263                 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2264                 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
2265         }
2266
2267         startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2268         if (startup_mutex == NULL)
2269                 return 1;
2270
2271         set_genesis_time();
2272         stat_init();
2273         create_helper_thread();
2274
2275         cgroup_list = smalloc(sizeof(*cgroup_list));
2276         INIT_FLIST_HEAD(cgroup_list);
2277
2278         run_threads();
2279
2280         wait_for_helper_thread_exit();
2281
2282         if (!fio_abort) {
2283                 __show_run_stats();
2284                 if (write_bw_log) {
2285                         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2286                                 struct io_log *log = agg_io_log[i];
2287
2288                                 flush_log(log, 0);
2289                                 free_log(log);
2290                         }
2291                 }
2292         }
2293
2294         for_each_td(td, i) {
2295                 fio_options_free(td);
2296                 if (td->rusage_sem) {
2297                         fio_mutex_remove(td->rusage_sem);
2298                         td->rusage_sem = NULL;
2299                 }
2300         }
2301
2302         free_disk_util();
2303         cgroup_kill(cgroup_list);
2304         sfree(cgroup_list);
2305         sfree(cgroup_mnt);
2306
2307         fio_mutex_remove(startup_mutex);
2308         stat_exit();
2309         return exit_value;
2310 }