diff --git a/Cargo.lock b/Cargo.lock index adf8e8a..a691c82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,7 +20,7 @@ checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" [[package]] name = "nload-rs" -version = "0.1.4" +version = "0.1.5" dependencies = [ "libc", "signal-hook", diff --git a/Cargo.toml b/Cargo.toml index de02945..e4c5590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nload-rs" -version = "0.1.4" +version = "0.1.5" edition = "2024" description = "A Linux single-interface network traffic monitor inspired by nload" license = "GPL-2.0-only" diff --git a/README.md b/README.md index 2c094d8..78e91cb 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,15 @@ requested one when it first appears. New interfaces become available as they appear; disconnected interfaces stay in the list and are marked unavailable. Use `-i VALUE` and `-o VALUE` to set the incoming and outgoing graph ceilings. -Both default to 10,240 kBit/s (10 MBit/s in binary units). Run -`nload-rs --help` for all options. +Both default to 10,240 kBit/s (10 MBit/s in binary units). + +Use `--auto-scale` to adjust each graph independently. The displayed ceiling +rises immediately with spikes and falls in steps after traffic stays low. +Older spikes may clip as the ceiling falls, but statistics are unchanged. + +An explicit `-i` or `-o` keeps that direction fixed, regardless of option order. +For example, `nload-rs --auto-scale -o 0 lo` auto-scales incoming traffic and +keeps outgoing traffic at the default ceiling. Run `nload-rs --help` for all options. Use a terminal at least 60×16 with UTF-8 and a font that renders Braille. `TERM` must be nonempty and neither `dumb` nor `linux`. `--color` requires diff --git a/src/application/tests.rs b/src/application/tests.rs index 41333ed..fcb3d8a 100644 --- a/src/application/tests.rs +++ b/src/application/tests.rs @@ -443,8 +443,8 @@ fn observed_wire(selected: usize, width: usize, height: usize, rounds: usize) -> &first, None, arguments::Ceilings { - incoming: 128, - outgoing: 128, + incoming: arguments::CeilingPolicy::Fixed(128), + outgoing: arguments::CeilingPolicy::Fixed(128), }, 0, ) diff --git a/src/arguments.rs b/src/arguments.rs index 09ff588..1555cee 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -8,20 +8,23 @@ pub(crate) const DEFAULT_CEILING_KBIT: u64 = 10240; pub(crate) const DEFAULT_CEILING_BYTES: u64 = DEFAULT_CEILING_KBIT * 128; #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct Ceilings { - pub(crate) incoming: u64, - pub(crate) outgoing: u64, +pub(crate) enum CeilingPolicy { + Fixed(u64), + Auto, } -impl Default for Ceilings { +impl Default for CeilingPolicy { fn default() -> Self { - Self { - incoming: DEFAULT_CEILING_BYTES, - outgoing: DEFAULT_CEILING_BYTES, - } + Self::Fixed(DEFAULT_CEILING_BYTES) } } +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub(crate) struct Ceilings { + pub(crate) incoming: CeilingPolicy, + pub(crate) outgoing: CeilingPolicy, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct RunConfig { pub(crate) interface: Option>, @@ -59,11 +62,11 @@ pub(crate) fn parse( } } - let mut config = RunConfig { - interface: None, - ceilings: Ceilings::default(), - color: false, - }; + let mut interface = None; + let mut color = false; + let mut auto_scale = false; + let mut incoming = None; + let mut outgoing = None; let mut options = true; let mut tokens = tokens.iter(); while let Some(token) = tokens.next() { @@ -75,7 +78,11 @@ pub(crate) fn parse( continue; } b"--color" => { - config.color = true; + color = true; + continue; + } + b"--auto-scale" => { + auto_scale = true; continue; } b"-i" | b"-o" => { @@ -84,9 +91,9 @@ pub(crate) fn parse( .ok_or(ArgumentError("ceiling option requires a separate value"))?; let ceiling = parse_ceiling(value.as_bytes())?; if bytes == b"-i" { - config.ceilings.incoming = ceiling; + incoming = Some(ceiling); } else { - config.ceilings.outgoing = ceiling; + outgoing = Some(ceiling); } continue; } @@ -101,12 +108,24 @@ pub(crate) fn parse( _ => {} } } - if config.interface.is_some() { + if interface.is_some() { return Err(ArgumentError("at most one interface name is allowed")); } - config.interface = Some(bytes.to_vec()); + interface = Some(bytes.to_vec()); } - Ok(Invocation::Run(config)) + let default = if auto_scale { + CeilingPolicy::Auto + } else { + CeilingPolicy::default() + }; + Ok(Invocation::Run(RunConfig { + interface, + ceilings: Ceilings { + incoming: incoming.map_or(default, CeilingPolicy::Fixed), + outgoing: outgoing.map_or(default, CeilingPolicy::Fixed), + }, + color, + })) } fn parse_ceiling(bytes: &[u8]) -> Result { @@ -134,7 +153,7 @@ pub(crate) fn version_text() -> String { pub(crate) fn help_text() -> String { format!( - "Usage: {name} [--color] [-i VALUE] [-o VALUE] [--] [INTERFACE]\n\ + "Usage: {name} [--color] [--auto-scale] [-i VALUE] [-o VALUE] [--] [INTERFACE]\n\ \x20 {name} --help\n\ \x20 {name} --version\n\n\ Monitor one interface; arrows navigate the naturally sorted collection.\n\ @@ -147,10 +166,12 @@ pub(crate) fn help_text() -> String { VALUE is ASCII decimal binary-scaled kBit/s (1 kBit/s = 1024 Bit/s\n\ = 128 bytes/s), from 1 through 144115188075855871.\n\ Each direction defaults independently to {default} kBit/s (10.00 MBit/s).\n\ - Zero restores that direction's default; repeated options use the last value.\n\ - Ceiling values must be separate tokens; options may precede or follow a name.\n\ - --color Enable colored Braille graphs (off by default; ignores NO_COLOR).\n\ - --help and --version are standalone; no short aliases are accepted.\n", + Zero restores that direction's fixed default, even with --auto-scale.\n\ + Repeated options use the last value.\n\ + Ceiling values must be separate tokens; options may precede or follow a name.\n\ + --auto-scale Adapt each graph's ceiling to traffic unless -i or -o fixes it.\n\ + --color Enable colored Braille graphs (off by default; ignores NO_COLOR).\n\ + --help and --version are standalone; no short aliases are accepted.\n", name = env!("CARGO_PKG_NAME"), default = DEFAULT_CEILING_KBIT, ) @@ -159,6 +180,7 @@ pub(crate) fn help_text() -> String { #[cfg(test)] mod tests { use super::*; + use CeilingPolicy::{Auto, Fixed}; use std::os::unix::ffi::OsStringExt; fn invoke(tokens: &[&str]) -> Result { @@ -186,6 +208,9 @@ mod tests { "INTERFACE", "-i VALUE", "-o VALUE", + "--auto-scale", + "unless -i or -o fixes it", + "fixed default, even with --auto-scale", "--color", "off by default", "ignores NO_COLOR", @@ -222,6 +247,8 @@ mod tests { vec!["--color", "--help"], vec!["--version", "--color"], vec!["--color=always"], + vec!["--auto-scale=always"], + vec!["--auto-scale", "--help"], vec!["--", "eth0", "eth1"], ] { let error = invoke(&tokens).expect_err(&format!("accepted {tokens:?}")); @@ -236,7 +263,15 @@ mod tests { panic!("expected run"); }; assert_eq!(config.interface, Some(name)); - for name in ["-eth0", "--help", "--version", "--color", "-i", "--"] { + for name in [ + "-eth0", + "--help", + "--version", + "--color", + "--auto-scale", + "-i", + "--", + ] { assert_eq!(run(&["--", name]).interface, Some(name.as_bytes().to_vec())); } assert_eq!(run(&["ETH0"]).interface, Some(b"ETH0".to_vec())); @@ -245,6 +280,36 @@ mod tests { assert!(invoke(&["--", "eth0", "-i", "1"]).is_err()); } + #[test] + fn automatic_scaling_yields_to_explicit_ceilings_in_either_order() { + assert_eq!( + run(&["--auto-scale", "eth0"]).ceilings, + Ceilings { + incoming: Auto, + outgoing: Auto, + } + ); + assert_eq!(run(&["--", "--auto-scale"]).ceilings, Ceilings::default()); + for (flag, value, incoming, outgoing) in [ + ("-i", "10", Fixed(1280), Auto), + ("-o", "20", Auto, Fixed(2560)), + ("-i", "0", Fixed(DEFAULT_CEILING_BYTES), Auto), + ("-o", "0", Auto, Fixed(DEFAULT_CEILING_BYTES)), + ] { + let expected = Ceilings { incoming, outgoing }; + assert_eq!(run(&["--auto-scale", flag, value]).ceilings, expected); + assert_eq!( + run(&[flag, value, "eth0", "--auto-scale"]).ceilings, + expected + ); + } + let fixed = ["-i", "1", "-o", "0", "-i", "2"]; + assert_eq!( + run(&["-i", "1", "--auto-scale", "-o", "0", "-i", "2"]).ceilings, + run(&fixed).ceilings, + ); + } + #[test] fn graph_colors_are_opt_in_and_follow_option_grammar() { for tokens in [vec![], vec!["eth0"], vec!["--", "--color"]] { @@ -272,8 +337,8 @@ mod tests { assert_eq!( default.ceilings, Ceilings { - incoming: 1_310_720, - outgoing: 1_310_720 + incoming: Fixed(1_310_720), + outgoing: Fixed(1_310_720), } ); let before = run(&["-i", "20480", "-o", "5120", "eth0"]); @@ -281,24 +346,30 @@ mod tests { assert_eq!( before.ceilings, Ceilings { - incoming: 2_621_440, - outgoing: 655_360 + incoming: Fixed(2_621_440), + outgoing: Fixed(655_360), } ); - assert_eq!(run(&["-i", "10", "-i", "00020"]).ceilings.incoming, 2560); - assert_eq!(run(&["-o", "10", "-o", "20"]).ceilings.outgoing, 2560); + assert_eq!( + run(&["-i", "10", "-i", "00020"]).ceilings.incoming, + Fixed(2560) + ); + assert_eq!( + run(&["-o", "10", "-o", "20"]).ceilings.outgoing, + Fixed(2560) + ); assert_eq!( run(&["-i", "10", "-o", "20", "-i", "0000"]).ceilings, Ceilings { - incoming: 1_310_720, - outgoing: 2560 + incoming: Fixed(1_310_720), + outgoing: Fixed(2560), } ); assert_eq!( run(&["-i", "10", "-o", "20", "-o", "0"]).ceilings, Ceilings { - incoming: 1280, - outgoing: 1_310_720 + incoming: Fixed(1280), + outgoing: Fixed(1_310_720), } ); } @@ -317,7 +388,7 @@ mod tests { } else { ceilings.outgoing }, - expected + Fixed(expected) ); } for value in [ @@ -347,10 +418,10 @@ mod tests { let long_zero = "0".repeat(1000); assert_eq!( run(&["-i", &long_zero]).ceilings.incoming, - DEFAULT_CEILING_BYTES + Fixed(DEFAULT_CEILING_BYTES) ); let long_one = format!("{long_zero}1"); - assert_eq!(run(&["-i", &long_one]).ceilings.incoming, 128); + assert_eq!(run(&["-i", &long_one]).ceilings.incoming, Fixed(128)); assert!(parse([OsString::from("-i"), OsString::from_vec(vec![0xff])]).is_err()); assert!(invoke(&["-i", "bad", "-i", "1"]).is_err()); } diff --git a/src/monitor.rs b/src/monitor.rs index 715e4a4..8fc71e1 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -2,7 +2,7 @@ use std::collections::VecDeque; -use crate::arguments::Ceilings; +use crate::arguments::{CeilingPolicy, Ceilings}; use crate::proc_sampler::{ AcquisitionError, CounterOutcome, Counters, Snapshot, StartupError, StartupSelection, counter_outcome, natural_order, @@ -10,6 +10,8 @@ use crate::proc_sampler::{ const SECOND_NS: u64 = 1_000_000_000; const HISTORY_NS: u64 = 300 * SECOND_NS; +const AUTO_DECAY_NS: u64 = 2 * SECOND_NS; +const AUTO_MIN_CEILING: u64 = 1; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum ViewState { @@ -41,6 +43,18 @@ pub(crate) struct DirectionView<'a> { pub(crate) ceiling: u64, } +impl DirectionView<'_> { + fn unavailable(policy: CeilingPolicy) -> Self { + static EMPTY_SAMPLES: VecDeque = VecDeque::new(); + Self { + total: None, + rates: None, + samples: &EMPTY_SAMPLES, + ceiling: DirectionScale::new(policy).ceiling(), + } + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub(crate) struct SelectedView<'a> { pub(crate) name: &'a [u8], @@ -87,13 +101,14 @@ impl Monitor { mut outcome: impl FnMut(&[u8]) -> CounterOutcome, ) -> Self { assert!(!startup.names.is_empty() && startup.selected < startup.names.len()); - assert!(ceilings.incoming > 0 && ceilings.outgoing > 0); + assert!(!matches!(ceilings.incoming, CeilingPolicy::Fixed(0))); + assert!(!matches!(ceilings.outgoing, CeilingPolicy::Fixed(0))); Self { entries: startup .names .into_iter() .map(|name| { - let state = EntryState::from_outcome(at, outcome(&name)); + let state = EntryState::from_outcome(at, outcome(&name), ceilings); Entry { name, state } }) .collect(), @@ -116,9 +131,9 @@ impl Monitor { for entry in &mut self.entries { match (&mut entry.state, outcome(&entry.name)) { (EntryState::Run(run), CounterOutcome::Readable(counters)) => { - run.observe(at, counters); + run.observe(at, counters, self.ceilings); } - (state, outcome) => *state = EntryState::from_outcome(at, outcome), + (state, outcome) => *state = EntryState::from_outcome(at, outcome, self.ceilings), } } for name in names { @@ -132,7 +147,7 @@ impl Monitor { index, Entry { name: name.to_vec(), - state: EntryState::from_outcome(at, outcome(name)), + state: EntryState::from_outcome(at, outcome(name), self.ceilings), }, ); if self.pending_request.as_deref() == Some(name) { @@ -163,23 +178,24 @@ impl Monitor { pub(crate) fn selected_view(&self) -> SelectedView<'_> { let entry = &self.entries[self.selected]; - let (state, totals, incoming, outgoing) = match &entry.state { + let (state, incoming, outgoing) = match &entry.state { EntryState::Run(run) => ( if run.incoming.rates.is_some() { ViewState::Measured } else { ViewState::Baseline }, - Some(run.totals), - &run.incoming, - &run.outgoing, + run.incoming.view(run.totals.incoming), + run.outgoing.view(run.totals.outgoing), ), - EntryState::Absent => (ViewState::Absent, None, &EMPTY_DIRECTION, &EMPTY_DIRECTION), - EntryState::ReadFailed => ( - ViewState::ReadFailed, - None, - &EMPTY_DIRECTION, - &EMPTY_DIRECTION, + state @ (EntryState::Absent | EntryState::ReadFailed) => ( + if matches!(state, EntryState::Absent) { + ViewState::Absent + } else { + ViewState::ReadFailed + }, + DirectionView::unavailable(self.ceilings.incoming), + DirectionView::unavailable(self.ceilings.outgoing), ), }; SelectedView { @@ -187,8 +203,8 @@ impl Monitor { position: self.selected + 1, count: self.entries.len(), state, - incoming: incoming.view(totals.map(|pair| pair.incoming), self.ceilings.incoming), - outgoing: outgoing.view(totals.map(|pair| pair.outgoing), self.ceilings.outgoing), + incoming, + outgoing, } } } @@ -205,9 +221,11 @@ enum EntryState { } impl EntryState { - fn from_outcome(at: u64, outcome: CounterOutcome) -> Self { + fn from_outcome(at: u64, outcome: CounterOutcome, ceilings: Ceilings) -> Self { match outcome { - CounterOutcome::Readable(counters) => Self::Run(Box::new(Run::new(at, counters))), + CounterOutcome::Readable(counters) => { + Self::Run(Box::new(Run::new(at, counters, ceilings))) + } CounterOutcome::Absent => Self::Absent, CounterOutcome::ReadFailed => Self::ReadFailed, } @@ -230,25 +248,25 @@ struct Interval { } impl Run { - fn new(at: u64, totals: Counters) -> Self { + fn new(at: u64, totals: Counters, ceilings: Ceilings) -> Self { Self { baseline_at: at, latest_at: at, totals, intervals: VecDeque::new(), - incoming: Direction::default(), - outgoing: Direction::default(), + incoming: Direction::new(ceilings.incoming), + outgoing: Direction::new(ceilings.outgoing), } } - fn observe(&mut self, at: u64, totals: Counters) { + fn observe(&mut self, at: u64, totals: Counters, ceilings: Ceilings) { let delta = match ( totals.incoming.checked_sub(self.totals.incoming), totals.outgoing.checked_sub(self.totals.outgoing), ) { (Some(incoming), Some(outgoing)) => Counters { incoming, outgoing }, _ => { - *self = Self::new(at, totals); + *self = Self::new(at, totals, ceilings); return; } }; @@ -289,19 +307,66 @@ impl Run { } } -#[derive(Default)] +enum DirectionScale { + Fixed(u64), + Auto { + ceiling: u64, + low_since: Option, + }, +} + +impl DirectionScale { + fn new(policy: CeilingPolicy) -> Self { + match policy { + CeilingPolicy::Fixed(ceiling) => Self::Fixed(ceiling), + CeilingPolicy::Auto => Self::Auto { + ceiling: AUTO_MIN_CEILING, + low_since: None, + }, + } + } + + fn ceiling(&self) -> u64 { + match *self { + Self::Fixed(ceiling) | Self::Auto { ceiling, .. } => ceiling, + } + } + + fn observe(&mut self, at: u64, rate: f64) { + let Self::Auto { ceiling, low_since } = self else { + return; + }; + if rate > *ceiling as f64 { + *ceiling = (rate.ceil() as u64) + .checked_next_power_of_two() + .unwrap_or(u64::MAX); + *low_since = None; + } else if *ceiling == AUTO_MIN_CEILING || rate > *ceiling as f64 / 4.0 { + *low_since = None; + } else if at - *low_since.get_or_insert(at) >= AUTO_DECAY_NS { + *ceiling = ceiling.div_ceil(2); + *low_since = (rate <= *ceiling as f64 / 4.0).then_some(at); + } + } +} + struct Direction { rates: Option, samples: VecDeque, + scale: DirectionScale, } -static EMPTY_DIRECTION: Direction = Direction { - rates: None, - samples: VecDeque::new(), -}; - impl Direction { + fn new(policy: CeilingPolicy) -> Self { + Self { + rates: None, + samples: VecDeque::new(), + scale: DirectionScale::new(policy), + } + } + fn record(&mut self, at: u64, current: f64, average: f64) { + self.scale.observe(at, current); self.rates = Some(Rates { current, average, @@ -319,12 +384,12 @@ impl Direction { self.samples.push_back(GraphSample { at, rate: current }); } - fn view(&self, total: Option, ceiling: u64) -> DirectionView<'_> { + fn view(&self, total: u64) -> DirectionView<'_> { DirectionView { - total, + total: Some(total), rates: self.rates, samples: &self.samples, - ceiling, + ceiling: self.scale.ceiling(), } } } @@ -683,8 +748,8 @@ mod tests { #[test] fn navigation_wraps_symmetrically_preserving_views_and_global_ceilings() { let ceilings = Ceilings { - incoming: 2_621_440, - outgoing: 655_360, + incoming: CeilingPolicy::Fixed(2_621_440), + outgoing: CeilingPolicy::Fixed(655_360), }; let names = vec![b"eth1".to_vec(), b"eth02".to_vec(), b"e\xff2".to_vec()]; let mut monitor = Monitor::new( @@ -704,8 +769,8 @@ mod tests { assert_eq!(view.name, names[index]); assert_eq!(view.position, index + 1); assert_eq!(view.count, 3); - assert_eq!(view.incoming.ceiling, ceilings.incoming); - assert_eq!(view.outgoing.ceiling, ceilings.outgoing); + assert_eq!(view.incoming.ceiling, 2_621_440); + assert_eq!(view.outgoing.ceiling, 655_360); assert_eq!(view.incoming.rates.unwrap().current, 5_242_880.0); assert_eq!(view.outgoing.rates.unwrap().current, 1_310_720.0); expected.push(saved(view)); @@ -743,6 +808,89 @@ mod tests { Snapshot::parse(&bytes).unwrap() } + const AUTO_CEILINGS: Ceilings = Ceilings { + incoming: CeilingPolicy::Auto, + outgoing: CeilingPolicy::Auto, + }; + + #[test] + fn auto_scale_fits_quiet_traffic_and_recovers_without_jitter() { + let initial = snapshot(&[(b"eth0", 0, 0)]); + let mut monitor = Monitor::from_snapshot(&initial, None, AUTO_CEILINGS, 0).unwrap(); + let mut fixed = Monitor::from_snapshot(&initial, None, Ceilings::default(), 0).unwrap(); + assert_eq!(monitor.selected_view().incoming.ceiling, 1); + let mut total = 0; + for (second, delta, ceiling) in [ + (1, 3, 4), + (2, 1025, 2048), + (3, 1025, 2048), + (4, 512, 2048), + (5, 513, 2048), + (6, 512, 2048), + (7, 512, 2048), + (8, 512, 1024), + (9, 4, 1024), + (10, 4, 1024), + (20, 40, 512), + (21, 4, 512), + (22, 4, 256), + ] { + total += delta; + observe(&mut monitor, second * SECOND_NS, total, 0); + observe(&mut fixed, second * SECOND_NS, total, 0); + let auto_view = monitor.selected_view(); + let fixed_view = fixed.selected_view(); + assert_eq!(auto_view.incoming.ceiling, ceiling, "second {second}"); + assert_eq!(auto_view.outgoing.ceiling, 1); + assert_eq!(fixed_view.incoming.ceiling, 1_310_720); + assert_eq!(auto_view.incoming.rates, fixed_view.incoming.rates); + assert_eq!(auto_view.incoming.samples, fixed_view.incoming.samples); + } + let incoming = monitor.selected_view().incoming; + assert_eq!(incoming.samples[1].rate, 1025.0); + assert!(incoming.samples[1].rate > incoming.ceiling as f64); + } + + #[test] + fn auto_scale_tracks_each_direction_and_interface_even_when_unselected() { + let initial = snapshot(&[(b"eth0", 0, 0), (b"eth1", 0, 0)]); + let mut monitor = + Monitor::from_snapshot(&initial, Some(b"eth0"), AUTO_CEILINGS, 0).unwrap(); + for (second, totals) in [ + (1, [(b"eth0".as_slice(), 1000, 4), (b"eth1", 8, 200)]), + (2, [(b"eth0".as_slice(), 1000, 8), (b"eth1", 16, 400)]), + (3, [(b"eth0".as_slice(), 1000, 12), (b"eth1", 24, 600)]), + (4, [(b"eth0".as_slice(), 1000, 16), (b"eth1", 32, 600)]), + ] { + monitor.observe_round(second * SECOND_NS, &Ok(snapshot(&totals))); + } + for (name, incoming, outgoing) in [(b"eth0", 512, 4), (b"eth1", 8, 256), (b"eth0", 512, 4)] + { + let view = monitor.selected_view(); + assert_eq!(view.name, name); + assert_eq!( + (view.incoming.ceiling, view.outgoing.ceiling), + (incoming, outgoing) + ); + monitor.forward(); + } + } + + #[test] + fn auto_scale_saturates_then_recovers_in_power_of_two_steps() { + let initial = snapshot(&[(b"eth0", 0, 0)]); + let mut monitor = Monitor::from_snapshot(&initial, None, AUTO_CEILINGS, 0).unwrap(); + observe(&mut monitor, 1, u64::MAX, 0); + assert_eq!(monitor.selected_view().incoming.ceiling, u64::MAX); + observe(&mut monitor, SECOND_NS + 1, u64::MAX, 0); + observe(&mut monitor, 3 * SECOND_NS, u64::MAX, 0); + assert_eq!(monitor.selected_view().incoming.ceiling, u64::MAX); + observe(&mut monitor, 3 * SECOND_NS + 1, u64::MAX, 0); + assert_eq!(monitor.selected_view().incoming.ceiling, 1 << 63); + observe(&mut monitor, 5 * SECOND_NS + 1, u64::MAX, 0); + assert_eq!(monitor.selected_view().incoming.ceiling, 1 << 62); + } + #[test] fn snapshot_lifecycle_retains_raw_membership_selection_and_retries_all_names() { let raw = b"A\xff".as_slice(); diff --git a/src/proc_sampler.rs b/src/proc_sampler.rs index 82e7e97..7937b03 100644 --- a/src/proc_sampler.rs +++ b/src/proc_sampler.rs @@ -619,7 +619,7 @@ mod tests { #[test] fn os_invocation_to_startup_preserves_identity_and_full_membership() { - use crate::arguments::{self, Invocation}; + use crate::arguments::{self, CeilingPolicy::Fixed, Invocation}; use std::ffi::OsString; use std::os::unix::ffi::OsStringExt; @@ -645,8 +645,8 @@ mod tests { startup.names, [b"-eth0".to_vec(), b"e\xff2".to_vec(), b"lo".to_vec()] ); - assert_eq!(config.ceilings.incoming, 2_621_440); - assert_eq!(config.ceilings.outgoing, 1_310_720); + assert_eq!(config.ceilings.incoming, Fixed(2_621_440)); + assert_eq!(config.ceilings.outgoing, Fixed(1_310_720)); } } diff --git a/src/screen.rs b/src/screen.rs index 5c585ed..1158186 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -299,7 +299,7 @@ mod tests { use super::*; use std::collections::VecDeque; - use crate::arguments::Ceilings; + use crate::arguments::{CeilingPolicy::Fixed, Ceilings}; use crate::monitor::{GraphSample, Monitor, Rates}; use crate::proc_sampler::{CounterOutcome, Counters, StartupSelection}; @@ -934,8 +934,8 @@ mod tests { #[test] fn largest_exact_totals_and_ceilings_and_overwide_rates_keep_labels() { let ceilings = Ceilings { - incoming: 18_446_744_073_709_551_488, - outgoing: 128, + incoming: Fixed(18_446_744_073_709_551_488), + outgoing: Fixed(128), }; let mut monitor = monitor(b"eth0".to_vec(), ceilings, 0, 0); monitor.observe(1, [], |_| { @@ -1070,8 +1070,8 @@ mod tests { let mut monitor = monitor( b"eth0".to_vec(), Ceilings { - incoming: 100, - outgoing: 100, + incoming: Fixed(100), + outgoing: Fixed(100), }, 0, 0, @@ -1175,8 +1175,8 @@ mod tests { let mut monitor = monitor( b"eth0".to_vec(), Ceilings { - incoming: 100, - outgoing: 100, + incoming: Fixed(100), + outgoing: Fixed(100), }, 0, 0, @@ -1229,8 +1229,8 @@ mod tests { let mut monitor = monitor( b"eth0".to_vec(), Ceilings { - incoming: 100, - outgoing: 100, + incoming: Fixed(100), + outgoing: Fixed(100), }, 0, 0, @@ -1275,8 +1275,8 @@ mod tests { pending_request: None, }, Ceilings { - incoming: 100, - outgoing: 100, + incoming: Fixed(100), + outgoing: Fixed(100), }, 0, |_| { diff --git a/src/terminal/runtime/tests.rs b/src/terminal/runtime/tests.rs index 015fd20..bd3f321 100644 --- a/src/terminal/runtime/tests.rs +++ b/src/terminal/runtime/tests.rs @@ -2,7 +2,7 @@ use super::scripted::{Call, ScriptedEffects}; use super::*; -use crate::arguments::Ceilings; +use crate::arguments::{CeilingPolicy::Fixed, Ceilings}; use crate::monitor::Monitor; use crate::proc_sampler::{CounterOutcome, Counters, StartupSelection}; @@ -33,8 +33,8 @@ fn colored_frame(name: &[u8], width: usize, height: usize) -> crate::screen::Fra let mut monitor = baseline_monitor( name, Ceilings { - incoming: 24, - outgoing: 24, + incoming: Fixed(24), + outgoing: Fixed(24), }, ); for (at, total) in [(1_000_000_000, 24), (2_000_000_000, 48)] { diff --git a/src/terminal/session/tests.rs b/src/terminal/session/tests.rs index 3c0c9cb..bf4e3c8 100644 --- a/src/terminal/session/tests.rs +++ b/src/terminal/session/tests.rs @@ -214,7 +214,7 @@ const RESTORE: &[&str] = &[ #[test] fn explicit_and_drop_cleanup_cancel_partial_sgr_and_reset_before_screen_restoration() { - use crate::arguments::Ceilings; + use crate::arguments::{CeilingPolicy::Fixed, Ceilings}; use crate::monitor::Monitor; use crate::proc_sampler::{CounterOutcome, Counters, StartupSelection}; let mut monitor = Monitor::new( @@ -224,8 +224,8 @@ fn explicit_and_drop_cleanup_cancel_partial_sgr_and_reset_before_screen_restorat pending_request: None, }, Ceilings { - incoming: 128, - outgoing: 128, + incoming: Fixed(128), + outgoing: Fixed(128), }, 0, |_| { diff --git a/tests/linux_pty.rs b/tests/linux_pty.rs index b15108f..387dd90 100644 --- a/tests/linux_pty.rs +++ b/tests/linux_pty.rs @@ -526,6 +526,31 @@ fn loopback_burst() { } } +#[test] +fn auto_scale_raises_incoming_ceiling_without_changing_fixed_outgoing() { + let mut product = Product::spawn(60, 16, |command, _, _| { + command.args(["--auto-scale", "-o", "1", "lo"]); + }); + let baseline = product.frame(60, 16, |rows| rows[1] == "Waiting for rate sample"); + assert!(baseline[2].starts_with("Incoming: (ceiling ")); + assert_eq!(baseline[9], "Outgoing: (ceiling 1.00 kBit/s)"); + + loopback_burst(); + let measured = product.frame(60, 16, |rows| rows[2] != baseline[2]); + assert!(measured[2].starts_with("Incoming: (ceiling ")); + assert_eq!(measured[9], baseline[9]); + assert!( + measured[3..9] + .iter() + .flat_map(|row| row.chars()) + .any(|glyph| ('\u{2801}'..='\u{28ff}').contains(&glyph)) + ); + + product.send(b"q"); + assert!(product.finish(0, true).is_empty()); + product.assert_cleanup(); +} + fn colored_product() -> Product { let mut product = Product::spawn(60, 16, |command, _, _| { command.args(["--color", "-i", "1", "-o", "1", "lo"]);