1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//! # 🚄 Profiler API
//!
//! This provides bindings between the Puffin Profiler library and Ark.
//!
//! ## Usage
//!
//! In `Cargo.toml`:
//! * Add `"profiler"` to the `ark` `features` list.
//! * Add `puffin = "0.4"`
//!
//! 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 `puffin::profile_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 `puffin::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()
}
/// 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());
};
}
