Health monitoring and heartbeat in FreeRTOS - Time & Space Complexity
When monitoring system health with heartbeat signals, it is important to understand how the time to check tasks grows as more tasks are monitored.
We want to know how the checking process scales when the number of tasks increases.
Analyze the time complexity of the following code snippet.
void CheckHealth(TaskStatus_t *tasks, int numTasks) {
for (int i = 0; i < numTasks; i++) {
if (tasks[i].heartbeat == 0) {
// Handle missed heartbeat
}
}
}
This code loops through all tasks to check if their heartbeat signal is missing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each task to check heartbeat.
- How many times: Once for each task, so numTasks times.
As the number of tasks increases, the time to check all heartbeats grows in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: Doubling the number of tasks doubles the work needed.
Time Complexity: O(n)
This means the time to check heartbeats grows linearly with the number of tasks.
[X] Wrong: "Checking heartbeats happens instantly no matter how many tasks there are."
[OK] Correct: Each task must be checked one by one, so more tasks mean more time needed.
Understanding how monitoring scales helps you design systems that stay reliable as they grow. This skill shows you can think about real-world system behavior.
"What if we only checked a random sample of tasks instead of all? How would the time complexity change?"