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