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 and crosvm 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.

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

source

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 the shmem.
  • num_of_pages - The number of pages in the region.
source

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.
source

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.
source

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.
source

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.
source

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.
source

pub fn present_pages(&self) -> usize

Returns the count of present pages in the staging memory.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.