Common to all versions of paging we looked at are the following assumptions.
Common to address translation by the CPU in all versions of paging we looked at are the following.
1 bit 19 bits 12 bits
| which page table | virtual page number | offset |
virtual page number page frame number
0 PFN0 1 PFN1 2 PFN2 ... ...
The above is how the CPU translates byte addresses from compiler-generated to actual physical RAM byte address since everything is stored as binary bits. We humans do computations in decimal (base 10) and this Java hack (addressTranslate.java) shows how we perform address translation.
Paging.
The entire process (all code-data-heap pages and all stack pages) must be loaded into RAM before the process can start executing. All process code and data is copied from the executable file into RAM page frames and enough RAM page frames are pre-allocated for the compiler's guess about the heap and stack sizes during the program execution.
When a process is to be executed, the OS builds two page tables for the process. The size of each page table is based on the compiler's guess about how much heap and stack the process will need when it executes. Each page table entry is loaded with a PFN drawn by the OS from the free page frame list.
Each PTE consists of one field: the PFN containing that page of the process.
A page table is allowed to grow if the compiler's guess about heap size or stack size is exceeded. If a stack push occurs onto a stack which has reached the end of the last page frame allocated to the stack, the OS increases the stack page table length by one, fills the new PTE with a PFN drawn from the free list, and allows the process to continue executing.
Demand paging with no cap or maximum placed by the OS on the number of page frames a process can own (have allocated to it).
Each PTE has a valid bit (VB) field in addition to a PFN field. A VB of 1 means the PFN field is valid and contains a PFN earlier drawn by the OS from the page frame free list; a VB of 0 means the PFN field is null.
virtual page number page frame number VB
0 PFN0 x 1 PFN1 x 2 PFN2 x ... ... ...
The OS starts a process executing with just a few pages loaded into RAM, for example, one page of code, one page of data, and one page of stack.
This means each process starts executing with its page tables mostly empty: one PTE for code has a VB of 1, one PTE for data has a VB of 1, one PTE for stack has a VB of 1, the rest of the VBs are 0.
During address translation, if the CPU detects a VB of 0 in a PTE, the CPU generates an exception (interrupt) called a page fault. The OS page fault interrupt handler draws a PFN from the free list, puts the PFN into the PTE, and changes the VB from 0 to 1. If the PTE corresponds to a page of process code or process data in the executable file produced by the compiler, the OS will initiate a disk read from the executable file into the page frame; otherwise the page frame is left unitialized or filled with zeros. During this disk read, the process is removed from the ready queue, and some other ready process is scheduled to use the CPU. When the disk read is done, the process is put back into the ready queue.
Note that if a process does not ever use any part (page) of its virtual address space, then that part (page) is never loaded into RAM.
Demand paging with an explicit cap or maximum placed by the OS on the number of page frames a process can own (have allocated to it).
When a process is to be executed, the OS calculates a cap or maximum on the number of page frames the process can own while executing, based on the process priority, on the size of the process guessed by the compiler, and on the number of page frames currently in the free page frame list. This is a policy decision (the president gets a big cap, students a small cap). The OS saves this cap in the PCB.
Each PTE has (in addition to PFN and VB fields) a modified bit (MB) field, a referenced bit (RB) field, a 16-bit age field, a swapped out bit (SB), and a swap area address.
virtual page number page frame number VB MB RB age field SB swap address
0 PFN0 x x x xxxxxxxxxxxxxxxx x xxx 1 PFN1 x x x xxxxxxxxxxxxxxxx x xxx 2 PFN2 x x x xxxxxxxxxxxxxxxx x xxx ... ... ... ... ... ... ... ...
The CPU sets the MB in a PTE whenever the CPU writes into the page frame containing the page of the process corresponding to the PTE.
The CPU sets the RB in a PTE whenever the CPU uses (reads one or more bytes in, writes one or more bytes in, or executes an instruction in) the page frame containing the page of the process corresponding to the PTE.
At every clock tick, the clock interrupt handler performs the following three steps on each PTE with a VB of 1 of the process executing on the CPU when the clock interrupt occurred.
If a page fault occurs, the OS page fault handler checks to see if the current number of page frames allocated to the process (number of PTEs with VB of 1) is less than the cap. If so, the page fault handler draws a PFN from the free list, puts it into the PTE, and set the VB in the PTE to 1.
However, if the process has reached its cap, the page fault handler finds the page frame allocated to the process which was last used the longest ago. It does this by finding the lowest binary integer value of the 17 bit RB and age field concatenation in all PTEs of the process with VB of 1.
Once found, the page fault handler examines the MB of the PTE with the lowest 17 bit value. If the MB is 1, the page fault handler writes out the current contents of the page frame (whose number is in the PTE) to an area of the disk called the swapping area. The OS sets the SB bit to 1 and puts the swap area address in the PTE.
After examining the MB, the page fault handler
The reference bit RB of this page will be set to 1 as soon as the process gets the CPU back from the OS and reexecutes the instruction that had caused the page failt. Perhaps as part of handling the page fault, the OS should set the age field to all ones to ensure that the page that caused the fault, now in RAM, is marked as the most recently used page and is not removed from RAM earlier than it should be because its age field started out as all zeros. Rowan computer science major David Galos noticed this fall 2009. On the other hand, spatial and temporal locality will most likely lead to the age field of this page rapidly filling with 1 bits.
If the PTE that caused the page fault corresponds to a page of process code or process data in the executable file produced by the compiler, the OS will initiate a disk read from the executable file into the page frame and some other process in the ready state is scheduled to use the CPU. If the PTE that caused the page fault corresponds to a page that was previously written to the swaping area of the disk (SB is 1), the OS will initiate a disk read from the swaping area into the page frame and some other process in the ready state is scheduled to use the CPU. When this disk read is done, the process is put back into the ready queue.
Demand paging with a paging daemon.
Page faults by a process are handled as in Version 2 since there is no explicit cap. See the comment above about filling the missing page's age field with ones instead of zeros. If the PTE that caused the page fault corresponds to a page of process code or process data in the executable file produced by the compiler, the OS will initiate a disk read from the executable file into the page frame. If the PTE that caused the page fault corresponds to a page that was previously written to the swaping area of the disk, the OS will initiate a disk read from the swaping area into the page frame.
If the number of free page frames of RAM drops below a ``danger'' (uncomfortable) level, as detected by the page fault handler, a thread in the OS called the paging daemon, is activated. The paging daemon examines all PTEs with VB of 1 of all processes currently in the system, looking for page frames last used a long time ago. The paging daemon looks for low values (say less than 16) of the 17 bit RB age field concatenation. For each such PTE found, the paging daemon looks at the MB and if the MB is 1, writes the contents of the page frame whose number is in the PTE to the disk swaping area. The PFN is added to the free list. Then the paging daemon zeros out the VB, MB, RB, and age field, and sets the PFN to null in the found PTE.
The paging daemon scans PTEs until the size of the page frame free list is built up to a comfortable level, somewhat above the ``danger'' level.
If the paging daemon scans all PTEs and the size of the page frame free list is still uncomfortable, the paging daemon will rescan all PTEs, this time using a higher value to compare against the 17 bit RB age field concatenation (such as 64).
If the paging daemon still can't get the page frame free list size up to a comfortable level, the OS can swap out an entire process to the swaping area of the disk by writing out all page frames (with MB of 1) owned by a process. The priority of a process, how long it has been executing, how many page frames it owns can be used by the OS to decide which process to swap out.
Note that if a process does not ever use any part (page) of its virtual address space, then that part (page) is never loaded into RAM. And if a process does not use for a long time any part (page) of its virtual address space, then that part (page) will have its containing page frame reclaimed by the OS if free RAM page frames are in short supply.
External versus internal fragmentation. External fragmentation is when the hole list has a bunch of too small holes. This does not occur in paging. However, part of a page frame might be wasted by a process since the exact size of a process is rarely an exact multiple of the page size. This is called internal fragmentation.
Spatial and temporal locality. Most programs exhibit certain patterns when they execute. Locality refers to the pattern that a program will reference with high probability in the near future the stuff (code, data, heap, stack) it has referenced in the recent past and/or stuff near that stuff. Most code and data is accessed sequentially, and this is called sequential locality. A program will reference with high probability in the near future stuff close to stuff it has referenced in the recent past. Therefore, if a program uses a page once, there is a high probability the program will use that same page again in the near future. If an array or procedure is referenced inside a program loop, this pattern is called temporal locality. The data of the array or code of the procedure will be referenced again and again during a short span of time. These patterns of program behavior are what make paging usually work well in a computing system. If the paging daemon takes a page frame away from a process, the process is probably done with the contents of that page frame anyway, so the process will not ``miss'' it. Processes usually execute with a reasonable number of page faults in a memory allocation (number of page frames allocated to the process) substantially less than the size of the process guessed by the compiler.
Translation lookaside buffer (TLB). This is a cache memory next to the CPU that holds PTEs recently used by the CPU to translate addresses. The CPU will check the TLB for a PTE it needs to translate an address to avoid a RAM access if possible. If the TLB does not contain a needed PTE, the CPU reads RAM (the page table in the OS page frames) and saves the PTE in the TLB for future use since there is a nearly 100 percent chance the CPU will use the PTE again soon. The TLB can be accessed in one tenth the time that it takes the CPU to access RAM. A TLB containing 64 or so PTEs eliminates 98--99 percent of CPU accesses to RAM for PTEs.
Page size. If the page size is too small, then page tables become huge in size and take up more RAM. Also it will take more disk seeks to handle the increased number of page faults since a process has more smaller pages. And the same-sized TLB will fill up more quickly and not work as well. The paging daemon has to do more work scanning more page table entries.
If the page size is too large, then internal fragmentation becomes excessive. Also, each page fault has more chance to bring into RAM stuff the program will never use or not need now. The paging daemon will not be able to reclaim it.
So the page size has to be ``just right.''
home page:
http://elvis.rowan.edu/~hartley/index.html
e-mail:
hartley@elvis.rowan.edu