Untangle the file creation mess
authorJens Axboe <jens.axboe@oracle.com>
Thu, 15 Mar 2007 19:49:25 +0000 (20:49 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Thu, 15 Mar 2007 19:49:25 +0000 (20:49 +0100)
Still needs a lot of work, will do a rewrite of this soonish. At
least it should work now.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
filesetup.c
fio.c
fio.h
init.c

index 3b3f8684366ea6321915e4861bcce981dc2d8fcf..08155db1ac256d238175dd96d56afc050e455538 100644 (file)
@@ -119,6 +119,28 @@ static unsigned long long set_rand_file_size(struct thread_data *td,
        return ret;
 }
 
+static int fill_file_size(struct thread_data *td, struct fio_file *f,
+                         unsigned long long *file_size, int new_files)
+{
+       if (!td->o.file_size_low) {
+               f->file_size = *file_size / new_files;
+               f->real_file_size = f->file_size;
+       } else {
+               /*
+                * If we don't have enough space left for a file
+                * of the minimum size, bail.
+                */
+               if (*file_size < td->o.file_size_low)
+                       return 1;
+
+               f->file_size = set_rand_file_size(td, *file_size);
+               f->real_file_size = f->file_size;
+               *file_size -= f->file_size;
+       }
+
+       return 0;
+}
+
 static int create_files(struct thread_data *td)
 {
        struct fio_file *f;
@@ -150,8 +172,17 @@ static int create_files(struct thread_data *td)
         * unless specifically asked for overwrite, let normal io extend it
         */
        can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
-       if (can_extend)
+       if (can_extend) {
+               for_each_file(td, f, i) {
+                       if (fill_file_size(td, f, &total_file_size, new_files)) {
+                               log_info("fio: limited to %d files\n", i);
+                               td->o.nr_files = i;
+                               break;
+                       }
+               }
+
                return 0;
+       }
 
        local_file_size = total_file_size;
        if (!local_file_size)
@@ -170,22 +201,11 @@ static int create_files(struct thread_data *td)
                        continue;
                }
 
-               if (!td->o.file_size_low)
-                       f->file_size = total_file_size / new_files;
-               else {
-                       /*
-                        * If we don't have enough space left for a file
-                        * of the minimum size, bail.
-                        */
-                       if (local_file_size < td->o.file_size_low) {
-                               log_info("fio: limited to %d files\n", i);
-                               new_files -= (td->o.nr_files - i);
-                               td->o.nr_files = i;
-                               break;
-                       }
-
-                       f->file_size = set_rand_file_size(td, local_file_size);
-                       local_file_size -= f->file_size;
+               if (fill_file_size(td, f, &local_file_size, new_files)) {
+                       log_info("fio: limited to %d files\n", i);
+                       new_files -= (td->o.nr_files - i);
+                       td->o.nr_files = i;
+                       break;
                }
 
                total_file_size += f->file_size;
@@ -461,6 +481,30 @@ int setup_files(struct thread_data *td)
        return err;
 }
 
+int init_random_map(struct thread_data *td)
+{
+       int num_maps, blocks;
+       struct fio_file *f;
+       unsigned int i;
+
+       if (td->o.norandommap)
+               return 0;
+
+       for_each_file(td, f, i) {
+               blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
+               num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
+               f->file_map = malloc(num_maps * sizeof(long));
+               if (!f->file_map) {
+                       log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
+                       return 1;
+               }
+               f->num_maps = num_maps;
+               memset(f->file_map, 0, num_maps * sizeof(long));
+       }
+
+       return 0;
+}
+
 void close_files(struct thread_data *td)
 {
        struct fio_file *f;
diff --git a/fio.c b/fio.c
index de5c976e85cb0d636eadd21768fefaf601a66c95..a416856459c12ccf59cef5792efd126701bd7c0c 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -773,6 +773,9 @@ static void *thread_main(void *data)
        if (open_files(td))
                goto err;
 
+       if (init_random_map(td))
+               goto err;
+
        if (td->o.exec_prerun) {
                if (system(td->o.exec_prerun) < 0)
                        goto err;
diff --git a/fio.h b/fio.h
index cfbe37838962fe7c5220cd83c5f900fde19ca317..6e9311be9517347892e1adc4dd8037f57676c31d 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -692,6 +692,7 @@ extern void add_file(struct thread_data *, const char *);
 extern void get_file(struct fio_file *);
 extern void put_file(struct thread_data *, struct fio_file *);
 extern int add_dir_files(struct thread_data *, const char *);
+extern int init_random_map(struct thread_data *);
 
 /*
  * ETA/status stuff
diff --git a/init.c b/init.c
index e38d67786a1a80284c1886a90b3e083a19afc5e1..23fe8dc69e2bfb820e9de322919bd315eac9249a 100644 (file)
--- a/init.c
+++ b/init.c
@@ -323,9 +323,7 @@ static int exists_and_not_file(const char *filename)
 static int init_random_state(struct thread_data *td)
 {
        unsigned long seeds[6];
-       int fd, num_maps, blocks;
-       struct fio_file *f;
-       unsigned int i;
+       int fd;
 
        fd = open("/dev/urandom", O_RDONLY);
        if (fd == -1) {
@@ -356,20 +354,6 @@ static int init_random_state(struct thread_data *td)
        if (td->o.rand_repeatable)
                seeds[4] = FIO_RANDSEED * td->thread_number;
 
-       if (!td->o.norandommap) {
-               for_each_file(td, f, i) {
-                       blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
-                       num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
-                       f->file_map = malloc(num_maps * sizeof(long));
-                       if (!f->file_map) {
-                               log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
-                               return 1;
-                       }
-                       f->num_maps = num_maps;
-                       memset(f->file_map, 0, num_maps * sizeof(long));
-               }
-       }
-
        os_random_seed(seeds[4], &td->random_state);
        return 0;
 }