Commit aa2558ce authored by Konstantin Khlebnikov's avatar Konstantin Khlebnikov

ioping: use xorshift128+ for randomization

Signed-off-by: default avatarKonstantin Khlebnikov <koct9i@gmail.com>
parent 37608312
...@@ -963,24 +963,40 @@ void set_signal(void) ...@@ -963,24 +963,40 @@ void set_signal(void)
#endif /* __MINGW32__ */ #endif /* __MINGW32__ */
void random_memory(void *buf, size_t len) static unsigned long long random_state[2];
/* xorshift128+ */
static inline unsigned long long random64(void)
{ {
unsigned char *ptr = buf; unsigned long long s1 = random_state[0];
size_t i; const unsigned long long s0 = random_state[1];
random_state[0] = s0;
s1 ^= s1 << 23; // a
random_state[1] = s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26); // b, c
return random_state[1] + s0;
}
for (i = 0; i < len; i++) static void random_init(void)
ptr[i] = random(); {
srandom(now());
random_state[0] = random();
random_state[1] = random();
(void)random64();
(void)random64();
} }
/* not so random but much faster */ static void random_memory(void *buf, size_t len)
void shake_memory(void *buf, size_t len)
{ {
unsigned int *ptr = buf; unsigned long long *ptr = buf;
unsigned int word = random(); size_t words = len >> 3;
size_t i;
while (words--)
*ptr++ = random64();
for (i = 0, len /= 4; i < len; i++) if (len & 7) {
ptr[i] ^= word; unsigned long long last = random64();
memcpy(ptr, &last, len & 7);
}
} }
int main (int argc, char **argv) int main (int argc, char **argv)
...@@ -1094,6 +1110,8 @@ int main (int argc, char **argv) ...@@ -1094,6 +1110,8 @@ int main (int argc, char **argv)
if (ret) if (ret)
errx(2, "buffer allocation failed"); errx(2, "buffer allocation failed");
random_init();
random_memory(buf, size); random_memory(buf, size);
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
...@@ -1141,8 +1159,6 @@ skip_preparation: ...@@ -1141,8 +1159,6 @@ skip_preparation:
#endif #endif
} }
srandom(now());
if (deadline) if (deadline)
deadline += now(); deadline += now();
...@@ -1168,7 +1184,7 @@ skip_preparation: ...@@ -1168,7 +1184,7 @@ skip_preparation:
part_request++; part_request++;
if (randomize) if (randomize)
woffset = random() % (wsize / size) * size; woffset = random64() % (wsize / size) * size;
#ifdef HAVE_POSIX_FADVICE #ifdef HAVE_POSIX_FADVICE
if (!cached) { if (!cached) {
...@@ -1180,7 +1196,7 @@ skip_preparation: ...@@ -1180,7 +1196,7 @@ skip_preparation:
#endif #endif
if (write_test) if (write_test)
shake_memory(buf, size); random_memory(buf, size);
this_time = now(); this_time = now();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment