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