Skip to main content

Profiler

Trait Profiler 

Source
pub trait Profiler: Send + Sync {
    // Required methods
    fn enabled(&self, target: &str) -> bool;
    fn enter(&self, data: &SpanData) -> SpanId;
    fn exit(&self, id: SpanId, elapsed_ns: u64);
    fn counter(&self, name: &'static str, value: u64);
    fn histogram(&self, name: &'static str, value_us: u64);
}
Expand description

Profiler trait - kernel defines mechanism, drivers implement policy.

Following the Logger pattern: the kernel provides the trait interface, and drivers (e.g., shared/trace/) implement it with the tracing ecosystem.

§Thread Safety

Implementations must be Send + Sync as the profiler is accessed from multiple threads concurrently. Implementations should minimize locking to avoid blocking hot paths.

§Example

use reovim_kernel::api::v1::*;

struct MyProfiler;

impl Profiler for MyProfiler {
    fn enabled(&self, _target: &str) -> bool {
        true // Always enabled
    }

    fn enter(&self, data: &SpanData) -> SpanId {
        println!("Entering span: {}", data.name);
        data.id
    }

    fn exit(&self, _id: SpanId, elapsed_ns: u64) {
        println!("Exiting span, elapsed: {}ns", elapsed_ns);
    }

    fn counter(&self, name: &'static str, value: u64) {
        println!("Counter {}: {}", name, value);
    }

    fn histogram(&self, name: &'static str, value_us: u64) {
        println!("Histogram {}: {}us", name, value_us);
    }
}

Required Methods§

Source

fn enabled(&self, target: &str) -> bool

Check if profiling is enabled for the given target.

Called before creating a span to avoid allocation overhead. If this returns false, ProfileScope will be a no-op.

§Arguments
  • target - The module/target path (e.g., runner::input)
Source

fn enter(&self, data: &SpanData) -> SpanId

Enter a new span.

Called when a profiling scope begins. The driver should record the span and return the span ID for later correlation.

§Arguments
  • data - Span metadata including name, target, and timestamps
§Returns

The span ID to use for exit() (usually data.id).

Source

fn exit(&self, id: SpanId, elapsed_ns: u64)

Exit a span with timing information.

Called when a profiling scope ends. The driver should record the elapsed time and close the span.

§Arguments
  • id - The span ID returned from enter()
  • elapsed_ns - Elapsed time in nanoseconds
Source

fn counter(&self, name: &'static str, value: u64)

Record a counter increment.

Used for counting events like keys processed, commands executed, etc.

§Arguments
  • name - Counter name (static for zero allocation)
  • value - Value to add (typically 1)
Source

fn histogram(&self, name: &'static str, value_us: u64)

Record a histogram sample.

Used for timing distributions (e.g., command execution latency).

§Arguments
  • name - Histogram name (static for zero allocation)
  • value_us - Sample value in microseconds

Implementors§