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