Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Operating Systems - Virtual Memory
1. Operating Systems
CMPSCI 377
Virtual Memory
Emery Berger
University of Massachusetts Amherst
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
2. Virtual Memory
Virtual, not physical memory
Divided into pages
Page tables, TLB
Provides isolation, protection, optimization
Managed using eviction policies
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
3. Demand-Paged Virtual Memory
Key idea: use RAM as cache for disk
OS transparently moves pages
Requires locality:
Working set (memory referenced recently)
must fit in RAM
If not: thrashing (nothing but disk traffic)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
4. Virtual Memory Pages
Memory divided into
fixed-sized pages (e.g.,
4K, 8K)
Allocates pages to frames
A
in memory
OS manages pages
Moves, removes,
reallocates
Copied to and from disk
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4
5. Demand-Paging Diagram
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
6. Paging + Locality
Most programs obey
90/10 “rule” A
90% of time spent
accessing 10% of A
B
memory
B
Exploit this rule:
Only keep “live” parts
of process in memory
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
7. Virtual Memory
Processes use virtual addresses
Addresses start at 0
OS lays process down on pages
MMU (memory-management unit):
Hardware support for paging
Translates virtual to physical addresses
Uses page table to keep track of frame
assigned to memory page
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7
8. Mapping Virtual to Physical
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
9. Address Translation
Powers of 2:
Virtual address space:
size 2m
Page size 2n
High-order m-n bits
of virtual address
select page
Low order n bits
select offset in page
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
10. Paging Hardware: Diagram
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
11. Translation Lookaside Buffer (TLB)
TLB: fast, fully associative memory
Caches page table entries
Stores page numbers (key) and frame (value)
in which they are stored
Assumption: locality of reference
Locality in memory accesses =
locality in address translation
TLB sizes: 8 to 2048 entries
Powers of 2 simplifies translation
of virtual to physical addresses
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
12. TLB Action
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
13. TLB: Diagram
v = valid bit: entry is up-to-date
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
14. Cost of Using TLB
Measure in terms of memory access cost
What is cost if:
Page table is in memory?
Page table managed with TLB?
Large TLB:
Improves hit ratio
Decreases average memory cost
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
15. Sharing
Paging allows sharing of memory across
processes
Different virtual addresses,
point to same physical address
Shared stuff includes code, data
Compiler marks “text” segment (i.e., code) of
applications (e.g., emacs) - read-only
OS: keeps track of such segments
Reuses if another instance of app arrives
Copy-on-write
Can greatly reduce memory requirements
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
16. Key Policy Decisions
Two key questions: (for any cache):
When do we read page from disk?
When do we write page to disk?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
17. 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
Most systems just do demand paging
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
18. 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
Begins loading new page from disk
Switches to other process
(paging = implicit I/O)
Note: must restart instruction later
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
19. 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?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
20. 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?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
21. Swap Space
Swap space = where victim pages go
Partition or special file reserved on disk
Sometimes: special filesystem (“tmpfs”)
Only read-only or undirtied victim pages can
be evicted without going to swap
“text” (code), untouched anonymous pages
Size of reserved swap space limits
total virtual memory
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
22. 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
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
23. 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)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
24. 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
Still useful point of comparison
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
25. MIN/OPT example
Page faults: 5
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25
26. RANDOM
Evict any page
Works surprisingly well – why?
Theoretically: very good,
but not used in practice:
takes no advantage of locality
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 26
27. 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
Variant of LRU used in all real operating
systems
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 27
28. LRU example
Page faults: ?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 28
29. LRU example
Page faults: 5
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 29
30. LRU, example II
Page faults: ?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 30
31. LRU, example II
Page faults: 12!
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 31
32. 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
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 32
33. FIFO & Belady’s Anomaly
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 33
34. MRU
Evict most-recently used page
Shines for LRU’s worst-case: loop that
exceeds RAM size
A, B, C, D, A, B, C, D, ...
size of available memory
What we really want: adaptive algorithms
(e.g., EELRU – Kaplan & Smaragdakis)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 34
35. The End
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 35
36. LRU: No Belady’s Anomaly
Why no anomaly for LRU?
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 36