Firstin Firstout n Remove the oldest page n

  • Slides: 42
Download presentation
First-in, First-out n Remove the oldest page n n Old pages may be heavily

First-in, First-out n Remove the oldest page n n Old pages may be heavily used n n Determine the age based on the loading time, not on the time being referenced Process table, scheduler Pure FIFO is rarely used n Improve: second chance algorithm 1

Second Chance Algorithm n n Idea: Looking for an old page not recently referenced

Second Chance Algorithm n n Idea: Looking for an old page not recently referenced Algorithm: Inspect the R bit before removing old pages n 0 A n 0: throw it away 1: clear R bit, move the page to the tail Page loaded first 18 … Most recently loaded page 18 … 20 A A is treated like a newly loaded page 2

Clock Page Algorithm n n Moving pages around in second chance algorithm is inefficient

Clock Page Algorithm n n Moving pages around in second chance algorithm is inefficient Keep all page frames on a circular list n n A hand points to the oldest one A variation of second chance algorithm H A B G C F E D 3

Least Recently Used (LRU) n n n Observation: Pages heavily used in the last

Least Recently Used (LRU) n n n Observation: Pages heavily used in the last few instructions will probably be heavily used again in the next few instructions. Remove the page unused for the longest time Maintaining a list of pages? n n Most recently used page at the front Least recently used page at the rear Update the list on every memory reference Very costly! 4

LRU: Hardware Solution n n A global hardware counter automatically incremented after each instruction

LRU: Hardware Solution n n A global hardware counter automatically incremented after each instruction A 64 -bit local counter per entry in page table n n Local counter global counter when page is referenced When a page fault occurs, examine all the local counters. The page with lowest value is the least recently used one 5

Software LRU n NFU – Not Frequently Used n n Each page has a

Software LRU n NFU – Not Frequently Used n n Each page has a software counter initially zero Add R bit to the counter every clock interrupt, then clear the R bit The page with least counter value is removed Never forget anything, hurt recently used pages n n A page was heavily used before but no longer be referenced. This page cannot be removed due to the high value counter. Improvement: Aging n n Counters are shifted right 1 bit before the R bit is added in The R bit is added to the leftmost 6

Example n The page with lowest counter value is removed Before clock tick 1

Example n The page with lowest counter value is removed Before clock tick 1 Page R bit Counter 0 0 11110000 1 1 01100000 2 1 3 After clock tick 1 Page R bit Counter 01111000 0 0 01111000 10110000 1 0 10110000 00100000 2 0 10010000 0 01000000 3 0 00100000 4 0 10110000 4 0 01011000 5 0 01010001 5 0 00101000 7

Aging Vs. LRU • In aging, no information about which page was referenced last

Aging Vs. LRU • In aging, no information about which page was referenced last (first) during the interval between clock ticks. • Clock interval is 10 seconds. One page is referenced at the 3 rd second; while another at the 7 th second. Aging does not know which page is referenced first. • Limited history kept by aging • 8 -bit counter only keeps the history of 8 clock ticks. • Referenced 9 ticks ago or 1000 ticks ago: no difference 8

Working Set & Thrashing n Working set: the set of pages currently used by

Working Set & Thrashing n Working set: the set of pages currently used by a process n n The entire working set in main memory not many page faults until process moves to next phase Only part of the working set in main memory many page faults, slow Like page table, each process has its own unique working set Trashing: a program causing page faults every few instructions 9

Working Set Model: Avoid Trashing n Demand paging n n n Pages are loaded

Working Set Model: Avoid Trashing n Demand paging n n n Pages are loaded on demand Lots of page faults when process starts Working set model: bring it in memory before running a process n n Keep track of working sets Pre-paging: before run a process, allocate its working set 10

The Function of Working Set n Working set (WS): at time t, the set

The Function of Working Set n Working set (WS): at time t, the set of pages used by the k most recent memory references n n The set changes slowly in time, i. e. , the content of working set is not sensitive to k E. g. , a loop referring 10 pages has 1, 000 instructions for each iteration n Working set has the same 10 pages from k=1, 000 to k=1, 000 if the loop executes 1000 times. w(k, t) k 11

Maintaining WS: A Simple Way n Store page numbers in a shift register of

Maintaining WS: A Simple Way n Store page numbers in a shift register of length k, and with every memory reference, we do Shift the register left one position, and n Insert the most recently referenced page number on the right n The set of k page numbers in the register is the working set. p 2 p 3 … p(k+1) p 1 p 2 … pk n the eldest page Page (k+1) is referenced The most recent page 12

Approximating Working Set n n Maintaining the shift register is expensive Alternative: using the

Approximating Working Set n n Maintaining the shift register is expensive Alternative: using the execution time ( ) instead of references n n E. g. , the set of pages used during the last =100 msec of execution time. Usually > the interval of clock interrupt 13

WS Page Replacement Algorithm • Basic idea: find a page not in WS and

WS Page Replacement Algorithm • Basic idea: find a page not in WS and evict it • The pages in WS are most likely to be used next • On every page fault, the page table is scanned to look for a suitable page to evict • Each entry contains (at least) two items: the time the page was last used and R (referenced) bit. n The hardware sets the R each time a page is used. n Software clears the R bit periodically. 14

Algorithm Details 15

Algorithm Details 15

WSClock Algorithm n n The basic working set page replacement scans the entire page

WSClock Algorithm n n The basic working set page replacement scans the entire page table per page fault, costly! Improvement: clock algorithm + working set information n n A circular list of pages. Initially empty. When a page is loaded, it is added to the list. Pages form a ring. Widely used Based on: time of last use, R and M bits 16

Algorithm Details n A page not in working set n n Clean (no modification)

Algorithm Details n A page not in working set n n Clean (no modification) use it Dirty schedule a write back to disk and keep searching n n It is OS who is responsible for the write back! If the hand comes to its staring point again n n Some write-backs find a clean page (it must be not in working set) No write-back just grab a clean page or current page 17

Example 2204 =200 Current virtual time Time of last use R bit (M bit

Example 2204 =200 Current virtual time Time of last use R bit (M bit not shown) 2014 1 2014 0 1213 0 1980 0 1213 0 1 1980 0 2 2014 1 2014 0 1213 0 2204 1 New page M=1 1213 0 4 1980 0 M=0 3 18

Summary Algorithm Optimal NRU FIFO Comment Not implementable, good as benchmark Very crude Might

Summary Algorithm Optimal NRU FIFO Comment Not implementable, good as benchmark Very crude Might throw out important pages Second chance Clock LRU NFU Aging Working set WSClock Big improvement over FIFO Realistic Excellent, but difficult to implement exactly Fairly crude approximation to LRU Efficient algorithm approximates LRU well Somewhat expensive to implement Good efficient algorithm 19

Maintaining WS: A Simple Way n Store page numbers in a shift register of

Maintaining WS: A Simple Way n Store page numbers in a shift register of length k, and with every memory reference, we do Shift the register left one position, and n Insert the most recently referenced page number on the right n The set of k page numbers in the register is the working set. p 2 p 3 … p(k+1) p 1 p 2 … pk n the eldest page Page (k+1) is referenced The most recent page 20

Approximating Working Set n n Maintaining the shift register is expensive Alternative: using the

Approximating Working Set n n Maintaining the shift register is expensive Alternative: using the execution time ( ) instead of references n n E. g. , the set of pages used during the last =100 msec of execution time. Usually > the interval of clock interrupt 21

WS Page Replacement Algorithm • Basic idea: find a page not in WS and

WS Page Replacement Algorithm • Basic idea: find a page not in WS and evict it • The pages in WS are most likely to be used next • Pre-requisites • Each entry contains (at least) two items: the time the page was last used and R (referenced) bit. n The hardware sets the R each time a page is used. n Software clears the R bit after every clock interrupt. 22

Algorithm Details 23

Algorithm Details 23

WSClock Algorithm n n The basic working set page replacement scans the entire page

WSClock Algorithm n n The basic working set page replacement scans the entire page table per page fault, costly! Improvement: clock algorithm + working set information n n A circular list of pages. Initially empty. When a page is loaded, it is added to the list. Pages form a ring. Widely used Based on: time of last use, R and M bits 24

Algorithm Details n When page fault occurs, check the page pointed by the clock-hand

Algorithm Details n When page fault occurs, check the page pointed by the clock-hand n n If it is clean and not in working set, Get it! If it is dirty, schedule a write back to disk and move the hand one page ahead n n It is OS who is responsible for the write back, and OS will clear the M bit once that page is written back. If the hand comes to its staring point again n n Some write-backs find a clean page (it must be not in working set) No write-back just grab a clean page or current page 25

Example 2204 =200 Current virtual time Time of last use R bit (M bit

Example 2204 =200 Current virtual time Time of last use R bit (M bit not shown) 2014 1 2014 0 1213 0 1980 0 1213 0 1 1980 0 2 2014 1 2014 0 1213 0 2204 1 New page M=1 1213 0 4 1980 0 M=0 3 26

Summary Algorithm Optimal NRU FIFO Comment Not implementable, good as benchmark Very crude Might

Summary Algorithm Optimal NRU FIFO Comment Not implementable, good as benchmark Very crude Might throw out important pages Second chance Clock LRU NFU Aging Working set WSClock Big improvement over FIFO Realistic Excellent, but difficult to implement exactly Fairly crude approximation to LRU Efficient algorithm approximates LRU well Somewhat expensive to implement Good efficient algorithm 27

Outline n n n n Basic memory management Swapping Virtual memory Page replacement algorithms

Outline n n n n Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation 28

Motivation n For the preceding algorithms like LRU, WSClock n Based on the empirical

Motivation n For the preceding algorithms like LRU, WSClock n Based on the empirical studies n n E. g. , more page frames are better. No theoretical analysis n n Given the number of page frames, how many page faults are expected? To reduce the page fault occurrence to certain level, how many page frames are required? 29

More Page Frames Fewer Page Faults? n Belady’s anomaly Not always the case. n

More Page Frames Fewer Page Faults? n Belady’s anomaly Not always the case. n n n 5 virtual pages and 3/4 page frames Page reference string: 0 1 2 3 0 1 4 0 1 2 3 4 FIFO is used Youngest 0 1 2 3 0 1 4 0 1 2 3 0 1 2 0 1 P P P Oldest P Youngest Oldest 0 1 2 0 1 0 3 3 2 1 0 P P 3 4 0 3 2 P 1 0 3 P 4 4 4 2 3 3 1 1 1 4 2 2 0 0 0 1 4 4 P P P 9 page faults 0 3 2 1 0 1 3 2 1 0 4 4 3 2 1 P 10 page faults 0 0 4 3 2 P 1 1 0 4 3 P 2 2 1 0 4 P 3 3 2 1 0 P 4 4 3 2 1 P 30

Model of Paging System n Three elements for the system n n Page reference

Model of Paging System n Three elements for the system n n Page reference string Page replacement algorithm #page frames (size of main memory): m M: the array keeping track of the state of memory. n n |M|=n, the number of virtual pages |top part|=m, n n n recording the pages in main memory Empty entry free page frame |bottom part|=n-m, storing the pages have been referenced but have been paged out. 31

How M Works? n n Initially empty When a page is referenced, n If

How M Works? n n Initially empty When a page is referenced, n If it is not in memory (i. e. , not in the top part of M), page fault occurs. n n n If empty entry in the top part exists, load it, or A page is moved from the top part to the bottom part. Pages in both parts may be separately rearranged. 32

An Example With LRU Algorithm n n When referenced, move to the top entry

An Example With LRU Algorithm n n When referenced, move to the top entry in M. All pages above the just referenced page move down one position n Reference string Top part Keep the most recently referenced page at top 0 2 1 3 5 4 6 3 7 4 7 3 3 5 5 3 1 1 1 7 1 3 4 1 0 2 1 3 5 4 6 3 7 4 7 7 3 3 5 3 3 3 1 7 1 3 4 0 2 1 3 5 4 6 3 3 4 4 7 7 7 5 5 5 3 3 7 1 3 0 2 1 3 5 4 6 6 4 4 4 7 7 7 5 5 5 7 7 0 2 1 1 5 5 5 6 6 6 4 4 4 5 5 Bottom part Page faults 0 2 2 1 1 1 1 6 6 6 6 0 0 2 2 2 2 0 0 0 0 P P P 33

Stack Algorithms n The algorithms that have the property: M(m, r) M(m+1, r), where

Stack Algorithms n The algorithms that have the property: M(m, r) M(m+1, r), where m is the number of page frames and r is an index into the reference string. n n n M(m, r) is a set of pages in the top part of M for index r. m is the size of top part. E. g. , M(4, 1) If increase main memory size by one page frame and re-execute the process, at every point during the execution, all the pages that were present in memory in the first run are also present in the second run Stack algorithms do not suffer from Belady’s anomaly n LRU is but FIFO is not. 34

An Observation Reference string LRU 0 2 1 0 2 0 M(4, r) M(5,

An Observation Reference string LRU 0 2 1 0 2 0 M(4, r) M(5, r) Youngest FIFO Oldest Youngest Oldest 3 3 1 2 0 5 5 3 1 2 0 4 4 5 3 1 2 0 0 1 2 0 1 0 P P P 0 1 0 6 6 4 5 3 1 2 0 3 3 6 4 5 1 2 0 3 3 2 1 P 2 2 1 0 7 7 3 6 4 5 1 2 0 0 0 3 2 P 3 3 2 1 0 P P 4 4 7 3 6 5 1 2 0 1 1 0 3 P 0 3 2 1 0 7 7 4 3 6 5 1 2 0 4 4 1 0 P 1 3 2 1 0 3 3 7 4 6 5 1 2 0 0 4 1 0 4 4 3 2 1 P 3 3 7 4 6 5 1 2 0 1 4 1 0 0 0 4 3 2 P 5 5 3 7 4 6 1 2 0 2 2 4 1 P 5 5 3 7 4 6 1 2 0 3 3 5 7 4 6 1 2 0 3 3 2 4 P 1 1 0 4 3 P 4 3 2 4 2 2 1 0 4 P 1 1 3 5 7 4 6 2 0 9 3 3 2 1 0 P 1 1 3 5 7 4 6 2 0 7 7 1 3 5 4 6 2 0 1 1 7 3 5 4 6 2 0 3 3 1 7 5 4 6 2 0 P 4 4 3 1 7 5 6 2 0 1 1 4 3 7 5 6 2 0 page faults 4 4 3 2 1 35 P 10 page faults

Outline n n n n Basic memory management Swapping Virtual memory Page replacement algorithms

Outline n n n n Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation 36

Allocating Memory for Processes Process A has a page fault… A 0 A 1

Allocating Memory for Processes Process A has a page fault… A 0 A 1 A 2 A 3 A 4 A 5 B 0 B 1 B 2 B 3 B 4 B 5 B 6 C 1 C 2 C 3 Age 10 7 5 4 6 3 9 4 6 2 5 6 12 3 5 6 Local page replacement Local lowest Global lowest A 0 A 1 A 2 A 3 A 6 A 5 B 0 B 1 B 2 B 3 B 4 B 5 B 6 C 1 C 2 C 3 Global page replacement A 0 A 1 A 2 A 3 A 6 A 5 B 0 B 1 B 2 A 6 B 4 B 5 B 6 C 1 C 2 C 3 37

Local/Global Page Replacement n Local page replacement: every process with a fixed fraction of

Local/Global Page Replacement n Local page replacement: every process with a fixed fraction of memory n n n Thrashing even if plenty of free page frames are available. Waste memory if working set shrinks Global page replacement: dynamically allocate page frames for each process n Generally works better. But how to determine how many page frames to assign to each process? 38

Assign Page Frames to Processes n Allocate each process an equal share n n

Assign Page Frames to Processes n Allocate each process an equal share n n n 1000 frames and 10 processes. Each process gets 100 frames. Assign 100 frames to a 10 -page process! Dynamically adjust #frames n n Based on program size Allocate based on page fault frequency (PFF) A Page faults/sec B #frames Assigned 39

Page Size n Arguments for small page size n Internal fragmentation: on average, half

Page Size n Arguments for small page size n Internal fragmentation: on average, half of the final page for a process is empty and wasted n n n p: page size, thus p/2 memory wasted for each segment The size of process could be smaller than one page Arguments for large page size n Small pages large page table and overhead n s: size of process, e: size of page entry. The over head of page table entries is: s*e/p Total overhead = s*e / p + p / 2 Solution: p = sqrt(2 * s* e) 40

Shared Pages for Programs n Processes sharing the same program should share the pages

Shared Pages for Programs n Processes sharing the same program should share the pages n n n Avoid two copies of the same page Keep the share pages when a process swaps out or terminates. Identify the share pages n Costly if search all the page tables. 41

Cleaning Dirty Pages n Writing back dirty pages when free pages are needed is

Cleaning Dirty Pages n Writing back dirty pages when free pages are needed is costly! n n n Processes are blocked and have to wait Overhead of process switching Keep enough clean pages for paging n n Background paging daemon: evicting pages Keep page content, can be reused 42