CPU load measurement
The CPU load measurement module allows you to monitor the CPU utilization based on the amount of time during which the CPU has been asleep. The module measures the amount of the sleep time and divides this value by the total amount of time since the reset of the measurement. The resulting CPU load value is a percentage value that corresponds to the amount of time when the CPU has not been sleeping. For example, a value of 75% means that the CPU has been busy for 75% of the time during the measurement period. This can be used for debugging purposes to calculate the CPU load when using an application.
To precisely measure the sleep period, the module requires the POWER peripheral events SLEEPENTER and SLEEPEXIT. The events are connected to a TIMER peripheral using PPI/DPPI.
The sleep period is measured using the TIMER peripheral, which is clocked by default by the high frequency clock.
Alternatively, it can be clocked using low frequency clock (see
It is then compared against the system clock, which is clocked by the low frequency clock.
The accuracy of measurements depends on the accuracy of the given clock sources.
The module allows you to configure the following options in Kconfig:
Enabling or disabling the shell commands for controlling the library.
Toggling the periodic load measurement logging.
Enabling the alignment of the clock sources for more accurate measurement.
Choosing the TIMER instance for the load measurement.
The module allows the following usage scenarios:
- Enabling the module
cpu_load_init()to initialize the module. Calling this function resets the TIMER peripheral and the system clock.
The module can be enabled also by using the
cpu_load initcommand, if you enabled the shell commands.
- Getting the results
After the initialization, you can get the CPU load value by calling the
If called for the first time, the function will provide the load value since the start of the measurement period.
If called after a reset, the function will provide the load value since the last reset.
You can also get the CPU load value by using the
cpu_load getor the
cpu_loadcommand, if you enabled the shell commands.
In the periodic load measurement logging, the
cpu_load_get()function is called alternately with the
- Resetting the measurement
You can reset the TIMER peripheral and the system clock read-out by using
cpu_load_reset(). This provides a new reference point from which the
cpu_load_get()function measures the CPU load.
You can also reset the measurement using the
cpu_load resetcommand, if you enabled the shell commands.
- group cpu_load
Module for measuring CPU load.
Initialize the CPU load measurement module.
The TIMER driver and PPI channels are allocated during the initialization of this module.
- Return values:
0 – The initialization is successful.
-ENODEV – PPI channels could not be allocated.
-EBUSY – TIMER instance is busy.
Measurement must be reset at least every 4294 seconds. If not, results are invalid.
Get the CPU load measurement value.
The CPU load is represented in 0,001% units where a 100000 value represents 100% load (e.g. 12345 represents 12,345% load).
The current CPU load value.
- int cpu_load_init(void)