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

@@ -1,9 +1,11 @@
use anyhow::Result;
use std::cell::RefCell;
use std::fmt;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use wasmtime_environ::settings::{self, Configurable};
use wasmtime_environ::CacheConfig;
use wasmtime_jit::{native, CompilationStrategy, Compiler, Features};
// Runtime Environment
@@ -21,6 +23,7 @@ pub struct Config {
pub(crate) features: Features,
pub(crate) debug_info: bool,
pub(crate) strategy: CompilationStrategy,
pub(crate) cache_config: CacheConfig,
}
impl Config {
@@ -45,6 +48,7 @@ impl Config {
features: Default::default(),
flags,
strategy: CompilationStrategy::Auto,
cache_config: CacheConfig::new_cache_disabled(),
}
}
@@ -221,6 +225,49 @@ impl Config {
.expect("should be valid flag");
self
}
/// Loads cache configuration specified at `path`.
///
/// This method will read the file specified by `path` on the filesystem and
/// attempt to load cache configuration from it. This method can also fail
/// due to I/O errors, misconfiguration, syntax errors, etc. For expected
/// syntax in the configuration file see the [documentation online][docs].
///
/// By default cache configuration is not enabled or loaded.
///
/// # Errors
///
/// This method can fail due to any error that happens when loading the file
/// pointed to by `path` and attempting to load the cache configuration.
///
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
pub fn cache_config_load(&mut self, path: impl AsRef<Path>) -> Result<&mut Self> {
self.cache_config = wasmtime_environ::CacheConfig::from_file(Some(path.as_ref()))?;
Ok(self)
}
/// Loads cache configuration from the system default path.
///
/// This commit is the same as [`Config::cache_config_load`] except that it
/// does not take a path argument and instead loads the default
/// configuration present on the system. This is located, for example, on
/// Unix at `$HOME/.config/wasmtime/config.toml` and is typically created
/// with the `wasmtime config new` command.
///
/// By default cache configuration is not enabled or loaded.
///
/// # Errors
///
/// This method can fail due to any error that happens when loading the
/// default system configuration. Note that it is not an error if the
/// default config file does not exist, in which case the default settings
/// for an enabled cache are applied.
///
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
pub fn cache_config_load_default(&mut self) -> Result<&mut Self> {
self.cache_config = wasmtime_environ::CacheConfig::from_file(None)?;
Ok(self)
}
}
impl Default for Config {
@@ -364,7 +411,11 @@ impl Store {
/// Creates a new store to be associated with the given [`Engine`].
pub fn new(engine: &Engine) -> Store {
let isa = native::builder().finish(settings::Flags::new(engine.config.flags.clone()));
let compiler = Compiler::new(isa, engine.config.strategy);
let compiler = Compiler::new(
isa,
engine.config.strategy,
engine.config.cache_config.clone(),
);
Store {
inner: Rc::new(StoreInner {
engine: engine.clone(),