From 16af5debbee436d83d743d4e59847fe1dfbc23a6 Mon Sep 17 00:00:00 2001 From: "Dongjia \"toka\" Zhang" Date: Fri, 24 Nov 2023 13:50:57 +0100 Subject: [PATCH] Fix aggreagator ui (#1693) * fix * more --------- Co-authored-by: Andrea Fioraldi --- libafl/src/monitors/mod.rs | 35 ++++++++++++++++++++++++------- libafl/src/monitors/multi.rs | 14 ++++++------- libafl/src/monitors/prometheus.rs | 1 + libafl_bolts/Cargo.toml | 4 ---- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/libafl/src/monitors/mod.rs b/libafl/src/monitors/mod.rs index 7bc5d446a1..dd1614d31e 100644 --- a/libafl/src/monitors/mod.rs +++ b/libafl/src/monitors/mod.rs @@ -163,6 +163,8 @@ pub enum UserStatsValue { String(String), /// A ratio of two values Ratio(u64, u64), + /// Percent + Percent(f64), } impl UserStatsValue { @@ -170,7 +172,7 @@ impl UserStatsValue { #[must_use] pub fn is_numeric(&self) -> bool { match &self { - Self::Number(_) | Self::Float(_) | Self::Ratio(_, _) => true, + Self::Number(_) | Self::Float(_) | Self::Ratio(_, _) | Self::Percent(_) => true, Self::String(_) => false, } } @@ -181,7 +183,8 @@ impl UserStatsValue { match self { Self::Number(x) => Some(Self::Float(*x as f64 / divisor as f64)), Self::Float(x) => Some(Self::Float(*x / divisor as f64)), - Self::Ratio(x, y) => Some(Self::Float((*x as f64 / divisor as f64) / *y as f64)), + Self::Percent(x) => Some(Self::Percent(*x / divisor as f64)), + Self::Ratio(x, y) => Some(Self::Percent((*x as f64 / divisor as f64) / *y as f64)), Self::String(_) => None, } } @@ -208,9 +211,16 @@ impl UserStatsValue { let first = *x as f64 / *a as f64; let second = *y as f64 / *b as f64; if first > second { - Some(Self::Float(first)) + Some(Self::Percent(first)) } else { - Some(Self::Float(second)) + Some(Self::Percent(second)) + } + } + (Self::Percent(x), Self::Percent(y)) => { + if y > x { + Some(Self::Percent(*y)) + } else { + Some(Self::Percent(*x)) } } _ => None, @@ -239,9 +249,16 @@ impl UserStatsValue { let first = *x as f64 / *a as f64; let second = *y as f64 / *b as f64; if first > second { - Some(Self::Float(second)) + Some(Self::Percent(second)) } else { - Some(Self::Float(first)) + Some(Self::Percent(first)) + } + } + (Self::Percent(x), Self::Percent(y)) => { + if y > x { + Some(Self::Percent(*x)) + } else { + Some(Self::Percent(*y)) } } _ => None, @@ -254,10 +271,11 @@ impl UserStatsValue { match (self, other) { (Self::Number(x), Self::Number(y)) => Some(Self::Number(*x + *y)), (Self::Float(x), Self::Float(y)) => Some(Self::Float(*x + *y)), + (Self::Percent(x), Self::Percent(y)) => Some(Self::Percent(*x + *y)), (Self::Ratio(x, a), Self::Ratio(y, b)) => { let first = *x as f64 / *a as f64; let second = *y as f64 / *b as f64; - Some(Self::Float(first + second)) + Some(Self::Percent(first + second)) } _ => None, } @@ -274,7 +292,8 @@ impl fmt::Display for UserStatsValue { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self { UserStatsValue::Number(n) => write!(f, "{n}"), - UserStatsValue::Float(n) => write!(f, "{n}"), + UserStatsValue::Float(n) => write!(f, "{}", prettify_float(*n)), + UserStatsValue::Percent(n) => write!(f, "{:.3}%", n * 100.0), UserStatsValue::String(s) => write!(f, "{s}"), UserStatsValue::Ratio(a, b) => { if *b == 0 { diff --git a/libafl/src/monitors/multi.rs b/libafl/src/monitors/multi.rs index 13621ab6a6..dfc2db607c 100644 --- a/libafl/src/monitors/multi.rs +++ b/libafl/src/monitors/multi.rs @@ -74,7 +74,7 @@ where String::new() }; let head = format!("{event_msg}{pad} {sender}"); - let global_fmt = format!( + let mut global_fmt = format!( "[{}] (GLOBAL) run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}", head, format_duration_hms(&(current_time() - self.start_time)), @@ -84,6 +84,12 @@ where self.total_execs(), self.execs_per_sec_pretty() ); + let mut aggregated_fmt = " (Aggregated):".to_string(); + for (key, val) in &self.aggregator.aggregated { + write!(aggregated_fmt, " {key}: {val}").unwrap(); + } + write!(global_fmt, "{aggregated_fmt}").unwrap(); + (self.print_fn)(global_fmt); self.client_stats_insert(sender_id); @@ -101,12 +107,6 @@ where } (self.print_fn)(fmt); - let mut aggregated_fmt = "(Aggregated): ".to_string(); - for (key, val) in &self.aggregator.aggregated { - write!(aggregated_fmt, " {key}: {val}").unwrap(); - } - (self.print_fn)(aggregated_fmt); - // Only print perf monitor if the feature is enabled #[cfg(feature = "introspection")] { diff --git a/libafl/src/monitors/prometheus.rs b/libafl/src/monitors/prometheus.rs index 272d1e15fd..ab6e3b904c 100644 --- a/libafl/src/monitors/prometheus.rs +++ b/libafl/src/monitors/prometheus.rs @@ -178,6 +178,7 @@ where UserStatsValue::Float(f) => *f, UserStatsValue::String(_s) => 0.0, UserStatsValue::Ratio(a, b) => (*a as f64 / *b as f64) * 100.0, + UserStatsValue::Percent(p) => *p * 100.0, }; self.custom_stat .get_or_create(&Labels { diff --git a/libafl_bolts/Cargo.toml b/libafl_bolts/Cargo.toml index 500d214e9b..da641da1a9 100644 --- a/libafl_bolts/Cargo.toml +++ b/libafl_bolts/Cargo.toml @@ -84,10 +84,6 @@ llmp_small_maps = ["alloc"] [build-dependencies] rustversion = "1.0" -[dev-dependencies] -# clippy-suggested optimised byte counter -bytecount = "0.6.3" - [dependencies] libafl_derive = { version = "0.11.1", optional = true, path = "../libafl_derive" }