1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/* Copyright (c) 2012, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/table_cache.h"
#include <stdio.h>
#include <string.h>
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "sql/sql_test.h" // lock_descriptions[]
#include "thr_lock.h"
#include "thr_mutex.h"
/**
Container for all table cache instances in the system.
*/
Table_cache_manager table_cache_manager;
#ifdef HAVE_PSI_INTERFACE
PSI_mutex_key Table_cache::m_lock_key;
PSI_mutex_info Table_cache::m_mutex_keys[] = {
{&m_lock_key, "LOCK_table_cache", 0, 0, PSI_DOCUMENT_ME}};
#endif
/**
Initialize instance of table cache.
@retval false - success.
@retval true - failure.
*/
bool Table_cache::init() {
mysql_mutex_init(m_lock_key, &m_lock, MY_MUTEX_INIT_FAST);
m_unused_tables = nullptr;
m_table_count = 0;
return false;
}
/** Destroy instance of table cache. */
void Table_cache::destroy() { mysql_mutex_destroy(&m_lock); }
/** Init P_S instrumentation key for mutex protecting Table_cache instance. */
void Table_cache::init_psi_keys() {
#ifdef HAVE_PSI_INTERFACE
mysql_mutex_register("sql", m_mutex_keys,
static_cast<int>(array_elements(m_mutex_keys)));
#endif
}
#ifdef EXTRA_DEBUG
void Table_cache::check_unused() {
uint count = 0;
if (m_unused_tables != NULL) {
TABLE *cur_link = m_unused_tables;
TABLE *start_link = m_unused_tables;
do {
if (cur_link != cur_link->next->prev ||
cur_link != cur_link->prev->next) {
DBUG_PRINT("error", ("Unused_links aren't linked properly"));
return;
}
} while (count++ < m_table_count &&
(cur_link = cur_link->next) != start_link);
if (cur_link != start_link)
DBUG_PRINT("error", ("Unused_links aren't connected"));
}
for (const auto &hp : m_cache) {
Table_cache_element *el = hp.second.get();
Table_cache_element::TABLE_list::Iterator it(el->free_tables);
TABLE *entry;
while ((entry = it++)) {
/* We must not have TABLEs in the free list that have their file closed.
*/
assert(entry->db_stat && entry->file);
if (entry->in_use)
DBUG_PRINT("error", ("Used table is in share's list of unused tables"));
count--;
}
it.init(el->used_tables);
while ((entry = it++)) {
if (!entry->in_use)
DBUG_PRINT("error", ("Unused table is in share's list of used tables"));
}
}
if (count != 0)
DBUG_PRINT("error",
("Unused_links doesn't match open_cache: diff: %d", count));
}
#endif
/** Free all unused TABLE objects in the table cache. */
void Table_cache::free_all_unused_tables() {
assert_owner();
while (m_unused_tables) {
TABLE *table_to_free = m_unused_tables;
remove_table(table_to_free);
intern_close_table(table_to_free);
}
}
#ifndef NDEBUG
/**
Print debug information for the contents of the table cache.
*/
void Table_cache::print_tables() {
uint unused = 0;
uint count = 0;
static_assert(TL_WRITE_ONLY + 1 == array_elements(lock_descriptions), "");
for (const auto &key_and_value : m_cache) {
Table_cache_element *el = key_and_value.second.get();
Table_cache_element::TABLE_list::Iterator it(el->used_tables);
TABLE *entry;
while ((entry = it++)) {
printf("%-14.14s %-32s%6ld%8u%6d %s\n", entry->s->db.str,
entry->s->table_name.str, entry->s->version(),
entry->in_use->thread_id(), entry->db_stat ? 1 : 0,
lock_descriptions[(int)entry->reginfo.lock_type]);
}
it.init(el->free_tables);
while ((entry = it++)) {
unused++;
printf("%-14.14s %-32s%6ld%8ld%6d %s\n", entry->s->db.str,
entry->s->table_name.str, entry->s->version(), 0L,
entry->db_stat ? 1 : 0, "Not in use");
}
}
if (m_unused_tables != nullptr) {
TABLE *start_link = m_unused_tables;
TABLE *lnk = m_unused_tables;
do {
if (lnk != lnk->next->prev || lnk != lnk->prev->next) {
printf("unused_links isn't linked properly\n");
return;
}
} while (count++ < m_table_count && (lnk = lnk->next) != start_link);
if (lnk != start_link) printf("Unused_links aren't connected\n");
}
if (count != unused)
printf("Unused_links (%d) doesn't match table_def_cache: %d\n", count,
unused);
}
#endif
/**
Initialize all instances of table cache to be used by server.
@retval false - success.
@retval true - failure.
*/
bool Table_cache_manager::init() {
Table_cache::init_psi_keys();
for (uint i = 0; i < table_cache_instances; i++) {
if (m_table_cache[i].init()) {
for (uint j = 0; j < i; j++) m_table_cache[i].destroy();
return true;
}
}
return false;
}
/** Destroy all instances of table cache which were used by server. */
void Table_cache_manager::destroy() {
for (uint i = 0; i < table_cache_instances; i++) m_table_cache[i].destroy();
}
/**
Get total number of used and unused TABLE objects in all table caches.
@note Doesn't require acquisition of table cache locks if inexact number
of tables is acceptable.
*/
uint Table_cache_manager::cached_tables() {
uint result = 0;
for (uint i = 0; i < table_cache_instances; i++)
result += m_table_cache[i].cached_tables();
return result;
}
/**
Acquire locks on all instances of table cache and table definition
cache (i.e. LOCK_open).
*/
void Table_cache_manager::lock_all_and_tdc() {
for (uint i = 0; i < table_cache_instances; i++) m_table_cache[i].lock();
mysql_mutex_lock(&LOCK_open);
}
/**
Release locks on all instances of table cache and table definition
cache.
*/
void Table_cache_manager::unlock_all_and_tdc() {
mysql_mutex_unlock(&LOCK_open);
for (uint i = 0; i < table_cache_instances; i++) m_table_cache[i].unlock();
}
/**
Assert that caller owns lock on the table cache.
@param thd Thread handle
*/
void Table_cache_manager::assert_owner(THD *thd) {
Table_cache *tc = get_cache(thd);
tc->assert_owner();
}
/**
Assert that caller owns locks on all instances of table cache.
*/
void Table_cache_manager::assert_owner_all() {
for (uint i = 0; i < table_cache_instances; i++)
m_table_cache[i].assert_owner();
}
/**
Assert that caller owns locks on all instances of table cache
and table definition cache.
*/
void Table_cache_manager::assert_owner_all_and_tdc() {
assert_owner_all();
mysql_mutex_assert_owner(&LOCK_open);
}
/**
Remove and free all or some (depending on parameter) TABLE objects
for the table from all table cache instances.
@param thd Thread context
@param remove_type Type of removal. @sa tdc_remove_table().
@param share TABLE_SHARE for the table to be removed.
@note Caller should own LOCK_open and locks on all table cache
instances.
*/
void Table_cache_manager::free_table(THD *thd [[maybe_unused]],
enum_tdc_remove_table_type remove_type
[[maybe_unused]],
TABLE_SHARE *share) {
Table_cache_element *cache_el[MAX_TABLE_CACHES];
assert_owner_all_and_tdc();
/*
Freeing last TABLE instance for the share will destroy the share
and corresponding TABLE_SHARE::cache_element[] array. To make
iteration over this array safe, even when share is destroyed in
the middle of iteration, we create copy of this array on the stack
and iterate over it.
*/
memcpy(&cache_el, share->cache_element,
table_cache_instances * sizeof(Table_cache_element *));
for (uint i = 0; i < table_cache_instances; i++) {
if (cache_el[i]) {
Table_cache_element::TABLE_list::Iterator it(cache_el[i]->free_tables);
TABLE *table;
#ifndef NDEBUG
if (remove_type == TDC_RT_REMOVE_ALL)
assert(cache_el[i]->used_tables.is_empty());
else if (remove_type == TDC_RT_REMOVE_NOT_OWN ||
remove_type == TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE) {
Table_cache_element::TABLE_list::Iterator it2(cache_el[i]->used_tables);
while ((table = it2++)) {
if (table->in_use != thd) assert(0);
}
}
#endif
if (remove_type == TDC_RT_MARK_FOR_REOPEN) {
Table_cache_element::TABLE_list::Iterator it2(cache_el[i]->used_tables);
while ((table = it2++)) {
table->invalidate_stats();
}
}
while ((table = it++)) {
m_table_cache[i].remove_table(table);
intern_close_table(table);
}
}
}
}
/** Free all unused TABLE objects in all table cache instances. */
void Table_cache_manager::free_all_unused_tables() {
assert_owner_all_and_tdc();
for (uint i = 0; i < table_cache_instances; i++)
m_table_cache[i].free_all_unused_tables();
}
#ifndef NDEBUG
/**
Print debug information for the contents of all table cache instances.
*/
void Table_cache_manager::print_tables() {
puts(
"DB Table Version Thread Open "
"Lock");
for (uint i = 0; i < table_cache_instances; i++)
m_table_cache[i].print_tables();
}
#endif
|