Add support for keeping pooling allocator pages resident (#5207)

When new wasm instances are created repeatedly in high-concurrency
environments one of the largest bottlenecks is the contention on
kernel-level locks having to do with the virtual memory. It's expected
that usage in this environment is leveraging the pooling instance
allocator with the `memory-init-cow` feature enabled which means that
the kernel level VM lock is acquired in operations such as:

1. Growing a heap with `mprotect` (write lock)
2. Faulting in memory during usage (read lock)
3. Resetting a heap's contents with `madvise` (read lock)
4. Shrinking a heap with `mprotect` when reusing a slot (write lock)

Rapid usage of these operations can lead to detrimental performance
especially on otherwise heavily loaded systems, worsening the more
frequent the above operations are. This commit is aimed at addressing
the (2) case above, reducing the number of page faults that are
fulfilled by the kernel.

Currently these page faults happen for three reasons:

* When memory is first accessed after the heap is grown.
* When the initial linear memory image is accessed for the first time.
* When the initial zero'd heap contents, not part of the linear memory
  image, are accessed.

This PR is attempting to address the latter of these cases, and to a
lesser extent the first case as well. Specifically this PR provides the
ability to partially reset a pooled linear memory with `memset` rather
than `madvise`. This is done to have the same effect of resetting
contents to zero but namely has a different effect on paging, notably
keeping the pages resident in memory rather than returning them to the
kernel. This means that reuse of a linear memory slot on a page that was
previously `memset` will not trigger a page fault since everything
remains paged into the process.

The end result is that any access to linear memory which has been
touched by `memset` will no longer page fault on reuse. On more recent
kernels (6.0+) this also means pages which were zero'd by `memset`, made
inaccessible with `PROT_NONE`, and then made accessible again with
`PROT_READ | PROT_WRITE` will not page fault. This can be common when a
wasm instances grows its heap slightly, uses that memory, but then it's
shrunk when the memory is reused for the next instance. Note that this
kernel optimization requires a 6.0+ kernel.

This same optimization is furthermore applied to both async stacks with
the pooling memory allocator in addition to table elements. The defaults
of Wasmtime are not changing with this PR, instead knobs are being
exposed for embedders to turn if they so desire. This is currently being
experimented with at Fastly and I may come back and alter the defaults
of Wasmtime if it seems suitable after our measurements.
This commit is contained in:
Alex Crichton
2022-11-04 15:56:34 -05:00
committed by GitHub
parent b14551d7ca
commit d3a6181939
5 changed files with 320 additions and 52 deletions

View File

@@ -1664,14 +1664,11 @@ pub enum WasmBacktraceDetails {
Environment,
}
/// Global configuration options used to create an [`Engine`](crate::Engine)
/// and customize its behavior.
/// Configuration options used with [`InstanceAllocationStrategy::Pooling`] to
/// change the behavior of the pooling instance allocator.
///
/// This structure exposed a builder-like interface and is primarily consumed by
/// [`Engine::new()`](crate::Engine::new).
///
/// The validation of `Config` is deferred until the engine is being built, thus
/// a problematic config may cause `Engine::new` to fail.
/// This structure has a builder-style API in the same manner as [`Config`] and
/// is configured with [`Config::allocation_strategy`].
#[cfg(feature = "pooling-allocator")]
#[derive(Debug, Clone, Default)]
pub struct PoolingAllocationConfig {
@@ -1703,11 +1700,8 @@ impl PoolingAllocationConfig {
/// Wasmtime and the [`call_async`] variant
/// of calling WebAssembly is used then Wasmtime will create a separate
/// runtime execution stack for each future produced by [`call_async`].
/// When using the pooling instance allocator
/// ([`InstanceAllocationStrategy::Pooling`]) this allocation will happen
/// from a pool of stacks and additionally deallocation will simply release
/// the stack back to the pool. During the deallocation process Wasmtime
/// won't by default reset the contents of the stack back to zero.
/// During the deallocation process Wasmtime won't by default reset the
/// contents of the stack back to zero.
///
/// When this option is enabled it can be seen as a defense-in-depth
/// mechanism to reset a stack back to zero. This is not required for
@@ -1725,6 +1719,57 @@ impl PoolingAllocationConfig {
self
}
/// How much memory, in bytes, to keep resident for async stacks allocated
/// with the pooling allocator.
///
/// When [`PoolingAllocationConfig::async_stack_zeroing`] is enabled then
/// Wasmtime will reset the contents of async stacks back to zero upon
/// deallocation. This option can be used to perform the zeroing operation
/// with `memset` up to a certain threshold of bytes instead of using system
/// calls to reset the stack to zero.
///
/// Note that when using this option the memory with async stacks will
/// never be decommitted.
#[cfg(feature = "async")]
#[cfg_attr(nightlydoc, doc(cfg(feature = "async")))]
pub fn async_stack_keep_resident(&mut self, size: usize) -> &mut Self {
let size = round_up_to_pages(size as u64) as usize;
self.config.async_stack_keep_resident = size;
self
}
/// How much memory, in bytes, to keep resident for each linear memory
/// after deallocation.
///
/// This option is only applicable on Linux and has no effect on other
/// platforms.
///
/// By default Wasmtime will use `madvise` to reset the entire contents of
/// linear memory back to zero when a linear memory is deallocated. This
/// option can be used to use `memset` instead to set memory back to zero
/// which can, in some configurations, reduce the number of page faults
/// taken when a slot is reused.
pub fn linear_memory_keep_resident(&mut self, size: usize) -> &mut Self {
let size = round_up_to_pages(size as u64) as usize;
self.config.linear_memory_keep_resident = size;
self
}
/// How much memory, in bytes, to keep resident for each table after
/// deallocation.
///
/// This option is only applicable on Linux and has no effect on other
/// platforms.
///
/// This option is the same as
/// [`PoolingAllocationConfig::linear_memory_keep_resident`] except that it
/// is applicable to tables instead.
pub fn table_keep_resident(&mut self, size: usize) -> &mut Self {
let size = round_up_to_pages(size as u64) as usize;
self.config.table_keep_resident = size;
self
}
/// The maximum number of concurrent instances supported (default is 1000).
///
/// This value has a direct impact on the amount of memory allocated by the pooling