6 Swapping
6 Swapping
COMS(3010A)
Swapping
Branden Ingram
[email protected]
Recap
• Paging
• Translation with paging
• Memory accessing
• TLB
• Replacement Policies
Ok well where are these tables stored?
• Recall also: we usually have one page table for every process in the system
• With a 100 active processes (not uncommon on a modern system),
• we will be allocating hundreds of megabytes of memory just for page tables!
So how can we reduce this burden?
• 32 bit address space with 4KB pages, 20bits for the VPN
• 4MB = 220 entries x 4 bytes per page table entry
• 32 bit address space with 16KB pages, 18bits for the VPN
• 1MB = 218 entries x 4 bytes per page table entry
• Instead of having a single page table for the entire address space of the process,
why not have one per logical segment?
• In this example, we might thus have three page tables, one for the code, heap, and
stack parts of the address space.
Page Table
Segment Register Page Table
Page Page Frame
Segment Base Size Page Table
Page
0 Page3 Frame
Code 256KB 4KB
Heap 260KB 60KB 1 0Page 7Page
3 Frame
Stack 128KB 64KB 2 1 0 5 7 3
3 2 1 2 5 7
3 2 2 5
3 2
A hybrid Approach
• Now, remember with segmentation, we had a base register that told us where each
segment lived in physical memory, and a bound or limit register that told us the size
of said segment.
• In our hybrid, we still have those structures in the MMU; here, we use the base not
to point to the segment itself but rather to hold the physical address of the page
table of that segment. The bounds register is used to indicate the end of the page
table (i.e., how many valid pages it has).
VPN Offset
VPN Offset
SEG PT index
Example
Page Table
Segment Register Page Table
Page Page Frame
Segment Base Size Page Table
Page
0 Page3 Frame
Code 12KB 4
Heap 3KB 4 1 0Page 7Page
3 Frame
Stack 7KB 4 2 1 0 5 7 3
3 2 1 2 5 7
3 2 2 5
3 2
Address Translation : Example
01 heap 256
3 2
011 000010
PFN Offset
0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0
A hybrid Approach
• The critical difference in our hybrid scheme is the presence of a bounds register per
segment; each bounds register holds the value of the maximum valid page in the
segment
• However, as you might notice, this approach is not without problems. First, it still
requires us to use segmentation; as we discussed before, segmentation is not quite as
flexible as we would like
Multi-level Paging
• A different approach doesn’t rely on segmentation but attacks the same problem:
• how to get rid of all those invalid regions in the page table instead of keeping
them all in memory?
• We call this approach a multi-level page table, as it turns the linear page table into
something like a tree
Multi-level Paging
• The basic idea behind a multi-level page table is simple. First, chop up the page table
multiple levels of smaller tables, creating a hierarchical structure
VPN Offset
VPN Offset
PD PT index
Multi-level Paging
• The basic idea behind a multi-level page table is simple. First, chop up the page table
multiple levels of smaller tables, creating a hierarchical structure
Page Table
Page Table
Page Table Page Page
Page Frame
Table
Page PagePage Frame
Table
0 Page 3 Frame
Page
Page Directory Page Table 0 Page 3
1 7 Page Frame
0 3 1 0 7 3
2 0 5 3
1 7 2 1 5 7
3 1 2 7
2 5 3 2 2 5
2 5
3 2 3 2
3 2
Address Translation : Example
1 7
01 111
3 2
011 000010
PFN Offset
0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0
Comparison to Standard Paging
Multi-Level Paging
• 32 – 12(offset) = 20bits for VPN • The top-level page table has 2^10 (1024)
• 2^(20) = 1,048,576 pages entries.
• Each entry points to a second-level page
table, which also has 2^10 entries.
• 2^10 ×2^10 =2^20 = 1,048,576 pages
Advantages
• Flexibility: Multi-level paging can adapt to different sizes of memory by adding more
levels or changing the sizes of the tables at each level.
Disadvantages
• Additional Overhead: Every memory access may involve multiple page table
lookups, which can slow down address translation. However, modern processors often
mitigate this with Translation Lookaside Buffers (TLBs) that cache recent address
translations.
Swapping
Registers
Cache
Main Memory
Registers
Cache
Main Memory
Registers
Cache
Main Memory
• OS need a place to stash away portions of address space that currently aren’t in
great demand.
• In modern systems, this role is usually served by a hard disk drive
Registers
Cache
Main Memory
• At some later time, the system swaps back the process from the secondary storage
to main memory.
Why would we need swapping
• Swapping makes use of a swap space which allows the OS to support the illusion
of a large virtual memory for multiple concurrently-running processes
Swap Space
• Swapping makes use of a swap space which allows the OS to support the illusion
of a large virtual memory for multiple concurrently-running processes
• Reserve some space on the disk for moving pages back and forth.
• OS need to remember to divide the swap space, in page-sized unit
Swap Space
• Swapping makes use of a swap space which allows the OS to support the illusion
of a large virtual memory for multiple concurrently-running processes
• Reserve some space on the disk for moving pages back and forth.
• OS need to remember to divide the swap space, in page-sized unit
PFN 0 PFN 1 PFN 2 PFN 3
• Add some machinery higher up in the system in order to support swapping pages
to and from the disk.
• When the hardware looks in the PTE, it may find that the page is not present in
physical memory.
Value Meaning
What if we try to access a page not in
memory?
Secondary Storage
2.Trap
1. Reference
Load M
i
6. reinstruction
Page Frame
Page Frame
Virtual Address
When the OS receives a page fault, it looks in the PTE and issues the request to disk.
Page Fault Control
1: PFN = FindFreePhysicalPage()
2: if (PFN == -1) // no free page found
3: PFN = EvictPage() // run replacement algorithm
4: DiskRead(PTE.DiskAddr, pfn) // sleep (waiting for I/O)
5: PTE.present = True // update page table with present
6: PTE.PFN = PFN // bit and translation (PFN)
7: RetryInstruction() // retry instruction
The OS likes to page out pages to make room for the new pages the OS is about to
bring in.
The process of picking a page to kick out, or replace is known as page-
replacement policy
When do page replacements actually occur?
• OS waits until memory is entirely full, and only then replaces a page to make room
for some other page
• This is a little bit unrealistic, and there are many reason for the OS to keep a
small portion of memory free more proactively.
When do page replacements actually occur?
• Swap Daemon
• There are fewer than “free_page_low” pages available, a background thread
that is responsible for freeing memory runs.
• The thread evicts pages until there are “free_page_high” pages available.
• Page Daemon
• Periodically checks which pages can be discarded
Summary
• Memory pressure forces the OS to start paging out pages to make room for actively-
used pages.
• Deciding which page to evict is encapsulated within the replacement policy of the OS.
Cache Management
• Goal in picking a replacement policy for this cache is to minimize the number of
cache misses
Cache Management
• Goal in picking a replacement policy for this cache is to minimize the number of
cache misses.
• The number of cache hits and misses let us calculate the average memory access
time(AMAT).
Argument Meaning
TM The cost of accessing memory
Phit The probability of finding the data item in the cache(a hit)
Pmiss The probability of not finding the data in the cache(a miss)
• For example, let us imagine a machine with a (tiny) address space: 4KB, with 256-
byte pages.
• Thus, a virtual address has two components: a 4-bit VPN (the most-significant bits)
and an 8-bit offset (the least-significant bits).
• Thus, a process in this example can access 16 total virtual pages.
Cache Management - Example
• In this example, the process generates the following memory references (i.e., virtual
addresses): 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800, 0x900.
• These virtual addresses refer to the first byte of each of the first ten pages of the
address space (the page number being the first hex digit of each virtual address).
• Consider 0x800 the first hex digit is 816 =10002 Therefore the VPN for 0x800 = 10002
Cache Management - Example
• (i.e., virtual addresses): 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700,
0x800, 0x900.
• Consider 0x800 the first hex digit is 816 =10002 Therefore the VPN for 0x800 = 10002
• Let us further assume that every page except virtual page 3 is already in memory.
Thus, our sequence of memory references will encounter the following behaviour:
hit, hit, hit, miss, hit, hit, hit, hit, hit, hit.
Cache Management - Example
• (i.e., virtual addresses): 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700,
0x800, 0x900.
• Consider 0x800 the first hex digit is 816 =10002 Therefore the VPN for 0x800 = 10002
• hit, hit, hit, miss, hit, hit, hit, hit, hit, hit.
• We can compute the hit rate (the percent of references found in memory): 90%, as
9 out of 10 references are in memory. The miss rate is thus 10% (PMiss = 0.1). In
general, PHit + PMiss = 1.0
Cache Management - Example
• 1Microsecond = 1000nano
• 100ns + 0.1*10(1000) = 1100
Cache Management - Example
• We would expect the cache hit rate to increase when the cache gets larger. But in
this case, with FIFO, something strange occurs.
Reference Row 1 2 3 4 1 2 5 1 2 3 4 5
14
12
Page Fault Count
10
0
1 2 3 4 5 6 7
Page Frame Count
A Simple Policy: Random
50
40
Frequency
30
20
10
0
1 2 3 4 5 6
Number of Hits
Historical
Meaning Algorithms
Information
Reference Row 0 1 2 0 1 3 0 3 1 2 1
100%
80%
When the cache is large enough to fit
Hit Rate
20%
20 40 60 80 100
Cache Size (Blocks)
Workload Example : The 80-20 Workload
• Exhibits locality: 80% of the reference are made to 20% of the page
• The remaining 20% of the reference are made to the remaining 80% of the pages.
100%
80%
LRU is more likely to
hold onto the hot pages.
Hit Rate
60%
OPT
LRU
FIFO
40%
RAND
20%
20 40 60 80 100
Cache Size (Blocks)
Workload Example : The Looping Sequential
•Refer to 50 pages in sequence.
• Starting at 0, then 1, … up to page 49, and then we Loop, repeating those
accesses, for total of 10,000 accesses to 50 unique pages.
The Looping-Sequential Workload
100%
80%
Hit Rate
60%
OPT
LRU
FIFO
40%
RAND
20%
20 40 60 80 100
Cache Size (Blocks)
Implementing Historical Algorithms
• To keep track of which pages have been least-and-recently used, the system has to
do some accounting work on every memory reference.
• Add a little bit of hardware support.
Approximating LRU
• Require some hardware support, in the form of a use bit
• Whenever a page is referenced, the use bit is set by hardware to 1.
• Hardware never clears the bit, though; that is the responsibility of the OS
Approximating LRU
• Require some hardware support, in the form of a use bit
• Whenever a page is referenced, the use bit is set by hardware to 1.
• Hardware never clears the bit, though; that is the responsibility of the OS
• Clock Algorithm
• All pages of the system arranges in a circular list.
• A clock hand points to some particular page to begin with.
Clock Algorithm
•The "Clock" algorithm, also known as the "Second-Chance" algorithm, is a page
replacement policy used in operating systems to manage memory and control
swapping.
• It's primarily used in virtual memory systems to determine which pages should be
replaced when a new page needs to be brought into memory, and there's not enough
free space available.
Clock Algorithm
• The basic idea behind the Clock algorithm is to maintain a circular list of pages in
memory, similar to the way a clock's hands move around its face. This circular list is
implemented using a data structure called a circular buffer or circular linked list
A
H B
G C
F D
E
Clock Algorithm
• Each page in the list is associated with a “Use bit” that indicates whether the page
has been accessed (read or written to).
A
H B
Use bit Meaning
G C 0 Evict the page
1 Clear Use bit and advance hand
F D
E
The Clock page replacement algorithm
Clock Algorithm
1.Initialization: Each page frame in memory is
represented in the circular list. The list is initialized to
point at a random frame.
A
H B
G C
F D
E
When a page fault occurs, the page the hand is pointing to is inspected.
The action taken depends on the Use bit
Clock Algorithm
1.Initialization: Each page frame in memory is represented in the circular
list. The list is initialized to point at a random frame.
2.Page Request: When a new page needs to be brought
A into memory, the operating system checks the page
H B frames in the circular list in the order they appear. It
starts at the current position and proceeds in a circular
G C manner.
F D
E
• It's important to note that the Clock algorithm doesn't take into account how
frequently a page has been accessed over time. This can be a limitation in
scenarios where pages have different levels of importance or where access
patterns change dynamically.
Workload Example : Clock Algorithm
• Clock algorithm doesn’t do as well as perfect LRU, it does better than approaches
that don’t consider history at all.
100%
80%
Hit Rate
60%
OPT
LRU
Clock
40%
FIFO
RAND
20%
20 40 60 80 100
Cache Size (Blocks)
Page Selection Policy
• The OS has to decide when to bring a page into memory.
• Presents the OS with some different options.
Prefetching
• The OS guess that a page is about to be used, and thus bring it in ahead of time.
Page n
Page 3
Page 4
Page 5
…
Physical Memory
Page 1
Page 2
Page 3
Page 4
…
Secondary
Storage
Page 2 likely soon be accessed and
thus should be brought into memory too
Clustering, Grouping
• Collect a number of pending writes together in memory and write them to disk in
one write.
• Perform a single large write more efficiently than many small ones.
Pending writes
Page n
Page 1
Page 2
Page 3
Page 4
Page 5
…
Physical Memory
write in one write
Page 1
Page 2
Page 3
Page 4
Secondary
Storage
Thrashing
• Memory is oversubscribed and the memory demands of the set of running
processes exceeds the available physical memory.
• Leads to a constant state of paging and page faults, inhibiting most application-
level processing
CPU
Utilization
Trashing
Degree of multiprogramming