Embassy timer very slow #1408
-
I'm trying to use embassy with sub-millisecond timers, but it seems the overhead of scheduling the future (and thus minimum possible delay) is 1.4ms, which is a really long time. Is there any way to get higher precision async delays? I probably need something that can do at least 100us, but lower would be better/safer. Here's a really simple test program I was using to measure on an ESP32-C6: #![no_std]
#![no_main]
use esp_backtrace as _;
use embassy_time::{Timer, Instant};
use embassy_executor::Spawner;
use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, embassy::{self}};
use esp_hal::timer::TimerGroup;
use esp_println::println;
#[main]
async fn main(spawner: Spawner) {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::max(system.clock_control).freeze();
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);
let first = Instant::now();
let second = Instant::now();
println!("first={} second={} diff={}", first, second, (second - first).as_micros());
let first = Instant::now();
Timer::after_nanos(0).await;
let second = Instant::now();
println!("first={} second={} diff={}", first, second, (second - first).as_micros());
loop{}
} Which produces:
The first line shows the overhead of just calling |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
That is indeed very slow, for instance the latency on serving an interrupt since #1310 (comment) is around 10us, so 1400us is huge. I imagine there is a bug here some where. Perhaps the timer isn't firing if it's past the target count, and instead waiting for the timer to loop back around? |
Beta Was this translation helpful? Give feedback.
-
I am surprised it takes 26us to call |
Beta Was this translation helpful? Give feedback.
-
I just ran this example using the
Using the
So I will say it's pretty likely that Closing this as I think it's a non-issue, however if my analysis is incorrect please feel free to re-open the discussion or to create a new one. |
Beta Was this translation helpful? Give feedback.
-
The warning (maybe phrased in a very scary way) would be really nice. We have that in Having a robust solution to emit warnings would be a good thing. Mabe something like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=bbb23848b4b04193fa712539ab3fb17d |
Beta Was this translation helpful? Give feedback.
I just ran this example using the
release
profile and got the following results (ran multiple times, got the same results):Using the
dev
profile I get similar results to OP:So I will say it's pretty likely that
release
is not being used in this case. I guess we should prioritize #1261.Closing this as I think it's a non-issue, however if my analysis is incorrect please feel free to re-open the discussion or to create a new one.