[PATCH] OS abstract out the random seed and value retrieval
authorJens Axboe <axboe@suse.de>
Wed, 7 Jun 2006 11:57:06 +0000 (13:57 +0200)
committerJens Axboe <axboe@suse.de>
Wed, 7 Jun 2006 11:57:06 +0000 (13:57 +0200)
This is pretty crappy on FreeBSD, the seed function doesn't seed the
state we want. But it works, so it'll have to suffice for now...

fio.c
fio.h
init.c
os-freebsd.h
os-linux.h

diff --git a/fio.c b/fio.c
index 1e94cc6..09c960f 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -160,7 +160,7 @@ static int get_next_offset(struct thread_data *td, unsigned long long *offset)
                int loops = 50;
 
                do {
-                       lrand48_r(&td->random_state, &r);
+                       r = os_random_long(&td->random_state);
                        b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
                        rb = b + (td->file_offset / td->min_bs);
                        loops--;
@@ -188,7 +188,7 @@ static unsigned int get_next_buflen(struct thread_data *td)
        if (td->min_bs == td->max_bs)
                buflen = td->min_bs;
        else {
-               lrand48_r(&td->bsrange_state, &r);
+               r = os_random_long(&td->bsrange_state);
                buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
                buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
        }
@@ -250,7 +250,7 @@ static void fill_random_bytes(struct thread_data *td,
        double r;
 
        while (len) {
-               drand48_r(&td->verify_state, &r);
+               r = os_random_double(&td->verify_state);
 
                /*
                 * lrand48_r seems to be broken and only fill the bottom
@@ -363,7 +363,7 @@ static int get_rw_ddir(struct thread_data *td)
                        unsigned long v;
                        long r;
 
-                       lrand48_r(&td->random_state, &r);
+                       r = os_random_long(&td->random_state);
                        v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
                        if (v < td->rwmixread)
                                td->rwmix_ddir = DDIR_READ;
diff --git a/fio.h b/fio.h
index 89d7bf9..0b25bb8 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -169,8 +169,8 @@ struct thread_data {
 
        char *ioscheduler;
 
-       struct drand48_data bsrange_state;
-       struct drand48_data verify_state;
+       os_random_state_t bsrange_state;
+       os_random_state_t verify_state;
 
        int shm_id;
 
@@ -207,7 +207,7 @@ struct thread_data {
        unsigned long long last_pos;
        sem_t mutex;
 
-       struct drand48_data random_state;
+       os_random_state_t random_state;
        unsigned long *file_map;
        unsigned int num_maps;
 
@@ -237,7 +237,7 @@ struct thread_data {
        unsigned int do_disk_util;
        unsigned int override_sync;
 
-       struct drand48_data rwmix_state;
+       os_random_state_t rwmix_state;
        struct timeval rwmix_switch;
        int rwmix_ddir;
 
diff --git a/init.c b/init.c
index d02056c..1bd79e2 100644 (file)
--- a/init.c
+++ b/init.c
@@ -256,9 +256,9 @@ int init_random_state(struct thread_data *td)
 
        close(fd);
 
-       srand48_r(seeds[0], &td->bsrange_state);
-       srand48_r(seeds[1], &td->verify_state);
-       srand48_r(seeds[2], &td->rwmix_state);
+       os_random_seed(seeds[0], &td->bsrange_state);
+       os_random_seed(seeds[1], &td->verify_state);
+       os_random_seed(seeds[2], &td->rwmix_state);
 
        if (td->sequential)
                return 0;
@@ -272,7 +272,7 @@ int init_random_state(struct thread_data *td)
        td->num_maps = num_maps;
        memset(td->file_map, 0, num_maps * sizeof(long));
 
-       srand48_r(seeds[3], &td->random_state);
+       os_random_seed(seeds[3], &td->random_state);
        return 0;
 }
 
index 31fa010..b56ba80 100644 (file)
@@ -11,6 +11,7 @@
 #define OS_MAP_ANON            (MAP_ANON)
 
 typedef unsigned long os_cpu_mask_t;
+typedef unsigned int os_random_state_t;
 
 /*
  * FIXME
@@ -30,4 +31,24 @@ static inline unsigned long long os_phys_mem(void)
        return mem;
 }
 
+static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
+{
+       srand(seed);
+}
+
+static inline long os_random_long(os_random_state_t *rs)
+{
+       long val;
+
+       val = rand_r(rs);
+       return val;
+}
+
+static inline double os_random_double(os_random_state_t *rs)
+{
+       double val;
+
+       val = (double) rand_r(rs);
+       return val;
+}
 #endif
index f2df97d..f9094cb 100644 (file)
@@ -20,6 +20,7 @@
 #define OS_MAP_ANON            (MAP_ANONYMOUS)
 
 typedef cpu_set_t os_cpu_mask_t;
+typedef struct drand48_data os_random_state_t;
 
 /*
  * we want fadvise64 really, but it's so tangled... later
@@ -99,4 +100,25 @@ static inline unsigned long long os_phys_mem(void)
        return (unsigned long long) pages * (unsigned long long) pagesize;
 }
 
+static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
+{
+       srand48_r(seed, rs);
+}
+
+static inline long os_random_long(os_random_state_t *rs)
+{
+       long val;
+
+       lrand48_r(rs, &val);
+       return val;
+}
+
+static inline double os_random_double(os_random_state_t *rs)
+{
+       double val;
+
+       drand48_r(rs, &val);
+       return val;
+}
+
 #endif