keycache.h 8.6 KB

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