More file setting bug fixes
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 62a76b9db9c4b98759fbb46f86231ea0c9ac9539..27014c8aad503b4cb8ed7eba374a954b6b7c6a2d 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -194,13 +194,20 @@ static int get_next_offset(struct thread_data *td, struct io_u *io_u)
        if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
                td->ddir_nr = td->o.ddir_nr;
 
-               if (get_next_rand_offset(td, f, ddir, &b))
+               if (get_next_rand_offset(td, f, ddir, &b)) {
+                       dprint(FD_IO, "%s: getting rand offset failed\n",
+                               f->file_name);
                        return 1;
+               }
        } else {
                if (f->last_pos >= f->real_file_size) {
                        if (!td_random(td) ||
-                            get_next_rand_offset(td, f, ddir, &b))
+                            get_next_rand_offset(td, f, ddir, &b)) {
+                               dprint(FD_IO, "%s: pos %llu > size %llu\n",
+                                               f->file_name, f->last_pos,
+                                               f->real_file_size);
                                return 1;
+                       }
                } else
                        b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
        }
@@ -621,6 +628,7 @@ static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
 
        do {
                long r = os_random_long(&td->next_file_state);
+               int opened = 0;
 
                fno = (unsigned int) ((double) td->o.nr_files
                        * (r / (OS_RAND_MAX + 1.0)));
@@ -628,10 +636,21 @@ static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
                if (f->flags & FIO_FILE_DONE)
                        continue;
 
+               if (!(f->flags & FIO_FILE_OPEN)) {
+                       int err;
+
+                       err = td_io_open_file(td, f);
+                       if (err)
+                               continue;
+                       opened = 1;
+               }
+
                if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
                        dprint(FD_FILE, "get_next_file_rand: %p\n", f);
                        return f;
                }
+               if (opened)
+                       td_io_close_file(td, f);
        } while (1);
 }
 
@@ -645,20 +664,39 @@ static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
        struct fio_file *f;
 
        do {
+               int opened = 0;
+
                f = td->files[td->next_file];
 
                td->next_file++;
                if (td->next_file >= td->o.nr_files)
                        td->next_file = 0;
 
+               dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
                if (f->flags & FIO_FILE_DONE) {
                        f = NULL;
                        continue;
                }
 
+               if (!(f->flags & FIO_FILE_OPEN)) {
+                       int err;
+
+                       err = td_io_open_file(td, f);
+                       if (err) {
+                               dprint(FD_FILE, "error %d on open of %s\n",
+                                       err, f->file_name);
+                               continue;
+                       }
+                       opened = 1;
+               }
+
+               dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf, f->flags);
                if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
                        break;
 
+               if (opened)
+                       td_io_close_file(td, f);
+
                f = NULL;
        } while (td->next_file != old_next_file);
 
@@ -672,7 +710,7 @@ static struct fio_file *get_next_file(struct thread_data *td)
 
        assert(td->o.nr_files <= td->files_index);
 
-       if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
+       if (td->nr_done_files >= td->o.nr_files) {
                dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
                                " nr_files=%d\n", td->nr_open_files,
                                                  td->nr_done_files,
@@ -681,10 +719,15 @@ static struct fio_file *get_next_file(struct thread_data *td)
        }
 
        f = td->file_service_file;
-       if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
-               goto out;
+       if (f && (f->flags & FIO_FILE_OPEN) && !(f->flags & FIO_FILE_CLOSING)) {
+               if (td->o.file_service_type == FIO_FSERVICE_SEQ)
+                       goto out;
+               if (td->file_service_left--)
+                       goto out;
+       }
 
-       if (td->o.file_service_type == FIO_FSERVICE_RR)
+       if (td->o.file_service_type == FIO_FSERVICE_RR ||
+           td->o.file_service_type == FIO_FSERVICE_SEQ)
                f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
        else
                f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
@@ -692,22 +735,7 @@ static struct fio_file *get_next_file(struct thread_data *td)
        td->file_service_file = f;
        td->file_service_left = td->file_service_nr - 1;
 out:
-       dprint(FD_FILE, "get_next_file: %p\n", f);
-       return f;
-}
-
-static struct fio_file *find_next_new_file(struct thread_data *td)
-{
-       struct fio_file *f;
-
-       if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
-               return NULL;
-
-       if (td->o.file_service_type == FIO_FSERVICE_RR)
-               f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
-       else
-               f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
-
+       dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
        return f;
 }
 
@@ -737,28 +765,12 @@ set_file:
                        goto set_file;
                }
 
-               /*
-                * td_io_close() does a put_file() as well, so no need to
-                * do that here.
-                */
-               io_u->file = NULL;
+               put_file_log(td, f);
                td_io_close_file(td, f);
+               io_u->file = NULL;
                f->flags |= FIO_FILE_DONE;
                td->nr_done_files++;
-
-               /*
-                * probably not the right place to do this, but see
-                * if we need to open a new file
-                */
-               if (td->nr_open_files < td->o.open_files &&
-                   td->o.open_files != td->o.nr_files) {
-                       f = find_next_new_file(td);
-
-                       if (!f || td_io_open_file(td, f))
-                               return 1;
-
-                       goto set_file;
-               }
+               dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name, td->nr_done_files, td->o.nr_files);
        } while (1);
 
        return 0;
@@ -851,7 +863,8 @@ struct io_u *get_io_u(struct thread_data *td)
 
 out:
        if (!td_io_prep(td, io_u)) {
-               fio_gettime(&io_u->start_time, NULL);
+               if (!td->o.disable_slat)
+                       fio_gettime(&io_u->start_time, NULL);
                return io_u;
        }
 err_put:
@@ -881,7 +894,11 @@ void io_u_log_error(struct thread_data *td, struct io_u *io_u)
 static void io_completed(struct thread_data *td, struct io_u *io_u,
                         struct io_completion_data *icd)
 {
-       unsigned long usec;
+       /*
+        * Older gcc's are too dumb to realize that usec is always used
+        * initialized, silence that warning.
+        */
+       unsigned long uninitialized_var(usec);
 
        dprint_io_u(io_u, "io complete");