31
Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution - some can be on disk Disk address can be stored in place of frame number A valid–invalid bit is associated with each page table entry (1 in-memory, 0 not-in-memory) During address translation, if valid–invalid bit in page table entry is 0 page fault bring in to memory

Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Embed Size (px)

Citation preview

Page 1: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.1Operating System Concepts

Virtual Memory

Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for

execution - some can be on disk Disk address can be stored in place of frame number

A valid–invalid bit is associated with each page table entry (1 in-memory, 0 not-in-memory) During address translation, if valid–invalid bit in page table

entry is 0 page fault bring in to memory

Page 2: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.2Operating System Concepts

Page Table When Some Pages Are Not in Main Memory

Sum logical address space can therefore be much larger than physical address space. More processes

Page 3: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.3Operating System Concepts

Page Faults Bringing a Page into Memory

Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction

Page 4: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.4Operating System Concepts

Need For Page Replacement

Page 5: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.5Operating System Concepts

What happens if there is no free frame?

Page replacement – find some page in memory, but not really in use, swap it out.

Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk.

Page 6: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.6Operating System Concepts

Basic Page Replacement

1. Find the location of the desired page on disk.

2. Find a free frame:

1. If there is a free frame, use it.

2. If there is no free frame, use a page replacement algorithm to select a victim frame.

3. If victim is dirty, write it out to disk

3. Read the desired page into the (newly) free frame. Update the page and frame tables.

4. Restart the process

Page 7: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.7Operating System Concepts

Page Replacement Policies

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 - only for crucial OS processes

Local replacement – each process selects from only its own set of allocated frames - the normal case

I/O interlock Pages that are used for copying a file from a device must be

locked

Page 8: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.8Operating System Concepts

Thrashing

VM works because of locality Process migrates from

one locality to another. Localities may overlap

What happens if size of localities > total memory size

Page 9: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.9Operating System Concepts

Thrashing

If a process does not have “enough” pages for its locality, the page-fault rate is very high. Thrashing a process is busy swapping pages in and out.

Thrashing may lead to: low CPU utilization. operating system thinks that it needs to increase the degree

of multiprogramming. another process added to the system

It is important to allocate enough frames to each process If that’s not possible, the process must be swapped out

Page 10: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.10Operating System Concepts

Allocation of Frames

Each process needs minimum number of frames Example: IBM 370 – 6 pages to handle SS MOVE

instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle from. 2 pages to handle to.

Two major allocation schemes. fixed allocation priority allocation

Page 11: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.11Operating System Concepts

Fixed Allocation

Equal allocation – e.g., if 100 frames and 5 processes, give each 20 frames.

Proportional allocation – Allocate according to the size of process.

mSs

pa

m

sS

ps

iii

i

ii

×==

=∑=

=

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

≈×=

≈×=

===

a

a

ssm

i

Page 12: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.12Operating System Concepts

Priority Allocation

Use a 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.

Page 13: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.13Operating System Concepts

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.

Page 14: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.14Operating System Concepts

Initial Load

Load pages predicted to be needed (compiler flags)

or Load no pages

Allows for more efficient process creation

Page 15: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.15Operating System Concepts

Performance of Demand Paging

Page Fault Rate 0 p 1.0 (hopefully low, e.g. 5%) 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)

Values Assume memory access roughly 1 (with TLB hits) Page fault overhead is small Swap is about 10000 Restart is small

Page 16: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.16Operating System Concepts

Page Replacement Algorithms

Want lowest page-fault rate (disk is slow)

Evaluate algorithm by running it on a particular string of page references (reference string) and computing the number of page faults on that string. A page fault means a disk access (or two), and that’s where

the time goes.

Assume no initial load, local replacement, no I/O (no interlock issues)

Page 17: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.17Operating System Concepts

Optimal Algorithm

Replace page that will not be used for longest period of time in future.

3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (9)

4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (6) But the future is unknown (like SJF scheduling)

Page 18: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.18Operating System Concepts

First-In-First-Out (FIFO) Algorithm

3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (15)

Page 19: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.19Operating System Concepts

FIFO Illustrating Belady’s Anamoly

4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (10) 3 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (9)

Belady’s Anomaly - more frames does not imply less page faults

Page 20: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.20Operating System Concepts

Counting Algorithms

Keep a counter of the number of references that have been made to each page. Count is reset each time a page is brought in

LFU Algorithm: replaces page with smallest count. May delay start of counting to avoid bias from an initial rush

MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used.

See example in Geoff’s notes

Page 21: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.21Operating System Concepts

LRU Page Replacement

3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (11)

4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (?)

Page 22: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.22Operating System Concepts

Least Recently Used (LRU) Algorithm

Counter implementation Every page entry has a counter; every time page is

referenced through this entry, copy the clock into the counter.

When a page needs to be changed, look at the counters to determine which are to change.

Stack implementation – keep a stack of page numbers in a double link form: Page referenced:

move it to the top requires 6 pointers to be changed

No search for replacement Both need HW support, as they are done very very often

Page 23: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.23Operating System Concepts

Use Of A Stack to Record The Most Recent Page References

Page 24: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.24Operating System Concepts

LRU Approximation Algorithms

Usually not enough HW support for full LRU, so … Reference bit in the page table

With each page associate a bit, in HW initially = 0 When page is referenced bit set to 1. Replace the one which is 0 (if one exists). We do not know the order, however. Clear when all 1

Reference bytes in the page table Make top bit the reference bit Shift right reference byte at regular intervals Victim is page with smallest value byte

Second chance bit associated with each frame Looks through frames in order, with wrap-around, starting at frame after last

loaded If frame has bit = 1 then:

set bit 0. leave page in memory. replace next page, subject to same rules.

Page 25: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.25Operating System Concepts

Second-Chance (clock) Page-Replacement Algorithm

Page 26: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.26Operating System Concepts

Enhanced Second Chance

Keep a reference bit and a modified bit with each frame 0,0 => not recently used or modified => best to replace 0,1 => not recently used but modified => must be written out 1,0 => recently used but not modified => may be used again soon 1,1 => Recently used and modified => best keep this one in RAM

Victim selection After each load, the current position is set to the frame after the

loaded one. Scan from current position round for a 0,0 frame If not found, scan round for a 0,1 frame, reseting the use bit (ala

2nd chance) If not found, start again (a victim must be found)

Used by Mac OS

Page 27: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.27Operating System Concepts

Page Buffering

Keep some frames free -a frame pool Immediately swap in on page fault Write out while first process uses CPU

Remember what pages are in the frame pool, in case they are requested again - used by VMS with FIFO to recover from preemptive removal

Write out modified pages whenever the swap disk is idle

Page 28: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.28Operating System Concepts

Other Techniques

Pre-paging - predicting page needs Windows NT clustering

Copy-on-Write Copy-on-Write (COW) allows both parent and child

processes to initially share the same pages in memory. If either process modifies a shared page, only then is the

page copied. COW allows more efficient process creation as only

modified pages are copied.

Page 29: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.29Operating System Concepts

Other Considerations

Program structure int A[ ][ ] = new int[1024][1024]; Each row is stored in one page Program 1 for (j = 0; j < A.length; j++)

for (i = 0; i < A.length; i++)A[i,j] = 0;

1024 x 1024 page faults

Program 2 for (i = 0; i < A.length; i++)for (j = 0; j < A.length; j++)

A[i,j] = 0;

1024 page faults

Page 30: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.30Operating System Concepts

Windows NT

Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page.

Processes are assigned working set minimum and working set maximum. 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.

Working set trimming removes pages from processes that have pages in excess of their working set minimum.

On single x86 CPUS, uses a second chance style algorithm to select victims

On Alpha and SMP, uses a modified FIFO

Page 31: Silberschatz, Galvin and Gagne 2002 10.1 Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory

Silberschatz, Galvin and Gagne 200210.31Operating System Concepts

Solaris 2

Maintains a list of free pages to assign faulting processes.

Lotsfree – threshold parameter to begin pageout. Pageout is called more frequently depending upon the

amount of free memory available.

Pageout scans pages using variation on the second chance algorithm (2 handed clock algorithm) Scanrate is the rate at which pages are scanned. This

ranged from slowscan to fastscan. This increases as free memory decreases

Handspread affects time process has to reuse a page before the big-hand send the page out

Pages from shared libraries are kept in memory (recent variation)