Improve LFSR implementation
[fio.git] / filesetup.c
index 9d3e06268fffc35786749ea0eb482d0f9bb7ce11..57553c962ce9fdc65429207231c3dcd786bb9a13 100644 (file)
@@ -15,7 +15,7 @@
 #include "hash.h"
 #include "lib/axmap.h"
 
-#ifdef FIO_HAVE_LINUX_FALLOCATE
+#ifdef CONFIG_LINUX_FALLOCATE
 #include <linux/falloc.h>
 #endif
 
@@ -72,14 +72,15 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                return 1;
        }
 
-#ifdef FIO_HAVE_FALLOCATE
+#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, f->real_file_size);
+                                f->file_name,
+                                (unsigned long long) f->real_file_size);
 
                        r = posix_fallocate(f->fd, 0, f->real_file_size);
                        if (r > 0) {
@@ -87,12 +88,12 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                                                strerror(r));
                        }
                        break;
-#ifdef FIO_HAVE_LINUX_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, f->real_file_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);
@@ -100,14 +101,14 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                                td_verror(td, errno, "fallocate");
                        }
                        break;
-#endif /* FIO_HAVE_LINUX_FALLOCATE */
+#endif /* CONFIG_LINUX_FALLOCATE */
                default:
                        log_err("fio: unknown fallocate mode: %d\n",
                                td->o.fallocate_mode);
                        assert(0);
                }
        }
-#endif /* FIO_HAVE_FALLOCATE */
+#endif /* CONFIG_POSIX_FALLOCATE */
 
        if (!new_layout)
                goto done;
@@ -118,7 +119,7 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
         */
        if (!td->o.fill_device) {
                dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
-                                                       f->real_file_size);
+                                       (unsigned long long) f->real_file_size);
                if (ftruncate(f->fd, f->real_file_size) == -1) {
                        td_verror(td, errno, "ftruncate");
                        goto err;
@@ -563,7 +564,7 @@ open_again:
                if (__e == EMFILE && file_close_shadow_fds(td))
                        goto open_again;
 
-               snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
+               snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
 
                if (__e == EINVAL && (flags & OS_O_DIRECT)) {
                        log_err("fio: looks like your file system does not " \
@@ -709,7 +710,7 @@ static unsigned long long get_fs_free_counts(struct thread_data *td)
        return ret;
 }
 
-unsigned long long get_start_offset(struct thread_data *td)
+uint64_t get_start_offset(struct thread_data *td)
 {
        return td->o.start_offset +
                (td->thread_number - 1) * td->o.offset_increment;
@@ -907,10 +908,12 @@ static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
 {
        unsigned int range_size, seed;
        unsigned long nranges;
+       uint64_t file_size;
 
        range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
+       file_size = min(f->real_file_size, f->io_size);
 
-       nranges = (f->real_file_size + range_size - 1) / range_size;
+       nranges = (file_size + range_size - 1) / range_size;
 
        seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
        if (!td->o.rand_repeatable)
@@ -954,14 +957,16 @@ int init_random_map(struct thread_data *td)
                return 0;
 
        for_each_file(td, f, i) {
-               blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
-                               (unsigned long long) td->o.rw_min_bs;
+               uint64_t file_size = min(f->real_file_size, f->io_size);
+
+               blocks = file_size / (unsigned long long) td->o.rw_min_bs;
+
                if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
                        unsigned long seed;
 
                        seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
                        
-                       if (!lfsr_init(&f->lfsr, blocks, seed))
+                       if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
                                continue;
                } else if (!td->o.norandommap) {
                        f->io_axmap = axmap_new(blocks);
@@ -1069,14 +1074,18 @@ int add_file(struct thread_data *td, const char *fname)
 
        f->fd = -1;
        f->shadow_fd = -1;
-       fio_file_reset(f);
+       fio_file_reset(td, f);
 
        if (td->files_size <= td->files_index) {
-               int new_size = td->o.nr_files + 1;
+               unsigned int new_size = td->o.nr_files + 1;
 
                dprint(FD_FILE, "resize file array to %d files\n", new_size);
 
                td->files = realloc(td->files, new_size * sizeof(f));
+               if (td->files == NULL) {
+                       log_err("fio: realloc OOM\n");
+                       assert(0);
+               }
                td->files_size = new_size;
        }
        td->files[cur_files] = f;
@@ -1242,7 +1251,7 @@ static int recurse_dir(struct thread_data *td, const char *dirname)
        if (!D) {
                char buf[FIO_VERROR_SIZE];
 
-               snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
+               snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
                td_verror(td, errno, buf);
                return 1;
        }
@@ -1311,7 +1320,7 @@ void dup_files(struct thread_data *td, struct thread_data *org)
                        assert(0);
                }
                __f->fd = -1;
-               fio_file_reset(__f);
+               fio_file_reset(td, __f);
 
                if (f->file_name) {
                        __f->file_name = smalloc_strdup(f->file_name);
@@ -1351,3 +1360,13 @@ void free_release_files(struct thread_data *td)
        td->files_index = 0;
        td->nr_normal_files = 0;
 }
+
+void fio_file_reset(struct thread_data *td, struct fio_file *f)
+{
+       f->last_pos = f->file_offset;
+       f->last_start = -1ULL;
+       if (f->io_axmap)
+               axmap_reset(f->io_axmap);
+       if (td->o.random_generator == FIO_RAND_GEN_LFSR)
+               lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
+}