Chapter 10: Virtual Memory Questions?
Chapter 10: Virtual Memory Questions?
Questions?
2
Maria Hybinette, UGA Maria Hybinette, UGA
Observations
7 8
Maria Hybinette, UGA Maria Hybinette, UGA
! Each page in virtual address space maps to one of three ! Extend page tables with an extra bit to 1
indicate whether it is in memory 1
locations: 1
(resident bit): valid (invalid) or on 1
» Physical main memory: Small, fast, expensive disk. 0
M
» Disk (backing store): Large, slow, cheap » permissions (r/w), valid 0
» Nothing (error): Free » Page in memory: valid bit set in page page table
0
table entry (PTE)
» Page out to disk: valid bit cleared
Smaller, faster (invalid)
and more
Leverage memory registers expensive – PTE points to block on disk
hierarchy of machine – Causes trap into OS when page is
architecture cache referenced
Each layer acts as – Trap: page fault
“backing store” for the main memory Bigger, smaller
and cheaper
layer above
Disk Storage
9 10
Maria Hybinette, UGA Maria Hybinette, UGA
Paging Operations
Virtual Memory Mechanisms
PTE in TLB?
– Write victim page out to disk if modified (add dirty bit to PTE) Yes
Memory
» OS reads referenced page from disk into memory Full?
No
» Page table is updated, valid bit is set Page replacement
Page tables
» Process continues execution updated
11
Maria Hybinette, UGA
Virtual Memory Policies Page Selection
13 14
Maria Hybinette, UGA Maria Hybinette, UGA
15 16
Maria Hybinette, UGA Maria Hybinette, UGA
! Page replacement
! Which page in main memory should selected as
» find some page in memory, but not really in use, victim?
swap it out » Write out victim page to disk if modified (dirty bit set)
! Observation: Same page may be brought into » If victim page is not modified (clean), just discard
memory several times
! OPT: Replace page not used for longest time in future
» Advantage: Guaranteed to minimize number of page
faults
» Disadvantage: Requires that OS predict the future
– Not practical, but good for comparison
! Random: Replace any page at random
» Advantage: Easy to implement
» Works okay when memory is not severely over-committed
17 18
Maria Hybinette, UGA Maria Hybinette, UGA
Page Replacement Continued Evaluate Page Replacement Algorithms
! FIFO: Replace page that has been in memory the ! Want lowest page-fault rate.
longest
» Intuition: First referenced long time ago, done with it now
! Evaluate algorithm by running it on a particular
» Advantages:
string of memory references (reference string)
– Fair: All pages receive equal residency and computing the number of page faults on
– Easy to implement (circular buffer) that string.
» Disadvantage: Some pages may always be needed ! Convert address to page
! LRU: Replace page not used for longest time in past » Example: Assume 100 bytes per page and
» Intuition: Use past to predict the future – Assume the address sequence:
» Advantages: ! 0100, 0210, 0250, 0300, 0350, 0380, 0400, 0160, 0250, 0505,
– With locality, LRU approximates OPT 0100, 0110, 0230, 0350, 0450, 0450, 0500, 0500
» Disadvantages: – Convert address to a reference string:
– Harder to implement, must track which pages have been ! 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
accessed
– Does not handle all workloads well 19 20
Maria Hybinette, UGA Maria Hybinette, UGA
B
D
A
21 B 22
Maria Hybinette, UGA Maria Hybinette, UGA
First-In-First-Out (FIFO)
Page Replacement: Add memory Algorithm: Add Memory
! Add more physical memory, what happens to ! Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
performance? ! 3 frames (3 pages can be in memory at a time per process)
» Ideally the numbers of page faults should should
decrease as number of available frames increases 1 2 3
» 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. 4 1 2
5 3 4
» If 1 page frame : Number of page faults?
– 12 page faults, one fault for every page (11 faults) ! 4 frames
» If 12 frames : Number of page faults?
1 2 3 4
– 5 page faults
5 1 2 3
4 5
23 24
Maria Hybinette, UGA Maria Hybinette, UGA
Page Replacement Comparison Implementing LRU
! Software Perfect LRU
! Add more physical memory, what happens to
» OS maintains ordered list of physical pages by reference time
performance? » When page is referenced: Move page to front of list (top)
» LRU, OPT: Add more memory, guaranteed to have » When need victim: Pick page at back of list (bottom)
fewer (or same number of) page faults » Trade-off: Slow on memory reference, fast on replacement
– Smaller memory sizes are guaranteed to ! Hardware Perfect LRU
contain a subset of larger memory sizes » Associate register with each page
» FIFO: Add more memory, usually have fewer page » When page is referenced: Store system clock in register
faults » When need victim: Scan through registers to find oldest clock
– Belady’s anomaly: May actually have more page » Trade-off: Fast on memory reference, slow on replacement
faults! (especially as size of memory grows)
! In practice, do not implement Perfect LRU
» LRU is an approximation anyway, so approximate more
» Goal: Find an old page, but not necessarily the very oldest
25 26
Maria Hybinette, UGA Maria Hybinette, UGA
! Hardware
» Keep use (or reference) bit for each page frame
initialized to 0.
» When page is referenced: set use bit (1)
! Operating System
» Page replacement: Look for page with use bit cleared (0)
(has not been referenced for awhile)
» Implementation:
– Keep pointer to last examined page frame
– Traverse pages in circular buffer
– Clear use bits while searching for replacement
– Stop when find page with already cleared use bit, replace
this page
! Worst Case:
» All bits are set -> FIFO (slow)
27 28
Maria Hybinette, UGA Maria Hybinette, UGA
29 30
Maria Hybinette, UGA Maria Hybinette, UGA
More Clock Extensions More Clock Extensions
! Add software counter (“chance”) ! Use dirty bit to give preference to dirty pages
» Intuition: Better ability to differentiate across pages (how » Intuition: More expensive to replace dirty pages
much they are being accessed) – Dirty pages must be written to disk, clean pages do not
» Increment software counter chance if use bit is 0 » Replace pages that have use bit and dirty bit cleared
» Replace when chance exceeds some specified limit
31 32
Maria Hybinette, UGA Maria Hybinette, UGA
33 34
Maria Hybinette, UGA Maria Hybinette, UGA
35 36
Maria Hybinette, UGA Maria Hybinette, UGA
Allocating Memory across
Questions Processes
37 38
Maria Hybinette, UGA Maria Hybinette, UGA
39 40
Maria Hybinette, UGA Maria Hybinette, UGA
! Advantages: Users running more processes ! When does the Virtual Memory illusion break?
cannot hog memory ! Example:
» Set of processes frequently referencing 33 important
! Disadvantage: Inefficient allocation pages
» Physical memory can fit 32 pages
! What happens?
» System Repeat Cycle:
– Reference page not in memory
– Replace a page in memory with newly referenced page
– Replace another page right away again, since all its pages
are in active use…
41 42
Maria Hybinette, UGA Maria Hybinette, UGA
System does not know it is
Thrashing thrashing
! If a process does not have “enough” pages, the page-fault rate is very
high. This leads to:
! Thrashing:
» low CPU utilization.
» Spends more time paging than execution, i.e. system » operating system thinks that it needs to increase the degree of
reading and writing pages instead of executing useful multiprogramming.
instructions » another process added to the system
» Average memory access time equals to disk access time ! Why the CPU utilization decreases:
» Illusion breaks: Memory appears slow as disk rather than » Suppose a process need more frames, starts faulting, removing frames
disk appearing fast as memory from others, in turn making the other processes fault
! Add more processes, thrashing gets worse » Processes queue up for the paging device, CPU decreases
! System is reading and writing pages instead of » OS add processes that immediately need new frames further taking away
pages from running processes
executing useful instructions
» Implication: Average memory access time = disk access
time
» Memory appears as slow as disk, instead of disk
appearing as fast as memory
43 44
Maria Hybinette, UGA Maria Hybinette, UGA
» Pages referenced by process in last T seconds of ! Balance set: Sum of working sets of all active
execution processes
» Working set changes slowly over time ! Interaction with scheduler
» If balance set exceeds size of memory, move some
! Example: process to inactive set
– Which process???
"=8 Time
» If balance set is less than size of memory, move some
AABCBBBCDCDEBBEEDFB FDBBEDB process to active set
– Which process?
ABC BDEF
47 » Any other decisions? 48
Maria Hybinette, UGA Maria Hybinette, UGA
Working Set Implementation Unresolved Questions
! Leverage use bits (as in clock algorithm) ! How should value of T be configured?
» What if T is too large?
! OS maintains idle time for each page
! How should working set be defined when pages are
» Amount of CPU received by process since last shared?
access to page » Put jobs sharing pages in same balance set
» Periodically scan all resident pages of a process ! What processes should compose balance set?
– If use bit is set, clear page’s idle time ! How much memory needed for a “balanced system”?
– If use bit is clear, add process CPU time (since last » Balanced system: Each resource (e.g., CPU, memory,
scan) to idle time disk) becomes bottleneck at nearly same time
» If idle time < T, page is in working set » How much memory is needed to keep the CPU busy?
» With working set approach, CPU may be idle even with
runnable processes
49 50
Maria Hybinette, UGA Maria Hybinette, UGA
51 52
Maria Hybinette, UGA Maria Hybinette, UGA
53 54
Maria Hybinette, UGA Maria Hybinette, UGA
Memory Hierarchy Average Access Time calculation
main memory Bigger, smaller ! Example: 1 out of every 33 references misses, H = 97%
and cheaper
» 0.97 * (100 ns) + (0.03) * (20000000ns) = 750000 ns = 750
us
Disk Storage » More than 1000 times slower than physical memory
access
! Need very high hit rate for acceptable performance
55 56
Maria Hybinette, UGA Maria Hybinette, UGA