Add buffer_compress_percentage
[fio.git] / lib / rand.c
index 3b2d67ad6e9ebc4a242b24c162f94aa667a11743..66d04729a49eaa073f822d77a93b66ae426f659d 100644 (file)
 
 */
 
+#include <string.h>
+#include <assert.h>
 #include "rand.h"
 #include "../hash.h"
 
-struct frand_state __fio_rand_state;
-
 static inline int __seed(unsigned int x, unsigned int m)
 {
        return (x < m) ? x + m : x;
@@ -79,13 +79,54 @@ void __fill_random_buf(void *buf, unsigned int len, unsigned long seed)
        }
 }
 
-unsigned long fill_random_buf(void *buf, unsigned int len)
+unsigned long fill_random_buf(struct frand_state *fs, void *buf,
+                             unsigned int len)
 {
-       unsigned long r = __rand(&__fio_rand_state);
+       unsigned long r = __rand(fs);
 
        if (sizeof(int) != sizeof(long *))
-               r *= (unsigned long) __rand(&__fio_rand_state);
+               r *= (unsigned long) __rand(fs);
 
        __fill_random_buf(buf, len, r);
        return r;
 }
+
+unsigned long fill_random_buf_percentage(struct frand_state *fs, void *buf,
+                                        unsigned int percentage,
+                                        unsigned int segment, unsigned int len)
+{
+       unsigned int this_len, rep_len;
+       unsigned long r = __rand(fs);
+
+       assert(segment <= len);
+
+       if (sizeof(int) != sizeof(long *))
+               r *= (unsigned long) __rand(fs);
+
+       while (len) {
+               /*
+                * Fill random chunk
+                */
+               this_len = (segment * (100 - percentage)) / 100;
+               if (this_len > len)
+                       this_len = len;
+
+               __fill_random_buf(buf, this_len, r);
+
+               len -= this_len;
+               buf += this_len;
+
+               /*
+                * Now duplicate random chunk in rest of buf
+                */
+               rep_len = segment - this_len;
+               if (rep_len > len)
+                       rep_len = len;
+
+               memcpy(buf, buf + rep_len, rep_len);
+               buf += rep_len;
+               len -= rep_len;
+       }
+
+       return r;
+}