Analysis of Innodb buffer hit ratio calculation from MySQL source code

  • 2020-11-03 22:37:40
  • OfStack

As recommended by the official manual, Innodb buffer Hit Ratios is calculated as:


100-((iReads / iReadRequests)*100)
iReads : mysql->status->Innodb_buffer_pool_reads
iReadRequests: mysql->status->Innodb_buffer_pool_read_requests

Reference: http: / / dev. mysql. com/doc/mysql - monitor / 2.0 / en/mem_graphref html
Search "Hit Ratios"
I would like to recommend interested students to look at this page 1 should also be a great harvest.
In addition, on hackmysql: ES27en. hackmysql. com website: mysqlsqlreport about buffer hit calculation is:



$ib_bp_read_ratio = sprintf "%.2f",
($stats{'Innodb_buffer_pool_read_requests'} ?
100 - ($stats{'Innodb_buffer_pool_reads'} /
$stats{'Innodb_buffer_pool_read_requests'}) * 100 :0);

That is:


ib_bp_hit=100-(Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests)*100

Another place we know to look at Innodb Buffer Hit Ratios is:

show engine innodb status\G;

Buffer pool hit rate : XXXX/1000;
That XXX/1000 is the hit of buffer pool hit ratios.
You can also look at the bp hit calculation from the code:


storage/innobase/buf/buf0buf.c # void buf_print_io
storage/innodbase/include/buf0buf.h #struct buf_block_struct

In es62EN0ES63en. c buf_print_io function, we can see:


void
buf_print_io(
 ... 
 
if (buf_pool->n_page_gets > buf_pool->n_page_gets_old) {
fprintf(file, "Buffer pool hit rate %lu / 1000\n",
(ulong)
(1000 - ((1000 * (buf_pool->n_pages_read
- buf_pool->n_pages_read_old))
/ (buf_pool->n_page_gets
- buf_pool->n_page_gets_old))));
} else {
fputs("No buffer pool page gets since the last printout\n",
file);
}
 
buf_pool->n_page_gets_old = buf_pool->n_page_gets;
buf_pool->n_pages_read_old = buf_pool->n_pages_read;
 ... 
}

Combined with:
storage \ innobase \ include \ buf0buf h


struct buf_block_struct{
 ... 
ulint n_pages_read; /* number read operations */
 ... 
ulint n_page_gets; /* number of page gets performed;
also successful searches through
the adaptive hash index are
counted as page gets; this field
is NOT protected by the buffer
pool mutex */
 ... 
ulint n_page_gets_old;/* n_page_gets when buf_print was
last time called: used to calculate
hit rate */
 ... 
ulint n_pages_read_old;/* n_pages_read when buf_print was
last time called */
 ... 


From this, it can be seen that the hit calculation of innodb buffer hit Ratios requires a subtraction formula between the value taken this time and the last value, which should be


ib_bp_hit=1000  �  (t2.iReads  �  t1.iReads)/(t2.iReadRequest  �  t1.iReadRequest)*1000

t(n): The interval between two time points is at least more than 30 seconds, which is of little significance.


iReads: Innodb_buffer_pool_reads
iReadRequest: Innodb_buffer_pool_read_requests

Interested in the output parameters of innodb can focus on: storage innobase/buf/Srv0srv c the:


void srv_export_innodb_status()

Think about:
For innodb_buffer_pool_read_requests, innodb_buffer_pool_reads, the accumulative value of innodb_buffer_pool_reads/ innodb_ES127en_ES129en_ES130en can only show the hit ratio from the beginning to the present. If you want to get the hit rate per minute between 5 minutes, 1 minute, or 8 to 9 points, if you follow innodb_buffer_pool_reads/innodb_buffer_pool_read_requests, you only get the cumulative average hit rate per minute of mysqld starting from 8 to 9 points.
So if you think about every (5) minute hit, you subtract the value you got this time from the value you got 1(5) minutes ago, and then you do the operation. In this way, we can get 1 current bp hit.
There is no real right or wrong problem with either method, but it is much easier to find the database jitter problem than the one in the source code.

Problems you can solve:
Occasionally database performance jitter can be intuitively reflected.


Related articles: