From 0fee8b6edbf95ab9838a363b87687abb985b8093 Mon Sep 17 00:00:00 2001 From: Davide Miola Date: Fri, 21 Jul 2023 07:32:06 +0000 Subject: [PATCH] Move trace saving to optional feature --- main/Cargo.toml | 3 +++ main/src/actors/trace_analyzer.rs | 7 ++++++- main/src/ksyms.rs | 26 ++++++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/main/Cargo.toml b/main/Cargo.toml index 01c8583..394afeb 100644 --- a/main/Cargo.toml +++ b/main/Cargo.toml @@ -21,3 +21,6 @@ metrics-common = { path = "../metrics-common" } [build-dependencies] libbpf-cargo = "0.20" bindgen = "0.64" + +[features] +save-traces = [] diff --git a/main/src/actors/trace_analyzer.rs b/main/src/actors/trace_analyzer.rs index bb14868..b89dbd4 100644 --- a/main/src/actors/trace_analyzer.rs +++ b/main/src/actors/trace_analyzer.rs @@ -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; @@ -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 @@ -53,6 +55,7 @@ pub struct TraceAnalyzer { /// previous update cycle prev_total_energy: u64, + #[cfg(feature = "save-traces")] traces_output_file: File } @@ -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")? }) } @@ -162,6 +166,7 @@ impl TraceAnalyzer { &self.ksyms, trace_ptr.add(1), trace_size as _, + #[cfg(feature = "save-traces")] &mut self.traces_output_file ); } diff --git a/main/src/ksyms.rs b/main/src/ksyms.rs index aba75c6..ffee601 100644 --- a/main/src/ksyms.rs +++ b/main/src/ksyms.rs @@ -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)] @@ -174,15 +176,23 @@ 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(); @@ -190,8 +200,11 @@ impl Counts { 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 @@ -206,6 +219,7 @@ impl Counts { } } + #[cfg(feature = "save-traces")] let _ = writeln!(output); *self += c;