CPSC 410 / 611 : Operating Systems CPSC 410/ 611: “Week 7” • Vir t ual Memor y • Reading: Silber shat z, Chapt er 9 • Vir t ual Memor y, MI PS-St yle – pr epar at ion f or MP3 Virt ual Memory • Over view / Mot ivat ion • Simple Approach: Overlays • Localit y of Ref erence • Demand P aging • Policies – P lacement – Replacement – Allocat ion • Case St udies: Unix Syst emV • Reading: Silberschat z, Chapt er 9 1
CPSC 410 / 611 : Operating Systems Virt ual Memory • Allow execut ion of processes t hat may not be complet ely in memory. – 1990: Run dBaseI V on MS/ DOS wit hout expanded memory. – 1995: Run X and Net scape on a Sun wit h 12MB memory. – 2004: Run RiseOf Nat ions on Windows box wit h 128MB memory. • Benef it s: – Program size not const rained by amount of physical memory available. – More programs can be run simult aneously – Less need f or swapping Virt ual Memory at it s Simplest : Overlays • Keep in memory only t hose common inst ruct ions and dat a t hat are needed dat a at any given t ime. • Special relocat ion and linking needed common t o const r uct over lays. rout ines • Don’t need special support f rom OS. overlay driver • Require proper design of overlay st r uct ur e. overlay overlay overlay 1 2 3 2
CPSC 410 / 611 : Operating Systems Demand Paging • “Lazy Swapper”: only swap in pages t hat are needed. • Whenever CPU t ries t o access a page t hat is not swapped in, a page f ault occur s. 0 1 2 3 0 A 4 A 0 4 v 1 B 5 1 i 2 C 2 A B 10 v 6 3 i 3 D 7 C D E F 4 i 4 E 5 8 F 8 v 5 F 6 i 9 7 6 i G 10 C 7 H 11 page backing st ore logical t able 12 memory physical memor y Localit y of Ref erence • Page f ault s are expensive! • Thrashing : Process spends most of t he t ime paging in and out inst ead of execut ing code. • Most programs display a pat t ern of behavior called t he principle of locality of ref erence . • Localit y of ref erence: A program t hat ref erences a locat ion n at some A program t hat ref erences a locat ion n at some point in t ime is likely t o ref erence t he same point in t ime is likely t o ref erence t he same locat ion n and locat ions in t he immediat e locat ion n and locat ions in t he immediat e vicinit y of n in t he near f ut ure. vicinit y of n in t he near f ut ure. 3
CPSC 410 / 611 : Operating Systems Memory Access Trace Mechanics of a Page Fault page is on backing st ore 3 OS OS t r ap 2 ref erence 1 i CPU i CPU 6 r est ar t inst ruct ion page t able 4 f r ee f r ee 5 f rame f rame load page updat e page t able physical memory 4
CPSC 410 / 611 : Operating Systems Archit ect ural Considerat ions • Must be able t o rest art any inst ruct ion af t er a page f ault . • e.g. ADD A,B TO C • What about operat ions t hat modif y several locat ions in memory? – e.g. block copy operat ions? • What about operat ions wit h side ef f ect s? – e.g. PDP-11, 80x86 aut o-decrement , aut o-increment operat ions? – Add mechanism f or OS t o “undo” inst ruct ions. Perf ormance of Demand Paging • Ef f ect ive Memory Access t ime ema : ema = (1- p) * ma + p * “page f ault time” • where – p = probabilit y of a page f ault – ma = memory access t ime • Operat ions during Page Fault : page is on backing st ore OS OS 1. service page f ault t r ap int er r upt ref erence 2. swap in page i CPU i CPU 3. rest art process r est ar t inst ruct ion page t able f r ee f r ee f r ame f r ame load page updat e page t able 5
CPSC 410 / 611 : Operating Systems OS Policies f or Virt ual Memory • Fetch Policy – How/ when t o get pages int o physical memory. – demand paging vs . prepaging. • Placement Policy – Where in physical memory t o put pages. – Only relevant in NUMA machines. • Replacement Policy – Physical memory is f ull. Which f rame t o page out ? • Resident Set Management Policy – How many f rames t o allocat e t o process? – Replace someone elses f r ame? • Cleaning Policy – When t o wr it e a modif ied page t o disk. • Load Control Conf iguring t he Win2k Memory Manager • Regist ry Values t hat Af f ect t he Memory Manager: ClearPageFileAtShutdown DisablePagingExecutive IoPageLockLimit LargePageMinimum LargeSystemCache NonPagedPoolQuota NonPagedPoolSize PagedPoolQuota PagedPoolSize SystemPages 6
CPSC 410 / 611 : Operating Systems Page Replacement • Virt ual memory allows higher degrees of mult iprogramming by over -allocat ing memory. 256kB 256kB 256kB 256kB 256kB 1024kB • Example: 0 K 0 2 v 1 L 1 4 v 0 N 2 i 2 M 3 1 C 0 v M 3 N 2 K B 3 A 0 A 0 3 v 4 L 1 B 1 i 5 2 D 1 v 2 C 3 5 v 3 D Mechanics of Page Replacement • I nvoked whenever no f ree f rame can be f ound. swap out vict im invalidat e page f v/ i 2 1 ent ry f or vict im page vict im updat e nil/ f i/ v 4 3 swap ent ry f or in new new page page page t able backing st ore physical memory • Problem: Need t wo page t ransf ers! Solut ion: Dirty bit . 7
CPSC 410 / 611 : Operating Systems Page Replacement Algorit hms • Obj ect ive: Minimize page f ault rat e. i • Why bot her? a • Example for(int i=0; i<10; i++) { a = x * a; x } • Evaluat ion: Sequence of memory ref erences: ref erence string . FI FO Page Replacement ent er f rame in FI FO queue FI FO queue select 1 vict im 6 swap out invalidat e vict im f v/ i 3 2 ent ry f or page vict im page vict im updat e nil/ f i/ v 5 4 swap ent ry f or in new new page page page t able backing st ore physical memory 8
CPSC 410 / 611 : Operating Systems FI FO Page Replacement (cont .) • Example: time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a e e e e e d b b b b b b b a a a a c c c c c c c c b b b d d d d d d d d d c c ! ! ! ! ! • Advant age: simplicit y • Disadvant age: Assumes t hat pages residing t he longest in memory are t he least likely t o be ref erenced in t he f ut ure (does not exploit principle of localit y ). Opt imal Replacement Algorit hm • Algorit hm wit h lowest page f ault rat e of all algorit hms: Replace t hat page which will not be used f or t he longest period of t ime. • Example: time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a a a a a a d b b b b b b b b b b b c c c c c c c c c c c d d d d d e e e e e e ! ! 9
CPSC 410 / 611 : Operating Systems Approximat ion t o Opt imal: LRU • Least Recently Used : replace t he page t hat has not been accessed f or longest per iod of t ime. • Example: time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a a a a a a a b b b b b b b b b b b c c c c c e e e e e d d d d d d d d d d c c ! ! ! LRU: I mplement at ion • Need t o keep chronological hist ory of page ref erences; need t o be reordered upon each ref erence. • Stack: stack ? c a d b e b a b c d ? ? c a d b e b a b c ? ? ? c a d d e e a b ? ? ? ? c a a d d e a • Capacitors: Associat e a capacit or wit h each memory f rame. Capacit or is charged wit h every ref erence t o t he f rame. The subsequent exponent ial decay of t he charge can be direct ly convert ed int o a t ime int erval . • Aging registers: Associat e aging regist er of n bit s (R n-1 , ..., R 0 ) wit h each f r ame in memory. Set R n-1 t o 1 f or each ref erence. Periodically shif t regist ers t o t he r ight . 10
CPSC 410 / 611 : Operating Systems Appr oximat ion t o LRU:Clock Algor it hm • Associat e a use_bit wit h every f rame in memory. – Upon each ref erence, set use_bit t o 1. – Keep a point er t o f irst “vict im candidat e” page. – To select vict im: I f current f rame’s use_bit is 0, select f rame and increment point er. Ot herwise delet e use_bit and increment point er. time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a/1 a/1 a/1 a/1 a/1 e/1 e/1 e/1 e/1 e/1 d/1 b/1 b/1 b/1 b/1 b/1 b/0 b/1 b/0 b/1 b/1 b/0 c/1 c/1 c/1 c/1 c/1 c/0 c/0 a/1 a/1 a/1 a/0 d/1 d/1 d/1 d/1 d/1 d/0 d/0 d/0 d/0 c/1 c/0 ! ! ! ! I mpr ovement on Clock Algor it hm (Second Chance Algor it hm) • Consider read/ writ e act ivit y of page: dirt y_bit (or modif y_bit ) • Algorit hm same as clock algorit hm, except t hat we scan f or f rame wit h bot h use_bit and dirt y_bit equal t o 0. • Each t ime t he point er advances, t he use_bit and dirt y_bit are updat ed as f ollows: u d u d u d u d before 1 1 1 0 0 1 0 0 after 0 1 0 0 0 0* (select) • Called Second Chance because a f rame t hat has been writ t en t o is not removed unt il t wo f ull scans of t he list lat er. • Not e: St allings describes a slight ly dif f erent algorit hm! 11
Recommend
More recommend