The buffer mechanism in Linux memory management-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1.1. buffer cache
The buffer cache contains the data buffer used by Block devices. These buffers contain data blocks read from the device or data blocks written to the device. The high-speed cache of the buffer zone is indexed by the device ID number and block number to quickly find data. If the data can be found in the buffer cache, you do not have to perform actual read operations on the physical block device.
The data structure describing the buffer zone is buffer_head. The buffer size is generally smaller than the page size. Therefore, a page can contain several buffers, And the buffer on the same page is connected by a linked list.
Because Linux uses this caching mechanism, when writing a data to a file, the inner volume will write the data to the memory buffer instead of directly writing it to the disk. In this way, the disk write command has been returned, and the actual write operation has not been executed. For this reason, the user should use the correct shutdown command to shut down, rather than directly turn off the power of the computer. You can also use the sync command to refresh the buffer cache and forcibly write data in the buffer to the disk.
1.2. Page Cache
The page cache is used to accelerate the access to images and data on the disk. The page cache is used to cache the logical content of a file and access the file through the VFS index node and offset. When the page reads physical memory from the disk, It is cached in the page cache. The following source code stores the read pages in the cache:
Add_page_to_hash_queue (page, page_hash (mapping, index ));
Lru_cache_add (page );
Spin_unlock (pg_lock );
}
1.3. Switching high-speed cache
When switching pages to swap files, Linux always avoids writing pages. After the page has been swapped out of memory, if a process accesses the page again, it must be re-transferred to the memory. As long as the page has not been written in the memory, the copy in the swap file is valid.
Linux uses swap cache to track these pages. This switching cache is a page table entry linked list, each corresponding to a physical page in the system. The swap cache corresponds to the page table entry of the swap out page, and describes the swap file in which the page is placed and the location in the swap file. If the value of the switch cache entry is not 0, this page in the switch file is not modified. If this page is modified (or written), its entry will be deleted from the swap cache.
When Linux needs to switch a physical page to a swap file, it checks the swap cache. If a valid entry exists for this page, this page cannot be written to the swap file. This is because this page in the memory has not been modified since it was last read from the swap file.
The entry in the exchange cache is the page table entry that has been swapped out. Although they are marked as invalid, they provide Linux with information such as the page in which the switch file is located and the location of the file.
1.4. Hardware high-speed cache
A common hardware cache is the cache of page table items, such as TLB. It is usually within MMU, with a small number of entries. Each entry in each TLB register contains one page of information: valid bit, virtual page bit, modified bit, protected bit, and all the physical page numbers on the page. When a virtual address is sent to MMU translation, the hardware first compares it with all entries in TLB. If the virtual page number is in TLB and the access does not violate the protection bit, its page frame will be taken directly from TLB without accessing the page table. If the page number is in TLB but the current command tries to write a read-only page, a page failure will occur, which is the same as when you directly access the page table. If MMU finds that the page does not exist in TLB, it searches for the regular page table, removes an entry from TLB, and replaces it with the newly found page table item.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service