Commit 72830a77 authored by unknown's avatar unknown

Merge sinisa@bk-internal.mysql.com:/home/bk/mysql-4.1

into sinisa.nasamreza.org:/mnt/work/mysql-4.1

parents c9458e16 c8bdced2
...@@ -219,9 +219,9 @@ extern my_bool my_charset_same(CHARSET_INFO *cs1, CHARSET_INFO *cs2); ...@@ -219,9 +219,9 @@ extern my_bool my_charset_same(CHARSET_INFO *cs1, CHARSET_INFO *cs2);
/* statistics */ /* statistics */
extern ulong _my_cache_w_requests,_my_cache_write,_my_cache_r_requests, extern ulong my_cache_w_requests, my_cache_write, my_cache_r_requests,
_my_cache_read; my_cache_read;
extern ulong _my_blocks_used,_my_blocks_changed; extern ulong my_blocks_used, my_blocks_changed;
extern uint key_cache_block_size; extern uint key_cache_block_size;
extern ulong my_file_opened,my_stream_opened, my_tmp_file_created; extern ulong my_file_opened,my_stream_opened, my_tmp_file_created;
extern my_bool key_cache_inited, my_init_done; extern my_bool key_cache_inited, my_init_done;
......
...@@ -646,13 +646,13 @@ int main(int argc, char *argv[]) ...@@ -646,13 +646,13 @@ int main(int argc, char *argv[])
(long) range_records > (long) records*14/10+2) (long) range_records > (long) records*14/10+2)
{ {
printf("mi_records_range for key: %d returned %ld; Should be about %ld\n", printf("mi_records_range for key: %d returned %ld; Should be about %ld\n",
i, range_records, records); i, (long) range_records, (long) records);
goto end; goto end;
} }
if (verbose && records) if (verbose && records)
{ {
printf("mi_records_range returned %ld; Exact is %ld (diff: %4.2g %%)\n", printf("mi_records_range returned %ld; Exact is %ld (diff: %4.2g %%)\n",
range_records,records, (long) range_records, (long) records,
labs((long) range_records-(long) records)*100.0/records); labs((long) range_records-(long) records)*100.0/records);
} }
...@@ -667,7 +667,7 @@ int main(int argc, char *argv[]) ...@@ -667,7 +667,7 @@ int main(int argc, char *argv[])
{ {
puts("Wrong info from mi_info"); puts("Wrong info from mi_info");
printf("Got: records: %ld delete: %ld i_keys: %d\n", printf("Got: records: %ld delete: %ld i_keys: %d\n",
info.records,info.deleted,info.keys); (long) info.records, (long) info.deleted,info.keys);
} }
if (verbose) if (verbose)
{ {
...@@ -822,8 +822,8 @@ w_requests: %10lu\n\ ...@@ -822,8 +822,8 @@ w_requests: %10lu\n\
writes: %10lu\n\ writes: %10lu\n\
r_requests: %10lu\n\ r_requests: %10lu\n\
reads: %10lu\n", reads: %10lu\n",
_my_blocks_used,_my_cache_w_requests, _my_cache_write, my_blocks_used, my_cache_w_requests, my_cache_write,
_my_cache_r_requests,_my_cache_read); my_cache_r_requests, my_cache_read);
} }
end_key_cache(); end_key_cache();
if (blob_buffer) if (blob_buffer)
......
...@@ -272,7 +272,7 @@ int run_test(const char *filename) ...@@ -272,7 +272,7 @@ int run_test(const char *filename)
create_key(key, nrecords*upd); create_key(key, nrecords*upd);
print_key(key," INTERSECT\n"); print_key(key," INTERSECT\n");
hrows=mi_records_in_range(file,0,key,0,HA_READ_MBR_INTERSECT,record+1,0,0); hrows=mi_records_in_range(file,0,key,0,HA_READ_MBR_INTERSECT,record+1,0,0);
printf(" %ld rows\n",hrows); printf(" %ld rows\n", (long) hrows);
if (mi_close(file)) goto err; if (mi_close(file)) goto err;
......
...@@ -29,42 +29,42 @@ ...@@ -29,42 +29,42 @@
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
/* /*
Some compilation flags have been added specifically for this module Some compilation flags have been added specifically for this module
to control the following: to control the following:
- not to let a thread to yield the control when reading directly - not to let a thread to yield the control when reading directly
from key cache, which might improve performance in many cases; from key cache, which might improve performance in many cases;
to enable this add: to enable this add:
#define SERIALIZED_READ_FROM_CACHE #define SERIALIZED_READ_FROM_CACHE
- to set an upper bound for number of threads simultaneously - to set an upper bound for number of threads simultaneously
using the key cache; this setting helps to determine an optimal using the key cache; this setting helps to determine an optimal
size for hash table and improve performance when the number of size for hash table and improve performance when the number of
blocks in the key cache much less than the number of threads blocks in the key cache much less than the number of threads
accessing it; accessing it;
to set this number equal to <N> add to set this number equal to <N> add
#define MAX_THREADS <N> #define MAX_THREADS <N>
- to substitute calls of pthread_cond_wait for calls of - to substitute calls of pthread_cond_wait for calls of
pthread_cond_timedwait (wait with timeout set up); pthread_cond_timedwait (wait with timeout set up);
this setting should be used only when you want to trap a deadlock this setting should be used only when you want to trap a deadlock
situation, which theoretically should not happen; situation, which theoretically should not happen;
to set timeout equal to <T> seconds add to set timeout equal to <T> seconds add
#define KEYCACHE_TIMEOUT <T> #define KEYCACHE_TIMEOUT <T>
- to enable the module traps and to send debug information from - to enable the module traps and to send debug information from
key cache module to a special debug log add: key cache module to a special debug log add:
#define KEYCACHE_DEBUG #define KEYCACHE_DEBUG
the name of this debug log file <LOG NAME> can be set through: the name of this debug log file <LOG NAME> can be set through:
#define KEYCACHE_DEBUG_LOG <LOG NAME> #define KEYCACHE_DEBUG_LOG <LOG NAME>
if the name is not defined, it's set by default; if the name is not defined, it's set by default;
if the KEYCACHE_DEBUG flag is not set up and we are in a debug if the KEYCACHE_DEBUG flag is not set up and we are in a debug
mode, i.e. when ! defined(DBUG_OFF), the debug information from the mode, i.e. when ! defined(DBUG_OFF), the debug information from the
module is sent to the regular debug log. module is sent to the regular debug log.
Example of the settings: Example of the settings:
#define SERIALIZED_READ_FROM_CACHE #define SERIALIZED_READ_FROM_CACHE
#define MAX_THREADS 100 #define MAX_THREADS 100
#define KEYCACHE_TIMEOUT 1 #define KEYCACHE_TIMEOUT 1
#define KEYCACHE_DEBUG #define KEYCACHE_DEBUG
#define KEYCACHE_DEBUG_LOG "my_key_cache_debug.log" #define KEYCACHE_DEBUG_LOG "my_key_cache_debug.log"
*/ */
#if defined(MSDOS) && !defined(M_IC80386) #if defined(MSDOS) && !defined(M_IC80386)
...@@ -83,20 +83,24 @@ ...@@ -83,20 +83,24 @@
#define COND_FOR_SAVED 1 #define COND_FOR_SAVED 1
#define COND_FOR_READERS 2 #define COND_FOR_READERS 2
typedef pthread_cond_t KEYCACHE_CONDVAR; typedef pthread_cond_t KEYCACHE_CONDVAR;
typedef struct st_keycache_wqueue
{ /* info about requests in a waiting queue */ /* info about requests in a waiting queue */
typedef struct st_keycache_wqueue
{
struct st_my_thread_var *last_thread; /* circular list of waiting threads */ struct st_my_thread_var *last_thread; /* circular list of waiting threads */
} KEYCACHE_WQUEUE; } KEYCACHE_WQUEUE;
/* descriptor of the page in the key cache block buffer */
typedef struct st_keycache_page typedef struct st_keycache_page
{ /* descriptor of the page in the key cache block buffer */ {
int file; /* file to which the page belongs to */ int file; /* file to which the page belongs to */
my_off_t filepos; /* position of the page in the file */ my_off_t filepos; /* position of the page in the file */
} KEYCACHE_PAGE; } KEYCACHE_PAGE;
typedef struct st_hash_link /* element in the chain of a hash table bucket */
{ /* element in the chain of a hash table bucket */ typedef struct st_hash_link
{
struct st_hash_link *next, **prev; /* to connect links in the same bucket */ struct st_hash_link *next, **prev; /* to connect links in the same bucket */
struct st_block_link *block; /* reference to the block for the page: */ struct st_block_link *block; /* reference to the block for the page: */
File file; /* from such a file */ File file; /* from such a file */
...@@ -117,8 +121,9 @@ typedef struct st_hash_link ...@@ -117,8 +121,9 @@ typedef struct st_hash_link
#define PAGE_TO_BE_READ 1 #define PAGE_TO_BE_READ 1
#define PAGE_WAIT_TO_BE_READ 2 #define PAGE_WAIT_TO_BE_READ 2
/* key cache block */
typedef struct st_block_link typedef struct st_block_link
{ /* key cache block */ {
struct st_block_link struct st_block_link
*next_used, **prev_used; /* to connect links in the LRU chain (ring) */ *next_used, **prev_used; /* to connect links in the LRU chain (ring) */
struct st_block_link struct st_block_link
...@@ -143,40 +148,36 @@ static uint key_cache_shift; ...@@ -143,40 +148,36 @@ static uint key_cache_shift;
#define CHANGED_BLOCKS_HASH 128 /* must be power of 2 */ #define CHANGED_BLOCKS_HASH 128 /* must be power of 2 */
#define FLUSH_CACHE 2000 /* sort this many blocks at once */ #define FLUSH_CACHE 2000 /* sort this many blocks at once */
static KEYCACHE_WQUEUE static KEYCACHE_WQUEUE
waiting_for_hash_link; /* queue of requests waiting for a free hash link */ waiting_for_hash_link; /* queue of requests waiting for a free hash link */
static KEYCACHE_WQUEUE static KEYCACHE_WQUEUE
waiting_for_block; /* queue of requests waiting for a free block */ waiting_for_block; /* queue of requests waiting for a free block */
static HASH_LINK **_my_hash_root; /* arr. of entries into hash table buckets */ static HASH_LINK **my_hash_root; /* arr. of entries into hash table buckets */
static uint _my_hash_entries; /* max number of entries in the hash table */ static uint my_hash_entries; /* max number of entries in the hash table */
static HASH_LINK *_my_hash_link_root; /* memory for hash table links */ static HASH_LINK *my_hash_link_root; /* memory for hash table links */
static int _my_hash_links; /* max number of hash links */ static int my_hash_links; /* max number of hash links */
static int _my_hash_links_used; /* number of hash links currently used */ static int my_hash_links_used; /* number of hash links currently used */
static HASH_LINK *_my_free_hash_list; /* list of free hash links */ static HASH_LINK *my_free_hash_list; /* list of free hash links */
static BLOCK_LINK *_my_block_root; /* memory for block links */ static BLOCK_LINK *my_block_root; /* memory for block links */
static int _my_disk_blocks; /* max number of blocks in the cache */ static int my_disk_blocks; /* max number of blocks in the cache */
static byte HUGE_PTR *_my_block_mem; /* memory for block buffers */ static byte HUGE_PTR *my_block_mem; /* memory for block buffers */
static BLOCK_LINK *_my_used_last; /* ptr to the last block of the LRU chain */ static BLOCK_LINK *my_used_last; /* ptr to the last block of the LRU chain */
ulong _my_blocks_used, /* number of currently used blocks */ ulong my_blocks_used, /* number of currently used blocks */
_my_blocks_changed; /* number of currently dirty blocks */ my_blocks_changed; /* number of currently dirty blocks */
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
static static
ulong _my_blocks_available; /* number of blocks available in the LRU chain */ ulong my_blocks_available; /* number of blocks available in the LRU chain */
#endif /* defined(KEYCACHE_DEBUG) */ #endif /* defined(KEYCACHE_DEBUG) */
ulong _my_cache_w_requests,_my_cache_write, /* counters */ ulong my_cache_w_requests, my_cache_write, /* counters */
_my_cache_r_requests,_my_cache_read; /* for statistics */ my_cache_r_requests, my_cache_read; /* for statistics */
static BLOCK_LINK static BLOCK_LINK
*changed_blocks[CHANGED_BLOCKS_HASH]; /* hash table for file dirty blocks */ *changed_blocks[CHANGED_BLOCKS_HASH]; /* hash table for file dirty blocks */
static BLOCK_LINK static BLOCK_LINK
*file_blocks[CHANGED_BLOCKS_HASH]; /* hash table for other file blocks */ *file_blocks[CHANGED_BLOCKS_HASH]; /* hash table for other file blocks */
/* that are not free */ /* that are not free */
#ifndef DBUG_OFF
static my_bool _my_printed;
#endif
#define KEYCACHE_HASH(f, pos) \ #define KEYCACHE_HASH(f, pos) \
(((ulong) ((pos) >> key_cache_shift)+(ulong) (f)) & (_my_hash_entries-1)) (((ulong) ((pos) >> key_cache_shift)+(ulong) (f)) & (my_hash_entries-1))
#define FILE_HASH(f) ((uint) (f) & (CHANGED_BLOCKS_HASH-1)) #define FILE_HASH(f) ((uint) (f) & (CHANGED_BLOCKS_HASH-1))
#define DEFAULT_KEYCACHE_DEBUG_LOG "keycache_debug.log" #define DEFAULT_KEYCACHE_DEBUG_LOG "keycache_debug.log"
...@@ -194,11 +195,11 @@ static void keycache_debug_print _VARARGS((const char *fmt,...)); ...@@ -194,11 +195,11 @@ static void keycache_debug_print _VARARGS((const char *fmt,...));
#define KEYCACHE_DEBUG_CLOSE \ #define KEYCACHE_DEBUG_CLOSE \
if (keycache_debug_log) fclose(keycache_debug_log) if (keycache_debug_log) fclose(keycache_debug_log)
#else #else
#define KEYCACHE_DEBUG_OPEN #define KEYCACHE_DEBUG_OPEN
#define KEYCACHE_DEBUG_CLOSE #define KEYCACHE_DEBUG_CLOSE
#endif /* defined(KEYCACHE_DEBUG_LOG) */ #endif /* defined(KEYCACHE_DEBUG_LOG) */
#if defined(KEYCACHE_DEBUG_LOG) && defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG_LOG) && defined(KEYCACHE_DEBUG)
#define KEYCACHE_DBUG_PRINT(l, m) \ #define KEYCACHE_DBUG_PRINT(l, m) \
{ if (keycache_debug_log) fprintf(keycache_debug_log, "%s: ", l); \ { if (keycache_debug_log) fprintf(keycache_debug_log, "%s: ", l); \
keycache_debug_print m; } keycache_debug_print m; }
...@@ -224,15 +225,15 @@ static long keycache_thread_id; ...@@ -224,15 +225,15 @@ static long keycache_thread_id;
#define KEYCACHE_THREAD_TRACE_END(l) \ #define KEYCACHE_THREAD_TRACE_END(l) \
KEYCACHE_DBUG_PRINT(l,("]thread %ld",keycache_thread_id)) KEYCACHE_DBUG_PRINT(l,("]thread %ld",keycache_thread_id))
#else #else
#define KEYCACHE_THREAD_TRACE_BEGIN(l) #define KEYCACHE_THREAD_TRACE_BEGIN(l)
#define KEYCACHE_THREAD_TRACE_END(l) #define KEYCACHE_THREAD_TRACE_END(l)
#define KEYCACHE_THREAD_TRACE(l) #define KEYCACHE_THREAD_TRACE(l)
#endif /* defined(KEYCACHE_DEBUG) || !defined(DBUG_OFF) */ #endif /* defined(KEYCACHE_DEBUG) || !defined(DBUG_OFF) */
#define BLOCK_NUMBER(b) \ #define BLOCK_NUMBER(b) \
((uint) (((char*)(b) - (char *) _my_block_root) / sizeof(BLOCK_LINK))) ((uint) (((char*)(b) - (char *) my_block_root) / sizeof(BLOCK_LINK)))
#define HASH_LINK_NUMBER(h) \ #define HASH_LINK_NUMBER(h) \
((uint) (((char*)(h) - (char *) _my_hash_link_root) / sizeof(HASH_LINK))) ((uint) (((char*)(h) - (char *) my_hash_link_root) / sizeof(HASH_LINK)))
#if (defined(KEYCACHE_TIMEOUT) && !defined(__WIN__)) || defined(KEYCACHE_DEBUG) #if (defined(KEYCACHE_TIMEOUT) && !defined(__WIN__)) || defined(KEYCACHE_DEBUG)
static int keycache_pthread_cond_wait(pthread_cond_t *cond, static int keycache_pthread_cond_wait(pthread_cond_t *cond,
...@@ -266,18 +267,19 @@ static uint next_power(uint value) ...@@ -266,18 +267,19 @@ static uint next_power(uint value)
/* /*
Initialize the key cache, Initialize the key cache,
return number of blocks in it return number of blocks in it
*/ */
int init_key_cache(ulong use_mem) int init_key_cache(ulong use_mem)
{ {
uint blocks, hash_links, length; uint blocks, hash_links, length;
int error; int error;
DBUG_ENTER("init_key_cache"); DBUG_ENTER("init_key_cache");
KEYCACHE_DEBUG_OPEN; KEYCACHE_DEBUG_OPEN;
if (key_cache_inited && _my_disk_blocks > 0) if (key_cache_inited && my_disk_blocks > 0)
{ {
DBUG_PRINT("warning",("key cache already in use")); DBUG_PRINT("warning",("key cache already in use"));
DBUG_RETURN(0); DBUG_RETURN(0);
...@@ -285,101 +287,101 @@ int init_key_cache(ulong use_mem) ...@@ -285,101 +287,101 @@ int init_key_cache(ulong use_mem)
if (! key_cache_inited) if (! key_cache_inited)
{ {
key_cache_inited=TRUE; key_cache_inited=TRUE;
_my_disk_blocks= -1; my_disk_blocks= -1;
key_cache_shift=my_bit_log2(key_cache_block_size); key_cache_shift=my_bit_log2(key_cache_block_size);
DBUG_PRINT("info",("key_cache_block_size: %u", DBUG_PRINT("info",("key_cache_block_size: %u",
key_cache_block_size)); key_cache_block_size));
#ifndef DBUG_OFF
_my_printed=0;
#endif
} }
_my_cache_w_requests=_my_cache_r_requests=_my_cache_read=_my_cache_write=0; my_cache_w_requests= my_cache_r_requests= my_cache_read= my_cache_write=0;
_my_block_mem=NULL; my_block_mem=NULL;
_my_block_root=NULL; my_block_root=NULL;
blocks= (uint) (use_mem/(sizeof(BLOCK_LINK)+2*sizeof(HASH_LINK)+ blocks= (uint) (use_mem/(sizeof(BLOCK_LINK)+2*sizeof(HASH_LINK)+
sizeof(HASH_LINK*)*5/4+key_cache_block_size)); sizeof(HASH_LINK*)*5/4+key_cache_block_size));
/* It doesn't make sense to have too few blocks (less than 8) */ /* It doesn't make sense to have too few blocks (less than 8) */
if (blocks >= 8 && _my_disk_blocks < 0) if (blocks >= 8 && my_disk_blocks < 0)
{ {
for (;;) for (;;)
{ {
/* Set _my_hash_entries to the next bigger 2 power */ /* Set my_hash_entries to the next bigger 2 power */
if ((_my_hash_entries=next_power(blocks)) < blocks*5/4) if ((my_hash_entries=next_power(blocks)) < blocks*5/4)
_my_hash_entries<<=1; my_hash_entries<<=1;
hash_links=2*blocks; hash_links=2*blocks;
#if defined(MAX_THREADS) #if defined(MAX_THREADS)
if (hash_links < MAX_THREADS + blocks - 1) if (hash_links < MAX_THREADS + blocks - 1)
hash_links=MAX_THREADS + blocks - 1; hash_links=MAX_THREADS + blocks - 1;
#endif #endif
while ((length=blocks*sizeof(BLOCK_LINK)+hash_links*sizeof(HASH_LINK)+ while ((length=(ALIGN_SIZE(blocks*sizeof(BLOCK_LINK))+
sizeof(HASH_LINK*)*_my_hash_entries)+ ALIGN_SIZE(hash_links*sizeof(HASH_LINK))+
((ulong) blocks << key_cache_shift) > ALIGN_SIZE(sizeof(HASH_LINK*)*my_hash_entries)))+
use_mem) ((ulong) blocks << key_cache_shift) > use_mem)
blocks--; blocks--;
/* Allocate memory for cache page buffers */ /* Allocate memory for cache page buffers */
if ((_my_block_mem=my_malloc_lock((ulong) blocks*key_cache_block_size, if ((my_block_mem=my_malloc_lock((ulong) blocks*key_cache_block_size,
MYF(0)))) MYF(0))))
{ {
/* /*
Allocate memory for blocks, hash_links and hash entries; Allocate memory for blocks, hash_links and hash entries;
For each block 2 hash links are allocated For each block 2 hash links are allocated
*/ */
if ((_my_block_root=(BLOCK_LINK*) my_malloc((uint) length,MYF(0)))) if ((my_block_root=(BLOCK_LINK*) my_malloc((uint) length,MYF(0))))
break; break;
my_free_lock(_my_block_mem,MYF(0)); my_free_lock(my_block_mem,MYF(0));
} }
if (blocks < 8) if (blocks < 8)
{ {
my_errno=ENOMEM; my_errno=ENOMEM;
goto err; goto err;
} }
blocks=blocks/4*3; blocks=blocks/4*3;
} }
_my_disk_blocks=(int) blocks; my_disk_blocks=(int) blocks;
_my_hash_links=hash_links; my_hash_links=hash_links;
_my_hash_root=(HASH_LINK**) (_my_block_root+blocks); my_hash_root= (HASH_LINK**) ((char*) my_block_root +
_my_hash_link_root=(HASH_LINK*) (_my_hash_root+_my_hash_entries); ALIGN_SIZE(blocks*sizeof(BLOCK_LINK)));
bzero((byte*) _my_block_root,_my_disk_blocks*sizeof(BLOCK_LINK)); my_hash_link_root= (HASH_LINK*) ((char*) my_hash_root +
bzero((byte*) _my_hash_root,_my_hash_entries*sizeof(HASH_LINK*)); ALIGN_SIZE((sizeof(HASH_LINK*) *
bzero((byte*) _my_hash_link_root,_my_hash_links*sizeof(HASH_LINK)); my_hash_entries)));
_my_hash_links_used=0; bzero((byte*) my_block_root, my_disk_blocks*sizeof(BLOCK_LINK));
_my_free_hash_list=NULL; bzero((byte*) my_hash_root, my_hash_entries*sizeof(HASH_LINK*));
_my_blocks_used=_my_blocks_changed=0; bzero((byte*) my_hash_link_root, my_hash_links*sizeof(HASH_LINK));
my_hash_links_used=0;
my_free_hash_list=NULL;
my_blocks_used= my_blocks_changed=0;
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
_my_blocks_available=0; my_blocks_available=0;
#endif #endif
/* The LRU chain is empty after initialization */ /* The LRU chain is empty after initialization */
_my_used_last=NULL; my_used_last=NULL;
waiting_for_hash_link.last_thread=NULL; waiting_for_hash_link.last_thread=NULL;
waiting_for_block.last_thread=NULL; waiting_for_block.last_thread=NULL;
DBUG_PRINT("exit", DBUG_PRINT("exit",
("disk_blocks: %d block_root: %lx hash_entries: %d hash_root: %lx \ ("disk_blocks: %d block_root: %lx hash_entries: %d hash_root: %lx \
hash_links: %d hash_link_root %lx", hash_links: %d hash_link_root %lx",
_my_disk_blocks,_my_block_root,_my_hash_entries,_my_hash_root, my_disk_blocks, my_block_root, my_hash_entries, my_hash_root,
_my_hash_links,_my_hash_link_root)); my_hash_links, my_hash_link_root));
} }
bzero((gptr) changed_blocks,sizeof(changed_blocks[0])*CHANGED_BLOCKS_HASH); bzero((gptr) changed_blocks,sizeof(changed_blocks[0])*CHANGED_BLOCKS_HASH);
bzero((gptr) file_blocks,sizeof(file_blocks[0])*CHANGED_BLOCKS_HASH); bzero((gptr) file_blocks,sizeof(file_blocks[0])*CHANGED_BLOCKS_HASH);
DBUG_RETURN((int) blocks); DBUG_RETURN((int) blocks);
err: err:
error=my_errno; error=my_errno;
if (_my_block_mem) if (my_block_mem)
my_free_lock((gptr) _my_block_mem,MYF(0)); my_free_lock((gptr) my_block_mem,MYF(0));
if (_my_block_mem) if (my_block_mem)
my_free((gptr) _my_block_root,MYF(0)); my_free((gptr) my_block_root,MYF(0));
my_errno=error; my_errno=error;
DBUG_RETURN(0); DBUG_RETURN(0);
} }
/* /*
Resize the key cache Resize the key cache
*/ */
int resize_key_cache(ulong use_mem) int resize_key_cache(ulong use_mem)
{ {
...@@ -394,51 +396,54 @@ int resize_key_cache(ulong use_mem) ...@@ -394,51 +396,54 @@ int resize_key_cache(ulong use_mem)
end_key_cache(); end_key_cache();
/* the following will work even if memory is 0 */ /* the following will work even if memory is 0 */
blocks=init_key_cache(use_mem); blocks=init_key_cache(use_mem);
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
return blocks; return blocks;
} }
/* /*
Remove key_cache from memory Remove key_cache from memory
*/ */
void end_key_cache(void) void end_key_cache(void)
{ {
DBUG_ENTER("end_key_cache"); DBUG_ENTER("end_key_cache");
if (_my_disk_blocks > 0) if (my_disk_blocks > 0)
{ {
if (_my_block_mem) if (my_block_mem)
{ {
my_free_lock((gptr) _my_block_mem,MYF(0)); my_free_lock((gptr) my_block_mem,MYF(0));
my_free((gptr) _my_block_root,MYF(0)); my_free((gptr) my_block_root,MYF(0));
} }
_my_disk_blocks= -1; my_disk_blocks= -1;
} }
KEYCACHE_DEBUG_CLOSE; KEYCACHE_DEBUG_CLOSE;
key_cache_inited=0; key_cache_inited=0;
DBUG_PRINT("status", DBUG_PRINT("status",
("used: %d changed: %d w_requests: %ld \ ("used: %d changed: %d w_requests: %ld \
writes: %ld r_requests: %ld reads: %ld", writes: %ld r_requests: %ld reads: %ld",
_my_blocks_used,_my_blocks_changed,_my_cache_w_requests, my_blocks_used, my_blocks_changed, my_cache_w_requests,
_my_cache_write,_my_cache_r_requests,_my_cache_read)); my_cache_write, my_cache_r_requests, my_cache_read));
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} /* end_key_cache */ } /* end_key_cache */
/* /*
Link a thread into double-linked queue of waiting threads Link a thread into double-linked queue of waiting threads
*/ */
static inline void link_into_queue(KEYCACHE_WQUEUE *wqueue, static inline void link_into_queue(KEYCACHE_WQUEUE *wqueue,
struct st_my_thread_var *thread) struct st_my_thread_var *thread)
{ {
struct st_my_thread_var *last; struct st_my_thread_var *last;
if (! (last=wqueue->last_thread)) if (! (last=wqueue->last_thread))
{ /* Queue is empty */ {
/* Queue is empty */
thread->next=thread; thread->next=thread;
thread->prev=&thread->next; thread->prev=&thread->next;
} }
else else
{ {
thread->prev=last->next->prev; thread->prev=last->next->prev;
last->next->prev=&thread->next; last->next->prev=&thread->next;
thread->next=last->next; thread->next=last->next;
...@@ -448,16 +453,17 @@ static inline void link_into_queue(KEYCACHE_WQUEUE *wqueue, ...@@ -448,16 +453,17 @@ static inline void link_into_queue(KEYCACHE_WQUEUE *wqueue,
} }
/* /*
Unlink a thread from double-linked queue of waiting threads Unlink a thread from double-linked queue of waiting threads
*/ */
static inline void unlink_from_queue(KEYCACHE_WQUEUE *wqueue, static inline void unlink_from_queue(KEYCACHE_WQUEUE *wqueue,
struct st_my_thread_var *thread) struct st_my_thread_var *thread)
{ {
KEYCACHE_DBUG_PRINT("unlink_from_queue", ("thread %ld", thread->id)); KEYCACHE_DBUG_PRINT("unlink_from_queue", ("thread %ld", thread->id));
if (thread->next == thread) if (thread->next == thread)
/* The queue contains only one member */ /* The queue contains only one member */
wqueue->last_thread=NULL; wqueue->last_thread=NULL;
else else
{ {
thread->next->prev=thread->prev; thread->next->prev=thread->prev;
*thread->prev=thread->next; *thread->prev=thread->next;
...@@ -470,16 +476,17 @@ static inline void unlink_from_queue(KEYCACHE_WQUEUE *wqueue, ...@@ -470,16 +476,17 @@ static inline void unlink_from_queue(KEYCACHE_WQUEUE *wqueue,
/* /*
Add a thread to single-linked queue of waiting threads Add a thread to single-linked queue of waiting threads
*/ */
static inline void add_to_queue(KEYCACHE_WQUEUE *wqueue, static inline void add_to_queue(KEYCACHE_WQUEUE *wqueue,
struct st_my_thread_var *thread) struct st_my_thread_var *thread)
{ {
struct st_my_thread_var *last; struct st_my_thread_var *last;
if (! (last=wqueue->last_thread)) if (! (last=wqueue->last_thread))
thread->next=thread; thread->next=thread;
else else
{ {
thread->next=last->next; thread->next=last->next;
last->next=thread; last->next=thread;
} }
...@@ -488,10 +495,11 @@ static inline void add_to_queue(KEYCACHE_WQUEUE *wqueue, ...@@ -488,10 +495,11 @@ static inline void add_to_queue(KEYCACHE_WQUEUE *wqueue,
/* /*
Remove all threads from queue signaling them to proceed Remove all threads from queue signaling them to proceed
*/ */
static inline void release_queue(KEYCACHE_WQUEUE *wqueue)
{ static void release_queue(KEYCACHE_WQUEUE *wqueue)
{
struct st_my_thread_var *last=wqueue->last_thread; struct st_my_thread_var *last=wqueue->last_thread;
struct st_my_thread_var *next=last->next; struct st_my_thread_var *next=last->next;
struct st_my_thread_var *thread; struct st_my_thread_var *thread;
...@@ -509,8 +517,9 @@ static inline void release_queue(KEYCACHE_WQUEUE *wqueue) ...@@ -509,8 +517,9 @@ static inline void release_queue(KEYCACHE_WQUEUE *wqueue)
/* /*
Unlink a block from the chain of dirty/clean blocks Unlink a block from the chain of dirty/clean blocks
*/ */
static inline void unlink_changed(BLOCK_LINK *block) static inline void unlink_changed(BLOCK_LINK *block)
{ {
if (block->next_changed) if (block->next_changed)
...@@ -520,8 +529,9 @@ static inline void unlink_changed(BLOCK_LINK *block) ...@@ -520,8 +529,9 @@ static inline void unlink_changed(BLOCK_LINK *block)
/* /*
Link a block into the chain of dirty/clean blocks Link a block into the chain of dirty/clean blocks
*/ */
static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead) static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
{ {
block->prev_changed=phead; block->prev_changed=phead;
...@@ -532,11 +542,12 @@ static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead) ...@@ -532,11 +542,12 @@ static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
/* /*
Unlink a block from the chain of dirty/clean blocks, if it's asked for, Unlink a block from the chain of dirty/clean blocks, if it's asked for,
and link it to the chain of clean blocks for the specified file and link it to the chain of clean blocks for the specified file
*/ */
static inline void link_to_file_list(BLOCK_LINK *block,int file,
my_bool unlink) static void link_to_file_list(BLOCK_LINK *block,int file,
my_bool unlink)
{ {
if (unlink) if (unlink)
unlink_changed(block); unlink_changed(block);
...@@ -544,27 +555,29 @@ static inline void link_to_file_list(BLOCK_LINK *block,int file, ...@@ -544,27 +555,29 @@ static inline void link_to_file_list(BLOCK_LINK *block,int file,
if (block->status & BLOCK_CHANGED) if (block->status & BLOCK_CHANGED)
{ {
block->status&=~BLOCK_CHANGED; block->status&=~BLOCK_CHANGED;
_my_blocks_changed--; my_blocks_changed--;
} }
} }
/* /*
Unlink a block from the chain of clean blocks for the specified Unlink a block from the chain of clean blocks for the specified
file and link it to the chain of dirty blocks for this file file and link it to the chain of dirty blocks for this file
*/ */
static inline void link_to_changed_list(BLOCK_LINK *block) static inline void link_to_changed_list(BLOCK_LINK *block)
{ {
unlink_changed(block); unlink_changed(block);
link_changed(block,&changed_blocks[FILE_HASH(block->hash_link->file)]); link_changed(block,&changed_blocks[FILE_HASH(block->hash_link->file)]);
block->status|=BLOCK_CHANGED; block->status|=BLOCK_CHANGED;
_my_blocks_changed++; my_blocks_changed++;
} }
/* /*
Link a block to the LRU chain at the beginning or at the end Link a block to the LRU chain at the beginning or at the end
*/ */
static void link_block(BLOCK_LINK *block, my_bool at_end) static void link_block(BLOCK_LINK *block, my_bool at_end)
{ {
KEYCACHE_DBUG_ASSERT(! (block->hash_link && block->hash_link->requests)); KEYCACHE_DBUG_ASSERT(! (block->hash_link && block->hash_link->requests));
...@@ -579,7 +592,7 @@ static void link_block(BLOCK_LINK *block, my_bool at_end) ...@@ -579,7 +592,7 @@ static void link_block(BLOCK_LINK *block, my_bool at_end)
{ {
thread=next_thread; thread=next_thread;
next_thread=thread->next; next_thread=thread->next;
/* /*
We notify about the event all threads that ask We notify about the event all threads that ask
for the same page as the first thread in the queue for the same page as the first thread in the queue
*/ */
...@@ -589,78 +602,79 @@ static void link_block(BLOCK_LINK *block, my_bool at_end) ...@@ -589,78 +602,79 @@ static void link_block(BLOCK_LINK *block, my_bool at_end)
unlink_from_queue(&waiting_for_block, thread); unlink_from_queue(&waiting_for_block, thread);
block->requests++; block->requests++;
} }
} }
while (thread != last_thread); while (thread != last_thread);
hash_link->block=block; hash_link->block=block;
KEYCACHE_THREAD_TRACE("link_block: after signaling"); KEYCACHE_THREAD_TRACE("link_block: after signaling");
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
KEYCACHE_DBUG_PRINT("link_block", KEYCACHE_DBUG_PRINT("link_block",
("linked,unlinked block %u status=%x #requests=%u #available=%u", ("linked,unlinked block %u status=%x #requests=%u #available=%u",
BLOCK_NUMBER(block),block->status, BLOCK_NUMBER(block),block->status,
block->requests,_my_blocks_available)); block->requests, my_blocks_available));
#endif #endif
return; return;
} }
if (_my_used_last) if (my_used_last)
{ {
_my_used_last->next_used->prev_used=&block->next_used; my_used_last->next_used->prev_used=&block->next_used;
block->next_used=_my_used_last->next_used; block->next_used= my_used_last->next_used;
block->prev_used=&_my_used_last->next_used; block->prev_used= &my_used_last->next_used;
_my_used_last->next_used=block; my_used_last->next_used=block;
if (at_end) if (at_end)
_my_used_last=block; my_used_last=block;
} }
else else
{ {
/* The LRU chain is empty */ /* The LRU chain is empty */
_my_used_last=block->next_used=block; my_used_last=block->next_used=block;
block->prev_used=&block->next_used; block->prev_used=&block->next_used;
} }
KEYCACHE_THREAD_TRACE("link_block"); KEYCACHE_THREAD_TRACE("link_block");
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
_my_blocks_available++; my_blocks_available++;
KEYCACHE_DBUG_PRINT("link_block", KEYCACHE_DBUG_PRINT("link_block",
("linked block %u:%1u status=%x #requests=%u #available=%u", ("linked block %u:%1u status=%x #requests=%u #available=%u",
BLOCK_NUMBER(block),at_end,block->status, BLOCK_NUMBER(block),at_end,block->status,
block->requests,_my_blocks_available)); block->requests, my_blocks_available));
KEYCACHE_DBUG_ASSERT(_my_blocks_available <= _my_blocks_used); KEYCACHE_DBUG_ASSERT(my_blocks_available <= my_blocks_used);
#endif #endif
} }
/* /*
Unlink a block from the LRU chain Unlink a block from the LRU chain
*/ */
static inline void unlink_block(BLOCK_LINK *block)
static void unlink_block(BLOCK_LINK *block)
{ {
if (block->next_used == block) if (block->next_used == block)
/* The list contains only one member */ /* The list contains only one member */
_my_used_last=NULL; my_used_last=NULL;
else else
{ {
block->next_used->prev_used=block->prev_used; block->next_used->prev_used=block->prev_used;
*block->prev_used=block->next_used; *block->prev_used=block->next_used;
if (_my_used_last == block) if (my_used_last == block)
_my_used_last=STRUCT_PTR(BLOCK_LINK, next_used, block->prev_used); my_used_last=STRUCT_PTR(BLOCK_LINK, next_used, block->prev_used);
} }
block->next_used=NULL; block->next_used=NULL;
KEYCACHE_THREAD_TRACE("unlink_block"); KEYCACHE_THREAD_TRACE("unlink_block");
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
_my_blocks_available--; my_blocks_available--;
KEYCACHE_DBUG_PRINT("unlink_block", KEYCACHE_DBUG_PRINT("unlink_block",
("unlinked block %u status=%x #requests=%u #available=%u", ("unlinked block %u status=%x #requests=%u #available=%u",
BLOCK_NUMBER(block),block->status, BLOCK_NUMBER(block),block->status,
block->requests,_my_blocks_available)); block->requests, my_blocks_available));
KEYCACHE_DBUG_ASSERT(_my_blocks_available >= 0); KEYCACHE_DBUG_ASSERT(my_blocks_available >= 0);
#endif #endif
} }
/* /*
Register requests for a block Register requests for a block
*/ */
static inline void reg_requests(BLOCK_LINK *block, int count) static void reg_requests(BLOCK_LINK *block, int count)
{ {
if (! block->requests) if (! block->requests)
/* First request for the block unlinks it */ /* First request for the block unlinks it */
...@@ -669,10 +683,11 @@ static inline void reg_requests(BLOCK_LINK *block, int count) ...@@ -669,10 +683,11 @@ static inline void reg_requests(BLOCK_LINK *block, int count)
} }
/* /*
Unregister request for a block Unregister request for a block
linking it to the LRU chain if it's the last request linking it to the LRU chain if it's the last request
*/ */
static inline void unreg_request(BLOCK_LINK *block, int at_end) static inline void unreg_request(BLOCK_LINK *block, int at_end)
{ {
if (! --block->requests) if (! --block->requests)
...@@ -680,18 +695,19 @@ static inline void unreg_request(BLOCK_LINK *block, int at_end) ...@@ -680,18 +695,19 @@ static inline void unreg_request(BLOCK_LINK *block, int at_end)
} }
/* /*
Remove a reader of the page in block Remove a reader of the page in block
*/ */
static inline void remove_reader(BLOCK_LINK *block) static inline void remove_reader(BLOCK_LINK *block)
{ {
if (! --block->hash_link->requests && block->condvar) if (! --block->hash_link->requests && block->condvar)
keycache_pthread_cond_signal(block->condvar); keycache_pthread_cond_signal(block->condvar);
} }
/* /*
Wait until the last reader of the page in block Wait until the last reader of the page in block
signals on its termination signals on its termination
*/ */
static inline void wait_for_readers(BLOCK_LINK *block) static inline void wait_for_readers(BLOCK_LINK *block)
{ {
...@@ -706,8 +722,9 @@ static inline void wait_for_readers(BLOCK_LINK *block) ...@@ -706,8 +722,9 @@ static inline void wait_for_readers(BLOCK_LINK *block)
/* /*
add a hash link to a bucket in the hash_table Add a hash link to a bucket in the hash_table
*/ */
static inline void link_hash(HASH_LINK **start, HASH_LINK *hash_link) static inline void link_hash(HASH_LINK **start, HASH_LINK *hash_link)
{ {
if (*start) if (*start)
...@@ -719,9 +736,10 @@ static inline void link_hash(HASH_LINK **start, HASH_LINK *hash_link) ...@@ -719,9 +736,10 @@ static inline void link_hash(HASH_LINK **start, HASH_LINK *hash_link)
/* /*
Remove a hash link from the hash table Remove a hash link from the hash table
*/ */
static inline void unlink_hash(HASH_LINK *hash_link)
static void unlink_hash(HASH_LINK *hash_link)
{ {
KEYCACHE_DBUG_PRINT("unlink_hash", ("file %u, filepos %lu #requests=%u", KEYCACHE_DBUG_PRINT("unlink_hash", ("file %u, filepos %lu #requests=%u",
(uint) hash_link->file,(ulong) hash_link->diskpos, hash_link->requests)); (uint) hash_link->file,(ulong) hash_link->diskpos, hash_link->requests));
...@@ -730,7 +748,8 @@ static inline void unlink_hash(HASH_LINK *hash_link) ...@@ -730,7 +748,8 @@ static inline void unlink_hash(HASH_LINK *hash_link)
hash_link->next->prev=hash_link->prev; hash_link->next->prev=hash_link->prev;
hash_link->block=NULL; hash_link->block=NULL;
if (waiting_for_hash_link.last_thread) if (waiting_for_hash_link.last_thread)
{ /* Signal that A free hash link appeared */ {
/* Signal that A free hash link appeared */
struct st_my_thread_var *last_thread=waiting_for_hash_link.last_thread; struct st_my_thread_var *last_thread=waiting_for_hash_link.last_thread;
struct st_my_thread_var *first_thread=last_thread->next; struct st_my_thread_var *first_thread=last_thread->next;
struct st_my_thread_var *next_thread=first_thread; struct st_my_thread_var *next_thread=first_thread;
...@@ -745,7 +764,7 @@ static inline void unlink_hash(HASH_LINK *hash_link) ...@@ -745,7 +764,7 @@ static inline void unlink_hash(HASH_LINK *hash_link)
thread=next_thread; thread=next_thread;
page= (KEYCACHE_PAGE *) thread->opt_info; page= (KEYCACHE_PAGE *) thread->opt_info;
next_thread=thread->next; next_thread=thread->next;
/* /*
We notify about the event all threads that ask We notify about the event all threads that ask
for the same page as the first thread in the queue for the same page as the first thread in the queue
*/ */
...@@ -756,18 +775,20 @@ static inline void unlink_hash(HASH_LINK *hash_link) ...@@ -756,18 +775,20 @@ static inline void unlink_hash(HASH_LINK *hash_link)
} }
} }
while (thread != last_thread); while (thread != last_thread);
link_hash(&_my_hash_root[KEYCACHE_HASH(hash_link->file, link_hash(&my_hash_root[KEYCACHE_HASH(hash_link->file,
hash_link->diskpos)], hash_link); hash_link->diskpos)], hash_link);
return; return;
} }
hash_link->next=_my_free_hash_list; hash_link->next= my_free_hash_list;
_my_free_hash_list=hash_link; my_free_hash_list=hash_link;
} }
/* /*
Get the hash link for a page Get the hash link for a page
*/ */
static inline HASH_LINK *get_hash_link(int file, my_off_t filepos)
static HASH_LINK *get_hash_link(int file, my_off_t filepos)
{ {
reg1 HASH_LINK *hash_link, **start; reg1 HASH_LINK *hash_link, **start;
KEYCACHE_PAGE page; KEYCACHE_PAGE page;
...@@ -784,7 +805,7 @@ restart: ...@@ -784,7 +805,7 @@ restart:
start contains the head of the bucket list, start contains the head of the bucket list,
hash_link points to the first member of the list hash_link points to the first member of the list
*/ */
hash_link=*(start=&_my_hash_root[KEYCACHE_HASH(file, filepos)]); hash_link= *(start= &my_hash_root[KEYCACHE_HASH(file, filepos)]);
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt=0; cnt=0;
#endif #endif
...@@ -795,7 +816,7 @@ restart: ...@@ -795,7 +816,7 @@ restart:
hash_link= hash_link->next; hash_link= hash_link->next;
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt++; cnt++;
if (! (cnt <= _my_hash_links_used)) if (! (cnt <= my_hash_links_used))
{ {
int i; int i;
for (i=0, hash_link=*start ; for (i=0, hash_link=*start ;
...@@ -805,22 +826,24 @@ restart: ...@@ -805,22 +826,24 @@ restart:
(uint) hash_link->file,(ulong) hash_link->diskpos)); (uint) hash_link->file,(ulong) hash_link->diskpos));
} }
} }
KEYCACHE_DBUG_ASSERT(n <= _my_hash_links_used); KEYCACHE_DBUG_ASSERT(n <= my_hash_links_used);
#endif #endif
} }
if (! hash_link) if (! hash_link)
{ /* There is no hash link in the hash table for the pair (file, filepos) */ {
if (_my_free_hash_list) /* There is no hash link in the hash table for the pair (file, filepos) */
if (my_free_hash_list)
{ {
hash_link=_my_free_hash_list; hash_link= my_free_hash_list;
_my_free_hash_list=hash_link->next; my_free_hash_list=hash_link->next;
} }
else if (_my_hash_links_used < _my_hash_links) else if (my_hash_links_used < my_hash_links)
{ {
hash_link= &_my_hash_link_root[_my_hash_links_used++]; hash_link= &my_hash_link_root[my_hash_links_used++];
} }
else else
{ /* Wait for a free hash link */ {
/* Wait for a free hash link */
struct st_my_thread_var *thread=my_thread_var; struct st_my_thread_var *thread=my_thread_var;
KEYCACHE_DBUG_PRINT("get_hash_link", ("waiting")); KEYCACHE_DBUG_PRINT("get_hash_link", ("waiting"));
page.file=file; page.filepos=filepos; page.file=file; page.filepos=filepos;
...@@ -836,16 +859,17 @@ restart: ...@@ -836,16 +859,17 @@ restart:
} }
/* Register the request for the page */ /* Register the request for the page */
hash_link->requests++; hash_link->requests++;
return hash_link; return hash_link;
} }
/* /*
Get a block for the file page requested by a keycache read/write operation; Get a block for the file page requested by a keycache read/write operation;
If the page is not in the cache return a free block, if there is none If the page is not in the cache return a free block, if there is none
return the lru block after saving its buffer if the page is dirty return the lru block after saving its buffer if the page is dirty
*/ */
static BLOCK_LINK *find_key_block(int file, my_off_t filepos, static BLOCK_LINK *find_key_block(int file, my_off_t filepos,
int wrmode, int *page_st) int wrmode, int *page_st)
{ {
...@@ -853,7 +877,7 @@ static BLOCK_LINK *find_key_block(int file, my_off_t filepos, ...@@ -853,7 +877,7 @@ static BLOCK_LINK *find_key_block(int file, my_off_t filepos,
BLOCK_LINK *block; BLOCK_LINK *block;
int error=0; int error=0;
int page_status; int page_status;
DBUG_ENTER("find_key_block"); DBUG_ENTER("find_key_block");
KEYCACHE_THREAD_TRACE("find_key_block:begin"); KEYCACHE_THREAD_TRACE("find_key_block:begin");
DBUG_PRINT("enter", ("file %u, filepos %lu, wrmode %lu", DBUG_PRINT("enter", ("file %u, filepos %lu, wrmode %lu",
...@@ -863,22 +887,22 @@ static BLOCK_LINK *find_key_block(int file, my_off_t filepos, ...@@ -863,22 +887,22 @@ static BLOCK_LINK *find_key_block(int file, my_off_t filepos,
#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG)
DBUG_EXECUTE("check_keycache2",test_key_cache("start of find_key_block",0);); DBUG_EXECUTE("check_keycache2",test_key_cache("start of find_key_block",0););
#endif #endif
restart: restart:
/* Find the hash link for the requested page (file, filepos) */ /* Find the hash link for the requested page (file, filepos) */
hash_link=get_hash_link(file, filepos); hash_link=get_hash_link(file, filepos);
page_status=-1; page_status=-1;
if ((block=hash_link->block) && if ((block=hash_link->block) &&
block->hash_link == hash_link && (block->status & BLOCK_READ)) block->hash_link == hash_link && (block->status & BLOCK_READ))
page_status=PAGE_READ; page_status=PAGE_READ;
if (page_status == PAGE_READ && (block->status & BLOCK_IN_SWITCH)) if (page_status == PAGE_READ && (block->status & BLOCK_IN_SWITCH))
{ /* This is a request for a page to be removed from cache */ {
/* This is a request for a page to be removed from cache */
KEYCACHE_DBUG_PRINT("find_key_block", KEYCACHE_DBUG_PRINT("find_key_block",
("request for old page in block %u",BLOCK_NUMBER(block))); ("request for old page in block %u",BLOCK_NUMBER(block)));
/* /*
Only reading requests can proceed until the old dirty page is flushed, Only reading requests can proceed until the old dirty page is flushed,
all others are to be suspended, then resubmitted all others are to be suspended, then resubmitted
*/ */
...@@ -887,7 +911,7 @@ restart: ...@@ -887,7 +911,7 @@ restart:
else else
{ {
hash_link->requests--; hash_link->requests--;
KEYCACHE_DBUG_PRINT("find_key_block", KEYCACHE_DBUG_PRINT("find_key_block",
("request waiting for old page to be saved")); ("request waiting for old page to be saved"));
{ {
struct st_my_thread_var *thread=my_thread_var; struct st_my_thread_var *thread=my_thread_var;
...@@ -900,47 +924,51 @@ restart: ...@@ -900,47 +924,51 @@ restart:
} }
while(thread->next); while(thread->next);
} }
KEYCACHE_DBUG_PRINT("find_key_block", KEYCACHE_DBUG_PRINT("find_key_block",
("request for old page resubmitted")); ("request for old page resubmitted"));
/* Resubmit the request */ /* Resubmit the request */
goto restart; goto restart;
} }
} }
else else
{ /* This is a request for a new page or for a page not to be removed */ {
/* This is a request for a new page or for a page not to be removed */
if (! block) if (! block)
{ /* No block is assigned for the page yet */ {
if (_my_blocks_used < (uint) _my_disk_blocks) /* No block is assigned for the page yet */
{ /* There are some never used blocks, take first of them */ if (my_blocks_used < (uint) my_disk_blocks)
hash_link->block=block= &_my_block_root[_my_blocks_used]; {
block->buffer=ADD_TO_PTR(_my_block_mem, /* There are some never used blocks, take first of them */
((ulong) _my_blocks_used*key_cache_block_size), hash_link->block=block= &my_block_root[my_blocks_used];
block->buffer=ADD_TO_PTR(my_block_mem,
((ulong) my_blocks_used*key_cache_block_size),
byte*); byte*);
block->status=0; block->status=0;
block->length=0; block->length=0;
block->offset=key_cache_block_size; block->offset=key_cache_block_size;
block->requests=1; block->requests=1;
_my_blocks_used++; my_blocks_used++;
link_to_file_list(block, file, 0); link_to_file_list(block, file, 0);
block->hash_link=hash_link; block->hash_link=hash_link;
page_status=PAGE_TO_BE_READ; page_status=PAGE_TO_BE_READ;
KEYCACHE_DBUG_PRINT("find_key_block", KEYCACHE_DBUG_PRINT("find_key_block",
("got never used block %u",BLOCK_NUMBER(block))); ("got never used block %u",BLOCK_NUMBER(block)));
} }
else else
{ /* There are no never used blocks, use a block from the LRU chain */ {
/* There are no never used blocks, use a block from the LRU chain */
/* /*
Wait until a new block is added to the LRU chain; Wait until a new block is added to the LRU chain;
several threads might wait here for the same page, several threads might wait here for the same page,
all of them must get the same block all of them must get the same block
*/ */
if (! _my_used_last) if (! my_used_last)
{ {
struct st_my_thread_var *thread=my_thread_var; struct st_my_thread_var *thread=my_thread_var;
thread->opt_info=(void *) hash_link; thread->opt_info=(void *) hash_link;
link_into_queue(&waiting_for_block, thread); link_into_queue(&waiting_for_block, thread);
do do
{ {
keycache_pthread_cond_wait(&thread->suspend,&THR_LOCK_keycache); keycache_pthread_cond_wait(&thread->suspend,&THR_LOCK_keycache);
} }
...@@ -950,50 +978,53 @@ restart: ...@@ -950,50 +978,53 @@ restart:
block=hash_link->block; block=hash_link->block;
if (! block) if (! block)
{ {
/* /*
Take the first block from the LRU chain Take the first block from the LRU chain
unlinking it from the chain unlinking it from the chain
*/ */
block=_my_used_last->next_used; block= my_used_last->next_used;
reg_requests(block,1); reg_requests(block,1);
hash_link->block=block; hash_link->block=block;
} }
if (block->hash_link != hash_link && ! (block->status & BLOCK_IN_SWITCH) ) if (block->hash_link != hash_link &&
{ /* this is a primary request for a new page */ ! (block->status & BLOCK_IN_SWITCH) )
{
/* this is a primary request for a new page */
block->status|=BLOCK_IN_SWITCH; block->status|=BLOCK_IN_SWITCH;
KEYCACHE_DBUG_PRINT("find_key_block", KEYCACHE_DBUG_PRINT("find_key_block",
("got block %u for new page",BLOCK_NUMBER(block))); ("got block %u for new page",BLOCK_NUMBER(block)));
if (block->status & BLOCK_CHANGED) if (block->status & BLOCK_CHANGED)
{ /* The block contains a dirty page - push it out of the cache */ {
/* The block contains a dirty page - push it out of the cache */
KEYCACHE_DBUG_PRINT("find_key_block",("block is dirty")); KEYCACHE_DBUG_PRINT("find_key_block",("block is dirty"));
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
/* /*
The call is thread safe because only the current The call is thread safe because only the current
thread might change the block->hash_link value thread might change the block->hash_link value
*/ */
error=my_pwrite(block->hash_link->file,block->buffer, error=my_pwrite(block->hash_link->file,block->buffer,
block->length,block->hash_link->diskpos, block->length,block->hash_link->diskpos,
MYF(MY_NABP | MY_WAIT_IF_FULL)); MYF(MY_NABP | MY_WAIT_IF_FULL));
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
_my_cache_write++; my_cache_write++;
} }
block->status|=BLOCK_REASSIGNED; block->status|=BLOCK_REASSIGNED;
if (block->hash_link) if (block->hash_link)
{ {
/* /*
Wait until all pending read requests Wait until all pending read requests
for this page are executed for this page are executed
(we could have avoided this waiting, if we had read (we could have avoided this waiting, if we had read
a page in the cache in a sweep, without yielding control) a page in the cache in a sweep, without yielding control)
*/ */
wait_for_readers(block); wait_for_readers(block);
/* Remove the hash link for this page from the hash table */ /* Remove the hash link for this page from the hash table */
unlink_hash(block->hash_link); unlink_hash(block->hash_link);
/* All pending requests for this page must be resubmitted */ /* All pending requests for this page must be resubmitted */
...@@ -1006,7 +1037,7 @@ restart: ...@@ -1006,7 +1037,7 @@ restart:
block->offset=key_cache_block_size; block->offset=key_cache_block_size;
block->hash_link=hash_link; block->hash_link=hash_link;
page_status=PAGE_TO_BE_READ; page_status=PAGE_TO_BE_READ;
KEYCACHE_DBUG_ASSERT(block->hash_link->block == block); KEYCACHE_DBUG_ASSERT(block->hash_link->block == block);
KEYCACHE_DBUG_ASSERT(hash_link->block->hash_link == hash_link); KEYCACHE_DBUG_ASSERT(hash_link->block->hash_link == hash_link);
} }
...@@ -1018,8 +1049,8 @@ restart: ...@@ -1018,8 +1049,8 @@ restart:
PAGE_READ : PAGE_WAIT_TO_BE_READ; PAGE_READ : PAGE_WAIT_TO_BE_READ;
} }
} }
_my_cache_read++; my_cache_read++;
} }
else else
{ {
...@@ -1029,10 +1060,10 @@ restart: ...@@ -1029,10 +1060,10 @@ restart:
PAGE_READ : PAGE_WAIT_TO_BE_READ; PAGE_READ : PAGE_WAIT_TO_BE_READ;
} }
} }
KEYCACHE_DBUG_ASSERT(page_status != -1); KEYCACHE_DBUG_ASSERT(page_status != -1);
*page_st=page_status; *page_st=page_status;
#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG)
DBUG_EXECUTE("check_keycache2",test_key_cache("end of find_key_block",0);); DBUG_EXECUTE("check_keycache2",test_key_cache("end of find_key_block",0););
#endif #endif
...@@ -1042,55 +1073,58 @@ restart: ...@@ -1042,55 +1073,58 @@ restart:
/* /*
Read into a key cache block buffer from disk; Read into a key cache block buffer from disk;
do not to report error when the size of successfully read do not to report error when the size of successfully read
portion is less than read_length, but not less than min_length portion is less than read_length, but not less than min_length
*/ */
static void read_block(BLOCK_LINK *block, uint read_length, static void read_block(BLOCK_LINK *block, uint read_length,
uint min_length, my_bool primary) uint min_length, my_bool primary)
{ {
uint got_length; uint got_length;
/* On entry THR_LOCK_keycache is locked */ /* On entry THR_LOCK_keycache is locked */
KEYCACHE_THREAD_TRACE("read_block"); KEYCACHE_THREAD_TRACE("read_block");
if (primary) if (primary)
{ /* {
This code is executed only by threads /*
that submitted primary requests This code is executed only by threads
that submitted primary requests
*/ */
KEYCACHE_DBUG_PRINT("read_block", KEYCACHE_DBUG_PRINT("read_block",
("page to be read by primary request")); ("page to be read by primary request"));
/* Page is not in buffer yet, is to be read from disk */ /* Page is not in buffer yet, is to be read from disk */
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
got_length=my_pread(block->hash_link->file,block->buffer, got_length=my_pread(block->hash_link->file,block->buffer,
read_length,block->hash_link->diskpos,MYF(0)); read_length,block->hash_link->diskpos,MYF(0));
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
if (got_length < min_length) if (got_length < min_length)
block->status|=BLOCK_ERROR; block->status|=BLOCK_ERROR;
else else
{ {
block->status=BLOCK_READ; block->status=BLOCK_READ;
block->length=got_length; block->length=got_length;
} }
KEYCACHE_DBUG_PRINT("read_block", KEYCACHE_DBUG_PRINT("read_block",
("primary request: new page in cache")); ("primary request: new page in cache"));
/* Signal that all pending requests for this page now can be processed */ /* Signal that all pending requests for this page now can be processed */
if (block->wqueue[COND_FOR_REQUESTED].last_thread) if (block->wqueue[COND_FOR_REQUESTED].last_thread)
release_queue(&block->wqueue[COND_FOR_REQUESTED]); release_queue(&block->wqueue[COND_FOR_REQUESTED]);
} }
else else
{ /* {
This code is executed only by threads /*
that submitted secondary requests This code is executed only by threads
that submitted secondary requests
*/ */
KEYCACHE_DBUG_PRINT("read_block", KEYCACHE_DBUG_PRINT("read_block",
("secondary request waiting for new page to be read")); ("secondary request waiting for new page to be read"));
{ {
struct st_my_thread_var *thread=my_thread_var; struct st_my_thread_var *thread=my_thread_var;
/* Put the request into a queue and wait until it can be processed */ /* Put the request into a queue and wait until it can be processed */
add_to_queue(&block->wqueue[COND_FOR_REQUESTED],thread); add_to_queue(&block->wqueue[COND_FOR_REQUESTED],thread);
do do
{ {
...@@ -1098,44 +1132,45 @@ static void read_block(BLOCK_LINK *block, uint read_length, ...@@ -1098,44 +1132,45 @@ static void read_block(BLOCK_LINK *block, uint read_length,
} }
while (thread->next); while (thread->next);
} }
KEYCACHE_DBUG_PRINT("read_block", KEYCACHE_DBUG_PRINT("read_block",
("secondary request: new page in cache")); ("secondary request: new page in cache"));
} }
} }
/* /*
Read a block of data from a cached file into a buffer; Read a block of data from a cached file into a buffer;
if return_buffer is set then the cache buffer is returned if if return_buffer is set then the cache buffer is returned if
it can be used; it can be used;
filepos must be a multiple of 'block_length', but it doesn't filepos must be a multiple of 'block_length', but it doesn't
have to be a multiple of key_cache_block_size; have to be a multiple of key_cache_block_size;
returns adress from where data is read returns adress from where data is read
*/ */
byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length,
uint block_length __attribute__((unused)), uint block_length __attribute__((unused)),
int return_buffer __attribute__((unused))) int return_buffer __attribute__((unused)))
{ {
int error=0; int error=0;
DBUG_ENTER("key_cache_read"); DBUG_ENTER("key_cache_read");
DBUG_PRINT("enter", ("file %u, filepos %lu, length %u", DBUG_PRINT("enter", ("file %u, filepos %lu, length %u",
(uint) file,(ulong) filepos,length)); (uint) file,(ulong) filepos,length));
if (_my_disk_blocks > 0) if (my_disk_blocks > 0)
{ /* Key cache is used */ {
/* Key cache is used */
reg1 BLOCK_LINK *block; reg1 BLOCK_LINK *block;
uint offset= (uint) (filepos & (key_cache_block_size-1)); uint offset= (uint) (filepos & (key_cache_block_size-1));
byte *start=buff; byte *start=buff;
uint read_length; uint read_length;
uint status; uint status;
int page_st; int page_st;
#ifndef THREAD #ifndef THREAD
if (block_length > key_cache_block_size || offset) if (block_length > key_cache_block_size || offset)
return_buffer=0; return_buffer=0;
#endif #endif
/* Read data in key_cache_block_size increments */ /* Read data in key_cache_block_size increments */
filepos-= offset; filepos-= offset;
do do
...@@ -1144,7 +1179,7 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1144,7 +1179,7 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length,
key_cache_block_size : length; key_cache_block_size : length;
KEYCACHE_DBUG_ASSERT(read_length > 0); KEYCACHE_DBUG_ASSERT(read_length > 0);
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
_my_cache_r_requests++; my_cache_r_requests++;
block=find_key_block(file,filepos,0,&page_st); block=find_key_block(file,filepos,0,&page_st);
if (page_st != PAGE_READ) if (page_st != PAGE_READ)
{ {
...@@ -1154,26 +1189,26 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1154,26 +1189,26 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length,
} }
else if (! (block->status & BLOCK_ERROR) && else if (! (block->status & BLOCK_ERROR) &&
block->length < read_length + offset) block->length < read_length + offset)
{ {
/* /*
Impossible if nothing goes wrong: Impossible if nothing goes wrong:
this could only happen if we are using a file with this could only happen if we are using a file with
small key blocks and are trying to read outside the file small key blocks and are trying to read outside the file
*/ */
my_errno=-1; my_errno=-1;
block->status|=BLOCK_ERROR; block->status|=BLOCK_ERROR;
} }
if (! ((status=block->status) & BLOCK_ERROR)) if (! ((status=block->status) & BLOCK_ERROR))
{ {
#ifndef THREAD #ifndef THREAD
if (! return_buffer) if (! return_buffer)
#endif #endif
{ {
#if !defined(SERIALIZED_READ_FROM_CACHE) #if !defined(SERIALIZED_READ_FROM_CACHE)
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
#endif #endif
/* Copy data from the cache buffer */ /* Copy data from the cache buffer */
if (!(read_length & 511)) if (!(read_length & 511))
bmove512(buff,block->buffer+offset,read_length); bmove512(buff,block->buffer+offset,read_length);
...@@ -1185,35 +1220,35 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1185,35 +1220,35 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length,
#endif #endif
} }
} }
remove_reader(block); remove_reader(block);
/* /*
Link the block into the LRU chain Link the block into the LRU chain
if it's the last submitted request for the block if it's the last submitted request for the block
*/ */
unreg_request(block,1); unreg_request(block,1);
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
if (status & BLOCK_ERROR) if (status & BLOCK_ERROR)
DBUG_RETURN((byte *) 0); DBUG_RETURN((byte *) 0);
#ifndef THREAD #ifndef THREAD
if (return_buffer) if (return_buffer)
return (block->buffer); return (block->buffer);
#endif #endif
buff+=read_length; buff+=read_length;
filepos+=read_length; filepos+=read_length;
offset=0; offset=0;
} while ((length-= read_length)); } while ((length-= read_length));
DBUG_RETURN(start); DBUG_RETURN(start);
} }
/* Key cache is not used */ /* Key cache is not used */
statistic_increment(_my_cache_r_requests,&THR_LOCK_keycache); statistic_increment(my_cache_r_requests,&THR_LOCK_keycache);
statistic_increment(_my_cache_read,&THR_LOCK_keycache); statistic_increment(my_cache_read,&THR_LOCK_keycache);
if (my_pread(file,(byte*) buff,length,filepos,MYF(MY_NABP))) if (my_pread(file,(byte*) buff,length,filepos,MYF(MY_NABP)))
error=1; error=1;
DBUG_RETURN(error? (byte*) 0 : buff); DBUG_RETURN(error? (byte*) 0 : buff);
...@@ -1221,12 +1256,13 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1221,12 +1256,13 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length,
/* /*
Write a buffer into disk; Write a buffer into disk;
filepos must be a multiple of 'block_length', but it doesn't filepos must be a multiple of 'block_length', but it doesn't
have to be a multiple of key cache block size; have to be a multiple of key cache block size;
if !dont_write then all dirty pages involved in writing should if !dont_write then all dirty pages involved in writing should
have been flushed from key cache before the function starts have been flushed from key cache before the function starts
*/ */
int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
uint block_length __attribute__((unused)), uint block_length __attribute__((unused)),
int dont_write) int dont_write)
...@@ -1239,31 +1275,33 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1239,31 +1275,33 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
(uint) file,(ulong) filepos,length,block_length)); (uint) file,(ulong) filepos,length,block_length));
if (!dont_write) if (!dont_write)
{ /* Force writing from buff into disk */ {
statistic_increment(_my_cache_write, &THR_LOCK_keycache); /* Force writing from buff into disk */
statistic_increment(my_cache_write, &THR_LOCK_keycache);
if (my_pwrite(file,buff,length,filepos,MYF(MY_NABP | MY_WAIT_IF_FULL))) if (my_pwrite(file,buff,length,filepos,MYF(MY_NABP | MY_WAIT_IF_FULL)))
DBUG_RETURN(1); DBUG_RETURN(1);
} }
#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG)
DBUG_EXECUTE("check_keycache",test_key_cache("start of key_cache_write",1);); DBUG_EXECUTE("check_keycache",test_key_cache("start of key_cache_write",1););
#endif #endif
if (_my_disk_blocks > 0) if (my_disk_blocks > 0)
{ /* Key cache is used */ {
/* Key cache is used */
uint read_length; uint read_length;
uint offset= (uint) (filepos & (key_cache_block_size-1)); uint offset= (uint) (filepos & (key_cache_block_size-1));
int page_st; int page_st;
/* Write data in key_cache_block_size increments */ /* Write data in key_cache_block_size increments */
filepos-= offset; filepos-= offset;
do do
{ {
read_length= length > key_cache_block_size ? read_length= length > key_cache_block_size ?
key_cache_block_size : length; key_cache_block_size : length;
KEYCACHE_DBUG_ASSERT(read_length > 0); KEYCACHE_DBUG_ASSERT(read_length > 0);
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
_my_cache_w_requests++; my_cache_w_requests++;
block=find_key_block(file, filepos, 1, &page_st); block=find_key_block(file, filepos, 1, &page_st);
if (page_st != PAGE_READ && if (page_st != PAGE_READ &&
(offset || read_length < key_cache_block_size)) (offset || read_length < key_cache_block_size))
...@@ -1271,19 +1309,20 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1271,19 +1309,20 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
offset + read_length >= key_cache_block_size? offset + read_length >= key_cache_block_size?
offset : key_cache_block_size, offset : key_cache_block_size,
offset,(my_bool)(page_st == PAGE_TO_BE_READ)); offset,(my_bool)(page_st == PAGE_TO_BE_READ));
if (!dont_write) if (!dont_write)
{ /* buff has been written to disk at start */ {
if ((block->status & BLOCK_CHANGED) && /* buff has been written to disk at start */
if ((block->status & BLOCK_CHANGED) &&
(!offset && read_length >= key_cache_block_size)) (!offset && read_length >= key_cache_block_size))
link_to_file_list(block, block->hash_link->file, 1); link_to_file_list(block, block->hash_link->file, 1);
} }
else if (! (block->status & BLOCK_CHANGED)) else if (! (block->status & BLOCK_CHANGED))
link_to_changed_list(block); link_to_changed_list(block);
set_if_smaller(block->offset,offset) set_if_smaller(block->offset,offset)
set_if_bigger(block->length,read_length+offset); set_if_bigger(block->length,read_length+offset);
if (! (block->status & BLOCK_ERROR)) if (! (block->status & BLOCK_ERROR))
{ {
if (!(read_length & 511)) if (!(read_length & 511))
...@@ -1291,26 +1330,26 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1291,26 +1330,26 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
else else
memcpy(block->buffer+offset,buff,(size_t) read_length); memcpy(block->buffer+offset,buff,(size_t) read_length);
} }
block->status|=BLOCK_READ; block->status|=BLOCK_READ;
/* Unregister the request */ /* Unregister the request */
block->hash_link->requests--; block->hash_link->requests--;
unreg_request(block,1); unreg_request(block,1);
if (block->status & BLOCK_ERROR) if (block->status & BLOCK_ERROR)
{ {
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
error=1; error=1;
break; break;
} }
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
buff+=read_length; buff+=read_length;
filepos+=read_length; filepos+=read_length;
offset=0; offset=0;
} while ((length-= read_length)); } while ((length-= read_length));
} }
else else
...@@ -1318,8 +1357,8 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1318,8 +1357,8 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
/* Key cache is not used */ /* Key cache is not used */
if (dont_write) if (dont_write)
{ {
statistic_increment(_my_cache_w_requests, &THR_LOCK_keycache); statistic_increment(my_cache_w_requests, &THR_LOCK_keycache);
statistic_increment(_my_cache_write, &THR_LOCK_keycache); statistic_increment(my_cache_write, &THR_LOCK_keycache);
if (my_pwrite(file,(byte*) buff,length,filepos,MYF(MY_NABP | MY_WAIT_IF_FULL))) if (my_pwrite(file,(byte*) buff,length,filepos,MYF(MY_NABP | MY_WAIT_IF_FULL)))
error=1; error=1;
} }
...@@ -1332,32 +1371,33 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, ...@@ -1332,32 +1371,33 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length,
} }
/* /*
Free block: remove reference to it from hash table, Free block: remove reference to it from hash table,
remove it from the chain file of dirty/clean blocks remove it from the chain file of dirty/clean blocks
and add it at the beginning of the LRU chain and add it at the beginning of the LRU chain
*/ */
static void free_block(BLOCK_LINK *block) static void free_block(BLOCK_LINK *block)
{ {
KEYCACHE_THREAD_TRACE("free block"); KEYCACHE_THREAD_TRACE("free block");
KEYCACHE_DBUG_PRINT("free_block", KEYCACHE_DBUG_PRINT("free_block",
("block %u to be freed",BLOCK_NUMBER(block))); ("block %u to be freed",BLOCK_NUMBER(block)));
if (block->hash_link) if (block->hash_link)
{ {
block->status|=BLOCK_REASSIGNED; block->status|=BLOCK_REASSIGNED;
wait_for_readers(block); wait_for_readers(block);
unlink_hash(block->hash_link); unlink_hash(block->hash_link);
} }
unlink_changed(block); unlink_changed(block);
block->status=0; block->status=0;
block->length=0; block->length=0;
block->offset=key_cache_block_size; block->offset=key_cache_block_size;
KEYCACHE_THREAD_TRACE("free block"); KEYCACHE_THREAD_TRACE("free block");
KEYCACHE_DBUG_PRINT("free_block", KEYCACHE_DBUG_PRINT("free_block",
("block is freed")); ("block is freed"));
unreg_request(block,0); unreg_request(block,0);
block->hash_link=NULL; block->hash_link=NULL;
} }
...@@ -1368,10 +1408,11 @@ static int cmp_sec_link(BLOCK_LINK **a, BLOCK_LINK **b) ...@@ -1368,10 +1408,11 @@ static int cmp_sec_link(BLOCK_LINK **a, BLOCK_LINK **b)
} }
/* /*
Flush a portion of changed blocks to disk, Flush a portion of changed blocks to disk,
free used blocks if requested free used blocks if requested
*/ */
static int flush_cached_blocks(File file, BLOCK_LINK **cache, static int flush_cached_blocks(File file, BLOCK_LINK **cache,
BLOCK_LINK **end, BLOCK_LINK **end,
enum flush_type type) enum flush_type type)
...@@ -1379,27 +1420,27 @@ static int flush_cached_blocks(File file, BLOCK_LINK **cache, ...@@ -1379,27 +1420,27 @@ static int flush_cached_blocks(File file, BLOCK_LINK **cache,
int error; int error;
int last_errno=0; int last_errno=0;
uint count=end-cache; uint count=end-cache;
/* Don't lock the cache during the flush */ /* Don't lock the cache during the flush */
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
/* /*
As all blocks referred in 'cache' are marked by BLOCK_IN_FLUSH As all blocks referred in 'cache' are marked by BLOCK_IN_FLUSH
we are guarunteed no thread will change them we are guarunteed no thread will change them
*/ */
qsort((byte*) cache,count,sizeof(*cache),(qsort_cmp) cmp_sec_link); qsort((byte*) cache,count,sizeof(*cache),(qsort_cmp) cmp_sec_link);
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
for ( ; cache != end ; cache++) for ( ; cache != end ; cache++)
{ {
BLOCK_LINK *block= *cache; BLOCK_LINK *block= *cache;
KEYCACHE_DBUG_PRINT("flush_cached_blocks", KEYCACHE_DBUG_PRINT("flush_cached_blocks",
("block %u to be flushed", BLOCK_NUMBER(block))); ("block %u to be flushed", BLOCK_NUMBER(block)));
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
error=my_pwrite(file,block->buffer+block->offset,block->length, error=my_pwrite(file,block->buffer+block->offset,block->length,
block->hash_link->diskpos,MYF(MY_NABP | MY_WAIT_IF_FULL)); block->hash_link->diskpos,MYF(MY_NABP | MY_WAIT_IF_FULL));
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
_my_cache_write++; my_cache_write++;
if (error) if (error)
{ {
block->status|= BLOCK_ERROR; block->status|= BLOCK_ERROR;
...@@ -1409,42 +1450,44 @@ static int flush_cached_blocks(File file, BLOCK_LINK **cache, ...@@ -1409,42 +1450,44 @@ static int flush_cached_blocks(File file, BLOCK_LINK **cache,
/* type will never be FLUSH_IGNORE_CHANGED here */ /* type will never be FLUSH_IGNORE_CHANGED here */
if (! (type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE)) if (! (type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE))
{ {
_my_blocks_changed--; my_blocks_changed--;
free_block(block); free_block(block);
} }
else else
{ {
block->status&=~BLOCK_IN_FLUSH; block->status&=~BLOCK_IN_FLUSH;
link_to_file_list(block,file,1); link_to_file_list(block,file,1);
unreg_request(block,1); unreg_request(block,1);
} }
} }
return last_errno; return last_errno;
} }
/* /*
Flush all blocks for a file to disk Flush all blocks for a file to disk
*/ */
int flush_key_blocks(File file, enum flush_type type) int flush_key_blocks(File file, enum flush_type type)
{ {
int last_errno=0; int last_errno=0;
BLOCK_LINK *cache_buff[FLUSH_CACHE],**cache; BLOCK_LINK *cache_buff[FLUSH_CACHE],**cache;
DBUG_ENTER("flush_key_blocks"); DBUG_ENTER("flush_key_blocks");
DBUG_PRINT("enter",("file: %d blocks_used: %d blocks_changed: %d", DBUG_PRINT("enter",("file: %d blocks_used: %d blocks_changed: %d",
file,_my_blocks_used,_my_blocks_changed)); file, my_blocks_used, my_blocks_changed));
#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG)
DBUG_EXECUTE("check_keycache",test_key_cache("start of flush_key_blocks",0);); DBUG_EXECUTE("check_keycache",test_key_cache("start of flush_key_blocks",0););
#endif #endif
keycache_pthread_mutex_lock(&THR_LOCK_keycache); keycache_pthread_mutex_lock(&THR_LOCK_keycache);
cache=cache_buff; cache=cache_buff;
if (_my_disk_blocks > 0 && if (my_disk_blocks > 0 &&
(!my_disable_flush_key_blocks || type != FLUSH_KEEP)) (!my_disable_flush_key_blocks || type != FLUSH_KEEP))
{ /* Key cache exists and flush is not disabled */ {
/* Key cache exists and flush is not disabled */
int error=0; int error=0;
uint count=0; uint count=0;
BLOCK_LINK **pos,**end; BLOCK_LINK **pos,**end;
...@@ -1453,10 +1496,10 @@ int flush_key_blocks(File file, enum flush_type type) ...@@ -1453,10 +1496,10 @@ int flush_key_blocks(File file, enum flush_type type)
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
uint cnt=0; uint cnt=0;
#endif #endif
if (type != FLUSH_IGNORE_CHANGED) if (type != FLUSH_IGNORE_CHANGED)
{ {
/* /*
Count how many key blocks we have to cache to be able Count how many key blocks we have to cache to be able
to flush all dirty pages with minimum seek moves to flush all dirty pages with minimum seek moves
*/ */
...@@ -1467,18 +1510,18 @@ int flush_key_blocks(File file, enum flush_type type) ...@@ -1467,18 +1510,18 @@ int flush_key_blocks(File file, enum flush_type type)
if (block->hash_link->file == file) if (block->hash_link->file == file)
{ {
count++; count++;
KEYCACHE_DBUG_ASSERT(count<=_my_blocks_used); KEYCACHE_DBUG_ASSERT(count<= my_blocks_used);
} }
} }
/* Allocate a new buffer only if its bigger than the one we have */ /* Allocate a new buffer only if its bigger than the one we have */
if (count > FLUSH_CACHE && if (count > FLUSH_CACHE &&
!(cache=(BLOCK_LINK**) my_malloc(sizeof(BLOCK_LINK*)*count,MYF(0)))) !(cache=(BLOCK_LINK**) my_malloc(sizeof(BLOCK_LINK*)*count,MYF(0))))
{ {
cache=cache_buff; cache=cache_buff;
count=FLUSH_CACHE; count=FLUSH_CACHE;
} }
} }
/* Retrieve the blocks and write them to a buffer to be flushed */ /* Retrieve the blocks and write them to a buffer to be flushed */
restart: restart:
end=(pos=cache)+count; end=(pos=cache)+count;
...@@ -1488,37 +1531,40 @@ restart: ...@@ -1488,37 +1531,40 @@ restart:
{ {
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt++; cnt++;
KEYCACHE_DBUG_ASSERT(cnt <= _my_blocks_used); KEYCACHE_DBUG_ASSERT(cnt <= my_blocks_used);
#endif #endif
next=block->next_changed; next=block->next_changed;
if (block->hash_link->file == file) if (block->hash_link->file == file)
{ {
/* /*
Mark the block with BLOCK_IN_FLUSH in order not to let Mark the block with BLOCK_IN_FLUSH in order not to let
other threads to use it for new pages and interfere with other threads to use it for new pages and interfere with
our sequence ot flushing dirty file pages our sequence ot flushing dirty file pages
*/ */
block->status|= BLOCK_IN_FLUSH; block->status|= BLOCK_IN_FLUSH;
if (! (block->status & BLOCK_IN_SWITCH)) if (! (block->status & BLOCK_IN_SWITCH))
{ /* {
We care only for the blocks for which flushing was not /*
initiated by other threads as a result of page swapping We care only for the blocks for which flushing was not
*/ initiated by other threads as a result of page swapping
*/
reg_requests(block,1); reg_requests(block,1);
if (type != FLUSH_IGNORE_CHANGED) if (type != FLUSH_IGNORE_CHANGED)
{ /* It's not a temporary file */ {
/* It's not a temporary file */
if (pos == end) if (pos == end)
{ /* {
This happens only if there is not enough /*
memory for the big block This happens only if there is not enough
memory for the big block
*/ */
if ((error=flush_cached_blocks(file,cache,end,type))) if ((error=flush_cached_blocks(file,cache,end,type)))
last_errno=error; last_errno=error;
/* /*
Restart the scan as some other thread might have changed Restart the scan as some other thread might have changed
the changed blocks chain: the blocks that were in switch the changed blocks chain: the blocks that were in switch
state before the flush started have to be excluded state before the flush started have to be excluded
*/ */
goto restart; goto restart;
} }
...@@ -1527,12 +1573,13 @@ restart: ...@@ -1527,12 +1573,13 @@ restart:
else else
{ {
/* It's a temporary file */ /* It's a temporary file */
_my_blocks_changed--; my_blocks_changed--;
free_block(block); free_block(block);
} }
} }
else else
{ /* Link the block into a list of blocks 'in switch' */ {
/* Link the block into a list of blocks 'in switch' */
unlink_changed(block); unlink_changed(block);
link_changed(block,&first_in_switch); link_changed(block,&first_in_switch);
} }
...@@ -1561,7 +1608,7 @@ restart: ...@@ -1561,7 +1608,7 @@ restart:
} }
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt++; cnt++;
KEYCACHE_DBUG_ASSERT(cnt <= _my_blocks_used); KEYCACHE_DBUG_ASSERT(cnt <= my_blocks_used);
#endif #endif
} }
/* The following happens very seldom */ /* The following happens very seldom */
...@@ -1576,7 +1623,7 @@ restart: ...@@ -1576,7 +1623,7 @@ restart:
{ {
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt++; cnt++;
KEYCACHE_DBUG_ASSERT(cnt <= _my_blocks_used); KEYCACHE_DBUG_ASSERT(cnt <= my_blocks_used);
#endif #endif
next=block->next_changed; next=block->next_changed;
if (block->hash_link->file == file && if (block->hash_link->file == file &&
...@@ -1589,9 +1636,9 @@ restart: ...@@ -1589,9 +1636,9 @@ restart:
} }
} }
} }
keycache_pthread_mutex_unlock(&THR_LOCK_keycache); keycache_pthread_mutex_unlock(&THR_LOCK_keycache);
#ifndef DBUG_OFF #ifndef DBUG_OFF
DBUG_EXECUTE("check_keycache", DBUG_EXECUTE("check_keycache",
test_key_cache("end of flush_key_blocks",0);); test_key_cache("end of flush_key_blocks",0););
...@@ -1604,30 +1651,31 @@ restart: ...@@ -1604,30 +1651,31 @@ restart:
} }
/* /*
Flush all blocks in the key cache to disk Flush all blocks in the key cache to disk
*/ */
static int flush_all_key_blocks() static int flush_all_key_blocks()
{ {
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
uint cnt=0; uint cnt=0;
#endif #endif
while (_my_blocks_changed > 0) while (my_blocks_changed > 0)
{ {
BLOCK_LINK *block; BLOCK_LINK *block;
for (block=_my_used_last->next_used ; ; block=block->next_used) for (block= my_used_last->next_used ; ; block=block->next_used)
{ {
if (block->hash_link) if (block->hash_link)
{ {
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
cnt++; cnt++;
KEYCACHE_DBUG_ASSERT(cnt <= _my_blocks_used); KEYCACHE_DBUG_ASSERT(cnt <= my_blocks_used);
#endif #endif
if (flush_key_blocks(block->hash_link->file, FLUSH_RELEASE)) if (flush_key_blocks(block->hash_link->file, FLUSH_RELEASE))
return 1; return 1;
break; break;
} }
if (block == _my_used_last) if (block == my_used_last)
break; break;
} }
} }
...@@ -1637,13 +1685,13 @@ static int flush_all_key_blocks() ...@@ -1637,13 +1685,13 @@ static int flush_all_key_blocks()
#ifndef DBUG_OFF #ifndef DBUG_OFF
/* /*
Test if disk-cache is ok Test if disk-cache is ok
*/ */
static void test_key_cache(const char *where __attribute__((unused)), static void test_key_cache(const char *where __attribute__((unused)),
my_bool lock __attribute__((unused))) my_bool lock __attribute__((unused)))
{ {
/* TODO */ /* TODO */
} }
#endif #endif
#if defined(KEYCACHE_TIMEOUT) #if defined(KEYCACHE_TIMEOUT)
...@@ -1680,7 +1728,7 @@ static void keycache_dump() ...@@ -1680,7 +1728,7 @@ static void keycache_dump()
break; break;
} }
while (thread != last); while (thread != last);
i=0; i=0;
thread=last=waiting_for_block.last_thread; thread=last=waiting_for_block.last_thread;
fprintf(keycache_dump_file, "queue of threads waiting for block\n"); fprintf(keycache_dump_file, "queue of threads waiting for block\n");
...@@ -1698,11 +1746,11 @@ static void keycache_dump() ...@@ -1698,11 +1746,11 @@ static void keycache_dump()
} }
while (thread != last); while (thread != last);
for (i=0 ; i< _my_blocks_used ; i++) for (i=0 ; i< my_blocks_used ; i++)
{ {
int j; int j;
block=&_my_block_root[i]; block= &my_block_root[i];
hash_link=block->hash_link; hash_link= block->hash_link;
fprintf(keycache_dump_file, fprintf(keycache_dump_file,
"block:%u hash_link:%d status:%x #requests=%u waiting_for_readers:%d\n", "block:%u hash_link:%d status:%x #requests=%u waiting_for_readers:%d\n",
i, (int) (hash_link ? HASH_LINK_NUMBER(hash_link) : -1), i, (int) (hash_link ? HASH_LINK_NUMBER(hash_link) : -1),
...@@ -1713,6 +1761,7 @@ static void keycache_dump() ...@@ -1713,6 +1761,7 @@ static void keycache_dump()
thread=last=wqueue->last_thread; thread=last=wqueue->last_thread;
fprintf(keycache_dump_file, "queue #%d\n", j); fprintf(keycache_dump_file, "queue #%d\n", j);
if (thread) if (thread)
{
do do
{ {
thread=thread->next; thread=thread->next;
...@@ -1722,20 +1771,23 @@ static void keycache_dump() ...@@ -1722,20 +1771,23 @@ static void keycache_dump()
break; break;
} }
while (thread != last); while (thread != last);
}
} }
} }
fprintf(keycache_dump_file, "LRU chain:"); fprintf(keycache_dump_file, "LRU chain:");
block=_my_used_last; block= my_used_last;
if (block) if (block)
{
do do
{ {
block=block->next_used; block=block->next_used;
fprintf(keycache_dump_file, fprintf(keycache_dump_file,
"block:%u, ", BLOCK_NUMBER(block)); "block:%u, ", BLOCK_NUMBER(block));
} }
while (block != _my_used_last); while (block != my_used_last);
}
fprintf(keycache_dump_file, "\n"); fprintf(keycache_dump_file, "\n");
fclose(keycache_dump_file); fclose(keycache_dump_file);
} }
...@@ -1744,7 +1796,7 @@ static void keycache_dump() ...@@ -1744,7 +1796,7 @@ static void keycache_dump()
#if defined(KEYCACHE_TIMEOUT) && !defined(__WIN__) #if defined(KEYCACHE_TIMEOUT) && !defined(__WIN__)
static int keycache_pthread_cond_wait(pthread_cond_t *cond, static int keycache_pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex) pthread_mutex_t *mutex)
{ {
int rc; int rc;
...@@ -1754,8 +1806,8 @@ static int keycache_pthread_cond_wait(pthread_cond_t *cond, ...@@ -1754,8 +1806,8 @@ static int keycache_pthread_cond_wait(pthread_cond_t *cond,
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
int cnt=0; int cnt=0;
#endif #endif
/* Get current time */ /* Get current time */
gettimeofday(&now, &tz); gettimeofday(&now, &tz);
/* Prepare timeout value */ /* Prepare timeout value */
timeout.tv_sec = now.tv_sec + KEYCACHE_TIMEOUT; timeout.tv_sec = now.tv_sec + KEYCACHE_TIMEOUT;
...@@ -1773,16 +1825,16 @@ static int keycache_pthread_cond_wait(pthread_cond_t *cond, ...@@ -1773,16 +1825,16 @@ static int keycache_pthread_cond_wait(pthread_cond_t *cond,
KEYCACHE_THREAD_TRACE_BEGIN("finished waiting"); KEYCACHE_THREAD_TRACE_BEGIN("finished waiting");
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
if (rc == ETIMEDOUT) if (rc == ETIMEDOUT)
{ {
fprintf(keycache_debug_log,"aborted by keycache timeout\n"); fprintf(keycache_debug_log,"aborted by keycache timeout\n");
fclose(keycache_debug_log); fclose(keycache_debug_log);
abort(); abort();
} }
#endif #endif
if (rc == ETIMEDOUT) if (rc == ETIMEDOUT)
keycache_dump(); keycache_dump();
#if defined(KEYCACHE_DEBUG) #if defined(KEYCACHE_DEBUG)
KEYCACHE_DBUG_ASSERT(rc != ETIMEDOUT); KEYCACHE_DBUG_ASSERT(rc != ETIMEDOUT);
#else #else
...@@ -1867,4 +1919,3 @@ void keycache_debug_log_close(void) ...@@ -1867,4 +1919,3 @@ void keycache_debug_log_close(void)
#endif /* defined(KEYCACHE_DEBUG_LOG) */ #endif /* defined(KEYCACHE_DEBUG_LOG) */
#endif /* defined(KEYCACHE_DEBUG) */ #endif /* defined(KEYCACHE_DEBUG) */
...@@ -235,8 +235,6 @@ String *Item_func_concat::val_str(String *str) ...@@ -235,8 +235,6 @@ String *Item_func_concat::val_str(String *str)
use_as_buff= &tmp_value; use_as_buff= &tmp_value;
for (i=1 ; i < arg_count ; i++) for (i=1 ; i < arg_count ; i++)
{ {
if (args[i]->binary())
set_charset(&my_charset_bin);
if (res->length() == 0) if (res->length() == 0)
{ {
if (!(res=args[i]->val_str(str))) if (!(res=args[i]->val_str(str)))
...@@ -265,7 +263,6 @@ String *Item_func_concat::val_str(String *str) ...@@ -265,7 +263,6 @@ String *Item_func_concat::val_str(String *str)
str->append(*res2); str->append(*res2);
} }
res=str; res=str;
res->set_charset(charset());
} }
else if (res == &tmp_value) else if (res == &tmp_value)
{ {
...@@ -277,7 +274,6 @@ String *Item_func_concat::val_str(String *str) ...@@ -277,7 +274,6 @@ String *Item_func_concat::val_str(String *str)
if (tmp_value.replace(0,0,*res)) if (tmp_value.replace(0,0,*res))
goto null; goto null;
res= &tmp_value; res= &tmp_value;
res->set_charset(charset());
use_as_buff=str; // Put next arg here use_as_buff=str; // Put next arg here
} }
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() && else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
...@@ -296,7 +292,6 @@ String *Item_func_concat::val_str(String *str) ...@@ -296,7 +292,6 @@ String *Item_func_concat::val_str(String *str)
*res)) *res))
goto null; goto null;
res= &tmp_value; res= &tmp_value;
res->set_charset(charset());
use_as_buff=str; // Put next arg here use_as_buff=str; // Put next arg here
} }
else else
...@@ -306,11 +301,11 @@ String *Item_func_concat::val_str(String *str) ...@@ -306,11 +301,11 @@ String *Item_func_concat::val_str(String *str)
tmp_value.append(*res2)) tmp_value.append(*res2))
goto null; goto null;
res= &tmp_value; res= &tmp_value;
res->set_charset(charset());
use_as_buff=str; use_as_buff=str;
} }
} }
} }
res->set_charset(charset());
return res; return res;
null: null:
...@@ -602,6 +597,7 @@ String *Item_func_concat_ws::val_str(String *str) ...@@ -602,6 +597,7 @@ String *Item_func_concat_ws::val_str(String *str)
use_as_buff=str; use_as_buff=str;
} }
} }
res->set_charset(charset());
return res; return res;
null: null:
...@@ -626,9 +622,18 @@ void Item_func_concat_ws::split_sum_func(Item **ref_pointer_array, ...@@ -626,9 +622,18 @@ void Item_func_concat_ws::split_sum_func(Item **ref_pointer_array,
void Item_func_concat_ws::fix_length_and_dec() void Item_func_concat_ws::fix_length_and_dec()
{ {
set_charset(separator->charset(),separator->coercibility);
max_length=separator->max_length*(arg_count-1); max_length=separator->max_length*(arg_count-1);
for (uint i=0 ; i < arg_count ; i++) for (uint i=0 ; i < arg_count ; i++)
{
max_length+=args[i]->max_length; max_length+=args[i]->max_length;
if (set_charset(charset(), coercibility,
args[i]->charset(), args[i]->coercibility))
{
my_error(ER_WRONG_ARGUMENTS,MYF(0),func_name());
break;
}
}
if (max_length > MAX_BLOB_WIDTH) if (max_length > MAX_BLOB_WIDTH)
{ {
max_length=MAX_BLOB_WIDTH; max_length=MAX_BLOB_WIDTH;
......
...@@ -528,6 +528,7 @@ int mysqld_show_variables(THD *thd,const char *wild); ...@@ -528,6 +528,7 @@ int mysqld_show_variables(THD *thd,const char *wild);
int mysqld_show(THD *thd, const char *wild, show_var_st *variables, int mysqld_show(THD *thd, const char *wild, show_var_st *variables,
enum enum_var_type value_type); enum enum_var_type value_type);
int mysqld_show_charsets(THD *thd,const char *wild); int mysqld_show_charsets(THD *thd,const char *wild);
int mysqld_show_collations(THD *thd,const char *wild);
int mysqld_show_table_types(THD *thd); int mysqld_show_table_types(THD *thd);
int mysqld_show_privileges(THD *thd); int mysqld_show_privileges(THD *thd);
int mysqld_show_column_types(THD *thd); int mysqld_show_column_types(THD *thd);
......
...@@ -4415,13 +4415,13 @@ struct show_var_st status_vars[]= { ...@@ -4415,13 +4415,13 @@ struct show_var_st status_vars[]= {
{"Handler_rollback", (char*) &ha_rollback_count, SHOW_LONG}, {"Handler_rollback", (char*) &ha_rollback_count, SHOW_LONG},
{"Handler_update", (char*) &ha_update_count, SHOW_LONG}, {"Handler_update", (char*) &ha_update_count, SHOW_LONG},
{"Handler_write", (char*) &ha_write_count, SHOW_LONG}, {"Handler_write", (char*) &ha_write_count, SHOW_LONG},
{"Key_blocks_used", (char*) &_my_blocks_used, SHOW_LONG_CONST}, {"Key_blocks_used", (char*) &my_blocks_used, SHOW_LONG_CONST},
{"Key_read_requests", (char*) &_my_cache_r_requests, SHOW_LONG}, {"Key_read_requests", (char*) &my_cache_r_requests, SHOW_LONG},
{"Key_reads", (char*) &_my_cache_read, SHOW_LONG}, {"Key_reads", (char*) &my_cache_read, SHOW_LONG},
{"Key_write_requests", (char*) &_my_cache_w_requests, SHOW_LONG}, {"Key_write_requests", (char*) &my_cache_w_requests, SHOW_LONG},
{"Key_writes", (char*) &_my_cache_write, SHOW_LONG}, {"Key_writes", (char*) &my_cache_write, SHOW_LONG},
{"Max_used_connections", (char*) &max_used_connections, SHOW_LONG}, {"Max_used_connections", (char*) &max_used_connections, SHOW_LONG},
{"Not_flushed_key_blocks", (char*) &_my_blocks_changed, SHOW_LONG_CONST}, {"Not_flushed_key_blocks", (char*) &my_blocks_changed, SHOW_LONG_CONST},
{"Not_flushed_delayed_rows", (char*) &delayed_rows_in_use, SHOW_LONG_CONST}, {"Not_flushed_delayed_rows", (char*) &delayed_rows_in_use, SHOW_LONG_CONST},
{"Open_tables", (char*) 0, SHOW_OPENTABLES}, {"Open_tables", (char*) 0, SHOW_OPENTABLES},
{"Open_files", (char*) &my_file_opened, SHOW_LONG_CONST}, {"Open_files", (char*) &my_file_opened, SHOW_LONG_CONST},
......
...@@ -240,9 +240,18 @@ To make maintaining easier please: ...@@ -240,9 +240,18 @@ To make maintaining easier please:
<alias>BalticRim</alias> <alias>BalticRim</alias>
<alias>iso-8859-13</alias> <alias>iso-8859-13</alias>
<alias>l7</alias> <alias>l7</alias>
<collation name="estonia" id="20" order="Estonian" flag="primary"/> <collation name="latin7_estonian_ci_as" id="20">
<collation name="latvian" id="41" order="Latvian"/> <order>Estonian</order>
<collation name="latvian1" id="42" order="Latvian"/> <flag>primary</flag>
</collation>
<collation name="latin7_ci_as" id="41">
<order>Latvian</order>
<order>Lithuanian</order>
</collation>
<collation name="latin7_cs_as" id="42">
<order>Latvian</order>
<order>Lithuanian</order>
</collation>
<collation name="latin7_bin" id="79" order="Binary" flag="binary"/> <collation name="latin7_bin" id="79" order="Binary" flag="binary"/>
</charset> </charset>
......
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
</unicode> </unicode>
<collation name="estonia"> <collation name="latin7_estonian_ci_as">
<map> <map>
00 02 03 04 05 06 07 08 09 2E 2F 30 31 32 0A 0B 00 02 03 04 05 06 07 08 09 2E 2F 30 31 32 0A 0B
0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B
...@@ -115,7 +115,7 @@ ...@@ -115,7 +115,7 @@
</collation> </collation>
<collation name="latvian"> <collation name="latin7_ci_as">
<!-- Created for case-sensitive record search --> <!-- Created for case-sensitive record search -->
<!-- by Andis Grasis & Rihards Grasis e-mail:andis@cata.lv --> <!-- by Andis Grasis & Rihards Grasis e-mail:andis@cata.lv -->
<map> <map>
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
</collation> </collation>
<collation name="latvian1"> <collation name="latin7_cs_as">
<!-- Created for case-insensitive record search --> <!-- Created for case-insensitive record search -->
<!-- Created by Andis & Rihards --> <!-- Created by Andis & Rihards -->
<map> <map>
......
...@@ -52,7 +52,7 @@ enum enum_sql_command { ...@@ -52,7 +52,7 @@ enum enum_sql_command {
SQLCOM_SHOW_INNODB_STATUS, SQLCOM_SHOW_INNODB_STATUS,
SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT, SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
SQLCOM_SHOW_GRANTS, SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS, SQLCOM_SHOW_GRANTS, SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB,
SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES, SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
SQLCOM_GRANT, SQLCOM_GRANT,
......
...@@ -2566,6 +2566,9 @@ mysql_execute_command(THD *thd) ...@@ -2566,6 +2566,9 @@ mysql_execute_command(THD *thd)
case SQLCOM_SHOW_CHARSETS: case SQLCOM_SHOW_CHARSETS:
res= mysqld_show_charsets(thd,(lex->wild ? lex->wild->ptr() : NullS)); res= mysqld_show_charsets(thd,(lex->wild ? lex->wild->ptr() : NullS));
break; break;
case SQLCOM_SHOW_COLLATIONS:
res= mysqld_show_collations(thd,(lex->wild ? lex->wild->ptr() : NullS));
break;
case SQLCOM_SHOW_FIELDS: case SQLCOM_SHOW_FIELDS:
#ifdef DONT_ALLOW_SHOW_COMMANDS #ifdef DONT_ALLOW_SHOW_COMMANDS
send_error(thd,ER_NOT_ALLOWED_COMMAND); /* purecov: inspected */ send_error(thd,ER_NOT_ALLOWED_COMMAND); /* purecov: inspected */
......
...@@ -1414,7 +1414,22 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose) ...@@ -1414,7 +1414,22 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
Status functions Status functions
*****************************************************************************/ *****************************************************************************/
int mysqld_show_charsets(THD *thd, const char *wild) static bool write_collation(Protocol *protocol, CHARSET_INFO *cs)
{
char flags[4];
protocol->prepare_for_resend();
protocol->store(cs->csname, system_charset_info);
protocol->store(cs->name, system_charset_info);
protocol->store_short((longlong) cs->number);
flags[0]='\0';
if (cs->state & MY_CS_PRIMARY)
strcat(flags,"def");
protocol->store(flags, system_charset_info);
protocol->store_short((longlong) cs->strxfrm_multiply);
return protocol->write();
}
int mysqld_show_collations(THD *thd, const char *wild)
{ {
char buff[8192]; char buff[8192];
String packet2(buff,sizeof(buff),thd->charset()); String packet2(buff,sizeof(buff),thd->charset());
...@@ -1425,32 +1440,69 @@ int mysqld_show_charsets(THD *thd, const char *wild) ...@@ -1425,32 +1440,69 @@ int mysqld_show_charsets(THD *thd, const char *wild)
DBUG_ENTER("mysqld_show_charsets"); DBUG_ENTER("mysqld_show_charsets");
field_list.push_back(new Item_empty_string("CS_Name",30)); field_list.push_back(new Item_empty_string("Charset",30));
field_list.push_back(new Item_empty_string("COL_Name",30)); field_list.push_back(new Item_empty_string("Collation",30));
field_list.push_back(new Item_return_int("Id",11, FIELD_TYPE_SHORT)); field_list.push_back(new Item_return_int("Id",11, FIELD_TYPE_SHORT));
field_list.push_back(new Item_empty_string("Flags",30)); field_list.push_back(new Item_empty_string("Flags",30));
field_list.push_back(new Item_return_int("strx_maxlen",3, FIELD_TYPE_TINY)); field_list.push_back(new Item_return_int("strx_maxlen",3, FIELD_TYPE_SHORT));
field_list.push_back(new Item_return_int("mb_maxlen",3, FIELD_TYPE_TINY));
if (protocol->send_fields(&field_list, 1)) if (protocol->send_fields(&field_list, 1))
DBUG_RETURN(1); DBUG_RETURN(1);
for (cs=all_charsets ; cs < all_charsets+255 ; cs++ ) for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
{ {
if (cs[0] && !(wild && wild[0] && CHARSET_INFO **cl;
wild_case_compare(system_charset_info,cs[0]->name,wild))) for ( cl= all_charsets; cl < all_charsets+255 ;cl ++)
{ {
protocol->prepare_for_resend(); if (!cs[0] || !cl[0] || !my_charset_same(cs[0],cl[0]) || !(cs[0]->state & MY_CS_PRIMARY))
protocol->store(cs[0]->csname, system_charset_info); continue;
protocol->store(cs[0]->name, system_charset_info); if (cs[0] && !(wild && wild[0] &&
protocol->store_short((longlong) cs[0]->number); wild_case_compare(system_charset_info,cl[0]->name,wild)))
flags[0]='\0'; {
if (cs[0]->state & MY_CS_PRIMARY) if (write_collation(protocol, cl[0]))
strcat(flags,"pri"); goto err;
protocol->store(flags, system_charset_info); }
protocol->store_tiny((longlong) cs[0]->strxfrm_multiply); }
protocol->store_tiny((longlong) cs[0]->mbmaxlen); }
if (protocol->write()) send_eof(thd);
DBUG_RETURN(0);
err:
DBUG_RETURN(1);
}
static bool write_charset(Protocol *protocol, CHARSET_INFO *cs)
{
protocol->prepare_for_resend();
protocol->store(cs->csname, system_charset_info);
protocol->store(cs->name, system_charset_info);
protocol->store_short((longlong) cs->mbmaxlen);
return protocol->write();
}
int mysqld_show_charsets(THD *thd, const char *wild)
{
char buff[8192];
String packet2(buff,sizeof(buff),thd->charset());
List<Item> field_list;
CHARSET_INFO **cs;
Protocol *protocol= thd->protocol;
char flags[64];
DBUG_ENTER("mysqld_show_charsets");
field_list.push_back(new Item_empty_string("Charset",30));
field_list.push_back(new Item_empty_string("Default collation",60));
field_list.push_back(new Item_return_int("Maxlen",3, FIELD_TYPE_SHORT));
if (protocol->send_fields(&field_list, 1))
DBUG_RETURN(1);
for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
{
if (cs[0] && (cs[0]->state & MY_CS_PRIMARY) && !(wild && wild[0] &&
wild_case_compare(system_charset_info,cs[0]->name,wild)))
{
if (write_charset(protocol, cs[0]))
goto err; goto err;
} }
} }
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
CHARSET_INFO *system_charset_info= &my_charset_utf8; CHARSET_INFO *system_charset_info= &my_charset_utf8;
CHARSET_INFO *files_charset_info= &my_charset_utf8; CHARSET_INFO *files_charset_info= &my_charset_utf8;
CHARSET_INFO *national_charset_info= &my_charset_utf8; CHARSET_INFO *national_charset_info= &my_charset_latin1;
extern gptr sql_alloc(unsigned size); extern gptr sql_alloc(unsigned size);
extern void sql_element_free(void *ptr); extern void sql_element_free(void *ptr);
......
...@@ -321,8 +321,8 @@ w_requests: %10lu\n\ ...@@ -321,8 +321,8 @@ w_requests: %10lu\n\
writes: %10lu\n\ writes: %10lu\n\
r_requests: %10lu\n\ r_requests: %10lu\n\
reads: %10lu\n", reads: %10lu\n",
_my_blocks_used,_my_blocks_changed,_my_cache_w_requests, my_blocks_used,my_blocks_changed,my_cache_w_requests,
_my_cache_write,_my_cache_r_requests,_my_cache_read); my_cache_write,my_cache_r_requests,my_cache_read);
pthread_mutex_unlock(&THR_LOCK_keycache); pthread_mutex_unlock(&THR_LOCK_keycache);
if (thd) if (thd)
......
...@@ -3578,6 +3578,8 @@ show_param: ...@@ -3578,6 +3578,8 @@ show_param:
} }
| charset wild | charset wild
{ Lex->sql_command= SQLCOM_SHOW_CHARSETS; } { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
| COLLATION_SYM wild
{ Lex->sql_command= SQLCOM_SHOW_COLLATIONS; }
| LOGS_SYM | LOGS_SYM
{ Lex->sql_command= SQLCOM_SHOW_LOGS; } { Lex->sql_command= SQLCOM_SHOW_LOGS; }
| GRANTS FOR_SYM user | GRANTS FOR_SYM user
......
...@@ -3169,7 +3169,7 @@ CHARSET_INFO compiled_charsets[] = { ...@@ -3169,7 +3169,7 @@ CHARSET_INFO compiled_charsets[] = {
20,0,0, /* number */ 20,0,0, /* number */
MY_CS_COMPILED, /* state */ MY_CS_COMPILED, /* state */
"latin7", /* cs name */ "latin7", /* cs name */
"estonia", /* name */ "latin7_estonian_ci_as", /* name */
"", /* comment */ "", /* comment */
ctype_estonia, ctype_estonia,
to_lower_estonia, to_lower_estonia,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment