YouTube placeholder

Swapping

Out of Core

When we run out, there are two options:

  1. Fail, and either don’t load the process (exec()), don’t create a new process (fork()), refuse to allocate more heap (sbrk()), or kill the process if it is trying to allocate more stack.

  2. Create more space, preserving the contents of memory for later use.

Virtually Sneaky

Virtual address translation gives the kernel the ability to remove memory from a process behind its back.

What are the requirements for doing this?
  • The last time the process used the virtual address, it behaved like memory.

  • The next time the process uses the virtual address, it behaves like memory.

  • In between, whatever data was stored at that address must be preserved.

Swapping

  • The place operating systems typically place data stored in memory in order to borrow the memory from the process is on disk.

  • We call the process of moving data back and forth from memory to disk in order to improve memory usage swapping.

  • Goal: when swapping is done well your system feels like it has memory that is as large as the size of the disk but as fast as actual RAM.

  • Unfortunately, when swapping is not done well your system feels like it has memory that is as small as RAM and as slow as the disk.

TLB v. Page Faults

We distinguish between two kinds of memory-related fault:
  • TLB Fault: a required virtual to physical address translation is not in the TLB.

  • Page Fault: the contents of a virtual page are either not initialized or not in memory.

Every page fault is preceded by a TLB fault.
  • If the contents of the virtual page are not in memory, a translation cannot exist for it!

Not every TLB fault generates a page fault.
  • If page is in memory and the translation is the page table, the TLB fault can be handled without generating a page fault.

Swap Out

To swap a page to disk we must:
  1. Remove the translation from the TLB, if it exists.

  2. Copy the contents of the page to disk.

  3. Update the page table entry to indicate that the page is on disk.

swapoutexample 1
swapoutexample 2
swapoutexample 3
swapoutexample 4
swapoutexample 5
swapoutexample 6
swapoutexample 7
swapoutexample 8
swapoutexample 9
swapoutexample 10

Swap Out Speed

  1. Remove the translation from the TLB: fast.

  2. Copy the contents of the page to disk: sloooowwww…​

  3. Update the page table entry to indicate that the page is on disk: fast.

Page Cleaning

Frequently when we are swapping out a page it is in order to allocate new memory to a running process, or possibly to swap in a page.

So it would be great if swapping out were fast.

Can we prepare the system to optimize swap out?
  • Yes!

  • Each page has a dedicated place on disk.

  • During idle periods operating system writes data from active memory pages to swap disk.

  • Pages with matching content on the swap disk are called clean.

  • Pages that do not match their swap disk content are called dirty.

Swap In

When must we swap in a page?
  • When the virtual address is used by the process!

To translate a virtual address used by a process that points to a page that has been swapped out, we must:
  1. Stop the instruction that is trying to translate the address until we can retrieve the contents.

  2. Allocate a page in memory to hold the new page contents.

  3. Locate the page on disk using the page table entry.

  4. Copy the contents of the page from disk.

  5. Update the page table entry to indicate that the page is in memory.

  6. Load the TLB.

  7. Restart the instruction that was addressing the virtual address we retrieved.

swapinexample 1
swapinexample 2
swapinexample 3
swapinexample 4
swapinexample 5
swapinexample 6
swapinexample 7
swapinexample 8
swapinexample 9
swapinexample 10
swapinexample 11

(On) Demand Paging

Sometimes procrastination is useful, particularly when you end up never having to do the thing you’re being asked to do!

  • Process: Kernel! Load this huge chunk of code into my address space!

  • Kernel: Well, I’m kind of busy now, so maybe later? But I’ll make a note of it!

  • Process: Kernel! Give me 4 MB more heap!

  • Kernel: Your request is granted, but come back and ask again when you really need it.

Until:
  • an instruction on a code page is executed, or

  • a read or write occurs to a data or heap page.

  • the kernel does not load the contents of that page into memory!

Why not?
  • A lot of code is never executed and some global variables are never used. Why waste memory?

Demanded Paging

What happens the first time a process executes an instruction from a new code page?
  • That page contents are loaded from disk and the instruction is restarted.

What happens the first time a does a load or store to an uninitialized heap, stack or data page?
  • The kernel allocates a new page filled with zeros and the instruction is restarted.

Aside: Hardware-Managed TLBs

  • On certain architectures the MMU will search the page table itself to locate virtual-to-physical address translations missing from the TLB.

    • Pro: hardware is faster!

    • Con: operating system must set up the page tables in a fixed way that the hardware understands.

  • With a hardware-managed TLB, the kernel never sees TLB faults: they are handled in hardware.

  • Don’t worry: System/161 has a software-managed TLB, so you get to do all of the hard work and design your own page table structures!


Created 3/27/2017
Updated 9/18/2020
Commit 4eceaab // History // View
Built 3/26/2017 @ 20:00 EDT