filesetup: __init_rand_distribution() can be void
[fio.git] / filesetup.c
index 9335dcd3f8dd4b2c38548ee23dbc7e56282caeb0..2cc7158ead7eb1a8cf16020106c5002aaee9b441 100644 (file)
@@ -38,12 +38,73 @@ static inline void clear_error(struct thread_data *td)
        td->verror[0] = '\0';
 }
 
+static int native_fallocate(struct thread_data *td, struct fio_file *f)
+{
+       bool success;
+
+       success = fio_fallocate(f, 0, f->real_file_size);
+       dprint(FD_FILE, "native fallocate of file %s size %llu was "
+                       "%ssuccessful\n", f->file_name,
+                       (unsigned long long) f->real_file_size,
+                       !success ? "un": "");
+
+       if (success)
+               return false;
+
+       if (errno == ENOSYS)
+               dprint(FD_FILE, "native fallocate is not implemented\n");
+
+       return true;
+}
+
+static void fallocate_file(struct thread_data *td, struct fio_file *f)
+{
+       int r;
+
+       if (td->o.fill_device)
+               return;
+
+       switch (td->o.fallocate_mode) {
+       case FIO_FALLOCATE_NATIVE:
+               native_fallocate(td, f);
+               break;
+       case FIO_FALLOCATE_NONE:
+               break;
+#ifdef CONFIG_POSIX_FALLOCATE
+       case FIO_FALLOCATE_POSIX:
+               dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
+                                f->file_name,
+                                (unsigned long long) f->real_file_size);
+
+               r = posix_fallocate(f->fd, 0, f->real_file_size);
+               if (r > 0)
+                       log_err("fio: posix_fallocate fails: %s\n", strerror(r));
+               break;
+#endif /* CONFIG_POSIX_FALLOCATE */
+#ifdef CONFIG_LINUX_FALLOCATE
+       case FIO_FALLOCATE_KEEP_SIZE:
+               dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) "
+                               "file %s size %llu\n", f->file_name,
+                               (unsigned long long) f->real_file_size);
+
+               r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size);
+               if (r != 0)
+                       td_verror(td, errno, "fallocate");
+
+               break;
+#endif /* CONFIG_LINUX_FALLOCATE */
+       default:
+               log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode);
+               assert(0);
+       }
+}
+
 /*
  * Leaves f->fd open on success, caller must close
  */
 static int extend_file(struct thread_data *td, struct fio_file *f)
 {
-       int r, new_layout = 0, unlink_file = 0, flags;
+       int new_layout = 0, unlink_file = 0, flags;
        unsigned long long left;
        unsigned int bs;
        char *b = NULL;
@@ -100,43 +161,7 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                return 1;
        }
 
-#ifdef CONFIG_POSIX_FALLOCATE
-       if (!td->o.fill_device) {
-               switch (td->o.fallocate_mode) {
-               case FIO_FALLOCATE_NONE:
-                       break;
-               case FIO_FALLOCATE_POSIX:
-                       dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
-                                f->file_name,
-                                (unsigned long long) f->real_file_size);
-
-                       r = posix_fallocate(f->fd, 0, f->real_file_size);
-                       if (r > 0) {
-                               log_err("fio: posix_fallocate fails: %s\n",
-                                               strerror(r));
-                       }
-                       break;
-#ifdef CONFIG_LINUX_FALLOCATE
-               case FIO_FALLOCATE_KEEP_SIZE:
-                       dprint(FD_FILE,
-                               "fallocate(FALLOC_FL_KEEP_SIZE) "
-                               "file %s size %llu\n", f->file_name,
-                               (unsigned long long) f->real_file_size);
-
-                       r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
-                                       f->real_file_size);
-                       if (r != 0)
-                               td_verror(td, errno, "fallocate");
-
-                       break;
-#endif /* CONFIG_LINUX_FALLOCATE */
-               default:
-                       log_err("fio: unknown fallocate mode: %d\n",
-                               td->o.fallocate_mode);
-                       assert(0);
-               }
-       }
-#endif /* CONFIG_POSIX_FALLOCATE */
+       fallocate_file(td, f);
 
        /*
         * If our jobs don't require regular files initially, we're done.
@@ -171,6 +196,8 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
        }
 
        while (left && !td->terminate) {
+               ssize_t r;
+
                if (bs > left)
                        bs = left;
 
@@ -228,20 +255,25 @@ err:
        return 1;
 }
 
-static int pre_read_file(struct thread_data *td, struct fio_file *f)
+static bool pre_read_file(struct thread_data *td, struct fio_file *f)
 {
-       int ret = 0, r, did_open = 0, old_runstate;
+       int r, did_open = 0, old_runstate;
        unsigned long long left;
        unsigned int bs;
+       bool ret = true;
        char *b;
 
-       if (td_ioengine_flagged(td, FIO_PIPEIO))
-               return 0;
+       if (td_ioengine_flagged(td, FIO_PIPEIO) ||
+           td_ioengine_flagged(td, FIO_NOIO))
+               return true;
+
+       if (f->filetype == FIO_TYPE_CHAR)
+               return true;
 
        if (!fio_file_open(f)) {
                if (td->io_ops->open_file(td, f)) {
                        log_err("fio: cannot pre-read, failed to open file\n");
-                       return 1;
+                       return false;
                }
                did_open = 1;
        }
@@ -256,7 +288,7 @@ static int pre_read_file(struct thread_data *td, struct fio_file *f)
        b = malloc(bs);
        if (!b) {
                td_verror(td, errno, "malloc");
-               ret = 1;
+               ret = false;
                goto error;
        }
        memset(b, 0, bs);
@@ -264,7 +296,7 @@ static int pre_read_file(struct thread_data *td, struct fio_file *f)
        if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
                td_verror(td, errno, "lseek");
                log_err("fio: failed to lseek pre-read file\n");
-               ret = 1;
+               ret = false;
                goto error;
        }
 
@@ -493,8 +525,6 @@ static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
                }
                if (ret < 0)
                        errval = errno;
-               else if (ret) /* probably not supported */
-                       errval = ret;
        } else if (f->filetype == FIO_TYPE_CHAR ||
                   f->filetype == FIO_TYPE_PIPE) {
                dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
@@ -829,12 +859,40 @@ static unsigned long long get_fs_free_counts(struct thread_data *td)
 uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
 {
        struct thread_options *o = &td->o;
+       unsigned long long align_bs;
+       unsigned long long offset;
 
        if (o->file_append && f->filetype == FIO_TYPE_FILE)
                return f->real_file_size;
 
-       return td->o.start_offset +
-               td->subjob_number * td->o.offset_increment;
+       if (o->start_offset_percent > 0) {
+               /*
+                * if offset_align is provided, set initial offset
+                */
+               if (fio_option_is_set(o, start_offset_align)) {
+                       align_bs = o->start_offset_align;
+               } else {
+                       /* else take the minimum block size */
+                       align_bs = td_min_bs(td);
+               }
+
+               /* calculate the raw offset */
+               offset = (f->real_file_size * o->start_offset_percent / 100) +
+                       (td->subjob_number * o->offset_increment);
+
+               /*
+                * block align the offset at the next available boundary at
+                * ceiling(offset / align_bs) * align_bs
+                */
+               offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs;
+
+       } else {
+               /* start_offset_percent not set */
+               offset = o->start_offset +
+                               td->subjob_number * o->offset_increment;
+       }
+
+       return offset;
 }
 
 /*
@@ -982,7 +1040,14 @@ int setup_files(struct thread_data *td)
                        total_size = -1ULL;
                else {
                         if (o->size_percent) {
-                               f->io_size = (f->io_size * o->size_percent) / 100;
+                               uint64_t file_size;
+
+                               file_size = f->io_size + f->file_offset;
+                               f->io_size = (file_size *
+                                             o->size_percent) / 100;
+                               if (f->io_size > (file_size - f->file_offset))
+                                       f->io_size = file_size - f->file_offset;
+
                                f->io_size -= (f->io_size % td_min_bs(td));
                        }
                        total_size += f->io_size;
@@ -1110,7 +1175,7 @@ err_out:
        return 1;
 }
 
-int pre_read_files(struct thread_data *td)
+bool pre_read_files(struct thread_data *td)
 {
        struct fio_file *f;
        unsigned int i;
@@ -1118,13 +1183,14 @@ int pre_read_files(struct thread_data *td)
        dprint(FD_FILE, "pre_read files\n");
 
        for_each_file(td, f, i) {
-               pre_read_file(td, f);
+               if (!pre_read_file(td, f))
+                       return false;
        }
 
-       return 1;
+       return true;
 }
 
-static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
+static void __init_rand_distribution(struct thread_data *td, struct fio_file *f)
 {
        unsigned int range_size, seed;
        unsigned long nranges;
@@ -1145,8 +1211,6 @@ static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
                pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
        else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
                gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
-
-       return 1;
 }
 
 static int init_rand_distribution(struct thread_data *td)
@@ -1272,6 +1336,7 @@ void close_and_free_files(struct thread_data *td)
 {
        struct fio_file *f;
        unsigned int i;
+       bool use_free = td_ioengine_flagged(td, FIO_NOFILEHASH);
 
        dprint(FD_FILE, "close files\n");
 
@@ -1291,13 +1356,19 @@ void close_and_free_files(struct thread_data *td)
                        td_io_unlink_file(td, f);
                }
 
-               sfree(f->file_name);
+               if (use_free)
+                       free(f->file_name);
+               else
+                       sfree(f->file_name);
                f->file_name = NULL;
                if (fio_file_axmap(f)) {
                        axmap_free(f->io_axmap);
                        f->io_axmap = NULL;
                }
-               sfree(f);
+               if (use_free)
+                       free(f);
+               else
+                       sfree(f);
        }
 
        td->o.filename = NULL;
@@ -1411,7 +1482,10 @@ static struct fio_file *alloc_new_file(struct thread_data *td)
 {
        struct fio_file *f;
 
-       f = smalloc(sizeof(*f));
+       if (td_ioengine_flagged(td, FIO_NOFILEHASH))
+               f = calloc(1, sizeof(*f));
+       else
+               f = smalloc(sizeof(*f));
        if (!f) {
                assert(0);
                return NULL;
@@ -1443,6 +1517,42 @@ bool exists_and_not_regfile(const char *filename)
        return true;
 }
 
+static int create_work_dirs(struct thread_data *td, const char *fname)
+{
+       char path[PATH_MAX];
+       char *start, *end;
+
+       if (td->o.directory) {
+               snprintf(path, PATH_MAX, "%s%c%s", td->o.directory,
+                        FIO_OS_PATH_SEPARATOR, fname);
+               start = strstr(path, fname);
+       } else {
+               snprintf(path, PATH_MAX, "%s", fname);
+               start = path;
+       }
+
+       end = start;
+       while ((end = strchr(end, FIO_OS_PATH_SEPARATOR)) != NULL) {
+               if (end == start)
+                       break;
+               *end = '\0';
+               errno = 0;
+#ifdef CONFIG_HAVE_MKDIR_TWO
+               if (mkdir(path, 0600) && errno != EEXIST) {
+#else
+               if (mkdir(path) && errno != EEXIST) {
+#endif
+                       log_err("fio: failed to create dir (%s): %d\n",
+                               start, errno);
+                       return 1;
+               }
+               *end = FIO_OS_PATH_SEPARATOR;
+               end++;
+       }
+       td->flags |= TD_F_DIRS_CREATED;
+       return 0;
+}
+
 int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
 {
        int cur_files = td->files_index;
@@ -1458,6 +1568,11 @@ int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
 
        sprintf(file_name + len, "%s", fname);
 
+       if (strchr(fname, FIO_OS_PATH_SEPARATOR) &&
+           !(td->flags & TD_F_DIRS_CREATED) &&
+           create_work_dirs(td, fname))
+               return 1;
+
        /* clean cloned siblings using existing files */
        if (numjob && is_already_allocated(file_name) &&
            !exists_and_not_regfile(fname))
@@ -1494,7 +1609,10 @@ int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
        if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
                f->real_file_size = -1ULL;
 
-       f->file_name = smalloc_strdup(file_name);
+       if (td_ioengine_flagged(td, FIO_NOFILEHASH))
+               f->file_name = strdup(file_name);
+       else
+               f->file_name = smalloc_strdup(file_name);
        if (!f->file_name)
                assert(0);
 
@@ -1518,7 +1636,8 @@ int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
        if (f->filetype == FIO_TYPE_FILE)
                td->nr_normal_files++;
 
-       set_already_allocated(file_name);
+       if (td->o.numjobs > 1)
+               set_already_allocated(file_name);
 
        if (inc)
                td->o.nr_files++;
@@ -1641,7 +1760,7 @@ static int recurse_dir(struct thread_data *td, const char *dirname)
                if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
                        continue;
 
-               sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
+               sprintf(full_path, "%s%c%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
 
                if (lstat(full_path, &sb) == -1) {
                        if (errno != ENOENT) {
@@ -1698,7 +1817,10 @@ void dup_files(struct thread_data *td, struct thread_data *org)
                __f = alloc_new_file(td);
 
                if (f->file_name) {
-                       __f->file_name = smalloc_strdup(f->file_name);
+                       if (td_ioengine_flagged(td, FIO_NOFILEHASH))
+                               __f->file_name = strdup(f->file_name);
+                       else
+                               __f->file_name = smalloc_strdup(f->file_name);
                        if (!__f->file_name)
                                assert(0);
 
@@ -1773,3 +1895,32 @@ void filesetup_mem_free(void)
 {
        free_already_allocated();
 }
+
+/*
+ * This function is for platforms which support direct I/O but not O_DIRECT.
+ */
+int fio_set_directio(struct thread_data *td, struct fio_file *f)
+{
+#ifdef FIO_OS_DIRECTIO
+       int ret = fio_set_odirect(f);
+
+       if (ret) {
+               td_verror(td, ret, "fio_set_directio");
+#if defined(__sun__)
+               if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */
+                       log_err("fio: doing directIO to RAW devices or ZFS not supported\n");
+               } else {
+                       log_err("fio: the file system does not seem to support direct IO\n");
+               }
+#else
+               log_err("fio: the file system does not seem to support direct IO\n");
+#endif
+               return -1;
+       }
+
+       return 0;
+#else
+       log_err("fio: direct IO is not supported on this host operating system\n");
+       return -1;
+#endif
+}