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