filesetup: __init_rand_distribution() can be void
[fio.git] / filesetup.c
index 839aefc33752e28c7a8e7a628593f9f8f758066f..2cc7158ead7eb1a8cf16020106c5002aaee9b441 100644 (file)
@@ -38,7 +38,7 @@ static inline void clear_error(struct thread_data *td)
        td->verror[0] = '\0';
 }
 
-static inline int native_fallocate(struct thread_data *td, struct fio_file *f)
+static int native_fallocate(struct thread_data *td, struct fio_file *f)
 {
        bool success;
 
@@ -49,12 +49,12 @@ static inline int native_fallocate(struct thread_data *td, struct fio_file *f)
                        !success ? "un": "");
 
        if (success)
-               return 0;
+               return false;
 
        if (errno == ENOSYS)
                dprint(FD_FILE, "native fallocate is not implemented\n");
 
-       return -1;
+       return true;
 }
 
 static void fallocate_file(struct thread_data *td, struct fio_file *f)
@@ -66,10 +66,7 @@ static void fallocate_file(struct thread_data *td, struct fio_file *f)
 
        switch (td->o.fallocate_mode) {
        case FIO_FALLOCATE_NATIVE:
-               r = native_fallocate(td, f);
-               if (r != 0 && errno != ENOSYS)
-                       log_err("fio: native_fallocate call failed: %s\n",
-                                       strerror(errno));
+               native_fallocate(td, f);
                break;
        case FIO_FALLOCATE_NONE:
                break;
@@ -258,24 +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) ||
            td_ioengine_flagged(td, FIO_NOIO))
-               return 0;
+               return true;
 
        if (f->filetype == FIO_TYPE_CHAR)
-               return 0;
+               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;
        }
@@ -290,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);
@@ -298,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;
        }
 
@@ -869,12 +867,10 @@ uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
 
        if (o->start_offset_percent > 0) {
                /*
-                * if blockalign is provided, find the min across read, write,
-                * and trim
+                * if offset_align is provided, set initial offset
                 */
-               if (fio_option_is_set(o, ba)) {
-                       align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]);
-                       align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs);
+               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);
@@ -1179,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;
@@ -1187,14 +1183,14 @@ int pre_read_files(struct thread_data *td)
        dprint(FD_FILE, "pre_read files\n");
 
        for_each_file(td, f, i) {
-               if (pre_read_file(td, f))
-                       return -1;
+               if (!pre_read_file(td, f))
+                       return false;
        }
 
-       return 0;
+       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;
@@ -1215,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)
@@ -1342,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");
 
@@ -1361,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;
@@ -1481,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;
@@ -1513,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;
@@ -1528,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))
@@ -1564,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);
 
@@ -1588,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++;
@@ -1711,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) {
@@ -1768,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);
 
@@ -1843,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
+}