Add buffer_compress_percentage
[fio.git] / lib / rand.c
index 7c6fed1fabbcc01f1803f00521d881a9c9662603..66d04729a49eaa073f822d77a93b66ae426f659d 100644 (file)
@@ -33,6 +33,8 @@
 
 */
 
+#include <string.h>
+#include <assert.h>
 #include "rand.h"
 #include "../hash.h"
 
@@ -88,3 +90,43 @@ unsigned long fill_random_buf(struct frand_state *fs, void *buf,
        __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;
+}