Computer Systems II Virtual Memory Recap Paging Memory

  • Slides: 24
Download presentation
Computer Systems II Virtual Memory

Computer Systems II Virtual Memory

Recap: Paging • Memory is divided into frames • Process is divided into pages

Recap: Paging • Memory is divided into frames • Process is divided into pages (same size as frames) • Pages are loaded into frames (need not be contiguous) • Page table translates page addresses to frames addresses Process A (1) Process B (0) Process C (0) Process D (0) Process A (2) Process B (1) Process C (1) Process D (1) Process A (3) Process B (2) Process C (2) Process D (2) Process A (4) Process B (3) Process C (3) Process D (3) Process B (4) Process D (4) Process B (5) Process D (5) Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Main Memory

Recap: Page Tables • One page table for each process • Page table used

Recap: Page Tables • One page table for each process • Page table used to translate virtual addresses to physical addresses Process B Process C Process D Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Main Memory Process D A (0) (1) A (1) (2) Process D Process A D (3) (2) A (3) (4) Process D Process B (0) Process B (1) Process B (2) Process B (3) Process B (4) Process B (5) Process C (0) Process C (1) Process C (2) Process C (3) Process D (4) Process D (5)

Recap: Problems that Still Remain • Should all pages be loaded in memory? –

Recap: Problems that Still Remain • Should all pages be loaded in memory? – Limits the number of active processes • Solution: Virtual Memory – Load pages only when needed – Less need to swap blocked processes out to disk

Virtual Memory allows the OS to load pieces only when needed (demand paging)

Virtual Memory allows the OS to load pieces only when needed (demand paging)

Virtual Addressing • Virtual (logical) address is mapped to a real address using a

Virtual Addressing • Virtual (logical) address is mapped to a real address using a page table • Size of physical address limited by memory size • What limits the size of a virtual address? ? Page Number Offset Virtual Address Page table mapping Frame Number Offset Physical Address

Virtual Memory Expands Memory Size • Virtual memory can be larger than physical memory

Virtual Memory Expands Memory Size • Virtual memory can be larger than physical memory – A process can use more memory – Only limited by swap space on disk • Pages not in main memory are stored in a dedicated swap area on the disk Main Memory Disk Pages 0 3 Swap Area 4 6 7 5 2 1

Demand Paging • Only load those pages needed by process • Keep a permanent

Demand Paging • Only load those pages needed by process • Keep a permanent store of pages on disk • Allows more processes to be active • Eliminates needless loading and unloading Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Main Memory Process A (1) Process A (2) Process D (1) Process B (3) Process C (1) Process D (4) Process E (2) Process C (4) Process F (1) Process G (2) Process B (1) Process H (11) Process I (1) Process J (3) Process E (3) Process J (5)

Page Faults • Imagine process A attempting to execute: mov ecx, [ebx] Register ebx

Page Faults • Imagine process A attempting to execute: mov ecx, [ebx] Register ebx contains a virtual address that corresponds to page 3 of process A • What happens? PAGE FAULT Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Main Memory Process A (1) Process A (2) Process D (1) Process B (3) Process C (1) Process D (4) Process E (2) Process C (4) Process F (1) Process G (2) Process B (1) Process H (11) Process I (1) Process J (3) Process E (3) Process J (5)

Page Fault Processing • Occurs when a process accesses a page not in memory

Page Fault Processing • Occurs when a process accesses a page not in memory Initial page fault processing Block current process Memory Full? No Setup I/O HW to read page from disk Start a new ready process Replacement Policy? yes Evict a page from memory When page is loaded (Disk Interrupt) Update page table Mark process ready

Thrashing • Occurs when pages are swapped in and out frequently • In steady

Thrashing • Occurs when pages are swapped in and out frequently • In steady state, memory will be full for ( i=0; i<LIMIT; i++ ) { /* access the first page */ a = array[i]; /* access the second page */ b = array[i+5000]; sum = sum + a + b; } Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Main Memory Process A (2) (1) Process D (2) Process D (1) Process B (3) Process C (1) Process D (4) Process E (2) Process C (4) Process F (1) Process G (2) Process B (1) Process H (11) Process I (1) Process J (3) Process E (3) Process J (5)

Aside: Programming Considerations • Program structure int A[1024]; • Assume – each row is

Aside: Programming Considerations • Program structure int A[1024]; • Assume – each row is stored in one page – Only one page fits in memory • Program 1: ____ page faults for (j = 0; j < 1024; j++) for (i = 0; i < 1024; i++) A[i][j] = 0; • Program 2: ____ page faults for (i = 0; i < 1024; i++) for (j = 0; j < 1024; j++) A[i][j] = 0;

Locality Limits Thrashing • Memory references tend to cluster • Different types of locality:

Locality Limits Thrashing • Memory references tend to cluster • Different types of locality: – Spatial: If we access one memory item, we are likely to access other items close to it (i. e. code and array accesses) – Temporal: Over time, memory tends to be accessed multiple times (i. e. a loop) • OS may also limit the number of active processes to control thrashing

Operating System Policies

Operating System Policies

Operating System Policies • Fetch Policy: When to bring in pages • Replacement Policy:

Operating System Policies • Fetch Policy: When to bring in pages • Replacement Policy: Which pages to replace • Resident Set Policy: How many pages to allow for each process • Load Control: How many active processes to allow

Fetch Policy • Demand paging (most widely used) – Bring in page only when

Fetch Policy • Demand paging (most widely used) – Bring in page only when needed – Flurry of page transfers when process starts • Prepaging – Bring in extra pages on a page fault – Block transfers are more efficient – Ineffective if extra pages are not used

Replacement Policy • When a page fault occurs, a victim page must be evicted

Replacement Policy • When a page fault occurs, a victim page must be evicted from memory to make room for the new one • Goal is to replace the page needed furthest in the future (minimize page faults) • Some pages not subject to replacement – Frames are locked • Policies include: – Optimal: not possible but something to try for – First-in, first-out (FIFO) – Least recently used (LRU)

Optimal • Selects page furthest referenced in the future – gives lower bound on

Optimal • Selects page furthest referenced in the future – gives lower bound on the number of page faults • Three page replacements for example: Page Address Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 2 4 4 4 2 2 2 3 3 3 1 5 5 5 5 F F F

FIFO: First-In First-Out • Replace the page that is the oldest (has been in

FIFO: First-In First-Out • Replace the page that is the oldest (has been in memory longest) • Six page replacements for example: Page Reference Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 5 5 3 3 3 3 2 2 2 5 5 1 1 1 4 4 4 2 F F F

LRU: Least Recently Used • Replace the page that was referenced furthest in the

LRU: Least Recently Used • Replace the page that was referenced furthest in the past • Four page replacements for example: Page Address Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 2 3 3 3 5 5 5 5 1 1 1 4 4 4 2 2 F F

Resident Set Policy • How many frames to give each process? • Tradeoffs are:

Resident Set Policy • How many frames to give each process? • Tradeoffs are: – few pages: load more processes, but more page faults – large number of pages: fewer resident processes, may waste page frames • Number of frames can be – fixed: decide at load time – variable: dynamically adjust the allocation

Variable Frame Allocation • Keep track of the page fault count for each process

Variable Frame Allocation • Keep track of the page fault count for each process – Define upper bound U , lower bound L for page fault rates – Allocate more frames to a process if fault rate > U – Allocate fewer frames to a process if fault rate < L • Load Control: – suspend a process with page fault rate > U, if all frames occupied

Measuring the Memory Usage Virtual memory usage Physical memory usage (“resident set size”) CPU

Measuring the Memory Usage Virtual memory usage Physical memory usage (“resident set size”) CPU time used by this process so far Unix % ps -l F UID PPID PRI 0 115 7264 7262 17 1400 SN 0: 00 -csh 0 115 7290 7264 17 15380 10940 SN 5: 52 emacs 0 115 3283 7264 23 RN 0: 00 ps l Windows VSZ 4716 2864 RSS STAT 812 TIME COMMAND

Recap • Virtual memory can be larger than physical memory – Bring pages in

Recap • Virtual memory can be larger than physical memory – Bring pages in memory as needed – Use disk swap area for pages not in memory • Fetch policy – Demand: bring page in memory only when needed – Prepaging: bring extra pages in on page fault • Replacement policy – FIFO: replace oldest page – LRU: replace page least recently referenced • Resident Set Policy – Adjust page allocation based on fault rate