tracing_perfetto_sdk_sys/lib.rs
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
//! # `tracing-perfetto-sdk-sys`: C++ bindings to the raw Perfetto SDK
//!
//! This crate only contains low-level bindings to the C++ library. While the
//! interface is safe, it is recommended to use a higher level API, for example
//! via the `tracing-perfetto-sdk-layer` crate.
#![deny(clippy::all)]
use std::sync::mpsc;
#[cfg(feature = "async")]
use futures::channel::oneshot;
/// FFI bridge: Definitions of functions and types that are shared across the
/// C++ boundary.
#[cxx::bridge]
pub mod ffi {
pub enum LogLev {
Debug = 0,
Info = 1,
Important = 2,
Error = 3,
}
pub struct DebugStringAnnotation {
pub key: &'static str,
pub value: String,
}
pub struct DebugBoolAnnotation {
pub key: &'static str,
pub value: bool,
}
pub struct DebugIntAnnotation {
pub key: &'static str,
pub value: i64,
}
pub struct DebugDoubleAnnotation {
pub key: &'static str,
pub value: f64,
}
pub struct DebugAnnotations<'a> {
pub strings: &'a [DebugStringAnnotation],
pub bools: &'a [DebugBoolAnnotation],
pub ints: &'a [DebugIntAnnotation],
pub doubles: &'a [DebugDoubleAnnotation],
}
extern "Rust" {
// Opaque type passed to C++ code to be sent back during callbacks; essentially
// like `void *context` but type-safe
type PollTracesCtx;
type FlushCtx;
}
unsafe extern "C++" {
include!("src/perfetto-bindings.h");
/// Initialize the global tracing infrastructure.
///
/// Must be called once before all other functions in this module.
fn perfetto_global_init(
log_callback: fn(level: LogLev, line: i32, filename: &str, message: &str),
enable_in_process_backend: bool,
enable_system_backend: bool,
);
/// The native C++ class that is safe to be passed across the C++/Rust
/// boundary via a `unique_ptr`.
type PerfettoTracingSession;
/// Create a new tracing session using the provided Protobuf-encoded
/// TraceConfig.
///
/// If `output_fd` is non-negative, we will write traces directly to the
/// provided file descriptor, in which case `poll_traces` will never
/// return any data.
fn new_tracing_session(
trace_config_bytes: &[u8],
output_fd: i32,
) -> Result<UniquePtr<PerfettoTracingSession>>;
/// Create a trace event signifying that a new slice (aka span) begins.
///
/// Make sure to call this in the hot path instead of delaying the call
/// to some background queue, etc., as the call will collect additional
/// timing data at the time of the actual call.
///
/// This call is very cheap and will be a no-op if trace collection is
/// not yet started/enabled.
fn trace_track_event_slice_begin<'a>(
track_uuid: u64,
name: &str,
location_file: &str,
location_line: u32,
debug_annotations: &DebugAnnotations<'a>,
);
/// Create a trace event signifying that a slice (aka span) ends.
///
/// Make sure to call this in the hot path instead of delaying the call
/// to some background queue, etc., as the call will collect additional
/// timing data at the time of the actual call.
///
/// This call is very cheap and will be a no-op if trace collection is
/// not yet started/enabled.
fn trace_track_event_slice_end(
track_uuid: u64,
name: &str,
location_file: &str,
location_line: u32,
);
/// Create a trace event signifying that an instant event has happened,
/// similar to a log message or something else that takes ~zero time.
///
/// Make sure to call this in the hot path instead of delaying the call
/// to some background queue, etc., as the call will collect additional
/// timing data at the time of the actual call.
///
/// This call is very cheap and will be a no-op if trace collection is
/// not yet started/enabled.
fn trace_track_event_instant<'a>(
track_uuid: u64,
name: &str,
location_file: &str,
location_line: u32,
debug_annotations: &DebugAnnotations<'a>,
);
/// Create a track descriptor for a process.
///
/// Make sure to call this in the hot path instead of delaying the call
/// to some background queue, etc., as the call will collect additional
/// timing data at the time of the actual call.
///
/// This call is very cheap and will be a no-op if trace collection is
/// not yet started/enabled.
fn trace_track_descriptor_process(
parent_uuid: u64,
track_uuid: u64,
process_name: &str,
process_pid: u32,
);
/// Create a track descriptor for a thread.
///
/// Make sure to call this in the hot path instead of delaying the call
/// to some background queue, etc., as the call will collect additional
/// timing data at the time of the actual call.
///
/// This call is very cheap and will be a no-op if trace collection is
/// not yet started/enabled.
fn trace_track_descriptor_thread(
parent_uuid: u64,
track_uuid: u64,
process_pid: u32,
thread_name: &str,
thread_tid: u32,
);
/// Get the current trace time according to Perfetto's managed monotonic
/// clock(s).
fn trace_time_ns() -> u64;
/// Get the id of the clock that is used by `trace_time_ns`.
///
/// Valid values correspond to enum variants of the `BuiltinClock`
/// protobuf enum.
fn trace_clock_id() -> u32;
/// Start collecting traces from all data sources.
fn start(self: Pin<&mut PerfettoTracingSession>);
/// Stop collecting traces from all data sources.
fn stop(self: Pin<&mut PerfettoTracingSession>);
/// Flush buffered traces.
///
/// The passed-in callback is called with `true` on success; `false`
/// indicates that some data source didn't ack before the
/// timeout, or because something else went wrong (e.g. tracing
/// system wasn't initialized).
fn flush(
self: Pin<&mut PerfettoTracingSession>,
timeout_ms: u32,
ctx: Box<FlushCtx>,
done: fn(ctx: Box<FlushCtx>, success: bool),
);
/// Poll for new traces, and call the provided `done` callback with new
/// trace records.
///
/// Polling will only return data if an `output_fd` was *not* specified
/// when creating the session.
///
/// If there are no more records, the callback will be called with an
/// empty slice and `has_more == true` as a special case to signal EOF.
///
/// The data in the callback buffer is guaranteed to consist of complete
/// trace records; in other words, there will not be any partial records
/// that cross buffer boundaries.
fn poll_traces(
self: Pin<&mut PerfettoTracingSession>,
ctx: Box<PollTracesCtx>,
done: fn(ctx: Box<PollTracesCtx>, data: &[u8], has_more: bool),
);
}
}
// Safe to use session from all threads
unsafe impl Send for ffi::PerfettoTracingSession {}
unsafe impl Sync for ffi::PerfettoTracingSession {}
/// A context that will be passed-in in a call to [`ffi::poll_traces`] and later
/// passed back in the `done` callback when the async operation has completed.
pub struct PollTracesCtx(CallbackSender<PolledTraces>);
/// A context that will be passed-in in a call to [`ffi::flush`] and later
/// passed back in the `done` callback when the async operation has completed.
pub struct FlushCtx(CallbackSender<bool>);
/// Traces returned from `poll_traces`/the channel returned by
/// `PollTracesCtx::new`.
pub struct PolledTraces {
pub data: bytes::BytesMut,
pub has_more: bool,
}
enum CallbackSender<A> {
Sync(mpsc::Sender<A>),
#[cfg(feature = "async")]
Async(Option<oneshot::Sender<A>>),
}
/// A context to be passed into `poll_traces`.
///
/// Intended to be called like:
///
/// ```no_run
/// # use std::pin::Pin;
/// # use tracing_perfetto_sdk_sys::ffi::PerfettoTracingSession;
/// # use tracing_perfetto_sdk_sys::{PollTracesCtx, PolledTraces};
/// let session: Pin<&mut PerfettoTracingSession> = todo!();
/// let (ctx, rx) = PollTracesCtx::new_sync();
/// session.poll_traces(Box::new(ctx), PollTracesCtx::callback);
/// let polled_traces: PolledTraces = rx.recv().unwrap(); // Should return polled traces
/// ```
impl PollTracesCtx {
pub fn new_sync() -> (Self, mpsc::Receiver<PolledTraces>) {
let (tx, rx) = mpsc::channel();
let tx = CallbackSender::Sync(tx);
(Self(tx), rx)
}
#[cfg(feature = "async")]
pub fn new_async() -> (Self, oneshot::Receiver<PolledTraces>) {
let (tx, rx) = oneshot::channel();
let tx = CallbackSender::Async(Some(tx));
(Self(tx), rx)
}
#[allow(clippy::boxed_local)]
pub fn callback(mut self: Box<Self>, data: &[u8], has_more: bool) {
let data = bytes::BytesMut::from(data);
self.0.send(PolledTraces { data, has_more });
}
}
/// A context to be passed into `flush`.
///
/// Intended to be called like:
///
/// ```no_run
/// # use std::pin::Pin;
/// # use tracing_perfetto_sdk_sys::ffi::PerfettoTracingSession;
/// # use tracing_perfetto_sdk_sys::FlushCtx;
/// let session: Pin<&mut PerfettoTracingSession> = todo!();
/// let (ctx, rx) = FlushCtx::new_sync();
/// let timeout_ms = 100;
/// session.flush(timeout_ms, Box::new(ctx), FlushCtx::callback);
/// let success: bool = rx.recv().unwrap();
/// ```
impl FlushCtx {
pub fn new_sync() -> (Self, mpsc::Receiver<bool>) {
let (tx, rx) = mpsc::channel();
let tx = CallbackSender::Sync(tx);
(Self(tx), rx)
}
#[cfg(feature = "async")]
pub fn new_async() -> (Self, oneshot::Receiver<bool>) {
let (tx, rx) = oneshot::channel();
let tx = CallbackSender::Async(Some(tx));
(Self(tx), rx)
}
#[allow(clippy::boxed_local)]
pub fn callback(mut self: Box<Self>, success: bool) {
self.0.send(success);
}
}
impl<A> CallbackSender<A> {
fn send(&mut self, value: A) {
match self {
CallbackSender::Sync(ref mut tx) => {
let _ = tx.send(value);
}
#[cfg(feature = "async")]
CallbackSender::Async(ref mut tx) => {
if let Some(tx) = tx.take() {
let _ = tx.send(value);
}
}
}
}
}