Paging Design and Implementation Issues
- Local v. Global
- Global: The replacement algorithm considers all pages in memory.
- Local: Replacement only considers pages owned by the faulting
- With global, processes which need more pages naturally obtain them.
Local requires a policy to modify the number of pages a process has.
- Measuring the Page Fault Frequency (PFF) for each process can reveal
which is suffering from its allocation size.
- Load Control
- Measure the overall PFF to see if the system is overloaded.
- May want to swap a process out to reduce load on the VM system.
- The process with the largest PFF is the obvious choice, but
need to consider priorities, and keep enough processes running
to keep the CPU busy.
- Page Size
- Basic page size is determined by hardware.
- An OS can allocate pages in (small)
groups, simulating larger pages for some purposes.
- Most common size is 4K; systems have used 512 bytes to 64K.
- Advantages of larger pages.
- Smaller page table, and fewer TLB entries needed.
- For page I/O, less time per byte moved. (I/O operations have
a fixed time plus a variable time.)
- Advantages of smaller pages.
- Reduced internal fragmentation in the last page.
- For page I/O, less time per page.
- When a reference brings a page into memory, less chance
some of its contents is unrelated and not actually needed.
- Derive a page size to minimize space overhead.
s: The average process size in bytes.
p: The page size in bytes.
e: Size of a page table entry in bytes.
- Each process needs s/p pages, using se/p bytes of
page table entry space.
- The unused space (waste) in the last page will average p/2.
- overhead = se/p + p/2
- First derivative wrt p is −se/p2 + 1/2
- Set to zero, and we get √2se
- For s = 1MB and e = 8 bytes, optimum page size
- Starting a Program
- When a program is run, it isn't loaded into memory.
- A page table is created, with all entries invalid.
- Execution begins and needed pages are brought in on demand.
- Only the parts of the program actually run are ever loaded into memory.
- Shared Pages
- Each process has its own page table. The same page may be entered into
more than one PT to allow sharing.
- Creates extra record-keeping so shared pages aren't immediately
removed when one process exits.
- Explicit sharing for communication.
- Two processes may request a region of memory be mapped into both their page
- Allows process to share information as threads do.
- Also creates the same sort of synchronization problems that shared data structures
- Sharing can be created by the OS, invisible to the process, to reduce real memory use.
- Sharing read-only areas.
- A read-only region can be mapped into multiple page tables.
None of the processes can detect the sharing.
- Particularly useful for code, since it is always read-only.
- When multiple processes run the same program, avoids loading into
memory multiple times.
- Sharing read-write areas.
- If two processes simply share a writable area, the processes can easily
detect that fact.
- To give each process the appearance of having its own copy, a
process called copy-on-write is used.
- Pages are referenced by multiple page tables.
- All shared pages are marked read-only, even though the data is
intended to be writable.
- When a write causes a fault, the O/S makes a copy of the page,
and updates the PT of the faulting process to refer to the copy.
- The modified PTE is marked writable. (If the original page is no longer shared,
its PTE can be marked writable also.)
- The program resumes with a private, writable copy of the page.
- None of the processes can detect what just happened.
- Only pages actually modified are copied.
- The Linux fork operation makes a COW copy of the calling process.
- Shared Libraries
- Modern systems do not actually add all libraries to a compiled
- The first call loads the library at run time.
- The first process to load the library just maps it into the page table.
- Additional users of the library share the pages, which are read-only.
- Mapped Files
- A file can be mapped into memory using paging.
- Make page table entries that are invalid, but the disk image of each
page is the corresponding portion of the file.
- Can effectively perform I/O by memory reference.
- Cleaning Policy
- Instead of running the replacement algorithm when a fault occurs,
most OS's keep a pool of available pages.
- A paging daemon runs periodically and checks if there are
enough pages in the pool. If not, it runs the replacement alg. and
to add more.
- When a page enters the available pool, it is written back if modified,
but its contents remain in memory. If that page is needed again
before it is actually replaced, it just be reclaimed from the pool
without any I/O cost.
- User Interface. Primarily, VM operation is transparent to the programmer.
But there may be some operations:
- Ask for a shared memory region. Allows fast communication.
- Ask to memory-map a file.
- Configure a distributed shared memory.
- Pages on different system can map to the same data.
- Fault handling requires a network operation.
- OS Involvement: What the OS has to take care of to make VM work.
- On process creation, set up paging.
- Allocate space for page table. Initial
size usually based on size of the executable program.
- Swap area on disk must be set up so data can be accessed on a fault.
More on this later.
- Information about the page table and swap area are recorded in the
process table entry.
- When a process changes state to running.
- Reset the MMU to clear out information from the previous
virtual space. The TLB will be flushed.
- Make the new page table current by placing a pointer in some
particular register, or possibly copying the whole table.
On a Pentium, the page table address goes in a
register is called cr3
- Possibly bring in some pages to prevent later faults.
- When a fault occurs.
- Compute which page is needed and find it on disk.
- Find a frame, evicting a current one if needed.
- Move the needed page into the frame.
- Restart the instruction.
- When a process exits, clean up.
- Free the page table space, and mark its real pages available.
Modified pages backed by a file may need to be written to disk.
- But only for the pages no other process is using.
- Page fault detail
- A trap saves the PC, and usually some other information, and
transfers to the OS.
- An assembly-language trap handler saves the general registers,
then calls a handler in the OS, usually written in C or a
- Determine the virtual address which caused the fault
- Convenient hardware has saved this in a register.
- Inconvenient hardware requires the OS to fetch the instruction
pointed to by the saved PC, and figure out what it was doing.
- If the address is invalid, or the fault represents a protection
violation, the program is generally terminated.
- Find a free frame. Either one is not in use, or from
a page pool, or by running the replacement algorithm.
- If the chosen frame is modified, schedule a write and suspend the
faulting process. The frame is marked busy so it isn't
accidentally reused while waiting.
- When the page is clean (after write-back or immediately),
find the needed page on disk and schedule a read to place it into
memory. The process remains suspended until this finishes.
- When the disk interrupts to indicate read completion, the page table
is updated and marked for normal use (remove busy mark).
- Depending on the architecture, the PC value saved at the fault may
need to be adjust to point to the start of the faulting
instruction. On some architectures, partial results may need to
- Eventually, the faulting process is scheduled to run again.
The assembly-level fault handler resumes. This reloads the state
information (including the PC), returns to user space, and the
program retries the instruction which faulted.
- Instruction Backup
- On some architectures leave a mess after a page fault.
- The hardware may interpret the instruction in steps, and be part
way through when the fault occurs.
- An instruction may have multiple arguments in memory, any one of
which could fault.
- Some architectures auto-increment registers.
- The OS will need to figure out which address faulted, which PC
value is the start of the instruction, and reverse any auto-increments.
- Hardware may give more or less help.
- Newer architectures are simpler to deal with than this.
- Locked Pages.
- If an DMA operation is moving information to or from a
some page, it must not be paged out.
- The OS marks the page as locked so the replacement algorithm
will ignore it.
- Backing Store.
- Each page must have a backing location on disk.
- A swap area
- A partition or file holding the on-disk copies.
- Disk locations computed by adding the virtual page number to
the start of the disk area.
- Information may need to be copied to the swap area first.
- Dynamic location.
- Place pages wherever there's room.
- Requires a directory in memory to map from virtual page number
to disk location.
- Memory-mapped files, including running executables and shared
libraries, are treated as small swap areas.
- Separation of Policy and Mechanism.
- Move replacement policy and backing store management to user space.
- Faults reach the kernel, which requests pages from paging server.
- User can control paging policy.
- Different processes can use different approaches as desired.
- Something your author likes, but not widely used.