Ensure that set_name_idx() doesn't exceed target bounds
[fio.git] / backend.c
CommitLineData
2e1df07d
JA
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>
e43606c2 33#include <inttypes.h>
2e1df07d
JA
34#include <sys/stat.h>
35#include <sys/wait.h>
36#include <sys/ipc.h>
2e1df07d
JA
37#include <sys/mman.h>
38
39#include "fio.h"
a5e0ee11
O
40#ifndef FIO_NO_HAVE_SHM_H
41#include <sys/shm.h>
42#endif
2e1df07d
JA
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"
44404c5a 53#include "lib/getrusage.h"
f2a2ce0e 54#include "idletime.h"
002fe734 55#include "err.h"
78d55e72 56#include "lib/tp.h"
a9da8ab2 57#include "workqueue.h"
2e1df07d 58
5ddc6707
JA
59static pthread_t helper_thread;
60static pthread_mutex_t helper_lock;
61pthread_cond_t helper_cond;
62int helper_do_stat = 0;
8aab824f 63
2e1df07d 64static struct fio_mutex *startup_mutex;
2e1df07d
JA
65static struct flist_head *cgroup_list;
66static char *cgroup_mnt;
67static int exit_value;
68static volatile int fio_abort;
3a5f6bde
JA
69static unsigned int nr_process = 0;
70static unsigned int nr_thread = 0;
2e1df07d 71
6eaf09d6 72struct io_log *agg_io_log[DDIR_RWDIR_CNT];
2e1df07d 73
a3efc919
JA
74int groupid = 0;
75unsigned int thread_number = 0;
108fea77 76unsigned int stat_number = 0;
a3efc919
JA
77int shm_id = 0;
78int temp_stall_ts;
79unsigned long done_secs = 0;
5ddc6707 80volatile int helper_exit = 0;
a3efc919 81
2e1df07d 82#define PAGE_ALIGN(buf) \
e43606c2 83 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
2e1df07d
JA
84
85#define JOB_START_TIMEOUT (5 * 1000)
86
87static 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);
e411c301 94 log_info_flush();
2e1df07d
JA
95 exit_value = 128;
96 }
97
98 fio_terminate_threads(TERMINATE_ALL);
99 }
100}
101
4c6d91e8
JA
102static void sig_show_status(int sig)
103{
104 show_running_run_stats();
105}
106
2e1df07d
JA
107static 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
2f694507
BC
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
4c6d91e8
JA
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
2e1df07d
JA
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 */
145static 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 {
49cba9b3
JA
190 if (spent)
191 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
192 else
193 rate = 0;
194
2e1df07d
JA
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 {
4c707a3b
JA
212 if (spent)
213 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
214 else
215 rate = 0;
216
2e1df07d
JA
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
55312f9f 233static int check_min_rate(struct thread_data *td, struct timeval *now)
2e1df07d
JA
234{
235 int ret = 0;
236
55312f9f 237 if (td->bytes_done[DDIR_READ])
6eaf09d6 238 ret |= __check_min_rate(td, now, DDIR_READ);
55312f9f 239 if (td->bytes_done[DDIR_WRITE])
6eaf09d6 240 ret |= __check_min_rate(td, now, DDIR_WRITE);
55312f9f 241 if (td->bytes_done[DDIR_TRIM])
6eaf09d6 242 ret |= __check_min_rate(td, now, DDIR_TRIM);
2e1df07d
JA
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 */
251static void cleanup_pending_aio(struct thread_data *td)
252{
2e1df07d
JA
253 int r;
254
255 /*
256 * get immediately available events, if any
257 */
55312f9f 258 r = io_u_queued_complete(td, 0);
2e1df07d
JA
259 if (r < 0)
260 return;
261
262 /*
263 * now cancel remaining active events
264 */
265 if (td->io_ops->cancel) {
2ae0b204
JA
266 struct io_u *io_u;
267 int i;
2e1df07d 268
2ae0b204
JA
269 io_u_qiter(&td->io_u_all, io_u, i) {
270 if (io_u->flags & IO_U_F_FLIGHT) {
2e1df07d
JA
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)
55312f9f 279 r = io_u_queued_complete(td, td->cur_depth);
2e1df07d
JA
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 */
286static 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
302requeue:
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) {
55312f9f 309 if (io_u_queued_complete(td, 1) < 0)
2e1df07d
JA
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
55312f9f 317 if (io_u_sync_complete(td, io_u) < 0)
2e1df07d
JA
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}
a3efc919 327
61ee0f86
JA
328static 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
2e1df07d
JA
343static inline void __update_tv_cache(struct thread_data *td)
344{
345 fio_gettime(&td->tv_cache, NULL);
346}
347
348static 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
354static 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;
0de5b26f 360 if (utime_since(&td->epoch, t) >= td->o.timeout)
2e1df07d
JA
361 return 1;
362
363 return 0;
364}
365
95603b74
BF
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 */
371static 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
2e1df07d
JA
380static 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) {
8b28bd41
DM
386 int err = td->error;
387 enum error_type_bit eb;
2e1df07d
JA
388
389 if (ret < 0)
390 err = -ret;
2e1df07d 391
8b28bd41
DM
392 eb = td_error_type(ddir, err);
393 if (!(td->o.continue_on_error & (1 << eb)))
2e1df07d
JA
394 return 1;
395
8b28bd41 396 if (td_non_fatal_error(td, eb, err)) {
2e1df07d
JA
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);
ebea2133 411 fio_mark_td_terminate(td);
2e1df07d
JA
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
c97f1ad6
JA
426static 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
55312f9f 435static int wait_for_completions(struct thread_data *td, struct timeval *time)
69fea81e
JA
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);
77e7b330 445 if ((full && !min_evts) || !td->o.iodepth_batch_complete)
69fea81e
JA
446 min_evts = 1;
447
66b6c5ef 448 if (time && (__should_check_rate(td, DDIR_READ) ||
69fea81e
JA
449 __should_check_rate(td, DDIR_WRITE) ||
450 __should_check_rate(td, DDIR_TRIM)))
451 fio_gettime(time, NULL);
452
453 do {
55312f9f 454 ret = io_u_queued_complete(td, min_evts);
69fea81e
JA
455 if (ret < 0)
456 break;
457 } while (full && (td->cur_depth > td->o.iodepth_low));
458
459 return ret;
460}
461
e9d512d8
JA
462int 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 {
507sync_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
2e1df07d
JA
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 */
100f49f1 553static void do_verify(struct thread_data *td, uint64_t verify_bytes)
2e1df07d
JA
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
c97f1ad6
JA
575 check_update_rusage(td);
576
2e1df07d
JA
577 if (td->error)
578 return;
579
580 td_set_runstate(td, TD_VERIFYING);
581
582 io_u = NULL;
583 while (!td->terminate) {
fbccf46c 584 enum fio_ddir ddir;
e9d512d8 585 int full;
2e1df07d
JA
586
587 update_tv_cache(td);
c97f1ad6 588 check_update_rusage(td);
2e1df07d
JA
589
590 if (runtime_exceeded(td, &td->tv_cache)) {
591 __update_tv_cache(td);
592 if (runtime_exceeded(td, &td->tv_cache)) {
ebea2133 593 fio_mark_td_terminate(td);
2e1df07d
JA
594 break;
595 }
596 }
597
9e684a49
DE
598 if (flow_threshold_exceeded(td))
599 continue;
600
44cbc6da
JA
601 if (!td->o.experimental_verify) {
602 io_u = __get_io_u(td);
603 if (!io_u)
604 break;
2e1df07d 605
44cbc6da
JA
606 if (get_next_verify(td, io_u)) {
607 put_io_u(td, io_u);
608 break;
609 }
2e1df07d 610
44cbc6da
JA
611 if (td_io_prep(td, io_u)) {
612 put_io_u(td, io_u);
613 break;
614 }
615 } else {
55312f9f 616 if (ddir_rw_sum(td->bytes_done) + td->o.rw_min_bs > verify_bytes)
100f49f1
JA
617 break;
618
bcd5abfa 619 while ((io_u = get_io_u(td)) != NULL) {
002fe734
JA
620 if (IS_ERR(io_u)) {
621 io_u = NULL;
622 ret = FIO_Q_BUSY;
623 goto reap;
624 }
625
bcd5abfa
JA
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;
a9da8ab2 641 io_u_set(io_u, IO_U_F_TRIMMED);
bcd5abfa
JA
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 }
44cbc6da 651
bcd5abfa 652 if (!io_u)
44cbc6da 653 break;
2e1df07d
JA
654 }
655
ca09be4b
JA
656 if (verify_state_should_stop(td, io_u)) {
657 put_io_u(td, io_u);
658 break;
659 }
660
2e1df07d
JA
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
fbccf46c 666 ddir = io_u->ddir;
10adc4a7
GG
667 if (!td->o.disable_slat)
668 fio_gettime(&io_u->start_time, NULL);
fbccf46c 669
2e1df07d 670 ret = td_io_queue(td, io_u);
2e1df07d 671
e9d512d8 672 if (io_queue_event(td, io_u, &ret, ddir, NULL, 1, NULL))
2e1df07d
JA
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 */
002fe734 680reap:
2e1df07d 681 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
69fea81e 682 if (full || !td->o.iodepth_batch_complete)
55312f9f 683 ret = wait_for_completions(td, NULL);
2e1df07d 684
2e1df07d
JA
685 if (ret < 0)
686 break;
687 }
688
c97f1ad6
JA
689 check_update_rusage(td);
690
2e1df07d
JA
691 if (!td->error) {
692 min_events = td->cur_depth;
693
694 if (min_events)
55312f9f 695 ret = io_u_queued_complete(td, min_events);
2e1df07d
JA
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
3939fe85
JA
704static 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
cf8a46a7 711 number_ios = ddir_rw_sum(td->io_blocks);
3939fe85
JA
712 number_ios += td->io_u_queued + td->io_u_in_flight;
713
cf8a46a7 714 return number_ios >= (td->o.number_ios * td->loops);
3939fe85
JA
715}
716
e28dd2cf 717static int io_issue_bytes_exceeded(struct thread_data *td)
f7078f7b 718{
77731b29 719 unsigned long long bytes, limit;
f7078f7b
JA
720
721 if (td_rw(td))
74d6277f 722 bytes = td->io_issue_bytes[DDIR_READ] + td->io_issue_bytes[DDIR_WRITE];
f7078f7b 723 else if (td_write(td))
74d6277f 724 bytes = td->io_issue_bytes[DDIR_WRITE];
6eaf09d6 725 else if (td_read(td))
74d6277f 726 bytes = td->io_issue_bytes[DDIR_READ];
f7078f7b 727 else
74d6277f 728 bytes = td->io_issue_bytes[DDIR_TRIM];
f7078f7b 729
77731b29
JA
730 if (td->o.io_limit)
731 limit = td->o.io_limit;
732 else
733 limit = td->o.size;
734
cf8a46a7 735 limit *= td->loops;
77731b29 736 return bytes >= limit || exceeds_number_ios(td);
f7078f7b
JA
737}
738
e28dd2cf
JE
739static 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
cf8a46a7 757 limit *= td->loops;
e28dd2cf
JE
758 return bytes >= limit || exceeds_number_ios(td);
759}
760
2e1df07d
JA
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.
100f49f1
JA
764 *
765 * Returns number of bytes written and trimmed.
2e1df07d 766 */
100f49f1 767static uint64_t do_io(struct thread_data *td)
2e1df07d
JA
768{
769 unsigned int i;
770 int ret = 0;
c2703bf3 771 uint64_t total_bytes, bytes_issued = 0;
2e1df07d
JA
772
773 if (in_ramp_time(td))
774 td_set_runstate(td, TD_RAMP);
775 else
776 td_set_runstate(td, TD_RUNNING);
777
3e260a46
JA
778 lat_target_init(td);
779
1e564979
JE
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);
78a6469c
JA
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 */
78a6469c
JA
792 if (td->o.verify != VERIFY_NONE &&
793 (td_write(td) && td->o.verify_backlog))
c2703bf3
JA
794 total_bytes += td->o.size;
795
82a90686 796 /* In trimwrite mode, each byte is trimmed and then written, so
0e4dd95c 797 * allow total_bytes to be twice as big */
82a90686 798 if (td_trimwrite(td))
0e4dd95c
DE
799 total_bytes += td->total_io_size;
800
f7078f7b 801 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
e28dd2cf 802 (!flist_empty(&td->trim_list)) || !io_issue_bytes_exceeded(td) ||
c04e4661 803 td->o.time_based) {
2e1df07d 804 struct timeval comp_time;
2e1df07d 805 struct io_u *io_u;
e9d512d8 806 int full;
2e1df07d
JA
807 enum fio_ddir ddir;
808
c97f1ad6
JA
809 check_update_rusage(td);
810
7d7803fa 811 if (td->terminate || td->done)
2e1df07d
JA
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)) {
ebea2133 819 fio_mark_td_terminate(td);
2e1df07d
JA
820 break;
821 }
822 }
823
9e684a49
DE
824 if (flow_threshold_exceeded(td))
825 continue;
826
c2703bf3 827 if (bytes_issued >= total_bytes)
20876c53
JC
828 break;
829
2e1df07d 830 io_u = get_io_u(td);
002fe734
JA
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 }
3e260a46
JA
839 if (td->o.latency_target)
840 goto reap;
2e1df07d 841 break;
3e260a46 842 }
2e1df07d
JA
843
844 ddir = io_u->ddir;
845
846 /*
82af2a7c
JA
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)
2e1df07d
JA
850 */
851 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
82af2a7c 852 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
c4b6117b
PV
853
854 if (!td->o.verify_pattern_bytes) {
d6b72507 855 io_u->rand_seed = __rand(&td->verify_state);
c4b6117b 856 if (sizeof(int) != sizeof(long *))
d6b72507 857 io_u->rand_seed *= __rand(&td->verify_state);
c4b6117b
PV
858 }
859
ca09be4b
JA
860 if (verify_state_should_stop(td, io_u)) {
861 put_io_u(td, io_u);
862 break;
863 }
864
2e1df07d
JA
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
9a50c5c5 875 /*
f9401285
JA
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.
9a50c5c5 879 */
c4b6117b
PV
880 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
881 td->o.do_verify &&
882 td->o.verify != VERIFY_NONE &&
f9401285 883 !td->o.experimental_verify)
c4b6117b
PV
884 log_io_piece(td, io_u);
885
a9da8ab2
JA
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);
2e1df07d 892
a9da8ab2
JA
893 if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 1, &comp_time))
894 break;
2e1df07d 895
a9da8ab2
JA
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 */
3e260a46 901reap:
a9da8ab2
JA
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 }
2e1df07d
JA
907 if (ret < 0)
908 break;
55312f9f
JA
909 if (!ddir_rw_sum(td->bytes_done) &&
910 !(td->io_ops->flags & FIO_NOIO))
2e1df07d
JA
911 continue;
912
55312f9f
JA
913 if (!in_ramp_time(td) && should_check_rate(td)) {
914 if (check_min_rate(td, &comp_time)) {
2e1df07d
JA
915 if (exitall_on_terminate)
916 fio_terminate_threads(td->groupid);
917 td_verror(td, EIO, "check_min_rate");
918 break;
919 }
920 }
3e260a46
JA
921 if (!in_ramp_time(td) && td->o.latency_target)
922 lat_target_check(td);
2e1df07d
JA
923
924 if (td->o.thinktime) {
925 unsigned long long b;
926
342f4be4 927 b = ddir_rw_sum(td->io_blocks);
2e1df07d
JA
928 if (!(b % td->o.thinktime_blocks)) {
929 int left;
930
002e7183
JA
931 io_u_quiesce(td);
932
2e1df07d
JA
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
c97f1ad6
JA
943 check_update_rusage(td);
944
2e1df07d 945 if (td->trim_entries)
4e0a8fa2 946 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
2e1df07d
JA
947
948 if (td->o.fill_device && td->error == ENOSPC) {
949 td->error = 0;
ebea2133 950 fio_mark_td_terminate(td);
2e1df07d
JA
951 }
952 if (!td->error) {
953 struct fio_file *f;
954
a9da8ab2
JA
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
2e1df07d 961 if (i) {
55312f9f 962 ret = io_u_queued_complete(td, i);
2e1df07d
JA
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) {
61ee0f86 971 if (!fio_file_fsync(td, f))
2e1df07d 972 continue;
61ee0f86
JA
973
974 log_err("fio: end_fsync failed for file %s\n",
975 f->file_name);
2e1df07d
JA
976 }
977 }
978 } else
979 cleanup_pending_aio(td);
980
981 /*
982 * stop job if we failed doing any IO
983 */
342f4be4 984 if (!ddir_rw_sum(td->this_io_bytes))
2e1df07d 985 td->done = 1;
100f49f1 986
55312f9f 987 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
2e1df07d
JA
988}
989
990static void cleanup_io_u(struct thread_data *td)
991{
2e1df07d
JA
992 struct io_u *io_u;
993
2ae0b204 994 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
c73ed246
JA
995
996 if (td->io_ops->io_u_free)
997 td->io_ops->io_u_free(td, io_u);
998
2e1df07d
JA
999 fio_memfree(io_u, sizeof(*io_u));
1000 }
1001
1002 free_io_mem(td);
2ae0b204
JA
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);
ca09be4b
JA
1007
1008 if (td->last_write_comp)
1009 sfree(td->last_write_comp);
2e1df07d
JA
1010}
1011
1012static int init_io_u(struct thread_data *td)
1013{
1014 struct io_u *io_u;
9c42684e 1015 unsigned int max_bs, min_write;
2e1df07d 1016 int cl_align, i, max_units;
2ae0b204 1017 int data_xfer = 1, err;
2e1df07d
JA
1018 char *p;
1019
1020 max_units = td->o.iodepth;
74f4b020 1021 max_bs = td_max_bs(td);
9c42684e 1022 min_write = td->o.min_bs[DDIR_WRITE];
2e1df07d
JA
1023 td->orig_buffer_size = (unsigned long long) max_bs
1024 * (unsigned long long) max_units;
1025
88045e04 1026 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
59d8d0f5
JA
1027 data_xfer = 0;
1028
2ae0b204
JA
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
fd8a09b8 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 */
d01612f3
CM
1045 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1046 (td->io_ops->flags & FIO_RAWIO))
fd8a09b8 1047 td->orig_buffer_size += page_mask + td->o.mem_align;
1048
2e1df07d
JA
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
59d8d0f5 1061 if (data_xfer && allocate_io_mem(td))
2e1df07d
JA
1062 return 1;
1063
d01612f3 1064 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
2e1df07d
JA
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));
2ae0b204 1086 INIT_FLIST_HEAD(&io_u->verify_list);
2e1df07d
JA
1087 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1088
59d8d0f5 1089 if (data_xfer) {
2e1df07d
JA
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))
9c42684e 1094 io_u_fill_buffer(td, io_u, min_write, max_bs);
2e1df07d
JA
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 */
ce35b1ec 1100 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
2e1df07d
JA
1101 }
1102 }
1103
1104 io_u->index = i;
1105 io_u->flags = IO_U_F_FREE;
2ae0b204
JA
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);
c73ed246
JA
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
2e1df07d
JA
1123 p += max_bs;
1124 }
1125
ca09be4b
JA
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
2e1df07d
JA
1134 return 0;
1135}
1136
1137static 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 */
49c6f33d 1174 ret = fread(tmp, sizeof(tmp), 1, f);
2e1df07d
JA
1175 if (ferror(f) || ret < 0) {
1176 td_verror(td, errno, "fread");
1177 fclose(f);
1178 return 1;
1179 }
49c6f33d
JA
1180 tmp[sizeof(tmp) - 1] = '\0';
1181
2e1df07d
JA
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
1195static int keep_running(struct thread_data *td)
1196{
77731b29
JA
1197 unsigned long long limit;
1198
2e1df07d
JA
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 }
3939fe85
JA
1207 if (exceeds_number_ios(td))
1208 return 0;
26251d8d 1209
77731b29
JA
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) {
5bd5f71a
JA
1216 uint64_t diff;
1217
1218 /*
1219 * If the difference is less than the minimum IO size, we
1220 * are done.
1221 */
77731b29 1222 diff = limit - ddir_rw_sum(td->io_bytes);
74f4b020 1223 if (diff < td_max_bs(td))
5bd5f71a
JA
1224 return 0;
1225
002fe734
JA
1226 if (fio_files_done(td))
1227 return 0;
1228
2e1df07d 1229 return 1;
5bd5f71a 1230 }
2e1df07d
JA
1231
1232 return 0;
1233}
1234
ce486495 1235static int exec_string(struct thread_options *o, const char *string, const char *mode)
2e1df07d 1236{
ce486495 1237 int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
2e1df07d
JA
1238 char *str;
1239
1240 str = malloc(newlen);
ce486495 1241 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
2e1df07d 1242
ce486495 1243 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
2e1df07d
JA
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
62167762
JC
1252/*
1253 * Dry run to compute correct state of numberio for verification.
1254 */
1255static uint64_t do_dry_run(struct thread_data *td)
1256{
62167762
JC
1257 td_set_runstate(td, TD_RUNNING);
1258
1259 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
e28dd2cf 1260 (!flist_empty(&td->trim_list)) || !io_complete_bytes_exceeded(td)) {
62167762
JC
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
a9da8ab2 1271 io_u_set(io_u, IO_U_F_FLIGHT);
62167762
JC
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
2e63e96b
PV
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
55312f9f 1287 ret = io_u_sync_complete(td, io_u);
62167762
JC
1288 (void) ret;
1289 }
1290
55312f9f 1291 return td->bytes_done[DDIR_WRITE] + td->bytes_done[DDIR_TRIM];
62167762
JC
1292}
1293
a9da8ab2
JA
1294static 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
2e1df07d
JA
1317/*
1318 * Entry point for the thread based jobs. The process based jobs end up
1319 * here as well, after a little setup.
1320 */
1321static void *thread_main(void *data)
1322{
95603b74 1323 unsigned long long elapsed_us[DDIR_RWDIR_CNT] = { 0, };
2e1df07d 1324 struct thread_data *td = data;
4896473e 1325 struct thread_options *o = &td->o;
2e1df07d
JA
1326 pthread_condattr_t attr;
1327 int clear_state;
28727df7 1328 int ret;
2e1df07d 1329
4896473e 1330 if (!o->use_thread) {
2e1df07d
JA
1331 setsid();
1332 td->pid = getpid();
1333 } else
1334 td->pid = gettid();
1335
4896473e 1336 fio_local_clock_init(o->use_thread);
5d879392 1337
2e1df07d
JA
1338 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1339
122c7725
JA
1340 if (is_backend)
1341 fio_server_send_start(td);
1342
2e1df07d
JA
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);
1ae83d45 1347 INIT_FLIST_HEAD(&td->next_rand_list);
2e1df07d
JA
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
2e1df07d
JA
1362 /*
1363 * A new gid requires privilege, so we need to do this before setting
1364 * the uid.
1365 */
4896473e 1366 if (o->gid != -1U && setgid(o->gid)) {
2e1df07d
JA
1367 td_verror(td, errno, "setgid");
1368 goto err;
1369 }
4896473e 1370 if (o->uid != -1U && setuid(o->uid)) {
2e1df07d
JA
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 */
4896473e
JA
1379 if (o->gtod_cpu)
1380 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
2e1df07d
JA
1381
1382 /*
1383 * Set affinity first, in case it has an impact on the memory
1384 * allocations.
1385 */
b2a9e649 1386 if (fio_option_is_set(o, cpumask)) {
c2acfbac 1387 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
30cb4c65 1388 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
c2acfbac
JA
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 }
28727df7
JA
1396 ret = fio_setaffinity(td->pid, o->cpumask);
1397 if (ret == -1) {
4896473e
JA
1398 td_verror(td, errno, "cpu_set_affinity");
1399 goto err;
1400 }
2e1df07d
JA
1401 }
1402
67bf9823 1403#ifdef CONFIG_LIBNUMA
d0b937ed 1404 /* numa node setup */
b2a9e649
JA
1405 if (fio_option_is_set(o, numa_cpunodes) ||
1406 fio_option_is_set(o, numa_memnodes)) {
43522848 1407 struct bitmask *mask;
d0b937ed
YR
1408
1409 if (numa_available() < 0) {
1410 td_verror(td, errno, "Does not support NUMA API\n");
1411 goto err;
1412 }
1413
b2a9e649 1414 if (fio_option_is_set(o, numa_cpunodes)) {
43522848
DG
1415 mask = numa_parse_nodestring(o->numa_cpunodes);
1416 ret = numa_run_on_node_mask(mask);
1417 numa_free_nodemask(mask);
d0b937ed
YR
1418 if (ret == -1) {
1419 td_verror(td, errno, \
1420 "numa_run_on_node_mask failed\n");
1421 goto err;
1422 }
1423 }
1424
b2a9e649 1425 if (fio_option_is_set(o, numa_memnodes)) {
43522848
DG
1426 mask = NULL;
1427 if (o->numa_memnodes)
1428 mask = numa_parse_nodestring(o->numa_memnodes);
1429
4896473e 1430 switch (o->numa_mem_mode) {
d0b937ed 1431 case MPOL_INTERLEAVE:
43522848 1432 numa_set_interleave_mask(mask);
d0b937ed
YR
1433 break;
1434 case MPOL_BIND:
43522848 1435 numa_set_membind(mask);
d0b937ed
YR
1436 break;
1437 case MPOL_LOCAL:
1438 numa_set_localalloc();
1439 break;
1440 case MPOL_PREFERRED:
4896473e 1441 numa_set_preferred(o->numa_mem_prefer_node);
d0b937ed
YR
1442 break;
1443 case MPOL_DEFAULT:
1444 default:
1445 break;
1446 }
1447
43522848
DG
1448 if (mask)
1449 numa_free_nodemask(mask);
1450
d0b937ed
YR
1451 }
1452 }
1453#endif
1454
9a3f1100
JA
1455 if (fio_pin_memory(td))
1456 goto err;
1457
2e1df07d
JA
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
4896473e 1468 if (o->verify_async && verify_async_init(td))
2e1df07d
JA
1469 goto err;
1470
27d74836
JA
1471 if (fio_option_is_set(o, ioprio) ||
1472 fio_option_is_set(o, ioprio_class)) {
28727df7
JA
1473 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1474 if (ret == -1) {
2e1df07d
JA
1475 td_verror(td, errno, "ioprio_set");
1476 goto err;
1477 }
1478 }
1479
4896473e 1480 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
2e1df07d
JA
1481 goto err;
1482
649c10c5 1483 errno = 0;
4896473e 1484 if (nice(o->nice) == -1 && errno != 0) {
2e1df07d
JA
1485 td_verror(td, errno, "nice");
1486 goto err;
1487 }
1488
4896473e 1489 if (o->ioscheduler && switch_ioscheduler(td))
2e1df07d
JA
1490 goto err;
1491
4896473e 1492 if (!o->create_serialize && setup_files(td))
2e1df07d
JA
1493 goto err;
1494
1495 if (td_io_init(td))
1496 goto err;
1497
1498 if (init_random_map(td))
1499 goto err;
1500
ce486495 1501 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
4896473e 1502 goto err;
2e1df07d 1503
4896473e 1504 if (o->pre_read) {
2e1df07d
JA
1505 if (pre_read_files(td) < 0)
1506 goto err;
1507 }
1508
aee2ab67
JA
1509 if (td->flags & TD_F_COMPRESS_LOG)
1510 tp_init(&td->tp_data);
1511
dc5bfbb2
JA
1512 fio_verify_init(td);
1513
a9da8ab2
JA
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
2e1df07d 1518 fio_gettime(&td->epoch, NULL);
44404c5a 1519 fio_getrusage(&td->ru_start);
2e1df07d
JA
1520 clear_state = 0;
1521 while (keep_running(td)) {
100f49f1
JA
1522 uint64_t verify_bytes;
1523
2e1df07d
JA
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
4896473e
JA
1529 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1530 o->ratemin[DDIR_TRIM]) {
6eaf09d6 1531 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
2e1df07d 1532 sizeof(td->bw_sample_time));
6eaf09d6
SL
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,
2e1df07d
JA
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
62167762
JC
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);
2e1df07d
JA
1548
1549 clear_state = 1;
1550
40c666c8
JA
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
e5437a07 1560 fio_mutex_down(stat_mutex);
95603b74
BF
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);
e5437a07
VT
1567 fio_gettime(&td->start, NULL);
1568 fio_mutex_up(stat_mutex);
2e1df07d
JA
1569
1570 if (td->error || td->terminate)
1571 break;
1572
4896473e
JA
1573 if (!o->do_verify ||
1574 o->verify == VERIFY_NONE ||
2e1df07d
JA
1575 (td->io_ops->flags & FIO_UNIDIR))
1576 continue;
1577
1578 clear_io_state(td);
1579
1580 fio_gettime(&td->start, NULL);
1581
100f49f1 1582 do_verify(td, verify_bytes);
2e1df07d 1583
40c666c8
JA
1584 /*
1585 * See comment further up for why this is done here.
1586 */
1587 check_update_rusage(td);
1588
e5437a07 1589 fio_mutex_down(stat_mutex);
95603b74 1590 update_runtime(td, elapsed_us, DDIR_READ);
e5437a07
VT
1591 fio_gettime(&td->start, NULL);
1592 fio_mutex_up(stat_mutex);
2e1df07d
JA
1593
1594 if (td->error || td->terminate)
1595 break;
1596 }
1597
1598 update_rusage_stat(td);
2e1df07d 1599 td->ts.total_run_time = mtime_since_now(&td->epoch);
6eaf09d6
SL
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];
2e1df07d 1603
ca09be4b
JA
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
9a3f1100
JA
1616 fio_unpin_memory(td);
1617
905e3d4f 1618 fio_writeout_logs(td);
2e1df07d 1619
a9da8ab2
JA
1620 if (o->io_submit_mode == IO_MODE_OFFLOAD)
1621 workqueue_exit(&td->io_wq);
1622
aee2ab67
JA
1623 if (td->flags & TD_F_COMPRESS_LOG)
1624 tp_exit(&td->tp_data);
1625
4896473e 1626 if (o->exec_postrun)
ce486495 1627 exec_string(o, o->exec_postrun, (const char *)"postrun");
2e1df07d
JA
1628
1629 if (exitall_on_terminate)
1630 fio_terminate_threads(td->groupid);
1631
1632err:
1633 if (td->error)
1634 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1635 td->verror);
1636
4896473e 1637 if (o->verify_async)
2e1df07d
JA
1638 verify_async_exit(td);
1639
1640 close_and_free_files(td);
2e1df07d 1641 cleanup_io_u(td);
32dbca2c 1642 close_ioengine(td);
2e1df07d 1643 cgroup_shutdown(td, &cgroup_mnt);
ca09be4b 1644 verify_free_state(td);
2e1df07d 1645
b2a9e649 1646 if (fio_option_is_set(o, cpumask)) {
8a1db9a1
JA
1647 ret = fio_cpuset_exit(&o->cpumask);
1648 if (ret)
1649 td_verror(td, ret, "fio_cpuset_exit");
2e1df07d
JA
1650 }
1651
1652 /*
1653 * do this very late, it will log file closing as well
1654 */
4896473e 1655 if (o->write_iolog_file)
2e1df07d
JA
1656 write_iolog_close(td);
1657
ea66e04f
JA
1658 fio_mutex_remove(td->mutex);
1659 td->mutex = NULL;
1660
2e1df07d 1661 td_set_runstate(td, TD_EXITED);
fda2cfac
JA
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
e43606c2 1669 return (void *) (uintptr_t) td->error;
2e1df07d
JA
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 */
1677static int fork_main(int shmid, int offset)
1678{
1679 struct thread_data *td;
1680 void *data, *ret;
1681
c8931876 1682#if !defined(__hpux) && !defined(CONFIG_NO_SHM)
2e1df07d
JA
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);
e43606c2 1700 return (int) (uintptr_t) ret;
2e1df07d
JA
1701}
1702
cba5460c
JA
1703static 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
2e1df07d
JA
1710/*
1711 * Run over the job map and reap the threads that have exited, if any.
1712 */
1713static 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);
a5e371a6 1762 td->sig = ECHILD;
2e1df07d
JA
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
36d80bc7 1771 if (sig != SIGTERM && sig != SIGUSR2)
2e1df07d
JA
1772 log_err("fio: pid=%d, got signal=%d\n",
1773 (int) td->pid, sig);
a5e371a6 1774 td->sig = sig;
2e1df07d
JA
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
cba5460c
JA
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
2e1df07d
JA
1798 /*
1799 * thread is not dead, continue
1800 */
1801 pending++;
1802 continue;
1803reaped:
1804 (*nr_running)--;
342f4be4
JA
1805 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1806 (*t_rate) -= ddir_rw_sum(td->o.rate);
2e1df07d
JA
1807 if (!td->pid)
1808 pending--;
1809
1810 if (td->error)
1811 exit_value++;
1812
1813 done_secs += mtime_since_now(&td->epoch) / 1000;
4a88752a 1814 profile_td_exit(td);
2e1df07d
JA
1815 }
1816
1817 if (*nr_running == cputhreads && !pending && realthreads)
1818 fio_terminate_threads(TERMINATE_ALL);
1819}
1820
ca09be4b
JA
1821static 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
1838static int trigger_timedout(void)
1839{
1840 if (trigger_timeout)
1841 return time_since_genesis() >= trigger_timeout;
1842
1843 return 0;
1844}
1845
1846void 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
1858void check_trigger_file(void)
1859{
1860 if (__check_trigger_file() || trigger_timedout()) {
796fb3ce
JA
1861 if (nr_clients)
1862 fio_clients_send_trigger(trigger_remote_cmd);
1863 else {
ca09be4b
JA
1864 verify_save_state();
1865 fio_terminate_threads(TERMINATE_ALL);
1866 exec_trigger(trigger_cmd);
1867 }
1868 }
1869}
1870
1871static 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
06464907
JA
1891static void do_usleep(unsigned int usecs)
1892{
1893 check_for_running_stats();
ca09be4b 1894 check_trigger_file();
06464907
JA
1895 usleep(usecs);
1896}
1897
2e1df07d
JA
1898/*
1899 * Main function for kicking off and reaping jobs, as needed.
1900 */
1901static void run_threads(void)
1902{
1903 struct thread_data *td;
2e1df07d 1904 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
0de5b26f 1905 uint64_t spent;
2e1df07d 1906
2e1df07d
JA
1907 if (fio_gtod_offload && fio_start_gtod_thread())
1908 return;
334185e9 1909
f2a2ce0e 1910 fio_idle_prof_init();
2e1df07d
JA
1911
1912 set_sig_handlers();
1913
3a5f6bde
JA
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
f3afa57e 1922 if (output_format == FIO_OUTPUT_NORMAL) {
2e1df07d
JA
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");
e411c301 1934 log_info_flush();
2e1df07d
JA
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
ca09be4b
JA
1948 if (fio_verify_load_state(td))
1949 goto reap;
1950
2e1df07d
JA
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)) {
ca09be4b 1957reap:
2e1df07d
JA
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
f2a2ce0e
HL
1980 /* start idle threads before io threads start to run */
1981 fio_idle_prof_start();
1982
2e1df07d
JA
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) {
0de5b26f 2007 spent = utime_since_genesis();
2e1df07d 2008
74454ce4 2009 if (td->o.start_delay > spent)
2e1df07d
JA
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
c97f1ad6
JA
2021 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
2022 td->update_rusage = 0;
2023
2e1df07d
JA
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
06464907 2080 do_usleep(100000);
2e1df07d
JA
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) {
4e87c37a
JA
2099 log_err("fio: %d job%s failed to start\n", left,
2100 left > 1 ? "s" : "");
2e1df07d
JA
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--;
342f4be4
JA
2123 m_rate += ddir_rw_sum(td->o.ratemin);
2124 t_rate += ddir_rw_sum(td->o.rate);
2e1df07d
JA
2125 todo--;
2126 fio_mutex_up(td->mutex);
2127 }
2128
2129 reap_threads(&nr_running, &t_rate, &m_rate);
2130
122c7725 2131 if (todo)
06464907 2132 do_usleep(100000);
2e1df07d
JA
2133 }
2134
2135 while (nr_running) {
2136 reap_threads(&nr_running, &t_rate, &m_rate);
06464907 2137 do_usleep(10000);
2e1df07d
JA
2138 }
2139
f2a2ce0e
HL
2140 fio_idle_prof_stop();
2141
2e1df07d 2142 update_io_ticks();
2e1df07d
JA
2143}
2144
5ddc6707 2145static void wait_for_helper_thread_exit(void)
9ec7779f 2146{
8aab824f
JA
2147 void *ret;
2148
98b4b0a2 2149 helper_exit = 1;
5ddc6707
JA
2150 pthread_cond_signal(&helper_cond);
2151 pthread_join(helper_thread, &ret);
9ec7779f
JA
2152}
2153
27357187
JA
2154static void free_disk_util(void)
2155{
27357187 2156 disk_util_prune_entries();
8aab824f 2157
5ddc6707 2158 pthread_cond_destroy(&helper_cond);
27357187
JA
2159}
2160
5ddc6707 2161static void *helper_thread_main(void *data)
2e1df07d 2162{
9ec7779f
JA
2163 int ret = 0;
2164
2e1df07d
JA
2165 fio_mutex_up(startup_mutex);
2166
8aab824f
JA
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;
f893b76d 2176
823952af 2177 if (ts.tv_nsec >= 1000000000ULL) {
8aab824f
JA
2178 ts.tv_nsec -= 1000000000ULL;
2179 ts.tv_sec++;
2180 }
2181
5ddc6707 2182 pthread_cond_timedwait(&helper_cond, &helper_lock, &ts);
8aab824f 2183
9ec7779f 2184 ret = update_io_ticks();
2e1df07d 2185
5ddc6707
JA
2186 if (helper_do_stat) {
2187 helper_do_stat = 0;
2188 __show_running_run_stats();
2189 }
2190
2e1df07d
JA
2191 if (!is_backend)
2192 print_thread_status();
2193 }
2194
2195 return NULL;
2196}
2197
5ddc6707 2198static int create_helper_thread(void)
2e1df07d
JA
2199{
2200 int ret;
2201
9ec7779f
JA
2202 setup_disk_util();
2203
5ddc6707
JA
2204 pthread_cond_init(&helper_cond, NULL);
2205 pthread_mutex_init(&helper_lock, NULL);
8aab824f 2206
5ddc6707 2207 ret = pthread_create(&helper_thread, NULL, helper_thread_main, NULL);
2e1df07d 2208 if (ret) {
5ddc6707 2209 log_err("Can't create helper thread: %s\n", strerror(ret));
2e1df07d
JA
2210 return 1;
2211 }
2212
2e1df07d
JA
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
2e1df07d
JA
2219int 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) {
aee2ab67
JA
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");
2e1df07d
JA
2241 }
2242
521da527 2243 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2e1df07d
JA
2244 if (startup_mutex == NULL)
2245 return 1;
2e1df07d
JA
2246
2247 set_genesis_time();
cef9175e 2248 stat_init();
5ddc6707 2249 create_helper_thread();
2e1df07d
JA
2250
2251 cgroup_list = smalloc(sizeof(*cgroup_list));
2252 INIT_FLIST_HEAD(cgroup_list);
2253
2254 run_threads();
2255
5ddc6707 2256 wait_for_helper_thread_exit();
8aab824f 2257
2e1df07d 2258 if (!fio_abort) {
83f7b64e 2259 __show_run_stats();
2e1df07d 2260 if (write_bw_log) {
cb7e0ace
JA
2261 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2262 struct io_log *log = agg_io_log[i];
2263
aee2ab67 2264 flush_log(log);
518dac09 2265 free_log(log);
cb7e0ace 2266 }
2e1df07d
JA
2267 }
2268 }
2269
fda2cfac 2270 for_each_td(td, i) {
2e1df07d 2271 fio_options_free(td);
8049adc1
JA
2272 if (td->rusage_sem) {
2273 fio_mutex_remove(td->rusage_sem);
2274 td->rusage_sem = NULL;
2275 }
fda2cfac 2276 }
2e1df07d 2277
a462baef 2278 free_disk_util();
2e1df07d
JA
2279 cgroup_kill(cgroup_list);
2280 sfree(cgroup_list);
2281 sfree(cgroup_mnt);
2282
2283 fio_mutex_remove(startup_mutex);
cef9175e 2284 stat_exit();
2e1df07d
JA
2285 return exit_value;
2286}