Cse211 - Unit 5
Cse211 - Unit 5
1/15
Memory Hierarchy
• Memory Hierarchy is to obtain the highest possible access speed.
• While minimizing the total cost of the memory system.
Magnetic
tapes I/O Main
processor memory
Magnetic
disks
CPU Cache
memory
Register
Cache
Main Memory
Magnetic Disk
Magnetic Tape
Main Memory
RAM and ROM Chips
Typical RAM chip
Chip select 1 CS1
Chip select 2 CS2
Read RD 128 x 8 8-bit data bus
RAM
Write WR
7-bit address AD 7
- The low-order lines in the address bus select the byte within the
chips
- And other lines in the address bus select a particular chip through
its chip select inputs
Connection of Memory to CPU
Address bus CPU
16-11 10 9 8 7-1 RD WR Data bus
Decoder
3 2 1 0
CS1
CS2
Data
RD 128 x 8
RAM 1
WR
AD7
CS1
CS2
Data
RD 128 x 8
RAM 2
WR
AD7
CS1
CS2
Data
RD 128 x 8
RAM 3
WR
AD7
CS1
CS2
Data
RD 128 x 8
RAM 4
WR
AD7
CS1
CS2
Data
1- 7 512 x 8
8
9 } AD9 ROM
Auxiliary Memory
Information Organization on Magnetic Tapes
file i
block 1 block 2
EOF
R1
block 3
R2 R3 R4
R5
R6
block 3 IRG
R1
EOF R3 R2
R5 R4 block 1
block 2
Track
Associative Memory
- Accessed by the content of the data rather than by an address
- Also called Content Addressable Memory (CAM)
Hardware Organization
Argument register(A)
K1 Kj Kn
Write
R S Match
F ij logic To M i
Read
Output
Cache Memory
Locality of Reference
- Temporal Locality
The information which will be used in near future is likely to be in use
already
( e.g. Reuse of same address or information in loops)
- Spatial Locality
If a word is accessed, adjacent(near) words are likely accessed soon
Cache
- The property of Locality of Reference makes the cache memory systems work
- Cache is a fast small capacity memory that should hold those information
which are most likely to be accessed
Cache memory
Performance of Cache
Memory Access
• All the memory accesses are directed first to Cache
• If the word is not in Cache; Bring a block (or a line) including that word to
replace a block now in Cache
Te = Tc + (1 - h) Tm
Mapping Function
- Mapping Table
Stores both address and the content of the memory word
address (15 bits)
Argument register
Address Data
01000 3450
CAM 02777 6710
22235 1234
Cache Mapping – Direct mapping
- Each memory block has only one place to load in Cache
- n-bit memory address consists of 2 parts; k bits of Index field and n-k bits of Tag field
- n-bit addresses are used to access main memory and k-bit Index is used to access the Cache
n -k k
Addressing Relationships Tag(6) Index(9)
01777 4560
02000 5670
777 02 6710
02777 6710
n -k k
Addressing Relationships Tag(6) Index(9)
00 000
32K x 12
000
512 x 12
Main memory Cache memory
Address = 15 bits
Address = 9 bits
Data = 12 bits
Data = 12 bits
777
77 777
Direct Mapping Cache Organization
Memory
address Memory data
Cache memory
00000 1220
Index
address Tag Data
00777 2340 000 00 1220
01000 3450
01777 4560
02000 5670
777 02 6710
02777 6710
Cache Mapping – Direct mapping
Operation
Block 63 770 02
777 02 6710
Cache Mapping –
Set Associative Mapping
- Each memory block has a set of locations in the Cache to load
Set Associative Mapping Cache with set size of two
Write Through
• When writing into memory
If Hit, both Cache and memory is written in parallel
If Miss, Memory is written
• For a read miss, missing block may be overloaded onto a cache block
Memory is always updated
-> Important when CPU and DMA I/O are both executing
Slow, due to the memory access time
Cache Write
Write-Back (Copy-Back)
• When writing into memory
If Hit, only Cache is written
If Miss, missing block is brought to Cache and write into Cache
• For a read miss, candidate block must be written back to the memory
Memory is not up-to-date, i.e.,
The same item in Cache and memory may have different value
VIRTUAL MEMORY
Virtual Memory
Give the programmer the illusion that the system has a very large memory, even
though the computer actually has a relatively small main memory
Address Mapping
Memory Mapping Table for Virtual Address -> Physical Address
Virtual address
Physical
Address
Memory table Main memory
buffer register buffer register
Address Mapping
Address Space and Memory Space are each divided into fixed size
group of words called blocks or pages
Page 0
1K words group Page 1
Page 2
Address space Memory space Block 0
Page 3
N = 8K = 213 M = 4K = 212 Block 1
Page 4
Block 2
Page 5
Block 3
Page 6
Page 7
Organization of memory Mapping Table in a paged system (Page and line no.)
Page no. Line number
1 0 1 0 1 0 1 0 1 0 0 1 1 Virtual address
Table Presence
address bit
000 0 Main memory
001 11 1 Block 0
010 00 1 Block 1
011 0 01 0101010011 Block 2
100 0 Block 3
Main memory
101 01 1 address register
Memory page table 110 10 1
111 0 MBR
01 1
Page Replacement
Decision on which page to displace to make room for an incoming page
when no free frame is available
valid/
frame invalid bit swap
out
1 victim
page
2
change to victim
f 0 v i invalid
3
4 swap
f v reset page
table for desired
new page page in
backing store
page table
physical memory
Page Replacement
Modified page fault service routine
1. Find the location of the desired page on the backing store
2. Find a free frame
- If there is a free frame, use it
- Otherwise, use a page-replacement algorithm to select a victim frame
- Write the victim page to the backing store
3. Read the desired page into the (newly) free frame
4. Restart the user process
valid/
frame invalid bit swap
out
1 victim
page
2 change to
f 0 v i invalid victim
3
4
f v reset page swap
table for desired
new page page in
backing store
page table
physical memory
Page Replacement Algorithms
FIFO Reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 4 4 4 0 0 0 7 7 7
0 0 0 3 3 3 2 2 2 1 1 1 0 0
1 1 1 0 0 0 3 3 3 2 2 2 1
Page frames
• FIFO algorithm selects the page that has been in memory the longest time.
• Using a queue - every time a page is loaded, its identification is inserted in
the queue.
-
• Easy to implement.
• May result in a frequent page fault.
0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 2 7
0 0 0 0 4 0 0 0
1 1 3 3 3 1 1
Page frames
Page Replacement Algorithms
LRU
Reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 4 4 4 0 1 1 1
0 0 0 0 0 0 3 3 3 0 0
1 1 3 3 2 2 2 2 2 7
Page frames