Page 1 of 1

Measuring CPU usage

Posted: Tue Jul 07, 2020 1:00 am
by dannyf
Wrote some routines to measure CPU usage. Basic concept is shown here, on a cortex-m0 chips: https://dannyelectronics.wordpress.com/ ... -mx-chips/

it runs off of SysTick.

Ported it over to Arduino here: https://dannyelectronics.wordpress.com/ ... duino-way/

the code should run on a stm32duino board.

enjoy.

Re: Measuring CPU usage

Posted: Mon Aug 03, 2020 12:34 pm
by fpiSTM
Interesting. Thanks.
Maybe for other than M0 DWT could be used.

Re: Measuring CPU usage

Posted: Thu Aug 06, 2020 2:35 am
by dannyf
it would certainly work with DWT. the code is even simpler. but you lose a little bit of portability.

Re: Measuring CPU usage

Posted: Thu Aug 06, 2020 4:30 am
by ag123
as i often use a delay() implementation such as

Code: Select all

void delay(int count) {
  for(int i=0; i<count; i++) 
    asm("wfi");
}
this basically 'sleeps' and wait for the systick interrupt (1ms) i'm thinking another implementation is to measure the number of systick interrupts spent in such "WFI" delays() vs systick spend outside delay(), then the load may be something like

load = systicks outside delay() / total systicks

this probably isn't a best implementation as such a formula would lead to a mex load of 1.0

it would also be necessary to hook the systick interrupt so that it increases counts only based on systicks as in this case all interrupts are counted.
the sample period could be say for 1 sec which would be 1000 systicks per sec. a simple variable such as in_delay can be used to keep state.

Re: Measuring CPU usage

Posted: Sat Aug 08, 2020 12:59 pm
by dannyf
it should work as well. i think it boils down to 1) what we consider to be "active" vs. "inactive", and 2) how to make the assessment less expensive.

1. it seems to me that the number of ms spent in sleep is an indication of low cpu utilization. so i would count that instance and and use it to estimate cpu utilization;
2. try to avoid division as much as possible - less of an issue here but still it is going to be a good example. for example, count the sleep instances in 1024ms so you can use right shift.

combining the two, if you go into sleep 800 times in a 1024ms period, your cpu utilization is 20% (=1-800/1024).... shouldn't be difficult to implement.