1 of 46

Caches III: Direct Mapped

Assistant

Teaching Professor

Lisa Yan

CS61C

Great Ideas

in

Computer Architecture

(a.k.a. Machine Structures)

cs61c.org

Head TA

Nicolas Reed

Yan, SP26

28-Caches III: Direct Mapped (1)

2 of 46

Great Idea #3: Principle of Locality / Memory Hierarchy

Storage Latency Analogy: How Far Away is the Data?

Jim Gray�1998 Turing Award

B.S. Cal 1966

Ph.D. Cal 1969

On-chip cache

Sacramento

This Campus

This Room

My Head

10 min

1.5 hr

2 Years

1 min

Pluto

200 Years

Alpha Centauri

Registers

On-board cache

RAM/Memory

Storage/Disk

1

2

10

100

Cloud

[ns]

108

106

Yan, SP26

28-Caches III: Direct Mapped (2)

3 of 46

Line Replacement Policies

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (3)

4 of 46

Fully Associative Cache with LRU policy

  • By the end of instr 5, cache is warm.
  • Load byte 0x43F 0x10F,0x3
  • Load byte 0x5E2 0x178,0x2
  • Load word 0x824 0x209,0x0
  • Load word 0x5E0 0x178,0x0
  • Load word 0x524 0x149,0x0
  • Load byte 0x972 0x25C,0x2

Suppose that LRU = 0 means most recently used, and 3 means least recently used.

After the end of instruction 5, what are the LRU tags on each row?

Valid

LRU

Tag

Data

11

10

01

00

Valid

LRU

Tag

1

0

0x10F

0

0

0

Valid

LRU

Tag

1

1

0x10F

1

0

0x178

0

0

Valid

LRU

Tag

1

2

0x10F

1

1

0x178

1

0

0x209

0

Valid

LRU

Tag

1

2

0x10F

1

0

0x178

1

1

0x209

0

Valid

LRU

Tag

1

3

0x10F

1

1

0x178

1

2

0x209

1

0

0x149

Yan, SP26

28-Caches III: Direct Mapped (4)

5 of 46

A Warmed up Cache Can Still Miss

  • By the end of instr 5, cache is warm.
  • Load byte 0x43F 0x10F,0x3
  • Load byte 0x5E2 0x178,0x2
  • Load word 0x824 0x209,0x0
  • Load word 0x5E0 0x178,0x0
  • Load word 0x524 0x149,0x0
  • Load byte 0x972 0x25C,0x2
  1. Cache miss!
  2. Load into cache the 4-byte line from 0x970 to 0x973. Mark valid bit. Update line replacement policy fields (e.g., LRU).

Valid

LRU

Tag

Data

11

10

01

00

1

3

0x10F

1

1

0x178

1

2

0x209

1

0

0x149

LRU

Tag

0

0x25C

2

0x178

3

0x209

1

0x149

c. Read byte at 0x2 offset, return to processor.

Yan, SP26

28-Caches III: Direct Mapped (5)

6 of 46

Line Replacement Policies

  • Least Recently Used (LRU)
    • Replace the entry that has not been�used for the longest time,�i.e., has the oldest previous access.
    • Pro: Temporal locality!
      • recent past use implies likely future use
    • Con: Complicated hardware to keep track of access history
  • In practice: First In, First Out (FIFO)
    • Replace the oldest line in the set (“queue”).

  • Other policies (see in notes):
    • Most Recently Used (MRU)
    • Random
    • Last In, First Out (LIFO) - “stack”

LRU is ideal for temporal locality but in practice, FIFO is good enough.

Yan, SP26

28-Caches III: Direct Mapped (6)

7 of 46

Cache Design: Placement Policies, Soon

Fully Associative Cache

Put a new line anywhere

(Caches II)

Direct Mapped Cache

Put a new line in one specific place

(Caches III, today)

Set-Associative Cache

Something in-between (how?)

(Caches IV)

Yan, SP26

28-Caches III: Direct Mapped (7)

8 of 46

Write Policies

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (8)

9 of 46

Stores: How to write back to memory?

  • By the end of instr 5, cache is warm.
  • Load byte 0x43F 0x10F,0x3
  • Load byte 0x5E2 0x178,0x2
  • Load word 0x824 0x209,0x0
  • Load word 0x5E0 0x178,0x0
  • Load word 0x524 0x149,0x0
  • Load byte 0x972 0x25C,0x2
  • Store byte 0x524 0x149,0x0

Valid

LRU

Tag

Data

11

10

01

00

1

0

0x25C

1

2

0x178

1

3

0x209

1

1

0x149

Cache hit! …and then?

How to handle stores?

Yan, SP26

28-Caches III: Direct Mapped (9)

10 of 46

Write-through vs. Write-back Policies

  • Store instructions write to memory, which changes values.
  • Hardware needs to ensure that cache and memory “sync” their data.
  • Write-through:
    • Write to the cache and memory at the same time.�
  • Write-back:
    • Write data in cache for now.
    • When this line gets replaced from the cache�(and “back” to memory), write to memory.

Valid

LRU

Tag

Data

11

10

01

00

1

0

0x25C

1

2

0x178

1

3

0x209

1

1

0x149

Store byte 0x524(0x149,0x0)

Yan, SP26

28-Caches III: Direct Mapped (10)

11 of 46

What does Write-Back Look Like? Dirty Bit

  • Write-back:
    • Write data in cache and set a dirty bit to 1.
    • When this line gets replaced from the cache�(and “back” to memory), write to memory.
  • Fully Associative Cache w/ LRU, Write-Back

Valid

Dirty

LRU

Tag

Data

11

10

01

00

1

0

0

0x25C

1

0

2

0x178

1

0

3

0x209

1

1

1

0x149

Cache hit w/write-back:

update cache line, and wait until this line is replaced before writing back to memory

Store byte 0x524(0x149,0x0)

Yan, SP26

28-Caches III: Direct Mapped (11)

12 of 46

Write-through vs. Write-back Policies

  • Write-through:
    • Write to the cache and memory at the same time.
    • (more writes to memory → longer AMAT)
  • Write-back:
    • Write data in cache and set a dirty bit to 1.
    • When this line gets replaced from the cache�(and “back” to memory), write to memory.

Valid

LRU

Tag

Data

11

10

01

00

1

0

0x25C

1

2

0x178

1

3

0x209

1

1

0x149

Store byte 0x524(0x149,0x0)

Write policies have tradeoffs:

simple to implement

(typically) lower traffic to memory

Yan, SP26

28-Caches III: Direct Mapped (12)

13 of 46

Direct Mapped Cache

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (13)

14 of 46

Cache Design: Placement Policies

Fully Associative Cache

Find data in any line

(Caches II)

Direct Mapped Cache

Find data in one specific line

(Caches III, today)

Set-Associative Cache

(Caches IV)

Fully associative caches need expensive hardware.

Yan, SP26

28-Caches III: Direct Mapped (14)

15 of 46

Direct Mapped Cache

  • Placement policy: The data at a memory address can be stored at exactly one possible line in the cache.

flags

Tag

Data

11

10

01

00

How do we identify this location from the memory address?

31

0

Full 32b address

Yan, SP26

28-Caches III: Direct Mapped (15)

16 of 46

Direct Mapped Cache

  • Placement policy: The data at a memory address can be stored at exactly one possible line in the cache.

flags

Tag

Data

11

10

01

00

tag to connect line to memory address

31

0

Full 32b address

tag

index

offset

index to select line in cache

byte offset within line

Example: 0x61B

0b0110 0001 1011

tag

0x61

offset

0x3

index

0x2

0

1

2

3

indices

Yan, SP26

28-Caches III: Direct Mapped (16)

17 of 46

Direct Mapped Caches: 4B Cache

  • 4B Direct Mapped Cache�(line size 1B)

Line starting… maps to… has tag…

    • @ 0x0 index 0 000000
    • @ 0x1 index 1 000000
    • @ 0x2 index 2 000000
    • @ 0x3 index 3 000000

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

1F

0x

(assume 8 bit address)

Different addresses get same line index but different tags.

Tag: 000000

000001

flags

Tag

Data

Yan, SP26

28-Caches III: Direct Mapped (17)

18 of 46

Direct Mapped Caches: 4B Cache

  • 4B Direct Mapped Cache�(line size 1B)

Line starting… maps to… has tag…

    • @ 0x0 index 0 000000
    • @ 0x1 index 1 000000
    • @ 0x2 index 2 000000
    • @ 0x3 index 3 000000
    • @ 0x4 index 0 000001
    • @ 0x8 index 0 000010
    • @ 0xC index 0 000011
    • etc.

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

1F

0x

Lines with same line index replace each other.

flags

Tag

Data

Yan, SP26

28-Caches III: Direct Mapped (18)

19 of 46

Direct Mapped Caches: 8B Cache

  • 8B Direct Mapped Cache�(line size 2B)

Line starting… maps to… has tag…

    • @ 0x0 index 0 00000
    • @ 0x2 index 1 00000
    • @ 0x4 index 2 00000
    • @ 0x6 index 3 00000
    • @ 0x8 index 0 00001
    • @ 0x10 index 0 00010
    • @ 0x18 index 0 00011
    • etc.

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

1F

0x

Tag: 00000

flags

Tag

Data

1

0

Yan, SP26

28-Caches III: Direct Mapped (19)

20 of 46

Direct Mapped Caches: 16B Cache

  • 16B Direct Mapped Cache�(line size 4B)

Line starting… maps to… has tag…

    • @ 0x0 index 0 0000
    • @ 0x4 index 1 0000
    • @ 0x8 index 2 0000
    • @ 0xC index 3 0000
    • @ 0x10 index 0 0001
    • etc.

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

1F

0x

Tag: 0000

flags

Tag

Data

11

10

01

00

Yan, SP26

28-Caches III: Direct Mapped (20)

21 of 46

Direct Mapped Cache

  • Placement policy: Each memory address is associated with exactly one possible line in the cache.

flags

Tag

Data

11

10

01

00

Unlike FA caches, DM caches have an index to identify the only cache line for this memory address.

tag to connect line to memory address

31

0

Full 32b address

tag

index

offset

index to select line in cache

byte offset within line

Yan, SP26

28-Caches III: Direct Mapped (21)

22 of 46

Fill in the blank: Direct Mapped, 12 bit addresses

Suppose we have the below direct mapped cache for 12 bit addresses.

  1. What is the line size / block size, in bytes?
  2. What is the capacity, �in bytes?
  3. For a 12b address, how many bits is the byte offset?
  4. For a 12b address, how many bits is the index?
  5. For a 12b address, how many bits is the tag?

A. B. C. D. E. F.

2 4 8 10 16 Other

2 4 8 10 16 Other

2 4 8 10 16 Other

2 4 8 10 16 Other

flags

Tag

Data

11

10

01

00

Yan, SP26

28-Caches III: Direct Mapped (22)

23 of 46

Yan, SP26

28-Caches III: Direct Mapped (23)

24 of 46

Terminology for Direct Mapped, 12b addresses

1. Line size / block size:

    • # bytes per cache line

2. Capacity

    • Total # data bytes that can be stored in a cache

3. Offset

    • Identifies byte address of data stored at a given cache line

4. Index

    • Selects cache line

5. Tag

    • Identifies data stored at a given cache line to a given memory address

11 4

3 2

1 0

offset

tag

memory address

4 bytes

4 x 4 bytes

= 16 bytes

log2(line size) = 2 bits

# address bits - # offset bits - # index bits = 8 bits

Tag

Data

11

10

01

00

log2(# lines) = 2 bits

index

Yan, SP26

28-Caches III: Direct Mapped (24)

25 of 46

Direct Mapped Cache Analysis

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (25)

26 of 46

Example: Direct Mapped Cache

Suppose we have the following direct mapped cache, for 12 bit addresses.

� Load byte 0xFE2

11 4

3 2

1 0

offset

tag

memory address

index

valid

dirty

Tag

Data

11

10

01

00

0

0

0

0

0b1111 1110 0010

tag

0xFE

offset

0x2

index

0x0

Compute T/I/O

0

Yan, SP26

28-Caches III: Direct Mapped (26)

27 of 46

Warming up the Direct Mapped Cache

  • Suppose we have the following direct mapped cache, for 12b addresses.

Load byte 0xFE2

  • Cache miss! Tag 0xFE is not valid at index 0.

valid

dirty

Tag

Data

11

10

01

00

0

0

0

0

0b1111 1110 0000 0xFE0

0b1111 1110 0011 ← 0xFE3

b. Load into cache the 4-byte line � from 0xFE0 to 0xFE3.� Mark valid bit.

c. Read byte at 0x2 offset� and return to processor.

1

0

0xFE

0b1111 1110 0010

tag

offset

index

0xFE,0x0,0x2

Yan, SP26

28-Caches III: Direct Mapped (27)

28 of 46

Warming up the Direct Mapped Cache

  • Suppose we have the following direct mapped cache, for 12b addresses.

Store byte 0x61C �

valid

dirty

Tag

Data

11

10

01

00

1

0

0xFE

0

0

0

1

1

0x61

0b0110 0001 1100

tag

offset

index

0x61,0x3,0x0

  • Cache miss! Tag 0x61 is not valid at index 3.

b. Load into cache the 4-byte line.� Mark valid bit.

c. Write byte at 0x0 offset.� Mark dirty bit.

Yan, SP26

28-Caches III: Direct Mapped (28)

29 of 46

Warming up the Direct Mapped Cache

  • Suppose we have the following direct mapped cache, for 12b addresses.

Load byte 0x61B �

valid

dirty

Tag

Data

11

10

01

00

1

0

0xFE

0

0

1

1

0x61

1

0

0x61

0b0110 0001 1011

tag

offset

index

0x61,0x2,0x3

  • Cache miss! Tag 0x61 is not valid at index 2.

b. Load into cache the 4-byte line.� Mark valid bit.

c. Read byte at 0x3 offset� and return to processor.

Yan, SP26

28-Caches III: Direct Mapped (29)

30 of 46

Warming up the Direct Mapped Cache

  • Suppose we have the following direct mapped cache, for 12b addresses.

Load byte 0xCAD �

valid

dirty

Tag

Data

11

10

01

00

1

0

0xFE

0

1

0

0x61

1

1

0x61

1

0

0xCA

0b1100 1010 1101

tag

offset

index

0xCA,0x3,0x1

  • Cache miss! Tag 0xCA is not valid at index 3.

b. Evict current line at index 3. � Write back data.

c. Load into cache the 4-byte line.� Mark valid bit.

d. Read byte at 0x1 offset� and return to processor.

Yan, SP26

28-Caches III: Direct Mapped (30)

31 of 46

Direct Mapped Cache

  • Placement policy: The data at a memory address can be stored at exactly one possible line in the cache.

flags

Tag

Data

11

10

01

00

tag to connect line to memory address

31

0

Full 32b address

tag

index

offset

index to select line in cache

byte offset within line

Example: 0x61B

0b0110 0001 1011

tag

0x61

offset

0x3

index

0x2

Yan, SP26

28-Caches III: Direct Mapped (31)

32 of 46

Direct Mapped: Policies

1. Write Policy

A. Write-through� (memory access per write)

B. Write-back� (dirty bit, write to memory � on replacement)

What policies can be implemented for a direct-mapped cache?

Select all that apply.

2. Line Replacement Policy

A. Least Recently Used

B. Most Recently Used

C. FIFO

D. Random

E. None of the Above

Yan, SP26

28-Caches III: Direct Mapped (32)

33 of 46

Direct Mapped: Policies Solution

1. Write Policy

A. Write-through� (memory access per write)

B. Write-back� (dirty bit, write to memory � on replacement)

What policies can be implemented for a direct-mapped cache?

Select all that apply.

2. Line Replacement Policy

A. Least Recently Used

B. Most Recently Used

C. FIFO

D. Random

E. None of the Above

In direct mapped caches, there is only ever one line to replace—the existing line with matching index.

Yan, SP26

28-Caches III: Direct Mapped (33)

34 of 46

Cache Design: Placement Policies, Summary

  • Direct Mapped
    • Placement policy: Each memory address is associated with exactly one possible line in the cache.
    • Simpler hardware: check one line
    • Write policies: write-back, write-through
    • No replacement policy, because we know which line to replace
    • Has conflict misses (see soon)
  • Fully Associative
    • Placement policy: Data at any memory address can be associated with any cache line
    • Expensive hardware: check all lines
    • Write policies: write-back, write-through
    • Must decide replacement policy (LRU, MRU, FIFO, etc.)

For the above reasons, smaller caches are generally fully associative.

Yan, SP26

28-Caches III: Direct Mapped (34)

35 of 46

(pause)

Yan, SP26

28-Caches III: Direct Mapped (35)

36 of 46

Looking Ahead

  • CS 61C: Lectures 26-29, Caches I-IV
  • EECS 151 (Digital/IC Design): Reveals the circuitry behind caches
  • EECS 151LA (ASIC Lab): Design your own cache in SystemVerilog
  • CS 152 (Comp Arch): Explores advanced cache implementations
  • CS 162 (OS): Introduces software and hardware caches

Yan, SP26

28-Caches III: Direct Mapped (36)

37 of 46

Types of Misses

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (37)

38 of 46

Types of Misses

  • Compulsory Miss
    • Caused by the first access to data that has never been in the cache.
  • Capacity Miss
    • Caused when the cache cannot contain all the lines needed during the execution of a program.
    • Occur when lines were in the cache, replaced, and later retrieved.
  • Conflict Miss
    • Multiple lines compete for the same location in the cache, even when the cache has not reached full capacity.

In this class, we will only distinguish between compulsory and non-compulsory misses.

“Non-compulsory” miss

Yan, SP26

28-Caches III: Direct Mapped (38)

39 of 46

Types of Misses

  • Compulsory Miss
    • Caused by the first access to data that has never been in the cache.
  • Capacity Miss
    • Caused when the cache cannot contain all the lines needed during the execution of a program.
    • Occur when lines were in the cache, replaced, and later retrieved.
  • Conflict Miss
    • Multiple lines compete for the same location in the cache, even when the cache has not reached full capacity.

A. FA: Compulsory

B. DM: Compulsory

C. FA: Capacity

D. DM: Capacity

E. FA: Conflict

F. DM: Conflict

G. None of the above

Which types of misses can occur for Fully Associative caches (FA)? For Direct Mapped caches (DM)?

Select all that apply.

Yan, SP26

28-Caches III: Direct Mapped (39)

40 of 46

Yan, SP26

28-Caches III: Direct Mapped (40)

41 of 46

Types of Misses

  • Compulsory Miss
    • Caused by the first access to data that has never been in the cache.
  • Capacity Miss
    • Caused when the cache cannot contain all the lines needed during the execution of a program.
    • Occur when lines were in the cache, replaced, and later retrieved.
  • Conflict Miss
    • Multiple lines compete for the same location in the cache, even when the cache has not reached full capacity.

A. FA: Compulsory

B. DM: Compulsory

C. FA: Capacity

D. DM: Capacity

E. FA: Conflict

F. DM: Conflict

G. None of the above

Which types of misses can occur for Fully Associative caches (FA)? For Direct Mapped caches (DM)?

Select all that apply.

Yan, SP26

28-Caches III: Direct Mapped (41)

42 of 46

Types of Misses

  • Compulsory Miss
    • Caused by the first access to data that has never been in the cache.
  • Capacity Miss
    • Caused when the cache cannot contain all the lines needed during the execution of a program.
    • Occur when lines were in the cache, replaced, and later retrieved.
  • Conflict Miss
    • Multiple lines compete for the same location in the cache, even when the cache has not reached full capacity.
    • Occurs in direct mapped caches, as well as set associative caches (more later).
    • Misses of this type would not occur in a fully associative cache with similar specs.

In this class, we will only distinguish between compulsory and non-compulsory misses.

Yan, SP26

28-Caches III: Direct Mapped (42)

43 of 46

How to categorize misses

  • Run an address trace against a set of caches.
    • (thanks Prof. Kubiatowicz for the algorithm).

  1. First, consider an infinite-size, fully-associative cache. For every miss that occurs now, consider it a compulsory miss.

  • Next, consider a finite-sized cache (of the size you want to examine) with full-associativity. Every miss that is not in #1 is a capacity miss.

  • Finally, consider a finite-size cache with finite-associativity. All of the remaining misses that are not #1 or #2 are conflict misses.

[reference]

In this class, we will only distinguish between compulsory and non-compulsory misses.

Yan, SP26

28-Caches III: Direct Mapped (43)

44 of 46

[Extra] Practice

Agenda

  • Line Replacement Policies
  • Write Policies
  • Direct Mapped Cache
  • Direct Mapped Cache Analysis

  • Types of Misses

Yan, SP26

28-Caches III: Direct Mapped (44)

45 of 46

Direct Mapped Cache (write-back)

Suppose the 4-line, 4B line size cache below starts cold.

  1. Load byte 0xFE2
  2. Load byte 0xFE8
  3. Load word 0xFE9
  4. Load word 0xDF9
  5. Load byte 0xFE8

  1. What is the resulting state of the cache?
  2. What misses occur, and are they compulsory or non-compulsory?

valid

dirty

Tag

Data

11

10

01

00

0

0

0

0

Yan, SP26

28-Caches III: Direct Mapped (45)

46 of 46

Direct Mapped Cache (write-back)

Suppose the 4-line, 4B line size cache below starts cold.

  • Load byte 0xFE2 0b1111 1110 0010 0xFE, 0x0, 0x2
  • Load byte 0xFE8 0b1111 1110 1000 0xFE, 0x2, 0x0
  • Load word 0xFE9 0b1111 1110 1001 0xFE, 0x2, 0x1
  • Load word 0xDF9 0b1101 1111 1001 0xDF, 0x2, 0x1
  • Load byte 0xFE8 0b1111 1110 1000 0xFE, 0x2, 0x0

  • What is the resulting state of the cache?
  • What misses occur, and are they compulsory or non-compulsory?

valid

dirty

Tag

Data

11

10

01

00

0

0

0

0

Yan, SP26

28-Caches III: Direct Mapped (46)