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