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