Bug #23068 key_cache_block_size is not set or displayes correctly
Submitted: 7 Oct 2006 10:35 Modified: 20 Jun 2007 0:53
Reporter: Oli Sennhauser Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.0.27-BK, 5.0.n (n=20, 24, ?) OS:Linux (Linux)
Assigned to: Ingo Strüwing CPU Architecture:Any
Tags: key_cache_block_size

[7 Oct 2006 10:35] Oli Sennhauser
Description:
If key_cache_block_size is set within mysql it is displayed correctly but when it is set in my.cnf some strange/wrong values are displayed.

How to repeat:
my.cnf: key_cache_block_size=1024

show variables like 'key_cache_block_size';
| key_cache_block_size | 512   |

my.cnf: key_cache_block_size=2048

| key_cache_block_size | 1536  |

my.cnf: key_cache_block_size=4096
| key_cache_block_size | 3584  |

my.cnf: key_cache_block_size=8192
| key_cache_block_size | 7680  |

my.cnf: key_cache_block_size=16384
| key_cache_block_size | 15872 |

It looks like a 512 byte block is somehow subtracted. Or rounded to an amount of 512 byte blocks!!!

my.cnf: key_cache_block_size=1725
| key_cache_block_size | 1536  |

Suggested fix:
I think it is somehow a bug and this round to 512 byte blocks should be documented in our doku.
[7 Oct 2006 10:46] Valeriy Kravchuk
Thank you for a problem report. Verified as described with 5.0.27-BK on Linux.
[7 Oct 2006 13:12] Mark Leith
The rouding to 512 byte chunks is intentional, however the fact that 512 bytes are being subtracted as well is curious:

  {"key_cache_block_size", OPT_KEY_CACHE_BLOCK_SIZE,
   "The default size of key cache blocks",
   (gptr*) &dflt_key_cache_var.param_block_size,
   (gptr*) 0,
   0, (GET_ULONG | GET_ASK_ADDR), REQUIRED_ARG,
   KEY_CACHE_BLOCK_SIZE , 512, 1024*16, MALLOC_OVERHEAD, 512, 0}

The last 512 here is what the value should be a multiple of, as defined in the my_option struct for "block_size":

struct my_option
{
  const char *name;                     /* Name of the option */
  int        id;                        /* unique id or short option */
  const char *comment;                  /* option comment, for autom. --help */
  gptr       *value;                    /* The variable value */
  gptr       *u_max_value;              /* The user def. max variable value */
  const char **str_values;              /* Pointer to possible values */
  ulong     var_type;
  enum get_opt_arg_type arg_type;
  longlong   def_value;                 /* Default value */
  longlong   min_value;                 /* Min allowed value */
  longlong   max_value;                 /* Max allowed value */
  longlong   sub_size;                  /* Subtract this from given value */
  long       block_size;                /* Value should be a mult. of this */
  int        app_type;                  /* To be used by an application */
};

I feel that this should probably be documented here:

http://dev.mysql.com/doc/refman/5.0/en/key-cache-block-size.html

Still digging for why 512 bytes are being subtracted from the values that are multiples of 512.
[10 May 2007 14:50] Calvin Sun
This will be fixed by patches for bug#17332.
[15 May 2007 10:49] Ingo Strüwing
When I looked at the problem quickly, I believed that it reported table corruptions due to "unusual" block sizes. That one I did fix with bug#17332.

But the offsets required on the option value to get what one wants, I detected during my work on bug#17332, but did not fix it, to keep the changes as small as possible. The thing is that key_cache_block_size, when given as a commad line option or in a configuration file, needs to be given bigger at least by 8 in a production server or by 36 in a debug server. Option handling deduces MALLOC_OVERHEAD from the values and then rounds down to the next multiple of 512. IMHO this is not useful and should be fixed.

Another, much more severe problem is that one can specify block sizes which are not an exponent of 2. In this case the key cache should fail dramatically. I am going to fix this too.
[16 May 2007 8:14] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/26778

ChangeSet@1.2465, 2007-05-16 10:14:06+02:00, istruewing@chilla.local +5 -0
  Bug#23068 - key_cache_block_size is not set or displayes correctly
  
  Command line and configuration file option 'key_cache_block_size'
  was mangled by the server and could take weird values.
  
  We deduced MALLOC_OVERHEAD (8 in a production server, 36 in a
  debug server) from the user supplied value and restricted it to
  the greatest multiple of 512 less or equal to the reduced value.
  We accepted values that were no power of 2 (eg. 1536), which
  could result in bad key cache behavior.
  
  This patch changes option 'key_cache_block_size' to not deduce
  MALLOC_OVERHEAD from the input value and restrict the
  result value to be a power of 2.
[16 May 2007 10:56] Ingo Strüwing
Patch has been rejected.
[16 May 2007 13:55] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/26830

ChangeSet@1.2465, 2007-05-16 15:55:36+02:00, istruewing@chilla.local +4 -0
  Bug#23068 - key_cache_block_size is not set or displayes correctly
  
  Command line and configuration file option 'key_cache_block_size'
  was mangled by the server and could take weird values.
  
  We deduced MALLOC_OVERHEAD (8 in a production server, 36 in a
  debug server) from the user supplied value and restricted it to
  the greatest multiple of 512 less or equal to the reduced value.
  We accepted values that were no power of 2 (eg. 1536), which
  could result in bad key cache behavior.
  
  This patch changes option 'key_cache_block_size' to not deduce
  MALLOC_OVERHEAD from the input value and restrict the
  result value to be a power of 2.
[16 May 2007 14:35] Ingo Strüwing
Patch has been rejected again.
[16 May 2007 18:09] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/26865

ChangeSet@1.2465, 2007-05-16 20:00:23+02:00, istruewing@chilla.local +1 -0
  Bug#23068 - key_cache_block_size is not set or displayes correctly
  
  Command line and configuration file option 'key_cache_block_size'
  was reduced by MALLOC_OVERHEAD (8 in a production server, 36 in a
  debug server) from the user supplied value and restricted it to
  the greatest multiple of 512 less or equal to the reduced value.
  
  This patch changes option 'key_cache_block_size' to not deduce
  MALLOC_OVERHEAD from the input value. However, the restriction
  to a multiple of 512 is still done.
[23 May 2007 10:38] Sergey Vojtovich
ok to push.
[4 Jun 2007 10:16] Ingo Strüwing
Queued to 5.1-engines and 5.0-engines.
[18 Jun 2007 7:48] Bugs System
Pushed into 5.1.20-beta
[18 Jun 2007 7:50] Bugs System
Pushed into 5.0.44
[18 Jun 2007 17:22] Paul DuBois
Noted in 5.0.44, 5.1.20 changelogs.

The key_cache_block_size system variable value was set differently
depending on whether the new value was supplied via a startup option
or at runtime via SET.
[19 Jun 2007 10:25] Daniel Fischer
Didn't make it into 5.0.44, will be in 5.0.46.
[20 Jun 2007 0:53] Paul DuBois
Moved 5.0.44 changelog entry to 5.0.46.
[20 Jun 2007 1:02] Paul DuBois
Revised changelog entry:

The server deducted some bytes from the key_cache_block_size option
value and reduced it to the next lower 512 byte boundary. The
resulting block size was not a power of two. Setting the 
key_cache_block_size system variable to a value that is not a power
of two resulted in MyISAM table corruption.

(This description applies to Bug#23068, Bug#25853, Bug#28478)