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,14 +4,12 @@ use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOption
use anyhow::{bail, Context as _, Result};
use std::{
ffi::{OsStr, OsString},
fmt::Write,
fs::File,
path::{Component, Path, PathBuf},
};
use structopt::{clap::AppSettings, StructOpt};
use wasi_common::preopen_dir;
use wasmtime::{Config, Engine, Instance, Module, Store};
use wasmtime_environ::cache_init;
use wasmtime_interface_types::ModuleData;
use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi};
@@ -91,28 +89,11 @@ pub struct RunCommand {
impl RunCommand {
/// Executes the command.
pub fn execute(&self) -> Result<()> {
let log_config = if self.common.debug {
if self.common.debug {
pretty_env_logger::init();
None
} else {
let prefix = "wasmtime.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);
}
let mut config = Config::new();
@@ -124,6 +105,7 @@ impl RunCommand {
self.common.cranelift,
self.common.lightbeam,
)?)?;
self.common.configure_cache(&mut config)?;
if self.common.optimize {
config.cranelift_opt_level(wasmtime::OptLevel::Speed);