Skip to content

Commit

Permalink
Implement the pooling instance allocator.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
peterhuene committed Feb 11, 2021
1 parent 1fe28b7 commit ff44af8
Show file tree
Hide file tree
Showing 16 changed files with 2,284 additions and 20 deletions.
66 changes: 65 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/environ/src/vmoffsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn align(offset: u32, width: u32) -> u32 {

/// This class computes offsets to fields within `VMContext` and other
/// related structs that JIT code accesses directly.
#[derive(Debug, Clone, Copy)]
pub struct VMOffsets {
/// The size in bytes of a pointer on the target.
pub pointer_size: u8,
Expand Down
4 changes: 4 additions & 0 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ cfg-if = "1.0"
backtrace = "0.3.55"
lazy_static = "1.3.0"
psm = "0.1.11"
rand = "0.7.3"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.7", features = ["winbase", "memoryapi", "errhandlingapi"] }

[target.'cfg(target_os = "linux")'.dependencies]
userfaultfd = { version = "0.3.0", optional = true }

[build-dependencies]
cc = "1.0"

Expand Down
15 changes: 14 additions & 1 deletion crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ use wasmtime_environ::{
ir, Module, ModuleTranslation, ModuleType, OwnedDataInitializer, TableElements, VMOffsets,
};

mod pooling;

pub use self::pooling::{
InstanceLimits, ModuleLimits, PoolingAllocationStrategy, PoolingInstanceAllocator,
};

/// Represents a request for a new runtime instance.
pub struct InstanceAllocationRequest<'a> {
/// The module being instantiated.
Expand Down Expand Up @@ -72,11 +78,18 @@ pub enum InstantiationError {
/// A trap ocurred during instantiation, after linking.
#[error("Trap occurred during instantiation")]
Trap(Trap),

/// A limit on how many instances are supported has been reached.
#[error("Limit of {0} concurrent instances has been reached")]
Limit(u32),
}

/// An error while creating a fiber stack.
#[derive(Error, Debug)]
pub enum FiberStackError {
/// Insufficient resources available for the request.
#[error("Insufficient resources: {0}")]
Resource(String),
/// An error for when the allocator doesn't support custom fiber stacks.
#[error("Custom fiber stacks are not supported by the allocator")]
NotSupported,
Expand Down Expand Up @@ -218,7 +231,7 @@ unsafe fn initialize_vmcontext(
globals.len(),
);

// Initialize the defined functions
// Initialize the functions
for (index, sig) in instance.module.functions.iter() {
let type_index = lookup_shared_signature(*sig);

Expand Down
Loading

0 comments on commit ff44af8

Please sign in to comment.