Memory Systems
A memory hierarchy is a layered organization of storage according to access time, capacity, cost per bit, volatility and proximity to the CPU. No one technology is simultaneously fastest, largest, cheapest and non-volatile, so active information is copied into small fast levels while bulk information remains in larger slow levels.
The physical memory hierarchy. Virtual memory is a separate address-translation and protection mechanism that may use backing storage.
| Level | Typical property | Main function |
|---|---|---|
| CPU registers | Fastest, only a few bytes or words, volatile | Hold the current instruction, addresses, operands and results |
| Cache (L1/L2/L3) | Very fast SRAM, small and volatile | Keep copies of recently or nearby used memory blocks |
| Main memory | DRAM, typically gigabytes, volatile | Hold active programs and data |
| Auxiliary storage | Large non-volatile SSD, HDD, optical disk or tape | Store programs, files and archives persistently |
Levels of the memory hierarchy.
Virtual memory is not another physical storage tier between RAM and disk. It is an address-translation, isolation and protection mechanism: virtual pages map to RAM frames, and selected nonresident pages may have copies in backing storage. A system can use virtual addressing even when it does not page data to disk.
Moving downward in the figure increases capacity and access time while reducing cost per bit. Moving upward increases speed and cost per bit. Registers and caches are managed mainly by compiler and hardware, physical-memory allocation and virtual mappings by the memory-management unit (MMU) plus the operating system, and files by the operating system and applications.
Locality and Block Transfer
Section titled “Locality and Block Transfer”The hierarchy works because normal programs do not reference all addresses uniformly:
-
Temporal locality: a recently referenced instruction or datum is likely to be referenced again soon, as in a loop variable.
-
Spatial locality: addresses near a referenced item are likely to be used soon, as in sequential array traversal.
-
Sequential access: instruction execution normally proceeds through neighboring addresses until a branch.
Information moves between adjacent levels in blocks: a cache line between cache and RAM, a page between RAM and backing storage, and a file-system block between secondary storage and memory. Larger blocks exploit spatial locality but increase transfer time and may fetch unused bytes.
Cache Memory
Section titled “Cache Memory”A cache is a small, fast semiconductor memory logically between the CPU and main memory. It stores copies of selected instruction and data blocks so that most references avoid the longer DRAM access time.
Typical systems use a small private L1 instruction cache and L1 data cache, a larger L2 cache and a still larger shared L3 cache. On a hit, a valid entry has a tag matching the requested address and the selected word is returned. On a miss, the next hierarchy level supplies a complete line; the requested word is forwarded and the line is installed for reuse.
Cache lookup, tag test, hit return and miss-refill paths. The address is split into tag, index and offset fields, and locality makes hits common.
As the figure shows, an address is interpreted as tag index offset. The index selects a line or set, the tag identifies which main-memory block occupies it, and the offset selects the byte or word within the line. A matching tag without a set valid bit is not a hit.
Placement or Mapping
Section titled “Placement or Mapping”| Mapping | Placement rule | Main trade-off |
|---|---|---|
| Direct mapped | Each memory block has exactly one possible cache line | Fast, simple and inexpensive, but vulnerable to repeated conflicts |
| Fully associative | A block may occupy any cache line | Eliminates placement conflicts, but requires costly parallel tag search and replacement choice |
| Set associative | A block selects one set and may occupy any way in that set | Practical balance among hit time, hardware cost and conflict misses |
Cache mapping methods.
Hit Ratio and Average Memory Access Time
Section titled “Hit Ratio and Average Memory Access Time”If is hit ratio, is cache lookup time and is the additional main-memory time on a miss, then
The equivalent general notation is
where and is the additional miss penalty. Thus, for , and ,
Miss Types
Section titled “Miss Types”| Miss | Cause | Typical mitigation |
|---|---|---|
| Compulsory (cold) | First access to a block; no prior copy can be present | Prefetching or a larger line when spatial locality is strong |
| Capacity | Active working set is larger than the cache | Larger cache or a smaller/reorganized working set |
| Conflict | Mapping forces simultaneously useful blocks into the same line or set despite free space elsewhere | More associativity or different placement |
The three classical cache-miss causes.
Random access weakens spatial locality; a workload larger than the cache causes capacity misses. In multicore systems, coherence actions can invalidate a line and create additional coherence traffic or misses.
Write Policies
Section titled “Write Policies”| Policy | Operation | Consequence |
|---|---|---|
| Write-through | Update cache and next memory level on every hit | Simple memory consistency, but high write traffic; a write buffer is normally used |
| Write-back | Update cache only and mark the line dirty; write it to the next level when evicted | Lower traffic, but needs dirty bits and more complex replacement/coherence handling |
| Write-allocate | On a write miss, fetch the line then write in cache | Exploits future locality; commonly paired with write-back |
| No-write-allocate | On a write miss, send the write downward without filling the cache | Avoids filling with data unlikely to be reused; often paired with write-through |
Cache write-policy choices.
Memory Technologies
Section titled “Memory Technologies”RAM and ROM
Section titled “RAM and ROM”Random-access memory (RAM) permits direct read/write access to any address but is volatile. Read-only memory (ROM) and its programmable variants retain contents without power and are used for firmware or fixed data.
| Type | Storage and refresh | Relative property | Main use |
|---|---|---|---|
| SRAM | Bistable cell; no periodic refresh while powered | Faster, costlier and lower density | Processor cache |
| DRAM | Capacitor charge; must be periodically refreshed | Slower, cheaper and higher density | Main memory |
SRAM and DRAM comparison.
| Variant | Defining feature |
|---|---|
| PROM | User-programmable once; it cannot normally be erased |
| EPROM | Erased by ultraviolet light and then reprogrammed |
| EEPROM | Electrically erased and rewritten, commonly at byte granularity |
| Flash memory | Electrically erased in blocks, giving high density for firmware and solid-state storage |
ROM and non-volatile semiconductor variants.
Secondary and Offline Storage
Section titled “Secondary and Offline Storage”| Storage | Characteristics | Typical use |
|---|---|---|
| HDD | Magnetic rotating disk with mechanical seek and low cost per gigabyte | Large economical online storage |
| SSD | Flash based, with no moving parts and low random-access latency; cells have finite write endurance | Operating system, applications and active data |
| Optical disk | Laser-read or laser-written removable medium | CD, DVD and Blu-ray distribution or archive |
| Magnetic tape | Very high capacity and low cost, but primarily sequential access | Backup and long-term archive |
Common secondary-storage technologies.
| Property | Cache | Main memory | Auxiliary storage |
|---|---|---|---|
| Technology | SRAM | DRAM | Flash or magnetic/optical media |
| CPU access | Hardware tag lookup | Through memory controller | Through I/O and the operating system |
| Unit moved | Cache line | Byte/word at the interface | Block or file |
| Persistence | No | No | Yes |
| Main limit | Capacity, misses and coherence cost | Refresh and finite capacity | High latency, wear or mechanical delay |
Cache, main memory and auxiliary storage.
Virtual Memory and Paging
Section titled “Virtual Memory and Paging”Virtual memory is a hardware-and-operating-system mechanism that gives each process a large private logical address space independent of the amount and placement of physical RAM. With paging, virtual memory is divided into equal-size pages and RAM into frames of the same size.
A virtual address is divided into
The MMU looks for the VPN in a translation lookaside buffer (TLB), a small cache of recent address translations. On a TLB hit, its frame number is combined with the unchanged offset. On a TLB miss, hardware or operating-system software reads the process page table. A TLB miss is therefore not necessarily a page fault: the page-table entry may show that the page is already in RAM.
| Field | Purpose |
|---|---|
| Frame number | Identifies the RAM frame containing a present page |
| Present/valid bit | Distinguishes a resident legal translation from an absent page; architectures may use separate bits for validity and presence |
| Protection bits | Permit or forbid read, write, execute and user-mode access |
| Referenced/accessed | Records recent use for page-replacement decisions |
| Dirty/modified bit | Records a write so an evicted page can be saved first |
| Backing-store slot | Identifies where an absent page can be reloaded |
Essential page-table-entry fields.
For byte addressing and page size bytes, the physical address is
Virtual-memory translation through the TLB and page table. An illegal or forbidden access raises a protection fault; a legal but nonresident VPN 1 causes a page-fault reload from disk slot S17 into physical frame 2.
Page-Fault Service
Section titled “Page-Fault Service”If a referenced legal page is absent, the page fault in the figure traps to the operating system:
-
Save enough process state to restart the faulting instruction and verify that the virtual address and requested access are legal.
-
Obtain a free frame or choose a victim using the replacement policy. If the victim is dirty, write it to backing storage before reuse.
-
Read the required page from SSD or disk into the selected frame. The process waits while another process may run.
-
Update the page table, repair or invalidate the affected TLB entry, and restart the faulting instruction so translation is retried.
An illegal address or a forbidden read/write/execute access causes a protection exception and normally a process signal or termination; it must not be treated as an ordinary demand-page reload. Storage service is millions of CPU cycles slower than a cache or RAM access, so even a low page-fault rate can dominate execution time.
Benefits, Costs and Thrashing
Section titled “Benefits, Costs and Thrashing”Paging allows a program to use an address space larger than currently assigned RAM, relocates pages without changing program addresses, isolates processes, supports multiprogramming, shares selected pages and applies per-page protection. Its costs include page-table memory, TLB/page-walk overhead, internal fragmentation in partly used final pages and extremely slow faults.
Thrashing occurs when the active working sets of running processes do not fit in available frames. The system repeatedly faults and replaces pages that are needed again almost immediately, so storage traffic rises and useful CPU work collapses. Remedies include allocating more frames, reducing the degree of multiprogramming, using locality-aware replacement and controlling each process by its working set.
Quick Review
Section titled “Quick Review”-
Hierarchy: registers cache RAM secondary storage trades speed for capacity and cost.
-
Cache: uses temporal and spatial locality; a hit returns a matching valid line, while a miss fetches and installs a block.
-
AMAT: ; always identify whether the stated penalty is additional.
-
Paging: maps VPNs to equal-size physical frames; the offset is unchanged.
-
TLB miss versus page fault: a TLB miss requests a page-table lookup; a page fault means the legal page is absent from RAM.