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