From 58e57621d1e11a1bafbd990c3aa10c824771d790 Mon Sep 17 00:00:00 2001 From: bmc-msft <41130664+bmc-msft@users.noreply.github.com> Date: Fri, 16 Jul 2021 13:32:26 -0400 Subject: [PATCH] add a short sleep in the supervisor's event loop to reduce CPU usage (#1080) --- src/agent/onefuzz-supervisor/src/agent.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/agent/onefuzz-supervisor/src/agent.rs b/src/agent/onefuzz-supervisor/src/agent.rs index 2827c0fc4..9239132cb 100644 --- a/src/agent/onefuzz-supervisor/src/agent.rs +++ b/src/agent/onefuzz-supervisor/src/agent.rs @@ -14,6 +14,7 @@ use crate::work::IWorkQueue; use crate::worker::IWorkerRunner; const PENDING_COMMANDS_DELAY: time::Duration = time::Duration::from_secs(10); +const BUSY_DELAY: time::Duration = time::Duration::from_secs(1); pub struct Agent { coordinator: Box, @@ -231,6 +232,15 @@ impl Agent { async fn busy(&mut self, state: State) -> Result { self.emit_state_update_if_changed(StateUpdateEvent::Busy) .await?; + + // Without this sleep, the `Agent.run` loop turns into an extremely tight loop calling + // `wait4` of the running agents. This sleep adds a small window to allow the rest of the + // system to work. Emperical testing shows this has a significant reduction in CPU use. + // + // TODO: The worker_runner monitoring needs to be turned into something event driven. Once + // that is done, this sleep should be removed. + time::sleep(BUSY_DELAY).await; + let mut events = vec![]; let updated = state .update(&mut events, self.worker_runner.as_mut())