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