Cache

  
for Linux Regardless of hardware design or software design, caching is a common means of achieving high performance. Linux uses a variety of caches related to memory management.
1.
Buffer Cache
:
The buffer cache contains the data buffer used by the block device. These buffers contain data blocks that are read from the device or that are written to the device. The buffer cache is indexed by the device identification number and block label, so you can quickly find the data block. If the data can be found in the buffer cache, there is no need for the system to perform the actual read operation on the physical block device.
The kernel maintains a lot of information for each buffer to help ease writes. This information includes a "dirty" bit, indicating that the buffer in memory has been modified, must Write to disk; also includes a timestamp indicating how long the buffer has been in memory before it is flushed to disk. Because the information about the buffer is kept in the buffer header, these data structures, along with the buffer of the user data itself, need to be maintained.
The size of the buffer cache can vary. When a new buffer is needed and there are no buffers available, the page is allocated as needed. When free memory becomes insufficient, as seen in the previous section, the buffer is freed and the corresponding page is used repeatedly.
2.
Page Cache

The Page Cache is the disk cache used by page I/O operations to access data. We will see in the file system that the read(), write(), and mmap() system calls access to regular files are done through the page cache. Because the page I/O operation is to transfer the entire page of data, the information unit retained in the cache is a whole page. A page contains data that is not necessarily physically adjacent disk blocks, so you cannot use device numbers and block numbers to identify pages. Instead, the identity of a page in the page cache is reached by the offset of the file's inode and file.
There are three main operations related to page caching: adding a page when the portion of the file being accessed is not in the cache, deleting a page when the cache becomes too large, and finding a given The page where the file offset is located.

3. Swap Cache

Only modified (dirty) pages are saved in the swap file. After the modified page is written to the swap file, if the page is exchanged again but has not been modified, there is no need to write the swap file. Instead, just drop the page. The swap cache actually contains a page table necklace table, and each physical page of the system corresponds to a page table entry. For the swapped page, the page table entry contains the exchange file information for saving the page, and the location information of the page in the swap file. If a swap page entry is non-zero, it indicates that the corresponding physical page saved in the swap file has not been modified. If this page is modified in a subsequent operation, the page table entry in the swap cache is cleared. When Linux needs to swap out a page from physical memory, it first analyzes the information in the swap cache. If the cache contains a non-zero page entry for the physical page, the page has not been modified since the memory was swapped out. At this point, the system only needs to discard the page.
Some of the functions and functions of the swap cache are given here: in /linux/mm/swap_state.c.

Initialize swap buffer, set size, position function:
extern unsigned long init_swap_cache(unsigned long, unsigned long);

Functions that display swap buffer information:
extern void show_swap_cache_info(void);
< Br>Add swap buffer function:
int add_to_swap_cache(unsigned long index, unsigned long entry)
Parameter index is the index into the buffer (index is in the index table) An entry), entry is ‘ page table entry & rsquo; (that is, the location record of this page in the exchange file, this record is similar to the page table entry, see the switch system)

Copy the swapped out page:
extern void swap_duplicate(unsigned long);
When using the copy_page_tables() call to implement the child process in fork() Inherit the page that was swapped out, see the Switching section.

Remove a page from the buffer
delete_from_swap_cache(page_nr);

4 Hardware Cache
Common hardware cache is a cache of page table entries. This work is actually done by the processor, and its operation is related to the specific processor hardware (but management depends on The software is completed), and this cache will be further described next.


Copyright © Windows knowledge All Rights Reserved