Fio 1.28
[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
375/*
376 * The main verify engine. Runs over the writes we previously submitted,
377 * reads the blocks back in, and checks the crc/md5 of the data.
378 */
379static void do_verify(struct thread_data *td)
380{
381 struct fio_file *f;
382 struct io_u *io_u;
383 int ret, min_events;
384 unsigned int i;
385
386 /*
387 * sync io first and invalidate cache, to make sure we really
388 * read from disk.
389 */
390 for_each_file(td, f, i) {
391 if (!fio_file_open(f))
392 continue;
393 if (fio_io_sync(td, f))
394 break;
395 if (file_invalidate_cache(td, f))
396 break;
397 }
398
399 if (td->error)
400 return;
401
402 td_set_runstate(td, TD_VERIFYING);
403
404 io_u = NULL;
405 while (!td->terminate) {
406 int ret2, full;
407
408 io_u = __get_io_u(td);
409 if (!io_u)
410 break;
411
412 update_tv_cache(td);
413
414 if (runtime_exceeded(td, &td->tv_cache)) {
415 put_io_u(td, io_u);
416 td->terminate = 1;
417 break;
418 }
419
420 if (get_next_verify(td, io_u)) {
421 put_io_u(td, io_u);
422 break;
423 }
424
425 if (td_io_prep(td, io_u)) {
426 put_io_u(td, io_u);
427 break;
428 }
429
430 io_u->end_io = verify_io_u;
431
432 ret = td_io_queue(td, io_u);
433 switch (ret) {
434 case FIO_Q_COMPLETED:
435 if (io_u->error)
436 ret = -io_u->error;
437 else if (io_u->resid) {
438 int bytes = io_u->xfer_buflen - io_u->resid;
439 struct fio_file *f = io_u->file;
440
441 /*
442 * zero read, fail
443 */
444 if (!bytes) {
445 td_verror(td, EIO, "full resid");
446 put_io_u(td, io_u);
447 break;
448 }
449
450 io_u->xfer_buflen = io_u->resid;
451 io_u->xfer_buf += bytes;
452 io_u->offset += bytes;
453
454 td->ts.short_io_u[io_u->ddir]++;
455
456 if (io_u->offset == f->real_file_size)
457 goto sync_done;
458
459 requeue_io_u(td, &io_u);
460 } else {
461sync_done:
462 ret = io_u_sync_complete(td, io_u, NULL);
463 if (ret < 0)
464 break;
465 }
466 continue;
467 case FIO_Q_QUEUED:
468 break;
469 case FIO_Q_BUSY:
470 requeue_io_u(td, &io_u);
471 ret2 = td_io_commit(td);
472 if (ret2 < 0)
473 ret = ret2;
474 break;
475 default:
476 assert(ret < 0);
477 td_verror(td, -ret, "td_io_queue");
478 break;
479 }
480
481 if (ret < 0 || td->error)
482 break;
483
484 /*
485 * if we can queue more, do so. but check if there are
486 * completed io_u's first.
487 */
488 full = queue_full(td) || ret == FIO_Q_BUSY;
489 if (full || !td->o.iodepth_batch_complete) {
490 min_events = td->o.iodepth_batch_complete;
491 if (full && !min_events)
492 min_events = 1;
493
494 do {
495 /*
496 * Reap required number of io units, if any,
497 * and do the verification on them through
498 * the callback handler
499 */
500 if (io_u_queued_complete(td, min_events, NULL) < 0) {
501 ret = -1;
502 break;
503 }
504 } while (full && (td->cur_depth > td->o.iodepth_low));
505 }
506 if (ret < 0)
507 break;
508 }
509
510 if (!td->error) {
511 min_events = td->cur_depth;
512
513 if (min_events)
514 ret = io_u_queued_complete(td, min_events, NULL);
515 } else
516 cleanup_pending_aio(td);
517
518 td_set_runstate(td, TD_RUNNING);
519}
520
521/*
522 * Main IO worker function. It retrieves io_u's to process and queues
523 * and reaps them, checking for rate and errors along the way.
524 */
525static void do_io(struct thread_data *td)
526{
527 unsigned int i;
528 int ret = 0;
529
530 if (in_ramp_time(td))
531 td_set_runstate(td, TD_RAMP);
532 else
533 td_set_runstate(td, TD_RUNNING);
534
535 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
536 struct timeval comp_time;
537 unsigned long bytes_done[2] = { 0, 0 };
538 int min_evts = 0;
539 struct io_u *io_u;
540 int ret2, full;
541
542 if (td->terminate)
543 break;
544
545 io_u = get_io_u(td);
546 if (!io_u)
547 break;
548
549 update_tv_cache(td);
550
551 if (runtime_exceeded(td, &td->tv_cache)) {
552 put_io_u(td, io_u);
553 td->terminate = 1;
554 break;
555 }
556
557 /*
558 * Add verification end_io handler, if asked to verify
559 * a previously written file.
560 */
561 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
562 io_u->end_io = verify_io_u;
563 td_set_runstate(td, TD_VERIFYING);
564 } else if (in_ramp_time(td))
565 td_set_runstate(td, TD_RAMP);
566 else
567 td_set_runstate(td, TD_RUNNING);
568
569 ret = td_io_queue(td, io_u);
570 switch (ret) {
571 case FIO_Q_COMPLETED:
572 if (io_u->error)
573 ret = -io_u->error;
574 else if (io_u->resid) {
575 int bytes = io_u->xfer_buflen - io_u->resid;
576 struct fio_file *f = io_u->file;
577
578 /*
579 * zero read, fail
580 */
581 if (!bytes) {
582 td_verror(td, EIO, "full resid");
583 put_io_u(td, io_u);
584 break;
585 }
586
587 io_u->xfer_buflen = io_u->resid;
588 io_u->xfer_buf += bytes;
589 io_u->offset += bytes;
590
591 td->ts.short_io_u[io_u->ddir]++;
592
593 if (io_u->offset == f->real_file_size)
594 goto sync_done;
595
596 requeue_io_u(td, &io_u);
597 } else {
598sync_done:
599 if (__should_check_rate(td, 0) ||
600 __should_check_rate(td, 1))
601 fio_gettime(&comp_time, NULL);
602
603 ret = io_u_sync_complete(td, io_u, bytes_done);
604 if (ret < 0)
605 break;
606 }
607 break;
608 case FIO_Q_QUEUED:
609 /*
610 * if the engine doesn't have a commit hook,
611 * the io_u is really queued. if it does have such
612 * a hook, it has to call io_u_queued() itself.
613 */
614 if (td->io_ops->commit == NULL)
615 io_u_queued(td, io_u);
616 break;
617 case FIO_Q_BUSY:
618 requeue_io_u(td, &io_u);
619 ret2 = td_io_commit(td);
620 if (ret2 < 0)
621 ret = ret2;
622 break;
623 default:
624 assert(ret < 0);
625 put_io_u(td, io_u);
626 break;
627 }
628
629 if (ret < 0 || td->error)
630 break;
631
632 /*
633 * See if we need to complete some commands
634 */
635 full = queue_full(td) || ret == FIO_Q_BUSY;
636 if (full || !td->o.iodepth_batch_complete) {
637 min_evts = td->o.iodepth_batch_complete;
638 if (full && !min_evts)
639 min_evts = 1;
640
641 if (__should_check_rate(td, 0) ||
642 __should_check_rate(td, 1))
643 fio_gettime(&comp_time, NULL);
644
645 do {
646 ret = io_u_queued_complete(td, min_evts, bytes_done);
647 if (ret < 0)
648 break;
649
650 } while (full && (td->cur_depth > td->o.iodepth_low));
651 }
652
653 if (ret < 0)
654 break;
655 if (!(bytes_done[0] + bytes_done[1]))
656 continue;
657
658 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
659 if (check_min_rate(td, &comp_time, bytes_done)) {
660 if (exitall_on_terminate)
661 terminate_threads(td->groupid);
662 td_verror(td, EIO, "check_min_rate");
663 break;
664 }
665 }
666
667 if (td->o.thinktime) {
668 unsigned long long b;
669
670 b = td->io_blocks[0] + td->io_blocks[1];
671 if (!(b % td->o.thinktime_blocks)) {
672 int left;
673
674 if (td->o.thinktime_spin)
675 usec_spin(td->o.thinktime_spin);
676
677 left = td->o.thinktime - td->o.thinktime_spin;
678 if (left)
679 usec_sleep(td, left);
680 }
681 }
682 }
683
684 if (td->o.fill_device && td->error == ENOSPC) {
685 td->error = 0;
686 td->terminate = 1;
687 }
688 if (!td->error) {
689 struct fio_file *f;
690
691 i = td->cur_depth;
692 if (i)
693 ret = io_u_queued_complete(td, i, NULL);
694
695 if (should_fsync(td) && td->o.end_fsync) {
696 td_set_runstate(td, TD_FSYNCING);
697
698 for_each_file(td, f, i) {
699 if (!fio_file_open(f))
700 continue;
701 fio_io_sync(td, f);
702 }
703 }
704 } else
705 cleanup_pending_aio(td);
706
707 /*
708 * stop job if we failed doing any IO
709 */
710 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
711 td->done = 1;
712}
713
714static void cleanup_io_u(struct thread_data *td)
715{
716 struct flist_head *entry, *n;
717 struct io_u *io_u;
718
719 flist_for_each_safe(entry, n, &td->io_u_freelist) {
720 io_u = flist_entry(entry, struct io_u, list);
721
722 flist_del(&io_u->list);
723 free(io_u);
724 }
725
726 free_io_mem(td);
727}
728
729static int init_io_u(struct thread_data *td)
730{
731 struct io_u *io_u;
732 unsigned int max_bs;
733 int cl_align, i, max_units;
734 char *p;
735
736 max_units = td->o.iodepth;
737 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
738 td->orig_buffer_size = (unsigned long long) max_bs
739 * (unsigned long long) max_units;
740
741 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
742 unsigned long bs;
743
744 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
745 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
746 }
747
748 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
749 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
750 return 1;
751 }
752
753 if (allocate_io_mem(td))
754 return 1;
755
756 if (td->o.odirect)
757 p = ALIGN(td->orig_buffer);
758 else
759 p = td->orig_buffer;
760
761 cl_align = os_cache_line_size();
762
763 for (i = 0; i < max_units; i++) {
764 void *ptr;
765
766 if (td->terminate)
767 return 1;
768
769 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
770 log_err("fio: posix_memalign=%s\n", strerror(errno));
771 break;
772 }
773
774 io_u = ptr;
775 memset(io_u, 0, sizeof(*io_u));
776 INIT_FLIST_HEAD(&io_u->list);
777
778 if (!(td->io_ops->flags & FIO_NOIO)) {
779 io_u->buf = p + max_bs * i;
780
781 if (td_write(td) && !td->o.refill_buffers)
782 io_u_fill_buffer(td, io_u, max_bs);
783 }
784
785 io_u->index = i;
786 io_u->flags = IO_U_F_FREE;
787 flist_add(&io_u->list, &td->io_u_freelist);
788 }
789
790 return 0;
791}
792
793static int switch_ioscheduler(struct thread_data *td)
794{
795 char tmp[256], tmp2[128];
796 FILE *f;
797 int ret;
798
799 if (td->io_ops->flags & FIO_DISKLESSIO)
800 return 0;
801
802 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
803
804 f = fopen(tmp, "r+");
805 if (!f) {
806 if (errno == ENOENT) {
807 log_err("fio: os or kernel doesn't support IO scheduler"
808 " switching\n");
809 return 0;
810 }
811 td_verror(td, errno, "fopen iosched");
812 return 1;
813 }
814
815 /*
816 * Set io scheduler.
817 */
818 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
819 if (ferror(f) || ret != 1) {
820 td_verror(td, errno, "fwrite");
821 fclose(f);
822 return 1;
823 }
824
825 rewind(f);
826
827 /*
828 * Read back and check that the selected scheduler is now the default.
829 */
830 ret = fread(tmp, 1, sizeof(tmp), f);
831 if (ferror(f) || ret < 0) {
832 td_verror(td, errno, "fread");
833 fclose(f);
834 return 1;
835 }
836
837 sprintf(tmp2, "[%s]", td->o.ioscheduler);
838 if (!strstr(tmp, tmp2)) {
839 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
840 td_verror(td, EINVAL, "iosched_switch");
841 fclose(f);
842 return 1;
843 }
844
845 fclose(f);
846 return 0;
847}
848
849static int keep_running(struct thread_data *td)
850{
851 unsigned long long io_done;
852
853 if (td->done)
854 return 0;
855 if (td->o.time_based)
856 return 1;
857 if (td->o.loops) {
858 td->o.loops--;
859 return 1;
860 }
861
862 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
863 + td->io_skip_bytes;
864 if (io_done < td->o.size)
865 return 1;
866
867 return 0;
868}
869
870static void reset_io_counters(struct thread_data *td)
871{
872 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
873 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
874 td->zone_bytes = 0;
875 td->rate_bytes[0] = td->rate_bytes[1] = 0;
876 td->rate_blocks[0] = td->rate_blocks[1] = 0;
877
878 td->last_was_sync = 0;
879
880 /*
881 * reset file done count if we are to start over
882 */
883 if (td->o.time_based || td->o.loops)
884 td->nr_done_files = 0;
885
886 /*
887 * Set the same seed to get repeatable runs
888 */
889 td_fill_rand_seeds(td);
890}
891
892void reset_all_stats(struct thread_data *td)
893{
894 struct timeval tv;
895 int i;
896
897 reset_io_counters(td);
898
899 for (i = 0; i < 2; i++) {
900 td->io_bytes[i] = 0;
901 td->io_blocks[i] = 0;
902 td->io_issues[i] = 0;
903 td->ts.total_io_u[i] = 0;
904 }
905
906 fio_gettime(&tv, NULL);
907 memcpy(&td->epoch, &tv, sizeof(tv));
908 memcpy(&td->start, &tv, sizeof(tv));
909}
910
911static void clear_io_state(struct thread_data *td)
912{
913 struct fio_file *f;
914 unsigned int i;
915
916 reset_io_counters(td);
917
918 close_files(td);
919 for_each_file(td, f, i)
920 fio_file_clear_done(f);
921}
922
923static int exec_string(const char *string)
924{
925 int ret, newlen = strlen(string) + 1 + 8;
926 char *str;
927
928 str = malloc(newlen);
929 sprintf(str, "sh -c %s", string);
930
931 ret = system(str);
932 if (ret == -1)
933 log_err("fio: exec of cmd <%s> failed\n", str);
934
935 free(str);
936 return ret;
937}
938
939/*
940 * Entry point for the thread based jobs. The process based jobs end up
941 * here as well, after a little setup.
942 */
943static void *thread_main(void *data)
944{
945 unsigned long long runtime[2], elapsed;
946 struct thread_data *td = data;
947 int clear_state;
948
949 if (!td->o.use_thread)
950 setsid();
951
952 td->pid = getpid();
953
954 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
955
956 INIT_FLIST_HEAD(&td->io_u_freelist);
957 INIT_FLIST_HEAD(&td->io_u_busylist);
958 INIT_FLIST_HEAD(&td->io_u_requeues);
959 INIT_FLIST_HEAD(&td->io_log_list);
960 INIT_FLIST_HEAD(&td->io_hist_list);
961 td->io_hist_tree = RB_ROOT;
962
963 td_set_runstate(td, TD_INITIALIZED);
964 dprint(FD_MUTEX, "up startup_mutex\n");
965 fio_mutex_up(startup_mutex);
966 dprint(FD_MUTEX, "wait on td->mutex\n");
967 fio_mutex_down(td->mutex);
968 dprint(FD_MUTEX, "done waiting on td->mutex\n");
969
970 /*
971 * the ->mutex mutex is now no longer used, close it to avoid
972 * eating a file descriptor
973 */
974 fio_mutex_remove(td->mutex);
975
976 /*
977 * May alter parameters that init_io_u() will use, so we need to
978 * do this first.
979 */
980 if (init_iolog(td))
981 goto err;
982
983 if (init_io_u(td))
984 goto err;
985
986 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
987 td_verror(td, errno, "cpu_set_affinity");
988 goto err;
989 }
990
991 /*
992 * If we have a gettimeofday() thread, make sure we exclude that
993 * thread from this job
994 */
995 if (td->o.gtod_cpu) {
996 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
997 if (fio_setaffinity(td) == -1) {
998 td_verror(td, errno, "cpu_set_affinity");
999 goto err;
1000 }
1001 }
1002
1003 if (td->ioprio_set) {
1004 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1005 td_verror(td, errno, "ioprio_set");
1006 goto err;
1007 }
1008 }
1009
1010 if (nice(td->o.nice) == -1) {
1011 td_verror(td, errno, "nice");
1012 goto err;
1013 }
1014
1015 if (td->o.ioscheduler && switch_ioscheduler(td))
1016 goto err;
1017
1018 if (!td->o.create_serialize && setup_files(td))
1019 goto err;
1020
1021 if (td_io_init(td))
1022 goto err;
1023
1024 if (init_random_map(td))
1025 goto err;
1026
1027 if (td->o.exec_prerun) {
1028 if (exec_string(td->o.exec_prerun))
1029 goto err;
1030 }
1031
1032 if (td->o.pre_read) {
1033 if (pre_read_files(td) < 0)
1034 goto err;
1035 }
1036
1037 fio_gettime(&td->epoch, NULL);
1038 getrusage(RUSAGE_SELF, &td->ts.ru_start);
1039
1040 runtime[0] = runtime[1] = 0;
1041 clear_state = 0;
1042 while (keep_running(td)) {
1043 fio_gettime(&td->start, NULL);
1044 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
1045 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1046
1047 if (td->o.ratemin[0] || td->o.ratemin[1])
1048 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1049 sizeof(td->lastrate));
1050
1051 if (clear_state)
1052 clear_io_state(td);
1053
1054 prune_io_piece_log(td);
1055
1056 do_io(td);
1057
1058 clear_state = 1;
1059
1060 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1061 elapsed = utime_since_now(&td->start);
1062 runtime[DDIR_READ] += elapsed;
1063 }
1064 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1065 elapsed = utime_since_now(&td->start);
1066 runtime[DDIR_WRITE] += elapsed;
1067 }
1068
1069 if (td->error || td->terminate)
1070 break;
1071
1072 if (!td->o.do_verify ||
1073 td->o.verify == VERIFY_NONE ||
1074 (td->io_ops->flags & FIO_UNIDIR))
1075 continue;
1076
1077 clear_io_state(td);
1078
1079 fio_gettime(&td->start, NULL);
1080
1081 do_verify(td);
1082
1083 runtime[DDIR_READ] += utime_since_now(&td->start);
1084
1085 if (td->error || td->terminate)
1086 break;
1087 }
1088
1089 update_rusage_stat(td);
1090 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1091 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
1092 td->ts.total_run_time = mtime_since_now(&td->epoch);
1093 td->ts.io_bytes[0] = td->io_bytes[0];
1094 td->ts.io_bytes[1] = td->io_bytes[1];
1095
1096 fio_mutex_down(writeout_mutex);
1097 if (td->ts.bw_log) {
1098 if (td->o.bw_log_file) {
1099 finish_log_named(td, td->ts.bw_log,
1100 td->o.bw_log_file, "bw");
1101 } else
1102 finish_log(td, td->ts.bw_log, "bw");
1103 }
1104 if (td->ts.slat_log) {
1105 if (td->o.lat_log_file) {
1106 finish_log_named(td, td->ts.slat_log,
1107 td->o.lat_log_file, "slat");
1108 } else
1109 finish_log(td, td->ts.slat_log, "slat");
1110 }
1111 if (td->ts.clat_log) {
1112 if (td->o.lat_log_file) {
1113 finish_log_named(td, td->ts.clat_log,
1114 td->o.lat_log_file, "clat");
1115 } else
1116 finish_log(td, td->ts.clat_log, "clat");
1117 }
1118 fio_mutex_up(writeout_mutex);
1119 if (td->o.exec_postrun)
1120 exec_string(td->o.exec_postrun);
1121
1122 if (exitall_on_terminate)
1123 terminate_threads(td->groupid);
1124
1125err:
1126 if (td->error)
1127 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1128 td->verror);
1129 close_and_free_files(td);
1130 close_ioengine(td);
1131 cleanup_io_u(td);
1132
1133 if (td->o.cpumask_set) {
1134 int ret = fio_cpuset_exit(&td->o.cpumask);
1135
1136 td_verror(td, ret, "fio_cpuset_exit");
1137 }
1138
1139 /*
1140 * do this very late, it will log file closing as well
1141 */
1142 if (td->o.write_iolog_file)
1143 write_iolog_close(td);
1144
1145 options_mem_free(td);
1146 td_set_runstate(td, TD_EXITED);
1147 return (void *) (unsigned long) td->error;
1148}
1149
1150/*
1151 * We cannot pass the td data into a forked process, so attach the td and
1152 * pass it to the thread worker.
1153 */
1154static int fork_main(int shmid, int offset)
1155{
1156 struct thread_data *td;
1157 void *data, *ret;
1158
1159 data = shmat(shmid, NULL, 0);
1160 if (data == (void *) -1) {
1161 int __err = errno;
1162
1163 perror("shmat");
1164 return __err;
1165 }
1166
1167 td = data + offset * sizeof(struct thread_data);
1168 ret = thread_main(td);
1169 shmdt(data);
1170 return (int) (unsigned long) ret;
1171}
1172
1173/*
1174 * Run over the job map and reap the threads that have exited, if any.
1175 */
1176static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1177{
1178 struct thread_data *td;
1179 int i, cputhreads, realthreads, pending, status, ret;
1180
1181 /*
1182 * reap exited threads (TD_EXITED -> TD_REAPED)
1183 */
1184 realthreads = pending = cputhreads = 0;
1185 for_each_td(td, i) {
1186 int flags = 0;
1187
1188 /*
1189 * ->io_ops is NULL for a thread that has closed its
1190 * io engine
1191 */
1192 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1193 cputhreads++;
1194 else
1195 realthreads++;
1196
1197 if (!td->pid) {
1198 pending++;
1199 continue;
1200 }
1201 if (td->runstate == TD_REAPED)
1202 continue;
1203 if (td->o.use_thread) {
1204 if (td->runstate == TD_EXITED) {
1205 td_set_runstate(td, TD_REAPED);
1206 goto reaped;
1207 }
1208 continue;
1209 }
1210
1211 flags = WNOHANG;
1212 if (td->runstate == TD_EXITED)
1213 flags = 0;
1214
1215 /*
1216 * check if someone quit or got killed in an unusual way
1217 */
1218 ret = waitpid(td->pid, &status, flags);
1219 if (ret < 0) {
1220 if (errno == ECHILD) {
1221 log_err("fio: pid=%d disappeared %d\n",
1222 (int) td->pid, td->runstate);
1223 td_set_runstate(td, TD_REAPED);
1224 goto reaped;
1225 }
1226 perror("waitpid");
1227 } else if (ret == td->pid) {
1228 if (WIFSIGNALED(status)) {
1229 int sig = WTERMSIG(status);
1230
1231 if (sig != SIGQUIT)
1232 log_err("fio: pid=%d, got signal=%d\n",
1233 (int) td->pid, sig);
1234 td_set_runstate(td, TD_REAPED);
1235 goto reaped;
1236 }
1237 if (WIFEXITED(status)) {
1238 if (WEXITSTATUS(status) && !td->error)
1239 td->error = WEXITSTATUS(status);
1240
1241 td_set_runstate(td, TD_REAPED);
1242 goto reaped;
1243 }
1244 }
1245
1246 /*
1247 * thread is not dead, continue
1248 */
1249 pending++;
1250 continue;
1251reaped:
1252 (*nr_running)--;
1253 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1254 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
1255 if (!td->pid)
1256 pending--;
1257
1258 if (td->error)
1259 exit_value++;
1260
1261 done_secs += mtime_since_now(&td->epoch) / 1000;
1262 }
1263
1264 if (*nr_running == cputhreads && !pending && realthreads)
1265 terminate_threads(TERMINATE_ALL);
1266}
1267
1268static void *gtod_thread_main(void *data)
1269{
1270 fio_mutex_up(startup_mutex);
1271
1272 /*
1273 * As long as we have jobs around, update the clock. It would be nice
1274 * to have some way of NOT hammering that CPU with gettimeofday(),
1275 * but I'm not sure what to use outside of a simple CPU nop to relax
1276 * it - we don't want to lose precision.
1277 */
1278 while (threads) {
1279 fio_gtod_update();
1280 nop;
1281 }
1282
1283 return NULL;
1284}
1285
1286static int fio_start_gtod_thread(void)
1287{
1288 int ret;
1289
1290 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1291 if (ret) {
1292 log_err("Can't create gtod thread: %s\n", strerror(ret));
1293 return 1;
1294 }
1295
1296 ret = pthread_detach(gtod_thread);
1297 if (ret) {
1298 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
1299 return 1;
1300 }
1301
1302 dprint(FD_MUTEX, "wait on startup_mutex\n");
1303 fio_mutex_down(startup_mutex);
1304 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1305 return 0;
1306}
1307
1308/*
1309 * Main function for kicking off and reaping jobs, as needed.
1310 */
1311static void run_threads(void)
1312{
1313 struct thread_data *td;
1314 unsigned long spent;
1315 int i, todo, nr_running, m_rate, t_rate, nr_started;
1316
1317 if (fio_pin_memory())
1318 return;
1319
1320 if (fio_gtod_offload && fio_start_gtod_thread())
1321 return;
1322
1323 if (!terse_output) {
1324 printf("Starting ");
1325 if (nr_thread)
1326 printf("%d thread%s", nr_thread,
1327 nr_thread > 1 ? "s" : "");
1328 if (nr_process) {
1329 if (nr_thread)
1330 printf(" and ");
1331 printf("%d process%s", nr_process,
1332 nr_process > 1 ? "es" : "");
1333 }
1334 printf("\n");
1335 fflush(stdout);
1336 }
1337
1338 set_sig_handlers();
1339
1340 todo = thread_number;
1341 nr_running = 0;
1342 nr_started = 0;
1343 m_rate = t_rate = 0;
1344
1345 for_each_td(td, i) {
1346 print_status_init(td->thread_number - 1);
1347
1348 if (!td->o.create_serialize) {
1349 init_disk_util(td);
1350 continue;
1351 }
1352
1353 /*
1354 * do file setup here so it happens sequentially,
1355 * we don't want X number of threads getting their
1356 * client data interspersed on disk
1357 */
1358 if (setup_files(td)) {
1359 exit_value++;
1360 if (td->error)
1361 log_err("fio: pid=%d, err=%d/%s\n",
1362 (int) td->pid, td->error, td->verror);
1363 td_set_runstate(td, TD_REAPED);
1364 todo--;
1365 } else {
1366 struct fio_file *f;
1367 unsigned int i;
1368
1369 /*
1370 * for sharing to work, each job must always open
1371 * its own files. so close them, if we opened them
1372 * for creation
1373 */
1374 for_each_file(td, f, i) {
1375 if (fio_file_open(f))
1376 td_io_close_file(td, f);
1377 }
1378 }
1379
1380 init_disk_util(td);
1381 }
1382
1383 set_genesis_time();
1384
1385 while (todo) {
1386 struct thread_data *map[MAX_JOBS];
1387 struct timeval this_start;
1388 int this_jobs = 0, left;
1389
1390 /*
1391 * create threads (TD_NOT_CREATED -> TD_CREATED)
1392 */
1393 for_each_td(td, i) {
1394 if (td->runstate != TD_NOT_CREATED)
1395 continue;
1396
1397 /*
1398 * never got a chance to start, killed by other
1399 * thread for some reason
1400 */
1401 if (td->terminate) {
1402 todo--;
1403 continue;
1404 }
1405
1406 if (td->o.start_delay) {
1407 spent = mtime_since_genesis();
1408
1409 if (td->o.start_delay * 1000 > spent)
1410 continue;
1411 }
1412
1413 if (td->o.stonewall && (nr_started || nr_running)) {
1414 dprint(FD_PROCESS, "%s: stonewall wait\n",
1415 td->o.name);
1416 break;
1417 }
1418
1419 /*
1420 * Set state to created. Thread will transition
1421 * to TD_INITIALIZED when it's done setting up.
1422 */
1423 td_set_runstate(td, TD_CREATED);
1424 map[this_jobs++] = td;
1425 nr_started++;
1426
1427 if (td->o.use_thread) {
1428 int ret;
1429
1430 dprint(FD_PROCESS, "will pthread_create\n");
1431 ret = pthread_create(&td->thread, NULL,
1432 thread_main, td);
1433 if (ret) {
1434 log_err("pthread_create: %s\n",
1435 strerror(ret));
1436 nr_started--;
1437 break;
1438 }
1439 ret = pthread_detach(td->thread);
1440 if (ret)
1441 log_err("pthread_detach: %s",
1442 strerror(ret));
1443 } else {
1444 pid_t pid;
1445 dprint(FD_PROCESS, "will fork\n");
1446 pid = fork();
1447 if (!pid) {
1448 int ret = fork_main(shm_id, i);
1449
1450 _exit(ret);
1451 } else if (i == fio_debug_jobno)
1452 *fio_debug_jobp = pid;
1453 }
1454 dprint(FD_MUTEX, "wait on startup_mutex\n");
1455 fio_mutex_down(startup_mutex);
1456 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1457 }
1458
1459 /*
1460 * Wait for the started threads to transition to
1461 * TD_INITIALIZED.
1462 */
1463 fio_gettime(&this_start, NULL);
1464 left = this_jobs;
1465 while (left && !fio_abort) {
1466 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1467 break;
1468
1469 usleep(100000);
1470
1471 for (i = 0; i < this_jobs; i++) {
1472 td = map[i];
1473 if (!td)
1474 continue;
1475 if (td->runstate == TD_INITIALIZED) {
1476 map[i] = NULL;
1477 left--;
1478 } else if (td->runstate >= TD_EXITED) {
1479 map[i] = NULL;
1480 left--;
1481 todo--;
1482 nr_running++; /* work-around... */
1483 }
1484 }
1485 }
1486
1487 if (left) {
1488 log_err("fio: %d jobs failed to start\n", left);
1489 for (i = 0; i < this_jobs; i++) {
1490 td = map[i];
1491 if (!td)
1492 continue;
1493 kill(td->pid, SIGTERM);
1494 }
1495 break;
1496 }
1497
1498 /*
1499 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1500 */
1501 for_each_td(td, i) {
1502 if (td->runstate != TD_INITIALIZED)
1503 continue;
1504
1505 if (in_ramp_time(td))
1506 td_set_runstate(td, TD_RAMP);
1507 else
1508 td_set_runstate(td, TD_RUNNING);
1509 nr_running++;
1510 nr_started--;
1511 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1512 t_rate += td->o.rate[0] + td->o.rate[1];
1513 todo--;
1514 fio_mutex_up(td->mutex);
1515 }
1516
1517 reap_threads(&nr_running, &t_rate, &m_rate);
1518
1519 if (todo)
1520 usleep(100000);
1521 }
1522
1523 while (nr_running) {
1524 reap_threads(&nr_running, &t_rate, &m_rate);
1525 usleep(10000);
1526 }
1527
1528 update_io_ticks();
1529 fio_unpin_memory();
1530}
1531
1532int main(int argc, char *argv[])
1533{
1534 long ps;
1535
1536 sinit();
1537
1538 /*
1539 * We need locale for number printing, if it isn't set then just
1540 * go with the US format.
1541 */
1542 if (!getenv("LC_NUMERIC"))
1543 setlocale(LC_NUMERIC, "en_US");
1544
1545 if (parse_options(argc, argv))
1546 return 1;
1547
1548 if (!thread_number)
1549 return 0;
1550
1551 ps = sysconf(_SC_PAGESIZE);
1552 if (ps < 0) {
1553 log_err("Failed to get page size\n");
1554 return 1;
1555 }
1556
1557 page_size = ps;
1558 page_mask = ps - 1;
1559
1560 if (write_bw_log) {
1561 setup_log(&agg_io_log[DDIR_READ]);
1562 setup_log(&agg_io_log[DDIR_WRITE]);
1563 }
1564
1565 startup_mutex = fio_mutex_init(0);
1566 writeout_mutex = fio_mutex_init(1);
1567
1568 set_genesis_time();
1569
1570 status_timer_arm();
1571
1572 run_threads();
1573
1574 if (!fio_abort) {
1575 show_run_stats();
1576 if (write_bw_log) {
1577 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1578 __finish_log(agg_io_log[DDIR_WRITE],
1579 "agg-write_bw.log");
1580 }
1581 }
1582
1583 fio_mutex_remove(startup_mutex);
1584 fio_mutex_remove(writeout_mutex);
1585 return exit_value;
1586}