ark-api 0.17.0-pre.15

Ark API
Documentation
//! # 🚄 Profiler API
//!
//! This provides bindings between the Puffin Profiler library and Ark.
//!
//! ## Usage
//!
//! In `Cargo.toml`:
//! * Add `"profiler"` to the `ark` `features` list.
//!
//! In your `lib.rs` you need a call `require_profiler_api();`.
//! To enable profiling you need to call `ark::setup_profiler!();` from your
//! module entry point.
//!
//! Now you can add profile scopes with `ark::profiler::function!();`
//! in functions you want to profile.
//!
//! You should now be able to see your profile scopes in the ark-client profiler window.
//!
//! It is also recommended that you call `ark::profiler::set_scopes_on(ark::profiler::is_active());`
//! on each frame update, to allow `ark-client` to switch the module profiler on/off.

use crate::ffi::profiler_v0 as ffi;

#[doc(hidden)]
pub use ffi::API as FFI_API;

/// Current time in nanoseconds, from some unspecified event.
///
/// Used for high precision profiling and is only supported for profiling. May return 0 if profiling is not enabled.
/// Can't be used for absolute time determination, only relative difference between calls.
#[inline]
pub fn now_ns() -> i64 {
    ffi::now_ns()
}

/// Report Puffin profiler event stream
///
/// Used by the puffin profiler running in user space to report
/// profiler events to the host profiler instance.
#[inline]
pub fn report_thread_stream_info(stream_info: &puffin::StreamInfoRef<'_>) {
    let puffin::StreamInfoRef {
        stream,
        num_scopes,
        depth,
        range_ns: (range_ns_min, range_ns_max),
    } = stream_info;
    let stream_meta = ffi::StreamMeta {
        num_scopes: *num_scopes as _,
        depth: *depth as _,
        range_ns_min: *range_ns_min,
        range_ns_max: *range_ns_max,
    };
    ffi::report_thread_stream_info(&stream_meta, stream);
}

/// Is the module-side profiler on?
///
/// Call `puffin::set_scopes_on(ark::profiler::is_active());`
/// at the top of each frame to heed this.
#[inline]
pub fn is_active() -> bool {
    ffi::is_active()
}

// re-export key profiling macros from puffin
//
// this makes it easier for the user as they do not have to depend on the
// puffin crate themselves and match the right version.
pub use puffin::profile_function as function;
pub use puffin::profile_scope as scope;
pub use puffin::set_scopes_on;

/// Call at least once from your Module setup code to enable profiling
/// using the `puffin` profiler.
#[macro_export]
macro_rules! setup_profiler {
    () => {
        $crate::__puffin::ThreadProfiler::initialize(
            $crate::profiler::now_ns,
            |_thread_info, stream_info| $crate::profiler::report_thread_stream_info(stream_info),
        );
        $crate::__puffin::set_scopes_on($crate::profiler::is_active());
    };
}