How to find CPU Load for microcontrollers

Thread Starter

Vihaan@123

Joined Oct 7, 2025
231
Is it possible to find the CPU load for example PIC controllers? I mean i want to know if the controller is executing every task properly or not? It is just a vague idea to find out the CPU load and from that i can assess the overall system performance.
 

panic mode

Joined Oct 10, 2011
4,968
simple way is to use counter... its value is to be incremented at some point in the program, the faster the execution, the faster counter value changes. in fact that is how every platform measures performance, they are called "performance counters". that is what you see in task manager...
 

sagor

Joined Mar 10, 2019
1,049
Most PICs, especially the smaller ones, simply "Loop" forever, so they are executing code 100% all the time. That can be by design as there is nothing else for the PIC to do other than run continuously.
Some PICs can be put into a sleep mode to save power. At that point, they are not "running" per say, just idle and waiting for a wake up interrupt...
 

joeyd999

Joined Jun 6, 2011
6,271
In the cooperative multitasking framework that I use to develop my PIC projects, during debug I'll sometimes toggle a pin on when useful work is being done and off otherwise (i.e. during idle periods when no work is being done). The result is a (noisy) PWM output that exactly gauges MCU instruction cycle utilization -- at a cost of exactly 2 instruction cycles per task.
 

BobTPH

Joined Jun 5, 2013
11,503
Set up a small idle loop that executes when no other task is executing.

Set up a periodic timer interrupt that checks the return address in the handler. The percentage of times that the return address is not within the idle loop is the cpu utilization.
 

Irving

Joined Jan 30, 2016
5,107
Set up a small idle loop that executes when no other task is executing.

Set up a periodic timer interrupt that checks the return address in the handler. The percentage of times that the return address is not within the idle loop is the cpu utilization.
That's essentially the approach I use.
 

atferrari

Joined Jan 6, 2004
5,011
I work with PICs.

One pin is dedicated to output a short pulse every time the software clock elapses a period. This is running since I start developing a new design and kept unchanged forever.

Other pin/s is/are dedicated to show the start and / or the end of specific subroutines/s.
 

Irving

Joined Jan 30, 2016
5,107
In the cooperative multitasking framework that I use to develop my PIC projects, during debug I'll sometimes toggle a pin on when useful work is being done and off otherwise (i.e. during idle periods when no work is being done). The result is a (noisy) PWM output that exactly gauges MCU instruction cycle utilization -- at a cost of exactly 2 instruction cycles per task.

I work with PICs.

One pin is dedicated to output a short pulse every time the software clock elapses a period. This is running since I start developing a new design and kept unchanged forever.

Other pin/s is/are dedicated to show the start and / or the end of specific subroutines/s.
Probably not an option for Vihaan, the board he is working with pre-exists and he doesn't have direct access to the hardware.
 

Irving

Joined Jan 30, 2016
5,107
Hola Irving
Cannot see what you say in the OP.
That's because its not in the OP. ;) Elsewhere Vihaan shared some info on the hardware he's working with; it is likely he has no physical access to the MCU to access pins etc. and a number of questionable architecture and hardware decisions have been made without understanding or consideration of the system requirements or the software regime.
 
Last edited:

drjohsmith

Joined Dec 13, 2021
1,596
Is it possible to find the CPU load for example PIC controllers? I mean i want to know if the controller is executing every task properly or not? It is just a vague idea to find out the CPU load and from that i can assess the overall system performance.
Are you asking how many "free" instructions you have so you can dd extra code without something not being done in time or the watts consumed ?
 

Thread Starter

Vihaan@123

Joined Oct 7, 2025
231
I really do not know how to use the CPU load, i only wanted to know if all the program tasks are running within the time and i was thinking the CPU load calculation will help to assess. As recommended, I will try to implement the function.
 

drjohsmith

Joined Dec 13, 2021
1,596
I really do not know how to use the CPU load, i only wanted to know if all the program tasks are running within the time and i was thinking the CPU load calculation will help to assess. As recommended, I will try to implement the function.
Ok.
Trying to see if some code finishes fast enough, implies that the CPU is "waiting" for something to trigger it and it has a finish.
There are a few techniques dependent upon your experience and equipment.
A. Long handed , each instruction in the pic takes a defined number of clocks , thus by studying the assembly code ,the number of clocks needed can be calculated , and thus how long. We used to do this , but not now !.
B. There are debug tools built into some ide , which slow you to know the number of clock.
C. If you have a scope , set an io bit like a led low at start of function , high at end , measure the period .
D. Have a dummy routine , that's called when the routine you want to measure is NOT running , in it have counter , need to be big so it didn't wrap , periodically "print" to screen the counter . Gives idea of number of "free" clock cycles .
 
Top