Skip to content

Commit

Permalink
feat: add a knob for reset stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia committed Aug 30, 2022
1 parent 573ae0c commit 0020cac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions crates/runtime/src/instance/allocator/pooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,11 +887,16 @@ struct StackPool {
max_instances: usize,
page_size: usize,
index_allocator: Mutex<PoolingAllocationState>,
reset_stack_page: bool,
}

#[cfg(all(feature = "async", unix))]
impl StackPool {
fn new(instance_limits: &InstanceLimits, stack_size: usize) -> Result<Self> {
fn new(
instance_limits: &InstanceLimits,
stack_size: usize,
reset_stack_page: bool,
) -> Result<Self> {
use rustix::mm::{mprotect, MprotectFlags};

let page_size = crate::page_size();
Expand Down Expand Up @@ -931,6 +936,7 @@ impl StackPool {
stack_size,
max_instances,
page_size,
reset_stack_page,
// We always use a `NextAvailable` strategy for stack
// allocation. We don't want or need an affinity policy
// here: stacks do not benefit from being allocated to the
Expand Down Expand Up @@ -997,8 +1003,9 @@ impl StackPool {
let index = (start_of_stack - base) / self.stack_size;
assert!(index < self.max_instances);

decommit_stack_pages(bottom_of_stack as _, stack_size).unwrap();

if self.reset_stack_page {
decommit_stack_pages(bottom_of_stack as _, stack_size).unwrap();
}
self.index_allocator.lock().unwrap().free(SlotId(index));
}
}
Expand All @@ -1024,6 +1031,7 @@ impl PoolingInstanceAllocator {
instance_limits: InstanceLimits,
stack_size: usize,
tunables: &Tunables,
reset_stack_page: bool,
) -> Result<Self> {
if instance_limits.count == 0 {
bail!("the instance count limit cannot be zero");
Expand All @@ -1036,7 +1044,7 @@ impl PoolingInstanceAllocator {
Ok(Self {
instances: instances,
#[cfg(all(feature = "async", unix))]
stacks: StackPool::new(&instance_limits, stack_size)?,
stacks: StackPool::new(&instance_limits, stack_size, reset_stack_page)?,
#[cfg(all(feature = "async", windows))]
stack_size,
})
Expand Down
5 changes: 5 additions & 0 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub struct Config {
pub(crate) memory_init_cow: bool,
pub(crate) memory_guaranteed_dense_image_size: u64,
pub(crate) force_memory_init_memfd: bool,
#[cfg(feature = "async")]
pub(crate) reset_stack_page: bool,
}

/// User-provided configuration for the compiler.
Expand Down Expand Up @@ -196,6 +198,8 @@ impl Config {
memory_init_cow: true,
memory_guaranteed_dense_image_size: 16 << 20,
force_memory_init_memfd: false,
#[cfg(feature = "async")]
reset_stack_page: true,
};
#[cfg(compiler)]
{
Expand Down Expand Up @@ -1432,6 +1436,7 @@ impl Config {
instance_limits,
stack_size,
&self.tunables,
self.reset_stack_page,
)?)),
}
}
Expand Down

0 comments on commit 0020cac

Please sign in to comment.