This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
WasmExecutor takes a cache directory #8057
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ use crate::state_holder; | |
|
||
use std::rc::Rc; | ||
use std::sync::Arc; | ||
use std::path::Path; | ||
use sc_executor_common::{ | ||
error::{Result, WasmError}, | ||
wasm_runtime::{WasmModule, WasmInstance, InvokeMethod}, | ||
|
@@ -119,20 +120,68 @@ impl WasmInstance for WasmtimeInstance { | |
} | ||
} | ||
|
||
/// Prepare a directory structure and a config file to enable wasmtime caching. | ||
/// | ||
/// In case of an error the caching will not be enabled. | ||
fn setup_wasmtime_caching( | ||
cache_path: &Path, | ||
config: &mut Config, | ||
) -> std::result::Result<(), String> { | ||
use std::fs; | ||
|
||
let wasmtime_cache_root = cache_path.join("wasmtime"); | ||
fs::create_dir_all(&wasmtime_cache_root) | ||
.map_err(|err| format!("cannot create the dirs to cache: {:?}", err))?; | ||
|
||
// Canonicalize the path after creating the directories. | ||
let wasmtime_cache_root = wasmtime_cache_root | ||
.canonicalize() | ||
.map_err(|err| format!("failed to canonicalize the path: {:?}", err))?; | ||
|
||
// Write the cache config file | ||
let cache_config_path = wasmtime_cache_root.join("cache-config.toml"); | ||
let config_content = format!( | ||
"\ | ||
[cache] | ||
enabled = true | ||
directory = \"{cache_dir}\" | ||
", | ||
cache_dir = wasmtime_cache_root.display() | ||
); | ||
fs::write(&cache_config_path, config_content) | ||
.map_err(|err| format!("cannot write the cache config: {:?}", err))?; | ||
|
||
config | ||
.cache_config_load(cache_config_path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In 0.19 it is not feature gated, in latest (0.21) it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I assume you meant the |
||
.map_err(|err| format!("failed to parse the config: {:?}", err))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Create a new `WasmtimeRuntime` given the code. This function performs translation from Wasm to | ||
/// machine code, which can be computationally heavy. | ||
/// | ||
/// The `cache_path` designates where this executor implementation can put compiled artifacts. | ||
pub fn create_runtime( | ||
code: &[u8], | ||
heap_pages: u64, | ||
host_functions: Vec<&'static dyn Function>, | ||
allow_missing_func_imports: bool, | ||
cache_path: Option<&Path>, | ||
) -> std::result::Result<WasmtimeRuntime, WasmError> { | ||
// Create the engine, store and finally the module from the given code. | ||
let mut config = Config::new(); | ||
config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); | ||
if let Some(cache_path) = cache_path { | ||
if let Err(reason) = setup_wasmtime_caching(cache_path, &mut config) { | ||
log::warn!( | ||
"failed to setup wasmtime cache. Performance may degrade significantly: {}.", | ||
reason, | ||
); | ||
} | ||
} | ||
|
||
let engine = Engine::new(&config); | ||
|
||
let module_wrapper = ModuleWrapper::new(&engine, code) | ||
.map_err(|e| WasmError::Other(format!("cannot create module: {}", e)))?; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean caching is disabled when running substrate node?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I deliberately didn't put it here.
The thing is that this change is dictated by needs of polkadot. I believe it should mitigate the stalls we are seeing there right now. Therefore, it is critical to land it quickly. I figured that we should move the question about the cache out of the way.