tools/testing/selftests/kvm/lib/memstress.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/lib/memstress.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/kvm/lib/memstress.c- Extension
.c- Size
- 10234 bytes
- Lines
- 388
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
inttypes.hlinux/bitmap.hkvm_util.hmemstress.hprocessor.hucall_common.h
Detected Declarations
struct vcpu_threadfunction memstress_guest_codefunction memstress_setup_vcpusfunction memstress_destroy_vmfunction memstress_set_write_percentfunction memstress_set_random_accessfunction memstress_nested_pagesfunction memstress_setup_nestedfunction memstress_start_vcpu_threadsfunction memstress_join_vcpu_threadsfunction toggle_dirty_loggingfunction memstress_enable_dirty_loggingfunction memstress_disable_dirty_loggingfunction memstress_get_dirty_logfunction memstress_clear_dirty_logfunction memstress_free_bitmaps
Annotated Snippet
struct vcpu_thread {
/* The index of the vCPU. */
int vcpu_idx;
/* The pthread backing the vCPU. */
pthread_t thread;
/* Set to true once the vCPU thread is up and running. */
bool running;
};
/* The vCPU threads involved in this test. */
static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
/* The function run by each vCPU thread, as provided by the test. */
static void (*vcpu_thread_fn)(struct memstress_vcpu_args *);
/* Set to true once all vCPU threads are up and running. */
static bool all_vcpu_threads_running;
static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
/*
* Continuously write to the first 8 bytes of each page in the
* specified region.
*/
void memstress_guest_code(u32 vcpu_idx)
{
struct memstress_args *args = &memstress_args;
struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];
struct guest_random_state rand_state;
gva_t gva;
u64 pages;
u64 addr;
u64 page;
int i;
rand_state = new_guest_random_state(guest_random_seed + vcpu_idx);
gva = vcpu_args->gva;
pages = vcpu_args->pages;
/* Make sure vCPU args data structure is not corrupt. */
GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
while (true) {
for (i = 0; i < sizeof(memstress_args); i += args->guest_page_size)
(void) *((volatile char *)args + i);
for (i = 0; i < pages; i++) {
if (args->random_access)
page = guest_random_u32(&rand_state) % pages;
else
page = i;
addr = gva + (page * args->guest_page_size);
if (__guest_random_bool(&rand_state, args->write_percent))
*(u64 *)addr = 0x0123456789ABCDEF;
else
READ_ONCE(*(u64 *)addr);
}
GUEST_SYNC(1);
}
}
void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
struct kvm_vcpu *vcpus[],
u64 vcpu_memory_bytes,
bool partition_vcpu_memory_access)
{
struct memstress_args *args = &memstress_args;
struct memstress_vcpu_args *vcpu_args;
int i;
for (i = 0; i < nr_vcpus; i++) {
vcpu_args = &args->vcpu_args[i];
vcpu_args->vcpu = vcpus[i];
vcpu_args->vcpu_idx = i;
if (partition_vcpu_memory_access) {
vcpu_args->gva = guest_test_virt_mem +
(i * vcpu_memory_bytes);
vcpu_args->pages = vcpu_memory_bytes /
args->guest_page_size;
vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes);
} else {
vcpu_args->gva = guest_test_virt_mem;
Annotation
- Immediate include surface: `inttypes.h`, `linux/bitmap.h`, `kvm_util.h`, `memstress.h`, `processor.h`, `ucall_common.h`.
- Detected declarations: `struct vcpu_thread`, `function memstress_guest_code`, `function memstress_setup_vcpus`, `function memstress_destroy_vm`, `function memstress_set_write_percent`, `function memstress_set_random_access`, `function memstress_nested_pages`, `function memstress_setup_nested`, `function memstress_start_vcpu_threads`, `function memstress_join_vcpu_threads`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.