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

@@ -18,9 +18,8 @@ use std::path::{Path, PathBuf};
mod config;
mod worker;
use config::{cache_config, CacheConfig};
pub use config::{create_new_config, init};
use worker::{worker, Worker};
pub use config::{create_new_config, CacheConfig};
use worker::Worker;
lazy_static! {
static ref SELF_MTIME: String = {
@@ -48,12 +47,11 @@ lazy_static! {
};
}
pub struct ModuleCacheEntry<'config, 'worker>(Option<ModuleCacheEntryInner<'config, 'worker>>);
pub struct ModuleCacheEntry<'config>(Option<ModuleCacheEntryInner<'config>>);
struct ModuleCacheEntryInner<'config, 'worker> {
struct ModuleCacheEntryInner<'config> {
mod_cache_path: PathBuf,
cache_config: &'config CacheConfig,
worker: &'worker Worker,
}
/// Cached compilation data of a Wasm module.
@@ -79,15 +77,15 @@ pub type ModuleCacheDataTupleType = (
struct Sha256Hasher(Sha256);
impl<'config, 'worker> ModuleCacheEntry<'config, 'worker> {
impl<'config> ModuleCacheEntry<'config> {
pub fn new<'data>(
module: &Module,
function_body_inputs: &PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
isa: &dyn isa::TargetIsa,
compiler_name: &str,
generate_debug_info: bool,
cache_config: &'config CacheConfig,
) -> Self {
let cache_config = cache_config();
if cache_config.enabled() {
Self(Some(ModuleCacheEntryInner::new(
module,
@@ -96,7 +94,6 @@ impl<'config, 'worker> ModuleCacheEntry<'config, 'worker> {
compiler_name,
generate_debug_info,
cache_config,
worker(),
)))
} else {
Self(None)
@@ -104,14 +101,17 @@ impl<'config, 'worker> ModuleCacheEntry<'config, 'worker> {
}
#[cfg(test)]
fn from_inner(inner: ModuleCacheEntryInner<'config, 'worker>) -> Self {
fn from_inner(inner: ModuleCacheEntryInner<'config>) -> Self {
Self(Some(inner))
}
pub fn get_data(&self) -> Option<ModuleCacheData> {
if let Some(inner) = &self.0 {
inner.get_data().map(|val| {
inner.worker.on_cache_get_async(&inner.mod_cache_path); // call on success
inner
.cache_config
.worker()
.on_cache_get_async(&inner.mod_cache_path); // call on success
val
})
} else {
@@ -122,13 +122,16 @@ impl<'config, 'worker> ModuleCacheEntry<'config, 'worker> {
pub fn update_data(&self, data: &ModuleCacheData) {
if let Some(inner) = &self.0 {
if inner.update_data(data).is_some() {
inner.worker.on_cache_update_async(&inner.mod_cache_path); // call on success
inner
.cache_config
.worker()
.on_cache_update_async(&inner.mod_cache_path); // call on success
}
}
}
}
impl<'config, 'worker> ModuleCacheEntryInner<'config, 'worker> {
impl<'config> ModuleCacheEntryInner<'config> {
fn new<'data>(
module: &Module,
function_body_inputs: &PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
@@ -136,7 +139,6 @@ impl<'config, 'worker> ModuleCacheEntryInner<'config, 'worker> {
compiler_name: &str,
generate_debug_info: bool,
cache_config: &'config CacheConfig,
worker: &'worker Worker,
) -> Self {
let hash = Sha256Hasher::digest(module, function_body_inputs);
let compiler_dir = if cfg!(debug_assertions) {
@@ -167,7 +169,6 @@ impl<'config, 'worker> ModuleCacheEntryInner<'config, 'worker> {
Self {
mod_cache_path,
cache_config,
worker,
}
}