Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 503 of 9367. Real-Time Operating Systems (RTOS)Free lesson

Calculating Worst-Case Execution Time for Tasks

Apply analytical techniques to determine the maximum time a task requires to complete, ensuring it meets its defined real-time deadline.

In this lesson, you will learn how to analytically determine the maximum time a task requires to complete, ensuring your real-time system never misses a critical deadline.

Why WCET Matters

In hard real-time systems, the average execution time is irrelevant; the system is only as reliable as its slowest possible path. The Worst-Case Execution Time (WCET) represents the upper bound of time a specific task takes to execute on a given hardware platform. If your task’s WCET exceeds its assigned deadline, the system fails, regardless of how fast it runs on average.

Analytical WCET Estimation

To calculate the WCET, we decompose the code into segments and identify the "longest path." We look for:

  1. Loop Bounds: The maximum number of iterations for every loop.
  2. Branching: The path that executes the most complex instruction sequences (e.g., the else branch might contain heavier computations).
  3. Hardware Latency: Pipeline stalls, cache misses, and memory access times.

Consider this snippet:

void process_sensor_data(int *data, int size) {
    for (int i = 0; i < size; i++) { // Max size is 10
        if (data[i] > THRESHOLD) {
            perform_heavy_calculation(data[i]); // Path A
        } else {
            perform_simple_op();               // Path B
        }
    }
}

To find the WCET here, we don't look at the average case. We assume:

  • The loop always hits the maximum size (10).
  • Every iteration takes the if branch (Path A).
  • We account for the worst-case cycles for perform_heavy_calculation and the associated memory access latency.

The Calculation Logic

The WCET is essentially the sum of the execution times of the most expensive path: WCET = Σ (max_cycles_per_instruction * number_of_iterations)

When analyzing this, remember that cache misses and pipeline hazards often make the "most complex" branch unpredictable. In practice, we use static analysis tools to traverse the control-flow graph (CFG) of your compiled binary to identify this path, as manual counting becomes error-prone once compiler optimizations (like loop unrolling) are applied.

Key Takeaway: The WCET must be calculated based on the absolute longest logical path and the most pessimistic hardware performance state to guarantee that your task consistently meets its real-time deadline.