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