CHAP8
CHAP8
Chapter 8
Memory Hierarchy
CPU regs
8B
C a c h e
32 B
Memory
4 KB
disk
4B
Types of Memory
Real memory
Main memory
Virtual memory
Memory on disk Allows for effective multiprogramming and relieves the user of tight constraints of main memory
Advantages:
More processes may be maintained in main memory With so many processes in main memory, it is very likely a process will be in the Ready state at any particular time (CPU efficiency) A process may be larger than all of main memory
Memory
0 1
Virtual Addresses
CPU
P-1:
N-1
Disk Address Translation: Hardware converts virtual addresses to physical addresses via page table
VM Address Translation
Parameters
P = 2p = page size (bytes) N = 2n = Virtual address limit M = 2m = Physical address limit
n1 virtual page number p p1 page offset 0 virtual address
address translation
0
physical address
Execution of a Program
Operating system brings into main memory a few pieces of the program Resident set - portion of process that is in main memory
An interrupt is generated when an address is needed that is not in main memory - page fault
Operating system places the process in a blocking state and a DMA is started to get the page from disk An interrupt is issued when disk I/O is complete which causes the OS to place the affected process in the Ready Queue
Thrashing Swapping out a piece of a process just before that piece is needed The processor spends most of its time swapping pieces rather than executing user instructions
HERE
Principle of Locality
Program and data references within a process tend to cluster Only a small portion of a process will be needed over a short period of time Possible to make intelligent guesses about which pieces will be needed in the future Therefore, principle of locality can be used to make virtual memory to work efficiently
Page Fault
Page table entry indicates virtual address not in memory OS exception handler invoked to move data from disk into memory
current process suspends, others can resume OS has full control over placement, etc.
Before fault
Memory Page Table
Virtual Address Physical Address
After fault
Memory Page Table
CPU
CPU
Disk
Disk
Read block of length P starting at disk address X and store starting at memory address Y
Read Occurs
Direct Memory Access (DMA) under control of I/O controller Memory-I/O bus (2) DMA Transfer Memory
I/O controller
disk Disk
disk Disk
Takes control of the system from the CPU to transfer data to and from memory over the system bus Only one bus master, usually the DMA controller due to tight timing constraints.
Page Tables
Virtual Page Number Memory resident page table
(physical page Valid or disk address) 1 1 0 1 1 1 0 1 0 1
Physical Memory
Address Translation
page table base register
VPN acts as table index
m1
p p1
page offset
physical address
Present bit (P) == valid bit Modify bit (M) == dirty bit is needed to indicate if the page has been altered since it was last loaded into main memory If no change has been made, the page does not have to be written back to the disk when it needs to be swapped out In addition, a Reference (or use) Bit can be used to indicate if the page is referenced (read) since it was last checked. This can be particularly useful for Clock Replacement Policy (to be studied later)
To overcome this problem a high-speed cache is set up for page table entries
called the TLB - Translation Lookaside Buffer
TLB stores most recently used page table entries. Stores VP numbers and the mapping for it. Uses associative mapping (i.e. many virtual page numbers map to the same TLB index). Given a virtual address, processor examines the TLB first
If page table entry is present (a hit), the frame number is retrieved and the real address is formed
If page table entry is not found in the TLB (a miss), the page number is used to index the page table, and TLB is updated to include the new page entry
Given:
Level 2 Tables
(4 MB)
Problem:
Common solution
multi-level page tables e.g., 2-level table Level 1 table: 1024 entries, each of which points to a Level 2 page table. Level 2 table: 1024 entries, each of which points to a page
...
Page Size ?
Smaller page size less amount of internal fragmentation
(due to the last page of a process)
Smaller page size More pages required per process Larger page tables Page tables in virtual memory
(i.e. double page faults possible)
Also, secondary memory is designed to efficiently transfer large blocks of data which favors a larger page size.
**HERE ** Small page size large number of pages will fit in main memory (large working set) As time goes on during execution, the pages in memory will all contain portions of the process near recent references Page faults low
Increased page size causes pages to contain references which may not be resident in memory (because fewer number of page frames per process) Page faults rise
Fetch Policy
Determines when a page should be brought into memory Demand paging: bring pages into main memory only when it is referenced
Many page faults when the process first started
Replacement Policy
If memory is full, which page should be replaced? Page that is least likely to be referenced in the near future Most policies predict the future behavior on the basis of past behavior Frame Locking
Associate a lock bit with each frame. If frame is locked, it may not be replaced Kernel, Control structures, I/O buffers
Clock Policy
Additional bit called use bit When a page is first loaded in memory use bit = 1 When the page is referenced use bit = 1 Going clockwise, the first frame encountered with the use bit = 0 is replaced. During the search for replacement, use bit is changed from 1 to 0
Variable-allocation
number of pages allocated to a process varies over the lifetime of the process
From time to time; increase/decrease the resident set size to improve performance
If multiprogramming level is to be reduced, some processes must be suspended Here are some good choices for suspension: Lowest priority process Faulting process (will soon be blocked anyway)
high probability that it does not have its working set in main memory
Last process activated (least likely to have its working set resident) Process with smallest resident set
this process requires the least future effort to reload
Largest process
generates the most free frames making future suspensions unlikely