9 - Virtual Memory Management

Helpful questions for determining page table sizes etc.

Motivation

Removing the assumption that physical memory must be large enough to contain $\geq 1$ processes with complete memory space.

Secondary storage capacity

split logical address space into chunks (extension of paging)

Logical (Virtual) Memory space

Extended paging scheme

Steps:

  1. (Check page table) X is memory resident ? done : trigger-
  2. Page Fault:

Note that the page is never removed from secondary storage

Thrashing:

Summary (overall benefits)

  1. Complete separation of logical from physical memory
  2. More efficient use of physical memory
  3. More processes allowed to reside in memory

Demand Paging

Scenario: a process needs $n$ pages of memory (too slow to load all into memory at once at startup).

Page Table Structures

Tradeoffs between performance and memory consumption.

Given the following constraints, the following will be an example of time and memory needed.

Page Table Entry size

\[\text{PTE size} = \left\lceil \left( \frac{\text{Physical memory size}}{\text{Page size}} + \text{Protection bits} \right) \div 8 \text{bits} \right\rceil\]

Hence PTE size is 4B here.

Time for page hit

Next, due to the TLB, best time is always: 1ns (TLB) + 30ns (physical addr) = 31ns. The TLB has an associative memory of <page#, frame#> pairs, not page directory, so the pages can be found instantly.

Direct paging

Time for page fault: 1ns (TLB) + 30ns (page table, page fault) + 5ms (write) + 5ms (locate and load from swap) $\approx$ 10ms

Page table space:

Two-level paging

The TLB must have access to the page directory base register to access the page tables.

Address breakdown

Time and Memory space

Time for page fault: 1ns (TLB) + 10ms (Level 1 page table, page fault, write old page, load new page) + 10ms (Level 2 page table, page fault, write old page, load new page) $\approx$ 20ms

Memory Space:

Pages needed: $(2^{16} , 2^{21} , 2^{21}) / 2^{12} = 2^4 , 2^{9} , 2^{9}$ (3 page tables, each region takes up < 1 page table.)​

Level Size Count
2 (page table) $(\text{page size / PTE size})(\text{PTE size}) = 2^{12}$​​ = 4KB 3
1 (page directory) $2^{\text{vaddr-offset-lvl2}}(\text{PTE size}) = 2^{32-10-12}(4) = 2^{12}$​ = 4KB​ 1

Total: 4KB + (3)4KB = 16KB

Inverted Page Table

Motivation: With $M$​ processes, we have $M$​ page tables, but only $N$​ memory frames can be occupied ($N < M$​ overhead).

Inverted PT : frame $\mapsto$​​ <pid, page#>. [PHYSICAL $\mapsto$​​ VIRTUAL]. Then lookup with the VIRTUAL memory.

Input: <pid, page#, D>: pid to O(n) lookup IPT for page#. Once found, the index $i$ is the frame.

Time and Memory space

Suppose each entry is 8B, bigger than the PTE due to PID.

Time: Going through every frame i.e. $2^{20}(30)$ns.

Memory Space:

Page Replacement Algorithms

When a page must be evicted:

  1. Identify the process and page number in the chosen frame
  2. Mark old process as non-memory resident.
  3. Mark new process as memory resident.

Memory references are modelled as memory reference strings, a sequence of page numbers. $p_0, p_1, \dots$

$T_\text{access} = (1 - p)T_\text{mem} + pT_\text{page fault}$: a good algo minimizes page faults i.e. $p$​, and thus $T_\text{access}$.

Optimal Page Replacement (OPT)

First in First out (FIFO)

Least Recently Used (LRU)

Implementation A: use a counter.

Store the logical time when reference occurs

Implementation B: stack.

Stack implementation of LRU

Second-Change Page Replacement (CLOCK)

Modified FIFO.

Reference bit marking Circular nature

Frame Allocation Policies

$M$ policies and $N$ frames.

Equal allocation: each process gets $N/M$

Proportional allocation: each process gets $\frac{x_i}{X} (N)$ frames for $X$ memory space, $x_i$ being size of process $i$​.

Global Replacement: Process A can steal a frame from Process B

Local replacement:

Working Set

Set of pages in the virtual address space of the process that are currently resident in physical memory.

Working set changes over time

$\Delta = 5$ in this example

MMU - Hardware component

Memory management unit

  1. CPU needs page 2
  2. Memory management unit (MMU) checks TLB
  3. Not in TLB, MMU accesses the page table (in memory space of OS)

  4. Not in page, MMU signals a page fault and control passed to OS.
  5. OS brings from secondary storage to the physical memory.

The page table is not in the PCB. Rather it is connected by links, and held in a register (page directory register) which is populated at the context switch