use torvyn_types::{ComponentId, SpanId};
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct CompactSpanRecord {
pub span_id: SpanId,
pub parent_span_id: SpanId,
pub component_id: ComponentId,
pub start_ns: u64,
pub end_ns: u64,
pub status_code: u8,
pub element_sequence: u64,
}
impl CompactSpanRecord {
pub const fn zeroed() -> Self {
Self {
span_id: SpanId::invalid(),
parent_span_id: SpanId::invalid(),
component_id: ComponentId::new(0),
start_ns: 0,
end_ns: 0,
status_code: 0,
element_sequence: 0,
}
}
#[inline]
pub fn duration_ns(&self) -> u64 {
self.end_ns.saturating_sub(self.start_ns)
}
#[inline]
pub fn is_error(&self) -> bool {
self.status_code == 1
}
}
pub struct SpanRingBuffer {
slots: Box<[CompactSpanRecord]>,
write_pos: u64,
mask: u64,
capacity: usize,
}
impl SpanRingBuffer {
pub fn new(capacity: usize) -> Self {
debug_assert!(capacity.is_power_of_two() && capacity >= 8);
let slots = vec![CompactSpanRecord::zeroed(); capacity].into_boxed_slice();
Self {
slots,
write_pos: 0,
mask: (capacity as u64) - 1,
capacity,
}
}
#[inline]
pub fn push(&mut self, record: CompactSpanRecord) {
let idx = (self.write_pos & self.mask) as usize;
self.slots[idx] = record;
self.write_pos = self.write_pos.wrapping_add(1);
}
pub fn drain(&mut self) -> Vec<CompactSpanRecord> {
let total = self.write_pos.min(self.capacity as u64) as usize;
let mut result = Vec::with_capacity(total);
if self.write_pos <= self.capacity as u64 {
for i in 0..total {
result.push(self.slots[i]);
}
} else {
let start = (self.write_pos & self.mask) as usize;
for i in 0..self.capacity {
let idx = (start + i) % self.capacity;
result.push(self.slots[idx]);
}
}
self.write_pos = 0;
result
}
#[inline]
pub fn len(&self) -> usize {
self.write_pos.min(self.capacity as u64) as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.write_pos == 0
}
#[inline]
pub fn has_wrapped(&self) -> bool {
self.write_pos >= self.capacity as u64
}
#[inline]
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn contains_error(&self) -> bool {
let total = self.len();
for i in 0..total {
let idx = if self.has_wrapped() {
((self.write_pos & self.mask) as usize + i) % self.capacity
} else {
i
};
if self.slots[idx].is_error() {
return true;
}
}
false
}
}
impl std::fmt::Debug for SpanRingBuffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SpanRingBuffer")
.field("capacity", &self.capacity)
.field("len", &self.len())
.field("write_pos", &self.write_pos)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_record(seq: u64, start: u64, end: u64) -> CompactSpanRecord {
CompactSpanRecord {
span_id: SpanId::new([seq as u8; 8]),
parent_span_id: SpanId::invalid(),
component_id: ComponentId::new(1),
start_ns: start,
end_ns: end,
status_code: 0,
element_sequence: seq,
}
}
fn make_error_record(seq: u64) -> CompactSpanRecord {
let mut r = make_record(seq, 0, 100);
r.status_code = 1;
r
}
#[test]
fn test_ring_buffer_new() {
let rb = SpanRingBuffer::new(16);
assert_eq!(rb.capacity(), 16);
assert_eq!(rb.len(), 0);
assert!(rb.is_empty());
}
#[test]
fn test_ring_buffer_push_and_len() {
let mut rb = SpanRingBuffer::new(8);
rb.push(make_record(1, 0, 100));
assert_eq!(rb.len(), 1);
rb.push(make_record(2, 100, 200));
assert_eq!(rb.len(), 2);
}
#[test]
fn test_ring_buffer_drain_no_wrap() {
let mut rb = SpanRingBuffer::new(8);
for i in 0..5 {
rb.push(make_record(i, i * 100, (i + 1) * 100));
}
let records = rb.drain();
assert_eq!(records.len(), 5);
assert_eq!(records[0].element_sequence, 0);
assert_eq!(records[4].element_sequence, 4);
assert!(rb.is_empty());
}
#[test]
fn test_ring_buffer_wrap_and_drain() {
let mut rb = SpanRingBuffer::new(8);
for i in 0..12 {
rb.push(make_record(i, i * 100, (i + 1) * 100));
}
assert!(rb.has_wrapped());
assert_eq!(rb.len(), 8);
let records = rb.drain();
assert_eq!(records.len(), 8);
assert_eq!(records[0].element_sequence, 4);
assert_eq!(records[7].element_sequence, 11);
}
#[test]
fn test_ring_buffer_contains_error() {
let mut rb = SpanRingBuffer::new(8);
rb.push(make_record(0, 0, 100));
assert!(!rb.contains_error());
rb.push(make_error_record(1));
assert!(rb.contains_error());
}
#[test]
fn test_compact_span_record_duration() {
let r = make_record(0, 100, 350);
assert_eq!(r.duration_ns(), 250);
}
#[test]
fn test_ring_buffer_drain_resets() {
let mut rb = SpanRingBuffer::new(8);
rb.push(make_record(0, 0, 100));
let _ = rb.drain();
assert!(rb.is_empty());
assert_eq!(rb.len(), 0);
}
}