The Swap Cache

Now that I have described the layout of the swap subsystem by reference to its data structures, let us turn our focus in the following sections on the techniques employed by the kernel to write pages from memory into a swap area and to read pages from the swap area back into memory.

The kernel makes use of a further cache, known as a swap cache, that acts as a liaison between the operation to select the pages to be swapped out and the mechanism that actually performs swapping. At first sight, this seems a little peculiar. What's the use of another swapping cache and what exactly needs to be cached? The answers are given below.

Figure 18-4 shows how the swap cache interacts with the other components of the swap subsystem.

The swap cache is an agent between the page selection policy and the mechanism for transferring data between memory and swap areas. These two parts interact via the swap cache. Input in one part triggers corresponding actions in the other. Notice that the policy routines can, nevertheless, directly interact with the writeback routines for pages that need not be swapped out, but can be synchronized.

Figure 18-4: Interaction between the swap cache and the other components of the swap subsystem.

Which data are kept in the swap cache? As the swap cache is simply another page cache built using the structures discussed in Chapter 3, the answer is simple — memory pages. However, in contrast to the other page caches in the system, its purpose is not to keep pages in RAM for performance reasons although the associated data could also always be obtained from a block storage medium (this would run totally counter to the swapping principle). Instead, the swap cache is used for the following, depending on the ''direction'' of the swapping request (read or write):

□ When pages are swapped out, the selection logic first selects a suitable seldom-used page frame. This is buffered in the page cache, from where it is transferred to the page cache.

□ If a page used simultaneously by several processes is swapped out, the kernel must set the page entry in the directories of the process to point to the relevant position in the swap-out file. When one of the processes accesses the data on the page, the page is swapped in again, and the page table entry for this single process is set to the current memory address at which the page is now located. However, this causes a problem. The entries of all other processes still point to the entry in the swap-out file because, although it is possible to determine the number of processes that share a page, it is not possible to identify which processes these are.

When shared pages are swapped in, they are therefore retained in the swap cache until all processes have requested the page from the swap area and are all thus aware of the new position of the page in memory. This situation is illustrated in Figure 18-5.

Without the aid of the swap cache, the kernel is not able to determine whether or not a shared memory page has already been swapped back in, and this would inevitably result in redundant reading of data.

The importance of the swap cache is not the same in both directions. It is far more important when pages are swapped in than when they are swapped out. This asymmetry came about during the development of 2.5 when the reverse mapping scheme (rmap) described in Chapter 4 was introduced. Recall that the rmap mechanism finds all processes that share a page.5

5 In earlier versions, shared memory pages could only be swapped out using the swap cache. Once the page had been removed from the page tables of a single process, the kernel had to wait until the page had also been removed from the page tables of all other processes before it could remove the data from memory; this required the systematic scanning of all system page tables. The pages were kept in the swap cache in the meantime.

Swap Area

iCvJ

Swap Area

Swap In of B,C

Swap Area

CD-T

Figure 18-5: Swapping a page in via the swap cache.

When a shared page is swapped out, rmap finds all processes that reference the data in the page. Consequently, the relevant page table entries of all processes that reference the page can be updated to indicate the new position in the swap area. This means that the page data can be swapped out immediately without having to retain in the swap cache for a lengthy period.

Continue reading here: Identifying Swapped Out Pages

Was this article helpful?

+1 0