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