|
| 1 | +//! FFI shim for installing the `tracing-texray` subscriber from Lean. |
| 2 | +//! |
| 3 | +//! With `streaming = true`, each tracked span (e.g. `aiur/execute`, |
| 4 | +//! `aiur/witness`, `aiur/prove`) emits a one-line `[texray] <name>: <dur> |
| 5 | +//! ── RAM Δ +X peak Y` to stderr as it closes, and the full texray |
| 6 | +//! graph prints when the examined root span exits. |
| 7 | +
|
| 8 | +use lean_ffi::object::{LeanBorrowed, LeanIOResult, LeanOwned, LeanString}; |
| 9 | +use tracing_subscriber::{ |
| 10 | + Layer, Registry, filter::FilterFn, layer::SubscriberExt, |
| 11 | + util::SubscriberInitExt, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Install the `tracing-texray` subscriber as the global default. |
| 15 | +/// |
| 16 | +/// Idempotent: subsequent calls are no-ops once the global subscriber has |
| 17 | +/// been set. |
| 18 | +/// |
| 19 | +/// Parameters: |
| 20 | +/// - `name_prefixes`: comma-separated list of span-name prefixes to render |
| 21 | +/// (e.g. `"aiur/,stark/"`). Empty string disables filtering and renders |
| 22 | +/// everything, including upstream library spans. |
| 23 | +/// - `track_ram`: sample VmRSS/VmHWM per span. Linux-only; zeros elsewhere. |
| 24 | +/// - `streaming`: emit per-span `[texray] <name>: <dur> ── RAM Δ +X peak Y` |
| 25 | +/// lines on stderr as each span closes. The texray graph prints either |
| 26 | +/// way when the examined root span exits. |
| 27 | +#[unsafe(no_mangle)] |
| 28 | +extern "C" fn rs_texray_init( |
| 29 | + name_prefixes: LeanString<LeanBorrowed<'_>>, |
| 30 | + track_ram: bool, |
| 31 | + streaming: bool, |
| 32 | +) -> LeanIOResult<LeanOwned> { |
| 33 | + let prefixes: Vec<String> = name_prefixes |
| 34 | + .to_string() |
| 35 | + .split(',') |
| 36 | + .map(|s| s.trim().to_string()) |
| 37 | + .filter(|s| !s.is_empty()) |
| 38 | + .collect(); |
| 39 | + |
| 40 | + let mut layer = tracing_texray::TeXRayLayer::new(); |
| 41 | + if track_ram { |
| 42 | + layer = layer.track_ram(); |
| 43 | + } |
| 44 | + if streaming { |
| 45 | + layer = layer.streaming(); |
| 46 | + } |
| 47 | + |
| 48 | + let filter = FilterFn::new(move |metadata| { |
| 49 | + if !metadata.is_span() || prefixes.is_empty() { |
| 50 | + return true; |
| 51 | + } |
| 52 | + let name = metadata.name(); |
| 53 | + prefixes.iter().any(|p| name.starts_with(p.as_str())) |
| 54 | + }); |
| 55 | + |
| 56 | + let _ = Registry::default().with(layer.with_filter(filter)).try_init(); |
| 57 | + LeanIOResult::ok(LeanOwned::box_usize(0)) |
| 58 | +} |
0 commit comments