17 #include "lib/axmap.h"
18 #include "lib/memalign.h"
20 #ifdef CONFIG_LINUX_FALLOCATE
21 #include <linux/falloc.h>
26 static FLIST_HEAD(filename_list);
29 * List entry for filename_list
32 struct flist_head list;
36 static inline void clear_error(struct thread_data *td)
42 static inline int native_fallocate(struct thread_data *td, struct fio_file *f)
46 success = fio_fallocate(f, 0, f->real_file_size);
47 dprint(FD_FILE, "native fallocate of file %s size %llu was "
48 "%ssuccessful\n", f->file_name,
49 (unsigned long long) f->real_file_size,
56 dprint(FD_FILE, "native fallocate is not implemented\n");
61 static void fallocate_file(struct thread_data *td, struct fio_file *f)
65 if (td->o.fill_device)
68 switch (td->o.fallocate_mode) {
69 case FIO_FALLOCATE_NATIVE:
70 r = native_fallocate(td, f);
71 if (r != 0 && errno != ENOSYS)
72 log_err("fio: native_fallocate call failed: %s\n",
75 case FIO_FALLOCATE_NONE:
77 #ifdef CONFIG_POSIX_FALLOCATE
78 case FIO_FALLOCATE_POSIX:
79 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
81 (unsigned long long) f->real_file_size);
83 r = posix_fallocate(f->fd, 0, f->real_file_size);
85 log_err("fio: posix_fallocate fails: %s\n", strerror(r));
87 #endif /* CONFIG_POSIX_FALLOCATE */
88 #ifdef CONFIG_LINUX_FALLOCATE
89 case FIO_FALLOCATE_KEEP_SIZE:
90 dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) "
91 "file %s size %llu\n", f->file_name,
92 (unsigned long long) f->real_file_size);
94 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size);
96 td_verror(td, errno, "fallocate");
99 #endif /* CONFIG_LINUX_FALLOCATE */
101 log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode);
107 * Leaves f->fd open on success, caller must close
109 static int extend_file(struct thread_data *td, struct fio_file *f)
111 int new_layout = 0, unlink_file = 0, flags;
112 unsigned long long left;
117 log_err("fio: refusing extend of file due to read-only\n");
122 * check if we need to lay the file out complete again. fio
123 * does that for operations involving reads, or for writes
124 * where overwrite is set
127 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
128 (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND)))
130 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
133 if (unlink_file || new_layout) {
136 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
138 ret = td_io_unlink_file(td, f);
139 if (ret != 0 && ret != ENOENT) {
140 td_verror(td, errno, "unlink");
146 if (td->o.allow_create)
151 flags |= OS_O_DIRECT;
157 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
158 f->fd = open(f->file_name, flags, 0644);
162 if (err == ENOENT && !td->o.allow_create)
163 log_err("fio: file creation disallowed by "
164 "allow_file_create=0\n");
166 if (err == EINVAL && (flags & OS_O_DIRECT))
167 log_err("fio: looks like your filesystem "
169 "direct=1/buffered=0\n");
171 td_verror(td, err, "open");
176 fallocate_file(td, f);
179 * If our jobs don't require regular files initially, we're done.
185 * The size will be -1ULL when fill_device is used, so don't truncate
186 * or fallocate this file, just write it
188 if (!td->o.fill_device) {
189 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
190 (unsigned long long) f->real_file_size);
191 if (ftruncate(f->fd, f->real_file_size) == -1) {
192 if (errno != EFBIG) {
193 td_verror(td, errno, "ftruncate");
199 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
202 left = f->real_file_size;
203 bs = td->o.max_bs[DDIR_WRITE];
207 b = fio_memalign(page_size, bs);
209 td_verror(td, errno, "fio_memalign");
213 while (left && !td->terminate) {
219 fill_io_buffer(td, b, bs, bs);
221 r = write(f->fd, b, bs);
231 if (td->o.fill_device)
233 log_info("fio: ENOSPC on laying out "
237 td_verror(td, errno, "write");
239 td_verror(td, EIO, "write");
246 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
247 td_io_unlink_file(td, f);
248 } else if (td->o.create_fsync) {
249 if (fsync(f->fd) < 0) {
250 td_verror(td, errno, "fsync");
254 if (td->o.fill_device && !td_write(td)) {
255 fio_file_clear_size_known(f);
256 if (td_io_get_file_size(td, f))
258 if (f->io_size > f->real_file_size)
259 f->io_size = f->real_file_size;
273 static int pre_read_file(struct thread_data *td, struct fio_file *f)
275 int ret = 0, r, did_open = 0, old_runstate;
276 unsigned long long left;
280 if (td_ioengine_flagged(td, FIO_PIPEIO) ||
281 td_ioengine_flagged(td, FIO_NOIO))
284 if (f->filetype == FIO_TYPE_CHAR)
287 if (!fio_file_open(f)) {
288 if (td->io_ops->open_file(td, f)) {
289 log_err("fio: cannot pre-read, failed to open file\n");
295 old_runstate = td_bump_runstate(td, TD_PRE_READING);
298 bs = td->o.max_bs[DDIR_READ];
304 td_verror(td, errno, "malloc");
310 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
311 td_verror(td, errno, "lseek");
312 log_err("fio: failed to lseek pre-read file\n");
317 while (left && !td->terminate) {
321 r = read(f->fd, b, bs);
327 td_verror(td, EIO, "pre_read");
333 td_restore_runstate(td, old_runstate);
336 td->io_ops->close_file(td, f);
342 unsigned long long get_rand_file_size(struct thread_data *td)
344 unsigned long long ret, sized;
348 frand_max = rand_max(&td->file_size_state);
349 r = __rand(&td->file_size_state);
350 sized = td->o.file_size_high - td->o.file_size_low;
351 ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0)));
352 ret += td->o.file_size_low;
353 ret -= (ret % td->o.rw_min_bs);
357 static int file_size(struct thread_data *td, struct fio_file *f)
361 if (stat(f->file_name, &st) == -1) {
362 td_verror(td, errno, "fstat");
366 f->real_file_size = st.st_size;
370 static int bdev_size(struct thread_data *td, struct fio_file *f)
372 unsigned long long bytes = 0;
375 if (td->io_ops->open_file(td, f)) {
376 log_err("fio: failed opening blockdev %s for size check\n",
381 r = blockdev_size(f, &bytes);
383 td_verror(td, r, "blockdev_size");
388 log_err("%s: zero sized block device?\n", f->file_name);
392 f->real_file_size = bytes;
393 td->io_ops->close_file(td, f);
396 td->io_ops->close_file(td, f);
400 static int char_size(struct thread_data *td, struct fio_file *f)
402 #ifdef FIO_HAVE_CHARDEV_SIZE
403 unsigned long long bytes = 0;
406 if (td->io_ops->open_file(td, f)) {
407 log_err("fio: failed opening chardev %s for size check\n",
412 r = chardev_size(f, &bytes);
414 td_verror(td, r, "chardev_size");
419 log_err("%s: zero sized char device?\n", f->file_name);
423 f->real_file_size = bytes;
424 td->io_ops->close_file(td, f);
427 td->io_ops->close_file(td, f);
430 f->real_file_size = -1ULL;
435 static int get_file_size(struct thread_data *td, struct fio_file *f)
439 if (fio_file_size_known(f))
442 if (f->filetype == FIO_TYPE_FILE)
443 ret = file_size(td, f);
444 else if (f->filetype == FIO_TYPE_BLOCK)
445 ret = bdev_size(td, f);
446 else if (f->filetype == FIO_TYPE_CHAR)
447 ret = char_size(td, f);
449 f->real_file_size = -1ULL;
452 * Leave ->real_file_size with 0 since it could be expectation
453 * of initial setup for regular files.
459 * If ->real_file_size is -1, a conditional for the message
460 * "offset extends end" is always true, but it makes no sense,
461 * so just return the same value here.
463 if (f->real_file_size == -1ULL) {
464 log_info("%s: failed to get file size of %s\n", td->o.name,
469 if (td->o.start_offset && f->file_offset == 0)
470 dprint(FD_FILE, "offset of file %s not initialized yet\n",
473 * ->file_offset normally hasn't been initialized yet, so this
474 * is basically always false.
476 if (f->file_offset > f->real_file_size) {
477 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
478 (unsigned long long) f->file_offset,
479 (unsigned long long) f->real_file_size);
483 fio_file_set_size_known(f);
487 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
488 unsigned long long off,
489 unsigned long long len)
491 int errval = 0, ret = 0;
500 off = f->file_offset;
502 if (len == -1ULL || off == -1ULL)
505 if (td->io_ops->invalidate) {
506 dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name,
508 ret = td->io_ops->invalidate(td, f);
511 } else if (f->filetype == FIO_TYPE_FILE) {
512 dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n",
513 f->file_name, off, len);
514 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
517 } else if (f->filetype == FIO_TYPE_BLOCK) {
520 dprint(FD_IO, "drop page cache %s\n", f->file_name);
521 ret = blockdev_invalidate_cache(f);
522 while (ret < 0 && errno == EAGAIN && retry_count++ < 25) {
524 * Linux multipath devices reject ioctl while
525 * the maps are being updated. That window can
526 * last tens of milliseconds; we'll try up to
527 * a quarter of a second.
530 ret = blockdev_invalidate_cache(f);
532 if (ret < 0 && errno == EACCES && geteuid()) {
534 log_err("fio: only root may flush block "
535 "devices. Cache flush bypassed!\n");
542 } else if (f->filetype == FIO_TYPE_CHAR ||
543 f->filetype == FIO_TYPE_PIPE) {
544 dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
549 * Cache flushing isn't a fatal condition, and we know it will
550 * happen on some platforms where we don't have the proper
551 * function to flush eg block device caches. So just warn and
552 * continue on our way.
555 log_info("fio: cache invalidation of %s failed: %s\n",
556 f->file_name, strerror(errval));
562 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
564 if (!fio_file_open(f))
567 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
570 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
574 dprint(FD_FILE, "fd close %s\n", f->file_name);
578 if (close(f->fd) < 0)
583 if (f->shadow_fd != -1) {
592 int file_lookup_open(struct fio_file *f, int flags)
594 struct fio_file *__f;
597 __f = lookup_file_hash(f->file_name);
599 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
603 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
611 f->fd = open(f->file_name, flags, 0600);
615 static int file_close_shadow_fds(struct thread_data *td)
621 for_each_file(td, f, i) {
622 if (f->shadow_fd == -1)
633 int generic_open_file(struct thread_data *td, struct fio_file *f)
639 dprint(FD_FILE, "fd open %s\n", f->file_name);
641 if (!strcmp(f->file_name, "-")) {
643 log_err("fio: can't read/write to stdin/out\n");
649 * move output logging to stderr, if we are writing to stdout
658 flags |= OS_O_DIRECT;
661 td_verror(td, EINVAL, "OS does not support atomic IO");
664 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
668 if (td->o.create_on_open && td->o.allow_create)
671 if (f->filetype != FIO_TYPE_FILE)
672 flags |= FIO_O_NOATIME;
679 if (f->filetype == FIO_TYPE_FILE && td->o.allow_create)
683 f->fd = dup(STDOUT_FILENO);
685 from_hash = file_lookup_open(f, flags);
686 } else if (td_read(td)) {
687 if (f->filetype == FIO_TYPE_CHAR && !read_only)
693 f->fd = dup(STDIN_FILENO);
695 from_hash = file_lookup_open(f, flags);
696 } else if (td_trim(td)) {
697 assert(!td_rw(td)); /* should have matched above */
699 from_hash = file_lookup_open(f, flags);
703 char buf[FIO_VERROR_SIZE];
706 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
707 flags &= ~FIO_O_NOATIME;
710 if (__e == EMFILE && file_close_shadow_fds(td))
713 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
715 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
716 log_err("fio: looks like your file system does not " \
717 "support direct=1/buffered=0\n");
720 td_verror(td, __e, buf);
724 if (!from_hash && f->fd != -1) {
725 if (add_file_hash(f)) {
729 * Stash away descriptor for later close. This is to
730 * work-around a "feature" on Linux, where a close of
731 * an fd that has been opened for write will trigger
732 * udev to call blkid to check partitions, fs id, etc.
733 * That pollutes the device cache, which can slow down
734 * unbuffered accesses.
736 if (f->shadow_fd == -1)
737 f->shadow_fd = f->fd;
740 * OK to ignore, we haven't done anything
743 ret = generic_close_file(td, f);
753 * This function i.e. get_file_size() is the default .get_file_size
754 * implementation of majority of I/O engines.
756 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
758 return get_file_size(td, f);
762 * open/close all files, so that ->real_file_size gets set
764 static int get_file_sizes(struct thread_data *td)
770 for_each_file(td, f, i) {
771 dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i,
774 if (td_io_get_file_size(td, f)) {
775 if (td->error != ENOENT) {
776 log_err("%s\n", td->verror);
784 * There are corner cases where we end up with -1 for
785 * ->real_file_size due to unsupported file type, etc.
786 * We then just set to size option value divided by number
787 * of files, similar to the way file ->io_size is set.
788 * stat(2) failure doesn't set ->real_file_size to -1.
790 if (f->real_file_size == -1ULL && td->o.size)
791 f->real_file_size = td->o.size / td->o.nr_files;
798 struct flist_head list;
805 * Get free number of bytes for each file on each unique mount.
807 static unsigned long long get_fs_free_counts(struct thread_data *td)
809 struct flist_head *n, *tmp;
810 unsigned long long ret = 0;
811 struct fio_mount *fm;
816 for_each_file(td, f, i) {
820 if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) {
821 if (f->real_file_size != -1ULL)
822 ret += f->real_file_size;
824 } else if (f->filetype != FIO_TYPE_FILE)
828 strncpy(buf, f->file_name, 255);
830 if (stat(buf, &sb) < 0) {
834 if (stat(buf, &sb) < 0)
839 flist_for_each(n, &list) {
840 fm = flist_entry(n, struct fio_mount, list);
841 if (fm->key == sb.st_dev)
850 fm = calloc(1, sizeof(*fm));
851 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
852 fm->base = basename(fm->__base);
854 flist_add(&fm->list, &list);
857 flist_for_each_safe(n, tmp, &list) {
858 unsigned long long sz;
860 fm = flist_entry(n, struct fio_mount, list);
861 flist_del(&fm->list);
863 sz = get_fs_free_size(fm->base);
864 if (sz && sz != -1ULL)
873 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
875 struct thread_options *o = &td->o;
876 unsigned long long align_bs;
877 unsigned long long offset;
879 if (o->file_append && f->filetype == FIO_TYPE_FILE)
880 return f->real_file_size;
882 if (o->start_offset_percent > 0) {
884 * if blockalign is provided, find the min across read, write,
887 if (fio_option_is_set(o, ba)) {
888 align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]);
889 align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs);
891 /* else take the minimum block size */
892 align_bs = td_min_bs(td);
895 /* calculate the raw offset */
896 offset = (f->real_file_size * o->start_offset_percent / 100) +
897 (td->subjob_number * o->offset_increment);
900 * block align the offset at the next available boundary at
901 * ceiling(offset / align_bs) * align_bs
903 offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs;
906 /* start_offset_percent not set */
907 offset = o->start_offset +
908 td->subjob_number * o->offset_increment;
915 * Open the files and setup files sizes, creating files if necessary.
917 int setup_files(struct thread_data *td)
919 unsigned long long total_size, extend_size;
920 struct thread_options *o = &td->o;
922 unsigned int i, nr_fs_extra = 0;
923 int err = 0, need_extend;
925 const unsigned int bs = td_min_bs(td);
928 dprint(FD_FILE, "setup files\n");
930 old_state = td_bump_runstate(td, TD_SETTING_UP);
932 if (o->read_iolog_file)
936 * Find out physical size of files or devices for this thread,
937 * before we determine I/O size and range of our targets.
938 * If ioengine defines a setup() method, it's responsible for
939 * opening the files and setting f->real_file_size to indicate
940 * the valid range for that file.
942 if (td->io_ops->setup)
943 err = td->io_ops->setup(td);
945 err = get_file_sizes(td);
951 * check sizes. if the files/devices do not exist and the size
952 * isn't passed to fio, abort.
955 for_each_file(td, f, i) {
957 if (f->real_file_size == -1ULL)
960 total_size += f->real_file_size;
964 td->fill_device_size = get_fs_free_counts(td);
967 * device/file sizes are zero and no size given, punt
969 if ((!total_size || total_size == -1ULL) && !o->size &&
970 !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device &&
971 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
972 log_err("%s: you need to specify size=\n", o->name);
973 td_verror(td, EINVAL, "total_file_size");
978 * Calculate per-file size and potential extra size for the
979 * first files, if needed (i.e. if we don't have a fixed size).
981 if (!o->file_size_low && o->nr_files) {
984 fs = o->size / o->nr_files;
985 all_fs = fs * o->nr_files;
987 if (all_fs < o->size)
988 nr_fs_extra = (o->size - all_fs) / bs;
992 * now file sizes are known, so we can set ->io_size. if size= is
993 * not given, ->io_size is just equal to ->real_file_size. if size
994 * is given, ->io_size is size / nr_files.
996 extend_size = total_size = 0;
998 for_each_file(td, f, i) {
999 f->file_offset = get_start_offset(td, f);
1002 * Update ->io_size depending on options specified.
1003 * ->file_size_low being 0 means filesize option isn't set.
1004 * Non zero ->file_size_low equals ->file_size_high means
1005 * filesize option is set in a fixed size format.
1006 * Non zero ->file_size_low not equals ->file_size_high means
1007 * filesize option is set in a range format.
1009 if (!o->file_size_low) {
1011 * no file size or range given, file size is equal to
1012 * total size divided by number of files. If the size
1013 * doesn't divide nicely with the min blocksize,
1014 * make the first files bigger.
1023 * We normally don't come here for regular files, but
1024 * if the result is 0 for a regular file, set it to the
1025 * real file size. This could be size of the existing
1026 * one if it already exists, but otherwise will be set
1027 * to 0. A new file won't be created because
1028 * ->io_size + ->file_offset equals ->real_file_size.
1031 if (f->file_offset > f->real_file_size)
1033 f->io_size = f->real_file_size - f->file_offset;
1035 log_info("fio: file %s may be ignored\n",
1038 } else if (f->real_file_size < o->file_size_low ||
1039 f->real_file_size > o->file_size_high) {
1040 if (f->file_offset > o->file_size_low)
1043 * file size given. if it's fixed, use that. if it's a
1044 * range, generate a random size in-between.
1046 if (o->file_size_low == o->file_size_high)
1047 f->io_size = o->file_size_low - f->file_offset;
1049 f->io_size = get_rand_file_size(td)
1053 f->io_size = f->real_file_size - f->file_offset;
1055 if (f->io_size == -1ULL)
1058 if (o->size_percent) {
1061 file_size = f->io_size + f->file_offset;
1062 f->io_size = (file_size *
1063 o->size_percent) / 100;
1064 if (f->io_size > (file_size - f->file_offset))
1065 f->io_size = file_size - f->file_offset;
1067 f->io_size -= (f->io_size % td_min_bs(td));
1069 total_size += f->io_size;
1072 if (f->filetype == FIO_TYPE_FILE &&
1073 (f->io_size + f->file_offset) > f->real_file_size &&
1074 !td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1075 if (!o->create_on_open) {
1077 extend_size += (f->io_size + f->file_offset);
1078 fio_file_set_extend(f);
1080 f->real_file_size = f->io_size + f->file_offset;
1084 if (td->o.block_error_hist) {
1087 assert(td->o.nr_files == 1); /* checked in fixup_options */
1089 len = f->io_size / td->o.bs[DDIR_TRIM];
1090 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
1091 log_err("fio: cannot calculate block histogram with "
1092 "%d trim blocks, maximum %d\n",
1093 len, MAX_NR_BLOCK_INFOS);
1094 td_verror(td, EINVAL, "block_error_hist");
1098 td->ts.nr_block_infos = len;
1099 for (i = 0; i < len; i++)
1100 td->ts.block_infos[i] =
1101 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
1103 td->ts.nr_block_infos = 0;
1105 if (!o->size || (total_size && o->size > total_size))
1106 o->size = total_size;
1108 if (o->size < td_min_bs(td)) {
1109 log_err("fio: blocksize too large for data set\n");
1114 * See if we need to extend some files, typically needed when our
1115 * target regular files don't exist yet, but our jobs require them
1116 * initially due to read I/Os.
1120 if (output_format & FIO_OUTPUT_NORMAL) {
1121 log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n",
1123 need_extend > 1 ? "s" : "",
1125 need_extend > 1 ? "s" : "",
1126 need_extend > 1 ? "total " : "",
1130 for_each_file(td, f, i) {
1131 unsigned long long old_len = -1ULL, extend_len = -1ULL;
1133 if (!fio_file_extend(f))
1136 assert(f->filetype == FIO_TYPE_FILE);
1137 fio_file_clear_extend(f);
1138 if (!o->fill_device) {
1139 old_len = f->real_file_size;
1140 extend_len = f->io_size + f->file_offset -
1143 f->real_file_size = (f->io_size + f->file_offset);
1144 err = extend_file(td, f);
1148 err = __file_invalidate_cache(td, f, old_len,
1152 * Shut up static checker
1168 o->zone_size = o->size;
1171 * iolog already set the total io size, if we read back
1174 if (!o->read_iolog_file) {
1176 td->total_io_size = o->io_size * o->loops;
1178 td->total_io_size = o->size * o->loops;
1185 td_restore_runstate(td, old_state);
1188 log_err("%s: you need to specify valid offset=\n", o->name);
1190 td_restore_runstate(td, old_state);
1194 int pre_read_files(struct thread_data *td)
1199 dprint(FD_FILE, "pre_read files\n");
1201 for_each_file(td, f, i) {
1202 if (pre_read_file(td, f))
1209 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1211 unsigned int range_size, seed;
1212 unsigned long nranges;
1215 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1216 fsize = min(f->real_file_size, f->io_size);
1218 nranges = (fsize + range_size - 1) / range_size;
1220 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1221 if (!td->o.rand_repeatable)
1222 seed = td->rand_seeds[4];
1224 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1225 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1226 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
1227 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1228 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
1229 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1234 static int init_rand_distribution(struct thread_data *td)
1240 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1243 state = td_bump_runstate(td, TD_SETTING_UP);
1245 for_each_file(td, f, i)
1246 __init_rand_distribution(td, f);
1248 td_restore_runstate(td, state);
1254 * Check if the number of blocks exceeds the randomness capability of
1255 * the selected generator. Tausworthe is 32-bit, the others are fullly
1258 static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f,
1261 if (blocks <= FRAND32_MAX)
1263 if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE)
1267 * If the user hasn't specified a random generator, switch
1268 * to tausworthe64 with informational warning. If the user did
1269 * specify one, just warn.
1271 log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n",
1274 if (!fio_option_is_set(&td->o, random_generator)) {
1275 log_info("fio: Switching to tausworthe64. Use the "
1276 "random_generator= option to get rid of this "
1278 td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64;
1283 * Just make this information to avoid breaking scripts.
1285 log_info("fio: Use the random_generator= option to switch to lfsr or "
1290 int init_random_map(struct thread_data *td)
1292 unsigned long long blocks;
1296 if (init_rand_distribution(td))
1301 for_each_file(td, f, i) {
1302 uint64_t fsize = min(f->real_file_size, f->io_size);
1304 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1306 if (check_rand_gen_limits(td, f, blocks))
1309 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1312 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1314 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1315 fio_file_set_lfsr(f);
1318 } else if (!td->o.norandommap) {
1319 f->io_axmap = axmap_new(blocks);
1321 fio_file_set_axmap(f);
1324 } else if (td->o.norandommap)
1327 if (!td->o.softrandommap) {
1328 log_err("fio: failed allocating random map. If running"
1329 " a large number of jobs, try the 'norandommap'"
1330 " option or set 'softrandommap'. Or give"
1331 " a larger --alloc-size to fio.\n");
1335 log_info("fio: file %s failed allocating random map. Running "
1336 "job without.\n", f->file_name);
1342 void close_files(struct thread_data *td)
1347 for_each_file(td, f, i) {
1348 if (fio_file_open(f))
1349 td_io_close_file(td, f);
1353 void close_and_free_files(struct thread_data *td)
1358 dprint(FD_FILE, "close files\n");
1360 for_each_file(td, f, i) {
1361 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1362 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1363 td_io_unlink_file(td, f);
1366 if (fio_file_open(f))
1367 td_io_close_file(td, f);
1369 remove_file_hash(f);
1371 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1372 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1373 td_io_unlink_file(td, f);
1376 sfree(f->file_name);
1377 f->file_name = NULL;
1378 if (fio_file_axmap(f)) {
1379 axmap_free(f->io_axmap);
1385 td->o.filename = NULL;
1387 free(td->file_locks);
1388 td->files_index = 0;
1390 td->file_locks = NULL;
1391 td->o.file_lock_mode = FILE_LOCK_NONE;
1395 static void get_file_type(struct fio_file *f)
1399 if (!strcmp(f->file_name, "-"))
1400 f->filetype = FIO_TYPE_PIPE;
1402 f->filetype = FIO_TYPE_FILE;
1405 /* \\.\ is the device namespace in Windows, where every file is
1407 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1408 f->filetype = FIO_TYPE_BLOCK;
1411 if (!stat(f->file_name, &sb)) {
1412 if (S_ISBLK(sb.st_mode))
1413 f->filetype = FIO_TYPE_BLOCK;
1414 else if (S_ISCHR(sb.st_mode))
1415 f->filetype = FIO_TYPE_CHAR;
1416 else if (S_ISFIFO(sb.st_mode))
1417 f->filetype = FIO_TYPE_PIPE;
1421 static bool __is_already_allocated(const char *fname, bool set)
1423 struct flist_head *entry;
1426 ret = file_bloom_exists(fname, set);
1430 flist_for_each(entry, &filename_list) {
1431 struct file_name *fn;
1433 fn = flist_entry(entry, struct file_name, list);
1435 if (!strcmp(fn->filename, fname))
1442 static bool is_already_allocated(const char *fname)
1446 fio_file_hash_lock();
1447 ret = __is_already_allocated(fname, false);
1448 fio_file_hash_unlock();
1453 static void set_already_allocated(const char *fname)
1455 struct file_name *fn;
1457 fn = malloc(sizeof(struct file_name));
1458 fn->filename = strdup(fname);
1460 fio_file_hash_lock();
1461 if (!__is_already_allocated(fname, true)) {
1462 flist_add_tail(&fn->list, &filename_list);
1465 fio_file_hash_unlock();
1473 static void free_already_allocated(void)
1475 struct flist_head *entry, *tmp;
1476 struct file_name *fn;
1478 if (flist_empty(&filename_list))
1481 fio_file_hash_lock();
1482 flist_for_each_safe(entry, tmp, &filename_list) {
1483 fn = flist_entry(entry, struct file_name, list);
1485 flist_del(&fn->list);
1489 fio_file_hash_unlock();
1492 static struct fio_file *alloc_new_file(struct thread_data *td)
1496 f = smalloc(sizeof(*f));
1504 fio_file_reset(td, f);
1508 bool exists_and_not_regfile(const char *filename)
1512 if (lstat(filename, &sb) == -1)
1515 #ifndef WIN32 /* NOT Windows */
1516 if (S_ISREG(sb.st_mode))
1519 /* \\.\ is the device namespace in Windows, where every file
1520 * is a device node */
1521 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
1528 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1530 int cur_files = td->files_index;
1531 char file_name[PATH_MAX];
1535 dprint(FD_FILE, "add file %s\n", fname);
1537 if (td->o.directory)
1538 len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob,
1539 td->o.unique_filename);
1541 sprintf(file_name + len, "%s", fname);
1543 /* clean cloned siblings using existing files */
1544 if (numjob && is_already_allocated(file_name) &&
1545 !exists_and_not_regfile(fname))
1548 f = alloc_new_file(td);
1550 if (td->files_size <= td->files_index) {
1551 unsigned int new_size = td->o.nr_files + 1;
1553 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1555 td->files = realloc(td->files, new_size * sizeof(f));
1556 if (td->files == NULL) {
1557 log_err("fio: realloc OOM\n");
1560 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1561 td->file_locks = realloc(td->file_locks, new_size);
1562 if (!td->file_locks) {
1563 log_err("fio: realloc OOM\n");
1566 td->file_locks[cur_files] = FILE_LOCK_NONE;
1568 td->files_size = new_size;
1570 td->files[cur_files] = f;
1571 f->fileno = cur_files;
1574 * init function, io engine may not be loaded yet
1576 if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
1577 f->real_file_size = -1ULL;
1579 f->file_name = smalloc_strdup(file_name);
1585 switch (td->o.file_lock_mode) {
1586 case FILE_LOCK_NONE:
1588 case FILE_LOCK_READWRITE:
1589 f->rwlock = fio_rwlock_init();
1591 case FILE_LOCK_EXCLUSIVE:
1592 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1595 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1600 if (f->filetype == FIO_TYPE_FILE)
1601 td->nr_normal_files++;
1603 set_already_allocated(file_name);
1608 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1614 int add_file_exclusive(struct thread_data *td, const char *fname)
1619 for_each_file(td, f, i) {
1620 if (!strcmp(f->file_name, fname))
1624 return add_file(td, fname, 0, 1);
1627 void get_file(struct fio_file *f)
1629 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1630 assert(fio_file_open(f));
1634 int put_file(struct thread_data *td, struct fio_file *f)
1636 int f_ret = 0, ret = 0;
1638 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1640 if (!fio_file_open(f)) {
1641 assert(f->fd == -1);
1645 assert(f->references);
1646 if (--f->references)
1649 if (should_fsync(td) && td->o.fsync_on_close) {
1650 f_ret = fsync(f->fd);
1655 if (td->io_ops->close_file)
1656 ret = td->io_ops->close_file(td, f);
1661 td->nr_open_files--;
1662 fio_file_clear_open(f);
1663 assert(f->fd == -1);
1667 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1669 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1672 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1673 if (ddir == DDIR_READ)
1674 fio_rwlock_read(f->rwlock);
1676 fio_rwlock_write(f->rwlock);
1677 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1678 fio_mutex_down(f->lock);
1680 td->file_locks[f->fileno] = td->o.file_lock_mode;
1683 void unlock_file(struct thread_data *td, struct fio_file *f)
1685 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1688 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1689 fio_rwlock_unlock(f->rwlock);
1690 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1691 fio_mutex_up(f->lock);
1693 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1696 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1698 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1700 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1704 static int recurse_dir(struct thread_data *td, const char *dirname)
1710 D = opendir(dirname);
1712 char buf[FIO_VERROR_SIZE];
1714 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1715 td_verror(td, errno, buf);
1719 while ((dir = readdir(D)) != NULL) {
1720 char full_path[PATH_MAX];
1723 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1726 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1728 if (lstat(full_path, &sb) == -1) {
1729 if (errno != ENOENT) {
1730 td_verror(td, errno, "stat");
1736 if (S_ISREG(sb.st_mode)) {
1737 add_file(td, full_path, 0, 1);
1740 if (!S_ISDIR(sb.st_mode))
1743 ret = recurse_dir(td, full_path);
1752 int add_dir_files(struct thread_data *td, const char *path)
1754 int ret = recurse_dir(td, path);
1757 log_info("fio: opendir added %d files\n", td->o.nr_files);
1762 void dup_files(struct thread_data *td, struct thread_data *org)
1767 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1772 td->files = malloc(org->files_index * sizeof(f));
1774 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1775 td->file_locks = malloc(org->files_index);
1777 for_each_file(org, f, i) {
1778 struct fio_file *__f;
1780 __f = alloc_new_file(td);
1783 __f->file_name = smalloc_strdup(f->file_name);
1784 if (!__f->file_name)
1787 __f->filetype = f->filetype;
1790 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1791 __f->lock = f->lock;
1792 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1793 __f->rwlock = f->rwlock;
1800 * Returns the index that matches the filename, or -1 if not there
1802 int get_fileno(struct thread_data *td, const char *fname)
1807 for_each_file(td, f, i)
1808 if (!strcmp(f->file_name, fname))
1815 * For log usage, where we add/open/close files automatically
1817 void free_release_files(struct thread_data *td)
1821 td->o.open_files = 0;
1822 td->files_index = 0;
1823 td->nr_normal_files = 0;
1826 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1830 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1831 f->last_pos[i] = f->file_offset;
1832 f->last_start[i] = -1ULL;
1835 if (fio_file_axmap(f))
1836 axmap_reset(f->io_axmap);
1837 else if (fio_file_lfsr(f))
1838 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1841 bool fio_files_done(struct thread_data *td)
1846 for_each_file(td, f, i)
1847 if (!fio_file_done(f))
1853 /* free memory used in initialization phase only */
1854 void filesetup_mem_free(void)
1856 free_already_allocated();
1860 * This function is for platforms which support direct I/O but not O_DIRECT.
1862 int fio_set_directio(struct thread_data *td, struct fio_file *f)
1864 #ifdef FIO_OS_DIRECTIO
1865 int ret = fio_set_odirect(f);
1868 td_verror(td, ret, "fio_set_directio");
1869 #if defined(__sun__)
1870 if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */
1871 log_err("fio: doing directIO to RAW devices or ZFS not supported\n");
1873 log_err("fio: the file system does not seem to support direct IO\n");
1876 log_err("fio: the file system does not seem to support direct IO\n");