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