Commit b31f21a7 authored by Colton Lewis's avatar Colton Lewis Committed by Sean Christopherson

KVM: selftests: implement random number generator for guest code

Implement random number generator for guest code to randomize parts
of the test, making it less predictable and a more accurate reflection
of reality.

The random number generator chosen is the Park-Miller Linear
Congruential Generator, a fancy name for a basic and well-understood
random number generator entirely sufficient for this purpose.
Signed-off-by: default avatarColton Lewis <coltonlewis@google.com>
Reviewed-by: default avatarSean Christopherson <seanjc@google.com>
Reviewed-by: default avatarDavid Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20221107182208.479157-2-coltonlewis@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
parent d886724e
...@@ -77,6 +77,13 @@ struct timespec timespec_sub(struct timespec ts1, struct timespec ts2); ...@@ -77,6 +77,13 @@ struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
struct timespec timespec_elapsed(struct timespec start); struct timespec timespec_elapsed(struct timespec start);
struct timespec timespec_div(struct timespec ts, int divisor); struct timespec timespec_div(struct timespec ts, int divisor);
struct guest_random_state {
uint32_t seed;
};
struct guest_random_state new_guest_random_state(uint32_t seed);
uint32_t guest_random_u32(struct guest_random_state *state);
enum vm_mem_backing_src_type { enum vm_mem_backing_src_type {
VM_MEM_SRC_ANONYMOUS, VM_MEM_SRC_ANONYMOUS,
VM_MEM_SRC_ANONYMOUS_THP, VM_MEM_SRC_ANONYMOUS_THP,
......
...@@ -17,6 +17,23 @@ ...@@ -17,6 +17,23 @@
#include "test_util.h" #include "test_util.h"
/*
* Random number generator that is usable from guest code. This is the
* Park-Miller LCG using standard constants.
*/
struct guest_random_state new_guest_random_state(uint32_t seed)
{
struct guest_random_state s = {.seed = seed};
return s;
}
uint32_t guest_random_u32(struct guest_random_state *state)
{
state->seed = (uint64_t)state->seed * 48271 % ((uint32_t)(1 << 31) - 1);
return state->seed;
}
/* /*
* Parses "[0-9]+[kmgt]?". * Parses "[0-9]+[kmgt]?".
*/ */
......
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