~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mf_keycache.cc

  • Committer: Brian Aker
  • Date: 2009-10-16 10:27:33 UTC
  • mfrom: (1183.1.4 merge)
  • Revision ID: brian@gaz-20091016102733-b10po5oup0hjlilh
MergeĀ EngineĀ changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
#include <drizzled/global.h>
105
105
#include <mysys/mysys_err.h>
106
106
#include <mysys/my_sys.h>
107
 
#include <keycache.h>
 
107
#include "keycache.h"
108
108
#include <mystrings/m_string.h>
109
109
#include <mysys/my_bit.h>
110
110
#include <errno.h>
111
111
#include <stdarg.h>
112
112
 
 
113
static void change_key_cache_param(KEY_CACHE *keycache, uint32_t division_limit,
 
114
                            uint32_t age_threshold);
 
115
 
113
116
/*
114
117
  Some compilation flags have been added specifically for this module
115
118
  to control the following:
143
146
/* types of condition variables */
144
147
#define  COND_FOR_REQUESTED 0
145
148
#define  COND_FOR_SAVED     1
146
 
#define  COND_FOR_READERS   2
147
149
 
148
150
typedef pthread_cond_t KEYCACHE_CONDVAR;
149
151
 
161
163
  struct st_block_link *block;       /* reference to the block for the page: */
162
164
  File file;                         /* from such a file                     */
163
165
  my_off_t diskpos;                  /* with such an offset                  */
164
 
  uint requests;                     /* number of requests for the page      */
 
166
  uint32_t requests;                     /* number of requests for the page      */
165
167
};
166
168
 
167
169
/* simple states of a block */
193
195
    *next_changed, **prev_changed; /* for lists of file dirty/clean blocks   */
194
196
  struct st_hash_link *hash_link; /* backward ptr to referring hash_link     */
195
197
  KEYCACHE_WQUEUE wqueue[2]; /* queues on waiting requests for new/old pages */
196
 
  uint requests;          /* number of requests for the block                */
197
 
  uchar *buffer;           /* buffer for the block page                       */
198
 
  uint offset;            /* beginning of modified data in the buffer        */
199
 
  uint length;            /* end of data in the buffer                       */
200
 
  uint status;            /* state of the block                              */
 
198
  uint32_t requests;          /* number of requests for the block                */
 
199
  unsigned char *buffer;           /* buffer for the block page                       */
 
200
  uint32_t offset;            /* beginning of modified data in the buffer        */
 
201
  uint32_t length;            /* end of data in the buffer                       */
 
202
  uint32_t status;            /* state of the block                              */
201
203
  enum BLOCK_TEMPERATURE temperature; /* block temperature: cold, warm, hot */
202
 
  uint hits_left;         /* number of hits left until promotion             */
 
204
  uint32_t hits_left;         /* number of hits left until promotion             */
203
205
  uint64_t last_hit_time; /* timestamp of the last hit                      */
204
206
  KEYCACHE_CONDVAR *condvar; /* condition variable for 'no readers' event    */
205
207
};
220
222
                                     (uint32_t) (f)) & (keycache->hash_entries-1))
221
223
#define FILE_HASH(f)                 ((uint) (f) & (CHANGED_BLOCKS_HASH-1))
222
224
 
223
 
#define BLOCK_NUMBER(b)                                                       \
224
 
  ((uint) (((char*)(b)-(char *) keycache->block_root)/sizeof(BLOCK_LINK)))
225
 
#define HASH_LINK_NUMBER(h)                                                   \
226
 
  ((uint) (((char*)(h)-(char *) keycache->hash_link_root)/sizeof(HASH_LINK)))
227
225
 
228
226
#ifdef KEYCACHE_TIMEOUT
229
227
static int keycache_pthread_cond_wait(pthread_cond_t *cond,
236
234
#define keycache_pthread_mutex_unlock pthread_mutex_unlock
237
235
#define keycache_pthread_cond_signal pthread_cond_signal
238
236
 
239
 
static inline uint next_power(uint value)
 
237
static inline uint32_t next_power(uint32_t value)
240
238
{
241
239
  return (uint) my_round_up_to_next_power((uint32_t) value) << 1;
242
240
}
267
265
 
268
266
*/
269
267
 
270
 
int init_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
271
 
                   size_t use_mem, uint division_limit,
272
 
                   uint age_threshold)
 
268
int init_key_cache(KEY_CACHE *keycache, uint32_t key_cache_block_size,
 
269
                   size_t use_mem, uint32_t division_limit,
 
270
                   uint32_t age_threshold)
273
271
{
274
272
  uint32_t blocks, hash_links;
275
273
  size_t length;
325
323
             ((size_t) blocks * keycache->key_cache_block_size) > use_mem)
326
324
        blocks--;
327
325
      /* Allocate memory for cache page buffers */
328
 
      if ((keycache->block_mem= malloc((size_t) blocks * keycache->key_cache_block_size)))
 
326
      if ((keycache->block_mem= (unsigned char *)malloc((size_t) blocks * keycache->key_cache_block_size)))
329
327
      {
330
328
        /*
331
329
          Allocate memory for blocks, hash_links and hash entries;
332
330
          For each block 2 hash links are allocated
333
331
        */
334
 
        if ((keycache->block_root= (BLOCK_LINK*) my_malloc(length,
335
 
                                                           MYF(0))))
 
332
        if ((keycache->block_root= (BLOCK_LINK*) malloc(length)))
336
333
          break;
337
334
        free(keycache->block_mem);
338
335
        keycache->block_mem= 0;
408
405
  }
409
406
  if (keycache->block_root)
410
407
  {
411
 
    my_free((uchar*) keycache->block_root, MYF(0));
 
408
    free((unsigned char*) keycache->block_root);
412
409
    keycache->block_root= NULL;
413
410
  }
414
411
  my_errno= error;
446
443
    (when cnt_for_resize=0).
447
444
*/
448
445
 
449
 
int resize_key_cache(KEY_CACHE *keycache, uint key_cache_block_size,
450
 
                     size_t use_mem, uint division_limit,
451
 
                     uint age_threshold)
 
446
int resize_key_cache(KEY_CACHE *keycache, uint32_t key_cache_block_size,
 
447
                     size_t use_mem, uint32_t division_limit,
 
448
                     uint32_t age_threshold)
452
449
{
453
450
  int blocks;
454
451
 
472
469
  */
473
470
  while (keycache->in_resize)
474
471
  {
475
 
    /* purecov: begin inspected */
476
472
    wait_on_queue(&keycache->resize_queue, &keycache->cache_lock);
477
 
    /* purecov: end */
478
473
  }
479
474
 
480
475
  /*
580
575
    age_threshold.
581
576
*/
582
577
 
583
 
void change_key_cache_param(KEY_CACHE *keycache, uint division_limit,
584
 
                            uint age_threshold)
 
578
static void change_key_cache_param(KEY_CACHE *keycache, uint32_t division_limit,
 
579
                            uint32_t age_threshold)
585
580
{
586
581
  keycache_pthread_mutex_lock(&keycache->cache_lock);
587
582
  if (division_limit)
618
613
    {
619
614
      free(keycache->block_mem);
620
615
      keycache->block_mem= NULL;
621
 
      my_free((uchar*) keycache->block_root, MYF(0));
 
616
      free((unsigned char*) keycache->block_root);
622
617
      keycache->block_root= NULL;
623
618
    }
624
619
    keycache->disk_blocks= -1;
808
803
/*
809
804
  Unlink a block from the chain of dirty/clean blocks
810
805
*/
811
 
static inline void unlink_changed(BLOCK_LINK *block)
 
806
static void unlink_changed(BLOCK_LINK *block)
812
807
{
813
808
  assert(block->prev_changed && *block->prev_changed == block);
814
809
  if (block->next_changed)
823
818
  Link a block into the chain of dirty/clean blocks
824
819
*/
825
820
 
826
 
static inline void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
 
821
static void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
827
822
{
828
823
  assert(!block->next_changed);
829
824
  assert(!block->prev_changed);
1779
1774
          block->buffer= ADD_TO_PTR(keycache->block_mem,
1780
1775
                                    ((uint32_t) keycache->blocks_used*
1781
1776
                                     keycache->key_cache_block_size),
1782
 
                                    uchar*);
 
1777
                                    unsigned char*);
1783
1778
          keycache->blocks_used++;
1784
1779
          assert(!block->next_used);
1785
1780
        }
2098
2093
*/
2099
2094
 
2100
2095
static void read_block(KEY_CACHE *keycache,
2101
 
                       BLOCK_LINK *block, uint read_length,
2102
 
                       uint min_length, bool primary)
 
2096
                       BLOCK_LINK *block, uint32_t read_length,
 
2097
                       uint32_t min_length, bool primary)
2103
2098
{
2104
 
  uint got_length;
 
2099
  uint32_t got_length;
2105
2100
 
2106
2101
  /* On entry cache_lock is locked */
2107
2102
 
2197
2192
    have to be a multiple of key_cache_block_size;
2198
2193
*/
2199
2194
 
2200
 
uchar *key_cache_read(KEY_CACHE *keycache,
 
2195
unsigned char *key_cache_read(KEY_CACHE *keycache,
2201
2196
                      File file, my_off_t filepos, int level,
2202
 
                      uchar *buff, uint length,
2203
 
                      uint block_length __attribute__((unused)),
2204
 
                      int return_buffer __attribute__((unused)))
 
2197
                      unsigned char *buff, uint32_t length,
 
2198
                      uint32_t block_length,
 
2199
                      int return_buffer)
2205
2200
{
 
2201
  (void)block_length;
 
2202
  (void)return_buffer;
2206
2203
  bool locked_and_incremented= false;
2207
2204
  int error=0;
2208
 
  uchar *start= buff;
 
2205
  unsigned char *start= buff;
2209
2206
 
2210
2207
  if (keycache->key_cache_inited)
2211
2208
  {
2212
2209
    /* Key cache is used */
2213
2210
    register BLOCK_LINK *block;
2214
 
    uint read_length;
2215
 
    uint offset;
2216
 
    uint status;
 
2211
    uint32_t read_length;
 
2212
    uint32_t offset;
 
2213
    uint32_t status;
2217
2214
    int page_st;
2218
2215
 
2219
2216
    /*
2270
2267
        */
2271
2268
        keycache->global_cache_read++;
2272
2269
        keycache_pthread_mutex_unlock(&keycache->cache_lock);
2273
 
        error= (pread(file, (uchar*) buff, read_length, filepos + offset) == 0);
 
2270
        error= (pread(file, (unsigned char*) buff, read_length, filepos + offset) == 0);
2274
2271
        keycache_pthread_mutex_lock(&keycache->cache_lock);
2275
2272
        goto next_block;
2276
2273
      }
2354
2351
 
2355
2352
  if (locked_and_incremented)
2356
2353
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
2357
 
  if (pread(file, (uchar*) buff, length, filepos))
 
2354
  if (!pread(file, (unsigned char*) buff, length, filepos))
2358
2355
    error= 1;
2359
2356
  if (locked_and_incremented)
2360
2357
    keycache_pthread_mutex_lock(&keycache->cache_lock);
2365
2362
    dec_counter_for_resize_op(keycache);
2366
2363
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
2367
2364
  }
2368
 
  return(error ? (uchar*) 0 : start);
 
2365
  return(error ? (unsigned char*) 0 : start);
2369
2366
}
2370
2367
 
2371
2368
 
2391
2388
 
2392
2389
int key_cache_insert(KEY_CACHE *keycache,
2393
2390
                     File file, my_off_t filepos, int level,
2394
 
                     uchar *buff, uint length)
 
2391
                     unsigned char *buff, uint32_t length)
2395
2392
{
2396
2393
  int error= 0;
2397
2394
 
2399
2396
  {
2400
2397
    /* Key cache is used */
2401
2398
    register BLOCK_LINK *block;
2402
 
    uint read_length;
2403
 
    uint offset;
 
2399
    uint32_t read_length;
 
2400
    uint32_t offset;
2404
2401
    int page_st;
2405
2402
    bool locked_and_incremented= false;
2406
2403
 
2625
2622
 
2626
2623
int key_cache_write(KEY_CACHE *keycache,
2627
2624
                    File file, my_off_t filepos, int level,
2628
 
                    uchar *buff, uint length,
2629
 
                    uint block_length  __attribute__((unused)),
 
2625
                    unsigned char *buff, uint32_t length,
 
2626
                    uint32_t block_length,
2630
2627
                    int dont_write)
2631
2628
{
 
2629
  (void)block_length;
2632
2630
  bool locked_and_incremented= false;
2633
2631
  int error=0;
2634
2632
 
2635
2633
  if (!dont_write)
2636
2634
  {
2637
 
    /* purecov: begin inspected */
2638
2635
    /* Not used in the server. */
2639
2636
    /* Force writing from buff into disk. */
2640
2637
    keycache->global_cache_w_requests++;
2641
2638
    keycache->global_cache_write++;
2642
2639
    if (pwrite(file, buff, length, filepos) == 0)
2643
2640
      return(1);
2644
 
    /* purecov: end */
2645
2641
  }
2646
2642
 
2647
2643
  if (keycache->key_cache_inited)
2648
2644
  {
2649
2645
    /* Key cache is used */
2650
2646
    register BLOCK_LINK *block;
2651
 
    uint read_length;
2652
 
    uint offset;
 
2647
    uint32_t read_length;
 
2648
    uint32_t offset;
2653
2649
    int page_st;
2654
2650
 
2655
2651
    /*
2709
2705
          /* Used in the server. */
2710
2706
          keycache->global_cache_write++;
2711
2707
          keycache_pthread_mutex_unlock(&keycache->cache_lock);
2712
 
          if (pwrite(file, (uchar*) buff, read_length, filepos + offset) == 0)
 
2708
          if (pwrite(file, (unsigned char*) buff, read_length, filepos + offset) == 0)
2713
2709
            error=1;
2714
2710
          keycache_pthread_mutex_lock(&keycache->cache_lock);
2715
2711
        }
2873
2869
    keycache->global_cache_write++;
2874
2870
    if (locked_and_incremented)
2875
2871
      keycache_pthread_mutex_unlock(&keycache->cache_lock);
2876
 
    if (pwrite(file, (uchar*) buff, length, filepos) == 0)
 
2872
    if (pwrite(file, (unsigned char*) buff, length, filepos) == 0)
2877
2873
      error=1;
2878
2874
    if (locked_and_incremented)
2879
2875
      keycache_pthread_mutex_lock(&keycache->cache_lock);
3052
3048
{
3053
3049
  int error;
3054
3050
  int last_errno= 0;
3055
 
  uint count= (uint) (end-cache);
 
3051
  uint32_t count= (uint) (end-cache);
3056
3052
 
3057
3053
  /* Don't lock the cache during the flush */
3058
3054
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
3060
3056
     As all blocks referred in 'cache' are marked by BLOCK_IN_FLUSH
3061
3057
     we are guarunteed no thread will change them
3062
3058
  */
3063
 
  my_qsort((uchar*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link);
 
3059
  my_qsort((unsigned char*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link);
3064
3060
 
3065
3061
  keycache_pthread_mutex_lock(&keycache->cache_lock);
3066
3062
  /*
3184
3180
  {
3185
3181
    /* Key cache exists and flush is not disabled */
3186
3182
    int error= 0;
3187
 
    uint count= FLUSH_CACHE;
 
3183
    uint32_t count= FLUSH_CACHE;
3188
3184
    BLOCK_LINK **pos,**end;
3189
3185
    BLOCK_LINK *first_in_switch= NULL;
3190
3186
    BLOCK_LINK *last_in_flush;
3191
3187
    BLOCK_LINK *last_for_update;
 
3188
    BLOCK_LINK *last_in_switch;
3192
3189
    BLOCK_LINK *block, *next;
3193
3190
 
3194
3191
    if (type != FLUSH_IGNORE_CHANGED)
3215
3212
        changed blocks appear while we need to wait for something.
3216
3213
      */
3217
3214
      if ((count > FLUSH_CACHE) &&
3218
 
          !(cache= (BLOCK_LINK**) my_malloc(sizeof(BLOCK_LINK*)*count,
3219
 
                                            MYF(0))))
 
3215
          !(cache= (BLOCK_LINK**) malloc(sizeof(BLOCK_LINK*)*count)))
3220
3216
        cache= cache_buff;
3221
3217
      /*
3222
3218
        After a restart there could be more changed blocks than now.
3431
3427
 
3432
3428
    if (! (type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE))
3433
3429
    {
3434
 
      BLOCK_LINK *last_for_update= NULL;
3435
 
      BLOCK_LINK *last_in_switch= NULL;
3436
 
      uint total_found= 0;
3437
 
      uint found;
 
3430
      last_for_update= NULL;
 
3431
      last_in_switch= NULL;
 
3432
      uint32_t total_found= 0;
 
3433
      uint32_t found;
3438
3434
 
3439
3435
      /*
3440
3436
        Finally free all clean blocks for this file.
3472
3468
              struct st_hash_link *next_hash_link= NULL;
3473
3469
              my_off_t            next_diskpos= 0;
3474
3470
              File                next_file= 0;
3475
 
              uint                next_status= 0;
3476
 
              uint                hash_requests= 0;
 
3471
              uint32_t                next_status= 0;
 
3472
              uint32_t                hash_requests= 0;
3477
3473
 
3478
3474
              total_found++;
3479
3475
              found++;
3573
3569
 
3574
3570
err:
3575
3571
  if (cache != cache_buff)
3576
 
    my_free((uchar*) cache, MYF(0));
 
3572
    free((unsigned char*) cache);
3577
3573
  if (last_errno)
3578
3574
    errno=last_errno;                /* Return first error */
3579
3575
  return(last_errno != 0);
3651
3647
static int flush_all_key_blocks(KEY_CACHE *keycache)
3652
3648
{
3653
3649
  BLOCK_LINK    *block;
3654
 
  uint          total_found;
3655
 
  uint          found;
3656
 
  uint          idx;
 
3650
  uint32_t          total_found;
 
3651
  uint32_t          found;
 
3652
  uint32_t          idx;
3657
3653
 
3658
3654
  do
3659
3655
  {
3744
3740
 
3745
3741
  SYNOPSIS
3746
3742
    reset_key_cache_counters()
3747
 
    name       the name of a key cache
3748
 
    key_cache  pointer to the key kache to be reset
3749
3743
 
3750
3744
  DESCRIPTION
3751
 
   This procedure is used by process_key_caches() to reset the counters of all
3752
 
   currently used key caches, both the default one and the named ones.
 
3745
   This procedure is used by process_key_caches() to reset the key_cache.
3753
3746
 
3754
3747
  RETURN
3755
3748
    0 on success (always because it can't fail)
3756
3749
*/
3757
3750
 
3758
 
int reset_key_cache_counters(const char *name __attribute__((unused)),
3759
 
                             KEY_CACHE *key_cache)
 
3751
void reset_key_cache_counters()
3760
3752
{
3761
 
  if (!key_cache->key_cache_inited)
3762
 
  {
3763
 
    return(0);
3764
 
  }
3765
 
  key_cache->global_blocks_changed= 0;   /* Key_blocks_not_flushed */
3766
 
  key_cache->global_cache_r_requests= 0; /* Key_read_requests */
3767
 
  key_cache->global_cache_read= 0;       /* Key_reads */
3768
 
  key_cache->global_cache_w_requests= 0; /* Key_write_requests */
3769
 
  key_cache->global_cache_write= 0;      /* Key_writes */
3770
 
  return(0);
 
3753
  dflt_key_cache->global_blocks_changed= 0;   /* Key_blocks_not_flushed */
 
3754
  dflt_key_cache->global_cache_r_requests= 0; /* Key_read_requests */
 
3755
  dflt_key_cache->global_cache_read= 0;       /* Key_reads */
 
3756
  dflt_key_cache->global_cache_w_requests= 0; /* Key_write_requests */
 
3757
  dflt_key_cache->global_cache_write= 0;      /* Key_writes */
3771
3758
}
3772
3759
 
3773
3760
#if defined(KEYCACHE_TIMEOUT)
3774
3761
 
 
3762
 
 
3763
static inline
 
3764
unsigned int hash_link_number(HASH_LINK *hash_link, KEY_CACHE *keycache)
 
3765
{
 
3766
  return ((unsigned int) (((char*)hash_link-(char *) keycache->hash_link_root)/
 
3767
                  sizeof(HASH_LINK)));
 
3768
}
 
3769
 
 
3770
static inline
 
3771
unsigned int block_number(BLOCK_LINK *block, KEY_CACHE *keycache)
 
3772
{
 
3773
  return ((unsigned int) (((char*)block-(char *)keycache->block_root)/
 
3774
                  sizeof(BLOCK_LINK)));
 
3775
}
 
3776
 
 
3777
 
3775
3778
#define KEYCACHE_DUMP_FILE  "keycache_dump.txt"
3776
3779
#define MAX_QUEUE_LEN  100
3777
3780
 
3784
3787
  BLOCK_LINK *block;
3785
3788
  HASH_LINK *hash_link;
3786
3789
  KEYCACHE_PAGE *page;
3787
 
  uint i;
 
3790
  uint32_t i;
3788
3791
 
3789
3792
  fprintf(keycache_dump_file, "thread:%u\n", thread->id);
3790
3793
 
3813
3816
      thread=thread->next;
3814
3817
      hash_link= (HASH_LINK *) thread->opt_info;
3815
3818
      fprintf(keycache_dump_file,
3816
 
        "thread:%u hash_link:%u (file,filepos)=(%u,%u)\n",
3817
 
        thread->id, (uint) HASH_LINK_NUMBER(hash_link),
3818
 
        (uint) hash_link->file,(uint32_t) hash_link->diskpos);
 
3819
              "thread:%u hash_link:%u (file,filepos)=(%u,%u)\n",
 
3820
              thread->id, (uint) hash_link_number(hash_link, keycache),
 
3821
              (uint) hash_link->file,(uint32_t) hash_link->diskpos);
3819
3822
      if (++i == MAX_QUEUE_LEN)
3820
3823
        break;
3821
3824
    }
3827
3830
    block= &keycache->block_root[i];
3828
3831
    hash_link= block->hash_link;
3829
3832
    fprintf(keycache_dump_file,
3830
 
            "block:%u hash_link:%d status:%x #requests=%u waiting_for_readers:%d\n",
3831
 
            i, (int) (hash_link ? HASH_LINK_NUMBER(hash_link) : -1),
3832
 
            block->status, block->requests, block->condvar ? 1 : 0);
 
3833
            "block:%u hash_link:%d status:%x #requests=%u "
 
3834
            "waiting_for_readers:%d\n",
 
3835
            i, (int) (hash_link ? hash_link_number(hash_link, keycache) : -1),
 
3836
            block->status, block->requests, block->condvar ? 1 : 0);
3833
3837
    for (j=0 ; j < 2; j++)
3834
3838
    {
3835
3839
      KEYCACHE_WQUEUE *wqueue=&block->wqueue[j];
3857
3861
    {
3858
3862
      block= block->next_used;
3859
3863
      fprintf(keycache_dump_file,
3860
 
              "block:%u, ", BLOCK_NUMBER(block));
 
3864
              "block:%u, ", block_number(block, keycache));
3861
3865
    }
3862
3866
    while (block != keycache->used_last);
3863
3867
  }