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