Skip to content

Commit

Permalink
Merge pull request #27 from plaes/teleprobe-reduce-cpu
Browse files Browse the repository at this point in the history
RFC: Reduce CPU usage with std::thread::sleep
  • Loading branch information
Dirbaio authored May 29, 2024
2 parents 2df25d8 + be044c8 commit f29415d
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 f29415d

Please sign in to comment.