|
| 1 | +//! Shared atomic counter state for buffer introspection (`metrics` feature). |
| 2 | +//! |
| 3 | +//! Embedded in adapter buffers (`TokioBuffer`, `EmbassyBuffer`) so the same |
| 4 | +//! `produced`/`consumed`/`dropped` accounting is reused across runtimes. |
| 5 | +//! `portable-atomic` keeps this `no_std` and works on targets without native |
| 6 | +//! 64-bit atomics (e.g. `thumbv7em`) when the |
| 7 | +//! `portable-atomic/{fallback,critical-section}` features are enabled. |
| 8 | +
|
| 9 | +use core::sync::atomic::Ordering; |
| 10 | +use portable_atomic::AtomicU64; |
| 11 | + |
| 12 | +use super::BufferMetricsSnapshot; |
| 13 | + |
| 14 | +/// Atomic counters shared between a buffer and its readers. |
| 15 | +/// |
| 16 | +/// Updated with `Ordering::Relaxed` — these are diagnostics, not synchronization |
| 17 | +/// primitives. |
| 18 | +#[derive(Debug)] |
| 19 | +pub struct BufferCounters { |
| 20 | + produced: AtomicU64, |
| 21 | + consumed: AtomicU64, |
| 22 | + dropped: AtomicU64, |
| 23 | + capacity: usize, |
| 24 | +} |
| 25 | + |
| 26 | +impl BufferCounters { |
| 27 | + /// Creates new counters, remembering the buffer's declared capacity for |
| 28 | + /// inclusion in [`Self::snapshot`] occupancy tuples. |
| 29 | + pub fn new(capacity: usize) -> Self { |
| 30 | + Self { |
| 31 | + produced: AtomicU64::new(0), |
| 32 | + consumed: AtomicU64::new(0), |
| 33 | + dropped: AtomicU64::new(0), |
| 34 | + capacity, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Increments the `produced` counter (call on each successful push). |
| 39 | + pub fn increment_produced(&self) { |
| 40 | + self.produced.fetch_add(1, Ordering::Relaxed); |
| 41 | + } |
| 42 | + |
| 43 | + /// Increments the `consumed` counter (call on each successful recv). |
| 44 | + pub fn increment_consumed(&self) { |
| 45 | + self.consumed.fetch_add(1, Ordering::Relaxed); |
| 46 | + } |
| 47 | + |
| 48 | + /// Adds `count` to the `dropped` counter (call on lag/overflow detection). |
| 49 | + pub fn add_dropped(&self, count: u64) { |
| 50 | + self.dropped.fetch_add(count, Ordering::Relaxed); |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns the declared buffer capacity passed to [`Self::new`]. |
| 54 | + pub fn capacity(&self) -> usize { |
| 55 | + self.capacity |
| 56 | + } |
| 57 | + |
| 58 | + /// Snapshots the current counter values, attaching the supplied |
| 59 | + /// `(current_items, capacity)` occupancy tuple. |
| 60 | + pub fn snapshot(&self, occupancy: (usize, usize)) -> BufferMetricsSnapshot { |
| 61 | + BufferMetricsSnapshot { |
| 62 | + produced_count: self.produced.load(Ordering::Relaxed), |
| 63 | + consumed_count: self.consumed.load(Ordering::Relaxed), |
| 64 | + dropped_count: self.dropped.load(Ordering::Relaxed), |
| 65 | + occupancy, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Zeroes all counters. Capacity is preserved. |
| 70 | + pub fn reset(&self) { |
| 71 | + self.produced.store(0, Ordering::Relaxed); |
| 72 | + self.consumed.store(0, Ordering::Relaxed); |
| 73 | + self.dropped.store(0, Ordering::Relaxed); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn zero_state() { |
| 83 | + let c = BufferCounters::new(16); |
| 84 | + let snap = c.snapshot((0, 16)); |
| 85 | + assert_eq!(snap.produced_count, 0); |
| 86 | + assert_eq!(snap.consumed_count, 0); |
| 87 | + assert_eq!(snap.dropped_count, 0); |
| 88 | + assert_eq!(snap.occupancy, (0, 16)); |
| 89 | + assert_eq!(c.capacity(), 16); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn record_one_of_each() { |
| 94 | + let c = BufferCounters::new(4); |
| 95 | + c.increment_produced(); |
| 96 | + c.increment_consumed(); |
| 97 | + c.add_dropped(3); |
| 98 | + let snap = c.snapshot((1, 4)); |
| 99 | + assert_eq!(snap.produced_count, 1); |
| 100 | + assert_eq!(snap.consumed_count, 1); |
| 101 | + assert_eq!(snap.dropped_count, 3); |
| 102 | + assert_eq!(snap.occupancy, (1, 4)); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn record_many() { |
| 107 | + let c = BufferCounters::new(8); |
| 108 | + for _ in 0..5 { |
| 109 | + c.increment_produced(); |
| 110 | + } |
| 111 | + for _ in 0..3 { |
| 112 | + c.increment_consumed(); |
| 113 | + } |
| 114 | + c.add_dropped(2); |
| 115 | + c.add_dropped(5); |
| 116 | + let snap = c.snapshot((2, 8)); |
| 117 | + assert_eq!(snap.produced_count, 5); |
| 118 | + assert_eq!(snap.consumed_count, 3); |
| 119 | + assert_eq!(snap.dropped_count, 7); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn reset_clears_counts_but_not_capacity() { |
| 124 | + let c = BufferCounters::new(32); |
| 125 | + c.increment_produced(); |
| 126 | + c.increment_consumed(); |
| 127 | + c.add_dropped(9); |
| 128 | + c.reset(); |
| 129 | + let snap = c.snapshot((0, 32)); |
| 130 | + assert_eq!(snap.produced_count, 0); |
| 131 | + assert_eq!(snap.consumed_count, 0); |
| 132 | + assert_eq!(snap.dropped_count, 0); |
| 133 | + assert_eq!(c.capacity(), 32); |
| 134 | + // Still usable afterwards. |
| 135 | + c.increment_produced(); |
| 136 | + assert_eq!(c.snapshot((1, 32)).produced_count, 1); |
| 137 | + } |
| 138 | + |
| 139 | + #[cfg(feature = "std")] |
| 140 | + #[test] |
| 141 | + fn concurrent_increments() { |
| 142 | + use std::sync::Arc; |
| 143 | + use std::thread; |
| 144 | + |
| 145 | + let c = Arc::new(BufferCounters::new(64)); |
| 146 | + let threads = 8; |
| 147 | + let per_thread = 1000u64; |
| 148 | + let handles: Vec<_> = (0..threads) |
| 149 | + .map(|_| { |
| 150 | + let c = Arc::clone(&c); |
| 151 | + thread::spawn(move || { |
| 152 | + for _ in 0..per_thread { |
| 153 | + c.increment_produced(); |
| 154 | + c.increment_consumed(); |
| 155 | + c.add_dropped(1); |
| 156 | + } |
| 157 | + }) |
| 158 | + }) |
| 159 | + .collect(); |
| 160 | + for h in handles { |
| 161 | + h.join().unwrap(); |
| 162 | + } |
| 163 | + let snap = c.snapshot((0, 64)); |
| 164 | + assert_eq!(snap.produced_count, threads as u64 * per_thread); |
| 165 | + assert_eq!(snap.consumed_count, threads as u64 * per_thread); |
| 166 | + assert_eq!(snap.dropped_count, threads as u64 * per_thread); |
| 167 | + } |
| 168 | +} |
0 commit comments