© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Paging
© Janice Regan, CMPT 300, May Translation Lookaside buffers TLB (associative memory): a hardware device that allows translation of virtual addresses to physical addresses without reference to the paging table TLB stores the translations (page table entries, and virtual page addresses) for the most recently or heavily used pages for fast reference. The commonest usage pattern for a process is to use the same few pages many times TLB can give a significant savings by not requiring reuse of the paging table mechanism for each reference. Usually holds only a few references (<64)
© Janice Regan, CMPT 300, May Finding an address: TLB How do we find an address in physical memory in a system using TLB? The page number of the virtual page is compared to all the pages in the TLB. (simultaneously). If a match is found (and the protections allow access) the page frame number is extracted from the appropriate TLB entry and prepended to the virtual offset to give the physical address Otherwise load the required page into the TLB replacing on that is already there. This is done by using the normal page table lookup process to find the correct page
© Janice Regan, CMPT 300, May Operation: paging and TLB Check TLB Page in TLB Y MMU generated Physical address continue N Access page table Page in memory Y Update TLB N Read page From disk memory full N Update Page table Y Page replacement
© Janice Regan, CMPT 300, May Inverted page tables We have seen that a 32 bit system will have a page table of 4MBytes, a lot but possible. What about a 64 bit system using the same page size, with larger will still be huge (2 52 * 4 bytes) or 15 million GBytes. This is not possible! To reduce page table size use an inverted page table with one entry per page frame, rather than one entry per page. Fewer entries and only one table regardless of the number of active processes The problem with the inverted page table is that the page in virtual memory can no longer be found using an index into a page table, the entire page table must be searched to find the desired entry. THIS IS SLOW!
Virtual to Physical: Inverted If we have one entry in our page table for each page frame, how do we find the entry for a particular page If pages of virtual memory can be placed in any page frame, then we must check every page frame looking for the page number of the page we are trying to find Can speed up access by assigning particular pages of virtual memory be loaded only into particular page frames. (remember our cache replacement algorithms) © Janice Regan, CMPT 300, May
Assigning page frames How do we assign pages of virtual memory to particular page frames. More pages of virtual than physical memory so must assign more than one page of virtual memory to some or all page frames Can use a hash function that translates a label in the virtual page to an index in the physical page frame number. For example you could hash based on the page number © Janice Regan, CMPT 300, May
Algorithm for hashing 1. Take the label from the virtual page 2. Apply the hashing function to give an index into the page frame table 3. If the indicated page frame is empty load the page in that page frame 4. If the indicated pate frame is occupied add 1 to the label and repeat the previous step 2 and 3 © Janice Regan, CMPT 300, May
Algorithm for referencing 1. Take the label for the virtual page containing the reference 2. apply the hashing function to give an index into the page frame table 3. If the indicated page frame is empty the referenced page has not been stored in the page frame table: generate a page fault 4. If the indicated page frame is occupied check the hash value, 1. if it matches access the page © Janice Regan, CMPT 300, May
9 Page replacement When a new page is placed in physical memory how do we decide which page it replaces? Ideally we wish to remove a page that is not likely to be accessed in the near future (to prevent extra page faults) How do we know which pages are likely to be accessed? Assume pages that have been recently accessed are more likely to be used again
© Janice Regan, CMPT 300, May Page table entries (1) Order and details of contents are architecture dependent but each entry in the page table includes The page frame number of the page frame holding that page in memory (if there is one) A flag indicating if the page is in a page frame at the present time A flag indicating if the page in physical memory has been changed since in was last copied back into virtual memory (dirty bit)
© Janice Regan, CMPT 300, May Page table entries (2) Each entry in the page table consists of A flag indicating if the page has been accessed Used to determine which pages have been recently used, This information is used by the page replacement algorithms to determine which frames to replace Protection information indicating what kinds of access are allowed Caching flag set to indicate page should not be cached Memory that refers to hardware registers should not be cached
© Janice Regan, CMPT 300, May Page replacement information used The page table includes a flag to indicate if the page has been accessed. OS has a paging management time period defined. Each time this time period ends the accessed bit (R bit) for each page is reset to 0 Each time the page is accessed the R bit is set to 1, and the last accessed time is updated to reflect the time of the most recent access
© Janice Regan, CMPT 300, May Page replacement information used Page table includes a flag (dirty bit) to indicate if the page has been modified since it was last synced with virtual memory. The flag is reset when the page is saved to virtual memory When a page is removed from physical memory If the dirty bit is set it will be written to virtual memory If the dirty bit is not set it will be abandoned and overwritten
© Janice Regan, CMPT 300, May Simple page replacement: FIFO First in first out page replacement Maintain a list of pages presently in page frames When a page is placed in a page frame in physical memory add it to the end of the list When a page fault occurs and all slots in physical memory are full remove the first page in the list from memory and replace it with the new page. Problem: if the page removed is heavily and continuously used it will then have to be reloaded almost immediately (as soon as it is next accessed)
© Janice Regan, CMPT 300, May Simple page replacement: NRU Not recently used page replacement (NRU) Use the accessed and modified bits in the page table entries. Gives 4 possibilities Not referenced not modified Not referenced modified (reference cleared on clock interrupt) Referenced not modified Reference modified When a page fault occurs, find any page in group 1, failing that any page in group 2, failing that any page in group 3, failing that any page in group 4. If there is more than one page in the lowest group containing pages randomly choose one page from the group of pages.
© Janice Regan, CMPT 300, May Page replacement: 2 nd chance Second chance page replacement A modification to FIFO Maintain a list of pages presently in page frames When a page is placed in a page frame in physical memory add it to the end of the list When a page fault occurs and all slots in physical memory are full Check the R (accessed) bit on the first entry If it is one move the first entry to be the end of the list and clear R Repeat if necessary until a page with R=0 is found Remove that first page with R=0 from physical memory and replace it with the new page.
© Janice Regan, CMPT 300, May Clock page replacement Implementation of second chance Keep page references in a circular buffer with a pointer to the oldest entry When page fault occurs look at R bit of that entry, if one clear R bit and advance hand, if one replace After replacement advance clock hand 1
© Janice Regan, CMPT 300, May Simple page replacement: LRU Least Recently Used replacement (LRU) Expensive to implement Using an actual linked list that must be updated with each memory access is prohibitive In rare cases may have hardware counter to count instructions and keep last access time value of the counter as part of the page table reference information More likely to implement a variant in software. The variant simulates a hardware counter in software to implement a similar algorithm called aging
© Janice Regan, CMPT 300, May Aging algorithm A K bit software counter is associated with each page, the counter is initialized to 0 At the end of each time interval the OS scans to determine which pages are in physical memory For each of these pages the bits in the page counters are shifted right 1 bit and the value of the R flag for the page replaces the leftmost bit, and the R bit is reset to 0. The page with the smallest value in its counter is chosen as the page to be replaced by the new page Aging is a commonly used algorithm
© Janice Regan, CMPT 300, May Aging End of 1 st clock interval Each clock (time) interval includes several memory accesses There may be a tie that cannot be resolved (value of counter for multiple pages may be identical.) In case of a non zero tie we do not know which of the tied counters were accessed first or least frequently within each time interval In case of a zero tie do not know which was accessed most recently (one may be 9 time intervals ago, one 7,000 time intervals ago, with an 8 bit counter we can no longer remember to differentiate) Initial counters R counter End of 2 nd clock intervalEnd of 3 rd clock interval
© Janice Regan, CMPT 300, May Working set Replacement Demand paging: pages are loaded as needed not preloaded at start of the process Working set: set of pages that the process is currently using. Currently usually defined as within the last n ms If physical memory is too small to hold the whole working set many page faults will occur. In extreme cases this causes thrashing or continuous page faulting bringing the system to an effective halt When a process is swapped may want to keep track of its present working set so that those pages can be pre reloaded (prepaging) into memory when the swapped process is restored. The working set tends to change slowly over time. Why?
© Janice Regan, CMPT 300, May Thrashing: CPU Utilization Starting new processes will reduce the number of page frames available to each process. Each process will load some set of frames into memory When the number of page frames needed to hold all these sets exceeds the number of available page frames the number of page faults begin to increase As the page fault rate increases, processes may be suspended to reduce the page fault rate. The system may try to optimize performance by starting new jobs to replace those swapped. System throughput is greatly decreased Solution: do not load a process unless there is room for its working set of pages in physical memory CPU utilization Number of simultaneous processes
© Janice Regan, CMPT 300, May How does the working set vary? If we use demand paging the working set will vary rapidly when a new process begins running The pages for that process are not in memory Each time a new page is accessed it will cause a page fault, the page will then be loaded into physical memory and become part of the processes working set Since the process is likely to frequently access a small number of pages at any given time the content of the working set will rapidly converge to this set of frequently accessed pages If we continue to use demand paging as the process continues to run, the number of new pages needed after the initial period will be small (like moving onto the next page of commands). The contents of the working set will then remain quite constant, with only occasional addition or deletions
© Janice Regan, CMPT 300, May Implementation: Defining a working set OS must keep track of which pages are part of a processes working set When a process is swapped out of physical memory, all its pages may be swapped out The working set will give us a good idea of the pages we should load when we bring the swapped process back into physical memory (we may want to preload these rather than wait for them to be requested) Not practical to keep a list of each access (for example storing page number each time a page is accessed for the last N accesses) Instead use which pages have been accessed in the last time interval (that is the R bit). This means the pages accessed in the last N seconds of CPU time used by the process
© Janice Regan, CMPT 300, May Working set replacement Find a page not in the working set and replace that page Each entry must contain the time of last access The page table entry for each page is scanned If R=1 the page has been accessed this time interval and is part of working set. Its last referenced time is updated to the present time. If R=1 for all pages in memory a random page is evicted If R=0 the difference between the present time and the last accessed time is determined. If that time difference is larger than the time interval then the page is no longer considered to be part of the working set. It may be removed and replaced with the new page If that time difference is less than the time interval, the page is still in the working set. If all pages in physical memory are still in the working set the oldest of the pages in the working set with R=0 is chosen for eviction
© Janice Regan, CMPT 300, May Implementation: working set replacement The implementation suggested in the previous slide is expensive since the entire page table must be scanned for each replacement The WSClock implementation is more efficient and is commonly used (Linux) Begin by creating a circular buffer to hold page table entries that contain the time of last access and the R and M bits for each page. As pages are loaded into physical memory add the page table entries in to the circular buffer
© Janice Regan, CMPT 300, May WSClock: adding new pages At first, while there are still empty page frames each new page is just added to the circular buffer of pages New entry New entry time R bit M bit
© Janice Regan, CMPT 300, May WSClock: example 1 Later when all page frames are already full we must select a page frame to replace with the new frame Current time: 3010 Current time interval:
© Janice Regan, CMPT 300, May WSClock: Page Fault step 1 Examine the page pointed to by the clock hand If R bit is 1, then the page has been referenced during the current time interval, it is part of the working set and not a candidate for removal. Set the R bit to 0 and advance the clock hand to the next possible page Otherwise the page might be a candidate for replacement proceed to step 2 to determine if it is
WSClock: Page Fault step 2 Examine the page pointed to by the clock hand If R (accessed) bit is 0, then check if it is in working set window (if present time – last accessed time is less than duration of the current time interval). If it is in the working set window the page is not a candidate for removal, set the R bit to 0 and advance the clock hand to the next page Otherwise the page might be a candidate for replacement proceed to step 3 to determine if it is © Janice Regan, CMPT 300, May
WSClock: Page Fault step 3 Examine the page pointed to by the clock hand Check to see if the dirty bit is 1 If the dirty bit is 1, then the page has been changed, and the changes have not yet been written back to memory. If we choose this page we would have to wait for it to be written back to disk. Initiate writing back to disk, and advance clock hand to the next page (remember this) Otherwise the page may be selected as the page to be replaced. © Janice Regan, CMPT 300, May
© Janice Regan, CMPT 300, May WSClock: Page Fault step 4 If the clock hand has advanced one full rotation (back to where it started) If a write has been scheduled continue until the algorithm finds a clean page (one that has finished writing) If no write has been scheduled choose the next clean page If no write has been scheduled and no clean page exists then choose the next page
© Janice Regan, CMPT 300, May WSClock: Page Fault step 5 When a page is selected for replacement The page does not need to be written to disk (it is already synchronized with the disk copy of the page) The R bit is set to 1 (loading the page counts as an access) The M bit is set to 0 (the page is synched) The present time is recorded in the entry
© Janice Regan, CMPT 300, May WSClock: example 1 Later when all page frames are already full we must select a page frame to replace with the new frame Current time: 3010 Current time interval:
© Janice Regan, CMPT 300, May WSClock: example 1 On a page fault, new page is needed, Examine the page pointed to by the clock hand The R bit is 1, then the page has been referenced during the current time interval, it is part of the working set and not a candidate for removal advance the clock hand by 1 (reset the R bit to 0) The R bit is 0 check if it is in working set window present time – last accessed time = = < duration of the current time interval ( 1000 ) The page is in the working set advance the clock hand by 1
© Janice Regan, CMPT 300, May WSClock: example Later when all page frames are already full we must select a page frame to replace with the new frame Current time: 3010 Current time interval:
© Janice Regan, CMPT 300, May WSClock: example 2 Examine the page pointed to by the clock hand The R bit is 0 check if it is in working set window present time – last accessed time = = > duration of the current time interval ( 1000 ) The page is not in the working set The modified bit (dirty bit) is 1 so start a print of the page frame to virtual memory, then advance the clock hand
© Janice Regan, CMPT 300, May WSClock: example 3 Examine the page pointed to by the clock hand The R bit is 0 check if it is in working set window present time – last accessed time = = > duration of the current time interval ( 1000 ) The page is not in the working set The modified bit (dirty bit) is 0 so this page frame is a candidate for replacement Replace the page frame with the new page from virtual memory
© Janice Regan, CMPT 300, May WSClock: example 4 Current time: 3010 Current time interval: