Virtual Memory - Computer Science

advertisement
Operating Systems Implementation
COMP3300/COMP6330
Virtual Memory
Eric McCreath and
Bob Edwards
Research School of Computer Science
The Australian National University
Introduction
Virtual memory is a technique that permits processes to be executed even when they are not completely in memory.
This has many advantages, including:
programs can be larger than physical memory,
virtual memory abstracts main memory into an extremely large logical storage area, and
virtual memory increases the degree of multi­
programming.
However, it is complex to implement and can dramatically decrease performance if it is used carelessly.
2
Introduction
●
●
●
●
●
Processes often only use a small part of their text (machine code instruction) space at any given time
Also data structures, such as arrays, lists, and tables, are often allocated more memory that they actually use.
The programming task is simplified by not being constrained by the physical memory.
Memory overlays have basically disappeared on systems which support virtual memory.
Virtual memory should be transparent to the user.
3
Introduction
●
The diagram below shows virtual memory larger than physical memory.
Virtual Memory
Physical Memory
Secondary Storage
Memory Map
4
Demand Paging
“Swapping” is an approach that involves bringing an entire process from disk into main memory so it may be executed.
Demand­paging is a lazy “swapper” which brings into memory the pages of the process that are needed. The pager is concerned with the manipulation of the pages to and from disk.
5
Hardware Support
●
●
●
If a logical address is accessed that is not in physical memory then the pager must bring this page into memory.
This requires some hardware support to distinguish between pages that are in memory and those that are on disk. A valid­invalid bit in the page table may be used to achieve this. If a page is invalid then either it is on disk or the page is not within the logical address space.
Also, the CPU must be able to restart an instruction that has causes a page fault.
6
Demand Paging
●
7
A page fault occurs when an invalid page is addressed.
Memory
Page Table
0 5 RW v
1 RO i
2 8 RO v
3
i
Valid/Invalid Bit
Read Only/Read Write Bit 0
1
2
3
4
5
6
7
8
Steps in dealing with a page fault
●
The process:
Kernel
Error­ Address outside logical address space
Logical Memory of Process A
Invalid Page Reference
Get Page from disk
(dispatch another process)
Page Table
Trap
i
Physical Memory
Restart Instruction
Update page table
Copy page into free frame
8
Pure demand paging
●
One approach to paging is to only bring pages into memory when they are needed. This is called Pure demand paging. The very first instruction would cause a page fault.
9
Hardware constraints
●
●
●
A page­fault may occur in the middle of an instruction. Paging requires that the instruction is restarted so it is in exactly the same state before the page fault occurred
This generally involves restarting the instruction fetch­
execute cycle.
Problems arise when one instruction modifies several different locations. Examples of this are:
­ IBM system 360/370 – instruction that can move up to 256 bytes
­ PDP­11 ­auto­increment and auto­decrement addressing models.
10
Performance
●
●
11
Demand paging can significantly decrease the performance of a computer system by greatly increasing the effective access time of memory.
Given ma is the memory access time. Let p be the probability of a page fault occurring. The effective access time is then:
effective access time = (1­p) x ma +
p x page fault time
EXAMPLE: Suppose the average page fault service time is 20 milliseconds and a memory access time is 200 nanoseconds. Suppose we wish to have less that 10% degradation what must the page fault rate be less that?
Swap Space
●
●
●
Demand paging must manage the swap space. The swap space is generally faster than the file system, as file lookups and indirect allocation methods are not used.
When a process is started, one approach is to copy the entire program into the swap space and then to demand page the pages from the swap space.
Another approach is to page the program directly from the file system the first time a page is used, and then to use the swap space for the following page­faults.
12
Page Replacement
●
●
Once the main memory fills up a page must be swapped out to make room for any pages to be swapped in. This is known as page replacement.
The page­fault service routine will:
–
Find the location of the desired page on disk
–
Either find a free frame or create a free frame by: selecting a victim frame, writing it to disk, and updating the page and frame tables.
–
Read the desired page into the free frame.
–
Restart the process.
13
Page Replacement
●
●
14
Page replacement requires two page transfers.
One approach to reduce this overhead is by using a dirty bit in the page table. When a page is modified the dirty bit is set. Suppose a page fault occurs and a victim is selected. If the dirty bit of the victim is set then the service routine must write this page to disk, however, if the bit is not set then there is no need to write the page to disk as there already exists the identical page on disk.
Demand Paging
●
Demand paging requires a:
–
Frame­allocation algorithm ­ How many frames do we allocate to each process?
–
Page­replacement algorithm – How do we select the victim to be replaced?
15
Page­Replacement Algorithms
●
●
●
The goal of the page­replacement algorithm is to minimise the page­fault rate.
Different algorithms may be compared by computing the number of page faults on a particular reference string of memory accesses.
Given the overhead of a page fault, small improvements in the page replacement algorithm will greatly improve the performance of the entire system.
16
Page­Replacement Algorithms
●
For example suppose the pages are 1000 bytes each. And if we trace a particular process it may reference the following logical addresses:
02001, 03234, 02002, 04345, 03345, 02998, 03987, 07111, 02000, 02001
This would reduce to the following reference string:
2, 3, 2, 4, 3, 2, 3, 7, 2, 2, Given the number of available frames and the page replacement algorithm used we can determine the number of page faults.
17
Page­Replacement Algorithms
●
Generally, increasing the number of frames reduces the number of page faults.
Page Faults
Frames
18
FIFO Algorithm
●
●
19
The FIFO is the simplest page­replacement algorithm. When a page fault occurs and the page frames are full a victim must be selected. The FIFO algorithm selects the oldest frame (this is the frame that has been in memory the longest). This page is swapped out and the required page is swapped into its location.
The performance of FIFO is generally poor, as heavily used frames become old and are paged out, only to be paged immediately back in.
FIFO Algorithm
●
Consider the following reference string with 3 frames available:
1 3 1 2 4 1 4 3 4 4 5 6 4
20
FIFO Algorithm
●
●
FIFO suffers from Belady's anomaly where an increase in the number of available frames may in fact increase the number of page faults.
Consider the below reference string with either three or four frames available:
●
1 2 3 4 1 2 5 1 2 3 4 5
1 2 3 4 1 2 5 1 2 3 4 5 21
Optimal Algorithm
●
●
An optimal page­replacement algorithm exists and is called OPT or MIN. The approach is to simply replace the page that will not be used for the longest period of time.
The optimal algorithm, like the Shortest Job First (SJF) algorithm for CPU scheduling, is impossible to implement as it requires the future knowledge of the reference string.
22
Optimal Algorithm
●
Consider the following reference string with 3 frames available:
●
●
1 3 1 2 4 1 4 3 4 4 5 6 4
23
LRU Algorithm
●
●
●
24
The page references that occurred in the recent past are good indicators of what page references will occur in the future. That is if a page has just been referenced it is likely that it will be referenced again. This gives rise to the least recently used (LRU) algorithm.
The LRU keeps track of how recently a page has been used and keeping the pages that have been recently used. LRU selects the page that reference is the most distant past.
Note that, the LRU is identical to running OPT on the reference string in reverse order.
LRU Algorithm
●
Consider the following reference string with 3 frames available:
1 3 1 2 4 1 4 3 4 4 5 6 4 25
LRU Algorithm
●
26
Two implementations are feasible:
­ counters, or
­ stack
Although it is possible to implement LRU it is impractical due to the amount of work that needs to be done for each memory reference.
LRU Approximation Algorithms
●
●
Most architectures help the operating system approximate the LRU algorithm by providing a reference bit. The reference bit associated with pages and is located in each entry in the page table.
Initially the operating system will set the reference bits to 0. When a page is referenced the corresponding reference bit is set to 1. This is all done in hardware. After a period of time the operating system can examine all the reference bits and determine which pages have been read from or written to in that period of time. Note that the order and frequency that pages have been referenced in unknown.
27
Reference Bit
28
Physical Address Space
Frame Number
0
Logical Address Space
Process A
P0
P1
P2
P3
Page Table A
3
0
v
0
2
3
1
1
v
10
v
2
3
i
1
0
1
P0 A
4
5
6
Frame
7
Valid­Invalid
Reference Bit
8
9
10
P2 A
11
P3 A
Reference Bit History
●
●
●
The reference bit may be sampled at regular intervals and shifted into a register. A timer interrupt may be used to undertake this task regularly.
eg. A page with a history register 01100111 was not accessed in the previous time interval, however it was accessed in the time interval before that, etc.
These history registers may be compared to select a victim. Thus approximating the LRU algorithm. 29
Second­Chance Algorithm
●
●
●
The second­chance page replacement algorithm is a FIFO replacement algorithm where referenced pages are given a second chance.
A circular queue may be used to implement the second­chance algorithm. When a victim is required the pointer advances around the queue until if finds a page with a 0 reference bit. As the pointer advances the reference bits are set to zero.
If all bits are set then the second chance is the same as the FIFO replacement algorithm.
30
Second­Chance Algorithm
1
1
1
Pointer
0
1
0
1
Page
0
1
Reference Bit
31
Enhanced Second­Chance Algorithm
32
●
●
If the victim has not been modified then it is not required to be copied to disk. This makes these victims more attractive as the required page can be obtained in half the time.
This gives rise to the enhanced second­chance algorithm which selects unmodified pages over modified pages.
Page Buffering Algorithms
●
A pool of free frames may be maintained. When a page fault occurs, a victim is chosen as before. However, the required page is read into a free frame from the pool of free frames. The process may continue once this is complete without waiting for the victim to be written out to the swap space. The victim may be copied to the swap space at the pages leisure and its frame will form part of the pool of free frames.
Frames
Victim
Swap Space
Frames
Victim
1
2
Free Frame
Swap Space
2
1
33
Allocation of Frames
●
There exists a minimum number of frames that must be allocated to each process. This number is dependent on the instruction set, as, one instruction may require access to several memory locations.
34
Allocation of Frames
Frame allocation to processes may be either:
global, or
local
A variety of approaches may be used when locally allocating the number of frames to give to a process.
These include:
equal allocation,
proportional allocation (size of the process), or
proportional allocation (priority).
35
Thrashing
●
●
A process will have a number of frames in active use (working set). If a process does not have at least this number of frames allocated to it then the process will be constantly paging. This is known as thrashing.
Generally the problem is fixed by reducing the degree of multiprogramming.
CPU Utilisation
thrashing
Degree of Multiprogramming
36
Program Structure
●
You spend weeks writing the following program and for some reason it is very slow. What is the problem? How could you fix it? (Assuming pages are 4K and integers are 4 bytes.)
#define SIZE 1024
int data[SIZE][SIZE];
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
A[k][j] = 0;
}
}
37
Download