6 years, 5 months ago.

Measuring amount of time in idle thread

What is currently the most effective way to do this? Goal is to see how much excess capacity we have with our MCU when running our application.

1 Answer

6 years, 5 months ago.

Hi Kevin,

The best way to do this is to create a variable counter that count how many CPU clock spend in the thread. I would recommend you use inline C assembly code so that you can get the most accurate idle time: Something like the example below would do:

Inline assembly counter

int main(){
      register unsigned int counter asm("r4"); //making register r4 as a local variable
      asm volatile ( 
        "b1: add     r4, #1 \n\t" 
        "jmp b1 \n\t"
      );
}

Please let me know if you have any questions!

- Peter, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

So main is the idle thread?

posted by Kevin McQuown 09 Jul 2018

Also, in looking through the mbed structs over the weekend I can across mbed_stats_cpu_t. One of the items in the struct is us_timestamp_t_idle_time which is documented ass the Time spent in idle thread since system is up and running. I'm gessing that the RTOS is doing this for me here. Not sure what function to call to get this struct populated?

posted by Kevin McQuown 09 Jul 2018

You just put this code to the parent thread. Usually it is the main thread because it where you call and initialize other thread. However, if you want to measure how much time your CPU is idle then you can follow this example here: https://github.com/ARMmbed/mbed-os-example-cpu-stats

-Peter, Team Mbed

posted by Peter Nguyen 09 Jul 2018