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