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