MySQL 8.3.0
Source Code Documentation
Go to the documentation of this file.
1/* Copyright (c) 2003, 2023, Oracle and/or its affiliates.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 GNU General Public License, version 2.0, for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 @file include/keycache.h
25 Key cache variable structures.
28#ifndef _keycache_h
29#define _keycache_h
31#include <stddef.h>
32#include <sys/types.h>
34#include <string_view>
36#include "my_inttypes.h"
37#include "my_io.h"
38#include "my_macros.h"
39#include "my_sys.h" /* flush_type */
43/* declare structures that is used by KEY_CACHE */
45struct BLOCK_LINK;
46struct HASH_LINK;
48/* Thread specific variables */
52 void *opt_info;
55/* info about requests in a waiting queue */
57 st_keycache_thread_var *last_thread; /* circular list of waiting threads */
60/* Typical key cash */
61#define KEY_CACHE_SIZE (uint)(8 * 1024 * 1024)
62/* Default size of a key cache block */
63#define KEY_CACHE_BLOCK_SIZE (uint)1024
65#define CHANGED_BLOCKS_HASH 128 /* must be power of 2 */
68 The key cache structure
69 It also contains read-only statistics parameters.
72struct KEY_CACHE {
74 bool in_resize; /* true during resize operation */
75 bool resize_in_flush; /* true during flush of resize operation */
76 bool can_be_used; /* usage of cache for read/write is allowed */
77 size_t key_cache_mem_size; /* specified size of the cache memory */
78 uint key_cache_block_size; /* size of the page buffer of a cache block */
79 ulonglong min_warm_blocks; /* min number of warm blocks; */
80 ulonglong age_threshold; /* age threshold for hot blocks */
81 ulonglong keycache_time; /* total number of block link operations */
82 uint hash_entries; /* max number of entries in the hash table */
83 int hash_links; /* max number of hash links */
84 int hash_links_used; /* number of hash links currently used */
85 int disk_blocks; /* max number of blocks in the cache */
86 ulong blocks_used; /* maximum number of concurrently used blocks */
87 ulong blocks_unused; /* number of currently unused blocks */
88 ulong blocks_changed; /* number of currently dirty blocks */
89 ulong warm_blocks; /* number of blocks in warm sub-chain */
90 ulong cnt_for_resize_op; /* counter to block resize operation */
91 long blocks_available; /* number of blocks available in the LRU chain */
92 HASH_LINK **hash_root; /* arr. of entries into hash table buckets */
93 HASH_LINK *hash_link_root; /* memory for hash table links */
94 HASH_LINK *free_hash_list; /* list of free hash links */
95 BLOCK_LINK *free_block_list; /* list of free blocks */
96 BLOCK_LINK *block_root; /* memory for block links */
97 uchar *block_mem; /* memory for block buffers */
98 BLOCK_LINK *used_last; /* ptr to the last block of the LRU chain */
99 BLOCK_LINK *used_ins; /* ptr to the insertion block in LRU chain */
100 mysql_mutex_t cache_lock; /* to lock access to the cache structure */
101 KEYCACHE_WQUEUE resize_queue; /* threads waiting during resize operation */
102 /*
103 Waiting for a zero resize count. Using a queue for symmetry though
104 only one thread can wait here.
105 */
107 KEYCACHE_WQUEUE waiting_for_hash_link; /* waiting for a free hash link */
108 KEYCACHE_WQUEUE waiting_for_block; /* requests waiting for a free block */
109 BLOCK_LINK *changed_blocks[CHANGED_BLOCKS_HASH]; /* hash for dirty file bl.*/
110 BLOCK_LINK *file_blocks[CHANGED_BLOCKS_HASH]; /* hash for other file bl.*/
112 /*
113 The following variables are and variables used to hold parameters for
114 initializing the key cache.
115 */
117 ulonglong param_buff_size; /* size the memory allocated for the cache */
118 ulonglong param_block_size; /* size of the blocks in the key cache */
119 ulonglong param_division_limit; /* min. percentage of warm blocks */
120 ulonglong param_age_threshold; /* determines when hot block is downgraded */
122 /* Statistics variables. These are reset in reset_key_cache_counters(). */
123 ulong global_blocks_changed; /* number of currently dirty blocks */
124 ulonglong global_cache_w_requests; /* number of write requests (write hits) */
125 ulonglong global_cache_write; /* number of writes from cache to files */
126 ulonglong global_cache_r_requests; /* number of read requests (read hits) */
127 ulonglong global_cache_read; /* number of reads from files to cache */
129 int blocks; /* max number of blocks in the cache */
130 bool in_init; /* Set to 1 in MySQL during init/resize */
133/* The default key cache */
136extern int init_key_cache(KEY_CACHE *keycache, ulonglong key_cache_block_size,
137 size_t use_mem, ulonglong division_limit,
138 ulonglong age_threshold);
139extern int resize_key_cache(KEY_CACHE *keycache,
140 st_keycache_thread_var *thread_var,
141 ulonglong key_cache_block_size, size_t use_mem,
142 ulonglong division_limit, ulonglong age_threshold);
143extern uchar *key_cache_read(KEY_CACHE *keycache,
144 st_keycache_thread_var *thread_var, File file,
145 my_off_t filepos, int level, uchar *buff,
146 uint length, uint block_length, int return_buffer);
147extern int key_cache_insert(KEY_CACHE *keycache,
148 st_keycache_thread_var *thread_var, File file,
149 my_off_t filepos, int level, uchar *buff,
150 uint length);
151extern int key_cache_write(KEY_CACHE *keycache,
152 st_keycache_thread_var *thread_var, File file,
153 my_off_t filepos, int level, uchar *buff,
154 uint length, uint block_length, int force_write);
155extern int flush_key_blocks(KEY_CACHE *keycache,
156 st_keycache_thread_var *thread_var, int file,
157 enum flush_type type);
158extern void end_key_cache(KEY_CACHE *keycache, bool cleanup);
160/* Functions to handle multiple key caches */
161extern bool multi_keycache_init(void);
162extern void multi_keycache_free(void);
164extern bool multi_key_cache_set(const uchar *key, uint length,
165 KEY_CACHE *key_cache);
166extern void multi_key_cache_change(KEY_CACHE *old_data, KEY_CACHE *new_data);
167extern int reset_key_cache_counters(std::string_view name,
168 KEY_CACHE *key_cache);
169#endif /* _keycache_h */
app_data_ptr new_data(u_int n, char *val, cons_type consensus)
Definition: my_sys.h:296
void end_key_cache(KEY_CACHE *keycache, bool cleanup)
bool multi_keycache_init(void)
KEY_CACHE dflt_key_cache_var
int key_cache_insert(KEY_CACHE *keycache, st_keycache_thread_var *thread_var, File file, my_off_t filepos, int level, uchar *buff, uint length)
void multi_keycache_free(void)
bool multi_key_cache_set(const uchar *key, uint length, KEY_CACHE *key_cache)
Definition: keycache.h:65
int init_key_cache(KEY_CACHE *keycache, ulonglong key_cache_block_size, size_t use_mem, ulonglong division_limit, ulonglong age_threshold)
int reset_key_cache_counters(std::string_view name, KEY_CACHE *key_cache)
int flush_key_blocks(KEY_CACHE *keycache, st_keycache_thread_var *thread_var, int file, enum flush_type type)
void multi_key_cache_change(KEY_CACHE *old_data, KEY_CACHE *new_data)
KEY_CACHE * multi_key_cache_search(uchar *key, uint length)
KEY_CACHE * dflt_key_cache
Definition: keycache.h:134
uchar * key_cache_read(KEY_CACHE *keycache, st_keycache_thread_var *thread_var, File file, my_off_t filepos, int level, uchar *buff, uint length, uint block_length, int return_buffer)
int resize_key_cache(KEY_CACHE *keycache, st_keycache_thread_var *thread_var, ulonglong key_cache_block_size, size_t use_mem, ulonglong division_limit, ulonglong age_threshold)
int key_cache_write(KEY_CACHE *keycache, st_keycache_thread_var *thread_var, File file, my_off_t filepos, int level, uchar *buff, uint length, uint block_length, int force_write)
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
ulonglong my_off_t
Definition: my_inttypes.h:71
unsigned char uchar
Definition: my_inttypes.h:51
Common #defines and includes for file and socket I/O.
int File
Definition: my_io_bits.h:50
Some common macros.
Common header for many mysys elements.
Definition: os0file.h:88
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Instrumentation helpers for conditions.
Instrumentation helpers for mutexes.
required string key
Definition: replication_asynchronous_connection_failover.proto:59
required string type
Definition: replication_group_member_actions.proto:33
case opt name
Definition: sslopt-case.h:32
Definition: keycache.h:56
st_keycache_thread_var * last_thread
Definition: keycache.h:57
Definition: keycache.h:72
ulonglong global_cache_read
Definition: keycache.h:127
bool can_be_used
Definition: keycache.h:76
BLOCK_LINK * used_ins
Definition: keycache.h:99
HASH_LINK ** hash_root
Definition: keycache.h:92
ulonglong param_division_limit
Definition: keycache.h:119
BLOCK_LINK * used_last
Definition: keycache.h:98
int disk_blocks
Definition: keycache.h:85
ulonglong param_age_threshold
Definition: keycache.h:120
uint hash_entries
Definition: keycache.h:82
int hash_links_used
Definition: keycache.h:84
ulonglong global_cache_r_requests
Definition: keycache.h:126
int blocks
Definition: keycache.h:129
KEYCACHE_WQUEUE resize_queue
Definition: keycache.h:101
ulonglong global_cache_w_requests
Definition: keycache.h:124
ulonglong age_threshold
Definition: keycache.h:80
Definition: keycache.h:110
bool resize_in_flush
Definition: keycache.h:75
ulonglong global_cache_write
Definition: keycache.h:125
ulonglong param_buff_size
Definition: keycache.h:117
ulong blocks_unused
Definition: keycache.h:87
ulonglong param_block_size
Definition: keycache.h:118
KEYCACHE_WQUEUE waiting_for_resize_cnt
Definition: keycache.h:106
KEYCACHE_WQUEUE waiting_for_hash_link
Definition: keycache.h:107
HASH_LINK * hash_link_root
Definition: keycache.h:93
bool key_cache_inited
Definition: keycache.h:73
KEYCACHE_WQUEUE waiting_for_block
Definition: keycache.h:108
ulong blocks_changed
Definition: keycache.h:88
ulonglong min_warm_blocks
Definition: keycache.h:79
bool in_resize
Definition: keycache.h:74
ulong global_blocks_changed
Definition: keycache.h:123
ulong cnt_for_resize_op
Definition: keycache.h:90
bool in_init
Definition: keycache.h:130
int hash_links
Definition: keycache.h:83
ulong blocks_used
Definition: keycache.h:86
size_t key_cache_mem_size
Definition: keycache.h:77
long blocks_available
Definition: keycache.h:91
ulong warm_blocks
Definition: keycache.h:89
BLOCK_LINK * block_root
Definition: keycache.h:96
BLOCK_LINK * free_block_list
Definition: keycache.h:95
HASH_LINK * free_hash_list
Definition: keycache.h:94
uint key_cache_block_size
Definition: keycache.h:78
Definition: keycache.h:109
ulonglong keycache_time
Definition: keycache.h:81
mysql_mutex_t cache_lock
Definition: keycache.h:100
uchar * block_mem
Definition: keycache.h:97
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
Definition: keycache.h:49
struct st_keycache_thread_var * next
Definition: keycache.h:51
mysql_cond_t suspend
Definition: keycache.h:50
struct st_keycache_thread_var ** prev
Definition: keycache.h:51
void * opt_info
Definition: keycache.h:52