Swapping
Out of Core
When we run out, there are two options:
-
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. -
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.
-
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
-
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.
-
If the contents of the virtual page are not in memory, a translation cannot exist for it!
-
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
-
Remove the translation from the TLB, if it exists.
-
Copy the contents of the page to disk.
-
Update the page table entry to indicate that the page is on disk.
Swap Out Speed
-
Remove the translation from the TLB:
-
Copy the contents of the page to disk: sloooowwww…
-
Update the page table entry to indicate that the page is on disk:
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.
-
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 the virtual address is used by the process!
-
Stop the instruction that is trying to translate the address until we can retrieve the contents.
-
Allocate a page in memory to hold the new page contents.
-
Locate the page on disk using the page table entry.
-
Copy the contents of the page from disk.
-
Update the page table entry to indicate that the page is in memory.
-
Load the TLB.
-
Restart the instruction that was addressing the virtual address we retrieved.
(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.
-
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!
-
A lot of code is never executed and some global variables are never used. Why waste memory?
Demanded Paging
-
That page contents are loaded from disk and the instruction is restarted.
-
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!