Computer Systems Lecture 16 Caching Introduction CS 230 - Spring - - PowerPoint PPT Presentation

computer systems
SMART_READER_LITE
LIVE PREVIEW

Computer Systems Lecture 16 Caching Introduction CS 230 - Spring - - PowerPoint PPT Presentation

CS 230 Introduction to Computers and Computer Systems Lecture 16 Caching Introduction CS 230 - Spring 2020 3-1 MEM Memory Access If instruction is lw or sw load from or store to memory How? accessing memory is slow


slide-1
SLIDE 1

CS 230 - Spring 2020 3-1

CS 230 – Introduction to Computers and Computer Systems Lecture 16 – Caching Introduction

slide-2
SLIDE 2

CS 230 - Spring 2020 3-2

MEM – Memory Access

 If instruction is lw or sw

 load from or store to memory

 How?

 accessing memory is slow  much slower than registers  how do we do the MEM stage quickly?

slide-3
SLIDE 3

CS 230 - Spring 2020 3-3

Memory Hierarchy

 Going down the

hierarchy memory is

 cheaper  bigger  slower  further away from CPU

slide-4
SLIDE 4

CS 230 - Spring 2020 3-4

Typical Memory Hierarchy

 Registers  Cache  Main memory  SSD/HDD  Network  Off-site archive

 (tape, optical disk, etc.)

slide-5
SLIDE 5

CS 230 - Spring 2020 3-5

Registers

 Very expensive, thus limited  Access at instruction speed

 very low latency  very high throughput  can access in less than a cycle in ID and WB

 Not persistent

 lose values on power-off

slide-6
SLIDE 6

CS 230 - Spring 2020 3-6

Main Memory

 Cheap and large

 several gigabytes

 Noticeable access latency

 can be approximately 100x slower than registers

 Not persistent  Random Access Memory (RAM)

 send address to memory controller on memory bus  memory controller responds with value

slide-7
SLIDE 7

CS 230 - Spring 2020 3-7

Disk

 Hard Disk Drive (HDD) or Solid State Drive (SSD)  Very large storage

 hundreds of gigabytes to multiple terabytes

 Very long access latency

 thousands of times slower than main memory

 Persistent

slide-8
SLIDE 8

CS 230 - Spring 2020 3-8

Memory Stalls

 How long does the MEM stage actually take?  Delay until response from memory controller

slide-9
SLIDE 9

CS 230 - Spring 2020 3-9

Locality

 The set of data used during certain time period

is called the working set

 Assumption:

 size(working set) < size(all data/memory)  working set much smaller than available memory

 Example: books kept on your desk vs. books on

shelves in library (Dewey Decimal System)

slide-10
SLIDE 10

CS 230 - Spring 2020 3-10

Locality Principle

 Temporal locality

 same data item likely to be used again soon  example: loop  library example continued: you’ll likely need the

same book again

 Spatial locality

 close data item likely to be used soon  example: iteration through array  library example continued: books beside ones you

are using are likely relevant

slide-11
SLIDE 11

CS 230 - Spring 2020 3-11

Caching

 A cache is a small amount of fast memory used

to keep recently used (and nearby) data quickly accessible

 exploits the locality principle

 Basic challenges

 limited fast memory: replacement strategy  shared remote data: invalidation

 Data vs. Instruction cache

 in CS 230 we focus on data cache

slide-12
SLIDE 12

CS 230 - Spring 2020 3-12

Terminology

 When a memory access (lw) happens the CPU

checks the cache first:

 hit = data found in the cache  miss = data not found, go get it from main memory

 Timing

 time if hit: hit time  time if missed: miss penalty

 Recall byte-addressable

 each byte has it’s own address

slide-13
SLIDE 13

CS 230 - Spring 2020 3-13

Memory Caching

 Cache is organized into cache-blocks

 also called cache lines  multiple words/bytes per block (block size)  load from main memory to cache in blocks  library example continued: page vs. book

 How to know whether data is present?

 each block from main memory has an identity tag  library example continued: Dewey Decimal Index

 Where do we put newly loaded cache-blocks?

slide-14
SLIDE 14

CS 230 - Spring 2020 3-14

Direct-Mapped Cache

 Assume a computer with M cache-blocks and a

block size of B bytes

 Want to load address P  Address P goes into cache-block C = (P/B) mod M

 Typically M and B are powers of 2  Discard remainder of P/B

 The tag of cache-block C will be T = P/(BM)

 Discard remainder

slide-15
SLIDE 15

CS 230 - Spring 2020 3-15

Direct-Mapped Cache

 Consider a computer with a word size of 32-bits,

8 cache-blocks, and a block size of 4 words

 4 * 32-bits = 16 byte cache-block size (B)  M = 8

 Consider address P = 0x00000113

 P = 27510  C = (P/B)MOD M = (275/16)MOD 8 = 110  T = P/(BM) = 275/(16*8) = 210

slide-16
SLIDE 16

CS 230 - Spring 2020 3-16

Direct-Mapped Cache Continued

 Conveniently we can also look directly at bits:

 example: M = 8, B = 16, 32-bit machine word  consider address P = 0x00000113

00000000000000000000000100010011 So address 0x113 goes in cache block 1 with tag 2

 Least significant 4 bits are within block since log216 = 4  Next 3 bits are cache-block number since log28 = 3  Remaining 25 bits are tag since 32 - log2(8 * 16) = 25

slide-17
SLIDE 17

CS 230 - Spring 2020 3-17

Direct-Mapped Cache

Addresses in Memory (XXXX means any value) Blocks Numbers in Cache

XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX

slide-18
SLIDE 18

CS 230 - Spring 2020 3-18

Cache Entries

 Many memory blocks map to same cache-block  Tag identifies which one is present  How do we tell if a cache-block actually has

data in it?

 add valid bit  1 (Yes) if data is loaded, 0 (No) if empty

 What if we load into already valid cache-block

 overwrite cache-block with new value  called an eviction

slide-19
SLIDE 19

CS 230 - Spring 2020 3-19

Example Cache Diagram

 Assume M = 8, B = 2 bytes, 8-bit machine word

Index Valid Tag Data 000 N 001 N 010 N 011 N 100 N 101 N 110 N 111 N

slide-20
SLIDE 20

CS 230 - Spring 2020 3-20

Example

Address Binary addr Hit/miss Cache block 4410 0010 110 0 Miss 110 Index Valid Tag Data 000 N 001 N 010 N 011 N 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N The block in memory containing all address 0010110X where X is any value

slide-21
SLIDE 21

CS 230 - Spring 2020 3-21

Example

Address Binary addr Hit/miss Cache block 5310 0011 010 1 Miss 010 Index Valid Tag Data 000 N 001 N 010 Y 0011 Mem[0011010X] 011 N 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N

slide-22
SLIDE 22

CS 230 - Spring 2020 3-22

Example

Address Binary addr Hit/miss Cache block 4510 0010 110 1 Hit 110 5210 0011 010 0 Hit 010 Index Valid Tag Data 000 N 001 N 010 Y 0011 Mem[0011010X] 011 N 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N 2 Hits! The blocks we were looking for were already in the cache.

slide-23
SLIDE 23

CS 230 - Spring 2020 3-23

Example

Address Binary addr Hit/miss Cache block 3310 0010 000 1 Miss 000 610 0000 011 0 Miss 011 Index Valid Tag Data 000 Y 0010 Mem[0010000X] 001 N 010 Y 0011 Mem[0011010X] 011 Y 0000 Mem[0000011X] 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N

slide-24
SLIDE 24

CS 230 - Spring 2020 3-24

Example

Address Binary addr Hit/miss Cache block 3310 0010 000 1 Hit 000 Index Valid Tag Data 000 Y 0010 Mem[0010000X] 001 N 010 Y 0011 Mem[0011010X] 011 Y 0000 Mem[0000011X] 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N

slide-25
SLIDE 25

CS 230 - Spring 2020 3-25

Example

Address Binary addr Hit/miss Cache block 3710 0010 010 1 Miss 010 Index Valid Tag Data 000 Y 0010 Mem[0010000X] 001 N 010 Y 0010 Mem[0010010X] 011 Y 0000 Mem[0000011X] 100 N 101 N 110 Y 0010 Mem[0010110X] 111 N The block that was here before got evicted

slide-26
SLIDE 26

CS 230 - Spring 2020 3-26

Hit Chance

 How many of the accesses were hits?

 previous example had 8 accesses: 3 hits, 5 misses

 3/8 = 0.37510 hit chance  5/8 = 0.62510 miss chance

slide-27
SLIDE 27

CS 230 - Spring 2020 3-27

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

slide-28
SLIDE 28

CS 230 - Spring 2020 3-28

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 N 001 N 010 N 011 N 100 N 101 N 110 N 111 N

slide-29
SLIDE 29

CS 230 - Spring 2020 3-29

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 100 Mem[100000XX] 001 N 010 N 011 N 100 N 101 N 110 N 111 N

miss

100 000 00

tag block xx

slide-30
SLIDE 30

CS 230 - Spring 2020 3-30

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 100 Mem[100000XX] 001 N 010 N 011 N 100 N 101 N 110 N 111 Y 001 Mem[001111XX]

miss miss

001 111 10

tag block xx

slide-31
SLIDE 31

CS 230 - Spring 2020 3-31

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 100 Mem[100000XX] 001 N 010 N 011 N 100 N 101 N 110 N 111 Y 001 Mem[001111XX]

miss miss hit

100 000 11

tag block xx

slide-32
SLIDE 32

CS 230 - Spring 2020 3-32

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 100 Mem[100000XX] 001 N 010 N 011 N 100 N 101 N 110 N 111 Y 001 Mem[001111XX]

miss miss hit hit

001 111 00

tag block xx

slide-33
SLIDE 33

CS 230 - Spring 2020 3-33

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 001 Mem[001000XX] 001 N 010 N 011 N 100 N 101 N 110 N 111 Y 001 Mem[001111XX]

miss miss hit hit miss+evic

001 000 10

tag block xx

slide-34
SLIDE 34

CS 230 - Spring 2020 3-34

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

Index Valid Tag Data 000 Y 001 Mem[001000XX] 001 Y 001 Mem[001001XX] 010 N 011 N 100 N 101 N 110 N 111 Y 001 Mem[001111XX]

miss miss hit hit miss+evic miss

001 001 00

tag block xx

slide-35
SLIDE 35

CS 230 - Spring 2020 3-35

Try it Yourself

Draw a cache diagram showing what the cache would look like after loading the following addresses from memory. What is the hit chance of this cache for this access sequence? Assume M = 8, B = 4 bytes, 8-bit machine word. 0x80, 0x3E, 0x83, 0x3C, 0x22, 0x24

miss miss hit hit miss+evic miss

6 accesses 2 hits 4 misses 2/6 = 0.33310 hit chance