1986 lines
63 KiB
Rust
1986 lines
63 KiB
Rust
use std::{
|
|
collections::{BTreeMap, VecDeque},
|
|
io::{self, Stdout},
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
use crossterm::{
|
|
cursor::Show,
|
|
execute,
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
};
|
|
use irtt_client::managed::{ManagedTargetEndReason, ManagedTargetOutcome, TargetInstance};
|
|
use irtt_client::{ClientEvent, NegotiationResult, SignedDuration};
|
|
use irtt_stats::{Snapshot, StatsCollector, TimeStats};
|
|
use ratatui::{
|
|
backend::CrosstermBackend,
|
|
layout::{Constraint, Direction, Layout, Rect},
|
|
style::{Color, Modifier, Style},
|
|
symbols,
|
|
text::{Line, Span},
|
|
widgets::{Axis, Block, Borders, Chart, Dataset, GraphType, Paragraph, Wrap},
|
|
Frame, Terminal,
|
|
};
|
|
|
|
use crate::{
|
|
cmd::{
|
|
format::{
|
|
format_count, format_duration, format_ns_f64, format_optional_count,
|
|
format_optional_ns_i128, format_percent, format_percent_ratio, ABSENT,
|
|
},
|
|
tui::args::TuiArgs,
|
|
},
|
|
shared::client::{expected_probe_count, GroupPacingArg, ManagedRunSetup},
|
|
};
|
|
|
|
const HISTORY_LIMIT: usize = 100_000;
|
|
const RECENT_EVENT_LIMIT: usize = 80;
|
|
const MIN_WIDTH: u16 = 56;
|
|
const MIN_HEIGHT: u16 = 18;
|
|
const DEFAULT_GRAPH_WINDOW: Duration = Duration::from_secs(60);
|
|
const MIN_GRAPH_WINDOW: Duration = Duration::from_secs(5);
|
|
const MAX_GRAPH_WINDOW: Duration = Duration::from_secs(60 * 60);
|
|
const PAN_STEP_NUMERATOR: u32 = 1;
|
|
const PAN_STEP_DENOMINATOR: u32 = 4;
|
|
|
|
pub(super) struct TuiTerminal {
|
|
terminal: Terminal<CrosstermBackend<Stdout>>,
|
|
}
|
|
|
|
impl TuiTerminal {
|
|
pub(super) fn enter() -> io::Result<Self> {
|
|
enable_raw_mode()?;
|
|
let mut stdout = io::stdout();
|
|
if let Err(err) = execute!(stdout, EnterAlternateScreen) {
|
|
let _ = disable_raw_mode();
|
|
return Err(err);
|
|
}
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
|
match Terminal::new(backend) {
|
|
Ok(mut terminal) => {
|
|
if let Err(err) = terminal.clear() {
|
|
let _ = disable_raw_mode();
|
|
let _ = execute!(terminal.backend_mut(), LeaveAlternateScreen, Show);
|
|
let _ = terminal.show_cursor();
|
|
return Err(err);
|
|
}
|
|
Ok(Self { terminal })
|
|
}
|
|
Err(err) => {
|
|
let _ = disable_raw_mode();
|
|
let mut stdout = io::stdout();
|
|
let _ = execute!(stdout, LeaveAlternateScreen, Show);
|
|
Err(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn draw(&mut self, state: &TuiState) -> io::Result<()> {
|
|
self.terminal
|
|
.draw(|frame| draw_dashboard(frame, state))
|
|
.map(|_| ())
|
|
}
|
|
}
|
|
|
|
impl Drop for TuiTerminal {
|
|
fn drop(&mut self) {
|
|
let _ = disable_raw_mode();
|
|
let _ = execute!(self.terminal.backend_mut(), LeaveAlternateScreen, Show);
|
|
let _ = self.terminal.show_cursor();
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub(super) struct TuiState {
|
|
status: TuiStatus,
|
|
started_at: Instant,
|
|
config: TuiConfig,
|
|
target_index: BTreeMap<String, usize>,
|
|
targets: Vec<TuiTargetState>,
|
|
recent_events: VecDeque<String>,
|
|
last_warning: Option<String>,
|
|
dropped_events: u64,
|
|
graph_metric: GraphMetric,
|
|
graph_viewport: GraphViewport,
|
|
view: TuiView,
|
|
pub(super) paused: bool,
|
|
pub(super) quit_requested: bool,
|
|
}
|
|
|
|
impl TuiState {
|
|
pub(super) fn new(config: TuiConfig) -> Self {
|
|
Self::with_target_labels(config, ["target".to_owned()])
|
|
}
|
|
|
|
pub(super) fn with_target_labels(
|
|
config: TuiConfig,
|
|
labels: impl IntoIterator<Item = String>,
|
|
) -> Self {
|
|
let stats_config = stats_config(config.duration.is_none());
|
|
let targets = labels
|
|
.into_iter()
|
|
.map(|label| TuiTargetState::new(label, stats_config))
|
|
.collect::<Vec<_>>();
|
|
let target_index = targets
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(idx, target)| (target.label.clone(), idx))
|
|
.collect();
|
|
Self {
|
|
status: TuiStatus::Opening,
|
|
started_at: Instant::now(),
|
|
config,
|
|
target_index,
|
|
targets,
|
|
recent_events: VecDeque::with_capacity(RECENT_EVENT_LIMIT),
|
|
last_warning: None,
|
|
dropped_events: 0,
|
|
graph_metric: GraphMetric::EffectiveRtt,
|
|
graph_viewport: GraphViewport::default(),
|
|
view: TuiView::Graph,
|
|
paused: false,
|
|
quit_requested: false,
|
|
}
|
|
}
|
|
|
|
pub(super) fn process_target_event(&mut self, target: &TargetInstance, event: &ClientEvent) {
|
|
let label = target.id.as_str();
|
|
let idx = if let Some(idx) = self.target_index.get(label).copied() {
|
|
idx
|
|
} else {
|
|
let idx = self.targets.len();
|
|
let mut target = TuiTargetState::new(
|
|
label.to_owned(),
|
|
stats_config(self.config.duration.is_none()),
|
|
);
|
|
target.status = TargetStatus::Unknown;
|
|
self.targets.push(target);
|
|
self.target_index.insert(label.to_owned(), idx);
|
|
idx
|
|
};
|
|
self.process_event_for_target(idx, event);
|
|
}
|
|
|
|
pub(super) fn process_target_outcome(&mut self, outcome: &ManagedTargetOutcome) {
|
|
let label = outcome.target.id.as_str();
|
|
let idx = if let Some(idx) = self.target_index.get(label).copied() {
|
|
idx
|
|
} else {
|
|
let idx = self.targets.len();
|
|
self.targets.push(TuiTargetState::new(
|
|
label.to_owned(),
|
|
stats_config(self.config.duration.is_none()),
|
|
));
|
|
self.target_index.insert(label.to_owned(), idx);
|
|
idx
|
|
};
|
|
|
|
let (status, recent, primary_warning) = match &outcome.end_reason {
|
|
ManagedTargetEndReason::TestComplete => {
|
|
(TargetStatus::Closed, "test completed".to_owned(), None)
|
|
}
|
|
ManagedTargetEndReason::PeerClosed => (
|
|
TargetStatus::Closed,
|
|
"session closed by peer".to_owned(),
|
|
None,
|
|
),
|
|
ManagedTargetEndReason::NoTestComplete => (
|
|
TargetStatus::NoTest,
|
|
"no-test negotiation completed".to_owned(),
|
|
None,
|
|
),
|
|
ManagedTargetEndReason::Stopped => {
|
|
(TargetStatus::Stopped, "target stopped".to_owned(), None)
|
|
}
|
|
ManagedTargetEndReason::Removed => {
|
|
(TargetStatus::Removed, "target removed".to_owned(), None)
|
|
}
|
|
ManagedTargetEndReason::Replaced => {
|
|
(TargetStatus::Replaced, "target replaced".to_owned(), None)
|
|
}
|
|
ManagedTargetEndReason::Failed(failure) => {
|
|
let message = format!("{}: {}", failure.kind, failure.message);
|
|
(
|
|
TargetStatus::Failed,
|
|
format!("target failed: {message}"),
|
|
Some(message),
|
|
)
|
|
}
|
|
};
|
|
|
|
let cleanup_warning = outcome.cleanup_failure.as_ref().map(|failure| {
|
|
format!(
|
|
"cleanup failed ({} {}): {}",
|
|
failure.phase, failure.kind, failure.message
|
|
)
|
|
});
|
|
let warning = match (primary_warning, cleanup_warning.as_ref()) {
|
|
(Some(primary), Some(cleanup)) => Some(format!("{primary}; {cleanup}")),
|
|
(Some(primary), None) => Some(primary),
|
|
(None, Some(cleanup)) => Some(cleanup.clone()),
|
|
(None, None) => None,
|
|
};
|
|
|
|
if let Some(target) = self.targets.get_mut(idx) {
|
|
target.remote = outcome.remote.map(|remote| remote.to_string());
|
|
target.status = status;
|
|
if let Some(warning) = &warning {
|
|
target.last_warning = Some(warning.clone());
|
|
}
|
|
}
|
|
if let Some(warning) = warning {
|
|
self.last_warning = Some(warning);
|
|
}
|
|
self.push_event(format!("{label}: {recent}"));
|
|
if let Some(cleanup_warning) = cleanup_warning {
|
|
self.push_event(format!("{label}: {cleanup_warning}"));
|
|
}
|
|
|
|
if self
|
|
.targets
|
|
.iter()
|
|
.all(|target| target.status.is_terminal())
|
|
{
|
|
self.status = if self.targets.iter().any(|target| target.status.is_success()) {
|
|
TuiStatus::Complete
|
|
} else if self
|
|
.targets
|
|
.iter()
|
|
.any(|target| target.status == TargetStatus::Failed)
|
|
{
|
|
TuiStatus::Error
|
|
} else {
|
|
TuiStatus::Complete
|
|
};
|
|
}
|
|
}
|
|
|
|
fn process_event_for_target(&mut self, target_idx: usize, event: &ClientEvent) {
|
|
let recent;
|
|
let mut global_status = None;
|
|
let mut global_warning = None;
|
|
let label = {
|
|
let Some(target) = self.targets.get_mut(target_idx) else {
|
|
return;
|
|
};
|
|
target.stats.process(event);
|
|
match event {
|
|
ClientEvent::SessionStarted(irtt_client::SessionStarted {
|
|
remote,
|
|
token,
|
|
negotiation: negotiated,
|
|
..
|
|
}) => {
|
|
target.remote = Some(remote.to_string());
|
|
target.session = Some(format!("{token:#x}"));
|
|
target.negotiated = Some(negotiated.clone());
|
|
target.status = TargetStatus::Active;
|
|
global_status = Some(TuiStatus::Running);
|
|
recent = Some(format!("session started token={token:#x}"));
|
|
}
|
|
ClientEvent::NoTestCompleted(irtt_client::NoTestCompleted {
|
|
remote,
|
|
negotiation: negotiated,
|
|
..
|
|
}) => {
|
|
target.remote = Some(remote.to_string());
|
|
target.negotiated = Some(negotiated.clone());
|
|
target.status = TargetStatus::NoTest;
|
|
global_status = Some(TuiStatus::Complete);
|
|
recent = Some("no-test negotiation completed".to_owned());
|
|
}
|
|
ClientEvent::SessionClosed { token, .. } => {
|
|
target.session = Some(format!("{token:#x}"));
|
|
target.status = TargetStatus::Closed;
|
|
global_status = Some(TuiStatus::Complete);
|
|
recent = Some(format!("session closed token={token:#x}"));
|
|
}
|
|
ClientEvent::EchoSent { seq, bytes, .. } => {
|
|
recent = Some(format!("sent seq={} bytes={bytes}", format_seq(*seq)));
|
|
}
|
|
ClientEvent::EchoReply {
|
|
seq,
|
|
received_at,
|
|
rtt,
|
|
one_way,
|
|
server_timing,
|
|
..
|
|
} => {
|
|
let client_to_server_ns = one_way
|
|
.and_then(|sample| sample.client_to_server)
|
|
.map(SignedDuration::as_nanos);
|
|
let server_to_client_ns = one_way
|
|
.and_then(|sample| sample.server_to_client)
|
|
.map(SignedDuration::as_nanos);
|
|
let server_processing_ns = server_timing
|
|
.and_then(|timing| timing.processing)
|
|
.map(duration_ns);
|
|
target.push_graph_sample(GraphSample {
|
|
timestamp: received_at.mono,
|
|
seq: *seq,
|
|
effective_ns: rtt.effective.as_nanos(),
|
|
raw_ns: duration_ns(rtt.raw),
|
|
adjusted_ns: rtt.adjusted.map(SignedDuration::as_nanos),
|
|
client_to_server_ns,
|
|
server_to_client_ns,
|
|
server_processing_ns,
|
|
});
|
|
target.last_sample = Some(LastSample {
|
|
seq: *seq,
|
|
raw_ns: duration_ns(rtt.raw),
|
|
adjusted_ns: rtt.adjusted.map(SignedDuration::as_nanos),
|
|
effective_ns: rtt.effective.as_nanos(),
|
|
client_to_server_ns,
|
|
server_to_client_ns,
|
|
server_processing_ns,
|
|
});
|
|
recent = Some(format!(
|
|
"reply seq={} effective={}",
|
|
format_seq(*seq),
|
|
format_optional_ns_i128(Some(rtt.effective.as_nanos()))
|
|
));
|
|
}
|
|
ClientEvent::EchoLoss { seq, .. } => {
|
|
recent = Some(format!("loss seq={}", format_seq(*seq)));
|
|
}
|
|
ClientEvent::DuplicateReply { seq, remote, .. } => {
|
|
recent = Some(format!("duplicate seq={} from {remote}", format_seq(*seq)));
|
|
}
|
|
ClientEvent::LateReply {
|
|
seq,
|
|
highest_seen,
|
|
rtt,
|
|
..
|
|
} => {
|
|
let timing = rtt
|
|
.map(|sample| {
|
|
format!(
|
|
" effective={}",
|
|
format_optional_ns_i128(Some(sample.effective.as_nanos()))
|
|
)
|
|
})
|
|
.unwrap_or_default();
|
|
recent = Some(format!(
|
|
"late seq={} highest_seen={}{}",
|
|
format_seq(*seq),
|
|
format_seq(*highest_seen),
|
|
timing
|
|
));
|
|
}
|
|
ClientEvent::Warning { kind, message, .. } => {
|
|
let warning = format!("{kind:?}: {message}");
|
|
target.last_warning = Some(warning.clone());
|
|
global_warning = Some(warning.clone());
|
|
recent = Some(format!("warning {warning}"));
|
|
}
|
|
}
|
|
target.label.clone()
|
|
};
|
|
if let Some(status) = global_status {
|
|
self.status = if status == TuiStatus::Complete
|
|
&& self.is_multi_target()
|
|
&& !self
|
|
.targets
|
|
.iter()
|
|
.all(|target| target.status.is_terminal())
|
|
{
|
|
TuiStatus::Running
|
|
} else {
|
|
status
|
|
};
|
|
}
|
|
if let Some(warning) = global_warning {
|
|
self.last_warning = Some(warning);
|
|
}
|
|
if let Some(recent) = recent {
|
|
self.push_event(format!("{label}: {recent}"));
|
|
}
|
|
}
|
|
|
|
pub(super) fn set_status(&mut self, status: TuiStatus) {
|
|
self.status = status;
|
|
}
|
|
|
|
pub(super) fn mark_dropped_managed_events(&mut self, dropped_events: u64) {
|
|
self.mark_dropped_events(dropped_events, "managed run");
|
|
}
|
|
|
|
fn mark_dropped_events(&mut self, dropped_events: u64, source: &str) {
|
|
if dropped_events == 0 {
|
|
return;
|
|
}
|
|
self.dropped_events = dropped_events;
|
|
let event_word = if dropped_events == 1 {
|
|
"event"
|
|
} else {
|
|
"events"
|
|
};
|
|
let warning =
|
|
format!("dropped {dropped_events} {source} {event_word}; statistics may be incomplete");
|
|
self.last_warning = Some(warning.clone());
|
|
self.push_event(format!("warning {warning}"));
|
|
}
|
|
|
|
pub(super) fn set_error(&mut self, message: String) {
|
|
self.status = TuiStatus::Error;
|
|
self.last_warning = Some(message.clone());
|
|
if let Some(target) = self.targets.first_mut() {
|
|
target.status = TargetStatus::Failed;
|
|
target.last_warning = Some(message.clone());
|
|
}
|
|
self.push_event(format!("error {message}"));
|
|
}
|
|
|
|
pub(super) fn set_run_error(&mut self, message: String) {
|
|
self.status = TuiStatus::Error;
|
|
self.last_warning = Some(message.clone());
|
|
self.push_event(format!("error {message}"));
|
|
}
|
|
|
|
pub(super) fn clear_visible_history(&mut self) {
|
|
for target in &mut self.targets {
|
|
target.graph_history.clear();
|
|
}
|
|
self.graph_viewport.follow_live();
|
|
self.push_event("visible graph history reset".to_owned());
|
|
}
|
|
|
|
pub(super) fn toggle_pause(&mut self) {
|
|
self.paused = !self.paused;
|
|
}
|
|
|
|
pub(super) fn cycle_graph_metric(&mut self) {
|
|
self.graph_metric = self.graph_metric.next();
|
|
}
|
|
|
|
pub(super) fn toggle_view(&mut self) {
|
|
self.view = self.view.next();
|
|
}
|
|
|
|
pub(super) fn pan_graph_left(&mut self) {
|
|
let oldest = self.oldest_graph_sample_time();
|
|
let newest = self.newest_graph_sample_time();
|
|
self.graph_viewport.pan_backward(oldest, newest);
|
|
}
|
|
|
|
pub(super) fn pan_graph_right(&mut self) {
|
|
let newest = self.newest_graph_sample_time();
|
|
self.graph_viewport.pan_forward(newest);
|
|
}
|
|
|
|
pub(super) fn pan_graph_page_left(&mut self) {
|
|
let oldest = self.oldest_graph_sample_time();
|
|
let newest = self.newest_graph_sample_time();
|
|
self.graph_viewport.page_backward(oldest, newest);
|
|
}
|
|
|
|
pub(super) fn pan_graph_page_right(&mut self) {
|
|
let newest = self.newest_graph_sample_time();
|
|
self.graph_viewport.page_forward(newest);
|
|
}
|
|
|
|
pub(super) fn jump_graph_oldest(&mut self) {
|
|
let oldest = self.oldest_graph_sample_time();
|
|
let newest = self.newest_graph_sample_time();
|
|
self.graph_viewport.jump_oldest(oldest, newest);
|
|
}
|
|
|
|
pub(super) fn jump_graph_live(&mut self) {
|
|
self.graph_viewport.follow_live();
|
|
}
|
|
|
|
pub(super) fn zoom_graph_in(&mut self) {
|
|
self.graph_viewport.zoom_in(self.newest_graph_sample_time());
|
|
}
|
|
|
|
pub(super) fn zoom_graph_out(&mut self) {
|
|
self.graph_viewport
|
|
.zoom_out(self.newest_graph_sample_time());
|
|
}
|
|
|
|
pub(super) fn reset_graph_window(&mut self) {
|
|
self.graph_viewport
|
|
.reset_window(self.newest_graph_sample_time());
|
|
}
|
|
|
|
fn push_event(&mut self, event: String) {
|
|
push_bounded(&mut self.recent_events, event, RECENT_EVENT_LIMIT);
|
|
}
|
|
|
|
fn selected_target(&self) -> Option<&TuiTargetState> {
|
|
self.targets.first()
|
|
}
|
|
|
|
fn selected_snapshot(&self) -> Snapshot {
|
|
self.selected_target()
|
|
.map(|target| target.stats.snapshot())
|
|
.unwrap_or_else(|| StatsCollector::new(stats_config(true)).snapshot())
|
|
}
|
|
|
|
fn is_multi_target(&self) -> bool {
|
|
self.targets.len() > 1
|
|
}
|
|
|
|
fn oldest_graph_sample_time(&self) -> Option<Instant> {
|
|
self.targets
|
|
.iter()
|
|
.filter_map(|target| target.graph_history.front().map(|sample| sample.timestamp))
|
|
.min()
|
|
}
|
|
|
|
fn newest_graph_sample_time(&self) -> Option<Instant> {
|
|
self.targets
|
|
.iter()
|
|
.filter_map(|target| target.graph_history.back().map(|sample| sample.timestamp))
|
|
.max()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub(super) struct TuiTargetState {
|
|
label: String,
|
|
remote: Option<String>,
|
|
session: Option<String>,
|
|
status: TargetStatus,
|
|
negotiated: Option<NegotiationResult>,
|
|
graph_history: VecDeque<GraphSample>,
|
|
last_sample: Option<LastSample>,
|
|
last_warning: Option<String>,
|
|
stats: StatsCollector,
|
|
}
|
|
|
|
impl TuiTargetState {
|
|
fn new(label: String, stats_config: irtt_stats::StatsConfig) -> Self {
|
|
Self {
|
|
label,
|
|
remote: None,
|
|
session: None,
|
|
status: TargetStatus::Opening,
|
|
negotiated: None,
|
|
graph_history: VecDeque::new(),
|
|
last_sample: None,
|
|
last_warning: None,
|
|
stats: StatsCollector::new(stats_config),
|
|
}
|
|
}
|
|
|
|
fn push_graph_sample(&mut self, sample: GraphSample) {
|
|
push_bounded(&mut self.graph_history, sample, HISTORY_LIMIT);
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(super) enum TargetStatus {
|
|
Opening,
|
|
Active,
|
|
Closed,
|
|
Failed,
|
|
NoTest,
|
|
Stopped,
|
|
Removed,
|
|
Replaced,
|
|
Unknown,
|
|
}
|
|
|
|
impl TargetStatus {
|
|
fn label(self) -> &'static str {
|
|
match self {
|
|
Self::Opening => "opening",
|
|
Self::Active => "active",
|
|
Self::Closed => "closed",
|
|
Self::Failed => "failed",
|
|
Self::NoTest => "no-test",
|
|
Self::Stopped => "stopped",
|
|
Self::Removed => "removed",
|
|
Self::Replaced => "replaced",
|
|
Self::Unknown => "unknown",
|
|
}
|
|
}
|
|
|
|
fn is_terminal(self) -> bool {
|
|
matches!(
|
|
self,
|
|
Self::Closed
|
|
| Self::Failed
|
|
| Self::NoTest
|
|
| Self::Stopped
|
|
| Self::Removed
|
|
| Self::Replaced
|
|
)
|
|
}
|
|
|
|
fn is_success(self) -> bool {
|
|
matches!(self, Self::Closed | Self::NoTest)
|
|
}
|
|
}
|
|
|
|
impl GroupPacingArg {
|
|
fn label(self) -> &'static str {
|
|
match self {
|
|
Self::Staggered => "staggered",
|
|
Self::Burst => "burst",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Statistics policy for the TUI.
|
|
///
|
|
/// The TUI treats late replies as diagnostics, so it counts a matched late
|
|
/// reply without letting it contribute timing samples. That is an explicit
|
|
/// statistics policy rather than something the TUI expresses by degrading the
|
|
/// event it forwards.
|
|
fn stats_config(continuous: bool) -> irtt_stats::StatsConfig {
|
|
let base = if continuous {
|
|
irtt_stats::StatsConfig::continuous()
|
|
} else {
|
|
irtt_stats::StatsConfig::finite()
|
|
};
|
|
irtt_stats::StatsConfig {
|
|
late_replies: irtt_stats::LateReplyMode::CountOnly,
|
|
..base
|
|
}
|
|
}
|
|
|
|
impl Default for TuiState {
|
|
fn default() -> Self {
|
|
Self::new(TuiConfig::default())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(super) struct TuiConfig {
|
|
interval: Duration,
|
|
duration: Option<Duration>,
|
|
timeout: Duration,
|
|
target_probes: Option<u64>,
|
|
pacing: GroupPacingArg,
|
|
}
|
|
|
|
impl TuiConfig {
|
|
pub(super) fn from_args(args: &TuiArgs, setup: &ManagedRunSetup) -> Self {
|
|
Self {
|
|
interval: args.interval,
|
|
duration: (!args.is_continuous()).then_some(args.duration),
|
|
timeout: setup.client.probe_timeout,
|
|
target_probes: (!args.is_continuous())
|
|
.then(|| expected_probe_count(args.duration, args.interval)),
|
|
pacing: args.pacing,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for TuiConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
interval: Duration::from_secs(1),
|
|
duration: Some(Duration::from_secs(10)),
|
|
timeout: Duration::from_secs(2),
|
|
target_probes: Some(10),
|
|
pacing: GroupPacingArg::Staggered,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(super) enum TuiStatus {
|
|
Opening,
|
|
Running,
|
|
Interrupted,
|
|
Closing,
|
|
Complete,
|
|
Error,
|
|
}
|
|
|
|
impl TuiStatus {
|
|
fn label(self) -> &'static str {
|
|
match self {
|
|
Self::Opening => "opening",
|
|
Self::Running => "running",
|
|
Self::Interrupted => "interrupted",
|
|
Self::Closing => "closing",
|
|
Self::Complete => "complete",
|
|
Self::Error => "error",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum TuiView {
|
|
Graph,
|
|
Dashboard,
|
|
}
|
|
|
|
impl TuiView {
|
|
fn next(self) -> Self {
|
|
match self {
|
|
Self::Graph => Self::Dashboard,
|
|
Self::Dashboard => Self::Graph,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
struct GraphViewport {
|
|
mode: GraphViewportMode,
|
|
window: Duration,
|
|
}
|
|
|
|
impl Default for GraphViewport {
|
|
fn default() -> Self {
|
|
Self {
|
|
mode: GraphViewportMode::Follow,
|
|
window: DEFAULT_GRAPH_WINDOW,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl GraphViewport {
|
|
fn range(self, now: Instant, newest_sample: Option<Instant>) -> GraphViewportRange {
|
|
let end = match self.mode {
|
|
GraphViewportMode::Follow => newest_sample.unwrap_or(now).max(now),
|
|
GraphViewportMode::Historical { end } => end,
|
|
};
|
|
GraphViewportRange {
|
|
start: end.checked_sub(self.window).unwrap_or(end),
|
|
end,
|
|
window: self.window,
|
|
is_live: matches!(self.mode, GraphViewportMode::Follow),
|
|
}
|
|
}
|
|
|
|
fn follow_live(&mut self) {
|
|
self.mode = GraphViewportMode::Follow;
|
|
}
|
|
|
|
fn pan_backward(&mut self, oldest: Option<Instant>, newest: Option<Instant>) {
|
|
self.pan_by(
|
|
-duration_fraction(self.window, PAN_STEP_NUMERATOR, PAN_STEP_DENOMINATOR),
|
|
oldest,
|
|
newest,
|
|
);
|
|
}
|
|
|
|
fn pan_forward(&mut self, newest: Option<Instant>) {
|
|
self.pan_by(
|
|
duration_fraction(self.window, PAN_STEP_NUMERATOR, PAN_STEP_DENOMINATOR),
|
|
None,
|
|
newest,
|
|
);
|
|
}
|
|
|
|
fn page_backward(&mut self, oldest: Option<Instant>, newest: Option<Instant>) {
|
|
self.pan_by(-signed_duration(self.window), oldest, newest);
|
|
}
|
|
|
|
fn page_forward(&mut self, newest: Option<Instant>) {
|
|
self.pan_by(signed_duration(self.window), None, newest);
|
|
}
|
|
|
|
fn jump_oldest(&mut self, oldest: Option<Instant>, newest: Option<Instant>) {
|
|
let Some(oldest) = oldest else {
|
|
return;
|
|
};
|
|
let live_end = newest.unwrap_or(oldest);
|
|
self.mode = GraphViewportMode::Historical {
|
|
end: (oldest + self.window).min(live_end),
|
|
};
|
|
}
|
|
|
|
fn zoom_in(&mut self, newest: Option<Instant>) {
|
|
self.set_window(duration_fraction(self.window, 2, 3).duration, newest);
|
|
}
|
|
|
|
fn zoom_out(&mut self, newest: Option<Instant>) {
|
|
self.set_window(duration_fraction(self.window, 3, 2).duration, newest);
|
|
}
|
|
|
|
fn reset_window(&mut self, newest: Option<Instant>) {
|
|
self.set_window(DEFAULT_GRAPH_WINDOW, newest);
|
|
}
|
|
|
|
fn set_window(&mut self, window: Duration, newest: Option<Instant>) {
|
|
self.window = window.clamp(MIN_GRAPH_WINDOW, MAX_GRAPH_WINDOW);
|
|
if let GraphViewportMode::Historical { end } = &mut self.mode {
|
|
if let Some(newest) = newest {
|
|
*end = (*end).min(newest);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn pan_by(
|
|
&mut self,
|
|
delta: SignedViewportDuration,
|
|
oldest: Option<Instant>,
|
|
newest: Option<Instant>,
|
|
) {
|
|
let Some(newest) = newest else {
|
|
return;
|
|
};
|
|
let current_end = match self.mode {
|
|
GraphViewportMode::Follow => newest,
|
|
GraphViewportMode::Historical { end } => end,
|
|
};
|
|
let mut end = delta.apply(current_end);
|
|
if let Some(oldest) = oldest {
|
|
end = end.max(oldest + self.window);
|
|
}
|
|
end = end.min(newest);
|
|
self.mode = GraphViewportMode::Historical { end };
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum GraphViewportMode {
|
|
Follow,
|
|
Historical { end: Instant },
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
struct GraphViewportRange {
|
|
start: Instant,
|
|
end: Instant,
|
|
window: Duration,
|
|
is_live: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
struct SignedViewportDuration {
|
|
duration: Duration,
|
|
negative: bool,
|
|
}
|
|
|
|
impl SignedViewportDuration {
|
|
fn apply(self, instant: Instant) -> Instant {
|
|
if self.negative {
|
|
instant.checked_sub(self.duration).unwrap_or(instant)
|
|
} else {
|
|
instant + self.duration
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::ops::Neg for SignedViewportDuration {
|
|
type Output = Self;
|
|
|
|
fn neg(self) -> Self::Output {
|
|
Self {
|
|
duration: self.duration,
|
|
negative: !self.negative,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn signed_duration(duration: Duration) -> SignedViewportDuration {
|
|
SignedViewportDuration {
|
|
duration,
|
|
negative: false,
|
|
}
|
|
}
|
|
|
|
fn duration_fraction(
|
|
duration: Duration,
|
|
numerator: u32,
|
|
denominator: u32,
|
|
) -> SignedViewportDuration {
|
|
let nanos =
|
|
duration.as_nanos().saturating_mul(u128::from(numerator)) / u128::from(denominator.max(1));
|
|
let nanos = nanos.max(1).min(u128::from(u64::MAX));
|
|
SignedViewportDuration {
|
|
duration: Duration::from_nanos(nanos as u64),
|
|
negative: false,
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum GraphMetric {
|
|
EffectiveRtt,
|
|
RawRtt,
|
|
AdjustedRtt,
|
|
ClientToServer,
|
|
ServerToClient,
|
|
ServerProcessing,
|
|
}
|
|
|
|
impl GraphMetric {
|
|
fn next(self) -> Self {
|
|
match self {
|
|
Self::EffectiveRtt => Self::RawRtt,
|
|
Self::RawRtt => Self::AdjustedRtt,
|
|
Self::AdjustedRtt => Self::ClientToServer,
|
|
Self::ClientToServer => Self::ServerToClient,
|
|
Self::ServerToClient => Self::ServerProcessing,
|
|
Self::ServerProcessing => Self::EffectiveRtt,
|
|
}
|
|
}
|
|
|
|
fn label(self) -> &'static str {
|
|
match self {
|
|
Self::EffectiveRtt => "effective RTT",
|
|
Self::RawRtt => "raw RTT",
|
|
Self::AdjustedRtt => "adjusted RTT",
|
|
Self::ClientToServer => "client to server",
|
|
Self::ServerToClient => "server to client",
|
|
Self::ServerProcessing => "server processing",
|
|
}
|
|
}
|
|
|
|
fn title(self) -> &'static str {
|
|
match self {
|
|
Self::EffectiveRtt => "effective RTT",
|
|
Self::RawRtt => "raw RTT",
|
|
Self::AdjustedRtt => "adjusted RTT",
|
|
Self::ClientToServer => "client to server delay",
|
|
Self::ServerToClient => "server to client delay",
|
|
Self::ServerProcessing => "server processing",
|
|
}
|
|
}
|
|
|
|
fn empty_message(self) -> &'static str {
|
|
match self {
|
|
Self::EffectiveRtt | Self::RawRtt => "waiting for primary replies",
|
|
Self::AdjustedRtt => "waiting for adjusted RTT samples",
|
|
Self::ClientToServer | Self::ServerToClient => "waiting for one-way delay samples",
|
|
Self::ServerProcessing => "waiting for server processing samples",
|
|
}
|
|
}
|
|
|
|
fn value_ns(self, sample: &GraphSample) -> Option<i128> {
|
|
match self {
|
|
Self::EffectiveRtt => Some(sample.effective_ns),
|
|
Self::RawRtt => Some(sample.raw_ns),
|
|
Self::AdjustedRtt => sample.adjusted_ns,
|
|
Self::ClientToServer => sample.client_to_server_ns,
|
|
Self::ServerToClient => sample.server_to_client_ns,
|
|
Self::ServerProcessing => sample.server_processing_ns,
|
|
}
|
|
}
|
|
|
|
fn axis_kind(self) -> ChartAxisKind {
|
|
match self {
|
|
Self::EffectiveRtt | Self::RawRtt | Self::ServerProcessing => {
|
|
ChartAxisKind::NonNegative
|
|
}
|
|
Self::AdjustedRtt | Self::ClientToServer | Self::ServerToClient => {
|
|
ChartAxisKind::Signed
|
|
}
|
|
}
|
|
}
|
|
|
|
fn style(self) -> Style {
|
|
match self {
|
|
Self::EffectiveRtt | Self::RawRtt | Self::AdjustedRtt => {
|
|
target_style(0).add_modifier(Modifier::BOLD)
|
|
}
|
|
Self::ClientToServer => Style::default().fg(Color::Magenta),
|
|
Self::ServerToClient => Style::default().fg(Color::LightBlue),
|
|
Self::ServerProcessing => Style::default().fg(Color::Green),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(super) struct GraphSample {
|
|
timestamp: Instant,
|
|
seq: u32,
|
|
effective_ns: i128,
|
|
raw_ns: i128,
|
|
adjusted_ns: Option<i128>,
|
|
client_to_server_ns: Option<i128>,
|
|
server_to_client_ns: Option<i128>,
|
|
server_processing_ns: Option<i128>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(super) struct LastSample {
|
|
seq: u32,
|
|
raw_ns: i128,
|
|
adjusted_ns: Option<i128>,
|
|
effective_ns: i128,
|
|
client_to_server_ns: Option<i128>,
|
|
server_to_client_ns: Option<i128>,
|
|
server_processing_ns: Option<i128>,
|
|
}
|
|
|
|
fn push_bounded<T>(items: &mut VecDeque<T>, item: T, limit: usize) {
|
|
if items.len() == limit {
|
|
items.pop_front();
|
|
} else if items.len() == items.capacity() && items.capacity() >= limit / 2 {
|
|
// Growth is naturally exponential; once we're within reach of `limit`,
|
|
// reserve exactly up to it instead of letting the next doubling step
|
|
// overshoot it for long-running, heavily populated buffers.
|
|
items.reserve_exact(limit - items.len());
|
|
}
|
|
items.push_back(item);
|
|
}
|
|
|
|
pub(super) fn draw_dashboard(frame: &mut Frame<'_>, state: &TuiState) {
|
|
let area = frame.area();
|
|
if area.width < MIN_WIDTH || area.height < MIN_HEIGHT {
|
|
frame.render_widget(too_small(), area);
|
|
return;
|
|
}
|
|
|
|
let snapshot = state.selected_snapshot();
|
|
match state.view {
|
|
TuiView::Graph => draw_graph_view(frame, area, state, &snapshot),
|
|
TuiView::Dashboard => draw_dashboard_view(frame, area, state, &snapshot),
|
|
}
|
|
}
|
|
|
|
pub(super) fn should_render(now: Instant, next_render: Instant, paused: bool, force: bool) -> bool {
|
|
force || (!paused && now >= next_render)
|
|
}
|
|
|
|
fn draw_graph_view(frame: &mut Frame<'_>, area: Rect, state: &TuiState, snapshot: &Snapshot) {
|
|
let header_height = if state.is_multi_target() { 7 } else { 3 };
|
|
let rows = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([
|
|
Constraint::Length(header_height),
|
|
Constraint::Min(10),
|
|
Constraint::Length(3),
|
|
])
|
|
.split(area);
|
|
|
|
if state.is_multi_target() {
|
|
frame.render_widget(target_table_panel(state, rows[0].height), rows[0]);
|
|
} else {
|
|
frame.render_widget(graph_summary_panel(state, snapshot), rows[0]);
|
|
}
|
|
render_graph_area(frame, rows[1], state);
|
|
frame.render_widget(status_line(state), rows[2]);
|
|
}
|
|
|
|
fn draw_dashboard_view(frame: &mut Frame<'_>, area: Rect, state: &TuiState, snapshot: &Snapshot) {
|
|
if area.width >= 110 && area.height >= 32 {
|
|
draw_large(frame, area, state, snapshot);
|
|
} else {
|
|
draw_compact(frame, area, state, snapshot);
|
|
}
|
|
}
|
|
|
|
fn draw_large(frame: &mut Frame<'_>, area: Rect, state: &TuiState, snapshot: &Snapshot) {
|
|
let rows = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([
|
|
Constraint::Length(8),
|
|
Constraint::Min(12),
|
|
Constraint::Length(9),
|
|
Constraint::Length(3),
|
|
])
|
|
.split(area);
|
|
let top = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)])
|
|
.split(rows[0]);
|
|
let middle = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(36), Constraint::Percentage(64)])
|
|
.split(rows[1]);
|
|
let bottom = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(55), Constraint::Percentage(45)])
|
|
.split(rows[2]);
|
|
|
|
frame.render_widget(header(state, HeaderDensity::Large), top[0]);
|
|
frame.render_widget(packet_panel(state, snapshot, top[1].height), top[1]);
|
|
frame.render_widget(timing_panel(state, snapshot), middle[0]);
|
|
render_graph_area(frame, middle[1], state);
|
|
frame.render_widget(recent_events_panel(state, bottom[0].height), bottom[0]);
|
|
frame.render_widget(sample_panel(state, snapshot), bottom[1]);
|
|
frame.render_widget(status_line(state), rows[3]);
|
|
}
|
|
|
|
fn draw_compact(frame: &mut Frame<'_>, area: Rect, state: &TuiState, snapshot: &Snapshot) {
|
|
let rows = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([
|
|
Constraint::Length(6),
|
|
Constraint::Length(7),
|
|
Constraint::Min(7),
|
|
Constraint::Length(3),
|
|
])
|
|
.split(area);
|
|
|
|
frame.render_widget(header(state, HeaderDensity::Compact), rows[0]);
|
|
frame.render_widget(packet_panel(state, snapshot, rows[1].height), rows[1]);
|
|
render_graph_area(frame, rows[2], state);
|
|
frame.render_widget(status_line(state), rows[3]);
|
|
}
|
|
|
|
fn too_small() -> Paragraph<'static> {
|
|
Paragraph::new(vec![
|
|
Line::from("terminal too small"),
|
|
Line::from("resize, or press q / Ctrl-C to quit gracefully"),
|
|
])
|
|
.block(Block::default().title("irtt-rs").borders(Borders::ALL))
|
|
.wrap(Wrap { trim: true })
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum HeaderDensity {
|
|
Large,
|
|
Compact,
|
|
}
|
|
|
|
fn header(state: &TuiState, density: HeaderDensity) -> Paragraph<'_> {
|
|
let selected = state.selected_target();
|
|
let selected_label = selected
|
|
.map(|target| target.label.as_str())
|
|
.unwrap_or("target");
|
|
let remote = selected
|
|
.and_then(|target| target.remote.as_deref())
|
|
.unwrap_or("-");
|
|
let session = selected
|
|
.and_then(|target| target.session.as_deref())
|
|
.unwrap_or("-");
|
|
let elapsed = format_span(state.started_at.elapsed());
|
|
let duration = format_optional_span(state.config.duration);
|
|
let mode = if state.config.duration.is_some() {
|
|
"finite"
|
|
} else {
|
|
"continuous"
|
|
};
|
|
let negotiated = selected
|
|
.and_then(|target| target.negotiated.as_ref())
|
|
.map(|params| format!("negotiated: {}", format_negotiated(params)))
|
|
.unwrap_or_else(|| "negotiated: -".to_owned());
|
|
let target_count = state.targets.len();
|
|
let pacing = state.config.pacing.label();
|
|
|
|
let mut lines = vec![
|
|
Line::from(vec![
|
|
Span::styled("irtt-rs", Style::default().add_modifier(Modifier::BOLD)),
|
|
Span::raw(format!(
|
|
" status: {} targets: {target_count} pacing: {pacing}",
|
|
state.status.label()
|
|
)),
|
|
]),
|
|
Line::from(if state.is_multi_target() {
|
|
format!("first target: {selected_label} remote: {remote}")
|
|
} else {
|
|
format!("remote: {remote}")
|
|
}),
|
|
];
|
|
match density {
|
|
HeaderDensity::Large => {
|
|
lines.extend([
|
|
Line::from(format!("session: {session}")),
|
|
Line::from(format!(
|
|
"mode: {mode} elapsed: {elapsed} duration: {duration}"
|
|
)),
|
|
Line::from(format!(
|
|
"interval: {} timeout: {}",
|
|
format_span(state.config.interval),
|
|
format_span(state.config.timeout)
|
|
)),
|
|
Line::from(negotiated),
|
|
]);
|
|
}
|
|
HeaderDensity::Compact => {
|
|
lines.extend([
|
|
Line::from(format!("session: {session}")),
|
|
Line::from(format!(
|
|
"{mode} elapsed: {elapsed} interval: {}",
|
|
format_span(state.config.interval)
|
|
)),
|
|
]);
|
|
}
|
|
}
|
|
|
|
Paragraph::new(lines)
|
|
.block(Block::default().title("session").borders(Borders::ALL))
|
|
.wrap(Wrap { trim: true })
|
|
}
|
|
|
|
fn graph_summary_panel(state: &TuiState, snapshot: &Snapshot) -> Paragraph<'static> {
|
|
let selected = state.selected_target();
|
|
let selected_label = selected
|
|
.map(|target| target.label.as_str())
|
|
.unwrap_or("target");
|
|
let remote = selected
|
|
.and_then(|target| target.remote.as_deref())
|
|
.unwrap_or("-");
|
|
let elapsed = format_span(state.started_at.elapsed());
|
|
let packets = snapshot.packets;
|
|
let last = state
|
|
.selected_target()
|
|
.and_then(|target| target.last_sample)
|
|
.map(|sample| format_optional_ns_i128(Some(sample.effective_ns)))
|
|
.unwrap_or_else(|| "-".to_owned());
|
|
|
|
let target_context = if state.is_multi_target() {
|
|
format!("first target {selected_label} remote {remote}")
|
|
} else {
|
|
format!("remote {remote}")
|
|
};
|
|
|
|
Paragraph::new(Line::from(format!(
|
|
"irtt-rs {} {target_context} elapsed {elapsed} sent {} replies {} last {last}",
|
|
state.status.label(),
|
|
format_count(packets.packets_sent),
|
|
format_count(packets.unique_replies)
|
|
)))
|
|
.block(
|
|
Block::default()
|
|
.title(if state.is_multi_target() {
|
|
"session - first target"
|
|
} else {
|
|
"session"
|
|
})
|
|
.borders(Borders::ALL),
|
|
)
|
|
}
|
|
|
|
fn packet_panel(state: &TuiState, snapshot: &Snapshot, panel_height: u16) -> Paragraph<'static> {
|
|
if state.is_multi_target() {
|
|
return target_table_panel(state, panel_height);
|
|
}
|
|
|
|
let packets = snapshot.packets;
|
|
let loss = snapshot.loss;
|
|
let progress = state
|
|
.config
|
|
.target_probes
|
|
.map(|target| {
|
|
format!(
|
|
"{}/{} ({})",
|
|
packets.packets_sent,
|
|
target,
|
|
format_percent_ratio(packets.packets_sent, target)
|
|
)
|
|
})
|
|
.unwrap_or_else(|| "-".to_owned());
|
|
let elapsed = state.started_at.elapsed();
|
|
let recv_rate = format_rate(packets.unique_replies, elapsed);
|
|
|
|
Paragraph::new(vec![
|
|
Line::from(format!(
|
|
"sent {:>8} received {:>8} unique {:>8}",
|
|
format_count(packets.packets_sent),
|
|
format_count(packets.packets_received),
|
|
format_count(packets.unique_replies)
|
|
)),
|
|
Line::from(format!(
|
|
"lost {:>8} duplicates {:>6} late {:>10} warnings {:>6}",
|
|
format_count(loss.lost_packets),
|
|
format_count(packets.duplicates),
|
|
format_count(packets.late_packets),
|
|
format_count(snapshot.events.warning_events)
|
|
)),
|
|
Line::from(format!(
|
|
"loss {:>8} reply rate {:>10} progress {progress}",
|
|
format_percent(loss.packet_loss_percent),
|
|
recv_rate
|
|
)),
|
|
Line::from(format!(
|
|
"bytes sent {} received {}",
|
|
format_count(packets.bytes_sent),
|
|
format_count(packets.bytes_received)
|
|
)),
|
|
Line::from(format!(
|
|
"server received {} window {}",
|
|
format_optional_count(packets.server_packets_received),
|
|
format_optional_hex(packets.server_received_window)
|
|
)),
|
|
])
|
|
.block(Block::default().title("packets").borders(Borders::ALL))
|
|
.wrap(Wrap { trim: true })
|
|
}
|
|
|
|
fn target_table_panel(state: &TuiState, panel_height: u16) -> Paragraph<'static> {
|
|
let visible = usize::from(panel_height.saturating_sub(3));
|
|
let mut lines = vec![Line::from(format!(
|
|
"{:<16} {:<8} {:>9} {:>5} {:>4} {:>4} {:>4}",
|
|
"target", "status", "last", "loss", "dup", "late", "warn"
|
|
))];
|
|
|
|
for (idx, target) in state.targets.iter().enumerate().take(visible) {
|
|
let snapshot = target.stats.snapshot();
|
|
let last = target
|
|
.last_sample
|
|
.map(|sample| format_optional_ns_i128(Some(sample.effective_ns)))
|
|
.unwrap_or_else(|| "-".to_owned());
|
|
lines.push(Line::from(vec![
|
|
target_label_span(&target.label, idx, 16),
|
|
Span::raw(format!(
|
|
" {:<8} {:>9} {:>5} {:>4} {:>4} {:>4}",
|
|
target.status.label(),
|
|
last,
|
|
format_count(snapshot.loss.lost_packets),
|
|
format_count(snapshot.packets.duplicates),
|
|
format_count(snapshot.packets.late_packets),
|
|
format_count(snapshot.events.warning_events)
|
|
)),
|
|
]));
|
|
}
|
|
|
|
Paragraph::new(lines)
|
|
.block(Block::default().title("targets").borders(Borders::ALL))
|
|
.wrap(Wrap { trim: false })
|
|
}
|
|
|
|
fn target_label_span(label: &str, target_idx: usize, width: usize) -> Span<'static> {
|
|
Span::styled(
|
|
format!("{:<width$}", truncate(label, width), width = width),
|
|
target_style(target_idx).add_modifier(Modifier::BOLD),
|
|
)
|
|
}
|
|
|
|
fn timing_panel(state: &TuiState, snapshot: &Snapshot) -> Paragraph<'static> {
|
|
let mut lines = vec![Line::from(format!(
|
|
"{:<18} {:>5} {:>9} {:>9} {:>9} {:>9}",
|
|
"metric", "n", "min", "mean", "max", "stddev"
|
|
))];
|
|
push_time_line(&mut lines, "effective RTT", &snapshot.rtt.primary);
|
|
push_time_line(&mut lines, "raw RTT", &snapshot.rtt.raw);
|
|
push_time_line(&mut lines, "adjusted RTT", &snapshot.rtt.adjusted);
|
|
push_time_line(&mut lines, "IPDV/jitter", &snapshot.ipdv.round_trip);
|
|
push_time_line(&mut lines, "send IPDV", &snapshot.ipdv.send);
|
|
push_time_line(&mut lines, "receive IPDV", &snapshot.ipdv.receive);
|
|
push_time_line(&mut lines, "send delay", &snapshot.one_way_delay.send_delay);
|
|
push_time_line(
|
|
&mut lines,
|
|
"receive delay",
|
|
&snapshot.one_way_delay.receive_delay,
|
|
);
|
|
push_time_line(
|
|
&mut lines,
|
|
"server process",
|
|
&snapshot.server_processing.processing,
|
|
);
|
|
push_time_line(&mut lines, "send call", &snapshot.send_call);
|
|
push_time_line(&mut lines, "timer error", &snapshot.timer_error);
|
|
|
|
Paragraph::new(lines)
|
|
.block(
|
|
Block::default()
|
|
.title(if state.is_multi_target() {
|
|
"timing - first target"
|
|
} else {
|
|
"timing"
|
|
})
|
|
.borders(Borders::ALL),
|
|
)
|
|
.wrap(Wrap { trim: false })
|
|
}
|
|
|
|
fn render_graph_area(frame: &mut Frame<'_>, area: Rect, state: &TuiState) {
|
|
let viewport = state
|
|
.graph_viewport
|
|
.range(Instant::now(), state.newest_graph_sample_time());
|
|
if state.is_multi_target() {
|
|
render_multi_target_graph(frame, area, state, viewport);
|
|
return;
|
|
}
|
|
|
|
let history = state
|
|
.selected_target()
|
|
.map(|target| &target.graph_history)
|
|
.expect("TuiState always has at least one target");
|
|
let visible = visible_history_window(history, viewport);
|
|
let series = graph_series(state.graph_metric, &visible, viewport);
|
|
|
|
render_chart(
|
|
frame,
|
|
area,
|
|
&visible,
|
|
&series,
|
|
ChartRenderConfig {
|
|
metric: state.graph_metric,
|
|
context: graph_context(state, viewport),
|
|
viewport,
|
|
},
|
|
);
|
|
}
|
|
|
|
fn render_multi_target_graph(
|
|
frame: &mut Frame<'_>,
|
|
area: Rect,
|
|
state: &TuiState,
|
|
viewport: GraphViewportRange,
|
|
) {
|
|
let metric = state.graph_metric;
|
|
let series = state
|
|
.targets
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(idx, target)| target_metric_series(target, idx, viewport, metric))
|
|
.collect::<Vec<_>>();
|
|
if series.is_empty() {
|
|
frame.render_widget(
|
|
Paragraph::new(metric.empty_message())
|
|
.block(
|
|
Block::default()
|
|
.title(graph_chart_title(metric, &graph_context(state, viewport)))
|
|
.borders(Borders::ALL),
|
|
)
|
|
.wrap(Wrap { trim: true }),
|
|
area,
|
|
);
|
|
return;
|
|
}
|
|
|
|
let (min_y, max_y) = chart_y_bounds(&series, metric.axis_kind());
|
|
let datasets = chart_datasets(&series);
|
|
let chart = Chart::new(datasets)
|
|
.block(
|
|
Block::default()
|
|
.title(graph_chart_title(metric, &graph_context(state, viewport)))
|
|
.borders(Borders::ALL),
|
|
)
|
|
.x_axis(
|
|
Axis::default()
|
|
.bounds(viewport_x_bounds(viewport))
|
|
.labels(viewport_x_axis_labels(viewport))
|
|
.style(Style::default().fg(Color::Gray)),
|
|
)
|
|
.y_axis(
|
|
Axis::default()
|
|
.bounds([min_y, max_y])
|
|
.labels(y_axis_labels(min_y, max_y, y_axis_label_count(area.height)))
|
|
.style(Style::default().fg(Color::Gray)),
|
|
);
|
|
frame.render_widget(chart, area);
|
|
}
|
|
|
|
fn render_chart(
|
|
frame: &mut Frame<'_>,
|
|
area: Rect,
|
|
_visible: &[&GraphSample],
|
|
series: &[ChartSeries],
|
|
config: ChartRenderConfig,
|
|
) {
|
|
if series.is_empty() {
|
|
frame.render_widget(
|
|
Paragraph::new(config.metric.empty_message())
|
|
.block(
|
|
Block::default()
|
|
.title(graph_chart_title(config.metric, &config.context))
|
|
.borders(Borders::ALL),
|
|
)
|
|
.wrap(Wrap { trim: true }),
|
|
area,
|
|
);
|
|
return;
|
|
}
|
|
|
|
let (min_y, max_y) = chart_y_bounds(series, config.metric.axis_kind());
|
|
let datasets = chart_datasets(series);
|
|
let chart = Chart::new(datasets)
|
|
.block(
|
|
Block::default()
|
|
.title(graph_chart_title(config.metric, &config.context))
|
|
.borders(Borders::ALL),
|
|
)
|
|
.x_axis(
|
|
Axis::default()
|
|
.bounds(viewport_x_bounds(config.viewport))
|
|
.labels(viewport_x_axis_labels(config.viewport))
|
|
.style(Style::default().fg(Color::Gray)),
|
|
)
|
|
.y_axis(
|
|
Axis::default()
|
|
.bounds([min_y, max_y])
|
|
.labels(y_axis_labels(min_y, max_y, y_axis_label_count(area.height)))
|
|
.style(Style::default().fg(Color::Gray)),
|
|
);
|
|
frame.render_widget(chart, area);
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
struct ChartRenderConfig {
|
|
metric: GraphMetric,
|
|
context: String,
|
|
viewport: GraphViewportRange,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
struct ChartSeries {
|
|
name: String,
|
|
style: Style,
|
|
data: Vec<(f64, f64)>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum ChartAxisKind {
|
|
NonNegative,
|
|
Signed,
|
|
}
|
|
|
|
fn visible_history_window(
|
|
history: &VecDeque<GraphSample>,
|
|
viewport: GraphViewportRange,
|
|
) -> Vec<&GraphSample> {
|
|
history
|
|
.iter()
|
|
.filter(|sample| sample.timestamp >= viewport.start && sample.timestamp <= viewport.end)
|
|
.collect()
|
|
}
|
|
|
|
fn graph_series(
|
|
metric: GraphMetric,
|
|
visible: &[&GraphSample],
|
|
viewport: GraphViewportRange,
|
|
) -> Vec<ChartSeries> {
|
|
chart_series(metric, visible, viewport)
|
|
.into_iter()
|
|
.collect()
|
|
}
|
|
|
|
fn chart_series(
|
|
metric: GraphMetric,
|
|
visible: &[&GraphSample],
|
|
viewport: GraphViewportRange,
|
|
) -> Option<ChartSeries> {
|
|
let data: Vec<(f64, f64)> = visible
|
|
.iter()
|
|
.filter_map(|sample| {
|
|
metric
|
|
.value_ns(sample)
|
|
.map(|ns| (sample_x(sample, viewport), ns as f64 / 1_000_000.0))
|
|
})
|
|
.collect();
|
|
|
|
(!data.is_empty()).then_some(ChartSeries {
|
|
name: metric.label().to_owned(),
|
|
style: metric.style(),
|
|
data,
|
|
})
|
|
}
|
|
|
|
fn target_metric_series(
|
|
target: &TuiTargetState,
|
|
target_idx: usize,
|
|
viewport: GraphViewportRange,
|
|
metric: GraphMetric,
|
|
) -> Option<ChartSeries> {
|
|
let data = target
|
|
.graph_history
|
|
.iter()
|
|
.filter(|sample| sample.timestamp >= viewport.start && sample.timestamp <= viewport.end)
|
|
.filter_map(|sample| {
|
|
metric
|
|
.value_ns(sample)
|
|
.map(|ns| (sample_x(sample, viewport), ns as f64 / 1_000_000.0))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
(!data.is_empty()).then_some(ChartSeries {
|
|
name: target.label.clone(),
|
|
style: target_style(target_idx),
|
|
data,
|
|
})
|
|
}
|
|
|
|
fn sample_x(sample: &GraphSample, viewport: GraphViewportRange) -> f64 {
|
|
sample
|
|
.timestamp
|
|
.saturating_duration_since(viewport.start)
|
|
.as_secs_f64()
|
|
}
|
|
|
|
fn chart_datasets(series: &[ChartSeries]) -> Vec<Dataset<'_>> {
|
|
series
|
|
.iter()
|
|
.map(|series| {
|
|
Dataset::default()
|
|
.name(series.name.as_str())
|
|
.marker(symbols::Marker::Braille)
|
|
.graph_type(GraphType::Line)
|
|
.style(series.style)
|
|
.data(&series.data)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn target_style(idx: usize) -> Style {
|
|
const COLORS: [Color; 8] = [
|
|
Color::Cyan,
|
|
Color::Yellow,
|
|
Color::Green,
|
|
Color::Magenta,
|
|
Color::LightBlue,
|
|
Color::LightRed,
|
|
Color::LightGreen,
|
|
Color::White,
|
|
];
|
|
Style::default().fg(COLORS[idx % COLORS.len()])
|
|
}
|
|
|
|
fn chart_y_bounds(series: &[ChartSeries], axis_kind: ChartAxisKind) -> (f64, f64) {
|
|
let mut values = series.iter().flat_map(|series| {
|
|
series
|
|
.data
|
|
.iter()
|
|
.map(|(_, value)| match axis_kind {
|
|
ChartAxisKind::NonNegative => (*value).max(0.0),
|
|
ChartAxisKind::Signed => *value,
|
|
})
|
|
.filter(|value| value.is_finite())
|
|
});
|
|
let Some(first) = values.next() else {
|
|
return default_y_bounds(axis_kind);
|
|
};
|
|
|
|
let (mut min_y, mut max_y) = (first, first);
|
|
for value in values {
|
|
min_y = min_y.min(value);
|
|
max_y = max_y.max(value);
|
|
}
|
|
|
|
match axis_kind {
|
|
ChartAxisKind::NonNegative => padded_non_negative_chart_y_bounds(min_y, max_y),
|
|
ChartAxisKind::Signed => padded_signed_chart_y_bounds(min_y, max_y),
|
|
}
|
|
}
|
|
|
|
fn default_y_bounds(axis_kind: ChartAxisKind) -> (f64, f64) {
|
|
match axis_kind {
|
|
ChartAxisKind::NonNegative => (0.0, 10.0),
|
|
ChartAxisKind::Signed => (-1.0, 1.0),
|
|
}
|
|
}
|
|
|
|
fn padded_non_negative_chart_y_bounds(min_y: f64, max_y: f64) -> (f64, f64) {
|
|
let pad = chart_y_padding(min_y, max_y);
|
|
let lower = (min_y - pad).max(0.0);
|
|
let upper = max_y + pad;
|
|
if lower < upper {
|
|
(lower, upper)
|
|
} else {
|
|
(0.0, pad.max(1.0))
|
|
}
|
|
}
|
|
|
|
fn padded_signed_chart_y_bounds(mut min_y: f64, mut max_y: f64) -> (f64, f64) {
|
|
let pad = chart_y_padding(min_y, max_y);
|
|
min_y -= pad;
|
|
max_y += pad;
|
|
if min_y >= max_y {
|
|
(min_y - pad, max_y + pad)
|
|
} else {
|
|
(min_y, max_y)
|
|
}
|
|
}
|
|
|
|
fn chart_y_padding(min_y: f64, max_y: f64) -> f64 {
|
|
const MIN_PADDING_MS: f64 = 0.1;
|
|
let span = max_y - min_y;
|
|
if span <= f64::EPSILON {
|
|
(max_y.abs() * 0.1).max(MIN_PADDING_MS)
|
|
} else {
|
|
(span * 0.1).max(MIN_PADDING_MS)
|
|
}
|
|
}
|
|
|
|
fn viewport_x_bounds(viewport: GraphViewportRange) -> [f64; 2] {
|
|
[0.0, viewport.window.as_secs_f64().max(1.0)]
|
|
}
|
|
|
|
fn viewport_x_axis_labels(viewport: GraphViewportRange) -> Vec<Span<'static>> {
|
|
vec![
|
|
Span::raw(format!("-{}", format_span(viewport.window))),
|
|
Span::raw(if viewport.is_live { "live" } else { "end" }),
|
|
]
|
|
}
|
|
|
|
fn graph_context(state: &TuiState, viewport: GraphViewportRange) -> String {
|
|
format!(
|
|
"{} | window {}",
|
|
graph_viewport_status(state),
|
|
format_span(viewport.window)
|
|
)
|
|
}
|
|
|
|
fn graph_chart_title(metric: GraphMetric, context: &str) -> String {
|
|
format!("{} | {context}", metric.title())
|
|
}
|
|
|
|
fn y_axis_label_count(height: u16) -> usize {
|
|
let inner = height.saturating_sub(2);
|
|
if inner >= 14 {
|
|
7
|
|
} else if inner >= 9 {
|
|
5
|
|
} else if inner >= 5 {
|
|
3
|
|
} else {
|
|
2
|
|
}
|
|
}
|
|
|
|
fn y_axis_labels(min_y: f64, max_y: f64, label_count: usize) -> Vec<Span<'static>> {
|
|
let label_count = label_count.max(2);
|
|
let step = (max_y - min_y) / (label_count - 1) as f64;
|
|
(0..label_count)
|
|
.map(|idx| Span::raw(format_axis_time_ms(min_y + step * idx as f64)))
|
|
.collect()
|
|
}
|
|
|
|
fn format_axis_time_ms(value_ms: f64) -> String {
|
|
let value_ms = if value_ms.abs() < 0.000_5 {
|
|
0.0
|
|
} else {
|
|
value_ms
|
|
};
|
|
let sign = if value_ms < 0.0 { "-" } else { "" };
|
|
let abs_ms = value_ms.abs();
|
|
if abs_ms < 1.0 {
|
|
let us = abs_ms * 1_000.0;
|
|
if us < 10.0 {
|
|
format!("{sign}{us:.1}µs")
|
|
} else {
|
|
format!("{sign}{us:.0}µs")
|
|
}
|
|
} else if abs_ms < 1_000.0 {
|
|
if abs_ms < 10.0 {
|
|
format!("{sign}{abs_ms:.2}ms")
|
|
} else if abs_ms < 100.0 {
|
|
format!("{sign}{abs_ms:.1}ms")
|
|
} else {
|
|
format!("{sign}{abs_ms:.0}ms")
|
|
}
|
|
} else {
|
|
let secs = abs_ms / 1_000.0;
|
|
if secs < 10.0 {
|
|
format!("{sign}{secs:.2}s")
|
|
} else if secs < 100.0 {
|
|
format!("{sign}{secs:.1}s")
|
|
} else {
|
|
format!("{sign}{secs:.0}s")
|
|
}
|
|
}
|
|
}
|
|
|
|
fn recent_events_panel(state: &TuiState, panel_height: u16) -> Paragraph<'_> {
|
|
let lines: Vec<Line<'_>> = state
|
|
.recent_events
|
|
.iter()
|
|
.rev()
|
|
.take(recent_events_visible_count(panel_height))
|
|
.map(|event| Line::from(event.as_str()))
|
|
.collect();
|
|
Paragraph::new(lines)
|
|
.block(
|
|
Block::default()
|
|
.title("recent events")
|
|
.borders(Borders::ALL),
|
|
)
|
|
.wrap(Wrap { trim: true })
|
|
}
|
|
|
|
fn recent_events_visible_count(panel_height: u16) -> usize {
|
|
usize::from(panel_height.saturating_sub(2))
|
|
}
|
|
|
|
fn sample_panel(state: &TuiState, snapshot: &Snapshot) -> Paragraph<'static> {
|
|
let selected = state.selected_target();
|
|
let last = selected.and_then(|target| target.last_sample);
|
|
let warning = selected
|
|
.and_then(|target| target.last_warning.clone())
|
|
.or_else(|| state.last_warning.clone())
|
|
.unwrap_or_else(|| "-".to_owned());
|
|
Paragraph::new(vec![
|
|
Line::from(format!(
|
|
"last seq: {}",
|
|
last.map(|sample| format_seq(sample.seq))
|
|
.unwrap_or_else(|| "-".to_owned())
|
|
)),
|
|
Line::from(format!(
|
|
"raw / adjusted / effective: {} / {} / {}",
|
|
last.map(|sample| format_optional_ns_i128(Some(sample.raw_ns)))
|
|
.unwrap_or_else(|| "-".to_owned()),
|
|
last.map(|sample| format_optional_ns_i128(sample.adjusted_ns))
|
|
.unwrap_or_else(|| "-".to_owned()),
|
|
last.map(|sample| format_optional_ns_i128(Some(sample.effective_ns)))
|
|
.unwrap_or_else(|| "-".to_owned())
|
|
)),
|
|
Line::from(format!(
|
|
"one-way c2s / s2c: {} / {}",
|
|
last.map(|sample| format_optional_ns_i128(sample.client_to_server_ns))
|
|
.unwrap_or_else(|| "-".to_owned()),
|
|
last.map(|sample| format_optional_ns_i128(sample.server_to_client_ns))
|
|
.unwrap_or_else(|| "-".to_owned())
|
|
)),
|
|
Line::from(format!(
|
|
"server processing: {}",
|
|
last.map(|sample| format_optional_ns_i128(sample.server_processing_ns))
|
|
.unwrap_or_else(|| "-".to_owned())
|
|
)),
|
|
Line::from(format!(
|
|
"events sent={} replies={} losses={} warnings={}",
|
|
format_count(snapshot.events.sent_events),
|
|
format_count(snapshot.events.echo_replies),
|
|
format_count(snapshot.events.loss_events),
|
|
format_count(snapshot.events.warning_events)
|
|
)),
|
|
Line::from(format!("last warning: {warning}")),
|
|
])
|
|
.block(
|
|
Block::default()
|
|
.title(if state.is_multi_target() {
|
|
"sample - first target"
|
|
} else {
|
|
"sample"
|
|
})
|
|
.borders(Borders::ALL),
|
|
)
|
|
.wrap(Wrap { trim: true })
|
|
}
|
|
|
|
fn status_line(state: &TuiState) -> Paragraph<'_> {
|
|
let paused = if state.paused { " display paused" } else { "" };
|
|
let quitting = if state.quit_requested {
|
|
" quit requested"
|
|
} else {
|
|
""
|
|
};
|
|
let view_hint = match state.view {
|
|
TuiView::Graph => "g dashboard",
|
|
TuiView::Dashboard => "g graph",
|
|
};
|
|
let incomplete = if state.dropped_events > 0 {
|
|
format!(" incomplete:dropped={}", state.dropped_events)
|
|
} else {
|
|
String::new()
|
|
};
|
|
let controls = if state.graph_viewport.mode == GraphViewportMode::Follow {
|
|
format!(
|
|
"q quit | r reset | p pause | {view_hint} | m metric | arrows pan | +/- zoom | 0 reset window"
|
|
)
|
|
} else {
|
|
format!(
|
|
"q quit | End live | {view_hint} | m metric | arrows pan | PgUp/PgDn page | +/- zoom | 0 reset window"
|
|
)
|
|
};
|
|
let lines = vec![Line::from(format!(
|
|
"{}{}{}{} | {}",
|
|
state.status.label(),
|
|
paused,
|
|
quitting,
|
|
incomplete,
|
|
controls
|
|
))];
|
|
Paragraph::new(lines).block(Block::default().borders(Borders::ALL))
|
|
}
|
|
|
|
fn graph_viewport_status(state: &TuiState) -> String {
|
|
match state.graph_viewport.mode {
|
|
GraphViewportMode::Follow => "live".to_owned(),
|
|
GraphViewportMode::Historical { end } => {
|
|
let now = Instant::now();
|
|
let live_end = state.newest_graph_sample_time().unwrap_or(now).max(now);
|
|
format!(
|
|
"history -{}",
|
|
format_span(live_end.saturating_duration_since(end))
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn push_time_line(lines: &mut Vec<Line<'_>>, label: &str, stats: &TimeStats) {
|
|
if stats.count == 0 {
|
|
lines.push(Line::from(format!(
|
|
"{label:<18} {:>5} {:>9} {:>9} {:>9} {:>9}",
|
|
0, "-", "-", "-", "-"
|
|
)));
|
|
return;
|
|
}
|
|
|
|
lines.push(Line::from(format!(
|
|
"{label:<18} {:>5} {:>9} {:>9} {:>9} {:>9}",
|
|
format_count(stats.count),
|
|
format_optional_ns_i128(stats.min_ns),
|
|
format_ns_f64(stats.mean_ns),
|
|
format_optional_ns_i128(stats.max_ns),
|
|
format_ns_f64(stats.stddev_ns())
|
|
)));
|
|
}
|
|
|
|
fn format_negotiated(negotiated: &NegotiationResult) -> String {
|
|
let params = &negotiated.accepted;
|
|
let duration = params.duration.map_or_else(
|
|
|| "-".to_owned(),
|
|
|duration| format_optional_ns_i128(Some(duration.as_nanos() as i128)),
|
|
);
|
|
let changes = if negotiated.changes.is_empty() {
|
|
"none".to_owned()
|
|
} else {
|
|
negotiated.changes.len().to_string()
|
|
};
|
|
format!(
|
|
"duration={} interval={} length={} clock={:?} timestamps={:?} stats={:?} changes={}",
|
|
duration,
|
|
format_optional_ns_i128(Some(params.interval.as_nanos() as i128)),
|
|
params.length,
|
|
params.clock,
|
|
params.stamp_at,
|
|
params.received_stats,
|
|
changes
|
|
)
|
|
}
|
|
|
|
fn format_optional_span(value: Option<Duration>) -> String {
|
|
value.map(format_span).unwrap_or_else(|| ABSENT.to_owned())
|
|
}
|
|
|
|
/// Format a wall-clock span shown in the TUI's headers, config lines, and
|
|
/// graph window labels.
|
|
///
|
|
/// Spans are read at a glance rather than compared numerically, so they are
|
|
/// deliberately coarser than the shared scalar policy: a zero span is `0s`,
|
|
/// whole seconds carry one decimal, and a span of a minute or more is written
|
|
/// as minutes and seconds. Below one second there is nothing to gain from a
|
|
/// second spelling, so the shared scalar vocabulary is used directly.
|
|
fn format_span(value: Duration) -> String {
|
|
if value.is_zero() {
|
|
return "0s".to_owned();
|
|
}
|
|
let nanos = value.as_nanos();
|
|
if nanos < 1_000_000_000 {
|
|
return format_duration(value);
|
|
}
|
|
let secs = value.as_secs();
|
|
if nanos < 60_000_000_000 {
|
|
format!("{:.1}s", nanos as f64 / 1_000_000_000.0)
|
|
} else {
|
|
format!("{}m{:02}s", secs / 60, secs % 60)
|
|
}
|
|
}
|
|
|
|
fn format_rate(count: u64, elapsed: Duration) -> String {
|
|
let secs = elapsed.as_secs_f64();
|
|
if secs <= f64::EPSILON {
|
|
ABSENT.to_owned()
|
|
} else {
|
|
format!("{:.2}/s", count as f64 / secs)
|
|
}
|
|
}
|
|
|
|
fn truncate(value: &str, max_chars: usize) -> String {
|
|
if value.chars().count() <= max_chars {
|
|
return value.to_owned();
|
|
}
|
|
value
|
|
.chars()
|
|
.take(max_chars.saturating_sub(1))
|
|
.chain(std::iter::once('~'))
|
|
.collect()
|
|
}
|
|
|
|
fn format_seq(value: u32) -> String {
|
|
value.to_string()
|
|
}
|
|
|
|
fn format_optional_hex(value: Option<u64>) -> String {
|
|
value
|
|
.map(|value| format!("0x{value:x}"))
|
|
.unwrap_or_else(|| ABSENT.to_owned())
|
|
}
|
|
|
|
fn duration_ns(value: Duration) -> i128 {
|
|
i128::try_from(value.as_nanos()).unwrap_or(i128::MAX)
|
|
}
|