From 73ff6b20a7981f8b65cb0af6b76819984d16a43c Mon Sep 17 00:00:00 2001 From: librelois Date: Mon, 27 Sep 2021 13:09:41 +0200 Subject: [PATCH 01/11] add feature wasmtime-jitdump --- Cargo.lock | 23 +++++++++++++++++++++++ bin/node/executor/Cargo.toml | 1 + client/cli/Cargo.toml | 1 + client/executor/Cargo.toml | 1 + client/executor/wasmtime/Cargo.toml | 3 +++ client/executor/wasmtime/src/runtime.rs | 4 ++++ client/service/Cargo.toml | 1 + 7 files changed, 34 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9f740f4de4f59..79420faf39fc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8706,6 +8706,26 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scroll" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda28d4b4830b807a8b43f7b0e6b5df875311b3e7621d84577188c175b6ec1ec" +dependencies = [ + "scroll_derive", +] + +[[package]] +name = "scroll_derive" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaaae8f38bb311444cfb7f1979af0bc9240d95795f75f9ceddf6a59b79ceffa0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sct" version = "0.6.0" @@ -11624,8 +11644,11 @@ checksum = "e24364d522dcd67c897c8fffc42e5bdfc57207bbb6d7eeade0da9d4a7d70105b" dependencies = [ "anyhow", "cfg-if 1.0.0", + "gimli 0.24.0", "lazy_static", "libc", + "object 0.24.0", + "scroll", "serde", "target-lexicon 0.12.0", "wasmtime-environ", diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index f283a913915f3..312618bdabbfc 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -42,6 +42,7 @@ futures = "0.3.9" [features] wasmtime = ["sc-executor/wasmtime"] +wasmtime-jitdump = ["sc-executor/wasmtime-jitdump"] wasmi-errno = ["sc-executor/wasmi-errno"] stress-test = [] diff --git a/client/cli/Cargo.toml b/client/cli/Cargo.toml index e7a0330e76e0c..e42414f59d7ca 100644 --- a/client/cli/Cargo.toml +++ b/client/cli/Cargo.toml @@ -52,3 +52,4 @@ tempfile = "3.1.0" wasmtime = [ "sc-service/wasmtime", ] +wasmtime-jitdump = ["sc-service/wasmtime-jitdump"] diff --git a/client/executor/Cargo.toml b/client/executor/Cargo.toml index b7e2595b8e169..bf7aca6f449ca 100644 --- a/client/executor/Cargo.toml +++ b/client/executor/Cargo.toml @@ -54,5 +54,6 @@ default = ["std"] std = [] wasm-extern-trace = [] wasmtime = ["sc-executor-wasmtime"] +wasmtime-jitdump = ["wasmtime", "sc-executor-wasmtime/jitdump"] wasmi-errno = ["wasmi/errno"] wasmer-sandbox = ["sc-executor-common/wasmer-sandbox"] diff --git a/client/executor/wasmtime/Cargo.toml b/client/executor/wasmtime/Cargo.toml index 3158cdecc3263..a3cfa9987277f 100644 --- a/client/executor/wasmtime/Cargo.toml +++ b/client/executor/wasmtime/Cargo.toml @@ -33,3 +33,6 @@ wasmtime = { version = "0.27.0", default-features = false, features = [ sc-runtime-test = { version = "2.0.0", path = "../runtime-test" } sp-io = { version = "4.0.0-dev", path = "../../../primitives/io" } wat = "1.0" + +[features] +jitdump = ["wasmtime/jitdump"] diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index f6878ec5ee6e1..e7a74557fbd5c 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -251,6 +251,10 @@ directory = \"{cache_dir}\" fn common_config(semantics: &Semantics) -> std::result::Result { let mut config = wasmtime::Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); + #[cfg(feature = "jitdump")] + if std::env::var("WASMTIME_PROFILING_STRATEGY") == Ok("jitdump".to_owned()) { + config.profiler(wasmtime::ProfilingStrategy::JitDump).unwrap(); + } config.cranelift_nan_canonicalization(semantics.canonicalize_nans); if let Some(DeterministicStackLimit { native_stack_max, .. }) = diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 5120cc8f4dfaa..717099c9ce21a 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -18,6 +18,7 @@ default = ["db"] # a path to a database, an error will be produced at runtime. db = ["sc-client-db/with-kvdb-rocksdb", "sc-client-db/with-parity-db"] wasmtime = ["sc-executor/wasmtime"] +wasmtime-jitdump = ["sc-executor/wasmtime-jitdump"] # exposes the client type test-helpers = [] From 0ce88ab4b2b9de9e5dd7b13f1d7e6dafc8ba1bf0 Mon Sep 17 00:00:00 2001 From: librelois Date: Mon, 27 Sep 2021 14:54:09 +0200 Subject: [PATCH 02/11] remove unwrap --- client/executor/wasmtime/src/runtime.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index e7a74557fbd5c..5839fa6eabea1 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -253,7 +253,9 @@ fn common_config(semantics: &Semantics) -> std::result::Result Date: Tue, 28 Sep 2021 12:52:25 +0200 Subject: [PATCH 03/11] always enable wasmtime/jitdump feature --- bin/node/executor/Cargo.toml | 1 - client/cli/Cargo.toml | 1 - client/executor/Cargo.toml | 1 - client/executor/wasmtime/Cargo.toml | 4 +--- client/executor/wasmtime/src/runtime.rs | 1 - client/service/Cargo.toml | 1 - 6 files changed, 1 insertion(+), 8 deletions(-) diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index 312618bdabbfc..f283a913915f3 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -42,7 +42,6 @@ futures = "0.3.9" [features] wasmtime = ["sc-executor/wasmtime"] -wasmtime-jitdump = ["sc-executor/wasmtime-jitdump"] wasmi-errno = ["sc-executor/wasmi-errno"] stress-test = [] diff --git a/client/cli/Cargo.toml b/client/cli/Cargo.toml index e42414f59d7ca..e7a0330e76e0c 100644 --- a/client/cli/Cargo.toml +++ b/client/cli/Cargo.toml @@ -52,4 +52,3 @@ tempfile = "3.1.0" wasmtime = [ "sc-service/wasmtime", ] -wasmtime-jitdump = ["sc-service/wasmtime-jitdump"] diff --git a/client/executor/Cargo.toml b/client/executor/Cargo.toml index bf7aca6f449ca..b7e2595b8e169 100644 --- a/client/executor/Cargo.toml +++ b/client/executor/Cargo.toml @@ -54,6 +54,5 @@ default = ["std"] std = [] wasm-extern-trace = [] wasmtime = ["sc-executor-wasmtime"] -wasmtime-jitdump = ["wasmtime", "sc-executor-wasmtime/jitdump"] wasmi-errno = ["wasmi/errno"] wasmer-sandbox = ["sc-executor-common/wasmer-sandbox"] diff --git a/client/executor/wasmtime/Cargo.toml b/client/executor/wasmtime/Cargo.toml index a3cfa9987277f..b6f7e03af2a8e 100644 --- a/client/executor/wasmtime/Cargo.toml +++ b/client/executor/wasmtime/Cargo.toml @@ -26,6 +26,7 @@ sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" } sc-allocator = { version = "4.0.0-dev", path = "../../allocator" } wasmtime = { version = "0.27.0", default-features = false, features = [ "cache", + "jitdump", "parallel-compilation", ] } @@ -33,6 +34,3 @@ wasmtime = { version = "0.27.0", default-features = false, features = [ sc-runtime-test = { version = "2.0.0", path = "../runtime-test" } sp-io = { version = "4.0.0-dev", path = "../../../primitives/io" } wat = "1.0" - -[features] -jitdump = ["wasmtime/jitdump"] diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index 5839fa6eabea1..307e1352fbb29 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -251,7 +251,6 @@ directory = \"{cache_dir}\" fn common_config(semantics: &Semantics) -> std::result::Result { let mut config = wasmtime::Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); - #[cfg(feature = "jitdump")] if std::env::var("WASMTIME_PROFILING_STRATEGY") == Ok("jitdump".to_owned()) { config.profiler(wasmtime::ProfilingStrategy::JitDump).map_err(|e| { WasmError::Instantiation(format!("fail to set jitdump profiler: {}", e)) diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 717099c9ce21a..5120cc8f4dfaa 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -18,7 +18,6 @@ default = ["db"] # a path to a database, an error will be produced at runtime. db = ["sc-client-db/with-kvdb-rocksdb", "sc-client-db/with-parity-db"] wasmtime = ["sc-executor/wasmtime"] -wasmtime-jitdump = ["sc-executor/wasmtime-jitdump"] # exposes the client type test-helpers = [] From 2a9c2eeda600e7b12cb9a607e7167070bf3c2d58 Mon Sep 17 00:00:00 2001 From: librelois Date: Tue, 28 Sep 2021 13:13:52 +0200 Subject: [PATCH 04/11] env WASMTIME_PROFILING_STRATEGY: retun an error for unknown value --- client/executor/wasmtime/src/runtime.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index 307e1352fbb29..c3ecf8ea7844e 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -251,13 +251,17 @@ directory = \"{cache_dir}\" fn common_config(semantics: &Semantics) -> std::result::Result { let mut config = wasmtime::Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); - if std::env::var("WASMTIME_PROFILING_STRATEGY") == Ok("jitdump".to_owned()) { - config.profiler(wasmtime::ProfilingStrategy::JitDump).map_err(|e| { - WasmError::Instantiation(format!("fail to set jitdump profiler: {}", e)) - })?; - } config.cranelift_nan_canonicalization(semantics.canonicalize_nans); + let profiler = match std::env::var_os("WASMTIME_PROFILING_STRATEGY") { + Some(os_string) if os_string == "jitdump" => wasmtime::ProfilingStrategy::JitDump, + Some(_) => return Err(WasmError::Instantiation("Unknown profiling strategy".to_owned())), + None => wasmtime::ProfilingStrategy::None, + }; + config + .profiler(profiler) + .map_err(|e| WasmError::Instantiation(format!("fail to set jitdump profiler: {}", e)))?; + if let Some(DeterministicStackLimit { native_stack_max, .. }) = semantics.deterministic_stack_limit { From b616975803494f786aaaf22e2aacfae03f257aca Mon Sep 17 00:00:00 2001 From: librelois Date: Tue, 28 Sep 2021 13:14:24 +0200 Subject: [PATCH 05/11] Add doc for env var WASMTIME_PROFILING_STRATEGY --- client/executor/wasmtime/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/client/executor/wasmtime/src/lib.rs b/client/executor/wasmtime/src/lib.rs index 62b0b205f6de6..bac5d186f6a93 100644 --- a/client/executor/wasmtime/src/lib.rs +++ b/client/executor/wasmtime/src/lib.rs @@ -16,7 +16,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -/// ! Defines a `WasmRuntime` that uses the Wasmtime JIT to execute. +//! Defines a `WasmRuntime` that uses the Wasmtime JIT to execute. +//! +//! You can choose a profiling strategy at runtime with +//! environment variable `WASMTIME_PROFILING_STRATEGY`: +//! +//! | `WASMTIME_PROFILING_STRATEGY` | Effect | +//! |-------------|-------------------| +//! | undefined | No profiling | +//! | `"jitdump"` | jitdump profiling | +//! | other value | error | + mod host; mod imports; mod instance_wrapper; From 2a249b0399420e47b4632370c4b028ff314cdad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Tue, 28 Sep 2021 14:31:17 +0200 Subject: [PATCH 06/11] Update client/executor/wasmtime/Cargo.toml Co-authored-by: Sergei Shulepov --- client/executor/wasmtime/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/executor/wasmtime/Cargo.toml b/client/executor/wasmtime/Cargo.toml index b6f7e03af2a8e..a94cefb133564 100644 --- a/client/executor/wasmtime/Cargo.toml +++ b/client/executor/wasmtime/Cargo.toml @@ -26,7 +26,7 @@ sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" } sc-allocator = { version = "4.0.0-dev", path = "../../allocator" } wasmtime = { version = "0.27.0", default-features = false, features = [ "cache", - "jitdump", + "jitdump", "parallel-compilation", ] } From d1d6fedb813b8b50bacda5709f028421ca386962 Mon Sep 17 00:00:00 2001 From: librelois Date: Tue, 28 Sep 2021 14:46:38 +0200 Subject: [PATCH 07/11] warning instead of error --- client/executor/wasmtime/src/runtime.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index c3ecf8ea7844e..dc65b6b1a6525 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -255,7 +255,10 @@ fn common_config(semantics: &Semantics) -> std::result::Result wasmtime::ProfilingStrategy::JitDump, - Some(_) => return Err(WasmError::Instantiation("Unknown profiling strategy".to_owned())), + Some(_) => { + log::warn!("WASMTIME_PROFILING_STRATEGY is set to unknown value, ignored."); + wasmtime::ProfilingStrategy::None + }, None => wasmtime::ProfilingStrategy::None, }; config From 5e620bdcadd5e89250f30c258da9bda66fff847f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Tue, 28 Sep 2021 16:35:41 +0200 Subject: [PATCH 08/11] Update client/executor/wasmtime/src/runtime.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/executor/wasmtime/src/runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index dc65b6b1a6525..f71c2091fcd70 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -263,7 +263,7 @@ fn common_config(semantics: &Semantics) -> std::result::Result Date: Tue, 28 Sep 2021 17:39:01 +0200 Subject: [PATCH 09/11] update doc: unknown value cause warning instead of error --- client/executor/wasmtime/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/executor/wasmtime/src/lib.rs b/client/executor/wasmtime/src/lib.rs index bac5d186f6a93..83a9839e5a57f 100644 --- a/client/executor/wasmtime/src/lib.rs +++ b/client/executor/wasmtime/src/lib.rs @@ -22,10 +22,10 @@ //! environment variable `WASMTIME_PROFILING_STRATEGY`: //! //! | `WASMTIME_PROFILING_STRATEGY` | Effect | -//! |-------------|-------------------| -//! | undefined | No profiling | -//! | `"jitdump"` | jitdump profiling | -//! | other value | error | +//! |-------------|-------------------------| +//! | undefined | No profiling | +//! | `"jitdump"` | jitdump profiling | +//! | other value | No profiling (warning) | mod host; mod imports; From 80fd4aedd4e7299ff8f3a457625a6ba0a9c59ff2 Mon Sep 17 00:00:00 2001 From: librelois Date: Wed, 29 Sep 2021 15:19:15 +0200 Subject: [PATCH 10/11] log warning only once --- client/executor/wasmtime/src/runtime.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index f71c2091fcd70..c68f7afa52951 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -36,7 +36,10 @@ use sp_wasm_interface::{Function, Pointer, Value, WordSize}; use std::{ path::{Path, PathBuf}, rc::Rc, - sync::Arc, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, }; use wasmtime::{Engine, Store}; @@ -248,6 +251,9 @@ directory = \"{cache_dir}\" Ok(()) } +// Remember if we have already logged a warning due to an unknown profiling strategy. +static UNKNOWN_PROFILING_STRATEGY: AtomicBool = AtomicBool::new(false); + fn common_config(semantics: &Semantics) -> std::result::Result { let mut config = wasmtime::Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); @@ -256,7 +262,10 @@ fn common_config(semantics: &Semantics) -> std::result::Result wasmtime::ProfilingStrategy::JitDump, Some(_) => { - log::warn!("WASMTIME_PROFILING_STRATEGY is set to unknown value, ignored."); + // Make sure that the warning will not be relogged regularly. + if !UNKNOWN_PROFILING_STRATEGY.swap(true, Ordering::Relaxed) { + log::warn!("WASMTIME_PROFILING_STRATEGY is set to unknown value, ignored."); + } wasmtime::ProfilingStrategy::None }, None => wasmtime::ProfilingStrategy::None, From b543fa594b6a1345ee6bdbc42ead327fb3e2ca3d Mon Sep 17 00:00:00 2001 From: librelois Date: Wed, 29 Sep 2021 17:24:18 +0200 Subject: [PATCH 11/11] static right next to the usage --- client/executor/wasmtime/src/runtime.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index 150affc109530..a62356357b1f4 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -320,9 +320,6 @@ directory = \"{cache_dir}\" Ok(()) } -// Remember if we have already logged a warning due to an unknown profiling strategy. -static UNKNOWN_PROFILING_STRATEGY: AtomicBool = AtomicBool::new(false); - fn common_config(semantics: &Semantics) -> std::result::Result { let mut config = wasmtime::Config::new(); config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); @@ -330,14 +327,16 @@ fn common_config(semantics: &Semantics) -> std::result::Result wasmtime::ProfilingStrategy::JitDump, + None => wasmtime::ProfilingStrategy::None, Some(_) => { + // Remember if we have already logged a warning due to an unknown profiling strategy. + static UNKNOWN_PROFILING_STRATEGY: AtomicBool = AtomicBool::new(false); // Make sure that the warning will not be relogged regularly. if !UNKNOWN_PROFILING_STRATEGY.swap(true, Ordering::Relaxed) { log::warn!("WASMTIME_PROFILING_STRATEGY is set to unknown value, ignored."); } wasmtime::ProfilingStrategy::None }, - None => wasmtime::ProfilingStrategy::None, }; config .profiler(profiler)