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