Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Virtual Memory

Similar presentations


Presentation on theme: "Chapter 9: Virtual Memory"— Presentation transcript:

1 Chapter 9: Virtual Memory
Joe McCarthy CSS 430: Operating Systems - Virtual Memory

2 Chapter 9: Virtual Memory
Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel Memory Other Considerations Operating-System Examples Material derived, in part, from Operating Systems Concepts with Java, 8th Ed. © 2009 Silberschatz, Galvin & Gagne CSS 430: Operating Systems - Virtual Memory

3 CSS 430: Operating Systems - Virtual Memory

4 CSS 430: Operating Systems - Virtual Memory
Background Virtual memory – separation of logical memory from physical memory Only part of the program needs to be in memory for execution Logical address space vs. physical address space? CSS 430: Operating Systems - Virtual Memory

5 CSS 430: Operating Systems - Virtual Memory
Background Virtual memory – separation of logical memory from physical memory Only part of the program needs to be in memory for execution Logical address space >> physical address space Degree of multiprogramming? CSS 430: Operating Systems - Virtual Memory

6 CSS 430: Operating Systems - Virtual Memory
Background Virtual memory – separation of logical memory from physical memory Only part of the program needs to be in memory for execution Logical address space >> physical address space Higher degree of multiprogramming CSS 430: Operating Systems - Virtual Memory

7 CSS 430: Operating Systems - Virtual Memory
Swapping vs. Paging Swapping Paging CSS 430: Operating Systems - Virtual Memory

8 Resident & Non-resident Pages
A, C, F Non-resident: B, D, E, G, H CSS 430: Operating Systems - Virtual Memory

9 CSS 430: Operating Systems - Virtual Memory
Demand Paging Bring a page into memory only when it is needed Alternative: anticipatory or pre-paging When page is needed but not [yet] in memory page fault not-in-memory  bring into memory Swapper that deals with pages is a pager Advantages of demand vs. anticipatory paging? CSS 430: Operating Systems - Virtual Memory

10 CSS 430: Operating Systems - Virtual Memory
Demand Paging Bring a page into memory only when it is needed Alternative: anticipatory or pre-paging When page is needed but not [yet] in memory page fault not-in-memory  bring into memory Swapper that deals with pages is a pager Advantages of demand vs. anticipatory paging: Less I/O needed Less memory needed Faster response More users / programs CSS 430: Operating Systems - Virtual Memory

11 CSS 430: Operating Systems - Virtual Memory
Handling a Page Fault Page fault: Reference to non-resident page [Page table has entries for all pages, but all pages may not be resident] Reference to non-resident page Trap to OS; look at PCB: If invalid reference  abort Otherwise, bring page into memory Find empty frame Swap page into frame Update tables (page table, PCB) Set valid-invalid bit = v Restart the instruction that caused the page fault CSS 430: Operating Systems - Virtual Memory

12 CSS 430: Operating Systems - Virtual Memory
Copy on Write Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory Shared page is copied only if either process modifies it COW allows more efficient process creation as only modified pages are copied CSS 430: Operating Systems - Virtual Memory

13 What if there is no free frame?
Page fault: Reference to non-resident page Reference to non-resident page Trap to OS; look at PCB: If invalid reference  abort Otherwise, bring page into memory Find empty frame Swap page into frame Update tables (page table, PCB) Set valid-invalid bit = v Restart the instruction that caused the page fault CSS 430: Operating Systems - Virtual Memory

14 What if there is no free frame?
Page fault: Reference to non-resident page Reference to non-resident page Trap to OS; look at PCB: If invalid reference  abort Otherwise, bring page into memory Find empty frame (or select victim frame) Swap page into frame (possibly swap victim out) Update tables (page table, PCB) Set valid-invalid bit = v Restart the instruction that caused the page fault CSS 430: Operating Systems - Virtual Memory

15 CSS 430: Operating Systems - Virtual Memory
Effective Access Time Page Fault Rate 0  p  1.0 Effective Access Time EAT = CSS 430: Operating Systems - Virtual Memory

16 CSS 430: Operating Systems - Virtual Memory
Effective Access Time Page Fault Rate 0  p  1.0 Effective Access Time EAT = (1 – p) x memory access time + p (page fault overhead + swap page out + swap page in + restart overhead) CSS 430: Operating Systems - Virtual Memory

17 Effective Access Time Example
Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 – p) * 200 ns + p (8 ms) = (1 – p) * 200 ns + p * 8,000,000 ns = p * 7,999,800 ns If 1 access out of 1,000 (0.1%, p = 0.001) causes a page fault, then EAT = 8.2 microseconds This is a slowdown by a factor of 40 (!) CSS 430: Operating Systems - Virtual Memory

18 Page Fault rates & Effective Access Times
CSS 430: Operating Systems - Virtual Memory

19 CSS 430: Operating Systems - Virtual Memory
Page Replacement Prevent over-allocation of memory by modifying page-fault service routine to include page replacement Use a modify (“dirty”) bit to reduce overhead of page transfers – only modified pages are written to disk Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory CSS 430: Operating Systems - Virtual Memory

20 Need For Page Replacement
CSS 430: Operating Systems - Virtual Memory

21 CSS 430: Operating Systems - Virtual Memory
Page Replacement CSS 430: Operating Systems - Virtual Memory

22 Page Replacement Algorithms
Goal: low page-fault rate Evaluation? CSS 430: Operating Systems - Virtual Memory

23 Page Replacement Algorithms
Goal: low page-fault rate Evaluation: simulate algorithm using a particular string of memory references (reference string) and computing the number of page faults on that string Example reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 CSS 430: Operating Systems - Virtual Memory

24 CSS 430: Operating Systems - Virtual Memory
FIFO Page Replacement Replace frame which has been in memory for longest time CSS 430: Operating Systems - Virtual Memory

25 CSS 430: Operating Systems - Virtual Memory
FIFO Page Replacement Replace frame which has been in memory for longest time CSS 430: Operating Systems - Virtual Memory

26 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames 4 frames 1 2 3 1 2 3 4 CSS 430: Operating Systems - Virtual Memory

27 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames 4 frames 1 2 3 4 5 9 page faults 1 2 3 4 CSS 430: Operating Systems - Virtual Memory

28 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames 4 frames 1 2 3 4 5 9 page faults 1 2 3 5 4 10 page faults CSS 430: Operating Systems - Virtual Memory

29 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames 4 frames Belady’s Anomaly: more frames  more page faults 1 2 3 4 5 9 page faults 1 2 3 5 4 10 page faults CSS 430: Operating Systems - Virtual Memory

30 Optimal Page Replacement
Replace frame which will not be used for longest time CSS 430: Operating Systems - Virtual Memory

31 Optimal Page Replacement
Replace frame which will not be used for longest time CSS 430: Operating Systems - Virtual Memory

32 Optimal Page Replacement
Replace frame which will not be used for longest time Is this knowable? CSS 430: Operating Systems - Virtual Memory

33 Optimal Page Replacement
Replace frame which will not be used for longest time Is this knowable? Is this useful? CSS 430: Operating Systems - Virtual Memory

34 CSS 430: Operating Systems - Virtual Memory
LRU Page Replacement Replace frame which has not been used for longest time CSS 430: Operating Systems - Virtual Memory

35 CSS 430: Operating Systems - Virtual Memory
LRU Page Replacement Replace frame which has not been used for longest time CSS 430: Operating Systems - Virtual Memory

36 CSS 430: Operating Systems - Virtual Memory
LRU Implementations Counter implementation Every page table entry has a counter When page is referenced, counter = current time When a page needs to be replaced, look at the counters to determine which is oldest Advantages / disadvantages? CSS 430: Operating Systems - Virtual Memory

37 CSS 430: Operating Systems - Virtual Memory
LRU Implementations Stack implementation Maintain a stack of page numbers When page is referenced, move it to the top When page needs to be replaced, select bottom Stack: doubly linked list Advantages / disadvantages? CSS 430: Operating Systems - Virtual Memory

38 CSS 430: Operating Systems - Virtual Memory
LRU Approximations Reference bit Associated with each page table entry When page is referenced, set ref bit = 1 Replace a page with ref bit = 0 (if it exists) pages with ref bit = 0 less recently used than pages with ref bit = 1 Second chance algorithm aka “Clock” replacement For each page, If ref bit = 0, replace this page, stop Else set ref bit = 0 (for next round), continue CSS 430: Operating Systems - Virtual Memory

39 Second View of Second Chance
Find a frame for page 727 Operating Systems Internals & Design Principles, 7th Edition by William Stallings CSS 430: Operating Systems - Virtual Memory

40 Second View of Second Chance
Find a frame for page 727 Operating Systems Internals & Design Principles, 7th Edition by William Stallings CSS 430: Operating Systems - Virtual Memory

41 CSS 430: Operating Systems - Virtual Memory
Counting Algorithms Keep a counter of the number of references that have been made to each page CSS 430: Operating Systems - Virtual Memory

42 CSS 430: Operating Systems - Virtual Memory
Counting Algorithms Keep a counter of the number of references that have been made to each page Least Frequently Used (LFU) Algorithm: Replace page with smallest count Most Frequently Used (MFU) Algorithm: Replace page with largest count Idea: the pages with the smallest counts were probably just brought in and have yet to be used CSS 430: Operating Systems - Virtual Memory

43 CSS 430: Operating Systems - Virtual Memory
Allocation of Frames Each process needs a minimum number of pages Example: IBM 370 May require 6 pages to handle a single SS MOVE instruction Instruction is 6 bytes, might span 2 pages from block might span 2 pages to block might span 2 pages Two major allocation schemes fixed allocation priority allocation CSS 430: Operating Systems - Virtual Memory

44 CSS 430: Operating Systems - Virtual Memory
Fixed Allocation CSS 430: Operating Systems - Virtual Memory

45 CSS 430: Operating Systems - Virtual Memory
Fixed Allocation Equal allocation: all processes get the same number of frames E.g., if there are 100 frames and 5 processes, give each process 20 frames. Proportional allocation: differentially allocate according to the size of process (in pages) CSS 430: Operating Systems - Virtual Memory

46 CSS 430: Operating Systems - Virtual Memory
Fixed Allocation Equal allocation: all processes get the same number of frames E.g., if there are 100 frames and 5 processes, give each process 20 frames. Proportional allocation: differentially allocate according to the size of process (in pages) CSS 430: Operating Systems - Virtual Memory

47 CSS 430: Operating Systems - Virtual Memory
Priority Allocation Proportional allocation scheme using priorities rather than size If process Pi generates a page fault, select for replacement one of its frames select for replacement a frame from a process with lower priority number CSS 430: Operating Systems - Virtual Memory

48 Global vs. Local Allocation
Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another Priority allocation must use global replacement Local replacement – each process selects from only its own set of allocated frames CSS 430: Operating Systems - Virtual Memory

49 Sufficient Allocation
If a process is not allocated enough pages, the page-fault rate is very high. This leads to: ? CPU utilization CSS 430: Operating Systems - Virtual Memory

50 Sufficient Allocation
If a process is not allocated enough pages, the page-fault rate is very high. This leads to: low CPU utilization CSS 430: Operating Systems - Virtual Memory

51 Sufficient Allocation
If a process is not allocated enough pages, the page-fault rate is very high. This leads to: low CPU utilization OS decides it needs to increase the degree of multiprogramming CSS 430: Operating Systems - Virtual Memory

52 Sufficient Allocation
If a process is not allocated enough pages, the page-fault rate is very high. This leads to: low CPU utilization OS decides it needs to increase the degree of multiprogramming another process added to the system CSS 430: Operating Systems - Virtual Memory

53 CSS 430: Operating Systems - Virtual Memory
Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: low CPU utilization OS decides it needs to increase the degree of multiprogramming another process added to the system Thrashing  more time spent paging than executing user processes CSS 430: Operating Systems - Virtual Memory

54 Memory Reference Patterns
CSS 430: Operating Systems - Virtual Memory

55 Demand Paging & Thrashing
Why does demand paging work? Locality model Memory references tend to cluster together Process migrates from one locality to another Localities may overlap Why does thrashing occur? size of localities > total memory size allocated CSS 430: Operating Systems - Virtual Memory

56 CSS 430: Operating Systems - Virtual Memory
Working-Set Model   working-set window  sequence of page references (e.g., 10,000) WSSi (working set of Process Pi) = total number of pages referenced in the most recent  (varies in time) if  too small  will not encompass entire locality if  too large  will encompass several localities if  =   will encompass entire program D =  WSSi  total demand frames if D > m  Thrashing ( suspend a process) CSS 430: Operating Systems - Virtual Memory

57 CSS 430: Operating Systems - Virtual Memory
Working-Set Model   working-set window  sequence of page references (e.g., 10,000) WSSi (working set of Process Pi) = total number of pages referenced in the most recent  (varies in time) if  too small  will not encompass entire locality if  too large  will encompass several localities if  =   will encompass entire program D =  WSSi  total demand frames if D > m  Thrashing ( suspend a process) CSS 430: Operating Systems - Virtual Memory

58 Page-Fault Frequency Scheme
Establish “acceptable” page-fault rate If actual rate too low, process loses frame If actual rate too high, process gains frame CSS 430: Operating Systems - Virtual Memory

59 CSS 430: Operating Systems - Virtual Memory
End of Chapter 9 CSS 430: Operating Systems - Virtual Memory

60 Keeping Track of the Working Set
Approximate with interval timer + a reference bit Example:  = 10,000 Timer interrupts after every 5000 time units Keep in memory 2 bits for each page Whenever a timer interrupts, copy & set the values of all reference bits to 0 If one of the bits in memory = 1  page in working set Why is this not completely accurate? Improvement = 10 bits and interrupt every 1000 time units CSS 430: Operating Systems - Virtual Memory

61 Working Sets and Page Fault Rates
CSS 430: Operating Systems - Virtual Memory

62 CSS 430: Operating Systems - Virtual Memory
Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than read() write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared. CSS 430: Operating Systems - Virtual Memory

63 CSS 430: Operating Systems - Virtual Memory
Memory Mapped Files CSS 430: Operating Systems - Virtual Memory

64 Memory-Mapped Shared Memory in Windows
CSS 430: Operating Systems - Virtual Memory

65 Memory-Mapped Files in Java
CSS 430: Operating Systems - Virtual Memory

66 Allocating Kernel Memory
Treated differently from user memory Often allocated from a free-memory pool Kernel requests memory for various structures, many of which have fixed sizes Some kernel memory needs to be contiguous All kernel memory accesses needs to be fast CSS 430: Operating Systems - Virtual Memory

67 CSS 430: Operating Systems - Virtual Memory
Buddy System Allocates memory from fixed-size segment consisting of physically-contiguous pages Power-of-2 allocator Satisfies requests in units sized as power of 2 (rounded up) When smaller allocation needed than is available, split a chunk into two buddies of next-lower power of 2 Continue as needed Coalescing: Combine adjacent chunks when freed CSS 430: Operating Systems - Virtual Memory

68 CSS 430: Operating Systems - Virtual Memory
Buddy System Operating Systems Internals & Design Principles, 7th Edition by William Stallings CSS 430: Operating Systems - Virtual Memory

69 CSS 430: Operating Systems - Virtual Memory
Tree Representation Operating Systems Internals & Design Principles, 7th Edition by William Stallings CSS 430: Operating Systems - Virtual Memory

70 CSS 430: Operating Systems - Virtual Memory
Slab Allocation CSS 430: Operating Systems - Virtual Memory

71 CSS 430: Operating Systems - Virtual Memory
Slab Allocator Alternate strategy Slab is one or more physically contiguous pages Cache consists of one or more slabs Single cache for each unique kernel data structure Each cache filled with objects – instantiations of the data structure When cache created, filled with objects marked as free When structures stored, objects marked as used If slab is full of used objects, next object allocated from empty slab If no empty slabs, new slab allocated Benefits? CSS 430: Operating Systems - Virtual Memory

72 CSS 430: Operating Systems - Virtual Memory
Slab Allocator Alternate strategy Slab is one or more physically contiguous pages Cache consists of one or more slabs Single cache for each unique kernel data structure Each cache filled with objects – instantiations of the data structure When cache created, filled with objects marked as free When structures stored, objects marked as used If slab is full of used objects, next object allocated from empty slab If no empty slabs, new slab allocated Benefits include no fragmentation, fast memory request satisfaction CSS 430: Operating Systems - Virtual Memory

73 CSS 430: Operating Systems - Virtual Memory
Other Issues Pre-paging: [when] is pre-paging worthwhile? Page size What factors influence page size? What factors are influenced by page size? CSS 430: Operating Systems - Virtual Memory

74 Other Issues -- Prepaging
To reduce the large number of page faults that occurs at process startup Prepage all or some of the pages a process will need, before they are referenced But if prepaged pages are unused, I/O and memory was wasted Assume s pages are prepaged and α of the pages is used Is cost of s * α save pages faults > or < than the cost of prepaging s * (1- α) unnecessary pages? α near zero  prepaging loses CSS 430: Operating Systems - Virtual Memory

75 Other Issues – Page Size
Page size selection must take into consideration: fragmentation table size I/O overhead locality CSS 430: Operating Systems - Virtual Memory

76 Other Issues – TLB Reach
TLB Reach - The amount of memory accessible from the TLB TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB Otherwise there is a high degree of page faults Increase the Page Size This may lead to an increase in fragmentation as not all applications require a large page size Provide Multiple Page Sizes This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation CSS 430: Operating Systems - Virtual Memory

77 Other Issues – Program Structure
Int[128,128] data; Each row is stored in one page Program 1 for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; Page faults: Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; CSS 430: Operating Systems - Virtual Memory

78 Other Issues – Program Structure
Int[128,128] data; Each row is stored in one page Program 1 for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; Page faults: 128 x 128 = 16,384 Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; Page faults: 1 x 128 = 128 CSS 430: Operating Systems - Virtual Memory

79 Operating System Examples
Windows XP Solaris CSS 430: Operating Systems - Virtual Memory

80 CSS 430: Operating Systems - Virtual Memory
Windows XP Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page. Processes are assigned working set minimum and working set maximum (typically 50 and 345 pages) Working set minimum is the minimum number of pages the process is guaranteed to have in memory. A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory (clock or FIFO algorithm) Working set trimming removes pages from processes that have pages in excess of their working set minimum. CSS 430: Operating Systems - Virtual Memory

81 CSS 430: Operating Systems - Virtual Memory
Solaris Maintains free list with thresholds Lotsfree: start paging (1/64 size of phys. memory) Desfree: increase paging Minfree: start swapping Pageout process 2-handed clock algorithm Handspread Scanrate: slowscan  fastscan Operating Systems Internals & Design Principles, 7th Edition by William Stallings CSS 430: Operating Systems - Virtual Memory


Download ppt "Chapter 9: Virtual Memory"

Similar presentations


Ads by Google