crosvm/swap/tests/common/mod.rs
Shintaro Kawamura fc88a6091d swap: add disable command to vmm-swap
Handling page faults using userfaultfd for all the time after vmm-swap
is enabled originally has overhead. By disabling swapping, we can use
crosvm without the overhead when the crosvm become used heavily again.

This also refactor PageHandler and extract register logic to lib.rs.

BUG=b:215093219
TEST=cargo test -p swap

Change-Id: Id81d84ebe40067b4f19b212f6b81cbaf249c0c3c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4005474
Reviewed-by: David Stevens <stevensd@chromium.org>
Commit-Queue: Shin Kawamura <kawasin@google.com>
2022-11-18 01:23:45 +00:00

38 lines
1,009 B
Rust

// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use base::MemoryMapping;
use base::MemoryMappingBuilder;
use base::SharedMemory;
use data_model::VolatileMemory;
use swap::userfaultfd::Userfaultfd;
use userfaultfd::UffdBuilder;
pub fn create_uffd_for_test() -> Userfaultfd {
UffdBuilder::new()
.non_blocking(false)
.create()
.unwrap()
.into()
}
pub struct SharedMemoryMapping {
pub shm: SharedMemory,
pub mmap: MemoryMapping,
}
impl SharedMemoryMapping {
pub fn base_addr(&self) -> usize {
self.mmap.get_ref::<u8>(0).unwrap().as_mut_ptr() as usize
}
}
pub fn create_shared_memory(name: &str, size: usize) -> SharedMemoryMapping {
let shm = SharedMemory::new(name, size as u64).unwrap();
let mmap = MemoryMappingBuilder::new(size)
.from_shared_memory(&shm)
.build()
.unwrap();
SharedMemoryMapping { shm, mmap }
}