Fix read/write mix and different levels of randomness
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 751e2cf7e1d7404e66e30ecd5d27f731dac01064..efbcea9eb15bb47dca4e0f802f85ad86197665de 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -83,14 +83,16 @@ struct rand_off {
 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
                                  enum fio_ddir ddir, uint64_t *b)
 {
-       uint64_t r, lastb;
-
-       lastb = last_block(td, f, ddir);
-       if (!lastb)
-               return 1;
+       uint64_t r;
 
        if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE) {
-               r = __rand(&td->__random_state);
+               uint64_t lastb;
+
+               lastb = last_block(td, f, ddir);
+               if (!lastb)
+                       return 1;
+
+               r = __rand(&td->random_state);
 
                dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
 
@@ -98,7 +100,7 @@ static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
        } else {
                uint64_t off = 0;
 
-               if (lfsr_next(&f->lfsr, &off, lastb))
+               if (lfsr_next(&f->lfsr, &off))
                        return 1;
 
                *b = off;
@@ -190,7 +192,7 @@ static int should_do_random(struct thread_data *td, enum fio_ddir ddir)
        if (td->o.perc_rand[ddir] == 100)
                return 1;
 
-       r = __rand(&td->__seq_rand_state[ddir]);
+       r = __rand(&td->seq_rand_state[ddir]);
        v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
 
        return v <= td->o.perc_rand[ddir];
@@ -206,7 +208,6 @@ static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
                return get_off_from_method(td, f, ddir, b);
 
        if (!flist_empty(&td->next_rand_list)) {
-               struct rand_off *r;
 fetch:
                r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
                flist_del(&r->list);
@@ -248,7 +249,7 @@ static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
        }
 
        dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
-                       f->file_name, (unsigned long long) f->last_pos,
+                       f->file_name, (unsigned long long) f->last_pos[ddir],
                        (unsigned long long) f->real_file_size);
        return 1;
 }
@@ -260,17 +261,17 @@ static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
 
        assert(ddir_rw(ddir));
 
-       if (f->last_pos >= f->io_size + get_start_offset(td, f) &&
+       if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
            o->time_based)
-               f->last_pos = f->last_pos - f->io_size;
+               f->last_pos[ddir] = f->last_pos[ddir] - f->io_size;
 
-       if (f->last_pos < f->real_file_size) {
+       if (f->last_pos[ddir] < f->real_file_size) {
                uint64_t pos;
 
-               if (f->last_pos == f->file_offset && o->ddir_seq_add < 0)
-                       f->last_pos = f->real_file_size;
+               if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0)
+                       f->last_pos[ddir] = f->real_file_size;
 
-               pos = f->last_pos - f->file_offset;
+               pos = f->last_pos[ddir] - f->file_offset;
                if (pos && o->ddir_seq_add) {
                        pos += o->ddir_seq_add;
 
@@ -329,8 +330,8 @@ static int get_next_block(struct thread_data *td, struct io_u *io_u,
                                *is_random = 0;
                        }
                } else if (td->o.rw_seq == RW_SEQ_IDENT) {
-                       if (f->last_start != -1ULL)
-                               offset = f->last_start - f->file_offset;
+                       if (f->last_start[ddir] != -1ULL)
+                               offset = f->last_start[ddir] - f->file_offset;
                        else
                                offset = 0;
                        ret = 0;
@@ -441,7 +442,7 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
                return 0;
 
        do {
-               r = __rand(&td->__bsrange_state);
+               r = __rand(&td->bsrange_state);
 
                if (!td->o.bssplit_nr[ddir]) {
                        buflen = 1 + (unsigned int) ((double) maxbs *
@@ -506,7 +507,7 @@ static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
        unsigned int v;
        unsigned long r;
 
-       r = __rand(&td->__rwmix_state);
+       r = __rand(&td->rwmix_state);
        v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
 
        if (v <= td->o.rwmix[DDIR_READ])
@@ -581,8 +582,8 @@ static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
        if (td_rw(td) && __should_check_rate(td, odir))
                td->rate_pending_usleep[odir] -= usec;
 
-       if (ddir_trim(ddir))
-               return ddir;
+       if (ddir == DDIR_TRIM)
+               return DDIR_TRIM;
 
        return ddir;
 }
@@ -742,7 +743,7 @@ static int fill_io_u(struct thread_data *td, struct io_u *io_u)
                 */
                if (f->file_offset >= f->real_file_size)
                        f->file_offset = f->real_file_size - f->file_offset;
-               f->last_pos = f->file_offset;
+               f->last_pos[io_u->ddir] = f->file_offset;
                td->io_skip_bytes += td->o.zone_skip;
        }
 
@@ -959,7 +960,7 @@ static struct fio_file *get_next_file_rand(struct thread_data *td,
                int opened = 0;
                unsigned long r;
 
-               r = __rand(&td->__next_file_state);
+               r = __rand(&td->next_file_state);
                fno = (unsigned int) ((double) td->o.nr_files
                                * (r / (FRAND_MAX + 1.0)));
 
@@ -1283,6 +1284,9 @@ struct io_u *__get_io_u(struct thread_data *td)
 {
        struct io_u *io_u = NULL;
 
+       if (td->stop_io)
+               return NULL;
+
        td_io_u_lock(td);
 
 again:
@@ -1467,8 +1471,8 @@ struct io_u *get_io_u(struct thread_data *td)
                        goto err_put;
                }
 
-               f->last_start = io_u->offset;
-               f->last_pos = io_u->offset + io_u->buflen;
+               f->last_start[io_u->ddir] = io_u->offset;
+               f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
 
                if (io_u->ddir == DDIR_WRITE) {
                        if (td->flags & TD_F_REFILL_BUFFERS) {
@@ -1643,13 +1647,22 @@ 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 && 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 (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 (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
@@ -1846,9 +1859,9 @@ static void save_buf_state(struct thread_data *td, struct frand_state *rs)
 void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
                    unsigned int max_bs)
 {
-       if (td->o.buffer_pattern_bytes)
-               fill_buffer_pattern(td, buf, max_bs);
-       else if (!td->o.zero_buffers) {
+       struct thread_options *o = &td->o;
+
+       if (o->compress_percentage) {
                unsigned int perc = td->o.compress_percentage;
                struct frand_state *rs;
                unsigned int left = max_bs;
@@ -1866,7 +1879,8 @@ void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
                                        seg = min_write;
 
                                fill_random_buf_percentage(rs, buf, perc, seg,
-                                                               min_write);
+                                       min_write, o->buffer_pattern,
+                                                  o->buffer_pattern_bytes);
                        } else
                                fill_random_buf(rs, buf, min_write);
 
@@ -1874,7 +1888,9 @@ void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
                        left -= min_write;
                        save_buf_state(td, rs);
                } while (left);
-       } else
+       } else if (o->buffer_pattern_bytes)
+               fill_buffer_pattern(td, buf, max_bs);
+       else
                memset(buf, 0, max_bs);
 }