CS 241 Fall 2007 System Programming 1 Virtual Memory Lawrence Angrave and Vikram Adve.

Slides:



Advertisements
Similar presentations
CS 241 Spring 2007 System Programming 1 Memory Replacement Policies Lecture 32 Klara Nahrstedt.
Advertisements

Page Replacement Algorithms
Virtual Memory (Chapter 4.3)
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Virtual Memory: Page Replacement
Paging: Design Issues. Readings r Silbershatz et al: ,
Chapter 8 Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
CS 241 Spring 2007 System Programming 1 Memory Implementation Issues Lecture 33 Klara Nahrstedt.
Copyright ©: Lawrence Angrave, Vikram Adve, Caccamo 1 Virtual Memory III.
Allocation of Frames Each process needs minimum number of pages
Page 15/4/2015 CSE 30341: Operating Systems Principles Allocation of Frames  How should the OS distribute the frames among the various processes?  Each.
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
Chapter 101 Cleaning Policy When should a modified page be written out to disk?  Demand cleaning write page out only when its frame has been selected.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 15: Background Information for the VMWare ESX Memory Management.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Virtual Memory Chapter 8.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
1 Virtual Memory vs. Physical Memory So far, all of a job’s virtual address space must be in physical memory However, many parts of programs are never.
Paging Algorithms Vivek Pai / Kai Li Princeton University.
Memory Management 2010.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
Virtual Memory Chapter 8.
1 Virtual Memory Chapter 8. 2 Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Computer Organization and Architecture
1 Virtual Memory Management B.Ramamurthy Chapter 10.
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Virtual Memory.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
1 Memory Management 4.1 Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement algorithms.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
1 Virtual Memory Chapter 8. 2 Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Copyright ©: Lawrence Angrave, Vikram Adve, Caccamo 1 Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
1 Lecture 8: Virtual Memory Operating System Fall 2006.
NETW3005 Virtual Memory. Reading For this lecture, you should have read Chapter 9 (Sections 1-7). NETW3005 (Operating Systems) Lecture 08 - Virtual Memory2.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Virtual Memory.
Virtual Memory Chapter 8.
CS161 – Design and Architecture of Computer
OPERATING SYSTEM CONCEPTS AND PRACTISE
CS161 – Design and Architecture of Computer
Chapter 8 Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
Page Replacement.
What Happens if There is no Free Frame?
5: Virtual Memory Background Demand Paging
Chapter 9: Virtual Memory
Module 9: Virtual Memory
COMP755 Advanced Operating Systems
Chapter 9: Virtual Memory CSS503 Systems Programming
Virtual Memory.
CSE 542: Operating Systems
Presentation transcript:

CS 241 Fall 2007 System Programming 1 Virtual Memory Lawrence Angrave and Vikram Adve

2 Contents  Memory mapped files  Page sharing  Page protection  Virtual Memory and Multiprogramming – Page eviction policies – Page frame allocation and thrashing – Working set model and implementation

3 Memory Mapped Files Memory Mapped File In Blocks VM of User Mmap requests Disk File Blocks of data From file mapped To VM

4 Uses of Memory Mapped Files Dynamic loading. By mapping executable files and shared libraries into its address space, a program can load and unload executable code sections dynamically. Fast File I/O. When you call file I/O functions, such as read() and write(), the data is copied to a kernel's intermediary buffer before it is transferred to the physical file or the process. This intermediary buffering is slow and expensive. Memory mapping eliminates this intermediary buffering, thereby improving performance significantly.

5 Uses of Memory Mapped Files Streamlining file access. Once you map a file to a memory region, you access it via pointers, just as you would access ordinary variables and objects. Memory sharing. Memory mapping enables different processes to share physical memory pages Memory persistence. Memory mapping enables processes to share memory sections that persist independently of the lifetime of a certain process.

6 POSIX caddr_t mmap( caddress_t map_addr, /* VM address hint; 0 for no preference */ size_t length, /* Length of file map*/ int protection,/* types of access*/ int flags,/*attributes*/ int fd,/*file descriptor*/ off_t offset);/*Offset file map start*/

7 Protection Attributes PROT_READ /* the mapped region may be read */ PROT_WRITE /* the mapped region may be written */ PROT_EXEC /* the mapped region may be executed */ SIGSEGV signal if you reference memory with wrong protection mode.

8 Map first 4kb of file and read int #include int main(int argc, char *argv[]) { int fd; void * pregion; if (fd= open(argv[1], O_RDONLY) <0) { perror("failed on open"); return –1; }

9 Map first 4kb of file and read int /*map first 4 kilobytes of fd*/ pregion =mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0); if (pregion==(caddr_t)-1) { perror("mmap failed") return –1; } close(fd); /*close physical file: we don't need it */ /* access mapped memory; read the first int in the mapped file */ int val= *((int*) pregion); }

10 munmap, msync int munmap(caddr_t addr, int length); int msync (caddr_t addr, size_t length, int flags); addr must be multiple of page size: size_t page_size = (size_t) sysconf (_SC_PAGESIZE);

11 Sharing Pages  Code and data can be shared – Map common page frame in two processes  Code, data must be position-independent – VM mappings for same code, data in different processes are different

12 Shared Pages

13 Protection Why Page Protection? Implementing Page Protection  Read, Write, eXecute bits in page table entry  Check is done by hardware during access – Illegal access generates SIGSEGV  Each process can have different protection bits

14 Page Protection via PTE Legend: Reference - page has been accessed Valid - page exists Resident - page is cached in primary memory Dirty - page has been changed since page in

15 Virtual Memory Under Multiprogramming Eviction of Virtual Pages  On page fault: Choose VM page to page out  How to choose which data to page out? Allocation of Physical Page Frames  How to assign frames to processes?

16 Terminology  Reference string: memory reference sequence generated by a program: – Reference = (R/W, address)  Paging – moving pages to or from disk  Demand Paging – moving pages only when needed  Optimal – the best (theoretical) strategy  Eviction – throwing something out  Pollution – bringing in useless pages/lines 

17 Issue: Eviction  Hopefully, kick out a less-useful page – Dirty pages require writing, clean pages don’t  Goal: kick out the page that’s least useful  Problem: how do you determine utility? – Heuristic: temporal locality exists – Kick out pages that aren’t likely to be used again

18 Page Replacement Strategies  The Principle of Optimality – Replace page that will be used the farthest in the future.  Random page replacement – Choose a page randomly  FIFO - First in First Out – Replace the page that has been in primary memory the longest  LRU - Least Recently Used – Replace the page that has not been used for the longest time  LFU - Least Frequently Used – Replace the page that has been used least often  NRU - Not Recently Used – An approximation to LRU.  Working Set – Keep in memory those pages that the process is actively using.

19 Principal of Optimality  Description: – Assume each page can be labeled with number of references that will be executed before that page is first referenced. – Then the optimal page algorithm would choose the page with the highest label to be removed from the memory.  Impractical! Why?  Provides a basis for comparison with other schemes.  If future references are known – should not use demand paging – should use “pre-paging” to overlap paging with computation.

20 Frame Allocation for Multiple Processes  How are the page frames allocated to individual virtual memories of the various jobs running in a multi-programmed environment?  Simple solution – Allocate a minimum number (??) of frames per process.  One page from the current executed instruction  Most instructions require two operands  include an extra page for paging out and one for paging in

21 Multi-Programming Frame Allocation  Solution 2 – allocate an equal number of frames per job  but jobs use memory unequally  high priority jobs have same number of page frames as low priority jobs  degree of multiprogramming might vary  Solution 3: – allocate a number of frames per job proportional to job size  how do you determine job size: by run command parameters or dynamically?

22 Multi-Programming Frame Allocation  Why is multi-programming frame allocation is important? – If not solved appropriately, it will result in a severe problem--- Thrashing

23 Thrashing  Thrashing: As page frames per VM space decrease, the page fault rate increases.  – Each time one page is brought in, another page, whose contents will soon be referenced, is thrown out. – Processes will spend all of their time blocked, waiting for pages to be fetched from disk – I/O devs at 100% utilization but system not getting much useful work done – Memory and CPU mostly idle Real mem P1 P2 P3

24 Page Fault Rate vs. Size Curve

25 Why Thrashing?  Computations have locality  As page frames decrease, the page frames available are not large enough to contain the locality of the process.  The processes start faulting heavily – Pages that are read in, are used and immediately paged out.

26 Results of Thrashing

27 Why?  As the page fault rate goes up, processes get suspended on page out queues for the disk.  The system may start new jobs.  Starting new jobs will reduce the number of page frames available to each process, increasing the page fault requests.  System throughput plunges.

28 Solution: Working Set  Main idea – figure out how much memory a process needs to keep most of its recent computation in memory with very few page faults  How? – The working set model assumes locality – the principle of locality states that a program clusters its access to data and text in time  Recently accessed page is more likely to be accessed again than less recently accessed page – Thus, as the number of page frames increases above some threshold, the page fault rate will drop dramatically

29 Working set (1968, Denning)  What we want to know: collection of pages process must have in order to avoid thrashing – This requires knowing the future. And our trick is?  Working set: – Pages referenced by process in last  seconds of execution considered to comprise its working set –  : the working set parameter  Usages of working set sizes? – Cache partitioning: give each app enough space for WS – Page replacement: preferentially discard non-WS pages – Scheduling: process not executed unless WS in memory

30 Working Set At least allocate this many frames for this process

31 Calculating Working Set 12 references, 8 faults Window size is 

32 Working Set in Action to Prevent Thrashing  Algorithm  if #free page frames > working set of some suspended process i, then activate process i and map in all its working set  if working set size of some process k increases and no page frame is free, suspend process k and release all its pages

33 Working sets of real programs  Typical programs have phases Working set size transition, stable Sum of both

34 Working Set Implementation Issues  Moving window over reference string used for determination  Keeping track of working set

35 Page Fault Frequency Working Set  Approximation of pure working set – Assume that if the working set is correct there will not be many page faults. – If page fault rate increases beyond assumed knee of curve, then increase number of page frames available to process. – If page fault rate decreases below foot of knee of curve, then decrease number of page frames available to process.

36 Page Fault Frequency Working Set

37 Summary  Memory mapped files  Page sharing  Page protection  Virtual Memory and Multiprogramming – Page eviction policies – Page frame allocation and thrashing – Working set model and implementation