|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::{cell::RefCell, env, path::PathBuf}; |
|
|
|
|
|
use anyhow::anyhow; |
|
|
use napi::bindgen_prelude::{External, Status}; |
|
|
use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; |
|
|
use tracing_subscriber::{Layer, filter, layer::SubscriberExt, util::SubscriberInitExt}; |
|
|
|
|
|
#[napi] |
|
|
pub fn get_target_triple() -> &'static str { |
|
|
env!("VERGEN_CARGO_TARGET_TRIPLE") |
|
|
} |
|
|
|
|
|
pub trait MapErr<T>: Into<Result<T, anyhow::Error>> { |
|
|
fn convert_err(self) -> napi::Result<T> { |
|
|
self.into() |
|
|
.map_err(|err| napi::Error::new(Status::GenericFailure, format!("{err:?}"))) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> MapErr<T> for Result<T, anyhow::Error> {} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc"))] |
|
|
#[non_exhaustive] |
|
|
pub struct DhatProfilerGuard(dhat::Profiler); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc")))] |
|
|
#[non_exhaustive] |
|
|
pub struct DhatProfilerGuard; |
|
|
|
|
|
impl DhatProfilerGuard { |
|
|
|
|
|
pub fn try_init() -> Option<Self> { |
|
|
#[cfg(feature = "__internal_dhat-heap")] |
|
|
{ |
|
|
println!("[dhat-heap]: Initializing heap profiler"); |
|
|
Some(Self(dhat::Profiler::new_heap())) |
|
|
} |
|
|
#[cfg(feature = "__internal_dhat-ad-hoc")] |
|
|
{ |
|
|
println!("[dhat-ad-hoc]: Initializing ad-hoc profiler"); |
|
|
Some(Self(dhat::Profiler::new_ad_hoc())) |
|
|
} |
|
|
#[cfg(not(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc")))] |
|
|
{ |
|
|
None |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl Drop for DhatProfilerGuard { |
|
|
fn drop(&mut self) { |
|
|
#[cfg(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc"))] |
|
|
println!("[dhat]: Teardown profiler"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[napi] |
|
|
pub fn init_custom_trace_subscriber( |
|
|
trace_out_file_path: Option<String>, |
|
|
) -> napi::Result<External<RefCell<Option<FlushGuard>>>> { |
|
|
let trace_out_file_path = trace_out_file_path.map(PathBuf::from); |
|
|
|
|
|
let mut layer = ChromeLayerBuilder::new().include_args(true); |
|
|
if let Some(trace_out_file) = trace_out_file_path { |
|
|
let dir = trace_out_file |
|
|
.parent() |
|
|
.ok_or_else(|| anyhow!("Not able to find path to the trace output")) |
|
|
.convert_err()?; |
|
|
std::fs::create_dir_all(dir)?; |
|
|
|
|
|
layer = layer.file(trace_out_file); |
|
|
} |
|
|
|
|
|
let (chrome_layer, guard) = layer.build(); |
|
|
tracing_subscriber::registry() |
|
|
.with(chrome_layer.with_filter(filter::filter_fn(|metadata| { |
|
|
!metadata.target().contains("cranelift") && !metadata.name().contains("log ") |
|
|
}))) |
|
|
.try_init() |
|
|
.expect("Failed to register tracing subscriber"); |
|
|
|
|
|
let guard_cell = RefCell::new(Some(guard)); |
|
|
Ok(External::new(guard_cell)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[napi] |
|
|
pub fn teardown_trace_subscriber(guard_external: External<RefCell<Option<FlushGuard>>>) { |
|
|
let guard_cell = &*guard_external; |
|
|
|
|
|
if let Some(guard) = guard_cell.take() { |
|
|
drop(guard); |
|
|
} |
|
|
} |
|
|
|