Measuring Performance

Measuring Performance

In our last part of this cpu course, part 3, we introduced the HDL Verilog and showed how to build the different components with those basic blocks. That CPU was working, but not fast. In this upcoming part 4, we will focus on performance and how to improve it. We can improve what’s measurable, so we will start with measuring the performance (of our CPU) and in general. "Faster" must become a number, not just a feeling. We will introduce the CPU performance equation and Amdahl’s law, which will help us to understand how to improve the performance of our CPU.

Tip

All examples of this chapter are in the folder 31_performance of our repository hdl-training: 00-instruction-mix. If you cloned it earlier, git pull fetches the latest version.

What does "fast" mean?

We start with two different questions:

  • How long does ONE task take (latency)

  • and how many tasks are finished per second (throughput).

The best analogy here is a car vs. a bus from A to B: the car has lower latency, but the bus has higher throughput.

For a CPU, we can think of the time an instruction needs vs. how many instructions are finished per second. Pipelining (we will discuss that in chapter 33) will improve throughput - and not latency!

Measured in numbers: The performance of a CPU is defined as the reciprocal of the execution time. If we say "A is n times faster than B", we mean that the execution time of B divided by the execution time of A equals n.

\[\large \text{Performance} = \frac{1}{t_\text{execution}}\]

The only honest measure of performance is the time a real program needs. Clock frequency alone says little, as we will see in the next chapters.

The CPU performance equation (or iron law of performance)

The central formula of this chapter is the CPU performance equation, which relates the execution time of a program to three factors: the number of instructions executed, the average number of clock cycles per instruction, and the clock period. Later on we will meet another law, Amdahl’s law, which will help us to understand how to improve the performance of our CPU. But first things fist.

\[\large {t_{\text{CPU}}} = IC \cdot CPI \cdot T = IC \cdot \frac{CPI}{f}\]

Each factor is explained in the following:

  • IC: instruction count, the number of instructions executed by the program. It depends on the program itself, the compiler, and the instruction set architecture (ISA). For example, a complex instruction may take fewer instructions to accomplish a task than a simpler one.

  • CPI: cycles per instruction, the average number of clock cycles each instruction takes to execute. This is influenced by the microarchitecture of the CPU. A single-cycle CPU has a CPI of 1, while a multi-cycle CPU may have a higher CPI due to multiple stages in instruction execution.

  • T: clock period, the duration of one clock cycle. It is determined by the critical path of the circuit and the technology used. A shorter clock period means a higher clock frequency (f = 1/T), which can lead to faster execution times.

Note
The critical path

Every gate needs a short time until its output reacts to a change at its inputs - the propagation delay. On a path through several gates, these delays add up.

In a clocked circuit, every path starts at a register and ends at the next register (the RTL picture from chapter 24). The critical path is the slowest of all these paths. Within one clock period, three things must happen on it:

  • the first register passes on its new value (clock-to-Q time),

  • the signal runs through the combinational logic,

  • and it must arrive at the next register a little before the clock edge (setup time).

\[T \geq t_\text{clk-to-Q} + t_\text{logic} + t_\text{setup}\]

The critical path therefore determines the shortest possible clock period. In our single-cycle CPU, it is the path of lw: instruction fetch, register read, ALU, data memory and write back - 800 ps (chapter 17). In chapter 27, nextpnr found it for us on the FPGA.

\[\cancel{\text{instructions}} \times \frac{\cancel{\text{cycles}}}{\cancel{\text{instruction}}} \times \frac{\text{seconds}}{\cancel{\text{cycle}}} = \text{seconds}\]

The units confirm it: instructions × cycles per instruction × seconds per cycle = seconds. It is exact, not just a simple approximation: every speedup must reduce at least one of the three factors. The three factors are not independent, though. Shortening T often increases CPI, and vice versa. This trade-off runs through the whole part 4.

This whole law is known as the "classic CPU performance equation" (Patterson/Hennessy); in some lectures also called the "iron law of performance".

Our single-cycle CPU in numbers

The counter program, we introduced in chapter 15, is a good example to illustrate the performance equation. It consists of 3 addi instructions followed by 10 iterations of a loop containing add, sw, and bne.

Where does the program end? The counting loop ends when x1 = 10 (bne no longer jumps back). Then the CPU reaches "beq x0, x0, done" at 0x18, which jumps to itself forever: RISC-V has no halt instruction, so the program ends with an endless loop (chapter 15).

So the instruction count is 33. The single-cycle CPU has a CPI of 1, and the clock period is 800 ps (see table from chapter 17). That equates to a CPU time of 33 × 1 × 800 ps = 26.4 ns Here is the table from chapter 17 again, which shows the time each instruction type takes in our single-cycle CPU:

Instruction

Instruction memory

Register read

ALU

Data memory

Register write

Total

R-type

200 ps

100 ps

200 ps

-

100 ps

600 ps

lw

200 ps

100 ps

200 ps

200 ps

100 ps

800 ps

sw

200 ps

100 ps

200 ps

200 ps

-

700 ps

beq

200 ps

100 ps

200 ps

-

-

500 ps

The instruction mix of the counter program is as follows: addi 3 × 600 ps, add 10 × 600 ps, sw 10 × 700 ps, bne 10 × 500 ps ⇒ on average 600 ps per instruction.

Conclusion: 25 % of the time the CPU only waits. And the clock is set by lw - which does not even occur in this program! So the single-cycle CPU wastes time in two ways: the slowest instruction sets the pace for all, and while one part works (e.g. the ALU), all other parts are idle. So the next question is: how can we improve this?

Measuring the instruction mix in simulation

We are starting with the example of the counter program, which we already know from chapter 15. The instruction mix is the share of each instruction type in the program. The program runs forever as it does not have a halt instruction, so we can only count the instructions until the program reaches the endless loop at 0x18. The expected output of the simulation is: addi 3, add 10, sw 10, bne 10, IC = 33, CPU time = 26.4 ns In simulation, we can count the instructions by type (from the opcode) and compute the CPU time with the performance equation. That is exactly how designs are evaluated before they are built in real chip development.

Amdahl’s law

Now, as we know WHERE the time goes (instruction mix, 25 % waiting), the natural next question is: if I make one part faster, how much faster does the whole program get?

The formula for the total speedup of a program is given by Amdahl’s law:

\[\large \text{Speedup}_{total} = \frac{1}{((1-p) + p/s)}\]

with p = fraction of the time that benefits, s = speedup of that part.

Example 1: 30 % of the time is spent in memory accesses, they get 2× faster ⇒ 1.18× (not 2×). Even with infinitely fast memory: at most 1 / 0.7 = 1.43×.

Example 2 with our CPU: a 2× faster ALU (200 ⇒ 100 ps) shortens the single-cycle clock from 800 to 700 ps ⇒ only 1.14×, because the ALU is only one of five steps on the critical path.

Lesson: make the common case fast. Optimize what takes the most time, not what is easiest to optimize.

Why the clock frequency alone is misleading

We take a peek into the next chapters to see how the performance equation works out for the same program on different CPU designs.

  • single-cycle CPU: T = 800 ps (1.25 GHz), 33 cycles, CPI 1.0 ⇒ 26.4 ns

  • multi-cycle CPU: T = 200 ps (5 GHz), 122 cycles, CPI 3.7 ⇒ 24.4 ns (4× the clock, only 8 % faster!)

  • pipeline CPU: T = 200 ps (5 GHz), 37 cycles, CPI 1.12 ⇒ 7.4 ns (3.6× faster)

  • (multi-cycle step counts: R-type/addi 4, sw 4, branch 3 - will be derived and explained in chapter 32)

  • The multi-cycle CPU has a 4× higher clock frequency, but is hardly faster - CPI ate up the gain. The pipeline keeps the short clock AND gets CPI close to 1. That is the plan for part 4.

  • Historical note (short): the "megahertz myth" - processors were sold by clock frequency for a long time, although the frequency alone does not say how fast a program runs.

Also: MIPS (million instructions per second) as a measure is problematic - it ignores what an instruction does.

The ways to a faster CPU

Idea

Factor

Chapter

shorter clock, several steps

T down, CPI up

32 multi-cycle

steps overlap (assembly line)

T down, CPI ~1

33 pipelining

resolve conflicts in the pipeline

CPI closer to 1

34 data hazards, 35 control hazards

execute out of order

CPI below the in-order limit

36 Tomasulo

execute before it is certain

CPI

37 speculation

faster technology / shorter critical path

T

(chapter 27: routing vs. logic)

better compiler, more powerful instructions

IC

(outside this course)

Try it yourself

  • A program with lw: change the counter so that it loads the value from memory each time (lw, add, sw, bne). Compute the CPU time for single-cycle and multi-cycle (lw = 5 steps).

  • At which share of branch instructions would the multi-cycle CPU be faster than the single-cycle CPU?

  • Amdahl: a program spends 80 % of its time in a loop. How much faster must the loop get for the whole program to be 2× faster? 4× faster?

  • Our CPU on the iCE40 (chapter 27) runs at 30.31 MHz max: how long would the counter program take? (≈ 1.09 µs) - and why is that so much slower than the 800 ps per instruction from chapter 17? (FPGA vs. ASIC, ALU built from single cells, routing - chapter 27)

  • FPGA vs. ASIC: why is the FPGA so much slower than the ASIC? (FPGA: ALU built from single cells, routing - chapter 27)

Summary

In conclusion, we have learned that performance is the reciprocal of execution time, and that the CPU performance equation relates execution time to instruction count, cycles per instruction, and clock period. Amdahl’s law shows that the speedup of a program is limited by the portion that does not benefit from optimization. Our single-cycle CPU has a long clock period due to the slowest instruction, leading to wasted time. To improve performance, we can shorten the clock period, overlap instruction execution through pipelining, and optimize the most time-consuming parts of the program.

Outlook: In the next chapters, we will explore multi-cycle and pipelined CPU designs, which will allow us to shorten the clock period and improve performance significantly.

(Outlined with Claude AI, written by Sven Wehrend)