Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a module cache abstraction #3841

Merged
merged 18 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/cli/src/commands/run_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,24 @@ impl TargetOnDisk {
let leading_bytes = &buffer[..bytes_read];

if wasmer::is_wasm(leading_bytes) {
Ok(TargetOnDisk::WebAssemblyBinary(path))
} else if webc::detect(leading_bytes).is_ok() {
Ok(TargetOnDisk::Webc(path))
} else if path.extension() == Some("wat".as_ref()) {
Ok(TargetOnDisk::Wat(path))
} else {
#[cfg(feature = "compiler")]
if ArtifactBuild::is_deserializable(leading_bytes) {
return Ok(TargetOnDisk::Artifact(path));
}
anyhow::bail!("Unable to determine how to execute \"{}\"", path.display());
return Ok(TargetOnDisk::WebAssemblyBinary(path));
}

if webc::detect(leading_bytes).is_ok() {
return Ok(TargetOnDisk::Webc(path));
}

#[cfg(feature = "compiler")]
if ArtifactBuild::is_deserializable(leading_bytes) {
return Ok(TargetOnDisk::Artifact(path));
}

// If we can't figure out the file type based on its content, fall back
// to checking the extension.

match path.extension().and_then(|s| s.to_str()) {
Some("wat") => Ok(TargetOnDisk::Wat(path)),
_ => anyhow::bail!("Unable to determine how to execute \"{}\"", path.display()),
}
}

theduke marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
32 changes: 19 additions & 13 deletions lib/wasi/src/bin_factory/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ use std::{pin::Pin, sync::Arc};

use crate::{
os::task::{thread::WasiThreadRunGuard, TaskJoinHandle},
runtime::module_cache::ModuleCache,
VirtualBusError, WasiRuntimeError,
};
use futures::Future;
use tracing::*;
use wasmer::{FunctionEnvMut, Instance, Memory, Module, Store};
use wasmer_wasix_types::wasi::Errno;

use super::{BinFactory, BinaryPackage, ModuleCache};
use super::{BinFactory, BinaryPackage};
use crate::{
import_object_for_all_wasi_versions, runtime::SpawnType, SpawnedMemory, WasiEnv,
WasiFunctionEnv, WasiRuntime,
};

pub fn spawn_exec(
#[tracing::instrument(level = "trace", skip_all, fields(%name, %binary.package_name))]
pub async fn spawn_exec(
binary: BinaryPackage,
name: &str,
store: Store,
env: WasiEnv,
runtime: &Arc<dyn WasiRuntime + Send + Sync + 'static>,
compiled_modules: &ModuleCache,
) -> Result<TaskJoinHandle, VirtualBusError> {
// The deterministic id for this engine
let compiler = store.engine().deterministic_id();

let module = compiled_modules.get_compiled_module(&store, binary.hash().as_str(), compiler);
let key = format!("{}-{}", binary.hash().as_str(), compiler);

let compiled_modules = runtime.module_cache();
let module = compiled_modules.load(&key, store.engine()).await.ok();

let module = match (module, binary.entry.as_ref()) {
(Some(a), _) => a,
(None, Some(entry)) => {
Expand All @@ -43,7 +48,15 @@ pub fn spawn_exec(
env.blocking_cleanup(Some(Errno::Noexec.into()));
}
let module = module?;
compiled_modules.set_compiled_module(binary.hash().as_str(), compiler, &module);

if let Err(e) = compiled_modules.save(&key, &module).await {
tracing::debug!(
%key,
package_name=%binary.package_name,
error=&e as &dyn std::error::Error,
"Unable to save the compiled module",
);
}
module
}
(None, None) => {
Expand Down Expand Up @@ -205,14 +218,7 @@ impl BinFactory {
let binary = binary?;

// Execute
spawn_exec(
binary,
name.as_str(),
store,
env,
&self.runtime,
&self.cache,
)
spawn_exec(binary, name.as_str(), store, env, &self.runtime).await
})
}

Expand Down
11 changes: 2 additions & 9 deletions lib/wasi/src/bin_factory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,27 @@ use virtual_fs::{AsyncReadExt, FileSystem};

mod binary_package;
mod exec;
mod module_cache;

use sha2::*;

pub use self::{
binary_package::*,
exec::{spawn_exec, spawn_exec_module},
module_cache::ModuleCache,
};
use crate::{os::command::Commands, WasiRuntime};

#[derive(Debug, Clone)]
pub struct BinFactory {
pub(crate) commands: Commands,
runtime: Arc<dyn WasiRuntime + Send + Sync + 'static>,
pub(crate) cache: Arc<ModuleCache>,
pub(crate) local: Arc<RwLock<HashMap<String, Option<BinaryPackage>>>>,
}

impl BinFactory {
pub fn new(
compiled_modules: Arc<ModuleCache>,
runtime: Arc<dyn WasiRuntime + Send + Sync + 'static>,
) -> BinFactory {
pub fn new(runtime: Arc<dyn WasiRuntime + Send + Sync + 'static>) -> BinFactory {
BinFactory {
commands: Commands::new_with_builtins(runtime.clone(), compiled_modules.clone()),
commands: Commands::new_with_builtins(runtime.clone()),
runtime,
cache: compiled_modules,
local: Arc::new(RwLock::new(HashMap::new())),
}
}
Expand Down
208 changes: 0 additions & 208 deletions lib/wasi/src/bin_factory/module_cache.rs

This file was deleted.

Loading