Add option priorities
[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) {
4950421a 358 int ret2, full;
5451792e 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 */
4950421a
JA
438 full = queue_full(td) || ret == FIO_Q_BUSY;
439 if (full || !td->o.iodepth_batch_complete) {
440 min_events = td->o.iodepth_batch_complete;
441 if (full && !min_events)
838bc709 442 min_events = 1;
e916b390 443
4950421a
JA
444 do {
445 /*
446 * Reap required number of io units, if any,
447 * and do the verification on them through
448 * the callback handler
449 */
450 if (io_u_queued_complete(td, min_events) < 0) {
451 ret = -1;
452 break;
453 }
454 } while (full && (td->cur_depth > td->o.iodepth_low));
455 }
456 if (ret < 0)
ebac4655 457 break;
36167d82 458 }
ebac4655 459
c01c0395
JA
460 if (!td->error) {
461 min_events = td->cur_depth;
462
463 if (min_events)
464 ret = io_u_queued_complete(td, min_events);
465 } else
ebac4655
JA
466 cleanup_pending_aio(td);
467
468 td_set_runstate(td, TD_RUNNING);
469}
470
32cd46a0 471/*
906c8d75 472 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
473 * and reaps them, checking for rate and errors along the way.
474 */
ebac4655
JA
475static void do_io(struct thread_data *td)
476{
02bcaa8c 477 struct timeval s;
ebac4655 478 unsigned long usec;
af52b345
JA
479 unsigned int i;
480 int ret = 0;
ebac4655 481
5853e5a8
JA
482 td_set_runstate(td, TD_RUNNING);
483
7bb48f84 484 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
97601024
JA
485 struct timeval comp_time;
486 long bytes_done = 0;
84585003 487 int min_evts = 0;
ebac4655 488 struct io_u *io_u;
4950421a 489 int ret2, full;
ebac4655
JA
490
491 if (td->terminate)
492 break;
493
3d7c391d 494 io_u = get_io_u(td);
ebac4655
JA
495 if (!io_u)
496 break;
497
498 memcpy(&s, &io_u->start_time, sizeof(s));
97601024
JA
499
500 if (runtime_exceeded(td, &s)) {
501 put_io_u(td, io_u);
90a87d4b 502 td->terminate = 1;
97601024
JA
503 break;
504 }
36167d82 505
b67740d3
JA
506 /*
507 * Add verification end_io handler, if asked to verify
508 * a previously written file.
509 */
cc3fc855 510 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
b67740d3 511 io_u->end_io = verify_io_u;
d8fab1b6
JA
512 td_set_runstate(td, TD_VERIFYING);
513 } else
514 td_set_runstate(td, TD_RUNNING);
b67740d3 515
11786802 516 ret = td_io_queue(td, io_u);
36167d82
JA
517 switch (ret) {
518 case FIO_Q_COMPLETED:
5451792e
JA
519 if (io_u->error)
520 ret = -io_u->error;
9e9d164e 521 else if (io_u->resid) {
36167d82 522 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 523 struct fio_file *f = io_u->file;
36167d82 524
9e9d164e
JA
525 /*
526 * zero read, fail
527 */
528 if (!bytes) {
41d9f0fd 529 td_verror(td, EIO, "full resid");
9e9d164e
JA
530 put_io_u(td, io_u);
531 break;
532 }
533
cec6b55d 534 io_u->xfer_buflen = io_u->resid;
36167d82 535 io_u->xfer_buf += bytes;
5a7c5680
JA
536 io_u->offset += bytes;
537
30061b97
JA
538 td->ts.short_io_u[io_u->ddir]++;
539
d460eb31 540 if (io_u->offset == f->real_file_size)
5a7c5680
JA
541 goto sync_done;
542
11786802
JA
543 requeue_io_u(td, &io_u);
544 } else {
5a7c5680 545sync_done:
11786802
JA
546 fio_gettime(&comp_time, NULL);
547 bytes_done = io_u_sync_complete(td, io_u);
548 if (bytes_done < 0)
549 ret = bytes_done;
cec6b55d 550 }
36167d82
JA
551 break;
552 case FIO_Q_QUEUED:
7e77dd02
JA
553 /*
554 * if the engine doesn't have a commit hook,
555 * the io_u is really queued. if it does have such
556 * a hook, it has to call io_u_queued() itself.
557 */
558 if (td->io_ops->commit == NULL)
559 io_u_queued(td, io_u);
36167d82 560 break;
755200a3
JA
561 case FIO_Q_BUSY:
562 requeue_io_u(td, &io_u);
5451792e
JA
563 ret2 = td_io_commit(td);
564 if (ret2 < 0)
565 ret = ret2;
755200a3 566 break;
36167d82
JA
567 default:
568 assert(ret < 0);
569 put_io_u(td, io_u);
570 break;
ebac4655
JA
571 }
572
99784632 573 if (ret < 0 || td->error)
36167d82
JA
574 break;
575
97601024
JA
576 /*
577 * See if we need to complete some commands
578 */
4950421a
JA
579 full = queue_full(td) || ret == FIO_Q_BUSY;
580 if (full || !td->o.iodepth_batch_complete) {
581 min_evts = td->o.iodepth_batch_complete;
582 if (full && !min_evts)
36167d82 583 min_evts = 1;
4950421a 584
97601024 585 fio_gettime(&comp_time, NULL);
4950421a
JA
586
587 do {
588 ret = io_u_queued_complete(td, min_evts);
589 if (ret <= 0)
590 break;
591
592 bytes_done += ret;
593 } while (full && (td->cur_depth > td->o.iodepth_low));
ebac4655
JA
594 }
595
4950421a
JA
596 if (ret < 0)
597 break;
97601024
JA
598 if (!bytes_done)
599 continue;
600
ebac4655
JA
601 /*
602 * the rate is batched for now, it should work for batches
603 * of completions except the very first one which may look
604 * a little bursty
605 */
97601024 606 usec = utime_since(&s, &comp_time);
ebac4655 607
413dd459 608 rate_throttle(td, usec, bytes_done);
ebac4655 609
97601024 610 if (check_min_rate(td, &comp_time)) {
98aa62d8 611 if (exitall_on_terminate)
390c40e2 612 terminate_threads(td->groupid);
41d9f0fd 613 td_verror(td, EIO, "check_min_rate");
ebac4655
JA
614 break;
615 }
616
2dc1bbeb 617 if (td->o.thinktime) {
9c1f7434
JA
618 unsigned long long b;
619
620 b = td->io_blocks[0] + td->io_blocks[1];
2dc1bbeb 621 if (!(b % td->o.thinktime_blocks)) {
48097d5c
JA
622 int left;
623
2dc1bbeb
JA
624 if (td->o.thinktime_spin)
625 __usec_sleep(td->o.thinktime_spin);
48097d5c 626
2dc1bbeb 627 left = td->o.thinktime - td->o.thinktime_spin;
48097d5c
JA
628 if (left)
629 usec_sleep(td, left);
630 }
9c1f7434 631 }
ebac4655
JA
632 }
633
aa31f1f1
SL
634 if (td->o.fill_device && td->error == ENOSPC) {
635 td->error = 0;
636 td->terminate = 1;
637 }
4d2413c6 638 if (!td->error) {
3d7c391d
JA
639 struct fio_file *f;
640
c01c0395
JA
641 i = td->cur_depth;
642 if (i)
643 ret = io_u_queued_complete(td, i);
ebac4655 644
2dc1bbeb 645 if (should_fsync(td) && td->o.end_fsync) {
84585003 646 td_set_runstate(td, TD_FSYNCING);
3d7b485f
JA
647
648 for_each_file(td, f, i) {
649 if (!(f->flags & FIO_FILE_OPEN))
650 continue;
858a3d47 651 fio_io_sync(td, f);
3d7b485f 652 }
84585003 653 }
c01c0395
JA
654 } else
655 cleanup_pending_aio(td);
2ba1c290
JA
656
657 /*
658 * stop job if we failed doing any IO
659 */
660 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
661 td->done = 1;
ebac4655
JA
662}
663
ebac4655
JA
664static void cleanup_io_u(struct thread_data *td)
665{
01743ee1 666 struct flist_head *entry, *n;
ebac4655
JA
667 struct io_u *io_u;
668
01743ee1
JA
669 flist_for_each_safe(entry, n, &td->io_u_freelist) {
670 io_u = flist_entry(entry, struct io_u, list);
ebac4655 671
01743ee1 672 flist_del(&io_u->list);
ebac4655
JA
673 free(io_u);
674 }
675
2f9ade3c 676 free_io_mem(td);
ebac4655
JA
677}
678
679static int init_io_u(struct thread_data *td)
680{
681 struct io_u *io_u;
a00735e6 682 unsigned int max_bs;
ebac4655
JA
683 int i, max_units;
684 char *p;
685
1e3c09af 686 max_units = td->o.iodepth;
2dc1bbeb 687 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
5ec10eaa
JA
688 td->orig_buffer_size = (unsigned long long) max_bs
689 * (unsigned long long) max_units;
690
691 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
692 unsigned long bs;
74b025b0 693
5ec10eaa
JA
694 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
695 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
696 }
ebac4655 697
c3990645
JA
698 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
699 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
700 return 1;
701 }
702
2f9ade3c
JA
703 if (allocate_io_mem(td))
704 return 1;
ebac4655 705
b3f378e9
JA
706 if (td->o.odirect)
707 p = ALIGN(td->orig_buffer);
708 else
709 p = td->orig_buffer;
710
ebac4655 711 for (i = 0; i < max_units; i++) {
db1cd90b
JA
712 if (td->terminate)
713 return 1;
ebac4655
JA
714 io_u = malloc(sizeof(*io_u));
715 memset(io_u, 0, sizeof(*io_u));
01743ee1 716 INIT_FLIST_HEAD(&io_u->list);
ebac4655 717
b4c5e1ac
JA
718 if (!(td->io_ops->flags & FIO_NOIO)) {
719 io_u->buf = p + max_bs * i;
e9459e5a 720
5973cafb
JA
721 if (td_write(td) && !td->o.refill_buffers)
722 io_u_fill_buffer(td, io_u, max_bs);
b4c5e1ac 723 }
6b9cea23 724
b1ff3403 725 io_u->index = i;
0c6e7517 726 io_u->flags = IO_U_F_FREE;
01743ee1 727 flist_add(&io_u->list, &td->io_u_freelist);
ebac4655
JA
728 }
729
433afcb4
JA
730 io_u_init_timeout();
731
ebac4655
JA
732 return 0;
733}
734
da86774e
JA
735static int switch_ioscheduler(struct thread_data *td)
736{
737 char tmp[256], tmp2[128];
738 FILE *f;
739 int ret;
740
ba0fbe10 741 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
742 return 0;
743
da86774e
JA
744 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
745
746 f = fopen(tmp, "r+");
747 if (!f) {
cbf5c1d7 748 if (errno == ENOENT) {
5ec10eaa
JA
749 log_err("fio: os or kernel doesn't support IO scheduler"
750 " switching\n");
cbf5c1d7
JA
751 return 0;
752 }
753 td_verror(td, errno, "fopen iosched");
da86774e
JA
754 return 1;
755 }
756
757 /*
758 * Set io scheduler.
759 */
2dc1bbeb 760 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
da86774e 761 if (ferror(f) || ret != 1) {
e1161c32 762 td_verror(td, errno, "fwrite");
da86774e
JA
763 fclose(f);
764 return 1;
765 }
766
767 rewind(f);
768
769 /*
770 * Read back and check that the selected scheduler is now the default.
771 */
772 ret = fread(tmp, 1, sizeof(tmp), f);
773 if (ferror(f) || ret < 0) {
e1161c32 774 td_verror(td, errno, "fread");
da86774e
JA
775 fclose(f);
776 return 1;
777 }
778
2dc1bbeb 779 sprintf(tmp2, "[%s]", td->o.ioscheduler);
da86774e 780 if (!strstr(tmp, tmp2)) {
2dc1bbeb 781 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
e1161c32 782 td_verror(td, EINVAL, "iosched_switch");
da86774e
JA
783 fclose(f);
784 return 1;
785 }
786
787 fclose(f);
788 return 0;
789}
790
492158cf
JA
791static int keep_running(struct thread_data *td)
792{
793 unsigned long long io_done;
794
20e354ef
JA
795 if (td->done)
796 return 0;
492158cf
JA
797 if (td->o.time_based)
798 return 1;
799 if (td->o.loops) {
800 td->o.loops--;
801 return 1;
802 }
803
5ec10eaa
JA
804 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
805 + td->io_skip_bytes;
492158cf
JA
806 if (io_done < td->o.size)
807 return 1;
808
809 return 0;
810}
811
a978ba68 812static int clear_io_state(struct thread_data *td)
ebac4655 813{
53cdc686 814 struct fio_file *f;
af52b345
JA
815 unsigned int i;
816 int ret;
ebac4655 817
756867bd 818 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
ebac4655 819 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 820 td->zone_bytes = 0;
d7d3b49b 821 td->rate_bytes = 0;
4e991c23 822 td->rate_blocks = 0;
38d77cae 823 td->rw_end_set[0] = td->rw_end_set[1] = 0;
ebac4655 824
c1324df1
JA
825 td->last_was_sync = 0;
826
2ba1c290
JA
827 /*
828 * reset file done count if we are to start over
829 */
830 if (td->o.time_based || td->o.loops)
48f5abd3 831 td->nr_done_files = 0;
281f9b63 832
24ffd2c2 833 close_files(td);
53cdc686 834
a978ba68
JA
835 ret = 0;
836 for_each_file(td, f, i) {
281f9b63 837 f->flags &= ~FIO_FILE_DONE;
a978ba68
JA
838 ret = td_io_open_file(td, f);
839 if (ret)
840 break;
53cdc686 841 }
a978ba68
JA
842
843 return ret;
ebac4655
JA
844}
845
906c8d75
JA
846/*
847 * Entry point for the thread based jobs. The process based jobs end up
848 * here as well, after a little setup.
849 */
ebac4655
JA
850static void *thread_main(void *data)
851{
10927316 852 unsigned long long runtime[2], elapsed;
ebac4655 853 struct thread_data *td = data;
a978ba68 854 int clear_state;
ebac4655 855
2dc1bbeb 856 if (!td->o.use_thread)
ebac4655
JA
857 setsid();
858
859 td->pid = getpid();
860
5921e80c 861 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
ee56ad50 862
01743ee1
JA
863 INIT_FLIST_HEAD(&td->io_u_freelist);
864 INIT_FLIST_HEAD(&td->io_u_busylist);
865 INIT_FLIST_HEAD(&td->io_u_requeues);
866 INIT_FLIST_HEAD(&td->io_log_list);
867 INIT_FLIST_HEAD(&td->io_hist_list);
bb5d7d0b 868 td->io_hist_tree = RB_ROOT;
aea47d44 869
db1cd90b 870 td_set_runstate(td, TD_INITIALIZED);
cdd18ad8
JA
871 fio_mutex_up(startup_mutex);
872 fio_mutex_down(td->mutex);
db1cd90b
JA
873
874 /*
cdd18ad8 875 * the ->mutex mutex is now no longer used, close it to avoid
db1cd90b
JA
876 * eating a file descriptor
877 */
cdd18ad8 878 fio_mutex_remove(td->mutex);
db1cd90b 879
d84f8d49
JA
880 /*
881 * May alter parameters that init_io_u() will use, so we need to
882 * do this first.
883 */
884 if (init_iolog(td))
885 goto err;
886
ebac4655 887 if (init_io_u(td))
db1cd90b 888 goto err;
ebac4655 889
375b2695 890 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
e1161c32 891 td_verror(td, errno, "cpu_set_affinity");
db1cd90b 892 goto err;
ebac4655
JA
893 }
894
ac684785 895 if (td->ioprio_set) {
ebac4655 896 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
e1161c32 897 td_verror(td, errno, "ioprio_set");
db1cd90b 898 goto err;
ebac4655
JA
899 }
900 }
901
2dc1bbeb 902 if (nice(td->o.nice) == -1) {
e1161c32 903 td_verror(td, errno, "nice");
db1cd90b 904 goto err;
b6f4d880
JA
905 }
906
2dc1bbeb 907 if (td->o.ioscheduler && switch_ioscheduler(td))
db1cd90b 908 goto err;
37f56873 909
2dc1bbeb 910 if (!td->o.create_serialize && setup_files(td))
ebac4655
JA
911 goto err;
912
7d6c5283
JA
913 if (td_io_init(td))
914 goto err;
915
b5af8293
JA
916 if (open_files(td))
917 goto err;
918
68727076
JA
919 if (init_random_map(td))
920 goto err;
921
2dc1bbeb
JA
922 if (td->o.exec_prerun) {
923 if (system(td->o.exec_prerun) < 0)
69cfd7e0
JA
924 goto err;
925 }
4e0ba8af 926
69008999 927 fio_gettime(&td->epoch, NULL);
433afcb4 928 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
756867bd 929 getrusage(RUSAGE_SELF, &td->ts.ru_start);
69008999
JA
930
931 runtime[0] = runtime[1] = 0;
a978ba68 932 clear_state = 0;
492158cf 933 while (keep_running(td)) {
02bcaa8c 934 fio_gettime(&td->start, NULL);
756867bd 935 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
ebac4655 936
2dc1bbeb 937 if (td->o.ratemin)
5ec10eaa
JA
938 memcpy(&td->lastrate, &td->ts.stat_sample_time,
939 sizeof(td->lastrate));
ebac4655 940
a978ba68
JA
941 if (clear_state && clear_io_state(td))
942 break;
943
ebac4655
JA
944 prune_io_piece_log(td);
945
ba0fbe10 946 do_io(td);
ebac4655 947
a978ba68
JA
948 clear_state = 1;
949
38d77cae
JA
950 if (td_read(td) && td->io_bytes[DDIR_READ]) {
951 if (td->rw_end_set[DDIR_READ])
5ec10eaa
JA
952 elapsed = utime_since(&td->start,
953 &td->rw_end[DDIR_READ]);
38d77cae
JA
954 else
955 elapsed = utime_since_now(&td->start);
956
957 runtime[DDIR_READ] += elapsed;
958 }
959 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
960 if (td->rw_end_set[DDIR_WRITE])
5ec10eaa
JA
961 elapsed = utime_since(&td->start,
962 &td->rw_end[DDIR_WRITE]);
38d77cae
JA
963 else
964 elapsed = utime_since_now(&td->start);
965
966 runtime[DDIR_WRITE] += elapsed;
967 }
5ec10eaa 968
ebac4655
JA
969 if (td->error || td->terminate)
970 break;
971
e84c73a8
SL
972 if (!td->o.do_verify ||
973 td->o.verify == VERIFY_NONE ||
b67740d3 974 (td->io_ops->flags & FIO_UNIDIR))
ebac4655
JA
975 continue;
976
a978ba68
JA
977 if (clear_io_state(td))
978 break;
979
02bcaa8c 980 fio_gettime(&td->start, NULL);
ebac4655
JA
981
982 do_verify(td);
983
69008999 984 runtime[DDIR_READ] += utime_since_now(&td->start);
ebac4655
JA
985
986 if (td->error || td->terminate)
987 break;
988 }
989
36dff966 990 update_rusage_stat(td);
47f0cc48
YHJT
991 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
992 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
756867bd
JA
993 td->ts.total_run_time = mtime_since_now(&td->epoch);
994 td->ts.io_bytes[0] = td->io_bytes[0];
995 td->ts.io_bytes[1] = td->io_bytes[1];
996
997 if (td->ts.bw_log)
998 finish_log(td, td->ts.bw_log, "bw");
999 if (td->ts.slat_log)
1000 finish_log(td, td->ts.slat_log, "slat");
1001 if (td->ts.clat_log)
1002 finish_log(td, td->ts.clat_log, "clat");
2dc1bbeb
JA
1003 if (td->o.exec_postrun) {
1004 if (system(td->o.exec_postrun) < 0)
1005 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
69cfd7e0 1006 }
ebac4655
JA
1007
1008 if (exitall_on_terminate)
390c40e2 1009 terminate_threads(td->groupid);
ebac4655
JA
1010
1011err:
5bf13a5a 1012 if (td->error)
5921e80c 1013 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
5ec10eaa 1014 td->verror);
24ffd2c2 1015 close_and_free_files(td);
2866c82d 1016 close_ioengine(td);
ebac4655 1017 cleanup_io_u(td);
f29b25a3
JA
1018
1019 /*
1020 * do this very late, it will log file closing as well
1021 */
1022 if (td->o.write_iolog_file)
1023 write_iolog_close(td);
1024
d23bb327 1025 options_mem_free(td);
ebac4655 1026 td_set_runstate(td, TD_EXITED);
43d76807 1027 return (void *) (unsigned long) td->error;
ebac4655
JA
1028}
1029
906c8d75
JA
1030/*
1031 * We cannot pass the td data into a forked process, so attach the td and
1032 * pass it to the thread worker.
1033 */
a6418147 1034static int fork_main(int shmid, int offset)
ebac4655
JA
1035{
1036 struct thread_data *td;
a6418147 1037 void *data, *ret;
ebac4655
JA
1038
1039 data = shmat(shmid, NULL, 0);
1040 if (data == (void *) -1) {
a6418147
JA
1041 int __err = errno;
1042
ebac4655 1043 perror("shmat");
a6418147 1044 return __err;
ebac4655
JA
1045 }
1046
1047 td = data + offset * sizeof(struct thread_data);
a6418147 1048 ret = thread_main(td);
ebac4655 1049 shmdt(data);
43d76807 1050 return (int) (unsigned long) ret;
ebac4655
JA
1051}
1052
906c8d75
JA
1053/*
1054 * Run over the job map and reap the threads that have exited, if any.
1055 */
ebac4655
JA
1056static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1057{
34572e28 1058 struct thread_data *td;
1f809d15 1059 int i, cputhreads, realthreads, pending, status, ret;
ebac4655
JA
1060
1061 /*
1062 * reap exited threads (TD_EXITED -> TD_REAPED)
1063 */
1f809d15 1064 realthreads = pending = cputhreads = 0;
34572e28 1065 for_each_td(td, i) {
3707f45b 1066 int flags = 0;
a2f77c9f 1067
84585003
JA
1068 /*
1069 * ->io_ops is NULL for a thread that has closed its
1070 * io engine
1071 */
ba0fbe10 1072 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
b990b5c0 1073 cputhreads++;
1f809d15
JA
1074 else
1075 realthreads++;
b990b5c0 1076
a1dedc1f
AC
1077 if (!td->pid) {
1078 pending++;
1079 continue;
1080 }
1081 if (td->runstate == TD_REAPED)
a2f77c9f 1082 continue;
2dc1bbeb 1083 if (td->o.use_thread) {
3707f45b
JA
1084 if (td->runstate == TD_EXITED) {
1085 td_set_runstate(td, TD_REAPED);
1086 goto reaped;
1087 }
1088 continue;
1089 }
a2f77c9f
JA
1090
1091 flags = WNOHANG;
1092 if (td->runstate == TD_EXITED)
1093 flags = 0;
1094
1095 /*
1096 * check if someone quit or got killed in an unusual way
1097 */
1098 ret = waitpid(td->pid, &status, flags);
3707f45b 1099 if (ret < 0) {
a2f77c9f 1100 if (errno == ECHILD) {
5921e80c
JA
1101 log_err("fio: pid=%d disappeared %d\n",
1102 (int) td->pid, td->runstate);
a2f77c9f
JA
1103 td_set_runstate(td, TD_REAPED);
1104 goto reaped;
1105 }
1106 perror("waitpid");
1107 } else if (ret == td->pid) {
1108 if (WIFSIGNALED(status)) {
fab6aa71
JA
1109 int sig = WTERMSIG(status);
1110
d9244941 1111 if (sig != SIGQUIT)
5ec10eaa 1112 log_err("fio: pid=%d, got signal=%d\n",
5921e80c 1113 (int) td->pid, sig);
fab6aa71
JA
1114 td_set_runstate(td, TD_REAPED);
1115 goto reaped;
1116 }
a2f77c9f
JA
1117 if (WIFEXITED(status)) {
1118 if (WEXITSTATUS(status) && !td->error)
1119 td->error = WEXITSTATUS(status);
a2f77c9f 1120
a2f77c9f
JA
1121 td_set_runstate(td, TD_REAPED);
1122 goto reaped;
a6418147
JA
1123 }
1124 }
ebac4655 1125
a2f77c9f
JA
1126 /*
1127 * thread is not dead, continue
1128 */
e48676ba 1129 pending++;
a2f77c9f 1130 continue;
fab6aa71 1131reaped:
ebac4655 1132 (*nr_running)--;
2dc1bbeb
JA
1133 (*m_rate) -= td->o.ratemin;
1134 (*t_rate) -= td->o.rate;
6f88bcb0
JA
1135 if (!td->pid)
1136 pending--;
a2f77c9f
JA
1137
1138 if (td->error)
1139 exit_value++;
d3eeeabc
JA
1140
1141 done_secs += mtime_since_now(&td->epoch) / 1000;
ebac4655 1142 }
b990b5c0 1143
1f809d15 1144 if (*nr_running == cputhreads && !pending && realthreads)
390c40e2 1145 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1146}
1147
906c8d75
JA
1148/*
1149 * Main function for kicking off and reaping jobs, as needed.
1150 */
ebac4655
JA
1151static void run_threads(void)
1152{
ebac4655
JA
1153 struct thread_data *td;
1154 unsigned long spent;
1155 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1156
2f9ade3c
JA
1157 if (fio_pin_memory())
1158 return;
ebac4655 1159
c6ae0a5b 1160 if (!terse_output) {
9cedf167
JA
1161 printf("Starting ");
1162 if (nr_thread)
5ec10eaa
JA
1163 printf("%d thread%s", nr_thread,
1164 nr_thread > 1 ? "s" : "");
9cedf167
JA
1165 if (nr_process) {
1166 if (nr_thread)
1167 printf(" and ");
5ec10eaa
JA
1168 printf("%d process%s", nr_process,
1169 nr_process > 1 ? "es" : "");
9cedf167
JA
1170 }
1171 printf("\n");
c6ae0a5b
JA
1172 fflush(stdout);
1173 }
c04f7ec3 1174
697a606c 1175 set_sig_handlers();
4efa970e 1176
ebac4655
JA
1177 todo = thread_number;
1178 nr_running = 0;
1179 nr_started = 0;
1180 m_rate = t_rate = 0;
1181
34572e28 1182 for_each_td(td, i) {
263e529f 1183 print_status_init(td->thread_number - 1);
ebac4655 1184
2dc1bbeb 1185 if (!td->o.create_serialize) {
380cf265 1186 init_disk_util(td);
ebac4655 1187 continue;
380cf265 1188 }
ebac4655
JA
1189
1190 /*
1191 * do file setup here so it happens sequentially,
1192 * we don't want X number of threads getting their
1193 * client data interspersed on disk
1194 */
53cdc686 1195 if (setup_files(td)) {
5bf13a5a
JA
1196 exit_value++;
1197 if (td->error)
5921e80c
JA
1198 log_err("fio: pid=%d, err=%d/%s\n",
1199 (int) td->pid, td->error, td->verror);
ebac4655
JA
1200 td_set_runstate(td, TD_REAPED);
1201 todo--;
c69e8875
JA
1202 } else {
1203 struct fio_file *f;
1204 unsigned int i;
1205
1206 /*
1207 * for sharing to work, each job must always open
1208 * its own files. so close them, if we opened them
1209 * for creation
1210 */
1211 for_each_file(td, f, i)
1212 td_io_close_file(td, f);
5ec10eaa 1213 }
380cf265
JA
1214
1215 init_disk_util(td);
ebac4655
JA
1216 }
1217
a2f77c9f
JA
1218 set_genesis_time();
1219
ebac4655 1220 while (todo) {
75154845
JA
1221 struct thread_data *map[MAX_JOBS];
1222 struct timeval this_start;
1223 int this_jobs = 0, left;
1224
ebac4655
JA
1225 /*
1226 * create threads (TD_NOT_CREATED -> TD_CREATED)
1227 */
34572e28 1228 for_each_td(td, i) {
ebac4655
JA
1229 if (td->runstate != TD_NOT_CREATED)
1230 continue;
1231
1232 /*
1233 * never got a chance to start, killed by other
1234 * thread for some reason
1235 */
1236 if (td->terminate) {
1237 todo--;
1238 continue;
1239 }
1240
2dc1bbeb 1241 if (td->o.start_delay) {
263e529f 1242 spent = mtime_since_genesis();
ebac4655 1243
2dc1bbeb 1244 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1245 continue;
1246 }
1247
6f88bcb0
JA
1248 if (td->o.stonewall && (nr_started || nr_running)) {
1249 dprint(FD_PROCESS, "%s: stonewall wait\n",
1250 td->o.name);
ebac4655 1251 break;
6f88bcb0 1252 }
ebac4655 1253
75154845
JA
1254 /*
1255 * Set state to created. Thread will transition
1256 * to TD_INITIALIZED when it's done setting up.
1257 */
ebac4655 1258 td_set_runstate(td, TD_CREATED);
75154845 1259 map[this_jobs++] = td;
ebac4655
JA
1260 nr_started++;
1261
2dc1bbeb 1262 if (td->o.use_thread) {
ee56ad50 1263 dprint(FD_PROCESS, "will pthread_create\n");
5ec10eaa
JA
1264 if (pthread_create(&td->thread, NULL,
1265 thread_main, td)) {
f4705a73 1266 perror("pthread_create");
ebac4655 1267 nr_started--;
c8bb6faf 1268 break;
ebac4655 1269 }
f4705a73
JA
1270 if (pthread_detach(td->thread) < 0)
1271 perror("pthread_detach");
ebac4655 1272 } else {
5e1d306e 1273 pid_t pid;
ee56ad50 1274 dprint(FD_PROCESS, "will fork\n");
5e1d306e
JA
1275 pid = fork();
1276 if (!pid) {
a6418147
JA
1277 int ret = fork_main(shm_id, i);
1278
5e1d306e
JA
1279 _exit(ret);
1280 } else if (i == fio_debug_jobno)
1281 *fio_debug_jobp = pid;
ebac4655 1282 }
cdd18ad8 1283 fio_mutex_down(startup_mutex);
ebac4655
JA
1284 }
1285
1286 /*
75154845
JA
1287 * Wait for the started threads to transition to
1288 * TD_INITIALIZED.
ebac4655 1289 */
02bcaa8c 1290 fio_gettime(&this_start, NULL);
75154845 1291 left = this_jobs;
6ce15a32 1292 while (left && !fio_abort) {
75154845
JA
1293 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1294 break;
1295
1296 usleep(100000);
1297
1298 for (i = 0; i < this_jobs; i++) {
1299 td = map[i];
1300 if (!td)
1301 continue;
b6f4d880 1302 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1303 map[i] = NULL;
1304 left--;
b6f4d880
JA
1305 } else if (td->runstate >= TD_EXITED) {
1306 map[i] = NULL;
1307 left--;
1308 todo--;
1309 nr_running++; /* work-around... */
75154845
JA
1310 }
1311 }
1312 }
1313
1314 if (left) {
3b70d7e5 1315 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1316 for (i = 0; i < this_jobs; i++) {
1317 td = map[i];
1318 if (!td)
1319 continue;
1320 kill(td->pid, SIGTERM);
1321 }
1322 break;
1323 }
1324
1325 /*
b6f4d880 1326 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1327 */
34572e28 1328 for_each_td(td, i) {
75154845 1329 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1330 continue;
1331
1332 td_set_runstate(td, TD_RUNNING);
1333 nr_running++;
1334 nr_started--;
2dc1bbeb
JA
1335 m_rate += td->o.ratemin;
1336 t_rate += td->o.rate;
75154845 1337 todo--;
cdd18ad8 1338 fio_mutex_up(td->mutex);
ebac4655
JA
1339 }
1340
1341 reap_threads(&nr_running, &t_rate, &m_rate);
1342
1343 if (todo)
1344 usleep(100000);
1345 }
1346
1347 while (nr_running) {
1348 reap_threads(&nr_running, &t_rate, &m_rate);
1349 usleep(10000);
1350 }
1351
1352 update_io_ticks();
2f9ade3c 1353 fio_unpin_memory();
ebac4655
JA
1354}
1355
ebac4655
JA
1356int main(int argc, char *argv[])
1357{
29d610e1
JA
1358 long ps;
1359
2e5cdb11
JA
1360 sinit();
1361
dbe1125e
JA
1362 /*
1363 * We need locale for number printing, if it isn't set then just
1364 * go with the US format.
1365 */
1366 if (!getenv("LC_NUMERIC"))
1367 setlocale(LC_NUMERIC, "en_US");
1368
ebac4655
JA
1369 if (parse_options(argc, argv))
1370 return 1;
1371
4b472fa3
JA
1372 if (!thread_number)
1373 return 0;
ebac4655 1374
29d610e1
JA
1375 ps = sysconf(_SC_PAGESIZE);
1376 if (ps < 0) {
1377 log_err("Failed to get page size\n");
1378 return 1;
1379 }
1380
cfc99db7 1381 page_size = ps;
29d610e1
JA
1382 page_mask = ps - 1;
1383
bb3884d8
JA
1384 if (write_bw_log) {
1385 setup_log(&agg_io_log[DDIR_READ]);
1386 setup_log(&agg_io_log[DDIR_WRITE]);
1387 }
1388
cdd18ad8 1389 startup_mutex = fio_mutex_init(0);
07739b57 1390
a2f77c9f
JA
1391 set_genesis_time();
1392
ccb0fa24 1393 status_timer_arm();
ebac4655
JA
1394
1395 run_threads();
6ce15a32 1396
bb3884d8 1397 if (!fio_abort) {
6ce15a32 1398 show_run_stats();
bb3884d8 1399 if (write_bw_log) {
5ec10eaa
JA
1400 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1401 __finish_log(agg_io_log[DDIR_WRITE],
1402 "agg-write_bw.log");
bb3884d8
JA
1403 }
1404 }
ebac4655 1405
cdd18ad8 1406 fio_mutex_remove(startup_mutex);
437c9b71 1407 return exit_value;
ebac4655 1408}