mirror of
https://github.com/torvalds/linux.git
synced 2026-05-24 23:22:31 +02:00
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: Colton Lewis <coltonlewis@google.com> Reviewed-by: Sean Christopherson <seanjc@google.com> Reviewed-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20221107182208.479157-2-coltonlewis@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
parent
d886724ea8
commit
b31f21a7e9
|
|
@ -77,6 +77,13 @@ struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
|
|||
struct timespec timespec_elapsed(struct timespec start);
|
||||
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 {
|
||||
VM_MEM_SRC_ANONYMOUS,
|
||||
VM_MEM_SRC_ANONYMOUS_THP,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,23 @@
|
|||
|
||||
#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]?".
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user