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