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