Add support for non-uniformly random file service type
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 3299f2907af47bb3e6d7b59d2c35aad004509df2..c0790b254065acf59c279eda5e1d133d632d77f1 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -177,7 +177,7 @@ bail:
        /*
         * Generate a value, v, between 1 and 100, both inclusive
         */
-       v = rand_between(&td->zone_state, 1, 100);
+       v = rand32_between(&td->zone_state, 1, 100);
 
        zsi = &td->zone_state_index[ddir][v - 1];
        stotal = zsi->size_perc_prev;
@@ -279,7 +279,7 @@ static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
        if (td->o.perc_rand[ddir] == 100)
                return true;
 
-       v = rand_between(&td->seq_rand_state[ddir], 1, 100);
+       v = rand32_between(&td->seq_rand_state[ddir], 1, 100);
 
        return v <= td->o.perc_rand[ddir];
 }
@@ -328,7 +328,8 @@ static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
        if (!get_next_rand_offset(td, f, ddir, b))
                return 0;
 
-       if (td->o.time_based) {
+       if (td->o.time_based ||
+           (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
                fio_file_reset(td, f);
                if (!get_next_rand_offset(td, f, ddir, b))
                        return 0;
@@ -371,10 +372,15 @@ static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
                        /*
                         * If we reach beyond the end of the file
                         * with holed IO, wrap around to the
-                        * beginning again.
+                        * beginning again. If we're doing backwards IO,
+                        * wrap to the end.
                         */
-                       if (pos >= f->real_file_size)
-                               pos = f->file_offset;
+                       if (pos >= f->real_file_size) {
+                               if (o->ddir_seq_add > 0)
+                                       pos = f->file_offset;
+                               else
+                                       pos = f->real_file_size + o->ddir_seq_add;
+                       }
                }
 
                *offset = pos;
@@ -601,7 +607,7 @@ static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
 {
        unsigned int v;
 
-       v = rand_between(&td->rwmix_state, 1, 100);
+       v = rand32_between(&td->rwmix_state, 1, 100);
 
        if (v <= td->o.rwmix[DDIR_READ])
                return DDIR_READ;
@@ -1065,6 +1071,34 @@ static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
                io_u_mark_lat_msec(td, usec / 1000);
 }
 
+static unsigned int __get_next_fileno_rand(struct thread_data *td)
+{
+       unsigned long fileno;
+
+       if (td->o.file_service_type == FIO_FSERVICE_RANDOM) {
+               uint64_t frand_max = rand_max(&td->next_file_state);
+               unsigned long r;
+
+               r = __rand(&td->next_file_state);
+               return (unsigned int) ((double) td->o.nr_files
+                               * (r / (frand_max + 1.0)));
+       }
+
+       if (td->o.file_service_type == FIO_FSERVICE_ZIPF)
+               fileno = zipf_next(&td->next_file_zipf);
+       else if (td->o.file_service_type == FIO_FSERVICE_PARETO)
+               fileno = pareto_next(&td->next_file_zipf);
+       else if (td->o.file_service_type == FIO_FSERVICE_GAUSS)
+               fileno = gauss_next(&td->next_file_gauss);
+       else {
+               log_err("fio: bad file service type: %d\n", td->o.file_service_type);
+               assert(0);
+               return 0;
+       }
+
+       return fileno >> FIO_FSERVICE_SHIFT;
+}
+
 /*
  * Get next file to service by choosing one at random
  */
@@ -1072,17 +1106,13 @@ static struct fio_file *get_next_file_rand(struct thread_data *td,
                                           enum fio_file_flags goodf,
                                           enum fio_file_flags badf)
 {
-       uint64_t frand_max = rand_max(&td->next_file_state);
        struct fio_file *f;
        int fno;
 
        do {
                int opened = 0;
-               unsigned long r;
 
-               r = __rand(&td->next_file_state);
-               fno = (unsigned int) ((double) td->o.nr_files
-                               * (r / (frand_max + 1.0)));
+               fno = __get_next_fileno_rand(td);
 
                f = td->files[fno];
                if (fio_file_done(f))
@@ -1235,10 +1265,14 @@ static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
                put_file_log(td, f);
                td_io_close_file(td, f);
                io_u->file = NULL;
-               fio_file_set_done(f);
-               td->nr_done_files++;
-               dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
+               if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
+                       fio_file_reset(td, f);
+               else {
+                       fio_file_set_done(f);
+                       td->nr_done_files++;
+                       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;
@@ -1710,16 +1744,18 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
                }
        }
 
-       if (!td->o.disable_clat) {
-               add_clat_sample(td, idx, lusec, bytes, io_u->offset);
-               io_u_mark_latency(td, lusec);
-       }
+       if (ddir_rw(idx)) {
+               if (!td->o.disable_clat) {
+                       add_clat_sample(td, idx, lusec, bytes, io_u->offset);
+                       io_u_mark_latency(td, lusec);
+               }
 
-       if (!td->o.disable_bw)
-               add_bw_sample(td, idx, bytes, &icd->time);
+               if (!td->o.disable_bw && per_unit_log(td->bw_log))
+                       add_bw_sample(td, io_u, bytes, lusec);
 
-       if (no_reduce)
-               add_iops_sample(td, idx, bytes, &icd->time);
+               if (no_reduce && per_unit_log(td->iops_log))
+                       add_iops_sample(td, io_u, bytes);
+       }
 
        if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) {
                uint32_t *info = io_u_block_info(td, io_u);
@@ -1735,6 +1771,28 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
        }
 }
 
+static void file_log_write_comp(const struct thread_data *td, struct fio_file *f,
+                               uint64_t offset, unsigned int bytes)
+{
+       int idx;
+
+       if (!f)
+               return;
+
+       if (f->first_write == -1ULL || offset < f->first_write)
+               f->first_write = offset;
+       if (f->last_write == -1ULL || ((offset + bytes) > f->last_write))
+               f->last_write = offset + bytes;
+
+       if (!f->last_write_comp)
+               return;
+
+       idx = f->last_write_idx++;
+       f->last_write_comp[idx] = offset;
+       if (f->last_write_idx == td->o.iodepth)
+               f->last_write_idx = 0;
+}
+
 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
                         struct io_completion_data *icd)
 {
@@ -1785,23 +1843,8 @@ static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
                if (!(io_u->flags & IO_U_F_VER_LIST))
                        td->this_io_bytes[ddir] += bytes;
 
-               if (ddir == DDIR_WRITE) {
-                       if (f) {
-                               if (f->first_write == -1ULL ||
-                                   io_u->offset < f->first_write)
-                                       f->first_write = io_u->offset;
-                               if (f->last_write == -1ULL ||
-                                   ((io_u->offset + bytes) > f->last_write))
-                                       f->last_write = io_u->offset + bytes;
-                       }
-                       if (td->last_write_comp) {
-                               int idx = td->last_write_idx++;
-
-                               td->last_write_comp[idx] = io_u->offset;
-                               if (td->last_write_idx == td->o.iodepth)
-                                       td->last_write_idx = 0;
-                       }
-               }
+               if (ddir == DDIR_WRITE)
+                       file_log_write_comp(td, f, io_u->offset, bytes);
 
                if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
                                           td->runstate == TD_VERIFYING))
@@ -1964,7 +2007,7 @@ static struct frand_state *get_buf_state(struct thread_data *td)
                return &td->buf_state;
        }
 
-       v = rand_between(&td->dedupe_state, 1, 100);
+       v = rand32_between(&td->dedupe_state, 1, 100);
 
        if (v <= td->o.dedupe_percentage)
                return &td->buf_state_prev;