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