104
104
#include <drizzled/global.h>
105
105
#include <mysys/mysys_err.h>
106
106
#include <mysys/my_sys.h>
107
#include <keycache.h>
107
#include "keycache.h"
108
108
#include <mystrings/m_string.h>
109
109
#include <mysys/my_bit.h>
110
110
#include <errno.h>
111
111
#include <stdarg.h>
113
static void change_key_cache_param(KEY_CACHE *keycache, uint32_t division_limit,
114
uint32_t age_threshold);
114
117
Some compilation flags have been added specifically for this module
115
118
to control the following:
161
163
struct st_block_link *block; /* reference to the block for the page: */
162
164
File file; /* from such a file */
163
165
my_off_t diskpos; /* with such an offset */
164
uint requests; /* number of requests for the page */
166
uint32_t requests; /* number of requests for the page */
167
169
/* simple states of a block */
193
195
*next_changed, **prev_changed; /* for lists of file dirty/clean blocks */
194
196
struct st_hash_link *hash_link; /* backward ptr to referring hash_link */
195
197
KEYCACHE_WQUEUE wqueue[2]; /* queues on waiting requests for new/old pages */
196
uint requests; /* number of requests for the block */
197
uchar *buffer; /* buffer for the block page */
198
uint offset; /* beginning of modified data in the buffer */
199
uint length; /* end of data in the buffer */
200
uint status; /* state of the block */
198
uint32_t requests; /* number of requests for the block */
199
unsigned char *buffer; /* buffer for the block page */
200
uint32_t offset; /* beginning of modified data in the buffer */
201
uint32_t length; /* end of data in the buffer */
202
uint32_t status; /* state of the block */
201
203
enum BLOCK_TEMPERATURE temperature; /* block temperature: cold, warm, hot */
202
uint hits_left; /* number of hits left until promotion */
204
uint32_t hits_left; /* number of hits left until promotion */
203
205
uint64_t last_hit_time; /* timestamp of the last hit */
204
206
KEYCACHE_CONDVAR *condvar; /* condition variable for 'no readers' event */
220
222
(uint32_t) (f)) & (keycache->hash_entries-1))
221
223
#define FILE_HASH(f) ((uint) (f) & (CHANGED_BLOCKS_HASH-1))
223
#define BLOCK_NUMBER(b) \
224
((uint) (((char*)(b)-(char *) keycache->block_root)/sizeof(BLOCK_LINK)))
225
#define HASH_LINK_NUMBER(h) \
226
((uint) (((char*)(h)-(char *) keycache->hash_link_root)/sizeof(HASH_LINK)))
228
226
#ifdef KEYCACHE_TIMEOUT
229
227
static int keycache_pthread_cond_wait(pthread_cond_t *cond,
236
234
#define keycache_pthread_mutex_unlock pthread_mutex_unlock
237
235
#define keycache_pthread_cond_signal pthread_cond_signal
239
static inline uint next_power(uint value)
237
static inline uint32_t next_power(uint32_t value)
241
239
return (uint) my_round_up_to_next_power((uint32_t) value) << 1;
270
int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
271
size_t use_mem, uint division_limit,
268
int init_key_cache(KEY_CACHE *keycache, uint32_t key_cache_block_size,
269
size_t use_mem, uint32_t division_limit,
270
uint32_t age_threshold)
274
272
uint32_t blocks, hash_links;
325
323
((size_t) blocks * keycache->key_cache_block_size) > use_mem)
327
325
/* Allocate memory for cache page buffers */
328
if ((keycache->block_mem= malloc((size_t) blocks * keycache->key_cache_block_size)))
326
if ((keycache->block_mem= (unsigned char *)malloc((size_t) blocks * keycache->key_cache_block_size)))
331
329
Allocate memory for blocks, hash_links and hash entries;
332
330
For each block 2 hash links are allocated
334
if ((keycache->block_root= (BLOCK_LINK*) my_malloc(length,
332
if ((keycache->block_root= (BLOCK_LINK*) malloc(length)))
337
334
free(keycache->block_mem);
338
335
keycache->block_mem= 0;
409
406
if (keycache->block_root)
411
my_free((uchar*) keycache->block_root, MYF(0));
408
free((unsigned char*) keycache->block_root);
412
409
keycache->block_root= NULL;
446
443
(when cnt_for_resize=0).
449
int resize_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
450
size_t use_mem, uint division_limit,
446
int resize_key_cache(KEY_CACHE *keycache, uint32_t key_cache_block_size,
447
size_t use_mem, uint32_t division_limit,
448
uint32_t age_threshold)
583
void change_key_cache_param(KEY_CACHE *keycache, uint division_limit,
578
static void change_key_cache_param(KEY_CACHE *keycache, uint32_t division_limit,
579
uint32_t age_threshold)
586
581
keycache_pthread_mutex_lock(&keycache->cache_lock);
587
582
if (division_limit)
619
614
free(keycache->block_mem);
620
615
keycache->block_mem= NULL;
621
my_free((uchar*) keycache->block_root, MYF(0));
616
free((unsigned char*) keycache->block_root);
622
617
keycache->block_root= NULL;
624
619
keycache->disk_blocks= -1;
809
804
Unlink a block from the chain of dirty/clean blocks
811
static inline void unlink_changed(BLOCK_LINK *block)
806
static void unlink_changed(BLOCK_LINK *block)
813
808
assert(block->prev_changed && *block->prev_changed == block);
814
809
if (block->next_changed)
823
818
Link a block into the chain of dirty/clean blocks
826
static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
821
static void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
828
823
assert(!block->next_changed);
829
824
assert(!block->prev_changed);
1779
1774
block->buffer= ADD_TO_PTR(keycache->block_mem,
1780
1775
((uint32_t) keycache->blocks_used*
1781
1776
keycache->key_cache_block_size),
1783
1778
keycache->blocks_used++;
1784
1779
assert(!block->next_used);
2100
2095
static void read_block(KEY_CACHE *keycache,
2101
BLOCK_LINK *block, uint read_length,
2102
uint min_length, bool primary)
2096
BLOCK_LINK *block, uint32_t read_length,
2097
uint32_t min_length, bool primary)
2099
uint32_t got_length;
2106
2101
/* On entry cache_lock is locked */
2197
2192
have to be a multiple of key_cache_block_size;
2200
uchar *key_cache_read(KEY_CACHE *keycache,
2195
unsigned char *key_cache_read(KEY_CACHE *keycache,
2201
2196
File file, my_off_t filepos, int level,
2202
uchar *buff, uint length,
2203
uint block_length __attribute__((unused)),
2204
int return_buffer __attribute__((unused)))
2197
unsigned char *buff, uint32_t length,
2198
uint32_t block_length,
2202
(void)return_buffer;
2206
2203
bool locked_and_incremented= false;
2205
unsigned char *start= buff;
2210
2207
if (keycache->key_cache_inited)
2212
2209
/* Key cache is used */
2213
2210
register BLOCK_LINK *block;
2211
uint32_t read_length;
2271
2268
keycache->global_cache_read++;
2272
2269
keycache_pthread_mutex_unlock(&keycache->cache_lock);
2273
error= (pread(file, (uchar*) buff, read_length, filepos + offset) == 0);
2270
error= (pread(file, (unsigned char*) buff, read_length, filepos + offset) == 0);
2274
2271
keycache_pthread_mutex_lock(&keycache->cache_lock);
2275
2272
goto next_block;
2355
2352
if (locked_and_incremented)
2356
2353
keycache_pthread_mutex_unlock(&keycache->cache_lock);
2357
if (pread(file, (uchar*) buff, length, filepos))
2354
if (!pread(file, (unsigned char*) buff, length, filepos))
2359
2356
if (locked_and_incremented)
2360
2357
keycache_pthread_mutex_lock(&keycache->cache_lock);
2392
2389
int key_cache_insert(KEY_CACHE *keycache,
2393
2390
File file, my_off_t filepos, int level,
2394
uchar *buff, uint length)
2391
unsigned char *buff, uint32_t length)
2626
2623
int key_cache_write(KEY_CACHE *keycache,
2627
2624
File file, my_off_t filepos, int level,
2628
uchar *buff, uint length,
2629
uint block_length __attribute__((unused)),
2625
unsigned char *buff, uint32_t length,
2626
uint32_t block_length,
2630
2627
int dont_write)
2632
2630
bool locked_and_incremented= false;
2635
2633
if (!dont_write)
2637
/* purecov: begin inspected */
2638
2635
/* Not used in the server. */
2639
2636
/* Force writing from buff into disk. */
2640
2637
keycache->global_cache_w_requests++;
2641
2638
keycache->global_cache_write++;
2642
2639
if (pwrite(file, buff, length, filepos) == 0)
2647
2643
if (keycache->key_cache_inited)
2649
2645
/* Key cache is used */
2650
2646
register BLOCK_LINK *block;
2647
uint32_t read_length;
2709
2705
/* Used in the server. */
2710
2706
keycache->global_cache_write++;
2711
2707
keycache_pthread_mutex_unlock(&keycache->cache_lock);
2712
if (pwrite(file, (uchar*) buff, read_length, filepos + offset) == 0)
2708
if (pwrite(file, (unsigned char*) buff, read_length, filepos + offset) == 0)
2714
2710
keycache_pthread_mutex_lock(&keycache->cache_lock);
2873
2869
keycache->global_cache_write++;
2874
2870
if (locked_and_incremented)
2875
2871
keycache_pthread_mutex_unlock(&keycache->cache_lock);
2876
if (pwrite(file, (uchar*) buff, length, filepos) == 0)
2872
if (pwrite(file, (unsigned char*) buff, length, filepos) == 0)
2878
2874
if (locked_and_incremented)
2879
2875
keycache_pthread_mutex_lock(&keycache->cache_lock);
3060
3056
As all blocks referred in 'cache' are marked by BLOCK_IN_FLUSH
3061
3057
we are guarunteed no thread will change them
3063
my_qsort((uchar*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link);
3059
my_qsort((unsigned char*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link);
3065
3061
keycache_pthread_mutex_lock(&keycache->cache_lock);
3185
3181
/* Key cache exists and flush is not disabled */
3187
uint count= FLUSH_CACHE;
3183
uint32_t count= FLUSH_CACHE;
3188
3184
BLOCK_LINK **pos,**end;
3189
3185
BLOCK_LINK *first_in_switch= NULL;
3190
3186
BLOCK_LINK *last_in_flush;
3191
3187
BLOCK_LINK *last_for_update;
3188
BLOCK_LINK *last_in_switch;
3192
3189
BLOCK_LINK *block, *next;
3194
3191
if (type != FLUSH_IGNORE_CHANGED)
3215
3212
changed blocks appear while we need to wait for something.
3217
3214
if ((count > FLUSH_CACHE) &&
3218
!(cache= (BLOCK_LINK**) my_malloc(sizeof(BLOCK_LINK*)*count,
3215
!(cache= (BLOCK_LINK**) malloc(sizeof(BLOCK_LINK*)*count)))
3220
3216
cache= cache_buff;
3222
3218
After a restart there could be more changed blocks than now.
3746
3742
reset_key_cache_counters()
3747
name the name of a key cache
3748
key_cache pointer to the key kache to be reset
3751
This procedure is used by process_key_caches() to reset the counters of all
3752
currently used key caches, both the default one and the named ones.
3745
This procedure is used by process_key_caches() to reset the key_cache.
3755
3748
0 on success (always because it can't fail)
3758
int reset_key_cache_counters(const char *name __attribute__((unused)),
3759
KEY_CACHE *key_cache)
3751
void reset_key_cache_counters()
3761
if (!key_cache->key_cache_inited)
3765
key_cache->global_blocks_changed= 0; /* Key_blocks_not_flushed */
3766
key_cache->global_cache_r_requests= 0; /* Key_read_requests */
3767
key_cache->global_cache_read= 0; /* Key_reads */
3768
key_cache->global_cache_w_requests= 0; /* Key_write_requests */
3769
key_cache->global_cache_write= 0; /* Key_writes */
3753
dflt_key_cache->global_blocks_changed= 0; /* Key_blocks_not_flushed */
3754
dflt_key_cache->global_cache_r_requests= 0; /* Key_read_requests */
3755
dflt_key_cache->global_cache_read= 0; /* Key_reads */
3756
dflt_key_cache->global_cache_w_requests= 0; /* Key_write_requests */
3757
dflt_key_cache->global_cache_write= 0; /* Key_writes */
3773
3760
#if defined(KEYCACHE_TIMEOUT)
3764
unsigned int hash_link_number(HASH_LINK *hash_link, KEY_CACHE *keycache)
3766
return ((unsigned int) (((char*)hash_link-(char *) keycache->hash_link_root)/
3767
sizeof(HASH_LINK)));
3771
unsigned int block_number(BLOCK_LINK *block, KEY_CACHE *keycache)
3773
return ((unsigned int) (((char*)block-(char *)keycache->block_root)/
3774
sizeof(BLOCK_LINK)));
3775
3778
#define KEYCACHE_DUMP_FILE "keycache_dump.txt"
3776
3779
#define MAX_QUEUE_LEN 100
3813
3816
thread=thread->next;
3814
3817
hash_link= (HASH_LINK *) thread->opt_info;
3815
3818
fprintf(keycache_dump_file,
3816
"thread:%u hash_link:%u (file,filepos)=(%u,%u)\n",
3817
thread->id, (uint) HASH_LINK_NUMBER(hash_link),
3818
(uint) hash_link->file,(uint32_t) hash_link->diskpos);
3819
"thread:%u hash_link:%u (file,filepos)=(%u,%u)\n",
3820
thread->id, (uint) hash_link_number(hash_link, keycache),
3821
(uint) hash_link->file,(uint32_t) hash_link->diskpos);
3819
3822
if (++i == MAX_QUEUE_LEN)
3827
3830
block= &keycache->block_root[i];
3828
3831
hash_link= block->hash_link;
3829
3832
fprintf(keycache_dump_file,
3830
"block:%u hash_link:%d status:%x #requests=%u waiting_for_readers:%d\n",
3831
i, (int) (hash_link ? HASH_LINK_NUMBER(hash_link) : -1),
3832
block->status, block->requests, block->condvar ? 1 : 0);
3833
"block:%u hash_link:%d status:%x #requests=%u "
3834
"waiting_for_readers:%d\n",
3835
i, (int) (hash_link ? hash_link_number(hash_link, keycache) : -1),
3836
block->status, block->requests, block->condvar ? 1 : 0);
3833
3837
for (j=0 ; j < 2; j++)
3835
3839
KEYCACHE_WQUEUE *wqueue=&block->wqueue[j];
3858
3862
block= block->next_used;
3859
3863
fprintf(keycache_dump_file,
3860
"block:%u, ", BLOCK_NUMBER(block));
3864
"block:%u, ", block_number(block, keycache));
3862
3866
while (block != keycache->used_last);