Lec12 Swapping
Lec12 Swapping
Operating Systems
Mechanisms
Policies
Registers
Cache
Main Memory
Reserve some space on the disk for moving pages back and forth.
OS needs to remember the swap space, in page-sized unit.
Value Meaning
1 page is present in physical memory
0 The page is not in memory but rather on disk.
Page fault
Accessing page that is not in physical memory.
If a page is not present and has been swapped to disk, the OS needs to
swap the page back into memory in order to service the page fault.
Page replacement
The OS likes to page out some pages to make room for the new ones it is
about to bring in
Lazy approach…
OS waits until memory is full to start replacing pages.
The thread evicts pages until there are HW (high watermark) pages
available.
Bits used for data such as the page’s PFN are used for a disk address
in the PTE
3. Check storage whether the page exists.
kernel Secondary Storage
2.Trap
1. Reference
Load M
i
6. reinstruction
Page Frame
Page Frame
Address Space
When the OS receives a page fault, it looks in the PTE and issues the request to disk.
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
Notation Meaning
𝑇𝑀 The cost of accessing memory
𝑇𝐷 The cost of accessing disk
𝑃𝐻𝑖𝑡 The probability of hitting the data item in the cache
𝑃𝑀𝑖𝑠𝑠 The probability of not finding the data in the cache
0 1 2 0 1 3 0 3 1 2 1
𝐻𝑖𝑡𝑠
Hit rate is = 𝟓𝟒. 𝟔% Future is unknown!
𝐻𝑖𝑡𝑠+𝑀𝑖𝑠𝑠𝑒𝑠
0 1 2 0 1 3 0 3 1 2 1
Hit rate is
𝐻𝑖𝑡𝑠
= 𝟑𝟔. 𝟒% Even though page 0 had been accessed a number of
𝐻𝑖𝑡𝑠+𝑀𝑖𝑠𝑠𝑒𝑠 times, FIFO still kicks it out.
We would expect the cache hit rate to increase when the cache gets
larger. But in this case, with FIFO, it gets worse.
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
Random does depends entirely upon how lucky Random gets in its choice.
Historical
Meaning Algorithms
Information
The more recently a page has been accessed, the more likely it
Recency LRU
will be accessed again
If a page has been accessed many times, It should not be
Frequency LFU
replaced as it clearly has some value
0 1 2 0 1 3 0 3 1 2 1
100%
80%
20%
20 40 60 80 100
Cache Size (Blocks)
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)
100%
80%
Hit Rate
60%
OPT
LRU
FIFO
40%
RAND
20%
20 40 60 80 100
Cache Size (Blocks)
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
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
100%
80%
Hit Rate
60%
OPT
LRU
Clock
40%
FIFO
RAND
20%
20 40 60 80 100
Cache Size (Blocks)
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
should be brought into memory too
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
CPU
Utilization
Trashing
Degree of multiprogramming
Grouping
Prefetching