diff options
author | ingo@mysql.com <> | 2004-05-03 15:55:21 +0200 |
---|---|---|
committer | ingo@mysql.com <> | 2004-05-03 15:55:21 +0200 |
commit | 5a78b4c746555d4c1891ce82d4c0b2d23125caff (patch) | |
tree | f82a460f807cff5eea040e4950e8f7b334c28f33 /mysys/mf_keycache.c | |
parent | b89e5bbc9528d2acd77e5dcfd22a373ab5a1e0c8 (diff) | |
download | mariadb-git-5a78b4c746555d4c1891ce82d4c0b2d23125caff.tar.gz |
WL#1700 - Properly count key_blocks_used and key_blocks_current.
Introduced a new free blocks list. Free blocks are now re-used before
new blocks are allocated from the pool. There is a new status variable
which can be queried by "show status like key_blocks_unused".
Diffstat (limited to 'mysys/mf_keycache.c')
-rw-r--r-- | mysys/mf_keycache.c | 84 |
1 files changed, 68 insertions, 16 deletions
diff --git a/mysys/mf_keycache.c b/mysys/mf_keycache.c index 689391537f8..168483f276b 100644 --- a/mysys/mf_keycache.c +++ b/mysys/mf_keycache.c @@ -20,6 +20,23 @@ One cache can handle many files. It must contain buffers of the same blocksize. init_key_cache() should be used to init cache handler. + + The free list (free_block_list) is a stack like structure. + When a block is freed by free_block(), it is pushed onto the stack. + When a new block is required it is first tried to pop one from the stack. + If the stack is empty, it is tried to get a never-used block from the pool. + If this is empty too, then a block is taken from the LRU ring, flushing it + to disk, if neccessary. This is handled in find_key_block(). + With the new free list, the blocks can have three temperatures: + hot, warm and cold (which is free). This is remembered in the block header + by the enum BLOCK_TEMPERATURE temperature variable. Remembering the + temperature is neccessary to correctly count the number of warm blocks, + which is required to decide when blocks are allowed to become hot. Whenever + a block is inserted to another (sub-)chain, we take the old and new + temperature into account to decide if we got one more or less warm block. + blocks_unused is the sum of never used blocks in the pool and of currently + free blocks. blocks_used is the number of blocks fetched from the pool and + as such gives the maximum number of in-use blocks at any time. */ #include "mysys_priv.h" @@ -116,6 +133,9 @@ struct st_hash_link #define PAGE_TO_BE_READ 1 #define PAGE_WAIT_TO_BE_READ 2 +/* block temperature determines in which (sub-)chain the block currently is */ +enum BLOCK_TEMPERATURE { BLOCK_COLD /*free*/ , BLOCK_WARM , BLOCK_HOT }; + /* key cache block */ struct st_block_link { @@ -130,6 +150,7 @@ struct st_block_link uint offset; /* beginning of modified data in the buffer */ uint length; /* end of data in the buffer */ uint status; /* state of the block */ + enum BLOCK_TEMPERATURE temperature; /* block temperature: cold, warm, hot */ uint hits_left; /* number of hits left until promotion */ ulonglong last_hit_time; /* timestamp of the last hit */ KEYCACHE_CONDVAR *condvar; /* condition variable for 'no readers' event */ @@ -340,6 +361,7 @@ int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size, } blocks= blocks / 4*3; } + keycache->blocks_unused= (ulong) blocks; keycache->disk_blocks= (int) blocks; keycache->hash_links= hash_links; keycache->hash_root= (HASH_LINK**) ((char*) keycache->block_root + @@ -357,12 +379,13 @@ int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size, keycache->free_hash_list= NULL; keycache->blocks_used= keycache->blocks_changed= 0; - keycache->global_blocks_used= keycache->global_blocks_changed= 0; + keycache->global_blocks_changed= 0; keycache->blocks_available=0; /* For debugging */ /* The LRU chain is empty after initialization */ keycache->used_last= NULL; keycache->used_ins= NULL; + keycache->free_block_list= NULL; keycache->keycache_time= 0; keycache->warm_blocks= 0; keycache->min_warm_blocks= (division_limit ? @@ -596,7 +619,7 @@ void end_key_cache(KEY_CACHE *keycache, my_bool cleanup) DBUG_PRINT("status", ("used: %d changed: %d w_requests: %ld \ writes: %ld r_requests: %ld reads: %ld", - keycache->global_blocks_used, keycache->global_blocks_changed, + keycache->blocks_used, keycache->global_blocks_changed, keycache->global_cache_w_requests, keycache->global_cache_write, keycache->global_cache_r_requests, keycache->global_cache_read)); @@ -1014,7 +1037,9 @@ static inline void unreg_request(KEY_CACHE *keycache, keycache->warm_blocks > keycache->min_warm_blocks; if (hot) { - keycache->warm_blocks--; + if (block->temperature == BLOCK_WARM) + keycache->warm_blocks--; + block->temperature= BLOCK_HOT; KEYCACHE_DBUG_PRINT("unreg_request", ("#warm_blocks=%u", keycache->warm_blocks)); } @@ -1026,7 +1051,11 @@ static inline void unreg_request(KEY_CACHE *keycache, block= keycache->used_ins; unlink_block(keycache, block); link_block(keycache, block, 0, 0); - keycache->warm_blocks++; + if (block->temperature != BLOCK_WARM) + { + keycache->warm_blocks++; + block->temperature= BLOCK_WARM; + } KEYCACHE_DBUG_PRINT("unreg_request", ("#warm_blocks=%u", keycache->warm_blocks)); } @@ -1363,28 +1392,40 @@ restart: if (! block) { /* No block is assigned for the page yet */ - if (keycache->blocks_used < (uint) keycache->disk_blocks) + if (keycache->blocks_unused) { - /* There are some never used blocks, take first of them */ - hash_link->block= block= &keycache->block_root[keycache->blocks_used]; - block->buffer= ADD_TO_PTR(keycache->block_mem, - ((ulong) keycache->blocks_used* - keycache->key_cache_block_size), - byte*); + if (keycache->free_block_list) + { + /* There is a block in the free list. */ + block= keycache->free_block_list; + keycache->free_block_list= block->next_used; + block->next_used= NULL; + } + else + { + /* There are some never used blocks, take first of them */ + block= &keycache->block_root[keycache->blocks_used]; + block->buffer= ADD_TO_PTR(keycache->block_mem, + ((ulong) keycache->blocks_used* + keycache->key_cache_block_size), + byte*); + keycache->blocks_used++; + } + keycache->blocks_unused--; block->status= 0; block->length= 0; block->offset= keycache->key_cache_block_size; block->requests= 1; - keycache->blocks_used++; - keycache->global_blocks_used++; - keycache->warm_blocks++; + block->temperature= BLOCK_COLD; block->hits_left= init_hits_left; block->last_hit_time= 0; link_to_file_list(keycache, block, file, 0); block->hash_link= hash_link; + hash_link->block= block; page_status= PAGE_TO_BE_READ; KEYCACHE_DBUG_PRINT("find_key_block", - ("got never used block %u", BLOCK_NUMBER(block))); + ("got free or never used block %u", + BLOCK_NUMBER(block))); } else { @@ -2021,7 +2062,7 @@ end: /* Free block: remove reference to it from hash table, remove it from the chain file of dirty/clean blocks - and add it at the beginning of the LRU chain + and add it to the free list. */ static void free_block(KEY_CACHE *keycache, BLOCK_LINK *block) @@ -2045,6 +2086,17 @@ static void free_block(KEY_CACHE *keycache, BLOCK_LINK *block) ("block is freed")); unreg_request(keycache, block, 0); block->hash_link= NULL; + + /* Remove the free block from the LRU ring. */ + unlink_block(keycache, block); + if (block->temperature == BLOCK_WARM) + keycache->warm_blocks--; + block->temperature= BLOCK_COLD; + /* Insert the free block in the free list. */ + block->next_used= keycache->free_block_list; + keycache->free_block_list= block; + /* Keep track of the number of currently unused blocks. */ + keycache->blocks_unused++; } |