Struct swap::staging::StagingMemory
source · pub struct StagingMemory {
mmap: MemoryMapping,
present_list: PresentList,
}
Expand description
StagingMemory stores active pages from the guest memory in anonymous private memory.
StagingMemory is created per memory region.
On crosvm swap enable
command, the monitor process moves all the active pages in the guest
memory to this staging memory. StagingMemory has several advantages over writing all
pages from the guest memory to the swap file directly.
- Less non-responsive time
- While moving the guest memory pages, the monitor process have to freeze whole crosvm
- processes to guarantee no updates on the guest memory. Moving memory is faster than writing
- them to disk.
- Hot pages bypass the disk
- The faulting pages between
crosvm swap enable
andcrosvm swap out
are swapped in from - this StagingMemory directly without written into the swap file. This saves disk resouces
- and latency of page fault handling.
- The faulting pages between
NB: Staging memory is a memfd instead of private anonymous memory to match GuestMemory. This is done to make accounting easier when calculating total guest memory consumption.
Fields§
§mmap: MemoryMapping
§present_list: PresentList
Implementations§
source§impl StagingMemory
impl StagingMemory
sourcepub fn new(
shmem: &SharedMemory,
offset_bytes: u64,
num_of_pages: usize
) -> Result<Self, Error>
pub fn new( shmem: &SharedMemory, offset_bytes: u64, num_of_pages: usize ) -> Result<Self, Error>
Creates StagingMemory.
§Arguments
shmem
- [SharedMemory] to mmap from.offset_bytes
- The offset in bytes from the head of theshmem
.num_of_pages
- The number of pages in the region.
sourcepub unsafe fn copy(
&mut self,
src_addr: *const u8,
idx: usize,
pages: usize
) -> Result<CopyOp, Error>
pub unsafe fn copy( &mut self, src_addr: *const u8, idx: usize, pages: usize ) -> Result<CopyOp, Error>
Copy the guest memory pages into the staging memory.
§Arguments
src_addr
- the head address of the pages on the guest memory.idx
- the index of the head of the pages.pages
- the number of pages to copy.
§Safety
src_addr
must be aligned with the page size.- The pages indicated by
src_addr
+pages
must be within the guest memory.
sourcepub fn page_content(
&self,
idx: usize
) -> Result<Option<VolatileSlice<'_>>, Error>
pub fn page_content( &self, idx: usize ) -> Result<Option<VolatileSlice<'_>>, Error>
Returns a content of the page corresponding to the index.
Returns Option::None if no content in the staging memory.
Returns Error::OutOfRange if the idx
is out of range.
§Arguments
idx
- the index of the page from the head of the pages.
sourcepub fn clear_range(&mut self, idx_range: Range<usize>) -> Result<(), Error>
pub fn clear_range(&mut self, idx_range: Range<usize>) -> Result<(), Error>
Clears the pages in the staging memory corresponding to the indices.
§Arguments
idx_range
- the indices of consecutive pages to be cleared.
sourcepub fn first_data_range(&mut self, max_pages: usize) -> Option<Range<usize>>
pub fn first_data_range(&mut self, max_pages: usize) -> Option<Range<usize>>
Returns the first range of indices of consecutive pages present in the staging memory.
§Arguments
max_pages
- the max size of the returned chunk even if the chunk of consecutive present pages is longer than this.
sourcepub fn get_slice(
&self,
idx_range: Range<usize>
) -> Result<VolatileSlice<'_>, Error>
pub fn get_slice( &self, idx_range: Range<usize> ) -> Result<VolatileSlice<'_>, Error>
Returns the [VolatileSlice] corresponding to the indices.
If the range is out of the region, this returns Error::OutOfRange.
§Arguments
idx_range
- the indices of the pages.
sourcepub fn present_pages(&self) -> usize
pub fn present_pages(&self) -> usize
Returns the count of present pages in the staging memory.