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