Docsity

Notes on Memory and Cache - Computer Organization | CEG 320, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Notes on Memory and Cache - Computer Organization | CEG 320 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Memory and cacheMemory and cache

CPU (^) Memory I/O

2 Wright State University, College of Engineering CEG 320/

The Memory HierarchyThe Memory Hierarchy

Registers Primary cache Secondary cache Main memory Magnetic disk

  • ~2ns
  • ~4-5ns
  • ~30ns
  • ~220ns+
  • 1ms (~6ms)

4 Wright State University, College of Engineering CEG 320/

Locality of referenceLocality of reference

Temporal locality

  • Recently accessed data/instructions are likely to be accessed again.  Most program time is spent in loops  Arrays are often scanned multiple times  Spatial locality
  • If I access memory address n , I am likely to then access another address close to n (usually n +1, n +2, or n +4)  Linear execution of code  Linear access of arrays

5 Wright State University, College of Engineering CEG 320/

How a cache exploits localityHow a cache exploits locality

 Temporal – When an item is accessed from memory it is brought into the cache

  • If it is accessed again soon, it comes from the cache and not main memory  Spatial – When we access a memory word, we also fetch the next few words of memory into the cache
  • The number of words fetched is the cache line size, or the cache block size for the machine

7 Wright State University, College of Engineering CEG 320/

Cache mappingCache mapping

 Direct mapped – each memory block can occupy one and only one cache block  Example:

  • Cache block size: 16 words
  • Memory = 64K (4K blocks)
  • Cache = 2K (128 blocks)

8 Wright State University, College of Engineering CEG 320/

Direct Mapped CacheDirect Mapped Cache

 Memory block n occupies cache block ( n mod 128)  Consider address $2EF 001011101111 0100 block: $2EF = 751 word: 4  Cache: 00101 1101111 0100 tag: 5 block: 111 word: 4

10 Wright State University, College of Engineering CEG 320/

SetSet--associative mappingassociative mapping

 Blocks are grouped into sets  Each memory block can occupy any block in its set  This example is 2-way set- associative  Which of the two blocks do we replace?

11 Wright State University, College of Engineering CEG 320/

Replacement algorithmsReplacement algorithms

 Random  Oldest first  Least accesses  Least recently used (LRU): replace the block that has gone the longest time without being referenced.

  • This is the most commonly-used replacement algorithm
  • Easy to implement with a small counter…

13 Wright State University, College of Engineering CEG 320/

Interactions with DMAInteractions with DMA

 If we have a write-back cache, DMA must check the cache before acting.

  • Many systems simply flush the cache before DMA write operations  What if we have a memory word in the cache, and the DMA controller changes that word?
  • Stale data  We keep a valid bit for each cache line. When DMA changes memory, it must also set the valid bit to 0 for that cache line.
  • “Cache coherence”

14 Wright State University, College of Engineering CEG 320/

Typical Modern Cache ArchitectureTypical Modern Cache Architecture

 L0 cache

  • On chip
  • Split 16 KB data/16 KB instructions  L1 cache
  • On chip
  • 64 KB unified  L2 cache
  • Off chip
  • 128 KB to 16+ MB

16 Wright State University, College of Engineering CEG 320/

MultiMulti--level cache performancelevel cache performance

tave = h 1 C 1 + (1- h 1 ) h 2 C 2 + (1- h 1 ) (1- h 2 ) M

  • h 1 = hit rate in primary cache
  • h 2 = hit rate in secondary cache
  • C 1 = time to access primary cache
  • C 2 = time to access secondary cache
  • M = miss penalty (time to load an entire cache line from main memory)

17 Wright State University, College of Engineering CEG 320/

Memory InterleavingMemory Interleaving

  • Memory is organized into modules or banks
    • Within a bank, capacitors must be recharged after each read operation
    • Successive reads to the same bank are slow 0000 0002 0004 0006 … 00F 00FA 00FC 00FE Module 0100 0102 0104 0106 … 01F 01FA 01FC 01FE 0200 0202 0204 0206 … 02F 02FA 02FC 02FE Byte Non-interleaved 0000 0006 000C 0012 … 02E 02EE 02F 02FA Module 0002 0008 000E 0014 … 02EA 02F 02F 02FC 0004 000A 0010 0016 … 02EC 02F 02F 02FE Byte Interleaved

19 Wright State University, College of Engineering CEG 320/ Virtual Memory: IntroductionVirtual Memory: Introduction  Motorola 68000 has 24-bit memory addressing and is byte addressable

  • Can address 2^24 bytes of memory – 16 MB  Intel Pentiums have 32-bit memory addressing and are byte addressable
  • Can address 2 32 bytes of memory – 4 GB  What good is all that address space if you only have 256MB of main memory (RAM)?
  • How do you run a program that is LARGER than 256MB?
  • Examples:  Unreal Tournament – 2.4 gibabytes  Neverwinter Nights – 2 gigabytes  In modern computers it is possible to use more memory than the amount physically available in the system  Memory not currently being used is temporarily stored on magnetic disk  Essentially, the main memory acts as a cache for the virtual memory on disk.

20 Wright State University, College of Engineering CEG 320/ Working SetWorking Set  Not all of a program needs to be in memory while you are executing it:

  • Error handling routines are not called very often.
  • Character creation generally only happens at the start of the game… why keep that code easily available?  Working set is the memory that is consumed at any moment by a program while it is running.
  • Includes stack, allocated memory, active instructions, etc.  Examples:
  • Unreal Tournament – 100MB (requires 128MB)
  • Internet Explorer – 20MB