Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems I Virtual Memory. Swap out OS P1 P2 Backing Store (Swap Space) Main Memory Address Binding can be fixed or relocatable at runtime Swapping.

Similar presentations


Presentation on theme: "Operating Systems I Virtual Memory. Swap out OS P1 P2 Backing Store (Swap Space) Main Memory Address Binding can be fixed or relocatable at runtime Swapping."— Presentation transcript:

1 Operating Systems I Virtual Memory

2 Swap out OS P1 P2 Backing Store (Swap Space) Main Memory Address Binding can be fixed or relocatable at runtime Swapping F Active processes use more physical memory than system has

3 Swapping F Consider 100K proc, 1MB/s disk, 8ms seek –108 ms * 2 = 216 ms –If used for context switch, want large quantum! F Small processes faster F Pending I/O (DMA) –don’t swap –DMA to OS buffers F Unix uses swapping variant –Each process has “too large” address space –Demand Paging

4 Motivation F Logical address space larger than physical memory –“Virtual Memory” –on special disk F Abstraction for programmer F Performance ok? –Error handling not used –Maximum arrays

5 Demand Paging F Less I/O needed F Less memory needed F Faster response F More users F No pages in memory initially –Pure demand paging Page out A1 B1 Main Memory A2 A3 B2 A1 A3 B1

6 Paging Implementation Page 0Page 1Page 2 1 030 0 1 2 3 Page Table Logical Memory Physical Memory Page 0Page 2 0 1 2 3 v i v i Validation Bit Page 3 3 1 0 2

7 Page Fault F Page not in memory –interrupt OS => page fault F OS looks in table: –invalid reference? => abort –not in memory? => bring it in F Get empty frame (from list) F Swap page into frame F Reset tables (valid bit = 1) F Restart instruction

8 Performance of Demand Paging Page Fault Rate 0 < p < 1.0 (no page faults to every is fault) Effective Access Time = (1-p) (memory access) + p (page fault overhead) Page Fault Overhead = swap page out + swap page in + restart

9 Performance Example F memory access time = 100 nanoseconds F swap fault overhead = 25 msec F page fault rate = 1/1000 F EAT = (1-p) x 100 + p x (25 msec) = (1-p) x 100 + p x 25,000,000 = 100 + 24,999,900 x p = 100 + 24,999,900 x 1/1000 = 25 microseconds! F Want less than 10% degradation 110 > 100 + 24,999,900 x p 10 > 24,999,9000 x p p <.0000004 or 1 fault in 2,500,000 accesses!

10 Page Replacement F Page fault => What if no free frames? –terminate user process (ugh!) –swap out process (reduces degree of multiprog) –replace other page with needed page F Page replacement: –if free frame, use it –use algorithm to select victim frame –write page to disk, changing tables –read in new page –restart process

11 Page Replacement Page 0Page 1Page 2 1 030 0 1 2 3 Page Table Logical Memory Physical Memory Page 0Page 2 0 1 2 3 v i v i Page 3 3 1 0 2 victim (1) (3) (2) Page Table v 1 0 0 1 v i (4) i “Dirty” Bit - avoid page out 2 (0)

12 Page Replacement Algorithms F Every system has its own F Want lowest page fault rate F Evaluate by running it on a particular string of memory references (reference string) and computing number of page faults F Example: 1,2,3,4,1,2,5,1,2,3,4,5

13 First-In-First-Out (FIFO) 1 2 3 3 Frames / Process 1,2,3,4,1,2,5,1,2,3,4,5 4 1 2 5 3 4 9 Page Faults 1 2 3 4 Frames / Process 5 1 2 4 5 10 Page Faults! 4 3 Belady’s Anomaly

14 Optimal F Replace the page that will not be used for the longest period of time vs. 1 2 3 4 Frames / Process 4 6 Page Faults 4 5 1,2,3,4,1,2,5,1,2,3,4,5 How do we know this? Use as benchmark

15 Least Recently Used 1 2 3 5 4 3 1,2,3,4,1,2,5,1,2,3,4,5 F Replace the page that has not been used for the longest period of time 5 4 8 Page Faults No Belady’s Anomoly - “Stack” Algorithm - N frames subset of N+1

16 LRU Implementation F Counter implementation –every page has a counter; every time page is referenced, copy clock to counter –when a page needs to be changed, compare the counters to determine which to change F Stack implementation –keep a stack of page numbers –page referenced: move to top –no search needed for replacement

17 LRU Approximations F LRU good, but hardware support expensive F Some hardware support by reference bit –with each page, initially = 0 –when page is referenced, set = 1 –replace the one which is 0 (no order) –enhance by having 8 bits and shifting –approximate LRU

18 Second-Chance F FIFO replacement, but … –Get first in FIFO –Look at reference bit u bit == 0 then replace u bit == 1 then set bit = 0, get next in FIFO F If page referenced enough, never replaced F Implement with circular queue

19 Second-Chance 1 2 34 1 0 1 1 Next Vicitm 1 2 34 0 0 0 0 (a)(b) If all 1, degenerates to FIFO

20 Enhanced Second-Chance F 2-bits, reference bit and modify bit F (0,0) neither recently used nor modified –best page to replace F (0,1) not recently used but modified –needs write-out F (1,0) recently used but clean –probably used again soon F (1,1) recently used and modified –used soon, needs write-out F Circular queue in each class -- (Macintosh)

21 Counting Algorithms F Keep a counter of number of references –LFU - replace page with smallest count u if does all in beginning, won’t be replaced u decay values by shift –MFU - smallest count just brought in and will probably be used F Not too common (expensive) and not too good

22 Page Buffering F Pool of frames –start new process immediately, before writing old u write out when system idle –list of modified pages u write out when system idle –pool of free frames, remember content u page fault => check pool

23 Allocation of Frames F How many fixed frames per process? F Two allocation schemes: –fixed allocation –priority allocation

24 Fixed Allocation F Equal allocation –ex: 93 frames, 5 procs = 18 per proc (3 in pool) F Proportional Allocation –number of frames proportional to size –ex: 64 frames, s1 = 10, s2 = 127 u f1 = 10 / 137 x 64 = 5 u f2 = 127 / 137 x 64 = 59 F Treat processes equal

25 Priority Allocation F Use a proportional scheme based on priority F If process generates a page fault –select replacement a process with lower priority F “Global” versus “Local” replacement –local consistent (not influenced by others) –global more efficient (used more often)

26 Thrashing F If a process does not have “enough” pages, the page-fault rate is very high –low CPU utilization –OS thinks it needs increased multiprogramming –adds another procces to system F Thrashing is when a process is busy swapping pages in and out

27 Thrashing degree of muliprogramming CPU utilization

28 Cause of Thrashing F Why does paging work? –Locality model u process migrates from one locality to another u localities may overlap F Why does thrashing occur? –sum of localities > total memory size F How do we fix thrashing? –Working Set Model –Page Fault Frequency

29 Working-Set Model F Working set window W = a fixed number of page references –total number of pages references in time T F D = sum of size of W’s

30 Working Set Example F T = 5 F 1 2 3 2 3 1 2 4 3 4 7 4 3 3 4 1 1 2 2 2 1 W={1,2,3} W={3,4,7} W={1,2} –if T too small, will not encompass locality –if T too large, will encompass several localities –if T => infinity, will encompass entire program F if D > m => thrashing, so suspend a process F Modify LRU appx to include Working Set

31 Page Fault Frequency F Establish “acceptable” page-fault rate –If rate too low, process loses frame –If rate too high, process gains frame increase number of frames decrease number of frames upper bound lower bound Page Fault Rate Number of Frames

32 Prepaging F Pure demand paging has many page faults initially –use working set –does cost of prepaging unused frames outweigh cost of page-faulting?

33 Page Size F Old - Page size fixed, New -choose page size F How do we pick the right page size? Tradeoffs: –Fragmentation –Table size –Minimize I/O u transfer small (.1ms), latency + seek time large (10ms) –Locality u small finer resolution, but more faults –ex: 200K process (1/2 used), 1 fault / 200k, 100K faults/1 byte F Historical trend towards larger page sizes –CPU, mem faster proportionally than disks

34 Program Structure F consider: int A[1024][1024]; for (j=0; j<1024; j++) for (i=0; i<1024; i++) A[i][j] = 0; F suppose: –process has 1 frame –1 row per page –=> 1024x1024 page faults!

35 Program Structure int A[1024][1024]; for (i=0; i<1024; i++) for (j=0; j<1024; j++) A[i][j] = 0; F 1024 page faults F stack vs. hash table F Compiler –separate code from data –keep routines that call each other together F LISP (pointers) vs. Pascal (no-pointers)

36 Priority Processes F Consider –low priority process faults, u bring page in –low priority process in ready queue for awhile, waiting while high priority process runs –high priority process faults u low priority page clean, not used in a while => perfect! F Lock-bit (like for I/O) until used once

37 Real-Time Processes F Real-time –bounds on delay –hard-real time: systems crash, lives lost u air-traffic control, factor automation –soft-real time: application sucks u audio, video F Paging adds unexpected delays –don’t do it –lock bits for real-time processes

38 Virtual Memory and WinNT F Page Replacement Algorithm –FIFO –Missing page, plus adjacent pages F Working set –default is 30 –take victim frame periodically –if no fault, reduce set size by 1 F Reserve pool –hard page faults –soft page faults

39 Virtual Memory and WinNT F Shared pages –level of indirection for easier updates –same virtual entry F Page File –stores only modified logical pages –code and memory mapped files on disk already

40 Virtual Memory and Linux F Regions of virtual memory –paging disk (normal) –file (text segment, memory mapped file) F New Virtual Memory –exec() creates new page table –fork() copies page table u reference to common pages u if written, then copied F Page Replacement Algorithm –second chance (with more bits)

41 Application Performance Studies and Demand Paging in Windows NT Mikhail Mikhailov Ganga Kannan Mark Claypool David Finkel WPI Saqib Syed Divya Prakash Sujit Kumar BMC Software, Inc.

42 Capacity Planning Then and Now F Capacity Planning in the good old days –used to be just mainframes –simple CPU-load based queuing theory –Unix F Capacity Planning today –distributed systems –networks of workstations –Windows NT –MS Exchange, Lotus Notes

43 Experiment Design  System –Pentium 133 MHz –NT Server 4.0 –64 MB RAM –IDE NTFS  clearmem F Experiments –Page Faults –Caching F Analysis –perfmon

44 Page Fault Method F “Work hard” F Run lots of applications, open and close F All local access, not over network

45 Soft or Hard Page Faults?

46 Caching and Prefetching F Start process –wait for “Enter” F Start perfmon F Hit “Enter” F Read 1 4-K page F Exit F Repeat

47 Page Metrics with Caching On


Download ppt "Operating Systems I Virtual Memory. Swap out OS P1 P2 Backing Store (Swap Space) Main Memory Address Binding can be fixed or relocatable at runtime Swapping."

Similar presentations


Ads by Google