page replacement - computer science and …web.cse.ohio-state.edu › ... › 2431 ›...

36
Page Replacement 1 CSE 2431: Introduction to Operating Systems Instructor: Adam C. Champion, Ph.D. Reading: §§10.2–10.6, [OSC]

Upload: others

Post on 26-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Replacement

1

CSE 2431: Introduction to Operating SystemsInstructor: Adam C. Champion, Ph.D.Reading: §§10.2–10.6, [OSC]

Page 2: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Contents• Demand Paging• Page Replacement (1/2)

– Optimal– FIFO– LRU

• Belady’s Anomaly• Page Replacement (2/2)

– NFU– Clock– WSClock

• Working Set2

Page 3: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Review

• Page Table– Paging mapping hardware

• TLB: cache of page table– TLB miss

• Multi-level Paging• Inverted Page Table• Sharing and Protection

3

Page 4: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Paging Policies

• Fetch Strategies – When should a page be brought into primary (main)

memory from secondary (disk) storage.

• Placement Strategies– When a page is brought into primary storage, where

should it be put?

• Replacement Strategies– Which page now in primary storage is to be evicted from

primary storage when some other page is to be brought in and there is not enough room.

4

Page 5: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Demand Paging• Algorithm: NEVER bring a page into primary memory until

its needed. 1. Page fault 2. Check if a valid virtual memory address. Kill job if not. 3. If valid reference, check if its cached in memory already

(perhaps for other processes.) If so, skip to 7). 4. Find a free page frame. 5. Map address into disk block and fetch disk block into page

frame. Suspend user process. 6. When disk read finished, add vm mapping for page frame. 7. If necessary, restart the faulted instruction.

5

Page 6: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Demand Paging Example

6

Load M i

Free frame

Pagetable

VM

Ref Fault

Page 7: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Replacement1. Find location of page on disk 2. Find a free page frame

1. If there is a free page frame, use it 2. Otherwise, select a page frame using the page replacement

algorithm3. Write the selected modified page to the disk and update any

necessary tables

3. Read the requested page from the disk. 4. Restart the faulted instruction.

7

Page 8: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Issue: Eviction

• Hopefully, kick out a less-useful page– Dirty pages require writing, clean pages don’t

• Hardware has a dirty bit for each page frame indicating this page has been updated or not

– Where do you write? To “swap space”

• Goal: kick out the page that’s least useful• Problem: how do you determine usefulness?

– Heuristic: temporal locality exists– Kick out pages that aren’t likely to be used again

8

Page 9: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Terminology

• Reference string: the memory reference sequence generated by a program.

• Paging: moving pages from (to) disk• Optimal: the best (theoretical) strategy• Eviction: throwing something out • Pollution: bringing in useless pages or

cache lines

9

Page 10: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Replacement Strategies• The Principle of Optimality: Replace the page that will

not be used again the farthest time in the future. • Random page replacement: Choose a page randomly • FIFO - First in First Out: Replace the page that has

been in primary memory the longest • LRU - Least Recently Used: Replace the page that has

not been used for the longest time • LFU - Least Frequently Used – Replace the page that is used least often– An approximation to LRU

• NRU - Not Recently Used: Replace the page that is not used recently

• Working Set: Keep in memory those pages that the process is actively using.

10

Page 11: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Principle of Optimality • Description:

– Assume that each page can be labeled with the number of instructions that will be executed before that page is first referenced (i.e., we know the future reference string for a program)

– Then the optimal page algorithm would choose the page with the highest label to be removed from the memory.

• This algorithm provides a basis for comparison with other schemes.

• Impractical because it needs future references• If future references are known

– Should not use demand paging – Should use pre-paging to allow paging to be overlapped with

computation.

11

Page 12: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Example: Optimality

12

12 references, 7 faults Page Refs

3 Page FramesFault? Page Contents

– – – – –A Yes A – –B Yes B A –C Yes C B AD Yes D B AA No D B AB No D B AE Yes E B AA No E B AB No E B AC Yes C E BD Yes D C EE No D C E

time

Page 13: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Example: FIFO

13

12 references, 9 faults Page Refs

3 Page FramesFault? Page Contents

– – – – –A Yes A – –B Yes B A –C Yes C B AD Yes D C BA Yes A D CB Yes B A DE Yes E B AA No E B AB No E B AC Yes C E BD Yes D C EE No D C E

time

Page 14: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Expected Paging Behavior with Increasing Number of Page Frames

14

Page 15: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Belady’s Anomaly (for FIFO)

15

As the number of page frames increase, so does the fault rate.

12 references, 10 faults

FIFO, 4 physical pagesPage Refs

4 Page FramesFault? Page Contents

– – – – – –A Yes A – – –B Yes B A – –C Yes C B A –D Yes D C B AA No D C B AB No D C B AE Yes E D C BA Yes A E D CB Yes B A E DC Yes C B A ED Yes D C B AE Yes E D C Btime

Page 16: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Least Recently Used (LRU)

16

12 references, 10 faults

Page Refs

3 Page FramesFault? Page Contents

– – – – –A Yes A – –B Yes B A –C Yes C B AD Yes D C BA Yes A D CB Yes B A DE Yes E B AA No A E BB No B A EC Yes C B AD Yes D C BE Yes E D C

time

Page 17: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

LRU and Anomalies

17

Anomalies cannot occur. Why?

12 references, 8 faults

LRU, 4 physical pages

Page Refs

4 Page FramesFault? Page Contents

– – – – – –A Yes A – – –B Yes B A – –C Yes C B A –D Yes D C B AA No A D C BB No B A D CE Yes E B A DA No A E B DB No B A E DC Yes C B A ED Yes D C B AE Yes E D C Btime

Page 18: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

LRU Issues• How to track “recency”?

– Use time • Record time of reference with page table entry• Use counter as clock• Search for smallest time

– Use stack • Remove reference of page from stack (linked list)• Push it on top of stack

• Both approaches require large processing overhead, more space, and hardware support.

18

Page 19: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Not Recently Used (NRU)

• Page Classes (R, M)– (0, 0): Neither referenced nor modified– (0, 1): Not referenced (recently) but modified– (1, 0): Referenced but unmodified– (1, 1): Referenced and modified

• Algorithm– Select a page from lowest class– If conflict, use random or FIFO.– More details:

• Hardware sets up R and M for each memory access• OS periodically clear R bit

19

Page 20: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

NFU: A LRU Approximation• NFU (Not Frequently Used): Evict a page that is not

frequently used• LRU: evict a page that is least recently used.• NFU implementation: simpler than LRU

– A software counter is kept per page– The R bit (0/1) is added into the counter periodically

• Directly added (never forget history)• Right-shift the counter one bit and add the R bit to the leftmost (aging)

– 00110011 would be accessed more frequently than 00010111– The page with the counter holding the lowest number is the least

frequently used.– The value may not be unique; use FIFO to resolve conflicts

20

Page 21: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Second Chance (Clock)• Only one reference bit in the page table entry

– 0 initially – 1 when a page is referenced

• Pages are kept in FIFO order• Choose “victim” to evict

– Select the head of linked list– If page has reference bit set, reset bit, put it back to the tail of the list,

and process the next page.– Keep processing until reach page with zero reference bit and page

that one out.

• Clock algorithm is a variant of second chance (use circular list)

21

Page 22: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Frame Allocation: Minimum

• How are page frames allocated to individual processes’ virtual memories in a multi-programmed environment?

• Simple case: allocate a minimum number of frames per process– Most instructions require two operands

– Include extra page for paging out, one for paging in

– Moves, indirection instructions may need more pages22

Page 23: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Equal Allocation

• Allocate an equal number of frames per job – But jobs use memory unequally–High priority jobs have same number of page

frames as low priority jobs– Degree of multiprogramming might vary

23

Page 24: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Proportional Allocation

• Allocate a number of frames per job proportional to job size – Challenge: how do you determine job size: by

run command parameters or dynamically?

24

Page 25: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Fault Rate Curve

25

As page frames per virtual memory address space decrease, page fault rate increases

Page 26: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Thrashing

• Computation has locality• As page frames decrease, the page frames

available are not large enough to contain the locality of the process

• The processes start faulting heavily• Pages that are read in are used,

immediately paged out

26

Page 27: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Thrashing and CPU Utilization

27

• As page fault rate goes up, processes get suspended on page out queues for the disk

• System may try to optimize performance by starting new jobs• Starting new jobs will reduce the number of page frames

available to each process, increasing the page fault requests• System throughput plunges

Page 28: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Working Set• Working set model

assumes locality• Principle of locality: a

program clusters access to data and text temporally

• As the number of page frames exceeds a threshold, page fault rate drops dramatically

28

Page 29: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Working Set in Action

• Algorithm– If # free page frames > working set of some

suspended process, then activate process and map in all its working set

– If working set size of some process increases and no page frame is free, suspend process and release all its pages

• Moving window over reference string used for determination

• Keep track of working set

29

Page 30: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Refs

D = 4 Page FramesFault? Page Contents

– – – – – –A Yes A – – –B Yes A B – –C Yes A B C –D Yes A B C DA No A B C DB No A B C DE Yes A B D EA No A B E –B No A B E –C Yes A B C ED Yes A B C DE Yes A B C E

Working Set Example

30

12 references, 8 faults

Window size is Δ

time

Page 31: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Working Set Solution

• Approximate working set model using timer, reference bit, and age.

• Set timer to interrupt periodically to clear reference bit

• Once fault happens, remove pages that have not been referenced and old enough (> t) ⟹ outside of working set

31

Page 32: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Fault Frequency Version of Working Set (1)

• Assume that if the working set is correct there will not be many page faults.

• If page fault rate increases beyond assumed knee of curve, then increase number of page frames available to process.

• If page fault rate decreases below foot of knee of curve, then decrease number of page frames available to process.

32

Page 33: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Fault Frequency Version of Working Set (2)

33

Page 34: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

WSClock Page Replacement Algorithm• Carr and Hennessy (1981); used very widely (Linux)• Circular list as in the clock algorithm (initially empty list)

– As pages are loaded into memory, pages are added to the list – Each entry contains Time of last use plus reference, dirty bits.

• Algorithm: – At each page fault, examine page pointed to by clock hand– Repeat the following:

1. If reference bit is 1, then page has been referenced during current tick, so it’s in working set (poor candidate for eviction). Clear bit, update Time of last use

2. If reference bit is 0, then check if it’s in working set window (i.e., if Current Time – Time of last use < Working set window size time).

3. If page is not in the working set and the page is clean, then replace it. 4. If page is not in working set and page is dirty, request write to disk, go to

next page in circular list 34

Page 35: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Page Size Again• Small pages

– Rationale: • Locality of reference tends to be small (256)• Less fragmentation

– Problem: Require large page tables

• Large pages– Rationale:

• Small page table• I/O transfers have high seek time, so better to transfer

more data per seek– Problem: Internal fragmentation

35

Page 36: Page Replacement - Computer Science and …web.cse.ohio-state.edu › ... › 2431 › 12-PageReplacement.pdfDemand Paging • Algorithm: NEVER bring a page into primary memory until

Summary

• Demand Paging• Page Replacement (1/2)

– Optimal– FIFO– LRU

• Belady’s Anomaly• Page Replacement (2/2)

– NFU– Clock– WSClock

• Working Set 36