Fio 1.27.3
[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
906c8d75 375/*
34403fb1 376 * The main verify engine. Runs over the writes we previously submitted,
906c8d75
JA
377 * reads the blocks back in, and checks the crc/md5 of the data.
378 */
1e97cce9 379static void do_verify(struct thread_data *td)
ebac4655 380{
53cdc686 381 struct fio_file *f;
36167d82 382 struct io_u *io_u;
af52b345
JA
383 int ret, min_events;
384 unsigned int i;
e5b401d4
JA
385
386 /*
387 * sync io first and invalidate cache, to make sure we really
388 * read from disk.
389 */
390 for_each_file(td, f, i) {
d6aed795 391 if (!fio_file_open(f))
3d7b485f 392 continue;
b2fdda43
JA
393 if (fio_io_sync(td, f))
394 break;
395 if (file_invalidate_cache(td, f))
396 break;
e5b401d4 397 }
ebac4655 398
b2fdda43
JA
399 if (td->error)
400 return;
401
ebac4655
JA
402 td_set_runstate(td, TD_VERIFYING);
403
36167d82
JA
404 io_u = NULL;
405 while (!td->terminate) {
4950421a 406 int ret2, full;
5451792e 407
ebac4655
JA
408 io_u = __get_io_u(td);
409 if (!io_u)
410 break;
411
993bf48b
JA
412 update_tv_cache(td);
413
414 if (runtime_exceeded(td, &td->tv_cache)) {
069c2918 415 put_io_u(td, io_u);
90a87d4b 416 td->terminate = 1;
02bcaa8c 417 break;
069c2918 418 }
02bcaa8c 419
069c2918
JA
420 if (get_next_verify(td, io_u)) {
421 put_io_u(td, io_u);
ebac4655 422 break;
069c2918 423 }
ebac4655 424
069c2918
JA
425 if (td_io_prep(td, io_u)) {
426 put_io_u(td, io_u);
53cdc686 427 break;
069c2918 428 }
d7762cf8
JA
429
430 io_u->end_io = verify_io_u;
53cdc686 431
11786802 432 ret = td_io_queue(td, io_u);
36167d82
JA
433 switch (ret) {
434 case FIO_Q_COMPLETED:
435 if (io_u->error)
22819ec2 436 ret = -io_u->error;
9e9d164e 437 else if (io_u->resid) {
36167d82 438 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 439 struct fio_file *f = io_u->file;
ebac4655 440
9e9d164e
JA
441 /*
442 * zero read, fail
443 */
444 if (!bytes) {
41d9f0fd 445 td_verror(td, EIO, "full resid");
9e9d164e
JA
446 put_io_u(td, io_u);
447 break;
448 }
8400d9b2 449
36167d82
JA
450 io_u->xfer_buflen = io_u->resid;
451 io_u->xfer_buf += bytes;
8400d9b2
JA
452 io_u->offset += bytes;
453
30061b97
JA
454 td->ts.short_io_u[io_u->ddir]++;
455
d460eb31 456 if (io_u->offset == f->real_file_size)
8400d9b2
JA
457 goto sync_done;
458
11786802
JA
459 requeue_io_u(td, &io_u);
460 } else {
8400d9b2 461sync_done:
581e7141 462 ret = io_u_sync_complete(td, io_u, NULL);
11786802
JA
463 if (ret < 0)
464 break;
36167d82 465 }
36167d82
JA
466 continue;
467 case FIO_Q_QUEUED:
468 break;
755200a3
JA
469 case FIO_Q_BUSY:
470 requeue_io_u(td, &io_u);
5451792e
JA
471 ret2 = td_io_commit(td);
472 if (ret2 < 0)
473 ret = ret2;
755200a3 474 break;
36167d82
JA
475 default:
476 assert(ret < 0);
e1161c32 477 td_verror(td, -ret, "td_io_queue");
ebac4655
JA
478 break;
479 }
480
99784632 481 if (ret < 0 || td->error)
3af6ef39
JA
482 break;
483
ebac4655 484 /*
3af6ef39
JA
485 * if we can queue more, do so. but check if there are
486 * completed io_u's first.
ebac4655 487 */
4950421a
JA
488 full = queue_full(td) || ret == FIO_Q_BUSY;
489 if (full || !td->o.iodepth_batch_complete) {
490 min_events = td->o.iodepth_batch_complete;
491 if (full && !min_events)
838bc709 492 min_events = 1;
e916b390 493
4950421a
JA
494 do {
495 /*
496 * Reap required number of io units, if any,
497 * and do the verification on them through
498 * the callback handler
499 */
581e7141 500 if (io_u_queued_complete(td, min_events, NULL) < 0) {
4950421a
JA
501 ret = -1;
502 break;
503 }
504 } while (full && (td->cur_depth > td->o.iodepth_low));
505 }
506 if (ret < 0)
ebac4655 507 break;
36167d82 508 }
ebac4655 509
c01c0395
JA
510 if (!td->error) {
511 min_events = td->cur_depth;
512
513 if (min_events)
581e7141 514 ret = io_u_queued_complete(td, min_events, NULL);
c01c0395 515 } else
ebac4655
JA
516 cleanup_pending_aio(td);
517
518 td_set_runstate(td, TD_RUNNING);
519}
520
32cd46a0 521/*
906c8d75 522 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
523 * and reaps them, checking for rate and errors along the way.
524 */
ebac4655
JA
525static void do_io(struct thread_data *td)
526{
af52b345
JA
527 unsigned int i;
528 int ret = 0;
ebac4655 529
b29ee5b3
JA
530 if (in_ramp_time(td))
531 td_set_runstate(td, TD_RAMP);
532 else
533 td_set_runstate(td, TD_RUNNING);
5853e5a8 534
7bb48f84 535 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
97601024 536 struct timeval comp_time;
581e7141 537 unsigned long bytes_done[2] = { 0, 0 };
84585003 538 int min_evts = 0;
ebac4655 539 struct io_u *io_u;
4950421a 540 int ret2, full;
ebac4655
JA
541
542 if (td->terminate)
543 break;
544
3d7c391d 545 io_u = get_io_u(td);
ebac4655
JA
546 if (!io_u)
547 break;
548
993bf48b 549 update_tv_cache(td);
97601024 550
993bf48b 551 if (runtime_exceeded(td, &td->tv_cache)) {
97601024 552 put_io_u(td, io_u);
90a87d4b 553 td->terminate = 1;
97601024
JA
554 break;
555 }
36167d82 556
b67740d3
JA
557 /*
558 * Add verification end_io handler, if asked to verify
559 * a previously written file.
560 */
cc3fc855 561 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
b67740d3 562 io_u->end_io = verify_io_u;
d8fab1b6 563 td_set_runstate(td, TD_VERIFYING);
b29ee5b3
JA
564 } else if (in_ramp_time(td))
565 td_set_runstate(td, TD_RAMP);
566 else
d8fab1b6 567 td_set_runstate(td, TD_RUNNING);
b67740d3 568
11786802 569 ret = td_io_queue(td, io_u);
36167d82
JA
570 switch (ret) {
571 case FIO_Q_COMPLETED:
5451792e
JA
572 if (io_u->error)
573 ret = -io_u->error;
9e9d164e 574 else if (io_u->resid) {
36167d82 575 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 576 struct fio_file *f = io_u->file;
36167d82 577
9e9d164e
JA
578 /*
579 * zero read, fail
580 */
581 if (!bytes) {
41d9f0fd 582 td_verror(td, EIO, "full resid");
9e9d164e
JA
583 put_io_u(td, io_u);
584 break;
585 }
586
cec6b55d 587 io_u->xfer_buflen = io_u->resid;
36167d82 588 io_u->xfer_buf += bytes;
5a7c5680
JA
589 io_u->offset += bytes;
590
30061b97
JA
591 td->ts.short_io_u[io_u->ddir]++;
592
d460eb31 593 if (io_u->offset == f->real_file_size)
5a7c5680
JA
594 goto sync_done;
595
11786802
JA
596 requeue_io_u(td, &io_u);
597 } else {
5a7c5680 598sync_done:
581e7141
JA
599 if (__should_check_rate(td, 0) ||
600 __should_check_rate(td, 1))
993bf48b
JA
601 fio_gettime(&comp_time, NULL);
602
581e7141
JA
603 ret = io_u_sync_complete(td, io_u, bytes_done);
604 if (ret < 0)
605 break;
cec6b55d 606 }
36167d82
JA
607 break;
608 case FIO_Q_QUEUED:
7e77dd02
JA
609 /*
610 * if the engine doesn't have a commit hook,
611 * the io_u is really queued. if it does have such
612 * a hook, it has to call io_u_queued() itself.
613 */
614 if (td->io_ops->commit == NULL)
615 io_u_queued(td, io_u);
36167d82 616 break;
755200a3
JA
617 case FIO_Q_BUSY:
618 requeue_io_u(td, &io_u);
5451792e
JA
619 ret2 = td_io_commit(td);
620 if (ret2 < 0)
621 ret = ret2;
755200a3 622 break;
36167d82
JA
623 default:
624 assert(ret < 0);
625 put_io_u(td, io_u);
626 break;
ebac4655
JA
627 }
628
99784632 629 if (ret < 0 || td->error)
36167d82
JA
630 break;
631
97601024
JA
632 /*
633 * See if we need to complete some commands
634 */
4950421a
JA
635 full = queue_full(td) || ret == FIO_Q_BUSY;
636 if (full || !td->o.iodepth_batch_complete) {
637 min_evts = td->o.iodepth_batch_complete;
638 if (full && !min_evts)
36167d82 639 min_evts = 1;
4950421a 640
581e7141
JA
641 if (__should_check_rate(td, 0) ||
642 __should_check_rate(td, 1))
993bf48b 643 fio_gettime(&comp_time, NULL);
4950421a
JA
644
645 do {
581e7141
JA
646 ret = io_u_queued_complete(td, min_evts, bytes_done);
647 if (ret < 0)
4950421a
JA
648 break;
649
4950421a 650 } while (full && (td->cur_depth > td->o.iodepth_low));
ebac4655
JA
651 }
652
4950421a
JA
653 if (ret < 0)
654 break;
581e7141 655 if (!(bytes_done[0] + bytes_done[1]))
97601024
JA
656 continue;
657
581e7141
JA
658 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
659 if (check_min_rate(td, &comp_time, bytes_done)) {
721938ae
JA
660 if (exitall_on_terminate)
661 terminate_threads(td->groupid);
662 td_verror(td, EIO, "check_min_rate");
663 break;
664 }
ebac4655
JA
665 }
666
2dc1bbeb 667 if (td->o.thinktime) {
9c1f7434
JA
668 unsigned long long b;
669
670 b = td->io_blocks[0] + td->io_blocks[1];
2dc1bbeb 671 if (!(b % td->o.thinktime_blocks)) {
48097d5c
JA
672 int left;
673
2dc1bbeb 674 if (td->o.thinktime_spin)
6dd6f2cd 675 usec_spin(td->o.thinktime_spin);
48097d5c 676
2dc1bbeb 677 left = td->o.thinktime - td->o.thinktime_spin;
48097d5c
JA
678 if (left)
679 usec_sleep(td, left);
680 }
9c1f7434 681 }
ebac4655
JA
682 }
683
aa31f1f1
SL
684 if (td->o.fill_device && td->error == ENOSPC) {
685 td->error = 0;
686 td->terminate = 1;
687 }
4d2413c6 688 if (!td->error) {
3d7c391d
JA
689 struct fio_file *f;
690
c01c0395
JA
691 i = td->cur_depth;
692 if (i)
581e7141 693 ret = io_u_queued_complete(td, i, NULL);
ebac4655 694
2dc1bbeb 695 if (should_fsync(td) && td->o.end_fsync) {
84585003 696 td_set_runstate(td, TD_FSYNCING);
3d7b485f
JA
697
698 for_each_file(td, f, i) {
d6aed795 699 if (!fio_file_open(f))
3d7b485f 700 continue;
858a3d47 701 fio_io_sync(td, f);
3d7b485f 702 }
84585003 703 }
c01c0395
JA
704 } else
705 cleanup_pending_aio(td);
2ba1c290
JA
706
707 /*
708 * stop job if we failed doing any IO
709 */
710 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
711 td->done = 1;
ebac4655
JA
712}
713
ebac4655
JA
714static void cleanup_io_u(struct thread_data *td)
715{
01743ee1 716 struct flist_head *entry, *n;
ebac4655
JA
717 struct io_u *io_u;
718
01743ee1
JA
719 flist_for_each_safe(entry, n, &td->io_u_freelist) {
720 io_u = flist_entry(entry, struct io_u, list);
ebac4655 721
01743ee1 722 flist_del(&io_u->list);
ebac4655
JA
723 free(io_u);
724 }
725
2f9ade3c 726 free_io_mem(td);
ebac4655
JA
727}
728
729static int init_io_u(struct thread_data *td)
730{
731 struct io_u *io_u;
a00735e6 732 unsigned int max_bs;
eb7ccf38 733 int cl_align, i, max_units;
ebac4655
JA
734 char *p;
735
1e3c09af 736 max_units = td->o.iodepth;
2dc1bbeb 737 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
5ec10eaa
JA
738 td->orig_buffer_size = (unsigned long long) max_bs
739 * (unsigned long long) max_units;
740
741 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
742 unsigned long bs;
74b025b0 743
5ec10eaa
JA
744 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
745 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
746 }
ebac4655 747
c3990645
JA
748 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
749 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
750 return 1;
751 }
752
2f9ade3c
JA
753 if (allocate_io_mem(td))
754 return 1;
ebac4655 755
b3f378e9
JA
756 if (td->o.odirect)
757 p = ALIGN(td->orig_buffer);
758 else
759 p = td->orig_buffer;
760
eb7ccf38
JA
761 cl_align = os_cache_line_size();
762
ebac4655 763 for (i = 0; i < max_units; i++) {
eb7ccf38
JA
764 void *ptr;
765
db1cd90b
JA
766 if (td->terminate)
767 return 1;
eb7ccf38
JA
768
769 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
770 log_err("fio: posix_memalign=%s\n", strerror(errno));
771 break;
772 }
773
774 io_u = ptr;
ebac4655 775 memset(io_u, 0, sizeof(*io_u));
01743ee1 776 INIT_FLIST_HEAD(&io_u->list);
ebac4655 777
b4c5e1ac
JA
778 if (!(td->io_ops->flags & FIO_NOIO)) {
779 io_u->buf = p + max_bs * i;
e9459e5a 780
5973cafb
JA
781 if (td_write(td) && !td->o.refill_buffers)
782 io_u_fill_buffer(td, io_u, max_bs);
b4c5e1ac 783 }
6b9cea23 784
b1ff3403 785 io_u->index = i;
0c6e7517 786 io_u->flags = IO_U_F_FREE;
01743ee1 787 flist_add(&io_u->list, &td->io_u_freelist);
ebac4655
JA
788 }
789
790 return 0;
791}
792
da86774e
JA
793static int switch_ioscheduler(struct thread_data *td)
794{
795 char tmp[256], tmp2[128];
796 FILE *f;
797 int ret;
798
ba0fbe10 799 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
800 return 0;
801
da86774e
JA
802 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
803
804 f = fopen(tmp, "r+");
805 if (!f) {
cbf5c1d7 806 if (errno == ENOENT) {
5ec10eaa
JA
807 log_err("fio: os or kernel doesn't support IO scheduler"
808 " switching\n");
cbf5c1d7
JA
809 return 0;
810 }
811 td_verror(td, errno, "fopen iosched");
da86774e
JA
812 return 1;
813 }
814
815 /*
816 * Set io scheduler.
817 */
2dc1bbeb 818 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
da86774e 819 if (ferror(f) || ret != 1) {
e1161c32 820 td_verror(td, errno, "fwrite");
da86774e
JA
821 fclose(f);
822 return 1;
823 }
824
825 rewind(f);
826
827 /*
828 * Read back and check that the selected scheduler is now the default.
829 */
830 ret = fread(tmp, 1, sizeof(tmp), f);
831 if (ferror(f) || ret < 0) {
e1161c32 832 td_verror(td, errno, "fread");
da86774e
JA
833 fclose(f);
834 return 1;
835 }
836
2dc1bbeb 837 sprintf(tmp2, "[%s]", td->o.ioscheduler);
da86774e 838 if (!strstr(tmp, tmp2)) {
2dc1bbeb 839 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
e1161c32 840 td_verror(td, EINVAL, "iosched_switch");
da86774e
JA
841 fclose(f);
842 return 1;
843 }
844
845 fclose(f);
846 return 0;
847}
848
492158cf
JA
849static int keep_running(struct thread_data *td)
850{
851 unsigned long long io_done;
852
20e354ef
JA
853 if (td->done)
854 return 0;
492158cf
JA
855 if (td->o.time_based)
856 return 1;
857 if (td->o.loops) {
858 td->o.loops--;
859 return 1;
860 }
861
5ec10eaa
JA
862 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
863 + td->io_skip_bytes;
492158cf
JA
864 if (io_done < td->o.size)
865 return 1;
866
867 return 0;
868}
869
b29ee5b3 870static void reset_io_counters(struct thread_data *td)
ebac4655 871{
756867bd 872 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
ebac4655 873 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 874 td->zone_bytes = 0;
581e7141
JA
875 td->rate_bytes[0] = td->rate_bytes[1] = 0;
876 td->rate_blocks[0] = td->rate_blocks[1] = 0;
ebac4655 877
c1324df1
JA
878 td->last_was_sync = 0;
879
2ba1c290
JA
880 /*
881 * reset file done count if we are to start over
882 */
883 if (td->o.time_based || td->o.loops)
48f5abd3 884 td->nr_done_files = 0;
5bfc35d7
JA
885
886 /*
887 * Set the same seed to get repeatable runs
888 */
889 td_fill_rand_seeds(td);
b29ee5b3
JA
890}
891
892void reset_all_stats(struct thread_data *td)
893{
894 struct timeval tv;
895 int i;
896
897 reset_io_counters(td);
898
899 for (i = 0; i < 2; i++) {
900 td->io_bytes[i] = 0;
901 td->io_blocks[i] = 0;
902 td->io_issues[i] = 0;
903 td->ts.total_io_u[i] = 0;
904 }
905
906 fio_gettime(&tv, NULL);
907 memcpy(&td->epoch, &tv, sizeof(tv));
908 memcpy(&td->start, &tv, sizeof(tv));
909}
910
df9c26b1 911static void clear_io_state(struct thread_data *td)
b29ee5b3
JA
912{
913 struct fio_file *f;
914 unsigned int i;
b29ee5b3
JA
915
916 reset_io_counters(td);
281f9b63 917
24ffd2c2 918 close_files(td);
df9c26b1 919 for_each_file(td, f, i)
d6aed795 920 fio_file_clear_done(f);
ebac4655
JA
921}
922
398e8c4d
JA
923static int exec_string(const char *string)
924{
925 int ret, newlen = strlen(string) + 1 + 8;
926 char *str;
927
928 str = malloc(newlen);
929 sprintf(str, "sh -c %s", string);
930
931 ret = system(str);
932 if (ret == -1)
933 log_err("fio: exec of cmd <%s> failed\n", str);
934
935 free(str);
936 return ret;
937}
938
906c8d75
JA
939/*
940 * Entry point for the thread based jobs. The process based jobs end up
941 * here as well, after a little setup.
942 */
ebac4655
JA
943static void *thread_main(void *data)
944{
10927316 945 unsigned long long runtime[2], elapsed;
ebac4655 946 struct thread_data *td = data;
a978ba68 947 int clear_state;
ebac4655 948
2dc1bbeb 949 if (!td->o.use_thread)
ebac4655
JA
950 setsid();
951
952 td->pid = getpid();
953
5921e80c 954 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
ee56ad50 955
01743ee1
JA
956 INIT_FLIST_HEAD(&td->io_u_freelist);
957 INIT_FLIST_HEAD(&td->io_u_busylist);
958 INIT_FLIST_HEAD(&td->io_u_requeues);
959 INIT_FLIST_HEAD(&td->io_log_list);
960 INIT_FLIST_HEAD(&td->io_hist_list);
bb5d7d0b 961 td->io_hist_tree = RB_ROOT;
aea47d44 962
db1cd90b 963 td_set_runstate(td, TD_INITIALIZED);
29adda3c 964 dprint(FD_MUTEX, "up startup_mutex\n");
cdd18ad8 965 fio_mutex_up(startup_mutex);
29adda3c 966 dprint(FD_MUTEX, "wait on td->mutex\n");
cdd18ad8 967 fio_mutex_down(td->mutex);
29adda3c 968 dprint(FD_MUTEX, "done waiting on td->mutex\n");
db1cd90b
JA
969
970 /*
cdd18ad8 971 * the ->mutex mutex is now no longer used, close it to avoid
db1cd90b
JA
972 * eating a file descriptor
973 */
cdd18ad8 974 fio_mutex_remove(td->mutex);
db1cd90b 975
d84f8d49
JA
976 /*
977 * May alter parameters that init_io_u() will use, so we need to
978 * do this first.
979 */
980 if (init_iolog(td))
981 goto err;
982
ebac4655 983 if (init_io_u(td))
db1cd90b 984 goto err;
ebac4655 985
375b2695 986 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
e1161c32 987 td_verror(td, errno, "cpu_set_affinity");
db1cd90b 988 goto err;
ebac4655
JA
989 }
990
f484470b
JA
991 /*
992 * If we have a gettimeofday() thread, make sure we exclude that
993 * thread from this job
994 */
be4ecfdf 995 if (td->o.gtod_cpu) {
be4ecfdf
JA
996 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
997 if (fio_setaffinity(td) == -1) {
998 td_verror(td, errno, "cpu_set_affinity");
999 goto err;
1000 }
1001 }
1002
ac684785 1003 if (td->ioprio_set) {
ebac4655 1004 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
e1161c32 1005 td_verror(td, errno, "ioprio_set");
db1cd90b 1006 goto err;
ebac4655
JA
1007 }
1008 }
1009
2dc1bbeb 1010 if (nice(td->o.nice) == -1) {
e1161c32 1011 td_verror(td, errno, "nice");
db1cd90b 1012 goto err;
b6f4d880
JA
1013 }
1014
2dc1bbeb 1015 if (td->o.ioscheduler && switch_ioscheduler(td))
db1cd90b 1016 goto err;
37f56873 1017
2dc1bbeb 1018 if (!td->o.create_serialize && setup_files(td))
ebac4655
JA
1019 goto err;
1020
7d6c5283
JA
1021 if (td_io_init(td))
1022 goto err;
1023
68727076
JA
1024 if (init_random_map(td))
1025 goto err;
1026
2dc1bbeb 1027 if (td->o.exec_prerun) {
398e8c4d 1028 if (exec_string(td->o.exec_prerun))
69cfd7e0
JA
1029 goto err;
1030 }
4e0ba8af 1031
afad68f7
ZY
1032 if (td->o.pre_read) {
1033 if (pre_read_files(td) < 0)
1034 goto err;
1035 }
1036
69008999 1037 fio_gettime(&td->epoch, NULL);
756867bd 1038 getrusage(RUSAGE_SELF, &td->ts.ru_start);
69008999
JA
1039
1040 runtime[0] = runtime[1] = 0;
a978ba68 1041 clear_state = 0;
492158cf 1042 while (keep_running(td)) {
02bcaa8c 1043 fio_gettime(&td->start, NULL);
756867bd 1044 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
993bf48b 1045 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
ebac4655 1046
cf2dd8d6 1047 if (td->o.ratemin[0] || td->o.ratemin[1])
5ec10eaa
JA
1048 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1049 sizeof(td->lastrate));
ebac4655 1050
df9c26b1
JA
1051 if (clear_state)
1052 clear_io_state(td);
a978ba68 1053
ebac4655
JA
1054 prune_io_piece_log(td);
1055
ba0fbe10 1056 do_io(td);
ebac4655 1057
a978ba68
JA
1058 clear_state = 1;
1059
38d77cae 1060 if (td_read(td) && td->io_bytes[DDIR_READ]) {
0a4957c6 1061 elapsed = utime_since_now(&td->start);
38d77cae
JA
1062 runtime[DDIR_READ] += elapsed;
1063 }
1064 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
0a4957c6 1065 elapsed = utime_since_now(&td->start);
38d77cae
JA
1066 runtime[DDIR_WRITE] += elapsed;
1067 }
5ec10eaa 1068
ebac4655
JA
1069 if (td->error || td->terminate)
1070 break;
1071
e84c73a8
SL
1072 if (!td->o.do_verify ||
1073 td->o.verify == VERIFY_NONE ||
b67740d3 1074 (td->io_ops->flags & FIO_UNIDIR))
ebac4655
JA
1075 continue;
1076
df9c26b1 1077 clear_io_state(td);
a978ba68 1078
02bcaa8c 1079 fio_gettime(&td->start, NULL);
ebac4655
JA
1080
1081 do_verify(td);
1082
69008999 1083 runtime[DDIR_READ] += utime_since_now(&td->start);
ebac4655
JA
1084
1085 if (td->error || td->terminate)
1086 break;
1087 }
1088
36dff966 1089 update_rusage_stat(td);
47f0cc48
YHJT
1090 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1091 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
756867bd
JA
1092 td->ts.total_run_time = mtime_since_now(&td->epoch);
1093 td->ts.io_bytes[0] = td->io_bytes[0];
1094 td->ts.io_bytes[1] = td->io_bytes[1];
1095
d2bbb8d9 1096 fio_mutex_down(writeout_mutex);
e3cedca7 1097 if (td->ts.bw_log) {
f484470b
JA
1098 if (td->o.bw_log_file) {
1099 finish_log_named(td, td->ts.bw_log,
1100 td->o.bw_log_file, "bw");
1101 } else
e3cedca7
JA
1102 finish_log(td, td->ts.bw_log, "bw");
1103 }
1104 if (td->ts.slat_log) {
f484470b
JA
1105 if (td->o.lat_log_file) {
1106 finish_log_named(td, td->ts.slat_log,
1ca95738 1107 td->o.lat_log_file, "slat");
f484470b 1108 } else
e3cedca7
JA
1109 finish_log(td, td->ts.slat_log, "slat");
1110 }
1111 if (td->ts.clat_log) {
f484470b
JA
1112 if (td->o.lat_log_file) {
1113 finish_log_named(td, td->ts.clat_log,
1114 td->o.lat_log_file, "clat");
1115 } else
e3cedca7
JA
1116 finish_log(td, td->ts.clat_log, "clat");
1117 }
d2bbb8d9 1118 fio_mutex_up(writeout_mutex);
398e8c4d
JA
1119 if (td->o.exec_postrun)
1120 exec_string(td->o.exec_postrun);
ebac4655
JA
1121
1122 if (exitall_on_terminate)
390c40e2 1123 terminate_threads(td->groupid);
ebac4655
JA
1124
1125err:
5bf13a5a 1126 if (td->error)
5921e80c 1127 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
5ec10eaa 1128 td->verror);
24ffd2c2 1129 close_and_free_files(td);
2866c82d 1130 close_ioengine(td);
ebac4655 1131 cleanup_io_u(td);
f29b25a3 1132
d2ce18b5
JA
1133 if (td->o.cpumask_set) {
1134 int ret = fio_cpuset_exit(&td->o.cpumask);
1135
1136 td_verror(td, ret, "fio_cpuset_exit");
1137 }
4e78e405 1138
f29b25a3
JA
1139 /*
1140 * do this very late, it will log file closing as well
1141 */
1142 if (td->o.write_iolog_file)
1143 write_iolog_close(td);
1144
d23bb327 1145 options_mem_free(td);
ebac4655 1146 td_set_runstate(td, TD_EXITED);
43d76807 1147 return (void *) (unsigned long) td->error;
ebac4655
JA
1148}
1149
906c8d75
JA
1150/*
1151 * We cannot pass the td data into a forked process, so attach the td and
1152 * pass it to the thread worker.
1153 */
a6418147 1154static int fork_main(int shmid, int offset)
ebac4655
JA
1155{
1156 struct thread_data *td;
a6418147 1157 void *data, *ret;
ebac4655
JA
1158
1159 data = shmat(shmid, NULL, 0);
1160 if (data == (void *) -1) {
a6418147
JA
1161 int __err = errno;
1162
ebac4655 1163 perror("shmat");
a6418147 1164 return __err;
ebac4655
JA
1165 }
1166
1167 td = data + offset * sizeof(struct thread_data);
a6418147 1168 ret = thread_main(td);
ebac4655 1169 shmdt(data);
43d76807 1170 return (int) (unsigned long) ret;
ebac4655
JA
1171}
1172
906c8d75
JA
1173/*
1174 * Run over the job map and reap the threads that have exited, if any.
1175 */
ebac4655
JA
1176static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1177{
34572e28 1178 struct thread_data *td;
1f809d15 1179 int i, cputhreads, realthreads, pending, status, ret;
ebac4655
JA
1180
1181 /*
1182 * reap exited threads (TD_EXITED -> TD_REAPED)
1183 */
1f809d15 1184 realthreads = pending = cputhreads = 0;
34572e28 1185 for_each_td(td, i) {
3707f45b 1186 int flags = 0;
a2f77c9f 1187
84585003
JA
1188 /*
1189 * ->io_ops is NULL for a thread that has closed its
1190 * io engine
1191 */
ba0fbe10 1192 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
b990b5c0 1193 cputhreads++;
1f809d15
JA
1194 else
1195 realthreads++;
b990b5c0 1196
a1dedc1f
AC
1197 if (!td->pid) {
1198 pending++;
1199 continue;
1200 }
1201 if (td->runstate == TD_REAPED)
a2f77c9f 1202 continue;
2dc1bbeb 1203 if (td->o.use_thread) {
3707f45b
JA
1204 if (td->runstate == TD_EXITED) {
1205 td_set_runstate(td, TD_REAPED);
1206 goto reaped;
1207 }
1208 continue;
1209 }
a2f77c9f
JA
1210
1211 flags = WNOHANG;
1212 if (td->runstate == TD_EXITED)
1213 flags = 0;
1214
1215 /*
1216 * check if someone quit or got killed in an unusual way
1217 */
1218 ret = waitpid(td->pid, &status, flags);
3707f45b 1219 if (ret < 0) {
a2f77c9f 1220 if (errno == ECHILD) {
5921e80c
JA
1221 log_err("fio: pid=%d disappeared %d\n",
1222 (int) td->pid, td->runstate);
a2f77c9f
JA
1223 td_set_runstate(td, TD_REAPED);
1224 goto reaped;
1225 }
1226 perror("waitpid");
1227 } else if (ret == td->pid) {
1228 if (WIFSIGNALED(status)) {
fab6aa71
JA
1229 int sig = WTERMSIG(status);
1230
d9244941 1231 if (sig != SIGQUIT)
5ec10eaa 1232 log_err("fio: pid=%d, got signal=%d\n",
5921e80c 1233 (int) td->pid, sig);
fab6aa71
JA
1234 td_set_runstate(td, TD_REAPED);
1235 goto reaped;
1236 }
a2f77c9f
JA
1237 if (WIFEXITED(status)) {
1238 if (WEXITSTATUS(status) && !td->error)
1239 td->error = WEXITSTATUS(status);
a2f77c9f 1240
a2f77c9f
JA
1241 td_set_runstate(td, TD_REAPED);
1242 goto reaped;
a6418147
JA
1243 }
1244 }
ebac4655 1245
a2f77c9f
JA
1246 /*
1247 * thread is not dead, continue
1248 */
e48676ba 1249 pending++;
a2f77c9f 1250 continue;
fab6aa71 1251reaped:
ebac4655 1252 (*nr_running)--;
581e7141
JA
1253 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1254 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
6f88bcb0
JA
1255 if (!td->pid)
1256 pending--;
a2f77c9f
JA
1257
1258 if (td->error)
1259 exit_value++;
d3eeeabc
JA
1260
1261 done_secs += mtime_since_now(&td->epoch) / 1000;
ebac4655 1262 }
b990b5c0 1263
1f809d15 1264 if (*nr_running == cputhreads && !pending && realthreads)
390c40e2 1265 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1266}
1267
be4ecfdf
JA
1268static void *gtod_thread_main(void *data)
1269{
1270 fio_mutex_up(startup_mutex);
1271
1272 /*
1273 * As long as we have jobs around, update the clock. It would be nice
1274 * to have some way of NOT hammering that CPU with gettimeofday(),
1275 * but I'm not sure what to use outside of a simple CPU nop to relax
1276 * it - we don't want to lose precision.
1277 */
1278 while (threads) {
1279 fio_gtod_update();
1280 nop;
1281 }
1282
1283 return NULL;
1284}
1285
1286static int fio_start_gtod_thread(void)
1287{
f42c153d
JA
1288 int ret;
1289
1290 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1291 if (ret) {
1292 log_err("Can't create gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1293 return 1;
1294 }
f42c153d
JA
1295
1296 ret = pthread_detach(gtod_thread);
1297 if (ret) {
1298 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1299 return 1;
1300 }
1301
29adda3c 1302 dprint(FD_MUTEX, "wait on startup_mutex\n");
be4ecfdf 1303 fio_mutex_down(startup_mutex);
29adda3c 1304 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
be4ecfdf
JA
1305 return 0;
1306}
1307
906c8d75
JA
1308/*
1309 * Main function for kicking off and reaping jobs, as needed.
1310 */
ebac4655
JA
1311static void run_threads(void)
1312{
ebac4655
JA
1313 struct thread_data *td;
1314 unsigned long spent;
1315 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1316
2f9ade3c
JA
1317 if (fio_pin_memory())
1318 return;
ebac4655 1319
be4ecfdf
JA
1320 if (fio_gtod_offload && fio_start_gtod_thread())
1321 return;
1322
c6ae0a5b 1323 if (!terse_output) {
9cedf167
JA
1324 printf("Starting ");
1325 if (nr_thread)
5ec10eaa
JA
1326 printf("%d thread%s", nr_thread,
1327 nr_thread > 1 ? "s" : "");
9cedf167
JA
1328 if (nr_process) {
1329 if (nr_thread)
1330 printf(" and ");
5ec10eaa
JA
1331 printf("%d process%s", nr_process,
1332 nr_process > 1 ? "es" : "");
9cedf167
JA
1333 }
1334 printf("\n");
c6ae0a5b
JA
1335 fflush(stdout);
1336 }
c04f7ec3 1337
697a606c 1338 set_sig_handlers();
4efa970e 1339
ebac4655
JA
1340 todo = thread_number;
1341 nr_running = 0;
1342 nr_started = 0;
1343 m_rate = t_rate = 0;
1344
34572e28 1345 for_each_td(td, i) {
263e529f 1346 print_status_init(td->thread_number - 1);
ebac4655 1347
2dc1bbeb 1348 if (!td->o.create_serialize) {
380cf265 1349 init_disk_util(td);
ebac4655 1350 continue;
380cf265 1351 }
ebac4655
JA
1352
1353 /*
1354 * do file setup here so it happens sequentially,
1355 * we don't want X number of threads getting their
1356 * client data interspersed on disk
1357 */
53cdc686 1358 if (setup_files(td)) {
5bf13a5a
JA
1359 exit_value++;
1360 if (td->error)
5921e80c
JA
1361 log_err("fio: pid=%d, err=%d/%s\n",
1362 (int) td->pid, td->error, td->verror);
ebac4655
JA
1363 td_set_runstate(td, TD_REAPED);
1364 todo--;
c69e8875
JA
1365 } else {
1366 struct fio_file *f;
1367 unsigned int i;
1368
1369 /*
1370 * for sharing to work, each job must always open
1371 * its own files. so close them, if we opened them
1372 * for creation
1373 */
22a57ba8
JA
1374 for_each_file(td, f, i) {
1375 if (fio_file_open(f))
1376 td_io_close_file(td, f);
1377 else
1378 assert(f->fd == -1);
1379 }
5ec10eaa 1380 }
380cf265
JA
1381
1382 init_disk_util(td);
ebac4655
JA
1383 }
1384
a2f77c9f
JA
1385 set_genesis_time();
1386
ebac4655 1387 while (todo) {
75154845
JA
1388 struct thread_data *map[MAX_JOBS];
1389 struct timeval this_start;
1390 int this_jobs = 0, left;
1391
ebac4655
JA
1392 /*
1393 * create threads (TD_NOT_CREATED -> TD_CREATED)
1394 */
34572e28 1395 for_each_td(td, i) {
ebac4655
JA
1396 if (td->runstate != TD_NOT_CREATED)
1397 continue;
1398
1399 /*
1400 * never got a chance to start, killed by other
1401 * thread for some reason
1402 */
1403 if (td->terminate) {
1404 todo--;
1405 continue;
1406 }
1407
2dc1bbeb 1408 if (td->o.start_delay) {
263e529f 1409 spent = mtime_since_genesis();
ebac4655 1410
2dc1bbeb 1411 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1412 continue;
1413 }
1414
6f88bcb0
JA
1415 if (td->o.stonewall && (nr_started || nr_running)) {
1416 dprint(FD_PROCESS, "%s: stonewall wait\n",
1417 td->o.name);
ebac4655 1418 break;
6f88bcb0 1419 }
ebac4655 1420
75154845
JA
1421 /*
1422 * Set state to created. Thread will transition
1423 * to TD_INITIALIZED when it's done setting up.
1424 */
ebac4655 1425 td_set_runstate(td, TD_CREATED);
75154845 1426 map[this_jobs++] = td;
ebac4655
JA
1427 nr_started++;
1428
2dc1bbeb 1429 if (td->o.use_thread) {
f42c153d
JA
1430 int ret;
1431
ee56ad50 1432 dprint(FD_PROCESS, "will pthread_create\n");
f42c153d
JA
1433 ret = pthread_create(&td->thread, NULL,
1434 thread_main, td);
1435 if (ret) {
1436 log_err("pthread_create: %s\n",
1437 strerror(ret));
ebac4655 1438 nr_started--;
c8bb6faf 1439 break;
ebac4655 1440 }
f42c153d
JA
1441 ret = pthread_detach(td->thread);
1442 if (ret)
1443 log_err("pthread_detach: %s",
1444 strerror(ret));
ebac4655 1445 } else {
5e1d306e 1446 pid_t pid;
ee56ad50 1447 dprint(FD_PROCESS, "will fork\n");
5e1d306e
JA
1448 pid = fork();
1449 if (!pid) {
a6418147
JA
1450 int ret = fork_main(shm_id, i);
1451
5e1d306e
JA
1452 _exit(ret);
1453 } else if (i == fio_debug_jobno)
1454 *fio_debug_jobp = pid;
ebac4655 1455 }
29adda3c 1456 dprint(FD_MUTEX, "wait on startup_mutex\n");
cdd18ad8 1457 fio_mutex_down(startup_mutex);
29adda3c 1458 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
ebac4655
JA
1459 }
1460
1461 /*
75154845
JA
1462 * Wait for the started threads to transition to
1463 * TD_INITIALIZED.
ebac4655 1464 */
02bcaa8c 1465 fio_gettime(&this_start, NULL);
75154845 1466 left = this_jobs;
6ce15a32 1467 while (left && !fio_abort) {
75154845
JA
1468 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1469 break;
1470
1471 usleep(100000);
1472
1473 for (i = 0; i < this_jobs; i++) {
1474 td = map[i];
1475 if (!td)
1476 continue;
b6f4d880 1477 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1478 map[i] = NULL;
1479 left--;
b6f4d880
JA
1480 } else if (td->runstate >= TD_EXITED) {
1481 map[i] = NULL;
1482 left--;
1483 todo--;
1484 nr_running++; /* work-around... */
75154845
JA
1485 }
1486 }
1487 }
1488
1489 if (left) {
3b70d7e5 1490 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1491 for (i = 0; i < this_jobs; i++) {
1492 td = map[i];
1493 if (!td)
1494 continue;
1495 kill(td->pid, SIGTERM);
1496 }
1497 break;
1498 }
1499
1500 /*
b6f4d880 1501 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1502 */
34572e28 1503 for_each_td(td, i) {
75154845 1504 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1505 continue;
1506
b29ee5b3
JA
1507 if (in_ramp_time(td))
1508 td_set_runstate(td, TD_RAMP);
1509 else
1510 td_set_runstate(td, TD_RUNNING);
ebac4655
JA
1511 nr_running++;
1512 nr_started--;
581e7141
JA
1513 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1514 t_rate += td->o.rate[0] + td->o.rate[1];
75154845 1515 todo--;
cdd18ad8 1516 fio_mutex_up(td->mutex);
ebac4655
JA
1517 }
1518
1519 reap_threads(&nr_running, &t_rate, &m_rate);
1520
1521 if (todo)
1522 usleep(100000);
1523 }
1524
1525 while (nr_running) {
1526 reap_threads(&nr_running, &t_rate, &m_rate);
1527 usleep(10000);
1528 }
1529
1530 update_io_ticks();
2f9ade3c 1531 fio_unpin_memory();
ebac4655
JA
1532}
1533
ebac4655
JA
1534int main(int argc, char *argv[])
1535{
29d610e1
JA
1536 long ps;
1537
2e5cdb11
JA
1538 sinit();
1539
dbe1125e
JA
1540 /*
1541 * We need locale for number printing, if it isn't set then just
1542 * go with the US format.
1543 */
1544 if (!getenv("LC_NUMERIC"))
1545 setlocale(LC_NUMERIC, "en_US");
1546
ebac4655
JA
1547 if (parse_options(argc, argv))
1548 return 1;
1549
4b472fa3
JA
1550 if (!thread_number)
1551 return 0;
ebac4655 1552
29d610e1
JA
1553 ps = sysconf(_SC_PAGESIZE);
1554 if (ps < 0) {
1555 log_err("Failed to get page size\n");
1556 return 1;
1557 }
1558
cfc99db7 1559 page_size = ps;
29d610e1
JA
1560 page_mask = ps - 1;
1561
bb3884d8
JA
1562 if (write_bw_log) {
1563 setup_log(&agg_io_log[DDIR_READ]);
1564 setup_log(&agg_io_log[DDIR_WRITE]);
1565 }
1566
cdd18ad8 1567 startup_mutex = fio_mutex_init(0);
d2bbb8d9 1568 writeout_mutex = fio_mutex_init(1);
07739b57 1569
a2f77c9f
JA
1570 set_genesis_time();
1571
ccb0fa24 1572 status_timer_arm();
ebac4655
JA
1573
1574 run_threads();
6ce15a32 1575
bb3884d8 1576 if (!fio_abort) {
6ce15a32 1577 show_run_stats();
bb3884d8 1578 if (write_bw_log) {
5ec10eaa
JA
1579 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1580 __finish_log(agg_io_log[DDIR_WRITE],
1581 "agg-write_bw.log");
bb3884d8
JA
1582 }
1583 }
ebac4655 1584
cdd18ad8 1585 fio_mutex_remove(startup_mutex);
d2bbb8d9 1586 fio_mutex_remove(writeout_mutex);
437c9b71 1587 return exit_value;
ebac4655 1588}