To calculate the CPU load in a microcontroller, you should measure the number of tasks waiting or running at each time interval and average them over the total number of intervals. This involves tracking both active and waiting tasks at consistent time intervals. The CPU load is then calculated using the formula:
CPU Load = Sum of tasks waiting or running at each time interval / Number of time intervals
This formula ensures that you are accounting for both the tasks waiting to be processed and those currently running, averaged over a set number of time intervals.
Steps to Calculate CPU Load
- Identify Tasks: List all tasks running on the microcontroller.
- Monitor Time Intervals: Break down the observation period into equal time intervals.
- Track Task Status: For each time interval, record the number of tasks that are either waiting or running.
- Sum the Tasks: Add up the number of tasks waiting or running for each time interval.
- Calculate Average: Divide the sum by the number of time intervals.
Example Calculation
Let’s say you observe the CPU over 5 time intervals and record the following number of tasks waiting or running:
- Interval 1: 2 tasks
- Interval 2: 3 tasks
- Interval 3: 1 task
- Interval 4: 4 tasks
- Interval 5: 2 tasks
Sum of tasks: 2 + 3 + 1 + 4 + 2 = 12
Number of intervals: 5
CPU Load: 12 / 5 = 2.4
This means that, on average, there are 2.4 tasks either waiting or running at any given time.
Practical Example
In this example, I’m working with an STM32F4 series microcontroller. Here’s a practical example using pseudo-code:
#include "stm32f4xx_hal.h"
#include <stdint.h>
// Define the number of samples for load average calculation
#define NUM_SAMPLES 10
// Function prototypes
void task1(void);
void task2(void);
void task3(void);
float calculateCPULoad(void);
// Global variables
volatile uint32_t task_count = 0;
volatile uint32_t samples[NUM_SAMPLES] = {0};
volatile uint32_t sample_index = 0;
int main(void)
{
// Initialize HAL and system clock
HAL_Init();
SystemClock_Config();
// Main loop
while (1)
{
task1();
task2();
task3();
// Calculate and print CPU load every second
if (HAL_GetTick() % 1000 == 0)
{
float cpu_load = calculateCPULoad();
printf("CPU Load: %.2f\n", cpu_load);
}
}
}
void task1(void)
{
// Simulating a task that takes some time
HAL_Delay(10);
task_count++;
}
void task2(void)
{
// Simulating another task
HAL_Delay(5);
task_count++;
}
void task3(void)
{
// Simulating a third task
HAL_Delay(15);
task_count++;
}
float calculateCPULoad(void)
{
// Record the current task count
samples[sample_index] = task_count;
sample_index = (sample_index + 1) % NUM_SAMPLES;
// Calculate the sum of tasks over the sampling period
uint32_t total_tasks = 0;
for (int i = 0; i < NUM_SAMPLES; i++)
{
total_tasks += samples[i];
}
// Calculate CPU load
float cpu_load = (float)total_tasks / NUM_SAMPLES;
// Reset task count for the next sampling period
task_count = 0;
return cpu_load;
}
After running the system for a while, I got this result: CPU Load = 3.5
This means that, on average, 3.5 tasks were either running or waiting to run during each sampling interval.
Importance of Accurate CPU Load Calculation
- Optimizing Performance: Ensuring the microcontroller is not overloaded.
- Efficient Task Scheduling: Balancing tasks to avoid bottlenecks.
- Resource Management: Allocating CPU time effectively across tasks.
No comments yet