Skip to content

Commit

Permalink
feat: added max_flows to Trace and use to restriction the creatio…
Browse files Browse the repository at this point in the history
…n of new flows
  • Loading branch information
fujiapple852 committed Mar 2, 2024
1 parent ed1efac commit 9b1aabd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ pub struct Backend {

impl Backend {
/// Create a tracing `Backend`.
pub fn new(tracer_config: Config, channel_config: ChannelConfig, max_samples: usize) -> Self {
pub fn new(
tracer_config: Config,
channel_config: ChannelConfig,
max_samples: usize,
max_flows: usize,
) -> Self {
Self {
tracer_config,
channel_config,
trace: Arc::new(RwLock::new(Trace::new(max_samples))),
trace: Arc::new(RwLock::new(Trace::new(max_samples, max_flows))),
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/backend/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ use trippy::tracing::{Extensions, ProbeState, Round, TimeToLive, TracerRound};
/// The state of all hops in a trace.
#[derive(Debug, Clone)]
pub struct Trace {
/// The maximum number of samples to record per hop.
///
/// Once the maximum number of samples has been reached the oldest sample
/// is discarded (FIFO).
max_samples: usize,
/// The maximum number of flows to record.
///
/// Once the maximum number of flows has been reached no new flows will be
/// created, existing flows are updated and are never removed.
max_flows: usize,
/// The flow id for the current round.
round_flow_id: FlowId,
/// Tracing data per registered flow id.
Expand All @@ -23,12 +32,13 @@ pub struct Trace {

impl Trace {
/// Create a new `Trace`.
pub fn new(max_samples: usize) -> Self {
pub fn new(max_samples: usize, max_flows: usize) -> Self {
Self {
trace_data: once((Self::default_flow_id(), TraceData::new(max_samples)))
.collect::<HashMap<FlowId, TraceData>>(),
round_flow_id: Self::default_flow_id(),
max_samples,
max_flows,
registry: FlowRegistry::new(),
error: None,
}
Expand Down Expand Up @@ -105,10 +115,12 @@ impl Trace {
})
.take(usize::from(round.largest_ttl.0)),
);
let flow_id = self.registry.register(flow);
self.round_flow_id = flow_id;
self.update_trace_flow(Self::default_flow_id(), round);
self.update_trace_flow(flow_id, round);
if self.registry.flows().len() < self.max_flows {
let flow_id = self.registry.register(flow);
self.round_flow_id = flow_id;
self.update_trace_flow(flow_id, round);
}
}

fn update_trace_flow(&mut self, flow_id: FlowId, round: &TracerRound<'_>) {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl TuiApp {
trace_info: Vec<TraceInfo>,
) -> Self {
Self {
selected_tracer_data: Trace::new(tui_config.max_samples),
selected_tracer_data: Trace::new(tui_config.max_samples, tui_config.max_flows),
trace_info,
tui_config,
table_state: TableState::default(),
Expand Down Expand Up @@ -88,7 +88,7 @@ impl TuiApp {

pub fn clear_trace_data(&mut self) {
*self.trace_info[self.trace_selected].data.write() =
Trace::new(self.tui_config.max_samples);
Trace::new(self.tui_config.max_samples, self.tui_config.max_flows);
}

pub fn selected_hop_or_target(&self) -> &Hop {
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ fn start_tracer(
};
let channel_config = make_channel_config(cfg, source_addr, target_addr);
let tracer_config = make_tracer_config(cfg, target_addr, trace_identifier)?;
let backend = Backend::new(tracer_config, channel_config, cfg.tui_max_samples);
let backend = Backend::new(
tracer_config,
channel_config,
cfg.tui_max_samples,
cfg.tui_max_flows,
);
let trace_data = backend.trace();
thread::Builder::new()
.name(format!("tracer-{}", tracer_config.trace_identifier.0))
Expand Down

0 comments on commit 9b1aabd

Please sign in to comment.