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