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