Implement the pooling instance allocator.

This commit implements the pooling instance allocator.

The allocation strategy can be set with `Config::with_allocation_strategy`.

The pooling strategy uses the pooling instance allocator to preallocate a
contiguous region of memory for instantiating modules that adhere to various
limits.

The intention of the pooling instance allocator is to reserve as much of the
host address space needed for instantiating modules ahead of time and to reuse
committed memory pages wherever possible.
This commit is contained in:
Peter Huene
2020-12-08 16:00:48 -08:00
parent 16ca5e16d9
commit e71ccbf9bc
16 changed files with 2374 additions and 20 deletions

View File

@@ -0,0 +1,22 @@
use crate::Mmap;
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
}
pub unsafe fn decommit(addr: *mut u8, len: usize) {
region::protect(addr, len, region::Protection::NONE).unwrap();
// On Linux, this is enough to cause the kernel to initialize the pages to 0 on next access
assert_eq!(
libc::madvise(addr as _, len, libc::MADV_DONTNEED),
0,
"madvise failed to mark pages as missing: {}",
std::io::Error::last_os_error()
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
}

View File

@@ -0,0 +1,26 @@
use crate::Mmap;
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
}
pub unsafe fn decommit(addr: *mut u8, len: usize) {
assert_eq!(
libc::mmap(
addr as _,
len,
libc::PROT_NONE,
libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_FIXED,
-1,
0,
) as *mut u8,
addr,
"mmap failed to remap pages: {}",
std::io::Error::last_os_error()
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
}

View File

@@ -0,0 +1,21 @@
use crate::Mmap;
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
use winapi::um::winnt::{MEM_COMMIT, MEM_DECOMMIT, PAGE_READWRITE};
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
// This doesn't use the `region` crate because the memory needs to be committed
!VirtualAlloc(addr as _, len, MEM_COMMIT, PAGE_READWRITE).is_null()
}
pub unsafe fn decommit(addr: *mut u8, len: usize) {
assert!(
VirtualFree(addr as _, len, MEM_DECOMMIT) != 0,
"failed to decommit memory pages: {}",
std::io::Error::last_os_error()
);
}
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
Mmap::accessible_reserved(accessible_size, mapping_size)
.map_err(|e| format!("failed to allocate pool memory: {}", e))
}