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