wg icon indicating copy to clipboard operation
wg copied to clipboard

Benchmarking embedded rust code

Open KRACK-BIT opened this issue 2 years ago • 2 comments

I've been working on my own embedded rust project (a no_std KYBER implementation), and have been trying to run time consistency tests on my micro-controller. As such, I'm currently trying to validate that the number of clock cycles are constant for each of my critical functions, and I can measure the clock cycle time directly through cortex_m::Peripherals

However, I can't find any bench-marking framework which supports measuring this, or even any projects that take measurements (be they clock or real-world time) of a micro-controller's runtime.

As such, are there any good libraries/examples on profiling execution time on a micro-controller, and if not, are there any examples on how to write your own bench-marking framework for an embedded system?

KRACK-BIT avatar Apr 05 '23 19:04 KRACK-BIT

For cortex-m devices, the ITM can be used to do this if your microcontroller can support it. probe-rs supports this functionality via the CLI or via your own custom code.

Probe-rs's CLI has a trace command that will use the ITM to capture the cycle counts of all ISRs in your system, but it doesn't support bench marking arbitrary functions or sections (although I've been looking at adding such functionality).

Take a look at rtic-scope if you're using RTIC

ryan-summers avatar Apr 05 '23 19:04 ryan-summers

An approach has been mentioned in https://blog.japaric.io/rtfm-overhead/. I've been also using the external method using a GPIO quite successfully to do A/B comparisons of code and optimize it, though it won't yield exact measurements.

There have been some experiments with https://github.com/orbcode/orbuculum a long time ago but I can't find any reference. Using that software with ITM/ETM has the potential of yielding professional level results and details at the expense of a non-trivial setup and potentially research work.

therealprof avatar Apr 14 '23 06:04 therealprof

Closing this as part of the 2024 triage. I'd definitely recommend following up with probe-rs trace, and the other options mentioned above.

If anyone discovers this in the future or has a better solution - please feel free to submit it to the https://github.com/rust-embedded/awesome-embedded-rust list!

jamesmunns avatar Jun 11 '24 18:06 jamesmunns