[PATCH] Fix a bunch of bugs
[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
JA
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
ebac4655
JA
22#include <unistd.h>
23#include <fcntl.h>
24#include <string.h>
ebac4655
JA
25#include <signal.h>
26#include <time.h>
ebac4655 27#include <assert.h>
ebac4655
JA
28#include <sys/stat.h>
29#include <sys/wait.h>
30#include <sys/ipc.h>
31#include <sys/shm.h>
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34
35#include "fio.h"
36#include "os.h"
37
38#define MASK (4095)
39
40#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
41
42int groupid = 0;
43int thread_number = 0;
ebac4655 44int shm_id = 0;
53cdc686 45int temp_stall_ts;
c1d5725e 46char *fio_inst_prefix = _INST_PREFIX;
ebac4655 47
c04f7ec3
JA
48extern unsigned long long mlock_size;
49
3d60d1ed 50#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655 51
bbfd6b00 52static volatile int startup_sem;
ebac4655
JA
53
54#define TERMINATE_ALL (-1)
75154845 55#define JOB_START_TIMEOUT (5 * 1000)
ebac4655
JA
56
57static void terminate_threads(int group_id)
58{
59 int i;
60
61 for (i = 0; i < thread_number; i++) {
62 struct thread_data *td = &threads[i];
63
64 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
65 td->terminate = 1;
66 td->start_delay = 0;
67 }
68 }
69}
70
71static void sig_handler(int sig)
72{
73 switch (sig) {
74 case SIGALRM:
75 update_io_ticks();
76 disk_util_timer_arm();
77 print_thread_status();
78 break;
79 default:
80 printf("\nfio: terminating on signal\n");
81 fflush(stdout);
82 terminate_threads(TERMINATE_ALL);
83 break;
84 }
85}
86
906c8d75
JA
87/*
88 * The ->file_map[] contains a map of blocks we have or have not done io
89 * to yet. Used to make sure we cover the entire range in a fair fashion.
90 */
53cdc686
JA
91static int random_map_free(struct thread_data *td, struct fio_file *f,
92 unsigned long long block)
ebac4655 93{
53cdc686
JA
94 unsigned int idx = RAND_MAP_IDX(td, f, block);
95 unsigned int bit = RAND_MAP_BIT(td, f, block);
ebac4655 96
53cdc686 97 return (f->file_map[idx] & (1UL << bit)) == 0;
ebac4655
JA
98}
99
906c8d75
JA
100/*
101 * Return the next free block in the map.
102 */
53cdc686
JA
103static int get_next_free_block(struct thread_data *td, struct fio_file *f,
104 unsigned long long *b)
ebac4655
JA
105{
106 int i;
107
108 *b = 0;
109 i = 0;
53cdc686
JA
110 while ((*b) * td->min_bs < f->file_size) {
111 if (f->file_map[i] != -1UL) {
112 *b += ffz(f->file_map[i]);
ebac4655
JA
113 return 0;
114 }
115
116 *b += BLOCKS_PER_MAP;
117 i++;
118 }
119
120 return 1;
121}
122
906c8d75
JA
123/*
124 * Mark a given offset as used in the map.
125 */
53cdc686
JA
126static void mark_random_map(struct thread_data *td, struct fio_file *f,
127 struct io_u *io_u)
ebac4655 128{
200bc855 129 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
ebac4655
JA
130 unsigned int blocks = 0;
131
132 while (blocks < (io_u->buflen / td->min_bs)) {
133 unsigned int idx, bit;
134
53cdc686 135 if (!random_map_free(td, f, block))
ebac4655
JA
136 break;
137
53cdc686
JA
138 idx = RAND_MAP_IDX(td, f, block);
139 bit = RAND_MAP_BIT(td, f, block);
ebac4655 140
53cdc686 141 assert(idx < f->num_maps);
ebac4655 142
53cdc686 143 f->file_map[idx] |= (1UL << bit);
ebac4655
JA
144 block++;
145 blocks++;
146 }
147
148 if ((blocks * td->min_bs) < io_u->buflen)
149 io_u->buflen = blocks * td->min_bs;
150}
151
906c8d75
JA
152/*
153 * For random io, generate a random new block and see if it's used. Repeat
154 * until we find a free one. For sequential io, just return the end of
155 * the last io issued.
156 */
53cdc686
JA
157static int get_next_offset(struct thread_data *td, struct fio_file *f,
158 unsigned long long *offset)
20dc95c4
JA
159{
160 unsigned long long b, rb;
161 long r;
162
163 if (!td->sequential) {
085227ad 164 unsigned long long max_blocks = td->io_size / td->min_bs;
20dc95c4
JA
165 int loops = 50;
166
167 do {
6dfd46b9 168 r = os_random_long(&td->random_state);
085227ad 169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
53cdc686 170 rb = b + (f->file_offset / td->min_bs);
20dc95c4 171 loops--;
53cdc686 172 } while (!random_map_free(td, f, rb) && loops);
20dc95c4
JA
173
174 if (!loops) {
53cdc686 175 if (get_next_free_block(td, f, &b))
20dc95c4
JA
176 return 1;
177 }
178 } else
53cdc686 179 b = f->last_pos / td->min_bs;
20dc95c4 180
53cdc686
JA
181 *offset = (b * td->min_bs) + f->file_offset;
182 if (*offset > f->file_size)
20dc95c4
JA
183 return 1;
184
185 return 0;
186}
187
188static unsigned int get_next_buflen(struct thread_data *td)
189{
190 unsigned int buflen;
191 long r;
192
193 if (td->min_bs == td->max_bs)
194 buflen = td->min_bs;
195 else {
6dfd46b9 196 r = os_random_long(&td->bsrange_state);
20dc95c4
JA
197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
199 }
200
b2a15192
JA
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
202 /*
203 * if using direct/raw io, we may not be able to
204 * shrink the size. so just fail it.
205 */
206 if (td->io_ops->flags & FIO_RAWIO)
207 return 0;
208
20dc95c4 209 buflen = td->io_size - td->this_io_bytes[td->ddir];
b2a15192 210 }
20dc95c4
JA
211
212 return buflen;
213}
214
906c8d75
JA
215/*
216 * Check if we are above the minimum rate given.
217 */
ebac4655
JA
218static int check_min_rate(struct thread_data *td, struct timeval *now)
219{
220 unsigned long spent;
221 unsigned long rate;
222 int ddir = td->ddir;
223
224 /*
225 * allow a 2 second settle period in the beginning
226 */
227 if (mtime_since(&td->start, now) < 2000)
228 return 0;
229
230 /*
231 * if rate blocks is set, sample is running
232 */
233 if (td->rate_bytes) {
234 spent = mtime_since(&td->lastrate, now);
235 if (spent < td->ratecycle)
236 return 0;
237
238 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
239 if (rate < td->ratemin) {
eb8bbf48 240 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
ebac4655
JA
241 if (rate_quit)
242 terminate_threads(td->groupid);
243 return 1;
244 }
245 }
246
247 td->rate_bytes = td->this_io_bytes[ddir];
248 memcpy(&td->lastrate, now, sizeof(*now));
249 return 0;
250}
251
252static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
253{
254 if (!td->timeout)
255 return 0;
256 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
257 return 1;
258
259 return 0;
260}
261
906c8d75
JA
262/*
263 * Return the data direction for the next io_u. If the job is a
264 * mixed read/write workload, check the rwmix cycle and switch if
265 * necessary.
266 */
a6ccc7be 267static int get_rw_ddir(struct thread_data *td)
3d60d1ed 268{
a6ccc7be
JA
269 if (td_rw(td)) {
270 struct timeval now;
271 unsigned long elapsed;
3d60d1ed 272
a6ccc7be
JA
273 gettimeofday(&now, NULL);
274 elapsed = mtime_since_now(&td->rwmix_switch);
3d60d1ed 275
a6ccc7be
JA
276 /*
277 * Check if it's time to seed a new data direction.
278 */
279 if (elapsed >= td->rwmixcycle) {
0ab8db89 280 unsigned int v;
a6ccc7be 281 long r;
3d60d1ed 282
c1ee2ca4
JA
283 r = os_random_long(&td->rwmix_state);
284 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
a6ccc7be
JA
285 if (v < td->rwmixread)
286 td->rwmix_ddir = DDIR_READ;
287 else
288 td->rwmix_ddir = DDIR_WRITE;
289 memcpy(&td->rwmix_switch, &now, sizeof(now));
290 }
291 return td->rwmix_ddir;
292 } else if (td_read(td))
3d60d1ed
JA
293 return DDIR_READ;
294 else
295 return DDIR_WRITE;
296}
297
aea47d44 298static int td_io_prep(struct thread_data *td, struct io_u *io_u)
20dc95c4 299{
2866c82d 300 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
20dc95c4
JA
301 return 1;
302
303 return 0;
304}
305
b1ff3403 306void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655 307{
53cdc686 308 io_u->file = NULL;
ebac4655
JA
309 list_del(&io_u->list);
310 list_add(&io_u->list, &td->io_u_freelist);
311 td->cur_depth--;
312}
313
53cdc686
JA
314static int fill_io_u(struct thread_data *td, struct fio_file *f,
315 struct io_u *io_u)
843a7413
JA
316{
317 /*
318 * If using an iolog, grab next piece if any available.
319 */
320 if (td->read_iolog)
321 return read_iolog_get(td, io_u);
322
aea47d44
JA
323 /*
324 * No log, let the seq/rand engine retrieve the next position.
325 */
53cdc686 326 if (!get_next_offset(td, f, &io_u->offset)) {
aea47d44
JA
327 io_u->buflen = get_next_buflen(td);
328
329 if (io_u->buflen) {
330 io_u->ddir = get_rw_ddir(td);
843a7413
JA
331
332 /*
333 * If using a write iolog, store this entry.
334 */
335 if (td->write_iolog)
336 write_iolog_put(td, io_u);
337
53cdc686 338 io_u->file = f;
aea47d44
JA
339 return 0;
340 }
341 }
342
343 return 1;
344}
345
22f78b32 346#define queue_full(td) list_empty(&(td)->io_u_freelist)
ebac4655 347
b1ff3403 348struct io_u *__get_io_u(struct thread_data *td)
ebac4655 349{
22f78b32 350 struct io_u *io_u = NULL;
ebac4655 351
22f78b32
JA
352 if (!queue_full(td)) {
353 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
354
355 io_u->error = 0;
356 io_u->resid = 0;
357 list_del(&io_u->list);
358 list_add(&io_u->list, &td->io_u_busylist);
359 td->cur_depth++;
360 }
ebac4655 361
ebac4655
JA
362 return io_u;
363}
364
906c8d75
JA
365/*
366 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
367 * etc. The returned io_u is fully ready to be prepped and submitted.
368 */
53cdc686 369static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
ebac4655
JA
370{
371 struct io_u *io_u;
372
373 io_u = __get_io_u(td);
374 if (!io_u)
375 return NULL;
376
20dc95c4
JA
377 if (td->zone_bytes >= td->zone_size) {
378 td->zone_bytes = 0;
53cdc686 379 f->last_pos += td->zone_skip;
20dc95c4
JA
380 }
381
53cdc686 382 if (fill_io_u(td, f, io_u)) {
ebac4655
JA
383 put_io_u(td, io_u);
384 return NULL;
385 }
386
b2a15192
JA
387 if (io_u->buflen + io_u->offset > f->file_size) {
388 if (td->io_ops->flags & FIO_RAWIO) {
389 put_io_u(td, io_u);
390 return NULL;
391 }
392
53cdc686 393 io_u->buflen = f->file_size - io_u->offset;
b2a15192 394 }
ebac4655
JA
395
396 if (!io_u->buflen) {
397 put_io_u(td, io_u);
398 return NULL;
399 }
400
843a7413 401 if (!td->read_iolog && !td->sequential)
53cdc686 402 mark_random_map(td, f, io_u);
ebac4655 403
53cdc686 404 f->last_pos += io_u->buflen;
ebac4655
JA
405
406 if (td->verify != VERIFY_NONE)
e29d1b70 407 populate_verify_io_u(td, io_u);
ebac4655 408
aea47d44 409 if (td_io_prep(td, io_u)) {
ebac4655
JA
410 put_io_u(td, io_u);
411 return NULL;
412 }
413
414 gettimeofday(&io_u->start_time, NULL);
415 return io_u;
416}
417
418static inline void td_set_runstate(struct thread_data *td, int runstate)
419{
ebac4655
JA
420 td->runstate = runstate;
421}
422
53cdc686
JA
423static struct fio_file *get_next_file(struct thread_data *td)
424{
0ab8db89 425 unsigned int old_next_file = td->next_file;
b2a15192
JA
426 struct fio_file *f;
427
428 do {
429 f = &td->files[td->next_file];
430
431 td->next_file++;
432 if (td->next_file >= td->nr_files)
433 td->next_file = 0;
434
435 if (f->fd != -1)
436 break;
53cdc686 437
b2a15192
JA
438 f = NULL;
439 } while (td->next_file != old_next_file);
53cdc686
JA
440
441 return f;
442}
443
444static int td_io_sync(struct thread_data *td, struct fio_file *f)
ebac4655 445{
2866c82d 446 if (td->io_ops->sync)
53cdc686 447 return td->io_ops->sync(td, f);
ebac4655
JA
448
449 return 0;
450}
451
45bee283 452static int td_io_getevents(struct thread_data *td, int min, int max,
ebac4655
JA
453 struct timespec *t)
454{
2866c82d 455 return td->io_ops->getevents(td, min, max, t);
ebac4655
JA
456}
457
45bee283 458static int td_io_queue(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
459{
460 gettimeofday(&io_u->issue_time, NULL);
461
2866c82d 462 return td->io_ops->queue(td, io_u);
ebac4655
JA
463}
464
465#define iocb_time(iocb) ((unsigned long) (iocb)->data)
466
467static void io_completed(struct thread_data *td, struct io_u *io_u,
468 struct io_completion_data *icd)
469{
470 struct timeval e;
471 unsigned long msec;
472
473 gettimeofday(&e, NULL);
474
475 if (!io_u->error) {
20dc95c4 476 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 477 const int idx = io_u->ddir;
ebac4655
JA
478
479 td->io_blocks[idx]++;
20dc95c4
JA
480 td->io_bytes[idx] += bytes;
481 td->zone_bytes += bytes;
482 td->this_io_bytes[idx] += bytes;
ebac4655
JA
483
484 msec = mtime_since(&io_u->issue_time, &e);
485
3d60d1ed
JA
486 add_clat_sample(td, idx, msec);
487 add_bw_sample(td, idx);
ebac4655 488
3d60d1ed 489 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
490 log_io_piece(td, io_u);
491
20dc95c4 492 icd->bytes_done[idx] += bytes;
ebac4655
JA
493 } else
494 icd->error = io_u->error;
495}
496
497static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
498{
499 struct io_u *io_u;
500 int i;
501
502 icd->error = 0;
503 icd->bytes_done[0] = icd->bytes_done[1] = 0;
504
505 for (i = 0; i < icd->nr; i++) {
2866c82d 506 io_u = td->io_ops->event(td, i);
ebac4655
JA
507
508 io_completed(td, io_u, icd);
509 put_io_u(td, io_u);
510 }
511}
512
906c8d75
JA
513/*
514 * When job exits, we can cancel the in-flight IO if we are using async
515 * io. Attempt to do so.
516 */
ebac4655
JA
517static void cleanup_pending_aio(struct thread_data *td)
518{
519 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
520 struct list_head *entry, *n;
521 struct io_completion_data icd;
522 struct io_u *io_u;
523 int r;
524
525 /*
526 * get immediately available events, if any
527 */
45bee283 528 r = td_io_getevents(td, 0, td->cur_depth, &ts);
ebac4655
JA
529 if (r > 0) {
530 icd.nr = r;
531 ios_completed(td, &icd);
532 }
533
534 /*
535 * now cancel remaining active events
536 */
2866c82d 537 if (td->io_ops->cancel) {
ebac4655
JA
538 list_for_each_safe(entry, n, &td->io_u_busylist) {
539 io_u = list_entry(entry, struct io_u, list);
540
2866c82d 541 r = td->io_ops->cancel(td, io_u);
ebac4655
JA
542 if (!r)
543 put_io_u(td, io_u);
544 }
545 }
546
547 if (td->cur_depth) {
45bee283 548 r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL);
ebac4655
JA
549 if (r > 0) {
550 icd.nr = r;
551 ios_completed(td, &icd);
552 }
553 }
554}
555
906c8d75
JA
556/*
557 * The main verify engine. Runs over the writes we previusly submitted,
558 * reads the blocks back in, and checks the crc/md5 of the data.
559 */
a9619d44 560void do_verify(struct thread_data *td)
ebac4655
JA
561{
562 struct timeval t;
563 struct io_u *io_u, *v_io_u = NULL;
564 struct io_completion_data icd;
53cdc686 565 struct fio_file *f;
e5b401d4
JA
566 int ret, i;
567
568 /*
569 * sync io first and invalidate cache, to make sure we really
570 * read from disk.
571 */
572 for_each_file(td, f, i) {
573 td_io_sync(td, f);
574 file_invalidate_cache(td, f);
575 }
ebac4655
JA
576
577 td_set_runstate(td, TD_VERIFYING);
578
579 do {
580 if (td->terminate)
581 break;
582
583 gettimeofday(&t, NULL);
584 if (runtime_exceeded(td, &t))
585 break;
586
587 io_u = __get_io_u(td);
588 if (!io_u)
589 break;
590
aea47d44 591 if (get_next_verify(td, io_u)) {
ebac4655
JA
592 put_io_u(td, io_u);
593 break;
594 }
595
53cdc686
JA
596 f = get_next_file(td);
597 if (!f)
598 break;
599
600 io_u->file = f;
601
aea47d44 602 if (td_io_prep(td, io_u)) {
ebac4655
JA
603 put_io_u(td, io_u);
604 break;
605 }
606
45bee283 607 ret = td_io_queue(td, io_u);
ebac4655
JA
608 if (ret) {
609 put_io_u(td, io_u);
610 td_verror(td, ret);
611 break;
612 }
613
614 /*
615 * we have one pending to verify, do that while
616 * we are doing io on the next one
617 */
618 if (do_io_u_verify(td, &v_io_u))
619 break;
620
45bee283 621 ret = td_io_getevents(td, 1, 1, NULL);
ebac4655
JA
622 if (ret != 1) {
623 if (ret < 0)
624 td_verror(td, ret);
625 break;
626 }
627
2866c82d 628 v_io_u = td->io_ops->event(td, 0);
ebac4655
JA
629 icd.nr = 1;
630 icd.error = 0;
631 io_completed(td, v_io_u, &icd);
632
633 if (icd.error) {
634 td_verror(td, icd.error);
635 put_io_u(td, v_io_u);
636 v_io_u = NULL;
637 break;
638 }
639
ebac4655
JA
640 /*
641 * if we can't submit more io, we need to verify now
642 */
643 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
644 break;
645
646 } while (1);
647
648 do_io_u_verify(td, &v_io_u);
649
650 if (td->cur_depth)
651 cleanup_pending_aio(td);
652
653 td_set_runstate(td, TD_RUNNING);
654}
655
b990b5c0
JA
656/*
657 * Not really an io thread, all it does is burn CPU cycles in the specified
658 * manner.
659 */
660static void do_cpuio(struct thread_data *td)
661{
662 struct timeval e;
663 int split = 100 / td->cpuload;
664 int i = 0;
665
666 while (!td->terminate) {
667 gettimeofday(&e, NULL);
668
669 if (runtime_exceeded(td, &e))
670 break;
671
672 if (!(i % split))
673 __usec_sleep(10000);
674 else
675 usec_sleep(td, 10000);
676
677 i++;
678 }
679}
680
32cd46a0 681/*
906c8d75 682 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
683 * and reaps them, checking for rate and errors along the way.
684 */
ebac4655
JA
685static void do_io(struct thread_data *td)
686{
687 struct io_completion_data icd;
688 struct timeval s, e;
689 unsigned long usec;
53cdc686 690 struct fio_file *f;
84585003 691 int i, ret = 0;
ebac4655 692
5853e5a8
JA
693 td_set_runstate(td, TD_RUNNING);
694
ebac4655
JA
695 while (td->this_io_bytes[td->ddir] < td->io_size) {
696 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
697 struct timespec *timeout;
84585003 698 int min_evts = 0;
ebac4655
JA
699 struct io_u *io_u;
700
701 if (td->terminate)
702 break;
703
53cdc686
JA
704 f = get_next_file(td);
705 if (!f)
706 break;
707
708 io_u = get_io_u(td, f);
ebac4655
JA
709 if (!io_u)
710 break;
711
712 memcpy(&s, &io_u->start_time, sizeof(s));
713
45bee283 714 ret = td_io_queue(td, io_u);
ebac4655
JA
715 if (ret) {
716 put_io_u(td, io_u);
717 td_verror(td, ret);
718 break;
719 }
720
721 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
722
723 if (td->cur_depth < td->iodepth) {
724 timeout = &ts;
725 min_evts = 0;
726 } else {
727 timeout = NULL;
728 min_evts = 1;
729 }
730
84585003 731
45bee283 732 ret = td_io_getevents(td, min_evts, td->cur_depth, timeout);
ebac4655 733 if (ret < 0) {
84585003 734 td_verror(td, -ret);
ebac4655
JA
735 break;
736 } else if (!ret)
737 continue;
738
739 icd.nr = ret;
740 ios_completed(td, &icd);
741 if (icd.error) {
742 td_verror(td, icd.error);
743 break;
744 }
745
746 /*
747 * the rate is batched for now, it should work for batches
748 * of completions except the very first one which may look
749 * a little bursty
750 */
751 gettimeofday(&e, NULL);
752 usec = utime_since(&s, &e);
753
754 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
755
756 if (check_min_rate(td, &e)) {
757 td_verror(td, ENOMEM);
758 break;
759 }
760
761 if (runtime_exceeded(td, &e))
762 break;
763
764 if (td->thinktime)
765 usec_sleep(td, td->thinktime);
766
767 if (should_fsync(td) && td->fsync_blocks &&
768 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
53cdc686 769 td_io_sync(td, f);
ebac4655
JA
770 }
771
84585003
JA
772 if (!ret) {
773 if (td->cur_depth)
774 cleanup_pending_aio(td);
ebac4655 775
84585003
JA
776 if (should_fsync(td) && td->end_fsync) {
777 td_set_runstate(td, TD_FSYNCING);
778 for_each_file(td, f, i)
779 td_io_sync(td, f);
780 }
5853e5a8 781 }
ebac4655
JA
782}
783
45bee283 784static int td_io_init(struct thread_data *td)
ebac4655 785{
2866c82d
JA
786 if (td->io_ops->init)
787 return td->io_ops->init(td);
788
789 return 0;
ebac4655
JA
790}
791
792static void cleanup_io_u(struct thread_data *td)
793{
794 struct list_head *entry, *n;
795 struct io_u *io_u;
796
797 list_for_each_safe(entry, n, &td->io_u_freelist) {
798 io_u = list_entry(entry, struct io_u, list);
799
800 list_del(&io_u->list);
801 free(io_u);
802 }
803
804 if (td->mem_type == MEM_MALLOC)
805 free(td->orig_buffer);
806 else if (td->mem_type == MEM_SHM) {
807 struct shmid_ds sbuf;
808
809 shmdt(td->orig_buffer);
810 shmctl(td->shm_id, IPC_RMID, &sbuf);
811 } else if (td->mem_type == MEM_MMAP)
812 munmap(td->orig_buffer, td->orig_buffer_size);
813 else
3b70d7e5 814 log_err("Bad memory type %d\n", td->mem_type);
ebac4655
JA
815
816 td->orig_buffer = NULL;
817}
818
819static int init_io_u(struct thread_data *td)
820{
821 struct io_u *io_u;
822 int i, max_units;
823 char *p;
824
2866c82d 825 if (td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
826 return 0;
827
2866c82d 828 if (td->io_ops->flags & FIO_SYNCIO)
ebac4655
JA
829 max_units = 1;
830 else
831 max_units = td->iodepth;
832
833 td->orig_buffer_size = td->max_bs * max_units + MASK;
834
835 if (td->mem_type == MEM_MALLOC)
836 td->orig_buffer = malloc(td->orig_buffer_size);
837 else if (td->mem_type == MEM_SHM) {
838 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
839 if (td->shm_id < 0) {
840 td_verror(td, errno);
841 perror("shmget");
842 return 1;
843 }
844
845 td->orig_buffer = shmat(td->shm_id, NULL, 0);
846 if (td->orig_buffer == (void *) -1) {
847 td_verror(td, errno);
848 perror("shmat");
849 td->orig_buffer = NULL;
850 return 1;
851 }
852 } else if (td->mem_type == MEM_MMAP) {
853 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
854 if (td->orig_buffer == MAP_FAILED) {
855 td_verror(td, errno);
856 perror("mmap");
857 td->orig_buffer = NULL;
858 return 1;
859 }
860 }
861
ebac4655
JA
862 p = ALIGN(td->orig_buffer);
863 for (i = 0; i < max_units; i++) {
864 io_u = malloc(sizeof(*io_u));
865 memset(io_u, 0, sizeof(*io_u));
866 INIT_LIST_HEAD(&io_u->list);
867
868 io_u->buf = p + td->max_bs * i;
b1ff3403 869 io_u->index = i;
ebac4655
JA
870 list_add(&io_u->list, &td->io_u_freelist);
871 }
872
873 return 0;
874}
875
da86774e
JA
876static int switch_ioscheduler(struct thread_data *td)
877{
878 char tmp[256], tmp2[128];
879 FILE *f;
880 int ret;
881
882 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
883
884 f = fopen(tmp, "r+");
885 if (!f) {
886 td_verror(td, errno);
887 return 1;
888 }
889
890 /*
891 * Set io scheduler.
892 */
893 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
894 if (ferror(f) || ret != 1) {
895 td_verror(td, errno);
896 fclose(f);
897 return 1;
898 }
899
900 rewind(f);
901
902 /*
903 * Read back and check that the selected scheduler is now the default.
904 */
905 ret = fread(tmp, 1, sizeof(tmp), f);
906 if (ferror(f) || ret < 0) {
907 td_verror(td, errno);
908 fclose(f);
909 return 1;
910 }
911
912 sprintf(tmp2, "[%s]", td->ioscheduler);
913 if (!strstr(tmp, tmp2)) {
3b70d7e5 914 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
da86774e
JA
915 td_verror(td, EINVAL);
916 fclose(f);
917 return 1;
918 }
919
920 fclose(f);
921 return 0;
922}
923
ebac4655
JA
924static void clear_io_state(struct thread_data *td)
925{
53cdc686
JA
926 struct fio_file *f;
927 int i;
ebac4655 928
ebac4655
JA
929 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
930 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 931 td->zone_bytes = 0;
ebac4655 932
53cdc686
JA
933 for_each_file(td, f, i) {
934 f->last_pos = 0;
935 if (td->io_ops->flags & FIO_SYNCIO)
936 lseek(f->fd, SEEK_SET, 0);
937
938 if (f->file_map)
939 memset(f->file_map, 0, f->num_maps * sizeof(long));
940 }
ebac4655
JA
941}
942
906c8d75
JA
943/*
944 * Entry point for the thread based jobs. The process based jobs end up
945 * here as well, after a little setup.
946 */
ebac4655
JA
947static void *thread_main(void *data)
948{
949 struct thread_data *td = data;
ebac4655
JA
950
951 if (!td->use_thread)
952 setsid();
953
954 td->pid = getpid();
955
aea47d44
JA
956 INIT_LIST_HEAD(&td->io_u_freelist);
957 INIT_LIST_HEAD(&td->io_u_busylist);
958 INIT_LIST_HEAD(&td->io_hist_list);
959 INIT_LIST_HEAD(&td->io_log_list);
960
ebac4655
JA
961 if (init_io_u(td))
962 goto err;
963
964 if (fio_setaffinity(td) == -1) {
965 td_verror(td, errno);
966 goto err;
967 }
968
45bee283 969 if (td_io_init(td))
ebac4655
JA
970 goto err;
971
aea47d44
JA
972 if (init_iolog(td))
973 goto err;
974
ebac4655
JA
975 if (td->ioprio) {
976 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
977 td_verror(td, errno);
978 goto err;
979 }
980 }
981
1056eaad 982 if (nice(td->nice) == -1) {
b6f4d880
JA
983 td_verror(td, errno);
984 goto err;
985 }
986
75154845
JA
987 if (init_random_state(td))
988 goto err;
989
da86774e
JA
990 if (td->ioscheduler && switch_ioscheduler(td))
991 goto err;
992
75154845 993 td_set_runstate(td, TD_INITIALIZED);
bbfd6b00
JA
994 fio_sem_up(&startup_sem);
995 fio_sem_down(&td->mutex);
ebac4655 996
53cdc686 997 if (!td->create_serialize && setup_files(td))
ebac4655
JA
998 goto err;
999
ebac4655
JA
1000 gettimeofday(&td->epoch, NULL);
1001
4e0ba8af
JA
1002 if (td->exec_prerun)
1003 system(td->exec_prerun);
1004
ebac4655
JA
1005 while (td->loops--) {
1006 getrusage(RUSAGE_SELF, &td->ru_start);
1007 gettimeofday(&td->start, NULL);
1008 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1009
1010 if (td->ratemin)
1011 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1012
1013 clear_io_state(td);
1014 prune_io_piece_log(td);
1015
2866c82d 1016 if (td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
1017 do_cpuio(td);
1018 else
1019 do_io(td);
ebac4655
JA
1020
1021 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 1022 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
1023 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1024
ebac4655
JA
1025 update_rusage_stat(td);
1026
1027 if (td->error || td->terminate)
1028 break;
1029
1030 if (td->verify == VERIFY_NONE)
1031 continue;
1032
1033 clear_io_state(td);
1034 gettimeofday(&td->start, NULL);
1035
1036 do_verify(td);
1037
1038 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1039
1040 if (td->error || td->terminate)
1041 break;
1042 }
1043
ebac4655
JA
1044 if (td->bw_log)
1045 finish_log(td, td->bw_log, "bw");
1046 if (td->slat_log)
1047 finish_log(td, td->slat_log, "slat");
1048 if (td->clat_log)
1049 finish_log(td, td->clat_log, "clat");
843a7413
JA
1050 if (td->write_iolog)
1051 write_iolog_close(td);
4e0ba8af
JA
1052 if (td->exec_postrun)
1053 system(td->exec_postrun);
ebac4655
JA
1054
1055 if (exitall_on_terminate)
1056 terminate_threads(td->groupid);
1057
1058err:
53cdc686 1059 close_files(td);
2866c82d 1060 close_ioengine(td);
ebac4655 1061 cleanup_io_u(td);
ebac4655
JA
1062 td_set_runstate(td, TD_EXITED);
1063 return NULL;
1064
1065}
1066
906c8d75
JA
1067/*
1068 * We cannot pass the td data into a forked process, so attach the td and
1069 * pass it to the thread worker.
1070 */
ebac4655
JA
1071static void *fork_main(int shmid, int offset)
1072{
1073 struct thread_data *td;
1074 void *data;
1075
1076 data = shmat(shmid, NULL, 0);
1077 if (data == (void *) -1) {
1078 perror("shmat");
1079 return NULL;
1080 }
1081
1082 td = data + offset * sizeof(struct thread_data);
1083 thread_main(td);
1084 shmdt(data);
1085 return NULL;
1086}
1087
906c8d75
JA
1088/*
1089 * Run over the job map and reap the threads that have exited, if any.
1090 */
ebac4655
JA
1091static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1092{
b990b5c0 1093 int i, cputhreads;
ebac4655
JA
1094
1095 /*
1096 * reap exited threads (TD_EXITED -> TD_REAPED)
1097 */
b990b5c0 1098 for (i = 0, cputhreads = 0; i < thread_number; i++) {
ebac4655
JA
1099 struct thread_data *td = &threads[i];
1100
84585003
JA
1101 /*
1102 * ->io_ops is NULL for a thread that has closed its
1103 * io engine
1104 */
1105 if (td->io_ops && td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
1106 cputhreads++;
1107
ebac4655
JA
1108 if (td->runstate != TD_EXITED)
1109 continue;
1110
1111 td_set_runstate(td, TD_REAPED);
1112
1113 if (td->use_thread) {
1114 long ret;
1115
1116 if (pthread_join(td->thread, (void *) &ret))
1117 perror("thread_join");
1118 } else
1119 waitpid(td->pid, NULL, 0);
1120
1121 (*nr_running)--;
1122 (*m_rate) -= td->ratemin;
1123 (*t_rate) -= td->rate;
1124 }
b990b5c0
JA
1125
1126 if (*nr_running == cputhreads)
1127 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1128}
1129
fcb6ade2
JA
1130static void fio_unpin_memory(void *pinned)
1131{
1132 if (pinned) {
1133 if (munlock(pinned, mlock_size) < 0)
1134 perror("munlock");
1135 munmap(pinned, mlock_size);
1136 }
1137}
1138
1139static void *fio_pin_memory(void)
1140{
32cd46a0 1141 unsigned long long phys_mem;
fcb6ade2
JA
1142 void *ptr;
1143
1144 if (!mlock_size)
1145 return NULL;
1146
1147 /*
1148 * Don't allow mlock of more than real_mem-128MB
1149 */
32cd46a0
JA
1150 phys_mem = os_phys_mem();
1151 if (phys_mem) {
1152 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1153 mlock_size = phys_mem - 128 * 1024 * 1024;
eb8bbf48 1154 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
fcb6ade2
JA
1155 }
1156 }
1157
1158 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1159 if (!ptr) {
1160 perror("malloc locked mem");
1161 return NULL;
1162 }
1163 if (mlock(ptr, mlock_size) < 0) {
1164 munmap(ptr, mlock_size);
1165 perror("mlock");
1166 return NULL;
1167 }
1168
1169 return ptr;
1170}
1171
906c8d75
JA
1172/*
1173 * Main function for kicking off and reaping jobs, as needed.
1174 */
ebac4655
JA
1175static void run_threads(void)
1176{
ebac4655
JA
1177 struct thread_data *td;
1178 unsigned long spent;
1179 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2
JA
1180 void *mlocked_mem;
1181
1182 mlocked_mem = fio_pin_memory();
ebac4655 1183
c6ae0a5b
JA
1184 if (!terse_output) {
1185 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1186 fflush(stdout);
1187 }
c04f7ec3 1188
4efa970e
JA
1189 signal(SIGINT, sig_handler);
1190 signal(SIGALRM, sig_handler);
1191
ebac4655
JA
1192 todo = thread_number;
1193 nr_running = 0;
1194 nr_started = 0;
1195 m_rate = t_rate = 0;
1196
1197 for (i = 0; i < thread_number; i++) {
1198 td = &threads[i];
1199
263e529f 1200 print_status_init(td->thread_number - 1);
ebac4655
JA
1201
1202 init_disk_util(td);
1203
1204 if (!td->create_serialize)
1205 continue;
1206
1207 /*
1208 * do file setup here so it happens sequentially,
1209 * we don't want X number of threads getting their
1210 * client data interspersed on disk
1211 */
53cdc686 1212 if (setup_files(td)) {
ebac4655
JA
1213 td_set_runstate(td, TD_REAPED);
1214 todo--;
1215 }
1216 }
1217
263e529f 1218 time_init();
ebac4655
JA
1219
1220 while (todo) {
75154845
JA
1221 struct thread_data *map[MAX_JOBS];
1222 struct timeval this_start;
1223 int this_jobs = 0, left;
1224
ebac4655
JA
1225 /*
1226 * create threads (TD_NOT_CREATED -> TD_CREATED)
1227 */
1228 for (i = 0; i < thread_number; i++) {
1229 td = &threads[i];
1230
1231 if (td->runstate != TD_NOT_CREATED)
1232 continue;
1233
1234 /*
1235 * never got a chance to start, killed by other
1236 * thread for some reason
1237 */
1238 if (td->terminate) {
1239 todo--;
1240 continue;
1241 }
1242
1243 if (td->start_delay) {
263e529f 1244 spent = mtime_since_genesis();
ebac4655
JA
1245
1246 if (td->start_delay * 1000 > spent)
1247 continue;
1248 }
1249
1250 if (td->stonewall && (nr_started || nr_running))
1251 break;
1252
75154845
JA
1253 /*
1254 * Set state to created. Thread will transition
1255 * to TD_INITIALIZED when it's done setting up.
1256 */
ebac4655 1257 td_set_runstate(td, TD_CREATED);
75154845 1258 map[this_jobs++] = td;
bbfd6b00 1259 fio_sem_init(&startup_sem, 1);
ebac4655
JA
1260 nr_started++;
1261
1262 if (td->use_thread) {
1263 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1264 perror("thread_create");
1265 nr_started--;
1266 }
1267 } else {
1268 if (fork())
bbfd6b00 1269 fio_sem_down(&startup_sem);
ebac4655
JA
1270 else {
1271 fork_main(shm_id, i);
1272 exit(0);
1273 }
1274 }
1275 }
1276
1277 /*
75154845
JA
1278 * Wait for the started threads to transition to
1279 * TD_INITIALIZED.
ebac4655 1280 */
75154845
JA
1281 gettimeofday(&this_start, NULL);
1282 left = this_jobs;
1283 while (left) {
1284 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1285 break;
1286
1287 usleep(100000);
1288
1289 for (i = 0; i < this_jobs; i++) {
1290 td = map[i];
1291 if (!td)
1292 continue;
b6f4d880 1293 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1294 map[i] = NULL;
1295 left--;
b6f4d880
JA
1296 } else if (td->runstate >= TD_EXITED) {
1297 map[i] = NULL;
1298 left--;
1299 todo--;
1300 nr_running++; /* work-around... */
75154845
JA
1301 }
1302 }
1303 }
1304
1305 if (left) {
3b70d7e5 1306 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1307 for (i = 0; i < this_jobs; i++) {
1308 td = map[i];
1309 if (!td)
1310 continue;
1311 kill(td->pid, SIGTERM);
1312 }
1313 break;
1314 }
1315
1316 /*
b6f4d880 1317 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1318 */
ebac4655
JA
1319 for (i = 0; i < thread_number; i++) {
1320 td = &threads[i];
1321
75154845 1322 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1323 continue;
1324
1325 td_set_runstate(td, TD_RUNNING);
1326 nr_running++;
1327 nr_started--;
1328 m_rate += td->ratemin;
1329 t_rate += td->rate;
75154845 1330 todo--;
bbfd6b00 1331 fio_sem_up(&td->mutex);
ebac4655
JA
1332 }
1333
1334 reap_threads(&nr_running, &t_rate, &m_rate);
1335
1336 if (todo)
1337 usleep(100000);
1338 }
1339
1340 while (nr_running) {
1341 reap_threads(&nr_running, &t_rate, &m_rate);
1342 usleep(10000);
1343 }
1344
1345 update_io_ticks();
fcb6ade2 1346 fio_unpin_memory(mlocked_mem);
ebac4655
JA
1347}
1348
ebac4655
JA
1349int main(int argc, char *argv[])
1350{
1351 if (parse_options(argc, argv))
1352 return 1;
1353
1354 if (!thread_number) {
3b70d7e5 1355 log_err("Nothing to do\n");
ebac4655
JA
1356 return 1;
1357 }
1358
1359 disk_util_timer_arm();
1360
1361 run_threads();
1362 show_run_stats();
1363
1364 return 0;
1365}