Microprocessor and Computer ArchitectureUnit 614 min read
Memory Hierarchy & Organization: Speed, Cost, Trade-offs & Cache
Unit 6 of Microprocessor and Computer Architecture explores how modern computers organize memory into layers (registers → cache → RAM → storage) to balance speed, capacity, and cost, with a focus on cache mapping techniques, memory addressing, and real-world trade-offs like latency vs. throughput.
TAKEAWAYS:
- Memory hierarchy is a speed-cost trade-off where smaller, faster layers (cache) sit between the CPU and slower but larger layers (RAM/disk).
- Cache mapping (direct, associative, set-associative) determines how data blocks are placed in cache and how conflicts are resolved.
- Locality principles (temporal and spatial) explain why caches work: programs reuse nearby data and recently accessed data.
- Memory addressing uses base registers, page tables, and segmentation to map virtual addresses to physical memory locations.
- DRAM vs. SRAM differ in speed, power, and cost, with SRAM used exclusively for cache due to its low access time.
- Virtual memory extends physical RAM using disk storage, enabling larger address spaces than available hardware.
The Memory Hierarchy: Why Layers?
Computers use a memory hierarchy to balance three critical factors:
- Speed (how fast data can be accessed)
- Capacity (how much data can be stored)
- Cost (price per unit of storage)
The hierarchy is organized from fastest/smallest (closest to the CPU) to slowest/largest (farther from the CPU):
CPU Registers (ns) → Cache (1–100 ns) → Main Memory (RAM, 100–300 ns) → Secondary Storage (µs–ms)
Why Not Just Use Fast Memory Everywhere?
- Cost: Fast memory (e.g., SRAM) is 10–100x more expensive than slower DRAM.
- Power: Fast memory consumes more power, heating up the system.
- Limited capacity: Even the largest cache today holds <100 MB, while a typical program needs GBs of data.
The hierarchy exploits locality: programs tend to:
- Temporal locality: Reuse recently accessed data (e.g., looping through an array).
- Spatial locality: Access data near recently used memory (e.g., reading consecutive array elements).
Layer 1: CPU Registers (Fastest, Smallest)
Registers are the fastest memory in the system, directly connected to the CPU. They hold:
- Data registers (e.g.,
AX,BXin 8085) for arithmetic/logic operations. - Address registers (e.g.,
PC,SP) for program control. - Special-purpose registers (e.g.,
PSWfor flags).
Key Properties:
| Property | Value | Example (8085) |
|---|---|---|
| Speed | 1–4 clock cycles (~1 ns) | Register access |
| Size | 8–64 registers (typically 16–32 bits) | 8 general-purpose registers |
| Cost | Highest per bit | Built into CPU core |
| Volatility | Volatile (lost on power-off) | N/A |
Layer 2: Cache Memory (Fast, but Limited)
Cache sits between the CPU and RAM to reduce the average memory access time (AMAT). It is SRAM-based (static RAM), which is faster but more expensive than DRAM.
Cache Organization
Cache is divided into blocks (or lines), each holding a chunk of data (typically 32–128 bytes). The mapping of main memory to cache uses one of three strategies:
1. Direct-Mapped Cache
- Each memory block maps to exactly one cache line.
- Pros: Simple, fast lookup.
- Cons: Conflict misses (two frequently used blocks map to the same cache line).
stateDiagram-v2
[*] --> Main_Memory
Main_Memory --> Cache_Line: Direct Mapping
Cache_Line --> CPU: Hit
Cache_Line --> [*]: Miss (replace)2. Fully Associative Cache
- A memory block can go into any cache line.
- Pros: No conflict misses.
- Cons: Slow lookup (requires searching all lines).
3. Set-Associative Cache (Most Common)
- Cache is divided into sets; each memory block maps to a specific set.
- Within a set, blocks can be placed in any line (like associative but with limited choices).
- Example: 4-way set-associative means 4 lines per set.
| Mapping Type | Flexibility | Speed | Conflict Misses | Example Use Case |
|---|---|---|---|---|
| Direct-Mapped | Low | Fastest | High | Embedded systems (cost-sensitive) |
| Fully Associative | High | Slowest | None | High-performance CPUs (rare) |
| Set-Associative | Medium | Balanced | Medium | Modern x86 CPUs (e.g., Intel Core) |
Cache Mapping Example: Direct-Mapped
Problem: Suppose a cache has 16 lines and 32-byte blocks. Main memory is 32-bit addressed. How is block 0x12345678 mapped?
- Block offset: Lower 5 bits (
0x18) → identifies byte within block. - Index: Next 4 bits (
0x56) → selects cache line (0x56 / 16 = line 8). - Tag: Remaining bits (
0x1234) → stored with the block in cache.
Visualization:
Layer 3: Main Memory (RAM)
RAM is volatile DRAM (Dynamic RAM), where data is stored in capacitors that must be refreshed periodically. Key properties:
| Property | Value | Example (Modern Laptop) |
|---|---|---|
| Speed | 100–300 ns (100–300 MHz) | DDR4-3200 (3.2 GHz) |
| Capacity | 4 GB–128 GB (scalable) | 16 GB DDR4 |
| Cost | ~$0.05–$0.20 per GB | ~$80 for 16 GB |
| Volatility | Volatile | Lost on power-off |
How RAM Works Internally
RAM is organized in a 2D grid of rows and columns:
- Row: Activated by Row Address Strobe (RAS).
- Column: Activated by Column Address Strobe (CAS).
- Refresh: Required every 64 ms (for DRAM).
Layer 4: Secondary Storage (Slowest, Largest)
Secondary storage (HDD/SSD) is non-volatile and holds data even when powered off. Key types:
| Type | Speed (Read) | Capacity | Cost (per GB) | Use Case |
|---|---|---|---|---|
| HDD | 50–150 MB/s | 500 GB–16 TB | ~$0.02 | Bulk storage (e.g., servers) |
| SSD | 300–3500 MB/s | 120 GB–8 TB | ~$0.10 | OS, apps (e.g., laptops) |
| NVMe SSD | 2000–7000 MB/s | 256 GB–16 TB | ~$0.15 | High-performance (e.g., gaming PCs) |
Real-World Example: eSewa App (Nepal)
- Problem: eSewa needs to store millions of transactions securely and quickly.
- Solution:
- Cache: Frequently accessed user profiles and recent transactions are stored in CPU cache or RAM.
- Main Memory (RAM): Active sessions and temporary data (e.g., payment processing) reside here.
- Secondary Storage (SSD): All transactions are logged to SSDs for persistence.
- Database (SQL/NoSQL): Uses indexing (like cache mapping) to speed up queries.
Memory Addressing: How the CPU Finds Data
The CPU uses addresses to locate data in memory. Two key concepts:
1. Physical vs. Virtual Addressing
| Address Type | Description | Example |
|---|---|---|
| Physical | Direct hardware address (limited by RAM size). | 0x00000000 to 0xFFFFFFFF |
| Virtual | Abstract address (translated by MMU to physical). | Enables multitasking and large address spaces. |
2. Address Translation (Paging)
The Memory Management Unit (MMU) translates virtual addresses to physical addresses using a page table:
sequenceDiagram
CPU->>MMU: Virtual Address (e.g., 0x7FFE8000)
MMU->>Page_Table: Lookup Page Number (0x7FF)
Page_Table-->>MMU: Physical Frame (e.g., 0x1234)
MMU->>CPU: Physical Address (0x12348000)Example: In a system with 4 KB pages:
- Virtual address
0x7FFE8000:- Page number:
0x7FFE8000 >> 12 = 0x7FF(top 20 bits). - Offset:
0x8000(bottom 12 bits).
- Page number:
- The MMU looks up
0x7FFin the page table to find the physical frame (e.g.,0x1234), then combines it with the offset to get0x12348000.
Virtual Memory: Extending RAM with Disk
When physical RAM is full, the OS uses swap space (on disk) to:
- Page out: Move rarely used pages to disk.
- Page in: Load pages back when needed.
Example: Pathao Driver App (Nepal)
- Scenario: A driver’s app needs to display real-time traffic routes and user locations.
- Memory Hierarchy:
- Cache: Frequently accessed routes (e.g., Kathmandu-Thamel) are cached.
- RAM: Active user sessions and map tiles.
- SSD: Full map database (paged in/out as needed).
- HDD: Backup logs of driver trips.
Trade-off: Swapping slows down the system (thrashing), but enables running large apps (e.g., Adobe Photoshop) on machines with limited RAM.
Cache Performance Metrics
To evaluate cache effectiveness, we use:
- Hit Time (t_h): Time to access data in cache (e.g., 1 ns).
- Miss Penalty (t_m): Time to fetch data from RAM (e.g., 100 ns).
- Hit Rate (H): Probability of finding data in cache (e.g., 95% = 0.95).
Average Memory Access Time (AMAT): Example: For a cache with:
- ns,
- ns,
- :
Without cache: 100 ns → 17x speedup!
Real-World Applications of Memory Hierarchy
1. Khalti (Digital Payment System, Nepal)
- Problem: Millions of transactions per second require low-latency access to user accounts and transaction logs.
- Solution:
- Cache: Frequently accessed user profiles (e.g., top merchants) are cached in L1/L2 cache.
- RAM: Active transactions are stored in DRAM with in-memory databases (e.g., Redis).
- SSD: Persistent storage for all transactions (logged to disk every 100 ms).
- HDD: Archival storage for old transactions (compressed).
2. YouTube (Global)
- Problem: Serving billions of video streams requires fast access to video chunks.
- Solution:
- CDN Caches: Videos are cached in edge servers worldwide (using set-associative caches).
- RAM: Active video buffers are stored in DRAM.
- SSD/HDD: Original videos are stored in distributed storage (e.g., Google Cloud Storage).
3. Nepal Electricity Authority (NEA) Grid Management
- Problem: Real-time monitoring of 1000+ substations requires low-latency data access.
- Solution:
- Cache: Critical sensor data (e.g., voltage levels) is cached in CPU cache.
- RAM: Active monitoring data is stored in DRAM with priority queues.
- SSD: Historical data is stored in time-series databases (e.g., InfluxDB).
- HDD: Archival logs for audits.
Worked Example: Cache Hit/Miss Analysis
Scenario: A program accesses memory in this order:
0x000, 0x010, 0x020, 0x000, 0x030, 0x040, 0x000, 0x050
Assume:
- Cache size: 4 blocks (direct-mapped).
- Block size: 16 bytes (offset = 4 bits).
- Index bits: 2 (since blocks).
| Address | Block Number | Cache Line (Index) | Hit/Miss | Action |
|---|---|---|---|---|
| 0x000 | 0 | 0 | Miss | Load block 0 into line 0 |
| 0x010 | 1 | 1 | Miss | Load block 1 into line 1 |
| 0x020 | 2 | 2 | Miss | Load block 2 into line 2 |
| 0x000 | 0 | 0 | Hit | Data in line 0 |
| 0x030 | 3 | 3 | Miss | Load block 3 into line 3 |
| 0x040 | 4 | 0 (conflict!) | Miss | Evict block 0, load block 4 into line 0 |
| 0x000 | 4 | 0 | Miss | Block 4 is now in line 0 |
| 0x050 | 5 | 1 (conflict!) | Miss | Evict block 1, load block 5 into line 1 |
Hit Rate: 1 hit / 8 accesses = 12.5% (poor due to conflicts!). Solution: Use set-associative cache (e.g., 2-way) to reduce conflicts.
Memory Hierarchy Trade-offs Table
| Layer | Speed (ns) | Size (Typical) | Cost (per GB) | Volatility | Use Case |
|---|---|---|---|---|---|
| Registers | 0.5–4 | KB | Very High | Volatile | CPU arithmetic/logic operations |
| L1 Cache | 1–4 | 32–256 KB | High | Volatile | Frequently used instructions/data |
| L2 Cache | 4–10 | 256 KB–8 MB | Medium | Volatile | Less frequent but still critical |
| L3 Cache | 10–50 | 1–64 MB | Medium | Volatile | Shared among CPU cores |
| RAM (DRAM) | 100–300 | 4 GB–128 GB | Low | Volatile | Main program and data storage |
| SSD | 10,000–100,000 | 120 GB–8 TB | Very Low | Non-volatile | Persistent storage (OS, apps) |
| HDD | 1,000,000+ | 500 GB–16 TB | Very Low | Non-volatile | Bulk data storage (backups) |
Exam Tip: How to Score Full Marks
Diagrams Are Mandatory:
- Always draw the memory hierarchy pyramid (speed vs. size).
- Show cache mapping (direct/associative/set-associative) with labeled blocks.
- Include address translation (virtual → physical) with page tables.
Define Key Terms Clearly:
- Locality: Temporal vs. spatial.
- Cache hit/miss: Define with examples.
- AMAT formula: Derive and explain each term.
Real-World Examples:
- Link cache to eSewa transactions or Pathao routing.
- Compare HDD vs. SSD in NEA grid monitoring.
Worked Problems:
- Given a memory trace, calculate hit rate and AMAT.
- For a cache size/block size, determine index/tag bits.
Common Pitfalls:
- Don’t confuse associativity with mapping: Direct-mapped is 1-way, associative is N-way.
- Don’t forget the offset: It’s part of the address but not stored in cache.
- Virtual memory ≠ cache: Virtual memory extends RAM with disk; cache is separate.
Summary Checklist
Before the exam, ensure you can: ✅ Explain the memory hierarchy with speed/cost trade-offs. ✅ Compare direct, associative, and set-associative cache mapping. ✅ Calculate AMAT given hit rate and latencies. ✅ Describe address translation (virtual → physical) with page tables. ✅ Give 2 real-world examples (e.g., eSewa, YouTube) of memory hierarchy in action. ✅ Draw a cache block diagram with tags, indices, and offsets. ✅ Explain locality principles (temporal/spatial) with examples.
Based on the TU BCA syllabus for Microprocessor and Computer Architecture (CACS155), unit 6.
Discussion
Loading…