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;
47 static void print_thread_status(void);
49 extern unsigned long long mlock_size;
65 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
67 static volatile int startup_sem;
69 #define TERMINATE_ALL (-1)
70 #define JOB_START_TIMEOUT (5 * 1000)
72 static void terminate_threads(int group_id)
76 for (i = 0; i < thread_number; i++) {
77 struct thread_data *td = &threads[i];
79 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
86 static void sig_handler(int sig)
91 disk_util_timer_arm();
92 print_thread_status();
95 printf("\nfio: terminating on signal\n");
97 terminate_threads(TERMINATE_ALL);
102 static int random_map_free(struct thread_data *td, unsigned long long block)
104 unsigned int idx = RAND_MAP_IDX(td, block);
105 unsigned int bit = RAND_MAP_BIT(td, block);
107 return (td->file_map[idx] & (1UL << bit)) == 0;
110 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
116 while ((*b) * td->min_bs < td->io_size) {
117 if (td->file_map[i] != -1UL) {
118 *b += ffz(td->file_map[i]);
122 *b += BLOCKS_PER_MAP;
129 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
131 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
132 unsigned int blocks = 0;
134 while (blocks < (io_u->buflen / td->min_bs)) {
135 unsigned int idx, bit;
137 if (!random_map_free(td, block))
140 idx = RAND_MAP_IDX(td, block);
141 bit = RAND_MAP_BIT(td, block);
143 assert(idx < td->num_maps);
145 td->file_map[idx] |= (1UL << bit);
150 if ((blocks * td->min_bs) < io_u->buflen)
151 io_u->buflen = blocks * td->min_bs;
154 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
156 unsigned long long b, rb;
159 if (!td->sequential) {
160 unsigned long long max_blocks = td->io_size / td->min_bs;
164 r = os_random_long(&td->random_state);
165 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
166 rb = b + (td->file_offset / td->min_bs);
168 } while (!random_map_free(td, rb) && loops);
171 if (get_next_free_block(td, &b))
175 b = td->last_pos / td->min_bs;
177 *offset = (b * td->min_bs) + td->file_offset;
178 if (*offset > td->real_file_size)
184 static unsigned int get_next_buflen(struct thread_data *td)
189 if (td->min_bs == td->max_bs)
192 r = os_random_long(&td->bsrange_state);
193 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
194 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
197 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
198 buflen = td->io_size - td->this_io_bytes[td->ddir];
203 static int check_min_rate(struct thread_data *td, struct timeval *now)
210 * allow a 2 second settle period in the beginning
212 if (mtime_since(&td->start, now) < 2000)
216 * if rate blocks is set, sample is running
218 if (td->rate_bytes) {
219 spent = mtime_since(&td->lastrate, now);
220 if (spent < td->ratecycle)
223 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
224 if (rate < td->ratemin) {
225 printf("%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
227 terminate_threads(td->groupid);
232 td->rate_bytes = td->this_io_bytes[ddir];
233 memcpy(&td->lastrate, now, sizeof(*now));
237 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
241 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
247 static void fill_random_bytes(struct thread_data *td,
248 unsigned char *p, unsigned int len)
254 r = os_random_double(&td->verify_state);
257 * lrand48_r seems to be broken and only fill the bottom
258 * 32-bits, even on 64-bit archs with 64-bit longs
271 static void hexdump(void *buffer, int len)
273 unsigned char *p = buffer;
276 for (i = 0; i < len; i++)
277 printf("%02x", p[i]);
281 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
283 unsigned char *p = (unsigned char *) io_u->buf;
287 c = crc32(p, hdr->len - sizeof(*hdr));
289 if (c != hdr->crc32) {
290 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
291 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
298 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
300 unsigned char *p = (unsigned char *) io_u->buf;
301 struct md5_ctx md5_ctx;
303 memset(&md5_ctx, 0, sizeof(md5_ctx));
305 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
307 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
308 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
309 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
310 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
317 static int verify_io_u(struct io_u *io_u)
319 struct verify_header *hdr = (struct verify_header *) io_u->buf;
322 if (hdr->fio_magic != FIO_HDR_MAGIC)
325 if (hdr->verify_type == VERIFY_MD5)
326 ret = verify_io_u_md5(hdr, io_u);
327 else if (hdr->verify_type == VERIFY_CRC32)
328 ret = verify_io_u_crc32(hdr, io_u);
330 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
337 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
339 hdr->crc32 = crc32(p, len);
342 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
344 struct md5_ctx md5_ctx;
346 memset(&md5_ctx, 0, sizeof(md5_ctx));
347 md5_update(&md5_ctx, p, len);
348 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
351 static int get_rw_ddir(struct thread_data *td)
355 unsigned long elapsed;
357 gettimeofday(&now, NULL);
358 elapsed = mtime_since_now(&td->rwmix_switch);
361 * Check if it's time to seed a new data direction.
363 if (elapsed >= td->rwmixcycle) {
367 r = os_random_long(&td->rwmix_state);
368 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
369 if (v < td->rwmixread)
370 td->rwmix_ddir = DDIR_READ;
372 td->rwmix_ddir = DDIR_WRITE;
373 memcpy(&td->rwmix_switch, &now, sizeof(now));
375 return td->rwmix_ddir;
376 } else if (td_read(td))
383 * fill body of io_u->buf with random data and add a header with the
384 * crc32 or md5 sum of that data.
386 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
388 unsigned char *p = (unsigned char *) io_u->buf;
389 struct verify_header hdr;
391 hdr.fio_magic = FIO_HDR_MAGIC;
392 hdr.len = io_u->buflen;
394 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
396 if (td->verify == VERIFY_MD5) {
397 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
398 hdr.verify_type = VERIFY_MD5;
400 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
401 hdr.verify_type = VERIFY_CRC32;
404 memcpy(io_u->buf, &hdr, sizeof(hdr));
407 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
409 if (td->io_prep && td->io_prep(td, io_u))
415 void put_io_u(struct thread_data *td, struct io_u *io_u)
417 list_del(&io_u->list);
418 list_add(&io_u->list, &td->io_u_freelist);
422 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
425 * If using an iolog, grab next piece if any available.
428 return read_iolog_get(td, io_u);
431 * No log, let the seq/rand engine retrieve the next position.
433 if (!get_next_offset(td, &io_u->offset)) {
434 io_u->buflen = get_next_buflen(td);
437 io_u->ddir = get_rw_ddir(td);
440 * If using a write iolog, store this entry.
443 write_iolog_put(td, io_u);
452 #define queue_full(td) list_empty(&(td)->io_u_freelist)
454 struct io_u *__get_io_u(struct thread_data *td)
456 struct io_u *io_u = NULL;
458 if (!queue_full(td)) {
459 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
463 list_del(&io_u->list);
464 list_add(&io_u->list, &td->io_u_busylist);
471 static struct io_u *get_io_u(struct thread_data *td)
475 io_u = __get_io_u(td);
479 if (td->zone_bytes >= td->zone_size) {
481 td->last_pos += td->zone_skip;
484 if (fill_io_u(td, io_u)) {
489 if (io_u->buflen + io_u->offset > td->real_file_size)
490 io_u->buflen = td->real_file_size - io_u->offset;
497 if (!td->read_iolog && !td->sequential)
498 mark_random_map(td, io_u);
500 td->last_pos += io_u->buflen;
502 if (td->verify != VERIFY_NONE)
503 populate_io_u(td, io_u);
505 if (td_io_prep(td, io_u)) {
510 gettimeofday(&io_u->start_time, NULL);
514 static inline void td_set_runstate(struct thread_data *td, int runstate)
516 td->old_runstate = td->runstate;
517 td->runstate = runstate;
520 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
522 struct io_piece *ipo;
524 if (!list_empty(&td->io_hist_list)) {
525 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
527 list_del(&ipo->list);
529 io_u->offset = ipo->offset;
530 io_u->buflen = ipo->len;
531 io_u->ddir = DDIR_READ;
539 static int sync_td(struct thread_data *td)
542 return td->io_sync(td);
547 static int io_u_getevents(struct thread_data *td, int min, int max,
550 return td->io_getevents(td, min, max, t);
553 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
555 gettimeofday(&io_u->issue_time, NULL);
557 return td->io_queue(td, io_u);
560 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
562 static void io_completed(struct thread_data *td, struct io_u *io_u,
563 struct io_completion_data *icd)
568 gettimeofday(&e, NULL);
571 unsigned int bytes = io_u->buflen - io_u->resid;
572 const int idx = io_u->ddir;
574 td->io_blocks[idx]++;
575 td->io_bytes[idx] += bytes;
576 td->zone_bytes += bytes;
577 td->this_io_bytes[idx] += bytes;
579 msec = mtime_since(&io_u->issue_time, &e);
581 add_clat_sample(td, idx, msec);
582 add_bw_sample(td, idx);
584 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
585 log_io_piece(td, io_u);
587 icd->bytes_done[idx] += bytes;
589 icd->error = io_u->error;
592 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
598 icd->bytes_done[0] = icd->bytes_done[1] = 0;
600 for (i = 0; i < icd->nr; i++) {
601 io_u = td->io_event(td, i);
603 io_completed(td, io_u, icd);
608 static void cleanup_pending_aio(struct thread_data *td)
610 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
611 struct list_head *entry, *n;
612 struct io_completion_data icd;
617 * get immediately available events, if any
619 r = io_u_getevents(td, 0, td->cur_depth, &ts);
622 ios_completed(td, &icd);
626 * now cancel remaining active events
629 list_for_each_safe(entry, n, &td->io_u_busylist) {
630 io_u = list_entry(entry, struct io_u, list);
632 r = td->io_cancel(td, io_u);
639 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
642 ios_completed(td, &icd);
647 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
649 struct io_u *v_io_u = *io_u;
653 ret = verify_io_u(v_io_u);
654 put_io_u(td, v_io_u);
661 static void do_verify(struct thread_data *td)
664 struct io_u *io_u, *v_io_u = NULL;
665 struct io_completion_data icd;
668 td_set_runstate(td, TD_VERIFYING);
674 gettimeofday(&t, NULL);
675 if (runtime_exceeded(td, &t))
678 io_u = __get_io_u(td);
682 if (get_next_verify(td, io_u)) {
687 if (td_io_prep(td, io_u)) {
692 ret = io_u_queue(td, io_u);
700 * we have one pending to verify, do that while
701 * we are doing io on the next one
703 if (do_io_u_verify(td, &v_io_u))
706 ret = io_u_getevents(td, 1, 1, NULL);
713 v_io_u = td->io_event(td, 0);
716 io_completed(td, v_io_u, &icd);
719 td_verror(td, icd.error);
720 put_io_u(td, v_io_u);
726 * if we can't submit more io, we need to verify now
728 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
733 do_io_u_verify(td, &v_io_u);
736 cleanup_pending_aio(td);
738 td_set_runstate(td, TD_RUNNING);
742 * Main IO worker functions. It retrieves io_u's to process and queues
743 * and reaps them, checking for rate and errors along the way.
745 static void do_io(struct thread_data *td)
747 struct io_completion_data icd;
751 td_set_runstate(td, TD_RUNNING);
753 while (td->this_io_bytes[td->ddir] < td->io_size) {
754 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
755 struct timespec *timeout;
756 int ret, min_evts = 0;
766 memcpy(&s, &io_u->start_time, sizeof(s));
768 ret = io_u_queue(td, io_u);
775 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
777 if (td->cur_depth < td->iodepth) {
785 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
793 ios_completed(td, &icd);
795 td_verror(td, icd.error);
800 * the rate is batched for now, it should work for batches
801 * of completions except the very first one which may look
804 gettimeofday(&e, NULL);
805 usec = utime_since(&s, &e);
807 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
809 if (check_min_rate(td, &e)) {
810 td_verror(td, ENOMEM);
814 if (runtime_exceeded(td, &e))
818 usec_sleep(td, td->thinktime);
820 if (should_fsync(td) && td->fsync_blocks &&
821 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
826 cleanup_pending_aio(td);
828 if (should_fsync(td) && td->end_fsync) {
829 td_set_runstate(td, TD_FSYNCING);
834 static void cleanup_io(struct thread_data *td)
840 static int init_io(struct thread_data *td)
842 if (td->io_engine == FIO_SYNCIO)
843 return fio_syncio_init(td);
844 else if (td->io_engine == FIO_MMAPIO)
845 return fio_mmapio_init(td);
846 else if (td->io_engine == FIO_LIBAIO)
847 return fio_libaio_init(td);
848 else if (td->io_engine == FIO_POSIXAIO)
849 return fio_posixaio_init(td);
850 else if (td->io_engine == FIO_SGIO)
851 return fio_sgio_init(td);
852 else if (td->io_engine == FIO_SPLICEIO)
853 return fio_spliceio_init(td);
855 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
860 static void cleanup_io_u(struct thread_data *td)
862 struct list_head *entry, *n;
865 list_for_each_safe(entry, n, &td->io_u_freelist) {
866 io_u = list_entry(entry, struct io_u, list);
868 list_del(&io_u->list);
872 if (td->mem_type == MEM_MALLOC)
873 free(td->orig_buffer);
874 else if (td->mem_type == MEM_SHM) {
875 struct shmid_ds sbuf;
877 shmdt(td->orig_buffer);
878 shmctl(td->shm_id, IPC_RMID, &sbuf);
879 } else if (td->mem_type == MEM_MMAP)
880 munmap(td->orig_buffer, td->orig_buffer_size);
882 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
884 td->orig_buffer = NULL;
887 static int init_io_u(struct thread_data *td)
893 if (td->io_engine & FIO_SYNCIO)
896 max_units = td->iodepth;
898 td->orig_buffer_size = td->max_bs * max_units + MASK;
900 if (td->mem_type == MEM_MALLOC)
901 td->orig_buffer = malloc(td->orig_buffer_size);
902 else if (td->mem_type == MEM_SHM) {
903 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
904 if (td->shm_id < 0) {
905 td_verror(td, errno);
910 td->orig_buffer = shmat(td->shm_id, NULL, 0);
911 if (td->orig_buffer == (void *) -1) {
912 td_verror(td, errno);
914 td->orig_buffer = NULL;
917 } else if (td->mem_type == MEM_MMAP) {
918 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
919 if (td->orig_buffer == MAP_FAILED) {
920 td_verror(td, errno);
922 td->orig_buffer = NULL;
927 p = ALIGN(td->orig_buffer);
928 for (i = 0; i < max_units; i++) {
929 io_u = malloc(sizeof(*io_u));
930 memset(io_u, 0, sizeof(*io_u));
931 INIT_LIST_HEAD(&io_u->list);
933 io_u->buf = p + td->max_bs * i;
935 list_add(&io_u->list, &td->io_u_freelist);
941 static int create_file(struct thread_data *td, unsigned long long size,
944 unsigned long long left;
950 * unless specifically asked for overwrite, let normal io extend it
952 if (td_write(td) && !td->overwrite)
956 fprintf(stderr, "Need size for create\n");
957 td_verror(td, EINVAL);
962 oflags = O_CREAT | O_TRUNC;
963 printf("%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
966 printf("%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
969 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
971 td_verror(td, errno);
975 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
976 td_verror(td, errno);
980 td->io_size = td->file_size;
981 b = malloc(td->max_bs);
982 memset(b, 0, td->max_bs);
985 while (left && !td->terminate) {
990 r = write(td->fd, b, bs);
997 td_verror(td, errno);
1006 unlink(td->file_name);
1007 else if (td->create_fsync)
1016 static int file_size(struct thread_data *td)
1020 if (fstat(td->fd, &st) == -1) {
1021 td_verror(td, errno);
1025 td->real_file_size = st.st_size;
1027 if (!td->file_size || td->file_size > td->real_file_size)
1028 td->file_size = td->real_file_size;
1030 td->file_size -= td->file_offset;
1034 static int bdev_size(struct thread_data *td)
1036 unsigned long long bytes;
1039 r = blockdev_size(td->fd, &bytes);
1045 td->real_file_size = bytes;
1048 * no extend possibilities, so limit size to device size if too large
1050 if (!td->file_size || td->file_size > td->real_file_size)
1051 td->file_size = td->real_file_size;
1053 td->file_size -= td->file_offset;
1057 static int get_file_size(struct thread_data *td)
1061 if (td->filetype == FIO_TYPE_FILE)
1062 ret = file_size(td);
1063 else if (td->filetype == FIO_TYPE_BD)
1064 ret = bdev_size(td);
1066 td->real_file_size = -1;
1071 if (td->file_offset > td->real_file_size) {
1072 fprintf(stderr, "%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1076 td->io_size = td->file_size;
1077 if (td->io_size == 0) {
1078 fprintf(stderr, "%s: no io blocks\n", td->name);
1079 td_verror(td, EINVAL);
1084 td->zone_size = td->io_size;
1086 td->total_io_size = td->io_size * td->loops;
1090 static int setup_file_mmap(struct thread_data *td)
1095 flags = PROT_READ | PROT_WRITE;
1096 else if (td_write(td)) {
1099 if (td->verify != VERIFY_NONE)
1104 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1105 if (td->mmap == MAP_FAILED) {
1107 td_verror(td, errno);
1111 if (td->invalidate_cache) {
1112 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1113 td_verror(td, errno);
1118 if (td->sequential) {
1119 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1120 td_verror(td, errno);
1124 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1125 td_verror(td, errno);
1133 static int setup_file_plain(struct thread_data *td)
1135 if (td->invalidate_cache) {
1136 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1137 td_verror(td, errno);
1142 if (td->sequential) {
1143 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1144 td_verror(td, errno);
1148 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1149 td_verror(td, errno);
1157 static int setup_file(struct thread_data *td)
1162 if (stat(td->file_name, &st) == -1) {
1163 if (errno != ENOENT) {
1164 td_verror(td, errno);
1167 if (!td->create_file) {
1168 td_verror(td, ENOENT);
1171 if (create_file(td, td->file_size, 0))
1173 } else if (td->filetype == FIO_TYPE_FILE) {
1174 if (st.st_size < (off_t) td->file_size) {
1175 if (create_file(td, td->file_size - st.st_size, 1))
1181 flags |= OS_O_DIRECT;
1183 if (td_write(td) || td_rw(td)) {
1184 if (td->filetype == FIO_TYPE_FILE) {
1195 td->fd = open(td->file_name, flags, 0600);
1197 if (td->filetype == FIO_TYPE_CHAR)
1202 td->fd = open(td->file_name, flags);
1206 td_verror(td, errno);
1210 if (get_file_size(td))
1213 if (td->io_engine != FIO_MMAPIO)
1214 return setup_file_plain(td);
1216 return setup_file_mmap(td);
1219 static int switch_ioscheduler(struct thread_data *td)
1221 char tmp[256], tmp2[128];
1225 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1227 f = fopen(tmp, "r+");
1229 td_verror(td, errno);
1236 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1237 if (ferror(f) || ret != 1) {
1238 td_verror(td, errno);
1246 * Read back and check that the selected scheduler is now the default.
1248 ret = fread(tmp, 1, sizeof(tmp), f);
1249 if (ferror(f) || ret < 0) {
1250 td_verror(td, errno);
1255 sprintf(tmp2, "[%s]", td->ioscheduler);
1256 if (!strstr(tmp, tmp2)) {
1257 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1258 td_verror(td, EINVAL);
1267 static void clear_io_state(struct thread_data *td)
1269 if (td->io_engine == FIO_SYNCIO)
1270 lseek(td->fd, SEEK_SET, 0);
1273 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1274 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1278 memset(td->file_map, 0, td->num_maps * sizeof(long));
1281 static void *thread_main(void *data)
1283 struct thread_data *td = data;
1285 if (!td->use_thread)
1290 INIT_LIST_HEAD(&td->io_u_freelist);
1291 INIT_LIST_HEAD(&td->io_u_busylist);
1292 INIT_LIST_HEAD(&td->io_hist_list);
1293 INIT_LIST_HEAD(&td->io_log_list);
1298 if (fio_setaffinity(td) == -1) {
1299 td_verror(td, errno);
1310 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1311 td_verror(td, errno);
1316 if (nice(td->nice) < 0) {
1317 td_verror(td, errno);
1321 if (init_random_state(td))
1324 if (td->ioscheduler && switch_ioscheduler(td))
1327 td_set_runstate(td, TD_INITIALIZED);
1328 fio_sem_up(&startup_sem);
1329 fio_sem_down(&td->mutex);
1331 if (!td->create_serialize && setup_file(td))
1334 gettimeofday(&td->epoch, NULL);
1336 if (td->exec_prerun)
1337 system(td->exec_prerun);
1339 while (td->loops--) {
1340 getrusage(RUSAGE_SELF, &td->ru_start);
1341 gettimeofday(&td->start, NULL);
1342 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1345 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1348 prune_io_piece_log(td);
1352 td->runtime[td->ddir] += mtime_since_now(&td->start);
1353 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1354 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1356 update_rusage_stat(td);
1358 if (td->error || td->terminate)
1361 if (td->verify == VERIFY_NONE)
1365 gettimeofday(&td->start, NULL);
1369 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1371 if (td->error || td->terminate)
1376 finish_log(td, td->bw_log, "bw");
1378 finish_log(td, td->slat_log, "slat");
1380 finish_log(td, td->clat_log, "clat");
1381 if (td->write_iolog)
1382 write_iolog_close(td);
1383 if (td->exec_postrun)
1384 system(td->exec_postrun);
1386 if (exitall_on_terminate)
1387 terminate_threads(td->groupid);
1395 munmap(td->mmap, td->file_size);
1398 td_set_runstate(td, TD_EXITED);
1403 static void *fork_main(int shmid, int offset)
1405 struct thread_data *td;
1408 data = shmat(shmid, NULL, 0);
1409 if (data == (void *) -1) {
1414 td = data + offset * sizeof(struct thread_data);
1420 static void check_str_update(struct thread_data *td)
1422 char c = run_str[td->thread_number - 1];
1424 if (td->runstate == td->old_runstate)
1427 switch (td->runstate) {
1440 } else if (td_read(td)) {
1461 case TD_INITIALIZED:
1464 case TD_NOT_CREATED:
1468 printf("state %d\n", td->runstate);
1471 run_str[td->thread_number - 1] = c;
1472 td->old_runstate = td->runstate;
1475 static void eta_to_str(char *str, int eta_sec)
1477 unsigned int d, h, m, s;
1478 static int always_d, always_h;
1490 if (d || always_d) {
1492 str += sprintf(str, "%02dd:", d);
1494 if (h || always_h) {
1496 str += sprintf(str, "%02dh:", h);
1499 str += sprintf(str, "%02dm:", m);
1500 str += sprintf(str, "%02ds", s);
1503 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1505 unsigned long long bytes_total, bytes_done;
1506 unsigned int eta_sec = 0;
1508 bytes_total = td->total_io_size;
1511 * if writing, bytes_total will be twice the size. If mixing,
1512 * assume a 50/50 split and thus bytes_total will be 50% larger.
1516 bytes_total = bytes_total * 3 / 2;
1520 if (td->zone_size && td->zone_skip)
1521 bytes_total /= (td->zone_skip / td->zone_size);
1523 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1526 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1527 perc = (double) bytes_done / (double) bytes_total;
1531 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1533 if (td->timeout && eta_sec > (td->timeout - elapsed))
1534 eta_sec = td->timeout - elapsed;
1535 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1536 || td->runstate == TD_INITIALIZED) {
1537 int t_eta = 0, r_eta = 0;
1540 * We can only guess - assume it'll run the full timeout
1541 * if given, otherwise assume it'll run at the specified rate.
1544 t_eta = td->timeout + td->start_delay - elapsed;
1546 r_eta = (bytes_total / 1024) / td->rate;
1547 r_eta += td->start_delay - elapsed;
1551 eta_sec = min(r_eta, t_eta);
1560 * thread is already done or waiting for fsync
1568 static void print_thread_status(void)
1570 unsigned long elapsed = time_since_now(&genesis);
1571 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
1575 eta_secs = malloc(thread_number * sizeof(int));
1576 memset(eta_secs, 0, thread_number * sizeof(int));
1578 nr_running = t_rate = m_rate = 0;
1579 for (i = 0; i < thread_number; i++) {
1580 struct thread_data *td = &threads[i];
1582 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1583 td->runstate == TD_FSYNCING) {
1586 m_rate += td->ratemin;
1590 eta_secs[i] = thread_eta(td, elapsed);
1592 eta_secs[i] = INT_MAX;
1594 check_str_update(td);
1597 if (exitall_on_terminate)
1602 for (i = 0; i < thread_number; i++) {
1603 if (exitall_on_terminate) {
1604 if (eta_secs[i] < eta_sec)
1605 eta_sec = eta_secs[i];
1607 if (eta_secs[i] > eta_sec)
1608 eta_sec = eta_secs[i];
1612 if (eta_sec != INT_MAX && elapsed) {
1613 perc = (double) elapsed / (double) (elapsed + eta_sec);
1614 eta_to_str(eta_str, eta_sec);
1617 printf("Threads now running (%d)", nr_running);
1618 if (m_rate || t_rate)
1619 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1620 if (eta_sec != INT_MAX) {
1622 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1629 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1634 * reap exited threads (TD_EXITED -> TD_REAPED)
1636 for (i = 0; i < thread_number; i++) {
1637 struct thread_data *td = &threads[i];
1639 if (td->runstate != TD_EXITED)
1642 td_set_runstate(td, TD_REAPED);
1644 if (td->use_thread) {
1647 if (pthread_join(td->thread, (void *) &ret))
1648 perror("thread_join");
1650 waitpid(td->pid, NULL, 0);
1653 (*m_rate) -= td->ratemin;
1654 (*t_rate) -= td->rate;
1658 static void fio_unpin_memory(void *pinned)
1661 if (munlock(pinned, mlock_size) < 0)
1663 munmap(pinned, mlock_size);
1667 static void *fio_pin_memory(void)
1669 unsigned long long phys_mem;
1676 * Don't allow mlock of more than real_mem-128MB
1678 phys_mem = os_phys_mem();
1680 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1681 mlock_size = phys_mem - 128 * 1024 * 1024;
1682 printf("fio: limiting mlocked memory to %lluMiB\n",
1687 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1689 perror("malloc locked mem");
1692 if (mlock(ptr, mlock_size) < 0) {
1693 munmap(ptr, mlock_size);
1701 static void run_threads(void)
1703 struct thread_data *td;
1704 unsigned long spent;
1705 int i, todo, nr_running, m_rate, t_rate, nr_started;
1708 mlocked_mem = fio_pin_memory();
1710 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1713 signal(SIGINT, sig_handler);
1714 signal(SIGALRM, sig_handler);
1716 todo = thread_number;
1719 m_rate = t_rate = 0;
1721 for (i = 0; i < thread_number; i++) {
1724 run_str[td->thread_number - 1] = 'P';
1728 if (!td->create_serialize)
1732 * do file setup here so it happens sequentially,
1733 * we don't want X number of threads getting their
1734 * client data interspersed on disk
1736 if (setup_file(td)) {
1737 td_set_runstate(td, TD_REAPED);
1742 gettimeofday(&genesis, NULL);
1745 struct thread_data *map[MAX_JOBS];
1746 struct timeval this_start;
1747 int this_jobs = 0, left;
1750 * create threads (TD_NOT_CREATED -> TD_CREATED)
1752 for (i = 0; i < thread_number; i++) {
1755 if (td->runstate != TD_NOT_CREATED)
1759 * never got a chance to start, killed by other
1760 * thread for some reason
1762 if (td->terminate) {
1767 if (td->start_delay) {
1768 spent = mtime_since_now(&genesis);
1770 if (td->start_delay * 1000 > spent)
1774 if (td->stonewall && (nr_started || nr_running))
1778 * Set state to created. Thread will transition
1779 * to TD_INITIALIZED when it's done setting up.
1781 td_set_runstate(td, TD_CREATED);
1782 map[this_jobs++] = td;
1783 fio_sem_init(&startup_sem, 1);
1786 if (td->use_thread) {
1787 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1788 perror("thread_create");
1793 fio_sem_down(&startup_sem);
1795 fork_main(shm_id, i);
1802 * Wait for the started threads to transition to
1805 printf("fio: Waiting for threads to initialize...\n");
1806 gettimeofday(&this_start, NULL);
1809 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1814 for (i = 0; i < this_jobs; i++) {
1818 if (td->runstate == TD_INITIALIZED) {
1821 } else if (td->runstate >= TD_EXITED) {
1825 nr_running++; /* work-around... */
1831 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1832 for (i = 0; i < this_jobs; i++) {
1836 kill(td->pid, SIGTERM);
1842 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1844 printf("fio: Go for launch\n");
1845 for (i = 0; i < thread_number; i++) {
1848 if (td->runstate != TD_INITIALIZED)
1851 td_set_runstate(td, TD_RUNNING);
1854 m_rate += td->ratemin;
1857 fio_sem_up(&td->mutex);
1860 reap_threads(&nr_running, &t_rate, &m_rate);
1866 while (nr_running) {
1867 reap_threads(&nr_running, &t_rate, &m_rate);
1872 fio_unpin_memory(mlocked_mem);
1875 int main(int argc, char *argv[])
1877 if (parse_options(argc, argv))
1880 if (!thread_number) {
1881 printf("Nothing to do\n");
1885 disk_util_timer_arm();