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