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;
64 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
66 static sem_t startup_sem;
68 #define TERMINATE_ALL (-1)
69 #define JOB_START_TIMEOUT (5 * 1000)
71 static void terminate_threads(int group_id)
75 for (i = 0; i < thread_number; i++) {
76 struct thread_data *td = &threads[i];
78 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
85 static void sig_handler(int sig)
90 disk_util_timer_arm();
91 print_thread_status();
94 printf("\nfio: terminating on signal\n");
96 terminate_threads(TERMINATE_ALL);
101 static int random_map_free(struct thread_data *td, unsigned long long block)
103 unsigned int idx = RAND_MAP_IDX(td, block);
104 unsigned int bit = RAND_MAP_BIT(td, block);
106 return (td->file_map[idx] & (1UL << bit)) == 0;
109 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
115 while ((*b) * td->min_bs < td->io_size) {
116 if (td->file_map[i] != -1UL) {
117 *b += ffz(td->file_map[i]);
121 *b += BLOCKS_PER_MAP;
128 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
130 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
131 unsigned int blocks = 0;
133 while (blocks < (io_u->buflen / td->min_bs)) {
134 unsigned int idx, bit;
136 if (!random_map_free(td, block))
139 idx = RAND_MAP_IDX(td, block);
140 bit = RAND_MAP_BIT(td, block);
142 assert(idx < td->num_maps);
144 td->file_map[idx] |= (1UL << bit);
149 if ((blocks * td->min_bs) < io_u->buflen)
150 io_u->buflen = blocks * td->min_bs;
153 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
155 unsigned long long b, rb;
158 if (!td->sequential) {
159 unsigned long long max_blocks = td->io_size / td->min_bs;
163 lrand48_r(&td->random_state, &r);
164 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
165 rb = b + (td->file_offset / td->min_bs);
167 } while (!random_map_free(td, rb) && loops);
170 if (get_next_free_block(td, &b))
174 b = td->last_pos / td->min_bs;
176 *offset = (b * td->min_bs) + td->file_offset;
177 if (*offset > td->real_file_size)
183 static unsigned int get_next_buflen(struct thread_data *td)
188 if (td->min_bs == td->max_bs)
191 lrand48_r(&td->bsrange_state, &r);
192 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
193 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
196 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
197 buflen = td->io_size - td->this_io_bytes[td->ddir];
202 static int check_min_rate(struct thread_data *td, struct timeval *now)
209 * allow a 2 second settle period in the beginning
211 if (mtime_since(&td->start, now) < 2000)
215 * if rate blocks is set, sample is running
217 if (td->rate_bytes) {
218 spent = mtime_since(&td->lastrate, now);
219 if (spent < td->ratecycle)
222 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
223 if (rate < td->ratemin) {
224 printf("%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
226 terminate_threads(td->groupid);
231 td->rate_bytes = td->this_io_bytes[ddir];
232 memcpy(&td->lastrate, now, sizeof(*now));
236 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
240 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
246 static void fill_random_bytes(struct thread_data *td,
247 unsigned char *p, unsigned int len)
253 drand48_r(&td->verify_state, &r);
256 * lrand48_r seems to be broken and only fill the bottom
257 * 32-bits, even on 64-bit archs with 64-bit longs
270 static void hexdump(void *buffer, int len)
272 unsigned char *p = buffer;
275 for (i = 0; i < len; i++)
276 printf("%02x", p[i]);
280 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
282 unsigned char *p = (unsigned char *) io_u->buf;
287 c = crc32(p, hdr->len - sizeof(*hdr));
288 ret = c != hdr->crc32;
291 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
292 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
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;
304 memset(&md5_ctx, 0, sizeof(md5_ctx));
306 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
308 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
310 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
311 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
312 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
318 static int verify_io_u(struct io_u *io_u)
320 struct verify_header *hdr = (struct verify_header *) io_u->buf;
323 if (hdr->fio_magic != FIO_HDR_MAGIC)
326 if (hdr->verify_type == VERIFY_MD5)
327 ret = verify_io_u_md5(hdr, io_u);
328 else if (hdr->verify_type == VERIFY_CRC32)
329 ret = verify_io_u_crc32(hdr, io_u);
331 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
338 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
340 hdr->crc32 = crc32(p, len);
343 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
345 struct md5_ctx md5_ctx;
347 memset(&md5_ctx, 0, sizeof(md5_ctx));
348 md5_update(&md5_ctx, p, len);
349 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
352 static int get_rw_ddir(struct thread_data *td)
356 unsigned long elapsed;
358 gettimeofday(&now, NULL);
359 elapsed = mtime_since_now(&td->rwmix_switch);
362 * Check if it's time to seed a new data direction.
364 if (elapsed >= td->rwmixcycle) {
368 lrand48_r(&td->random_state, &r);
369 v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
370 if (v < td->rwmixread)
371 td->rwmix_ddir = DDIR_READ;
373 td->rwmix_ddir = DDIR_WRITE;
374 memcpy(&td->rwmix_switch, &now, sizeof(now));
376 return td->rwmix_ddir;
377 } else if (td_read(td))
384 * fill body of io_u->buf with random data and add a header with the
385 * (eg) sha1sum of that data.
387 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
389 unsigned char *p = (unsigned char *) io_u->buf;
390 struct verify_header hdr;
392 hdr.fio_magic = FIO_HDR_MAGIC;
393 hdr.len = io_u->buflen;
395 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
397 if (td->verify == VERIFY_MD5) {
398 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
399 hdr.verify_type = VERIFY_MD5;
401 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
402 hdr.verify_type = VERIFY_CRC32;
405 memcpy(io_u->buf, &hdr, sizeof(hdr));
408 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
410 if (td->io_prep && td->io_prep(td, io_u))
416 void put_io_u(struct thread_data *td, struct io_u *io_u)
418 list_del(&io_u->list);
419 list_add(&io_u->list, &td->io_u_freelist);
423 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
426 * If using an iolog, grab next piece if any available.
429 return read_iolog_get(td, io_u);
432 * No log, let the seq/rand engine retrieve the next position.
434 if (!get_next_offset(td, &io_u->offset)) {
435 io_u->buflen = get_next_buflen(td);
438 io_u->ddir = get_rw_ddir(td);
441 * If using a write iolog, store this entry.
444 write_iolog_put(td, io_u);
453 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
455 struct io_u *__get_io_u(struct thread_data *td)
462 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
465 list_del(&io_u->list);
466 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))
527 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
528 list_del(&ipo->list);
530 io_u->offset = ipo->offset;
531 io_u->buflen = ipo->len;
532 io_u->ddir = DDIR_READ;
537 static int sync_td(struct thread_data *td)
540 return td->io_sync(td);
545 static int io_u_getevents(struct thread_data *td, int min, int max,
548 return td->io_getevents(td, min, max, t);
551 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
553 gettimeofday(&io_u->issue_time, NULL);
555 return td->io_queue(td, io_u);
558 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
560 static void io_completed(struct thread_data *td, struct io_u *io_u,
561 struct io_completion_data *icd)
566 gettimeofday(&e, NULL);
569 unsigned int bytes = io_u->buflen - io_u->resid;
570 const int idx = io_u->ddir;
572 td->io_blocks[idx]++;
573 td->io_bytes[idx] += bytes;
574 td->zone_bytes += bytes;
575 td->this_io_bytes[idx] += bytes;
577 msec = mtime_since(&io_u->issue_time, &e);
579 add_clat_sample(td, idx, msec);
580 add_bw_sample(td, idx);
582 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
583 log_io_piece(td, io_u);
585 icd->bytes_done[idx] += bytes;
587 icd->error = io_u->error;
590 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
596 icd->bytes_done[0] = icd->bytes_done[1] = 0;
598 for (i = 0; i < icd->nr; i++) {
599 io_u = td->io_event(td, i);
601 io_completed(td, io_u, icd);
606 static void cleanup_pending_aio(struct thread_data *td)
608 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
609 struct list_head *entry, *n;
610 struct io_completion_data icd;
615 * get immediately available events, if any
617 r = io_u_getevents(td, 0, td->cur_depth, &ts);
620 ios_completed(td, &icd);
624 * now cancel remaining active events
627 list_for_each_safe(entry, n, &td->io_u_busylist) {
628 io_u = list_entry(entry, struct io_u, list);
630 r = td->io_cancel(td, io_u);
637 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
640 ios_completed(td, &icd);
645 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
647 struct io_u *v_io_u = *io_u;
651 ret = verify_io_u(v_io_u);
652 put_io_u(td, v_io_u);
659 static void do_verify(struct thread_data *td)
662 struct io_u *io_u, *v_io_u = NULL;
663 struct io_completion_data icd;
666 td_set_runstate(td, TD_VERIFYING);
672 gettimeofday(&t, NULL);
673 if (runtime_exceeded(td, &t))
676 io_u = __get_io_u(td);
680 if (get_next_verify(td, io_u)) {
685 if (td_io_prep(td, io_u)) {
690 ret = io_u_queue(td, io_u);
698 * we have one pending to verify, do that while
699 * we are doing io on the next one
701 if (do_io_u_verify(td, &v_io_u))
704 ret = io_u_getevents(td, 1, 1, NULL);
711 v_io_u = td->io_event(td, 0);
714 io_completed(td, v_io_u, &icd);
717 td_verror(td, icd.error);
718 put_io_u(td, v_io_u);
724 * if we can't submit more io, we need to verify now
726 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
731 do_io_u_verify(td, &v_io_u);
734 cleanup_pending_aio(td);
736 td_set_runstate(td, TD_RUNNING);
739 static void do_io(struct thread_data *td)
741 struct io_completion_data icd;
745 while (td->this_io_bytes[td->ddir] < td->io_size) {
746 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
747 struct timespec *timeout;
748 int ret, min_evts = 0;
758 memcpy(&s, &io_u->start_time, sizeof(s));
760 ret = io_u_queue(td, io_u);
767 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
769 if (td->cur_depth < td->iodepth) {
777 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
785 ios_completed(td, &icd);
787 td_verror(td, icd.error);
792 * the rate is batched for now, it should work for batches
793 * of completions except the very first one which may look
796 gettimeofday(&e, NULL);
797 usec = utime_since(&s, &e);
799 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
801 if (check_min_rate(td, &e)) {
802 td_verror(td, ENOMEM);
806 if (runtime_exceeded(td, &e))
810 usec_sleep(td, td->thinktime);
812 if (should_fsync(td) && td->fsync_blocks &&
813 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
818 cleanup_pending_aio(td);
820 if (should_fsync(td) && td->end_fsync)
824 static void cleanup_io(struct thread_data *td)
830 static int init_io(struct thread_data *td)
832 if (td->io_engine == FIO_SYNCIO)
833 return fio_syncio_init(td);
834 else if (td->io_engine == FIO_MMAPIO)
835 return fio_mmapio_init(td);
836 else if (td->io_engine == FIO_LIBAIO)
837 return fio_libaio_init(td);
838 else if (td->io_engine == FIO_POSIXAIO)
839 return fio_posixaio_init(td);
840 else if (td->io_engine == FIO_SGIO)
841 return fio_sgio_init(td);
842 else if (td->io_engine == FIO_SPLICEIO)
843 return fio_spliceio_init(td);
845 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
850 static void cleanup_io_u(struct thread_data *td)
852 struct list_head *entry, *n;
855 list_for_each_safe(entry, n, &td->io_u_freelist) {
856 io_u = list_entry(entry, struct io_u, list);
858 list_del(&io_u->list);
862 if (td->mem_type == MEM_MALLOC)
863 free(td->orig_buffer);
864 else if (td->mem_type == MEM_SHM) {
865 struct shmid_ds sbuf;
867 shmdt(td->orig_buffer);
868 shmctl(td->shm_id, IPC_RMID, &sbuf);
869 } else if (td->mem_type == MEM_MMAP)
870 munmap(td->orig_buffer, td->orig_buffer_size);
872 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
874 td->orig_buffer = NULL;
877 static int init_io_u(struct thread_data *td)
883 if (td->io_engine & FIO_SYNCIO)
886 max_units = td->iodepth;
888 td->orig_buffer_size = td->max_bs * max_units + MASK;
890 if (td->mem_type == MEM_MALLOC)
891 td->orig_buffer = malloc(td->orig_buffer_size);
892 else if (td->mem_type == MEM_SHM) {
893 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
894 if (td->shm_id < 0) {
895 td_verror(td, errno);
900 td->orig_buffer = shmat(td->shm_id, NULL, 0);
901 if (td->orig_buffer == (void *) -1) {
902 td_verror(td, errno);
904 td->orig_buffer = NULL;
907 } else if (td->mem_type == MEM_MMAP) {
908 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
909 if (td->orig_buffer == MAP_FAILED) {
910 td_verror(td, errno);
912 td->orig_buffer = NULL;
917 p = ALIGN(td->orig_buffer);
918 for (i = 0; i < max_units; i++) {
919 io_u = malloc(sizeof(*io_u));
920 memset(io_u, 0, sizeof(*io_u));
921 INIT_LIST_HEAD(&io_u->list);
923 io_u->buf = p + td->max_bs * i;
925 list_add(&io_u->list, &td->io_u_freelist);
931 static void cleanup_allocs(struct thread_data *td)
936 free(td->iolog_file);
938 free(td->exec_prerun);
939 if (td->exec_postrun)
940 free(td->exec_postrun);
942 free(td->ioscheduler);
944 free(td->sysfs_root);
947 static int create_file(struct thread_data *td, unsigned long long size,
950 unsigned long long left;
956 * unless specifically asked for overwrite, let normal io extend it
958 if (td_write(td) && !td->overwrite)
962 fprintf(stderr, "Need size for create\n");
963 td_verror(td, EINVAL);
968 oflags = O_CREAT | O_TRUNC;
969 printf("%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
972 printf("%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
975 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
977 td_verror(td, errno);
981 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
982 td_verror(td, errno);
986 td->io_size = td->file_size;
987 b = malloc(td->max_bs);
988 memset(b, 0, td->max_bs);
991 while (left && !td->terminate) {
996 r = write(td->fd, b, bs);
1003 td_verror(td, errno);
1012 unlink(td->file_name);
1013 else if (td->create_fsync)
1022 static int file_size(struct thread_data *td)
1026 if (fstat(td->fd, &st) == -1) {
1027 td_verror(td, errno);
1031 td->real_file_size = st.st_size;
1033 if (!td->file_size || td->file_size > td->real_file_size)
1034 td->file_size = td->real_file_size;
1036 td->file_size -= td->file_offset;
1040 static int bdev_size(struct thread_data *td)
1042 unsigned long long bytes;
1045 r = blockdev_size(td->fd, &bytes);
1051 td->real_file_size = bytes;
1054 * no extend possibilities, so limit size to device size if too large
1056 if (!td->file_size || td->file_size > td->real_file_size)
1057 td->file_size = td->real_file_size;
1059 td->file_size -= td->file_offset;
1063 static int get_file_size(struct thread_data *td)
1067 if (td->filetype == FIO_TYPE_FILE)
1068 ret = file_size(td);
1069 else if (td->filetype == FIO_TYPE_BD)
1070 ret = bdev_size(td);
1072 td->real_file_size = -1;
1077 if (td->file_offset > td->real_file_size) {
1078 fprintf(stderr, "%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1082 td->io_size = td->file_size;
1083 if (td->io_size == 0) {
1084 fprintf(stderr, "%s: no io blocks\n", td->name);
1085 td_verror(td, EINVAL);
1090 td->zone_size = td->io_size;
1092 td->total_io_size = td->io_size * td->loops;
1096 static int setup_file_mmap(struct thread_data *td)
1101 flags = PROT_READ | PROT_WRITE;
1102 else if (td_write(td)) {
1105 if (td->verify != VERIFY_NONE)
1110 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1111 if (td->mmap == MAP_FAILED) {
1113 td_verror(td, errno);
1117 if (td->invalidate_cache) {
1118 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1119 td_verror(td, errno);
1124 if (td->sequential) {
1125 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1126 td_verror(td, errno);
1130 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1131 td_verror(td, errno);
1139 static int setup_file_plain(struct thread_data *td)
1141 if (td->invalidate_cache) {
1142 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1143 td_verror(td, errno);
1148 if (td->sequential) {
1149 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1150 td_verror(td, errno);
1154 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1155 td_verror(td, errno);
1163 static int setup_file(struct thread_data *td)
1168 if (stat(td->file_name, &st) == -1) {
1169 if (errno != ENOENT) {
1170 td_verror(td, errno);
1173 if (!td->create_file) {
1174 td_verror(td, ENOENT);
1177 if (create_file(td, td->file_size, 0))
1179 } else if (td->filetype == FIO_TYPE_FILE) {
1180 if (st.st_size < (off_t) td->file_size) {
1181 if (create_file(td, td->file_size - st.st_size, 1))
1189 if (td_write(td) || td_rw(td)) {
1190 if (td->filetype == FIO_TYPE_FILE) {
1201 td->fd = open(td->file_name, flags, 0600);
1203 if (td->filetype == FIO_TYPE_CHAR)
1208 td->fd = open(td->file_name, flags);
1212 td_verror(td, errno);
1216 if (get_file_size(td))
1219 if (td->io_engine != FIO_MMAPIO)
1220 return setup_file_plain(td);
1222 return setup_file_mmap(td);
1225 static int switch_ioscheduler(struct thread_data *td)
1227 char tmp[256], tmp2[128];
1231 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1233 f = fopen(tmp, "r+");
1235 td_verror(td, errno);
1242 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1243 if (ferror(f) || ret != 1) {
1244 td_verror(td, errno);
1252 * Read back and check that the selected scheduler is now the default.
1254 ret = fread(tmp, 1, sizeof(tmp), f);
1255 if (ferror(f) || ret < 0) {
1256 td_verror(td, errno);
1261 sprintf(tmp2, "[%s]", td->ioscheduler);
1262 if (!strstr(tmp, tmp2)) {
1263 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1264 td_verror(td, EINVAL);
1273 static void clear_io_state(struct thread_data *td)
1275 if (td->io_engine == FIO_SYNCIO)
1276 lseek(td->fd, SEEK_SET, 0);
1279 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1280 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1284 memset(td->file_map, 0, td->num_maps * sizeof(long));
1287 static void *thread_main(void *data)
1289 struct thread_data *td = data;
1291 if (!td->use_thread)
1296 INIT_LIST_HEAD(&td->io_u_freelist);
1297 INIT_LIST_HEAD(&td->io_u_busylist);
1298 INIT_LIST_HEAD(&td->io_hist_list);
1299 INIT_LIST_HEAD(&td->io_log_list);
1304 if (fio_setaffinity(td) == -1) {
1305 td_verror(td, errno);
1316 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1317 td_verror(td, errno);
1322 if (nice(td->nice) < 0) {
1323 td_verror(td, errno);
1327 if (init_random_state(td))
1330 if (td->ioscheduler && switch_ioscheduler(td))
1333 td_set_runstate(td, TD_INITIALIZED);
1334 sem_post(&startup_sem);
1335 sem_wait(&td->mutex);
1337 if (!td->create_serialize && setup_file(td))
1340 gettimeofday(&td->epoch, NULL);
1342 if (td->exec_prerun)
1343 system(td->exec_prerun);
1345 while (td->loops--) {
1346 getrusage(RUSAGE_SELF, &td->ru_start);
1347 gettimeofday(&td->start, NULL);
1348 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1351 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1354 prune_io_piece_log(td);
1358 td->runtime[td->ddir] += mtime_since_now(&td->start);
1359 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1360 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1362 update_rusage_stat(td);
1364 if (td->error || td->terminate)
1367 if (td->verify == VERIFY_NONE)
1371 gettimeofday(&td->start, NULL);
1375 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1377 if (td->error || td->terminate)
1382 finish_log(td, td->bw_log, "bw");
1384 finish_log(td, td->slat_log, "slat");
1386 finish_log(td, td->clat_log, "clat");
1387 if (td->write_iolog)
1388 write_iolog_close(td);
1389 if (td->exec_postrun)
1390 system(td->exec_postrun);
1392 if (exitall_on_terminate)
1393 terminate_threads(td->groupid);
1401 munmap(td->mmap, td->file_size);
1405 td_set_runstate(td, TD_EXITED);
1410 static void *fork_main(int shmid, int offset)
1412 struct thread_data *td;
1415 data = shmat(shmid, NULL, 0);
1416 if (data == (void *) -1) {
1421 td = data + offset * sizeof(struct thread_data);
1427 static void check_str_update(struct thread_data *td)
1429 char c = run_str[td->thread_number - 1];
1431 if (td->runstate == td->old_runstate)
1434 switch (td->runstate) {
1447 } else if (td_read(td)) {
1465 case TD_INITIALIZED:
1468 case TD_NOT_CREATED:
1472 printf("state %d\n", td->runstate);
1475 run_str[td->thread_number - 1] = c;
1476 td->old_runstate = td->runstate;
1479 static void eta_to_str(char *str, int eta_sec)
1481 unsigned int d, h, m, s;
1482 static int always_d, always_h;
1494 if (d || always_d) {
1496 str += sprintf(str, "%02dd:", d);
1498 if (h || always_h) {
1500 str += sprintf(str, "%02dh:", h);
1503 str += sprintf(str, "%02dm:", m);
1504 str += sprintf(str, "%02ds", s);
1507 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1509 unsigned long long bytes_total, bytes_done;
1510 unsigned int eta_sec = 0;
1512 bytes_total = td->total_io_size;
1515 * if writing, bytes_total will be twice the size. If mixing,
1516 * assume a 50/50 split and thus bytes_total will be 50% larger.
1520 bytes_total = bytes_total * 3 / 2;
1524 if (td->zone_size && td->zone_skip)
1525 bytes_total /= (td->zone_skip / td->zone_size);
1527 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1530 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1531 perc = (double) bytes_done / (double) bytes_total;
1535 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1537 if (td->timeout && eta_sec > (td->timeout - elapsed))
1538 eta_sec = td->timeout - elapsed;
1539 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1540 || td->runstate == TD_INITIALIZED) {
1541 int t_eta = 0, r_eta = 0;
1544 * We can only guess - assume it'll run the full timeout
1545 * if given, otherwise assume it'll run at the specified rate.
1548 t_eta = td->timeout + td->start_delay - elapsed;
1550 r_eta = (bytes_total / 1024) / td->rate;
1551 r_eta += td->start_delay - elapsed;
1555 eta_sec = min(r_eta, t_eta);
1564 * thread is already done
1572 static void print_thread_status(void)
1574 unsigned long elapsed = time_since_now(&genesis);
1575 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
1579 eta_secs = malloc(thread_number * sizeof(int));
1580 memset(eta_secs, 0, thread_number * sizeof(int));
1582 nr_running = t_rate = m_rate = 0;
1583 for (i = 0; i < thread_number; i++) {
1584 struct thread_data *td = &threads[i];
1586 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1589 m_rate += td->ratemin;
1593 eta_secs[i] = thread_eta(td, elapsed);
1595 eta_secs[i] = INT_MAX;
1597 check_str_update(td);
1600 if (exitall_on_terminate)
1605 for (i = 0; i < thread_number; i++) {
1606 if (exitall_on_terminate) {
1607 if (eta_secs[i] < eta_sec)
1608 eta_sec = eta_secs[i];
1610 if (eta_secs[i] > eta_sec)
1611 eta_sec = eta_secs[i];
1615 if (eta_sec != INT_MAX && elapsed) {
1616 perc = (double) elapsed / (double) (elapsed + eta_sec);
1617 eta_to_str(eta_str, eta_sec);
1620 printf("Threads now running (%d)", nr_running);
1621 if (m_rate || t_rate)
1622 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1623 if (eta_sec != INT_MAX) {
1625 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1632 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1637 * reap exited threads (TD_EXITED -> TD_REAPED)
1639 for (i = 0; i < thread_number; i++) {
1640 struct thread_data *td = &threads[i];
1642 if (td->runstate != TD_EXITED)
1645 td_set_runstate(td, TD_REAPED);
1647 if (td->use_thread) {
1650 if (pthread_join(td->thread, (void *) &ret))
1651 perror("thread_join");
1653 waitpid(td->pid, NULL, 0);
1656 (*m_rate) -= td->ratemin;
1657 (*t_rate) -= td->rate;
1661 static void fio_unpin_memory(void *pinned)
1664 if (munlock(pinned, mlock_size) < 0)
1666 munmap(pinned, mlock_size);
1670 static void *fio_pin_memory(void)
1672 long pagesize, pages;
1679 * Don't allow mlock of more than real_mem-128MB
1681 pagesize = sysconf(_SC_PAGESIZE);
1682 pages = sysconf(_SC_PHYS_PAGES);
1683 if (pages != -1 && pagesize != -1) {
1684 unsigned long long real_mem = pages * pagesize;
1686 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
1687 mlock_size = real_mem - 128 * 1024 * 1024;
1688 printf("fio: limiting mlocked memory to %lluMiB\n",
1693 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1695 perror("malloc locked mem");
1698 if (mlock(ptr, mlock_size) < 0) {
1699 munmap(ptr, mlock_size);
1707 static void run_threads(void)
1709 struct thread_data *td;
1710 unsigned long spent;
1711 int i, todo, nr_running, m_rate, t_rate, nr_started;
1714 mlocked_mem = fio_pin_memory();
1716 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1719 signal(SIGINT, sig_handler);
1720 signal(SIGALRM, sig_handler);
1722 todo = thread_number;
1725 m_rate = t_rate = 0;
1727 for (i = 0; i < thread_number; i++) {
1730 run_str[td->thread_number - 1] = 'P';
1734 if (!td->create_serialize)
1738 * do file setup here so it happens sequentially,
1739 * we don't want X number of threads getting their
1740 * client data interspersed on disk
1742 if (setup_file(td)) {
1743 td_set_runstate(td, TD_REAPED);
1748 gettimeofday(&genesis, NULL);
1751 struct thread_data *map[MAX_JOBS];
1752 struct timeval this_start;
1753 int this_jobs = 0, left;
1756 * create threads (TD_NOT_CREATED -> TD_CREATED)
1758 for (i = 0; i < thread_number; i++) {
1761 if (td->runstate != TD_NOT_CREATED)
1765 * never got a chance to start, killed by other
1766 * thread for some reason
1768 if (td->terminate) {
1773 if (td->start_delay) {
1774 spent = mtime_since_now(&genesis);
1776 if (td->start_delay * 1000 > spent)
1780 if (td->stonewall && (nr_started || nr_running))
1784 * Set state to created. Thread will transition
1785 * to TD_INITIALIZED when it's done setting up.
1787 td_set_runstate(td, TD_CREATED);
1788 map[this_jobs++] = td;
1789 sem_init(&startup_sem, 0, 1);
1792 if (td->use_thread) {
1793 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1794 perror("thread_create");
1799 sem_wait(&startup_sem);
1801 fork_main(shm_id, i);
1808 * Wait for the started threads to transition to
1811 printf("fio: Waiting for threads to initialize...\n");
1812 gettimeofday(&this_start, NULL);
1815 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1820 for (i = 0; i < this_jobs; i++) {
1824 if (td->runstate == TD_INITIALIZED) {
1827 } else if (td->runstate >= TD_EXITED) {
1831 nr_running++; /* work-around... */
1837 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1838 for (i = 0; i < this_jobs; i++) {
1842 kill(td->pid, SIGTERM);
1848 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1850 printf("fio: Go for launch\n");
1851 for (i = 0; i < thread_number; i++) {
1854 if (td->runstate != TD_INITIALIZED)
1857 td_set_runstate(td, TD_RUNNING);
1860 m_rate += td->ratemin;
1863 sem_post(&td->mutex);
1866 reap_threads(&nr_running, &t_rate, &m_rate);
1872 while (nr_running) {
1873 reap_threads(&nr_running, &t_rate, &m_rate);
1878 fio_unpin_memory(mlocked_mem);
1881 int main(int argc, char *argv[])
1883 if (parse_options(argc, argv))
1886 if (!thread_number) {
1887 printf("Nothing to do\n");
1891 disk_util_timer_arm();