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