Why paging is slow and how do we make it fast?

The reason why paging is slow

Every time a process accesses a memory location, the CPU must translate the logical address to a physical address using the page table. This translation involves multiple steps:

  • Look Up the Page Table: The CPU must first access the page table to find out which physical frame corresponds to the logical page.
  • Access the Physical Memory: Then, it uses the frame number obtained from the page table to access the actual memory location.

Example:

  • Page Size: 4 KB
  • Logical Address: 6 KB
  • Page Table Entry: 2 (The logical address 6 KB is in page 2)
  • Frame Number: 5 (Page 2 maps to frame 5 in physical memory)

Steps:

  • Access the Page Table: The CPU checks the page table to find out which frame contains the data for page 2. This involves accessing the page table entry for page 2.
  • Access Physical Memory: Once the frame number (5) is known, the CPU accesses the physical memory at frame 5.
  • Total Time: This process involves two memory accesses:
  • One to look up the page table
  • Another to access the actual memory location. 

How do we make paging fast?

The Translation Lookaside Buffer (TLB) is a special, high-speed cache used by the CPU to speed up virtual-to-physical address translation. When a program accesses memory, the CPU needs to translate the virtual address (used by the program) into a physical address (actual location in RAM). The TLB helps reduce the time it takes to perform this translation by storing recent address mappings.

How TLB Works
  • Address Translation: When a process accesses memory, it provides a virtual address.
  • The CPU first checks the TLB to see if the virtual address is already mapped to a physical address in the TLB cache.
  • TLB Hit: If the virtual address is found in the TLB (a "TLB hit"), the corresponding physical address is quickly retrieved from the TLB, and the memory access is fast.
  • TLB Miss: If the virtual address is not in the TLB (a "TLB miss"), the CPU must look up the page table to find the physical address. This involves:
  • Accessing the page table, which may require multiple memory accesses.
  • Once the physical address is found, it is added to the TLB so that future accesses to this address can be handled more quickly.

How does TLB deal with context switching?

The Address Space Identifier (ASID) is a value used to differentiate between different processes or address spaces in the TLB. It helps the TLB keep track of which entries are valid for which process, reducing the need to flush the TLB during context switches. Example of TLB with ASID and Context Switching
  • Initial State: Process P1 is running with ASID = 1. The TLB has entries tagged with ASID = 1, containing mappings for P1’s page table.
  • Context Switch: Process P2 is about to start running with ASID = 2. The ASID in the CPU is updated from 1 to 2.
  • No TLB Flush Needed: The TLB does not need to be cleared because the ASID tags help in distinguishing the entries for P1 and P2.
  • Execution of P2: When Process P2 accesses memory, the TLB checks the ASID and finds that the existing entries are not tagged with ASID = 2. Thus, it results in a TLB miss. The CPU looks up P2’s page table, and the required page table entries are loaded into the TLB with ASID = 2. Subsequent memory accesses by P2 are faster as the TLB now has entries with the correct ASID.


Comments

Post a Comment

Popular posts from this blog

Indexed allocation

Thrashing

Basics of paging