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