Memory Management Virtual Memory Part of A3 course (by Theo Schouten) Biniam Gebremichael http://www.cs.ru.nl/~biniam/ Office: A6004 April 4 2005
Content Virtual memory Definition Advantage and challenges Virtual memory Paging Page table Transitional Lookaside Buffer (TLB) Virtual memory Segmentation Fetch policy Replacement policy Unix/Linux/Windows memory management 2/49
From Last Week Fixed and dynamic partition Paging and Segmentation Internal and External fragmentation Memory partition Fixed Dynamic Fixed Partition Dynamic Partiton Also Process partition Paging Segmentation 3/49
From last week Logical vs Physical address. Relocation allowed: A process may be swapped in and out of main memory such that it occupies different regions. A process can be broken up into pieces Contiguously not necessary All pieces of a process do not need to be loaded in main memory during execution. All pieces of a process do not have to be in the main memory during execution!! 4/49
Virtual Memory Keep only the active pieces (pages or segments) of the process in main memory. Inactive processes are kept in the secondary memory (hard disk). Advantages More processes can be maintained in the main memory, increases effective use of CPU. The main memory can execute a process bigger than its own capacity. 5/49
Challenges Who is active and who is inactive. Bad swapping results in Trashing. The heuristic principle of locality Expensive interrupt operation to retrieve inactive page/segment. Management complexity Extra hardware support needed The improvement gained by virtual memory is worth the difficulty introduced by it. 6/49
Thrashing Swapping out a piece of a process just before that piece is needed The processor spends most of its time swapping pieces rather than executing user instructions P1 Q2 P2 Q1 Q3 P3 P1 P2 P3 P4 P1 P2 P3 P4 P1 P1 P2 P3 P4 P1 P2 P1 P2 P3 P4 P1 P2 P3 P4 P1 P3 P2 7/49
Principle of Locality Program and data references within a process tend to cluster Only a few pieces of a process will be needed over a short period of time Possible to make intelligent guesses about which pieces will be needed in the future In practice virual memory works!! 8/49
Support Needed for Virtual Memory Hardware must support paging and segmentation Operating system must be able to management the movement of pages and/or segments between secondary memory and main memory 9/49
Paging and Page Tables Each process has its own page table Each page table entry contains the frame number of the corresponding page in main memory A bit is needed to indicate whether the page is in main memory or not 10/49
Modify Bit in Page Table Another modify bit is needed to indicate if the page has been altered since it was last loaded into main memory If no change has been made, the page does not have to be written to the disk when it needs to be swapped out 11/49
Page Table Entries P: page in memory? M: Modify bit Other control bit (OS specific) eg. Ageing... 12/49
Page Translation Virtual address Page number Offset Page table Physical address Frame number Offset Page # Page number P1 P2 P3 P4 Frame # Offset Frame number F4 F5 F10 F11 Offset 13/49
Page Starting bit 14/49
Page Tables The entire page table may take too much main memory space. Specially: With small size pages in 32-bit or higher addressing. 8000 entries in Windows2000 Solution: Store Page tables also in virtual memory When a process is running, part of its page table is in main memory 15/49
Translation Lookaside Buffer Each virtual memory reference can cause two physical memory accesses one to fetch the page table one to fetch the data To overcome this problem a high-speed cache is set up for page table entries called the TLB - Translation Lookaside Buffer Secondary memory TLB Main memory 16/49
Translation Lookaside Buffer Contains page table entries that have been most recently used TLB hit: page number in TLB TLB miss: page number in memory TLB page fault: page number in secondary memory. First load table and do like TLB miss Example: memory cache, proxy 17/49
18/49
19/49
Page Size Internal fragmentation Page table Pages in VM Page importing Small page size Small Large Few Large page size large small more Importing a page costs almost the same regardless of its size 20/49
Page Size Memory size and CPU speed grows, but TLB can not grow at the same rate. Complicate OS operations. Multiple (unequal) page sizes provide the flexibility needed to effectively use a TLB Example: Large pages can be used for program instructions Small pages can be used for threads Most operating system support only one page size. 21/49
Example Page Sizes 22/49
BREAK Segmentation Dynamic size Simplifies handling of growing data structures Process partitioning is easier. Allows programs to be altered and recompiled independently Sharing data among processes is easier Efficient protection 23/49
Segment Table Entries 24/49
Combined Paging and Segmentation Paging is transparent to the programmer Paging eliminates external fragmentation Equal page size, good for replacement policy Segmentation is visible to the programmer Segmentation allows for growing data structures, modularity, and support for sharing and protection Each segment is broken into fixed-size pages (paging after segmentation) 25/49
Combined Segmentation and Paging 26/49
27/49
Operating System Software Fetch Policy Demand and Prepaging Placement Policy Replacement Policy Algorithms Resident Set Management Cleaning Policy Load Control 28/49
Fetch Policy Fetch Policy: Determines when a page should be brought into memory Demand paging only brings pages into main memory when a reference is made to a location on the page Many page faults when process first started Prepaging brings in more pages than needed More efficient to bring in pages that reside contiguously on the disk. Exploit disk rotation latency Swapping follows the same 29/49
Placement Policy Where is a page placed? First-, worst-, best-fit algorithm. Distributed or Parallel computing with several processors accessing the same data. Placement is a serious design issue 30/49
Replacement Policy Issues to consider Avoid trashing How many should be replaced (prepaging?) Should a process replace its own page or not? Which one should be served first Page removed should be the page least likely to be referenced in the near future Most policies predict the future behavior on the basis of past behavior 31/49
Replacement Policy Frame Locking If frame is locked, it may not be replaced Example Kernel of the operating system Control structures I/O buffers Associate a lock bit with each frame 32/49
Basic Replacement Algorithms Optimal policy Selects for replacement that page for which the time to the next reference is the longest Impossible to have perfect knowledge of future events P1 Q2 P2 Q1 Q3 P3 P1 P2 P3 P4 P1 P2 P3 P4 P1 P1 P2 P3 P4 P1 P2 P1 P2 P3 P4 P1 P2 P3 P4 P1 P3 P2 33/49
Basic Replacement Algorithms Least Recently Used (LRU) Replaces the page that has not been referenced for the longest time By the principle of locality, this should be the page least likely to be referenced in the near future Each page could be tagged with the time of last reference (aging). This would require a great deal of overhead. (aging is not a bit) 34/49
Basic Replacement Algorithms First-in, first-out (FIFO) Treats page frames allocated to a process as a circular buffer Pages are removed in round-robin style Simplest replacement policy to implement Page that has been in memory the longest is replaced These pages may be needed again very soon 35/49
Basic Replacement Algorithms Clock Policy Additional bit called a use bit When a page is first loaded in memory, the use bit is set to 0 When the page is referenced, the use bit is set to 1 When it is time to replace a page, the first frame encountered with the use bit set to 0 is replaced. During the search for replacement, each use bit set to 1 is changed to 0 36/49
Replace Page 556 It is the next page with 0 use-bit 37/49
38/49
Resident Set Size Fixed-allocation gives a process a fixed number of pages within which to execute when a page fault occurs, one of the pages of that process must be replaced Variable-allocation number of pages allocated to a process varies over the lifetime of the process Local and Global scope. 39/49
Variable Allocation, Global Scope Easiest to implement Adopted by many operating systems Operating system keeps list of free frames Free frame is added to resident set of process when a page fault occurs If no free frame, replaces one from another process depending on page faults. 40/49
Variable Allocation, Local Scope When new process added, allocate number of page frames based on application type, program request, or other criteria When page fault occurs, select page from among the resident set of the process that suffers the fault Reevaluate allocation from time to time 41/49
Cleaning Policy Demand cleaning a page is written out only when it has been selected for replacement Precleaning pages are written out in batches Compare with fetch policy Demand paging, and Prepaging 42/49
Cleaning Policy Best approach uses page buffering Replaced pages are placed in two lists Modified and unmodified Pages in the modified list are periodically written out in batches later, to save time writing to disk is time consuming. Pages in the unmodified list are either reclaimed if referenced again or lost when its frame is assigned to another page 43/49
UNIX and Solaris Memory Management (Data Structures) Paging System Page table Disk block descriptor Page frame data table Swap-use table 44/49
Data Structures! 45/49
Data Structures 46/49
UNIX and Solaris Memory Management Page Replacement refinement of the clock policy Kernel Memory Allocator Uses different replacement policy for kernel Refinement of buddy system most blocks are smaller than a typical page size 47/49
Windows 2000 Memory Management 4 GB of virtual memory 2 OS 2 user space 1 OS 3 user space for servers W2K Paging Available: page not used. Available for use Reserved: page reserved for future use, but not yet used. Committed: page set aside for OS. 48/49
49/49