Lesson 6: Memory Management & Virtualization

Similar documents
Memory Management Outline. Background Swapping Contiguous Memory Allocation Paging Segmentation Segmented Paging

The Deadlock Problem. Deadlocks. Deadlocks. Bridge Crossing Example

Memory unit sees only the addresses, and not how they are generated (instruction counter, indexing, direct)

Memory Management 1. Memory Management. Multitasking without memory management is like having a party in a closet.

Memory management basics (1) Requirements (1) Objectives. Operating Systems Part of E1.9 - Principles of Computers and Software Engineering

OPERATING SYSTEM - VIRTUAL MEMORY

Operating Systems, 6 th ed. Test Bank Chapter 7

Operating Systems. Virtual Memory

Virtual Memory. Virtual Memory. Paging. CSE 380 Computer Operating Systems. Paging (1)

Chapter 12. Paging an Virtual Memory Systems

Virtual vs Physical Addresses

W4118: segmentation and paging. Instructor: Junfeng Yang

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

Chapter 7 Memory Management

Lecture 17: Virtual Memory II. Goals of virtual memory

OPERATING SYSTEM - MEMORY MANAGEMENT

Virtual Memory Paging

CS 61C: Great Ideas in Computer Architecture Virtual Memory Cont.

OPERATING SYSTEMS MEMORY MANAGEMENT

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

COS 318: Operating Systems. Virtual Memory and Address Translation

Chapter 6, The Operating System Machine Level

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

Computer Architecture

Chapter 7 Memory Management

Chapter 11 I/O Management and Disk Scheduling

Board Notes on Virtual Memory

Chapter 11: File System Implementation. Operating System Concepts with Java 8 th Edition

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

Chapter 11 I/O Management and Disk Scheduling

Computer-System Architecture

Chapter 12 File Management

Chapter 12 File Management. Roadmap

Chapter 12 File Management

System Virtual Machines

Chapter 1 13 Essay Question Review

Operating Systems. Steven Hand. Michaelmas / Lent Term 2008/ lectures for CST IA. Handout 3. Operating Systems N/H/MWF@12

HY345 Operating Systems

Lesson Objectives. To provide a grand tour of the major operating systems components To provide coverage of basic computer system organization

We r e going to play Final (exam) Jeopardy! "Answers:" "Questions:" - 1 -

Chapter 11: File System Implementation. Chapter 11: File System Implementation. Objectives. File-System Structure

Operating Systems CSE 410, Spring File Management. Stephen Wagner Michigan State University

1. Computer System Structure and Components

Chapter 1 Computer System Overview

Operating System Overview. Otto J. Anshus

Peter J. Denning, Naval Postgraduate School, Monterey, California

1 File Management. 1.1 Naming. COMP 242 Class Notes Section 6: File Management

Operating Systems. Lecture 03. February 11, 2013

Virtualization in Linux KVM + QEMU

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

W4118 Operating Systems. Instructor: Junfeng Yang

How To Write A Page Table

CHAPTER 17: File Management

CSC 2405: Computer Systems II

COS 318: Operating Systems

Virtualization. Explain how today s virtualization movement is actually a reinvention

Virtual Machines. COMP 3361: Operating Systems I Winter

Midterm Exam #2 Solutions November 10, 1999 CS162 Operating Systems

The Classical Architecture. Storage 1 / 36

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Memory Management CS 217. Two programs can t control all of memory simultaneously

How To Write A Virtual Machine (Or \"Virtual Machine\") On A Microsoft Linux Operating System (Or Microsoft) (Or Linux) ( Or Microsoft Microsoft Operating System) (For A Non-Power Os) (On A

Operating System Tutorial

CS5460: Operating Systems

Technical Properties. Mobile Operating Systems. Overview Concepts of Mobile. Functions Processes. Lecture 11. Memory Management.

Operating Systems 4 th Class

File Management. Chapter 12

This Unit: Virtual Memory. CIS 501 Computer Architecture. Readings. A Computer System: Hardware

CHAPTER 6 TASK MANAGEMENT

Chapter 3 Operating-System Structures

COS 318: Operating Systems

OS OBJECTIVE QUESTIONS

Presentation of Diagnosing performance overheads in the Xen virtual machine environment

Storage in Database Systems. CMPSCI 445 Fall 2010

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

CS5460: Operating Systems. Lecture: Virtualization 2. Anton Burtsev March, 2013

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

ELEC 377. Operating Systems. Week 1 Class 3

Segmentation Segmentation: Generalized Base/Bounds

361 Computer Architecture Lecture 14: Cache Memory

Storing Data: Disks and Files. Disks and Files. Why Not Store Everything in Main Memory? Chapter 7

Full and Para Virtualization

Chapter 11: File System Implementation. Operating System Concepts 8 th Edition

Kernel Optimizations for KVM. Rik van Riel Senior Software Engineer, Red Hat June

An Implementation Of Multiprocessor Linux

Memory management. Chapter 4: Memory Management. Memory hierarchy. In an ideal world. Basic memory management. Fixed partitions: multiple programs

Virtualization. Dr. Yingwu Zhu

Physical Data Organization

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

How To Write To A Linux Memory Map On A Microsoft Zseries (Amd64) On A Linux (Amd32) (

Chapter 2 Basic Structure of Computers. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

The Oracle Universal Server Buffer Manager

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

Virtual Memory Behavior in Red Hat Linux Advanced Server 2.1

Chapter 2: Computer-System Structures. Computer System Operation Storage Structure Storage Hierarchy Hardware Protection General System Architecture

Outline: Operating Systems

Transcription:

Lesson 6: Memory Management & Virtualization Contents Dynamic allocation, Memory fragmentation Paging Address translation with paging Paging hardware Page table structures Segmentation Virtual memory background Demand Paging Page Replacement Allocation of Frames Thrashing Working sets AE3B33OSD Lesson 6 / Page 2

Holes in Memory Space Contiguous allocation produces holes Holes are produced also by other allocation strategies OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 0 process 2 process 2 process 2 process 2 AE3B33OSD Lesson 6 / Page 3 Dynamic Storage-Allocation Problem How to satisfy a request of size n from a list of free holes First-fit: Allocate the first hole that is big enough Fastest method Best-fit: Allocate the smallest hole that is big enough; Must search entire list, unless ordered by size. Produces the smallest leftover hole Good storage utilization Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole. Low storage fragmentation AE3B33OSD Lesson 6 / Page 4

Fragmentation External Fragmentation Total free memory space exists to satisfy a request, but it is not contiguous and contiguous space is required Internal Fragmentation Memory is allocated using some fixed size memory partitions Allocation size often defined by hardware Allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used A compromise is needed: Large partitions too high internal fragmentation Small partitions too many of partitions to administer Reduce external fragmentation by compaction Shuffle memory contents to place all free memory together in one large block Compaction is possible only if relocation is dynamic, and is done at execution time I/O problem Latch job in memory while it is involved in I/O Do I/O only into OS buffers AE3B33OSD Lesson 6 / Page 5 Paging Contiguous logical address space of a process can be mapped to noncontiguous physical allocation process is allocated physical memory whenever the latter is available Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 52 bytes and 892 bytes) Divide logical memory into blocks of same size called pages Keep track of all free frames To run a program of size n pages, need to find n free frames and load program Set up a page table to translate logical to physical addresses Internal fragmentation may occur AE3B33OSD Lesson 6 / Page 6

Paging Address Translation Principle Physical addresses AE3B33OSD Lesson 6 / Page 7 Address Translation Scheme Address generated by CPU is divided into: Page number (p) used as an index into a page table which contains base address of each page in physical memory Page offset (d) combined with base address to define the physical memory address that is sent to the memory unit AE3B33OSD Lesson 6 / Page 8

Implementation of Page Table Paging is implemented in hardware Page table is kept in main memory Page-table base register (PTBR) points to the page table Page-table length register (PTLR) indicates size of the page table In this scheme every data/instruction access requires two memory accesses. One for the page table and one for the data/instruction. The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs) AE3B33OSD Lesson 6 / Page 9 Associative Memory Associative memory parallel search VERY COMPLEX CIRCUITRY Page # Frame # Address translation (P# F#) If P# is in associative register, get F# out Otherwise get F# from page table in memory AE3B33OSD Lesson 6 / Page 0

Paging Hardware With TLB AE3B33OSD Lesson 6 / Page Paging Properties Effective Access Time with TLB Associative Lookup = ε time unit Assume memory cycle time is microsecond Hit ratio percentage of times that a page number is found in the associative registers; ration related to number of associative registers, Hit ratio = α Effective Access Time (EAT) EAT = ( + ε) α + (2 + ε)( α) = 2 + ε α Memory protection implemented by associating protection bit with each page Usually two bits: Read-Only bit, Dirty bit (used as described later) Valid-invalid bit attached to each entry in the page table: valid indicates that the associated page is in the process logical address space, and is thus a legal page invalid indicates that the page is not in the process logical address space AE3B33OSD Lesson 6 / Page 2

Valid (v) or Invalid (i) Bit In A Page Table AE3B33OSD Lesson 6 / Page 3 Hierarchical Paging Hashed Page Tables Inverted Page Tables Page Table Structures AE3B33OSD Lesson 6 / Page 4

Hierarchical Page Tables Break up the logical address space into multiple page tables A simple technique is a two-level page table A logical address (on 32-bit machine with 4K page size) is divided into: a page number consisting of 20 bits an offset within page consisting of 2 bits The page table itself can also be paged, the page number can be further divided into: a 0-bit page number a 0-bit page offset Thus, a logical address is: page number offset in page p p 2 d 0 0 2 where p i is an index into the outer page table, and p 2 is the displacement within the page of the outer page table AE3B33OSD Lesson 6 / Page 5 Two-Level Page-Table Scheme.................................... Two-level paging structure Logical physical address translation scheme AE3B33OSD Lesson 6 / Page 6

Hashed Page Tables Common for address spaces > 32 bits The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location. Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted. AE3B33OSD Lesson 6 / Page 7 Inverted Page Table One entry for each frame of real memory Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs Main advantage: only one PT for all processes Use hash table to limit the search to one or at most a few page-table entries AE3B33OSD Lesson 6 / Page 8

Shared Pages Shared code One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems). Shared code must appear in same location in the logical address space of all processes Private code and data Each process keeps a separate copy of the code and data The pages for the private code and data can appear anywhere in the logical address space Code Code2 Code3 Data PT 3 4 6 Process Code Code2 Code3 Data2 PT 3 4 6 7 Process 2 Code Code2 Code3 Data3 PT 3 4 6 2 Process 3 0 2 3 4 5 6 7 8 9 0 Data Data3 Code Code2 Code3 Data2 Three instances of a program AE3B33OSD Lesson 6 / Page 9 Segmentation Memory-management scheme that supports user view of memory A program is a collection of segments. A segment is a logical unit such as: main program, procedure, function, method, object, local variables, global variables, common block, stack, symbol table, arrays AE3B33OSD Lesson 6 / Page 20

User s View of a Program AE3B33OSD Lesson 6 / Page 2 Logical View of Segmentation 4 2 3 4 2 3 user space = two dimensional view physical memory space = one dimensional structure AE3B33OSD Lesson 6 / Page 22

Segmentation Architecture Logical address consists of a couple: <segment-number, offset>, Segment table maps two-dimensional physical addresses; each table entry has: base contains the starting physical address where the segments reside in memory limit specifies the length of the segment Segment-table base register (STBR) points to the segment table s location in memory Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR AE3B33OSD Lesson 6 / Page 23 Segmentation Architecture (Cont.) Relocation dynamic by segment table Sharing shared segments same segment number Allocation. first fit/best fit external fragmentation Protection. With each entry in segment table associate: validation bit = 0 illegal segment read/write/execute privileges Protection bits associated with segments; code sharing occurs at segment level Since segments vary in length, memory allocation is a dynamic storage-allocation problem A segmentation example is shown in the following diagram AE3B33OSD Lesson 6 / Page 24

Address Translation Architecture AE3B33OSD Lesson 6 / Page 25 Example of Segmentation AE3B33OSD Lesson 6 / Page 26

Sharing of Segments AE3B33OSD Lesson 6 / Page 27 Segmentation with Paging Intel IA32 IA32 architecture uses segmentation with paging for memory management with a two-level paging scheme AE3B33OSD Lesson 6 / Page 28

Linux on Intel IA32 Uses minimal segmentation to keep memory management implementation more portable Uses 6 segments: Kernel code Kernel data User code (shared by all user processes, using logical addresses) User data (likewise shared) Task-state (per-process hardware context) LDT Uses 2 protection levels: Kernel mode User mode AE3B33OSD Lesson 6 / Page 29 Virtual Memory Background Virtual memory separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical address space can therefore be much larger than physical address space. Allows address spaces to be shared by several processes. Allows for more efficient process creation. Virtual memory can be implemented via: Demand paging Demand segmentation AE3B33OSD Lesson 6 / Page 30

If Virtual Memory is Larger Than Physical Memory AE3B33OSD Lesson 6 / Page 3 Shared Library Using Virtual Memory AE3B33OSD Lesson 6 / Page 32

Demand Paging Bring a page into memory only when it is needed Less I/O needed Less memory needed Faster response More users Page is needed reference to it invalid reference abort not-in-memory bring to memory AE3B33OSD Lesson 6 / Page 33 Valid-Invalid Bit With each page table entry a valid invalid bit is associated ( in-memory, 0 not-in-memory) Initially valid invalid but is set to 0 on all entries Example of a page table snapshot: Frame # M valid-invalid bit 0 During address translation, if valid invalid bit in page table entry is 0 page fault page table 0 0 AE3B33OSD Lesson 6 / Page 34

Page Table when Some Pages are Not in Main Memory AE3B33OSD Lesson 6 / Page 35 Page Fault. If there is a reference to a page with invalid bit, it will trap to OS page fault 2. OS looks at another table (usually in PCB) to decide if: Really invalid reference abort the offending process Just page not in memory 3. Find a free frame 4. Swap-in page into frame 5. Modify page table, validation bit = 6. Restart instruction Pure demand paging: Prepare the process memory image in the disk swap area Start process with all pages invalid AE3B33OSD Lesson 6 / Page 36

Steps in Handling a Page Fault AE3B33OSD Lesson 6 / Page 37 What Happens if There is no Free Frame? Page replacement find some page in memory, but not really in use, swap it out (if needed) Which page? (algorithm) Performance want an algorithm which will result in minimum number of page faults Consequence: Same page may have to be brought into memory many times AE3B33OSD Lesson 6 / Page 38

Performance of Demand Paging Page Fault Rate 0 p.0 if p = 0 no page faults if p =, every reference is a fault Effective Access Time (EAT) EAT = ( p) * memory access + p * ( page fault overhead + [swap page out] + swap page in + restart overhead ) Example: Memory access time = µsec 50% of the time the page that is being replaced has been modified and therefore needs to be swapped out Swap Page Time = 0 msec = 0,000 µsec EAT = ( p) * + p * (5,000) = + 4,999 p [in µsec] AE3B33OSD Lesson 6 / Page 39 Process Creation Virtual memory allows other benefits during process creation: Copy-on-Write Memory-Mapped Files (later) POSIX fork() very inefficient Need to duplicate (copy) all process memory space Not necessary with virtual memory Copy-on-Write (COW) Allows both parent and child processes to initially share the same pages in memory. If either process modifies a shared page, only then is the page copied COW allows more efficient process creation as only modified pages are copied Free pages are allocated from a pool of zeroed-out pages AE3B33OSD Lesson 6 / Page 40

Page Replacement Prevent over-allocation of memory by modifying page-fault service routine to include page replacement Use modify (dirty) bit to reduce overhead of page transfers only modified pages are swapped-out (written) to disk Page replacement completes separation between logical memory and physical memory large virtual memory can be provided on a smaller physical memory. Find the location of the desired page on disk 2. Find a free frame: If there is a free frame, use it If there is no free frame, use a page replacement algorithm to select a victim frame 3. Read the desired page into the (newly) free frame. Update the page and frame tables. 4. Restart the process AE3B33OSD Lesson 6 / Page 4 Page Replacement AE3B33OSD Lesson 6 / Page 42

Page Replacement Algorithms Want lowest page-fault rate Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string In all our examples, the reference string is, 2, 3, 4,, 2, 5,, 2, 3, 4, 5 First-In-First-Out (FIFO) Algorithm 3 frames 4 frames 4 5 5 4 2 2 3 9 page faults 2 2 5 0 page faults 3 3 2 4 3 3 2 4 4 3 FIFO Replacement more frames more page faults - Belady s Anomaly AE3B33OSD Lesson 6 / Page 43 The Optimal Algorithm Replace page that will not be used for longest period of time in future fiction 4 frames example, 2, 3, 4,, 2, 5,, 2, 3, 4, 5 4 frames 2 4 6 page faults 3 4 5 How do we know this? We cannot Nothing better exists Used to measure how well our algorithm performs AE3B33OSD Lesson 6 / Page 44

Least Recently Used (LRU) Algorithm Approximation to optimal algorithm Reference string:, 2, 3, 4,, 2, 5,, 2, 3, 4, 5 5 2 3 5 4 3 4 7 page faults Time-stamp implementation Every page entry has a time-stamp field; every time the page is referenced, copy the system clock into this field Time demanding Page-table grows When a page needs to be changed, look at the time-stamps to determine which is the victim candidate and select the one with the oldest time-stamp Expensive search AE3B33OSD Lesson 6 / Page 45 LRU Algorithm (Cont.) Stack implementation keep a stack of page numbers in a double linked list: Page referenced: move it to the top requires 6 pointers to be changed No search for replacement Difficult house-keeping on every page reference Should be done by hardware for efficiency Conclusion: Both Time-stamp and Stack implementations are too complicated AE3B33OSD Lesson 6 / Page 46

LRU Approximation Algorithms Reference (access) bit (a-bit) With each page associate an a-bit, on swap-in set a=0 When page is referenced set a= Replace the page with a == 0 (if one exists) Cheap, but we do not know the proper page search order Second chance (Clock replacement) Uses the a-bit, list of swapped-in pages and a clock face Every page reference adds a life, every clock-hand pointing removes a life Victim is the page pointed by the hand that has no life Example On page-fault the page pointed by the clock hand is examined if a==0 make this page the victim if a== then a=0, move the hand and examine the next pointed page The numeric simulations show very good approximation of the real LRU AE3B33OSD Lesson 6 / Page 47 Second-Chance (Clock) Page-Replacement Algorithm AE3B33OSD Lesson 6 / Page 48

Allocation of Frames Each process needs some minimum number of pages to execute Example: IBM 370 6 pages to handle MOVE from, to instruction: instruction is 6 bytes might span 2 pages 2 pages to handle from 2 pages to handle to 3 page faults can occur for this single instruction! Two major allocation schemes Fixed allocation Priority allocation AE3B33OSD Lesson 6 / Page 49 Allocation Strategies Equal allocation For example, if there are 00 frames and 5 processes, give each process 20 frames. Proportional allocation 2 processes example Allocate according to the size of process s S a Priority Allocation Use a proportional allocation scheme using priorities rather than size If process p i i i = size of process = s i m = total number of frames = allocation for p p i i si = m S m = 64 = 0 = 27 0 = 64 5 37 27 = 64 59 37 generates a page fault, select for replacement one of its frames, or select for replacement a frame from a process with lower priority s s a a 2 2 AE3B33OSD Lesson 6 / Page 50

Global vs. Local Allocation Global replacement process selects a replacement frame from the set of all frames one process can take a frame from another Local replacement each process selects from only its own set of allocated frames AE3B33OSD Lesson 6 / Page 5 Memory Thrashing If a process does not have enough pages, the page-fault rate is very high. This leads to: low CPU utilization operating system thinks that it may increase the degree of multiprogramming (number of processes) Thrashing a process is busy swapping pages in and out and does not compute Why does demand paging work properly? Locality model Process migrates from one locality to another Localities may overlap Why does thrashing occur? Σ size of locality > total memory size = m AE3B33OSD Lesson 6 / Page 52

Working-Set Model working-set window a fixed number of page references Example: 0,000 instructions WSS i (working set of process p i ) = total number of pages referenced in the most recent (varies in time) if small will not encompass entire locality if large will encompass several localities if will encompass entire program D = Σ WSS i total demanded frames if D > m Thrashing Policy: if D > m, then suspend one or more processes AE3B33OSD Lesson 6 / Page 53 Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: = 0,000 Timer interrupts after every 5000 memory references Keep in memory 2 indicator bits for each page Whenever a timer interrupts copy reference bit to indicators and reset the values of all reference bits If one of the bits in memory == assume page in working set Better accuracy: 0 bits and interrupt every 000 memory refs Very expensive AE3B33OSD Lesson 6 / Page 54

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 AE3B33OSD Lesson 6 / Page 55 Prepaging Prepaging To reduce the large number of page faults that occurs at process startup Prepage all or some of the pages a process will need, before they are referenced But if prepaged pages are unused, I/O and memory was wasted Assume s pages are prepaged and α of the pages is used Is cost of s * α save pages faults > or < than the cost of prepaging s * (-α) unnecessary pages? α near zero prepaging loses AE3B33OSD Lesson 6 / Page 56

Virtual Memory & Program Structure Program structure double[52][52] data; Each row is stored in one 4 KB page Program for (j = 0; j <52; j++) for (i = 0; i < 52; i++) data[i][j] = 0; Potentially 52 x 52 = 262,44 page faults Program 2 for (i = 0; i < 52; i++) for (j = 0; j < 52; j++) data[i][j] = 0; 52 page faults Good to know how data is stored AE3B33OSD Lesson 6 / Page 57 Windows XP Uses demand paging with clustering. Clustering swaps in pages surrounding the faulting page. Processes are assigned working set minimum and working set maximum Working set minimum is the minimum number of pages the process is guaranteed to have in memory A process may be assigned as many pages up to its working set maximum When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory Working set trimming removes pages from processes that have pages in excess of their working set minimum AE3B33OSD Lesson 6 / Page 58

End of Lesson 6