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)
41 static void fallocate_file(struct thread_data *td, struct fio_file *f)
45 if (td->o.fill_device)
48 #ifdef CONFIG_POSIX_FALLOCATE
49 switch (td->o.fallocate_mode) {
50 case FIO_FALLOCATE_NONE:
52 case FIO_FALLOCATE_POSIX:
53 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
55 (unsigned long long) f->real_file_size);
57 r = posix_fallocate(f->fd, 0, f->real_file_size);
59 log_err("fio: posix_fallocate fails: %s\n", strerror(r));
61 #ifdef CONFIG_LINUX_FALLOCATE
62 case FIO_FALLOCATE_KEEP_SIZE:
63 dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) "
64 "file %s size %llu\n", f->file_name,
65 (unsigned long long) f->real_file_size);
67 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size);
69 td_verror(td, errno, "fallocate");
72 #endif /* CONFIG_LINUX_FALLOCATE */
74 log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode);
77 #endif /* CONFIG_POSIX_FALLOCATE */
81 * Leaves f->fd open on success, caller must close
83 static int extend_file(struct thread_data *td, struct fio_file *f)
85 int new_layout = 0, unlink_file = 0, flags;
86 unsigned long long left;
91 log_err("fio: refusing extend of file due to read-only\n");
96 * check if we need to lay the file out complete again. fio
97 * does that for operations involving reads, or for writes
98 * where overwrite is set
101 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
102 (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND)))
104 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
107 if (unlink_file || new_layout) {
110 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
112 ret = td_io_unlink_file(td, f);
113 if (ret != 0 && ret != ENOENT) {
114 td_verror(td, errno, "unlink");
120 if (td->o.allow_create)
129 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
130 f->fd = open(f->file_name, flags, 0644);
134 if (err == ENOENT && !td->o.allow_create)
135 log_err("fio: file creation disallowed by "
136 "allow_file_create=0\n");
138 td_verror(td, err, "open");
142 fallocate_file(td, f);
145 * If our jobs don't require regular files initially, we're done.
151 * The size will be -1ULL when fill_device is used, so don't truncate
152 * or fallocate this file, just write it
154 if (!td->o.fill_device) {
155 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
156 (unsigned long long) f->real_file_size);
157 if (ftruncate(f->fd, f->real_file_size) == -1) {
158 if (errno != EFBIG) {
159 td_verror(td, errno, "ftruncate");
165 left = f->real_file_size;
166 bs = td->o.max_bs[DDIR_WRITE];
172 td_verror(td, errno, "malloc");
176 while (left && !td->terminate) {
182 fill_io_buffer(td, b, bs, bs);
184 r = write(f->fd, b, bs);
194 if (td->o.fill_device)
196 log_info("fio: ENOSPC on laying out "
200 td_verror(td, errno, "write");
202 td_verror(td, EIO, "write");
209 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
210 td_io_unlink_file(td, f);
211 } else if (td->o.create_fsync) {
212 if (fsync(f->fd) < 0) {
213 td_verror(td, errno, "fsync");
217 if (td->o.fill_device && !td_write(td)) {
218 fio_file_clear_size_known(f);
219 if (td_io_get_file_size(td, f))
221 if (f->io_size > f->real_file_size)
222 f->io_size = f->real_file_size;
236 static int pre_read_file(struct thread_data *td, struct fio_file *f)
238 int ret = 0, r, did_open = 0, old_runstate;
239 unsigned long long left;
243 if (td_ioengine_flagged(td, FIO_PIPEIO) ||
244 td_ioengine_flagged(td, FIO_NOIO))
247 if (f->filetype == FIO_TYPE_CHAR)
250 if (!fio_file_open(f)) {
251 if (td->io_ops->open_file(td, f)) {
252 log_err("fio: cannot pre-read, failed to open file\n");
258 old_runstate = td_bump_runstate(td, TD_PRE_READING);
261 bs = td->o.max_bs[DDIR_READ];
267 td_verror(td, errno, "malloc");
273 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
274 td_verror(td, errno, "lseek");
275 log_err("fio: failed to lseek pre-read file\n");
280 while (left && !td->terminate) {
284 r = read(f->fd, b, bs);
290 td_verror(td, EIO, "pre_read");
296 td_restore_runstate(td, old_runstate);
299 td->io_ops->close_file(td, f);
305 unsigned long long get_rand_file_size(struct thread_data *td)
307 unsigned long long ret, sized;
311 frand_max = rand_max(&td->file_size_state);
312 r = __rand(&td->file_size_state);
313 sized = td->o.file_size_high - td->o.file_size_low;
314 ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0)));
315 ret += td->o.file_size_low;
316 ret -= (ret % td->o.rw_min_bs);
320 static int file_size(struct thread_data *td, struct fio_file *f)
324 if (stat(f->file_name, &st) == -1) {
325 td_verror(td, errno, "fstat");
329 f->real_file_size = st.st_size;
333 static int bdev_size(struct thread_data *td, struct fio_file *f)
335 unsigned long long bytes = 0;
338 if (td->io_ops->open_file(td, f)) {
339 log_err("fio: failed opening blockdev %s for size check\n",
344 r = blockdev_size(f, &bytes);
346 td_verror(td, r, "blockdev_size");
351 log_err("%s: zero sized block device?\n", f->file_name);
355 f->real_file_size = bytes;
356 td->io_ops->close_file(td, f);
359 td->io_ops->close_file(td, f);
363 static int char_size(struct thread_data *td, struct fio_file *f)
365 #ifdef FIO_HAVE_CHARDEV_SIZE
366 unsigned long long bytes = 0;
369 if (td->io_ops->open_file(td, f)) {
370 log_err("fio: failed opening chardev %s for size check\n",
375 r = chardev_size(f, &bytes);
377 td_verror(td, r, "chardev_size");
382 log_err("%s: zero sized char device?\n", f->file_name);
386 f->real_file_size = bytes;
387 td->io_ops->close_file(td, f);
390 td->io_ops->close_file(td, f);
393 f->real_file_size = -1ULL;
398 static int get_file_size(struct thread_data *td, struct fio_file *f)
402 if (fio_file_size_known(f))
405 if (f->filetype == FIO_TYPE_FILE)
406 ret = file_size(td, f);
407 else if (f->filetype == FIO_TYPE_BLOCK)
408 ret = bdev_size(td, f);
409 else if (f->filetype == FIO_TYPE_CHAR)
410 ret = char_size(td, f);
412 f->real_file_size = -1ULL;
415 * Leave ->real_file_size with 0 since it could be expectation
416 * of initial setup for regular files.
422 * If ->real_file_size is -1, a conditional for the message
423 * "offset extends end" is always true, but it makes no sense,
424 * so just return the same value here.
426 if (f->real_file_size == -1ULL) {
427 log_info("%s: failed to get file size of %s\n", td->o.name,
432 if (td->o.start_offset && f->file_offset == 0)
433 dprint(FD_FILE, "offset of file %s not initialized yet\n",
436 * ->file_offset normally hasn't been initialized yet, so this
437 * is basically always false.
439 if (f->file_offset > f->real_file_size) {
440 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
441 (unsigned long long) f->file_offset,
442 (unsigned long long) f->real_file_size);
446 fio_file_set_size_known(f);
450 static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
451 unsigned long long off,
452 unsigned long long len)
454 int errval = 0, ret = 0;
463 off = f->file_offset;
465 if (len == -1ULL || off == -1ULL)
468 if (td->io_ops->invalidate) {
469 dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name,
471 ret = td->io_ops->invalidate(td, f);
474 } else if (f->filetype == FIO_TYPE_FILE) {
475 dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n",
476 f->file_name, off, len);
477 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
480 } else if (f->filetype == FIO_TYPE_BLOCK) {
483 dprint(FD_IO, "drop page cache %s\n", f->file_name);
484 ret = blockdev_invalidate_cache(f);
485 while (ret < 0 && errno == EAGAIN && retry_count++ < 25) {
487 * Linux multipath devices reject ioctl while
488 * the maps are being updated. That window can
489 * last tens of milliseconds; we'll try up to
490 * a quarter of a second.
493 ret = blockdev_invalidate_cache(f);
495 if (ret < 0 && errno == EACCES && geteuid()) {
497 log_err("fio: only root may flush block "
498 "devices. Cache flush bypassed!\n");
505 else if (ret) /* probably not supported */
507 } else if (f->filetype == FIO_TYPE_CHAR ||
508 f->filetype == FIO_TYPE_PIPE) {
509 dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
514 * Cache flushing isn't a fatal condition, and we know it will
515 * happen on some platforms where we don't have the proper
516 * function to flush eg block device caches. So just warn and
517 * continue on our way.
520 log_info("fio: cache invalidation of %s failed: %s\n",
521 f->file_name, strerror(errval));
527 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
529 if (!fio_file_open(f))
532 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
535 int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
539 dprint(FD_FILE, "fd close %s\n", f->file_name);
543 if (close(f->fd) < 0)
548 if (f->shadow_fd != -1) {
557 int file_lookup_open(struct fio_file *f, int flags)
559 struct fio_file *__f;
562 __f = lookup_file_hash(f->file_name);
564 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
568 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
576 f->fd = open(f->file_name, flags, 0600);
580 static int file_close_shadow_fds(struct thread_data *td)
586 for_each_file(td, f, i) {
587 if (f->shadow_fd == -1)
598 int generic_open_file(struct thread_data *td, struct fio_file *f)
604 dprint(FD_FILE, "fd open %s\n", f->file_name);
606 if (!strcmp(f->file_name, "-")) {
608 log_err("fio: can't read/write to stdin/out\n");
614 * move output logging to stderr, if we are writing to stdout
623 flags |= OS_O_DIRECT;
626 td_verror(td, EINVAL, "OS does not support atomic IO");
629 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
633 if (td->o.create_on_open && td->o.allow_create)
636 if (f->filetype != FIO_TYPE_FILE)
637 flags |= FIO_O_NOATIME;
644 if (f->filetype == FIO_TYPE_FILE && td->o.allow_create)
648 f->fd = dup(STDOUT_FILENO);
650 from_hash = file_lookup_open(f, flags);
651 } else if (td_read(td)) {
652 if (f->filetype == FIO_TYPE_CHAR && !read_only)
658 f->fd = dup(STDIN_FILENO);
660 from_hash = file_lookup_open(f, flags);
661 } else if (td_trim(td)) {
662 assert(!td_rw(td)); /* should have matched above */
664 from_hash = file_lookup_open(f, flags);
668 char buf[FIO_VERROR_SIZE];
671 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
672 flags &= ~FIO_O_NOATIME;
675 if (__e == EMFILE && file_close_shadow_fds(td))
678 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
680 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
681 log_err("fio: looks like your file system does not " \
682 "support direct=1/buffered=0\n");
685 td_verror(td, __e, buf);
689 if (!from_hash && f->fd != -1) {
690 if (add_file_hash(f)) {
694 * Stash away descriptor for later close. This is to
695 * work-around a "feature" on Linux, where a close of
696 * an fd that has been opened for write will trigger
697 * udev to call blkid to check partitions, fs id, etc.
698 * That pollutes the device cache, which can slow down
699 * unbuffered accesses.
701 if (f->shadow_fd == -1)
702 f->shadow_fd = f->fd;
705 * OK to ignore, we haven't done anything
708 ret = generic_close_file(td, f);
718 * This function i.e. get_file_size() is the default .get_file_size
719 * implementation of majority of I/O engines.
721 int generic_get_file_size(struct thread_data *td, struct fio_file *f)
723 return get_file_size(td, f);
727 * open/close all files, so that ->real_file_size gets set
729 static int get_file_sizes(struct thread_data *td)
735 for_each_file(td, f, i) {
736 dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i,
739 if (td_io_get_file_size(td, f)) {
740 if (td->error != ENOENT) {
741 log_err("%s\n", td->verror);
749 * There are corner cases where we end up with -1 for
750 * ->real_file_size due to unsupported file type, etc.
751 * We then just set to size option value divided by number
752 * of files, similar to the way file ->io_size is set.
753 * stat(2) failure doesn't set ->real_file_size to -1.
755 if (f->real_file_size == -1ULL && td->o.size)
756 f->real_file_size = td->o.size / td->o.nr_files;
763 struct flist_head list;
770 * Get free number of bytes for each file on each unique mount.
772 static unsigned long long get_fs_free_counts(struct thread_data *td)
774 struct flist_head *n, *tmp;
775 unsigned long long ret = 0;
776 struct fio_mount *fm;
781 for_each_file(td, f, i) {
785 if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) {
786 if (f->real_file_size != -1ULL)
787 ret += f->real_file_size;
789 } else if (f->filetype != FIO_TYPE_FILE)
793 strncpy(buf, f->file_name, 255);
795 if (stat(buf, &sb) < 0) {
799 if (stat(buf, &sb) < 0)
804 flist_for_each(n, &list) {
805 fm = flist_entry(n, struct fio_mount, list);
806 if (fm->key == sb.st_dev)
815 fm = calloc(1, sizeof(*fm));
816 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
817 fm->base = basename(fm->__base);
819 flist_add(&fm->list, &list);
822 flist_for_each_safe(n, tmp, &list) {
823 unsigned long long sz;
825 fm = flist_entry(n, struct fio_mount, list);
826 flist_del(&fm->list);
828 sz = get_fs_free_size(fm->base);
829 if (sz && sz != -1ULL)
838 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
840 struct thread_options *o = &td->o;
841 unsigned long long align_bs;
842 unsigned long long offset;
844 if (o->file_append && f->filetype == FIO_TYPE_FILE)
845 return f->real_file_size;
847 if (o->start_offset_percent > 0) {
849 * if blockalign is provided, find the min across read, write,
852 if (fio_option_is_set(o, ba)) {
853 align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]);
854 align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs);
856 /* else take the minimum block size */
857 align_bs = td_min_bs(td);
860 /* calculate the raw offset */
861 offset = (f->real_file_size * o->start_offset_percent / 100) +
862 (td->subjob_number * o->offset_increment);
865 * block align the offset at the next available boundary at
866 * ceiling(offset / align_bs) * align_bs
868 offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs;
871 /* start_offset_percent not set */
872 offset = o->start_offset +
873 td->subjob_number * o->offset_increment;
880 * Open the files and setup files sizes, creating files if necessary.
882 int setup_files(struct thread_data *td)
884 unsigned long long total_size, extend_size;
885 struct thread_options *o = &td->o;
887 unsigned int i, nr_fs_extra = 0;
888 int err = 0, need_extend;
890 const unsigned int bs = td_min_bs(td);
893 dprint(FD_FILE, "setup files\n");
895 old_state = td_bump_runstate(td, TD_SETTING_UP);
897 if (o->read_iolog_file)
901 * Find out physical size of files or devices for this thread,
902 * before we determine I/O size and range of our targets.
903 * If ioengine defines a setup() method, it's responsible for
904 * opening the files and setting f->real_file_size to indicate
905 * the valid range for that file.
907 if (td->io_ops->setup)
908 err = td->io_ops->setup(td);
910 err = get_file_sizes(td);
916 * check sizes. if the files/devices do not exist and the size
917 * isn't passed to fio, abort.
920 for_each_file(td, f, i) {
922 if (f->real_file_size == -1ULL)
925 total_size += f->real_file_size;
929 td->fill_device_size = get_fs_free_counts(td);
932 * device/file sizes are zero and no size given, punt
934 if ((!total_size || total_size == -1ULL) && !o->size &&
935 !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device &&
936 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
937 log_err("%s: you need to specify size=\n", o->name);
938 td_verror(td, EINVAL, "total_file_size");
943 * Calculate per-file size and potential extra size for the
944 * first files, if needed (i.e. if we don't have a fixed size).
946 if (!o->file_size_low && o->nr_files) {
949 fs = o->size / o->nr_files;
950 all_fs = fs * o->nr_files;
952 if (all_fs < o->size)
953 nr_fs_extra = (o->size - all_fs) / bs;
957 * now file sizes are known, so we can set ->io_size. if size= is
958 * not given, ->io_size is just equal to ->real_file_size. if size
959 * is given, ->io_size is size / nr_files.
961 extend_size = total_size = 0;
963 for_each_file(td, f, i) {
964 f->file_offset = get_start_offset(td, f);
967 * Update ->io_size depending on options specified.
968 * ->file_size_low being 0 means filesize option isn't set.
969 * Non zero ->file_size_low equals ->file_size_high means
970 * filesize option is set in a fixed size format.
971 * Non zero ->file_size_low not equals ->file_size_high means
972 * filesize option is set in a range format.
974 if (!o->file_size_low) {
976 * no file size or range given, file size is equal to
977 * total size divided by number of files. If the size
978 * doesn't divide nicely with the min blocksize,
979 * make the first files bigger.
988 * We normally don't come here for regular files, but
989 * if the result is 0 for a regular file, set it to the
990 * real file size. This could be size of the existing
991 * one if it already exists, but otherwise will be set
992 * to 0. A new file won't be created because
993 * ->io_size + ->file_offset equals ->real_file_size.
996 if (f->file_offset > f->real_file_size)
998 f->io_size = f->real_file_size - f->file_offset;
1000 log_info("fio: file %s may be ignored\n",
1003 } else if (f->real_file_size < o->file_size_low ||
1004 f->real_file_size > o->file_size_high) {
1005 if (f->file_offset > o->file_size_low)
1008 * file size given. if it's fixed, use that. if it's a
1009 * range, generate a random size in-between.
1011 if (o->file_size_low == o->file_size_high)
1012 f->io_size = o->file_size_low - f->file_offset;
1014 f->io_size = get_rand_file_size(td)
1018 f->io_size = f->real_file_size - f->file_offset;
1020 if (f->io_size == -1ULL)
1023 if (o->size_percent) {
1026 file_size = f->io_size + f->file_offset;
1027 f->io_size = (file_size *
1028 o->size_percent) / 100;
1029 if (f->io_size > (file_size - f->file_offset))
1030 f->io_size = file_size - f->file_offset;
1032 f->io_size -= (f->io_size % td_min_bs(td));
1034 total_size += f->io_size;
1037 if (f->filetype == FIO_TYPE_FILE &&
1038 (f->io_size + f->file_offset) > f->real_file_size &&
1039 !td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1040 if (!o->create_on_open) {
1042 extend_size += (f->io_size + f->file_offset);
1043 fio_file_set_extend(f);
1045 f->real_file_size = f->io_size + f->file_offset;
1049 if (td->o.block_error_hist) {
1052 assert(td->o.nr_files == 1); /* checked in fixup_options */
1054 len = f->io_size / td->o.bs[DDIR_TRIM];
1055 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
1056 log_err("fio: cannot calculate block histogram with "
1057 "%d trim blocks, maximum %d\n",
1058 len, MAX_NR_BLOCK_INFOS);
1059 td_verror(td, EINVAL, "block_error_hist");
1063 td->ts.nr_block_infos = len;
1064 for (i = 0; i < len; i++)
1065 td->ts.block_infos[i] =
1066 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
1068 td->ts.nr_block_infos = 0;
1070 if (!o->size || (total_size && o->size > total_size))
1071 o->size = total_size;
1073 if (o->size < td_min_bs(td)) {
1074 log_err("fio: blocksize too large for data set\n");
1079 * See if we need to extend some files, typically needed when our
1080 * target regular files don't exist yet, but our jobs require them
1081 * initially due to read I/Os.
1085 if (output_format & FIO_OUTPUT_NORMAL) {
1086 log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n",
1088 need_extend > 1 ? "s" : "",
1090 need_extend > 1 ? "s" : "",
1091 need_extend > 1 ? "total " : "",
1095 for_each_file(td, f, i) {
1096 unsigned long long old_len = -1ULL, extend_len = -1ULL;
1098 if (!fio_file_extend(f))
1101 assert(f->filetype == FIO_TYPE_FILE);
1102 fio_file_clear_extend(f);
1103 if (!o->fill_device) {
1104 old_len = f->real_file_size;
1105 extend_len = f->io_size + f->file_offset -
1108 f->real_file_size = (f->io_size + f->file_offset);
1109 err = extend_file(td, f);
1113 err = __file_invalidate_cache(td, f, old_len,
1117 * Shut up static checker
1133 o->zone_size = o->size;
1136 * iolog already set the total io size, if we read back
1139 if (!o->read_iolog_file) {
1141 td->total_io_size = o->io_size * o->loops;
1143 td->total_io_size = o->size * o->loops;
1150 td_restore_runstate(td, old_state);
1153 log_err("%s: you need to specify valid offset=\n", o->name);
1155 td_restore_runstate(td, old_state);
1159 int pre_read_files(struct thread_data *td)
1164 dprint(FD_FILE, "pre_read files\n");
1166 for_each_file(td, f, i) {
1167 if (pre_read_file(td, f))
1174 static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1176 unsigned int range_size, seed;
1177 unsigned long nranges;
1180 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
1181 fsize = min(f->real_file_size, f->io_size);
1183 nranges = (fsize + range_size - 1) / range_size;
1185 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
1186 if (!td->o.rand_repeatable)
1187 seed = td->rand_seeds[4];
1189 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
1190 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
1191 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
1192 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
1193 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
1194 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
1199 static int init_rand_distribution(struct thread_data *td)
1205 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1208 state = td_bump_runstate(td, TD_SETTING_UP);
1210 for_each_file(td, f, i)
1211 __init_rand_distribution(td, f);
1213 td_restore_runstate(td, state);
1219 * Check if the number of blocks exceeds the randomness capability of
1220 * the selected generator. Tausworthe is 32-bit, the others are fullly
1223 static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f,
1226 if (blocks <= FRAND32_MAX)
1228 if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE)
1232 * If the user hasn't specified a random generator, switch
1233 * to tausworthe64 with informational warning. If the user did
1234 * specify one, just warn.
1236 log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n",
1239 if (!fio_option_is_set(&td->o, random_generator)) {
1240 log_info("fio: Switching to tausworthe64. Use the "
1241 "random_generator= option to get rid of this "
1243 td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64;
1248 * Just make this information to avoid breaking scripts.
1250 log_info("fio: Use the random_generator= option to switch to lfsr or "
1255 int init_random_map(struct thread_data *td)
1257 unsigned long long blocks;
1261 if (init_rand_distribution(td))
1266 for_each_file(td, f, i) {
1267 uint64_t fsize = min(f->real_file_size, f->io_size);
1269 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
1271 if (check_rand_gen_limits(td, f, blocks))
1274 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
1277 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1279 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1280 fio_file_set_lfsr(f);
1283 } else if (!td->o.norandommap) {
1284 f->io_axmap = axmap_new(blocks);
1286 fio_file_set_axmap(f);
1289 } else if (td->o.norandommap)
1292 if (!td->o.softrandommap) {
1293 log_err("fio: failed allocating random map. If running"
1294 " a large number of jobs, try the 'norandommap'"
1295 " option or set 'softrandommap'. Or give"
1296 " a larger --alloc-size to fio.\n");
1300 log_info("fio: file %s failed allocating random map. Running "
1301 "job without.\n", f->file_name);
1307 void close_files(struct thread_data *td)
1312 for_each_file(td, f, i) {
1313 if (fio_file_open(f))
1314 td_io_close_file(td, f);
1318 void close_and_free_files(struct thread_data *td)
1323 dprint(FD_FILE, "close files\n");
1325 for_each_file(td, f, i) {
1326 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1327 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1328 td_io_unlink_file(td, f);
1331 if (fio_file_open(f))
1332 td_io_close_file(td, f);
1334 remove_file_hash(f);
1336 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1337 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1338 td_io_unlink_file(td, f);
1341 sfree(f->file_name);
1342 f->file_name = NULL;
1343 if (fio_file_axmap(f)) {
1344 axmap_free(f->io_axmap);
1350 td->o.filename = NULL;
1352 free(td->file_locks);
1353 td->files_index = 0;
1355 td->file_locks = NULL;
1356 td->o.file_lock_mode = FILE_LOCK_NONE;
1360 static void get_file_type(struct fio_file *f)
1364 if (!strcmp(f->file_name, "-"))
1365 f->filetype = FIO_TYPE_PIPE;
1367 f->filetype = FIO_TYPE_FILE;
1370 /* \\.\ is the device namespace in Windows, where every file is
1372 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1373 f->filetype = FIO_TYPE_BLOCK;
1376 if (!stat(f->file_name, &sb)) {
1377 if (S_ISBLK(sb.st_mode))
1378 f->filetype = FIO_TYPE_BLOCK;
1379 else if (S_ISCHR(sb.st_mode))
1380 f->filetype = FIO_TYPE_CHAR;
1381 else if (S_ISFIFO(sb.st_mode))
1382 f->filetype = FIO_TYPE_PIPE;
1386 static bool __is_already_allocated(const char *fname, bool set)
1388 struct flist_head *entry;
1391 ret = file_bloom_exists(fname, set);
1395 flist_for_each(entry, &filename_list) {
1396 struct file_name *fn;
1398 fn = flist_entry(entry, struct file_name, list);
1400 if (!strcmp(fn->filename, fname))
1407 static bool is_already_allocated(const char *fname)
1411 fio_file_hash_lock();
1412 ret = __is_already_allocated(fname, false);
1413 fio_file_hash_unlock();
1418 static void set_already_allocated(const char *fname)
1420 struct file_name *fn;
1422 fn = malloc(sizeof(struct file_name));
1423 fn->filename = strdup(fname);
1425 fio_file_hash_lock();
1426 if (!__is_already_allocated(fname, true)) {
1427 flist_add_tail(&fn->list, &filename_list);
1430 fio_file_hash_unlock();
1438 static void free_already_allocated(void)
1440 struct flist_head *entry, *tmp;
1441 struct file_name *fn;
1443 if (flist_empty(&filename_list))
1446 fio_file_hash_lock();
1447 flist_for_each_safe(entry, tmp, &filename_list) {
1448 fn = flist_entry(entry, struct file_name, list);
1450 flist_del(&fn->list);
1454 fio_file_hash_unlock();
1457 static struct fio_file *alloc_new_file(struct thread_data *td)
1461 f = smalloc(sizeof(*f));
1469 fio_file_reset(td, f);
1473 bool exists_and_not_regfile(const char *filename)
1477 if (lstat(filename, &sb) == -1)
1480 #ifndef WIN32 /* NOT Windows */
1481 if (S_ISREG(sb.st_mode))
1484 /* \\.\ is the device namespace in Windows, where every file
1485 * is a device node */
1486 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
1493 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
1495 int cur_files = td->files_index;
1496 char file_name[PATH_MAX];
1500 dprint(FD_FILE, "add file %s\n", fname);
1502 if (td->o.directory)
1503 len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob,
1504 td->o.unique_filename);
1506 sprintf(file_name + len, "%s", fname);
1508 /* clean cloned siblings using existing files */
1509 if (numjob && is_already_allocated(file_name) &&
1510 !exists_and_not_regfile(fname))
1513 f = alloc_new_file(td);
1515 if (td->files_size <= td->files_index) {
1516 unsigned int new_size = td->o.nr_files + 1;
1518 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1520 td->files = realloc(td->files, new_size * sizeof(f));
1521 if (td->files == NULL) {
1522 log_err("fio: realloc OOM\n");
1525 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1526 td->file_locks = realloc(td->file_locks, new_size);
1527 if (!td->file_locks) {
1528 log_err("fio: realloc OOM\n");
1531 td->file_locks[cur_files] = FILE_LOCK_NONE;
1533 td->files_size = new_size;
1535 td->files[cur_files] = f;
1536 f->fileno = cur_files;
1539 * init function, io engine may not be loaded yet
1541 if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
1542 f->real_file_size = -1ULL;
1544 f->file_name = smalloc_strdup(file_name);
1550 switch (td->o.file_lock_mode) {
1551 case FILE_LOCK_NONE:
1553 case FILE_LOCK_READWRITE:
1554 f->rwlock = fio_rwlock_init();
1556 case FILE_LOCK_EXCLUSIVE:
1557 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1560 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1565 if (f->filetype == FIO_TYPE_FILE)
1566 td->nr_normal_files++;
1568 set_already_allocated(file_name);
1573 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1579 int add_file_exclusive(struct thread_data *td, const char *fname)
1584 for_each_file(td, f, i) {
1585 if (!strcmp(f->file_name, fname))
1589 return add_file(td, fname, 0, 1);
1592 void get_file(struct fio_file *f)
1594 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
1595 assert(fio_file_open(f));
1599 int put_file(struct thread_data *td, struct fio_file *f)
1601 int f_ret = 0, ret = 0;
1603 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
1605 if (!fio_file_open(f)) {
1606 assert(f->fd == -1);
1610 assert(f->references);
1611 if (--f->references)
1614 if (should_fsync(td) && td->o.fsync_on_close) {
1615 f_ret = fsync(f->fd);
1620 if (td->io_ops->close_file)
1621 ret = td->io_ops->close_file(td, f);
1626 td->nr_open_files--;
1627 fio_file_clear_open(f);
1628 assert(f->fd == -1);
1632 void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
1634 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1637 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1638 if (ddir == DDIR_READ)
1639 fio_rwlock_read(f->rwlock);
1641 fio_rwlock_write(f->rwlock);
1642 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1643 fio_mutex_down(f->lock);
1645 td->file_locks[f->fileno] = td->o.file_lock_mode;
1648 void unlock_file(struct thread_data *td, struct fio_file *f)
1650 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1653 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1654 fio_rwlock_unlock(f->rwlock);
1655 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1656 fio_mutex_up(f->lock);
1658 td->file_locks[f->fileno] = FILE_LOCK_NONE;
1661 void unlock_file_all(struct thread_data *td, struct fio_file *f)
1663 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
1665 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1669 static int recurse_dir(struct thread_data *td, const char *dirname)
1675 D = opendir(dirname);
1677 char buf[FIO_VERROR_SIZE];
1679 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
1680 td_verror(td, errno, buf);
1684 while ((dir = readdir(D)) != NULL) {
1685 char full_path[PATH_MAX];
1688 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1691 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
1693 if (lstat(full_path, &sb) == -1) {
1694 if (errno != ENOENT) {
1695 td_verror(td, errno, "stat");
1701 if (S_ISREG(sb.st_mode)) {
1702 add_file(td, full_path, 0, 1);
1705 if (!S_ISDIR(sb.st_mode))
1708 ret = recurse_dir(td, full_path);
1717 int add_dir_files(struct thread_data *td, const char *path)
1719 int ret = recurse_dir(td, path);
1722 log_info("fio: opendir added %d files\n", td->o.nr_files);
1727 void dup_files(struct thread_data *td, struct thread_data *org)
1732 dprint(FD_FILE, "dup files: %d\n", org->files_index);
1737 td->files = malloc(org->files_index * sizeof(f));
1739 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1740 td->file_locks = malloc(org->files_index);
1742 for_each_file(org, f, i) {
1743 struct fio_file *__f;
1745 __f = alloc_new_file(td);
1748 __f->file_name = smalloc_strdup(f->file_name);
1749 if (!__f->file_name)
1752 __f->filetype = f->filetype;
1755 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1756 __f->lock = f->lock;
1757 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1758 __f->rwlock = f->rwlock;
1765 * Returns the index that matches the filename, or -1 if not there
1767 int get_fileno(struct thread_data *td, const char *fname)
1772 for_each_file(td, f, i)
1773 if (!strcmp(f->file_name, fname))
1780 * For log usage, where we add/open/close files automatically
1782 void free_release_files(struct thread_data *td)
1786 td->o.open_files = 0;
1787 td->files_index = 0;
1788 td->nr_normal_files = 0;
1791 void fio_file_reset(struct thread_data *td, struct fio_file *f)
1795 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1796 f->last_pos[i] = f->file_offset;
1797 f->last_start[i] = -1ULL;
1800 if (fio_file_axmap(f))
1801 axmap_reset(f->io_axmap);
1802 else if (fio_file_lfsr(f))
1803 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1806 bool fio_files_done(struct thread_data *td)
1811 for_each_file(td, f, i)
1812 if (!fio_file_done(f))
1818 /* free memory used in initialization phase only */
1819 void filesetup_mem_free(void)
1821 free_already_allocated();