2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
31 #include <sys/ioctl.h>
39 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
42 int thread_number = 0;
43 static char run_str[MAX_JOBS + 1];
45 static struct timeval genesis;
46 static int temp_stall_ts;
48 static void print_thread_status(void);
50 extern unsigned long long mlock_size;
53 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
54 * will never back again. It may cycle between running/verififying/fsyncing.
55 * Once the thread reaches TD_EXITED, it is just waiting for the core to
69 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
71 static volatile int startup_sem;
73 #define TERMINATE_ALL (-1)
74 #define JOB_START_TIMEOUT (5 * 1000)
76 static void terminate_threads(int group_id)
80 for (i = 0; i < thread_number; i++) {
81 struct thread_data *td = &threads[i];
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
90 static void sig_handler(int sig)
95 disk_util_timer_arm();
96 print_thread_status();
99 printf("\nfio: terminating on signal\n");
101 terminate_threads(TERMINATE_ALL);
106 static int random_map_free(struct thread_data *td, unsigned long long block)
108 unsigned int idx = RAND_MAP_IDX(td, block);
109 unsigned int bit = RAND_MAP_BIT(td, block);
111 return (td->file_map[idx] & (1UL << bit)) == 0;
114 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
120 while ((*b) * td->min_bs < td->io_size) {
121 if (td->file_map[i] != -1UL) {
122 *b += ffz(td->file_map[i]);
126 *b += BLOCKS_PER_MAP;
133 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
135 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
136 unsigned int blocks = 0;
138 while (blocks < (io_u->buflen / td->min_bs)) {
139 unsigned int idx, bit;
141 if (!random_map_free(td, block))
144 idx = RAND_MAP_IDX(td, block);
145 bit = RAND_MAP_BIT(td, block);
147 assert(idx < td->num_maps);
149 td->file_map[idx] |= (1UL << bit);
154 if ((blocks * td->min_bs) < io_u->buflen)
155 io_u->buflen = blocks * td->min_bs;
158 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
160 unsigned long long b, rb;
163 if (!td->sequential) {
164 unsigned long long max_blocks = td->io_size / td->min_bs;
168 r = os_random_long(&td->random_state);
169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
170 rb = b + (td->file_offset / td->min_bs);
172 } while (!random_map_free(td, rb) && loops);
175 if (get_next_free_block(td, &b))
179 b = td->last_pos / td->min_bs;
181 *offset = (b * td->min_bs) + td->file_offset;
182 if (*offset > td->real_file_size)
188 static unsigned int get_next_buflen(struct thread_data *td)
193 if (td->min_bs == td->max_bs)
196 r = os_random_long(&td->bsrange_state);
197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
202 buflen = td->io_size - td->this_io_bytes[td->ddir];
207 static int check_min_rate(struct thread_data *td, struct timeval *now)
214 * allow a 2 second settle period in the beginning
216 if (mtime_since(&td->start, now) < 2000)
220 * if rate blocks is set, sample is running
222 if (td->rate_bytes) {
223 spent = mtime_since(&td->lastrate, now);
224 if (spent < td->ratecycle)
227 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
228 if (rate < td->ratemin) {
229 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
231 terminate_threads(td->groupid);
236 td->rate_bytes = td->this_io_bytes[ddir];
237 memcpy(&td->lastrate, now, sizeof(*now));
241 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
245 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
251 static void fill_random_bytes(struct thread_data *td,
252 unsigned char *p, unsigned int len)
258 r = os_random_double(&td->verify_state);
261 * lrand48_r seems to be broken and only fill the bottom
262 * 32-bits, even on 64-bit archs with 64-bit longs
275 static void hexdump(void *buffer, int len)
277 unsigned char *p = buffer;
280 for (i = 0; i < len; i++)
281 fprintf(f_out, "%02x", p[i]);
282 fprintf(f_out, "\n");
285 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
287 unsigned char *p = (unsigned char *) io_u->buf;
291 c = crc32(p, hdr->len - sizeof(*hdr));
293 if (c != hdr->crc32) {
294 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
295 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
302 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
304 unsigned char *p = (unsigned char *) io_u->buf;
305 struct md5_ctx md5_ctx;
307 memset(&md5_ctx, 0, sizeof(md5_ctx));
309 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
311 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
312 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
313 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
314 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
321 static int verify_io_u(struct io_u *io_u)
323 struct verify_header *hdr = (struct verify_header *) io_u->buf;
326 if (hdr->fio_magic != FIO_HDR_MAGIC)
329 if (hdr->verify_type == VERIFY_MD5)
330 ret = verify_io_u_md5(hdr, io_u);
331 else if (hdr->verify_type == VERIFY_CRC32)
332 ret = verify_io_u_crc32(hdr, io_u);
334 log_err("Bad verify type %d\n", hdr->verify_type);
341 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
343 hdr->crc32 = crc32(p, len);
346 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
348 struct md5_ctx md5_ctx;
350 memset(&md5_ctx, 0, sizeof(md5_ctx));
351 md5_update(&md5_ctx, p, len);
352 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
355 static int get_rw_ddir(struct thread_data *td)
359 unsigned long elapsed;
361 gettimeofday(&now, NULL);
362 elapsed = mtime_since_now(&td->rwmix_switch);
365 * Check if it's time to seed a new data direction.
367 if (elapsed >= td->rwmixcycle) {
371 r = os_random_long(&td->rwmix_state);
372 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
373 if (v < td->rwmixread)
374 td->rwmix_ddir = DDIR_READ;
376 td->rwmix_ddir = DDIR_WRITE;
377 memcpy(&td->rwmix_switch, &now, sizeof(now));
379 return td->rwmix_ddir;
380 } else if (td_read(td))
387 * fill body of io_u->buf with random data and add a header with the
388 * crc32 or md5 sum of that data.
390 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
392 unsigned char *p = (unsigned char *) io_u->buf;
393 struct verify_header hdr;
395 hdr.fio_magic = FIO_HDR_MAGIC;
396 hdr.len = io_u->buflen;
398 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
400 if (td->verify == VERIFY_MD5) {
401 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
402 hdr.verify_type = VERIFY_MD5;
404 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
405 hdr.verify_type = VERIFY_CRC32;
408 memcpy(io_u->buf, &hdr, sizeof(hdr));
411 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
413 if (td->io_prep && td->io_prep(td, io_u))
419 void put_io_u(struct thread_data *td, struct io_u *io_u)
421 list_del(&io_u->list);
422 list_add(&io_u->list, &td->io_u_freelist);
426 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
429 * If using an iolog, grab next piece if any available.
432 return read_iolog_get(td, io_u);
435 * No log, let the seq/rand engine retrieve the next position.
437 if (!get_next_offset(td, &io_u->offset)) {
438 io_u->buflen = get_next_buflen(td);
441 io_u->ddir = get_rw_ddir(td);
444 * If using a write iolog, store this entry.
447 write_iolog_put(td, io_u);
456 #define queue_full(td) list_empty(&(td)->io_u_freelist)
458 struct io_u *__get_io_u(struct thread_data *td)
460 struct io_u *io_u = NULL;
462 if (!queue_full(td)) {
463 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
467 list_del(&io_u->list);
468 list_add(&io_u->list, &td->io_u_busylist);
475 static struct io_u *get_io_u(struct thread_data *td)
479 io_u = __get_io_u(td);
483 if (td->zone_bytes >= td->zone_size) {
485 td->last_pos += td->zone_skip;
488 if (fill_io_u(td, io_u)) {
493 if (io_u->buflen + io_u->offset > td->real_file_size)
494 io_u->buflen = td->real_file_size - io_u->offset;
501 if (!td->read_iolog && !td->sequential)
502 mark_random_map(td, io_u);
504 td->last_pos += io_u->buflen;
506 if (td->verify != VERIFY_NONE)
507 populate_io_u(td, io_u);
509 if (td_io_prep(td, io_u)) {
514 gettimeofday(&io_u->start_time, NULL);
518 static inline void td_set_runstate(struct thread_data *td, int runstate)
520 td->runstate = runstate;
523 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
525 struct io_piece *ipo;
527 if (!list_empty(&td->io_hist_list)) {
528 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
530 list_del(&ipo->list);
532 io_u->offset = ipo->offset;
533 io_u->buflen = ipo->len;
534 io_u->ddir = DDIR_READ;
542 static int sync_td(struct thread_data *td)
545 return td->io_sync(td);
550 static int io_u_getevents(struct thread_data *td, int min, int max,
553 return td->io_getevents(td, min, max, t);
556 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
558 gettimeofday(&io_u->issue_time, NULL);
560 return td->io_queue(td, io_u);
563 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
565 static void io_completed(struct thread_data *td, struct io_u *io_u,
566 struct io_completion_data *icd)
571 gettimeofday(&e, NULL);
574 unsigned int bytes = io_u->buflen - io_u->resid;
575 const int idx = io_u->ddir;
577 td->io_blocks[idx]++;
578 td->io_bytes[idx] += bytes;
579 td->zone_bytes += bytes;
580 td->this_io_bytes[idx] += bytes;
582 msec = mtime_since(&io_u->issue_time, &e);
584 add_clat_sample(td, idx, msec);
585 add_bw_sample(td, idx);
587 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
588 log_io_piece(td, io_u);
590 icd->bytes_done[idx] += bytes;
592 icd->error = io_u->error;
595 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
601 icd->bytes_done[0] = icd->bytes_done[1] = 0;
603 for (i = 0; i < icd->nr; i++) {
604 io_u = td->io_event(td, i);
606 io_completed(td, io_u, icd);
611 static void cleanup_pending_aio(struct thread_data *td)
613 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
614 struct list_head *entry, *n;
615 struct io_completion_data icd;
620 * get immediately available events, if any
622 r = io_u_getevents(td, 0, td->cur_depth, &ts);
625 ios_completed(td, &icd);
629 * now cancel remaining active events
632 list_for_each_safe(entry, n, &td->io_u_busylist) {
633 io_u = list_entry(entry, struct io_u, list);
635 r = td->io_cancel(td, io_u);
642 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
645 ios_completed(td, &icd);
650 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
652 struct io_u *v_io_u = *io_u;
656 ret = verify_io_u(v_io_u);
657 put_io_u(td, v_io_u);
664 static void do_verify(struct thread_data *td)
667 struct io_u *io_u, *v_io_u = NULL;
668 struct io_completion_data icd;
671 td_set_runstate(td, TD_VERIFYING);
677 gettimeofday(&t, NULL);
678 if (runtime_exceeded(td, &t))
681 io_u = __get_io_u(td);
685 if (get_next_verify(td, io_u)) {
690 if (td_io_prep(td, io_u)) {
695 ret = io_u_queue(td, io_u);
703 * we have one pending to verify, do that while
704 * we are doing io on the next one
706 if (do_io_u_verify(td, &v_io_u))
709 ret = io_u_getevents(td, 1, 1, NULL);
716 v_io_u = td->io_event(td, 0);
719 io_completed(td, v_io_u, &icd);
722 td_verror(td, icd.error);
723 put_io_u(td, v_io_u);
729 * if we can't submit more io, we need to verify now
731 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
736 do_io_u_verify(td, &v_io_u);
739 cleanup_pending_aio(td);
741 td_set_runstate(td, TD_RUNNING);
745 * Main IO worker functions. It retrieves io_u's to process and queues
746 * and reaps them, checking for rate and errors along the way.
748 static void do_io(struct thread_data *td)
750 struct io_completion_data icd;
754 td_set_runstate(td, TD_RUNNING);
756 while (td->this_io_bytes[td->ddir] < td->io_size) {
757 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
758 struct timespec *timeout;
759 int ret, min_evts = 0;
769 memcpy(&s, &io_u->start_time, sizeof(s));
771 ret = io_u_queue(td, io_u);
778 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
780 if (td->cur_depth < td->iodepth) {
788 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
796 ios_completed(td, &icd);
798 td_verror(td, icd.error);
803 * the rate is batched for now, it should work for batches
804 * of completions except the very first one which may look
807 gettimeofday(&e, NULL);
808 usec = utime_since(&s, &e);
810 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
812 if (check_min_rate(td, &e)) {
813 td_verror(td, ENOMEM);
817 if (runtime_exceeded(td, &e))
821 usec_sleep(td, td->thinktime);
823 if (should_fsync(td) && td->fsync_blocks &&
824 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
829 cleanup_pending_aio(td);
831 if (should_fsync(td) && td->end_fsync) {
832 td_set_runstate(td, TD_FSYNCING);
837 static void cleanup_io(struct thread_data *td)
843 static int init_io(struct thread_data *td)
845 if (td->io_engine == FIO_SYNCIO)
846 return fio_syncio_init(td);
847 else if (td->io_engine == FIO_MMAPIO)
848 return fio_mmapio_init(td);
849 else if (td->io_engine == FIO_LIBAIO)
850 return fio_libaio_init(td);
851 else if (td->io_engine == FIO_POSIXAIO)
852 return fio_posixaio_init(td);
853 else if (td->io_engine == FIO_SGIO)
854 return fio_sgio_init(td);
855 else if (td->io_engine == FIO_SPLICEIO)
856 return fio_spliceio_init(td);
858 log_err("bad io_engine %d\n", td->io_engine);
863 static void cleanup_io_u(struct thread_data *td)
865 struct list_head *entry, *n;
868 list_for_each_safe(entry, n, &td->io_u_freelist) {
869 io_u = list_entry(entry, struct io_u, list);
871 list_del(&io_u->list);
875 if (td->mem_type == MEM_MALLOC)
876 free(td->orig_buffer);
877 else if (td->mem_type == MEM_SHM) {
878 struct shmid_ds sbuf;
880 shmdt(td->orig_buffer);
881 shmctl(td->shm_id, IPC_RMID, &sbuf);
882 } else if (td->mem_type == MEM_MMAP)
883 munmap(td->orig_buffer, td->orig_buffer_size);
885 log_err("Bad memory type %d\n", td->mem_type);
887 td->orig_buffer = NULL;
890 static int init_io_u(struct thread_data *td)
896 if (td->io_engine & FIO_SYNCIO)
899 max_units = td->iodepth;
901 td->orig_buffer_size = td->max_bs * max_units + MASK;
903 if (td->mem_type == MEM_MALLOC)
904 td->orig_buffer = malloc(td->orig_buffer_size);
905 else if (td->mem_type == MEM_SHM) {
906 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
907 if (td->shm_id < 0) {
908 td_verror(td, errno);
913 td->orig_buffer = shmat(td->shm_id, NULL, 0);
914 if (td->orig_buffer == (void *) -1) {
915 td_verror(td, errno);
917 td->orig_buffer = NULL;
920 } else if (td->mem_type == MEM_MMAP) {
921 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
922 if (td->orig_buffer == MAP_FAILED) {
923 td_verror(td, errno);
925 td->orig_buffer = NULL;
930 p = ALIGN(td->orig_buffer);
931 for (i = 0; i < max_units; i++) {
932 io_u = malloc(sizeof(*io_u));
933 memset(io_u, 0, sizeof(*io_u));
934 INIT_LIST_HEAD(&io_u->list);
936 io_u->buf = p + td->max_bs * i;
938 list_add(&io_u->list, &td->io_u_freelist);
944 static int create_file(struct thread_data *td, unsigned long long size,
947 unsigned long long left;
953 * unless specifically asked for overwrite, let normal io extend it
955 if (td_write(td) && !td->overwrite) {
956 td->real_file_size = size;
961 log_err("Need size for create\n");
962 td_verror(td, EINVAL);
969 oflags = O_CREAT | O_TRUNC;
970 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
973 fprintf(f_out, "%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
976 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
978 td_verror(td, errno);
982 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
983 td_verror(td, errno);
987 td->io_size = td->file_size;
988 b = malloc(td->max_bs);
989 memset(b, 0, td->max_bs);
992 while (left && !td->terminate) {
997 r = write(td->fd, b, bs);
1004 td_verror(td, errno);
1013 unlink(td->file_name);
1014 else if (td->create_fsync)
1026 static int file_size(struct thread_data *td)
1030 if (td->overwrite) {
1031 if (fstat(td->fd, &st) == -1) {
1032 td_verror(td, errno);
1036 td->real_file_size = st.st_size;
1038 if (!td->file_size || td->file_size > td->real_file_size)
1039 td->file_size = td->real_file_size;
1042 td->file_size -= td->file_offset;
1046 static int bdev_size(struct thread_data *td)
1048 unsigned long long bytes;
1051 r = blockdev_size(td->fd, &bytes);
1057 td->real_file_size = bytes;
1060 * no extend possibilities, so limit size to device size if too large
1062 if (!td->file_size || td->file_size > td->real_file_size)
1063 td->file_size = td->real_file_size;
1065 td->file_size -= td->file_offset;
1069 static int get_file_size(struct thread_data *td)
1073 if (td->filetype == FIO_TYPE_FILE)
1074 ret = file_size(td);
1075 else if (td->filetype == FIO_TYPE_BD)
1076 ret = bdev_size(td);
1078 td->real_file_size = -1;
1083 if (td->file_offset > td->real_file_size) {
1084 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1088 td->io_size = td->file_size;
1089 if (td->io_size == 0) {
1090 log_err("%s: no io blocks\n", td->name);
1091 td_verror(td, EINVAL);
1096 td->zone_size = td->io_size;
1098 td->total_io_size = td->io_size * td->loops;
1102 static int setup_file_mmap(struct thread_data *td)
1107 flags = PROT_READ | PROT_WRITE;
1108 else if (td_write(td)) {
1111 if (td->verify != VERIFY_NONE)
1116 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1117 if (td->mmap == MAP_FAILED) {
1119 td_verror(td, errno);
1123 if (td->invalidate_cache) {
1124 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1125 td_verror(td, errno);
1130 if (td->sequential) {
1131 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1132 td_verror(td, errno);
1136 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1137 td_verror(td, errno);
1145 static int setup_file_plain(struct thread_data *td)
1147 if (td->invalidate_cache) {
1148 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1149 td_verror(td, errno);
1154 if (td->sequential) {
1155 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1156 td_verror(td, errno);
1160 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1161 td_verror(td, errno);
1169 static int setup_file(struct thread_data *td)
1174 if (stat(td->file_name, &st) == -1) {
1175 if (errno != ENOENT) {
1176 td_verror(td, errno);
1179 if (!td->create_file) {
1180 td_verror(td, ENOENT);
1183 if (create_file(td, td->file_size, 0))
1185 } else if (td->filetype == FIO_TYPE_FILE) {
1186 if (st.st_size < (off_t) td->file_size) {
1187 if (create_file(td, td->file_size, 1))
1193 flags |= OS_O_DIRECT;
1195 if (td_write(td) || td_rw(td)) {
1196 if (td->filetype == FIO_TYPE_FILE) {
1207 td->fd = open(td->file_name, flags, 0600);
1209 if (td->filetype == FIO_TYPE_CHAR)
1214 td->fd = open(td->file_name, flags);
1218 td_verror(td, errno);
1222 if (get_file_size(td))
1225 if (td->io_engine != FIO_MMAPIO)
1226 return setup_file_plain(td);
1228 return setup_file_mmap(td);
1231 static int switch_ioscheduler(struct thread_data *td)
1233 char tmp[256], tmp2[128];
1237 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1239 f = fopen(tmp, "r+");
1241 td_verror(td, errno);
1248 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1249 if (ferror(f) || ret != 1) {
1250 td_verror(td, errno);
1258 * Read back and check that the selected scheduler is now the default.
1260 ret = fread(tmp, 1, sizeof(tmp), f);
1261 if (ferror(f) || ret < 0) {
1262 td_verror(td, errno);
1267 sprintf(tmp2, "[%s]", td->ioscheduler);
1268 if (!strstr(tmp, tmp2)) {
1269 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
1270 td_verror(td, EINVAL);
1279 static void clear_io_state(struct thread_data *td)
1281 if (td->io_engine == FIO_SYNCIO)
1282 lseek(td->fd, SEEK_SET, 0);
1285 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1286 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1290 memset(td->file_map, 0, td->num_maps * sizeof(long));
1293 static void *thread_main(void *data)
1295 struct thread_data *td = data;
1297 if (!td->use_thread)
1302 INIT_LIST_HEAD(&td->io_u_freelist);
1303 INIT_LIST_HEAD(&td->io_u_busylist);
1304 INIT_LIST_HEAD(&td->io_hist_list);
1305 INIT_LIST_HEAD(&td->io_log_list);
1310 if (fio_setaffinity(td) == -1) {
1311 td_verror(td, errno);
1322 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1323 td_verror(td, errno);
1328 if (nice(td->nice) < 0) {
1329 td_verror(td, errno);
1333 if (init_random_state(td))
1336 if (td->ioscheduler && switch_ioscheduler(td))
1339 td_set_runstate(td, TD_INITIALIZED);
1340 fio_sem_up(&startup_sem);
1341 fio_sem_down(&td->mutex);
1343 if (!td->create_serialize && setup_file(td))
1346 gettimeofday(&td->epoch, NULL);
1348 if (td->exec_prerun)
1349 system(td->exec_prerun);
1351 while (td->loops--) {
1352 getrusage(RUSAGE_SELF, &td->ru_start);
1353 gettimeofday(&td->start, NULL);
1354 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1357 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1360 prune_io_piece_log(td);
1364 td->runtime[td->ddir] += mtime_since_now(&td->start);
1365 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1366 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1368 update_rusage_stat(td);
1370 if (td->error || td->terminate)
1373 if (td->verify == VERIFY_NONE)
1377 gettimeofday(&td->start, NULL);
1381 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1383 if (td->error || td->terminate)
1388 finish_log(td, td->bw_log, "bw");
1390 finish_log(td, td->slat_log, "slat");
1392 finish_log(td, td->clat_log, "clat");
1393 if (td->write_iolog)
1394 write_iolog_close(td);
1395 if (td->exec_postrun)
1396 system(td->exec_postrun);
1398 if (exitall_on_terminate)
1399 terminate_threads(td->groupid);
1407 munmap(td->mmap, td->file_size);
1410 td_set_runstate(td, TD_EXITED);
1415 static void *fork_main(int shmid, int offset)
1417 struct thread_data *td;
1420 data = shmat(shmid, NULL, 0);
1421 if (data == (void *) -1) {
1426 td = data + offset * sizeof(struct thread_data);
1432 static void check_str_update(struct thread_data *td)
1434 char c = run_str[td->thread_number - 1];
1436 switch (td->runstate) {
1449 } else if (td_read(td)) {
1470 case TD_INITIALIZED:
1473 case TD_NOT_CREATED:
1477 log_err("state %d\n", td->runstate);
1480 run_str[td->thread_number - 1] = c;
1483 static void eta_to_str(char *str, int eta_sec)
1485 unsigned int d, h, m, s;
1486 static int always_d, always_h;
1498 if (d || always_d) {
1500 str += sprintf(str, "%02dd:", d);
1502 if (h || always_h) {
1504 str += sprintf(str, "%02dh:", h);
1507 str += sprintf(str, "%02dm:", m);
1508 str += sprintf(str, "%02ds", s);
1511 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1513 unsigned long long bytes_total, bytes_done;
1514 unsigned int eta_sec = 0;
1516 bytes_total = td->total_io_size;
1519 * if writing, bytes_total will be twice the size. If mixing,
1520 * assume a 50/50 split and thus bytes_total will be 50% larger.
1524 bytes_total = bytes_total * 3 / 2;
1528 if (td->zone_size && td->zone_skip)
1529 bytes_total /= (td->zone_skip / td->zone_size);
1531 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1534 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1535 perc = (double) bytes_done / (double) bytes_total;
1539 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1541 if (td->timeout && eta_sec > (td->timeout - elapsed))
1542 eta_sec = td->timeout - elapsed;
1543 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1544 || td->runstate == TD_INITIALIZED) {
1545 int t_eta = 0, r_eta = 0;
1548 * We can only guess - assume it'll run the full timeout
1549 * if given, otherwise assume it'll run at the specified rate.
1552 t_eta = td->timeout + td->start_delay - elapsed;
1554 r_eta = (bytes_total / 1024) / td->rate;
1555 r_eta += td->start_delay - elapsed;
1559 eta_sec = min(r_eta, t_eta);
1568 * thread is already done or waiting for fsync
1576 static void print_thread_status(void)
1578 unsigned long elapsed = time_since_now(&genesis);
1579 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
1583 if (temp_stall_ts || terse_output)
1586 eta_secs = malloc(thread_number * sizeof(int));
1587 memset(eta_secs, 0, thread_number * sizeof(int));
1589 nr_pending = nr_running = t_rate = m_rate = 0;
1590 for (i = 0; i < thread_number; i++) {
1591 struct thread_data *td = &threads[i];
1593 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1594 td->runstate == TD_FSYNCING) {
1597 m_rate += td->ratemin;
1598 } else if (td->runstate < TD_RUNNING)
1602 eta_secs[i] = thread_eta(td, elapsed);
1604 eta_secs[i] = INT_MAX;
1606 check_str_update(td);
1609 if (exitall_on_terminate)
1614 for (i = 0; i < thread_number; i++) {
1615 if (exitall_on_terminate) {
1616 if (eta_secs[i] < eta_sec)
1617 eta_sec = eta_secs[i];
1619 if (eta_secs[i] > eta_sec)
1620 eta_sec = eta_secs[i];
1624 if (eta_sec != INT_MAX && elapsed) {
1625 perc = (double) elapsed / (double) (elapsed + eta_sec);
1626 eta_to_str(eta_str, eta_sec);
1629 if (!nr_running && !nr_pending)
1632 printf("Threads running: %d", nr_running);
1633 if (m_rate || t_rate)
1634 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1635 if (eta_sec != INT_MAX) {
1637 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1644 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1649 * reap exited threads (TD_EXITED -> TD_REAPED)
1651 for (i = 0; i < thread_number; i++) {
1652 struct thread_data *td = &threads[i];
1654 if (td->runstate != TD_EXITED)
1657 td_set_runstate(td, TD_REAPED);
1659 if (td->use_thread) {
1662 if (pthread_join(td->thread, (void *) &ret))
1663 perror("thread_join");
1665 waitpid(td->pid, NULL, 0);
1668 (*m_rate) -= td->ratemin;
1669 (*t_rate) -= td->rate;
1673 static void fio_unpin_memory(void *pinned)
1676 if (munlock(pinned, mlock_size) < 0)
1678 munmap(pinned, mlock_size);
1682 static void *fio_pin_memory(void)
1684 unsigned long long phys_mem;
1691 * Don't allow mlock of more than real_mem-128MB
1693 phys_mem = os_phys_mem();
1695 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1696 mlock_size = phys_mem - 128 * 1024 * 1024;
1697 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1701 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1703 perror("malloc locked mem");
1706 if (mlock(ptr, mlock_size) < 0) {
1707 munmap(ptr, mlock_size);
1715 static void run_threads(void)
1717 struct thread_data *td;
1718 unsigned long spent;
1719 int i, todo, nr_running, m_rate, t_rate, nr_started;
1722 mlocked_mem = fio_pin_memory();
1724 if (!terse_output) {
1725 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1729 signal(SIGINT, sig_handler);
1730 signal(SIGALRM, sig_handler);
1732 todo = thread_number;
1735 m_rate = t_rate = 0;
1737 for (i = 0; i < thread_number; i++) {
1740 run_str[td->thread_number - 1] = 'P';
1744 if (!td->create_serialize)
1748 * do file setup here so it happens sequentially,
1749 * we don't want X number of threads getting their
1750 * client data interspersed on disk
1752 if (setup_file(td)) {
1753 td_set_runstate(td, TD_REAPED);
1758 gettimeofday(&genesis, NULL);
1761 struct thread_data *map[MAX_JOBS];
1762 struct timeval this_start;
1763 int this_jobs = 0, left;
1766 * create threads (TD_NOT_CREATED -> TD_CREATED)
1768 for (i = 0; i < thread_number; i++) {
1771 if (td->runstate != TD_NOT_CREATED)
1775 * never got a chance to start, killed by other
1776 * thread for some reason
1778 if (td->terminate) {
1783 if (td->start_delay) {
1784 spent = mtime_since_now(&genesis);
1786 if (td->start_delay * 1000 > spent)
1790 if (td->stonewall && (nr_started || nr_running))
1794 * Set state to created. Thread will transition
1795 * to TD_INITIALIZED when it's done setting up.
1797 td_set_runstate(td, TD_CREATED);
1798 map[this_jobs++] = td;
1799 fio_sem_init(&startup_sem, 1);
1802 if (td->use_thread) {
1803 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1804 perror("thread_create");
1809 fio_sem_down(&startup_sem);
1811 fork_main(shm_id, i);
1818 * Wait for the started threads to transition to
1821 gettimeofday(&this_start, NULL);
1824 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1829 for (i = 0; i < this_jobs; i++) {
1833 if (td->runstate == TD_INITIALIZED) {
1836 } else if (td->runstate >= TD_EXITED) {
1840 nr_running++; /* work-around... */
1846 log_err("fio: %d jobs failed to start\n", left);
1847 for (i = 0; i < this_jobs; i++) {
1851 kill(td->pid, SIGTERM);
1857 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1859 for (i = 0; i < thread_number; i++) {
1862 if (td->runstate != TD_INITIALIZED)
1865 td_set_runstate(td, TD_RUNNING);
1868 m_rate += td->ratemin;
1871 fio_sem_up(&td->mutex);
1874 reap_threads(&nr_running, &t_rate, &m_rate);
1880 while (nr_running) {
1881 reap_threads(&nr_running, &t_rate, &m_rate);
1886 fio_unpin_memory(mlocked_mem);
1889 int main(int argc, char *argv[])
1891 if (parse_options(argc, argv))
1894 if (!thread_number) {
1895 log_err("Nothing to do\n");
1899 disk_util_timer_arm();