use crate::{Pid, jiffy::Jiffies};
#[derive(Clone, Copy, Default)]
pub enum BandwidthConfig {
#[default]
Unbounded,
Bounded { inbound: usize, outbound: usize },
}
pub(crate) struct Bandwidth {
in_bandwidth: usize,
out_bandwidth: usize,
in_pased: Vec<usize>,
out_pased: Vec<usize>,
}
impl Bandwidth {
pub(crate) fn new(bandwidth_type: BandwidthConfig, proc_num: usize) -> Self {
let (in_bandwidth, out_bandwidth) = match bandwidth_type {
BandwidthConfig::Unbounded => (usize::MAX, usize::MAX),
BandwidthConfig::Bounded { inbound, outbound } => (inbound, outbound),
};
Self {
in_bandwidth,
out_bandwidth,
in_pased: vec![0; proc_num],
out_pased: vec![0; proc_num],
}
}
pub(crate) fn try_recv(
&mut self,
message_size: usize,
target: Pid,
now: Jiffies,
) -> Option<Jiffies> {
if self.in_bandwidth == usize::MAX {
return None;
}
self.in_pased[target] += message_size;
if self.in_pased[target] > now.0 * self.in_bandwidth {
return Some(Jiffies(self.in_pased[target] / self.in_bandwidth));
}
None
}
pub(crate) fn try_send(
&mut self,
message_size: usize,
target: Pid,
now: Jiffies,
) -> Option<Jiffies> {
if self.out_bandwidth == usize::MAX {
return None;
}
self.out_pased[target] += message_size;
if self.out_pased[target] > now.0 * self.out_bandwidth {
return Some(Jiffies(self.out_pased[target] / self.out_bandwidth));
}
None
}
}