Skip to content

Commit

Permalink
Make wasm runtime cache size configurable (paritytech#10177)
Browse files Browse the repository at this point in the history
* Make wasm runtime cache size configurable

* apply review comments

* remove VersionedRuntimeValue

* fix compilation

* VersionedRuntime: replace clone by Arc

* fmt

* fix warnings

* fix tests compilation

* fmt
  • Loading branch information
librelois authored Dec 9, 2021
1 parent 6a634fb commit 1aea26e
Show file tree
Hide file tree
Showing 30 changed files with 142 additions and 82 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub fn new_partial(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);

let (client, backend, keystore_container, task_manager) =
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
tracing_targets: None,
tracing_receiver: Default::default(),
max_runtime_instances: 8,
runtime_cache_size: 2,
announce_block: true,
base_path: Some(base_path),
informant_output_format: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
tracing_targets: None,
tracing_receiver: Default::default(),
max_runtime_instances: 8,
runtime_cache_size: 2,
announce_block: true,
base_path: Some(base_path),
informant_output_format: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub fn new_partial(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);

let (client, backend, keystore_container, task_manager) =
Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn bench_execute_block(c: &mut Criterion) {
ExecutionMethod::Wasm(wasm_method) => (false, wasm_method),
};

let executor = NativeElseWasmExecutor::new(wasm_method, None, 8);
let executor = NativeElseWasmExecutor::new(wasm_method, None, 8, 2);
let runtime_code = RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()),
hash: vec![1, 2, 3],
Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn from_block_number(n: u32) -> Header {
}

pub fn executor() -> NativeElseWasmExecutor<ExecutorDispatch> {
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8)
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8, 2)
}

pub fn executor_call<
Expand Down
1 change: 1 addition & 0 deletions bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl InspectCmd {
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);

let client = new_full_client::<B, RA, _>(&config, None, executor)?;
Expand Down
2 changes: 1 addition & 1 deletion bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl BenchDb {
let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
let client = sc_service::new_client(
backend.clone(),
NativeElseWasmExecutor::new(WasmExecutionMethod::Compiled, None, 8),
NativeElseWasmExecutor::new(WasmExecutionMethod::Compiled, None, 8, 2),
&keyring.generate_genesis(),
None,
None,
Expand Down
8 changes: 8 additions & 0 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ pub struct RunCmd {
#[structopt(long)]
pub max_runtime_instances: Option<usize>,

/// Maximum number of different runtimes that can be cached.
#[structopt(long, default_value = "2")]
pub runtime_cache_size: u8,

/// Run a temporary node.
///
/// A temporary directory will be created to store the configuration and will be deleted
Expand Down Expand Up @@ -453,6 +457,10 @@ impl CliConfiguration for RunCmd {
Ok(self.max_runtime_instances.map(|x| x.min(256)))
}

fn runtime_cache_size(&self) -> Result<u8> {
Ok(self.runtime_cache_size)
}

fn base_path(&self) -> Result<Option<BasePath>> {
Ok(if self.tmp {
Some(BasePath::new_temp_dir()?)
Expand Down
9 changes: 9 additions & 0 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(Default::default())
}

/// Get maximum different runtimes in cache
///
/// By default this is `2`.
fn runtime_cache_size(&self) -> Result<u8> {
Ok(2)
}

/// Activate or not the automatic announcing of blocks after import
///
/// By default this is `false`.
Expand Down Expand Up @@ -482,6 +489,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
let is_validator = role.is_authority();
let (keystore_remote, keystore) = self.keystore_config(&config_dir)?;
let telemetry_endpoints = self.telemetry_endpoints(&chain_spec)?;
let runtime_cache_size = self.runtime_cache_size()?;

let unsafe_pruning = self.import_params().map(|p| p.unsafe_pruning).unwrap_or(false);

Expand Down Expand Up @@ -534,6 +542,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
role,
base_path: Some(base_path),
informant_output_format: Default::default(),
runtime_cache_size,
})
}

Expand Down
1 change: 1 addition & 0 deletions client/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ parking_lot = "0.11.1"
log = "0.4.8"
libsecp256k1 = "0.7"
sp-core-hashing-proc-macro = { version = "4.0.0-dev", path = "../../primitives/core/hashing/proc-macro" }
lru = "0.6.6"

[dev-dependencies]
wat = "1.0"
Expand Down
3 changes: 3 additions & 0 deletions client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn call_in_wasm<E: Externalities>(
HostFunctions::host_functions(),
8,
None,
2,
);
executor.uncached_call(
RuntimeBlob::uncompress_if_needed(&wasm_binary_unwrap()[..]).unwrap(),
Expand Down Expand Up @@ -480,6 +481,7 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
HostFunctions::host_functions(),
8,
None,
2,
);

let err = executor
Expand Down Expand Up @@ -593,6 +595,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
HostFunctions::host_functions(),
8,
None,
2,
));
let threads: Vec<_> = (0..8)
.map(|_| {
Expand Down
1 change: 1 addition & 0 deletions client/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod tests {
sp_io::SubstrateHostFunctions::host_functions(),
8,
None,
2,
);
let res = executor
.uncached_call(
Expand Down
10 changes: 9 additions & 1 deletion client/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ impl WasmExecutor {
host_functions: Vec<&'static dyn Function>,
max_runtime_instances: usize,
cache_path: Option<PathBuf>,
runtime_cache_size: u8,
) -> Self {
WasmExecutor {
method,
default_heap_pages: default_heap_pages.unwrap_or(DEFAULT_HEAP_PAGES),
host_functions: Arc::new(host_functions),
cache: Arc::new(RuntimeCache::new(max_runtime_instances, cache_path.clone())),
cache: Arc::new(RuntimeCache::new(
max_runtime_instances,
cache_path.clone(),
runtime_cache_size,
)),
cache_path,
}
}
Expand Down Expand Up @@ -330,6 +335,7 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
fallback_method: WasmExecutionMethod,
default_heap_pages: Option<u64>,
max_runtime_instances: usize,
runtime_cache_size: u8,
) -> Self {
let extended = D::ExtendHostFunctions::host_functions();
let mut host_functions = sp_io::SubstrateHostFunctions::host_functions()
Expand All @@ -351,6 +357,7 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
host_functions,
max_runtime_instances,
None,
runtime_cache_size,
);

NativeElseWasmExecutor {
Expand Down Expand Up @@ -636,6 +643,7 @@ mod tests {
WasmExecutionMethod::Interpreted,
None,
8,
2,
);
my_interface::HostFunctions::host_functions().iter().for_each(|function| {
assert_eq!(executor.wasm.host_functions.iter().filter(|f| f == &function).count(), 2);
Expand Down
Loading

0 comments on commit 1aea26e

Please sign in to comment.