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