Extend ipo file action capabilities
[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:
ebac4655 1111 (*nr_running)--;
2dc1bbeb
JA
1112 (*m_rate) -= td->o.ratemin;
1113 (*t_rate) -= td->o.rate;
e48676ba 1114 pending--;
a2f77c9f
JA
1115
1116 if (td->error)
1117 exit_value++;
ebac4655 1118 }
b990b5c0 1119
1f809d15 1120 if (*nr_running == cputhreads && !pending && realthreads)
390c40e2 1121 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1122}
1123
906c8d75
JA
1124/*
1125 * Main function for kicking off and reaping jobs, as needed.
1126 */
ebac4655
JA
1127static void run_threads(void)
1128{
ebac4655
JA
1129 struct thread_data *td;
1130 unsigned long spent;
1131 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1132
2f9ade3c
JA
1133 if (fio_pin_memory())
1134 return;
ebac4655 1135
c6ae0a5b 1136 if (!terse_output) {
9cedf167
JA
1137 printf("Starting ");
1138 if (nr_thread)
5ec10eaa
JA
1139 printf("%d thread%s", nr_thread,
1140 nr_thread > 1 ? "s" : "");
9cedf167
JA
1141 if (nr_process) {
1142 if (nr_thread)
1143 printf(" and ");
5ec10eaa
JA
1144 printf("%d process%s", nr_process,
1145 nr_process > 1 ? "es" : "");
9cedf167
JA
1146 }
1147 printf("\n");
c6ae0a5b
JA
1148 fflush(stdout);
1149 }
c04f7ec3 1150
4efa970e
JA
1151 signal(SIGINT, sig_handler);
1152 signal(SIGALRM, sig_handler);
1153
ebac4655
JA
1154 todo = thread_number;
1155 nr_running = 0;
1156 nr_started = 0;
1157 m_rate = t_rate = 0;
1158
34572e28 1159 for_each_td(td, i) {
263e529f 1160 print_status_init(td->thread_number - 1);
ebac4655 1161
2dc1bbeb 1162 if (!td->o.create_serialize) {
380cf265 1163 init_disk_util(td);
ebac4655 1164 continue;
380cf265 1165 }
ebac4655
JA
1166
1167 /*
1168 * do file setup here so it happens sequentially,
1169 * we don't want X number of threads getting their
1170 * client data interspersed on disk
1171 */
53cdc686 1172 if (setup_files(td)) {
5bf13a5a
JA
1173 exit_value++;
1174 if (td->error)
5ec10eaa
JA
1175 log_err("fio: pid=%d, err=%d/%s\n", td->pid,
1176 td->error, td->verror);
ebac4655
JA
1177 td_set_runstate(td, TD_REAPED);
1178 todo--;
c69e8875
JA
1179 } else {
1180 struct fio_file *f;
1181 unsigned int i;
1182
1183 /*
1184 * for sharing to work, each job must always open
1185 * its own files. so close them, if we opened them
1186 * for creation
1187 */
1188 for_each_file(td, f, i)
1189 td_io_close_file(td, f);
5ec10eaa 1190 }
380cf265
JA
1191
1192 init_disk_util(td);
ebac4655
JA
1193 }
1194
a2f77c9f
JA
1195 set_genesis_time();
1196
ebac4655 1197 while (todo) {
75154845
JA
1198 struct thread_data *map[MAX_JOBS];
1199 struct timeval this_start;
1200 int this_jobs = 0, left;
1201
ebac4655
JA
1202 /*
1203 * create threads (TD_NOT_CREATED -> TD_CREATED)
1204 */
34572e28 1205 for_each_td(td, i) {
ebac4655
JA
1206 if (td->runstate != TD_NOT_CREATED)
1207 continue;
1208
1209 /*
1210 * never got a chance to start, killed by other
1211 * thread for some reason
1212 */
1213 if (td->terminate) {
1214 todo--;
1215 continue;
1216 }
1217
2dc1bbeb 1218 if (td->o.start_delay) {
263e529f 1219 spent = mtime_since_genesis();
ebac4655 1220
2dc1bbeb 1221 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1222 continue;
1223 }
1224
2dc1bbeb 1225 if (td->o.stonewall && (nr_started || nr_running))
ebac4655
JA
1226 break;
1227
75154845
JA
1228 /*
1229 * Set state to created. Thread will transition
1230 * to TD_INITIALIZED when it's done setting up.
1231 */
ebac4655 1232 td_set_runstate(td, TD_CREATED);
75154845 1233 map[this_jobs++] = td;
ebac4655
JA
1234 nr_started++;
1235
2dc1bbeb 1236 if (td->o.use_thread) {
ee56ad50 1237 dprint(FD_PROCESS, "will pthread_create\n");
5ec10eaa
JA
1238 if (pthread_create(&td->thread, NULL,
1239 thread_main, td)) {
f4705a73 1240 perror("pthread_create");
ebac4655 1241 nr_started--;
c8bb6faf 1242 break;
ebac4655 1243 }
f4705a73
JA
1244 if (pthread_detach(td->thread) < 0)
1245 perror("pthread_detach");
ebac4655 1246 } else {
ee56ad50 1247 dprint(FD_PROCESS, "will fork\n");
07739b57 1248 if (!fork()) {
a6418147
JA
1249 int ret = fork_main(shm_id, i);
1250
1251 exit(ret);
ebac4655
JA
1252 }
1253 }
cdd18ad8 1254 fio_mutex_down(startup_mutex);
ebac4655
JA
1255 }
1256
1257 /*
75154845
JA
1258 * Wait for the started threads to transition to
1259 * TD_INITIALIZED.
ebac4655 1260 */
02bcaa8c 1261 fio_gettime(&this_start, NULL);
75154845 1262 left = this_jobs;
6ce15a32 1263 while (left && !fio_abort) {
75154845
JA
1264 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1265 break;
1266
1267 usleep(100000);
1268
1269 for (i = 0; i < this_jobs; i++) {
1270 td = map[i];
1271 if (!td)
1272 continue;
b6f4d880 1273 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1274 map[i] = NULL;
1275 left--;
b6f4d880
JA
1276 } else if (td->runstate >= TD_EXITED) {
1277 map[i] = NULL;
1278 left--;
1279 todo--;
1280 nr_running++; /* work-around... */
75154845
JA
1281 }
1282 }
1283 }
1284
1285 if (left) {
3b70d7e5 1286 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1287 for (i = 0; i < this_jobs; i++) {
1288 td = map[i];
1289 if (!td)
1290 continue;
1291 kill(td->pid, SIGTERM);
1292 }
1293 break;
1294 }
1295
1296 /*
b6f4d880 1297 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1298 */
34572e28 1299 for_each_td(td, i) {
75154845 1300 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1301 continue;
1302
1303 td_set_runstate(td, TD_RUNNING);
1304 nr_running++;
1305 nr_started--;
2dc1bbeb
JA
1306 m_rate += td->o.ratemin;
1307 t_rate += td->o.rate;
75154845 1308 todo--;
cdd18ad8 1309 fio_mutex_up(td->mutex);
ebac4655
JA
1310 }
1311
1312 reap_threads(&nr_running, &t_rate, &m_rate);
1313
1314 if (todo)
1315 usleep(100000);
1316 }
1317
1318 while (nr_running) {
1319 reap_threads(&nr_running, &t_rate, &m_rate);
1320 usleep(10000);
1321 }
1322
1323 update_io_ticks();
2f9ade3c 1324 fio_unpin_memory();
ebac4655
JA
1325}
1326
ebac4655
JA
1327int main(int argc, char *argv[])
1328{
29d610e1
JA
1329 long ps;
1330
2e5cdb11
JA
1331 sinit();
1332
dbe1125e
JA
1333 /*
1334 * We need locale for number printing, if it isn't set then just
1335 * go with the US format.
1336 */
1337 if (!getenv("LC_NUMERIC"))
1338 setlocale(LC_NUMERIC, "en_US");
1339
ebac4655
JA
1340 if (parse_options(argc, argv))
1341 return 1;
1342
4b472fa3
JA
1343 if (!thread_number)
1344 return 0;
ebac4655 1345
29d610e1
JA
1346 ps = sysconf(_SC_PAGESIZE);
1347 if (ps < 0) {
1348 log_err("Failed to get page size\n");
1349 return 1;
1350 }
1351
cfc99db7 1352 page_size = ps;
29d610e1
JA
1353 page_mask = ps - 1;
1354
bb3884d8
JA
1355 if (write_bw_log) {
1356 setup_log(&agg_io_log[DDIR_READ]);
1357 setup_log(&agg_io_log[DDIR_WRITE]);
1358 }
1359
cdd18ad8 1360 startup_mutex = fio_mutex_init(0);
07739b57 1361
a2f77c9f
JA
1362 set_genesis_time();
1363
ebac4655
JA
1364 disk_util_timer_arm();
1365
1366 run_threads();
6ce15a32 1367
bb3884d8 1368 if (!fio_abort) {
6ce15a32 1369 show_run_stats();
bb3884d8 1370 if (write_bw_log) {
5ec10eaa
JA
1371 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1372 __finish_log(agg_io_log[DDIR_WRITE],
1373 "agg-write_bw.log");
bb3884d8
JA
1374 }
1375 }
ebac4655 1376
cdd18ad8 1377 fio_mutex_remove(startup_mutex);
437c9b71 1378 return exit_value;
ebac4655 1379}