Skip to content

Commit

Permalink
Sleep for a short time when defmt buffer is empty to reduce CPU usage
Browse files Browse the repository at this point in the history
Interfacing with probe is a busy-loop, therefore the least we can do
is to sleep for a short period when defmt buffer read returns no data.

This lowers process' CPU usage to ~15% instead of previous full
utilization.
  • Loading branch information
plaes committed May 29, 2024
1 parent 2df25d8 commit be044c8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions teleprobe/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub const XPSR: RegisterId = RegisterId(16);
const THUMB_BIT: u32 = 1;
const TIMEOUT: Duration = Duration::from_secs(1);

const POLL_SLEEP_MILLIS: u64 = 100;

pub struct Options {
pub do_flash: bool,
pub deadline: Option<Instant>,
Expand Down Expand Up @@ -258,8 +260,14 @@ impl Runner {
let current_dir = std::env::current_dir()?;

let mut read_buf = [0; 1024];
let n = self.defmt.read(&mut sess.core(0).unwrap(), &mut read_buf)?;
self.defmt_stream.received(&read_buf[..n]);
match self.defmt.read(&mut sess.core(0).unwrap(), &mut read_buf)? {
0 => {
// Sleep to reduce CPU usage when defmt didn't return any data.
std::thread::sleep(Duration::from_millis(POLL_SLEEP_MILLIS));
return Ok(());
},
n => self.defmt_stream.received(&read_buf[..n]),
}

loop {
match self.defmt_stream.decode() {
Expand Down

0 comments on commit be044c8

Please sign in to comment.