InnoDB Cache System
Buffer operation
Check whether the buffer pool has been cached Based on the Space ID and offset of the tbl_test file. If yes, directly read the page information and return the result;
If no cache is available, a blank page is allocated first, and then the data on the disk is read and loaded into the memory, and the result is returned.
When SQL-level commands are executed on the buffer layer of InnoDB, they correspond to the read/write operations on the buffer block on the buf_pool.
For example, enter the following query statement on the client:
Select * From tbl_test;
The following actions are generated when layer-by-layer Parsing is executed to the cache system:
Introduction to InnoDB Buffer Pool System
Buf_pool-> free, idle block linked list
Buf_pool-> flush_list, block linked list to be written
Buf_pool-> LRU, LRU block linked list
Buf_pool-> unzip_lru, unzipped LRU block linked list
Buf_pool-> zip_clean, clean compressed block linked list
Buf_pool-> zip_free [buf_buddy_size], idle compressed block linked list
Buf0buf. c is the core part of the buffer system and provides interfaces for Disk File interaction with memory. The entire buffer pool is organized mainly through these linked lists.
Buf0lru. C is used for cache replacement in the buffer system. Algorithm The main operation object is the LRU linked list, which aims to better use the cache.
Buf0flu. c is the part in the system that writes data to the disk. To save the system I/O overhead, InnoDB also considers dirty pages in the current system, loads of the current system, and other factors.
Buf0rea. c is the part of the system that reads data, including common read, pre-read, insert buffer, and recovery.
The buf0buddy. c binary partner allocation algorithm is used when allocating compressed memory blocks to avoid Memory fragments in the system.
In addition, mem0pool. C is included to process some underlying data.
Read operations (loading data from disk to memory)
When database files are not stored in a compressed format, InnoDB directly uses buf_lru_get_free_block to obtain an idle control block. When MySQL is configured with Compressed Storage, the system allocates a suitable size control block through the binary partner algorithm to prevent memory fragments. The main calling process is as follows:
Buf_page_get_gen is a major API encapsulated by the InnoDB cache system for external system calls. This function is used to obtain a page containing data and return it to the called function. Shows the call stack in actual use:
Write operation (delete page from buffer pool and flush to disk)
The entry function is buf_page_release, which is used to release pages in the buffer pool. This function marks dirty data in the flush queue.
There is a master thread (srv_master_thread) in InnoDB, which is mainly responsible for regularly writing data to disk files. The theoretical refresh frequency is 1 second or 10 seconds, for details, refer to the current server load and the ratio of dirty pages in the buffer pool for dynamic adjustment.CodeThe call process is as follows: