Measuring CPU usage

Post here first, or if you can't find a relevant section!
Post Reply
dannyf
Posts: 25
Joined: Sat Jul 04, 2020 7:46 pm

Measuring CPU usage

Post 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.

User avatar
fpiSTM
Posts: 746
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 27
Location: Le Mans
Contact:

Re: Measuring CPU usage

Post by fpiSTM »

Interesting. Thanks.
Maybe for other than M0 DWT could be used.

dannyf
Posts: 25
Joined: Sat Jul 04, 2020 7:46 pm

Re: Measuring CPU usage

Post by dannyf »

it would certainly work with DWT. the code is even simpler. but you lose a little bit of portability.

ag123
Posts: 475
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: Measuring CPU usage

Post 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.

dannyf
Posts: 25
Joined: Sat Jul 04, 2020 7:46 pm

Re: Measuring CPU usage

Post 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.

Post Reply

Return to “General discussion”