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