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