Sub topic 3
Cache basics: blocks, lines, hits and misses
This chapter assumes you can convert between binary and hexadecimal, work with powers of two, and know that memory is byte-addressable. It also assumes you know that the CPU fetches instructions and data from memory. Nothing about mapping is needed yet. This chapter fixes the vocabulary that every later chapter uses.
What a cache does
A cache is a small, fast memory placed between the CPU and a larger, slower memory. It holds copies of data that the CPU has used recently or is likely to use soon. The CPU always asks for data by its main-memory address. The cache checks whether it holds a copy. If it does, the access is fast. If it does not, the data must come from the next level down.
A cache works because of locality. Programs reuse data they touched recently (temporal locality) and touch data near what they just used (spatial locality). The cache exploits temporal locality by keeping data after the first access. It exploits spatial locality by fetching more than was asked for. The unit it fetches is the block.
Blocks and lines
Two terms are often mixed up, and GATE questions use both.
- A block (also called a cache block) is a fixed-size chunk of main memory. Main memory is divided into consecutive blocks, and the block size is a power of two, such as 16 or 64 bytes.
- A line (also called a cache line or cache slot) is a place in the cache that can hold one block, together with its bookkeeping bits.
The block is the unit of transfer between levels. Even if the CPU wants one byte, the cache fetches the whole block that contains it. The line is a storage location. Memory has blocks, and the cache has lines. A cache with 512 lines can hold at most 512 blocks at once.
The number of lines follows from two sizes:
number of lines = cache data size / block sizeA 32 KB cache with 64-byte blocks has 32768 / 64 = 512 lines.
What a line contains
Each line stores more than the data.
| Field | Purpose |
|---|---|
| Data | The bytes of one block |
| Tag | Identifies which memory block currently occupies the line |
| Valid bit | 1 if the line holds real data, 0 if it is empty or invalid |
| Dirty bit | Used only by write-back caches. It is 1 if the line was modified and memory is out of date |
The tag exists because many memory blocks can share one line. How a block chooses its line is the subject of later chapters. For now, remember that the tag tells you which block is currently in the line.
The valid bit matters at start-up. When the machine powers on, the cache contains random bits. A tag match on such a line means nothing. Hardware therefore treats a line as a hit only if the valid bit is 1 and the tag matches.
Block number and offset
Because blocks are consecutive, you find the block that holds a byte address by integer division:
block number = address // block size
offset = address % block sizeThe block number says which block of memory contains the byte. The offset says where the byte sits inside that block. Block size is a power of two, so the offset is just the low bits of the address, and the block number is the remaining high bits.
Worked example. With 16-byte blocks, take address 0x1234 (4660 in decimal).
- Block number: 4660 // 16 = 291, which is 0x123.
- Offset: 4660 % 16 = 4.
- The block covers addresses 0x1230 through 0x123F. The block start is 291 × 16 = 4656 = 0x1230.
The CPU asked for one byte at 0x1234. The cache fetches all 16 bytes from 0x1230 to 0x123F.
Hits and misses
Every CPU access to the cache has one of two outcomes.
- A hit: the block containing the requested address is in the cache, and it is valid. The cache returns the data after its hit time.
- A miss: the block is not in the cache. The cache must fetch the whole block from the next level, place it in a line, and then return the requested data.
On a miss the cache may have to choose a line to reuse. If that line holds a different block, the old block is evicted. Which line is chosen depends on the mapping and replacement policy, covered in later chapters.
Simulating hits and misses
The following program counts hits and misses for a trace of byte addresses. To isolate the effect of block size, it assumes the cache is large enough that nothing is ever evicted. Every miss it reports is therefore a first touch of a block.
def count_hits(addresses, block_size):
resident = set() # block numbers currently in the cache
hits = misses = 0
for a in addresses:
block = a // block_size
if block in resident:
hits += 1
else:
misses += 1
resident.add(block) # whole block is fetched
return hits, misses
trace = list(range(32)) # read bytes 0..31 in order
for bs in (1, 4, 8, 16):
h, m = count_hits(trace, bs)
print(f"block size {bs:2d}: hits={h:2d} misses={m:2d} hit ratio={h/(h+m):.3f}")The output is:
block size 1: hits= 0 misses=32 hit ratio=0.000
block size 4: hits=24 misses= 8 hit ratio=0.750
block size 8: hits=28 misses= 4 hit ratio=0.875
block size 16: hits=30 misses= 2 hit ratio=0.938With 1-byte blocks, nothing is fetched ahead, so every access misses. With 16-byte blocks, the first access to each block misses and the next 15 hit. This is spatial locality at work.
A worked example with an array
A program sums an array of 64 integers of 4 bytes each, in order. The array starts at an address that is a multiple of 16, and the cache has 16-byte blocks. Assume a cold cache that is big enough to hold the whole array.
- Each block holds 16 / 4 = 4 integers.
- The array occupies 64 × 4 = 256 bytes, so it spans 256 / 16 = 16 blocks.
- The first access to each block misses, and the other 3 accesses to that block hit.
- Misses = 16, hits = 64 − 16 = 48, and hit ratio = 48 / 64 = 0.75.
If the array were not aligned to a block boundary, it could span 17 blocks, and there would be 17 misses. Alignment matters when you count exactly.
Why not just use huge blocks?
Larger blocks capture more spatial locality, as the simulation shows. They also have costs:
- A miss transfers more bytes, so the miss penalty grows.
- A fixed-size cache holds fewer lines, so fewer distinct blocks can be resident at once.
- If the program uses only a few bytes of each block, the extra bytes waste bandwidth and displace useful data.
Real designs choose a middle value, commonly 32 to 128 bytes. In exam questions, block size is given, so you only need to apply it.
Cold start and compulsory misses
The first access to any block must miss, because the block has never been loaded. These are called compulsory misses (or cold misses). A larger block size reduces the number of compulsory misses for sequential access, because one miss brings in more data. The simulator above counts only compulsory misses. Later chapters add capacity and conflict misses.
Common errors
- Confusing block and line. A block lives in memory, and a line lives in the cache. A question that gives "number of blocks" for memory and "number of lines" for the cache is not using the terms interchangeably.
- Treating block size as words when the question gives bytes, or the reverse. Convert everything to bytes before dividing.
- Assuming the cache fetches only the requested word. It always fetches the whole block.
- Forgetting the valid bit. A matching tag on an invalid line is a miss.
- Counting the first access to each block as a hit. On a cold cache it is a miss.
- Counting tag and valid bits as part of the "cache size". Cache size in a question normally means data capacity only. Tag overhead is computed separately in a later chapter.
Summary
- A cache holds copies of main-memory blocks in lines, and each line stores data, a tag, a valid bit, and sometimes a dirty bit.
- The block is the unit of transfer. A miss fetches the whole block, which is how the cache benefits from spatial locality.
- Block number = address // block size, and offset = address % block size.
- Number of lines = cache data size / block size.
- A hit needs a valid line with a matching tag. Otherwise the access is a miss.
Exercise
A cache has 8 KB of data and 64-byte blocks. (a) How many lines does it have? (b) For byte address 0x2F7, give the block number and the offset. (c) What range of byte addresses does that block cover?
Show answer
(a) 8 KB = 8192 bytes, and 8192 / 64 = 128 lines. (b) 0x2F7 = 759. Block number = 759 // 64 = 11 and offset = 759 % 64 = 55. (c) The block starts at 11 × 64 = 704 = 0x2C0 and ends at 704 + 63 = 767 = 0x2FF, so it covers 0x2C0 to 0x2FF.
Exercise
A program reads 100 consecutive 4-byte words, starting at address 0, in order. The cache is cold, has 16-byte blocks, and is large enough that nothing is evicted. How many hits and misses occur, and what is the hit ratio?
Show answer
The data occupies 100 × 4 = 400 bytes. With 16-byte blocks that is 400 / 16 = 25 blocks, and the start address 0 is block-aligned. Each block gives one miss on its first word and 3 hits on its other 3 words. So misses = 25, hits = 100 − 25 = 75, and hit ratio = 75 / 100 = 0.75.