Skip to content

Phase 5.2: Rust ProfilingSession + safe streaming API - #20

Merged
jayakasadev merged 1 commit into
mainfrom
feature/phase-5-2-rust-streaming
Jun 11, 2026
Merged

jayakasadev merged 1 commit into
mainfrom
feature/phase-5-2-rust-streaming

Conversation

@jayakasadev

Copy link
Copy Markdown
Owner

Summary

Safe Rust wrapper over Phase 5.1's sn_rust_profile_streaming_start/stop C exports.

  • ProfilingSession — RAII handle. start<F: Fn(StreamSample<'_>) + Send + Sync + 'static>(handler) -> Result<Self, StreamingError>; Drop unregisters.
  • StreamSample<'a> — zero-copy view over SnRustProfileRawSample; accessors: alloc_ptr(), requested_size(), allocated_size(), weight(), stack().
  • StreamingError — AlreadyActive | RegistrationFailed.
  • Single-session-at-a-time enforced via process-global Mutex<Option<Handler>>; second start() returns Err(AlreadyActive).
  • Trampoline wraps user closure in std::panic::catch_unwind (UB to unwind across extern \"C\").
  • ProfilingSession is !Send + !Sync (Drop runs on constructing thread). Handler itself is Send + Sync.
  • Poisoned-mutex recovery in start(); Drop leaves a poisoned mutex untouched (one-shot bounded leak; recovers next start).

Test plan

  • profile_streaming::smoke_session_receives_samples
  • profile_streaming::double_start_errors_then_recovers
  • profile_streaming::drop_unregisters_handler
  • profile_streaming::thread_safety_concurrent_workload
  • cargo build (no features + --features profiling)
  • Full cargo test suite (no features + --features profiling)

- Add snmalloc-sys extern "C" decls for sn_rust_profile_streaming_start
  / sn_rust_profile_streaming_stop, gated on the `profiling` feature.
- Introduce `snmalloc-rs::streaming` exposing `ProfilingSession`
  (RAII handle) plus a borrowed `StreamSample<'_>` view of the raw
  FFI sample.  Single-session-at-a-time semantics enforced through a
  process-global `Mutex<Option<Handler>>`; second `start()` returns
  `StreamingError::AlreadyActive`.
- Trampoline is a fixed `extern "C"` function that locks the slot,
  dispatches into the boxed `Fn` and catches panics so unwinds never
  cross the FFI boundary.  Handler bounds are `Send + Sync + 'static`.
- Drop unregisters from the C side, then clears the slot so a fresh
  `ProfilingSession::start` can succeed.
- Re-export `ProfilingSession`, `StreamSample`, `StreamingError`
  from the crate root under `#[cfg(feature = "profiling")]`.
- Add `tests/profile_streaming.rs` covering: smoke handler-invocation,
  double-start AlreadyActive recovery, drop-unregisters guarantee,
  and thread-safety under a concurrent allocator workload.
@jayakasadev
jayakasadev merged commit b5c6018 into main Jun 11, 2026
155 of 202 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant