Why runtime monitoring catches RTOS bugs in FreeRTOS - Performance Analysis
We want to understand how runtime monitoring in FreeRTOS affects the time it takes to detect bugs.
Specifically, how the monitoring operations grow as the system runs and tasks increase.
Analyze the time complexity of this runtime monitoring snippet.
void vTaskSwitchHook(void) {
for (int i = 0; i < monitoredTasksCount; i++) {
if (tasks[i].state == BLOCKED) {
checkTimeout(tasks[i]);
}
}
}
This code runs on every task switch and checks if any monitored task is blocked and needs timeout handling.
Look for loops or repeated checks in the code.
- Primary operation: Loop over all monitored tasks to check their state.
- How many times: This loop runs once every task switch, iterating over all monitored tasks.
As the number of monitored tasks grows, the checks increase linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks per task switch |
| 100 | 100 checks per task switch |
| 1000 | 1000 checks per task switch |
Pattern observation: The number of operations grows directly with the number of monitored tasks.
Time Complexity: O(n)
This means the time to check tasks grows in a straight line as more tasks are monitored.
[X] Wrong: "Runtime monitoring only adds a fixed small delay regardless of tasks."
[OK] Correct: The monitoring loop runs for each monitored task, so more tasks mean more checks and longer delays.
Understanding how monitoring scales helps you design systems that catch bugs efficiently without slowing down the RTOS too much.
"What if the monitoring only checked tasks that recently blocked instead of all monitored tasks? How would the time complexity change?"