MySQL source code: Innobase buffer pool _ MySQL

Source: Internet
Author: User
MySQL source code: Innobase buffer pool bitsCN.com

The Innodb buffer pool is mainly used to store accessed data pages. it is a piece of continuous memory that can be effectively managed by some algorithms. It is the largest block memory system in the database system. Data access in the Innodb storage engine reads data from database files to the buffer zone in the form of pages (16 KB by default), and then uses the same size of memory space for a ing in the memory, to improve data access efficiency, the database system allocates a lot of such space in advance to exchange data with files. During access, the buffer page is managed according to the least recently used (LRU) algorithm. frequently accessed pages are at the beginning and at the end, if there is no idle page in the buffer for file data ING, find the last and unused page from the buffer pool and then map it to the new data file page, at the same time, move it to the forefront of the LRU linked list. This ensures that frequently accessed pages are always in the buffer pool without disk flushing, thus ensuring database access efficiency.

The buffer pool size can be configured. it can be determined by the innodb_buffer_pool_size parameter in the configuration file. the default size is 128 MB. This value cannot be modified once MYSQL has been started. if you need to modify it, you can only exit MYSQL and modify the corresponding my. ini to set the new buffer size, and then start it to take effect.

When MySQL is started, if the storage engine is innodb, innodb initializes all its subsystems, including the page buffer subsystem, which is implemented through the buf_pool_init function. The Buffer pool can have multiple instances, which can be set by the innodb_buffer_pool_instances parameter in the configuration file. the default value is 1, the multi-instance buffer pool is implemented to improve the concurrency when querying data pages. The size of each instance is the same, that is, the system splits the configured buffer size equally by the number of instances, and then initializes each instance.

A buffer instance is described using the buf_pool_t struct in the code. this struct is the core tool used to manage a buffer instance. it contains a lot of information, including the above-mentioned LRU linked list, used to manage access to all pages in the instance; FREE linked list used to store all idle pages in the instance; flush_list linked list used to store all pages modified and need to be flush_list; mutex, which is mainly used to protect this buffer pool instance, because one instance can only be accessed by one thread; chunks, which is the first address pointing to the instance's first real memory page, therefore, pages are continuously stored, so you can directly access all other pages through this pointer.

The function for initializing the memory space of a buffer pool instance is buf_chunk_init. the memory distribution of a buffer pool instance is a continuous memory space, which stores two parts, the preceding figure shows the control header structure information (buf_block_t structure) of these data cache pages. each page corresponds to a control header information, so the control header information is continuously stored together, therefore, after the control information is stored, the real buffer page is used. Therefore, the actual space used by a buffer pool instance is larger than that specified in the configuration, because the space for the control header information needs to be stored, the following shows the memory distribution of a buffer pool instance:

For all pages in the buffer pool, there is a control header that corresponds to it. it can be seen that each ctl controls the usage of a page of its own. When initializing an instance, you also need to initialize each control header, that is, each buf_block_t structure. initializing a page control information is implemented through the buf_block_init function. the buf_block_t structure contains a lot of information, it mainly includes: its corresponding page address frame; the page information structure buf_page_t, which is used to describe the information of a page, including the ID number, page number, the LSN used for modification (newest_modification and oldest_modification), and usage status (9 statuses in total) of the tablespace; mutex is used to protect the mutex of the page, and lock (read/write) on the page when accessing the page. After each page is initialized, you need to add each page to the idle page linked list mentioned above, because the current status of these pages is not used (BUF_BLOCK_NOT_USED ).

Until now, even if initialization is complete for an instance in the buffer pool, file data is cached through these memory pages when accessing the database. Because the buffer pool involves a lot of content, such as MTR, only the buffer pool is introduced here. the other parts will be described in detail later.

BitsCN.com

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.