Measurement method
The CPU load measurement is based on evaluating the idle time, which, from the perspective of a PXROS-HR application, is the time that the idle task spends in the active (running) state. In the PXROS-HR application, the function of the idle task is supplied by the InitTask. At certain point, it lowers its priority to the lowest possible for the given CPU and therefore the InitTask’s main function is only activated when kernel services or handlers of any type are not running and all the other tasks on that CPU are blocked in a waiting state.
Idle time
The idle time is evaluated by incrementing a variable in the idle loop and evaluating the reached value at precise periodic intervals. The more time the idle task spends in the active state, the higher the reached value is. This value is then compared to the reference value of the "unloaded" system, and the ratio is used for CPU load computation. The following formula is used:
CPU_load = SCALING_FACTOR * ( 1 - current_value / reference_value )
In the above equation, the current value is the value reached by incrementing the variable during the measurement interval, while the reference value is the value taken during calibration. The calibration must be done when the system is "unloaded", i.e., when the user tasks and handlers are not yet running. The SCALING_FACTOR translates the CPU load value from the original range of 0 to 1 into a more convenient range (e.g. per cents in case it is set to 100).
Calibration
During the calibration phase, the reference value is obtained. Because the system is not loaded (neither user tasks, nor user handlers yet started), all CPU time is given to the idle task. The reference value should be the biggest value of all values counted later during the application runtime.
The idle task is only interrupted by PXROS-HR tick handling services and the delay job handler. These interrupts do not introduce any measurement error, because the same interrupts will occur during the regular application run. The scheduling example during calibration phase is shown in the picture below. For readability, the measurement period is set to 3 ticks only. Also, the kernel tick service and delay job handler are split even though both are running on the same (kernel priority) level.
Runtime load measurement
After the idle task starts CPU load measurement, it spends all its active time incrementing an internal value, which is used for load measurement. The below picture shows an example of the scheduling during runtime. For demonstration purposes, there are now two additional tasks utilizing the CPU time. To make the picture simple, transitions from running to waiting task states are shown as immediate, however, there are always kernel service calls during which the tasks are not running. Neither those service calls nor the short task ready states are shown.