Files
Easytier_lkddi/easytier/src/tunnels/stats.rs
T

96 lines
2.4 KiB
Rust
Raw Normal View History

2024-02-01 18:32:06 +08:00
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering::Relaxed};
2023-09-23 01:53:45 +00:00
pub struct WindowLatency {
latency_us_window: Vec<AtomicU32>,
2023-09-23 01:53:45 +00:00
latency_us_window_index: AtomicU32,
2024-02-01 18:32:06 +08:00
latency_us_window_size: u32,
sum: AtomicU32,
count: AtomicU32,
2023-09-23 01:53:45 +00:00
}
impl WindowLatency {
pub fn new(window_size: u32) -> Self {
Self {
latency_us_window: (0..window_size).map(|_| AtomicU32::new(0)).collect(),
2023-09-23 01:53:45 +00:00
latency_us_window_index: AtomicU32::new(0),
2024-02-01 18:32:06 +08:00
latency_us_window_size: window_size,
sum: AtomicU32::new(0),
count: AtomicU32::new(0),
2023-09-23 01:53:45 +00:00
}
}
pub fn record_latency(&self, latency_us: u32) {
2024-02-01 18:32:06 +08:00
let index = self.latency_us_window_index.fetch_add(1, Relaxed);
2024-02-06 13:29:12 +08:00
if self.count.load(Relaxed) < self.latency_us_window_size {
2024-02-01 18:32:06 +08:00
self.count.fetch_add(1, Relaxed);
}
2023-09-23 01:53:45 +00:00
2024-02-01 18:32:06 +08:00
let index = index % self.latency_us_window_size;
let old_lat = self.latency_us_window[index as usize].swap(latency_us, Relaxed);
if old_lat < latency_us {
self.sum.fetch_add(latency_us - old_lat, Relaxed);
} else {
self.sum.fetch_sub(old_lat - latency_us, Relaxed);
2023-09-23 01:53:45 +00:00
}
2024-02-01 18:32:06 +08:00
}
2023-09-23 01:53:45 +00:00
2024-02-01 18:32:06 +08:00
pub fn get_latency_us<T: From<u32> + std::ops::Div<Output = T>>(&self) -> T {
let count = self.count.load(Relaxed);
let sum = self.sum.load(Relaxed);
2023-09-23 01:53:45 +00:00
if count == 0 {
0.into()
2023-09-23 01:53:45 +00:00
} else {
(T::from(sum)) / T::from(count)
2023-09-23 01:53:45 +00:00
}
}
}
pub struct Throughput {
tx_bytes: AtomicU64,
rx_bytes: AtomicU64,
tx_packets: AtomicU64,
rx_packets: AtomicU64,
}
impl Throughput {
pub fn new() -> Self {
Self {
tx_bytes: AtomicU64::new(0),
rx_bytes: AtomicU64::new(0),
tx_packets: AtomicU64::new(0),
rx_packets: AtomicU64::new(0),
}
}
pub fn tx_bytes(&self) -> u64 {
2024-02-01 18:32:06 +08:00
self.tx_bytes.load(Relaxed)
2023-09-23 01:53:45 +00:00
}
pub fn rx_bytes(&self) -> u64 {
2024-02-01 18:32:06 +08:00
self.rx_bytes.load(Relaxed)
2023-09-23 01:53:45 +00:00
}
pub fn tx_packets(&self) -> u64 {
2024-02-01 18:32:06 +08:00
self.tx_packets.load(Relaxed)
2023-09-23 01:53:45 +00:00
}
pub fn rx_packets(&self) -> u64 {
2024-02-01 18:32:06 +08:00
self.rx_packets.load(Relaxed)
2023-09-23 01:53:45 +00:00
}
pub fn record_tx_bytes(&self, bytes: u64) {
2024-02-01 18:32:06 +08:00
self.tx_bytes.fetch_add(bytes, Relaxed);
self.tx_packets.fetch_add(1, Relaxed);
2023-09-23 01:53:45 +00:00
}
pub fn record_rx_bytes(&self, bytes: u64) {
2024-02-01 18:32:06 +08:00
self.rx_bytes.fetch_add(bytes, Relaxed);
self.rx_packets.fetch_add(1, Relaxed);
2023-09-23 01:53:45 +00:00
}
}