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