11b Paging

download 11b Paging

of 29

description

PAGING CONCEPTS

Transcript of 11b Paging

  • CSE451 Introduction to Operating Systems Winter 2012Paging

    Mark ZbikowskiGary Kimura

  • *Reminder: Mechanics of address translationpageframe 0pageframe 1pageframe 2pageframe Ypageframe 3physical memoryoffsetphysical addresspage frame #page frame #page tableoffsetvirtual addressvirtual page #Note: Each process has its own page table!

  • *Reminder: Page Table Entries (PTEs)PTEs control mappingthe valid bit says whether or not the PTE can be usedsays whether or not a virtual address is validit is checked each time a virtual address is usedthe referenced bit says whether the page has been accessedit is set when a page has been read or written tothe modified bit says whether or not the page is dirtyit is set when a write to the page has occurredthe protection bits control which operations are allowedread, write, executethe page frame number determines the physical pagephysical page start address = PFNpage frame numberprotMRV202111

  • *Paged virtual memoryWeve hinted that all the pages of an address space do not need to be resident in memorythe full (used) address space exists on secondary storage (disk) in page-sized blocksthe OS uses main memory as a (page) cachea page that is needed is transferred to a free page frameif there are no free page frames, a page must be evictedevicted pages go to disk (only need to write if they are dirty)all of this is transparent to the application (except for performance )managed by hardware and OSTraditionally called paged virtual memory

  • *Page faultsWhat happens when a process references a virtual address in a page that has been evicted?when the page was evicted, the OS set the PTE as invalid and noted the disk location of the page in a data structure (that looks like a page table but holds disk addresses)when a process tries to access the page, the invalid PTE will cause an exception (page fault) to be thrownOK, its actually an interrupt!the OS will run the page fault handler in responsehandler uses the like a page table data structure to locate the page on diskhandler reads page into a physical frame, updates PTE to point to it and to be validOS restarts the faulting processthere are a million and one details

  • *Demand pagingPages are only brought into main memory when they are referencedonly the code/data that is needed (demanded!) by a process needs to be loadedWhats needed changes over time, of courseHence, its called demand pagingFew systems try to anticipate future needsOS crystal ball module notoriously ineffectiveBut its not uncommon to cluster pagesOS keeps track of pages that should come and go togetherbring in all when one is referencedinterface may allow programmer or compiler to identify clusters

  • *Page replacementWhen you read in a page, where does it go?if there are free page frames, grab onewhat data structure might support this?if not, must evict something elsethis is called page replacementPage replacement algorithmstry to pick a page that wont be needed in the near futuretry to pick a page that hasnt been modified (thus saving the disk write)OS typically tries to keep a pool of free pages around so that allocations dont inevitably cause evictionsOS also typically tries to keep some clean pages around, so that even if you have to evict a page, you wont have to write itaccomplished by pre-writing when theres nothing better to doMuch more on this later!

  • *How do you load a program?Create process descriptor (process control block)Create page tablePut address space image on disk in page-sized chunksBuild page table (pointed to by process descriptor)all PTE valid bits falsean analogous data structure indicates the disk location of the corresponding pagewhen process starts executing:instructions immediately fault on both code and data pagesfaults taper off, as the necessary code/data pages enter memoryMuch more on this later when we discuss linking and loading

  • *Oh, man, how can any of this possibly work?Locality!temporal localitylocations referenced recently tend to be referenced again soonspatial localitylocations near recently references locations are likely to be referenced soon (think about why)Locality means paging can be infrequentonce youve paged something in, it will be used many timeson average, you use things that are paged inbut, this depends on many things:degree of locality in the applicationpage replacement policy and application reference patternamount of physical memory vs. application footprint or working set

  • *Evicting the best pageThe goal of the page replacement algorithm:reduce fault rate by selecting best victim page to removesystem fault rate or program fault rate??the best page to evict is one that will never be touched againduh never is a long timeBeladys proof: evicting the page that wont be used for the longest period of time minimizes page fault rateRest of this module:survey a bunch of page replacement algorithmsfor now (i.e., the next 5 slides), assume that a process pages against itself, using a fixed number of page frames

  • *#1: Beladys AlgorithmProvably optimal: lowest fault rate (remember SJF?)evict the page that wont be used for the longest time in futureproblem: impossible to predict the futureWhy is Beladys algorithm useful?as a yardstick to compare other algorithms to optimalif Beladys isnt much better than yours, yours is pretty goodhow could you do this comparison?Is there a best practical algorithm?no; depends on workloadIs there a worst algorithm?no, but random replacement does pretty badlydont laugh there are some other situations where OSs use near-random algorithms quite effectively!

  • *#2: FIFOFIFO is obvious, and simple to implementwhen you page in something, put it on the tail of a listevict page at the head of the listWhy might this be good?maybe the one brought in longest ago is not being usedWhy might this be bad?then again, maybe it is being usedhave absolutely no information either wayIn fact, FIFOs performance is typically lousyIn addition, FIFO suffers from Beladys Anomalythere are reference strings for which the fault rate increases when the process is given more physical memory

  • *#3: Least Recently Used (LRU)LRU uses reference information to make a more informed replacement decisionidea: past experience is a decent predictor of future behavioron replacement, evict the page that hasnt been used for the longest period of timeLRU looks at the past, Beladys wants to look at the futurehow is LRU different from FIFO?can you think of an example where LRU would be terrible?in general, it works exceedingly wellImplementationto be perfect, must grab a timestamp on every memory reference, put it in the PTE, order or search based on the timestamps way too $$$ in memory bandwidth, algorithm execution time, you name it

  • *Approximating LRUMany approximations, all use the PTE reference bitkeep a counter for each pageat some regular interval, for each page, do:if ref bit = 0, increment the counter (hasnt been used)if ref bit = 1, zero the counter (has been used)regardless, zero ref bitthe counter will contain the # of intervals since the last reference to the pagepage with largest counter is least recently usedSome architectures dont have PTE reference bitscan simulate reference bit using the valid bit to induce faultshack, hack, hack

  • *#4: LRU ClockAKA Not Recently Used (NRU) or Second Chancereplace page that is old enoughlogically, arrange all physical page frames in a big circle (clock)just a circular linked lista clock hand is used to select a good LRU candidatesweep through the pages in circular order like a clockif ref bit is off, it hasnt been used recently, we have a victimso, what is minimum age if ref bit is off?if the ref bit is on, turn it off and go to next pagearm moves quickly when pages are neededlow overhead if have plenty of memoryif memory is large, accuracy of information degradesadd more hands to fix

  • *Allocation of frames among processesFIFO and LRU Clock each can be implemented as either local or global replacement algorithmslocaleach process is given a limit of pages it can useit pages against itself (evicts its own pages)globalthe victim is chosen from among all page frames, regardless of ownerprocesses page frame allocation can vary dynamicallyIssues with local replacement?Issues with global replacement?Linux uses global replacement

  • *Hybrid algorithmslocal replacementan explicit mechanism for adding or removing page framesIssues with all approaches?

  • *Number of page frames allocated to processNumber of memory references between page faultsWhy?Why?Where would you like to operate?

  • *The working set model of program behaviorThe working set of a process is used to model the dynamic locality of its memory usageworking set = set of pages process currently needsformally defined by Peter Denning in the 1960sDefinition:WS(t,w) = {pages P such that P was referenced in the time interval (t, t-w)}t: timew: working set window (measured in page refs)a page is in the working set (WS) only if it was referenced in the last w referencesobviously the working set (the particular pages) varies over the life of the programso does the working set size (the number of pages in the WS)

  • *Working set sizeThe working set size, |WS(t,w)|, changes with program localityduring periods of poor locality, more pages are referencedwithin that period of time, the working set size is largerIntuitively, the working set must be in memory, otherwise youll experience heavy faulting (thrashing)when people ask How much memory does Vista need?, really theyre asking what is Vistas average (or worst case) working set size?

  • *#5: Hypothetical Working Set algorithmEstimate |WS(0,w)| for a processAllow that process to start only if you can allocate it that many page framesUse a local replacement algorithm (LRU Clock?) make sure that the right pages (the working set) are occupying the processs framesTrack each processs working set size, and re-allocate page frames among processes dynamicallyProblem? Solution?What the heck is w?

  • *#6: Page Fault Frequency (PFF)PFF is a variable-space algorithm that uses a more ad hoc approachAttempt to equalize the fault rate among all processes, and to have a tolerable system-wide fault ratemonitor the fault rate for each processif fault rate is above a given threshold, give it more memoryso that it faults lessif the fault rate is below threshold, take away memoryshould fault more, allowing someone else to fault less

  • *ThrashingThrashing is when the system spends most of its time servicing page faults, little time doing useful workcould be that there is enough memory but a lousy replacement algorithm (one incompatible with program behavior)could be that memory is over-committedtoo many active processes

  • *Number of active processesSystem throughput (requests/sec.) with zero overheadWhy?Why?

  • *Number of active processesSystem throughput (requests/sec.) with thrashingWhy?

  • *Where is life interesting?Not if system has too much memorypage replacement algorithm doesnt much matter (over-provisioning)Not if system has too little memorypage replacement algorithm doesnt much matter (over-committed)Life is only interesting on the border between over-provisioned and over-committedNetworking analogiesAloha Network as an example of thrashingover-provisioning as an alternative to Quality of Service guarantees

  • *SummaryVirtual memoryPage faultsDemand pagingdont try to anticipatePage replacementlocal, global, hybridLocalitytemporal, spatialWorking setThrashing

  • *Page Replacement AlgorithmsPage replacement algorithms#1: Beladys optimal, but unrealizable#2: FIFO replace page loaded furthest in the past#3: LRU replace page referenced furthest in the pastapproximate using PTE reference bit#4: LRU Clock replace page that is old enough#5: Working Set keep the working set in memory#6: Page Fault Frequency grow/shrink number of frames as a function of fault rate

  • *Still to come in Memory ManagementHard and Soft Page FaultsTwo-Level Page TablesTLB & ColoringMemory Mapped FilesVirtual Memory LayoutPaged versus Non-Paged MemorySubpage Allocation

    April 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating SystemsApril 25, 2007April 25, 2007CSE 451 Introduction to Operating Systems*CSE 451 Introduction to Operating Systems