Make experimental_verify=1 handle all cases properly
[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>
ec5c6b12 37#ifndef FIO_NO_HAVE_SHM_H
2e1df07d 38#include <sys/shm.h>
ec5c6b12 39#endif
2e1df07d
JA
40#include <sys/mman.h>
41
42#include "fio.h"
43#include "hash.h"
44#include "smalloc.h"
45#include "verify.h"
46#include "trim.h"
47#include "diskutil.h"
48#include "cgroup.h"
49#include "profile.h"
50#include "lib/rand.h"
51#include "memalign.h"
52#include "server.h"
53
54static pthread_t disk_util_thread;
9ec7779f 55static struct fio_mutex *disk_thread_mutex;
2e1df07d
JA
56static struct fio_mutex *startup_mutex;
57static struct fio_mutex *writeout_mutex;
58static struct flist_head *cgroup_list;
59static char *cgroup_mnt;
60static int exit_value;
61static volatile int fio_abort;
62
6eaf09d6 63struct io_log *agg_io_log[DDIR_RWDIR_CNT];
2e1df07d 64
a3efc919
JA
65int groupid = 0;
66unsigned int thread_number = 0;
108fea77 67unsigned int stat_number = 0;
a3efc919
JA
68unsigned int nr_process = 0;
69unsigned int nr_thread = 0;
70int shm_id = 0;
71int temp_stall_ts;
72unsigned long done_secs = 0;
27357187 73volatile int disk_util_exit = 0;
a3efc919 74
2e1df07d 75#define PAGE_ALIGN(buf) \
e43606c2 76 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
2e1df07d
JA
77
78#define JOB_START_TIMEOUT (5 * 1000)
79
80static void sig_int(int sig)
81{
82 if (threads) {
83 if (is_backend)
84 fio_server_got_signal(sig);
85 else {
86 log_info("\nfio: terminating on signal %d\n", sig);
87 fflush(stdout);
88 exit_value = 128;
89 }
90
91 fio_terminate_threads(TERMINATE_ALL);
92 }
93}
94
4c6d91e8
JA
95static void sig_show_status(int sig)
96{
97 show_running_run_stats();
98}
99
2e1df07d
JA
100static void set_sig_handlers(void)
101{
102 struct sigaction act;
103
104 memset(&act, 0, sizeof(act));
105 act.sa_handler = sig_int;
106 act.sa_flags = SA_RESTART;
107 sigaction(SIGINT, &act, NULL);
108
109 memset(&act, 0, sizeof(act));
110 act.sa_handler = sig_int;
111 act.sa_flags = SA_RESTART;
112 sigaction(SIGTERM, &act, NULL);
113
2f694507
BC
114/* Windows uses SIGBREAK as a quit signal from other applications */
115#ifdef WIN32
116 memset(&act, 0, sizeof(act));
117 act.sa_handler = sig_int;
118 act.sa_flags = SA_RESTART;
119 sigaction(SIGBREAK, &act, NULL);
120#endif
121
4c6d91e8
JA
122 memset(&act, 0, sizeof(act));
123 act.sa_handler = sig_show_status;
124 act.sa_flags = SA_RESTART;
125 sigaction(SIGUSR1, &act, NULL);
126
2e1df07d
JA
127 if (is_backend) {
128 memset(&act, 0, sizeof(act));
129 act.sa_handler = sig_int;
130 act.sa_flags = SA_RESTART;
131 sigaction(SIGPIPE, &act, NULL);
132 }
133}
134
135/*
136 * Check if we are above the minimum rate given.
137 */
138static int __check_min_rate(struct thread_data *td, struct timeval *now,
139 enum fio_ddir ddir)
140{
141 unsigned long long bytes = 0;
142 unsigned long iops = 0;
143 unsigned long spent;
144 unsigned long rate;
145 unsigned int ratemin = 0;
146 unsigned int rate_iops = 0;
147 unsigned int rate_iops_min = 0;
148
149 assert(ddir_rw(ddir));
150
151 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
152 return 0;
153
154 /*
155 * allow a 2 second settle period in the beginning
156 */
157 if (mtime_since(&td->start, now) < 2000)
158 return 0;
159
160 iops += td->this_io_blocks[ddir];
161 bytes += td->this_io_bytes[ddir];
162 ratemin += td->o.ratemin[ddir];
163 rate_iops += td->o.rate_iops[ddir];
164 rate_iops_min += td->o.rate_iops_min[ddir];
165
166 /*
167 * if rate blocks is set, sample is running
168 */
169 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
170 spent = mtime_since(&td->lastrate[ddir], now);
171 if (spent < td->o.ratecycle)
172 return 0;
173
174 if (td->o.rate[ddir]) {
175 /*
176 * check bandwidth specified rate
177 */
178 if (bytes < td->rate_bytes[ddir]) {
179 log_err("%s: min rate %u not met\n", td->o.name,
180 ratemin);
181 return 1;
182 } else {
183 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
184 if (rate < ratemin ||
185 bytes < td->rate_bytes[ddir]) {
186 log_err("%s: min rate %u not met, got"
187 " %luKB/sec\n", td->o.name,
188 ratemin, rate);
189 return 1;
190 }
191 }
192 } else {
193 /*
194 * checks iops specified rate
195 */
196 if (iops < rate_iops) {
197 log_err("%s: min iops rate %u not met\n",
198 td->o.name, rate_iops);
199 return 1;
200 } else {
201 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
202 if (rate < rate_iops_min ||
203 iops < td->rate_blocks[ddir]) {
204 log_err("%s: min iops rate %u not met,"
205 " got %lu\n", td->o.name,
206 rate_iops_min, rate);
207 }
208 }
209 }
210 }
211
212 td->rate_bytes[ddir] = bytes;
213 td->rate_blocks[ddir] = iops;
214 memcpy(&td->lastrate[ddir], now, sizeof(*now));
215 return 0;
216}
217
218static int check_min_rate(struct thread_data *td, struct timeval *now,
219 unsigned long *bytes_done)
220{
221 int ret = 0;
222
6eaf09d6
SL
223 if (bytes_done[DDIR_READ])
224 ret |= __check_min_rate(td, now, DDIR_READ);
225 if (bytes_done[DDIR_WRITE])
226 ret |= __check_min_rate(td, now, DDIR_WRITE);
227 if (bytes_done[DDIR_TRIM])
228 ret |= __check_min_rate(td, now, DDIR_TRIM);
2e1df07d
JA
229
230 return ret;
231}
232
233/*
234 * When job exits, we can cancel the in-flight IO if we are using async
235 * io. Attempt to do so.
236 */
237static void cleanup_pending_aio(struct thread_data *td)
238{
239 struct flist_head *entry, *n;
240 struct io_u *io_u;
241 int r;
242
243 /*
244 * get immediately available events, if any
245 */
246 r = io_u_queued_complete(td, 0, NULL);
247 if (r < 0)
248 return;
249
250 /*
251 * now cancel remaining active events
252 */
253 if (td->io_ops->cancel) {
254 flist_for_each_safe(entry, n, &td->io_u_busylist) {
255 io_u = flist_entry(entry, struct io_u, list);
256
257 /*
258 * if the io_u isn't in flight, then that generally
259 * means someone leaked an io_u. complain but fix
260 * it up, so we don't stall here.
261 */
262 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
263 log_err("fio: non-busy IO on busy list\n");
264 put_io_u(td, io_u);
265 } else {
266 r = td->io_ops->cancel(td, io_u);
267 if (!r)
268 put_io_u(td, io_u);
269 }
270 }
271 }
272
273 if (td->cur_depth)
274 r = io_u_queued_complete(td, td->cur_depth, NULL);
275}
276
277/*
278 * Helper to handle the final sync of a file. Works just like the normal
279 * io path, just does everything sync.
280 */
281static int fio_io_sync(struct thread_data *td, struct fio_file *f)
282{
283 struct io_u *io_u = __get_io_u(td);
284 int ret;
285
286 if (!io_u)
287 return 1;
288
289 io_u->ddir = DDIR_SYNC;
290 io_u->file = f;
291
292 if (td_io_prep(td, io_u)) {
293 put_io_u(td, io_u);
294 return 1;
295 }
296
297requeue:
298 ret = td_io_queue(td, io_u);
299 if (ret < 0) {
300 td_verror(td, io_u->error, "td_io_queue");
301 put_io_u(td, io_u);
302 return 1;
303 } else if (ret == FIO_Q_QUEUED) {
304 if (io_u_queued_complete(td, 1, NULL) < 0)
305 return 1;
306 } else if (ret == FIO_Q_COMPLETED) {
307 if (io_u->error) {
308 td_verror(td, io_u->error, "td_io_queue");
309 return 1;
310 }
311
312 if (io_u_sync_complete(td, io_u, NULL) < 0)
313 return 1;
314 } else if (ret == FIO_Q_BUSY) {
315 if (td_io_commit(td))
316 return 1;
317 goto requeue;
318 }
319
320 return 0;
321}
a3efc919 322
2e1df07d
JA
323static inline void __update_tv_cache(struct thread_data *td)
324{
325 fio_gettime(&td->tv_cache, NULL);
326}
327
328static inline void update_tv_cache(struct thread_data *td)
329{
330 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
331 __update_tv_cache(td);
332}
333
334static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
335{
336 if (in_ramp_time(td))
337 return 0;
338 if (!td->o.timeout)
339 return 0;
340 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
341 return 1;
342
343 return 0;
344}
345
346static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
347 int *retptr)
348{
349 int ret = *retptr;
350
351 if (ret < 0 || td->error) {
8b28bd41
DM
352 int err = td->error;
353 enum error_type_bit eb;
2e1df07d
JA
354
355 if (ret < 0)
356 err = -ret;
2e1df07d 357
8b28bd41
DM
358 eb = td_error_type(ddir, err);
359 if (!(td->o.continue_on_error & (1 << eb)))
2e1df07d
JA
360 return 1;
361
8b28bd41 362 if (td_non_fatal_error(td, eb, err)) {
2e1df07d
JA
363 /*
364 * Continue with the I/Os in case of
365 * a non fatal error.
366 */
367 update_error_count(td, err);
368 td_clear_error(td);
369 *retptr = 0;
370 return 0;
371 } else if (td->o.fill_device && err == ENOSPC) {
372 /*
373 * We expect to hit this error if
374 * fill_device option is set.
375 */
376 td_clear_error(td);
377 td->terminate = 1;
378 return 1;
379 } else {
380 /*
381 * Stop the I/O in case of a fatal
382 * error.
383 */
384 update_error_count(td, err);
385 return 1;
386 }
387 }
388
389 return 0;
390}
391
2e1df07d
JA
392/*
393 * The main verify engine. Runs over the writes we previously submitted,
394 * reads the blocks back in, and checks the crc/md5 of the data.
395 */
bcd5abfa 396static void do_verify(struct thread_data *td)
2e1df07d
JA
397{
398 struct fio_file *f;
399 struct io_u *io_u;
400 int ret, min_events;
401 unsigned int i;
402
403 dprint(FD_VERIFY, "starting loop\n");
404
405 /*
406 * sync io first and invalidate cache, to make sure we really
407 * read from disk.
408 */
409 for_each_file(td, f, i) {
410 if (!fio_file_open(f))
411 continue;
412 if (fio_io_sync(td, f))
413 break;
414 if (file_invalidate_cache(td, f))
415 break;
416 }
417
418 if (td->error)
419 return;
420
421 td_set_runstate(td, TD_VERIFYING);
422
423 io_u = NULL;
424 while (!td->terminate) {
fbccf46c 425 enum fio_ddir ddir;
2e1df07d
JA
426 int ret2, full;
427
428 update_tv_cache(td);
429
430 if (runtime_exceeded(td, &td->tv_cache)) {
431 __update_tv_cache(td);
432 if (runtime_exceeded(td, &td->tv_cache)) {
433 td->terminate = 1;
434 break;
435 }
436 }
437
9e684a49
DE
438 if (flow_threshold_exceeded(td))
439 continue;
440
44cbc6da
JA
441 if (!td->o.experimental_verify) {
442 io_u = __get_io_u(td);
443 if (!io_u)
444 break;
2e1df07d 445
44cbc6da
JA
446 if (get_next_verify(td, io_u)) {
447 put_io_u(td, io_u);
448 break;
449 }
2e1df07d 450
44cbc6da
JA
451 if (td_io_prep(td, io_u)) {
452 put_io_u(td, io_u);
453 break;
454 }
455 } else {
bcd5abfa
JA
456 while ((io_u = get_io_u(td)) != NULL) {
457 /*
458 * We are only interested in the places where
459 * we wrote or trimmed IOs. Turn those into
460 * reads for verification purposes.
461 */
462 if (io_u->ddir == DDIR_READ) {
463 /*
464 * Pretend we issued it for rwmix
465 * accounting
466 */
467 td->io_issues[DDIR_READ]++;
468 put_io_u(td, io_u);
469 continue;
470 } else if (io_u->ddir == DDIR_TRIM) {
471 io_u->ddir = DDIR_READ;
472 io_u->flags |= IO_U_F_TRIMMED;
473 break;
474 } else if (io_u->ddir == DDIR_WRITE) {
475 io_u->ddir = DDIR_READ;
476 break;
477 } else {
478 put_io_u(td, io_u);
479 continue;
480 }
481 }
44cbc6da 482
bcd5abfa 483 if (!io_u)
44cbc6da 484 break;
2e1df07d
JA
485 }
486
487 if (td->o.verify_async)
488 io_u->end_io = verify_io_u_async;
489 else
490 io_u->end_io = verify_io_u;
491
fbccf46c
JA
492 ddir = io_u->ddir;
493
2e1df07d
JA
494 ret = td_io_queue(td, io_u);
495 switch (ret) {
496 case FIO_Q_COMPLETED:
497 if (io_u->error) {
498 ret = -io_u->error;
499 clear_io_u(td, io_u);
500 } else if (io_u->resid) {
501 int bytes = io_u->xfer_buflen - io_u->resid;
502
503 /*
504 * zero read, fail
505 */
506 if (!bytes) {
507 td_verror(td, EIO, "full resid");
508 put_io_u(td, io_u);
509 break;
510 }
511
512 io_u->xfer_buflen = io_u->resid;
513 io_u->xfer_buf += bytes;
514 io_u->offset += bytes;
515
516 if (ddir_rw(io_u->ddir))
517 td->ts.short_io_u[io_u->ddir]++;
518
519 f = io_u->file;
520 if (io_u->offset == f->real_file_size)
521 goto sync_done;
522
523 requeue_io_u(td, &io_u);
524 } else {
525sync_done:
526 ret = io_u_sync_complete(td, io_u, NULL);
527 if (ret < 0)
528 break;
529 }
530 continue;
531 case FIO_Q_QUEUED:
532 break;
533 case FIO_Q_BUSY:
534 requeue_io_u(td, &io_u);
535 ret2 = td_io_commit(td);
536 if (ret2 < 0)
537 ret = ret2;
538 break;
539 default:
540 assert(ret < 0);
541 td_verror(td, -ret, "td_io_queue");
542 break;
543 }
544
fbccf46c 545 if (break_on_this_error(td, ddir, &ret))
2e1df07d
JA
546 break;
547
548 /*
549 * if we can queue more, do so. but check if there are
550 * completed io_u's first. Note that we can get BUSY even
551 * without IO queued, if the system is resource starved.
552 */
553 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
554 if (full || !td->o.iodepth_batch_complete) {
555 min_events = min(td->o.iodepth_batch_complete,
556 td->cur_depth);
8a74b56d
JA
557 /*
558 * if the queue is full, we MUST reap at least 1 event
559 */
560 if (full && !min_events)
2e1df07d
JA
561 min_events = 1;
562
563 do {
564 /*
565 * Reap required number of io units, if any,
566 * and do the verification on them through
567 * the callback handler
568 */
bcd5abfa 569 if (io_u_queued_complete(td, min_events, NULL) < 0) {
2e1df07d
JA
570 ret = -1;
571 break;
572 }
573 } while (full && (td->cur_depth > td->o.iodepth_low));
574 }
575 if (ret < 0)
576 break;
577 }
578
579 if (!td->error) {
580 min_events = td->cur_depth;
581
582 if (min_events)
583 ret = io_u_queued_complete(td, min_events, NULL);
584 } else
585 cleanup_pending_aio(td);
586
587 td_set_runstate(td, TD_RUNNING);
588
589 dprint(FD_VERIFY, "exiting loop\n");
590}
591
f7078f7b
JA
592static int io_bytes_exceeded(struct thread_data *td)
593{
594 unsigned long long bytes;
595
596 if (td_rw(td))
6eaf09d6 597 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
f7078f7b 598 else if (td_write(td))
6eaf09d6
SL
599 bytes = td->this_io_bytes[DDIR_WRITE];
600 else if (td_read(td))
601 bytes = td->this_io_bytes[DDIR_READ];
f7078f7b 602 else
6eaf09d6 603 bytes = td->this_io_bytes[DDIR_TRIM];
f7078f7b
JA
604
605 return bytes >= td->o.size;
606}
607
2e1df07d
JA
608/*
609 * Main IO worker function. It retrieves io_u's to process and queues
610 * and reaps them, checking for rate and errors along the way.
611 */
612static void do_io(struct thread_data *td)
613{
614 unsigned int i;
615 int ret = 0;
616
617 if (in_ramp_time(td))
618 td_set_runstate(td, TD_RAMP);
619 else
620 td_set_runstate(td, TD_RUNNING);
621
f7078f7b 622 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
c04e4661
DE
623 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
624 td->o.time_based) {
2e1df07d 625 struct timeval comp_time;
6eaf09d6 626 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
2e1df07d
JA
627 int min_evts = 0;
628 struct io_u *io_u;
629 int ret2, full;
630 enum fio_ddir ddir;
631
7d7803fa 632 if (td->terminate || td->done)
2e1df07d
JA
633 break;
634
635 update_tv_cache(td);
636
637 if (runtime_exceeded(td, &td->tv_cache)) {
638 __update_tv_cache(td);
639 if (runtime_exceeded(td, &td->tv_cache)) {
640 td->terminate = 1;
641 break;
642 }
643 }
644
9e684a49
DE
645 if (flow_threshold_exceeded(td))
646 continue;
647
2e1df07d
JA
648 io_u = get_io_u(td);
649 if (!io_u)
650 break;
651
652 ddir = io_u->ddir;
653
654 /*
82af2a7c
JA
655 * Add verification end_io handler if:
656 * - Asked to verify (!td_rw(td))
657 * - Or the io_u is from our verify list (mixed write/ver)
2e1df07d
JA
658 */
659 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
82af2a7c 660 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
2e1df07d
JA
661 if (td->o.verify_async)
662 io_u->end_io = verify_io_u_async;
663 else
664 io_u->end_io = verify_io_u;
665 td_set_runstate(td, TD_VERIFYING);
666 } else if (in_ramp_time(td))
667 td_set_runstate(td, TD_RAMP);
668 else
669 td_set_runstate(td, TD_RUNNING);
670
671 ret = td_io_queue(td, io_u);
672 switch (ret) {
673 case FIO_Q_COMPLETED:
674 if (io_u->error) {
675 ret = -io_u->error;
676 clear_io_u(td, io_u);
677 } else if (io_u->resid) {
678 int bytes = io_u->xfer_buflen - io_u->resid;
679 struct fio_file *f = io_u->file;
680
681 /*
682 * zero read, fail
683 */
684 if (!bytes) {
685 td_verror(td, EIO, "full resid");
686 put_io_u(td, io_u);
687 break;
688 }
689
690 io_u->xfer_buflen = io_u->resid;
691 io_u->xfer_buf += bytes;
692 io_u->offset += bytes;
693
694 if (ddir_rw(io_u->ddir))
695 td->ts.short_io_u[io_u->ddir]++;
696
697 if (io_u->offset == f->real_file_size)
698 goto sync_done;
699
700 requeue_io_u(td, &io_u);
701 } else {
702sync_done:
6eaf09d6
SL
703 if (__should_check_rate(td, DDIR_READ) ||
704 __should_check_rate(td, DDIR_WRITE) ||
705 __should_check_rate(td, DDIR_TRIM))
2e1df07d
JA
706 fio_gettime(&comp_time, NULL);
707
708 ret = io_u_sync_complete(td, io_u, bytes_done);
709 if (ret < 0)
710 break;
711 }
712 break;
713 case FIO_Q_QUEUED:
714 /*
715 * if the engine doesn't have a commit hook,
716 * the io_u is really queued. if it does have such
717 * a hook, it has to call io_u_queued() itself.
718 */
719 if (td->io_ops->commit == NULL)
720 io_u_queued(td, io_u);
721 break;
722 case FIO_Q_BUSY:
723 requeue_io_u(td, &io_u);
724 ret2 = td_io_commit(td);
725 if (ret2 < 0)
726 ret = ret2;
727 break;
728 default:
729 assert(ret < 0);
730 put_io_u(td, io_u);
731 break;
732 }
733
734 if (break_on_this_error(td, ddir, &ret))
735 break;
736
737 /*
738 * See if we need to complete some commands. Note that we
739 * can get BUSY even without IO queued, if the system is
740 * resource starved.
741 */
742 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
743 if (full || !td->o.iodepth_batch_complete) {
744 min_evts = min(td->o.iodepth_batch_complete,
745 td->cur_depth);
8a74b56d
JA
746 /*
747 * if the queue is full, we MUST reap at least 1 event
748 */
749 if (full && !min_evts)
2e1df07d
JA
750 min_evts = 1;
751
6eaf09d6
SL
752 if (__should_check_rate(td, DDIR_READ) ||
753 __should_check_rate(td, DDIR_WRITE) ||
754 __should_check_rate(td, DDIR_TRIM))
2e1df07d
JA
755 fio_gettime(&comp_time, NULL);
756
757 do {
758 ret = io_u_queued_complete(td, min_evts, bytes_done);
759 if (ret < 0)
760 break;
761
762 } while (full && (td->cur_depth > td->o.iodepth_low));
763 }
764
765 if (ret < 0)
766 break;
d5abee06 767 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
2e1df07d
JA
768 continue;
769
770 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
771 if (check_min_rate(td, &comp_time, bytes_done)) {
772 if (exitall_on_terminate)
773 fio_terminate_threads(td->groupid);
774 td_verror(td, EIO, "check_min_rate");
775 break;
776 }
777 }
778
779 if (td->o.thinktime) {
780 unsigned long long b;
781
342f4be4 782 b = ddir_rw_sum(td->io_blocks);
2e1df07d
JA
783 if (!(b % td->o.thinktime_blocks)) {
784 int left;
785
786 if (td->o.thinktime_spin)
787 usec_spin(td->o.thinktime_spin);
788
789 left = td->o.thinktime - td->o.thinktime_spin;
790 if (left)
791 usec_sleep(td, left);
792 }
793 }
794 }
795
796 if (td->trim_entries)
797 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
798
799 if (td->o.fill_device && td->error == ENOSPC) {
800 td->error = 0;
801 td->terminate = 1;
802 }
803 if (!td->error) {
804 struct fio_file *f;
805
806 i = td->cur_depth;
807 if (i) {
808 ret = io_u_queued_complete(td, i, NULL);
809 if (td->o.fill_device && td->error == ENOSPC)
810 td->error = 0;
811 }
812
813 if (should_fsync(td) && td->o.end_fsync) {
814 td_set_runstate(td, TD_FSYNCING);
815
816 for_each_file(td, f, i) {
817 if (!fio_file_open(f))
818 continue;
819 fio_io_sync(td, f);
820 }
821 }
822 } else
823 cleanup_pending_aio(td);
824
825 /*
826 * stop job if we failed doing any IO
827 */
342f4be4 828 if (!ddir_rw_sum(td->this_io_bytes))
2e1df07d
JA
829 td->done = 1;
830}
831
832static void cleanup_io_u(struct thread_data *td)
833{
834 struct flist_head *entry, *n;
835 struct io_u *io_u;
836
837 flist_for_each_safe(entry, n, &td->io_u_freelist) {
838 io_u = flist_entry(entry, struct io_u, list);
839
840 flist_del(&io_u->list);
c73ed246
JA
841
842 if (td->io_ops->io_u_free)
843 td->io_ops->io_u_free(td, io_u);
844
2e1df07d
JA
845 fio_memfree(io_u, sizeof(*io_u));
846 }
847
848 free_io_mem(td);
849}
850
851static int init_io_u(struct thread_data *td)
852{
853 struct io_u *io_u;
9c42684e 854 unsigned int max_bs, min_write;
2e1df07d 855 int cl_align, i, max_units;
59d8d0f5 856 int data_xfer = 1;
2e1df07d
JA
857 char *p;
858
859 max_units = td->o.iodepth;
860 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
6eaf09d6 861 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
9c42684e 862 min_write = td->o.min_bs[DDIR_WRITE];
2e1df07d
JA
863 td->orig_buffer_size = (unsigned long long) max_bs
864 * (unsigned long long) max_units;
865
88045e04 866 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
59d8d0f5
JA
867 data_xfer = 0;
868
2e1df07d
JA
869 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
870 unsigned long bs;
871
872 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
873 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
874 }
875
876 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
877 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
878 return 1;
879 }
880
59d8d0f5 881 if (data_xfer && allocate_io_mem(td))
2e1df07d
JA
882 return 1;
883
884 if (td->o.odirect || td->o.mem_align ||
885 (td->io_ops->flags & FIO_RAWIO))
886 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
887 else
888 p = td->orig_buffer;
889
890 cl_align = os_cache_line_size();
891
892 for (i = 0; i < max_units; i++) {
893 void *ptr;
894
895 if (td->terminate)
896 return 1;
897
898 ptr = fio_memalign(cl_align, sizeof(*io_u));
899 if (!ptr) {
900 log_err("fio: unable to allocate aligned memory\n");
901 break;
902 }
903
904 io_u = ptr;
905 memset(io_u, 0, sizeof(*io_u));
906 INIT_FLIST_HEAD(&io_u->list);
907 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
908
59d8d0f5 909 if (data_xfer) {
2e1df07d
JA
910 io_u->buf = p;
911 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
912
913 if (td_write(td))
9c42684e 914 io_u_fill_buffer(td, io_u, min_write, max_bs);
2e1df07d
JA
915 if (td_write(td) && td->o.verify_pattern_bytes) {
916 /*
917 * Fill the buffer with the pattern if we are
918 * going to be doing writes.
919 */
920 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
921 }
922 }
923
924 io_u->index = i;
925 io_u->flags = IO_U_F_FREE;
926 flist_add(&io_u->list, &td->io_u_freelist);
c73ed246
JA
927
928 if (td->io_ops->io_u_init) {
929 int ret = td->io_ops->io_u_init(td, io_u);
930
931 if (ret) {
932 log_err("fio: failed to init engine data: %d\n", ret);
933 return 1;
934 }
935 }
936
2e1df07d
JA
937 p += max_bs;
938 }
939
940 return 0;
941}
942
943static int switch_ioscheduler(struct thread_data *td)
944{
945 char tmp[256], tmp2[128];
946 FILE *f;
947 int ret;
948
949 if (td->io_ops->flags & FIO_DISKLESSIO)
950 return 0;
951
952 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
953
954 f = fopen(tmp, "r+");
955 if (!f) {
956 if (errno == ENOENT) {
957 log_err("fio: os or kernel doesn't support IO scheduler"
958 " switching\n");
959 return 0;
960 }
961 td_verror(td, errno, "fopen iosched");
962 return 1;
963 }
964
965 /*
966 * Set io scheduler.
967 */
968 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
969 if (ferror(f) || ret != 1) {
970 td_verror(td, errno, "fwrite");
971 fclose(f);
972 return 1;
973 }
974
975 rewind(f);
976
977 /*
978 * Read back and check that the selected scheduler is now the default.
979 */
980 ret = fread(tmp, 1, sizeof(tmp), f);
981 if (ferror(f) || ret < 0) {
982 td_verror(td, errno, "fread");
983 fclose(f);
984 return 1;
985 }
986
987 sprintf(tmp2, "[%s]", td->o.ioscheduler);
988 if (!strstr(tmp, tmp2)) {
989 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
990 td_verror(td, EINVAL, "iosched_switch");
991 fclose(f);
992 return 1;
993 }
994
995 fclose(f);
996 return 0;
997}
998
999static int keep_running(struct thread_data *td)
1000{
2e1df07d
JA
1001 if (td->done)
1002 return 0;
1003 if (td->o.time_based)
1004 return 1;
1005 if (td->o.loops) {
1006 td->o.loops--;
1007 return 1;
1008 }
1009
342f4be4 1010 if (ddir_rw_sum(td->io_bytes) < td->o.size)
2e1df07d
JA
1011 return 1;
1012
1013 return 0;
1014}
1015
1016static int exec_string(const char *string)
1017{
1018 int ret, newlen = strlen(string) + 1 + 8;
1019 char *str;
1020
1021 str = malloc(newlen);
1022 sprintf(str, "sh -c %s", string);
1023
1024 ret = system(str);
1025 if (ret == -1)
1026 log_err("fio: exec of cmd <%s> failed\n", str);
1027
1028 free(str);
1029 return ret;
1030}
1031
1032/*
1033 * Entry point for the thread based jobs. The process based jobs end up
1034 * here as well, after a little setup.
1035 */
1036static void *thread_main(void *data)
1037{
1038 unsigned long long elapsed;
1039 struct thread_data *td = data;
1040 pthread_condattr_t attr;
1041 int clear_state;
1042
1043 if (!td->o.use_thread) {
1044 setsid();
1045 td->pid = getpid();
1046 } else
1047 td->pid = gettid();
1048
5d879392
JA
1049 fio_local_clock_init(td->o.use_thread);
1050
2e1df07d
JA
1051 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1052
1053 INIT_FLIST_HEAD(&td->io_u_freelist);
1054 INIT_FLIST_HEAD(&td->io_u_busylist);
1055 INIT_FLIST_HEAD(&td->io_u_requeues);
1056 INIT_FLIST_HEAD(&td->io_log_list);
1057 INIT_FLIST_HEAD(&td->io_hist_list);
1058 INIT_FLIST_HEAD(&td->verify_list);
1059 INIT_FLIST_HEAD(&td->trim_list);
1ae83d45 1060 INIT_FLIST_HEAD(&td->next_rand_list);
2e1df07d
JA
1061 pthread_mutex_init(&td->io_u_lock, NULL);
1062 td->io_hist_tree = RB_ROOT;
1063
1064 pthread_condattr_init(&attr);
1065 pthread_cond_init(&td->verify_cond, &attr);
1066 pthread_cond_init(&td->free_cond, &attr);
1067
1068 td_set_runstate(td, TD_INITIALIZED);
1069 dprint(FD_MUTEX, "up startup_mutex\n");
1070 fio_mutex_up(startup_mutex);
1071 dprint(FD_MUTEX, "wait on td->mutex\n");
1072 fio_mutex_down(td->mutex);
1073 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1074
1075 /*
1076 * the ->mutex mutex is now no longer used, close it to avoid
1077 * eating a file descriptor
1078 */
1079 fio_mutex_remove(td->mutex);
1080
1081 /*
1082 * A new gid requires privilege, so we need to do this before setting
1083 * the uid.
1084 */
1085 if (td->o.gid != -1U && setgid(td->o.gid)) {
1086 td_verror(td, errno, "setgid");
1087 goto err;
1088 }
1089 if (td->o.uid != -1U && setuid(td->o.uid)) {
1090 td_verror(td, errno, "setuid");
1091 goto err;
1092 }
1093
1094 /*
1095 * If we have a gettimeofday() thread, make sure we exclude that
1096 * thread from this job
1097 */
1098 if (td->o.gtod_cpu)
1099 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1100
1101 /*
1102 * Set affinity first, in case it has an impact on the memory
1103 * allocations.
1104 */
1105 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1106 td_verror(td, errno, "cpu_set_affinity");
1107 goto err;
1108 }
1109
67bf9823 1110#ifdef CONFIG_LIBNUMA
d0b937ed
YR
1111 /* numa node setup */
1112 if (td->o.numa_cpumask_set || td->o.numa_memmask_set) {
1113 int ret;
1114
1115 if (numa_available() < 0) {
1116 td_verror(td, errno, "Does not support NUMA API\n");
1117 goto err;
1118 }
1119
1120 if (td->o.numa_cpumask_set) {
1121 ret = numa_run_on_node_mask(td->o.numa_cpunodesmask);
1122 if (ret == -1) {
1123 td_verror(td, errno, \
1124 "numa_run_on_node_mask failed\n");
1125 goto err;
1126 }
1127 }
1128
1129 if (td->o.numa_memmask_set) {
1130
1131 switch (td->o.numa_mem_mode) {
1132 case MPOL_INTERLEAVE:
1133 numa_set_interleave_mask(td->o.numa_memnodesmask);
1134 break;
1135 case MPOL_BIND:
1136 numa_set_membind(td->o.numa_memnodesmask);
1137 break;
1138 case MPOL_LOCAL:
1139 numa_set_localalloc();
1140 break;
1141 case MPOL_PREFERRED:
1142 numa_set_preferred(td->o.numa_mem_prefer_node);
1143 break;
1144 case MPOL_DEFAULT:
1145 default:
1146 break;
1147 }
1148
1149 }
1150 }
1151#endif
1152
2e1df07d
JA
1153 /*
1154 * May alter parameters that init_io_u() will use, so we need to
1155 * do this first.
1156 */
1157 if (init_iolog(td))
1158 goto err;
1159
1160 if (init_io_u(td))
1161 goto err;
1162
1163 if (td->o.verify_async && verify_async_init(td))
1164 goto err;
1165
1166 if (td->ioprio_set) {
1167 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1168 td_verror(td, errno, "ioprio_set");
1169 goto err;
1170 }
1171 }
1172
5d89ff79 1173 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
2e1df07d
JA
1174 goto err;
1175
649c10c5
BC
1176 errno = 0;
1177 if (nice(td->o.nice) == -1 && errno != 0) {
2e1df07d
JA
1178 td_verror(td, errno, "nice");
1179 goto err;
1180 }
1181
1182 if (td->o.ioscheduler && switch_ioscheduler(td))
1183 goto err;
1184
1185 if (!td->o.create_serialize && setup_files(td))
1186 goto err;
1187
1188 if (td_io_init(td))
1189 goto err;
1190
1191 if (init_random_map(td))
1192 goto err;
1193
1194 if (td->o.exec_prerun) {
1195 if (exec_string(td->o.exec_prerun))
1196 goto err;
1197 }
1198
1199 if (td->o.pre_read) {
1200 if (pre_read_files(td) < 0)
1201 goto err;
1202 }
1203
1204 fio_gettime(&td->epoch, NULL);
1205 getrusage(RUSAGE_SELF, &td->ru_start);
1206
1207 clear_state = 0;
1208 while (keep_running(td)) {
1209 fio_gettime(&td->start, NULL);
1210 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1211 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1212 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1213
6eaf09d6
SL
1214 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1215 td->o.ratemin[DDIR_TRIM]) {
1216 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
2e1df07d 1217 sizeof(td->bw_sample_time));
6eaf09d6
SL
1218 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1219 sizeof(td->bw_sample_time));
1220 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
2e1df07d
JA
1221 sizeof(td->bw_sample_time));
1222 }
1223
1224 if (clear_state)
1225 clear_io_state(td);
1226
1227 prune_io_piece_log(td);
1228
1229 do_io(td);
1230
1231 clear_state = 1;
1232
1233 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1234 elapsed = utime_since_now(&td->start);
1235 td->ts.runtime[DDIR_READ] += elapsed;
1236 }
1237 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1238 elapsed = utime_since_now(&td->start);
1239 td->ts.runtime[DDIR_WRITE] += elapsed;
1240 }
6eaf09d6
SL
1241 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1242 elapsed = utime_since_now(&td->start);
1243 td->ts.runtime[DDIR_TRIM] += elapsed;
1244 }
2e1df07d
JA
1245
1246 if (td->error || td->terminate)
1247 break;
1248
1249 if (!td->o.do_verify ||
1250 td->o.verify == VERIFY_NONE ||
1251 (td->io_ops->flags & FIO_UNIDIR))
1252 continue;
1253
1254 clear_io_state(td);
1255
1256 fio_gettime(&td->start, NULL);
1257
bcd5abfa 1258 do_verify(td);
2e1df07d
JA
1259
1260 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1261
1262 if (td->error || td->terminate)
1263 break;
1264 }
1265
1266 update_rusage_stat(td);
6eaf09d6
SL
1267 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1268 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1269 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
2e1df07d 1270 td->ts.total_run_time = mtime_since_now(&td->epoch);
6eaf09d6
SL
1271 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1272 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1273 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
2e1df07d
JA
1274
1275 fio_mutex_down(writeout_mutex);
1276 if (td->bw_log) {
1277 if (td->o.bw_log_file) {
1278 finish_log_named(td, td->bw_log,
1279 td->o.bw_log_file, "bw");
1280 } else
1281 finish_log(td, td->bw_log, "bw");
1282 }
1283 if (td->lat_log) {
1284 if (td->o.lat_log_file) {
1285 finish_log_named(td, td->lat_log,
1286 td->o.lat_log_file, "lat");
1287 } else
1288 finish_log(td, td->lat_log, "lat");
1289 }
1290 if (td->slat_log) {
1291 if (td->o.lat_log_file) {
1292 finish_log_named(td, td->slat_log,
1293 td->o.lat_log_file, "slat");
1294 } else
1295 finish_log(td, td->slat_log, "slat");
1296 }
1297 if (td->clat_log) {
1298 if (td->o.lat_log_file) {
1299 finish_log_named(td, td->clat_log,
1300 td->o.lat_log_file, "clat");
1301 } else
1302 finish_log(td, td->clat_log, "clat");
1303 }
1304 if (td->iops_log) {
1305 if (td->o.iops_log_file) {
1306 finish_log_named(td, td->iops_log,
1307 td->o.iops_log_file, "iops");
1308 } else
1309 finish_log(td, td->iops_log, "iops");
1310 }
1311
1312 fio_mutex_up(writeout_mutex);
1313 if (td->o.exec_postrun)
1314 exec_string(td->o.exec_postrun);
1315
1316 if (exitall_on_terminate)
1317 fio_terminate_threads(td->groupid);
1318
1319err:
1320 if (td->error)
1321 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1322 td->verror);
1323
1324 if (td->o.verify_async)
1325 verify_async_exit(td);
1326
1327 close_and_free_files(td);
2e1df07d 1328 cleanup_io_u(td);
32dbca2c 1329 close_ioengine(td);
2e1df07d
JA
1330 cgroup_shutdown(td, &cgroup_mnt);
1331
1332 if (td->o.cpumask_set) {
1333 int ret = fio_cpuset_exit(&td->o.cpumask);
1334
1335 td_verror(td, ret, "fio_cpuset_exit");
1336 }
1337
1338 /*
1339 * do this very late, it will log file closing as well
1340 */
1341 if (td->o.write_iolog_file)
1342 write_iolog_close(td);
1343
1344 td_set_runstate(td, TD_EXITED);
e43606c2 1345 return (void *) (uintptr_t) td->error;
2e1df07d
JA
1346}
1347
1348
1349/*
1350 * We cannot pass the td data into a forked process, so attach the td and
1351 * pass it to the thread worker.
1352 */
1353static int fork_main(int shmid, int offset)
1354{
1355 struct thread_data *td;
1356 void *data, *ret;
1357
1358#ifndef __hpux
1359 data = shmat(shmid, NULL, 0);
1360 if (data == (void *) -1) {
1361 int __err = errno;
1362
1363 perror("shmat");
1364 return __err;
1365 }
1366#else
1367 /*
1368 * HP-UX inherits shm mappings?
1369 */
1370 data = threads;
1371#endif
1372
1373 td = data + offset * sizeof(struct thread_data);
1374 ret = thread_main(td);
1375 shmdt(data);
e43606c2 1376 return (int) (uintptr_t) ret;
2e1df07d
JA
1377}
1378
1379/*
1380 * Run over the job map and reap the threads that have exited, if any.
1381 */
1382static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1383 unsigned int *m_rate)
1384{
1385 struct thread_data *td;
1386 unsigned int cputhreads, realthreads, pending;
1387 int i, status, ret;
1388
1389 /*
1390 * reap exited threads (TD_EXITED -> TD_REAPED)
1391 */
1392 realthreads = pending = cputhreads = 0;
1393 for_each_td(td, i) {
1394 int flags = 0;
1395
1396 /*
1397 * ->io_ops is NULL for a thread that has closed its
1398 * io engine
1399 */
1400 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1401 cputhreads++;
1402 else
1403 realthreads++;
1404
1405 if (!td->pid) {
1406 pending++;
1407 continue;
1408 }
1409 if (td->runstate == TD_REAPED)
1410 continue;
1411 if (td->o.use_thread) {
1412 if (td->runstate == TD_EXITED) {
1413 td_set_runstate(td, TD_REAPED);
1414 goto reaped;
1415 }
1416 continue;
1417 }
1418
1419 flags = WNOHANG;
1420 if (td->runstate == TD_EXITED)
1421 flags = 0;
1422
1423 /*
1424 * check if someone quit or got killed in an unusual way
1425 */
1426 ret = waitpid(td->pid, &status, flags);
1427 if (ret < 0) {
1428 if (errno == ECHILD) {
1429 log_err("fio: pid=%d disappeared %d\n",
1430 (int) td->pid, td->runstate);
a5e371a6 1431 td->sig = ECHILD;
2e1df07d
JA
1432 td_set_runstate(td, TD_REAPED);
1433 goto reaped;
1434 }
1435 perror("waitpid");
1436 } else if (ret == td->pid) {
1437 if (WIFSIGNALED(status)) {
1438 int sig = WTERMSIG(status);
1439
36d80bc7 1440 if (sig != SIGTERM && sig != SIGUSR2)
2e1df07d
JA
1441 log_err("fio: pid=%d, got signal=%d\n",
1442 (int) td->pid, sig);
a5e371a6 1443 td->sig = sig;
2e1df07d
JA
1444 td_set_runstate(td, TD_REAPED);
1445 goto reaped;
1446 }
1447 if (WIFEXITED(status)) {
1448 if (WEXITSTATUS(status) && !td->error)
1449 td->error = WEXITSTATUS(status);
1450
1451 td_set_runstate(td, TD_REAPED);
1452 goto reaped;
1453 }
1454 }
1455
1456 /*
1457 * thread is not dead, continue
1458 */
1459 pending++;
1460 continue;
1461reaped:
1462 (*nr_running)--;
342f4be4
JA
1463 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1464 (*t_rate) -= ddir_rw_sum(td->o.rate);
2e1df07d
JA
1465 if (!td->pid)
1466 pending--;
1467
1468 if (td->error)
1469 exit_value++;
1470
1471 done_secs += mtime_since_now(&td->epoch) / 1000;
1472 }
1473
1474 if (*nr_running == cputhreads && !pending && realthreads)
1475 fio_terminate_threads(TERMINATE_ALL);
1476}
1477
2e1df07d
JA
1478/*
1479 * Main function for kicking off and reaping jobs, as needed.
1480 */
1481static void run_threads(void)
1482{
1483 struct thread_data *td;
1484 unsigned long spent;
1485 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1486
1487 if (fio_pin_memory())
1488 return;
1489
1490 if (fio_gtod_offload && fio_start_gtod_thread())
1491 return;
1492
1493 set_sig_handlers();
1494
f3afa57e 1495 if (output_format == FIO_OUTPUT_NORMAL) {
2e1df07d
JA
1496 log_info("Starting ");
1497 if (nr_thread)
1498 log_info("%d thread%s", nr_thread,
1499 nr_thread > 1 ? "s" : "");
1500 if (nr_process) {
1501 if (nr_thread)
1502 log_info(" and ");
1503 log_info("%d process%s", nr_process,
1504 nr_process > 1 ? "es" : "");
1505 }
1506 log_info("\n");
1507 fflush(stdout);
1508 }
1509
1510 todo = thread_number;
1511 nr_running = 0;
1512 nr_started = 0;
1513 m_rate = t_rate = 0;
1514
1515 for_each_td(td, i) {
1516 print_status_init(td->thread_number - 1);
1517
1518 if (!td->o.create_serialize)
1519 continue;
1520
1521 /*
1522 * do file setup here so it happens sequentially,
1523 * we don't want X number of threads getting their
1524 * client data interspersed on disk
1525 */
1526 if (setup_files(td)) {
1527 exit_value++;
1528 if (td->error)
1529 log_err("fio: pid=%d, err=%d/%s\n",
1530 (int) td->pid, td->error, td->verror);
1531 td_set_runstate(td, TD_REAPED);
1532 todo--;
1533 } else {
1534 struct fio_file *f;
1535 unsigned int j;
1536
1537 /*
1538 * for sharing to work, each job must always open
1539 * its own files. so close them, if we opened them
1540 * for creation
1541 */
1542 for_each_file(td, f, j) {
1543 if (fio_file_open(f))
1544 td_io_close_file(td, f);
1545 }
1546 }
1547 }
1548
1549 set_genesis_time();
1550
1551 while (todo) {
1552 struct thread_data *map[REAL_MAX_JOBS];
1553 struct timeval this_start;
1554 int this_jobs = 0, left;
1555
1556 /*
1557 * create threads (TD_NOT_CREATED -> TD_CREATED)
1558 */
1559 for_each_td(td, i) {
1560 if (td->runstate != TD_NOT_CREATED)
1561 continue;
1562
1563 /*
1564 * never got a chance to start, killed by other
1565 * thread for some reason
1566 */
1567 if (td->terminate) {
1568 todo--;
1569 continue;
1570 }
1571
1572 if (td->o.start_delay) {
1573 spent = mtime_since_genesis();
1574
1575 if (td->o.start_delay * 1000 > spent)
1576 continue;
1577 }
1578
1579 if (td->o.stonewall && (nr_started || nr_running)) {
1580 dprint(FD_PROCESS, "%s: stonewall wait\n",
1581 td->o.name);
1582 break;
1583 }
1584
1585 init_disk_util(td);
1586
1587 /*
1588 * Set state to created. Thread will transition
1589 * to TD_INITIALIZED when it's done setting up.
1590 */
1591 td_set_runstate(td, TD_CREATED);
1592 map[this_jobs++] = td;
1593 nr_started++;
1594
1595 if (td->o.use_thread) {
1596 int ret;
1597
1598 dprint(FD_PROCESS, "will pthread_create\n");
1599 ret = pthread_create(&td->thread, NULL,
1600 thread_main, td);
1601 if (ret) {
1602 log_err("pthread_create: %s\n",
1603 strerror(ret));
1604 nr_started--;
1605 break;
1606 }
1607 ret = pthread_detach(td->thread);
1608 if (ret)
1609 log_err("pthread_detach: %s",
1610 strerror(ret));
1611 } else {
1612 pid_t pid;
1613 dprint(FD_PROCESS, "will fork\n");
1614 pid = fork();
1615 if (!pid) {
1616 int ret = fork_main(shm_id, i);
1617
1618 _exit(ret);
1619 } else if (i == fio_debug_jobno)
1620 *fio_debug_jobp = pid;
1621 }
1622 dprint(FD_MUTEX, "wait on startup_mutex\n");
1623 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1624 log_err("fio: job startup hung? exiting.\n");
1625 fio_terminate_threads(TERMINATE_ALL);
1626 fio_abort = 1;
1627 nr_started--;
1628 break;
1629 }
1630 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1631 }
1632
1633 /*
1634 * Wait for the started threads to transition to
1635 * TD_INITIALIZED.
1636 */
1637 fio_gettime(&this_start, NULL);
1638 left = this_jobs;
1639 while (left && !fio_abort) {
1640 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1641 break;
1642
1643 usleep(100000);
1644
1645 for (i = 0; i < this_jobs; i++) {
1646 td = map[i];
1647 if (!td)
1648 continue;
1649 if (td->runstate == TD_INITIALIZED) {
1650 map[i] = NULL;
1651 left--;
1652 } else if (td->runstate >= TD_EXITED) {
1653 map[i] = NULL;
1654 left--;
1655 todo--;
1656 nr_running++; /* work-around... */
1657 }
1658 }
1659 }
1660
1661 if (left) {
4e87c37a
JA
1662 log_err("fio: %d job%s failed to start\n", left,
1663 left > 1 ? "s" : "");
2e1df07d
JA
1664 for (i = 0; i < this_jobs; i++) {
1665 td = map[i];
1666 if (!td)
1667 continue;
1668 kill(td->pid, SIGTERM);
1669 }
1670 break;
1671 }
1672
1673 /*
1674 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1675 */
1676 for_each_td(td, i) {
1677 if (td->runstate != TD_INITIALIZED)
1678 continue;
1679
1680 if (in_ramp_time(td))
1681 td_set_runstate(td, TD_RAMP);
1682 else
1683 td_set_runstate(td, TD_RUNNING);
1684 nr_running++;
1685 nr_started--;
342f4be4
JA
1686 m_rate += ddir_rw_sum(td->o.ratemin);
1687 t_rate += ddir_rw_sum(td->o.rate);
2e1df07d
JA
1688 todo--;
1689 fio_mutex_up(td->mutex);
1690 }
1691
1692 reap_threads(&nr_running, &t_rate, &m_rate);
1693
1694 if (todo) {
1695 if (is_backend)
1696 fio_server_idle_loop();
1697 else
1698 usleep(100000);
1699 }
1700 }
1701
1702 while (nr_running) {
1703 reap_threads(&nr_running, &t_rate, &m_rate);
1704
1705 if (is_backend)
1706 fio_server_idle_loop();
1707 else
1708 usleep(10000);
1709 }
1710
1711 update_io_ticks();
1712 fio_unpin_memory();
1713}
1714
9ec7779f
JA
1715void wait_for_disk_thread_exit(void)
1716{
1717 fio_mutex_down(disk_thread_mutex);
1718}
1719
27357187
JA
1720static void free_disk_util(void)
1721{
1722 disk_util_start_exit();
1723 wait_for_disk_thread_exit();
1724 disk_util_prune_entries();
1725}
1726
2e1df07d
JA
1727static void *disk_thread_main(void *data)
1728{
9ec7779f
JA
1729 int ret = 0;
1730
2e1df07d
JA
1731 fio_mutex_up(startup_mutex);
1732
9ec7779f 1733 while (threads && !ret) {
2e1df07d
JA
1734 usleep(DISK_UTIL_MSEC * 1000);
1735 if (!threads)
1736 break;
9ec7779f 1737 ret = update_io_ticks();
2e1df07d
JA
1738
1739 if (!is_backend)
1740 print_thread_status();
1741 }
1742
9ec7779f 1743 fio_mutex_up(disk_thread_mutex);
2e1df07d
JA
1744 return NULL;
1745}
1746
1747static int create_disk_util_thread(void)
1748{
1749 int ret;
1750
9ec7779f
JA
1751 setup_disk_util();
1752
521da527 1753 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
9ec7779f 1754
2e1df07d
JA
1755 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1756 if (ret) {
9ec7779f 1757 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1758 log_err("Can't create disk util thread: %s\n", strerror(ret));
1759 return 1;
1760 }
1761
1762 ret = pthread_detach(disk_util_thread);
1763 if (ret) {
9ec7779f 1764 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1765 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1766 return 1;
1767 }
1768
1769 dprint(FD_MUTEX, "wait on startup_mutex\n");
1770 fio_mutex_down(startup_mutex);
1771 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1772 return 0;
1773}
1774
2e1df07d
JA
1775int fio_backend(void)
1776{
1777 struct thread_data *td;
1778 int i;
1779
1780 if (exec_profile) {
1781 if (load_profile(exec_profile))
1782 return 1;
1783 free(exec_profile);
1784 exec_profile = NULL;
1785 }
1786 if (!thread_number)
1787 return 0;
1788
1789 if (write_bw_log) {
1790 setup_log(&agg_io_log[DDIR_READ], 0);
1791 setup_log(&agg_io_log[DDIR_WRITE], 0);
6eaf09d6 1792 setup_log(&agg_io_log[DDIR_TRIM], 0);
2e1df07d
JA
1793 }
1794
521da527 1795 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2e1df07d
JA
1796 if (startup_mutex == NULL)
1797 return 1;
521da527 1798 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
2e1df07d
JA
1799 if (writeout_mutex == NULL)
1800 return 1;
1801
1802 set_genesis_time();
1803 create_disk_util_thread();
1804
1805 cgroup_list = smalloc(sizeof(*cgroup_list));
1806 INIT_FLIST_HEAD(cgroup_list);
1807
1808 run_threads();
1809
1810 if (!fio_abort) {
1811 show_run_stats();
1812 if (write_bw_log) {
1813 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1814 __finish_log(agg_io_log[DDIR_WRITE],
1815 "agg-write_bw.log");
6eaf09d6
SL
1816 __finish_log(agg_io_log[DDIR_TRIM],
1817 "agg-write_bw.log");
2e1df07d
JA
1818 }
1819 }
1820
1821 for_each_td(td, i)
1822 fio_options_free(td);
1823
a462baef 1824 free_disk_util();
2e1df07d
JA
1825 cgroup_kill(cgroup_list);
1826 sfree(cgroup_list);
1827 sfree(cgroup_mnt);
1828
1829 fio_mutex_remove(startup_mutex);
1830 fio_mutex_remove(writeout_mutex);
9ec7779f 1831 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1832 return exit_value;
1833}