Post on 19-Oct-2020
transcript
1
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 1
CS 537Lecture 9
Page Replacement
Michael Swift
Virtual Memory Intuition
Idea: OS keeps unreferenced pages on disk– Slower, cheaper backing store than memory
Process can run when not all pages are loaded into main memory
OS and hardware cooperate to provide illusion of large disk as fast as main memory– Same behavior as if all of address space in main memory– Hopefully have similar performance
Requirements:– OS must have mechanism to identify location of each page in
address space à in memory or on disk– OS must have policy for determining which pages live in memory
and which on disk
2
Virtual Address Space Mechanisms
Each page in virtual address space maps to one of three locations:– Physical main memory: Small, fast, expensive– Disk (backing store): Large, slow, cheap– Nothing (error): Free
Logically extend page tables with an extra bit: present– permissions (r/w), valid, present– Page in memory: present bit set in PTE– Page on disk: present bit cleared
• PTE points to block on disk• Causes trap into OS when page is referenced
– Reality: same as valid bit (both use present on x86)• Trap: page fault
Present BitPFN valid prot present10 1 r-x 1- 0 - -23 1 rw- 0
28 1 rw- 04 1 rw- 1
- 0 - -- 0 - -- 0 - -- 0 - -- 0 - -- 0 - -- 0 - -- 0 - -
Phys Memory
Disk
16 1 rw- 1
What if access vpn 0xb?
3
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 5
Hardware and Kernel structures for paging
• Hardware:– Page table base register– TLB
• Software:– Page table
• Virtual --> physical or virtual --> disk mapping– Page frame database
• One entry per physical page• Information on page, owning process
– Segment list:• List of valid segments in a process• Used to determine if an address is valid but not present or
invalid– Swap file
Virtual Memory Mechanisms
Hardware and OS cooperate to translate addressesFirst, hardware checks TLB for virtual address
– if TLB hit, address translation is done; page in physical memoryIf TLB miss...
– Hardware or OS walk page tables– If PTE designates page is present, then page in physical memory
If page fault (i.e., present bit is cleared)– Trap into OS (not handled by hardware)– OS selects victim page in memory to replace
• Write victim page out to disk if modified (add dirty bit to PTE)– OS reads referenced page from disk into memory– Page table is updated, present bit is set– Process continues execution
What should scheduler do?
4
Mechanism for Continuing a Process
Continuing a process after a page fault is tricky– Want page fault to be transparent to user– Page fault may have occurred in middle of instruction
• When instruction is being fetched• When data is being loaded or stored
– Requires hardware support• precise interrupts: stop CPU pipeline such that instructions before
faulting instruction have completed, and those after can be restarted
Complexity depends upon instruction set– Can faulting instruction be restarted from beginning?
• Example: move +(SP), R2• Must track side effects so hardware can undo
Virtual Memory Policies
Goal: Minimize number of page faults– Page faults require milliseconds to handle (reading from
disk)– Implication: Plenty of time for OS to make good decision
OS has two decisions– Page selection
• When should a page (or pages) on disk be brought into memory?
– Page replacement• Which resident page (or pages) in memory should be thrown out to
disk?
5
Page SelectionWhen should a page be brought from disk into memory?Demand paging: Load page only when page fault occurs
– Intuition: Wait until page must absolutely be in memory– When process starts: No pages are loaded in memory– Problems: Pay cost of page fault for every newly accessed page
Prepaging (anticipatory, prefetching): Load page before referenced– OS predicts future accesses (oracle) and brings pages into memory
early– Works well for some access patterns (e.g., sequential)– Problems?
Hints: Combine above with user-supplied hints about page references– User specifies: may need page in future, don’t need this page
anymore, or sequential access pattern, ...– Example: madvise() in Unix
Page ReplacementWhich page in main memory should selected as victim?
– Write out victim page to disk if modified (dirty bit set)– If victim page is not modified (clean), just discard
OPT: Replace page not used for longest time in future– Advantages: Guaranteed to minimize number of page faults– Disadvantages: Requires that OS predict the future; Not practical, but
good for comparisonFIFO: Replace page that has been in memory the longest
– Intuition: First referenced long time ago, done with it now– Advantages: Fair: All pages receive equal residency; Easy to implement
(circular buffer)– Disadvantage: Some pages may always be needed
LRU: Least-recently-used: Replace page not used for longest time in past– Intuition: Use past to predict the future– Advantages: With locality, LRU approximates OPT– Disadvantages:
• Harder to implement, must track which pages have been accessed• Does not handle all workloads well
6
Page Replacement Example
Page reference string: ABCABDADBCBOPT FIFO LRU
ABC
BD
A
D
B
CB
AThree pagesof physical memory
Metric:Miss count
5, 7, 5 misses
Page Replacement Comparison
Add more physical memory, what happens to performance?– LRU, OPT: Add more memory, guaranteed to have fewer (or
same number of) page faults• Smaller memory sizes are guaranteed to contain
a subset of larger memory sizes• Stack property: smaller cache always subset of bigger
– FIFO: Add more memory, usually have fewer page faults• Belady’s anomaly: May actually have more page faults!
7
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 13
FIFO may Decrease PerformancePage
Requests C B A E C B D C B A E D
Newest Page C B A E C B D D D A E EC B A E C B B B D A A
Oldest Page C B A E C C C B D D
Page Requests C B A E C B D C B A E D
Newest Page C B A E E E D C B A E DC B A A A E D C B A E
C B B B A E D C B AOldest Page C C C B A E D C B
(red italics indicates page fault)
3 pages
4 pages
Problems with LRU-based Replacement
LRU does not consider frequency of accesses– Is a page accessed once in the past equal to one accessed N
times?– Common workload problem:
• Scan (sequential read, never used again) one large data region flushes memory
Solution: Track frequency of accesses to pagePure LFU (Least-frequently-used) replacement
– Problem: LFU can never forget pages from the far pastExamples of other more sophisticated algorithms:
– LRU-K and 2Q: Combines recency and frequency attributes– Expensive to implement, LRU-2 used in databases
8
Implementing LRU
Software Perfect LRU– OS maintains ordered list of physical pages by reference time– When page is referenced: Move page to front of list– When need victim: Pick page at back of list– Trade-off: Slow on memory reference, fast on replacement
Hardware Perfect LRU– Associate timestamp register with each page– When page is referenced: Store system clock in register– When need victim: Scan through registers to find oldest clock– Trade-off: Fast on memory reference, slow on replacement
(especially as size of memory grows)In practice, do not implement Perfect LRU
– LRU is an approximation anyway, so approximate more– Goal: Find an old page, but not necessarily the very oldest
Clock Algorithm
Hardware– Keep use (or reference) bit for each page frame– When page is referenced: set use bit
Operating System– Page replacement: Look for page with use bit cleared
(has not been referenced for awhile)– Implementation:
• Keep pointer to last examined page frame• Traverse pages in circular buffer• Clear use bits as search• Stop when find page with already cleared use bit, replace this page
9
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=1 use=1 use=0 use=1
clock hand
Clock:Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=1 use=0 use=1
clock hand
10
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=0 use=0 use=1
clock hand
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=0 use=0 use=1
clock hand
evict page 2 because it has not been recently used
11
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=0 use=0 use=1
clock hand
page 0 is accessed…
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=1 use=0 use=0 use=1
clock hand
12
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=1 use=0 use=0 use=1
clock hand
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=1 use=0 use=0 use=1
clock hand
13
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=1 use=0 use=0 use=0
clock hand
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=0 use=0 use=0
clock hand
14
Clock: Look For a Page
0 1 2 3 …Physical Mem:
use=0 use=0 use=0 use=0
clock hand
evict page 1 because it has not been recently used
Clock Extensions
Replace multiple pages at once– Intuition:
Expensive to run replacement algorithm and to write single block to disk
– Find multiple victims each time and track free listAdd software counter (“chance”)
– Intuition: Better ability to differentiate across pages (how much they are being accessed)
– Increment software counter if use bit is 0– Replace when chance exceeds some specified limit
Use dirty bit to give preference to dirty pages– Intuition: More expensive to replace dirty pages
• Dirty pages must be written to disk, clean pages do not– Replace pages that have use bit and dirty bit cleared
15
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 29
2nd Chance FIFO• LRU Clock is a global algorithm
– It looks at all physical pages, from all processes– Every process gets its memory taken away gradually
• Local algorithms: run page replacement separately for each process• 2nd Chance FIFO:
– Maintain 2 FIFO queues per process– On first access, pages go at end of queue 1– When the drop off queue 1, page are invalidated and move to queue 2– When they drop off queue 2, they are replaced– If they are accessed in queue 2, they are put back on queue 1
• Options:– Move to queue 1 immediately when referenced: mark “invalid” when on queue 2– Move to queue 2 when about to be evicted: looks like clock
• Comparison to LRU clock:– Per-process, not whole machine– No scanning– Replacement order is FIFO, not PFN– Used in Windows NT, VMS
Second chance page replacement• Inspect R bit of oldest page
– Recall: R bits are set when page is referenced (read or write); periodically (after k clock interrupts), R bits are cleared.
– If R==0 then• page is old & unused so replace it
– Else• Clear R bit• Move page from head to tail of FIFO
– (treating it as a newly loaded page)• Try a different page
16
Second chance page replacementload time
page
Clear R
Working Set• How much memory does a process need?
– All memory allocated– What it is using this cycle?
• The working set for a process at time t, W(D,t), is the set of pages that have been referenced in the last D virtual time units– virtual time = time elapsed while the process was in
execution (eg: number of instructions executed)– D is a window of time – at any t, |W(D,t)| is non decreasing with D– W(D,t) is an approximation of the program’s locality
32
local
17
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 33
Another Problem: allocation of frames• In a multiprogramming system, we need a way to
allocate physical memory to competing processes– what if a victim page belongs to another process?– family of replacement algorithms that takes this into account
• Fixed space algorithms– each process is given a limit of pages it can use– when it reaches its limit, it replaces from its own pages– local replacement: some process may do well, others suffer
• Variable space algorithms– processes’ set of pages grows and shrinks dynamically– global replacement: one process can ruin it for the rest
• linux uses global replacement
34
The Working Set Strategy• The working set of a process first grows when it starts
executing• then stabilizes by the principle of locality• it grows again when the process enters a new locality
(transition period)– up to a point where the working set contains pages from two
localities• then decreases after a sufficient long time spent in the
new locality
• How determine sizes:– Monitor pager fault rate– High rate -> needs more memory– Low rate -> has enough
18
What if no Hardware Support?
What can the OS do if hardware does not have use bit (or dirty bit)?– Can the OS “emulate” these bits?
Leading question: – How can the OS get control (i.e., generate a trap) every time use
bit should be set? (i.e., when a page is accessed?)
Where do pages go on disk?• Kernel organizes regions of virtual memory as
“areas” or segments according to how they are swapped– Data that gets swapped to the same file all goes to a
segment– Multiple memory areas can get swapped to the same file in
different places, or to anywhere in a “swap file” or “swap partition”
• How do you find a place to swap a page in the swap file?– Swap daemon maintains a “swap map” of
• Which blocks on disk are in use• Which virtual pages are stored in those blocks
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 36
19
When is a page allocated space?• When should a page be allocated/assigned space in
swap?– When it is allocated?
• ensures space available• Saves time on swapping• Total memory usage = swap
– When it is evicted?• May never need to do it• Can put it in a better place, so write pages sequentially• total memory usage = swap + ram
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 37
10/17/17 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 38
A cool trick• Memory-mapped files
– instead of using open, read, write, close• “map” a file into a region of the virtual address space
– e.g., into region with base ‘X’• accessing virtual address ‘X+N’ refers to offset ‘N’ in file• initially, all pages in mapped region marked as invalid
– OS reads a page from file whenever invalid page accessed– OS writes a page to file when evicted from physical memory
• only necessary if page is dirty
20
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
Shared Memory Mapped Files
21
Conclusions
Illusion of virtual memory:Processes can run when sum of virtual address spaces > amount of physical memoryMechanism:
– Extend page table entry with “present” bit– OS handles page faults (or page misses) by reading in desired
page from diskPolicy:
– Page selection – demand paging, prefetching, hints– Page replacement – OPT, FIFO, LRU, others
Implementations (clock) perform approximation of LRU