Remove all global state from the caching system (#863)

* Remove all global state from the caching system

This commit is a continuation of an effort to remove usages of
`lazy_static!` and similar global state macros which can otherwise be
accomodated with passing objects around. Previously there was a global
cache system initialized per-process, but it was initialized in a bit of
a roundabout way and wasn't actually reachable from the `wasmtime` crate
itself. The changes here remove all global state, refactor many of the
internals in the cache system, and makes configuration possible through
the `wasmtime` crate.

Specifically some changes here are:

* Usage of `lazy_static!` and many `static` items in the cache module
  have all been removed.
* Global `cache_config()`, `worker()`, and `init()` functions have all
  been removed. Instead a `CacheConfig` is a "root object" which
  internally owns its worker and passing around the `CacheConfig` is
  required for cache usage.
* The `wasmtime::Config` structure has grown options to load and parse
  cache files at runtime. Currently only loading files is supported,
  although we can likely eventually support programmatically configuring
  APIs as well.
* Usage of the `spin` crate has been removed and the dependency is removed.
* The internal `errors` field of `CacheConfig` is removed, instead
  changing all relevant methods to return a `Result<()>` instead of
  storing errors internally.
* Tests have all been updated with the new interfaces and APIs.

Functionally no real change is intended here. Usage of the `wasmtime`
CLI, for example, should still enable the cache by default.

* Fix lightbeam compilation
This commit is contained in:
Alex Crichton
2020-02-06 13:11:06 -06:00
committed by GitHub
parent 4ff8257b17
commit 70345aff31
24 changed files with 283 additions and 479 deletions

View File

@@ -16,9 +16,9 @@ use wasmtime_environ::entity::{EntityRef, PrimaryMap};
use wasmtime_environ::isa::{TargetFrontendConfig, TargetIsa};
use wasmtime_environ::wasm::{DefinedFuncIndex, DefinedMemoryIndex, MemoryIndex};
use wasmtime_environ::{
Compilation, CompileError, CompiledFunction, CompiledFunctionUnwindInfo, Compiler as _C,
FunctionBodyData, Module, ModuleMemoryOffset, ModuleVmctxInfo, Relocations, Traps, Tunables,
VMOffsets,
CacheConfig, Compilation, CompileError, CompiledFunction, CompiledFunctionUnwindInfo,
Compiler as _C, FunctionBodyData, Module, ModuleMemoryOffset, ModuleVmctxInfo, Relocations,
Traps, Tunables, VMOffsets,
};
use wasmtime_runtime::{
jit_function_registry, InstantiationError, SignatureRegistry, TrapRegistration, TrapRegistry,
@@ -56,6 +56,7 @@ pub struct Compiler {
trampoline_park: HashMap<*const VMFunctionBody, *const VMFunctionBody>,
signatures: SignatureRegistry,
strategy: CompilationStrategy,
cache_config: CacheConfig,
/// The `FunctionBuilderContext`, shared between trampline function compilations.
fn_builder_ctx: FunctionBuilderContext,
@@ -63,7 +64,11 @@ pub struct Compiler {
impl Compiler {
/// Construct a new `Compiler`.
pub fn new(isa: Box<dyn TargetIsa>, strategy: CompilationStrategy) -> Self {
pub fn new(
isa: Box<dyn TargetIsa>,
strategy: CompilationStrategy,
cache_config: CacheConfig,
) -> Self {
Self {
isa,
code_memory: CodeMemory::new(),
@@ -73,6 +78,7 @@ impl Compiler {
fn_builder_ctx: FunctionBuilderContext::new(),
strategy,
trap_registry: TrapRegistry::default(),
cache_config,
}
}
}
@@ -124,6 +130,7 @@ impl Compiler {
function_body_inputs,
&*self.isa,
debug_data.is_some(),
&self.cache_config,
)
}
#[cfg(feature = "lightbeam")]
@@ -134,6 +141,7 @@ impl Compiler {
function_body_inputs,
&*self.isa,
debug_data.is_some(),
&self.cache_config,
)
}
}

View File

@@ -1,11 +1,10 @@
use crate::action::{get, inspect_memory, invoke};
use crate::{
instantiate, ActionError, ActionOutcome, CompilationStrategy, CompiledModule, Compiler,
InstanceHandle, Namespace, RuntimeValue, SetupError,
instantiate, ActionError, ActionOutcome, CompiledModule, Compiler, InstanceHandle, Namespace,
RuntimeValue, SetupError,
};
use thiserror::Error;
use wasmparser::{validate, OperatorValidatorConfig, ValidatingParserConfig};
use wasmtime_environ::isa::TargetIsa;
/// Indicates an unknown instance was specified.
#[derive(Error, Debug)]
@@ -83,11 +82,6 @@ impl Context {
self.debug_info = value;
}
/// Construct a new instance of `Context` with the given target.
pub fn with_isa(isa: Box<dyn TargetIsa>, strategy: CompilationStrategy) -> Self {
Self::new(Box::new(Compiler::new(isa, strategy)))
}
/// Retrieve the context features
pub fn features(&self) -> &Features {
&self.features