17 #include "lib/axmap.h"
19 #ifdef CONFIG_LINUX_FALLOCATE
20 #include <linux/falloc.h>
25 static FLIST_HEAD(filename_list);
28 * List entry for filename_list
31 struct flist_head list;
35 static inline void clear_error(struct thread_data *td)
42 * Leaves f->fd open on success, caller must close
44 static int extend_file(struct thread_data *td, struct fio_file *f)
46 int r, new_layout = 0, unlink_file = 0, flags;
47 unsigned long long left;
52 log_err("fio: refusing extend of file due to read-only\n");
57 * check if we need to lay the file out complete again. fio
58 * does that for operations involving reads, or for writes
59 * where overwrite is set
62 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
63 (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND)))
65 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
68 if (unlink_file || new_layout) {
71 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
73 ret = td_io_unlink_file(td, f);
74 if (ret != 0 && ret != ENOENT) {
75 td_verror(td, errno, "unlink");
81 if (td->o.allow_create)
90 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
91 f->fd = open(f->file_name, flags, 0644);
95 if (err == ENOENT && !td->o.allow_create)
96 log_err("fio: file creation disallowed by "
97 "allow_file_create=0\n");
99 td_verror(td, err, "open");
103 #ifdef CONFIG_POSIX_FALLOCATE
104 if (!td->o.fill_device) {
105 switch (td->o.fallocate_mode) {
106 case FIO_FALLOCATE_NONE:
108 case FIO_FALLOCATE_POSIX:
109 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
111 (unsigned long long) f->real_file_size);
113 r = posix_fallocate(f->fd, 0, f->real_file_size);
115 log_err("fio: posix_fallocate fails: %s\n",
119 #ifdef CONFIG_LINUX_FALLOCATE
120 case FIO_FALLOCATE_KEEP_SIZE:
122 "fallocate(FALLOC_FL_KEEP_SIZE) "
123 "file %s size %llu\n", f->file_name,
124 (unsigned long long) f->real_file_size);
126 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
129 td_verror(td, errno, "fallocate");
132 #endif /* CONFIG_LINUX_FALLOCATE */
134 log_err("fio: unknown fallocate mode: %d\n",
135 td->o.fallocate_mode);
139 #endif /* CONFIG_POSIX_FALLOCATE */
142 * If our jobs don't require regular files initially, we're done.
148 * The size will be -1ULL when fill_device is used, so don't truncate
149 * or fallocate this file, just write it
151 if (!td->o.fill_device) {
152 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
153 (unsigned long long) f->real_file_size);
154 if (ftruncate(f->fd, f->real_file_size) == -1) {
155 if (errno != EFBIG) {
156 td_verror(td, errno, "ftruncate");
162 left = f->real_file_size;
163 bs = td->o.max_bs[DDIR_WRITE];
169 td_verror(td, errno, "malloc");
173 while (left && !td->terminate) {
177 fill_io_buffer(td, b, bs, bs);
179 r = write(f->fd, b, bs);
189 if (td->o.fill_device)
191 log_info("fio: ENOSPC on laying out "
195 td_verror(td, errno, "write");
197 td_verror(td, EIO, "write");
204 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
205 td_io_unlink_file(td, f);
206 } else if (td->o.create_fsync) {
207 if (fsync(f->fd) < 0) {
208 td_verror(td, errno, "fsync");
212 if (td->o.fill_device && !td_write(td)) {
213 fio_file_clear_size_known(f);
214 if (td_io_get_file_size(td, f))
216 if (f->io_size > f->real_file_size)
217 f->io_size = f->real_file_size;
231 static int pre_read_file(struct thread_data *td, struct fio_file *f)
233 int ret = 0, r, did_open = 0, old_runstate;
234 unsigned long long left;
238 if (td_ioengine_flagged(td, FIO_PIPEIO))
241 if (!fio_file_open(f)) {
242 if (td->io_ops->open_file(td, f)) {
243 log_err("fio: cannot pre-read, failed to open file\n");
249 old_runstate = td_bump_runstate(td, TD_PRE_READING);
252 bs = td->o.max_bs[DDIR_READ];
258 td_verror(td, errno, "malloc");
264 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
265 td_verror(td, errno, "lseek");
266 log_err("fio: failed to lseek pre-read file\n");
271 while (left && !td->terminate) {
275 r = read(f->fd, b, bs);
281 td_verror(td, EIO, "pre_read");
287 td_restore_runstate(td, old_runstate);
290 td->io_ops->close_file(td, f);
296 unsigned long long get_rand_file_size(struct thread_data *td)
298 unsigned long long ret, sized;
302 frand_max = rand_max(&td->file_size_state);
303 r = __rand(&td->file_size_state);
304 sized = td->o.file_size_high - td->o.file_size_low;
305 ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0)));
306 ret += td->o.file_size_low;
307 ret -= (ret % td->o.rw_min_bs);
311 static int file_size(struct thread_data *td, struct fio_file *f)
315 if (stat(f->file_name, &st) == -1) {
316 td_verror(td, errno, "fstat");
320 f->real_file_size = st.st_size;
324 static int bdev_size(struct thread_data *td, struct fio_file *f)
326 unsigned long long bytes = 0;
329 if (td->io_ops->open_file(td, f)) {
330 log_err("fio: failed opening blockdev %s for size check\n",
335 r = blockdev_size(f, &bytes);
337 td_verror(td, r, "blockdev_size");
342 log_err("%s: zero sized block device?\n", f->file_name);
346 f->real_file_size = bytes;
347 td->io_ops->close_file(td, f);
350 td->io_ops->close_file(td, f);
354 static int char_size(struct thread_data *td, struct fio_file *f)
356 #ifdef FIO_HAVE_CHARDEV_SIZE
357 unsigned long long bytes = 0;
360 if (td->io_ops->open_file(td, f)) {
361 log_err("fio: failed opening chardev %s for size check\n",
366 r = chardev_size(f, &bytes);
368 td_verror(td, r, "chardev_size");
373 log_err("%s: zero sized char device?\n", f->file_name);
377 f->real_file_size = bytes;
378 td->io_ops->close_file(td, f);
381 td->io_ops->close_file(td, f);
384 f->real_file_size = -1ULL;
389 static int get_file_size(struct thread_data *td, struct fio_file *f)
393 if (fio_file_size_known(f))
396 if (f->filetype == FIO_TYPE_FILE)
397 ret = file_size(td, f);
398 else if (f->filetype == FIO_TYPE_BLOCK)
399 ret = bdev_size(td, f);
400 else if (f->filetype == FIO_TYPE_CHAR)
401 ret = char_size(td, f);
403 f->real_file_size = -1ULL;
406 * Leave ->real_file_size with 0 since it could be expectation
407 * of initial setup for regular files.
413 * If ->real_file_size is -1, a conditional for the message
414 * "offset extends end" is always true, but it makes no sense,
415 * so just return the same value here.
417 if (f->real_file_size == -1ULL) {
418 log_info("%s: failed to get file size of %s\n", td->o.name,
423 if (td->o.start_offset && f->file_offset == 0)
424 dprint(FD_FILE, "offset of file %s not initialized yet\n",
427 * ->file_offset normally hasn't been initialized yet, so this
428 * is basically always false.
430 if (f->file_offset > f->real_file_size) {
431 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
432 (unsigned long long) f->file_offset,
433 (unsigned long long) f->real_file_size);
437 fio_file_set_size_known(f);
441 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
442 unsigned long long off,
443 unsigned long long len)
445 int errval = 0, ret = 0;
454 off = f->file_offset;
456 if (len == -1ULL || off == -1ULL)
459 if (td->io_ops->invalidate) {
460 dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name,
462 ret = td->io_ops->invalidate(td, f);
465 } else if (f->filetype == FIO_TYPE_FILE) {
466 dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n",
467 f->file_name, off, len);
468 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
471 } else if (f->filetype == FIO_TYPE_BLOCK) {
474 dprint(FD_IO, "drop page cache %s\n", f->file_name);
475 ret = blockdev_invalidate_cache(f);
476 while (ret < 0 && errno == EAGAIN && retry_count++ < 25) {
478 * Linux multipath devices reject ioctl while
479 * the maps are being updated. That window can
480 * last tens of milliseconds; we'll try up to
481 * a quarter of a second.
484 ret = blockdev_invalidate_cache(f);
486 if (ret < 0 && errno == EACCES && geteuid()) {
488 log_err("fio: only root may flush block "
489 "devices. Cache flush bypassed!\n");
496 else if (ret) /* probably not supported */
498 } else if (f->filetype == FIO_TYPE_CHAR ||
499 f->filetype == FIO_TYPE_PIPE) {
500 dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
505 * Cache flushing isn't a fatal condition, and we know it will
506 * happen on some platforms where we don't have the proper
507 * function to flush eg block device caches. So just warn and
508 * continue on our way.
511 log_info("fio: cache invalidation of %s failed: %s\n",
512 f->file_name, strerror(errval));
518 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
520 if (!fio_file_open(f))
523 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
526 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
530 dprint(FD_FILE, "fd close %s\n", f->file_name);
534 if (close(f->fd) < 0)
539 if (f->shadow_fd != -1) {
548 int file_lookup_open(struct fio_file *f, int flags)
550 struct fio_file *__f;
553 __f = lookup_file_hash(f->file_name);
555 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
559 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
567 f->fd = open(f->file_name, flags, 0600);
571 static int file_close_shadow_fds(struct thread_data *td)
577 for_each_file(td, f, i) {
578 if (f->shadow_fd == -1)
589 int generic_open_file(struct thread_data *td, struct fio_file *f)
595 dprint(FD_FILE, "fd open %s\n", f->file_name);
597 if (!strcmp(f->file_name, "-")) {
599 log_err("fio: can't read/write to stdin/out\n");
605 * move output logging to stderr, if we are writing to stdout
614 flags |= OS_O_DIRECT;
617 td_verror(td, EINVAL, "OS does not support atomic IO");
620 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
624 if (td->o.create_on_open && td->o.allow_create)
627 if (f->filetype != FIO_TYPE_FILE)
628 flags |= FIO_O_NOATIME;
635 if (f->filetype == FIO_TYPE_FILE && td->o.allow_create)
639 f->fd = dup(STDOUT_FILENO);
641 from_hash = file_lookup_open(f, flags);
642 } else if (td_read(td)) {
643 if (f->filetype == FIO_TYPE_CHAR && !read_only)
649 f->fd = dup(STDIN_FILENO);
651 from_hash = file_lookup_open(f, flags);
652 } else if (td_trim(td)) {
653 assert(!td_rw(td)); /* should have matched above */
655 from_hash = file_lookup_open(f, flags);
659 char buf[FIO_VERROR_SIZE];
662 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
663 flags &= ~FIO_O_NOATIME;
666 if (__e == EMFILE && file_close_shadow_fds(td))
669 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
671 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
672 log_err("fio: looks like your file system does not " \
673 "support direct=1/buffered=0\n");
676 td_verror(td, __e, buf);
680 if (!from_hash && f->fd != -1) {
681 if (add_file_hash(f)) {
685 * Stash away descriptor for later close. This is to
686 * work-around a "feature" on Linux, where a close of
687 * an fd that has been opened for write will trigger
688 * udev to call blkid to check partitions, fs id, etc.
689 * That pollutes the device cache, which can slow down
690 * unbuffered accesses.
692 if (f->shadow_fd == -1)
693 f->shadow_fd = f->fd;
696 * OK to ignore, we haven't done anything
699 ret = generic_close_file(td, f);
709 * This function i.e. get_file_size() is the default .get_file_size
710 * implementation of majority of I/O engines.
712 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
714 return get_file_size(td, f);
718 * open/close all files, so that ->real_file_size gets set
720 static int get_file_sizes(struct thread_data *td)
726 for_each_file(td, f, i) {
727 dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i,
730 if (td_io_get_file_size(td, f)) {
731 if (td->error != ENOENT) {
732 log_err("%s\n", td->verror);
740 * There are corner cases where we end up with -1 for
741 * ->real_file_size due to unsupported file type, etc.
742 * We then just set to size option value divided by number
743 * of files, similar to the way file ->io_size is set.
744 * stat(2) failure doesn't set ->real_file_size to -1.
746 if (f->real_file_size == -1ULL && td->o.size)
747 f->real_file_size = td->o.size / td->o.nr_files;
754 struct flist_head list;
761 * Get free number of bytes for each file on each unique mount.
763 static unsigned long long get_fs_free_counts(struct thread_data *td)
765 struct flist_head *n, *tmp;
766 unsigned long long ret = 0;
767 struct fio_mount *fm;
772 for_each_file(td, f, i) {
776 if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) {
777 if (f->real_file_size != -1ULL)
778 ret += f->real_file_size;
780 } else if (f->filetype != FIO_TYPE_FILE)
784 strncpy(buf, f->file_name, 255);
786 if (stat(buf, &sb) < 0) {
790 if (stat(buf, &sb) < 0)
795 flist_for_each(n, &list) {
796 fm = flist_entry(n, struct fio_mount, list);
797 if (fm->key == sb.st_dev)
806 fm = calloc(1, sizeof(*fm));
807 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
808 fm->base = basename(fm->__base);
810 flist_add(&fm->list, &list);
813 flist_for_each_safe(n, tmp, &list) {
814 unsigned long long sz;
816 fm = flist_entry(n, struct fio_mount, list);
817 flist_del(&fm->list);
819 sz = get_fs_free_size(fm->base);
820 if (sz && sz != -1ULL)
829 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
831 struct thread_options *o = &td->o;
833 if (o->file_append && f->filetype == FIO_TYPE_FILE)
834 return f->real_file_size;
836 return td->o.start_offset +
837 td->subjob_number * td->o.offset_increment;
841 * Open the files and setup files sizes, creating files if necessary.
843 int setup_files(struct thread_data *td)
845 unsigned long long total_size, extend_size;
846 struct thread_options *o = &td->o;
848 unsigned int i, nr_fs_extra = 0;
849 int err = 0, need_extend;
851 const unsigned int bs = td_min_bs(td);
854 dprint(FD_FILE, "setup files\n");
856 old_state = td_bump_runstate(td, TD_SETTING_UP);
858 if (o->read_iolog_file)
862 * Find out physical size of files or devices for this thread,
863 * before we determine I/O size and range of our targets.
864 * If ioengine defines a setup() method, it's responsible for
865 * opening the files and setting f->real_file_size to indicate
866 * the valid range for that file.
868 if (td->io_ops->setup)
869 err = td->io_ops->setup(td);
871 err = get_file_sizes(td);
877 * check sizes. if the files/devices do not exist and the size
878 * isn't passed to fio, abort.
881 for_each_file(td, f, i) {
883 if (f->real_file_size == -1ULL)
886 total_size += f->real_file_size;
890 td->fill_device_size = get_fs_free_counts(td);
893 * device/file sizes are zero and no size given, punt
895 if ((!total_size || total_size == -1ULL) && !o->size &&
896 !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device &&
897 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
898 log_err("%s: you need to specify size=\n", o->name);
899 td_verror(td, EINVAL, "total_file_size");
904 * Calculate per-file size and potential extra size for the
905 * first files, if needed (i.e. if we don't have a fixed size).
907 if (!o->file_size_low && o->nr_files) {
910 fs = o->size / o->nr_files;
911 all_fs = fs * o->nr_files;
913 if (all_fs < o->size)
914 nr_fs_extra = (o->size - all_fs) / bs;
918 * now file sizes are known, so we can set ->io_size. if size= is
919 * not given, ->io_size is just equal to ->real_file_size. if size
920 * is given, ->io_size is size / nr_files.
922 extend_size = total_size = 0;
924 for_each_file(td, f, i) {
925 f->file_offset = get_start_offset(td, f);
928 * Update ->io_size depending on options specified.
929 * ->file_size_low being 0 means filesize option isn't set.
930 * Non zero ->file_size_low equals ->file_size_high means
931 * filesize option is set in a fixed size format.
932 * Non zero ->file_size_low not equals ->file_size_high means
933 * filesize option is set in a range format.
935 if (!o->file_size_low) {
937 * no file size or range given, file size is equal to
938 * total size divided by number of files. If the size
939 * doesn't divide nicely with the min blocksize,
940 * make the first files bigger.
949 * We normally don't come here for regular files, but
950 * if the result is 0 for a regular file, set it to the
951 * real file size. This could be size of the existing
952 * one if it already exists, but otherwise will be set
953 * to 0. A new file won't be created because
954 * ->io_size + ->file_offset equals ->real_file_size.
957 if (f->file_offset > f->real_file_size)
959 f->io_size = f->real_file_size - f->file_offset;
961 log_info("fio: file %s may be ignored\n",
964 } else if (f->real_file_size < o->file_size_low ||
965 f->real_file_size > o->file_size_high) {
966 if (f->file_offset > o->file_size_low)
969 * file size given. if it's fixed, use that. if it's a
970 * range, generate a random size in-between.
972 if (o->file_size_low == o->file_size_high)
973 f->io_size = o->file_size_low - f->file_offset;
975 f->io_size = get_rand_file_size(td)
979 f->io_size = f->real_file_size - f->file_offset;
981 if (f->io_size == -1ULL)
984 if (o->size_percent) {
985 f->io_size = (f->io_size * o->size_percent) / 100;
986 f->io_size -= (f->io_size % td_min_bs(td));
988 total_size += f->io_size;
991 if (f->filetype == FIO_TYPE_FILE &&
992 (f->io_size + f->file_offset) > f->real_file_size &&
993 !td_ioengine_flagged(td, FIO_DISKLESSIO)) {
994 if (!o->create_on_open) {
996 extend_size += (f->io_size + f->file_offset);
997 fio_file_set_extend(f);
999 f->real_file_size = f->io_size + f->file_offset;
1003 if (td->o.block_error_hist) {
1006 assert(td->o.nr_files == 1); /* checked in fixup_options */
1008 len = f->io_size / td->o.bs[DDIR_TRIM];
1009 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
1010 log_err("fio: cannot calculate block histogram with "
1011 "%d trim blocks, maximum %d\n",
1012 len, MAX_NR_BLOCK_INFOS);
1013 td_verror(td, EINVAL, "block_error_hist");
1017 td->ts.nr_block_infos = len;
1018 for (i = 0; i < len; i++)
1019 td->ts.block_infos[i] =
1020 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
1022 td->ts.nr_block_infos = 0;
1024 if (!o->size || (total_size && o->size > total_size))
1025 o->size = total_size;
1027 if (o->size < td_min_bs(td)) {
1028 log_err("fio: blocksize too large for data set\n");
1033 * See if we need to extend some files, typically needed when our
1034 * target regular files don't exist yet, but our jobs require them
1035 * initially due to read I/Os.
1039 if (output_format & FIO_OUTPUT_NORMAL) {
1040 log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n",
1042 need_extend > 1 ? "s" : "",
1044 need_extend > 1 ? "s" : "",
1045 need_extend > 1 ? "total " : "",
1049 for_each_file(td, f, i) {
1050 unsigned long long old_len = -1ULL, extend_len = -1ULL;
1052 if (!fio_file_extend(f))
1055 assert(f->filetype == FIO_TYPE_FILE);
1056 fio_file_clear_extend(f);
1057 if (!o->fill_device) {
1058 old_len = f->real_file_size;
1059 extend_len = f->io_size + f->file_offset -
1062 f->real_file_size = (f->io_size + f->file_offset);
1063 err = extend_file(td, f);
1067 err = __file_invalidate_cache(td, f, old_len,
1071 * Shut up static checker
1087 o->zone_size = o->size;
1090 * iolog already set the total io size, if we read back
1093 if (!o->read_iolog_file) {
1095 td->total_io_size = o->io_size * o->loops;
1097 td->total_io_size = o->size * o->loops;
1104 td_restore_runstate(td, old_state);
1107 log_err("%s: you need to specify valid offset=\n", o->name);
1109 td_restore_runstate(td, old_state);
1113 int pre_read_files(struct thread_data *td)
1118 dprint(FD_FILE, "pre_read files\n");
1120 for_each_file(td, f, i) {
1121 pre_read_file(td, f);
1127 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1129 unsigned int range_size, seed;
1130 unsigned long nranges;
1133 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1134 fsize = min(f->real_file_size, f->io_size);
1136 nranges = (fsize + range_size - 1) / range_size;
1138 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1139 if (!td->o.rand_repeatable)
1140 seed = td->rand_seeds[4];
1142 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1143 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1144 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
1145 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1146 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
1147 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1152 static int init_rand_distribution(struct thread_data *td)
1158 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1161 state = td_bump_runstate(td, TD_SETTING_UP);
1163 for_each_file(td, f, i)
1164 __init_rand_distribution(td, f);
1166 td_restore_runstate(td, state);
1172 * Check if the number of blocks exceeds the randomness capability of
1173 * the selected generator. Tausworthe is 32-bit, the others are fullly
1176 static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f,
1179 if (blocks <= FRAND32_MAX)
1181 if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE)
1185 * If the user hasn't specified a random generator, switch
1186 * to tausworthe64 with informational warning. If the user did
1187 * specify one, just warn.
1189 log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n",
1192 if (!fio_option_is_set(&td->o, random_generator)) {
1193 log_info("fio: Switching to tausworthe64. Use the "
1194 "random_generator= option to get rid of this "
1196 td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64;
1201 * Just make this information to avoid breaking scripts.
1203 log_info("fio: Use the random_generator= option to switch to lfsr or "
1208 int init_random_map(struct thread_data *td)
1210 unsigned long long blocks;
1214 if (init_rand_distribution(td))
1219 for_each_file(td, f, i) {
1220 uint64_t fsize = min(f->real_file_size, f->io_size);
1222 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1224 if (check_rand_gen_limits(td, f, blocks))
1227 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1230 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1232 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1233 fio_file_set_lfsr(f);
1236 } else if (!td->o.norandommap) {
1237 f->io_axmap = axmap_new(blocks);
1239 fio_file_set_axmap(f);
1242 } else if (td->o.norandommap)
1245 if (!td->o.softrandommap) {
1246 log_err("fio: failed allocating random map. If running"
1247 " a large number of jobs, try the 'norandommap'"
1248 " option or set 'softrandommap'. Or give"
1249 " a larger --alloc-size to fio.\n");
1253 log_info("fio: file %s failed allocating random map. Running "
1254 "job without.\n", f->file_name);
1260 void close_files(struct thread_data *td)
1265 for_each_file(td, f, i) {
1266 if (fio_file_open(f))
1267 td_io_close_file(td, f);
1271 void close_and_free_files(struct thread_data *td)
1276 dprint(FD_FILE, "close files\n");
1278 for_each_file(td, f, i) {
1279 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1280 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1281 td_io_unlink_file(td, f);
1284 if (fio_file_open(f))
1285 td_io_close_file(td, f);
1287 remove_file_hash(f);
1289 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1290 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1291 td_io_unlink_file(td, f);
1294 sfree(f->file_name);
1295 f->file_name = NULL;
1296 if (fio_file_axmap(f)) {
1297 axmap_free(f->io_axmap);
1303 td->o.filename = NULL;
1305 free(td->file_locks);
1306 td->files_index = 0;
1308 td->file_locks = NULL;
1309 td->o.file_lock_mode = FILE_LOCK_NONE;
1313 static void get_file_type(struct fio_file *f)
1317 if (!strcmp(f->file_name, "-"))
1318 f->filetype = FIO_TYPE_PIPE;
1320 f->filetype = FIO_TYPE_FILE;
1323 /* \\.\ is the device namespace in Windows, where every file is
1325 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1326 f->filetype = FIO_TYPE_BLOCK;
1329 if (!stat(f->file_name, &sb)) {
1330 if (S_ISBLK(sb.st_mode))
1331 f->filetype = FIO_TYPE_BLOCK;
1332 else if (S_ISCHR(sb.st_mode))
1333 f->filetype = FIO_TYPE_CHAR;
1334 else if (S_ISFIFO(sb.st_mode))
1335 f->filetype = FIO_TYPE_PIPE;
1339 static bool __is_already_allocated(const char *fname, bool set)
1341 struct flist_head *entry;
1344 ret = file_bloom_exists(fname, set);
1348 flist_for_each(entry, &filename_list) {
1349 struct file_name *fn;
1351 fn = flist_entry(entry, struct file_name, list);
1353 if (!strcmp(fn->filename, fname))
1360 static bool is_already_allocated(const char *fname)
1364 fio_file_hash_lock();
1365 ret = __is_already_allocated(fname, false);
1366 fio_file_hash_unlock();
1371 static void set_already_allocated(const char *fname)
1373 struct file_name *fn;
1375 fn = malloc(sizeof(struct file_name));
1376 fn->filename = strdup(fname);
1378 fio_file_hash_lock();
1379 if (!__is_already_allocated(fname, true)) {
1380 flist_add_tail(&fn->list, &filename_list);
1383 fio_file_hash_unlock();
1391 static void free_already_allocated(void)
1393 struct flist_head *entry, *tmp;
1394 struct file_name *fn;
1396 if (flist_empty(&filename_list))
1399 fio_file_hash_lock();
1400 flist_for_each_safe(entry, tmp, &filename_list) {
1401 fn = flist_entry(entry, struct file_name, list);
1403 flist_del(&fn->list);
1407 fio_file_hash_unlock();
1410 static struct fio_file *alloc_new_file(struct thread_data *td)
1414 f = smalloc(sizeof(*f));
1422 fio_file_reset(td, f);
1426 bool exists_and_not_regfile(const char *filename)
1430 if (lstat(filename, &sb) == -1)
1433 #ifndef WIN32 /* NOT Windows */
1434 if (S_ISREG(sb.st_mode))
1437 /* \\.\ is the device namespace in Windows, where every file
1438 * is a device node */
1439 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
1446 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1448 int cur_files = td->files_index;
1449 char file_name[PATH_MAX];
1453 dprint(FD_FILE, "add file %s\n", fname);
1455 if (td->o.directory)
1456 len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob,
1457 td->o.unique_filename);
1459 sprintf(file_name + len, "%s", fname);
1461 /* clean cloned siblings using existing files */
1462 if (numjob && is_already_allocated(file_name) &&
1463 !exists_and_not_regfile(fname))
1466 f = alloc_new_file(td);
1468 if (td->files_size <= td->files_index) {
1469 unsigned int new_size = td->o.nr_files + 1;
1471 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1473 td->files = realloc(td->files, new_size * sizeof(f));
1474 if (td->files == NULL) {
1475 log_err("fio: realloc OOM\n");
1478 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1479 td->file_locks = realloc(td->file_locks, new_size);
1480 if (!td->file_locks) {
1481 log_err("fio: realloc OOM\n");
1484 td->file_locks[cur_files] = FILE_LOCK_NONE;
1486 td->files_size = new_size;
1488 td->files[cur_files] = f;
1489 f->fileno = cur_files;
1492 * init function, io engine may not be loaded yet
1494 if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
1495 f->real_file_size = -1ULL;
1497 f->file_name = smalloc_strdup(file_name);
1503 switch (td->o.file_lock_mode) {
1504 case FILE_LOCK_NONE:
1506 case FILE_LOCK_READWRITE:
1507 f->rwlock = fio_rwlock_init();
1509 case FILE_LOCK_EXCLUSIVE:
1510 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1513 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1518 if (f->filetype == FIO_TYPE_FILE)
1519 td->nr_normal_files++;
1521 set_already_allocated(file_name);
1526 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1532 int add_file_exclusive(struct thread_data *td, const char *fname)
1537 for_each_file(td, f, i) {
1538 if (!strcmp(f->file_name, fname))
1542 return add_file(td, fname, 0, 1);
1545 void get_file(struct fio_file *f)
1547 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1548 assert(fio_file_open(f));
1552 int put_file(struct thread_data *td, struct fio_file *f)
1554 int f_ret = 0, ret = 0;
1556 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1558 if (!fio_file_open(f)) {
1559 assert(f->fd == -1);
1563 assert(f->references);
1564 if (--f->references)
1567 if (should_fsync(td) && td->o.fsync_on_close) {
1568 f_ret = fsync(f->fd);
1573 if (td->io_ops->close_file)
1574 ret = td->io_ops->close_file(td, f);
1579 td->nr_open_files--;
1580 fio_file_clear_open(f);
1581 assert(f->fd == -1);
1585 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1587 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1590 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1591 if (ddir == DDIR_READ)
1592 fio_rwlock_read(f->rwlock);
1594 fio_rwlock_write(f->rwlock);
1595 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1596 fio_mutex_down(f->lock);
1598 td->file_locks[f->fileno] = td->o.file_lock_mode;
1601 void unlock_file(struct thread_data *td, struct fio_file *f)
1603 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1606 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1607 fio_rwlock_unlock(f->rwlock);
1608 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1609 fio_mutex_up(f->lock);
1611 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1614 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1616 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1618 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1622 static int recurse_dir(struct thread_data *td, const char *dirname)
1628 D = opendir(dirname);
1630 char buf[FIO_VERROR_SIZE];
1632 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1633 td_verror(td, errno, buf);
1637 while ((dir = readdir(D)) != NULL) {
1638 char full_path[PATH_MAX];
1641 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1644 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1646 if (lstat(full_path, &sb) == -1) {
1647 if (errno != ENOENT) {
1648 td_verror(td, errno, "stat");
1654 if (S_ISREG(sb.st_mode)) {
1655 add_file(td, full_path, 0, 1);
1658 if (!S_ISDIR(sb.st_mode))
1661 ret = recurse_dir(td, full_path);
1670 int add_dir_files(struct thread_data *td, const char *path)
1672 int ret = recurse_dir(td, path);
1675 log_info("fio: opendir added %d files\n", td->o.nr_files);
1680 void dup_files(struct thread_data *td, struct thread_data *org)
1685 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1690 td->files = malloc(org->files_index * sizeof(f));
1692 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1693 td->file_locks = malloc(org->files_index);
1695 for_each_file(org, f, i) {
1696 struct fio_file *__f;
1698 __f = alloc_new_file(td);
1701 __f->file_name = smalloc_strdup(f->file_name);
1702 if (!__f->file_name)
1705 __f->filetype = f->filetype;
1708 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1709 __f->lock = f->lock;
1710 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1711 __f->rwlock = f->rwlock;
1718 * Returns the index that matches the filename, or -1 if not there
1720 int get_fileno(struct thread_data *td, const char *fname)
1725 for_each_file(td, f, i)
1726 if (!strcmp(f->file_name, fname))
1733 * For log usage, where we add/open/close files automatically
1735 void free_release_files(struct thread_data *td)
1739 td->o.open_files = 0;
1740 td->files_index = 0;
1741 td->nr_normal_files = 0;
1744 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1748 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1749 f->last_pos[i] = f->file_offset;
1750 f->last_start[i] = -1ULL;
1753 if (fio_file_axmap(f))
1754 axmap_reset(f->io_axmap);
1755 else if (fio_file_lfsr(f))
1756 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1759 bool fio_files_done(struct thread_data *td)
1764 for_each_file(td, f, i)
1765 if (!fio_file_done(f))
1771 /* free memory used in initialization phase only */
1772 void filesetup_mem_free(void)
1774 free_already_allocated();