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

@@ -4,7 +4,6 @@ use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOption
use anyhow::{anyhow, bail, Context as _, Result};
use faerie::Artifact;
use std::{
fmt::Write,
fs::File,
path::{Path, PathBuf},
str::FromStr,
@@ -16,9 +15,9 @@ use wasmtime_debug::{emit_debugsections, read_debuginfo};
#[cfg(feature = "lightbeam")]
use wasmtime_environ::Lightbeam;
use wasmtime_environ::{
cache_init, entity::EntityRef, settings, settings::Configurable, wasm::DefinedMemoryIndex,
wasm::MemoryIndex, Compiler, Cranelift, ModuleEnvironment, ModuleMemoryOffset, ModuleVmctxInfo,
Tunables, VMOffsets,
entity::EntityRef, settings, settings::Configurable, wasm::DefinedMemoryIndex,
wasm::MemoryIndex, CacheConfig, Compiler, Cranelift, ModuleEnvironment, ModuleMemoryOffset,
ModuleVmctxInfo, Tunables, VMOffsets,
};
use wasmtime_jit::native;
use wasmtime_obj::emit_module;
@@ -59,34 +58,22 @@ pub struct WasmToObjCommand {
impl WasmToObjCommand {
/// Executes the command.
pub fn execute(&self) -> Result<()> {
let log_config = if self.common.debug {
pretty_env_logger::init();
None
} else {
let prefix = "wasm2obj.dbg.";
init_file_per_thread_logger(prefix);
Some(prefix)
};
let errors = cache_init(
!self.common.disable_cache,
self.common.config.as_ref(),
log_config,
);
if !errors.is_empty() {
let mut message = String::new();
writeln!(message, "Cache initialization failed. Errors:")?;
for e in errors {
writeln!(message, " -> {}", e)?;
}
bail!(message);
}
self.handle_module()
}
fn handle_module(&self) -> Result<()> {
if self.common.debug {
pretty_env_logger::init();
} else {
let prefix = "wasm2obj.dbg.";
init_file_per_thread_logger(prefix);
}
let cache_config = if self.common.disable_cache {
CacheConfig::new_cache_disabled()
} else {
CacheConfig::from_file(self.common.config.as_deref())?
};
let strategy = pick_compilation_strategy(self.common.cranelift, self.common.lightbeam)?;
let data = wat::parse_file(&self.module).context("failed to parse module")?;
@@ -147,6 +134,7 @@ impl WasmToObjCommand {
lazy_function_body_inputs,
&*isa,
self.common.debug_info,
&cache_config,
),
#[cfg(feature = "lightbeam")]
Strategy::Lightbeam => Lightbeam::compile_module(
@@ -155,6 +143,7 @@ impl WasmToObjCommand {
lazy_function_body_inputs,
&*isa,
self.common.debug_info,
&cache_config,
),
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => bail!("lightbeam support not enabled"),