From 8824a60deea54d9b437407a21c8ceaf6a1902ee5 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 24 Jul 2020 00:45:09 +0200 Subject: [PATCH] replace with global tracing --- primitives/runtime-interface/Cargo.toml | 5 ++-- .../runtime-interface/proc-macro/src/lib.rs | 4 ++- .../bare_function_interface.rs | 15 ++++++++-- .../host_function_interface.rs | 28 +++++++++++++++---- .../proc-macro/src/runtime_interface/mod.rs | 10 +++++-- primitives/runtime-interface/src/lib.rs | 3 +- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/primitives/runtime-interface/Cargo.toml b/primitives/runtime-interface/Cargo.toml index 16d5a14e889b1..f50f11c42d319 100644 --- a/primitives/runtime-interface/Cargo.toml +++ b/primitives/runtime-interface/Cargo.toml @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-wasm-interface = { version = "2.0.0-rc5", path = "../wasm-interface", default-features = false } sp-std = { version = "2.0.0-rc5", default-features = false, path = "../std" } -sp-tracing = { version = "2.0.0-rc5", default-features = false, path = "../tracing" } +tracing = { version = "0.1.17", default-features = false, optional = true } sp-runtime-interface-proc-macro = { version = "2.0.0-rc5", path = "proc-macro" } sp-externalities = { version = "0.8.0-rc5", optional = true, path = "../externalities" } codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false } @@ -35,7 +35,8 @@ default = [ "std" ] std = [ "sp-wasm-interface/std", "sp-std/std", - "sp-tracing/std", + "tracing", + "tracing/std", "codec/std", "sp-externalities", "primitive-types/std", diff --git a/primitives/runtime-interface/proc-macro/src/lib.rs b/primitives/runtime-interface/proc-macro/src/lib.rs index 2f5b9de1c14e7..49f42f82408f7 100644 --- a/primitives/runtime-interface/proc-macro/src/lib.rs +++ b/primitives/runtime-interface/proc-macro/src/lib.rs @@ -39,8 +39,10 @@ pub fn runtime_interface( ) -> proc_macro::TokenStream { let trait_def = parse_macro_input!(input as ItemTrait); let wasm_only = parse_macro_input!(attrs as Option); + // FIXME: actual parse for this + let no_tracing : Option<()> = None; //parse_macro_input!(tracing_attrs as Option); - runtime_interface::runtime_interface_impl(trait_def, wasm_only.is_some()) + runtime_interface::runtime_interface_impl(trait_def, wasm_only.is_some(), no_tracing.is_none()) .unwrap_or_else(|e| e.to_compile_error()) .into() } diff --git a/primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs b/primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs index 6760e9656113a..2cc0c3551e87b 100644 --- a/primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs +++ b/primitives/runtime-interface/proc-macro/src/runtime_interface/bare_function_interface.rs @@ -46,7 +46,7 @@ use std::iter; /// Generate one bare function per trait method. The name of the bare function is equal to the name /// of the trait method. -pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result { +pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result { let trait_name = &trait_def.ident; let runtime_interface = get_runtime_interface(trait_def)?; @@ -63,7 +63,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result = runtime_interface.all_versions().try_fold(token_stream?, |mut t, (version, method)| { - t.extend(function_std_impl(trait_name, method, version, is_wasm_only)?); + t.extend(function_std_impl(trait_name, method, version, is_wasm_only, with_tracing)?); Ok(t) }); @@ -145,6 +145,7 @@ fn function_std_impl( method: &TraitItemMethod, version: u32, is_wasm_only: bool, + with_tracing: bool, ) -> Result { let function_name = create_function_ident_with_version(&method.sig.ident, version); let function_name_str = function_name.to_string(); @@ -164,6 +165,14 @@ fn function_std_impl( } ).take(1), ); + let tracing = if with_tracing { + Some(quote!( + let __trace_span = #crate_::tracing::trace_span!(#function_name_str); + let __guard = __trace_span.enter(); + )) + } else { + None + }; let return_value = &method.sig.output; let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version")); // Don't make the function public accessible when this is a wasm only interface. @@ -174,7 +183,7 @@ fn function_std_impl( #[cfg(feature = "std")] #( #attrs )* fn #function_name( #( #args, )* ) #return_value { - #crate_::sp_tracing::enter_span!(#function_name_str); + #tracing #call_to_trait } } diff --git a/primitives/runtime-interface/proc-macro/src/runtime_interface/host_function_interface.rs b/primitives/runtime-interface/proc-macro/src/runtime_interface/host_function_interface.rs index 721eed649c25d..ac436e1512180 100644 --- a/primitives/runtime-interface/proc-macro/src/runtime_interface/host_function_interface.rs +++ b/primitives/runtime-interface/proc-macro/src/runtime_interface/host_function_interface.rs @@ -43,7 +43,7 @@ use std::iter::{Iterator, self}; /// Generate the extern host functions for wasm and the `HostFunctions` struct that provides the /// implementations for the host functions on the host. -pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result { +pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result { let trait_name = &trait_def.ident; let extern_host_function_impls = get_runtime_interface(trait_def)? .latest_versions() @@ -57,7 +57,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result(t) })?; - let host_functions_struct = generate_host_functions_struct(trait_def, is_wasm_only)?; + let host_functions_struct = generate_host_functions_struct(trait_def, is_wasm_only, with_tracing)?; Ok( quote! { @@ -158,13 +158,21 @@ fn generate_exchangeable_host_function(method: &TraitItemMethod) -> Result Result { +fn generate_host_functions_struct(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) + -> Result +{ let crate_ = generate_crate_access(); let host_functions = get_runtime_interface(trait_def)? .all_versions() .map(|(version, method)| - generate_host_function_implementation(&trait_def.ident, method, version, is_wasm_only) + generate_host_function_implementation( + &trait_def.ident, + method, + version, + is_wasm_only, + with_tracing + ) ) .collect::>>()?; @@ -194,6 +202,7 @@ fn generate_host_function_implementation( method: &TraitItemMethod, version: u32, is_wasm_only: bool, + with_tracing: bool, ) -> Result { let name = create_host_function_ident(&method.sig.ident, version, trait_name).to_string(); let struct_name = Ident::new(&name.to_pascal_case(), Span::call_site()); @@ -207,6 +216,15 @@ fn generate_host_function_implementation( let host_function_call = generate_host_function_call(&method.sig, version, is_wasm_only); let into_preallocated_ffi_value = generate_into_preallocated_ffi_value(&method.sig)?; let convert_return_value = generate_return_value_into_wasm_value(&method.sig); + let tracing = if with_tracing { + Some(quote!( + let __trace_span = #crate_::tracing::trace_span!(#name); + let __guard = __trace_span.enter(); + )) + } else { + None + }; + Ok( quote! { @@ -227,7 +245,7 @@ fn generate_host_function_implementation( __function_context__: &mut dyn #crate_::sp_wasm_interface::FunctionContext, args: &mut dyn Iterator, ) -> std::result::Result, String> { - #crate_::sp_tracing::enter_span!(#name); + #tracing #( #wasm_to_ffi_values )* #( #ffi_to_host_values )* #host_function_call diff --git a/primitives/runtime-interface/proc-macro/src/runtime_interface/mod.rs b/primitives/runtime-interface/proc-macro/src/runtime_interface/mod.rs index c9b6edf68fd5a..0fce47c20aed4 100644 --- a/primitives/runtime-interface/proc-macro/src/runtime_interface/mod.rs +++ b/primitives/runtime-interface/proc-macro/src/runtime_interface/mod.rs @@ -33,18 +33,22 @@ mod trait_decl_impl; pub mod keywords { // Custom keyword `wasm_only` that can be given as attribute to [`runtime_interface`]. syn::custom_keyword!(wasm_only); + // Custom keyword `no_tracing` that can be given as attribute to [`runtime_interface`]. + syn::custom_keyword!(no_tracing); } /// Implementation of the `runtime_interface` attribute. /// /// It expects the trait definition the attribute was put above and if this should be an wasm only /// interface. -pub fn runtime_interface_impl(trait_def: ItemTrait, is_wasm_only: bool) -> Result { - let bare_functions = bare_function_interface::generate(&trait_def, is_wasm_only)?; +pub fn runtime_interface_impl(trait_def: ItemTrait, is_wasm_only: bool, with_tracing: bool) + -> Result +{ + let bare_functions = bare_function_interface::generate(&trait_def, is_wasm_only, with_tracing)?; let crate_include = generate_runtime_interface_include(); let mod_name = Ident::new(&trait_def.ident.to_string().to_snake_case(), Span::call_site()); let trait_decl_impl = trait_decl_impl::process(&trait_def, is_wasm_only)?; - let host_functions = host_function_interface::generate(&trait_def, is_wasm_only)?; + let host_functions = host_function_interface::generate(&trait_def, is_wasm_only, with_tracing)?; let vis = trait_def.vis; let attrs = &trait_def.attrs; diff --git a/primitives/runtime-interface/src/lib.rs b/primitives/runtime-interface/src/lib.rs index 562f94b278efc..6511f93e8de2a 100644 --- a/primitives/runtime-interface/src/lib.rs +++ b/primitives/runtime-interface/src/lib.rs @@ -111,7 +111,8 @@ extern crate self as sp_runtime_interface; pub use sp_wasm_interface; #[doc(hidden)] -pub use sp_tracing; +#[cfg(feature = "std")] +use tracing; #[doc(hidden)] pub use sp_std;