eta: improve accuracy for rwmix workloads and verify
[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 */
44cbc6da 396static void do_verify(struct thread_data *td, uint64_t verify_bytes)
2e1df07d
JA
397{
398 struct fio_file *f;
399 struct io_u *io_u;
400 int ret, min_events;
44cbc6da 401 uint64_t io_bytes;
2e1df07d
JA
402 unsigned int i;
403
404 dprint(FD_VERIFY, "starting loop\n");
405
406 /*
407 * sync io first and invalidate cache, to make sure we really
408 * read from disk.
409 */
410 for_each_file(td, f, i) {
411 if (!fio_file_open(f))
412 continue;
413 if (fio_io_sync(td, f))
414 break;
415 if (file_invalidate_cache(td, f))
416 break;
417 }
418
419 if (td->error)
420 return;
421
422 td_set_runstate(td, TD_VERIFYING);
423
424 io_u = NULL;
44cbc6da 425 io_bytes = 0;
2e1df07d 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 {
458 io_u = get_io_u(td);
459 if (!io_u)
460 break;
461
462 if (io_u->buflen + io_bytes > verify_bytes)
463 break;
2e1df07d
JA
464 }
465
466 if (td->o.verify_async)
467 io_u->end_io = verify_io_u_async;
468 else
469 io_u->end_io = verify_io_u;
470
fbccf46c
JA
471 ddir = io_u->ddir;
472
2e1df07d
JA
473 ret = td_io_queue(td, io_u);
474 switch (ret) {
475 case FIO_Q_COMPLETED:
476 if (io_u->error) {
477 ret = -io_u->error;
478 clear_io_u(td, io_u);
479 } else if (io_u->resid) {
480 int bytes = io_u->xfer_buflen - io_u->resid;
481
482 /*
483 * zero read, fail
484 */
485 if (!bytes) {
486 td_verror(td, EIO, "full resid");
487 put_io_u(td, io_u);
488 break;
489 }
490
491 io_u->xfer_buflen = io_u->resid;
492 io_u->xfer_buf += bytes;
493 io_u->offset += bytes;
44cbc6da 494 io_bytes += bytes;
2e1df07d
JA
495
496 if (ddir_rw(io_u->ddir))
497 td->ts.short_io_u[io_u->ddir]++;
498
499 f = io_u->file;
500 if (io_u->offset == f->real_file_size)
501 goto sync_done;
502
503 requeue_io_u(td, &io_u);
504 } else {
505sync_done:
506 ret = io_u_sync_complete(td, io_u, NULL);
507 if (ret < 0)
508 break;
509 }
44cbc6da 510 io_bytes += io_u->xfer_buflen;
2e1df07d
JA
511 continue;
512 case FIO_Q_QUEUED:
513 break;
514 case FIO_Q_BUSY:
515 requeue_io_u(td, &io_u);
516 ret2 = td_io_commit(td);
517 if (ret2 < 0)
518 ret = ret2;
519 break;
520 default:
521 assert(ret < 0);
522 td_verror(td, -ret, "td_io_queue");
523 break;
524 }
525
fbccf46c 526 if (break_on_this_error(td, ddir, &ret))
2e1df07d
JA
527 break;
528
529 /*
530 * if we can queue more, do so. but check if there are
531 * completed io_u's first. Note that we can get BUSY even
532 * without IO queued, if the system is resource starved.
533 */
534 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
535 if (full || !td->o.iodepth_batch_complete) {
536 min_events = min(td->o.iodepth_batch_complete,
537 td->cur_depth);
8a74b56d
JA
538 /*
539 * if the queue is full, we MUST reap at least 1 event
540 */
541 if (full && !min_events)
2e1df07d
JA
542 min_events = 1;
543
544 do {
44cbc6da
JA
545 unsigned long bytes = 0;
546
2e1df07d
JA
547 /*
548 * Reap required number of io units, if any,
549 * and do the verification on them through
550 * the callback handler
551 */
44cbc6da 552 if (io_u_queued_complete(td, min_events, &bytes) < 0) {
2e1df07d
JA
553 ret = -1;
554 break;
555 }
44cbc6da 556 io_bytes += bytes;
2e1df07d
JA
557 } while (full && (td->cur_depth > td->o.iodepth_low));
558 }
559 if (ret < 0)
560 break;
561 }
562
563 if (!td->error) {
564 min_events = td->cur_depth;
565
566 if (min_events)
567 ret = io_u_queued_complete(td, min_events, NULL);
568 } else
569 cleanup_pending_aio(td);
570
571 td_set_runstate(td, TD_RUNNING);
572
573 dprint(FD_VERIFY, "exiting loop\n");
574}
575
f7078f7b
JA
576static int io_bytes_exceeded(struct thread_data *td)
577{
578 unsigned long long bytes;
579
580 if (td_rw(td))
6eaf09d6 581 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
f7078f7b 582 else if (td_write(td))
6eaf09d6
SL
583 bytes = td->this_io_bytes[DDIR_WRITE];
584 else if (td_read(td))
585 bytes = td->this_io_bytes[DDIR_READ];
f7078f7b 586 else
6eaf09d6 587 bytes = td->this_io_bytes[DDIR_TRIM];
f7078f7b
JA
588
589 return bytes >= td->o.size;
590}
591
2e1df07d
JA
592/*
593 * Main IO worker function. It retrieves io_u's to process and queues
594 * and reaps them, checking for rate and errors along the way.
595 */
596static void do_io(struct thread_data *td)
597{
598 unsigned int i;
599 int ret = 0;
600
601 if (in_ramp_time(td))
602 td_set_runstate(td, TD_RAMP);
603 else
604 td_set_runstate(td, TD_RUNNING);
605
f7078f7b 606 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
c04e4661
DE
607 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
608 td->o.time_based) {
2e1df07d 609 struct timeval comp_time;
6eaf09d6 610 unsigned long bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
2e1df07d
JA
611 int min_evts = 0;
612 struct io_u *io_u;
613 int ret2, full;
614 enum fio_ddir ddir;
615
7d7803fa 616 if (td->terminate || td->done)
2e1df07d
JA
617 break;
618
619 update_tv_cache(td);
620
621 if (runtime_exceeded(td, &td->tv_cache)) {
622 __update_tv_cache(td);
623 if (runtime_exceeded(td, &td->tv_cache)) {
624 td->terminate = 1;
625 break;
626 }
627 }
628
9e684a49
DE
629 if (flow_threshold_exceeded(td))
630 continue;
631
2e1df07d
JA
632 io_u = get_io_u(td);
633 if (!io_u)
634 break;
635
636 ddir = io_u->ddir;
637
638 /*
82af2a7c
JA
639 * Add verification end_io handler if:
640 * - Asked to verify (!td_rw(td))
641 * - Or the io_u is from our verify list (mixed write/ver)
2e1df07d
JA
642 */
643 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
82af2a7c 644 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
2e1df07d
JA
645 if (td->o.verify_async)
646 io_u->end_io = verify_io_u_async;
647 else
648 io_u->end_io = verify_io_u;
649 td_set_runstate(td, TD_VERIFYING);
650 } else if (in_ramp_time(td))
651 td_set_runstate(td, TD_RAMP);
652 else
653 td_set_runstate(td, TD_RUNNING);
654
655 ret = td_io_queue(td, io_u);
656 switch (ret) {
657 case FIO_Q_COMPLETED:
658 if (io_u->error) {
659 ret = -io_u->error;
660 clear_io_u(td, io_u);
661 } else if (io_u->resid) {
662 int bytes = io_u->xfer_buflen - io_u->resid;
663 struct fio_file *f = io_u->file;
664
665 /*
666 * zero read, fail
667 */
668 if (!bytes) {
669 td_verror(td, EIO, "full resid");
670 put_io_u(td, io_u);
671 break;
672 }
673
674 io_u->xfer_buflen = io_u->resid;
675 io_u->xfer_buf += bytes;
676 io_u->offset += bytes;
677
678 if (ddir_rw(io_u->ddir))
679 td->ts.short_io_u[io_u->ddir]++;
680
681 if (io_u->offset == f->real_file_size)
682 goto sync_done;
683
684 requeue_io_u(td, &io_u);
685 } else {
686sync_done:
6eaf09d6
SL
687 if (__should_check_rate(td, DDIR_READ) ||
688 __should_check_rate(td, DDIR_WRITE) ||
689 __should_check_rate(td, DDIR_TRIM))
2e1df07d
JA
690 fio_gettime(&comp_time, NULL);
691
692 ret = io_u_sync_complete(td, io_u, bytes_done);
693 if (ret < 0)
694 break;
695 }
696 break;
697 case FIO_Q_QUEUED:
698 /*
699 * if the engine doesn't have a commit hook,
700 * the io_u is really queued. if it does have such
701 * a hook, it has to call io_u_queued() itself.
702 */
703 if (td->io_ops->commit == NULL)
704 io_u_queued(td, io_u);
705 break;
706 case FIO_Q_BUSY:
707 requeue_io_u(td, &io_u);
708 ret2 = td_io_commit(td);
709 if (ret2 < 0)
710 ret = ret2;
711 break;
712 default:
713 assert(ret < 0);
714 put_io_u(td, io_u);
715 break;
716 }
717
718 if (break_on_this_error(td, ddir, &ret))
719 break;
720
721 /*
722 * See if we need to complete some commands. Note that we
723 * can get BUSY even without IO queued, if the system is
724 * resource starved.
725 */
726 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
727 if (full || !td->o.iodepth_batch_complete) {
728 min_evts = min(td->o.iodepth_batch_complete,
729 td->cur_depth);
8a74b56d
JA
730 /*
731 * if the queue is full, we MUST reap at least 1 event
732 */
733 if (full && !min_evts)
2e1df07d
JA
734 min_evts = 1;
735
6eaf09d6
SL
736 if (__should_check_rate(td, DDIR_READ) ||
737 __should_check_rate(td, DDIR_WRITE) ||
738 __should_check_rate(td, DDIR_TRIM))
2e1df07d
JA
739 fio_gettime(&comp_time, NULL);
740
741 do {
742 ret = io_u_queued_complete(td, min_evts, bytes_done);
743 if (ret < 0)
744 break;
745
746 } while (full && (td->cur_depth > td->o.iodepth_low));
747 }
748
749 if (ret < 0)
750 break;
d5abee06 751 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
2e1df07d
JA
752 continue;
753
754 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
755 if (check_min_rate(td, &comp_time, bytes_done)) {
756 if (exitall_on_terminate)
757 fio_terminate_threads(td->groupid);
758 td_verror(td, EIO, "check_min_rate");
759 break;
760 }
761 }
762
763 if (td->o.thinktime) {
764 unsigned long long b;
765
342f4be4 766 b = ddir_rw_sum(td->io_blocks);
2e1df07d
JA
767 if (!(b % td->o.thinktime_blocks)) {
768 int left;
769
770 if (td->o.thinktime_spin)
771 usec_spin(td->o.thinktime_spin);
772
773 left = td->o.thinktime - td->o.thinktime_spin;
774 if (left)
775 usec_sleep(td, left);
776 }
777 }
778 }
779
780 if (td->trim_entries)
781 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
782
783 if (td->o.fill_device && td->error == ENOSPC) {
784 td->error = 0;
785 td->terminate = 1;
786 }
787 if (!td->error) {
788 struct fio_file *f;
789
790 i = td->cur_depth;
791 if (i) {
792 ret = io_u_queued_complete(td, i, NULL);
793 if (td->o.fill_device && td->error == ENOSPC)
794 td->error = 0;
795 }
796
797 if (should_fsync(td) && td->o.end_fsync) {
798 td_set_runstate(td, TD_FSYNCING);
799
800 for_each_file(td, f, i) {
801 if (!fio_file_open(f))
802 continue;
803 fio_io_sync(td, f);
804 }
805 }
806 } else
807 cleanup_pending_aio(td);
808
809 /*
810 * stop job if we failed doing any IO
811 */
342f4be4 812 if (!ddir_rw_sum(td->this_io_bytes))
2e1df07d
JA
813 td->done = 1;
814}
815
816static void cleanup_io_u(struct thread_data *td)
817{
818 struct flist_head *entry, *n;
819 struct io_u *io_u;
820
821 flist_for_each_safe(entry, n, &td->io_u_freelist) {
822 io_u = flist_entry(entry, struct io_u, list);
823
824 flist_del(&io_u->list);
c73ed246
JA
825
826 if (td->io_ops->io_u_free)
827 td->io_ops->io_u_free(td, io_u);
828
2e1df07d
JA
829 fio_memfree(io_u, sizeof(*io_u));
830 }
831
832 free_io_mem(td);
833}
834
835static int init_io_u(struct thread_data *td)
836{
837 struct io_u *io_u;
9c42684e 838 unsigned int max_bs, min_write;
2e1df07d 839 int cl_align, i, max_units;
59d8d0f5 840 int data_xfer = 1;
2e1df07d
JA
841 char *p;
842
843 max_units = td->o.iodepth;
844 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
6eaf09d6 845 max_bs = max(td->o.max_bs[DDIR_TRIM], max_bs);
9c42684e 846 min_write = td->o.min_bs[DDIR_WRITE];
2e1df07d
JA
847 td->orig_buffer_size = (unsigned long long) max_bs
848 * (unsigned long long) max_units;
849
88045e04 850 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
59d8d0f5
JA
851 data_xfer = 0;
852
2e1df07d
JA
853 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
854 unsigned long bs;
855
856 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
857 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
858 }
859
860 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
861 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
862 return 1;
863 }
864
59d8d0f5 865 if (data_xfer && allocate_io_mem(td))
2e1df07d
JA
866 return 1;
867
868 if (td->o.odirect || td->o.mem_align ||
869 (td->io_ops->flags & FIO_RAWIO))
870 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
871 else
872 p = td->orig_buffer;
873
874 cl_align = os_cache_line_size();
875
876 for (i = 0; i < max_units; i++) {
877 void *ptr;
878
879 if (td->terminate)
880 return 1;
881
882 ptr = fio_memalign(cl_align, sizeof(*io_u));
883 if (!ptr) {
884 log_err("fio: unable to allocate aligned memory\n");
885 break;
886 }
887
888 io_u = ptr;
889 memset(io_u, 0, sizeof(*io_u));
890 INIT_FLIST_HEAD(&io_u->list);
891 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
892
59d8d0f5 893 if (data_xfer) {
2e1df07d
JA
894 io_u->buf = p;
895 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
896
897 if (td_write(td))
9c42684e 898 io_u_fill_buffer(td, io_u, min_write, max_bs);
2e1df07d
JA
899 if (td_write(td) && td->o.verify_pattern_bytes) {
900 /*
901 * Fill the buffer with the pattern if we are
902 * going to be doing writes.
903 */
904 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
905 }
906 }
907
908 io_u->index = i;
909 io_u->flags = IO_U_F_FREE;
910 flist_add(&io_u->list, &td->io_u_freelist);
c73ed246
JA
911
912 if (td->io_ops->io_u_init) {
913 int ret = td->io_ops->io_u_init(td, io_u);
914
915 if (ret) {
916 log_err("fio: failed to init engine data: %d\n", ret);
917 return 1;
918 }
919 }
920
2e1df07d
JA
921 p += max_bs;
922 }
923
924 return 0;
925}
926
927static int switch_ioscheduler(struct thread_data *td)
928{
929 char tmp[256], tmp2[128];
930 FILE *f;
931 int ret;
932
933 if (td->io_ops->flags & FIO_DISKLESSIO)
934 return 0;
935
936 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
937
938 f = fopen(tmp, "r+");
939 if (!f) {
940 if (errno == ENOENT) {
941 log_err("fio: os or kernel doesn't support IO scheduler"
942 " switching\n");
943 return 0;
944 }
945 td_verror(td, errno, "fopen iosched");
946 return 1;
947 }
948
949 /*
950 * Set io scheduler.
951 */
952 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
953 if (ferror(f) || ret != 1) {
954 td_verror(td, errno, "fwrite");
955 fclose(f);
956 return 1;
957 }
958
959 rewind(f);
960
961 /*
962 * Read back and check that the selected scheduler is now the default.
963 */
964 ret = fread(tmp, 1, sizeof(tmp), f);
965 if (ferror(f) || ret < 0) {
966 td_verror(td, errno, "fread");
967 fclose(f);
968 return 1;
969 }
970
971 sprintf(tmp2, "[%s]", td->o.ioscheduler);
972 if (!strstr(tmp, tmp2)) {
973 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
974 td_verror(td, EINVAL, "iosched_switch");
975 fclose(f);
976 return 1;
977 }
978
979 fclose(f);
980 return 0;
981}
982
983static int keep_running(struct thread_data *td)
984{
2e1df07d
JA
985 if (td->done)
986 return 0;
987 if (td->o.time_based)
988 return 1;
989 if (td->o.loops) {
990 td->o.loops--;
991 return 1;
992 }
993
342f4be4 994 if (ddir_rw_sum(td->io_bytes) < td->o.size)
2e1df07d
JA
995 return 1;
996
997 return 0;
998}
999
1000static int exec_string(const char *string)
1001{
1002 int ret, newlen = strlen(string) + 1 + 8;
1003 char *str;
1004
1005 str = malloc(newlen);
1006 sprintf(str, "sh -c %s", string);
1007
1008 ret = system(str);
1009 if (ret == -1)
1010 log_err("fio: exec of cmd <%s> failed\n", str);
1011
1012 free(str);
1013 return ret;
1014}
1015
1016/*
1017 * Entry point for the thread based jobs. The process based jobs end up
1018 * here as well, after a little setup.
1019 */
1020static void *thread_main(void *data)
1021{
1022 unsigned long long elapsed;
1023 struct thread_data *td = data;
1024 pthread_condattr_t attr;
1025 int clear_state;
1026
1027 if (!td->o.use_thread) {
1028 setsid();
1029 td->pid = getpid();
1030 } else
1031 td->pid = gettid();
1032
5d879392
JA
1033 fio_local_clock_init(td->o.use_thread);
1034
2e1df07d
JA
1035 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1036
1037 INIT_FLIST_HEAD(&td->io_u_freelist);
1038 INIT_FLIST_HEAD(&td->io_u_busylist);
1039 INIT_FLIST_HEAD(&td->io_u_requeues);
1040 INIT_FLIST_HEAD(&td->io_log_list);
1041 INIT_FLIST_HEAD(&td->io_hist_list);
1042 INIT_FLIST_HEAD(&td->verify_list);
1043 INIT_FLIST_HEAD(&td->trim_list);
1ae83d45 1044 INIT_FLIST_HEAD(&td->next_rand_list);
2e1df07d
JA
1045 pthread_mutex_init(&td->io_u_lock, NULL);
1046 td->io_hist_tree = RB_ROOT;
1047
1048 pthread_condattr_init(&attr);
1049 pthread_cond_init(&td->verify_cond, &attr);
1050 pthread_cond_init(&td->free_cond, &attr);
1051
1052 td_set_runstate(td, TD_INITIALIZED);
1053 dprint(FD_MUTEX, "up startup_mutex\n");
1054 fio_mutex_up(startup_mutex);
1055 dprint(FD_MUTEX, "wait on td->mutex\n");
1056 fio_mutex_down(td->mutex);
1057 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1058
1059 /*
1060 * the ->mutex mutex is now no longer used, close it to avoid
1061 * eating a file descriptor
1062 */
1063 fio_mutex_remove(td->mutex);
1064
1065 /*
1066 * A new gid requires privilege, so we need to do this before setting
1067 * the uid.
1068 */
1069 if (td->o.gid != -1U && setgid(td->o.gid)) {
1070 td_verror(td, errno, "setgid");
1071 goto err;
1072 }
1073 if (td->o.uid != -1U && setuid(td->o.uid)) {
1074 td_verror(td, errno, "setuid");
1075 goto err;
1076 }
1077
1078 /*
1079 * If we have a gettimeofday() thread, make sure we exclude that
1080 * thread from this job
1081 */
1082 if (td->o.gtod_cpu)
1083 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1084
1085 /*
1086 * Set affinity first, in case it has an impact on the memory
1087 * allocations.
1088 */
1089 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1090 td_verror(td, errno, "cpu_set_affinity");
1091 goto err;
1092 }
1093
67bf9823 1094#ifdef CONFIG_LIBNUMA
d0b937ed
YR
1095 /* numa node setup */
1096 if (td->o.numa_cpumask_set || td->o.numa_memmask_set) {
1097 int ret;
1098
1099 if (numa_available() < 0) {
1100 td_verror(td, errno, "Does not support NUMA API\n");
1101 goto err;
1102 }
1103
1104 if (td->o.numa_cpumask_set) {
1105 ret = numa_run_on_node_mask(td->o.numa_cpunodesmask);
1106 if (ret == -1) {
1107 td_verror(td, errno, \
1108 "numa_run_on_node_mask failed\n");
1109 goto err;
1110 }
1111 }
1112
1113 if (td->o.numa_memmask_set) {
1114
1115 switch (td->o.numa_mem_mode) {
1116 case MPOL_INTERLEAVE:
1117 numa_set_interleave_mask(td->o.numa_memnodesmask);
1118 break;
1119 case MPOL_BIND:
1120 numa_set_membind(td->o.numa_memnodesmask);
1121 break;
1122 case MPOL_LOCAL:
1123 numa_set_localalloc();
1124 break;
1125 case MPOL_PREFERRED:
1126 numa_set_preferred(td->o.numa_mem_prefer_node);
1127 break;
1128 case MPOL_DEFAULT:
1129 default:
1130 break;
1131 }
1132
1133 }
1134 }
1135#endif
1136
2e1df07d
JA
1137 /*
1138 * May alter parameters that init_io_u() will use, so we need to
1139 * do this first.
1140 */
1141 if (init_iolog(td))
1142 goto err;
1143
1144 if (init_io_u(td))
1145 goto err;
1146
1147 if (td->o.verify_async && verify_async_init(td))
1148 goto err;
1149
1150 if (td->ioprio_set) {
1151 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1152 td_verror(td, errno, "ioprio_set");
1153 goto err;
1154 }
1155 }
1156
5d89ff79 1157 if (td->o.cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
2e1df07d
JA
1158 goto err;
1159
649c10c5
BC
1160 errno = 0;
1161 if (nice(td->o.nice) == -1 && errno != 0) {
2e1df07d
JA
1162 td_verror(td, errno, "nice");
1163 goto err;
1164 }
1165
1166 if (td->o.ioscheduler && switch_ioscheduler(td))
1167 goto err;
1168
1169 if (!td->o.create_serialize && setup_files(td))
1170 goto err;
1171
1172 if (td_io_init(td))
1173 goto err;
1174
1175 if (init_random_map(td))
1176 goto err;
1177
1178 if (td->o.exec_prerun) {
1179 if (exec_string(td->o.exec_prerun))
1180 goto err;
1181 }
1182
1183 if (td->o.pre_read) {
1184 if (pre_read_files(td) < 0)
1185 goto err;
1186 }
1187
1188 fio_gettime(&td->epoch, NULL);
1189 getrusage(RUSAGE_SELF, &td->ru_start);
1190
1191 clear_state = 0;
1192 while (keep_running(td)) {
44cbc6da
JA
1193 uint64_t write_bytes;
1194
2e1df07d
JA
1195 fio_gettime(&td->start, NULL);
1196 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1197 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1198 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1199
6eaf09d6
SL
1200 if (td->o.ratemin[DDIR_READ] || td->o.ratemin[DDIR_WRITE] ||
1201 td->o.ratemin[DDIR_TRIM]) {
1202 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
2e1df07d 1203 sizeof(td->bw_sample_time));
6eaf09d6
SL
1204 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1205 sizeof(td->bw_sample_time));
1206 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
2e1df07d
JA
1207 sizeof(td->bw_sample_time));
1208 }
1209
1210 if (clear_state)
1211 clear_io_state(td);
1212
1213 prune_io_piece_log(td);
1214
44cbc6da 1215 write_bytes = td->io_bytes[DDIR_WRITE];
2e1df07d 1216 do_io(td);
44cbc6da 1217 write_bytes = td->io_bytes[DDIR_WRITE] - write_bytes;
2e1df07d
JA
1218
1219 clear_state = 1;
1220
1221 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1222 elapsed = utime_since_now(&td->start);
1223 td->ts.runtime[DDIR_READ] += elapsed;
1224 }
1225 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1226 elapsed = utime_since_now(&td->start);
1227 td->ts.runtime[DDIR_WRITE] += elapsed;
1228 }
6eaf09d6
SL
1229 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1230 elapsed = utime_since_now(&td->start);
1231 td->ts.runtime[DDIR_TRIM] += elapsed;
1232 }
2e1df07d
JA
1233
1234 if (td->error || td->terminate)
1235 break;
1236
1237 if (!td->o.do_verify ||
1238 td->o.verify == VERIFY_NONE ||
1239 (td->io_ops->flags & FIO_UNIDIR))
1240 continue;
1241
1242 clear_io_state(td);
1243
1244 fio_gettime(&td->start, NULL);
1245
44cbc6da 1246 do_verify(td, write_bytes);
2e1df07d
JA
1247
1248 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1249
1250 if (td->error || td->terminate)
1251 break;
1252 }
1253
1254 update_rusage_stat(td);
6eaf09d6
SL
1255 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1256 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1257 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
2e1df07d 1258 td->ts.total_run_time = mtime_since_now(&td->epoch);
6eaf09d6
SL
1259 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1260 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1261 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
2e1df07d
JA
1262
1263 fio_mutex_down(writeout_mutex);
1264 if (td->bw_log) {
1265 if (td->o.bw_log_file) {
1266 finish_log_named(td, td->bw_log,
1267 td->o.bw_log_file, "bw");
1268 } else
1269 finish_log(td, td->bw_log, "bw");
1270 }
1271 if (td->lat_log) {
1272 if (td->o.lat_log_file) {
1273 finish_log_named(td, td->lat_log,
1274 td->o.lat_log_file, "lat");
1275 } else
1276 finish_log(td, td->lat_log, "lat");
1277 }
1278 if (td->slat_log) {
1279 if (td->o.lat_log_file) {
1280 finish_log_named(td, td->slat_log,
1281 td->o.lat_log_file, "slat");
1282 } else
1283 finish_log(td, td->slat_log, "slat");
1284 }
1285 if (td->clat_log) {
1286 if (td->o.lat_log_file) {
1287 finish_log_named(td, td->clat_log,
1288 td->o.lat_log_file, "clat");
1289 } else
1290 finish_log(td, td->clat_log, "clat");
1291 }
1292 if (td->iops_log) {
1293 if (td->o.iops_log_file) {
1294 finish_log_named(td, td->iops_log,
1295 td->o.iops_log_file, "iops");
1296 } else
1297 finish_log(td, td->iops_log, "iops");
1298 }
1299
1300 fio_mutex_up(writeout_mutex);
1301 if (td->o.exec_postrun)
1302 exec_string(td->o.exec_postrun);
1303
1304 if (exitall_on_terminate)
1305 fio_terminate_threads(td->groupid);
1306
1307err:
1308 if (td->error)
1309 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1310 td->verror);
1311
1312 if (td->o.verify_async)
1313 verify_async_exit(td);
1314
1315 close_and_free_files(td);
2e1df07d 1316 cleanup_io_u(td);
32dbca2c 1317 close_ioengine(td);
2e1df07d
JA
1318 cgroup_shutdown(td, &cgroup_mnt);
1319
1320 if (td->o.cpumask_set) {
1321 int ret = fio_cpuset_exit(&td->o.cpumask);
1322
1323 td_verror(td, ret, "fio_cpuset_exit");
1324 }
1325
1326 /*
1327 * do this very late, it will log file closing as well
1328 */
1329 if (td->o.write_iolog_file)
1330 write_iolog_close(td);
1331
1332 td_set_runstate(td, TD_EXITED);
e43606c2 1333 return (void *) (uintptr_t) td->error;
2e1df07d
JA
1334}
1335
1336
1337/*
1338 * We cannot pass the td data into a forked process, so attach the td and
1339 * pass it to the thread worker.
1340 */
1341static int fork_main(int shmid, int offset)
1342{
1343 struct thread_data *td;
1344 void *data, *ret;
1345
1346#ifndef __hpux
1347 data = shmat(shmid, NULL, 0);
1348 if (data == (void *) -1) {
1349 int __err = errno;
1350
1351 perror("shmat");
1352 return __err;
1353 }
1354#else
1355 /*
1356 * HP-UX inherits shm mappings?
1357 */
1358 data = threads;
1359#endif
1360
1361 td = data + offset * sizeof(struct thread_data);
1362 ret = thread_main(td);
1363 shmdt(data);
e43606c2 1364 return (int) (uintptr_t) ret;
2e1df07d
JA
1365}
1366
1367/*
1368 * Run over the job map and reap the threads that have exited, if any.
1369 */
1370static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1371 unsigned int *m_rate)
1372{
1373 struct thread_data *td;
1374 unsigned int cputhreads, realthreads, pending;
1375 int i, status, ret;
1376
1377 /*
1378 * reap exited threads (TD_EXITED -> TD_REAPED)
1379 */
1380 realthreads = pending = cputhreads = 0;
1381 for_each_td(td, i) {
1382 int flags = 0;
1383
1384 /*
1385 * ->io_ops is NULL for a thread that has closed its
1386 * io engine
1387 */
1388 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1389 cputhreads++;
1390 else
1391 realthreads++;
1392
1393 if (!td->pid) {
1394 pending++;
1395 continue;
1396 }
1397 if (td->runstate == TD_REAPED)
1398 continue;
1399 if (td->o.use_thread) {
1400 if (td->runstate == TD_EXITED) {
1401 td_set_runstate(td, TD_REAPED);
1402 goto reaped;
1403 }
1404 continue;
1405 }
1406
1407 flags = WNOHANG;
1408 if (td->runstate == TD_EXITED)
1409 flags = 0;
1410
1411 /*
1412 * check if someone quit or got killed in an unusual way
1413 */
1414 ret = waitpid(td->pid, &status, flags);
1415 if (ret < 0) {
1416 if (errno == ECHILD) {
1417 log_err("fio: pid=%d disappeared %d\n",
1418 (int) td->pid, td->runstate);
a5e371a6 1419 td->sig = ECHILD;
2e1df07d
JA
1420 td_set_runstate(td, TD_REAPED);
1421 goto reaped;
1422 }
1423 perror("waitpid");
1424 } else if (ret == td->pid) {
1425 if (WIFSIGNALED(status)) {
1426 int sig = WTERMSIG(status);
1427
36d80bc7 1428 if (sig != SIGTERM && sig != SIGUSR2)
2e1df07d
JA
1429 log_err("fio: pid=%d, got signal=%d\n",
1430 (int) td->pid, sig);
a5e371a6 1431 td->sig = sig;
2e1df07d
JA
1432 td_set_runstate(td, TD_REAPED);
1433 goto reaped;
1434 }
1435 if (WIFEXITED(status)) {
1436 if (WEXITSTATUS(status) && !td->error)
1437 td->error = WEXITSTATUS(status);
1438
1439 td_set_runstate(td, TD_REAPED);
1440 goto reaped;
1441 }
1442 }
1443
1444 /*
1445 * thread is not dead, continue
1446 */
1447 pending++;
1448 continue;
1449reaped:
1450 (*nr_running)--;
342f4be4
JA
1451 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1452 (*t_rate) -= ddir_rw_sum(td->o.rate);
2e1df07d
JA
1453 if (!td->pid)
1454 pending--;
1455
1456 if (td->error)
1457 exit_value++;
1458
1459 done_secs += mtime_since_now(&td->epoch) / 1000;
1460 }
1461
1462 if (*nr_running == cputhreads && !pending && realthreads)
1463 fio_terminate_threads(TERMINATE_ALL);
1464}
1465
2e1df07d
JA
1466/*
1467 * Main function for kicking off and reaping jobs, as needed.
1468 */
1469static void run_threads(void)
1470{
1471 struct thread_data *td;
1472 unsigned long spent;
1473 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
1474
1475 if (fio_pin_memory())
1476 return;
1477
1478 if (fio_gtod_offload && fio_start_gtod_thread())
1479 return;
1480
1481 set_sig_handlers();
1482
f3afa57e 1483 if (output_format == FIO_OUTPUT_NORMAL) {
2e1df07d
JA
1484 log_info("Starting ");
1485 if (nr_thread)
1486 log_info("%d thread%s", nr_thread,
1487 nr_thread > 1 ? "s" : "");
1488 if (nr_process) {
1489 if (nr_thread)
1490 log_info(" and ");
1491 log_info("%d process%s", nr_process,
1492 nr_process > 1 ? "es" : "");
1493 }
1494 log_info("\n");
1495 fflush(stdout);
1496 }
1497
1498 todo = thread_number;
1499 nr_running = 0;
1500 nr_started = 0;
1501 m_rate = t_rate = 0;
1502
1503 for_each_td(td, i) {
1504 print_status_init(td->thread_number - 1);
1505
1506 if (!td->o.create_serialize)
1507 continue;
1508
1509 /*
1510 * do file setup here so it happens sequentially,
1511 * we don't want X number of threads getting their
1512 * client data interspersed on disk
1513 */
1514 if (setup_files(td)) {
1515 exit_value++;
1516 if (td->error)
1517 log_err("fio: pid=%d, err=%d/%s\n",
1518 (int) td->pid, td->error, td->verror);
1519 td_set_runstate(td, TD_REAPED);
1520 todo--;
1521 } else {
1522 struct fio_file *f;
1523 unsigned int j;
1524
1525 /*
1526 * for sharing to work, each job must always open
1527 * its own files. so close them, if we opened them
1528 * for creation
1529 */
1530 for_each_file(td, f, j) {
1531 if (fio_file_open(f))
1532 td_io_close_file(td, f);
1533 }
1534 }
1535 }
1536
1537 set_genesis_time();
1538
1539 while (todo) {
1540 struct thread_data *map[REAL_MAX_JOBS];
1541 struct timeval this_start;
1542 int this_jobs = 0, left;
1543
1544 /*
1545 * create threads (TD_NOT_CREATED -> TD_CREATED)
1546 */
1547 for_each_td(td, i) {
1548 if (td->runstate != TD_NOT_CREATED)
1549 continue;
1550
1551 /*
1552 * never got a chance to start, killed by other
1553 * thread for some reason
1554 */
1555 if (td->terminate) {
1556 todo--;
1557 continue;
1558 }
1559
1560 if (td->o.start_delay) {
1561 spent = mtime_since_genesis();
1562
1563 if (td->o.start_delay * 1000 > spent)
1564 continue;
1565 }
1566
1567 if (td->o.stonewall && (nr_started || nr_running)) {
1568 dprint(FD_PROCESS, "%s: stonewall wait\n",
1569 td->o.name);
1570 break;
1571 }
1572
1573 init_disk_util(td);
1574
1575 /*
1576 * Set state to created. Thread will transition
1577 * to TD_INITIALIZED when it's done setting up.
1578 */
1579 td_set_runstate(td, TD_CREATED);
1580 map[this_jobs++] = td;
1581 nr_started++;
1582
1583 if (td->o.use_thread) {
1584 int ret;
1585
1586 dprint(FD_PROCESS, "will pthread_create\n");
1587 ret = pthread_create(&td->thread, NULL,
1588 thread_main, td);
1589 if (ret) {
1590 log_err("pthread_create: %s\n",
1591 strerror(ret));
1592 nr_started--;
1593 break;
1594 }
1595 ret = pthread_detach(td->thread);
1596 if (ret)
1597 log_err("pthread_detach: %s",
1598 strerror(ret));
1599 } else {
1600 pid_t pid;
1601 dprint(FD_PROCESS, "will fork\n");
1602 pid = fork();
1603 if (!pid) {
1604 int ret = fork_main(shm_id, i);
1605
1606 _exit(ret);
1607 } else if (i == fio_debug_jobno)
1608 *fio_debug_jobp = pid;
1609 }
1610 dprint(FD_MUTEX, "wait on startup_mutex\n");
1611 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1612 log_err("fio: job startup hung? exiting.\n");
1613 fio_terminate_threads(TERMINATE_ALL);
1614 fio_abort = 1;
1615 nr_started--;
1616 break;
1617 }
1618 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1619 }
1620
1621 /*
1622 * Wait for the started threads to transition to
1623 * TD_INITIALIZED.
1624 */
1625 fio_gettime(&this_start, NULL);
1626 left = this_jobs;
1627 while (left && !fio_abort) {
1628 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1629 break;
1630
1631 usleep(100000);
1632
1633 for (i = 0; i < this_jobs; i++) {
1634 td = map[i];
1635 if (!td)
1636 continue;
1637 if (td->runstate == TD_INITIALIZED) {
1638 map[i] = NULL;
1639 left--;
1640 } else if (td->runstate >= TD_EXITED) {
1641 map[i] = NULL;
1642 left--;
1643 todo--;
1644 nr_running++; /* work-around... */
1645 }
1646 }
1647 }
1648
1649 if (left) {
4e87c37a
JA
1650 log_err("fio: %d job%s failed to start\n", left,
1651 left > 1 ? "s" : "");
2e1df07d
JA
1652 for (i = 0; i < this_jobs; i++) {
1653 td = map[i];
1654 if (!td)
1655 continue;
1656 kill(td->pid, SIGTERM);
1657 }
1658 break;
1659 }
1660
1661 /*
1662 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1663 */
1664 for_each_td(td, i) {
1665 if (td->runstate != TD_INITIALIZED)
1666 continue;
1667
1668 if (in_ramp_time(td))
1669 td_set_runstate(td, TD_RAMP);
1670 else
1671 td_set_runstate(td, TD_RUNNING);
1672 nr_running++;
1673 nr_started--;
342f4be4
JA
1674 m_rate += ddir_rw_sum(td->o.ratemin);
1675 t_rate += ddir_rw_sum(td->o.rate);
2e1df07d
JA
1676 todo--;
1677 fio_mutex_up(td->mutex);
1678 }
1679
1680 reap_threads(&nr_running, &t_rate, &m_rate);
1681
1682 if (todo) {
1683 if (is_backend)
1684 fio_server_idle_loop();
1685 else
1686 usleep(100000);
1687 }
1688 }
1689
1690 while (nr_running) {
1691 reap_threads(&nr_running, &t_rate, &m_rate);
1692
1693 if (is_backend)
1694 fio_server_idle_loop();
1695 else
1696 usleep(10000);
1697 }
1698
1699 update_io_ticks();
1700 fio_unpin_memory();
1701}
1702
9ec7779f
JA
1703void wait_for_disk_thread_exit(void)
1704{
1705 fio_mutex_down(disk_thread_mutex);
1706}
1707
27357187
JA
1708static void free_disk_util(void)
1709{
1710 disk_util_start_exit();
1711 wait_for_disk_thread_exit();
1712 disk_util_prune_entries();
1713}
1714
2e1df07d
JA
1715static void *disk_thread_main(void *data)
1716{
9ec7779f
JA
1717 int ret = 0;
1718
2e1df07d
JA
1719 fio_mutex_up(startup_mutex);
1720
9ec7779f 1721 while (threads && !ret) {
2e1df07d
JA
1722 usleep(DISK_UTIL_MSEC * 1000);
1723 if (!threads)
1724 break;
9ec7779f 1725 ret = update_io_ticks();
2e1df07d
JA
1726
1727 if (!is_backend)
1728 print_thread_status();
1729 }
1730
9ec7779f 1731 fio_mutex_up(disk_thread_mutex);
2e1df07d
JA
1732 return NULL;
1733}
1734
1735static int create_disk_util_thread(void)
1736{
1737 int ret;
1738
9ec7779f
JA
1739 setup_disk_util();
1740
521da527 1741 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
9ec7779f 1742
2e1df07d
JA
1743 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
1744 if (ret) {
9ec7779f 1745 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1746 log_err("Can't create disk util thread: %s\n", strerror(ret));
1747 return 1;
1748 }
1749
1750 ret = pthread_detach(disk_util_thread);
1751 if (ret) {
9ec7779f 1752 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1753 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
1754 return 1;
1755 }
1756
1757 dprint(FD_MUTEX, "wait on startup_mutex\n");
1758 fio_mutex_down(startup_mutex);
1759 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1760 return 0;
1761}
1762
2e1df07d
JA
1763int fio_backend(void)
1764{
1765 struct thread_data *td;
1766 int i;
1767
1768 if (exec_profile) {
1769 if (load_profile(exec_profile))
1770 return 1;
1771 free(exec_profile);
1772 exec_profile = NULL;
1773 }
1774 if (!thread_number)
1775 return 0;
1776
1777 if (write_bw_log) {
1778 setup_log(&agg_io_log[DDIR_READ], 0);
1779 setup_log(&agg_io_log[DDIR_WRITE], 0);
6eaf09d6 1780 setup_log(&agg_io_log[DDIR_TRIM], 0);
2e1df07d
JA
1781 }
1782
521da527 1783 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
2e1df07d
JA
1784 if (startup_mutex == NULL)
1785 return 1;
521da527 1786 writeout_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
2e1df07d
JA
1787 if (writeout_mutex == NULL)
1788 return 1;
1789
1790 set_genesis_time();
1791 create_disk_util_thread();
1792
1793 cgroup_list = smalloc(sizeof(*cgroup_list));
1794 INIT_FLIST_HEAD(cgroup_list);
1795
1796 run_threads();
1797
1798 if (!fio_abort) {
1799 show_run_stats();
1800 if (write_bw_log) {
1801 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1802 __finish_log(agg_io_log[DDIR_WRITE],
1803 "agg-write_bw.log");
6eaf09d6
SL
1804 __finish_log(agg_io_log[DDIR_TRIM],
1805 "agg-write_bw.log");
2e1df07d
JA
1806 }
1807 }
1808
1809 for_each_td(td, i)
1810 fio_options_free(td);
1811
a462baef 1812 free_disk_util();
2e1df07d
JA
1813 cgroup_kill(cgroup_list);
1814 sfree(cgroup_list);
1815 sfree(cgroup_mnt);
1816
1817 fio_mutex_remove(startup_mutex);
1818 fio_mutex_remove(writeout_mutex);
9ec7779f 1819 fio_mutex_remove(disk_thread_mutex);
2e1df07d
JA
1820 return exit_value;
1821}