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 left = f->real_file_size;
200 bs = td->o.max_bs[DDIR_WRITE];
204 b = fio_memalign(page_size, bs);
206 td_verror(td, errno, "fio_memalign");
210 while (left && !td->terminate) {
216 fill_io_buffer(td, b, bs, bs);
218 r = write(f->fd, b, bs);
228 if (td->o.fill_device)
230 log_info("fio: ENOSPC on laying out "
234 td_verror(td, errno, "write");
236 td_verror(td, EIO, "write");
243 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
244 td_io_unlink_file(td, f);
245 } else if (td->o.create_fsync) {
246 if (fsync(f->fd) < 0) {
247 td_verror(td, errno, "fsync");
251 if (td->o.fill_device && !td_write(td)) {
252 fio_file_clear_size_known(f);
253 if (td_io_get_file_size(td, f))
255 if (f->io_size > f->real_file_size)
256 f->io_size = f->real_file_size;
270 static int pre_read_file(struct thread_data *td, struct fio_file *f)
272 int ret = 0, r, did_open = 0, old_runstate;
273 unsigned long long left;
277 if (td_ioengine_flagged(td, FIO_PIPEIO) ||
278 td_ioengine_flagged(td, FIO_NOIO))
281 if (f->filetype == FIO_TYPE_CHAR)
284 if (!fio_file_open(f)) {
285 if (td->io_ops->open_file(td, f)) {
286 log_err("fio: cannot pre-read, failed to open file\n");
292 old_runstate = td_bump_runstate(td, TD_PRE_READING);
295 bs = td->o.max_bs[DDIR_READ];
301 td_verror(td, errno, "malloc");
307 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
308 td_verror(td, errno, "lseek");
309 log_err("fio: failed to lseek pre-read file\n");
314 while (left && !td->terminate) {
318 r = read(f->fd, b, bs);
324 td_verror(td, EIO, "pre_read");
330 td_restore_runstate(td, old_runstate);
333 td->io_ops->close_file(td, f);
339 unsigned long long get_rand_file_size(struct thread_data *td)
341 unsigned long long ret, sized;
345 frand_max = rand_max(&td->file_size_state);
346 r = __rand(&td->file_size_state);
347 sized = td->o.file_size_high - td->o.file_size_low;
348 ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0)));
349 ret += td->o.file_size_low;
350 ret -= (ret % td->o.rw_min_bs);
354 static int file_size(struct thread_data *td, struct fio_file *f)
358 if (stat(f->file_name, &st) == -1) {
359 td_verror(td, errno, "fstat");
363 f->real_file_size = st.st_size;
367 static int bdev_size(struct thread_data *td, struct fio_file *f)
369 unsigned long long bytes = 0;
372 if (td->io_ops->open_file(td, f)) {
373 log_err("fio: failed opening blockdev %s for size check\n",
378 r = blockdev_size(f, &bytes);
380 td_verror(td, r, "blockdev_size");
385 log_err("%s: zero sized block device?\n", f->file_name);
389 f->real_file_size = bytes;
390 td->io_ops->close_file(td, f);
393 td->io_ops->close_file(td, f);
397 static int char_size(struct thread_data *td, struct fio_file *f)
399 #ifdef FIO_HAVE_CHARDEV_SIZE
400 unsigned long long bytes = 0;
403 if (td->io_ops->open_file(td, f)) {
404 log_err("fio: failed opening chardev %s for size check\n",
409 r = chardev_size(f, &bytes);
411 td_verror(td, r, "chardev_size");
416 log_err("%s: zero sized char device?\n", f->file_name);
420 f->real_file_size = bytes;
421 td->io_ops->close_file(td, f);
424 td->io_ops->close_file(td, f);
427 f->real_file_size = -1ULL;
432 static int get_file_size(struct thread_data *td, struct fio_file *f)
436 if (fio_file_size_known(f))
439 if (f->filetype == FIO_TYPE_FILE)
440 ret = file_size(td, f);
441 else if (f->filetype == FIO_TYPE_BLOCK)
442 ret = bdev_size(td, f);
443 else if (f->filetype == FIO_TYPE_CHAR)
444 ret = char_size(td, f);
446 f->real_file_size = -1ULL;
449 * Leave ->real_file_size with 0 since it could be expectation
450 * of initial setup for regular files.
456 * If ->real_file_size is -1, a conditional for the message
457 * "offset extends end" is always true, but it makes no sense,
458 * so just return the same value here.
460 if (f->real_file_size == -1ULL) {
461 log_info("%s: failed to get file size of %s\n", td->o.name,
466 if (td->o.start_offset && f->file_offset == 0)
467 dprint(FD_FILE, "offset of file %s not initialized yet\n",
470 * ->file_offset normally hasn't been initialized yet, so this
471 * is basically always false.
473 if (f->file_offset > f->real_file_size) {
474 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
475 (unsigned long long) f->file_offset,
476 (unsigned long long) f->real_file_size);
480 fio_file_set_size_known(f);
484 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
485 unsigned long long off,
486 unsigned long long len)
488 int errval = 0, ret = 0;
497 off = f->file_offset;
499 if (len == -1ULL || off == -1ULL)
502 if (td->io_ops->invalidate) {
503 dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name,
505 ret = td->io_ops->invalidate(td, f);
508 } else if (f->filetype == FIO_TYPE_FILE) {
509 dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n",
510 f->file_name, off, len);
511 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
514 } else if (f->filetype == FIO_TYPE_BLOCK) {
517 dprint(FD_IO, "drop page cache %s\n", f->file_name);
518 ret = blockdev_invalidate_cache(f);
519 while (ret < 0 && errno == EAGAIN && retry_count++ < 25) {
521 * Linux multipath devices reject ioctl while
522 * the maps are being updated. That window can
523 * last tens of milliseconds; we'll try up to
524 * a quarter of a second.
527 ret = blockdev_invalidate_cache(f);
529 if (ret < 0 && errno == EACCES && geteuid()) {
531 log_err("fio: only root may flush block "
532 "devices. Cache flush bypassed!\n");
539 } else if (f->filetype == FIO_TYPE_CHAR ||
540 f->filetype == FIO_TYPE_PIPE) {
541 dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
546 * Cache flushing isn't a fatal condition, and we know it will
547 * happen on some platforms where we don't have the proper
548 * function to flush eg block device caches. So just warn and
549 * continue on our way.
552 log_info("fio: cache invalidation of %s failed: %s\n",
553 f->file_name, strerror(errval));
559 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
561 if (!fio_file_open(f))
564 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
567 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
571 dprint(FD_FILE, "fd close %s\n", f->file_name);
575 if (close(f->fd) < 0)
580 if (f->shadow_fd != -1) {
589 int file_lookup_open(struct fio_file *f, int flags)
591 struct fio_file *__f;
594 __f = lookup_file_hash(f->file_name);
596 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
600 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
608 f->fd = open(f->file_name, flags, 0600);
612 static int file_close_shadow_fds(struct thread_data *td)
618 for_each_file(td, f, i) {
619 if (f->shadow_fd == -1)
630 int generic_open_file(struct thread_data *td, struct fio_file *f)
636 dprint(FD_FILE, "fd open %s\n", f->file_name);
638 if (!strcmp(f->file_name, "-")) {
640 log_err("fio: can't read/write to stdin/out\n");
646 * move output logging to stderr, if we are writing to stdout
655 flags |= OS_O_DIRECT;
658 td_verror(td, EINVAL, "OS does not support atomic IO");
661 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
665 if (td->o.create_on_open && td->o.allow_create)
668 if (f->filetype != FIO_TYPE_FILE)
669 flags |= FIO_O_NOATIME;
676 if (f->filetype == FIO_TYPE_FILE && td->o.allow_create)
680 f->fd = dup(STDOUT_FILENO);
682 from_hash = file_lookup_open(f, flags);
683 } else if (td_read(td)) {
684 if (f->filetype == FIO_TYPE_CHAR && !read_only)
690 f->fd = dup(STDIN_FILENO);
692 from_hash = file_lookup_open(f, flags);
693 } else if (td_trim(td)) {
694 assert(!td_rw(td)); /* should have matched above */
696 from_hash = file_lookup_open(f, flags);
700 char buf[FIO_VERROR_SIZE];
703 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
704 flags &= ~FIO_O_NOATIME;
707 if (__e == EMFILE && file_close_shadow_fds(td))
710 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
712 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
713 log_err("fio: looks like your file system does not " \
714 "support direct=1/buffered=0\n");
717 td_verror(td, __e, buf);
721 if (!from_hash && f->fd != -1) {
722 if (add_file_hash(f)) {
726 * Stash away descriptor for later close. This is to
727 * work-around a "feature" on Linux, where a close of
728 * an fd that has been opened for write will trigger
729 * udev to call blkid to check partitions, fs id, etc.
730 * That pollutes the device cache, which can slow down
731 * unbuffered accesses.
733 if (f->shadow_fd == -1)
734 f->shadow_fd = f->fd;
737 * OK to ignore, we haven't done anything
740 ret = generic_close_file(td, f);
750 * This function i.e. get_file_size() is the default .get_file_size
751 * implementation of majority of I/O engines.
753 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
755 return get_file_size(td, f);
759 * open/close all files, so that ->real_file_size gets set
761 static int get_file_sizes(struct thread_data *td)
767 for_each_file(td, f, i) {
768 dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i,
771 if (td_io_get_file_size(td, f)) {
772 if (td->error != ENOENT) {
773 log_err("%s\n", td->verror);
781 * There are corner cases where we end up with -1 for
782 * ->real_file_size due to unsupported file type, etc.
783 * We then just set to size option value divided by number
784 * of files, similar to the way file ->io_size is set.
785 * stat(2) failure doesn't set ->real_file_size to -1.
787 if (f->real_file_size == -1ULL && td->o.size)
788 f->real_file_size = td->o.size / td->o.nr_files;
795 struct flist_head list;
802 * Get free number of bytes for each file on each unique mount.
804 static unsigned long long get_fs_free_counts(struct thread_data *td)
806 struct flist_head *n, *tmp;
807 unsigned long long ret = 0;
808 struct fio_mount *fm;
813 for_each_file(td, f, i) {
817 if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) {
818 if (f->real_file_size != -1ULL)
819 ret += f->real_file_size;
821 } else if (f->filetype != FIO_TYPE_FILE)
825 strncpy(buf, f->file_name, 255);
827 if (stat(buf, &sb) < 0) {
831 if (stat(buf, &sb) < 0)
836 flist_for_each(n, &list) {
837 fm = flist_entry(n, struct fio_mount, list);
838 if (fm->key == sb.st_dev)
847 fm = calloc(1, sizeof(*fm));
848 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
849 fm->base = basename(fm->__base);
851 flist_add(&fm->list, &list);
854 flist_for_each_safe(n, tmp, &list) {
855 unsigned long long sz;
857 fm = flist_entry(n, struct fio_mount, list);
858 flist_del(&fm->list);
860 sz = get_fs_free_size(fm->base);
861 if (sz && sz != -1ULL)
870 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
872 struct thread_options *o = &td->o;
873 unsigned long long align_bs;
874 unsigned long long offset;
876 if (o->file_append && f->filetype == FIO_TYPE_FILE)
877 return f->real_file_size;
879 if (o->start_offset_percent > 0) {
881 * if blockalign is provided, find the min across read, write,
884 if (fio_option_is_set(o, ba)) {
885 align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]);
886 align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs);
888 /* else take the minimum block size */
889 align_bs = td_min_bs(td);
892 /* calculate the raw offset */
893 offset = (f->real_file_size * o->start_offset_percent / 100) +
894 (td->subjob_number * o->offset_increment);
897 * block align the offset at the next available boundary at
898 * ceiling(offset / align_bs) * align_bs
900 offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs;
903 /* start_offset_percent not set */
904 offset = o->start_offset +
905 td->subjob_number * o->offset_increment;
912 * Open the files and setup files sizes, creating files if necessary.
914 int setup_files(struct thread_data *td)
916 unsigned long long total_size, extend_size;
917 struct thread_options *o = &td->o;
919 unsigned int i, nr_fs_extra = 0;
920 int err = 0, need_extend;
922 const unsigned int bs = td_min_bs(td);
925 dprint(FD_FILE, "setup files\n");
927 old_state = td_bump_runstate(td, TD_SETTING_UP);
929 if (o->read_iolog_file)
933 * Find out physical size of files or devices for this thread,
934 * before we determine I/O size and range of our targets.
935 * If ioengine defines a setup() method, it's responsible for
936 * opening the files and setting f->real_file_size to indicate
937 * the valid range for that file.
939 if (td->io_ops->setup)
940 err = td->io_ops->setup(td);
942 err = get_file_sizes(td);
948 * check sizes. if the files/devices do not exist and the size
949 * isn't passed to fio, abort.
952 for_each_file(td, f, i) {
954 if (f->real_file_size == -1ULL)
957 total_size += f->real_file_size;
961 td->fill_device_size = get_fs_free_counts(td);
964 * device/file sizes are zero and no size given, punt
966 if ((!total_size || total_size == -1ULL) && !o->size &&
967 !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device &&
968 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
969 log_err("%s: you need to specify size=\n", o->name);
970 td_verror(td, EINVAL, "total_file_size");
975 * Calculate per-file size and potential extra size for the
976 * first files, if needed (i.e. if we don't have a fixed size).
978 if (!o->file_size_low && o->nr_files) {
981 fs = o->size / o->nr_files;
982 all_fs = fs * o->nr_files;
984 if (all_fs < o->size)
985 nr_fs_extra = (o->size - all_fs) / bs;
989 * now file sizes are known, so we can set ->io_size. if size= is
990 * not given, ->io_size is just equal to ->real_file_size. if size
991 * is given, ->io_size is size / nr_files.
993 extend_size = total_size = 0;
995 for_each_file(td, f, i) {
996 f->file_offset = get_start_offset(td, f);
999 * Update ->io_size depending on options specified.
1000 * ->file_size_low being 0 means filesize option isn't set.
1001 * Non zero ->file_size_low equals ->file_size_high means
1002 * filesize option is set in a fixed size format.
1003 * Non zero ->file_size_low not equals ->file_size_high means
1004 * filesize option is set in a range format.
1006 if (!o->file_size_low) {
1008 * no file size or range given, file size is equal to
1009 * total size divided by number of files. If the size
1010 * doesn't divide nicely with the min blocksize,
1011 * make the first files bigger.
1020 * We normally don't come here for regular files, but
1021 * if the result is 0 for a regular file, set it to the
1022 * real file size. This could be size of the existing
1023 * one if it already exists, but otherwise will be set
1024 * to 0. A new file won't be created because
1025 * ->io_size + ->file_offset equals ->real_file_size.
1028 if (f->file_offset > f->real_file_size)
1030 f->io_size = f->real_file_size - f->file_offset;
1032 log_info("fio: file %s may be ignored\n",
1035 } else if (f->real_file_size < o->file_size_low ||
1036 f->real_file_size > o->file_size_high) {
1037 if (f->file_offset > o->file_size_low)
1040 * file size given. if it's fixed, use that. if it's a
1041 * range, generate a random size in-between.
1043 if (o->file_size_low == o->file_size_high)
1044 f->io_size = o->file_size_low - f->file_offset;
1046 f->io_size = get_rand_file_size(td)
1050 f->io_size = f->real_file_size - f->file_offset;
1052 if (f->io_size == -1ULL)
1055 if (o->size_percent) {
1058 file_size = f->io_size + f->file_offset;
1059 f->io_size = (file_size *
1060 o->size_percent) / 100;
1061 if (f->io_size > (file_size - f->file_offset))
1062 f->io_size = file_size - f->file_offset;
1064 f->io_size -= (f->io_size % td_min_bs(td));
1066 total_size += f->io_size;
1069 if (f->filetype == FIO_TYPE_FILE &&
1070 (f->io_size + f->file_offset) > f->real_file_size &&
1071 !td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1072 if (!o->create_on_open) {
1074 extend_size += (f->io_size + f->file_offset);
1075 fio_file_set_extend(f);
1077 f->real_file_size = f->io_size + f->file_offset;
1081 if (td->o.block_error_hist) {
1084 assert(td->o.nr_files == 1); /* checked in fixup_options */
1086 len = f->io_size / td->o.bs[DDIR_TRIM];
1087 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
1088 log_err("fio: cannot calculate block histogram with "
1089 "%d trim blocks, maximum %d\n",
1090 len, MAX_NR_BLOCK_INFOS);
1091 td_verror(td, EINVAL, "block_error_hist");
1095 td->ts.nr_block_infos = len;
1096 for (i = 0; i < len; i++)
1097 td->ts.block_infos[i] =
1098 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
1100 td->ts.nr_block_infos = 0;
1102 if (!o->size || (total_size && o->size > total_size))
1103 o->size = total_size;
1105 if (o->size < td_min_bs(td)) {
1106 log_err("fio: blocksize too large for data set\n");
1111 * See if we need to extend some files, typically needed when our
1112 * target regular files don't exist yet, but our jobs require them
1113 * initially due to read I/Os.
1117 if (output_format & FIO_OUTPUT_NORMAL) {
1118 log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n",
1120 need_extend > 1 ? "s" : "",
1122 need_extend > 1 ? "s" : "",
1123 need_extend > 1 ? "total " : "",
1127 for_each_file(td, f, i) {
1128 unsigned long long old_len = -1ULL, extend_len = -1ULL;
1130 if (!fio_file_extend(f))
1133 assert(f->filetype == FIO_TYPE_FILE);
1134 fio_file_clear_extend(f);
1135 if (!o->fill_device) {
1136 old_len = f->real_file_size;
1137 extend_len = f->io_size + f->file_offset -
1140 f->real_file_size = (f->io_size + f->file_offset);
1141 err = extend_file(td, f);
1145 err = __file_invalidate_cache(td, f, old_len,
1149 * Shut up static checker
1165 o->zone_size = o->size;
1168 * iolog already set the total io size, if we read back
1171 if (!o->read_iolog_file) {
1173 td->total_io_size = o->io_size * o->loops;
1175 td->total_io_size = o->size * o->loops;
1182 td_restore_runstate(td, old_state);
1185 log_err("%s: you need to specify valid offset=\n", o->name);
1187 td_restore_runstate(td, old_state);
1191 int pre_read_files(struct thread_data *td)
1196 dprint(FD_FILE, "pre_read files\n");
1198 for_each_file(td, f, i) {
1199 if (pre_read_file(td, f))
1206 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1208 unsigned int range_size, seed;
1209 unsigned long nranges;
1212 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1213 fsize = min(f->real_file_size, f->io_size);
1215 nranges = (fsize + range_size - 1) / range_size;
1217 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1218 if (!td->o.rand_repeatable)
1219 seed = td->rand_seeds[4];
1221 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1222 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1223 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
1224 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1225 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
1226 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1231 static int init_rand_distribution(struct thread_data *td)
1237 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1240 state = td_bump_runstate(td, TD_SETTING_UP);
1242 for_each_file(td, f, i)
1243 __init_rand_distribution(td, f);
1245 td_restore_runstate(td, state);
1251 * Check if the number of blocks exceeds the randomness capability of
1252 * the selected generator. Tausworthe is 32-bit, the others are fullly
1255 static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f,
1258 if (blocks <= FRAND32_MAX)
1260 if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE)
1264 * If the user hasn't specified a random generator, switch
1265 * to tausworthe64 with informational warning. If the user did
1266 * specify one, just warn.
1268 log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n",
1271 if (!fio_option_is_set(&td->o, random_generator)) {
1272 log_info("fio: Switching to tausworthe64. Use the "
1273 "random_generator= option to get rid of this "
1275 td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64;
1280 * Just make this information to avoid breaking scripts.
1282 log_info("fio: Use the random_generator= option to switch to lfsr or "
1287 int init_random_map(struct thread_data *td)
1289 unsigned long long blocks;
1293 if (init_rand_distribution(td))
1298 for_each_file(td, f, i) {
1299 uint64_t fsize = min(f->real_file_size, f->io_size);
1301 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1303 if (check_rand_gen_limits(td, f, blocks))
1306 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1309 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1311 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1312 fio_file_set_lfsr(f);
1315 } else if (!td->o.norandommap) {
1316 f->io_axmap = axmap_new(blocks);
1318 fio_file_set_axmap(f);
1321 } else if (td->o.norandommap)
1324 if (!td->o.softrandommap) {
1325 log_err("fio: failed allocating random map. If running"
1326 " a large number of jobs, try the 'norandommap'"
1327 " option or set 'softrandommap'. Or give"
1328 " a larger --alloc-size to fio.\n");
1332 log_info("fio: file %s failed allocating random map. Running "
1333 "job without.\n", f->file_name);
1339 void close_files(struct thread_data *td)
1344 for_each_file(td, f, i) {
1345 if (fio_file_open(f))
1346 td_io_close_file(td, f);
1350 void close_and_free_files(struct thread_data *td)
1355 dprint(FD_FILE, "close files\n");
1357 for_each_file(td, f, i) {
1358 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1359 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1360 td_io_unlink_file(td, f);
1363 if (fio_file_open(f))
1364 td_io_close_file(td, f);
1366 remove_file_hash(f);
1368 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1369 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1370 td_io_unlink_file(td, f);
1373 sfree(f->file_name);
1374 f->file_name = NULL;
1375 if (fio_file_axmap(f)) {
1376 axmap_free(f->io_axmap);
1382 td->o.filename = NULL;
1384 free(td->file_locks);
1385 td->files_index = 0;
1387 td->file_locks = NULL;
1388 td->o.file_lock_mode = FILE_LOCK_NONE;
1392 static void get_file_type(struct fio_file *f)
1396 if (!strcmp(f->file_name, "-"))
1397 f->filetype = FIO_TYPE_PIPE;
1399 f->filetype = FIO_TYPE_FILE;
1402 /* \\.\ is the device namespace in Windows, where every file is
1404 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1405 f->filetype = FIO_TYPE_BLOCK;
1408 if (!stat(f->file_name, &sb)) {
1409 if (S_ISBLK(sb.st_mode))
1410 f->filetype = FIO_TYPE_BLOCK;
1411 else if (S_ISCHR(sb.st_mode))
1412 f->filetype = FIO_TYPE_CHAR;
1413 else if (S_ISFIFO(sb.st_mode))
1414 f->filetype = FIO_TYPE_PIPE;
1418 static bool __is_already_allocated(const char *fname, bool set)
1420 struct flist_head *entry;
1423 ret = file_bloom_exists(fname, set);
1427 flist_for_each(entry, &filename_list) {
1428 struct file_name *fn;
1430 fn = flist_entry(entry, struct file_name, list);
1432 if (!strcmp(fn->filename, fname))
1439 static bool is_already_allocated(const char *fname)
1443 fio_file_hash_lock();
1444 ret = __is_already_allocated(fname, false);
1445 fio_file_hash_unlock();
1450 static void set_already_allocated(const char *fname)
1452 struct file_name *fn;
1454 fn = malloc(sizeof(struct file_name));
1455 fn->filename = strdup(fname);
1457 fio_file_hash_lock();
1458 if (!__is_already_allocated(fname, true)) {
1459 flist_add_tail(&fn->list, &filename_list);
1462 fio_file_hash_unlock();
1470 static void free_already_allocated(void)
1472 struct flist_head *entry, *tmp;
1473 struct file_name *fn;
1475 if (flist_empty(&filename_list))
1478 fio_file_hash_lock();
1479 flist_for_each_safe(entry, tmp, &filename_list) {
1480 fn = flist_entry(entry, struct file_name, list);
1482 flist_del(&fn->list);
1486 fio_file_hash_unlock();
1489 static struct fio_file *alloc_new_file(struct thread_data *td)
1493 f = smalloc(sizeof(*f));
1501 fio_file_reset(td, f);
1505 bool exists_and_not_regfile(const char *filename)
1509 if (lstat(filename, &sb) == -1)
1512 #ifndef WIN32 /* NOT Windows */
1513 if (S_ISREG(sb.st_mode))
1516 /* \\.\ is the device namespace in Windows, where every file
1517 * is a device node */
1518 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
1525 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1527 int cur_files = td->files_index;
1528 char file_name[PATH_MAX];
1532 dprint(FD_FILE, "add file %s\n", fname);
1534 if (td->o.directory)
1535 len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob,
1536 td->o.unique_filename);
1538 sprintf(file_name + len, "%s", fname);
1540 /* clean cloned siblings using existing files */
1541 if (numjob && is_already_allocated(file_name) &&
1542 !exists_and_not_regfile(fname))
1545 f = alloc_new_file(td);
1547 if (td->files_size <= td->files_index) {
1548 unsigned int new_size = td->o.nr_files + 1;
1550 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1552 td->files = realloc(td->files, new_size * sizeof(f));
1553 if (td->files == NULL) {
1554 log_err("fio: realloc OOM\n");
1557 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1558 td->file_locks = realloc(td->file_locks, new_size);
1559 if (!td->file_locks) {
1560 log_err("fio: realloc OOM\n");
1563 td->file_locks[cur_files] = FILE_LOCK_NONE;
1565 td->files_size = new_size;
1567 td->files[cur_files] = f;
1568 f->fileno = cur_files;
1571 * init function, io engine may not be loaded yet
1573 if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
1574 f->real_file_size = -1ULL;
1576 f->file_name = smalloc_strdup(file_name);
1582 switch (td->o.file_lock_mode) {
1583 case FILE_LOCK_NONE:
1585 case FILE_LOCK_READWRITE:
1586 f->rwlock = fio_rwlock_init();
1588 case FILE_LOCK_EXCLUSIVE:
1589 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1592 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1597 if (f->filetype == FIO_TYPE_FILE)
1598 td->nr_normal_files++;
1600 set_already_allocated(file_name);
1605 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1611 int add_file_exclusive(struct thread_data *td, const char *fname)
1616 for_each_file(td, f, i) {
1617 if (!strcmp(f->file_name, fname))
1621 return add_file(td, fname, 0, 1);
1624 void get_file(struct fio_file *f)
1626 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1627 assert(fio_file_open(f));
1631 int put_file(struct thread_data *td, struct fio_file *f)
1633 int f_ret = 0, ret = 0;
1635 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1637 if (!fio_file_open(f)) {
1638 assert(f->fd == -1);
1642 assert(f->references);
1643 if (--f->references)
1646 if (should_fsync(td) && td->o.fsync_on_close) {
1647 f_ret = fsync(f->fd);
1652 if (td->io_ops->close_file)
1653 ret = td->io_ops->close_file(td, f);
1658 td->nr_open_files--;
1659 fio_file_clear_open(f);
1660 assert(f->fd == -1);
1664 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1666 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1669 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1670 if (ddir == DDIR_READ)
1671 fio_rwlock_read(f->rwlock);
1673 fio_rwlock_write(f->rwlock);
1674 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1675 fio_mutex_down(f->lock);
1677 td->file_locks[f->fileno] = td->o.file_lock_mode;
1680 void unlock_file(struct thread_data *td, struct fio_file *f)
1682 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1685 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1686 fio_rwlock_unlock(f->rwlock);
1687 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1688 fio_mutex_up(f->lock);
1690 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1693 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1695 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1697 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1701 static int recurse_dir(struct thread_data *td, const char *dirname)
1707 D = opendir(dirname);
1709 char buf[FIO_VERROR_SIZE];
1711 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1712 td_verror(td, errno, buf);
1716 while ((dir = readdir(D)) != NULL) {
1717 char full_path[PATH_MAX];
1720 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1723 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1725 if (lstat(full_path, &sb) == -1) {
1726 if (errno != ENOENT) {
1727 td_verror(td, errno, "stat");
1733 if (S_ISREG(sb.st_mode)) {
1734 add_file(td, full_path, 0, 1);
1737 if (!S_ISDIR(sb.st_mode))
1740 ret = recurse_dir(td, full_path);
1749 int add_dir_files(struct thread_data *td, const char *path)
1751 int ret = recurse_dir(td, path);
1754 log_info("fio: opendir added %d files\n", td->o.nr_files);
1759 void dup_files(struct thread_data *td, struct thread_data *org)
1764 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1769 td->files = malloc(org->files_index * sizeof(f));
1771 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1772 td->file_locks = malloc(org->files_index);
1774 for_each_file(org, f, i) {
1775 struct fio_file *__f;
1777 __f = alloc_new_file(td);
1780 __f->file_name = smalloc_strdup(f->file_name);
1781 if (!__f->file_name)
1784 __f->filetype = f->filetype;
1787 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1788 __f->lock = f->lock;
1789 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1790 __f->rwlock = f->rwlock;
1797 * Returns the index that matches the filename, or -1 if not there
1799 int get_fileno(struct thread_data *td, const char *fname)
1804 for_each_file(td, f, i)
1805 if (!strcmp(f->file_name, fname))
1812 * For log usage, where we add/open/close files automatically
1814 void free_release_files(struct thread_data *td)
1818 td->o.open_files = 0;
1819 td->files_index = 0;
1820 td->nr_normal_files = 0;
1823 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1827 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1828 f->last_pos[i] = f->file_offset;
1829 f->last_start[i] = -1ULL;
1832 if (fio_file_axmap(f))
1833 axmap_reset(f->io_axmap);
1834 else if (fio_file_lfsr(f))
1835 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1838 bool fio_files_done(struct thread_data *td)
1843 for_each_file(td, f, i)
1844 if (!fio_file_done(f))
1850 /* free memory used in initialization phase only */
1851 void filesetup_mem_free(void)
1853 free_already_allocated();