Runtime worker threads (#7089)

* std variant

* principal work

* format and naming

* format and naming continued

* working nested fork

* add comment

* naming and tabs

* line width

* fix wording

* address review

* refactor dynamic dispatch

* update wasmtime

* some care

* move ext

* more refactor

* doc effort

* simplify

* doc effort

* tests and docs

* address review

* naming

* explain some args

* add example

* unwinding for native and tests

* rename stray

* fix refs

* fix tests

* fix warnings

* stray naming

* fixes and comments

* Update primitives/io/src/tasks.rs

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* make examples "compile"

* dyn_dispatch -> spawn_call

* fix impl

* address review

* Update primitives/io/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/async_externalities.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/example-parallel/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* fix compilation

* Update client/executor/common/src/wasm_runtime.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* address review

* Update client/executor/wasmtime/src/instance_wrapper.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* Update client/executor/src/native_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/executor/src/native_executor.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/io/src/tasks.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/executor/wasmtime/src/instance_wrapper.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* address some issues

* address more issues

* wasm_only interface

* define sp_tasks

* avoid anyhow

* fix example

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Sergei Shulepov <sergei@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-10-20 05:41:51 -07:00
committed by GitHub
parent 203acda659
commit a062bc2f1d
26 changed files with 1498 additions and 112 deletions
+11 -9
View File
@@ -53,7 +53,7 @@ struct VersionedRuntime {
/// Wasm runtime type.
wasm_method: WasmExecutionMethod,
/// Shared runtime that can spawn instances.
module: Box<dyn WasmModule>,
module: Arc<dyn WasmModule>,
/// The number of WebAssembly heap pages this instance was created with.
heap_pages: u64,
/// Runtime version according to `Core_version` if any.
@@ -70,6 +70,7 @@ impl VersionedRuntime {
f: F,
) -> Result<R, Error>
where F: FnOnce(
&Arc<dyn WasmModule>,
&dyn WasmInstance,
Option<&RuntimeVersion>,
&mut dyn Externalities)
@@ -87,7 +88,7 @@ impl VersionedRuntime {
.map(|r| Ok((r, false)))
.unwrap_or_else(|| self.module.new_instance().map(|i| (i, true)))?;
let result = f(&*instance, self.version.as_ref(), ext);
let result = f(&self.module, &*instance, self.version.as_ref(), ext);
if let Err(e) = &result {
if new_inst {
log::warn!(
@@ -123,7 +124,7 @@ impl VersionedRuntime {
// Allocate a new instance
let instance = self.module.new_instance()?;
f(&*instance, self.version.as_ref(), ext)
f(&self.module, &*instance, self.version.as_ref(), ext)
}
}
}
@@ -199,6 +200,7 @@ impl RuntimeCache {
f: F,
) -> Result<Result<R, Error>, Error>
where F: FnOnce(
&Arc<dyn WasmModule>,
&dyn WasmInstance,
Option<&RuntimeVersion>,
&mut dyn Externalities)
@@ -267,7 +269,7 @@ pub fn create_wasm_runtime_with_code(
code: &[u8],
host_functions: Vec<&'static dyn Function>,
allow_missing_func_imports: bool,
) -> Result<Box<dyn WasmModule>, WasmError> {
) -> Result<Arc<dyn WasmModule>, WasmError> {
match wasm_method {
WasmExecutionMethod::Interpreted =>
sc_executor_wasmi::create_runtime(
@@ -275,7 +277,7 @@ pub fn create_wasm_runtime_with_code(
heap_pages,
host_functions,
allow_missing_func_imports
).map(|runtime| -> Box<dyn WasmModule> { Box::new(runtime) }),
).map(|runtime| -> Arc<dyn WasmModule> { Arc::new(runtime) }),
#[cfg(feature = "wasmtime")]
WasmExecutionMethod::Compiled =>
sc_executor_wasmtime::create_runtime(
@@ -283,7 +285,7 @@ pub fn create_wasm_runtime_with_code(
heap_pages,
host_functions,
allow_missing_func_imports
).map(|runtime| -> Box<dyn WasmModule> { Box::new(runtime) }),
).map(|runtime| -> Arc<dyn WasmModule> { Arc::new(runtime) }),
}
}
@@ -318,7 +320,7 @@ fn create_versioned_wasm_runtime(
) -> Result<VersionedRuntime, WasmError> {
#[cfg(not(target_os = "unknown"))]
let time = std::time::Instant::now();
let mut runtime = create_wasm_runtime_with_code(
let runtime = create_wasm_runtime_with_code(
wasm_method,
heap_pages,
&code,
@@ -333,10 +335,10 @@ fn create_versioned_wasm_runtime(
// The following unwind safety assertion is OK because if the method call panics, the
// runtime will be dropped.
let runtime = AssertUnwindSafe(runtime.as_mut());
let runtime = AssertUnwindSafe(runtime.as_ref());
crate::native_executor::with_externalities_safe(
&mut **ext,
move || runtime.new_instance()?.call("Core_version", &[])
move || runtime.new_instance()?.call("Core_version".into(), &[])
).map_err(|_| WasmError::Instantiation("panic in call to get runtime version".into()))?
};
let version = match version_result {