Skip to content

Commit

Permalink
Move trace saving to optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
miolad committed Jul 21, 2023
1 parent 6edc403 commit 0fee8b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ metrics-common = { path = "../metrics-common" }
[build-dependencies]
libbpf-cargo = "0.20"
bindgen = "0.64"

[features]
save-traces = []
7 changes: 6 additions & 1 deletion main/src/actors/trace_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{time::{Duration, Instant}, fs::File};
use std::time::{Duration, Instant};
use actix::{Actor, Context, AsyncContext, Addr};
use anyhow::anyhow;
use libbpf_rs::MapFlags;
Expand All @@ -7,6 +7,8 @@ use tokio::sync::mpsc::Sender;
use crate::{ksyms::{Counts, KSyms}, common::{event_types_EVENT_MAX, self, event_types_EVENT_SOCK_SENDMSG, event_types_EVENT_NET_TX_SOFTIRQ, event_types_EVENT_NET_RX_SOFTIRQ}, bpf::ProgSkel};
use libc::{mmap, PROT_READ, MAP_SHARED, sysconf, _SC_CLK_TCK};
use super::{metrics_collector::MetricsCollector, MetricUpdate, SubmitUpdate};
#[cfg(feature = "save-traces")]
use std::fs::File;

/// Actor responsible for interacting with BPF via shared maps,
/// retrieve stack traces from the ring buffer, and analyze them
Expand Down Expand Up @@ -53,6 +55,7 @@ pub struct TraceAnalyzer {
/// previous update cycle
prev_total_energy: u64,

#[cfg(feature = "save-traces")]
traces_output_file: File
}

Expand Down Expand Up @@ -102,6 +105,7 @@ impl TraceAnalyzer {
prev_update_ts: Instant::now(),
prev_total_times: vec![vec![0; event_types_EVENT_MAX as _]; num_possible_cpus],
prev_total_energy: 0,
#[cfg(feature = "save-traces")]
traces_output_file: File::create("traces")?
})
}
Expand Down Expand Up @@ -162,6 +166,7 @@ impl TraceAnalyzer {
&self.ksyms,
trace_ptr.add(1),
trace_size as _,
#[cfg(feature = "save-traces")]
&mut self.traces_output_file
);
}
Expand Down
26 changes: 20 additions & 6 deletions main/src/ksyms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{io::{self, BufReader, BufRead, Write}, fs::File, ops::{Add, AddAssign}, collections::BTreeMap, iter::Sum};
use std::{io::{self, BufReader, BufRead}, fs::File, ops::{Add, AddAssign}, collections::BTreeMap, iter::Sum};
#[cfg(feature = "save-traces")]
use std::io::Write;

/// Helper to load and manage application-defined kernel symbols
#[derive(Default)]
Expand Down Expand Up @@ -174,24 +176,35 @@ impl KSyms {
impl Counts {
/// Iterate over the frames in the trace and accumulate the instances of the symbols in this Counts
#[inline]
pub unsafe fn acc_trace(&mut self, ksyms: &KSyms, trace_ptr: *const u64, max_frames: usize, mut output: impl Write) {
pub unsafe fn acc_trace(
&mut self,
ksyms: &KSyms,
trace_ptr: *const u64,
max_frames: usize,
#[cfg(feature = "save-traces")]
mut output: impl Write
) {
#[cfg(feature = "save-traces")]
let mut first_iter = true;

let mut c = Self::default();
let mut frame_props = PerFrameProps {
in_nf_hook: 0,
ip_rcv_finish: 0
};

let mut first_iter = true;

for frame_idx in 0..max_frames {
// Load stack frame
let ip = trace_ptr.add(frame_idx).read_volatile();
if ip == 0 {
break;
}

let _ = write!(output, "{}{ip}", if first_iter { "" } else { "," });
first_iter = false;
#[cfg(feature = "save-traces")]
{
let _ = write!(output, "{}{ip}", if first_iter { "" } else { "," });
first_iter = false;
}

// Check for known symbols
if let Some((_, KSymsVal { range_end, fun })) = ksyms
Expand All @@ -206,6 +219,7 @@ impl Counts {
}
}

#[cfg(feature = "save-traces")]
let _ = writeln!(output);

*self += c;
Expand Down

0 comments on commit 0fee8b6

Please sign in to comment.