Record a metric sample (called from Data Plane core). Lock-free, allocation-free. If the ring is full, the oldest sample is overwritten and the drop counter incremented.
(&mut self, sample: MetricSample)
| 98 | /// Lock-free, allocation-free. If the ring is full, the oldest sample |
| 99 | /// is overwritten and the drop counter incremented. |
| 100 | pub fn record(&mut self, sample: MetricSample) { |
| 101 | let pos = self.write_pos.load(Ordering::Relaxed); |
| 102 | let read = self.read_pos.load(Ordering::Relaxed); |
| 103 | |
| 104 | // If we've lapped the reader, advance the reader (drop oldest). |
| 105 | if pos.wrapping_sub(read) >= self.capacity as u64 { |
| 106 | self.read_pos.store( |
| 107 | pos.wrapping_sub(self.capacity as u64 - 1), |
| 108 | Ordering::Relaxed, |
| 109 | ); |
| 110 | self.dropped.fetch_add(1, Ordering::Relaxed); |
| 111 | } |
| 112 | |
| 113 | let idx = (pos as usize) & self.mask; |
| 114 | self.slots[idx] = sample; |
| 115 | self.write_pos.store(pos.wrapping_add(1), Ordering::Release); |
| 116 | } |
| 117 | |
| 118 | /// Drain all available samples into the provided buffer (called from Control Plane). |
| 119 | /// |