Skip to content

Commit

Permalink
Add profiling::function_scope!() and profiling::function_scope!(data)…
Browse files Browse the repository at this point in the history
… as alternatives for #[profiling::function] (#80)
  • Loading branch information
teddemunnik authored Oct 13, 2024
1 parent 7010df4 commit c37694c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 45 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Currently, there's just six macros:
* `profiling::finish_frame!()`
* Many profilers have the concept of a "frame" as a unit of work. Use this to indicate where one frame ends and the
next one begins.
* `profiling::function_scope!([tag: &str])`
* Macro that can be placed within a function to create a scope with the function name
* tag: optional extra data

Support for individual profilers can be turned on/off with feature flags. By default, they're all off, resulting in
no dependencies or runtime code.
Expand Down
6 changes: 6 additions & 0 deletions demo-puffin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn some_inner_function(_iteration_index: usize) {
burn_time(1);
}

fn some_macro_function(){
profiling::function_scope!();
burn_time(5);
}

//
// Example of multiple scopes in a single function
//
Expand Down Expand Up @@ -82,6 +87,7 @@ impl eframe::App for TemplateApp {
profiling::scope!("Main Thread");
some_function();
some_other_function(3);
some_macro_function();

Foo::function1();
Foo::function2();
Expand Down
52 changes: 7 additions & 45 deletions profiling-procmacros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,42 +105,19 @@ fn impl_block(
}
}

#[cfg(feature = "profile-with-puffin")]
fn impl_block(
body: &syn::Block,
_instrumented_function_name: &str,
) -> syn::Block {
parse_quote! {
{
profiling::puffin::profile_function!();

#body
}
}
}

#[cfg(feature = "profile-with-optick")]
#[cfg(any(
feature = "profile-with-puffin",
feature = "profile-with-optick",
feature = "profile-with-superluminal",
feature = "profile-with-tracy"
))]
fn impl_block(
body: &syn::Block,
_instrumented_function_name: &str,
) -> syn::Block {
parse_quote! {
{
profiling::optick::event!();

#body
}
}
}

#[cfg(feature = "profile-with-superluminal")]
fn impl_block(
body: &syn::Block,
instrumented_function_name: &str,
) -> syn::Block {
parse_quote! {
{
let _superluminal_guard = profiling::superluminal::SuperluminalGuard::new(#instrumented_function_name);
profiling::function_scope!();

#body
}
Expand All @@ -161,18 +138,3 @@ fn impl_block(
}
}
}

#[cfg(feature = "profile-with-tracy")]
fn impl_block(
body: &syn::Block,
_instrumented_function_name: &str,
) -> syn::Block {
parse_quote! {
{
// Note: callstack_depth is 0 since this has significant overhead
let _tracy_span = profiling::tracy_client::span!();

#body
}
}
}
16 changes: 16 additions & 0 deletions profiling/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ fn some_inner_function(_iteration_index: usize) {
burn_time(10);
}

fn function_scope_function() {
profiling::function_scope!();
burn_time(10);
}

fn function_scope_function_with_data(_iteration_index: usize) {
profiling::function_scope!(_iteration_index.to_string().as_str());
burn_time(5);
}

//
// Example of multiple scopes in a single function
//
Expand Down Expand Up @@ -171,6 +181,12 @@ fn main() {
Foo::function1();
Foo::function2();

for i in 0..10 {
function_scope_function();
function_scope_function_with_data(i);
burn_time(1);
}

println!("frame complete");

// Finish the frame.
Expand Down
18 changes: 18 additions & 0 deletions profiling/src/empty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ macro_rules! scope {
($name:expr, $data:expr) => {};
}

/// Opens a scope automatically named after the current function.
/// - profiling::function_scope!() - Opens a scope with the current function name
/// - profiling::function_scope!(data: &str) - Opens a scope with the current function name and an extra data field.
///
/// ```
/// fn function_a(){
/// profiling::function_scope!();
/// }
/// fn function_b(iteration: u32){
/// profiling::function_scope!(format!("iteration {}", iteration).as_str());
/// }
/// ```
#[macro_export]
macro_rules! function_scope {
() => {};
($data:expr) => {};
}

/// Registers a thread with the profiler API(s). This is usually setting a name for the thread.
/// Two variants:
/// - register_thread!() - Tries to get the name of the thread, or an ID if no name is set
Expand Down
11 changes: 11 additions & 0 deletions profiling/src/optick_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ macro_rules! scope {
};
}

#[macro_export]
macro_rules! function_scope {
() => {
$crate::optick::event!();
};
($data:expr) => {
$crate::optick::event!();
$crate::optick::tag!("tag", $data);
};
}

#[macro_export]
macro_rules! register_thread {
() => {
Expand Down
10 changes: 10 additions & 0 deletions profiling/src/puffin_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ macro_rules! scope {
};
}

#[macro_export]
macro_rules! function_scope {
() => {
$crate::puffin::profile_function!();
};
($data:expr) => {
$crate::puffin::profile_function!($data);
};
}

#[macro_export]
macro_rules! register_thread {
() => {};
Expand Down
20 changes: 20 additions & 0 deletions profiling/src/superluminal_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ macro_rules! scope {
};
}

#[macro_export]
macro_rules! function_scope {
() => {
let function_name = {
struct S;
let type_name = core::any::type_name::<S>();
&type_name[..type_name.len() - 3]
};
$crate::scope!(function_name);
};
($data:expr) => {
let function_name = {
struct S;
let type_name = core::any::type_name::<S>();
&type_name[..type_name.len() - 3]
};
$crate::scope!(function_name, $data);
};
}

#[macro_export]
macro_rules! register_thread {
() => {
Expand Down
33 changes: 33 additions & 0 deletions profiling/src/tracing_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ macro_rules! scope {
};
}

// NOTE: Not supported as tracing does not support non literal spans. Use #[profiling::function] instead.
#[macro_export]
macro_rules! function_scope {
() => {
let function_name = {
struct S;
let type_name = core::any::type_name::<S>();
&type_name[..type_name.len() - 3]
};
let span = $crate::tracing::span!(
$crate::tracing::Level::INFO,
"function_scope",
"{}",
function_name
);
let _span_entered = span.enter();
};
($data:expr) => {
let function_name = {
struct S;
let type_name = core::any::type_name::<S>();
&type_name[..type_name.len() - 3]
};
let span = $crate::tracing::span!(
$crate::tracing::Level::INFO,
"function_scope",
tag = $data,
"{}",
function_name
);
};
}

#[macro_export]
macro_rules! register_thread {
() => {};
Expand Down
14 changes: 14 additions & 0 deletions profiling/src/tracy_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ macro_rules! scope {
};
}

#[macro_export]
macro_rules! function_scope {
() => {
$crate::tracy_client::span!();
};
($data:expr) => {
let location = $crate::tracy_client::span_location!();
let tracy_span = $crate::tracy_client::Client::running()
.expect("function_scope! without a running tracy_client::Client")
.span(location, 0);
tracy_span.emit_text($data);
};
}

/// Registers a thread with the profiler API(s). This is usually setting a name for the thread.
/// Two variants:
/// - register_thread!() - Tries to get the name of the thread, or an ID if no name is set
Expand Down
8 changes: 8 additions & 0 deletions profiling/src/type_check_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ macro_rules! scope {
};
}

#[macro_export]
macro_rules! function_scope {
() => {};
($data:expr) => {
let _: &str = $data;
};
}

#[macro_export]
macro_rules! register_thread {
() => {};
Expand Down

0 comments on commit c37694c

Please sign in to comment.