Skip to content

Commit

Permalink
replace with global tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Jul 23, 2020
1 parent 70492f5 commit 8824a60
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
5 changes: 3 additions & 2 deletions primitives/runtime-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion primitives/runtime-interface/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<runtime_interface::keywords::wasm_only>);
// FIXME: actual parse for this
let no_tracing : Option<()> = None; //parse_macro_input!(tracing_attrs as Option<runtime_interface::keywords::no_tracing>);

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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream> {
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result<TokenStream> {
let trait_name = &trait_def.ident;
let runtime_interface = get_runtime_interface(trait_def)?;

Expand All @@ -63,7 +63,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream
// earlier versions compatibility dispatch (only std variant)
let result: Result<TokenStream> = 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)
});

Expand Down Expand Up @@ -145,6 +145,7 @@ fn function_std_impl(
method: &TraitItemMethod,
version: u32,
is_wasm_only: bool,
with_tracing: bool,
) -> Result<TokenStream> {
let function_name = create_function_ident_with_version(&method.sig.ident, version);
let function_name_str = function_name.to_string();
Expand All @@ -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.
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream> {
pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool) -> Result<TokenStream> {
let trait_name = &trait_def.ident;
let extern_host_function_impls = get_runtime_interface(trait_def)?
.latest_versions()
Expand All @@ -57,7 +57,7 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream
t.extend(generate_exchangeable_host_function(m)?);
Ok::<_, Error>(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! {
Expand Down Expand Up @@ -158,13 +158,21 @@ fn generate_exchangeable_host_function(method: &TraitItemMethod) -> Result<Token

/// Generate the `HostFunctions` struct that implements `wasm-interface::HostFunctions` to provide
/// implementations for the extern host functions.
fn generate_host_functions_struct(trait_def: &ItemTrait, is_wasm_only: bool) -> Result<TokenStream> {
fn generate_host_functions_struct(trait_def: &ItemTrait, is_wasm_only: bool, with_tracing: bool)
-> Result<TokenStream>
{
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::<Result<Vec<_>>>()?;

Expand Down Expand Up @@ -194,6 +202,7 @@ fn generate_host_function_implementation(
method: &TraitItemMethod,
version: u32,
is_wasm_only: bool,
with_tracing: bool,
) -> Result<TokenStream> {
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());
Expand All @@ -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! {
Expand All @@ -227,7 +245,7 @@ fn generate_host_function_implementation(
__function_context__: &mut dyn #crate_::sp_wasm_interface::FunctionContext,
args: &mut dyn Iterator<Item = #crate_::sp_wasm_interface::Value>,
) -> std::result::Result<Option<#crate_::sp_wasm_interface::Value>, String> {
#crate_::sp_tracing::enter_span!(#name);
#tracing
#( #wasm_to_ffi_values )*
#( #ffi_to_host_values )*
#host_function_call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream> {
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<TokenStream>
{
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;

Expand Down
3 changes: 2 additions & 1 deletion primitives/runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8824a60

Please sign in to comment.