1/* Copyright (c) 2003, 2010, 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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/* Key cache variable structures */
17
18#ifndef _keycache_h
19#define _keycache_h
20
21#include "my_sys.h" /* flush_type */
22
23C_MODE_START
24
25/*
26 Currently the default key cache is created as non-partitioned at
27 the start of the server unless the server is started with the parameter
28 --key-cache-partitions that is greater than 0
29*/
30
31#define DEFAULT_KEY_CACHE_PARTITIONS 0
32
33/*
34 MAX_KEY_CACHE_PARTITIONS cannot be greater than
35 sizeof(MYISAM_SHARE::dirty_part_map)
36 Currently sizeof(MYISAM_SHARE::dirty_part_map)=sizeof(ulonglong)
37*/
38
39#define MAX_KEY_CACHE_PARTITIONS 64
40
41/* The structure to get statistical data about a key cache */
42
43typedef struct st_key_cache_statistics
44{
45 ulonglong mem_size; /* memory for cache buffers/auxiliary structures */
46 ulonglong block_size; /* size of the each buffers in the key cache */
47 ulonglong blocks_used; /* maximum number of used blocks/buffers */
48 ulonglong blocks_unused; /* number of currently unused blocks */
49 ulonglong blocks_changed; /* number of currently dirty blocks */
50 ulonglong blocks_warm; /* number of blocks in warm sub-chain */
51 ulonglong read_requests; /* number of read requests (read hits) */
52 ulonglong reads; /* number of actual reads from files into buffers */
53 ulonglong write_requests; /* number of write requests (write hits) */
54 ulonglong writes; /* number of actual writes from buffers into files */
55} KEY_CACHE_STATISTICS;
56
57#define NUM_LONG_KEY_CACHE_STAT_VARIABLES 3
58
59/* The type of a key cache object */
60typedef enum key_cache_type
61{
62 SIMPLE_KEY_CACHE,
63 PARTITIONED_KEY_CACHE
64} KEY_CACHE_TYPE;
65
66
67typedef
68 int (*INIT_KEY_CACHE)
69 (void *, uint key_cache_block_size,
70 size_t use_mem, uint division_limit, uint age_threshold,
71 uint changed_blocks_hash_size);
72typedef
73 int (*RESIZE_KEY_CACHE)
74 (void *, uint key_cache_block_size,
75 size_t use_mem, uint division_limit, uint age_threshold,
76 uint changed_blocks_hash_size);
77typedef
78 void (*CHANGE_KEY_CACHE_PARAM)
79 (void *keycache_cb,
80 uint division_limit, uint age_threshold);
81typedef
82 uchar* (*KEY_CACHE_READ)
83 (void *keycache_cb,
84 File file, my_off_t filepos, int level,
85 uchar *buff, uint length,
86 uint block_length, int return_buffer);
87typedef
88 int (*KEY_CACHE_INSERT)
89 (void *keycache_cb,
90 File file, my_off_t filepos, int level,
91 uchar *buff, uint length);
92typedef
93 int (*KEY_CACHE_WRITE)
94 (void *keycache_cb,
95 File file, void *file_extra,
96 my_off_t filepos, int level,
97 uchar *buff, uint length,
98 uint block_length, int force_write);
99typedef
100 int (*FLUSH_KEY_BLOCKS)
101 (void *keycache_cb,
102 int file, void *file_extra,
103 enum flush_type type);
104typedef
105 int (*RESET_KEY_CACHE_COUNTERS)
106 (const char *name, void *keycache_cb);
107typedef
108 void (*END_KEY_CACHE)
109 (void *keycache_cb, my_bool cleanup);
110typedef
111 void (*GET_KEY_CACHE_STATISTICS)
112 (void *keycache_cb, uint partition_no,
113 KEY_CACHE_STATISTICS *key_cache_stats);
114
115/*
116 An object of the type KEY_CACHE_FUNCS contains pointers to all functions
117 from the key cache interface.
118 Currently a key cache can be of two types: simple and partitioned.
119 For each of them its own static structure of the type KEY_CACHE_FUNCS is
120 defined . The structures contain the pointers to the implementations of
121 the interface functions used by simple key caches and partitioned key
122 caches respectively. Pointers to these structures are assigned to key cache
123 objects at the time of their creation.
124*/
125
126typedef struct st_key_cache_funcs
127{
128 INIT_KEY_CACHE init;
129 RESIZE_KEY_CACHE resize;
130 CHANGE_KEY_CACHE_PARAM change_param;
131 KEY_CACHE_READ read;
132 KEY_CACHE_INSERT insert;
133 KEY_CACHE_WRITE write;
134 FLUSH_KEY_BLOCKS flush;
135 RESET_KEY_CACHE_COUNTERS reset_counters;
136 END_KEY_CACHE end;
137 GET_KEY_CACHE_STATISTICS get_stats;
138} KEY_CACHE_FUNCS;
139
140
141typedef struct st_key_cache
142{
143 KEY_CACHE_TYPE key_cache_type; /* type of the key cache used for debugging */
144 void *keycache_cb; /* control block of the used key cache */
145 KEY_CACHE_FUNCS *interface_funcs; /* interface functions of the key cache */
146 ulonglong param_buff_size; /* size the memory allocated for the cache */
147 ulonglong param_block_size; /* size of the blocks in the key cache */
148 ulonglong param_division_limit;/* min. percentage of warm blocks */
149 ulonglong param_age_threshold; /* determines when hot block is downgraded */
150 ulonglong param_partitions; /* number of the key cache partitions */
151 ulonglong changed_blocks_hash_size; /* number of hash buckets for changed files */
152 my_bool key_cache_inited; /* <=> key cache has been created */
153 my_bool can_be_used; /* usage of cache for read/write is allowed */
154 my_bool in_init; /* set to 1 in MySQL during init/resize */
155 uint partitions; /* actual number of partitions */
156 size_t key_cache_mem_size; /* specified size of the cache memory */
157 pthread_mutex_t op_lock; /* to serialize operations like 'resize' */
158} KEY_CACHE;
159
160
161/* The default key cache */
162extern KEY_CACHE dflt_key_cache_var, *dflt_key_cache;
163
164extern int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
165 size_t use_mem, uint division_limit,
166 uint age_threshold, uint changed_blocks_hash_size,
167 uint partitions);
168extern int resize_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
169 size_t use_mem, uint division_limit,
170 uint age_threshold, uint changed_blocks_hash_size);
171extern void change_key_cache_param(KEY_CACHE *keycache, uint division_limit,
172 uint age_threshold);
173extern uchar *key_cache_read(KEY_CACHE *keycache,
174 File file, my_off_t filepos, int level,
175 uchar *buff, uint length,
176 uint block_length,int return_buffer);
177extern int key_cache_insert(KEY_CACHE *keycache,
178 File file, my_off_t filepos, int level,
179 uchar *buff, uint length);
180extern int key_cache_write(KEY_CACHE *keycache,
181 File file, void *file_extra,
182 my_off_t filepos, int level,
183 uchar *buff, uint length,
184 uint block_length, int force_write);
185extern int flush_key_blocks(KEY_CACHE *keycache,
186 int file, void *file_extra,
187 enum flush_type type);
188extern void end_key_cache(KEY_CACHE *keycache, my_bool cleanup);
189extern void get_key_cache_statistics(KEY_CACHE *keycache,
190 uint partition_no,
191 KEY_CACHE_STATISTICS *key_cache_stats);
192
193/* Functions to handle multiple key caches */
194extern my_bool multi_keycache_init(void);
195extern void multi_keycache_free(void);
196extern KEY_CACHE *multi_key_cache_search(uchar *key, uint length,
197 KEY_CACHE *def);
198extern my_bool multi_key_cache_set(const uchar *key, uint length,
199 KEY_CACHE *key_cache);
200extern void multi_key_cache_change(KEY_CACHE *old_data,
201 KEY_CACHE *new_data);
202extern int reset_key_cache_counters(const char *name,
203 KEY_CACHE *key_cache, void *);
204extern int repartition_key_cache(KEY_CACHE *keycache,
205 uint key_cache_block_size,
206 size_t use_mem,
207 uint division_limit,
208 uint age_threshold,
209 uint changed_blocks_hash_size,
210 uint partitions);
211C_MODE_END
212#endif /* _keycache_h */
213