Sub topic 10
Calculating CPU Overhead and Transfer Rates
Prerequisites and the main idea
This chapter assumes that you know basic CPU organization, registers, memory, buses, binary and hexadecimal numbers, and the fetch-decode-execute cycle. You should also know that an interrupt service routine, or ISR, is a small program that runs when hardware requests CPU attention.
The main calculation is:
CPU fraction = CPU cycles spent on I/O ÷ total CPU cycles
If the CPU runs at F cycles per second, and I/O consumes C cycles per second, then:
CPU fraction = C ÷ F
Multiply the result by 100 to express it as a percentage.
Counting I/O events
The first step is to determine how often the CPU must handle an I/O event.
For a device transferring R bytes per second:
- If one event transfers one byte, events per second = R.
- If one event transfers W bytes, events per second = R ÷ W.
- If one DMA block contains B bytes, blocks per second = R ÷ B.
The event size matters. An interrupt for every byte creates many more CPU events than one interrupt for every 4-kilobyte block.
| I/O method | CPU work per event | Typical event frequency |
|---|---|---|
| Polling | Repeated status checks | Very high while waiting |
| Interrupt-driven I/O | ISR execution | Once per byte, word, or device event |
| DMA | Setup and completion handling | Once per block |
Polling overhead
In programmed I/O, the CPU repeatedly reads a status register until a ready bit becomes set. If the CPU does no other work during this wait, the I/O operation consumes nearly 100% of the CPU time.
If each polling check costs P cycles and the CPU checks the device Q times per second, the polling cost is:
Polling CPU fraction = P × Q ÷ F
For example, a 500 MHz CPU that performs 2 million status checks per second, with each check costing 20 cycles, uses:
2,000,000 × 20 ÷ 500,000,000 = 0.08
Thus, polling uses 8% of the CPU while the polling loop is active. A continuously waiting loop can use much more because Q increases until the CPU spends nearly all of its time checking the device.
Polling can have low response delay, but it wastes cycles when the device is not ready.
Interrupt-driven overhead
For interrupt-driven I/O, calculate the number of interrupts per second and multiply by the cost of one ISR.
Interrupt CPU fraction = interrupts per second × ISR cycles ÷ CPU cycles per second
Suppose a device transfers 1 MB/s, one interrupt is generated for every 4-byte word, and the ISR costs 1,000 cycles. The interrupt rate is:
1,000,000 ÷ 4 = 250,000 interrupts per second
On a 500 MHz CPU:
250,000 × 1,000 ÷ 500,000,000 = 0.50
The ISR uses 50% of the CPU. This calculation includes only the stated ISR cost. Real systems may also spend cycles saving registers, restoring registers, scheduling work, and copying data.
DMA overhead
Direct memory access, or DMA, transfers a block between a device and memory without requiring the CPU to handle every byte. The CPU usually pays for setup and completion handling.
DMA CPU fraction = blocks per second × CPU cycles per block ÷ CPU cycles per second
The CPU cycles per block can include:
- DMA setup cycles
- A completion-interrupt ISR
- Buffer-management cycles
A larger block generally reduces CPU overhead because fewer setup and completion events are needed. However, very large blocks can increase latency and may occupy the bus for longer.
Worked calculation
The following program compares interrupt-driven I/O with DMA for a 1 MB/s device.
# CPU time used by a 1 MB/s device on a 500 MHz CPU
rate_bytes_per_s = 1_000_000
word_bytes = 4
cpu_hz = 500_000_000
# Interrupt-driven I/O: one interrupt per word
words_per_s = rate_bytes_per_s / word_bytes
interrupt_fraction = words_per_s * 1000 / cpu_hz
# DMA: one transfer per 4096-byte block
block_bytes = 4096
blocks_per_s = rate_bytes_per_s / block_bytes
dma_fraction = blocks_per_s * (2000 + 1000) / cpu_hz
print(f"Interrupt-driven: {interrupt_fraction:.2%} of CPU time")
print(f"DMA: {dma_fraction:.4%} of CPU time")The output is approximately:
Interrupt-driven: 50.00% of CPU time
DMA: 0.1465% of CPU timeDMA is much cheaper in this example because the CPU handles about 244 blocks per second instead of 250,000 words per second.
Transfer-rate limits
A transfer rate is the amount of data moved per unit time:
Transfer rate = bytes transferred ÷ elapsed time
A bus with a width of W bytes and a clock frequency of f transfers at most:
Theoretical rate = W × f
If the bus transfers data on both clock edges, multiply by two. If only a fraction u of bus cycles transfers useful data, include utilization:
Actual rate = W × f × u
The actual device rate cannot exceed the smallest limit imposed by the device, bus, memory, DMA controller, and software. This is the bottleneck.
GATE-style method
Use this sequence:
- Identify the data rate and convert it to bytes per second.
- Identify the transfer unit: byte, word, or block.
- Calculate events per second.
- Find CPU cycles consumed by each event.
- Divide consumed cycles by CPU frequency.
- Check whether the result is a fraction or a percentage.
- For a transfer-rate question, compare theoretical bandwidth with the device and bus limits.
Exercise
A 2 MB/s device generates one interrupt for every 2-byte word. Each ISR costs 600 CPU cycles, and the CPU runs at 600 MHz. What fraction of CPU time does interrupt handling consume?
Show answer
The interrupt rate is 2,000,000 ÷ 2 = 1,000,000 interrupts per second. CPU overhead is 1,000,000 × 600 ÷ 600,000,000 = 1, so interrupt handling consumes 100% of the CPU.