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