Presentation is loading. Please wait.

Presentation is loading. Please wait.

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.

Similar presentations


Presentation on theme: "U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture."— Presentation transcript:

1 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture 13: Demand Paging

2 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Last Time: Paging Motivation Fragmentation Page Tables Hardware Support Other Benefits

3 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 Today: Demand-Paged VM Reading pages Writing pages Swap space Page eviction Cost of paging Page replacement algorithms Evaluation

4 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Demand-Paged Virtual Memory Key idea: use RAM as cache for disk OS transparently moves pages Page table: page on disk, in memory OS updates whenever pages change state Requires locality: Working set (pages referenced recently) must fit in memory If not: thrashing (nothing but disk traffic)

5 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Demand-Paging Diagram

6 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Key Policy Decisions Two key questions: (for any cache): When do we read page from disk? When do we write page to disk?

7 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Reading Pages Read on-demand: OS loads page on its first reference May force an eviction of page in RAM Pause while loading page = page fault Can also perform pre-paging: OS guesses which page will next be needed, and begins loading it Advantages? Disadvantages? Most systems just do demand paging

8 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Demand Paging On every reference, check if page is in memory (valid bit in page table) If not: trap to OS OS checks address validity, and Selects victim page to be replaced Invalidates old page in page table Begins loading new page from disk Switches to other process (paging = implicit I/O) Note: must restart instruction later

9 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 Demand Paging, Continued Interrupt signals page arrival, then: OS updates page table entry Continues faulting process Stops current process We could continue currently executing process – but why not? And where does the victim page go?

10 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 Demand Paging, Continued Interrupt signals page arrival, then: OS updates page table entry Continues faulting process Stops current process We could continue currently executing process – but why not? Page just brought in could get paged out... And where does the victim page go?

11 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 Swap Space Swap space = where victim pages go Partition or special file reserved on disk Sometimes: special filesystem (“tmpfs”) What kind of victim pages can be evicted without going to swap? Size of reserved swap space limits what?

12 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 Swap Space Swap space = where victim pages go Partition or special file reserved on disk Sometimes: special filesystem (“tmpfs”) What kind of victim pages can be evicted without going to swap? Read-only (“text”), untouched anonymous pages Size of reserved swap space limits what?

13 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Swap Space Swap space = where victim pages go Partition or special file reserved on disk Sometimes: special filesystem (“tmpfs”) What kind of victim pages can be evicted without going to swap? Read-only (“text”), untouched anonymous pages Size of reserved swap space limits what? Total virtual memory

14 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 Virtual Memory Locations VM pages can now exist in one or more of following places: Physical memory (in RAM) Swap space (victim page) Filesystem (why?) Requires more sophisticated page table Slightly more expensive, but real expense...

15 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 Cost of Paging Worst-case analysis – useless Easy to construct adversary example: every page requires page fault A, B, C, D, E, F, G, H, I, J, A... size of available memory

16 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 16 Cost of Paging, continued “Average-case” No such thing (what’s “average” reference behavior?) But: processes exhibit locality, so performance generally not bad Temporal locality: processes tend to reference same items repeatedly Spatial locality: processes tend to reference items near each other (e.g., on same page)

17 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 17 Effective Access Time Let p = probability of page fault (0 · p · 1) ma = memory access time Effective access time = (1 – p) * ma + p * page fault service time Memory access = 200ns, page fault = 25ms: effective access time = (1-p)*200 + p*25,000,000 Desired effective access time = 10% slower than memory access time: p = ?

18 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 18 Effective Access Time Let p = probability of page fault (0 · p · 1) ma = memory access time Effective access time = (1 – p) * ma + p * page fault service time Memory access = 200ns, page fault = 25ms: effective access time = (1-p)*200 + p*25,000,000 Desired effective access time = 10% slower than memory access time: p = 8*10 -7

19 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 19 Evaluating Page Replacement Algorithms Worst-case – all just as bad Average-case – what does that mean? Empirical studies – real application behavior Theory: competitive analysis Can’t do better than optimal How far (in terms of faults) is algorithm from optimal in worst-case? Competitive ratio If algorithm can’t do worse than 2x optimal, it’s 2-competitive

20 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 20 Page Replacement Algorithms MIN, OPT (optimal) RANDOM evict random page FIFO (first-in, first-out) give every page equal residency LRU (least-recently used) MRU (most-recently used)

21 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 21 MIN/OPT Invented by Belady (“MIN”), now known as “OPT”: optimal page replacement Evict page to be accessed furthest in the future Provably optimal policy Just one small problem... Requires predicting the future Useful point of comparison

22 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 22 MIN/OPT example Page faults: 5

23 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 23 RANDOM Evict any page Works surprisingly well Theoretically: very good 2 * H k competitive (H k ¼ ln k) Not used in practice: takes no advantage of locality

24 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 24 LRU Evict page that has not been used in longest time (least-recently used) Approximation of MIN if recent past is good predictor of future A variant of LRU used in all real operating systems Competitive ratio: k, (k = # of page frames) Best possible for deterministic algs.

25 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 25 LRU example Page faults: ?

26 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 26 LRU example Page faults: 5

27 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 27 LRU, example II Page faults: ?

28 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 28 LRU, example II Page faults: 12!

29 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 29 FIFO First-in, first-out: evict oldest page Also has competitive ratio k But: performs miserably in practice! LRU takes advantage of locality FIFO does not Suffers from Belady’s anomaly: More memory can mean more paging! LRU & other “stack” algs. do not

30 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 30 FIFO & Belady’s Anomaly

31 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 31 LRU: No Belady’s Anomaly Why no anomaly for LRU?

32 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 32 MRU Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size What we really want: adaptive algorithms (e.g., EELRU – Kaplan & Smaragdakis) A, B, C, D, A, B, C, D,... size of available memory

33 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 33 Summary Reading pages Writing pages Swap space Page eviction Cost of paging Page replacement algorithms Evaluation

34 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 34 Next Time Virtual memory implementation Interaction with multiprogramming


Download ppt "U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture."

Similar presentations


Ads by Google