Hello chrisc@rmlgroup.com ,
We do not have a block that can tell you the CPU load, but I can think of ways in which you could achieve that.
Lets look at how an application generated by our toolbox looks like (main parts in regards with execution time):

We have the following:
- Step – a function called periodically, with a frequency controlled by the Discrete Step value set in the model
- Interrupts – asynchronous events that can be further grouped into 2 categories:
- Events that occur at a known period – e.g. a LPIT block that is used to trigger the Fast loop system for motor control applications – the important part is that you control the frequency at which this event occurs
- Events that have an unknown period – e.g. receiving a message via UART/SPI/I2C/etc. – this is something that you have no control over, it can happen any time
- Initialization – this is an important part of the application, but for CPU load it is of no importance
- IDLE – this is the time period in which the Step function finished execution and awaits to be re-triggered and you have no ISR to execute (no such events were generated)
Looking at these, I see 2 options:
- Either calculate the IDLE time between 2 events that trigger the Step function to be executed
- Either calculate the time you spend in Step + time spent in ISRs, all before a 2nd start of the Step function
For the 1st option, it’s harder to implement just in Simulink, so lets explore the 2nd option.
To get the CPU Load, we’ll calculate it like this:
CPU Load = Step Load + ISR Load
For this, we’ll start with the Step time – you can use the profiler block, put it into the top model and you’ll get the total execution time of the Step function. This is easy enough:
Step load = step execution time / discrete step value
Now onto the ISRs – this is where it gets a bit more complicated. I have grouped them into 2 categories (see above) for the following reason: even if we can calculate the execution time using the same Profiler blocks, we do not know the frequency of all events – for those controlled by the LPIT blocks, we can do the same as for the Step; but for the other, we can’t include them into our calculation.
I’m thinking that these kind of events tend to have small ISRs, so if we’re not aiming for perfect accuracy for the CPU load calculation, it should be close enough without taking them into account. If we do this, we end up with the following:
ISRn Load = ISRn execution time / ISRn period
To get ISR total Load we just sum every ISRn Load.
Basically you end up having to put 1 profiler block in the main loop (top model) + 1 profiler block per ISR. At the end, you can get these values in FreeMASTER and use them to get an approximation for CPU Load.
Let’s take an example:
We have set the following:
discrete time: 1s (so step will be triggered every 1s)
step execution time: 0.2s (code for step takes 200ms to execute)
ISR1 freq: 0.2s (we trigger a subsystem using LPIT block every 200ms)
ISR execution time: 0.1s (that subsystem takes 100ms to finish it’s execution)
Note: in this example ISR1 has higher priority than step
Time - Function starting execution
0.0 - ISR1
0.1 - Step
0.2 - ISR1
0.3 - Step
0.4 - ISR1
0.5 - IDLE
0.6 - ISR1
0.7 - IDLE
0.8 - ISR1
0.9 - IDLE
1.0 – the whole process starts repeating
Here we can see the following:
ISR1 execution time = 5 x 0.1s = 0.5s
step execution time = 1 x 0.2s = 0.2s
idle = 3 x 0.1s = 0.3s
So CPU load is 70%. Now lets test out the formula I proposed earlier:
Step load = 0.2 / 0.1 = 0.2 = 20%
ISR load = ISR1 load = 0.1 / 0.2 = 0.5 = 50%
CPU Load = Step load + ISR load = 20% + 50% = 70%
Note: the profiler block returns a value in ticks – those are ticks at a 40MHz 20MHz (edited) frequency – you can use this information to convert from ticks to execution time (both for the main loop and other ISRs).
Note2: you must select a different index for each profiler block (option available in the mask) – otherwise the values will just overwrite one another.
I hope I made things clear about how you could get the CPU load with our toolbox and FreeMASTER.
Kind regards,
Razvan.