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

@@ -5,16 +5,15 @@
//! but we guarantee eventual consistency and fault tolerancy.
//! Background tasks can be CPU intensive, but the worker thread has low priority.
use super::{cache_config, fs_write_atomic, CacheConfig};
use super::{fs_write_atomic, CacheConfig};
use log::{debug, info, trace, warn};
use serde::{Deserialize, Serialize};
use spin::Once;
use std::cmp;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{self, AtomicBool};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
#[cfg(test)]
use std::sync::{Arc, Condvar, Mutex};
@@ -25,6 +24,7 @@ use std::time::SystemTime;
#[cfg(test)]
use tests::system_time_stub::SystemTimeStub as SystemTime;
#[derive(Clone)]
pub(super) struct Worker {
sender: SyncSender<CacheEvent>,
#[cfg(test)]
@@ -46,29 +46,6 @@ struct WorkerStats {
handled: u32,
}
static WORKER: Once<Worker> = Once::new();
static INIT_CALLED: AtomicBool = AtomicBool::new(false);
pub(super) fn worker() -> &'static Worker {
WORKER
.r#try()
.expect("Cache worker must be initialized before usage")
}
pub(super) fn init(init_file_per_thread_logger: Option<&'static str>) {
INIT_CALLED
.compare_exchange(
false,
true,
atomic::Ordering::SeqCst,
atomic::Ordering::SeqCst,
)
.expect("Cache worker init must be called at most once");
let worker = Worker::start_new(cache_config(), init_file_per_thread_logger);
WORKER.call_once(|| worker);
}
#[derive(Debug, Clone)]
enum CacheEvent {
OnCacheGet(PathBuf),
@@ -168,6 +145,12 @@ impl Worker {
}
}
impl fmt::Debug for Worker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Worker").finish()
}
}
#[derive(Serialize, Deserialize)]
struct ModuleCacheStatistics {
pub usages: u64,
@@ -210,9 +193,6 @@ macro_rules! unwrap_or_warn {
impl WorkerThread {
fn run(self, init_file_per_thread_logger: Option<&'static str>) {
#[cfg(not(test))] // We want to test the worker without relying on init() being called
assert!(INIT_CALLED.load(atomic::Ordering::SeqCst));
if let Some(prefix) = init_file_per_thread_logger {
file_per_thread_logger::initialize(prefix);
}