Title: Chapter 9: Virtual Memory
1Chapter 9 Virtual Memory
- Paging
- Has advantages, but so far in our discussion
requires entire process to be in main memory - Virtual memory
- Adds capability of enabling process to run
without all pages resident in main memory - Some benefits
- Programs can be larger than physical memory
- Programmer views computer as having large memory
- More programs can be partially resident,
potentially increasing CPU utilization - Drawbacks
- Complexity of implementation (O/S)
- Performance penalties (e.g., page faults)
2Chapter 9 Outline
- Demand paging
- Page faults
- Locality of reference
- Restarting instructions
- Effective Access Time (EAT) applied to virtual
memory - Page replacement algorithms FIFO, OPT, LRU, LRU
approximations - Reference string analysis
- Beladys anomaly
- Victim (process) selection
- Thrashing, working set
- Misc technical issues
- Copy-on-write, memory mapped files, shared
memory, page locking, Free-frame list - Programming issues
3Demand Paging
- Pages for a process stored on disc
- Only brought into RAM when accessed by program
- Pure demand paging
- Start process with 0 pages resident
- Nonresident page
- A page that has not yet been brought in from disc
- Causes page fault upon a page access
- Operating system blocks process, and initiates
I/O to read page from disc - Bits in page table indicate page status
- Cases (a) page nonresident part of process,
(b) page resident part of process, (c) page not
part of process - Locality of reference principle
- Most of the time, a process uses code/data from a
small part of the program
4Figure 9.5 Page table when some pages are not in
main memory.
(address space)
5Figure 9.6 Steps in handling a page fault
6Restarting Instructions
- Page faults can occur either (a) prior to
executing an instruction or (b) during
instruction execution - An Issue
- Some machine instructions are atomic some are
not - For non-atomic instructions, may need to restart
midway through - Restarting instruction from beginning may not
cause same effect - E.g., block move where source and destination
overlap - One solution (in hardware) check operands
boundaries and make sure all pages in memory
before starting operation
7PerformanceAnother Application of EAT
- Let p page fault rate
- (I.e., probability of a page fault)
- EAT (1 p) hit-time p miss-time
- Now hit-time and miss-time refer to page faults,
and not TLB usage - Example
- Disk access time is 8 msec (0.008 s)
- Memory access 60 nsec (60x10-9 s)
- What is the hit-time? What is the miss-time?
- What page fault rate would give a 10 decrease in
memory access performance relative to no virtual
memory? - I.e., 10 increase in memory access time
- Dont worry about TLB/page table access in this
question
8Solution - 1
- A 10 decrease in performance would
- - Increase memory access time by 10
- - Current memory access time 60 nsec (60x10-9
s) - - Increase by 10 66 nsec (66x10-9 s)
- 66x10-9 (1 p) hit-time p miss-time
- What is hit-time?
- Consists of just memory access time, I.e.,
60x10-9 s - What is miss-time?Consists of accessing disk to
obtain page, o/s service time, data transfer
time, time to access item in RAM - Approximate with just disk access time, I.e.,
.008s and RAM access time
9Solution - 2
- (1 p) 60x10-9 p (0.008 60x10-9 )
66x10-9 - Now, need to solve for p
- p 60x10-9 p 0.00800006 66x10-9 -
60x10-9 - p (0.00800006 - 60x10-9 ) 6x10-9
- p (6x10-9 ) /(0.00800006 - 60x10-9)
- p 0.00000075
- This means a page fault can occur only 1 in
1,333,333 memory accesses (I.e., 1/p)
10When to Free Frames?
- O/S can wait until a process page faults to
allocate a free frame - I.e., in this case, could have 0 unused frames at
time a process page faults - Or can use a Free-Frame List (Pool)
- A list of frames of RAM that are unused
- When handling page fault, use a frame from
free-frame list - O/S maintains free-frame list with a number of
frames - When the number of free frames gets below a
certain level, operating system increases number
of free frames
11Page Replacement
- In any case, to create another free frame need to
find a victim frame - I.e., steal a frame that is presently being used
by another process - If this victim frame has been modified, may have
to write the frame to disc before putting it on
the free list (dirty frame) - This is called page replacement
- we are replacing the page in a currently used
frame with another page
12How can we do this?
- What algorithms can we use to select a victim
frame?
13Page Replacement Algorithms
- Some algorithms for selection of victim frames
- FIFO (First-in First-out)
- OPT (Optimal)
- LRU (Least recently used)
- LRU implementations
14Reference String Analysis
- Goal
- Determine number of page faults for a given
sequence of page accesses, and a given size
physical memory - Compare performance of algorithms
- Look at sequence of memory accesses
- Only consider page numbers
- Drop consecutive duplicate page numbers
- E.g.,
15General Expectation of Page Fault Behavior
16FIFO Page Replacement Algorithm
- Associate with each page the time it was brought
into a physical frame - Select as a victim page to be replaced, the
oldest page - Implement with a FIFO queue of references to pages
17Example
- Process with logical address space of 8 pages
- Pages 0 through 7
- Reference string will consist of a series of
numbers from 0 through 7 - Pure demand paging
- 3 frames of physical memory
- Assume we dont select a victim frame until all
frames of RAM are full
18FIFO Example (3 frames)
Reference String
Frames of RAM
19FIFO Example (3 frames)
Page fault
20FIFO Example (3 frames)
21FIFO Example (3 frames)
Now Frames are all full. On next page fault, we
will need to select victim frame
22FIFO Example (3 frames)
Circle the victim frame
Page 7 is selected as victim It has been
resident the longest
23FIFO Example (3 frames)
24FIFO Example (3 frames)
25FIFO Example (3 frames)
26FIFO Example (3 frames)
27FIFO Example (3 frames)
28FIFO Example (3 frames)
29FIFO Example (3 frames)
30FIFO Example (3 frames)
31FIFO Example (3 frames)
32FIFO Example (3 frames)
33FIFO Example (3 frames)
34FIFO Example (3 frames)
35FIFO Example (3 frames)
36FIFO Example (3 frames)
37FIFO Example (3 frames)
38FIFO Example (3 frames)
So, 15 page faults for this reference string,
FIFO, 3 frames
39Page Faults Size of Physical Memory
- Recall Our general expectation
- With increases in physical memory size (number of
frames), want the number of page faults to
decrease monotonically - One implication We dont want to see an increase
in page fault rate if we increase the amount of
RAM - Achieving this expectation is dependent on the
page replacement algorithm - Some page replacement algorithms exhibit Beladys
anomaly - Beladys anomaly Number of page faults actually
increase with an increase in number of physical
frames
40Beladys Anomaly
Graph gives performance of FIFO on reference
string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
41Beladys Anomaly
Graph gives performance of FIFO on reference
string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
42Beladys Anomaly
Graph gives performance of FIFO on reference
string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
43Beladys Anomaly
This is where we see the anomaly Page fault rate
increased with an increase RAM
Graph gives performance of FIFO on reference
string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
44Beladys Anomaly
Graph gives performance of FIFO on reference
string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
45OPT (Optimal) Page Replacement Algorithm
- Lowest page fault rate of any page replacement
algorithm - Algorithm
- Replace the page that will not be used for the
longest period of time
46OPT Example (3 frames)
Reference String
Frames of RAM
47OPT Example (3 frames)
48OPT Example (3 frames)
49OPT Example (3 frames)
50OPT Example (3 frames)
0 and 1 are used before 7 in the future, so
replace 7
51OPT Example (3 frames)
52OPT Example (3 frames)
53OPT Example (3 frames)
54OPT Example (3 frames)
55OPT Example (3 frames)
56OPT Example (3 frames)
57OPT Example (3 frames)
58OPT Example (3 frames)
59OPT Example (3 frames)
60OPT Example (3 frames)
61OPT Example (3 frames)
62OPT Example (3 frames)
63OPT Example (3 frames)
64OPT Example (3 frames)
65OPT Example (3 frames)
66OPT Example (3 frames)
So, 9 page faults for this reference string, OPT,
3 frames (15 for FIFO)
67But
- We cant directly implement OPT
- In general, we dont know the sequence of future
pages required by a process - What should we do?
68LRU (Least Recently Used) Page Replacement
Algorithm
- Idea
- Approximate OPT (future) with the history of page
usage - Algorithm
- Replace the page that has not been used for the
longest period of time - LRU does not suffer from Beladys anomaly
- Makes use of locality of reference principle
69LRU example
70Exact LRU Implementations
- Counters
- Have a counter field in each page table entry
- And each TLB entry
- When page accessed, update counter with clock
- Done by memory management unit (MMU)
- LRU page is one with smallest counter field
- Must search page table(s) to find victim
- Stack (LIFO)
- Maintain a stack (LIFO) data structure of
references to pages - When a page is accessed, remove from stack, put
this page at top of stack - LRU page is at bottom of stack
- Overhead of keeping a linked list structure per
page access - Definitely also needs hardware support
71LRU Approximations
- Additional reference bits
- Keep additional bits (e.g., 8 bits) per page
table entry - These are called reference bits
- Initially all reference bits are set to 0
- Upon a page access, set leftmost reference bit to
1 - Needs MMU support
- At regular intervals (e.g., once every 100 ms),
the O/S shifts all reference bits to right one
position - Victim page is one with all reference bits 0
- page has not been accessed for 8 time periods
- Second-chance algorithm (a.k.a. clock algorithm)
- Above method with 1 reference bit
72Second-chance algorithm (a.k.a. clock algorithm)
73Operating System Example Solaris (Section
9.10.2, p. 363-4)
- Faulting process given a page from free frame
list - Pageout when free frames drops below a
designated level (e.g., less than 1/64 of size
physical memory) - Pageout Two-handed clock algorithm
- First hand set sets all resident page reference
bits to zero - After a period of time (e.g., a few seconds),
second hand checks reference bits - Return to free list Pages with reference bits
still zero
74Frame Allocation to Processes
- So far weve not considered process-related
issues - However, we should consider the number of frames
to allocate to a process - Some issues
- 1) Also, must have enough frames to hold all the
different pages that a single instruction can
reference - 2) Victimize a frame from within a process or
from other processes? - 3) As the number of frames allocated to a process
decreases, the page fault rate for that process
will increase, thus slowing process execution
75Minimum Number of Frames per Process
- Consider the machine instruction
- LOAD R1, _at_memOp indirect addressing
- Need to first fetch instruction
- Then need to fetch from memOp location
- Then need to use this as an address to fetch
- Minimum number of frames needed for process?
- Minimum number of frames defined by instruction
set architecture, size of instructions and
operands, and data alignment of instructions
operands
76Global vs. Local
- Local page selection
- Selection from process that caused page fault
- Predictable run-time behavior
- Global page selection
- Selection from all processes
- Tends to improve system throughput
77What is the right number of frames per process?
- Define fi as the number of frames needed by
process i - E.g., all of the frames in the loop of code that
it is currently executing - What happens if process i doesnt have its fi
frames? - We get high page fault activity
- This is called thrashing
78Thrashing
79Controlling Thrashing - 1
- Local victim selection
- One process cannot cause another process to
thrash - Working set model
- Working set distinct pages in the most recent ?
memory references - Approximates the programs locality
- The set of data code that the process is
currently using - Idea Dont allow pages in the working set to be
selected as victims - Since the process is currently using these
pages, it will use those pages again soon and so
cause a page fault if they are not in memory - Page fault frequency
80Example of Working Set
- ? 10
- Sequence of pages in memory references
- 2 6 1 5 7 7 7 5 1 6 2 3 4 1 2 3 4
?
WS 1, 2, 5, 6, 7
81Working Set
- Issues with working set
- Setting the value of ? (will vary per process)
- Keeping track of the pages in the working set
- Can use reference bit methods
- Keep track of page set used in a time window
- Also gives us ?
82Controlling ThrashingPage-Fault Frequency
Method
- Thrashing
- The page fault rate is too high for the process
- Solution strategies
- Swap out the process
- Give it more frames
- Use upper lower page fault rates per process
- Past upper limit Allocate more frames (or swap
out) - Below lower limit Take away a frame
83Figure 9.21 Page-fault frequency
84Program Structure Issues
- We dont want the results of program execution to
depend on virtual memory - E.g., example of block move
- But What about performance
- When do different programs have different
performance on a virtual memory system? - In the following we look at array access of a 2D
array with row-major representation
85Bottom program, p. 360
Assuming row major representation of the array,
and page/frames that hold 128 integers, how many
page faults likely occur for the array?
86Data for the program (p. 360)
Page 0
With 128 word pages (1 word is one integer) Each
row may be stored in one page (row major order)
Page 1
A1270
A1271
A1272
Page 127
A127127
87Row-based access Bottom program, p. 360
- Accesses all elements in one row, one sequence of
operations - Thus, accesses all array elements in one page in
same sequence - May have one page fault to bring in page
initially - Then, all accesses will likely be to this page in
memory - So, at most 128 page faults for the array data of
the program (one per row)
88Top program, p. 360
Again, assuming row major representation of the
array, and page/frames that hold 128 integers,
how many page faults occur for the array?
89Data for the program (p. 360)
Page 0
With 128 word pages (1 word is one integer) Each
row is stored in one page (row major order)
Page 1
A1270
A1271
A1272
Page 127
A127127
90Column-based Access Top Program, p. 360
- Accesses all elements in one column, one sequence
of operations - Thus, accesses array elements in 128 pages in
same sequence - With pure demand paging, have one page fault per
page, for accessing all elements in one column - So, at most 128 page faults for accessing
elements in one column (each column element is on
a separate page) - There are 128 columns, so with limited memory may
have as many as 128 x 128 page faults 16, 384
faults
91Take-home message
- Some data structures or access to data structures
may have poor performance in a virtual memory
environment - E.g., accessing a row major array in a
column-based manner - This type of array access produces poor spatial
locality of reference - E.g., hashing techniques
- Scatters references in memory, producing poor
spatial locality of reference
92Additional Techniques with Paging Virtual Memory
- Memory-mapped files
- Copy-on-write
- Shared memory
- Page locking
93Memory Mapped Files
- Map the blocks of a file into the address space
of a process - When byte of a file block (memory address) first
accessed, demand pages block from disk into RAM - Handled via virtual memory
- Confusing picture on this topic Figure 9.23, p.
349
94Copy-On-Write
Figures 9.7 Figure 9.8 (p. 326)
- Technique for making memory usage more efficient
in a paged memory environment - E.g., in message passing (e.g., Mach), or in fork
- Initially, no additional pages have to be
allocated for new process or for message data,
and no pages need to be copied - Only requires changes to page tables of processes
- Method
- Processes initially share pages, but pages are
marked as copy-on-write (e.g., a bit in page
tables) - Processes can freely read from the pages
- When one of the processes tries to write the
copy-on-write page, a copy of the page is made
for the writing process - Changes entries in the process page tables
95Shared Memory
- The same frame of memory may be mapped into the
address space of two heavy-weight processes - System calls are used to establish shared memory
frames - E.g., shmat, shmctl, shmget, shmdt (BSD Unix, Mac
OS) - Similar to copy-on-write, both processes may be
able to write to the same frame of memory - But, process of writing does not change page
tables - Pages may also be marked as read-only
- If processes can write to shared pages, seems to
work against address space protection - Why do this?
- Can be used to enable communication between the
two heavy-weight processes
96Figure 9.3 Shared Library Using Virtual Memory
97Page Locking
- Sometimes important to keep pages resident
- E.g., when a process does I/O we may want to
ensure that the I/O device can write to a
resident page - E.g., for pages in standard libraries, we may
want to load these libraries once and keep pages
in memory - Associate a lock bit with each frame
- When frame is locked, it cannot be selected for
replacement (cannot be a victim)
98Exercise
- Problem 10.11, page 367, solve for four frames
only.
99(No Transcript)