MySQL 26.7.0
Source Code Documentation
buf0stats.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2015, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/buf0stats.h
29 Buffer pool stats
30
31 Created May 22, 2015 Vasil Dimov
32 *******************************************************/
33
34#ifndef buf0stats_h
35#define buf0stats_h
36
37#include "univ.i"
38
39#include "btr0btr.h" /* btr_page_get_index_id() */
40#include "dict0types.h" /* index_id_t, DICT_IBUF_ID_MIN */
41#include "fil0fil.h" /* fil_page_get_type() */
42#include "fsp0sysspace.h" /* srv_tmp_space */
43#include "ibuf0ibuf.h" /* IBUF_SPACE_ID */
44#include "mach0data.h" /* mach_read_from_4() */
45#include "page0page.h" /* page_is_leaf() */
46#include "srv0srv.h" /* srv_buf_pool_curr_size */
47#include "ut0lock_free_hash.h" /* ut_lock_free_hash_t */
48#include "ut0new.h" /* ut::new_withkey(), ut::delete_() */
49
50/** Per index buffer pool statistics.
51
52This tracks the number of logical buffer pool file pages that currently satisfy
53all of these predicates:
541. the page is present in the buffer pool page hash, not on the free list,
552. the FIL page type is FIL_PAGE_INDEX or FIL_PAGE_RTREE,
563. PAGE_LEVEL is 0, i.e. page_is_leaf() is true,
574. the page id's space id and PAGE_INDEX_ID identify the tracked index.
58
59For compressed tables, the compressed image and the uncompressed frame are two
60possible in-memory representations of the same buffer pool page. They are
61counted once while the page id remains cached. Discarding only the uncompressed
62frame must not decrement the count if the compressed descriptor remains cached.
63
64This is a key,value store where the key is a packed index_id_t and the value is
65the number of cached leaf pages that belong to the index. */
67 public:
68 /** Constructor. */
70 m_store = ut::new_withkey<ut_lock_free_hash_t>(
72 }
73
74 /** Destructor. */
76
77 /** Increment the number of pages for a given index with 1.
78 @param[in] id id of the index whose count to increment */
79 void inc(const index_id_t &id) {
80 if (should_skip(id)) {
81 return;
82 }
83
84 /* Compressed-only pages can be smaller than UNIV_PAGE_SIZE, so use the
85 smallest compressed page size as a conservative logical-page bound. */
86 ut_ad(get(id) <
87 static_cast<uint64_t>(srv_buf_pool_curr_size / UNIV_ZIP_SIZE_MIN));
88
89 m_store->inc(id.conv_to_int());
90 }
91
92 /** Set the count for given id to 0
93 @param[in] id id of the index whose count will be reset */
94 void reset(const index_id_t &id) {
95 if (should_skip(id)) {
96 return;
97 }
98
99 m_store->set(id.conv_to_int(), 0);
100 }
101
102 /** Decrement the number of pages for a given index with 1.
103 @param[in] id id of the index whose count to decrement */
104 void dec(const index_id_t &id) {
105 if (should_skip(id)) {
106 return;
107 }
108
109 ut_ad(get(id) > 0);
110
111 m_store->dec(id.conv_to_int());
112 }
113
114 /** Decrement the page count if a frame currently satisfies the accounting
115 predicate.
116 @param[in] frame buffer frame */
117 void dec_if_tracked_page(const page_t *frame) {
118 change_if_tracked_page(frame, false);
119 }
120
121 /** Increment the page count if a frame currently satisfies the accounting
122 predicate.
123 @param[in] frame buffer frame */
124 void inc_if_tracked_page(const page_t *frame) {
125 change_if_tracked_page(frame, true);
126 }
127
128 /** Check whether a frame currently satisfies the accounting predicate.
129 @param[in] frame buffer frame
130 @return true if the frame is counted by this structure */
131 bool is_tracked_page(const page_t *frame) {
132 const ulint page_type = fil_page_get_type(frame);
133
134 if ((page_type != FIL_PAGE_INDEX && page_type != FIL_PAGE_RTREE) ||
135 !page_is_leaf(frame)) {
136 return false;
137 }
138
139 const space_id_t space_id = get_space_id(frame);
140 const index_id_t id(space_id, btr_page_get_index_id(frame));
141
142 return !should_skip(id);
143 }
144
145 /** Get the number of pages in the buffer pool for a given index.
146 @param[in] id id of the index whose pages to peek
147 @return number of pages */
148 uint64_t get(const index_id_t &id) {
149 if (should_skip(id)) {
150 return (0);
151 }
152
153 const int64_t ret = m_store->get(id.conv_to_int());
154
156 /* If the index is not found in this structure,
157 then 0 of its pages are in the buffer pool. */
158 return (0);
159 }
160
161 return (static_cast<uint64_t>(ret >= 0 ? ret : 0));
162 }
163
164 private:
165 /** Change the page count if a frame currently satisfies the accounting
166 predicate.
167 @param[in] frame buffer frame
168 @param[in] increment true to increment, false to decrement */
169 void change_if_tracked_page(const page_t *frame, bool increment) {
170 if (is_tracked_page(frame)) {
171 const space_id_t space_id = get_space_id(frame);
172 const index_id_t id(space_id, btr_page_get_index_id(frame));
173
174 if (increment) {
175 inc(id);
176 } else {
177 dec(id);
178 }
179 }
180 }
181
182 /** Gets the space id from a page image. Do not use page_get_space_id() here:
183 it asserts that the frame is page-aligned, but compressed page images are
184 allocated by the buddy allocator and need not satisfy that assertion.
185 @param[in] frame page image
186 @return tablespace identifier from the FIL header */
187 static space_id_t get_space_id(const page_t *frame) {
189 }
190
191 /** Assess if we should skip a page from accounting.
192 @param[in] id index_id of the page
193 @return true if it should not be accounted */
194 bool should_skip(const index_id_t &id) {
195 const bool is_temp = fsp_is_system_temporary(id.m_space_id);
196
197 /* BTR_FREED_INDEX_ID marks an invalidated root page after freeing an index
198 tree. It is not a real index and must not be counted if that root page is
199 later flushed or evicted. index_id_t::conv_to_int() packs the space id into
200 the high 32 bits and the index id into the low 32 bits. Skip index ids with
201 high bits set to avoid collisions between index-id bits and space-id bits.
202 This also means SDI and IBUF pages are not measured here because they use
203 very high index ids; IBUF is named explicitly for readability. */
204 return (id.is_ibuf() || is_temp || id.m_index_id == BTR_FREED_INDEX_ID ||
205 (id.m_index_id & 0xFFFFFFFF00000000ULL) != 0);
206 }
207
208 /** (key, value) storage. */
210};
211
212/** Container for how many pages from each index are contained in the buffer
213pool(s). */
215
216#endif /* buf0stats_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:49
The B-tree.
static space_index_t btr_page_get_index_id(const page_t *page)
Gets the index id field of a page.
constexpr space_index_t BTR_FREED_INDEX_ID
PAGE_INDEX_ID value for freed index B-trees.
Definition: btr0btr.h:62
buf_stat_per_index_t * buf_stat_per_index
Container for how many pages from each index are contained in the buffer pool(s).
Definition: buf0buf.cc:329
Per index buffer pool statistics.
Definition: buf0stats.h:66
~buf_stat_per_index_t()
Destructor.
Definition: buf0stats.h:75
void change_if_tracked_page(const page_t *frame, bool increment)
Change the page count if a frame currently satisfies the accounting predicate.
Definition: buf0stats.h:169
buf_stat_per_index_t()
Constructor.
Definition: buf0stats.h:69
void inc_if_tracked_page(const page_t *frame)
Increment the page count if a frame currently satisfies the accounting predicate.
Definition: buf0stats.h:124
ut_lock_free_hash_t * m_store
(key, value) storage.
Definition: buf0stats.h:209
bool is_tracked_page(const page_t *frame)
Check whether a frame currently satisfies the accounting predicate.
Definition: buf0stats.h:131
void inc(const index_id_t &id)
Increment the number of pages for a given index with 1.
Definition: buf0stats.h:79
void reset(const index_id_t &id)
Set the count for given id to 0.
Definition: buf0stats.h:94
bool should_skip(const index_id_t &id)
Assess if we should skip a page from accounting.
Definition: buf0stats.h:194
void dec_if_tracked_page(const page_t *frame)
Decrement the page count if a frame currently satisfies the accounting predicate.
Definition: buf0stats.h:117
void dec(const index_id_t &id)
Decrement the number of pages for a given index with 1.
Definition: buf0stats.h:104
static space_id_t get_space_id(const page_t *frame)
Gets the space id from a page image.
Definition: buf0stats.h:187
uint64_t get(const index_id_t &id)
Get the number of pages in the buffer pool for a given index.
Definition: buf0stats.h:148
Globally unique index identifier.
Definition: dict0types.h:221
static const int64_t NOT_FOUND
The value that is returned when the searched for key is not found.
Definition: ut0lock_free_hash.h:56
Lock free hash table.
Definition: ut0lock_free_hash.h:374
void set(uint64_t key, int64_t val) override
Set the value for a given key, either inserting a new (key, val) tuple or overwriting an existent val...
Definition: ut0lock_free_hash.h:474
void inc(uint64_t key) override
Increment the value for a given key with 1 or insert a new tuple (key, 1).
Definition: ut0lock_free_hash.h:557
void dec(uint64_t key) override
Decrement the value of a given key with 1 or insert a new tuple (key, -1).
Definition: ut0lock_free_hash.h:571
int64_t get(uint64_t key) const override
Get the value mapped to a given key.
Definition: ut0lock_free_hash.h:426
Data dictionary global types.
The low-level file system.
constexpr page_type_t FIL_PAGE_INDEX
File page types (values of FIL_PAGE_TYPE)
Definition: fil0fil.h:1562
constexpr page_type_t FIL_PAGE_RTREE
R-tree node.
Definition: fil0fil.h:1565
page_type_t fil_page_get_type(const byte *page)
Get the file page type.
Definition: fil0fil.h:1675
constexpr uint32_t FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID
starting from 4.1.x this contains the space id of the page
Definition: fil0types.h:105
bool fsp_is_system_temporary(space_id_t space_id)
Check if tablespace is system temporary.
Definition: fsp0fsp.cc:294
Multi file, shared, system tablespace implementation.
Insert buffer.
Utilities for converting data from the database file to the machine format.
static uint32_t mach_read_from_4(const byte *b)
The following function is used to fetch data from 4 consecutive bytes.
void delete_(T *ptr) noexcept
Releases storage which has been dynamically allocated through any of the ut::new*() variants.
Definition: ut0new.h:651
PSI_memory_key_t make_psi_memory_key(PSI_memory_key key)
Convenience helper function to create type-safe representation of PSI_memory_key.
Definition: ut0new.h:190
Index page routines.
static bool page_is_leaf(const page_t *page)
Determine whether the page is a B-tree leaf.
byte page_t
Type of the index page.
Definition: page0types.h:152
The server main program.
long long srv_buf_pool_curr_size
Current size in bytes.
Definition: srv0srv.cc:450
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:403
constexpr uint32_t UNIV_ZIP_SIZE_MIN
Smallest compressed page size.
Definition: univ.i:327
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
Lock free hash implementation.
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...
PSI_memory_key mem_key_buf_stat_per_index_t
Definition: ut0new.cc:50
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510