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:
1666
crates/runtime/src/instance/allocator/pooling.rs
Normal file
1666
crates/runtime/src/instance/allocator/pooling.rs
Normal file
File diff suppressed because it is too large
Load Diff
22
crates/runtime/src/instance/allocator/pooling/linux.rs
Normal file
22
crates/runtime/src/instance/allocator/pooling/linux.rs
Normal 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))
|
||||
}
|
||||
26
crates/runtime/src/instance/allocator/pooling/unix.rs
Normal file
26
crates/runtime/src/instance/allocator/pooling/unix.rs
Normal 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))
|
||||
}
|
||||
21
crates/runtime/src/instance/allocator/pooling/windows.rs
Normal file
21
crates/runtime/src/instance/allocator/pooling/windows.rs
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user