240 likes | 252 Views
CSE 120 Principles of Operating. Systems Spring 201 7 Lecture 1 3 : Page Replacement. Memory Management. Final lecture on memory management: Goals of memory management To provide a convenient abstraction for programming
E N D
CSE120 Principles ofOperating Systems Spring2017 Lecture 13: PageReplacement
MemoryManagement • Final lecture on memorymanagement: • Goals of memorymanagement • To provide a convenient abstraction forprogramming • To allocate scarce memory resources among competing processes to maximize performance with minimaloverhead • Mechanisms • Physical and virtual addressing(1) • Techniques: Partitioning, paging, segmentation(1) • Page table management, TLBs, VM tricks(2) • Policies • Page replacement algorithms(3) CSE 120 – Lecture 11 – PageReplacement
LectureOverview • Review paging and pagereplacement • Survey page replacementalgorithms • Discuss local vs. globalreplacement • Discussthrashing CSE 120 – Lecture 11 – PageReplacement
Locality • All paging schemes depend onlocality • Processes reference pages in localizedpatterns • Temporallocality • Locations referenced recently likely to be referencedagain • Spatiallocality • Locations near recently referenced locations are likely tobe referencedsoon • Although the cost of paging is high, if it isinfrequent • enough it isacceptable • Processes usually exhibit both kinds of locality duringtheir execution, making pagingpractical CSE 120 – Lecture 11 – PageReplacement
Demand Paging(OS) • Recall demand paging from the OSperspective: • Pages are evicted to disk when memory isfull • Pages loaded from disk when referencedagain • References to evicted pages cause a TLBmiss • » PTE was invalid, causesfault • OS allocates a page frame, reads page fromdisk • When I/O completes, the OS fills in PTE, marks it valid,and restarts faultingprocess • Dirty vs. cleanpages • Actually, only dirty pages (modified) need to be written todisk • Clean pages do not – but you need to know where on diskto • read them fromagain CSE 120 – Lecture 11 – PageReplacement
Demand Paging(Process) • Demand paging is also used when a processfirst • startsup • When a process is created, ithas • A brand new page table with all valid bitsoff • No pages in physicalmemory • When the process startsexecuting • Instructions fault on code and datapages • Faulting stops when all necessary code and data pages arein • memory • Only code and data needed by a process needs to beloaded • This, of course, changes overtime… CSE 120 – Lecture 11 – PageReplacement
PageReplacement • When a page fault occurs, the OS loads thefaulted • page from disk into a page frame of memory • At some point, the process has used all of thepage • frames it is allowed touse • This is likely (much) less than all of availablememory • When this happens, the OS must replace a page for each page faulted in • It must evict a page to free up a pageframe • The page replacement algorithm determines how this isdone • And they come in all shapes andsizes CSE 120 – Lecture 11 – PageReplacement
Evicting the BestPage • The goal of the replacement algorithm is to reducethe • fault rate by selecting the best victim page toremove • The best page to evict is the one never touchedagain • Will never fault onit • Never is a long time, so picking the page closest to “never” is the next bestthing • Evicting the page that won’t be used for the longest periodof • time minimizes the number of pagefaults • Proved byBelady • We’re now going to survey various replacement algorithms, starting withBelady’s CSE 120 – Lecture 11 – PageReplacement
Belady’sAlgorithm • Belady’s algorithm is known as the optimal page replacement algorithm because it has the lowest fault rate for any page referencestream • Idea: Replace the page that will not be used for thelongest time in thefuture • Problem: Have to predict thefuture • Why is Belady’suseful then? Use it as ayardstick • Compare implementations of page replacementalgorithms • with the optimal to gauge room forimprovement • If optimal is not much better, then algorithm is prettygood • If optimal is much better, then algorithm could use somework • » Random replacement is often the lowerbound CSE 120 – Lecture 11 – PageReplacement
First-In First-Out(FIFO) • FIFO is an obvious algorithm and simple toimplement • Maintain a list of pages in order in which they were pagedin • On replacement, evict the one brought in longest timeago • Why might this begood? • Maybe the one brought in the longest ago is not beingused • Why might this bebad? • Then again, maybe it’snot • We don’t have any info to say one way or theother • FIFO suffers from “Belady’sAnomaly” • The fault rate might actually increase when the algorithmis given more memory (verybad) CSE 120 – Lecture 11 – PageReplacement
Least Recently Used(LRU) • LRU uses reference information to make amore • informed replacementdecision • Idea: We can’t predict the future, but we can make aguess based upon pastexperience • On replacement, evict the page that has not been used forthe longest time in the past (Belady’s:future) • When does LRUdowell? When does LRU dopoorly? • Implementation • To be perfect, need to time stamp every reference(or maintain a stack) – much toocostly • So we need to approximateit CSE 120 – Lecture 11 – PageReplacement
ApproximatingLRU • LRU approximations use the PTE referencebit • Keep a counter for eachpage • At regular intervals, for every pagedo: • » If ref bit = 0, incrementcounter • » If ref bit = 1, zero thecounter • » Zero the referencebit • The counter will contain the number of intervals since thelast reference to thepage • The page with the largest counter is the least recentlyused • Some architectures don’t have a referencebit • Can simulate reference bit using the valid bit to inducefaults • What happens when we make a pageinvalid? CSE 120 – Lecture 11 – PageReplacement
LRUClock (Not RecentlyUsed) • Not Recently Used (NRU) – Used byUnix • Replace page that is “oldenough” • Arrange all of physical page frames in a big circle(clock) • A clock hand is used to select a good LRUcandidate • » Sweep through the pages in circular order like aclock • » If the ref bit is off, it hasn’t been usedrecently • What is the minimum “age” if ref bit isoff? • » If the ref bit is on, turn it off and go to nextpage • Arm moves quickly when pages areneeded • Low overhead when plenty ofmemory • If memory is large, “accuracy” of informationdegrades • » What does it degradeto? • » One fix: use two hands (leading erase hand, trailing selecthand) CSE 120 – Lecture 11 – PageReplacement
Example: gcc PageReplace CSE 120 – Lecture 11 – PageReplacement
Example: Belady’s Anomaly CSE 120 – Lecture 11 – PageReplacement
Fixed vs. VariableSpace • In a multiprogramming system, we need a wayto • allocate memory to competingprocesses • Problem: How to determine how much memory to give to eachprocess? • Fixed spacealgorithms • » Each process is given a limit of pages it canuse • » When it reaches the limit, it replaces from its ownpages • » Localreplacement • Some processes may do well while otherssuffer • Variable spacealgorithms • » Process’ set of pages grows and shrinksdynamically • » Globalreplacement • One process can ruin it for therest CSE 120 – Lecture 11 – PageReplacement
Working SetModel • A working set of a process is used to modelthe • dynamic locality of its memoryusage • Defined by Peter Denning in60s • Definition • WS(t,w) = {pages P such that P was referenced in thetime interval (t,t-w)} • t – time, w – working set window (measured in pagerefs) • A page is in the working set (WS) only if it was • referenced in the last wreferences CSE 120 – Lecture 11 – PageReplacement
Working SetSize • The working set size is the number of unique pagesin • the workingset • The number of pages referenced in the interval (t,t-w) • The working set size changes with programlocality • During periods of poor locality, you reference morepages • Within that period of time, the working set size islarger • Intuitively, want the working set to be the set ofpages • a process needs in memory to prevent heavyfaulting • Each process has a parameter w that determines aworking set with fewfaults • Denning: Don’t run a process unless working set is inmemory CSE 120 – Lecture 11 – PageReplacement
Example: gcc WorkingSet CSE 120 – Lecture 11 – PageReplacement
Working SetProblems • Problems • How do we determinew? • How do we know when the working setchanges? • Too hard toanswer • So, working set is not used in practice as a pagereplacement algorithm • However, it is still used as anabstraction • The intuition is stillvalid • When people ask, “How much memory does Firefox need?”, they are in effect asking for the size of Firefox’s workingset CSE 120 – Lecture 11 – PageReplacement
Page Fault Frequency(PFF) • Page Fault Frequency (PFF) is a variablespace • algorithm that uses a more ad-hocapproach • Monitor the fault rate for eachprocess • If the fault rate is above a high threshold, give it morememory • » So that it faultsless • » But not always (FIFO, Belady’sAnomaly) • If the fault rate is below a low threshold, take awaymemory • » Should faultmore • » But notalways • Hard to use PFF to distinguish between changes in locality and changes in size of workingset CSE 120 – Lecture 11 – PageReplacement
Thrashing • Page replacement algorithms avoidthrashing • When most of the time is spent by the OS in paging databack and forth fromdisk • Little time spent doing useful work (makingprogress) • In this situation, the system isovercommitted • » No idea which pages should be in memory to reducefaults • » Could just be that there isn’t enough physical memory for all of the processes in thesystem • » Ex: Running Windows95 with 4 MB ofmemory… • Possiblesolutions • » Swapping – write out all pages of aprocess • » Buy morememory CSE 120 – Lecture 11 – PageReplacement
Summary • Page replacementalgorithms • Belady’s – optimal replacement (minimum # offaults) • FIFO – replace page loaded furthest inpast • LRU – replace page referenced furthest inpast • » Approximate using PTE referencebit • LRU Clock – replace page that is “oldenough” • Working Set – keep the set of pages in memory thathas minimal fault rate (the “workingset”) • Page Fault Frequency – grow/shrink page set as a functionof faultrate • Multiprogramming • Should a process replace its own page, or that ofanother? CSE 120 – Lecture 11 – PageReplacement
Nexttime… • Read Chapters 37, 39, 40 CSE 120 – Lecture 11 – PageReplacement