SimpleMonitor optionally displays user_monitor stats (#970)

* Adding with_user_monitor() to SimpleMonitor

* Satisfy clippy

* Satisfy fmt and pylibafl

* Fix leading whitespace
This commit is contained in:
Patrick Gersch 2022-12-26 11:20:30 +01:00 committed by GitHub
parent 476cb7e7dc
commit 2b092f40fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -195,13 +195,16 @@ fn fuzz(
let file_null = File::open("/dev/null")?;
// 'While the stats are state, they are usually used in the broker - which is likely never restarted
let monitor = SimpleMonitor::new(|s| {
let monitor = SimpleMonitor::with_user_monitor(
|s| {
#[cfg(unix)]
writeln!(&mut stdout_cpy, "{}", s).unwrap();
writeln!(&mut stdout_cpy, "{s}").unwrap();
#[cfg(windows)]
println!("{s}");
writeln!(log.borrow_mut(), "{:?} {}", current_time(), s).unwrap();
});
writeln!(log.borrow_mut(), "{:?} {s}", current_time()).unwrap();
},
true,
);
let mut shmem_provider = StdShMemProvider::new()?;

View File

@ -16,6 +16,7 @@ pub use prometheus::PrometheusMonitor;
#[cfg(feature = "std")]
pub mod disk;
use alloc::{fmt::Debug, string::String, vec::Vec};
use core::fmt::Write;
use core::{fmt, time::Duration};
#[cfg(feature = "std")]
@ -386,6 +387,7 @@ where
{
print_fn: F,
start_time: Duration,
print_user_monitor: bool,
client_stats: Vec<ClientStats>,
}
@ -421,7 +423,7 @@ where
}
fn display(&mut self, event_msg: String, sender_id: u32) {
let fmt = format!(
let mut fmt = format!(
"[{} #{}] run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
event_msg,
sender_id,
@ -432,6 +434,14 @@ where
self.total_execs(),
self.execs_per_sec_pretty()
);
if self.print_user_monitor {
let client = self.client_stats_mut_for(sender_id);
for (key, val) in &client.user_monitor {
write!(fmt, ", {key}: {val}").unwrap();
}
}
(self.print_fn)(fmt);
// Only print perf monitor if the feature is enabled
@ -459,6 +469,7 @@ where
Self {
print_fn,
start_time: current_time(),
print_user_monitor: false,
client_stats: vec![],
}
}
@ -468,6 +479,17 @@ where
Self {
print_fn,
start_time,
print_user_monitor: false,
client_stats: vec![],
}
}
/// Creates the monitor that also prints the user monitor
pub fn with_user_monitor(print_fn: F, print_user_monitor: bool) -> Self {
Self {
print_fn,
start_time: current_time(),
print_user_monitor,
client_stats: vec![],
}
}
@ -971,6 +993,7 @@ pub mod pybind {
inner: SimpleMonitor {
print_fn: Box::new(closure),
start_time: self.inner.start_time,
print_user_monitor: false,
client_stats: self.inner.client_stats.clone(),
},
print_fn: self.print_fn.clone(),