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