Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several enhancements to GuestMemory and VolatileSlice #101

Merged
merged 5 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
### Added
- [[#109]](https://github.com/rust-vmm/vm-memory/pull/109): Added `build_raw` to
`MmapRegion` which can be used to operate on externally created mappings.
- [[#101]](https://github.com/rust-vmm/vm-memory/pull/101): Added `check_range` for
GuestMemory which could be used to validate a range of guest memory.

## [v0.2.0]

Expand Down
28 changes: 28 additions & 0 deletions benches/guest_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2020 Alibaba Cloud Computing. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
#![cfg(feature = "backend-mmap")]

pub use criterion::{black_box, Criterion};
use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};

const REGION_SIZE: u64 = 0x10_0000;
const REGIONS_COUNT: u64 = 256;

pub fn benchmark_for_guest_memory(c: &mut Criterion) {
benchmark_find_region(c);
}

fn find_region(mem: &GuestMemoryMmap) {
for i in 0..REGIONS_COUNT {
let _ = mem.find_region(GuestAddress(i * REGION_SIZE)).unwrap();
}
}

fn benchmark_find_region(c: &mut Criterion) {
let memory = super::create_guest_memory_mmap(REGION_SIZE, REGIONS_COUNT);

c.bench_function("find_region", |b| {
b.iter(|| black_box(find_region(&memory)))
});
}
24 changes: 21 additions & 3 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,39 @@
extern crate criterion;

pub use criterion::{black_box, criterion_group, criterion_main, Criterion};
#[cfg(feature = "backend-mmap")]
use vm_memory::{GuestAddress, GuestMemoryMmap};

mod guest_memory;
mod mmap;
mod volatile;

use volatile::benchmark_for_volatile;

#[cfg(feature = "backend-mmap")]
use mmap::benchmark_for_mmap;
pub fn create_guest_memory_mmap(size: u64, count: u64) -> GuestMemoryMmap {
let mut regions: Vec<(GuestAddress, usize)> = Vec::new();
for i in 0..count {
regions.push((GuestAddress(i * size), size as usize));
}

GuestMemoryMmap::from_ranges(regions.as_slice()).unwrap()
}

pub fn criterion_benchmark(_c: &mut Criterion) {
#[cfg(feature = "backend-mmap")]
benchmark_for_mmap(_c);
mmap::benchmark_for_mmap(_c);
}

pub fn benchmark_guest_memory(_c: &mut Criterion) {
#[cfg(feature = "backend-mmap")]
guest_memory::benchmark_for_guest_memory(_c)
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(200).measurement_time(std::time::Duration::from_secs(50));
targets = criterion_benchmark
targets = criterion_benchmark, benchmark_guest_memory, benchmark_for_volatile
}

criterion_main! {
Expand Down
8 changes: 2 additions & 6 deletions benches/mmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::path::Path;

use criterion::{black_box, Criterion};

use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory};

const REGION_SIZE: u64 = 0x8000_0000;
const REGIONS_COUNT: u64 = 8;
Expand Down Expand Up @@ -62,12 +62,8 @@ impl AccessKind {
}

pub fn benchmark_for_mmap(c: &mut Criterion) {
let mut regions: Vec<(GuestAddress, usize)> = Vec::new();
for i in 0..REGIONS_COUNT {
regions.push((GuestAddress(i * REGION_SIZE), REGION_SIZE as usize));
}
let memory = super::create_guest_memory_mmap(REGION_SIZE, REGIONS_COUNT);

let memory = GuestMemoryMmap::from_ranges(regions.as_slice()).unwrap();
// Just a sanity check.
assert_eq!(
memory.last_addr(),
Expand Down
45 changes: 45 additions & 0 deletions benches/volatile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) 2020 Alibaba Cloud. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

pub use criterion::{black_box, Criterion};
use vm_memory::volatile_memory::VolatileMemory;

pub fn benchmark_for_volatile(c: &mut Criterion) {
let mut a = [0xa5u8; 1024];
let a_ref = &mut a[..];
let v_ref8 = a_ref.get_slice(0, a_ref.len()).unwrap();
let v_ref16 = a_ref.get_slice(0, a_ref.len() / 2).unwrap();
let mut d8 = [0u8; 1024];
let mut d16 = [0u16; 512];

// Check performance for read operations.
c.bench_function("VolatileSlice::copy_to_u8", |b| {
b.iter(|| black_box(v_ref8.copy_to(&mut d8[..])))
});
c.bench_function("VolatileSlice::copy_to_u16", |b| {
b.iter(|| black_box(v_ref16.copy_to(&mut d16[..])))
});
benchmark_volatile_copy_to_volatile_slice(c);

// Check performance for write operations.
c.bench_function("VolatileSlice::copy_from_u8", |b| {
b.iter(|| black_box(v_ref8.copy_from(&d8[..])))
});
c.bench_function("VolatileSlice::copy_from_u16", |b| {
b.iter(|| black_box(v_ref16.copy_from(&d16[..])))
});
}

fn benchmark_volatile_copy_to_volatile_slice(c: &mut Criterion) {
let mut a = [0xa5u8; 10240];
let a_ref = &mut a[..];
let a_slice = a_ref.get_slice(0, a_ref.len()).unwrap();
let mut d = [0u8; 10240];
let d_ref = &mut d[..];
let d_slice = d_ref.get_slice(0, d_ref.len()).unwrap();

c.bench_function("VolatileSlice::copy_to_volatile_slice", |b| {
b.iter(|| black_box(a_slice.copy_to_volatile_slice(d_slice)))
});
}
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 85.1,
"coverage_score": 84.8,
"exclude_path": "mmap_windows.rs",
"crate_features": "backend-mmap,backend-atomic"
}
8 changes: 8 additions & 0 deletions src/guest_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,14 @@ pub trait GuestMemory {
self.find_region(addr).map(|_| addr)
}

/// Check whether the range [base, base + len) is valid.
fn check_range(&self, base: GuestAddress, len: usize) -> bool {
match self.try_access(len, base, |_, count, _, _| -> Result<usize> { Ok(count) }) {
Ok(count) if count == len => true,
_ => false,
}
}

/// Returns the address plus the offset if it is present within the memory of the guest.
fn checked_offset(&self, base: GuestAddress, offset: usize) -> Option<GuestAddress> {
base.checked_add(offset as u64)
Expand Down
50 changes: 50 additions & 0 deletions src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,54 @@ mod tests {
assert!(guest_mem.get_slice(GuestAddress(0x600), 0x100).is_err());
assert!(guest_mem.get_slice(GuestAddress(0xc00), 0x100).is_err());
}

#[test]
fn test_checked_offset() {
let start_addr1 = GuestAddress(0);
let start_addr2 = GuestAddress(0x800);
let start_addr3 = GuestAddress(0xc00);
let guest_mem = GuestMemoryMmap::from_ranges(&[
(start_addr1, 0x400),
(start_addr2, 0x400),
(start_addr3, 0x400),
])
.unwrap();

assert_eq!(
guest_mem.checked_offset(start_addr1, 0x200),
Some(GuestAddress(0x200))
);
assert_eq!(
guest_mem.checked_offset(start_addr1, 0xa00),
Some(GuestAddress(0xa00))
);
assert_eq!(
guest_mem.checked_offset(start_addr2, 0x7ff),
Some(GuestAddress(0xfff))
);
assert_eq!(guest_mem.checked_offset(start_addr2, 0xc00), None);
assert_eq!(guest_mem.checked_offset(start_addr1, std::usize::MAX), None);
}

#[test]
fn test_check_range() {
let start_addr1 = GuestAddress(0);
let start_addr2 = GuestAddress(0x800);
let start_addr3 = GuestAddress(0xc00);
let guest_mem = GuestMemoryMmap::from_ranges(&[
(start_addr1, 0x400),
(start_addr2, 0x400),
(start_addr3, 0x400),
])
.unwrap();

assert_eq!(guest_mem.check_range(start_addr1, 0x0), true);
assert_eq!(guest_mem.check_range(start_addr1, 0x200), true);
assert_eq!(guest_mem.check_range(start_addr1, 0xa00), false);
assert_eq!(guest_mem.check_range(start_addr2, 0x7ff), true);
assert_eq!(guest_mem.check_range(start_addr2, 0x800), true);
assert_eq!(guest_mem.check_range(start_addr2, 0x801), false);
assert_eq!(guest_mem.check_range(start_addr2, 0xc00), false);
assert_eq!(guest_mem.check_range(start_addr1, std::usize::MAX), false);
}
}
2 changes: 0 additions & 2 deletions src/mmap_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use std::os::unix::io::AsRawFd;
use std::ptr::null_mut;
use std::result;

use libc;

use crate::guest_memory::FileOffset;
use crate::mmap::{check_file_offset, AsSlice};
use crate::volatile_memory::{self, compute_offset, VolatileMemory, VolatileSlice};
Expand Down
Loading