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