16
Lecture 33: Chapter 5 Today’s topic Cache Replacement Algorithms Multi-level Caches Virtual Memories 1

Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Embed Size (px)

Citation preview

Page 1: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Lecture 33: Chapter 5

• Today’s topic– Cache Replacement Algorithms– Multi-level Caches– Virtual Memories

1

Page 2: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Associative Cache Example

2

• The location of the block with address ‘12’ varies depending on the type of the cache memory and degree of associativity.• In a direct mapped of size 8 blocks, the memory address ‘12’ is associated with the block address ‘4’ in the cache memory because 12 =

(1100)2 Thus (12 modulo 8) = 4 = (100)2 is used to find location in the cache.• In a 2-way set associative, there will be 4 sets. Thus, the corresponding location is cash is the block number (12 modulo 4) = 0.• In a full associative scenario, the memory block for block address 12 can appear in any of the eight cache blocks.

Page 3: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Set Associative Cache Organization

3

Page 4: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Replacement Policy

• Direct mapped: no choice• Set associative

– Prefer non-valid entry, if there is one– Otherwise, choose among entries in the set

• Least-recently used (LRU)– Choose the one unused for the longest time

• Simple for 2-way, manageable for 4-way, too hard beyond that

• Random– Gives approximately the same performance as

LRU for high associativity

4

Page 5: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Multilevel Caches

• Primary cache attached to CPU– Small, but fast

• Level-2 cache services misses from primary cache– Larger, slower, but still faster than main memory

• Main memory services L-2 cache misses

• Some high-end systems include L-3 cache

5

Page 6: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Multilevel Cache Example

• Given– CPU base CPI = 1, clock rate = 4GHz– Miss rate/instruction = 2%– Main memory access time = 100ns

• With just primary cache– Miss penalty = 100ns/0.25ns = 400 cycles– Effective CPI = 1 + 0.02 × 400 = 9

6

Page 7: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Example (cont.)

• Now add L-2 cache– Access time = 5ns– Global miss rate to main memory = 0.5%

• Primary miss with L-2 hit– Penalty = 5ns/0.25ns = 20 cycles

• Primary miss with L-2 miss– Extra penalty = 400 cycles

• CPI = 1 + 0.02 × 20 + 0.005 × 400 = 3.4• Performance ratio = 9/3.4 = 2.6

7

Page 8: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Cache Summary

• Common principles apply at all levels of the memory hierarchy– Based on notions of caching

• At each level in the hierarchy– Block placement– Finding a block– Replacement on a miss– Write policy

The BIG Picture

8

Page 9: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Block Placement

• Determined by associativity– Direct mapped (1-way associative)

• One choice for placement

– n-way set associative• n choices within a set

– Fully associative• Any location

• Higher associativity reduces miss rate– Increases complexity, cost, and access

time9

Page 10: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Finding a Block

Associativity Location method Tag comparisons

Direct mapped Index 1

n-way set associative

Set index, then search entries within the set

n

Fully associative Search all entries #entries

Full lookup table 0

10

Page 11: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Replacement

• Choice of entry to replace on a miss– Least recently used (LRU)

• Complex and costly hardware for high associativity

– Random• Close to LRU, easier to implement

11

Page 12: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Write Policy

• Write-through– Update both upper and lower levels– Simplifies replacement, but may require

write buffer• Write-back

– Update upper level only– Update lower level when block is replaced– Need to keep more state

12

Page 13: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Sources of Misses

• Compulsory misses (aka cold start misses)– First access to a block

• Capacity misses– Occur because blocks are being discarded from cache because cache

cannot contain all blocks needed for program execution (program working set is much larger than cache capacity).

– Due to finite cache size– A replaced block is later accessed again

• Conflict misses (aka collision misses)– In the case of set associative or direct mapped block placement strategies,

conflict misses occur when several blocks are mapped to the same set or block frame; also called collision misses or interference misses.

– Due to competition for entries in a set– Would not occur in a fully associative cache of the same total size

13

Page 14: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Virtual Memory

• Use main memory as a “cache” for secondary (disk) storage– Managed jointly by CPU hardware and the

operating system (OS)• Programs share main memory

– Each gets a private virtual address space holding its frequently used code and data

– Protected from other programs• CPU and OS translate virtual addresses

to physical addresses– VM “block” is called a page– VM translation “miss” is called a page fault

14

Page 15: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Address Translation

• Fixed-size pages (e.g., 4K)

The page size is 4K = 212. The number of physical pages allowed in memory is 218 since the physical page number has 18 bits in it. Thus, main memory can have at most 1GB while the virtual address space is 4GB.

15

Page 16: Lecture 33: Chapter 5 Today’s topic –Cache Replacement Algorithms –Multi-level Caches –Virtual Memories 1

Page Fault Penalty

• On page fault, the page must be fetched from disk– Takes millions of clock cycles– Handled by OS code

• Try to minimize page fault rate– Fully associative placement– Smart replacement algorithms

16