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