blob: 981625b61095dd42995d32291ec3fdc4b58cb597 [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>
Jens Axboe2e1df072012-02-09 11:15:02 +010037#include <sys/mman.h>
38
39#include "fio.h"
Olega5e0ee12013-03-12 00:06:53 -070040#ifndef FIO_NO_HAVE_SHM_H
41#include <sys/shm.h>
42#endif
Jens Axboe2e1df072012-02-09 11:15:02 +010043#include "hash.h"
44#include "smalloc.h"
45#include "verify.h"
46#include "trim.h"
47#include "diskutil.h"
48#include "cgroup.h"
49#include "profile.h"
50#include "lib/rand.h"
51#include "memalign.h"
52#include "server.h"
Jens Axboe44404c52013-01-24 14:20:09 -070053#include "lib/getrusage.h"
Huadong Liuf2a2ce02013-01-30 13:22:24 +010054#include "idletime.h"
Jens Axboe002fe732014-02-11 08:31:13 -070055#include "err.h"
Jens Axboe65f93ca2014-07-03 14:16:25 -060056#include "lib/tp.h"
Jens Axboe2e1df072012-02-09 11:15:02 +010057
58static pthread_t disk_util_thread;
Jens Axboe9ec77792012-08-02 08:27:41 +020059static struct fio_mutex *disk_thread_mutex;
Jens Axboe2e1df072012-02-09 11:15:02 +010060static struct fio_mutex *startup_mutex;
Jens Axboe2e1df072012-02-09 11:15:02 +010061static struct flist_head *cgroup_list;
62static char *cgroup_mnt;
63static int exit_value;
64static volatile int fio_abort;
Jens Axboe3a5f6bd2013-04-11 12:04:38 +020065static unsigned int nr_process = 0;
66static unsigned int nr_thread = 0;
Jens Axboe2e1df072012-02-09 11:15:02 +010067
Shaohua Li6eaf09d2012-09-14 08:49:43 +020068struct io_log *agg_io_log[DDIR_RWDIR_CNT];
Jens Axboe2e1df072012-02-09 11:15:02 +010069
Jens Axboea3efc912012-02-09 11:25:24 +010070int groupid = 0;
71unsigned int thread_number = 0;
Jens Axboe108fea72012-11-14 13:09:45 -070072unsigned int stat_number = 0;
Jens Axboea3efc912012-02-09 11:25:24 +010073int shm_id = 0;
74int temp_stall_ts;
75unsigned long done_secs = 0;
Jens Axboe27357182012-09-27 11:31:50 +020076volatile int disk_util_exit = 0;
Jens Axboea3efc912012-02-09 11:25:24 +010077
Jens Axboe2e1df072012-02-09 11:15:02 +010078#define PAGE_ALIGN(buf) \
Bruce Crane43606c2012-02-20 09:34:24 +010079 (char *) (((uintptr_t) (buf) + page_mask) & ~page_mask)
Jens Axboe2e1df072012-02-09 11:15:02 +010080
81#define JOB_START_TIMEOUT (5 * 1000)
82
83static void sig_int(int sig)
84{
85 if (threads) {
86 if (is_backend)
87 fio_server_got_signal(sig);
88 else {
89 log_info("\nfio: terminating on signal %d\n", sig);
90 fflush(stdout);
91 exit_value = 128;
92 }
93
94 fio_terminate_threads(TERMINATE_ALL);
95 }
96}
97
Jens Axboe4c6d91e2012-03-30 10:30:35 +020098static void sig_show_status(int sig)
99{
100 show_running_run_stats();
101}
102
Jens Axboe2e1df072012-02-09 11:15:02 +0100103static void set_sig_handlers(void)
104{
105 struct sigaction act;
106
107 memset(&act, 0, sizeof(act));
108 act.sa_handler = sig_int;
109 act.sa_flags = SA_RESTART;
110 sigaction(SIGINT, &act, NULL);
111
112 memset(&act, 0, sizeof(act));
113 act.sa_handler = sig_int;
114 act.sa_flags = SA_RESTART;
115 sigaction(SIGTERM, &act, NULL);
116
Bruce Cran2f694502012-10-10 16:34:13 +0100117/* Windows uses SIGBREAK as a quit signal from other applications */
118#ifdef WIN32
119 memset(&act, 0, sizeof(act));
120 act.sa_handler = sig_int;
121 act.sa_flags = SA_RESTART;
122 sigaction(SIGBREAK, &act, NULL);
123#endif
124
Jens Axboe4c6d91e2012-03-30 10:30:35 +0200125 memset(&act, 0, sizeof(act));
126 act.sa_handler = sig_show_status;
127 act.sa_flags = SA_RESTART;
128 sigaction(SIGUSR1, &act, NULL);
129
Jens Axboe2e1df072012-02-09 11:15:02 +0100130 if (is_backend) {
131 memset(&act, 0, sizeof(act));
132 act.sa_handler = sig_int;
133 act.sa_flags = SA_RESTART;
134 sigaction(SIGPIPE, &act, NULL);
135 }
136}
137
138/*
139 * Check if we are above the minimum rate given.
140 */
141static int __check_min_rate(struct thread_data *td, struct timeval *now,
142 enum fio_ddir ddir)
143{
144 unsigned long long bytes = 0;
145 unsigned long iops = 0;
146 unsigned long spent;
147 unsigned long rate;
148 unsigned int ratemin = 0;
149 unsigned int rate_iops = 0;
150 unsigned int rate_iops_min = 0;
151
152 assert(ddir_rw(ddir));
153
154 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
155 return 0;
156
157 /*
158 * allow a 2 second settle period in the beginning
159 */
160 if (mtime_since(&td->start, now) < 2000)
161 return 0;
162
163 iops += td->this_io_blocks[ddir];
164 bytes += td->this_io_bytes[ddir];
165 ratemin += td->o.ratemin[ddir];
166 rate_iops += td->o.rate_iops[ddir];
167 rate_iops_min += td->o.rate_iops_min[ddir];
168
169 /*
170 * if rate blocks is set, sample is running
171 */
172 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
173 spent = mtime_since(&td->lastrate[ddir], now);
174 if (spent < td->o.ratecycle)
175 return 0;
176
177 if (td->o.rate[ddir]) {
178 /*
179 * check bandwidth specified rate
180 */
181 if (bytes < td->rate_bytes[ddir]) {
182 log_err("%s: min rate %u not met\n", td->o.name,
183 ratemin);
184 return 1;
185 } else {
Jens Axboe49cba9b2014-04-14 09:51:10 -0600186 if (spent)
187 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
188 else
189 rate = 0;
190
Jens Axboe2e1df072012-02-09 11:15:02 +0100191 if (rate < ratemin ||
192 bytes < td->rate_bytes[ddir]) {
193 log_err("%s: min rate %u not met, got"
194 " %luKB/sec\n", td->o.name,
195 ratemin, rate);
196 return 1;
197 }
198 }
199 } else {
200 /*
201 * checks iops specified rate
202 */
203 if (iops < rate_iops) {
204 log_err("%s: min iops rate %u not met\n",
205 td->o.name, rate_iops);
206 return 1;
207 } else {
Jens Axboe4c707a32014-04-15 08:26:57 -0600208 if (spent)
209 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
210 else
211 rate = 0;
212
Jens Axboe2e1df072012-02-09 11:15:02 +0100213 if (rate < rate_iops_min ||
214 iops < td->rate_blocks[ddir]) {
215 log_err("%s: min iops rate %u not met,"
216 " got %lu\n", td->o.name,
217 rate_iops_min, rate);
218 }
219 }
220 }
221 }
222
223 td->rate_bytes[ddir] = bytes;
224 td->rate_blocks[ddir] = iops;
225 memcpy(&td->lastrate[ddir], now, sizeof(*now));
226 return 0;
227}
228
229static int check_min_rate(struct thread_data *td, struct timeval *now,
Jens Axboe100f49f2013-01-23 10:15:57 -0700230 uint64_t *bytes_done)
Jens Axboe2e1df072012-02-09 11:15:02 +0100231{
232 int ret = 0;
233
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200234 if (bytes_done[DDIR_READ])
235 ret |= __check_min_rate(td, now, DDIR_READ);
236 if (bytes_done[DDIR_WRITE])
237 ret |= __check_min_rate(td, now, DDIR_WRITE);
238 if (bytes_done[DDIR_TRIM])
239 ret |= __check_min_rate(td, now, DDIR_TRIM);
Jens Axboe2e1df072012-02-09 11:15:02 +0100240
241 return ret;
242}
243
244/*
245 * When job exits, we can cancel the in-flight IO if we are using async
246 * io. Attempt to do so.
247 */
248static void cleanup_pending_aio(struct thread_data *td)
249{
Jens Axboe2e1df072012-02-09 11:15:02 +0100250 int r;
251
252 /*
253 * get immediately available events, if any
254 */
255 r = io_u_queued_complete(td, 0, NULL);
256 if (r < 0)
257 return;
258
259 /*
260 * now cancel remaining active events
261 */
262 if (td->io_ops->cancel) {
Jens Axboe2ae0b202013-05-28 14:16:55 +0200263 struct io_u *io_u;
264 int i;
Jens Axboe2e1df072012-02-09 11:15:02 +0100265
Jens Axboe2ae0b202013-05-28 14:16:55 +0200266 io_u_qiter(&td->io_u_all, io_u, i) {
267 if (io_u->flags & IO_U_F_FLIGHT) {
Jens Axboe2e1df072012-02-09 11:15:02 +0100268 r = td->io_ops->cancel(td, io_u);
269 if (!r)
270 put_io_u(td, io_u);
271 }
272 }
273 }
274
275 if (td->cur_depth)
276 r = io_u_queued_complete(td, td->cur_depth, NULL);
277}
278
279/*
280 * Helper to handle the final sync of a file. Works just like the normal
281 * io path, just does everything sync.
282 */
283static int fio_io_sync(struct thread_data *td, struct fio_file *f)
284{
285 struct io_u *io_u = __get_io_u(td);
286 int ret;
287
288 if (!io_u)
289 return 1;
290
291 io_u->ddir = DDIR_SYNC;
292 io_u->file = f;
293
294 if (td_io_prep(td, io_u)) {
295 put_io_u(td, io_u);
296 return 1;
297 }
298
299requeue:
300 ret = td_io_queue(td, io_u);
301 if (ret < 0) {
302 td_verror(td, io_u->error, "td_io_queue");
303 put_io_u(td, io_u);
304 return 1;
305 } else if (ret == FIO_Q_QUEUED) {
306 if (io_u_queued_complete(td, 1, NULL) < 0)
307 return 1;
308 } else if (ret == FIO_Q_COMPLETED) {
309 if (io_u->error) {
310 td_verror(td, io_u->error, "td_io_queue");
311 return 1;
312 }
313
314 if (io_u_sync_complete(td, io_u, NULL) < 0)
315 return 1;
316 } else if (ret == FIO_Q_BUSY) {
317 if (td_io_commit(td))
318 return 1;
319 goto requeue;
320 }
321
322 return 0;
323}
Jens Axboea3efc912012-02-09 11:25:24 +0100324
Jens Axboe61ee0f82013-02-11 11:26:55 +0100325static int fio_file_fsync(struct thread_data *td, struct fio_file *f)
326{
327 int ret;
328
329 if (fio_file_open(f))
330 return fio_io_sync(td, f);
331
332 if (td_io_open_file(td, f))
333 return 1;
334
335 ret = fio_io_sync(td, f);
336 td_io_close_file(td, f);
337 return ret;
338}
339
Jens Axboe2e1df072012-02-09 11:15:02 +0100340static inline void __update_tv_cache(struct thread_data *td)
341{
342 fio_gettime(&td->tv_cache, NULL);
343}
344
345static inline void update_tv_cache(struct thread_data *td)
346{
347 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
348 __update_tv_cache(td);
349}
350
351static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
352{
353 if (in_ramp_time(td))
354 return 0;
355 if (!td->o.timeout)
356 return 0;
Jens Axboe0de5b262014-02-21 15:26:01 -0800357 if (utime_since(&td->epoch, t) >= td->o.timeout)
Jens Axboe2e1df072012-02-09 11:15:02 +0100358 return 1;
359
360 return 0;
361}
362
363static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
364 int *retptr)
365{
366 int ret = *retptr;
367
368 if (ret < 0 || td->error) {
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +0400369 int err = td->error;
370 enum error_type_bit eb;
Jens Axboe2e1df072012-02-09 11:15:02 +0100371
372 if (ret < 0)
373 err = -ret;
Jens Axboe2e1df072012-02-09 11:15:02 +0100374
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +0400375 eb = td_error_type(ddir, err);
376 if (!(td->o.continue_on_error & (1 << eb)))
Jens Axboe2e1df072012-02-09 11:15:02 +0100377 return 1;
378
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +0400379 if (td_non_fatal_error(td, eb, err)) {
Jens Axboe2e1df072012-02-09 11:15:02 +0100380 /*
381 * Continue with the I/Os in case of
382 * a non fatal error.
383 */
384 update_error_count(td, err);
385 td_clear_error(td);
386 *retptr = 0;
387 return 0;
388 } else if (td->o.fill_device && err == ENOSPC) {
389 /*
390 * We expect to hit this error if
391 * fill_device option is set.
392 */
393 td_clear_error(td);
Jens Axboe3b44e8b2014-07-09 11:24:12 +0200394 fio_mark_td_terminate(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100395 return 1;
396 } else {
397 /*
398 * Stop the I/O in case of a fatal
399 * error.
400 */
401 update_error_count(td, err);
402 return 1;
403 }
404 }
405
406 return 0;
407}
408
Jens Axboec97f1ad2013-03-28 15:08:49 -0600409static void check_update_rusage(struct thread_data *td)
410{
411 if (td->update_rusage) {
412 td->update_rusage = 0;
413 update_rusage_stat(td);
414 fio_mutex_up(td->rusage_sem);
415 }
416}
417
Jens Axboe2e1df072012-02-09 11:15:02 +0100418/*
419 * The main verify engine. Runs over the writes we previously submitted,
420 * reads the blocks back in, and checks the crc/md5 of the data.
421 */
Jens Axboe100f49f2013-01-23 10:15:57 -0700422static void do_verify(struct thread_data *td, uint64_t verify_bytes)
Jens Axboe2e1df072012-02-09 11:15:02 +0100423{
Jens Axboe100f49f2013-01-23 10:15:57 -0700424 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
Jens Axboe2e1df072012-02-09 11:15:02 +0100425 struct fio_file *f;
426 struct io_u *io_u;
427 int ret, min_events;
428 unsigned int i;
429
430 dprint(FD_VERIFY, "starting loop\n");
431
432 /*
433 * sync io first and invalidate cache, to make sure we really
434 * read from disk.
435 */
436 for_each_file(td, f, i) {
437 if (!fio_file_open(f))
438 continue;
439 if (fio_io_sync(td, f))
440 break;
441 if (file_invalidate_cache(td, f))
442 break;
443 }
444
Jens Axboec97f1ad2013-03-28 15:08:49 -0600445 check_update_rusage(td);
446
Jens Axboe2e1df072012-02-09 11:15:02 +0100447 if (td->error)
448 return;
449
450 td_set_runstate(td, TD_VERIFYING);
451
452 io_u = NULL;
453 while (!td->terminate) {
Jens Axboefbccf462013-01-08 21:02:14 +0100454 enum fio_ddir ddir;
Jens Axboe2e1df072012-02-09 11:15:02 +0100455 int ret2, full;
456
457 update_tv_cache(td);
Jens Axboec97f1ad2013-03-28 15:08:49 -0600458 check_update_rusage(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100459
460 if (runtime_exceeded(td, &td->tv_cache)) {
461 __update_tv_cache(td);
462 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe3b44e8b2014-07-09 11:24:12 +0200463 fio_mark_td_terminate(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100464 break;
465 }
466 }
467
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100468 if (flow_threshold_exceeded(td))
469 continue;
470
Jens Axboe44cbc6d2013-01-21 09:47:03 -0700471 if (!td->o.experimental_verify) {
472 io_u = __get_io_u(td);
473 if (!io_u)
474 break;
Jens Axboe2e1df072012-02-09 11:15:02 +0100475
Jens Axboe44cbc6d2013-01-21 09:47:03 -0700476 if (get_next_verify(td, io_u)) {
477 put_io_u(td, io_u);
478 break;
479 }
Jens Axboe2e1df072012-02-09 11:15:02 +0100480
Jens Axboe44cbc6d2013-01-21 09:47:03 -0700481 if (td_io_prep(td, io_u)) {
482 put_io_u(td, io_u);
483 break;
484 }
485 } else {
Jens Axboe100f49f2013-01-23 10:15:57 -0700486 if (ddir_rw_sum(bytes_done) + td->o.rw_min_bs > verify_bytes)
487 break;
488
Jens Axboebcd5abf2013-01-23 09:27:25 -0700489 while ((io_u = get_io_u(td)) != NULL) {
Jens Axboe002fe732014-02-11 08:31:13 -0700490 if (IS_ERR(io_u)) {
491 io_u = NULL;
492 ret = FIO_Q_BUSY;
493 goto reap;
494 }
495
Jens Axboebcd5abf2013-01-23 09:27:25 -0700496 /*
497 * We are only interested in the places where
498 * we wrote or trimmed IOs. Turn those into
499 * reads for verification purposes.
500 */
501 if (io_u->ddir == DDIR_READ) {
502 /*
503 * Pretend we issued it for rwmix
504 * accounting
505 */
506 td->io_issues[DDIR_READ]++;
507 put_io_u(td, io_u);
508 continue;
509 } else if (io_u->ddir == DDIR_TRIM) {
510 io_u->ddir = DDIR_READ;
511 io_u->flags |= IO_U_F_TRIMMED;
512 break;
513 } else if (io_u->ddir == DDIR_WRITE) {
514 io_u->ddir = DDIR_READ;
515 break;
516 } else {
517 put_io_u(td, io_u);
518 continue;
519 }
520 }
Jens Axboe44cbc6d2013-01-21 09:47:03 -0700521
Jens Axboebcd5abf2013-01-23 09:27:25 -0700522 if (!io_u)
Jens Axboe44cbc6d2013-01-21 09:47:03 -0700523 break;
Jens Axboe2e1df072012-02-09 11:15:02 +0100524 }
525
526 if (td->o.verify_async)
527 io_u->end_io = verify_io_u_async;
528 else
529 io_u->end_io = verify_io_u;
530
Jens Axboefbccf462013-01-08 21:02:14 +0100531 ddir = io_u->ddir;
532
Jens Axboe2e1df072012-02-09 11:15:02 +0100533 ret = td_io_queue(td, io_u);
534 switch (ret) {
535 case FIO_Q_COMPLETED:
536 if (io_u->error) {
537 ret = -io_u->error;
538 clear_io_u(td, io_u);
539 } else if (io_u->resid) {
540 int bytes = io_u->xfer_buflen - io_u->resid;
541
542 /*
543 * zero read, fail
544 */
545 if (!bytes) {
546 td_verror(td, EIO, "full resid");
547 put_io_u(td, io_u);
548 break;
549 }
550
551 io_u->xfer_buflen = io_u->resid;
552 io_u->xfer_buf += bytes;
553 io_u->offset += bytes;
554
555 if (ddir_rw(io_u->ddir))
556 td->ts.short_io_u[io_u->ddir]++;
557
558 f = io_u->file;
559 if (io_u->offset == f->real_file_size)
560 goto sync_done;
561
562 requeue_io_u(td, &io_u);
563 } else {
564sync_done:
Jens Axboe100f49f2013-01-23 10:15:57 -0700565 ret = io_u_sync_complete(td, io_u, bytes_done);
Jens Axboe2e1df072012-02-09 11:15:02 +0100566 if (ret < 0)
567 break;
568 }
569 continue;
570 case FIO_Q_QUEUED:
571 break;
572 case FIO_Q_BUSY:
573 requeue_io_u(td, &io_u);
574 ret2 = td_io_commit(td);
575 if (ret2 < 0)
576 ret = ret2;
577 break;
578 default:
579 assert(ret < 0);
580 td_verror(td, -ret, "td_io_queue");
581 break;
582 }
583
Jens Axboefbccf462013-01-08 21:02:14 +0100584 if (break_on_this_error(td, ddir, &ret))
Jens Axboe2e1df072012-02-09 11:15:02 +0100585 break;
586
587 /*
588 * if we can queue more, do so. but check if there are
589 * completed io_u's first. Note that we can get BUSY even
590 * without IO queued, if the system is resource starved.
591 */
Jens Axboe002fe732014-02-11 08:31:13 -0700592reap:
Jens Axboe2e1df072012-02-09 11:15:02 +0100593 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
594 if (full || !td->o.iodepth_batch_complete) {
595 min_events = min(td->o.iodepth_batch_complete,
596 td->cur_depth);
Jens Axboe8a74b562012-03-16 13:55:50 +0100597 /*
598 * if the queue is full, we MUST reap at least 1 event
599 */
600 if (full && !min_events)
Jens Axboe2e1df072012-02-09 11:15:02 +0100601 min_events = 1;
602
603 do {
604 /*
605 * Reap required number of io units, if any,
606 * and do the verification on them through
607 * the callback handler
608 */
Jens Axboe100f49f2013-01-23 10:15:57 -0700609 if (io_u_queued_complete(td, min_events, bytes_done) < 0) {
Jens Axboe2e1df072012-02-09 11:15:02 +0100610 ret = -1;
611 break;
612 }
613 } while (full && (td->cur_depth > td->o.iodepth_low));
614 }
615 if (ret < 0)
616 break;
617 }
618
Jens Axboec97f1ad2013-03-28 15:08:49 -0600619 check_update_rusage(td);
620
Jens Axboe2e1df072012-02-09 11:15:02 +0100621 if (!td->error) {
622 min_events = td->cur_depth;
623
624 if (min_events)
625 ret = io_u_queued_complete(td, min_events, NULL);
626 } else
627 cleanup_pending_aio(td);
628
629 td_set_runstate(td, TD_RUNNING);
630
631 dprint(FD_VERIFY, "exiting loop\n");
632}
633
Jens Axboe3939fe82014-03-11 20:16:38 -0600634static unsigned int exceeds_number_ios(struct thread_data *td)
635{
636 unsigned long long number_ios;
637
638 if (!td->o.number_ios)
639 return 0;
640
641 number_ios = ddir_rw_sum(td->this_io_blocks);
642 number_ios += td->io_u_queued + td->io_u_in_flight;
643
644 return number_ios >= td->o.number_ios;
645}
646
Jens Axboef7078f72012-03-07 09:24:05 +0100647static int io_bytes_exceeded(struct thread_data *td)
648{
Jens Axboe77731b22014-04-28 12:08:47 -0600649 unsigned long long bytes, limit;
Jens Axboef7078f72012-03-07 09:24:05 +0100650
651 if (td_rw(td))
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200652 bytes = td->this_io_bytes[DDIR_READ] + td->this_io_bytes[DDIR_WRITE];
Jens Axboef7078f72012-03-07 09:24:05 +0100653 else if (td_write(td))
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200654 bytes = td->this_io_bytes[DDIR_WRITE];
655 else if (td_read(td))
656 bytes = td->this_io_bytes[DDIR_READ];
Jens Axboef7078f72012-03-07 09:24:05 +0100657 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200658 bytes = td->this_io_bytes[DDIR_TRIM];
Jens Axboef7078f72012-03-07 09:24:05 +0100659
Jens Axboe77731b22014-04-28 12:08:47 -0600660 if (td->o.io_limit)
661 limit = td->o.io_limit;
662 else
663 limit = td->o.size;
664
665 return bytes >= limit || exceeds_number_ios(td);
Jens Axboef7078f72012-03-07 09:24:05 +0100666}
667
Jens Axboe2e1df072012-02-09 11:15:02 +0100668/*
669 * Main IO worker function. It retrieves io_u's to process and queues
670 * and reaps them, checking for rate and errors along the way.
Jens Axboe100f49f2013-01-23 10:15:57 -0700671 *
672 * Returns number of bytes written and trimmed.
Jens Axboe2e1df072012-02-09 11:15:02 +0100673 */
Jens Axboe100f49f2013-01-23 10:15:57 -0700674static uint64_t do_io(struct thread_data *td)
Jens Axboe2e1df072012-02-09 11:15:02 +0100675{
Jens Axboe100f49f2013-01-23 10:15:57 -0700676 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
Jens Axboe2e1df072012-02-09 11:15:02 +0100677 unsigned int i;
678 int ret = 0;
Jens Axboec2703bf2014-02-05 12:36:02 -0700679 uint64_t total_bytes, bytes_issued = 0;
Jens Axboe2e1df072012-02-09 11:15:02 +0100680
681 if (in_ramp_time(td))
682 td_set_runstate(td, TD_RAMP);
683 else
684 td_set_runstate(td, TD_RUNNING);
685
Jens Axboe3e260a42013-12-09 12:38:53 -0700686 lat_target_init(td);
687
Jens Axboe78a64692014-02-05 13:15:22 -0700688 /*
689 * If verify_backlog is enabled, we'll run the verify in this
690 * handler as well. For that case, we may need up to twice the
691 * amount of bytes.
692 */
Jens Axboec2703bf2014-02-05 12:36:02 -0700693 total_bytes = td->o.size;
Jens Axboe78a64692014-02-05 13:15:22 -0700694 if (td->o.verify != VERIFY_NONE &&
695 (td_write(td) && td->o.verify_backlog))
Jens Axboec2703bf2014-02-05 12:36:02 -0700696 total_bytes += td->o.size;
697
Jens Axboef7078f72012-03-07 09:24:05 +0100698 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
Daniel Ehrenbergc04e4662012-03-16 18:54:15 +0100699 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
700 td->o.time_based) {
Jens Axboe2e1df072012-02-09 11:15:02 +0100701 struct timeval comp_time;
Jens Axboe2e1df072012-02-09 11:15:02 +0100702 int min_evts = 0;
703 struct io_u *io_u;
704 int ret2, full;
705 enum fio_ddir ddir;
706
Jens Axboec97f1ad2013-03-28 15:08:49 -0600707 check_update_rusage(td);
708
Yufei Ren7d7803f2012-10-22 21:29:38 -0400709 if (td->terminate || td->done)
Jens Axboe2e1df072012-02-09 11:15:02 +0100710 break;
711
712 update_tv_cache(td);
713
714 if (runtime_exceeded(td, &td->tv_cache)) {
715 __update_tv_cache(td);
716 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe3b44e8b2014-07-09 11:24:12 +0200717 fio_mark_td_terminate(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100718 break;
719 }
720 }
721
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100722 if (flow_threshold_exceeded(td))
723 continue;
724
Jens Axboec2703bf2014-02-05 12:36:02 -0700725 if (bytes_issued >= total_bytes)
Juan Casse20876c52013-09-16 09:22:10 -0700726 break;
727
Jens Axboe2e1df072012-02-09 11:15:02 +0100728 io_u = get_io_u(td);
Jens Axboe002fe732014-02-11 08:31:13 -0700729 if (IS_ERR_OR_NULL(io_u)) {
730 int err = PTR_ERR(io_u);
731
732 io_u = NULL;
733 if (err == -EBUSY) {
734 ret = FIO_Q_BUSY;
735 goto reap;
736 }
Jens Axboe3e260a42013-12-09 12:38:53 -0700737 if (td->o.latency_target)
738 goto reap;
Jens Axboe2e1df072012-02-09 11:15:02 +0100739 break;
Jens Axboe3e260a42013-12-09 12:38:53 -0700740 }
Jens Axboe2e1df072012-02-09 11:15:02 +0100741
742 ddir = io_u->ddir;
743
744 /*
Jens Axboe82af2a72012-03-13 13:45:58 +0100745 * Add verification end_io handler if:
746 * - Asked to verify (!td_rw(td))
747 * - Or the io_u is from our verify list (mixed write/ver)
Jens Axboe2e1df072012-02-09 11:15:02 +0100748 */
749 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
Jens Axboe82af2a72012-03-13 13:45:58 +0100750 ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700751
752 if (!td->o.verify_pattern_bytes) {
753 io_u->rand_seed = __rand(&td->__verify_state);
754 if (sizeof(int) != sizeof(long *))
755 io_u->rand_seed *= __rand(&td->__verify_state);
756 }
757
Jens Axboe2e1df072012-02-09 11:15:02 +0100758 if (td->o.verify_async)
759 io_u->end_io = verify_io_u_async;
760 else
761 io_u->end_io = verify_io_u;
762 td_set_runstate(td, TD_VERIFYING);
763 } else if (in_ramp_time(td))
764 td_set_runstate(td, TD_RAMP);
765 else
766 td_set_runstate(td, TD_RUNNING);
767
Puthikorn Voravootivat9a50c5c2014-02-05 10:28:15 -0800768 /*
Jens Axboef9401282014-02-06 12:17:37 -0700769 * Always log IO before it's issued, so we know the specific
770 * order of it. The logged unit will track when the IO has
771 * completed.
Puthikorn Voravootivat9a50c5c2014-02-05 10:28:15 -0800772 */
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700773 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
774 td->o.do_verify &&
775 td->o.verify != VERIFY_NONE &&
Jens Axboef9401282014-02-06 12:17:37 -0700776 !td->o.experimental_verify)
Puthikorn Voravootivatc4b61172014-02-05 10:17:11 -0700777 log_io_piece(td, io_u);
778
Jens Axboe2e1df072012-02-09 11:15:02 +0100779 ret = td_io_queue(td, io_u);
780 switch (ret) {
781 case FIO_Q_COMPLETED:
782 if (io_u->error) {
783 ret = -io_u->error;
Jens Axboe890b6652014-05-06 19:06:51 -0600784 unlog_io_piece(td, io_u);
Jens Axboe2e1df072012-02-09 11:15:02 +0100785 clear_io_u(td, io_u);
786 } else if (io_u->resid) {
787 int bytes = io_u->xfer_buflen - io_u->resid;
788 struct fio_file *f = io_u->file;
789
Juan Casse20876c52013-09-16 09:22:10 -0700790 bytes_issued += bytes;
Jens Axboe890b6652014-05-06 19:06:51 -0600791
792 trim_io_piece(td, io_u);
793
Jens Axboe2e1df072012-02-09 11:15:02 +0100794 /*
795 * zero read, fail
796 */
797 if (!bytes) {
Jens Axboe890b6652014-05-06 19:06:51 -0600798 unlog_io_piece(td, io_u);
Jens Axboe2e1df072012-02-09 11:15:02 +0100799 td_verror(td, EIO, "full resid");
800 put_io_u(td, io_u);
801 break;
802 }
803
804 io_u->xfer_buflen = io_u->resid;
805 io_u->xfer_buf += bytes;
806 io_u->offset += bytes;
807
808 if (ddir_rw(io_u->ddir))
809 td->ts.short_io_u[io_u->ddir]++;
810
811 if (io_u->offset == f->real_file_size)
812 goto sync_done;
813
814 requeue_io_u(td, &io_u);
815 } else {
816sync_done:
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200817 if (__should_check_rate(td, DDIR_READ) ||
818 __should_check_rate(td, DDIR_WRITE) ||
819 __should_check_rate(td, DDIR_TRIM))
Jens Axboe2e1df072012-02-09 11:15:02 +0100820 fio_gettime(&comp_time, NULL);
821
822 ret = io_u_sync_complete(td, io_u, bytes_done);
823 if (ret < 0)
824 break;
Juan Casse20876c52013-09-16 09:22:10 -0700825 bytes_issued += io_u->xfer_buflen;
Jens Axboe2e1df072012-02-09 11:15:02 +0100826 }
827 break;
828 case FIO_Q_QUEUED:
829 /*
830 * if the engine doesn't have a commit hook,
831 * the io_u is really queued. if it does have such
832 * a hook, it has to call io_u_queued() itself.
833 */
834 if (td->io_ops->commit == NULL)
835 io_u_queued(td, io_u);
Juan Casse20876c52013-09-16 09:22:10 -0700836 bytes_issued += io_u->xfer_buflen;
Jens Axboe2e1df072012-02-09 11:15:02 +0100837 break;
838 case FIO_Q_BUSY:
Jens Axboe890b6652014-05-06 19:06:51 -0600839 unlog_io_piece(td, io_u);
Jens Axboe2e1df072012-02-09 11:15:02 +0100840 requeue_io_u(td, &io_u);
841 ret2 = td_io_commit(td);
842 if (ret2 < 0)
843 ret = ret2;
844 break;
845 default:
846 assert(ret < 0);
847 put_io_u(td, io_u);
848 break;
849 }
850
851 if (break_on_this_error(td, ddir, &ret))
852 break;
853
854 /*
855 * See if we need to complete some commands. Note that we
856 * can get BUSY even without IO queued, if the system is
857 * resource starved.
858 */
Jens Axboe3e260a42013-12-09 12:38:53 -0700859reap:
Jens Axboe2e1df072012-02-09 11:15:02 +0100860 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
861 if (full || !td->o.iodepth_batch_complete) {
862 min_evts = min(td->o.iodepth_batch_complete,
863 td->cur_depth);
Jens Axboe8a74b562012-03-16 13:55:50 +0100864 /*
865 * if the queue is full, we MUST reap at least 1 event
866 */
867 if (full && !min_evts)
Jens Axboe2e1df072012-02-09 11:15:02 +0100868 min_evts = 1;
869
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200870 if (__should_check_rate(td, DDIR_READ) ||
871 __should_check_rate(td, DDIR_WRITE) ||
872 __should_check_rate(td, DDIR_TRIM))
Jens Axboe2e1df072012-02-09 11:15:02 +0100873 fio_gettime(&comp_time, NULL);
874
875 do {
876 ret = io_u_queued_complete(td, min_evts, bytes_done);
877 if (ret < 0)
878 break;
879
880 } while (full && (td->cur_depth > td->o.iodepth_low));
881 }
882
883 if (ret < 0)
884 break;
Jens Axboed5abee02012-10-22 20:36:26 +0200885 if (!ddir_rw_sum(bytes_done) && !(td->io_ops->flags & FIO_NOIO))
Jens Axboe2e1df072012-02-09 11:15:02 +0100886 continue;
887
888 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
889 if (check_min_rate(td, &comp_time, bytes_done)) {
890 if (exitall_on_terminate)
891 fio_terminate_threads(td->groupid);
892 td_verror(td, EIO, "check_min_rate");
893 break;
894 }
895 }
Jens Axboe3e260a42013-12-09 12:38:53 -0700896 if (!in_ramp_time(td) && td->o.latency_target)
897 lat_target_check(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100898
899 if (td->o.thinktime) {
900 unsigned long long b;
901
Jens Axboe342f4be2012-09-14 08:59:20 +0200902 b = ddir_rw_sum(td->io_blocks);
Jens Axboe2e1df072012-02-09 11:15:02 +0100903 if (!(b % td->o.thinktime_blocks)) {
904 int left;
905
Jens Axboe002e7182013-05-17 12:39:53 +0200906 io_u_quiesce(td);
907
Jens Axboe2e1df072012-02-09 11:15:02 +0100908 if (td->o.thinktime_spin)
909 usec_spin(td->o.thinktime_spin);
910
911 left = td->o.thinktime - td->o.thinktime_spin;
912 if (left)
913 usec_sleep(td, left);
914 }
915 }
916 }
917
Jens Axboec97f1ad2013-03-28 15:08:49 -0600918 check_update_rusage(td);
919
Jens Axboe2e1df072012-02-09 11:15:02 +0100920 if (td->trim_entries)
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200921 log_err("fio: %lu trim entries leaked?\n", td->trim_entries);
Jens Axboe2e1df072012-02-09 11:15:02 +0100922
923 if (td->o.fill_device && td->error == ENOSPC) {
924 td->error = 0;
Jens Axboe3b44e8b2014-07-09 11:24:12 +0200925 fio_mark_td_terminate(td);
Jens Axboe2e1df072012-02-09 11:15:02 +0100926 }
927 if (!td->error) {
928 struct fio_file *f;
929
930 i = td->cur_depth;
931 if (i) {
Jens Axboe5bd5f712013-02-03 14:20:44 +0100932 ret = io_u_queued_complete(td, i, bytes_done);
Jens Axboe2e1df072012-02-09 11:15:02 +0100933 if (td->o.fill_device && td->error == ENOSPC)
934 td->error = 0;
935 }
936
937 if (should_fsync(td) && td->o.end_fsync) {
938 td_set_runstate(td, TD_FSYNCING);
939
940 for_each_file(td, f, i) {
Jens Axboe61ee0f82013-02-11 11:26:55 +0100941 if (!fio_file_fsync(td, f))
Jens Axboe2e1df072012-02-09 11:15:02 +0100942 continue;
Jens Axboe61ee0f82013-02-11 11:26:55 +0100943
944 log_err("fio: end_fsync failed for file %s\n",
945 f->file_name);
Jens Axboe2e1df072012-02-09 11:15:02 +0100946 }
947 }
948 } else
949 cleanup_pending_aio(td);
950
951 /*
952 * stop job if we failed doing any IO
953 */
Jens Axboe342f4be2012-09-14 08:59:20 +0200954 if (!ddir_rw_sum(td->this_io_bytes))
Jens Axboe2e1df072012-02-09 11:15:02 +0100955 td->done = 1;
Jens Axboe100f49f2013-01-23 10:15:57 -0700956
957 return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
Jens Axboe2e1df072012-02-09 11:15:02 +0100958}
959
960static void cleanup_io_u(struct thread_data *td)
961{
Jens Axboe2e1df072012-02-09 11:15:02 +0100962 struct io_u *io_u;
963
Jens Axboe2ae0b202013-05-28 14:16:55 +0200964 while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
Jens Axboec73ed242012-12-06 20:30:38 +0100965
966 if (td->io_ops->io_u_free)
967 td->io_ops->io_u_free(td, io_u);
968
Jens Axboe2e1df072012-02-09 11:15:02 +0100969 fio_memfree(io_u, sizeof(*io_u));
970 }
971
972 free_io_mem(td);
Jens Axboe2ae0b202013-05-28 14:16:55 +0200973
974 io_u_rexit(&td->io_u_requeues);
975 io_u_qexit(&td->io_u_freelist);
976 io_u_qexit(&td->io_u_all);
Jens Axboe2e1df072012-02-09 11:15:02 +0100977}
978
979static int init_io_u(struct thread_data *td)
980{
981 struct io_u *io_u;
Jens Axboe9c426842012-03-02 21:02:12 +0100982 unsigned int max_bs, min_write;
Jens Axboe2e1df072012-02-09 11:15:02 +0100983 int cl_align, i, max_units;
Jens Axboe2ae0b202013-05-28 14:16:55 +0200984 int data_xfer = 1, err;
Jens Axboe2e1df072012-02-09 11:15:02 +0100985 char *p;
986
987 max_units = td->o.iodepth;
Jens Axboe74f4b022013-03-22 22:56:55 -0600988 max_bs = td_max_bs(td);
Jens Axboe9c426842012-03-02 21:02:12 +0100989 min_write = td->o.min_bs[DDIR_WRITE];
Jens Axboe2e1df072012-02-09 11:15:02 +0100990 td->orig_buffer_size = (unsigned long long) max_bs
991 * (unsigned long long) max_units;
992
Dmitry Monakhov88045e02012-09-25 12:12:36 +0200993 if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
Jens Axboe59d8d0f2012-09-24 14:25:33 +0200994 data_xfer = 0;
995
Jens Axboe2ae0b202013-05-28 14:16:55 +0200996 err = 0;
997 err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
998 err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
999 err += io_u_qinit(&td->io_u_all, td->o.iodepth);
1000
1001 if (err) {
1002 log_err("fio: failed setting up IO queues\n");
1003 return 1;
1004 }
1005
peter changfd8a09b2013-04-24 16:32:25 -06001006 /*
1007 * if we may later need to do address alignment, then add any
1008 * possible adjustment here so that we don't cause a buffer
1009 * overflow later. this adjustment may be too much if we get
1010 * lucky and the allocator gives us an aligned address.
1011 */
Chris Masond01612f2013-11-15 15:52:58 -07001012 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
1013 (td->io_ops->flags & FIO_RAWIO))
peter changfd8a09b2013-04-24 16:32:25 -06001014 td->orig_buffer_size += page_mask + td->o.mem_align;
1015
Jens Axboe2e1df072012-02-09 11:15:02 +01001016 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
1017 unsigned long bs;
1018
1019 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
1020 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
1021 }
1022
1023 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
1024 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
1025 return 1;
1026 }
1027
Jens Axboe59d8d0f2012-09-24 14:25:33 +02001028 if (data_xfer && allocate_io_mem(td))
Jens Axboe2e1df072012-02-09 11:15:02 +01001029 return 1;
1030
Chris Masond01612f2013-11-15 15:52:58 -07001031 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
Jens Axboe2e1df072012-02-09 11:15:02 +01001032 (td->io_ops->flags & FIO_RAWIO))
1033 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
1034 else
1035 p = td->orig_buffer;
1036
1037 cl_align = os_cache_line_size();
1038
1039 for (i = 0; i < max_units; i++) {
1040 void *ptr;
1041
1042 if (td->terminate)
1043 return 1;
1044
1045 ptr = fio_memalign(cl_align, sizeof(*io_u));
1046 if (!ptr) {
1047 log_err("fio: unable to allocate aligned memory\n");
1048 break;
1049 }
1050
1051 io_u = ptr;
1052 memset(io_u, 0, sizeof(*io_u));
Jens Axboe2ae0b202013-05-28 14:16:55 +02001053 INIT_FLIST_HEAD(&io_u->verify_list);
Jens Axboe2e1df072012-02-09 11:15:02 +01001054 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
1055
Jens Axboe59d8d0f2012-09-24 14:25:33 +02001056 if (data_xfer) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001057 io_u->buf = p;
1058 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
1059
1060 if (td_write(td))
Jens Axboe9c426842012-03-02 21:02:12 +01001061 io_u_fill_buffer(td, io_u, min_write, max_bs);
Jens Axboe2e1df072012-02-09 11:15:02 +01001062 if (td_write(td) && td->o.verify_pattern_bytes) {
1063 /*
1064 * Fill the buffer with the pattern if we are
1065 * going to be doing writes.
1066 */
Jens Axboece35b1e2014-01-14 15:35:58 -07001067 fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
Jens Axboe2e1df072012-02-09 11:15:02 +01001068 }
1069 }
1070
1071 io_u->index = i;
1072 io_u->flags = IO_U_F_FREE;
Jens Axboe2ae0b202013-05-28 14:16:55 +02001073 io_u_qpush(&td->io_u_freelist, io_u);
1074
1075 /*
1076 * io_u never leaves this stack, used for iteration of all
1077 * io_u buffers.
1078 */
1079 io_u_qpush(&td->io_u_all, io_u);
Jens Axboec73ed242012-12-06 20:30:38 +01001080
1081 if (td->io_ops->io_u_init) {
1082 int ret = td->io_ops->io_u_init(td, io_u);
1083
1084 if (ret) {
1085 log_err("fio: failed to init engine data: %d\n", ret);
1086 return 1;
1087 }
1088 }
1089
Jens Axboe2e1df072012-02-09 11:15:02 +01001090 p += max_bs;
1091 }
1092
1093 return 0;
1094}
1095
1096static int switch_ioscheduler(struct thread_data *td)
1097{
1098 char tmp[256], tmp2[128];
1099 FILE *f;
1100 int ret;
1101
1102 if (td->io_ops->flags & FIO_DISKLESSIO)
1103 return 0;
1104
1105 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1106
1107 f = fopen(tmp, "r+");
1108 if (!f) {
1109 if (errno == ENOENT) {
1110 log_err("fio: os or kernel doesn't support IO scheduler"
1111 " switching\n");
1112 return 0;
1113 }
1114 td_verror(td, errno, "fopen iosched");
1115 return 1;
1116 }
1117
1118 /*
1119 * Set io scheduler.
1120 */
1121 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
1122 if (ferror(f) || ret != 1) {
1123 td_verror(td, errno, "fwrite");
1124 fclose(f);
1125 return 1;
1126 }
1127
1128 rewind(f);
1129
1130 /*
1131 * Read back and check that the selected scheduler is now the default.
1132 */
Jens Axboe49c6f332014-04-14 11:36:01 -06001133 ret = fread(tmp, sizeof(tmp), 1, f);
Jens Axboe2e1df072012-02-09 11:15:02 +01001134 if (ferror(f) || ret < 0) {
1135 td_verror(td, errno, "fread");
1136 fclose(f);
1137 return 1;
1138 }
Jens Axboe49c6f332014-04-14 11:36:01 -06001139 tmp[sizeof(tmp) - 1] = '\0';
1140
Jens Axboe2e1df072012-02-09 11:15:02 +01001141
1142 sprintf(tmp2, "[%s]", td->o.ioscheduler);
1143 if (!strstr(tmp, tmp2)) {
1144 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
1145 td_verror(td, EINVAL, "iosched_switch");
1146 fclose(f);
1147 return 1;
1148 }
1149
1150 fclose(f);
1151 return 0;
1152}
1153
1154static int keep_running(struct thread_data *td)
1155{
Jens Axboe77731b22014-04-28 12:08:47 -06001156 unsigned long long limit;
1157
Jens Axboe2e1df072012-02-09 11:15:02 +01001158 if (td->done)
1159 return 0;
1160 if (td->o.time_based)
1161 return 1;
1162 if (td->o.loops) {
1163 td->o.loops--;
1164 return 1;
1165 }
Jens Axboe3939fe82014-03-11 20:16:38 -06001166 if (exceeds_number_ios(td))
1167 return 0;
Jens Axboe26251d82014-03-11 10:21:18 -06001168
Jens Axboe77731b22014-04-28 12:08:47 -06001169 if (td->o.io_limit)
1170 limit = td->o.io_limit;
1171 else
1172 limit = td->o.size;
1173
1174 if (limit != -1ULL && ddir_rw_sum(td->io_bytes) < limit) {
Jens Axboe5bd5f712013-02-03 14:20:44 +01001175 uint64_t diff;
1176
1177 /*
1178 * If the difference is less than the minimum IO size, we
1179 * are done.
1180 */
Jens Axboe77731b22014-04-28 12:08:47 -06001181 diff = limit - ddir_rw_sum(td->io_bytes);
Jens Axboe74f4b022013-03-22 22:56:55 -06001182 if (diff < td_max_bs(td))
Jens Axboe5bd5f712013-02-03 14:20:44 +01001183 return 0;
1184
Jens Axboe002fe732014-02-11 08:31:13 -07001185 if (fio_files_done(td))
1186 return 0;
1187
Jens Axboe2e1df072012-02-09 11:15:02 +01001188 return 1;
Jens Axboe5bd5f712013-02-03 14:20:44 +01001189 }
Jens Axboe2e1df072012-02-09 11:15:02 +01001190
1191 return 0;
1192}
1193
Erwan Veluce486492013-07-17 23:04:46 +02001194static int exec_string(struct thread_options *o, const char *string, const char *mode)
Jens Axboe2e1df072012-02-09 11:15:02 +01001195{
Erwan Veluce486492013-07-17 23:04:46 +02001196 int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
Jens Axboe2e1df072012-02-09 11:15:02 +01001197 char *str;
1198
1199 str = malloc(newlen);
Erwan Veluce486492013-07-17 23:04:46 +02001200 sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
Jens Axboe2e1df072012-02-09 11:15:02 +01001201
Erwan Veluce486492013-07-17 23:04:46 +02001202 log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
Jens Axboe2e1df072012-02-09 11:15:02 +01001203 ret = system(str);
1204 if (ret == -1)
1205 log_err("fio: exec of cmd <%s> failed\n", str);
1206
1207 free(str);
1208 return ret;
1209}
1210
1211/*
Juan Casse62167762013-09-17 14:06:13 -07001212 * Dry run to compute correct state of numberio for verification.
1213 */
1214static uint64_t do_dry_run(struct thread_data *td)
1215{
1216 uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
1217
1218 td_set_runstate(td, TD_RUNNING);
1219
1220 while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
1221 (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td)) {
1222 struct io_u *io_u;
1223 int ret;
1224
1225 if (td->terminate || td->done)
1226 break;
1227
1228 io_u = get_io_u(td);
1229 if (!io_u)
1230 break;
1231
1232 io_u->flags |= IO_U_F_FLIGHT;
1233 io_u->error = 0;
1234 io_u->resid = 0;
1235 if (ddir_rw(acct_ddir(io_u)))
1236 td->io_issues[acct_ddir(io_u)]++;
1237 if (ddir_rw(io_u->ddir)) {
1238 io_u_mark_depth(td, 1);
1239 td->ts.total_io_u[io_u->ddir]++;
1240 }
1241
Puthikorn Voravootivat2e63e962014-03-03 15:33:32 -08001242 if (td_write(td) && io_u->ddir == DDIR_WRITE &&
1243 td->o.do_verify &&
1244 td->o.verify != VERIFY_NONE &&
1245 !td->o.experimental_verify)
1246 log_io_piece(td, io_u);
1247
Juan Casse62167762013-09-17 14:06:13 -07001248 ret = io_u_sync_complete(td, io_u, bytes_done);
1249 (void) ret;
1250 }
1251
1252 return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
1253}
1254
1255/*
Jens Axboe2e1df072012-02-09 11:15:02 +01001256 * Entry point for the thread based jobs. The process based jobs end up
1257 * here as well, after a little setup.
1258 */
1259static void *thread_main(void *data)
1260{
1261 unsigned long long elapsed;
1262 struct thread_data *td = data;
Jens Axboe48964732013-04-11 12:01:48 +02001263 struct thread_options *o = &td->o;
Jens Axboe2e1df072012-02-09 11:15:02 +01001264 pthread_condattr_t attr;
1265 int clear_state;
Jens Axboe28727df2012-03-29 08:33:15 +02001266 int ret;
Jens Axboe2e1df072012-02-09 11:15:02 +01001267
Jens Axboe48964732013-04-11 12:01:48 +02001268 if (!o->use_thread) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001269 setsid();
1270 td->pid = getpid();
1271 } else
1272 td->pid = gettid();
1273
Jens Axboe48964732013-04-11 12:01:48 +02001274 fio_local_clock_init(o->use_thread);
Jens Axboe5d879392012-12-18 09:12:27 +01001275
Jens Axboe2e1df072012-02-09 11:15:02 +01001276 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
1277
Jens Axboe122c7722012-03-19 09:13:15 +01001278 if (is_backend)
1279 fio_server_send_start(td);
1280
Jens Axboe2e1df072012-02-09 11:15:02 +01001281 INIT_FLIST_HEAD(&td->io_log_list);
1282 INIT_FLIST_HEAD(&td->io_hist_list);
1283 INIT_FLIST_HEAD(&td->verify_list);
1284 INIT_FLIST_HEAD(&td->trim_list);
Jens Axboe1ae83d42013-01-12 01:44:15 -07001285 INIT_FLIST_HEAD(&td->next_rand_list);
Jens Axboe2e1df072012-02-09 11:15:02 +01001286 pthread_mutex_init(&td->io_u_lock, NULL);
1287 td->io_hist_tree = RB_ROOT;
1288
1289 pthread_condattr_init(&attr);
1290 pthread_cond_init(&td->verify_cond, &attr);
1291 pthread_cond_init(&td->free_cond, &attr);
1292
1293 td_set_runstate(td, TD_INITIALIZED);
1294 dprint(FD_MUTEX, "up startup_mutex\n");
1295 fio_mutex_up(startup_mutex);
1296 dprint(FD_MUTEX, "wait on td->mutex\n");
1297 fio_mutex_down(td->mutex);
1298 dprint(FD_MUTEX, "done waiting on td->mutex\n");
1299
1300 /*
Jens Axboe2e1df072012-02-09 11:15:02 +01001301 * A new gid requires privilege, so we need to do this before setting
1302 * the uid.
1303 */
Jens Axboe48964732013-04-11 12:01:48 +02001304 if (o->gid != -1U && setgid(o->gid)) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001305 td_verror(td, errno, "setgid");
1306 goto err;
1307 }
Jens Axboe48964732013-04-11 12:01:48 +02001308 if (o->uid != -1U && setuid(o->uid)) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001309 td_verror(td, errno, "setuid");
1310 goto err;
1311 }
1312
1313 /*
1314 * If we have a gettimeofday() thread, make sure we exclude that
1315 * thread from this job
1316 */
Jens Axboe48964732013-04-11 12:01:48 +02001317 if (o->gtod_cpu)
1318 fio_cpu_clear(&o->cpumask, o->gtod_cpu);
Jens Axboe2e1df072012-02-09 11:15:02 +01001319
1320 /*
1321 * Set affinity first, in case it has an impact on the memory
1322 * allocations.
1323 */
Jens Axboe48964732013-04-11 12:01:48 +02001324 if (o->cpumask_set) {
Jens Axboec2acfba2014-02-27 15:52:02 -08001325 if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
Jens Axboe30cb4c62014-02-28 08:35:40 -08001326 ret = fio_cpus_split(&o->cpumask, td->thread_number - 1);
Jens Axboec2acfba2014-02-27 15:52:02 -08001327 if (!ret) {
1328 log_err("fio: no CPUs set\n");
1329 log_err("fio: Try increasing number of available CPUs\n");
1330 td_verror(td, EINVAL, "cpus_split");
1331 goto err;
1332 }
1333 }
Jens Axboe28727df2012-03-29 08:33:15 +02001334 ret = fio_setaffinity(td->pid, o->cpumask);
1335 if (ret == -1) {
Jens Axboe48964732013-04-11 12:01:48 +02001336 td_verror(td, errno, "cpu_set_affinity");
1337 goto err;
1338 }
Jens Axboe2e1df072012-02-09 11:15:02 +01001339 }
1340
Jens Axboe67bf9822013-01-10 11:23:19 +01001341#ifdef CONFIG_LIBNUMA
Yufei Rend0b937e2012-10-19 23:11:52 -04001342 /* numa node setup */
Jens Axboe48964732013-04-11 12:01:48 +02001343 if (o->numa_cpumask_set || o->numa_memmask_set) {
Daniel Gollub43522842014-05-01 17:07:49 +02001344 struct bitmask *mask;
Yufei Rend0b937e2012-10-19 23:11:52 -04001345 int ret;
1346
1347 if (numa_available() < 0) {
1348 td_verror(td, errno, "Does not support NUMA API\n");
1349 goto err;
1350 }
1351
Jens Axboe48964732013-04-11 12:01:48 +02001352 if (o->numa_cpumask_set) {
Daniel Gollub43522842014-05-01 17:07:49 +02001353 mask = numa_parse_nodestring(o->numa_cpunodes);
1354 ret = numa_run_on_node_mask(mask);
1355 numa_free_nodemask(mask);
Yufei Rend0b937e2012-10-19 23:11:52 -04001356 if (ret == -1) {
1357 td_verror(td, errno, \
1358 "numa_run_on_node_mask failed\n");
1359 goto err;
1360 }
1361 }
1362
Jens Axboe48964732013-04-11 12:01:48 +02001363 if (o->numa_memmask_set) {
Yufei Rend0b937e2012-10-19 23:11:52 -04001364
Daniel Gollub43522842014-05-01 17:07:49 +02001365 mask = NULL;
1366 if (o->numa_memnodes)
1367 mask = numa_parse_nodestring(o->numa_memnodes);
1368
Jens Axboe48964732013-04-11 12:01:48 +02001369 switch (o->numa_mem_mode) {
Yufei Rend0b937e2012-10-19 23:11:52 -04001370 case MPOL_INTERLEAVE:
Daniel Gollub43522842014-05-01 17:07:49 +02001371 numa_set_interleave_mask(mask);
Yufei Rend0b937e2012-10-19 23:11:52 -04001372 break;
1373 case MPOL_BIND:
Daniel Gollub43522842014-05-01 17:07:49 +02001374 numa_set_membind(mask);
Yufei Rend0b937e2012-10-19 23:11:52 -04001375 break;
1376 case MPOL_LOCAL:
1377 numa_set_localalloc();
1378 break;
1379 case MPOL_PREFERRED:
Jens Axboe48964732013-04-11 12:01:48 +02001380 numa_set_preferred(o->numa_mem_prefer_node);
Yufei Rend0b937e2012-10-19 23:11:52 -04001381 break;
1382 case MPOL_DEFAULT:
1383 default:
1384 break;
1385 }
1386
Daniel Gollub43522842014-05-01 17:07:49 +02001387 if (mask)
1388 numa_free_nodemask(mask);
1389
Yufei Rend0b937e2012-10-19 23:11:52 -04001390 }
1391 }
1392#endif
1393
Jens Axboe9a3f1102012-03-28 20:50:15 +02001394 if (fio_pin_memory(td))
1395 goto err;
1396
Jens Axboe2e1df072012-02-09 11:15:02 +01001397 /*
1398 * May alter parameters that init_io_u() will use, so we need to
1399 * do this first.
1400 */
1401 if (init_iolog(td))
1402 goto err;
1403
1404 if (init_io_u(td))
1405 goto err;
1406
Jens Axboe48964732013-04-11 12:01:48 +02001407 if (o->verify_async && verify_async_init(td))
Jens Axboe2e1df072012-02-09 11:15:02 +01001408 goto err;
1409
Jens Axboe28727df2012-03-29 08:33:15 +02001410 if (o->ioprio) {
1411 ret = ioprio_set(IOPRIO_WHO_PROCESS, 0, o->ioprio_class, o->ioprio);
1412 if (ret == -1) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001413 td_verror(td, errno, "ioprio_set");
1414 goto err;
1415 }
1416 }
1417
Jens Axboe48964732013-04-11 12:01:48 +02001418 if (o->cgroup && cgroup_setup(td, cgroup_list, &cgroup_mnt))
Jens Axboe2e1df072012-02-09 11:15:02 +01001419 goto err;
1420
Bruce Cran649c10c2012-02-20 07:59:06 +01001421 errno = 0;
Jens Axboe48964732013-04-11 12:01:48 +02001422 if (nice(o->nice) == -1 && errno != 0) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001423 td_verror(td, errno, "nice");
1424 goto err;
1425 }
1426
Jens Axboe48964732013-04-11 12:01:48 +02001427 if (o->ioscheduler && switch_ioscheduler(td))
Jens Axboe2e1df072012-02-09 11:15:02 +01001428 goto err;
1429
Jens Axboe48964732013-04-11 12:01:48 +02001430 if (!o->create_serialize && setup_files(td))
Jens Axboe2e1df072012-02-09 11:15:02 +01001431 goto err;
1432
1433 if (td_io_init(td))
1434 goto err;
1435
1436 if (init_random_map(td))
1437 goto err;
1438
Erwan Veluce486492013-07-17 23:04:46 +02001439 if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
Jens Axboe48964732013-04-11 12:01:48 +02001440 goto err;
Jens Axboe2e1df072012-02-09 11:15:02 +01001441
Jens Axboe48964732013-04-11 12:01:48 +02001442 if (o->pre_read) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001443 if (pre_read_files(td) < 0)
1444 goto err;
1445 }
1446
Jens Axboe38a812d2014-07-03 09:10:39 -06001447 if (td->flags & TD_F_COMPRESS_LOG)
1448 tp_init(&td->tp_data);
1449
Jens Axboedc5bfbb2013-04-10 22:23:40 +02001450 fio_verify_init(td);
1451
Jens Axboe2e1df072012-02-09 11:15:02 +01001452 fio_gettime(&td->epoch, NULL);
Jens Axboe44404c52013-01-24 14:20:09 -07001453 fio_getrusage(&td->ru_start);
Jens Axboe2e1df072012-02-09 11:15:02 +01001454 clear_state = 0;
1455 while (keep_running(td)) {
Jens Axboe100f49f2013-01-23 10:15:57 -07001456 uint64_t verify_bytes;
1457
Jens Axboe2e1df072012-02-09 11:15:02 +01001458 fio_gettime(&td->start, NULL);
1459 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1460 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
1461 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
1462
Jens Axboe48964732013-04-11 12:01:48 +02001463 if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
1464 o->ratemin[DDIR_TRIM]) {
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001465 memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
Jens Axboe2e1df072012-02-09 11:15:02 +01001466 sizeof(td->bw_sample_time));
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001467 memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
1468 sizeof(td->bw_sample_time));
1469 memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
Jens Axboe2e1df072012-02-09 11:15:02 +01001470 sizeof(td->bw_sample_time));
1471 }
1472
1473 if (clear_state)
1474 clear_io_state(td);
1475
1476 prune_io_piece_log(td);
1477
Juan Casse62167762013-09-17 14:06:13 -07001478 if (td->o.verify_only && (td_write(td) || td_rw(td)))
1479 verify_bytes = do_dry_run(td);
1480 else
1481 verify_bytes = do_io(td);
Jens Axboe2e1df072012-02-09 11:15:02 +01001482
1483 clear_state = 1;
1484
1485 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1486 elapsed = utime_since_now(&td->start);
1487 td->ts.runtime[DDIR_READ] += elapsed;
1488 }
1489 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1490 elapsed = utime_since_now(&td->start);
1491 td->ts.runtime[DDIR_WRITE] += elapsed;
1492 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001493 if (td_trim(td) && td->io_bytes[DDIR_TRIM]) {
1494 elapsed = utime_since_now(&td->start);
1495 td->ts.runtime[DDIR_TRIM] += elapsed;
1496 }
Jens Axboe2e1df072012-02-09 11:15:02 +01001497
1498 if (td->error || td->terminate)
1499 break;
1500
Jens Axboe48964732013-04-11 12:01:48 +02001501 if (!o->do_verify ||
1502 o->verify == VERIFY_NONE ||
Jens Axboe2e1df072012-02-09 11:15:02 +01001503 (td->io_ops->flags & FIO_UNIDIR))
1504 continue;
1505
1506 clear_io_state(td);
1507
1508 fio_gettime(&td->start, NULL);
1509
Jens Axboe100f49f2013-01-23 10:15:57 -07001510 do_verify(td, verify_bytes);
Jens Axboe2e1df072012-02-09 11:15:02 +01001511
1512 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
1513
1514 if (td->error || td->terminate)
1515 break;
1516 }
1517
1518 update_rusage_stat(td);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001519 td->ts.runtime[DDIR_READ] = (td->ts.runtime[DDIR_READ] + 999) / 1000;
1520 td->ts.runtime[DDIR_WRITE] = (td->ts.runtime[DDIR_WRITE] + 999) / 1000;
1521 td->ts.runtime[DDIR_TRIM] = (td->ts.runtime[DDIR_TRIM] + 999) / 1000;
Jens Axboe2e1df072012-02-09 11:15:02 +01001522 td->ts.total_run_time = mtime_since_now(&td->epoch);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001523 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1524 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1525 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
Jens Axboe2e1df072012-02-09 11:15:02 +01001526
Jens Axboe9a3f1102012-03-28 20:50:15 +02001527 fio_unpin_memory(td);
1528
Jens Axboe905e3d42014-04-02 20:01:27 -06001529 fio_writeout_logs(td);
Jens Axboe2e1df072012-02-09 11:15:02 +01001530
Jens Axboe38a812d2014-07-03 09:10:39 -06001531 if (td->flags & TD_F_COMPRESS_LOG)
1532 tp_exit(&td->tp_data);
1533
Jens Axboe48964732013-04-11 12:01:48 +02001534 if (o->exec_postrun)
Erwan Veluce486492013-07-17 23:04:46 +02001535 exec_string(o, o->exec_postrun, (const char *)"postrun");
Jens Axboe2e1df072012-02-09 11:15:02 +01001536
1537 if (exitall_on_terminate)
1538 fio_terminate_threads(td->groupid);
1539
1540err:
1541 if (td->error)
1542 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
1543 td->verror);
1544
Jens Axboe48964732013-04-11 12:01:48 +02001545 if (o->verify_async)
Jens Axboe2e1df072012-02-09 11:15:02 +01001546 verify_async_exit(td);
1547
1548 close_and_free_files(td);
Jens Axboe2e1df072012-02-09 11:15:02 +01001549 cleanup_io_u(td);
Bruce Cran32dbca22012-12-06 20:32:40 +01001550 close_ioengine(td);
Jens Axboe2e1df072012-02-09 11:15:02 +01001551 cgroup_shutdown(td, &cgroup_mnt);
1552
Jens Axboe48964732013-04-11 12:01:48 +02001553 if (o->cpumask_set) {
1554 int ret = fio_cpuset_exit(&o->cpumask);
Jens Axboe2e1df072012-02-09 11:15:02 +01001555
1556 td_verror(td, ret, "fio_cpuset_exit");
1557 }
1558
1559 /*
1560 * do this very late, it will log file closing as well
1561 */
Jens Axboe48964732013-04-11 12:01:48 +02001562 if (o->write_iolog_file)
Jens Axboe2e1df072012-02-09 11:15:02 +01001563 write_iolog_close(td);
1564
Jens Axboec97f1ad2013-03-28 15:08:49 -06001565 fio_mutex_remove(td->rusage_sem);
1566 td->rusage_sem = NULL;
1567
Jens Axboeea66e042014-02-10 13:57:09 -07001568 fio_mutex_remove(td->mutex);
1569 td->mutex = NULL;
1570
Jens Axboe2e1df072012-02-09 11:15:02 +01001571 td_set_runstate(td, TD_EXITED);
Bruce Crane43606c2012-02-20 09:34:24 +01001572 return (void *) (uintptr_t) td->error;
Jens Axboe2e1df072012-02-09 11:15:02 +01001573}
1574
1575
1576/*
1577 * We cannot pass the td data into a forked process, so attach the td and
1578 * pass it to the thread worker.
1579 */
1580static int fork_main(int shmid, int offset)
1581{
1582 struct thread_data *td;
1583 void *data, *ret;
1584
Jens Axboeef7035a2014-06-18 15:30:09 -07001585#if !defined(__hpux) && !defined(CONFIG_NO_SHM)
Jens Axboe2e1df072012-02-09 11:15:02 +01001586 data = shmat(shmid, NULL, 0);
1587 if (data == (void *) -1) {
1588 int __err = errno;
1589
1590 perror("shmat");
1591 return __err;
1592 }
1593#else
1594 /*
1595 * HP-UX inherits shm mappings?
1596 */
1597 data = threads;
1598#endif
1599
1600 td = data + offset * sizeof(struct thread_data);
1601 ret = thread_main(td);
1602 shmdt(data);
Bruce Crane43606c2012-02-20 09:34:24 +01001603 return (int) (uintptr_t) ret;
Jens Axboe2e1df072012-02-09 11:15:02 +01001604}
1605
Jens Axboe958b8912014-07-09 08:46:12 +02001606static void dump_td_info(struct thread_data *td)
1607{
1608 log_err("fio: job '%s' hasn't exited in %lu seconds, it appears to "
1609 "be stuck. Doing forceful exit of this job.\n", td->o.name,
1610 (unsigned long) time_since_now(&td->terminate_time));
1611}
1612
Jens Axboe2e1df072012-02-09 11:15:02 +01001613/*
1614 * Run over the job map and reap the threads that have exited, if any.
1615 */
1616static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1617 unsigned int *m_rate)
1618{
1619 struct thread_data *td;
1620 unsigned int cputhreads, realthreads, pending;
1621 int i, status, ret;
1622
1623 /*
1624 * reap exited threads (TD_EXITED -> TD_REAPED)
1625 */
1626 realthreads = pending = cputhreads = 0;
1627 for_each_td(td, i) {
1628 int flags = 0;
1629
1630 /*
1631 * ->io_ops is NULL for a thread that has closed its
1632 * io engine
1633 */
1634 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
1635 cputhreads++;
1636 else
1637 realthreads++;
1638
1639 if (!td->pid) {
1640 pending++;
1641 continue;
1642 }
1643 if (td->runstate == TD_REAPED)
1644 continue;
1645 if (td->o.use_thread) {
1646 if (td->runstate == TD_EXITED) {
1647 td_set_runstate(td, TD_REAPED);
1648 goto reaped;
1649 }
1650 continue;
1651 }
1652
1653 flags = WNOHANG;
1654 if (td->runstate == TD_EXITED)
1655 flags = 0;
1656
1657 /*
1658 * check if someone quit or got killed in an unusual way
1659 */
1660 ret = waitpid(td->pid, &status, flags);
1661 if (ret < 0) {
1662 if (errno == ECHILD) {
1663 log_err("fio: pid=%d disappeared %d\n",
1664 (int) td->pid, td->runstate);
Jens Axboea5e371a2012-04-02 09:47:09 -07001665 td->sig = ECHILD;
Jens Axboe2e1df072012-02-09 11:15:02 +01001666 td_set_runstate(td, TD_REAPED);
1667 goto reaped;
1668 }
1669 perror("waitpid");
1670 } else if (ret == td->pid) {
1671 if (WIFSIGNALED(status)) {
1672 int sig = WTERMSIG(status);
1673
Jens Axboe36d80bc2012-11-30 21:46:06 +01001674 if (sig != SIGTERM && sig != SIGUSR2)
Jens Axboe2e1df072012-02-09 11:15:02 +01001675 log_err("fio: pid=%d, got signal=%d\n",
1676 (int) td->pid, sig);
Jens Axboea5e371a2012-04-02 09:47:09 -07001677 td->sig = sig;
Jens Axboe2e1df072012-02-09 11:15:02 +01001678 td_set_runstate(td, TD_REAPED);
1679 goto reaped;
1680 }
1681 if (WIFEXITED(status)) {
1682 if (WEXITSTATUS(status) && !td->error)
1683 td->error = WEXITSTATUS(status);
1684
1685 td_set_runstate(td, TD_REAPED);
1686 goto reaped;
1687 }
1688 }
1689
1690 /*
Jens Axboe958b8912014-07-09 08:46:12 +02001691 * If the job is stuck, do a forceful timeout of it and
1692 * move on.
1693 */
1694 if (td->terminate &&
1695 time_since_now(&td->terminate_time) >= FIO_REAP_TIMEOUT) {
1696 dump_td_info(td);
1697 td_set_runstate(td, TD_REAPED);
1698 goto reaped;
1699 }
1700
1701 /*
Jens Axboe2e1df072012-02-09 11:15:02 +01001702 * thread is not dead, continue
1703 */
1704 pending++;
1705 continue;
1706reaped:
1707 (*nr_running)--;
Jens Axboe342f4be2012-09-14 08:59:20 +02001708 (*m_rate) -= ddir_rw_sum(td->o.ratemin);
1709 (*t_rate) -= ddir_rw_sum(td->o.rate);
Jens Axboe2e1df072012-02-09 11:15:02 +01001710 if (!td->pid)
1711 pending--;
1712
1713 if (td->error)
1714 exit_value++;
1715
1716 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboe4a887522013-05-23 12:33:08 +02001717 profile_td_exit(td);
Jens Axboe2e1df072012-02-09 11:15:02 +01001718 }
1719
1720 if (*nr_running == cputhreads && !pending && realthreads)
1721 fio_terminate_threads(TERMINATE_ALL);
1722}
1723
Jens Axboe06464902013-04-24 20:38:54 -06001724static void do_usleep(unsigned int usecs)
1725{
1726 check_for_running_stats();
1727 usleep(usecs);
1728}
1729
Jens Axboe2e1df072012-02-09 11:15:02 +01001730/*
1731 * Main function for kicking off and reaping jobs, as needed.
1732 */
1733static void run_threads(void)
1734{
1735 struct thread_data *td;
Jens Axboe2e1df072012-02-09 11:15:02 +01001736 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboe0de5b262014-02-21 15:26:01 -08001737 uint64_t spent;
Jens Axboe2e1df072012-02-09 11:15:02 +01001738
Jens Axboe2e1df072012-02-09 11:15:02 +01001739 if (fio_gtod_offload && fio_start_gtod_thread())
1740 return;
Bruce Cran334185e2013-11-01 12:32:40 -06001741
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001742 fio_idle_prof_init();
Jens Axboe2e1df072012-02-09 11:15:02 +01001743
1744 set_sig_handlers();
1745
Jens Axboe3a5f6bd2013-04-11 12:04:38 +02001746 nr_thread = nr_process = 0;
1747 for_each_td(td, i) {
1748 if (td->o.use_thread)
1749 nr_thread++;
1750 else
1751 nr_process++;
1752 }
1753
Jens Axboef3afa572012-09-17 13:34:16 +02001754 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001755 log_info("Starting ");
1756 if (nr_thread)
1757 log_info("%d thread%s", nr_thread,
1758 nr_thread > 1 ? "s" : "");
1759 if (nr_process) {
1760 if (nr_thread)
1761 log_info(" and ");
1762 log_info("%d process%s", nr_process,
1763 nr_process > 1 ? "es" : "");
1764 }
1765 log_info("\n");
1766 fflush(stdout);
1767 }
1768
1769 todo = thread_number;
1770 nr_running = 0;
1771 nr_started = 0;
1772 m_rate = t_rate = 0;
1773
1774 for_each_td(td, i) {
1775 print_status_init(td->thread_number - 1);
1776
1777 if (!td->o.create_serialize)
1778 continue;
1779
1780 /*
1781 * do file setup here so it happens sequentially,
1782 * we don't want X number of threads getting their
1783 * client data interspersed on disk
1784 */
1785 if (setup_files(td)) {
1786 exit_value++;
1787 if (td->error)
1788 log_err("fio: pid=%d, err=%d/%s\n",
1789 (int) td->pid, td->error, td->verror);
1790 td_set_runstate(td, TD_REAPED);
1791 todo--;
1792 } else {
1793 struct fio_file *f;
1794 unsigned int j;
1795
1796 /*
1797 * for sharing to work, each job must always open
1798 * its own files. so close them, if we opened them
1799 * for creation
1800 */
1801 for_each_file(td, f, j) {
1802 if (fio_file_open(f))
1803 td_io_close_file(td, f);
1804 }
1805 }
1806 }
1807
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001808 /* start idle threads before io threads start to run */
1809 fio_idle_prof_start();
1810
Jens Axboe2e1df072012-02-09 11:15:02 +01001811 set_genesis_time();
1812
1813 while (todo) {
1814 struct thread_data *map[REAL_MAX_JOBS];
1815 struct timeval this_start;
1816 int this_jobs = 0, left;
1817
1818 /*
1819 * create threads (TD_NOT_CREATED -> TD_CREATED)
1820 */
1821 for_each_td(td, i) {
1822 if (td->runstate != TD_NOT_CREATED)
1823 continue;
1824
1825 /*
1826 * never got a chance to start, killed by other
1827 * thread for some reason
1828 */
1829 if (td->terminate) {
1830 todo--;
1831 continue;
1832 }
1833
1834 if (td->o.start_delay) {
Jens Axboe0de5b262014-02-21 15:26:01 -08001835 spent = utime_since_genesis();
Jens Axboe2e1df072012-02-09 11:15:02 +01001836
Christian Ehrhardt74454ce2014-02-20 14:20:01 +01001837 if (td->o.start_delay > spent)
Jens Axboe2e1df072012-02-09 11:15:02 +01001838 continue;
1839 }
1840
1841 if (td->o.stonewall && (nr_started || nr_running)) {
1842 dprint(FD_PROCESS, "%s: stonewall wait\n",
1843 td->o.name);
1844 break;
1845 }
1846
1847 init_disk_util(td);
1848
Jens Axboec97f1ad2013-03-28 15:08:49 -06001849 td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);
1850 td->update_rusage = 0;
1851
Jens Axboe2e1df072012-02-09 11:15:02 +01001852 /*
1853 * Set state to created. Thread will transition
1854 * to TD_INITIALIZED when it's done setting up.
1855 */
1856 td_set_runstate(td, TD_CREATED);
1857 map[this_jobs++] = td;
1858 nr_started++;
1859
1860 if (td->o.use_thread) {
1861 int ret;
1862
1863 dprint(FD_PROCESS, "will pthread_create\n");
1864 ret = pthread_create(&td->thread, NULL,
1865 thread_main, td);
1866 if (ret) {
1867 log_err("pthread_create: %s\n",
1868 strerror(ret));
1869 nr_started--;
1870 break;
1871 }
1872 ret = pthread_detach(td->thread);
1873 if (ret)
1874 log_err("pthread_detach: %s",
1875 strerror(ret));
1876 } else {
1877 pid_t pid;
1878 dprint(FD_PROCESS, "will fork\n");
1879 pid = fork();
1880 if (!pid) {
1881 int ret = fork_main(shm_id, i);
1882
1883 _exit(ret);
1884 } else if (i == fio_debug_jobno)
1885 *fio_debug_jobp = pid;
1886 }
1887 dprint(FD_MUTEX, "wait on startup_mutex\n");
1888 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1889 log_err("fio: job startup hung? exiting.\n");
1890 fio_terminate_threads(TERMINATE_ALL);
1891 fio_abort = 1;
1892 nr_started--;
1893 break;
1894 }
1895 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
1896 }
1897
1898 /*
1899 * Wait for the started threads to transition to
1900 * TD_INITIALIZED.
1901 */
1902 fio_gettime(&this_start, NULL);
1903 left = this_jobs;
1904 while (left && !fio_abort) {
1905 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1906 break;
1907
Jens Axboe06464902013-04-24 20:38:54 -06001908 do_usleep(100000);
Jens Axboe2e1df072012-02-09 11:15:02 +01001909
1910 for (i = 0; i < this_jobs; i++) {
1911 td = map[i];
1912 if (!td)
1913 continue;
1914 if (td->runstate == TD_INITIALIZED) {
1915 map[i] = NULL;
1916 left--;
1917 } else if (td->runstate >= TD_EXITED) {
1918 map[i] = NULL;
1919 left--;
1920 todo--;
1921 nr_running++; /* work-around... */
1922 }
1923 }
1924 }
1925
1926 if (left) {
Jens Axboe4e87c372012-02-15 14:27:08 +01001927 log_err("fio: %d job%s failed to start\n", left,
1928 left > 1 ? "s" : "");
Jens Axboe2e1df072012-02-09 11:15:02 +01001929 for (i = 0; i < this_jobs; i++) {
1930 td = map[i];
1931 if (!td)
1932 continue;
1933 kill(td->pid, SIGTERM);
1934 }
1935 break;
1936 }
1937
1938 /*
1939 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1940 */
1941 for_each_td(td, i) {
1942 if (td->runstate != TD_INITIALIZED)
1943 continue;
1944
1945 if (in_ramp_time(td))
1946 td_set_runstate(td, TD_RAMP);
1947 else
1948 td_set_runstate(td, TD_RUNNING);
1949 nr_running++;
1950 nr_started--;
Jens Axboe342f4be2012-09-14 08:59:20 +02001951 m_rate += ddir_rw_sum(td->o.ratemin);
1952 t_rate += ddir_rw_sum(td->o.rate);
Jens Axboe2e1df072012-02-09 11:15:02 +01001953 todo--;
1954 fio_mutex_up(td->mutex);
1955 }
1956
1957 reap_threads(&nr_running, &t_rate, &m_rate);
1958
Jens Axboe122c7722012-03-19 09:13:15 +01001959 if (todo)
Jens Axboe06464902013-04-24 20:38:54 -06001960 do_usleep(100000);
Jens Axboe2e1df072012-02-09 11:15:02 +01001961 }
1962
1963 while (nr_running) {
1964 reap_threads(&nr_running, &t_rate, &m_rate);
Jens Axboe06464902013-04-24 20:38:54 -06001965 do_usleep(10000);
Jens Axboe2e1df072012-02-09 11:15:02 +01001966 }
1967
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001968 fio_idle_prof_stop();
1969
Jens Axboe2e1df072012-02-09 11:15:02 +01001970 update_io_ticks();
Jens Axboe2e1df072012-02-09 11:15:02 +01001971}
1972
Jens Axboe9ec77792012-08-02 08:27:41 +02001973void wait_for_disk_thread_exit(void)
1974{
1975 fio_mutex_down(disk_thread_mutex);
1976}
1977
Jens Axboe27357182012-09-27 11:31:50 +02001978static void free_disk_util(void)
1979{
1980 disk_util_start_exit();
1981 wait_for_disk_thread_exit();
1982 disk_util_prune_entries();
1983}
1984
Jens Axboe2e1df072012-02-09 11:15:02 +01001985static void *disk_thread_main(void *data)
1986{
Jens Axboe9ec77792012-08-02 08:27:41 +02001987 int ret = 0;
1988
Jens Axboe2e1df072012-02-09 11:15:02 +01001989 fio_mutex_up(startup_mutex);
1990
Jens Axboe9ec77792012-08-02 08:27:41 +02001991 while (threads && !ret) {
Jens Axboe2e1df072012-02-09 11:15:02 +01001992 usleep(DISK_UTIL_MSEC * 1000);
1993 if (!threads)
1994 break;
Jens Axboe9ec77792012-08-02 08:27:41 +02001995 ret = update_io_ticks();
Jens Axboe2e1df072012-02-09 11:15:02 +01001996
1997 if (!is_backend)
1998 print_thread_status();
1999 }
2000
Jens Axboe9ec77792012-08-02 08:27:41 +02002001 fio_mutex_up(disk_thread_mutex);
Jens Axboe2e1df072012-02-09 11:15:02 +01002002 return NULL;
2003}
2004
2005static int create_disk_util_thread(void)
2006{
2007 int ret;
2008
Jens Axboe9ec77792012-08-02 08:27:41 +02002009 setup_disk_util();
2010
Jens Axboe521da522012-08-02 11:21:36 +02002011 disk_thread_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboe9ec77792012-08-02 08:27:41 +02002012
Jens Axboe2e1df072012-02-09 11:15:02 +01002013 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
2014 if (ret) {
Jens Axboe9ec77792012-08-02 08:27:41 +02002015 fio_mutex_remove(disk_thread_mutex);
Jens Axboe2e1df072012-02-09 11:15:02 +01002016 log_err("Can't create disk util thread: %s\n", strerror(ret));
2017 return 1;
2018 }
2019
2020 ret = pthread_detach(disk_util_thread);
2021 if (ret) {
Jens Axboe9ec77792012-08-02 08:27:41 +02002022 fio_mutex_remove(disk_thread_mutex);
Jens Axboe2e1df072012-02-09 11:15:02 +01002023 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
2024 return 1;
2025 }
2026
2027 dprint(FD_MUTEX, "wait on startup_mutex\n");
2028 fio_mutex_down(startup_mutex);
2029 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
2030 return 0;
2031}
2032
Jens Axboe2e1df072012-02-09 11:15:02 +01002033int fio_backend(void)
2034{
2035 struct thread_data *td;
2036 int i;
2037
2038 if (exec_profile) {
2039 if (load_profile(exec_profile))
2040 return 1;
2041 free(exec_profile);
2042 exec_profile = NULL;
2043 }
2044 if (!thread_number)
2045 return 0;
2046
2047 if (write_bw_log) {
Jens Axboe38a812d2014-07-03 09:10:39 -06002048 struct log_params p = {
2049 .log_type = IO_LOG_TYPE_BW,
2050 };
2051
2052 setup_log(&agg_io_log[DDIR_READ], &p, "agg-read_bw.log");
2053 setup_log(&agg_io_log[DDIR_WRITE], &p, "agg-write_bw.log");
2054 setup_log(&agg_io_log[DDIR_TRIM], &p, "agg-trim_bw.log");
Jens Axboe2e1df072012-02-09 11:15:02 +01002055 }
2056
Jens Axboe521da522012-08-02 11:21:36 +02002057 startup_mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboe2e1df072012-02-09 11:15:02 +01002058 if (startup_mutex == NULL)
2059 return 1;
Jens Axboe2e1df072012-02-09 11:15:02 +01002060
2061 set_genesis_time();
Jens Axboecef91752013-04-26 17:05:57 -06002062 stat_init();
Jens Axboe2e1df072012-02-09 11:15:02 +01002063 create_disk_util_thread();
2064
2065 cgroup_list = smalloc(sizeof(*cgroup_list));
2066 INIT_FLIST_HEAD(cgroup_list);
2067
2068 run_threads();
2069
2070 if (!fio_abort) {
Jens Axboe2e627242014-07-25 09:51:56 +02002071 __show_run_stats();
Jens Axboe2e1df072012-02-09 11:15:02 +01002072 if (write_bw_log) {
Jens Axboe987c4f42014-07-01 16:29:12 -06002073 int i;
2074
2075 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2076 struct io_log *log = agg_io_log[i];
2077
Jens Axboe38a812d2014-07-03 09:10:39 -06002078 flush_log(log);
Jens Axboe633d8252014-07-02 13:36:34 -06002079 free_log(log);
Jens Axboe987c4f42014-07-01 16:29:12 -06002080 }
Jens Axboe2e1df072012-02-09 11:15:02 +01002081 }
2082 }
2083
2084 for_each_td(td, i)
2085 fio_options_free(td);
2086
Jens Axboea462bae2012-03-30 08:33:27 +02002087 free_disk_util();
Jens Axboe2e1df072012-02-09 11:15:02 +01002088 cgroup_kill(cgroup_list);
2089 sfree(cgroup_list);
2090 sfree(cgroup_mnt);
2091
2092 fio_mutex_remove(startup_mutex);
Jens Axboe9ec77792012-08-02 08:27:41 +02002093 fio_mutex_remove(disk_thread_mutex);
Jens Axboecef91752013-04-26 17:05:57 -06002094 stat_exit();
Jens Axboe2e1df072012-02-09 11:15:02 +01002095 return exit_value;
2096}