Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtual Memory. 2 Virtual Memory Manager Provides abstraction of physical memory Creates virtual address space in secondary memory and then “automatically”

Similar presentations


Presentation on theme: "Virtual Memory. 2 Virtual Memory Manager Provides abstraction of physical memory Creates virtual address space in secondary memory and then “automatically”"— Presentation transcript:

1 Virtual Memory

2 2 Virtual Memory Manager Provides abstraction of physical memory Creates virtual address space in secondary memory and then “automatically” determines which part of the address space to load into primary memory at any given time Allows application programmers to think that they have a very large address space in which to write programs

3 3 Virtual Memory Organization Memory Image for p i Secondary Memory Primary Memory

4 4 Locality Programs do not access their address space uniformly –they access the same location over and over Spatial locality: processes tend to access location near to location they just accessed –because of sequential program execution –because data for a function is grouped together Temporal locality: processes tend to access data over and over again –because of program loops –because data is processed over and over again

5 5 Spatial Reference Locality Address space is logically partitioned –Text, data, stack –Initialization, main, error handle Different parts have different reference patterns: Address Space for p i Initialization code (used once) Code for  1 Code for  2 Code for  3 Code for error 1 Code for error 2 Code for error 3 Data & stack 30% 20% 35% 15% <1% Execution time

6 6 Virtual Memory Every process has code and data locality Dynamically load/unload currently-used address space fragments as the process executes Uses dynamic address relocation/binding –Generalization of base-limit registers –Physical address corresponding to a compile- time address is not bound until run time

7 7 Virtual Memory – cont Since binding changes with time, use a dynamic virtual address map,  t Virtual Address Space tt

8 8 Virtual Memory – cont Virtual Address Space for p i Virtual Address Space for p j Virtual Address Space for p k Secondary Memory Complete virtual address space is stored in secondary memory Primary Memory 0 n-1 Physical Address Space Fragments of the virtual address space are dynamically loaded into primary memory at any given time Each address space is fragmented

9 9 Address Translation Virtual memory systems distinguish among symbolic names, virtual address, and physical address spaces Need to map symbolic names to virtual addresses, and then to physical addresses Compiler/assembler and link editor handle mapping from symbolic names in name space to virtual address When program is executed, the virtual addresses are mapped to physical addresses

10 10 Names, Virtual Addresses & Physical Addresses Source Program Absolute Module Name Space P i ’s Virtual Address Space P i ’s Virtual Address Space Executable Image Physical Address Space Physical Address Space  t : Virtual Address Space  Physical Address Space

11 11 Address Formation Translation system creates an address space, but its address are virtual instead of physical A virtual address, x: –Is mapped to physical address y =  t (x) if x is loaded at physical address y –Is mapped to  (the null address) if x is not loaded The map,  t, changes as the process executes -- it is “time varying”  t : Virtual Address  Physical Address  {  }

12 12 Translation Process If  t (k) =  at time t and the process references location k, then –The virtual manager will stop the process –The referenced location is loaded at some location (say m) –The manager changes  t (k) = m –The manager lets the process continue execution Note that the referenced element was determined missing after an instruction started execution – CPU needs to be able to “back out” of an instruction and reexecute instruction after translation mapping

13 13 Size of Blocks of Memory Virtual memory system transfers “blocks” of the address space to/from primary memory Fixed size blocks: System-defined pages are moved back and forth between primary and secondary memory Variable size blocks: Programmer-defined segments – corresponding to logical fragments – are the unit of movement Paging is the commercially dominant form of virtual memory today

14 14 Paging A page is a fixed size, 2 h, block of virtual addresses A page frame is a fixed size, 2 h, block of physical memory (the same size as a page) When a virtual address, x, in page i is referenced by the CPU –If page i is loaded at page frame j, the virtual address is relocated to page frame j –If page is not loaded, the OS interrupts the process and loads the page into a page frame

15 15 Practicality of paging Paging only works because of locality –at any one point in time programs don’t need most of their pages Page fault rates must be very, very low for paging to be practical –like one page fault per 100,000 or more memory references

16 16 Addresses Suppose there are G= 2 g  2 h =2 g+h virtual addresses and H=2 j+h physical addresses assigned to a process –Each page/page frame is 2 h addresses –There are 2 g pages in the virtual address space –2 j page frames are allocated to the process –Rather than map individual addresses  t maps the 2 g pages to the 2 j page frames That is, page_frame j =  t (page i ) Address k in page i corresponds to address k in page_frame j

17 17 Page-Based Address Translation Let N = {d 0, d 1, … d n-1 } be the pages Let M = {b 0, b 1, …, b m-1 } be page frames Virtual address, i, satisfies 0  i<G= 2 g+h Physical address, k = U2 h +V (0  V<G= 2 h ) –U is page frame number –V is the line number within the page –  t :[0:G-1]   {  } –Since every page is size c=2 h page number = U =  i/c  line number = V = i mod c

18 18 Address Translation (cont) Page #Line # Virtual Address g bits h bits tt Missing Page Frame #Line # j bitsh bits Physical Address MAR CPU Memory “page table”

19 19 Paging Algorithms Two basic types of paging algorithms –Static allocation –Dynamic allocation Three basic policies in defining any paging algorithm –Fetch policy – when a page should be loaded –Replacement policy –which page is unloaded –Placement policy – where page should be loaded

20 20 Fetch Policy Determines when a page should be brought into primary memory Usually don’t have prior knowledge about what pages will be needed Majority of paging mechanisms use a demand fetch policy –Page is loaded only when process references it

21 21 Demand Paging Algorithm 1.Page fault occurs 2.Process with missing page is interrupted 3.Memory manager locates the missing page 4.Page frame is unloaded (replacement policy) 5.Page is loaded in the vacated page frame 6.Page table is updated 7.Process is restarted

22 22 Page references Processes continually reference memory –and so generate a stream of page references The page reference stream tells us everything about how a process uses memory –For a given size, we only need to consider the page number –If we have a reference to a page, then immediately following references to the page will never generate a page fault 0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101, 0611, 0102, 0103 0104, 0101, 0610, 0103, 0104, 0101, 0609, 0102, 0105 –Suppose the page size is 100 bytes, what is the page reference stream? We use page reference streams to evaluate paging algorithms

23 23 Modeling Page Behavior Let  = r 1, r 2, r 3, …, r i, … be a page reference stream –r i is the i th page # referenced by the process –The subscript is the virtual time for the process Given a page frame allocation of m, the memory state at time t, S t (m), is set of pages loaded –S t (m) = S t-1 (m)  X t - Y t X t is the set of fetched pages at time t Y t is the set of replaced pages at time t

24 24 More on Demand Paging If r t was loaded at time t-1, S t (m) = S t-1 (m) If r t was not loaded at time t-1 and there were empty page frames –S t (m) = S t-1 (m)  {r t } If r t was not loaded at time t-1 and there were no empty page frames –S t (m) = S t-1 (m)  {r t } - {y} where y is the page frame unloaded

25 25 Replacement Policy When there is no empty page frame in memory, we need to find one to replace –Write it out to the swap area if it has been changed since it was read in from the swap area Dirty bit or modified bit –pages that have been changed are referred to as “dirty” –these pages must be written out to disk because the disk version is out of date »this is called “cleaning” the page Which page to remove from memory to make room for a new page –We need a page replacement algorithm

26 26 Page replacement algorithms The goal of a page replacement algorithm is to produce the fewest page faults We can compare two algorithms –on a range of page reference streams Or we can compare an algorithm to the best possible algorithm We will start by considering static page replacement algorithms

27 27 Static Paging Algorithms A fixed number of page frames is allocated to each process when it is created Paging policy defines how these page frames will be loaded and unloaded Placement policy is fixed –The page frame holding the new page is always the one vacated by the page selected for replacement

28 28 Static Allocation, Demand Paging Number of page frames is static over the life of the process Fetch policy is demand Since S t (m) = S t-1 (m)  {r t } - {y}, the replacement policy must choose y -- which uniquely identifies the paging policy

29 29 Random page replacement Algorithm: replace a page randomly Theory: we cannot predict the future at all Implementation: easy Performance: poor –but it is easy to implement –but the best case, worse case and average case are all the same

30 30 Random Replacement Replaced page, y, is chosen from the m loaded page frames with probability 1/m Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 1 2 213213 213213 210210 310310 310310 312312 012012 032032 032032 062062 462462 452452 752752 203203 2020 2 No knowledge of   doesn’t perform well 13 page faults

31 31 Belady’s Optimal algorithm The one that produces the fewest possible page faults on all page reference sequences Algorithm: replace the page that will not be used for the longest time in the future Problem: it requires knowledge of the future Not realizable in practice –but it is used to measure the effectiveness of realizable algorithms

32 32 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3

33 33 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3 FWD 4 (2) = 1 FWD 4 (0) = 2 FWD 4 (3) = 3

34 34 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 1 0 0 0 2 3 1 FWD 4 (2) = 1 FWD 4 (0) = 2 FWD 4 (3) = 3

35 35 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 1 0 0 0 0 0 2 3 1 1 1

36 36 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 2 1 0 0 0 0 0 3 2 3 1 1 1 1 FWD 7 (2) = 2 FWD 7 (0) = 3 FWD 7 (1) = 1

37 37 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 2 2 2 0 1 0 0 0 0 0 3 3 3 3 2 3 1 1 1 1 1 1 1 FWD 10 (2) =  FWD 10 (3) = 2 FWD 10 (1) = 3

38 38 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 2 2 2 0 0 0 1 0 0 0 0 0 3 3 3 3 3 3 2 3 1 1 1 1 1 1 1 1 1 FWD 13 (0) =  FWD 13 (3) =  FWD 13 (1) = 

39 39 Belady’s Optimal Algorithm Replace page with maximal forward distance: y t = max xeS t-1(m) FWD t (x) Let page reference stream,  = 2031203120316457 Perfect knowledge of   perfect performance Impossible to implement 10 page faults Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 2 2 2 0 0 0 0 4 4 4 1 0 0 0 0 0 3 3 3 3 3 3 6 6 6 7 2 3 1 1 1 1 1 1 1 1 1 1 1 5 5

40 40 Theories of program behavior All replacement algorithms try to predict the future and act like Belady’s optimal algorithm All replacement algorithms have a theory of how program behave –they use it to predict the future, that is, when pages will be referenced –then the replace the page that they think won’t be referenced for the longest time.

41 41 LRU page replacement Least-recently used (LRU) Algorithm: remove the page that hasn’t been referenced for the longest time Theory: the future will be like the past, page accesses tend to be clustered in time Implementation: hard, requires hardware assistance (and then still not easy) Performance: very good, within 30%-40% of optimal

42 42 LRU model of the future

43 43 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3 BKWD 4 (2) = 3 BKWD 4 (0) = 2 BKWD 4 (3) = 1

44 44 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 0 0 0 2 3 3 BKWD 4 (2) = 3 BKWD 4 (0) = 2 BKWD 4 (3) = 1

45 45 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 1 0 0 0 2 2 3 3 3 BKWD 5 (1) = 1 BKWD 5 (0) = 3 BKWD 5 (3) = 2

46 46 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 1 1 0 0 0 2 2 2 3 3 3 0 BKWD 6 (1) = 2 BKWD 6 (2) = 1 BKWD 6 (3) = 3

47 47 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 1 3 3 3 0 0 0 6 6 6 7 1 0 0 0 2 2 2 1 1 1 3 3 3 4 4 4 2 3 3 3 0 0 0 2 2 2 1 1 1 5 5

48 48 Least Recently Used (LRU) Replace page with maximal forward distance: y t = max xeS t-1(m) BKWD t (x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 2 3 2 2 2 2 6 6 6 6 1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 2 3 3 3 3 3 3 3 3 3 3 3 3 5 5 3 1 1 1 1 1 1 1 1 1 1 1 1 7 Backward distance is a good predictor of forward distance -- locality

49 49 LFU page replacement Least-frequently used (LFU) Algorithm: remove the page that hasn’t been used often in the past Theory: an actively used page should have a large reference count Implementation: hard, also requires hardware assistance (and then still not easy) Performance: not very good

50 50 Least Frequently Used (LFU) Replace page with minimum use: y t = min xeS t-1(m) FREQ(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3 FREQ 4 (2) = 1 FREQ 4 (0) = 1 FREQ 4 (3) = 1

51 51 Least Frequently Used (LFU) Replace page with minimum use: y t = min xeS t-1(m) FREQ(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 1 0 0 1 2 3 3 FREQ 4 (2) = 1 FREQ 4 (0) = 1 FREQ 4 (3) = 1

52 52 Least Frequently Used (LFU) Replace page with minimum use: y t = min xeS t-1(m) FREQ(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 1 0 0 1 1 1 2 3 3 3 0 FREQ 6 (2) = 2 FREQ 6 (1) = 1 FREQ 6 (3) = 1

53 53 Least Frequently Used (LFU) Replace page with minimum use: y t = min xeS t-1(m) FREQ(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 2 2 2 1 0 0 1 1 1 2 3 3 3 0 FREQ 7 (2) = ? FREQ 7 (1) = ? FREQ 7 (0) = ?

54 54 FIFO page replacement Algorithm: replace the oldest page Theory: pages are used for a while and then stop being used Implementation: easy Performance: poor –because old pages are often accessed, that is, the theory if FIFO is not correct

55 55 First In First Out (FIFO) Replace page that has been in memory the longest: y t = max xeS t-1(m) AGE(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3

56 56 First In First Out (FIFO) Replace page that has been in memory the longest: y t = max xeS t-1(m) AGE(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 0 0 2 3 AGE 4 (2) = 3 AGE 4 (0) = 2 AGE 4 (3) = 1

57 57 First In First Out (FIFO) Replace page that has been in memory the longest: y t = max xeS t-1(m) AGE(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 0 0 0 2 3 3 AGE 4 (2) = 3 AGE 4 (0) = 2 AGE 4 (3) = 1

58 58 First In First Out (FIFO) Replace page that has been in memory the longest: y t = max xeS t-1(m) AGE(x) Let page reference stream,  = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 02 2 2 1 1 0 0 0 2 3 3 AGE 5 (1) = ? AGE 5 (0) = ? AGE 5 (3) = ?

59 59 Belady’s Anomaly FIFO with m = 3 has 9 faults FIFO with m = 4 has 10 faults Let page reference stream,  = 012301401234 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 3 4 4 4 4 4 4 1 1 1 1 0 0 0 0 0 2 2 2 2 2 2 2 1 1 1 1 1 3 3 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 0 4 4 4 4 3 3 1 1 1 1 1 1 1 0 0 0 0 4 2 2 2 2 2 2 2 1 1 1 1 3 3 3 3 3 3 3 2 2 2

60 60 Belady’s Anomaly The paging algorithm has worse performance when the amount of primary memory allocated to the process increases Problem arises because the set of pages loaded with the smaller memory allocation is not necessarily also loaded with the larger memory allocation

61 61 Avoiding Belady’s Anomaly Inclusion Property –Set of pages loaded with an allocation of m frames is always a subset of the set of pages that has a page frame allocation of m+1 FIFO does not satisfy the inclusion property LRU and LFU do Algorithms that satisfy the inclusion property are called stack algorithms

62 62 Stack Algorithms Some algorithms are well-behaved Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 1 1 2 2 2 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 1 1 2 2 2 3 LRU

63 63 Stack Algorithms Some algorithms are well-behaved Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 1 1 1 1 1 2 2 2 0 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 1 1 1 1 1 2 2 3 3 3 LRU

64 64 Stack Algorithms Some algorithms are well-behaved Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 3 1 1 1 1 0 0 2 2 2 2 1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 0 1 1 1 2 2 2 2 2 3 3 LRU

65 65 Stack Algorithms Some algorithms are well-behaved Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 3 4 1 1 1 1 0 0 0 2 2 2 2 1 1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 4 3 3 3 3 3 LRU

66 66 Stack Algorithms Some algorithms are well-behaved Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 3 4 4 4 2 2 2 1 1 1 1 0 0 0 0 0 0 3 3 2 2 2 2 1 1 1 1 1 1 4 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 0 0 0 0 0 0 4 1 1 1 1 1 1 2 2 2 2 2 4 4 4 4 3 3 3 3 3 3 3 3 3 2 2 2 LRU

67 67 Stack Algorithms Some algorithms are not well-behaved Inclusion Property: Pages loaded at time t with m aren’t loaded at time t with m+1 FIFO Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 3 3 3 4 4 4 4 4 4 1 1 1 1 0 0 0 0 0 2 2 2 2 2 2 2 1 1 1 1 1 3 3 Frame 0 1 2 3 0 1 4 0 1 2 3 4 00 0 0 0 0 0 4 4 4 4 3 3 1 1 1 1 1 1 1 0 0 0 0 4 2 2 2 2 2 2 2 1 1 1 1 3 3 3 3 3 3 3 2 2 2

68 68 Implementation LRU has become preferred algorithm Difficult to implement –Must record when each page was referenced –Difficult to do in hardware Approximate LRU with a reference bit –Periodically reset –Set for a page when it is referenced Dirty bit –pages that have been changed are referred to as “dirty” –these pages must be written out to disk because the disk version is out of date this is called “cleaning” the page

69 69 First LRU approximation When you get a page fault –replace any page whose referenced bit is off –then turn off all the referenced bits Two classes of pages –Pages referenced since the last page fault –Pages not referenced since the last page fault the least recently used page is in this class but you don’t know which one it is A crude approximation of LRU

70 70 Second LRU approximation Algorithm: –Keep a counter for each page –Have a daemon wake up every 500 ms and add one to the counter of each page that has not been referenced zero the counter of pages that have been referenced turn off all referenced bits –When you get a page fault replace the page whose counter is largest Divides pages into 256 classes

71 71 Dynamic Paging Algorithms Static page replacement algorithms assume that a process is allocated to a fixed amount of primary memory But, the amount of physical memory – the number of page frames – varies as the process executes How much memory should be allocated? –Fault rate must be “tolerable” –Will change according to the phase of process Need to define a placement & replacement policy Contemporary models based on working set

72 72 Working Set Intuitively, the working set is the set of pages in the process’s locality –Somewhat imprecise –Time varying –Given k processes in memory, let m i (t) be # of pages frames allocated to p i at time t m i (0) = 0  i=1 k m i (t)  |primary memory| Also have S t (m i (t)) = S t (m i (t-1))  X t - Y t Or, more simply S(m i (t)) = S(m i (t-1))  X t - Y t

73 73 Placed/Replaced Pages S(m i (t)) = S(m i (t-1))  X t - Y t For the missing page –Allocate a new page frame –X t = {r t } in the new page frame How should Y t be defined? Consider a parameter, , called the window size –Determine BKWD t (y) for every y  S(m i (t-1)) –if BKWD t (y)  , unload y and deallocate frame –if BKWD t (y) <  do not disturb y

74 74 Working Set Principle Process p i should only be loaded and active if it can be allocated enough page frames to hold its entire working set The size of the working set is estimated using  –Unfortunately, a “good” value of  depends on the size of the locality –Empirically this works with a fixed 

75 75 Working set algorithm Algorithm –Keep track of the working set of each running process –Only run a process if its entire working set fits in memory – called working set principle

76 76 Working set algorithm example With  =3, there are 16 page faults With  =4, there are 8 – the minimum size since there are 8 distinct pages

77 77 Working set algorithm example – cont. Letting  =8 does not reduce the number of page faults In fact, not all the page frames are used

78 78 Working set algorithm example – cont. Here the page frame allocation changes dynamically increasing and decreasing

79 79 Implementing the Working Set Global LRU will behave similarly to a working set algorithm –Page fault Add a page frame to one process Take away a page frame from another process –Use LRU implementation idea Reference bit for every page frame Cleared periodically, set with each reference Change allocation of some page frame with a clear reference bit –Clock algorithms use this technique by searching for cleared ref bits in a circular fashion

80 80 Performance of Demand Paging Page Fault Rate probability: 0  p  1.0 –if p = 0 no page faults –if p = 1, every reference is a fault Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)

81 81 Demand Paging Performance Example Assume memory access time = 100 nanosecond Assume fault service time = 25 ms = 25,000,000 ns Then EAT = (1 – p) x 100 + p (25,000,000) = 100 + 24,999,900 p (in ns) So, if one out of 1000 accesses causes a page fault, then EAT = 100+24,999,900x0.001=25,099.9 ns ≈ 25 microseconds

82 82 Demand Paging Performance Example So, if one access out of 1000 causes a page fault, the computer would be slowed down by a factor of 250 because of demand paging! Can calculate that if we want less than 10% degradation, need to allow only one access out of 2,500,000 to page fault

83 83 Evaluating paging algorithms Mathematical modeling –powerful where it works –but most real algorithms cannot be analyzed Measurement –implement it on a real system and measure it –extremely expensive Simulation –Test on page reference traces –reasonably efficient –effective

84 84 Performance of paging algorithms

85 85 Thrashing VM allows more processes in memory, so several processes are more likely to be ready to run at the same time If CPU usage is low, it is logical to bring more processes into memory But, low CPU use may to due to too many pages faults because there are too many processes competing for memory Bringing in processes makes it worse, and leads to thrashing

86 86 Thrashing Diagram There are too many processes in memory and no process has enough memory to run. As a result, the page fault is very high and the system spends all of its time handling page fault interrupts.

87 87 Load control Load control: deciding how many processes should be competing for page frames –too many leads to thrashing –too few means that memory is underused Load control determines which processes are running at a point in time –the others have no page frames and cannot run CPU load is a bad load control measure Page fault rate is a good load control measure

88 88 Load control and page replacement

89 89 Two levels of scheduling

90 90 Load control algorithms A load control algorithm measures memory load and swaps processes in and out depending on the current load Load control measures –rotational speed of the clock hand –average time spent in the standby list –page fault rate

91 91 Page fault frequency load control L = mean time between page faults S = mean time to service a page fault Try to keep L = S –if L < S, then swap a process out –if L > S, then swap a process in If L = S, then the paging system can just keep up with the page faults

92 92 Windows NT Paging System Primary Memory Virtual Address Space Supv space User space Paging Disk (Secondary Memory) 2.Lookup (Page i, Addr k) 3.Translate (Page i, Addr k) to (Page Frame j, Addr k) 4.Reference (Page Frame j, Addr k) 1.Reference to Address k in Page i (User space)

93 93 Windows Address Translation Page DirectoryPage TableByte Index Virtual page numberLine number Page Directory Page Tables Target Page Target Byte c b a A C B

94 94 Linux Virtual Address Translation

95 95 Segmentation Unit of memory movement is: –Variably sized –Defined by the programmer Two component addresses, –Seg # is reference to a base location –Offset is offset of target within segment Address translation is more complex than paging

96 96 Segment Address Translation  t : segments x offsets  physical address  {  }  t (i, j) = k; i = segment, j = offset, k = physical address Segment names typically symbolic; bound at runtime  : segments  segment addresses  t (  (segName), j) = k Offset may also not be bound until runtime : offset names  offset addresses So, address map could be as complex as  t (  (segName), (offsetName)) = k Address translation is more complex than paging

97 97 Segment Address Translation Task of designing segmentation system to handle such general address translation is very challenging Each memory reference is theoretically a pair of symbols to be translated when the reference occurs In addition, the mappings are time-varying –The segment could be anywhere in primary and/or secondary memory

98 98 Address Translation  segment #offset tt Missing segment LimitBaseP Limit Relocation + ? To Memory Address Register

99 99 Address Translation – cont. System maintains segment table for each process (which is a segment itself) Table contains a set of entries – called segment descriptors Descriptors contain fields to support relocation; also indicates if not loaded –Base: relocation register for segment –Limit: length of segment –Protection: allowable forms of access

100 100 Implementation Most implementations do not fully implement the address translation model Segmentation requires special hardware –Segment descriptor support –Segment base registers (segment, code, stack) –Translation hardware Some of translation can be static –No dynamic offset name binding –Limited protection

101 101 Multics Designed in late 60’s Old, but still state-of-the-art segmentation Uses linkage segments to support sharing Uses dynamic offset name binding Required sophisticated memory management unit See pp 500-502


Download ppt "Virtual Memory. 2 Virtual Memory Manager Provides abstraction of physical memory Creates virtual address space in secondary memory and then “automatically”"

Similar presentations


Ads by Google