[PATCH] Split up fio.c into log/stat/time parts
[fio.git] / fio.c
CommitLineData
ebac4655
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <string.h>
26#include <errno.h>
27#include <signal.h>
28#include <time.h>
ebac4655 29#include <assert.h>
ebac4655
JA
30#include <sys/stat.h>
31#include <sys/wait.h>
32#include <sys/ipc.h>
33#include <sys/shm.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36
37#include "fio.h"
38#include "os.h"
39
3c39a379
JA
40#include "fio-time.h"
41#include "fio-stat.h"
42#include "fio-log.h"
43
ebac4655
JA
44#define MASK (4095)
45
46#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
47
48int groupid = 0;
49int thread_number = 0;
50static char run_str[MAX_JOBS + 1];
51int shm_id = 0;
5289b847 52static struct timeval genesis;
ebac4655 53
ebac4655
JA
54static void print_thread_status(void);
55
c04f7ec3
JA
56extern unsigned long long mlock_size;
57
ebac4655
JA
58/*
59 * thread life cycle
60 */
61enum {
62 TD_NOT_CREATED = 0,
63 TD_CREATED,
75154845 64 TD_INITIALIZED,
ebac4655
JA
65 TD_RUNNING,
66 TD_VERIFYING,
67 TD_EXITED,
68 TD_REAPED,
69};
70
3d60d1ed 71#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655
JA
72
73static sem_t startup_sem;
74
75#define TERMINATE_ALL (-1)
75154845 76#define JOB_START_TIMEOUT (5 * 1000)
ebac4655
JA
77
78static void terminate_threads(int group_id)
79{
80 int i;
81
82 for (i = 0; i < thread_number; i++) {
83 struct thread_data *td = &threads[i];
84
85 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
86 td->terminate = 1;
87 td->start_delay = 0;
88 }
89 }
90}
91
92static void sig_handler(int sig)
93{
94 switch (sig) {
95 case SIGALRM:
96 update_io_ticks();
97 disk_util_timer_arm();
98 print_thread_status();
99 break;
100 default:
101 printf("\nfio: terminating on signal\n");
102 fflush(stdout);
103 terminate_threads(TERMINATE_ALL);
104 break;
105 }
106}
107
ebac4655
JA
108static int random_map_free(struct thread_data *td, unsigned long long block)
109{
110 unsigned int idx = RAND_MAP_IDX(td, block);
111 unsigned int bit = RAND_MAP_BIT(td, block);
112
113 return (td->file_map[idx] & (1UL << bit)) == 0;
114}
115
116static int get_next_free_block(struct thread_data *td, unsigned long long *b)
117{
118 int i;
119
120 *b = 0;
121 i = 0;
122 while ((*b) * td->min_bs < td->io_size) {
123 if (td->file_map[i] != -1UL) {
124 *b += ffz(td->file_map[i]);
125 return 0;
126 }
127
128 *b += BLOCKS_PER_MAP;
129 i++;
130 }
131
132 return 1;
133}
134
135static void mark_random_map(struct thread_data *td, struct io_u *io_u)
136{
200bc855 137 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
ebac4655
JA
138 unsigned int blocks = 0;
139
140 while (blocks < (io_u->buflen / td->min_bs)) {
141 unsigned int idx, bit;
142
143 if (!random_map_free(td, block))
144 break;
145
146 idx = RAND_MAP_IDX(td, block);
147 bit = RAND_MAP_BIT(td, block);
148
149 assert(idx < td->num_maps);
150
151 td->file_map[idx] |= (1UL << bit);
152 block++;
153 blocks++;
154 }
155
156 if ((blocks * td->min_bs) < io_u->buflen)
157 io_u->buflen = blocks * td->min_bs;
158}
159
20dc95c4
JA
160static int get_next_offset(struct thread_data *td, unsigned long long *offset)
161{
162 unsigned long long b, rb;
163 long r;
164
165 if (!td->sequential) {
085227ad 166 unsigned long long max_blocks = td->io_size / td->min_bs;
20dc95c4
JA
167 int loops = 50;
168
169 do {
170 lrand48_r(&td->random_state, &r);
085227ad 171 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
20dc95c4
JA
172 rb = b + (td->file_offset / td->min_bs);
173 loops--;
174 } while (!random_map_free(td, rb) && loops);
175
176 if (!loops) {
177 if (get_next_free_block(td, &b))
178 return 1;
179 }
180 } else
181 b = td->last_pos / td->min_bs;
182
183 *offset = (b * td->min_bs) + td->file_offset;
838a3cd3 184 if (*offset > td->real_file_size)
20dc95c4
JA
185 return 1;
186
187 return 0;
188}
189
190static unsigned int get_next_buflen(struct thread_data *td)
191{
192 unsigned int buflen;
193 long r;
194
195 if (td->min_bs == td->max_bs)
196 buflen = td->min_bs;
197 else {
198 lrand48_r(&td->bsrange_state, &r);
199 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
200 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
201 }
202
203 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
204 buflen = td->io_size - td->this_io_bytes[td->ddir];
205
206 return buflen;
207}
208
ebac4655
JA
209static int check_min_rate(struct thread_data *td, struct timeval *now)
210{
211 unsigned long spent;
212 unsigned long rate;
213 int ddir = td->ddir;
214
215 /*
216 * allow a 2 second settle period in the beginning
217 */
218 if (mtime_since(&td->start, now) < 2000)
219 return 0;
220
221 /*
222 * if rate blocks is set, sample is running
223 */
224 if (td->rate_bytes) {
225 spent = mtime_since(&td->lastrate, now);
226 if (spent < td->ratecycle)
227 return 0;
228
229 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
230 if (rate < td->ratemin) {
231 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
232 if (rate_quit)
233 terminate_threads(td->groupid);
234 return 1;
235 }
236 }
237
238 td->rate_bytes = td->this_io_bytes[ddir];
239 memcpy(&td->lastrate, now, sizeof(*now));
240 return 0;
241}
242
243static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
244{
245 if (!td->timeout)
246 return 0;
247 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
248 return 1;
249
250 return 0;
251}
252
253static void fill_random_bytes(struct thread_data *td,
254 unsigned char *p, unsigned int len)
255{
256 unsigned int todo;
257 double r;
258
259 while (len) {
260 drand48_r(&td->verify_state, &r);
261
262 /*
263 * lrand48_r seems to be broken and only fill the bottom
264 * 32-bits, even on 64-bit archs with 64-bit longs
265 */
266 todo = sizeof(r);
267 if (todo > len)
268 todo = len;
269
270 memcpy(p, &r, todo);
271
272 len -= todo;
273 p += todo;
274 }
275}
276
277static void hexdump(void *buffer, int len)
278{
279 unsigned char *p = buffer;
280 int i;
281
282 for (i = 0; i < len; i++)
283 printf("%02x", p[i]);
284 printf("\n");
285}
286
287static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
288{
289 unsigned char *p = (unsigned char *) io_u->buf;
290 unsigned long c;
291 int ret;
292
293 p += sizeof(*hdr);
294 c = crc32(p, hdr->len - sizeof(*hdr));
295 ret = c != hdr->crc32;
296
297 if (ret) {
298 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
299 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
300 }
301
302 return ret;
303}
304
305static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
306{
307 unsigned char *p = (unsigned char *) io_u->buf;
308 struct md5_ctx md5_ctx;
309 int ret;
310
311 memset(&md5_ctx, 0, sizeof(md5_ctx));
312 p += sizeof(*hdr);
313 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
314
315 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
316 if (ret) {
317 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
318 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
319 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
320 }
321
322 return ret;
323}
324
325static int verify_io_u(struct io_u *io_u)
326{
327 struct verify_header *hdr = (struct verify_header *) io_u->buf;
328 int ret;
329
330 if (hdr->fio_magic != FIO_HDR_MAGIC)
331 return 1;
332
333 if (hdr->verify_type == VERIFY_MD5)
334 ret = verify_io_u_md5(hdr, io_u);
335 else if (hdr->verify_type == VERIFY_CRC32)
336 ret = verify_io_u_crc32(hdr, io_u);
337 else {
338 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
339 ret = 1;
340 }
341
342 return ret;
343}
344
345static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
346{
347 hdr->crc32 = crc32(p, len);
348}
349
350static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
351{
352 struct md5_ctx md5_ctx;
353
354 memset(&md5_ctx, 0, sizeof(md5_ctx));
355 md5_update(&md5_ctx, p, len);
356 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
357}
358
a6ccc7be 359static int get_rw_ddir(struct thread_data *td)
3d60d1ed 360{
a6ccc7be
JA
361 if (td_rw(td)) {
362 struct timeval now;
363 unsigned long elapsed;
3d60d1ed 364
a6ccc7be
JA
365 gettimeofday(&now, NULL);
366 elapsed = mtime_since_now(&td->rwmix_switch);
3d60d1ed 367
a6ccc7be
JA
368 /*
369 * Check if it's time to seed a new data direction.
370 */
371 if (elapsed >= td->rwmixcycle) {
372 unsigned long v;
373 long r;
3d60d1ed 374
a6ccc7be
JA
375 lrand48_r(&td->random_state, &r);
376 v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
377 if (v < td->rwmixread)
378 td->rwmix_ddir = DDIR_READ;
379 else
380 td->rwmix_ddir = DDIR_WRITE;
381 memcpy(&td->rwmix_switch, &now, sizeof(now));
382 }
383 return td->rwmix_ddir;
384 } else if (td_read(td))
3d60d1ed
JA
385 return DDIR_READ;
386 else
387 return DDIR_WRITE;
388}
389
ebac4655
JA
390/*
391 * fill body of io_u->buf with random data and add a header with the
392 * (eg) sha1sum of that data.
393 */
394static void populate_io_u(struct thread_data *td, struct io_u *io_u)
395{
396 unsigned char *p = (unsigned char *) io_u->buf;
397 struct verify_header hdr;
398
399 hdr.fio_magic = FIO_HDR_MAGIC;
400 hdr.len = io_u->buflen;
401 p += sizeof(hdr);
402 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
403
404 if (td->verify == VERIFY_MD5) {
405 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
406 hdr.verify_type = VERIFY_MD5;
407 } else {
408 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
409 hdr.verify_type = VERIFY_CRC32;
410 }
411
412 memcpy(io_u->buf, &hdr, sizeof(hdr));
413}
414
aea47d44 415static int td_io_prep(struct thread_data *td, struct io_u *io_u)
20dc95c4 416{
20dc95c4
JA
417 if (td->io_prep && td->io_prep(td, io_u))
418 return 1;
419
420 return 0;
421}
422
b1ff3403 423void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
424{
425 list_del(&io_u->list);
426 list_add(&io_u->list, &td->io_u_freelist);
427 td->cur_depth--;
428}
429
843a7413
JA
430static int fill_io_u(struct thread_data *td, struct io_u *io_u)
431{
432 /*
433 * If using an iolog, grab next piece if any available.
434 */
435 if (td->read_iolog)
436 return read_iolog_get(td, io_u);
437
aea47d44
JA
438 /*
439 * No log, let the seq/rand engine retrieve the next position.
440 */
441 if (!get_next_offset(td, &io_u->offset)) {
442 io_u->buflen = get_next_buflen(td);
443
444 if (io_u->buflen) {
445 io_u->ddir = get_rw_ddir(td);
843a7413
JA
446
447 /*
448 * If using a write iolog, store this entry.
449 */
450 if (td->write_iolog)
451 write_iolog_put(td, io_u);
452
aea47d44
JA
453 return 0;
454 }
455 }
456
457 return 1;
458}
459
ebac4655
JA
460#define queue_full(td) (list_empty(&(td)->io_u_freelist))
461
b1ff3403 462struct io_u *__get_io_u(struct thread_data *td)
ebac4655
JA
463{
464 struct io_u *io_u;
465
466 if (queue_full(td))
467 return NULL;
468
469 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
470 io_u->error = 0;
471 io_u->resid = 0;
472 list_del(&io_u->list);
473 list_add(&io_u->list, &td->io_u_busylist);
474 td->cur_depth++;
475 return io_u;
476}
477
ebac4655
JA
478static struct io_u *get_io_u(struct thread_data *td)
479{
480 struct io_u *io_u;
481
482 io_u = __get_io_u(td);
483 if (!io_u)
484 return NULL;
485
20dc95c4
JA
486 if (td->zone_bytes >= td->zone_size) {
487 td->zone_bytes = 0;
488 td->last_pos += td->zone_skip;
489 }
490
aea47d44 491 if (fill_io_u(td, io_u)) {
ebac4655
JA
492 put_io_u(td, io_u);
493 return NULL;
494 }
495
838a3cd3
JA
496 if (io_u->buflen + io_u->offset > td->real_file_size)
497 io_u->buflen = td->real_file_size - io_u->offset;
ebac4655
JA
498
499 if (!io_u->buflen) {
500 put_io_u(td, io_u);
501 return NULL;
502 }
503
843a7413 504 if (!td->read_iolog && !td->sequential)
ebac4655
JA
505 mark_random_map(td, io_u);
506
20dc95c4 507 td->last_pos += io_u->buflen;
ebac4655
JA
508
509 if (td->verify != VERIFY_NONE)
510 populate_io_u(td, io_u);
511
aea47d44 512 if (td_io_prep(td, io_u)) {
ebac4655
JA
513 put_io_u(td, io_u);
514 return NULL;
515 }
516
517 gettimeofday(&io_u->start_time, NULL);
518 return io_u;
519}
520
521static inline void td_set_runstate(struct thread_data *td, int runstate)
522{
523 td->old_runstate = td->runstate;
524 td->runstate = runstate;
525}
526
aea47d44 527static int get_next_verify(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
528{
529 struct io_piece *ipo;
530
531 if (list_empty(&td->io_hist_list))
532 return 1;
533
534 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
535 list_del(&ipo->list);
536
aea47d44
JA
537 io_u->offset = ipo->offset;
538 io_u->buflen = ipo->len;
539 io_u->ddir = DDIR_READ;
ebac4655
JA
540 free(ipo);
541 return 0;
542}
543
ebac4655
JA
544static int sync_td(struct thread_data *td)
545{
546 if (td->io_sync)
547 return td->io_sync(td);
548
549 return 0;
550}
551
552static int io_u_getevents(struct thread_data *td, int min, int max,
553 struct timespec *t)
554{
555 return td->io_getevents(td, min, max, t);
556}
557
558static int io_u_queue(struct thread_data *td, struct io_u *io_u)
559{
560 gettimeofday(&io_u->issue_time, NULL);
561
562 return td->io_queue(td, io_u);
563}
564
565#define iocb_time(iocb) ((unsigned long) (iocb)->data)
566
567static void io_completed(struct thread_data *td, struct io_u *io_u,
568 struct io_completion_data *icd)
569{
570 struct timeval e;
571 unsigned long msec;
572
573 gettimeofday(&e, NULL);
574
575 if (!io_u->error) {
20dc95c4 576 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 577 const int idx = io_u->ddir;
ebac4655
JA
578
579 td->io_blocks[idx]++;
20dc95c4
JA
580 td->io_bytes[idx] += bytes;
581 td->zone_bytes += bytes;
582 td->this_io_bytes[idx] += bytes;
ebac4655
JA
583
584 msec = mtime_since(&io_u->issue_time, &e);
585
3d60d1ed
JA
586 add_clat_sample(td, idx, msec);
587 add_bw_sample(td, idx);
ebac4655 588
3d60d1ed 589 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
590 log_io_piece(td, io_u);
591
20dc95c4 592 icd->bytes_done[idx] += bytes;
ebac4655
JA
593 } else
594 icd->error = io_u->error;
595}
596
597static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
598{
599 struct io_u *io_u;
600 int i;
601
602 icd->error = 0;
603 icd->bytes_done[0] = icd->bytes_done[1] = 0;
604
605 for (i = 0; i < icd->nr; i++) {
606 io_u = td->io_event(td, i);
607
608 io_completed(td, io_u, icd);
609 put_io_u(td, io_u);
610 }
611}
612
613static void cleanup_pending_aio(struct thread_data *td)
614{
615 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
616 struct list_head *entry, *n;
617 struct io_completion_data icd;
618 struct io_u *io_u;
619 int r;
620
621 /*
622 * get immediately available events, if any
623 */
624 r = io_u_getevents(td, 0, td->cur_depth, &ts);
625 if (r > 0) {
626 icd.nr = r;
627 ios_completed(td, &icd);
628 }
629
630 /*
631 * now cancel remaining active events
632 */
633 if (td->io_cancel) {
634 list_for_each_safe(entry, n, &td->io_u_busylist) {
635 io_u = list_entry(entry, struct io_u, list);
636
637 r = td->io_cancel(td, io_u);
638 if (!r)
639 put_io_u(td, io_u);
640 }
641 }
642
643 if (td->cur_depth) {
644 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
645 if (r > 0) {
646 icd.nr = r;
647 ios_completed(td, &icd);
648 }
649 }
650}
651
652static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
653{
654 struct io_u *v_io_u = *io_u;
655 int ret = 0;
656
657 if (v_io_u) {
658 ret = verify_io_u(v_io_u);
659 put_io_u(td, v_io_u);
660 *io_u = NULL;
661 }
662
663 return ret;
664}
665
666static void do_verify(struct thread_data *td)
667{
668 struct timeval t;
669 struct io_u *io_u, *v_io_u = NULL;
670 struct io_completion_data icd;
671 int ret;
672
673 td_set_runstate(td, TD_VERIFYING);
674
675 do {
676 if (td->terminate)
677 break;
678
679 gettimeofday(&t, NULL);
680 if (runtime_exceeded(td, &t))
681 break;
682
683 io_u = __get_io_u(td);
684 if (!io_u)
685 break;
686
aea47d44 687 if (get_next_verify(td, io_u)) {
ebac4655
JA
688 put_io_u(td, io_u);
689 break;
690 }
691
aea47d44 692 if (td_io_prep(td, io_u)) {
ebac4655
JA
693 put_io_u(td, io_u);
694 break;
695 }
696
697 ret = io_u_queue(td, io_u);
698 if (ret) {
699 put_io_u(td, io_u);
700 td_verror(td, ret);
701 break;
702 }
703
704 /*
705 * we have one pending to verify, do that while
706 * we are doing io on the next one
707 */
708 if (do_io_u_verify(td, &v_io_u))
709 break;
710
711 ret = io_u_getevents(td, 1, 1, NULL);
712 if (ret != 1) {
713 if (ret < 0)
714 td_verror(td, ret);
715 break;
716 }
717
718 v_io_u = td->io_event(td, 0);
719 icd.nr = 1;
720 icd.error = 0;
721 io_completed(td, v_io_u, &icd);
722
723 if (icd.error) {
724 td_verror(td, icd.error);
725 put_io_u(td, v_io_u);
726 v_io_u = NULL;
727 break;
728 }
729
ebac4655
JA
730 /*
731 * if we can't submit more io, we need to verify now
732 */
733 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
734 break;
735
736 } while (1);
737
738 do_io_u_verify(td, &v_io_u);
739
740 if (td->cur_depth)
741 cleanup_pending_aio(td);
742
743 td_set_runstate(td, TD_RUNNING);
744}
745
746static void do_io(struct thread_data *td)
747{
748 struct io_completion_data icd;
749 struct timeval s, e;
750 unsigned long usec;
751
752 while (td->this_io_bytes[td->ddir] < td->io_size) {
753 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
754 struct timespec *timeout;
755 int ret, min_evts = 0;
756 struct io_u *io_u;
757
758 if (td->terminate)
759 break;
760
761 io_u = get_io_u(td);
762 if (!io_u)
763 break;
764
765 memcpy(&s, &io_u->start_time, sizeof(s));
766
767 ret = io_u_queue(td, io_u);
768 if (ret) {
769 put_io_u(td, io_u);
770 td_verror(td, ret);
771 break;
772 }
773
774 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
775
776 if (td->cur_depth < td->iodepth) {
777 timeout = &ts;
778 min_evts = 0;
779 } else {
780 timeout = NULL;
781 min_evts = 1;
782 }
783
784 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
785 if (ret < 0) {
786 td_verror(td, ret);
787 break;
788 } else if (!ret)
789 continue;
790
791 icd.nr = ret;
792 ios_completed(td, &icd);
793 if (icd.error) {
794 td_verror(td, icd.error);
795 break;
796 }
797
798 /*
799 * the rate is batched for now, it should work for batches
800 * of completions except the very first one which may look
801 * a little bursty
802 */
803 gettimeofday(&e, NULL);
804 usec = utime_since(&s, &e);
805
806 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
807
808 if (check_min_rate(td, &e)) {
809 td_verror(td, ENOMEM);
810 break;
811 }
812
813 if (runtime_exceeded(td, &e))
814 break;
815
816 if (td->thinktime)
817 usec_sleep(td, td->thinktime);
818
819 if (should_fsync(td) && td->fsync_blocks &&
820 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
821 sync_td(td);
822 }
823
824 if (td->cur_depth)
825 cleanup_pending_aio(td);
826
fc1a4713 827 if (should_fsync(td) && td->end_fsync)
ebac4655
JA
828 sync_td(td);
829}
830
831static void cleanup_io(struct thread_data *td)
832{
833 if (td->io_cleanup)
834 td->io_cleanup(td);
835}
836
837static int init_io(struct thread_data *td)
838{
839 if (td->io_engine == FIO_SYNCIO)
840 return fio_syncio_init(td);
841 else if (td->io_engine == FIO_MMAPIO)
842 return fio_mmapio_init(td);
843 else if (td->io_engine == FIO_LIBAIO)
844 return fio_libaio_init(td);
845 else if (td->io_engine == FIO_POSIXAIO)
846 return fio_posixaio_init(td);
847 else if (td->io_engine == FIO_SGIO)
848 return fio_sgio_init(td);
8756e4d4
JA
849 else if (td->io_engine == FIO_SPLICEIO)
850 return fio_spliceio_init(td);
ebac4655
JA
851 else {
852 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
853 return 1;
854 }
855}
856
857static void cleanup_io_u(struct thread_data *td)
858{
859 struct list_head *entry, *n;
860 struct io_u *io_u;
861
862 list_for_each_safe(entry, n, &td->io_u_freelist) {
863 io_u = list_entry(entry, struct io_u, list);
864
865 list_del(&io_u->list);
866 free(io_u);
867 }
868
869 if (td->mem_type == MEM_MALLOC)
870 free(td->orig_buffer);
871 else if (td->mem_type == MEM_SHM) {
872 struct shmid_ds sbuf;
873
874 shmdt(td->orig_buffer);
875 shmctl(td->shm_id, IPC_RMID, &sbuf);
876 } else if (td->mem_type == MEM_MMAP)
877 munmap(td->orig_buffer, td->orig_buffer_size);
878 else
879 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
880
881 td->orig_buffer = NULL;
882}
883
884static int init_io_u(struct thread_data *td)
885{
886 struct io_u *io_u;
887 int i, max_units;
888 char *p;
889
890 if (td->io_engine & FIO_SYNCIO)
891 max_units = 1;
892 else
893 max_units = td->iodepth;
894
895 td->orig_buffer_size = td->max_bs * max_units + MASK;
896
897 if (td->mem_type == MEM_MALLOC)
898 td->orig_buffer = malloc(td->orig_buffer_size);
899 else if (td->mem_type == MEM_SHM) {
900 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
901 if (td->shm_id < 0) {
902 td_verror(td, errno);
903 perror("shmget");
904 return 1;
905 }
906
907 td->orig_buffer = shmat(td->shm_id, NULL, 0);
908 if (td->orig_buffer == (void *) -1) {
909 td_verror(td, errno);
910 perror("shmat");
911 td->orig_buffer = NULL;
912 return 1;
913 }
914 } else if (td->mem_type == MEM_MMAP) {
915 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
916 if (td->orig_buffer == MAP_FAILED) {
917 td_verror(td, errno);
918 perror("mmap");
919 td->orig_buffer = NULL;
920 return 1;
921 }
922 }
923
ebac4655
JA
924 p = ALIGN(td->orig_buffer);
925 for (i = 0; i < max_units; i++) {
926 io_u = malloc(sizeof(*io_u));
927 memset(io_u, 0, sizeof(*io_u));
928 INIT_LIST_HEAD(&io_u->list);
929
930 io_u->buf = p + td->max_bs * i;
b1ff3403 931 io_u->index = i;
ebac4655
JA
932 list_add(&io_u->list, &td->io_u_freelist);
933 }
934
935 return 0;
936}
937
4b5bd14c
JA
938static void cleanup_allocs(struct thread_data *td)
939{
940 if (td->directory)
941 free(td->directory);
942 if (td->iolog_file)
943 free(td->iolog_file);
944 if (td->exec_prerun)
945 free(td->exec_prerun);
946 if (td->exec_postrun)
947 free(td->exec_postrun);
948 if (td->ioscheduler)
949 free(td->ioscheduler);
950 if (td->sysfs_root)
951 free(td->sysfs_root);
952}
953
ebac4655
JA
954static int create_file(struct thread_data *td, unsigned long long size,
955 int extend)
956{
957 unsigned long long left;
958 unsigned int bs;
959 int r, oflags;
960 char *b;
961
962 /*
963 * unless specifically asked for overwrite, let normal io extend it
964 */
965 if (td_write(td) && !td->overwrite)
966 return 0;
967
968 if (!size) {
969 fprintf(stderr, "Need size for create\n");
970 td_verror(td, EINVAL);
971 return 1;
972 }
973
974 if (!extend) {
975 oflags = O_CREAT | O_TRUNC;
976 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
977 } else {
978 oflags = O_APPEND;
979 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
980 }
981
982 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
983 if (td->fd < 0) {
984 td_verror(td, errno);
985 return 1;
986 }
987
988 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
989 td_verror(td, errno);
990 return 1;
991 }
992
993 td->io_size = td->file_size;
994 b = malloc(td->max_bs);
995 memset(b, 0, td->max_bs);
996
997 left = size;
998 while (left && !td->terminate) {
999 bs = td->max_bs;
1000 if (bs > left)
1001 bs = left;
1002
1003 r = write(td->fd, b, bs);
1004
1005 if (r == (int) bs) {
1006 left -= bs;
1007 continue;
1008 } else {
1009 if (r < 0)
1010 td_verror(td, errno);
1011 else
1012 td_verror(td, EIO);
1013
1014 break;
1015 }
1016 }
1017
1018 if (td->terminate)
1019 unlink(td->file_name);
1020 else if (td->create_fsync)
1021 fsync(td->fd);
1022
1023 close(td->fd);
1024 td->fd = -1;
1025 free(b);
1026 return 0;
1027}
1028
1029static int file_size(struct thread_data *td)
1030{
1031 struct stat st;
1032
1033 if (fstat(td->fd, &st) == -1) {
1034 td_verror(td, errno);
1035 return 1;
1036 }
1037
838a3cd3
JA
1038 td->real_file_size = st.st_size;
1039
1040 if (!td->file_size || td->file_size > td->real_file_size)
1041 td->file_size = td->real_file_size;
ebac4655 1042
e8e387c1 1043 td->file_size -= td->file_offset;
ebac4655
JA
1044 return 0;
1045}
1046
1047static int bdev_size(struct thread_data *td)
1048{
9104f874 1049 unsigned long long bytes;
ebac4655
JA
1050 int r;
1051
1052 r = blockdev_size(td->fd, &bytes);
1053 if (r) {
1054 td_verror(td, r);
1055 return 1;
1056 }
1057
838a3cd3
JA
1058 td->real_file_size = bytes;
1059
ebac4655
JA
1060 /*
1061 * no extend possibilities, so limit size to device size if too large
1062 */
838a3cd3
JA
1063 if (!td->file_size || td->file_size > td->real_file_size)
1064 td->file_size = td->real_file_size;
ebac4655 1065
e8e387c1 1066 td->file_size -= td->file_offset;
ebac4655
JA
1067 return 0;
1068}
1069
1070static int get_file_size(struct thread_data *td)
1071{
0af7b542 1072 int ret = 0;
ebac4655
JA
1073
1074 if (td->filetype == FIO_TYPE_FILE)
1075 ret = file_size(td);
0af7b542 1076 else if (td->filetype == FIO_TYPE_BD)
ebac4655 1077 ret = bdev_size(td);
0af7b542
JA
1078 else
1079 td->real_file_size = -1;
ebac4655
JA
1080
1081 if (ret)
1082 return ret;
1083
e8e387c1
JA
1084 if (td->file_offset > td->real_file_size) {
1085 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
ebac4655
JA
1086 return 1;
1087 }
1088
838a3cd3 1089 td->io_size = td->file_size;
ebac4655
JA
1090 if (td->io_size == 0) {
1091 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1092 td_verror(td, EINVAL);
1093 return 1;
1094 }
1095
20dc95c4
JA
1096 if (!td->zone_size)
1097 td->zone_size = td->io_size;
1098
ebac4655
JA
1099 td->total_io_size = td->io_size * td->loops;
1100 return 0;
1101}
1102
1103static int setup_file_mmap(struct thread_data *td)
1104{
1105 int flags;
1106
3d60d1ed
JA
1107 if (td_rw(td))
1108 flags = PROT_READ | PROT_WRITE;
1109 else if (td_write(td)) {
ebac4655
JA
1110 flags = PROT_WRITE;
1111
1112 if (td->verify != VERIFY_NONE)
1113 flags |= PROT_READ;
3d60d1ed
JA
1114 } else
1115 flags = PROT_READ;
ebac4655
JA
1116
1117 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1118 if (td->mmap == MAP_FAILED) {
1119 td->mmap = NULL;
1120 td_verror(td, errno);
1121 return 1;
1122 }
1123
1124 if (td->invalidate_cache) {
1125 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1126 td_verror(td, errno);
1127 return 1;
1128 }
1129 }
1130
1131 if (td->sequential) {
1132 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1133 td_verror(td, errno);
1134 return 1;
1135 }
1136 } else {
1137 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1138 td_verror(td, errno);
1139 return 1;
1140 }
1141 }
1142
1143 return 0;
1144}
1145
1146static int setup_file_plain(struct thread_data *td)
1147{
1148 if (td->invalidate_cache) {
1149 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1150 td_verror(td, errno);
1151 return 1;
1152 }
1153 }
1154
1155 if (td->sequential) {
1156 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1157 td_verror(td, errno);
1158 return 1;
1159 }
1160 } else {
1161 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1162 td_verror(td, errno);
1163 return 1;
1164 }
1165 }
1166
1167 return 0;
1168}
1169
1170static int setup_file(struct thread_data *td)
1171{
1172 struct stat st;
1173 int flags = 0;
1174
1175 if (stat(td->file_name, &st) == -1) {
1176 if (errno != ENOENT) {
1177 td_verror(td, errno);
1178 return 1;
1179 }
1180 if (!td->create_file) {
1181 td_verror(td, ENOENT);
1182 return 1;
1183 }
1184 if (create_file(td, td->file_size, 0))
1185 return 1;
1186 } else if (td->filetype == FIO_TYPE_FILE) {
6a0106a0 1187 if (st.st_size < (off_t) td->file_size) {
ebac4655
JA
1188 if (create_file(td, td->file_size - st.st_size, 1))
1189 return 1;
1190 }
1191 }
1192
1193 if (td->odirect)
1194 flags |= O_DIRECT;
1195
3d60d1ed 1196 if (td_write(td) || td_rw(td)) {
ebac4655
JA
1197 if (td->filetype == FIO_TYPE_FILE) {
1198 if (!td->overwrite)
1199 flags |= O_TRUNC;
1200
1201 flags |= O_CREAT;
1202 }
1203 if (td->sync_io)
1204 flags |= O_SYNC;
1205
1206 flags |= O_RDWR;
1207
1208 td->fd = open(td->file_name, flags, 0600);
3d60d1ed
JA
1209 } else {
1210 if (td->filetype == FIO_TYPE_CHAR)
1211 flags |= O_RDWR;
1212 else
1213 flags |= O_RDONLY;
1214
1215 td->fd = open(td->file_name, flags);
ebac4655
JA
1216 }
1217
1218 if (td->fd == -1) {
1219 td_verror(td, errno);
1220 return 1;
1221 }
1222
1223 if (get_file_size(td))
1224 return 1;
1225
1226 if (td->io_engine != FIO_MMAPIO)
1227 return setup_file_plain(td);
1228 else
1229 return setup_file_mmap(td);
1230}
1231
da86774e
JA
1232static int switch_ioscheduler(struct thread_data *td)
1233{
1234 char tmp[256], tmp2[128];
1235 FILE *f;
1236 int ret;
1237
1238 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1239
1240 f = fopen(tmp, "r+");
1241 if (!f) {
1242 td_verror(td, errno);
1243 return 1;
1244 }
1245
1246 /*
1247 * Set io scheduler.
1248 */
1249 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1250 if (ferror(f) || ret != 1) {
1251 td_verror(td, errno);
1252 fclose(f);
1253 return 1;
1254 }
1255
1256 rewind(f);
1257
1258 /*
1259 * Read back and check that the selected scheduler is now the default.
1260 */
1261 ret = fread(tmp, 1, sizeof(tmp), f);
1262 if (ferror(f) || ret < 0) {
1263 td_verror(td, errno);
1264 fclose(f);
1265 return 1;
1266 }
1267
1268 sprintf(tmp2, "[%s]", td->ioscheduler);
1269 if (!strstr(tmp, tmp2)) {
1270 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1271 td_verror(td, EINVAL);
1272 fclose(f);
1273 return 1;
1274 }
1275
1276 fclose(f);
1277 return 0;
1278}
1279
ebac4655
JA
1280static void clear_io_state(struct thread_data *td)
1281{
1282 if (td->io_engine == FIO_SYNCIO)
1283 lseek(td->fd, SEEK_SET, 0);
1284
20dc95c4 1285 td->last_pos = 0;
ebac4655
JA
1286 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1287 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 1288 td->zone_bytes = 0;
ebac4655
JA
1289
1290 if (td->file_map)
1291 memset(td->file_map, 0, td->num_maps * sizeof(long));
1292}
1293
ebac4655
JA
1294static void *thread_main(void *data)
1295{
1296 struct thread_data *td = data;
ebac4655
JA
1297
1298 if (!td->use_thread)
1299 setsid();
1300
1301 td->pid = getpid();
1302
aea47d44
JA
1303 INIT_LIST_HEAD(&td->io_u_freelist);
1304 INIT_LIST_HEAD(&td->io_u_busylist);
1305 INIT_LIST_HEAD(&td->io_hist_list);
1306 INIT_LIST_HEAD(&td->io_log_list);
1307
ebac4655
JA
1308 if (init_io_u(td))
1309 goto err;
1310
1311 if (fio_setaffinity(td) == -1) {
1312 td_verror(td, errno);
1313 goto err;
1314 }
1315
1316 if (init_io(td))
1317 goto err;
1318
aea47d44
JA
1319 if (init_iolog(td))
1320 goto err;
1321
ebac4655
JA
1322 if (td->ioprio) {
1323 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1324 td_verror(td, errno);
1325 goto err;
1326 }
1327 }
1328
b6f4d880
JA
1329 if (nice(td->nice) < 0) {
1330 td_verror(td, errno);
1331 goto err;
1332 }
1333
75154845
JA
1334 if (init_random_state(td))
1335 goto err;
1336
da86774e
JA
1337 if (td->ioscheduler && switch_ioscheduler(td))
1338 goto err;
1339
75154845 1340 td_set_runstate(td, TD_INITIALIZED);
ebac4655
JA
1341 sem_post(&startup_sem);
1342 sem_wait(&td->mutex);
1343
1344 if (!td->create_serialize && setup_file(td))
1345 goto err;
1346
ebac4655
JA
1347 gettimeofday(&td->epoch, NULL);
1348
4e0ba8af
JA
1349 if (td->exec_prerun)
1350 system(td->exec_prerun);
1351
ebac4655
JA
1352 while (td->loops--) {
1353 getrusage(RUSAGE_SELF, &td->ru_start);
1354 gettimeofday(&td->start, NULL);
1355 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1356
1357 if (td->ratemin)
1358 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1359
1360 clear_io_state(td);
1361 prune_io_piece_log(td);
1362
1363 do_io(td);
1364
1365 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 1366 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
1367 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1368
ebac4655
JA
1369 update_rusage_stat(td);
1370
1371 if (td->error || td->terminate)
1372 break;
1373
1374 if (td->verify == VERIFY_NONE)
1375 continue;
1376
1377 clear_io_state(td);
1378 gettimeofday(&td->start, NULL);
1379
1380 do_verify(td);
1381
1382 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1383
1384 if (td->error || td->terminate)
1385 break;
1386 }
1387
ebac4655
JA
1388 if (td->bw_log)
1389 finish_log(td, td->bw_log, "bw");
1390 if (td->slat_log)
1391 finish_log(td, td->slat_log, "slat");
1392 if (td->clat_log)
1393 finish_log(td, td->clat_log, "clat");
843a7413
JA
1394 if (td->write_iolog)
1395 write_iolog_close(td);
4e0ba8af
JA
1396 if (td->exec_postrun)
1397 system(td->exec_postrun);
ebac4655
JA
1398
1399 if (exitall_on_terminate)
1400 terminate_threads(td->groupid);
1401
1402err:
1403 if (td->fd != -1) {
1404 close(td->fd);
1405 td->fd = -1;
1406 }
1407 if (td->mmap)
1408 munmap(td->mmap, td->file_size);
4b5bd14c 1409 cleanup_allocs(td);
ebac4655
JA
1410 cleanup_io(td);
1411 cleanup_io_u(td);
ebac4655
JA
1412 td_set_runstate(td, TD_EXITED);
1413 return NULL;
1414
1415}
1416
1417static void *fork_main(int shmid, int offset)
1418{
1419 struct thread_data *td;
1420 void *data;
1421
1422 data = shmat(shmid, NULL, 0);
1423 if (data == (void *) -1) {
1424 perror("shmat");
1425 return NULL;
1426 }
1427
1428 td = data + offset * sizeof(struct thread_data);
1429 thread_main(td);
1430 shmdt(data);
1431 return NULL;
1432}
1433
ebac4655
JA
1434static void check_str_update(struct thread_data *td)
1435{
1436 char c = run_str[td->thread_number - 1];
1437
1438 if (td->runstate == td->old_runstate)
1439 return;
1440
1441 switch (td->runstate) {
1442 case TD_REAPED:
1443 c = '_';
1444 break;
1445 case TD_EXITED:
1446 c = 'E';
1447 break;
1448 case TD_RUNNING:
3d60d1ed
JA
1449 if (td_rw(td)) {
1450 if (td->sequential)
1451 c = 'M';
1452 else
1453 c = 'm';
1454 } else if (td_read(td)) {
ebac4655
JA
1455 if (td->sequential)
1456 c = 'R';
1457 else
1458 c = 'r';
1459 } else {
1460 if (td->sequential)
1461 c = 'W';
1462 else
1463 c = 'w';
1464 }
1465 break;
1466 case TD_VERIFYING:
1467 c = 'V';
1468 break;
1469 case TD_CREATED:
1470 c = 'C';
1471 break;
75154845
JA
1472 case TD_INITIALIZED:
1473 c = 'I';
1474 break;
ebac4655
JA
1475 case TD_NOT_CREATED:
1476 c = 'P';
1477 break;
1478 default:
1479 printf("state %d\n", td->runstate);
1480 }
1481
1482 run_str[td->thread_number - 1] = c;
1483 td->old_runstate = td->runstate;
1484}
1485
5289b847
JA
1486static void eta_to_str(char *str, int eta_sec)
1487{
1488 unsigned int d, h, m, s;
1489 static int always_d, always_h;
1490
1491 d = h = m = s = 0;
1492
1493 s = eta_sec % 60;
1494 eta_sec /= 60;
1495 m = eta_sec % 60;
1496 eta_sec /= 60;
1497 h = eta_sec % 24;
1498 eta_sec /= 24;
1499 d = eta_sec;
1500
1501 if (d || always_d) {
1502 always_d = 1;
1503 str += sprintf(str, "%02dd:", d);
1504 }
1505 if (h || always_h) {
1506 always_h = 1;
1507 str += sprintf(str, "%02dh:", h);
1508 }
1509
1510 str += sprintf(str, "%02dm:", m);
1511 str += sprintf(str, "%02ds", s);
1512}
1513
6a0106a0
JA
1514static int thread_eta(struct thread_data *td, unsigned long elapsed)
1515{
1516 unsigned long long bytes_total, bytes_done;
1517 unsigned int eta_sec = 0;
1518
1519 bytes_total = td->total_io_size;
3d60d1ed
JA
1520
1521 /*
1522 * if writing, bytes_total will be twice the size. If mixing,
1523 * assume a 50/50 split and thus bytes_total will be 50% larger.
1524 */
1525 if (td->verify) {
1526 if (td_rw(td))
1527 bytes_total = bytes_total * 3 / 2;
1528 else
1529 bytes_total <<= 1;
1530 }
6a0106a0
JA
1531 if (td->zone_size && td->zone_skip)
1532 bytes_total /= (td->zone_skip / td->zone_size);
1533
1534 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1535 double perc;
8b611c34 1536
6a0106a0
JA
1537 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1538 perc = (double) bytes_done / (double) bytes_total;
1539 if (perc > 1.0)
1540 perc = 1.0;
1541
1542 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1543
8b611c34 1544 if (td->timeout && eta_sec > (td->timeout - elapsed))
6a0106a0 1545 eta_sec = td->timeout - elapsed;
75154845
JA
1546 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1547 || td->runstate == TD_INITIALIZED) {
6a0106a0
JA
1548 int t_eta = 0, r_eta = 0;
1549
1550 /*
1551 * We can only guess - assume it'll run the full timeout
1552 * if given, otherwise assume it'll run at the specified rate.
1553 */
1554 if (td->timeout)
1555 t_eta = td->timeout + td->start_delay - elapsed;
1556 if (td->rate) {
1557 r_eta = (bytes_total / 1024) / td->rate;
1558 r_eta += td->start_delay - elapsed;
1559 }
1560
1561 if (r_eta && t_eta)
1562 eta_sec = min(r_eta, t_eta);
1563 else if (r_eta)
1564 eta_sec = r_eta;
1565 else if (t_eta)
1566 eta_sec = t_eta;
1567 else
1568 eta_sec = INT_MAX;
1569 } else {
1570 /*
1571 * thread is already done
1572 */
1573 eta_sec = 0;
1574 }
1575
1576 return eta_sec;
1577}
1578
ebac4655
JA
1579static void print_thread_status(void)
1580{
6a0106a0
JA
1581 unsigned long elapsed = time_since_now(&genesis);
1582 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
5289b847 1583 char eta_str[32];
6a0106a0
JA
1584 double perc = 0.0;
1585
1586 eta_secs = malloc(thread_number * sizeof(int));
1587 memset(eta_secs, 0, thread_number * sizeof(int));
ebac4655 1588
ebac4655
JA
1589 nr_running = t_rate = m_rate = 0;
1590 for (i = 0; i < thread_number; i++) {
1591 struct thread_data *td = &threads[i];
1592
1593 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1594 nr_running++;
1595 t_rate += td->rate;
1596 m_rate += td->ratemin;
1597 }
1598
6a0106a0
JA
1599 if (elapsed >= 3)
1600 eta_secs[i] = thread_eta(td, elapsed);
8b611c34
JA
1601 else
1602 eta_secs[i] = INT_MAX;
ebac4655
JA
1603
1604 check_str_update(td);
1605 }
1606
6a0106a0
JA
1607 if (exitall_on_terminate)
1608 eta_sec = INT_MAX;
1609 else
1610 eta_sec = 0;
5289b847 1611
6a0106a0
JA
1612 for (i = 0; i < thread_number; i++) {
1613 if (exitall_on_terminate) {
1614 if (eta_secs[i] < eta_sec)
1615 eta_sec = eta_secs[i];
1616 } else {
1617 if (eta_secs[i] > eta_sec)
1618 eta_sec = eta_secs[i];
5289b847 1619 }
6a0106a0 1620 }
5289b847 1621
8b611c34 1622 if (eta_sec != INT_MAX && elapsed) {
6a0106a0
JA
1623 perc = (double) elapsed / (double) (elapsed + eta_sec);
1624 eta_to_str(eta_str, eta_sec);
ebac4655
JA
1625 }
1626
5289b847 1627 printf("Threads now running (%d)", nr_running);
ebac4655
JA
1628 if (m_rate || t_rate)
1629 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8b611c34 1630 if (eta_sec != INT_MAX) {
6a0106a0
JA
1631 perc *= 100.0;
1632 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1633 }
5289b847 1634 printf("\r");
ebac4655 1635 fflush(stdout);
6a0106a0 1636 free(eta_secs);
ebac4655
JA
1637}
1638
1639static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1640{
1641 int i;
1642
1643 /*
1644 * reap exited threads (TD_EXITED -> TD_REAPED)
1645 */
1646 for (i = 0; i < thread_number; i++) {
1647 struct thread_data *td = &threads[i];
1648
1649 if (td->runstate != TD_EXITED)
1650 continue;
1651
1652 td_set_runstate(td, TD_REAPED);
1653
1654 if (td->use_thread) {
1655 long ret;
1656
1657 if (pthread_join(td->thread, (void *) &ret))
1658 perror("thread_join");
1659 } else
1660 waitpid(td->pid, NULL, 0);
1661
1662 (*nr_running)--;
1663 (*m_rate) -= td->ratemin;
1664 (*t_rate) -= td->rate;
1665 }
1666}
1667
fcb6ade2
JA
1668static void fio_unpin_memory(void *pinned)
1669{
1670 if (pinned) {
1671 if (munlock(pinned, mlock_size) < 0)
1672 perror("munlock");
1673 munmap(pinned, mlock_size);
1674 }
1675}
1676
1677static void *fio_pin_memory(void)
1678{
1679 long pagesize, pages;
1680 void *ptr;
1681
1682 if (!mlock_size)
1683 return NULL;
1684
1685 /*
1686 * Don't allow mlock of more than real_mem-128MB
1687 */
1688 pagesize = sysconf(_SC_PAGESIZE);
1689 pages = sysconf(_SC_PHYS_PAGES);
1690 if (pages != -1 && pagesize != -1) {
1691 unsigned long long real_mem = pages * pagesize;
1692
1693 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
1694 mlock_size = real_mem - 128 * 1024 * 1024;
1695 printf("fio: limiting mlocked memory to %lluMiB\n",
1696 mlock_size >> 20);
1697 }
1698 }
1699
1700 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1701 if (!ptr) {
1702 perror("malloc locked mem");
1703 return NULL;
1704 }
1705 if (mlock(ptr, mlock_size) < 0) {
1706 munmap(ptr, mlock_size);
1707 perror("mlock");
1708 return NULL;
1709 }
1710
1711 return ptr;
1712}
1713
ebac4655
JA
1714static void run_threads(void)
1715{
ebac4655
JA
1716 struct thread_data *td;
1717 unsigned long spent;
1718 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2
JA
1719 void *mlocked_mem;
1720
1721 mlocked_mem = fio_pin_memory();
ebac4655
JA
1722
1723 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
fcb6ade2 1724 fflush(stdout);
c04f7ec3 1725
4efa970e
JA
1726 signal(SIGINT, sig_handler);
1727 signal(SIGALRM, sig_handler);
1728
ebac4655
JA
1729 todo = thread_number;
1730 nr_running = 0;
1731 nr_started = 0;
1732 m_rate = t_rate = 0;
1733
1734 for (i = 0; i < thread_number; i++) {
1735 td = &threads[i];
1736
1737 run_str[td->thread_number - 1] = 'P';
1738
1739 init_disk_util(td);
1740
1741 if (!td->create_serialize)
1742 continue;
1743
1744 /*
1745 * do file setup here so it happens sequentially,
1746 * we don't want X number of threads getting their
1747 * client data interspersed on disk
1748 */
1749 if (setup_file(td)) {
1750 td_set_runstate(td, TD_REAPED);
1751 todo--;
1752 }
1753 }
1754
1755 gettimeofday(&genesis, NULL);
1756
1757 while (todo) {
75154845
JA
1758 struct thread_data *map[MAX_JOBS];
1759 struct timeval this_start;
1760 int this_jobs = 0, left;
1761
ebac4655
JA
1762 /*
1763 * create threads (TD_NOT_CREATED -> TD_CREATED)
1764 */
1765 for (i = 0; i < thread_number; i++) {
1766 td = &threads[i];
1767
1768 if (td->runstate != TD_NOT_CREATED)
1769 continue;
1770
1771 /*
1772 * never got a chance to start, killed by other
1773 * thread for some reason
1774 */
1775 if (td->terminate) {
1776 todo--;
1777 continue;
1778 }
1779
1780 if (td->start_delay) {
1781 spent = mtime_since_now(&genesis);
1782
1783 if (td->start_delay * 1000 > spent)
1784 continue;
1785 }
1786
1787 if (td->stonewall && (nr_started || nr_running))
1788 break;
1789
75154845
JA
1790 /*
1791 * Set state to created. Thread will transition
1792 * to TD_INITIALIZED when it's done setting up.
1793 */
ebac4655 1794 td_set_runstate(td, TD_CREATED);
75154845 1795 map[this_jobs++] = td;
ebac4655 1796 sem_init(&startup_sem, 0, 1);
ebac4655
JA
1797 nr_started++;
1798
1799 if (td->use_thread) {
1800 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1801 perror("thread_create");
1802 nr_started--;
1803 }
1804 } else {
1805 if (fork())
1806 sem_wait(&startup_sem);
1807 else {
1808 fork_main(shm_id, i);
1809 exit(0);
1810 }
1811 }
1812 }
1813
1814 /*
75154845
JA
1815 * Wait for the started threads to transition to
1816 * TD_INITIALIZED.
ebac4655 1817 */
75154845
JA
1818 printf("fio: Waiting for threads to initialize...\n");
1819 gettimeofday(&this_start, NULL);
1820 left = this_jobs;
1821 while (left) {
1822 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1823 break;
1824
1825 usleep(100000);
1826
1827 for (i = 0; i < this_jobs; i++) {
1828 td = map[i];
1829 if (!td)
1830 continue;
b6f4d880 1831 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1832 map[i] = NULL;
1833 left--;
b6f4d880
JA
1834 } else if (td->runstate >= TD_EXITED) {
1835 map[i] = NULL;
1836 left--;
1837 todo--;
1838 nr_running++; /* work-around... */
75154845
JA
1839 }
1840 }
1841 }
1842
1843 if (left) {
1844 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1845 for (i = 0; i < this_jobs; i++) {
1846 td = map[i];
1847 if (!td)
1848 continue;
1849 kill(td->pid, SIGTERM);
1850 }
1851 break;
1852 }
1853
1854 /*
b6f4d880 1855 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845
JA
1856 */
1857 printf("fio: Go for launch\n");
ebac4655
JA
1858 for (i = 0; i < thread_number; i++) {
1859 td = &threads[i];
1860
75154845 1861 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1862 continue;
1863
1864 td_set_runstate(td, TD_RUNNING);
1865 nr_running++;
1866 nr_started--;
1867 m_rate += td->ratemin;
1868 t_rate += td->rate;
75154845 1869 todo--;
ebac4655
JA
1870 sem_post(&td->mutex);
1871 }
1872
1873 reap_threads(&nr_running, &t_rate, &m_rate);
1874
1875 if (todo)
1876 usleep(100000);
1877 }
1878
1879 while (nr_running) {
1880 reap_threads(&nr_running, &t_rate, &m_rate);
1881 usleep(10000);
1882 }
1883
1884 update_io_ticks();
fcb6ade2 1885 fio_unpin_memory(mlocked_mem);
ebac4655
JA
1886}
1887
ebac4655
JA
1888int main(int argc, char *argv[])
1889{
1890 if (parse_options(argc, argv))
1891 return 1;
1892
1893 if (!thread_number) {
1894 printf("Nothing to do\n");
1895 return 1;
1896 }
1897
1898 disk_util_timer_arm();
1899
1900 run_threads();
1901 show_run_stats();
1902
1903 return 0;
1904}