[riot-devel] Compute the time elapsed when switching between two threads

Kaspar Schleiser kaspar at schleiser.de
Wed Nov 21 11:54:39 CET 2018

Hi Julien,

On 11/21/18 11:31 AM, Julien Gomez wrote:
> Indeed, using external equipment like an oscillograph was the first
> thing I did. 
> But I am looking to compute the context switching time only with software. 
> I am pretty sure I will have to tweak the kernel to get such
> measurements 😞 And even doing so, I will not have the closest value to
> the real elapsed time.

How exact do you expect the value to be?

As you've already realized, the benchmarks in tests/bench_* measure the
number of context switches within one second, for threads of the same
priority. (Actually, it counts double context switches.)

For the IPC tests, the priority of the threads doesn't matter, you
should be able to change them. But that should also not really change
the results.

You can easily calculate the time taken by one context switch.

On real MCUs, these numbers are fairly constant, and the overhead of the
actual test is minimal, so the results should be within a very low
single digit percentage of the actual time.

A less portable improvement would be to set up a periph timer with
maximum (e.g,, MCU) frequency. Then take the current timer value,
trigger a context switch (e.g., through "time_sleep()"), and in the
other thread, again as first thing take the timer value. That way you'd
get almost cycle-perfect context switch time measurements.


More information about the devel mailing list