Fix off-by-one in last_block()
[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
6b9cea23
JA
645/*
646 * "randomly" fill the buffer contents
647 */
e9459e5a 648static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
6b9cea23 649{
210f43b3 650 long *ptr = io_u->buf;
6b9cea23 651
e9459e5a
JA
652 if (!td->o.zero_buffers) {
653 while ((void *) ptr - io_u->buf < max_bs) {
210f43b3 654 *ptr = rand() * GOLDEN_RATIO_PRIME;
e9459e5a
JA
655 ptr++;
656 }
657 } else
658 memset(ptr, 0, max_bs);
6b9cea23
JA
659}
660
ebac4655
JA
661static int init_io_u(struct thread_data *td)
662{
663 struct io_u *io_u;
a00735e6 664 unsigned int max_bs;
ebac4655
JA
665 int i, max_units;
666 char *p;
667
1e3c09af 668 max_units = td->o.iodepth;
2dc1bbeb 669 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
5ec10eaa
JA
670 td->orig_buffer_size = (unsigned long long) max_bs
671 * (unsigned long long) max_units;
672
673 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
674 unsigned long bs;
74b025b0 675
5ec10eaa
JA
676 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
677 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
678 }
ebac4655 679
c3990645
JA
680 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
681 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
682 return 1;
683 }
684
2f9ade3c
JA
685 if (allocate_io_mem(td))
686 return 1;
ebac4655 687
b3f378e9
JA
688 if (td->o.odirect)
689 p = ALIGN(td->orig_buffer);
690 else
691 p = td->orig_buffer;
692
ebac4655 693 for (i = 0; i < max_units; i++) {
db1cd90b
JA
694 if (td->terminate)
695 return 1;
ebac4655
JA
696 io_u = malloc(sizeof(*io_u));
697 memset(io_u, 0, sizeof(*io_u));
698 INIT_LIST_HEAD(&io_u->list);
699
b4c5e1ac
JA
700 if (!(td->io_ops->flags & FIO_NOIO)) {
701 io_u->buf = p + max_bs * i;
e9459e5a 702
b4c5e1ac
JA
703 if (td_write(td))
704 fill_io_buf(td, io_u, max_bs);
705 }
6b9cea23 706
b1ff3403 707 io_u->index = i;
0c6e7517 708 io_u->flags = IO_U_F_FREE;
ebac4655
JA
709 list_add(&io_u->list, &td->io_u_freelist);
710 }
711
433afcb4
JA
712 io_u_init_timeout();
713
ebac4655
JA
714 return 0;
715}
716
da86774e
JA
717static int switch_ioscheduler(struct thread_data *td)
718{
719 char tmp[256], tmp2[128];
720 FILE *f;
721 int ret;
722
ba0fbe10 723 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
724 return 0;
725
da86774e
JA
726 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
727
728 f = fopen(tmp, "r+");
729 if (!f) {
cbf5c1d7 730 if (errno == ENOENT) {
5ec10eaa
JA
731 log_err("fio: os or kernel doesn't support IO scheduler"
732 " switching\n");
cbf5c1d7
JA
733 return 0;
734 }
735 td_verror(td, errno, "fopen iosched");
da86774e
JA
736 return 1;
737 }
738
739 /*
740 * Set io scheduler.
741 */
2dc1bbeb 742 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
da86774e 743 if (ferror(f) || ret != 1) {
e1161c32 744 td_verror(td, errno, "fwrite");
da86774e
JA
745 fclose(f);
746 return 1;
747 }
748
749 rewind(f);
750
751 /*
752 * Read back and check that the selected scheduler is now the default.
753 */
754 ret = fread(tmp, 1, sizeof(tmp), f);
755 if (ferror(f) || ret < 0) {
e1161c32 756 td_verror(td, errno, "fread");
da86774e
JA
757 fclose(f);
758 return 1;
759 }
760
2dc1bbeb 761 sprintf(tmp2, "[%s]", td->o.ioscheduler);
da86774e 762 if (!strstr(tmp, tmp2)) {
2dc1bbeb 763 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
e1161c32 764 td_verror(td, EINVAL, "iosched_switch");
da86774e
JA
765 fclose(f);
766 return 1;
767 }
768
769 fclose(f);
770 return 0;
771}
772
492158cf
JA
773static int keep_running(struct thread_data *td)
774{
775 unsigned long long io_done;
776
20e354ef
JA
777 if (td->done)
778 return 0;
492158cf
JA
779 if (td->o.time_based)
780 return 1;
781 if (td->o.loops) {
782 td->o.loops--;
783 return 1;
784 }
785
5ec10eaa
JA
786 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
787 + td->io_skip_bytes;
492158cf
JA
788 if (io_done < td->o.size)
789 return 1;
790
791 return 0;
792}
793
a978ba68 794static int clear_io_state(struct thread_data *td)
ebac4655 795{
53cdc686 796 struct fio_file *f;
af52b345
JA
797 unsigned int i;
798 int ret;
ebac4655 799
756867bd 800 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
ebac4655 801 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 802 td->zone_bytes = 0;
d7d3b49b 803 td->rate_bytes = 0;
4e991c23 804 td->rate_blocks = 0;
38d77cae 805 td->rw_end_set[0] = td->rw_end_set[1] = 0;
ebac4655 806
c1324df1
JA
807 td->last_was_sync = 0;
808
2ba1c290
JA
809 /*
810 * reset file done count if we are to start over
811 */
812 if (td->o.time_based || td->o.loops)
48f5abd3 813 td->nr_done_files = 0;
281f9b63 814
24ffd2c2 815 close_files(td);
53cdc686 816
a978ba68
JA
817 ret = 0;
818 for_each_file(td, f, i) {
281f9b63 819 f->flags &= ~FIO_FILE_DONE;
a978ba68
JA
820 ret = td_io_open_file(td, f);
821 if (ret)
822 break;
53cdc686 823 }
a978ba68
JA
824
825 return ret;
ebac4655
JA
826}
827
906c8d75
JA
828/*
829 * Entry point for the thread based jobs. The process based jobs end up
830 * here as well, after a little setup.
831 */
ebac4655
JA
832static void *thread_main(void *data)
833{
10927316 834 unsigned long long runtime[2], elapsed;
ebac4655 835 struct thread_data *td = data;
a978ba68 836 int clear_state;
ebac4655 837
2dc1bbeb 838 if (!td->o.use_thread)
ebac4655
JA
839 setsid();
840
841 td->pid = getpid();
842
ee56ad50
JA
843 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
844
aea47d44
JA
845 INIT_LIST_HEAD(&td->io_u_freelist);
846 INIT_LIST_HEAD(&td->io_u_busylist);
755200a3 847 INIT_LIST_HEAD(&td->io_u_requeues);
aea47d44 848 INIT_LIST_HEAD(&td->io_log_list);
8de8f047 849 INIT_LIST_HEAD(&td->io_hist_list);
bb5d7d0b 850 td->io_hist_tree = RB_ROOT;
aea47d44 851
db1cd90b 852 td_set_runstate(td, TD_INITIALIZED);
cdd18ad8
JA
853 fio_mutex_up(startup_mutex);
854 fio_mutex_down(td->mutex);
db1cd90b
JA
855
856 /*
cdd18ad8 857 * the ->mutex mutex is now no longer used, close it to avoid
db1cd90b
JA
858 * eating a file descriptor
859 */
cdd18ad8 860 fio_mutex_remove(td->mutex);
db1cd90b 861
d84f8d49
JA
862 /*
863 * May alter parameters that init_io_u() will use, so we need to
864 * do this first.
865 */
866 if (init_iolog(td))
867 goto err;
868
ebac4655 869 if (init_io_u(td))
db1cd90b 870 goto err;
ebac4655 871
375b2695 872 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
e1161c32 873 td_verror(td, errno, "cpu_set_affinity");
db1cd90b 874 goto err;
ebac4655
JA
875 }
876
ac684785 877 if (td->ioprio_set) {
ebac4655 878 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
e1161c32 879 td_verror(td, errno, "ioprio_set");
db1cd90b 880 goto err;
ebac4655
JA
881 }
882 }
883
2dc1bbeb 884 if (nice(td->o.nice) == -1) {
e1161c32 885 td_verror(td, errno, "nice");
db1cd90b 886 goto err;
b6f4d880
JA
887 }
888
2dc1bbeb 889 if (td->o.ioscheduler && switch_ioscheduler(td))
db1cd90b 890 goto err;
37f56873 891
2dc1bbeb 892 if (!td->o.create_serialize && setup_files(td))
ebac4655
JA
893 goto err;
894
7d6c5283
JA
895 if (td_io_init(td))
896 goto err;
897
b5af8293
JA
898 if (open_files(td))
899 goto err;
900
68727076
JA
901 if (init_random_map(td))
902 goto err;
903
2dc1bbeb
JA
904 if (td->o.exec_prerun) {
905 if (system(td->o.exec_prerun) < 0)
69cfd7e0
JA
906 goto err;
907 }
4e0ba8af 908
69008999 909 fio_gettime(&td->epoch, NULL);
433afcb4 910 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
756867bd 911 getrusage(RUSAGE_SELF, &td->ts.ru_start);
69008999
JA
912
913 runtime[0] = runtime[1] = 0;
a978ba68 914 clear_state = 0;
492158cf 915 while (keep_running(td)) {
02bcaa8c 916 fio_gettime(&td->start, NULL);
756867bd 917 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
ebac4655 918
2dc1bbeb 919 if (td->o.ratemin)
5ec10eaa
JA
920 memcpy(&td->lastrate, &td->ts.stat_sample_time,
921 sizeof(td->lastrate));
ebac4655 922
a978ba68
JA
923 if (clear_state && clear_io_state(td))
924 break;
925
ebac4655
JA
926 prune_io_piece_log(td);
927
ba0fbe10 928 do_io(td);
ebac4655 929
a978ba68
JA
930 clear_state = 1;
931
38d77cae
JA
932 if (td_read(td) && td->io_bytes[DDIR_READ]) {
933 if (td->rw_end_set[DDIR_READ])
5ec10eaa
JA
934 elapsed = utime_since(&td->start,
935 &td->rw_end[DDIR_READ]);
38d77cae
JA
936 else
937 elapsed = utime_since_now(&td->start);
938
939 runtime[DDIR_READ] += elapsed;
940 }
941 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
942 if (td->rw_end_set[DDIR_WRITE])
5ec10eaa
JA
943 elapsed = utime_since(&td->start,
944 &td->rw_end[DDIR_WRITE]);
38d77cae
JA
945 else
946 elapsed = utime_since_now(&td->start);
947
948 runtime[DDIR_WRITE] += elapsed;
949 }
5ec10eaa 950
ebac4655
JA
951 if (td->error || td->terminate)
952 break;
953
e84c73a8
SL
954 if (!td->o.do_verify ||
955 td->o.verify == VERIFY_NONE ||
b67740d3 956 (td->io_ops->flags & FIO_UNIDIR))
ebac4655
JA
957 continue;
958
a978ba68
JA
959 if (clear_io_state(td))
960 break;
961
02bcaa8c 962 fio_gettime(&td->start, NULL);
ebac4655
JA
963
964 do_verify(td);
965
69008999 966 runtime[DDIR_READ] += utime_since_now(&td->start);
ebac4655
JA
967
968 if (td->error || td->terminate)
969 break;
970 }
971
36dff966 972 update_rusage_stat(td);
47f0cc48
YHJT
973 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
974 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
756867bd
JA
975 td->ts.total_run_time = mtime_since_now(&td->epoch);
976 td->ts.io_bytes[0] = td->io_bytes[0];
977 td->ts.io_bytes[1] = td->io_bytes[1];
978
979 if (td->ts.bw_log)
980 finish_log(td, td->ts.bw_log, "bw");
981 if (td->ts.slat_log)
982 finish_log(td, td->ts.slat_log, "slat");
983 if (td->ts.clat_log)
984 finish_log(td, td->ts.clat_log, "clat");
2dc1bbeb
JA
985 if (td->o.exec_postrun) {
986 if (system(td->o.exec_postrun) < 0)
987 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
69cfd7e0 988 }
ebac4655
JA
989
990 if (exitall_on_terminate)
390c40e2 991 terminate_threads(td->groupid);
ebac4655
JA
992
993err:
5bf13a5a 994 if (td->error)
5ec10eaa
JA
995 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error,
996 td->verror);
24ffd2c2 997 close_and_free_files(td);
2866c82d 998 close_ioengine(td);
ebac4655 999 cleanup_io_u(td);
f29b25a3
JA
1000
1001 /*
1002 * do this very late, it will log file closing as well
1003 */
1004 if (td->o.write_iolog_file)
1005 write_iolog_close(td);
1006
d23bb327 1007 options_mem_free(td);
ebac4655 1008 td_set_runstate(td, TD_EXITED);
43d76807 1009 return (void *) (unsigned long) td->error;
ebac4655
JA
1010}
1011
906c8d75
JA
1012/*
1013 * We cannot pass the td data into a forked process, so attach the td and
1014 * pass it to the thread worker.
1015 */
a6418147 1016static int fork_main(int shmid, int offset)
ebac4655
JA
1017{
1018 struct thread_data *td;
a6418147 1019 void *data, *ret;
ebac4655
JA
1020
1021 data = shmat(shmid, NULL, 0);
1022 if (data == (void *) -1) {
a6418147
JA
1023 int __err = errno;
1024
ebac4655 1025 perror("shmat");
a6418147 1026 return __err;
ebac4655
JA
1027 }
1028
1029 td = data + offset * sizeof(struct thread_data);
a6418147 1030 ret = thread_main(td);
ebac4655 1031 shmdt(data);
43d76807 1032 return (int) (unsigned long) ret;
ebac4655
JA
1033}
1034
906c8d75
JA
1035/*
1036 * Run over the job map and reap the threads that have exited, if any.
1037 */
ebac4655
JA
1038static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1039{
34572e28 1040 struct thread_data *td;
1f809d15 1041 int i, cputhreads, realthreads, pending, status, ret;
ebac4655
JA
1042
1043 /*
1044 * reap exited threads (TD_EXITED -> TD_REAPED)
1045 */
1f809d15 1046 realthreads = pending = cputhreads = 0;
34572e28 1047 for_each_td(td, i) {
3707f45b 1048 int flags = 0;
a2f77c9f 1049
84585003
JA
1050 /*
1051 * ->io_ops is NULL for a thread that has closed its
1052 * io engine
1053 */
ba0fbe10 1054 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
b990b5c0 1055 cputhreads++;
1f809d15
JA
1056 else
1057 realthreads++;
b990b5c0 1058
a1dedc1f
AC
1059 if (!td->pid) {
1060 pending++;
1061 continue;
1062 }
1063 if (td->runstate == TD_REAPED)
a2f77c9f 1064 continue;
2dc1bbeb 1065 if (td->o.use_thread) {
3707f45b
JA
1066 if (td->runstate == TD_EXITED) {
1067 td_set_runstate(td, TD_REAPED);
1068 goto reaped;
1069 }
1070 continue;
1071 }
a2f77c9f
JA
1072
1073 flags = WNOHANG;
1074 if (td->runstate == TD_EXITED)
1075 flags = 0;
1076
1077 /*
1078 * check if someone quit or got killed in an unusual way
1079 */
1080 ret = waitpid(td->pid, &status, flags);
3707f45b 1081 if (ret < 0) {
a2f77c9f 1082 if (errno == ECHILD) {
5ec10eaa
JA
1083 log_err("fio: pid=%d disappeared %d\n", td->pid,
1084 td->runstate);
a2f77c9f
JA
1085 td_set_runstate(td, TD_REAPED);
1086 goto reaped;
1087 }
1088 perror("waitpid");
1089 } else if (ret == td->pid) {
1090 if (WIFSIGNALED(status)) {
fab6aa71
JA
1091 int sig = WTERMSIG(status);
1092
d9244941 1093 if (sig != SIGQUIT)
5ec10eaa
JA
1094 log_err("fio: pid=%d, got signal=%d\n",
1095 td->pid, sig);
fab6aa71
JA
1096 td_set_runstate(td, TD_REAPED);
1097 goto reaped;
1098 }
a2f77c9f
JA
1099 if (WIFEXITED(status)) {
1100 if (WEXITSTATUS(status) && !td->error)
1101 td->error = WEXITSTATUS(status);
a2f77c9f 1102
a2f77c9f
JA
1103 td_set_runstate(td, TD_REAPED);
1104 goto reaped;
a6418147
JA
1105 }
1106 }
ebac4655 1107
a2f77c9f
JA
1108 /*
1109 * thread is not dead, continue
1110 */
e48676ba 1111 pending++;
a2f77c9f 1112 continue;
fab6aa71 1113reaped:
ebac4655 1114 (*nr_running)--;
2dc1bbeb
JA
1115 (*m_rate) -= td->o.ratemin;
1116 (*t_rate) -= td->o.rate;
6f88bcb0
JA
1117 if (!td->pid)
1118 pending--;
a2f77c9f
JA
1119
1120 if (td->error)
1121 exit_value++;
d3eeeabc
JA
1122
1123 done_secs += mtime_since_now(&td->epoch) / 1000;
ebac4655 1124 }
b990b5c0 1125
1f809d15 1126 if (*nr_running == cputhreads && !pending && realthreads)
390c40e2 1127 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1128}
1129
906c8d75
JA
1130/*
1131 * Main function for kicking off and reaping jobs, as needed.
1132 */
ebac4655
JA
1133static void run_threads(void)
1134{
ebac4655
JA
1135 struct thread_data *td;
1136 unsigned long spent;
1137 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1138
2f9ade3c
JA
1139 if (fio_pin_memory())
1140 return;
ebac4655 1141
c6ae0a5b 1142 if (!terse_output) {
9cedf167
JA
1143 printf("Starting ");
1144 if (nr_thread)
5ec10eaa
JA
1145 printf("%d thread%s", nr_thread,
1146 nr_thread > 1 ? "s" : "");
9cedf167
JA
1147 if (nr_process) {
1148 if (nr_thread)
1149 printf(" and ");
5ec10eaa
JA
1150 printf("%d process%s", nr_process,
1151 nr_process > 1 ? "es" : "");
9cedf167
JA
1152 }
1153 printf("\n");
c6ae0a5b
JA
1154 fflush(stdout);
1155 }
c04f7ec3 1156
4efa970e
JA
1157 signal(SIGINT, sig_handler);
1158 signal(SIGALRM, sig_handler);
1159
ebac4655
JA
1160 todo = thread_number;
1161 nr_running = 0;
1162 nr_started = 0;
1163 m_rate = t_rate = 0;
1164
34572e28 1165 for_each_td(td, i) {
263e529f 1166 print_status_init(td->thread_number - 1);
ebac4655 1167
2dc1bbeb 1168 if (!td->o.create_serialize) {
380cf265 1169 init_disk_util(td);
ebac4655 1170 continue;
380cf265 1171 }
ebac4655
JA
1172
1173 /*
1174 * do file setup here so it happens sequentially,
1175 * we don't want X number of threads getting their
1176 * client data interspersed on disk
1177 */
53cdc686 1178 if (setup_files(td)) {
5bf13a5a
JA
1179 exit_value++;
1180 if (td->error)
5ec10eaa
JA
1181 log_err("fio: pid=%d, err=%d/%s\n", td->pid,
1182 td->error, td->verror);
ebac4655
JA
1183 td_set_runstate(td, TD_REAPED);
1184 todo--;
c69e8875
JA
1185 } else {
1186 struct fio_file *f;
1187 unsigned int i;
1188
1189 /*
1190 * for sharing to work, each job must always open
1191 * its own files. so close them, if we opened them
1192 * for creation
1193 */
1194 for_each_file(td, f, i)
1195 td_io_close_file(td, f);
5ec10eaa 1196 }
380cf265
JA
1197
1198 init_disk_util(td);
ebac4655
JA
1199 }
1200
a2f77c9f
JA
1201 set_genesis_time();
1202
ebac4655 1203 while (todo) {
75154845
JA
1204 struct thread_data *map[MAX_JOBS];
1205 struct timeval this_start;
1206 int this_jobs = 0, left;
1207
ebac4655
JA
1208 /*
1209 * create threads (TD_NOT_CREATED -> TD_CREATED)
1210 */
34572e28 1211 for_each_td(td, i) {
ebac4655
JA
1212 if (td->runstate != TD_NOT_CREATED)
1213 continue;
1214
1215 /*
1216 * never got a chance to start, killed by other
1217 * thread for some reason
1218 */
1219 if (td->terminate) {
1220 todo--;
1221 continue;
1222 }
1223
2dc1bbeb 1224 if (td->o.start_delay) {
263e529f 1225 spent = mtime_since_genesis();
ebac4655 1226
2dc1bbeb 1227 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1228 continue;
1229 }
1230
6f88bcb0
JA
1231 if (td->o.stonewall && (nr_started || nr_running)) {
1232 dprint(FD_PROCESS, "%s: stonewall wait\n",
1233 td->o.name);
ebac4655 1234 break;
6f88bcb0 1235 }
ebac4655 1236
75154845
JA
1237 /*
1238 * Set state to created. Thread will transition
1239 * to TD_INITIALIZED when it's done setting up.
1240 */
ebac4655 1241 td_set_runstate(td, TD_CREATED);
75154845 1242 map[this_jobs++] = td;
ebac4655
JA
1243 nr_started++;
1244
2dc1bbeb 1245 if (td->o.use_thread) {
ee56ad50 1246 dprint(FD_PROCESS, "will pthread_create\n");
5ec10eaa
JA
1247 if (pthread_create(&td->thread, NULL,
1248 thread_main, td)) {
f4705a73 1249 perror("pthread_create");
ebac4655 1250 nr_started--;
c8bb6faf 1251 break;
ebac4655 1252 }
f4705a73
JA
1253 if (pthread_detach(td->thread) < 0)
1254 perror("pthread_detach");
ebac4655 1255 } else {
ee56ad50 1256 dprint(FD_PROCESS, "will fork\n");
07739b57 1257 if (!fork()) {
a6418147
JA
1258 int ret = fork_main(shm_id, i);
1259
1260 exit(ret);
ebac4655
JA
1261 }
1262 }
cdd18ad8 1263 fio_mutex_down(startup_mutex);
ebac4655
JA
1264 }
1265
1266 /*
75154845
JA
1267 * Wait for the started threads to transition to
1268 * TD_INITIALIZED.
ebac4655 1269 */
02bcaa8c 1270 fio_gettime(&this_start, NULL);
75154845 1271 left = this_jobs;
6ce15a32 1272 while (left && !fio_abort) {
75154845
JA
1273 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1274 break;
1275
1276 usleep(100000);
1277
1278 for (i = 0; i < this_jobs; i++) {
1279 td = map[i];
1280 if (!td)
1281 continue;
b6f4d880 1282 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1283 map[i] = NULL;
1284 left--;
b6f4d880
JA
1285 } else if (td->runstate >= TD_EXITED) {
1286 map[i] = NULL;
1287 left--;
1288 todo--;
1289 nr_running++; /* work-around... */
75154845
JA
1290 }
1291 }
1292 }
1293
1294 if (left) {
3b70d7e5 1295 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1296 for (i = 0; i < this_jobs; i++) {
1297 td = map[i];
1298 if (!td)
1299 continue;
1300 kill(td->pid, SIGTERM);
1301 }
1302 break;
1303 }
1304
1305 /*
b6f4d880 1306 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1307 */
34572e28 1308 for_each_td(td, i) {
75154845 1309 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1310 continue;
1311
1312 td_set_runstate(td, TD_RUNNING);
1313 nr_running++;
1314 nr_started--;
2dc1bbeb
JA
1315 m_rate += td->o.ratemin;
1316 t_rate += td->o.rate;
75154845 1317 todo--;
cdd18ad8 1318 fio_mutex_up(td->mutex);
ebac4655
JA
1319 }
1320
1321 reap_threads(&nr_running, &t_rate, &m_rate);
1322
1323 if (todo)
1324 usleep(100000);
1325 }
1326
1327 while (nr_running) {
1328 reap_threads(&nr_running, &t_rate, &m_rate);
1329 usleep(10000);
1330 }
1331
1332 update_io_ticks();
2f9ade3c 1333 fio_unpin_memory();
ebac4655
JA
1334}
1335
ebac4655
JA
1336int main(int argc, char *argv[])
1337{
29d610e1
JA
1338 long ps;
1339
2e5cdb11
JA
1340 sinit();
1341
dbe1125e
JA
1342 /*
1343 * We need locale for number printing, if it isn't set then just
1344 * go with the US format.
1345 */
1346 if (!getenv("LC_NUMERIC"))
1347 setlocale(LC_NUMERIC, "en_US");
1348
ebac4655
JA
1349 if (parse_options(argc, argv))
1350 return 1;
1351
4b472fa3
JA
1352 if (!thread_number)
1353 return 0;
ebac4655 1354
29d610e1
JA
1355 ps = sysconf(_SC_PAGESIZE);
1356 if (ps < 0) {
1357 log_err("Failed to get page size\n");
1358 return 1;
1359 }
1360
cfc99db7 1361 page_size = ps;
29d610e1
JA
1362 page_mask = ps - 1;
1363
bb3884d8
JA
1364 if (write_bw_log) {
1365 setup_log(&agg_io_log[DDIR_READ]);
1366 setup_log(&agg_io_log[DDIR_WRITE]);
1367 }
1368
cdd18ad8 1369 startup_mutex = fio_mutex_init(0);
07739b57 1370
a2f77c9f
JA
1371 set_genesis_time();
1372
ebac4655
JA
1373 disk_util_timer_arm();
1374
1375 run_threads();
6ce15a32 1376
bb3884d8 1377 if (!fio_abort) {
6ce15a32 1378 show_run_stats();
bb3884d8 1379 if (write_bw_log) {
5ec10eaa
JA
1380 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1381 __finish_log(agg_io_log[DDIR_WRITE],
1382 "agg-write_bw.log");
bb3884d8
JA
1383 }
1384 }
ebac4655 1385
cdd18ad8 1386 fio_mutex_remove(startup_mutex);
437c9b71 1387 return exit_value;
ebac4655 1388}