19
EECS 482 Introduction to Operating Systems Fall 2019 Baris Kasikci Slides by: Harsha V. Madhyastha

EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

EECS 482Introduction to Operating

Systems

Fall 2019

Baris Kasikci

Slides by: Harsha V. Madhyastha

Page 2: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Recap: Paging● Both address spaces and physical memory

broken up into fixed size pages

Feb 25, 2019 EECS 482 – Lecture 14 2

Address Space

Page 1

Page 2

Page 3

Page N

Physical Memory

Page 3: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Paging

if (virtual page is invalid or non-resident or protected) {trap to OS fault handler; retry

} else {physical page # = pageTable[virtual page #].physPageNum

}

Feb 25, 2019 EECS 482 – Lecture 14 3

Virt. page # Phys. page # resident protected0 105 1 0

1 15 0 0

2 283 1 1

3 invalid

… invalid

Page 4: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page Replacement● Not all valid pages may fit in physical memory

• Some pages are swapped out to disk

● To read in a page from disk, some resident page must be swapped out to disk

● Which page to evict when you need a free page?

Feb 25, 2019 EECS 482 – Lecture 14 4

Page 5: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Replacement policies● Random

● FIFO• Replace page brought into memory longest time ago• May replace pages that continue to be frequently used

● Optimal (OPT)?

Feb 25, 2019 EECS 482 – Lecture 14 5

Page 6: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Replacement policies● LRU (least recently used)

• Approximates OPT by using past reference pattern» If page hasn’t been used for a while, it probably

won’t be used for a long time in the future

● LRU is hard to implement exactly• Can we simplify LRU by approximating it?

Feb 25, 2019 EECS 482 – Lecture 14 6

Page 7: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

The “referenced” bit● Most MMUs maintain a “referenced” bit for

each resident page• Set by MMU when page is read or written• Can be cleared by OS

● How to use reference bit to identify old pages?● Why maintain reference bit in hardware?

Feb 25, 2019 EECS 482 – Lecture 14 7

Page 8: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Clock replacement algorithm● Arrange resident pages around a clock

● Algorithm to select page for eviction:• Consider page pointed to by clock hand• If not referenced, page hasn’t been accessed since last sweep à

Evict• If referenced, page has been referenced since last sweep

» What to do?

● What if all pages have been referenced since last sweep?● What about new pages?

Feb 25, 2019 EECS 482 – Lecture 14 8

A

E

BF

D

C

Page 9: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

LRU Clock

Feb 25, 2019 EECS 482 – Lecture 14 9

P1: 1

P2: 1

P3: 1

P8: 1

P7: 0

P6: 1

P5: 1

P4: 0

P1: 0

P2: 0

P3: 0

P8: 1

P7: 0

P6: 1

P5: 1

P9: 1

● A nice feature of clock is that it only adds overhead when you need to evict a page

Page 10: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page eviction● Where to evict page to?● When do you NOT need to write page to disk?● Why not write to disk on every store?● The page that is brought from disk must wait

while some page is evicted• How could you optimize eviction?

● DON’T use these optimizations in Project 3!

Feb 25, 2019 EECS 482 – Lecture 14 10

Page 11: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page table contents

if (virtual page is non-resident or protected) {trap to OS fault handlerretry access

} else {physical page # = pageTable[virtual page #].physPageNumpageTable[virtual page #].referenced = trueif (access is write) {

pageTable[virtual page #].dirty = true}access physical memory

}Feb 25, 2019 EECS 482 – Lecture 14 11

Physical page # Resident Read/Writeenabled Dirty Referenced

Written by OS, Read by MMUWritten by OS/MMU

Read by OS

Page 12: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page table contents

● Why no valid bit in PTE?● For valid non-resident pages, does PTE contain

disk block?● Can we make do without resident bit?

Feb 25, 2019 EECS 482 – Lecture 14 12

Physical page # Resident Read/Writeenabled Dirty Referenced

Page 13: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page table contents

● Can we make do without dirty bit?● Won’t this increase # of page faults a lot?

Feb 25, 2019 EECS 482 – Lecture 14 13

Physical page # Read/Writeenabled Dirty Referenced

Page 14: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page table contents

● Can we make do without referenced bit?● Application too may want to control protection

• Not in project 3

Feb 25, 2019 EECS 482 – Lecture 14 14

Physical page # Read/Writeenabled Referenced

Page 15: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Page table contents

Feb 25, 2019 EECS 482 – Lecture 14 15

Physical page # read_enabled write_enabled

Page 16: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Midterm

● Review different synchronization techniques we have learned

● You should be comfortable with project-2 concepts/code

● Sleep well before theexam

Feb 25, 2019 EECS 482 – Lecture 14 16

Page 17: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Address Space Management● How to manage a process’s accesses to its

address space?• Kernel sets up page table per process and

manages which pages are resident• MMU looks up page table to translate any virtual

address to a physical memory address

● What about kernel’s address space?● How does MMU handle kernel’s loads and

stores?

Feb 25, 2019 EECS 482 – Lecture 14 17

Page 18: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Storing User Page Tables● Two options:

• In physical memory• In kernel’s virtual address space

● Difference: Is PTBR a physical or virtual addr?● Pros and cons

● Project 3 uses second option• Kernel’s address space managed by infrastructure

Feb 25, 2019 EECS 482 – Lecture 14 18

Page 19: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../section1/lec12_eviction-pre.pdfmemory addresses (e.g., to refer to translation data)? Feb 25, 2019 EECS 482 –Lecture

Kernel vs. user address spaces● Can you evict the kernel’s virtual pages?

● How can kernel access specific physical memory addresses (e.g., to refer to translation data)?

Feb 25, 2019 EECS 482 – Lecture 14 19