Skip to content

Commit

Permalink
Improve OnDiskTOMLMonitor (AFLplusplus#2125)
Browse files Browse the repository at this point in the history
* Allow for more frequent updates of TOML monitor

* Don't skip first client

* Reduce code duplication

* Immediately write first TOML file

* Rust fmt

* Use same client numbering as other monitors

* Fmt
  • Loading branch information
clesmian authored Apr 30, 2024
1 parent b49ab99 commit 61ac4ea
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions libafl/src/monitors/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ where
base: M,
filename: PathBuf,
last_update: Duration,
update_interval: Duration,
}

impl<M> Monitor for OnDiskTOMLMonitor<M>
Expand Down Expand Up @@ -55,7 +56,7 @@ where
fn display(&mut self, event_msg: &str, sender_id: ClientId) {
let cur_time = current_time();

if (cur_time - self.last_update).as_secs() >= 60 {
if cur_time - self.last_update >= self.update_interval {
self.last_update = cur_time;

let mut file = File::create(&self.filename).expect("Failed to open the TOML file");
Expand All @@ -80,7 +81,7 @@ exec_sec = {}
)
.expect("Failed to write to the TOML file");

for (i, client) in self.client_stats_mut().iter_mut().skip(1).enumerate() {
for (i, client) in self.client_stats_mut().iter_mut().enumerate() {
let exec_sec = client.execs_per_sec(cur_time);

write!(
Expand All @@ -92,11 +93,7 @@ objectives = {}
executions = {}
exec_sec = {}
",
i + 1,
client.corpus_size,
client.objective_size,
client.executions,
exec_sec
i, client.corpus_size, client.objective_size, client.executions, exec_sec
)
.expect("Failed to write to the TOML file");

Expand Down Expand Up @@ -125,13 +122,23 @@ where
/// Create new [`OnDiskTOMLMonitor`]
#[must_use]
pub fn new<P>(filename: P, base: M) -> Self
where
P: Into<PathBuf>,
{
Self::with_update_interval(filename, base, Duration::from_secs(60))
}

/// Create new [`OnDiskTOMLMonitor`] with custom update interval
#[must_use]
pub fn with_update_interval<P>(filename: P, base: M, update_interval: Duration) -> Self
where
P: Into<PathBuf>,
{
Self {
base,
filename: filename.into(),
last_update: current_time(),
last_update: current_time() - update_interval,
update_interval,
}
}
}
Expand Down

0 comments on commit 61ac4ea

Please sign in to comment.