~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mf_keycache.cc

  • Committer: Brian Aker
  • Date: 2009-09-27 21:52:36 UTC
  • mfrom: (1138.1.1 build)
  • Revision ID: brian@gaz-20090927215236-5nx5q6u1ub8uf0l6
Merge Monty (fix for distcheck)

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/*
17
17
  These functions handle keyblock cacheing for ISAM and MyISAM tables.
101
101
  I/O finished.
102
102
*/
103
103
 
104
 
#include "config.h"
105
 
#include "drizzled/error.h"
106
 
#include "drizzled/internal/my_sys.h"
 
104
#include <drizzled/global.h>
 
105
#include <mysys/mysys_err.h>
 
106
#include <mysys/my_sys.h>
107
107
#include "keycache.h"
108
 
#include "drizzled/internal/m_string.h"
109
 
#include "drizzled/internal/my_bit.h"
 
108
#include <mystrings/m_string.h>
 
109
#include <mysys/my_bit.h>
110
110
#include <errno.h>
111
111
#include <stdarg.h>
112
112
 
113
 
using namespace drizzled;
114
 
 
115
113
/*
116
114
  Some compilation flags have been added specifically for this module
117
115
  to control the following:
126
124
    accessing it;
127
125
    to set this number equal to <N> add
128
126
      #define MAX_THREADS <N>
 
127
  - to substitute calls of pthread_cond_wait for calls of
 
128
    pthread_cond_timedwait (wait with timeout set up);
 
129
    this setting should be used only when you want to trap a deadlock
 
130
    situation, which theoretically should not happen;
 
131
    to set timeout equal to <T> seconds add
 
132
      #define KEYCACHE_TIMEOUT <T>
129
133
 
130
134
  Example of the settings:
131
135
    #define SERIALIZED_READ_FROM_CACHE
132
136
    #define MAX_THREADS   100
 
137
    #define KEYCACHE_TIMEOUT  1
133
138
*/
134
139
 
135
140
#define STRUCT_PTR(TYPE, MEMBER, a)                                           \
145
150
struct st_keycache_page
146
151
{
147
152
  int file;               /* file to which the page belongs to  */
148
 
  internal::my_off_t filepos;       /* position of the page in the file   */
 
153
  my_off_t filepos;       /* position of the page in the file   */
149
154
};
150
155
 
151
156
/* element in the chain of a hash table bucket */
153
158
{
154
159
  struct st_hash_link *next, **prev; /* to connect links in the same bucket  */
155
160
  struct st_block_link *block;       /* reference to the block for the page: */
156
 
  int file;                         /* from such a file                     */
157
 
  internal::my_off_t diskpos;                  /* with such an offset                  */
 
161
  File file;                         /* from such a file                     */
 
162
  my_off_t diskpos;                  /* with such an offset                  */
158
163
  uint32_t requests;                     /* number of requests for the page      */
159
164
};
160
165
 
198
203
  KEYCACHE_CONDVAR *condvar; /* condition variable for 'no readers' event    */
199
204
};
200
205
 
 
206
KEY_CACHE dflt_key_cache_var;
 
207
KEY_CACHE *dflt_key_cache= &dflt_key_cache_var;
 
208
 
201
209
#define FLUSH_CACHE         2000            /* sort this many blocks at once */
202
210
 
 
211
static int flush_all_key_blocks(KEY_CACHE *keycache);
 
212
static void wait_on_queue(KEYCACHE_WQUEUE *wqueue,
 
213
                          pthread_mutex_t *mutex);
 
214
static void release_whole_queue(KEYCACHE_WQUEUE *wqueue);
 
215
static void free_block(KEY_CACHE *keycache, BLOCK_LINK *block);
 
216
 
203
217
#define KEYCACHE_HASH(f, pos)                                                 \
204
218
(((uint32_t) ((pos) / keycache->key_cache_block_size) +                          \
205
219
                                     (uint32_t) (f)) & (keycache->hash_entries-1))
206
220
#define FILE_HASH(f)                 ((uint) (f) & (CHANGED_BLOCKS_HASH-1))
207
221
 
208
222
 
209
 
#define  keycache_pthread_cond_wait(A,B) (void)A;
210
 
#define keycache_pthread_mutex_lock(A) (void)A;
211
 
#define keycache_pthread_mutex_unlock(A) (void)A;
212
 
#define keycache_pthread_cond_signal(A) (void)A;
 
223
#ifdef KEYCACHE_TIMEOUT
 
224
static int keycache_pthread_cond_wait(pthread_cond_t *cond,
 
225
                                      pthread_mutex_t *mutex);
 
226
#else
 
227
#define  keycache_pthread_cond_wait pthread_cond_wait
 
228
#endif
 
229
 
 
230
#define keycache_pthread_mutex_lock pthread_mutex_lock
 
231
#define keycache_pthread_mutex_unlock pthread_mutex_unlock
 
232
#define keycache_pthread_cond_signal pthread_cond_signal
213
233
 
214
234
static inline uint32_t next_power(uint32_t value)
215
235
{
216
 
  return my_round_up_to_next_power(value) << 1;
 
236
  return (uint) my_round_up_to_next_power((uint32_t) value) << 1;
217
237
}
218
238
 
219
239
 
246
266
                   size_t use_mem, uint32_t division_limit,
247
267
                   uint32_t age_threshold)
248
268
{
249
 
  (void)keycache;
250
 
  (void)key_cache_block_size;
251
 
  (void)use_mem;
252
 
  (void)division_limit;
253
 
  (void)age_threshold;
254
 
  memset(keycache, 0, sizeof(KEY_CACHE));
255
 
  
256
 
  return 0;
 
269
  uint32_t blocks, hash_links;
 
270
  size_t length;
 
271
  int error;
 
272
  assert(key_cache_block_size >= 512);
 
273
 
 
274
  if (keycache->key_cache_inited && keycache->disk_blocks > 0)
 
275
  {
 
276
    return(0);
 
277
  }
 
278
 
 
279
  keycache->global_cache_w_requests= keycache->global_cache_r_requests= 0;
 
280
  keycache->global_cache_read= keycache->global_cache_write= 0;
 
281
  keycache->disk_blocks= -1;
 
282
  if (! keycache->key_cache_inited)
 
283
  {
 
284
    keycache->key_cache_inited= 1;
 
285
    /*
 
286
      Initialize these variables once only.
 
287
      Their value must survive re-initialization during resizing.
 
288
    */
 
289
    keycache->in_resize= 0;
 
290
    keycache->resize_in_flush= 0;
 
291
    keycache->cnt_for_resize_op= 0;
 
292
    keycache->waiting_for_resize_cnt.last_thread= NULL;
 
293
    keycache->in_init= 0;
 
294
    pthread_mutex_init(&keycache->cache_lock, MY_MUTEX_INIT_FAST);
 
295
    keycache->resize_queue.last_thread= NULL;
 
296
  }
 
297
 
 
298
  keycache->key_cache_mem_size= use_mem;
 
299
  keycache->key_cache_block_size= key_cache_block_size;
 
300
 
 
301
  blocks= (uint32_t) (use_mem / (sizeof(BLOCK_LINK) + 2 * sizeof(HASH_LINK) +
 
302
                              sizeof(HASH_LINK*) * 5/4 + key_cache_block_size));
 
303
  /* It doesn't make sense to have too few blocks (less than 8) */
 
304
  if (blocks >= 8)
 
305
  {
 
306
    for ( ; ; )
 
307
    {
 
308
      /* Set my_hash_entries to the next bigger 2 power */
 
309
      if ((keycache->hash_entries= next_power(blocks)) < blocks * 5/4)
 
310
        keycache->hash_entries<<= 1;
 
311
      hash_links= 2 * blocks;
 
312
#if defined(MAX_THREADS)
 
313
      if (hash_links < MAX_THREADS + blocks - 1)
 
314
        hash_links= MAX_THREADS + blocks - 1;
 
315
#endif
 
316
      while ((length= (ALIGN_SIZE(blocks * sizeof(BLOCK_LINK)) +
 
317
                       ALIGN_SIZE(hash_links * sizeof(HASH_LINK)) +
 
318
                       ALIGN_SIZE(sizeof(HASH_LINK*) *
 
319
                                  keycache->hash_entries))) +
 
320
             ((size_t) blocks * keycache->key_cache_block_size) > use_mem)
 
321
        blocks--;
 
322
      /* Allocate memory for cache page buffers */
 
323
      if ((keycache->block_mem= (unsigned char *)malloc((size_t) blocks * keycache->key_cache_block_size)))
 
324
      {
 
325
        /*
 
326
          Allocate memory for blocks, hash_links and hash entries;
 
327
          For each block 2 hash links are allocated
 
328
        */
 
329
        if ((keycache->block_root= (BLOCK_LINK*) malloc(length)))
 
330
          break;
 
331
        free(keycache->block_mem);
 
332
        keycache->block_mem= 0;
 
333
      }
 
334
      if (blocks < 8)
 
335
      {
 
336
        my_errno= ENOMEM;
 
337
        my_error(EE_OUTOFMEMORY, MYF(0), blocks * keycache->key_cache_block_size);
 
338
        goto err;
 
339
      }
 
340
      blocks= blocks / 4*3;
 
341
    }
 
342
    keycache->blocks_unused= blocks;
 
343
    keycache->disk_blocks= (int) blocks;
 
344
    keycache->hash_links= hash_links;
 
345
    keycache->hash_root= (HASH_LINK**) ((char*) keycache->block_root +
 
346
                                        ALIGN_SIZE(blocks*sizeof(BLOCK_LINK)));
 
347
    keycache->hash_link_root= (HASH_LINK*) ((char*) keycache->hash_root +
 
348
                                            ALIGN_SIZE((sizeof(HASH_LINK*) *
 
349
                                                        keycache->hash_entries)));
 
350
    memset(keycache->block_root, 0,
 
351
           keycache->disk_blocks * sizeof(BLOCK_LINK));
 
352
    memset(keycache->hash_root, 0,
 
353
           keycache->hash_entries * sizeof(HASH_LINK*));
 
354
    memset(keycache->hash_link_root, 0,
 
355
           keycache->hash_links * sizeof(HASH_LINK));
 
356
    keycache->hash_links_used= 0;
 
357
    keycache->free_hash_list= NULL;
 
358
    keycache->blocks_used= keycache->blocks_changed= 0;
 
359
 
 
360
    keycache->global_blocks_changed= 0;
 
361
    keycache->blocks_available=0;               /* For debugging */
 
362
 
 
363
    /* The LRU chain is empty after initialization */
 
364
    keycache->used_last= NULL;
 
365
    keycache->used_ins= NULL;
 
366
    keycache->free_block_list= NULL;
 
367
    keycache->keycache_time= 0;
 
368
    keycache->warm_blocks= 0;
 
369
    keycache->min_warm_blocks= (division_limit ?
 
370
                                blocks * division_limit / 100 + 1 :
 
371
                                blocks);
 
372
    keycache->age_threshold= (age_threshold ?
 
373
                              blocks * age_threshold / 100 :
 
374
                              blocks);
 
375
 
 
376
    keycache->can_be_used= 1;
 
377
 
 
378
    keycache->waiting_for_hash_link.last_thread= NULL;
 
379
    keycache->waiting_for_block.last_thread= NULL;
 
380
    memset(keycache->changed_blocks, 0,
 
381
           sizeof(keycache->changed_blocks[0]) * CHANGED_BLOCKS_HASH);
 
382
    memset(keycache->file_blocks, 0,
 
383
           sizeof(keycache->file_blocks[0]) * CHANGED_BLOCKS_HASH);
 
384
  }
 
385
  else
 
386
  {
 
387
    /* key_buffer_size is specified too small. Disable the cache. */
 
388
    keycache->can_be_used= 0;
 
389
  }
 
390
 
 
391
  keycache->blocks= keycache->disk_blocks > 0 ? keycache->disk_blocks : 0;
 
392
  return((int) keycache->disk_blocks);
 
393
 
 
394
err:
 
395
  error= my_errno;
 
396
  keycache->disk_blocks= 0;
 
397
  keycache->blocks=  0;
 
398
  if (keycache->block_mem)
 
399
  {
 
400
    free(keycache->block_mem);
 
401
    keycache->block_mem= NULL;
 
402
  }
 
403
  if (keycache->block_root)
 
404
  {
 
405
    free((unsigned char*) keycache->block_root);
 
406
    keycache->block_root= NULL;
 
407
  }
 
408
  my_errno= error;
 
409
  keycache->can_be_used= 0;
 
410
  return(0);
 
411
}
 
412
 
 
413
 
 
414
/*
 
415
  Resize a key cache
 
416
 
 
417
  SYNOPSIS
 
418
    resize_key_cache()
 
419
    keycache                    pointer to a key cache data structure
 
420
    key_cache_block_size        size of blocks to keep cached data
 
421
    use_mem                     total memory to use for the new key cache
 
422
    division_limit              new division limit (if not zero)
 
423
    age_threshold               new age threshold (if not zero)
 
424
 
 
425
  RETURN VALUE
 
426
    number of blocks in the key cache, if successful,
 
427
    0 - otherwise.
 
428
 
 
429
  NOTES.
 
430
    The function first compares the memory size and the block size parameters
 
431
    with the key cache values.
 
432
 
 
433
    If they differ the function free the the memory allocated for the
 
434
    old key cache blocks by calling the end_key_cache function and
 
435
    then rebuilds the key cache with new blocks by calling
 
436
    init_key_cache.
 
437
 
 
438
    The function starts the operation only when all other threads
 
439
    performing operations with the key cache let her to proceed
 
440
    (when cnt_for_resize=0).
 
441
*/
 
442
 
 
443
int resize_key_cache(KEY_CACHE *keycache, uint32_t key_cache_block_size,
 
444
                     size_t use_mem, uint32_t division_limit,
 
445
                     uint32_t age_threshold)
 
446
{
 
447
  int blocks;
 
448
 
 
449
  if (!keycache->key_cache_inited)
 
450
    return(keycache->disk_blocks);
 
451
 
 
452
  if(key_cache_block_size == keycache->key_cache_block_size &&
 
453
     use_mem == keycache->key_cache_mem_size)
 
454
  {
 
455
    change_key_cache_param(keycache, division_limit, age_threshold);
 
456
    return(keycache->disk_blocks);
 
457
  }
 
458
 
 
459
  keycache_pthread_mutex_lock(&keycache->cache_lock);
 
460
 
 
461
  /*
 
462
    We may need to wait for another thread which is doing a resize
 
463
    already. This cannot happen in the MySQL server though. It allows
 
464
    one resizer only. In set_var.cc keycache->in_init is used to block
 
465
    multiple attempts.
 
466
  */
 
467
  while (keycache->in_resize)
 
468
  {
 
469
    wait_on_queue(&keycache->resize_queue, &keycache->cache_lock);
 
470
  }
 
471
 
 
472
  /*
 
473
    Mark the operation in progress. This blocks other threads from doing
 
474
    a resize in parallel. It prohibits new blocks to enter the cache.
 
475
    Read/write requests can bypass the cache during the flush phase.
 
476
  */
 
477
  keycache->in_resize= 1;
 
478
 
 
479
  /* Need to flush only if keycache is enabled. */
 
480
  if (keycache->can_be_used)
 
481
  {
 
482
    /* Start the flush phase. */
 
483
    keycache->resize_in_flush= 1;
 
484
 
 
485
    if (flush_all_key_blocks(keycache))
 
486
    {
 
487
      /* TODO: if this happens, we should write a warning in the log file ! */
 
488
      keycache->resize_in_flush= 0;
 
489
      blocks= 0;
 
490
      keycache->can_be_used= 0;
 
491
      goto finish;
 
492
    }
 
493
 
 
494
    /* End the flush phase. */
 
495
    keycache->resize_in_flush= 0;
 
496
  }
 
497
 
 
498
  /*
 
499
    Some direct read/write operations (bypassing the cache) may still be
 
500
    unfinished. Wait until they are done. If the key cache can be used,
 
501
    direct I/O is done in increments of key_cache_block_size. That is,
 
502
    every block is checked if it is in the cache. We need to wait for
 
503
    pending I/O before re-initializing the cache, because we may change
 
504
    the block size. Otherwise they could check for blocks at file
 
505
    positions where the new block division has none. We do also want to
 
506
    wait for I/O done when (if) the cache was disabled. It must not
 
507
    run in parallel with normal cache operation.
 
508
  */
 
509
  while (keycache->cnt_for_resize_op)
 
510
    wait_on_queue(&keycache->waiting_for_resize_cnt, &keycache->cache_lock);
 
511
 
 
512
  /*
 
513
    Free old cache structures, allocate new structures, and initialize
 
514
    them. Note that the cache_lock mutex and the resize_queue are left
 
515
    untouched. We do not lose the cache_lock and will release it only at
 
516
    the end of this function.
 
517
  */
 
518
  end_key_cache(keycache, 0);                   /* Don't free mutex */
 
519
  /* The following will work even if use_mem is 0 */
 
520
  blocks= init_key_cache(keycache, key_cache_block_size, use_mem,
 
521
                         division_limit, age_threshold);
 
522
 
 
523
finish:
 
524
  /*
 
525
    Mark the resize finished. This allows other threads to start a
 
526
    resize or to request new cache blocks.
 
527
  */
 
528
  keycache->in_resize= 0;
 
529
 
 
530
  /* Signal waiting threads. */
 
531
  release_whole_queue(&keycache->resize_queue);
 
532
 
 
533
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
534
  return(blocks);
 
535
}
 
536
 
 
537
 
 
538
/*
 
539
  Increment counter blocking resize key cache operation
 
540
*/
 
541
static inline void inc_counter_for_resize_op(KEY_CACHE *keycache)
 
542
{
 
543
  keycache->cnt_for_resize_op++;
 
544
}
 
545
 
 
546
 
 
547
/*
 
548
  Decrement counter blocking resize key cache operation;
 
549
  Signal the operation to proceed when counter becomes equal zero
 
550
*/
 
551
static inline void dec_counter_for_resize_op(KEY_CACHE *keycache)
 
552
{
 
553
  if (!--keycache->cnt_for_resize_op)
 
554
    release_whole_queue(&keycache->waiting_for_resize_cnt);
 
555
}
 
556
 
 
557
/*
 
558
  Change the key cache parameters
 
559
 
 
560
  SYNOPSIS
 
561
    change_key_cache_param()
 
562
    keycache                    pointer to a key cache data structure
 
563
    division_limit              new division limit (if not zero)
 
564
    age_threshold               new age threshold (if not zero)
 
565
 
 
566
  RETURN VALUE
 
567
    none
 
568
 
 
569
  NOTES.
 
570
    Presently the function resets the key cache parameters
 
571
    concerning midpoint insertion strategy - division_limit and
 
572
    age_threshold.
 
573
*/
 
574
 
 
575
void change_key_cache_param(KEY_CACHE *keycache, uint32_t division_limit,
 
576
                            uint32_t age_threshold)
 
577
{
 
578
  keycache_pthread_mutex_lock(&keycache->cache_lock);
 
579
  if (division_limit)
 
580
    keycache->min_warm_blocks= (keycache->disk_blocks *
 
581
                                division_limit / 100 + 1);
 
582
  if (age_threshold)
 
583
    keycache->age_threshold=   (keycache->disk_blocks *
 
584
                                age_threshold / 100);
 
585
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
586
  return;
257
587
}
258
588
 
259
589
 
271
601
 
272
602
void end_key_cache(KEY_CACHE *keycache, bool cleanup)
273
603
{
274
 
  (void)keycache;
275
 
  (void)cleanup;
 
604
  if (!keycache->key_cache_inited)
 
605
    return;
 
606
 
 
607
  if (keycache->disk_blocks > 0)
 
608
  {
 
609
    if (keycache->block_mem)
 
610
    {
 
611
      free(keycache->block_mem);
 
612
      keycache->block_mem= NULL;
 
613
      free((unsigned char*) keycache->block_root);
 
614
      keycache->block_root= NULL;
 
615
    }
 
616
    keycache->disk_blocks= -1;
 
617
    /* Reset blocks_changed to be safe if flush_all_key_blocks is called */
 
618
    keycache->blocks_changed= 0;
 
619
  }
 
620
 
 
621
  if (cleanup)
 
622
  {
 
623
    pthread_mutex_destroy(&keycache->cache_lock);
 
624
    keycache->key_cache_inited= keycache->can_be_used= 0;
 
625
  }
 
626
  return;
276
627
} /* end_key_cache */
277
628
 
278
629
 
279
630
/*
 
631
  Link a thread into double-linked queue of waiting threads.
 
632
 
 
633
  SYNOPSIS
 
634
    link_into_queue()
 
635
      wqueue              pointer to the queue structure
 
636
      thread              pointer to the thread to be added to the queue
 
637
 
 
638
  RETURN VALUE
 
639
    none
 
640
 
 
641
  NOTES.
 
642
    Queue is represented by a circular list of the thread structures
 
643
    The list is double-linked of the type (**prev,*next), accessed by
 
644
    a pointer to the last element.
 
645
*/
 
646
 
 
647
static void link_into_queue(KEYCACHE_WQUEUE *wqueue,
 
648
                                   struct st_my_thread_var *thread)
 
649
{
 
650
  struct st_my_thread_var *last;
 
651
 
 
652
  assert(!thread->next && !thread->prev);
 
653
  if (! (last= wqueue->last_thread))
 
654
  {
 
655
    /* Queue is empty */
 
656
    thread->next= thread;
 
657
    thread->prev= &thread->next;
 
658
  }
 
659
  else
 
660
  {
 
661
    thread->prev= last->next->prev;
 
662
    last->next->prev= &thread->next;
 
663
    thread->next= last->next;
 
664
    last->next= thread;
 
665
  }
 
666
  wqueue->last_thread= thread;
 
667
}
 
668
 
 
669
/*
 
670
  Unlink a thread from double-linked queue of waiting threads
 
671
 
 
672
  SYNOPSIS
 
673
    unlink_from_queue()
 
674
      wqueue              pointer to the queue structure
 
675
      thread              pointer to the thread to be removed from the queue
 
676
 
 
677
  RETURN VALUE
 
678
    none
 
679
 
 
680
  NOTES.
 
681
    See NOTES for link_into_queue
 
682
*/
 
683
 
 
684
static void unlink_from_queue(KEYCACHE_WQUEUE *wqueue,
 
685
                                     struct st_my_thread_var *thread)
 
686
{
 
687
  assert(thread->next && thread->prev);
 
688
  if (thread->next == thread)
 
689
    /* The queue contains only one member */
 
690
    wqueue->last_thread= NULL;
 
691
  else
 
692
  {
 
693
    thread->next->prev= thread->prev;
 
694
    *thread->prev=thread->next;
 
695
    if (wqueue->last_thread == thread)
 
696
      wqueue->last_thread= STRUCT_PTR(struct st_my_thread_var, next,
 
697
                                      thread->prev);
 
698
  }
 
699
  thread->next= NULL;
 
700
  thread->prev= NULL;
 
701
}
 
702
 
 
703
 
 
704
/*
 
705
  Add a thread to single-linked queue of waiting threads
 
706
 
 
707
  SYNOPSIS
 
708
    wait_on_queue()
 
709
      wqueue            Pointer to the queue structure.
 
710
      mutex             Cache_lock to acquire after awake.
 
711
 
 
712
  RETURN VALUE
 
713
    none
 
714
 
 
715
  NOTES.
 
716
    Queue is represented by a circular list of the thread structures
 
717
    The list is single-linked of the type (*next), accessed by a pointer
 
718
    to the last element.
 
719
 
 
720
    The function protects against stray signals by verifying that the
 
721
    current thread is unlinked from the queue when awaking. However,
 
722
    since several threads can wait for the same event, it might be
 
723
    necessary for the caller of the function to check again if the
 
724
    condition for awake is indeed matched.
 
725
*/
 
726
 
 
727
static void wait_on_queue(KEYCACHE_WQUEUE *wqueue,
 
728
                          pthread_mutex_t *mutex)
 
729
{
 
730
  struct st_my_thread_var *last;
 
731
  struct st_my_thread_var *thread= my_thread_var;
 
732
 
 
733
  /* Add to queue. */
 
734
  assert(!thread->next);
 
735
  assert(!thread->prev); /* Not required, but must be true anyway. */
 
736
  if (! (last= wqueue->last_thread))
 
737
    thread->next= thread;
 
738
  else
 
739
  {
 
740
    thread->next= last->next;
 
741
    last->next= thread;
 
742
  }
 
743
  wqueue->last_thread= thread;
 
744
 
 
745
  /*
 
746
    Wait until thread is removed from queue by the signalling thread.
 
747
    The loop protects against stray signals.
 
748
  */
 
749
  do
 
750
  {
 
751
    keycache_pthread_cond_wait(&thread->suspend, mutex);
 
752
  }
 
753
  while (thread->next);
 
754
}
 
755
 
 
756
 
 
757
/*
 
758
  Remove all threads from queue signaling them to proceed
 
759
 
 
760
  SYNOPSIS
 
761
    release_whole_queue()
 
762
      wqueue            pointer to the queue structure
 
763
 
 
764
  RETURN VALUE
 
765
    none
 
766
 
 
767
  NOTES.
 
768
    See notes for wait_on_queue().
 
769
    When removed from the queue each thread is signaled via condition
 
770
    variable thread->suspend.
 
771
*/
 
772
 
 
773
static void release_whole_queue(KEYCACHE_WQUEUE *wqueue)
 
774
{
 
775
  struct st_my_thread_var *last;
 
776
  struct st_my_thread_var *next;
 
777
  struct st_my_thread_var *thread;
 
778
 
 
779
  /* Queue may be empty. */
 
780
  if (!(last= wqueue->last_thread))
 
781
    return;
 
782
 
 
783
  next= last->next;
 
784
  do
 
785
  {
 
786
    thread=next;
 
787
    /* Signal the thread. */
 
788
    keycache_pthread_cond_signal(&thread->suspend);
 
789
    /* Take thread from queue. */
 
790
    next=thread->next;
 
791
    thread->next= NULL;
 
792
  }
 
793
  while (thread != last);
 
794
 
 
795
  /* Now queue is definitely empty. */
 
796
  wqueue->last_thread= NULL;
 
797
}
 
798
 
 
799
 
 
800
/*
 
801
  Unlink a block from the chain of dirty/clean blocks
 
802
*/
 
803
static void unlink_changed(BLOCK_LINK *block)
 
804
{
 
805
  assert(block->prev_changed && *block->prev_changed == block);
 
806
  if (block->next_changed)
 
807
    block->next_changed->prev_changed= block->prev_changed;
 
808
  *block->prev_changed= block->next_changed;
 
809
  block->next_changed= NULL;
 
810
  block->prev_changed= NULL;
 
811
}
 
812
 
 
813
 
 
814
/*
 
815
  Link a block into the chain of dirty/clean blocks
 
816
*/
 
817
 
 
818
static void link_changed(BLOCK_LINK *block, BLOCK_LINK **phead)
 
819
{
 
820
  assert(!block->next_changed);
 
821
  assert(!block->prev_changed);
 
822
  block->prev_changed= phead;
 
823
  if ((block->next_changed= *phead))
 
824
    (*phead)->prev_changed= &block->next_changed;
 
825
  *phead= block;
 
826
}
 
827
 
 
828
 
 
829
/*
 
830
  Link a block in a chain of clean blocks of a file.
 
831
 
 
832
  SYNOPSIS
 
833
    link_to_file_list()
 
834
      keycache          Key cache handle
 
835
      block             Block to relink
 
836
      file              File to be linked to
 
837
      unlink            If to unlink first
 
838
 
 
839
  DESCRIPTION
 
840
    Unlink a block from whichever chain it is linked in, if it's
 
841
    asked for, and link it to the chain of clean blocks of the
 
842
    specified file.
 
843
 
 
844
  NOTE
 
845
    Please do never set/clear BLOCK_CHANGED outside of
 
846
    link_to_file_list() or link_to_changed_list().
 
847
    You would risk to damage correct counting of changed blocks
 
848
    and to find blocks in the wrong hash.
 
849
 
 
850
  RETURN
 
851
    void
 
852
*/
 
853
 
 
854
static void link_to_file_list(KEY_CACHE *keycache,
 
855
                              BLOCK_LINK *block, int file,
 
856
                              bool unlink_block)
 
857
{
 
858
  assert(block->status & BLOCK_IN_USE);
 
859
  assert(block->hash_link && block->hash_link->block == block);
 
860
  assert(block->hash_link->file == file);
 
861
  if (unlink_block)
 
862
    unlink_changed(block);
 
863
  link_changed(block, &keycache->file_blocks[FILE_HASH(file)]);
 
864
  if (block->status & BLOCK_CHANGED)
 
865
  {
 
866
    block->status&= ~BLOCK_CHANGED;
 
867
    keycache->blocks_changed--;
 
868
    keycache->global_blocks_changed--;
 
869
  }
 
870
}
 
871
 
 
872
 
 
873
/*
 
874
  Re-link a block from the clean chain to the dirty chain of a file.
 
875
 
 
876
  SYNOPSIS
 
877
    link_to_changed_list()
 
878
      keycache          key cache handle
 
879
      block             block to relink
 
880
 
 
881
  DESCRIPTION
 
882
    Unlink a block from the chain of clean blocks of a file
 
883
    and link it to the chain of dirty blocks of the same file.
 
884
 
 
885
  NOTE
 
886
    Please do never set/clear BLOCK_CHANGED outside of
 
887
    link_to_file_list() or link_to_changed_list().
 
888
    You would risk to damage correct counting of changed blocks
 
889
    and to find blocks in the wrong hash.
 
890
 
 
891
  RETURN
 
892
    void
 
893
*/
 
894
 
 
895
static void link_to_changed_list(KEY_CACHE *keycache,
 
896
                                 BLOCK_LINK *block)
 
897
{
 
898
  assert(block->status & BLOCK_IN_USE);
 
899
  assert(!(block->status & BLOCK_CHANGED));
 
900
  assert(block->hash_link && block->hash_link->block == block);
 
901
 
 
902
  unlink_changed(block);
 
903
  link_changed(block,
 
904
               &keycache->changed_blocks[FILE_HASH(block->hash_link->file)]);
 
905
  block->status|=BLOCK_CHANGED;
 
906
  keycache->blocks_changed++;
 
907
  keycache->global_blocks_changed++;
 
908
}
 
909
 
 
910
 
 
911
/*
 
912
  Link a block to the LRU chain at the beginning or at the end of
 
913
  one of two parts.
 
914
 
 
915
  SYNOPSIS
 
916
    link_block()
 
917
      keycache            pointer to a key cache data structure
 
918
      block               pointer to the block to link to the LRU chain
 
919
      hot                 <-> to link the block into the hot subchain
 
920
      at_end              <-> to link the block at the end of the subchain
 
921
 
 
922
  RETURN VALUE
 
923
    none
 
924
 
 
925
  NOTES.
 
926
    The LRU ring is represented by a circular list of block structures.
 
927
    The list is double-linked of the type (**prev,*next) type.
 
928
    The LRU ring is divided into two parts - hot and warm.
 
929
    There are two pointers to access the last blocks of these two
 
930
    parts. The beginning of the warm part follows right after the
 
931
    end of the hot part.
 
932
    Only blocks of the warm part can be used for eviction.
 
933
    The first block from the beginning of this subchain is always
 
934
    taken for eviction (keycache->last_used->next)
 
935
 
 
936
    LRU chain:       +------+   H O T    +------+
 
937
                +----| end  |----...<----| beg  |----+
 
938
                |    +------+last        +------+    |
 
939
                v<-link in latest hot (new end)      |
 
940
                |     link in latest warm (new end)->^
 
941
                |    +------+  W A R M   +------+    |
 
942
                +----| beg  |---->...----| end  |----+
 
943
                     +------+            +------+ins
 
944
                  first for eviction
 
945
 
 
946
    It is also possible that the block is selected for eviction and thus
 
947
    not linked in the LRU ring.
 
948
*/
 
949
 
 
950
static void link_block(KEY_CACHE *keycache, BLOCK_LINK *block, bool hot,
 
951
                       bool at_end)
 
952
{
 
953
  BLOCK_LINK *ins;
 
954
  BLOCK_LINK **pins;
 
955
 
 
956
  assert((block->status & ~BLOCK_CHANGED) == (BLOCK_READ | BLOCK_IN_USE));
 
957
  assert(block->hash_link); /*backptr to block NULL from free_block()*/
 
958
  assert(!block->requests);
 
959
  assert(block->prev_changed && *block->prev_changed == block);
 
960
  assert(!block->next_used);
 
961
  assert(!block->prev_used);
 
962
  if (!hot && keycache->waiting_for_block.last_thread)
 
963
  {
 
964
    /* Signal that in the LRU warm sub-chain an available block has appeared */
 
965
    struct st_my_thread_var *last_thread=
 
966
                               keycache->waiting_for_block.last_thread;
 
967
    struct st_my_thread_var *first_thread= last_thread->next;
 
968
    struct st_my_thread_var *next_thread= first_thread;
 
969
    HASH_LINK *hash_link= (HASH_LINK *) first_thread->opt_info;
 
970
    struct st_my_thread_var *thread;
 
971
    do
 
972
    {
 
973
      thread= next_thread;
 
974
      next_thread= thread->next;
 
975
      /*
 
976
         We notify about the event all threads that ask
 
977
         for the same page as the first thread in the queue
 
978
      */
 
979
      if ((HASH_LINK *) thread->opt_info == hash_link)
 
980
      {
 
981
        keycache_pthread_cond_signal(&thread->suspend);
 
982
        unlink_from_queue(&keycache->waiting_for_block, thread);
 
983
        block->requests++;
 
984
      }
 
985
    }
 
986
    while (thread != last_thread);
 
987
    hash_link->block= block;
 
988
    /*
 
989
      NOTE: We assigned the block to the hash_link and signalled the
 
990
      requesting thread(s). But it is possible that other threads runs
 
991
      first. These threads see the hash_link assigned to a block which
 
992
      is assigned to another hash_link and not marked BLOCK_IN_SWITCH.
 
993
      This can be a problem for functions that do not select the block
 
994
      via its hash_link: flush and free. They do only see a block which
 
995
      is in a "normal" state and don't know that it will be evicted soon.
 
996
 
 
997
      We cannot set BLOCK_IN_SWITCH here because only one of the
 
998
      requesting threads must handle the eviction. All others must wait
 
999
      for it to complete. If we set the flag here, the threads would not
 
1000
      know who is in charge of the eviction. Without the flag, the first
 
1001
      thread takes the stick and sets the flag.
 
1002
 
 
1003
      But we need to note in the block that is has been selected for
 
1004
      eviction. It must not be freed. The evicting thread will not
 
1005
      expect the block in the free list. Before freeing we could also
 
1006
      check if block->requests > 1. But I think including another flag
 
1007
      in the check of block->status is slightly more efficient and
 
1008
      probably easier to read.
 
1009
    */
 
1010
    block->status|= BLOCK_IN_EVICTION;
 
1011
    return;
 
1012
  }
 
1013
  pins= hot ? &keycache->used_ins : &keycache->used_last;
 
1014
  ins= *pins;
 
1015
  if (ins)
 
1016
  {
 
1017
    ins->next_used->prev_used= &block->next_used;
 
1018
    block->next_used= ins->next_used;
 
1019
    block->prev_used= &ins->next_used;
 
1020
    ins->next_used= block;
 
1021
    if (at_end)
 
1022
      *pins= block;
 
1023
  }
 
1024
  else
 
1025
  {
 
1026
    /* The LRU ring is empty. Let the block point to itself. */
 
1027
    keycache->used_last= keycache->used_ins= block->next_used= block;
 
1028
    block->prev_used= &block->next_used;
 
1029
  }
 
1030
}
 
1031
 
 
1032
 
 
1033
/*
 
1034
  Unlink a block from the LRU chain
 
1035
 
 
1036
  SYNOPSIS
 
1037
    unlink_block()
 
1038
      keycache            pointer to a key cache data structure
 
1039
      block               pointer to the block to unlink from the LRU chain
 
1040
 
 
1041
  RETURN VALUE
 
1042
    none
 
1043
 
 
1044
  NOTES.
 
1045
    See NOTES for link_block
 
1046
*/
 
1047
 
 
1048
static void unlink_block(KEY_CACHE *keycache, BLOCK_LINK *block)
 
1049
{
 
1050
  assert((block->status & ~BLOCK_CHANGED) == (BLOCK_READ | BLOCK_IN_USE));
 
1051
  assert(block->hash_link); /*backptr to block NULL from free_block()*/
 
1052
  assert(!block->requests);
 
1053
  assert(block->prev_changed && *block->prev_changed == block);
 
1054
  assert(block->next_used && block->prev_used &&
 
1055
              (block->next_used->prev_used == &block->next_used) &&
 
1056
              (*block->prev_used == block));
 
1057
  if (block->next_used == block)
 
1058
    /* The list contains only one member */
 
1059
    keycache->used_last= keycache->used_ins= NULL;
 
1060
  else
 
1061
  {
 
1062
    block->next_used->prev_used= block->prev_used;
 
1063
    *block->prev_used= block->next_used;
 
1064
    if (keycache->used_last == block)
 
1065
      keycache->used_last= STRUCT_PTR(BLOCK_LINK, next_used, block->prev_used);
 
1066
    if (keycache->used_ins == block)
 
1067
      keycache->used_ins=STRUCT_PTR(BLOCK_LINK, next_used, block->prev_used);
 
1068
  }
 
1069
  block->next_used= NULL;
 
1070
  block->prev_used= NULL;
 
1071
}
 
1072
 
 
1073
 
 
1074
/*
 
1075
  Register requests for a block.
 
1076
 
 
1077
  SYNOPSIS
 
1078
    reg_requests()
 
1079
      keycache          Pointer to a key cache data structure.
 
1080
      block             Pointer to the block to register a request on.
 
1081
      count             Number of requests. Always 1.
 
1082
 
 
1083
  NOTE
 
1084
    The first request unlinks the block from the LRU ring. This means
 
1085
    that it is protected against eveiction.
 
1086
 
 
1087
  RETURN
 
1088
    void
 
1089
*/
 
1090
static void reg_requests(KEY_CACHE *keycache, BLOCK_LINK *block, int count)
 
1091
{
 
1092
  assert(block->status & BLOCK_IN_USE);
 
1093
  assert(block->hash_link);
 
1094
 
 
1095
  if (!block->requests)
 
1096
    unlink_block(keycache, block);
 
1097
  block->requests+=count;
 
1098
}
 
1099
 
 
1100
 
 
1101
/*
 
1102
  Unregister request for a block
 
1103
  linking it to the LRU chain if it's the last request
 
1104
 
 
1105
  SYNOPSIS
 
1106
    unreg_request()
 
1107
    keycache            pointer to a key cache data structure
 
1108
    block               pointer to the block to link to the LRU chain
 
1109
    at_end              <-> to link the block at the end of the LRU chain
 
1110
 
 
1111
  RETURN VALUE
 
1112
    none
 
1113
 
 
1114
  NOTES.
 
1115
    Every linking to the LRU ring decrements by one a special block
 
1116
    counter (if it's positive). If the at_end parameter is true the block is
 
1117
    added either at the end of warm sub-chain or at the end of hot sub-chain.
 
1118
    It is added to the hot subchain if its counter is zero and number of
 
1119
    blocks in warm sub-chain is not less than some low limit (determined by
 
1120
    the division_limit parameter). Otherwise the block is added to the warm
 
1121
    sub-chain. If the at_end parameter is false the block is always added
 
1122
    at beginning of the warm sub-chain.
 
1123
    Thus a warm block can be promoted to the hot sub-chain when its counter
 
1124
    becomes zero for the first time.
 
1125
    At the same time  the block at the very beginning of the hot subchain
 
1126
    might be moved to the beginning of the warm subchain if it stays untouched
 
1127
    for a too long time (this time is determined by parameter age_threshold).
 
1128
 
 
1129
    It is also possible that the block is selected for eviction and thus
 
1130
    not linked in the LRU ring.
 
1131
*/
 
1132
 
 
1133
static void unreg_request(KEY_CACHE *keycache,
 
1134
                          BLOCK_LINK *block, int at_end)
 
1135
{
 
1136
  assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1137
  assert(block->hash_link); /*backptr to block NULL from free_block()*/
 
1138
  assert(block->requests);
 
1139
  assert(block->prev_changed && *block->prev_changed == block);
 
1140
  assert(!block->next_used);
 
1141
  assert(!block->prev_used);
 
1142
  if (! --block->requests)
 
1143
  {
 
1144
    bool hot;
 
1145
    if (block->hits_left)
 
1146
      block->hits_left--;
 
1147
    hot= !block->hits_left && at_end &&
 
1148
      keycache->warm_blocks > keycache->min_warm_blocks;
 
1149
    if (hot)
 
1150
    {
 
1151
      if (block->temperature == BLOCK_WARM)
 
1152
        keycache->warm_blocks--;
 
1153
      block->temperature= BLOCK_HOT;
 
1154
    }
 
1155
    link_block(keycache, block, hot, (bool)at_end);
 
1156
    block->last_hit_time= keycache->keycache_time;
 
1157
    keycache->keycache_time++;
 
1158
    /*
 
1159
      At this place, the block might be in the LRU ring or not. If an
 
1160
      evicter was waiting for a block, it was selected for eviction and
 
1161
      not linked in the LRU ring.
 
1162
    */
 
1163
 
 
1164
    /*
 
1165
      Check if we should link a hot block to the warm block sub-chain.
 
1166
      It is possible that we select the same block as above. But it can
 
1167
      also be another block. In any case a block from the LRU ring is
 
1168
      selected. In other words it works even if the above block was
 
1169
      selected for eviction and not linked in the LRU ring. Since this
 
1170
      happens only if the LRU ring is empty, the block selected below
 
1171
      would be NULL and the rest of the function skipped.
 
1172
    */
 
1173
    block= keycache->used_ins;
 
1174
    if (block && keycache->keycache_time - block->last_hit_time >
 
1175
        keycache->age_threshold)
 
1176
    {
 
1177
      unlink_block(keycache, block);
 
1178
      link_block(keycache, block, 0, 0);
 
1179
      if (block->temperature != BLOCK_WARM)
 
1180
      {
 
1181
        keycache->warm_blocks++;
 
1182
        block->temperature= BLOCK_WARM;
 
1183
      }
 
1184
    }
 
1185
  }
 
1186
}
 
1187
 
 
1188
/*
 
1189
  Remove a reader of the page in block
 
1190
*/
 
1191
 
 
1192
static void remove_reader(BLOCK_LINK *block)
 
1193
{
 
1194
  assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1195
  assert(block->hash_link && block->hash_link->block == block);
 
1196
  assert(block->prev_changed && *block->prev_changed == block);
 
1197
  assert(!block->next_used);
 
1198
  assert(!block->prev_used);
 
1199
  assert(block->hash_link->requests);
 
1200
  if (! --block->hash_link->requests && block->condvar)
 
1201
    keycache_pthread_cond_signal(block->condvar);
 
1202
}
 
1203
 
 
1204
 
 
1205
/*
 
1206
  Wait until the last reader of the page in block
 
1207
  signals on its termination
 
1208
*/
 
1209
 
 
1210
static void wait_for_readers(KEY_CACHE *keycache,
 
1211
                             BLOCK_LINK *block)
 
1212
{
 
1213
  struct st_my_thread_var *thread= my_thread_var;
 
1214
  assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1215
  assert(!(block->status & (BLOCK_ERROR | BLOCK_IN_FLUSH |
 
1216
                                 BLOCK_CHANGED)));
 
1217
  assert(block->hash_link);
 
1218
  assert(block->hash_link->block == block);
 
1219
  /* Linked in file_blocks or changed_blocks hash. */
 
1220
  assert(block->prev_changed && *block->prev_changed == block);
 
1221
  /* Not linked in LRU ring. */
 
1222
  assert(!block->next_used);
 
1223
  assert(!block->prev_used);
 
1224
  while (block->hash_link->requests)
 
1225
  {
 
1226
    /* There must be no other waiter. We have no queue here. */
 
1227
    assert(!block->condvar);
 
1228
    block->condvar= &thread->suspend;
 
1229
    keycache_pthread_cond_wait(&thread->suspend, &keycache->cache_lock);
 
1230
    block->condvar= NULL;
 
1231
  }
 
1232
}
 
1233
 
 
1234
 
 
1235
/*
280
1236
  Add a hash link to a bucket in the hash_table
281
1237
*/
282
1238
 
291
1247
 
292
1248
 
293
1249
/*
 
1250
  Remove a hash link from the hash table
 
1251
*/
 
1252
 
 
1253
static void unlink_hash(KEY_CACHE *keycache, HASH_LINK *hash_link)
 
1254
{
 
1255
  assert(hash_link->requests == 0);
 
1256
  if ((*hash_link->prev= hash_link->next))
 
1257
    hash_link->next->prev= hash_link->prev;
 
1258
  hash_link->block= NULL;
 
1259
  if (keycache->waiting_for_hash_link.last_thread)
 
1260
  {
 
1261
    /* Signal that a free hash link has appeared */
 
1262
    struct st_my_thread_var *last_thread=
 
1263
                               keycache->waiting_for_hash_link.last_thread;
 
1264
    struct st_my_thread_var *first_thread= last_thread->next;
 
1265
    struct st_my_thread_var *next_thread= first_thread;
 
1266
    KEYCACHE_PAGE *first_page= (KEYCACHE_PAGE *) (first_thread->opt_info);
 
1267
    struct st_my_thread_var *thread;
 
1268
 
 
1269
    hash_link->file= first_page->file;
 
1270
    hash_link->diskpos= first_page->filepos;
 
1271
    do
 
1272
    {
 
1273
      KEYCACHE_PAGE *page;
 
1274
      thread= next_thread;
 
1275
      page= (KEYCACHE_PAGE *) thread->opt_info;
 
1276
      next_thread= thread->next;
 
1277
      /*
 
1278
         We notify about the event all threads that ask
 
1279
         for the same page as the first thread in the queue
 
1280
      */
 
1281
      if (page->file == hash_link->file && page->filepos == hash_link->diskpos)
 
1282
      {
 
1283
        keycache_pthread_cond_signal(&thread->suspend);
 
1284
        unlink_from_queue(&keycache->waiting_for_hash_link, thread);
 
1285
      }
 
1286
    }
 
1287
    while (thread != last_thread);
 
1288
    link_hash(&keycache->hash_root[KEYCACHE_HASH(hash_link->file,
 
1289
                                                 hash_link->diskpos)],
 
1290
              hash_link);
 
1291
    return;
 
1292
  }
 
1293
  hash_link->next= keycache->free_hash_list;
 
1294
  keycache->free_hash_list= hash_link;
 
1295
}
 
1296
 
 
1297
 
 
1298
/*
 
1299
  Get the hash link for a page
 
1300
*/
 
1301
 
 
1302
static HASH_LINK *get_hash_link(KEY_CACHE *keycache,
 
1303
                                int file, my_off_t filepos)
 
1304
{
 
1305
  register HASH_LINK *hash_link, **start;
 
1306
 
 
1307
restart:
 
1308
  /*
 
1309
     Find the bucket in the hash table for the pair (file, filepos);
 
1310
     start contains the head of the bucket list,
 
1311
     hash_link points to the first member of the list
 
1312
  */
 
1313
  hash_link= *(start= &keycache->hash_root[KEYCACHE_HASH(file, filepos)]);
 
1314
  /* Look for an element for the pair (file, filepos) in the bucket chain */
 
1315
  while (hash_link &&
 
1316
         (hash_link->diskpos != filepos || hash_link->file != file))
 
1317
  {
 
1318
    hash_link= hash_link->next;
 
1319
  }
 
1320
  if (! hash_link)
 
1321
  {
 
1322
    /* There is no hash link in the hash table for the pair (file, filepos) */
 
1323
    if (keycache->free_hash_list)
 
1324
    {
 
1325
      hash_link= keycache->free_hash_list;
 
1326
      keycache->free_hash_list= hash_link->next;
 
1327
    }
 
1328
    else if (keycache->hash_links_used < keycache->hash_links)
 
1329
    {
 
1330
      hash_link= &keycache->hash_link_root[keycache->hash_links_used++];
 
1331
    }
 
1332
    else
 
1333
    {
 
1334
      /* Wait for a free hash link */
 
1335
      struct st_my_thread_var *thread= my_thread_var;
 
1336
      KEYCACHE_PAGE page;
 
1337
      page.file= file;
 
1338
      page.filepos= filepos;
 
1339
      thread->opt_info= (void *) &page;
 
1340
      link_into_queue(&keycache->waiting_for_hash_link, thread);
 
1341
      keycache_pthread_cond_wait(&thread->suspend,
 
1342
                                 &keycache->cache_lock);
 
1343
      thread->opt_info= NULL;
 
1344
      goto restart;
 
1345
    }
 
1346
    hash_link->file= file;
 
1347
    hash_link->diskpos= filepos;
 
1348
    link_hash(start, hash_link);
 
1349
  }
 
1350
  /* Register the request for the page */
 
1351
  hash_link->requests++;
 
1352
 
 
1353
  return hash_link;
 
1354
}
 
1355
 
 
1356
 
 
1357
/*
 
1358
  Get a block for the file page requested by a keycache read/write operation;
 
1359
  If the page is not in the cache return a free block, if there is none
 
1360
  return the lru block after saving its buffer if the page is dirty.
 
1361
 
 
1362
  SYNOPSIS
 
1363
 
 
1364
    find_key_block()
 
1365
      keycache            pointer to a key cache data structure
 
1366
      file                handler for the file to read page from
 
1367
      filepos             position of the page in the file
 
1368
      init_hits_left      how initialize the block counter for the page
 
1369
      wrmode              <-> get for writing
 
1370
      page_st        out  {PAGE_READ,PAGE_TO_BE_READ,PAGE_WAIT_TO_BE_READ}
 
1371
 
 
1372
  RETURN VALUE
 
1373
    Pointer to the found block if successful, 0 - otherwise
 
1374
 
 
1375
  NOTES.
 
1376
    For the page from file positioned at filepos the function checks whether
 
1377
    the page is in the key cache specified by the first parameter.
 
1378
    If this is the case it immediately returns the block.
 
1379
    If not, the function first chooses  a block for this page. If there is
 
1380
    no not used blocks in the key cache yet, the function takes the block
 
1381
    at the very beginning of the warm sub-chain. It saves the page in that
 
1382
    block if it's dirty before returning the pointer to it.
 
1383
    The function returns in the page_st parameter the following values:
 
1384
      PAGE_READ         - if page already in the block,
 
1385
      PAGE_TO_BE_READ   - if it is to be read yet by the current thread
 
1386
      WAIT_TO_BE_READ   - if it is to be read by another thread
 
1387
    If an error occurs THE BLOCK_ERROR bit is set in the block status.
 
1388
    It might happen that there are no blocks in LRU chain (in warm part) -
 
1389
    all blocks  are unlinked for some read/write operations. Then the function
 
1390
    waits until first of this operations links any block back.
 
1391
*/
 
1392
 
 
1393
static BLOCK_LINK *find_key_block(KEY_CACHE *keycache,
 
1394
                                  File file, my_off_t filepos,
 
1395
                                  int init_hits_left,
 
1396
                                  int wrmode, int *page_st)
 
1397
{
 
1398
  HASH_LINK *hash_link;
 
1399
  BLOCK_LINK *block;
 
1400
  int error= 0;
 
1401
  int page_status;
 
1402
 
 
1403
restart:
 
1404
  /*
 
1405
    If the flush phase of a resize operation fails, the cache is left
 
1406
    unusable. This will be detected only after "goto restart".
 
1407
  */
 
1408
  if (!keycache->can_be_used)
 
1409
    return(0);
 
1410
 
 
1411
  /*
 
1412
    Find the hash_link for the requested file block (file, filepos). We
 
1413
    do always get a hash_link here. It has registered our request so
 
1414
    that no other thread can use it for another file block until we
 
1415
    release the request (which is done by remove_reader() usually). The
 
1416
    hash_link can have a block assigned to it or not. If there is a
 
1417
    block, it may be assigned to this hash_link or not. In cases where a
 
1418
    block is evicted from the cache, it is taken from the LRU ring and
 
1419
    referenced by the new hash_link. But the block can still be assigned
 
1420
    to its old hash_link for some time if it needs to be flushed first,
 
1421
    or if there are other threads still reading it.
 
1422
 
 
1423
    Summary:
 
1424
      hash_link is always returned.
 
1425
      hash_link->block can be:
 
1426
      - NULL or
 
1427
      - not assigned to this hash_link or
 
1428
      - assigned to this hash_link. If assigned, the block can have
 
1429
        - invalid data (when freshly assigned) or
 
1430
        - valid data. Valid data can be
 
1431
          - changed over the file contents (dirty) or
 
1432
          - not changed (clean).
 
1433
  */
 
1434
  hash_link= get_hash_link(keycache, file, filepos);
 
1435
  assert((hash_link->file == file) && (hash_link->diskpos == filepos));
 
1436
 
 
1437
  page_status= -1;
 
1438
  if ((block= hash_link->block) &&
 
1439
      block->hash_link == hash_link && (block->status & BLOCK_READ))
 
1440
  {
 
1441
    /* Assigned block with valid (changed or unchanged) contents. */
 
1442
    page_status= PAGE_READ;
 
1443
  }
 
1444
  /*
 
1445
    else (page_status == -1)
 
1446
      - block == NULL or
 
1447
      - block not assigned to this hash_link or
 
1448
      - block assigned but not yet read from file (invalid data).
 
1449
  */
 
1450
 
 
1451
  if (keycache->in_resize)
 
1452
  {
 
1453
    /* This is a request during a resize operation */
 
1454
 
 
1455
    if (!block)
 
1456
    {
 
1457
      struct st_my_thread_var *thread;
 
1458
 
 
1459
      /*
 
1460
        The file block is not in the cache. We don't need it in the
 
1461
        cache: we are going to read or write directly to file. Cancel
 
1462
        the request. We can simply decrement hash_link->requests because
 
1463
        we did not release cache_lock since increasing it. So no other
 
1464
        thread can wait for our request to become released.
 
1465
      */
 
1466
      if (hash_link->requests == 1)
 
1467
      {
 
1468
        /*
 
1469
          We are the only one to request this hash_link (this file/pos).
 
1470
          Free the hash_link.
 
1471
        */
 
1472
        hash_link->requests--;
 
1473
        unlink_hash(keycache, hash_link);
 
1474
        return(0);
 
1475
      }
 
1476
 
 
1477
      /*
 
1478
        More requests on the hash_link. Someone tries to evict a block
 
1479
        for this hash_link (could have started before resizing started).
 
1480
        This means that the LRU ring is empty. Otherwise a block could
 
1481
        be assigned immediately. Behave like a thread that wants to
 
1482
        evict a block for this file/pos. Add to the queue of threads
 
1483
        waiting for a block. Wait until there is one assigned.
 
1484
 
 
1485
        Refresh the request on the hash-link so that it cannot be reused
 
1486
        for another file/pos.
 
1487
      */
 
1488
      thread= my_thread_var;
 
1489
      thread->opt_info= (void *) hash_link;
 
1490
      link_into_queue(&keycache->waiting_for_block, thread);
 
1491
      do
 
1492
      {
 
1493
        keycache_pthread_cond_wait(&thread->suspend,
 
1494
                                   &keycache->cache_lock);
 
1495
      } while (thread->next);
 
1496
      thread->opt_info= NULL;
 
1497
      /*
 
1498
        A block should now be assigned to the hash_link. But it may
 
1499
        still need to be evicted. Anyway, we should re-check the
 
1500
        situation. page_status must be set correctly.
 
1501
      */
 
1502
      hash_link->requests--;
 
1503
      goto restart;
 
1504
    } /* end of if (!block) */
 
1505
 
 
1506
    /*
 
1507
      There is a block for this file/pos in the cache. Register a
 
1508
      request on it. This unlinks it from the LRU ring (if it is there)
 
1509
      and hence protects it against eviction (if not already in
 
1510
      eviction). We need this for returning the block to the caller, for
 
1511
      calling remove_reader() (for debugging purposes), and for calling
 
1512
      free_block(). The only case where we don't need the request is if
 
1513
      the block is in eviction. In that case we have to unregister the
 
1514
      request later.
 
1515
    */
 
1516
    reg_requests(keycache, block, 1);
 
1517
 
 
1518
    if (page_status != PAGE_READ)
 
1519
    {
 
1520
      /*
 
1521
        - block not assigned to this hash_link or
 
1522
        - block assigned but not yet read from file (invalid data).
 
1523
 
 
1524
        This must be a block in eviction. It will be read soon. We need
 
1525
        to wait here until this happened. Otherwise the caller could
 
1526
        access a wrong block or a block which is in read. While waiting
 
1527
        we cannot lose hash_link nor block. We have registered a request
 
1528
        on the hash_link. Everything can happen to the block but changes
 
1529
        in the hash_link -> block relationship. In other words:
 
1530
        everything can happen to the block but free or another completed
 
1531
        eviction.
 
1532
 
 
1533
        Note that we bahave like a secondary requestor here. We just
 
1534
        cannot return with PAGE_WAIT_TO_BE_READ. This would work for
 
1535
        read requests and writes on dirty blocks that are not in flush
 
1536
        only. Waiting here on COND_FOR_REQUESTED works in all
 
1537
        situations.
 
1538
      */
 
1539
      assert(((block->hash_link != hash_link) &&
 
1540
                   (block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH))) ||
 
1541
                  ((block->hash_link == hash_link) &&
 
1542
                   !(block->status & BLOCK_READ)));
 
1543
      wait_on_queue(&block->wqueue[COND_FOR_REQUESTED], &keycache->cache_lock);
 
1544
      /*
 
1545
        Here we can trust that the block has been assigned to this
 
1546
        hash_link (block->hash_link == hash_link) and read into the
 
1547
        buffer (BLOCK_READ). The worst things possible here are that the
 
1548
        block is in free (BLOCK_REASSIGNED). But the block is still
 
1549
        assigned to the hash_link. The freeing thread waits until we
 
1550
        release our request on the hash_link. The block must not be
 
1551
        again in eviction because we registered an request on it before
 
1552
        starting to wait.
 
1553
      */
 
1554
      assert(block->hash_link == hash_link);
 
1555
      assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1556
      assert(!(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH)));
 
1557
    }
 
1558
    /*
 
1559
      The block is in the cache. Assigned to the hash_link. Valid data.
 
1560
      Note that in case of page_st == PAGE_READ, the block can be marked
 
1561
      for eviction. In any case it can be marked for freeing.
 
1562
    */
 
1563
 
 
1564
    if (!wrmode)
 
1565
    {
 
1566
      /* A reader can just read the block. */
 
1567
      *page_st= PAGE_READ;
 
1568
      assert((hash_link->file == file) &&
 
1569
                  (hash_link->diskpos == filepos) &&
 
1570
                  (block->hash_link == hash_link));
 
1571
      return(block);
 
1572
    }
 
1573
 
 
1574
    /*
 
1575
      This is a writer. No two writers for the same block can exist.
 
1576
      This must be assured by locks outside of the key cache.
 
1577
    */
 
1578
    assert(!(block->status & BLOCK_FOR_UPDATE));
 
1579
 
 
1580
    while (block->status & BLOCK_IN_FLUSH)
 
1581
    {
 
1582
      /*
 
1583
        Wait until the block is flushed to file. Do not release the
 
1584
        request on the hash_link yet to prevent that the block is freed
 
1585
        or reassigned while we wait. While we wait, several things can
 
1586
        happen to the block, including another flush. But the block
 
1587
        cannot be reassigned to another hash_link until we release our
 
1588
        request on it. But it can be marked BLOCK_REASSIGNED from free
 
1589
        or eviction, while they wait for us to release the hash_link.
 
1590
      */
 
1591
      wait_on_queue(&block->wqueue[COND_FOR_SAVED], &keycache->cache_lock);
 
1592
      /*
 
1593
        If the flush phase failed, the resize could have finished while
 
1594
        we waited here.
 
1595
      */
 
1596
      if (!keycache->in_resize)
 
1597
      {
 
1598
        remove_reader(block);
 
1599
        unreg_request(keycache, block, 1);
 
1600
        goto restart;
 
1601
      }
 
1602
      assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1603
      assert(!(block->status & BLOCK_FOR_UPDATE));
 
1604
      assert(block->hash_link == hash_link);
 
1605
    }
 
1606
 
 
1607
    if (block->status & BLOCK_CHANGED)
 
1608
    {
 
1609
      /*
 
1610
        We want to write a block with changed contents. If the cache
 
1611
        block size is bigger than the callers block size (e.g. MyISAM),
 
1612
        the caller may replace part of the block only. Changes of the
 
1613
        other part of the block must be preserved. Since the block has
 
1614
        not yet been selected for flush, we can still add our changes.
 
1615
      */
 
1616
      *page_st= PAGE_READ;
 
1617
      assert((hash_link->file == file) &&
 
1618
                  (hash_link->diskpos == filepos) &&
 
1619
                  (block->hash_link == hash_link));
 
1620
      return(block);
 
1621
    }
 
1622
 
 
1623
    /*
 
1624
      This is a write request for a clean block. We do not want to have
 
1625
      new dirty blocks in the cache while resizing. We will free the
 
1626
      block and write directly to file. If the block is in eviction or
 
1627
      in free, we just let it go.
 
1628
 
 
1629
      Unregister from the hash_link. This must be done before freeing
 
1630
      the block. And it must be done if not freeing the block. Because
 
1631
      we could have waited above, we need to call remove_reader(). Other
 
1632
      threads could wait for us to release our request on the hash_link.
 
1633
    */
 
1634
    remove_reader(block);
 
1635
 
 
1636
    /* If the block is not in eviction and not in free, we can free it. */
 
1637
    if (!(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
1638
                           BLOCK_REASSIGNED)))
 
1639
    {
 
1640
      /*
 
1641
        Free block as we are going to write directly to file.
 
1642
        Although we have an exlusive lock for the updated key part,
 
1643
        the control can be yielded by the current thread as we might
 
1644
        have unfinished readers of other key parts in the block
 
1645
        buffer. Still we are guaranteed not to have any readers
 
1646
        of the key part we are writing into until the block is
 
1647
        removed from the cache as we set the BLOCK_REASSIGNED
 
1648
        flag (see the code below that handles reading requests).
 
1649
      */
 
1650
      free_block(keycache, block);
 
1651
    }
 
1652
    else
 
1653
    {
 
1654
      /*
 
1655
        The block will be evicted/freed soon. Don't touch it in any way.
 
1656
        Unregister the request that we registered above.
 
1657
      */
 
1658
      unreg_request(keycache, block, 1);
 
1659
 
 
1660
      /*
 
1661
        The block is still assigned to the hash_link (the file/pos that
 
1662
        we are going to write to). Wait until the eviction/free is
 
1663
        complete. Otherwise the direct write could complete before all
 
1664
        readers are done with the block. So they could read outdated
 
1665
        data.
 
1666
 
 
1667
        Since we released our request on the hash_link, it can be reused
 
1668
        for another file/pos. Hence we cannot just check for
 
1669
        block->hash_link == hash_link. As long as the resize is
 
1670
        proceeding the block cannot be reassigned to the same file/pos
 
1671
        again. So we can terminate the loop when the block is no longer
 
1672
        assigned to this file/pos.
 
1673
      */
 
1674
      do
 
1675
      {
 
1676
        wait_on_queue(&block->wqueue[COND_FOR_SAVED],
 
1677
                      &keycache->cache_lock);
 
1678
        /*
 
1679
          If the flush phase failed, the resize could have finished
 
1680
          while we waited here.
 
1681
        */
 
1682
        if (!keycache->in_resize)
 
1683
          goto restart;
 
1684
      } while (block->hash_link &&
 
1685
               (block->hash_link->file == file) &&
 
1686
               (block->hash_link->diskpos == filepos));
 
1687
    }
 
1688
    return(0);
 
1689
  }
 
1690
 
 
1691
  if (page_status == PAGE_READ &&
 
1692
      (block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
1693
                        BLOCK_REASSIGNED)))
 
1694
  {
 
1695
    /*
 
1696
      This is a request for a block to be removed from cache. The block
 
1697
      is assigned to this hash_link and contains valid data, but is
 
1698
      marked for eviction or to be freed. Possible reasons why it has
 
1699
      not yet been evicted/freed can be a flush before reassignment
 
1700
      (BLOCK_IN_SWITCH), readers of the block have not finished yet
 
1701
      (BLOCK_REASSIGNED), or the evicting thread did not yet awake after
 
1702
      the block has been selected for it (BLOCK_IN_EVICTION).
 
1703
 
 
1704
       Only reading requests can proceed until the old dirty page is flushed,
 
1705
       all others are to be suspended, then resubmitted
 
1706
    */
 
1707
    if (!wrmode && !(block->status & BLOCK_REASSIGNED))
 
1708
    {
 
1709
      /*
 
1710
        This is a read request and the block not yet reassigned. We can
 
1711
        register our request and proceed. This unlinks the block from
 
1712
        the LRU ring and protects it against eviction.
 
1713
      */
 
1714
      reg_requests(keycache, block, 1);
 
1715
    }
 
1716
    else
 
1717
    {
 
1718
      /*
 
1719
        Either this is a write request for a block that is in eviction
 
1720
        or in free. We must not use it any more. Instead we must evict
 
1721
        another block. But we cannot do this before the eviction/free is
 
1722
        done. Otherwise we would find the same hash_link + block again
 
1723
        and again.
 
1724
 
 
1725
        Or this is a read request for a block in eviction/free that does
 
1726
        not require a flush, but waits for readers to finish with the
 
1727
        block. We do not read this block to let the eviction/free happen
 
1728
        as soon as possible. Again we must wait so that we don't find
 
1729
        the same hash_link + block again and again.
 
1730
      */
 
1731
      assert(hash_link->requests);
 
1732
      hash_link->requests--;
 
1733
      wait_on_queue(&block->wqueue[COND_FOR_SAVED], &keycache->cache_lock);
 
1734
      /*
 
1735
        The block is no longer assigned to this hash_link.
 
1736
        Get another one.
 
1737
      */
 
1738
      goto restart;
 
1739
    }
 
1740
  }
 
1741
  else
 
1742
  {
 
1743
    /*
 
1744
      This is a request for a new block or for a block not to be removed.
 
1745
      Either
 
1746
      - block == NULL or
 
1747
      - block not assigned to this hash_link or
 
1748
      - block assigned but not yet read from file,
 
1749
      or
 
1750
      - block assigned with valid (changed or unchanged) data and
 
1751
      - it will not be reassigned/freed.
 
1752
    */
 
1753
    if (! block)
 
1754
    {
 
1755
      /* No block is assigned to the hash_link yet. */
 
1756
      if (keycache->blocks_unused)
 
1757
      {
 
1758
        if (keycache->free_block_list)
 
1759
        {
 
1760
          /* There is a block in the free list. */
 
1761
          block= keycache->free_block_list;
 
1762
          keycache->free_block_list= block->next_used;
 
1763
          block->next_used= NULL;
 
1764
        }
 
1765
        else
 
1766
        {
 
1767
          /* There are some never used blocks, take first of them */
 
1768
          assert(keycache->blocks_used <
 
1769
                      (uint32_t) keycache->disk_blocks);
 
1770
          block= &keycache->block_root[keycache->blocks_used];
 
1771
          block->buffer= ADD_TO_PTR(keycache->block_mem,
 
1772
                                    ((uint32_t) keycache->blocks_used*
 
1773
                                     keycache->key_cache_block_size),
 
1774
                                    unsigned char*);
 
1775
          keycache->blocks_used++;
 
1776
          assert(!block->next_used);
 
1777
        }
 
1778
        assert(!block->prev_used);
 
1779
        assert(!block->next_changed);
 
1780
        assert(!block->prev_changed);
 
1781
        assert(!block->hash_link);
 
1782
        assert(!block->status);
 
1783
        assert(!block->requests);
 
1784
        keycache->blocks_unused--;
 
1785
        block->status= BLOCK_IN_USE;
 
1786
        block->length= 0;
 
1787
        block->offset= keycache->key_cache_block_size;
 
1788
        block->requests= 1;
 
1789
        block->temperature= BLOCK_COLD;
 
1790
        block->hits_left= init_hits_left;
 
1791
        block->last_hit_time= 0;
 
1792
        block->hash_link= hash_link;
 
1793
        hash_link->block= block;
 
1794
        link_to_file_list(keycache, block, file, 0);
 
1795
        page_status= PAGE_TO_BE_READ;
 
1796
      }
 
1797
      else
 
1798
      {
 
1799
        /*
 
1800
          There are no free blocks and no never used blocks, use a block
 
1801
          from the LRU ring.
 
1802
        */
 
1803
 
 
1804
        if (! keycache->used_last)
 
1805
        {
 
1806
          /*
 
1807
            The LRU ring is empty. Wait until a new block is added to
 
1808
            it. Several threads might wait here for the same hash_link,
 
1809
            all of them must get the same block. While waiting for a
 
1810
            block, after a block is selected for this hash_link, other
 
1811
            threads can run first before this one awakes. During this
 
1812
            time interval other threads find this hash_link pointing to
 
1813
            the block, which is still assigned to another hash_link. In
 
1814
            this case the block is not marked BLOCK_IN_SWITCH yet, but
 
1815
            it is marked BLOCK_IN_EVICTION.
 
1816
          */
 
1817
 
 
1818
          struct st_my_thread_var *thread= my_thread_var;
 
1819
          thread->opt_info= (void *) hash_link;
 
1820
          link_into_queue(&keycache->waiting_for_block, thread);
 
1821
          do
 
1822
          {
 
1823
            keycache_pthread_cond_wait(&thread->suspend,
 
1824
                                       &keycache->cache_lock);
 
1825
          }
 
1826
          while (thread->next);
 
1827
          thread->opt_info= NULL;
 
1828
          /* Assert that block has a request registered. */
 
1829
          assert(hash_link->block->requests);
 
1830
          /* Assert that block is not in LRU ring. */
 
1831
          assert(!hash_link->block->next_used);
 
1832
          assert(!hash_link->block->prev_used);
 
1833
        }
 
1834
        /*
 
1835
          If we waited above, hash_link->block has been assigned by
 
1836
          link_block(). Otherwise it is still NULL. In the latter case
 
1837
          we need to grab a block from the LRU ring ourselves.
 
1838
        */
 
1839
        block= hash_link->block;
 
1840
        if (! block)
 
1841
        {
 
1842
          /* Select the last block from the LRU ring. */
 
1843
          block= keycache->used_last->next_used;
 
1844
          block->hits_left= init_hits_left;
 
1845
          block->last_hit_time= 0;
 
1846
          hash_link->block= block;
 
1847
          /*
 
1848
            Register a request on the block. This unlinks it from the
 
1849
            LRU ring and protects it against eviction.
 
1850
          */
 
1851
          assert(!block->requests);
 
1852
          reg_requests(keycache, block,1);
 
1853
          /*
 
1854
            We do not need to set block->status|= BLOCK_IN_EVICTION here
 
1855
            because we will set block->status|= BLOCK_IN_SWITCH
 
1856
            immediately without releasing the lock in between. This does
 
1857
            also support debugging. When looking at the block, one can
 
1858
            see if the block has been selected by link_block() after the
 
1859
            LRU ring was empty, or if it was grabbed directly from the
 
1860
            LRU ring in this branch.
 
1861
          */
 
1862
        }
 
1863
 
 
1864
        /*
 
1865
          If we had to wait above, there is a small chance that another
 
1866
          thread grabbed this block for the same file block already. But
 
1867
          in most cases the first condition is true.
 
1868
        */
 
1869
        if (block->hash_link != hash_link &&
 
1870
            ! (block->status & BLOCK_IN_SWITCH) )
 
1871
        {
 
1872
          /* this is a primary request for a new page */
 
1873
          block->status|= BLOCK_IN_SWITCH;
 
1874
 
 
1875
          if (block->status & BLOCK_CHANGED)
 
1876
          {
 
1877
            /* The block contains a dirty page - push it out of the cache */
 
1878
 
 
1879
            if (block->status & BLOCK_IN_FLUSH)
 
1880
            {
 
1881
              /*
 
1882
                The block is marked for flush. If we do not wait here,
 
1883
                it could happen that we write the block, reassign it to
 
1884
                another file block, then, before the new owner can read
 
1885
                the new file block, the flusher writes the cache block
 
1886
                (which still has the old contents) to the new file block!
 
1887
              */
 
1888
              wait_on_queue(&block->wqueue[COND_FOR_SAVED],
 
1889
                            &keycache->cache_lock);
 
1890
              /*
 
1891
                The block is marked BLOCK_IN_SWITCH. It should be left
 
1892
                alone except for reading. No free, no write.
 
1893
              */
 
1894
              assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
1895
              assert(!(block->status & (BLOCK_REASSIGNED |
 
1896
                                             BLOCK_CHANGED |
 
1897
                                             BLOCK_FOR_UPDATE)));
 
1898
            }
 
1899
            else
 
1900
            {
 
1901
              block->status|= BLOCK_IN_FLUSH | BLOCK_IN_FLUSHWRITE;
 
1902
              /*
 
1903
                BLOCK_IN_EVICTION may be true or not. Other flags must
 
1904
                have a fixed value.
 
1905
              */
 
1906
              assert((block->status & ~BLOCK_IN_EVICTION) ==
 
1907
                          (BLOCK_READ | BLOCK_IN_SWITCH |
 
1908
                           BLOCK_IN_FLUSH | BLOCK_IN_FLUSHWRITE |
 
1909
                           BLOCK_CHANGED | BLOCK_IN_USE));
 
1910
              assert(block->hash_link);
 
1911
 
 
1912
              keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
1913
              /*
 
1914
                The call is thread safe because only the current
 
1915
                thread might change the block->hash_link value
 
1916
              */
 
1917
              error= (pwrite(block->hash_link->file,
 
1918
                             block->buffer+block->offset,
 
1919
                             block->length - block->offset,
 
1920
                             block->hash_link->diskpos+ block->offset) == 0);
 
1921
              keycache_pthread_mutex_lock(&keycache->cache_lock);
 
1922
 
 
1923
              /* Block status must not have changed. */
 
1924
              assert((block->status & ~BLOCK_IN_EVICTION) ==
 
1925
                          (BLOCK_READ | BLOCK_IN_SWITCH |
 
1926
                           BLOCK_IN_FLUSH | BLOCK_IN_FLUSHWRITE |
 
1927
                           BLOCK_CHANGED | BLOCK_IN_USE));
 
1928
              keycache->global_cache_write++;
 
1929
            }
 
1930
          }
 
1931
 
 
1932
          block->status|= BLOCK_REASSIGNED;
 
1933
          /*
 
1934
            The block comes from the LRU ring. It must have a hash_link
 
1935
            assigned.
 
1936
          */
 
1937
          assert(block->hash_link);
 
1938
          if (block->hash_link)
 
1939
          {
 
1940
            /*
 
1941
              All pending requests for this page must be resubmitted.
 
1942
              This must be done before waiting for readers. They could
 
1943
              wait for the flush to complete. And we must also do it
 
1944
              after the wait. Flushers might try to free the block while
 
1945
              we wait. They would wait until the reassignment is
 
1946
              complete. Also the block status must reflect the correct
 
1947
              situation: The block is not changed nor in flush any more.
 
1948
              Note that we must not change the BLOCK_CHANGED flag
 
1949
              outside of link_to_file_list() so that it is always in the
 
1950
              correct queue and the *blocks_changed counters are
 
1951
              correct.
 
1952
            */
 
1953
            block->status&= ~(BLOCK_IN_FLUSH | BLOCK_IN_FLUSHWRITE);
 
1954
            link_to_file_list(keycache, block, block->hash_link->file, 1);
 
1955
            release_whole_queue(&block->wqueue[COND_FOR_SAVED]);
 
1956
            /*
 
1957
              The block is still assigned to its old hash_link.
 
1958
              Wait until all pending read requests
 
1959
              for this page are executed
 
1960
              (we could have avoided this waiting, if we had read
 
1961
              a page in the cache in a sweep, without yielding control)
 
1962
            */
 
1963
            wait_for_readers(keycache, block);
 
1964
            assert(block->hash_link && block->hash_link->block == block &&
 
1965
                        block->prev_changed);
 
1966
            /* The reader must not have been a writer. */
 
1967
            assert(!(block->status & BLOCK_CHANGED));
 
1968
 
 
1969
            /* Wake flushers that might have found the block in between. */
 
1970
            release_whole_queue(&block->wqueue[COND_FOR_SAVED]);
 
1971
 
 
1972
            /* Remove the hash link for the old file block from the hash. */
 
1973
            unlink_hash(keycache, block->hash_link);
 
1974
 
 
1975
            /*
 
1976
              For sanity checks link_to_file_list() asserts that block
 
1977
              and hash_link refer to each other. Hence we need to assign
 
1978
              the hash_link first, but then we would not know if it was
 
1979
              linked before. Hence we would not know if to unlink it. So
 
1980
              unlink it here and call link_to_file_list(..., false).
 
1981
            */
 
1982
            unlink_changed(block);
 
1983
          }
 
1984
          block->status= error ? BLOCK_ERROR : BLOCK_IN_USE ;
 
1985
          block->length= 0;
 
1986
          block->offset= keycache->key_cache_block_size;
 
1987
          block->hash_link= hash_link;
 
1988
          link_to_file_list(keycache, block, file, 0);
 
1989
          page_status= PAGE_TO_BE_READ;
 
1990
 
 
1991
          assert(block->hash_link->block == block);
 
1992
          assert(hash_link->block->hash_link == hash_link);
 
1993
        }
 
1994
        else
 
1995
        {
 
1996
          /*
 
1997
            Either (block->hash_link == hash_link),
 
1998
            or     (block->status & BLOCK_IN_SWITCH).
 
1999
 
 
2000
            This is for secondary requests for a new file block only.
 
2001
            Either it is already assigned to the new hash_link meanwhile
 
2002
            (if we had to wait due to empty LRU), or it is already in
 
2003
            eviction by another thread. Since this block has been
 
2004
            grabbed from the LRU ring and attached to this hash_link,
 
2005
            another thread cannot grab the same block from the LRU ring
 
2006
            anymore. If the block is in eviction already, it must become
 
2007
            attached to the same hash_link and as such destined for the
 
2008
            same file block.
 
2009
          */
 
2010
          page_status= (((block->hash_link == hash_link) &&
 
2011
                         (block->status & BLOCK_READ)) ?
 
2012
                        PAGE_READ : PAGE_WAIT_TO_BE_READ);
 
2013
        }
 
2014
      }
 
2015
    }
 
2016
    else
 
2017
    {
 
2018
      /*
 
2019
        Block is not NULL. This hash_link points to a block.
 
2020
        Either
 
2021
        - block not assigned to this hash_link (yet) or
 
2022
        - block assigned but not yet read from file,
 
2023
        or
 
2024
        - block assigned with valid (changed or unchanged) data and
 
2025
        - it will not be reassigned/freed.
 
2026
 
 
2027
        The first condition means hash_link points to a block in
 
2028
        eviction. This is not necessarily marked by BLOCK_IN_SWITCH yet.
 
2029
        But then it is marked BLOCK_IN_EVICTION. See the NOTE in
 
2030
        link_block(). In both cases it is destined for this hash_link
 
2031
        and its file block address. When this hash_link got its block
 
2032
        address, the block was removed from the LRU ring and cannot be
 
2033
        selected for eviction (for another hash_link) again.
 
2034
 
 
2035
        Register a request on the block. This is another protection
 
2036
        against eviction.
 
2037
      */
 
2038
      assert(((block->hash_link != hash_link) &&
 
2039
                   (block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH))) ||
 
2040
                  ((block->hash_link == hash_link) &&
 
2041
                   !(block->status & BLOCK_READ)) ||
 
2042
                  ((block->status & BLOCK_READ) &&
 
2043
                   !(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH))));
 
2044
      reg_requests(keycache, block, 1);
 
2045
      page_status= (((block->hash_link == hash_link) &&
 
2046
                     (block->status & BLOCK_READ)) ?
 
2047
                    PAGE_READ : PAGE_WAIT_TO_BE_READ);
 
2048
    }
 
2049
  }
 
2050
 
 
2051
  assert(page_status != -1);
 
2052
  /* Same assert basically, but be very sure. */
 
2053
  assert(block);
 
2054
  /* Assert that block has a request and is not in LRU ring. */
 
2055
  assert(block->requests);
 
2056
  assert(!block->next_used);
 
2057
  assert(!block->prev_used);
 
2058
  /* Assert that we return the correct block. */
 
2059
  assert((page_status == PAGE_WAIT_TO_BE_READ) ||
 
2060
              ((block->hash_link->file == file) &&
 
2061
               (block->hash_link->diskpos == filepos)));
 
2062
  *page_st=page_status;
 
2063
 
 
2064
  return(block);
 
2065
}
 
2066
 
 
2067
 
 
2068
/*
 
2069
  Read into a key cache block buffer from disk.
 
2070
 
 
2071
  SYNOPSIS
 
2072
 
 
2073
    read_block()
 
2074
      keycache            pointer to a key cache data structure
 
2075
      block               block to which buffer the data is to be read
 
2076
      read_length         size of data to be read
 
2077
      min_length          at least so much data must be read
 
2078
      primary             <-> the current thread will read the data
 
2079
 
 
2080
  RETURN VALUE
 
2081
    None
 
2082
 
 
2083
  NOTES.
 
2084
    The function either reads a page data from file to the block buffer,
 
2085
    or waits until another thread reads it. What page to read is determined
 
2086
    by a block parameter - reference to a hash link for this page.
 
2087
    If an error occurs THE BLOCK_ERROR bit is set in the block status.
 
2088
    We do not report error when the size of successfully read
 
2089
    portion is less than read_length, but not less than min_length.
 
2090
*/
 
2091
 
 
2092
static void read_block(KEY_CACHE *keycache,
 
2093
                       BLOCK_LINK *block, uint32_t read_length,
 
2094
                       uint32_t min_length, bool primary)
 
2095
{
 
2096
  uint32_t got_length;
 
2097
 
 
2098
  /* On entry cache_lock is locked */
 
2099
 
 
2100
  if (primary)
 
2101
  {
 
2102
    /*
 
2103
      This code is executed only by threads that submitted primary
 
2104
      requests. Until block->status contains BLOCK_READ, all other
 
2105
      request for the block become secondary requests. For a primary
 
2106
      request the block must be properly initialized.
 
2107
    */
 
2108
    assert(((block->status & ~BLOCK_FOR_UPDATE) == BLOCK_IN_USE));
 
2109
    assert((block->length == 0));
 
2110
    assert((block->offset == keycache->key_cache_block_size));
 
2111
    assert((block->requests > 0));
 
2112
 
 
2113
    keycache->global_cache_read++;
 
2114
    /* Page is not in buffer yet, is to be read from disk */
 
2115
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2116
    /*
 
2117
      Here other threads may step in and register as secondary readers.
 
2118
      They will register in block->wqueue[COND_FOR_REQUESTED].
 
2119
    */
 
2120
    got_length= pread(block->hash_link->file, block->buffer, read_length, block->hash_link->diskpos);
 
2121
    keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2122
    /*
 
2123
      The block can now have been marked for free (in case of
 
2124
      FLUSH_RELEASE). Otherwise the state must be unchanged.
 
2125
    */
 
2126
    assert(((block->status & ~(BLOCK_REASSIGNED |
 
2127
                                    BLOCK_FOR_UPDATE)) == BLOCK_IN_USE));
 
2128
    assert((block->length == 0));
 
2129
    assert((block->offset == keycache->key_cache_block_size));
 
2130
    assert((block->requests > 0));
 
2131
 
 
2132
    if (got_length < min_length)
 
2133
      block->status|= BLOCK_ERROR;
 
2134
    else
 
2135
    {
 
2136
      block->status|= BLOCK_READ;
 
2137
      block->length= got_length;
 
2138
      /*
 
2139
        Do not set block->offset here. If this block is marked
 
2140
        BLOCK_CHANGED later, we want to flush only the modified part. So
 
2141
        only a writer may set block->offset down from
 
2142
        keycache->key_cache_block_size.
 
2143
      */
 
2144
    }
 
2145
    /* Signal that all pending requests for this page now can be processed */
 
2146
    release_whole_queue(&block->wqueue[COND_FOR_REQUESTED]);
 
2147
  }
 
2148
  else
 
2149
  {
 
2150
    /*
 
2151
      This code is executed only by threads that submitted secondary
 
2152
      requests. At this point it could happen that the cache block is
 
2153
      not yet assigned to the hash_link for the requested file block.
 
2154
      But at awake from the wait this should be the case. Unfortunately
 
2155
      we cannot assert this here because we do not know the hash_link
 
2156
      for the requested file block nor the file and position. So we have
 
2157
      to assert this in the caller.
 
2158
    */
 
2159
    wait_on_queue(&block->wqueue[COND_FOR_REQUESTED], &keycache->cache_lock);
 
2160
  }
 
2161
}
 
2162
 
 
2163
 
 
2164
/*
294
2165
  Read a block of data from a cached file into a buffer;
295
2166
 
296
2167
  SYNOPSIS
319
2190
*/
320
2191
 
321
2192
unsigned char *key_cache_read(KEY_CACHE *keycache,
322
 
                      int file, internal::my_off_t filepos, int level,
 
2193
                      File file, my_off_t filepos, int level,
323
2194
                      unsigned char *buff, uint32_t length,
324
2195
                      uint32_t block_length,
325
2196
                      int return_buffer)
326
2197
{
327
2198
  (void)block_length;
328
2199
  (void)return_buffer;
329
 
  (void)level;
 
2200
  bool locked_and_incremented= false;
330
2201
  int error=0;
331
2202
  unsigned char *start= buff;
332
2203
 
333
 
  assert (! keycache->key_cache_inited);
334
 
 
 
2204
  if (keycache->key_cache_inited)
 
2205
  {
 
2206
    /* Key cache is used */
 
2207
    register BLOCK_LINK *block;
 
2208
    uint32_t read_length;
 
2209
    uint32_t offset;
 
2210
    uint32_t status;
 
2211
    int page_st;
 
2212
 
 
2213
    /*
 
2214
      When the key cache is once initialized, we use the cache_lock to
 
2215
      reliably distinguish the cases of normal operation, resizing, and
 
2216
      disabled cache. We always increment and decrement
 
2217
      'cnt_for_resize_op' so that a resizer can wait for pending I/O.
 
2218
    */
 
2219
    keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2220
    /*
 
2221
      Cache resizing has two phases: Flushing and re-initializing. In
 
2222
      the flush phase read requests are allowed to bypass the cache for
 
2223
      blocks not in the cache. find_key_block() returns NULL in this
 
2224
      case.
 
2225
 
 
2226
      After the flush phase new I/O requests must wait until the
 
2227
      re-initialization is done. The re-initialization can be done only
 
2228
      if no I/O request is in progress. The reason is that
 
2229
      key_cache_block_size can change. With enabled cache, I/O is done
 
2230
      in chunks of key_cache_block_size. Every chunk tries to use a
 
2231
      cache block first. If the block size changes in the middle, a
 
2232
      block could be missed and old data could be read.
 
2233
    */
 
2234
    while (keycache->in_resize && !keycache->resize_in_flush)
 
2235
      wait_on_queue(&keycache->resize_queue, &keycache->cache_lock);
 
2236
    /* Register the I/O for the next resize. */
 
2237
    inc_counter_for_resize_op(keycache);
 
2238
    locked_and_incremented= true;
 
2239
    /* Requested data may not always be aligned to cache blocks. */
 
2240
    offset= (uint) (filepos % keycache->key_cache_block_size);
 
2241
    /* Read data in key_cache_block_size increments */
 
2242
    do
 
2243
    {
 
2244
      /* Cache could be disabled in a later iteration. */
 
2245
 
 
2246
      if (!keycache->can_be_used)
 
2247
        goto no_key_cache;
 
2248
      /* Start reading at the beginning of the cache block. */
 
2249
      filepos-= offset;
 
2250
      /* Do not read beyond the end of the cache block. */
 
2251
      read_length= length;
 
2252
      set_if_smaller(read_length, keycache->key_cache_block_size-offset);
 
2253
      assert(read_length > 0);
 
2254
 
 
2255
      /* Request the cache block that matches file/pos. */
 
2256
      keycache->global_cache_r_requests++;
 
2257
      block=find_key_block(keycache, file, filepos, level, 0, &page_st);
 
2258
      if (!block)
 
2259
      {
 
2260
        /*
 
2261
          This happens only for requests submitted during key cache
 
2262
          resize. The block is not in the cache and shall not go in.
 
2263
          Read directly from file.
 
2264
        */
 
2265
        keycache->global_cache_read++;
 
2266
        keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2267
        error= (pread(file, (unsigned char*) buff, read_length, filepos + offset) == 0);
 
2268
        keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2269
        goto next_block;
 
2270
      }
 
2271
      if (!(block->status & BLOCK_ERROR))
 
2272
      {
 
2273
        if (page_st != PAGE_READ)
 
2274
        {
 
2275
          /* The requested page is to be read into the block buffer */
 
2276
          read_block(keycache, block,
 
2277
                     keycache->key_cache_block_size, read_length+offset,
 
2278
                     (bool)(page_st == PAGE_TO_BE_READ));
 
2279
          /*
 
2280
            A secondary request must now have the block assigned to the
 
2281
            requested file block. It does not hurt to check it for
 
2282
            primary requests too.
 
2283
          */
 
2284
          assert(keycache->can_be_used);
 
2285
          assert(block->hash_link->file == file);
 
2286
          assert(block->hash_link->diskpos == filepos);
 
2287
          assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2288
        }
 
2289
        else if (block->length < read_length + offset)
 
2290
        {
 
2291
          /*
 
2292
            Impossible if nothing goes wrong:
 
2293
            this could only happen if we are using a file with
 
2294
            small key blocks and are trying to read outside the file
 
2295
          */
 
2296
          my_errno= -1;
 
2297
          block->status|= BLOCK_ERROR;
 
2298
        }
 
2299
      }
 
2300
 
 
2301
      /* block status may have added BLOCK_ERROR in the above 'if'. */
 
2302
      if (!((status= block->status) & BLOCK_ERROR))
 
2303
      {
 
2304
        {
 
2305
          assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2306
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2307
          keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2308
#endif
 
2309
 
 
2310
          /* Copy data from the cache buffer */
 
2311
          memcpy(buff, block->buffer+offset, (size_t) read_length);
 
2312
 
 
2313
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2314
          keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2315
          assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2316
#endif
 
2317
        }
 
2318
      }
 
2319
 
 
2320
      remove_reader(block);
 
2321
 
 
2322
      /*
 
2323
         Link the block into the LRU ring if it's the last submitted
 
2324
         request for the block. This enables eviction for the block.
 
2325
           */
 
2326
      unreg_request(keycache, block, 1);
 
2327
 
 
2328
      if (status & BLOCK_ERROR)
 
2329
      {
 
2330
        error= 1;
 
2331
        break;
 
2332
      }
 
2333
 
 
2334
  next_block:
 
2335
      buff+= read_length;
 
2336
      filepos+= read_length+offset;
 
2337
      offset= 0;
 
2338
 
 
2339
    } while ((length-= read_length));
 
2340
    goto end;
 
2341
  }
 
2342
 
 
2343
no_key_cache:
 
2344
  /* Key cache is not used */
 
2345
 
 
2346
  keycache->global_cache_r_requests++;
 
2347
  keycache->global_cache_read++;
 
2348
 
 
2349
  if (locked_and_incremented)
 
2350
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
335
2351
  if (!pread(file, (unsigned char*) buff, length, filepos))
336
2352
    error= 1;
 
2353
  if (locked_and_incremented)
 
2354
    keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2355
 
 
2356
end:
 
2357
  if (locked_and_incremented)
 
2358
  {
 
2359
    dec_counter_for_resize_op(keycache);
 
2360
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2361
  }
337
2362
  return(error ? (unsigned char*) 0 : start);
338
2363
}
339
2364
 
359
2384
*/
360
2385
 
361
2386
int key_cache_insert(KEY_CACHE *keycache,
362
 
                     int file, internal::my_off_t filepos, int level,
 
2387
                     File file, my_off_t filepos, int level,
363
2388
                     unsigned char *buff, uint32_t length)
364
2389
{
365
 
  (void)file;
366
 
  (void)filepos;
367
 
  (void)level;
368
 
  (void)buff;
369
 
  (void)length;
370
 
 
371
 
  assert (!keycache->key_cache_inited);
372
 
  return 0;
 
2390
  int error= 0;
 
2391
 
 
2392
  if (keycache->key_cache_inited)
 
2393
  {
 
2394
    /* Key cache is used */
 
2395
    register BLOCK_LINK *block;
 
2396
    uint32_t read_length;
 
2397
    uint32_t offset;
 
2398
    int page_st;
 
2399
    bool locked_and_incremented= false;
 
2400
 
 
2401
    /*
 
2402
      When the keycache is once initialized, we use the cache_lock to
 
2403
      reliably distinguish the cases of normal operation, resizing, and
 
2404
      disabled cache. We always increment and decrement
 
2405
      'cnt_for_resize_op' so that a resizer can wait for pending I/O.
 
2406
    */
 
2407
    keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2408
    /*
 
2409
      We do not load index data into a disabled cache nor into an
 
2410
      ongoing resize.
 
2411
    */
 
2412
    if (!keycache->can_be_used || keycache->in_resize)
 
2413
        goto no_key_cache;
 
2414
    /* Register the pseudo I/O for the next resize. */
 
2415
    inc_counter_for_resize_op(keycache);
 
2416
    locked_and_incremented= true;
 
2417
    /* Loaded data may not always be aligned to cache blocks. */
 
2418
    offset= (uint) (filepos % keycache->key_cache_block_size);
 
2419
    /* Load data in key_cache_block_size increments. */
 
2420
    do
 
2421
    {
 
2422
      /* Cache could be disabled or resizing in a later iteration. */
 
2423
      if (!keycache->can_be_used || keycache->in_resize)
 
2424
        goto no_key_cache;
 
2425
      /* Start loading at the beginning of the cache block. */
 
2426
      filepos-= offset;
 
2427
      /* Do not load beyond the end of the cache block. */
 
2428
      read_length= length;
 
2429
      set_if_smaller(read_length, keycache->key_cache_block_size-offset);
 
2430
      assert(read_length > 0);
 
2431
 
 
2432
      /* The block has been read by the caller already. */
 
2433
      keycache->global_cache_read++;
 
2434
      /* Request the cache block that matches file/pos. */
 
2435
      keycache->global_cache_r_requests++;
 
2436
      block= find_key_block(keycache, file, filepos, level, 0, &page_st);
 
2437
      if (!block)
 
2438
      {
 
2439
        /*
 
2440
          This happens only for requests submitted during key cache
 
2441
          resize. The block is not in the cache and shall not go in.
 
2442
          Stop loading index data.
 
2443
        */
 
2444
        goto no_key_cache;
 
2445
      }
 
2446
      if (!(block->status & BLOCK_ERROR))
 
2447
      {
 
2448
        if ((page_st == PAGE_WAIT_TO_BE_READ) ||
 
2449
            ((page_st == PAGE_TO_BE_READ) &&
 
2450
             (offset || (read_length < keycache->key_cache_block_size))))
 
2451
        {
 
2452
          /*
 
2453
            Either
 
2454
 
 
2455
            this is a secondary request for a block to be read into the
 
2456
            cache. The block is in eviction. It is not yet assigned to
 
2457
            the requested file block (It does not point to the right
 
2458
            hash_link). So we cannot call remove_reader() on the block.
 
2459
            And we cannot access the hash_link directly here. We need to
 
2460
            wait until the assignment is complete. read_block() executes
 
2461
            the correct wait when called with primary == false.
 
2462
 
 
2463
            Or
 
2464
 
 
2465
            this is a primary request for a block to be read into the
 
2466
            cache and the supplied data does not fill the whole block.
 
2467
 
 
2468
            This function is called on behalf of a LOAD INDEX INTO CACHE
 
2469
            statement, which is a read-only task and allows other
 
2470
            readers. It is possible that a parallel running reader tries
 
2471
            to access this block. If it needs more data than has been
 
2472
            supplied here, it would report an error. To be sure that we
 
2473
            have all data in the block that is available in the file, we
 
2474
            read the block ourselves.
 
2475
 
 
2476
            Though reading again what the caller did read already is an
 
2477
            expensive operation, we need to do this for correctness.
 
2478
          */
 
2479
          read_block(keycache, block, keycache->key_cache_block_size,
 
2480
                     read_length + offset, (page_st == PAGE_TO_BE_READ));
 
2481
          /*
 
2482
            A secondary request must now have the block assigned to the
 
2483
            requested file block. It does not hurt to check it for
 
2484
            primary requests too.
 
2485
          */
 
2486
          assert(keycache->can_be_used);
 
2487
          assert(block->hash_link->file == file);
 
2488
          assert(block->hash_link->diskpos == filepos);
 
2489
          assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2490
        }
 
2491
        else if (page_st == PAGE_TO_BE_READ)
 
2492
        {
 
2493
          /*
 
2494
            This is a new block in the cache. If we come here, we have
 
2495
            data for the whole block.
 
2496
          */
 
2497
          assert(block->hash_link->requests);
 
2498
          assert(block->status & BLOCK_IN_USE);
 
2499
          assert((page_st == PAGE_TO_BE_READ) ||
 
2500
                      (block->status & BLOCK_READ));
 
2501
 
 
2502
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2503
          keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2504
          /*
 
2505
            Here other threads may step in and register as secondary readers.
 
2506
            They will register in block->wqueue[COND_FOR_REQUESTED].
 
2507
          */
 
2508
#endif
 
2509
 
 
2510
          /* Copy data from buff */
 
2511
          memcpy(block->buffer+offset, buff, (size_t) read_length);
 
2512
 
 
2513
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2514
          keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2515
          assert(block->status & BLOCK_IN_USE);
 
2516
          assert((page_st == PAGE_TO_BE_READ) ||
 
2517
                      (block->status & BLOCK_READ));
 
2518
#endif
 
2519
          /*
 
2520
            After the data is in the buffer, we can declare the block
 
2521
            valid. Now other threads do not need to register as
 
2522
            secondary readers any more. They can immediately access the
 
2523
            block.
 
2524
          */
 
2525
          block->status|= BLOCK_READ;
 
2526
          block->length= read_length+offset;
 
2527
          /*
 
2528
            Do not set block->offset here. If this block is marked
 
2529
            BLOCK_CHANGED later, we want to flush only the modified part. So
 
2530
            only a writer may set block->offset down from
 
2531
            keycache->key_cache_block_size.
 
2532
          */
 
2533
          /* Signal all pending requests. */
 
2534
          release_whole_queue(&block->wqueue[COND_FOR_REQUESTED]);
 
2535
        }
 
2536
        else
 
2537
        {
 
2538
          /*
 
2539
            page_st == PAGE_READ. The block is in the buffer. All data
 
2540
            must already be present. Blocks are always read with all
 
2541
            data available on file. Assert that the block does not have
 
2542
            less contents than the preloader supplies. If the caller has
 
2543
            data beyond block->length, it means that a file write has
 
2544
            been done while this block was in cache and not extended
 
2545
            with the new data. If the condition is met, we can simply
 
2546
            ignore the block.
 
2547
          */
 
2548
          assert((page_st == PAGE_READ) &&
 
2549
                      (read_length + offset <= block->length));
 
2550
        }
 
2551
 
 
2552
        /*
 
2553
          A secondary request must now have the block assigned to the
 
2554
          requested file block. It does not hurt to check it for primary
 
2555
          requests too.
 
2556
        */
 
2557
        assert(block->hash_link->file == file);
 
2558
        assert(block->hash_link->diskpos == filepos);
 
2559
        assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2560
      } /* end of if (!(block->status & BLOCK_ERROR)) */
 
2561
 
 
2562
 
 
2563
      remove_reader(block);
 
2564
 
 
2565
      /*
 
2566
         Link the block into the LRU ring if it's the last submitted
 
2567
         request for the block. This enables eviction for the block.
 
2568
      */
 
2569
      unreg_request(keycache, block, 1);
 
2570
 
 
2571
      error= (block->status & BLOCK_ERROR);
 
2572
 
 
2573
      if (error)
 
2574
        break;
 
2575
 
 
2576
      buff+= read_length;
 
2577
      filepos+= read_length+offset;
 
2578
      offset= 0;
 
2579
 
 
2580
    } while ((length-= read_length));
 
2581
 
 
2582
  no_key_cache:
 
2583
    if (locked_and_incremented)
 
2584
      dec_counter_for_resize_op(keycache);
 
2585
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2586
  }
 
2587
  return(error);
373
2588
}
374
2589
 
375
2590
 
403
2618
*/
404
2619
 
405
2620
int key_cache_write(KEY_CACHE *keycache,
406
 
                    int file, internal::my_off_t filepos, int level,
 
2621
                    File file, my_off_t filepos, int level,
407
2622
                    unsigned char *buff, uint32_t length,
408
2623
                    uint32_t block_length,
409
2624
                    int dont_write)
410
2625
{
411
2626
  (void)block_length;
412
 
  (void)level;
 
2627
  bool locked_and_incremented= false;
413
2628
  int error=0;
414
2629
 
415
2630
  if (!dont_write)
416
2631
  {
417
2632
    /* Not used in the server. */
418
2633
    /* Force writing from buff into disk. */
 
2634
    keycache->global_cache_w_requests++;
 
2635
    keycache->global_cache_write++;
419
2636
    if (pwrite(file, buff, length, filepos) == 0)
420
2637
      return(1);
421
2638
  }
422
2639
 
423
 
  assert (!keycache->key_cache_inited);
424
 
 
 
2640
  if (keycache->key_cache_inited)
 
2641
  {
 
2642
    /* Key cache is used */
 
2643
    register BLOCK_LINK *block;
 
2644
    uint32_t read_length;
 
2645
    uint32_t offset;
 
2646
    int page_st;
 
2647
 
 
2648
    /*
 
2649
      When the key cache is once initialized, we use the cache_lock to
 
2650
      reliably distinguish the cases of normal operation, resizing, and
 
2651
      disabled cache. We always increment and decrement
 
2652
      'cnt_for_resize_op' so that a resizer can wait for pending I/O.
 
2653
    */
 
2654
    keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2655
    /*
 
2656
      Cache resizing has two phases: Flushing and re-initializing. In
 
2657
      the flush phase write requests can modify dirty blocks that are
 
2658
      not yet in flush. Otherwise they are allowed to bypass the cache.
 
2659
      find_key_block() returns NULL in both cases (clean blocks and
 
2660
      non-cached blocks).
 
2661
 
 
2662
      After the flush phase new I/O requests must wait until the
 
2663
      re-initialization is done. The re-initialization can be done only
 
2664
      if no I/O request is in progress. The reason is that
 
2665
      key_cache_block_size can change. With enabled cache I/O is done in
 
2666
      chunks of key_cache_block_size. Every chunk tries to use a cache
 
2667
      block first. If the block size changes in the middle, a block
 
2668
      could be missed and data could be written below a cached block.
 
2669
    */
 
2670
    while (keycache->in_resize && !keycache->resize_in_flush)
 
2671
      wait_on_queue(&keycache->resize_queue, &keycache->cache_lock);
 
2672
    /* Register the I/O for the next resize. */
 
2673
    inc_counter_for_resize_op(keycache);
 
2674
    locked_and_incremented= true;
 
2675
    /* Requested data may not always be aligned to cache blocks. */
 
2676
    offset= (uint) (filepos % keycache->key_cache_block_size);
 
2677
    /* Write data in key_cache_block_size increments. */
 
2678
    do
 
2679
    {
 
2680
      /* Cache could be disabled in a later iteration. */
 
2681
      if (!keycache->can_be_used)
 
2682
        goto no_key_cache;
 
2683
      /* Start writing at the beginning of the cache block. */
 
2684
      filepos-= offset;
 
2685
      /* Do not write beyond the end of the cache block. */
 
2686
      read_length= length;
 
2687
      set_if_smaller(read_length, keycache->key_cache_block_size-offset);
 
2688
      assert(read_length > 0);
 
2689
 
 
2690
      /* Request the cache block that matches file/pos. */
 
2691
      keycache->global_cache_w_requests++;
 
2692
      block= find_key_block(keycache, file, filepos, level, 1, &page_st);
 
2693
      if (!block)
 
2694
      {
 
2695
        /*
 
2696
          This happens only for requests submitted during key cache
 
2697
          resize. The block is not in the cache and shall not go in.
 
2698
          Write directly to file.
 
2699
        */
 
2700
        if (dont_write)
 
2701
        {
 
2702
          /* Used in the server. */
 
2703
          keycache->global_cache_write++;
 
2704
          keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2705
          if (pwrite(file, (unsigned char*) buff, read_length, filepos + offset) == 0)
 
2706
            error=1;
 
2707
          keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2708
        }
 
2709
        goto next_block;
 
2710
      }
 
2711
      /*
 
2712
        Prevent block from flushing and from being selected for to be
 
2713
        freed. This must be set when we release the cache_lock.
 
2714
        However, we must not set the status of the block before it is
 
2715
        assigned to this file/pos.
 
2716
      */
 
2717
      if (page_st != PAGE_WAIT_TO_BE_READ)
 
2718
        block->status|= BLOCK_FOR_UPDATE;
 
2719
      /*
 
2720
        We must read the file block first if it is not yet in the cache
 
2721
        and we do not replace all of its contents.
 
2722
 
 
2723
        In cases where the cache block is big enough to contain (parts
 
2724
        of) index blocks of different indexes, our request can be
 
2725
        secondary (PAGE_WAIT_TO_BE_READ). In this case another thread is
 
2726
        reading the file block. If the read completes after us, it
 
2727
        overwrites our new contents with the old contents. So we have to
 
2728
        wait for the other thread to complete the read of this block.
 
2729
        read_block() takes care for the wait.
 
2730
      */
 
2731
      if (!(block->status & BLOCK_ERROR) &&
 
2732
          ((page_st == PAGE_TO_BE_READ &&
 
2733
            (offset || read_length < keycache->key_cache_block_size)) ||
 
2734
           (page_st == PAGE_WAIT_TO_BE_READ)))
 
2735
      {
 
2736
        read_block(keycache, block,
 
2737
                   offset + read_length >= keycache->key_cache_block_size?
 
2738
                   offset : keycache->key_cache_block_size,
 
2739
                   offset, (page_st == PAGE_TO_BE_READ));
 
2740
        assert(keycache->can_be_used);
 
2741
        assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2742
        /*
 
2743
          Prevent block from flushing and from being selected for to be
 
2744
          freed. This must be set when we release the cache_lock.
 
2745
          Here we set it in case we could not set it above.
 
2746
        */
 
2747
        block->status|= BLOCK_FOR_UPDATE;
 
2748
      }
 
2749
      /*
 
2750
        The block should always be assigned to the requested file block
 
2751
        here. It need not be BLOCK_READ when overwriting the whole block.
 
2752
      */
 
2753
      assert(block->hash_link->file == file);
 
2754
      assert(block->hash_link->diskpos == filepos);
 
2755
      assert(block->status & BLOCK_IN_USE);
 
2756
      assert((page_st == PAGE_TO_BE_READ) || (block->status & BLOCK_READ));
 
2757
      /*
 
2758
        The block to be written must not be marked BLOCK_REASSIGNED.
 
2759
        Otherwise it could be freed in dirty state or reused without
 
2760
        another flush during eviction. It must also not be in flush.
 
2761
        Otherwise the old contens may have been flushed already and
 
2762
        the flusher could clear BLOCK_CHANGED without flushing the
 
2763
        new changes again.
 
2764
      */
 
2765
      assert(!(block->status & BLOCK_REASSIGNED));
 
2766
 
 
2767
      while (block->status & BLOCK_IN_FLUSHWRITE)
 
2768
      {
 
2769
        /*
 
2770
          Another thread is flushing the block. It was dirty already.
 
2771
          Wait until the block is flushed to file. Otherwise we could
 
2772
          modify the buffer contents just while it is written to file.
 
2773
          An unpredictable file block contents would be the result.
 
2774
          While we wait, several things can happen to the block,
 
2775
          including another flush. But the block cannot be reassigned to
 
2776
          another hash_link until we release our request on it.
 
2777
        */
 
2778
        wait_on_queue(&block->wqueue[COND_FOR_SAVED], &keycache->cache_lock);
 
2779
        assert(keycache->can_be_used);
 
2780
        assert(block->status & (BLOCK_READ | BLOCK_IN_USE));
 
2781
        /* Still must not be marked for free. */
 
2782
        assert(!(block->status & BLOCK_REASSIGNED));
 
2783
        assert(block->hash_link && (block->hash_link->block == block));
 
2784
      }
 
2785
 
 
2786
      /*
 
2787
        We could perhaps release the cache_lock during access of the
 
2788
        data like in the other functions. Locks outside of the key cache
 
2789
        assure that readers and a writer do not access the same range of
 
2790
        data. Parallel accesses should happen only if the cache block
 
2791
        contains multiple index block(fragment)s. So different parts of
 
2792
        the buffer would be read/written. An attempt to flush during
 
2793
        memcpy() is prevented with BLOCK_FOR_UPDATE.
 
2794
      */
 
2795
      if (!(block->status & BLOCK_ERROR))
 
2796
      {
 
2797
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2798
        keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2799
#endif
 
2800
        memcpy(block->buffer+offset, buff, (size_t) read_length);
 
2801
 
 
2802
#if !defined(SERIALIZED_READ_FROM_CACHE)
 
2803
        keycache_pthread_mutex_lock(&keycache->cache_lock);
 
2804
#endif
 
2805
      }
 
2806
 
 
2807
      if (!dont_write)
 
2808
      {
 
2809
        /* Not used in the server. buff has been written to disk at start. */
 
2810
        if ((block->status & BLOCK_CHANGED) &&
 
2811
            (!offset && read_length >= keycache->key_cache_block_size))
 
2812
             link_to_file_list(keycache, block, block->hash_link->file, 1);
 
2813
      }
 
2814
      else if (! (block->status & BLOCK_CHANGED))
 
2815
        link_to_changed_list(keycache, block);
 
2816
      block->status|=BLOCK_READ;
 
2817
      /*
 
2818
        Allow block to be selected for to be freed. Since it is marked
 
2819
        BLOCK_CHANGED too, it won't be selected for to be freed without
 
2820
        a flush.
 
2821
      */
 
2822
      block->status&= ~BLOCK_FOR_UPDATE;
 
2823
      set_if_smaller(block->offset, offset);
 
2824
      set_if_bigger(block->length, read_length+offset);
 
2825
 
 
2826
      /* Threads may be waiting for the changes to be complete. */
 
2827
      release_whole_queue(&block->wqueue[COND_FOR_REQUESTED]);
 
2828
 
 
2829
      /*
 
2830
        If only a part of the cache block is to be replaced, and the
 
2831
        rest has been read from file, then the cache lock has been
 
2832
        released for I/O and it could be possible that another thread
 
2833
        wants to evict or free the block and waits for it to be
 
2834
        released. So we must not just decrement hash_link->requests, but
 
2835
        also wake a waiting thread.
 
2836
      */
 
2837
      remove_reader(block);
 
2838
 
 
2839
      /*
 
2840
         Link the block into the LRU ring if it's the last submitted
 
2841
         request for the block. This enables eviction for the block.
 
2842
      */
 
2843
      unreg_request(keycache, block, 1);
 
2844
 
 
2845
      if (block->status & BLOCK_ERROR)
 
2846
      {
 
2847
        error= 1;
 
2848
        break;
 
2849
      }
 
2850
 
 
2851
    next_block:
 
2852
      buff+= read_length;
 
2853
      filepos+= read_length+offset;
 
2854
      offset= 0;
 
2855
 
 
2856
    } while ((length-= read_length));
 
2857
    goto end;
 
2858
  }
 
2859
 
 
2860
no_key_cache:
425
2861
  /* Key cache is not used */
426
2862
  if (dont_write)
427
2863
  {
428
2864
    /* Used in the server. */
 
2865
    keycache->global_cache_w_requests++;
 
2866
    keycache->global_cache_write++;
 
2867
    if (locked_and_incremented)
 
2868
      keycache_pthread_mutex_unlock(&keycache->cache_lock);
429
2869
    if (pwrite(file, (unsigned char*) buff, length, filepos) == 0)
430
2870
      error=1;
 
2871
    if (locked_and_incremented)
 
2872
      keycache_pthread_mutex_lock(&keycache->cache_lock);
431
2873
  }
432
2874
 
 
2875
end:
 
2876
  if (locked_and_incremented)
 
2877
  {
 
2878
    dec_counter_for_resize_op(keycache);
 
2879
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
2880
  }
433
2881
  return(error);
434
2882
}
435
2883
 
436
2884
 
437
2885
/*
 
2886
  Free block.
 
2887
 
 
2888
  SYNOPSIS
 
2889
    free_block()
 
2890
      keycache          Pointer to a key cache data structure
 
2891
      block             Pointer to the block to free
 
2892
 
 
2893
  DESCRIPTION
 
2894
    Remove reference to block from hash table.
 
2895
    Remove block from the chain of clean blocks.
 
2896
    Add block to the free list.
 
2897
 
 
2898
  NOTE
 
2899
    Block must not be free (status == 0).
 
2900
    Block must not be in free_block_list.
 
2901
    Block must not be in the LRU ring.
 
2902
    Block must not be in eviction (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH).
 
2903
    Block must not be in free (BLOCK_REASSIGNED).
 
2904
    Block must not be in flush (BLOCK_IN_FLUSH).
 
2905
    Block must not be dirty (BLOCK_CHANGED).
 
2906
    Block must not be in changed_blocks (dirty) hash.
 
2907
    Block must be in file_blocks (clean) hash.
 
2908
    Block must refer to a hash_link.
 
2909
    Block must have a request registered on it.
 
2910
*/
 
2911
 
 
2912
static void free_block(KEY_CACHE *keycache, BLOCK_LINK *block)
 
2913
{
 
2914
  /*
 
2915
    Assert that the block is not free already. And that it is in a clean
 
2916
    state. Note that the block might just be assigned to a hash_link and
 
2917
    not yet read (BLOCK_READ may not be set here). In this case a reader
 
2918
    is registered in the hash_link and free_block() will wait for it
 
2919
    below.
 
2920
  */
 
2921
  assert((block->status & BLOCK_IN_USE) &&
 
2922
              !(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
2923
                                 BLOCK_REASSIGNED | BLOCK_IN_FLUSH |
 
2924
                                 BLOCK_CHANGED | BLOCK_FOR_UPDATE)));
 
2925
  /* Assert that the block is in a file_blocks chain. */
 
2926
  assert(block->prev_changed && *block->prev_changed == block);
 
2927
  /* Assert that the block is not in the LRU ring. */
 
2928
  assert(!block->next_used && !block->prev_used);
 
2929
  /*
 
2930
    IMHO the below condition (if()) makes no sense. I can't see how it
 
2931
    could be possible that free_block() is entered with a NULL hash_link
 
2932
    pointer. The only place where it can become NULL is in free_block()
 
2933
    (or before its first use ever, but for those blocks free_block() is
 
2934
    not called). I don't remove the conditional as it cannot harm, but
 
2935
    place an assert to confirm my hypothesis. Eventually the
 
2936
    condition (if()) can be removed.
 
2937
  */
 
2938
  assert(block->hash_link && block->hash_link->block == block);
 
2939
  if (block->hash_link)
 
2940
  {
 
2941
    /*
 
2942
      While waiting for readers to finish, new readers might request the
 
2943
      block. But since we set block->status|= BLOCK_REASSIGNED, they
 
2944
      will wait on block->wqueue[COND_FOR_SAVED]. They must be signalled
 
2945
      later.
 
2946
    */
 
2947
    block->status|= BLOCK_REASSIGNED;
 
2948
    wait_for_readers(keycache, block);
 
2949
    /*
 
2950
      The block must not have been freed by another thread. Repeat some
 
2951
      checks. An additional requirement is that it must be read now
 
2952
      (BLOCK_READ).
 
2953
    */
 
2954
    assert(block->hash_link && block->hash_link->block == block);
 
2955
    assert((block->status & (BLOCK_READ | BLOCK_IN_USE |
 
2956
                                  BLOCK_REASSIGNED)) &&
 
2957
                !(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
2958
                                   BLOCK_IN_FLUSH | BLOCK_CHANGED |
 
2959
                                   BLOCK_FOR_UPDATE)));
 
2960
    assert(block->prev_changed && *block->prev_changed == block);
 
2961
    assert(!block->prev_used);
 
2962
    /*
 
2963
      Unset BLOCK_REASSIGNED again. If we hand the block to an evicting
 
2964
      thread (through unreg_request() below), other threads must not see
 
2965
      this flag. They could become confused.
 
2966
    */
 
2967
    block->status&= ~BLOCK_REASSIGNED;
 
2968
    /*
 
2969
      Do not release the hash_link until the block is off all lists.
 
2970
      At least not if we hand it over for eviction in unreg_request().
 
2971
    */
 
2972
  }
 
2973
 
 
2974
  /*
 
2975
    Unregister the block request and link the block into the LRU ring.
 
2976
    This enables eviction for the block. If the LRU ring was empty and
 
2977
    threads are waiting for a block, then the block wil be handed over
 
2978
    for eviction immediately. Otherwise we will unlink it from the LRU
 
2979
    ring again, without releasing the lock in between. So decrementing
 
2980
    the request counter and updating statistics are the only relevant
 
2981
    operation in this case. Assert that there are no other requests
 
2982
    registered.
 
2983
  */
 
2984
  assert(block->requests == 1);
 
2985
  unreg_request(keycache, block, 0);
 
2986
  /*
 
2987
    Note that even without releasing the cache lock it is possible that
 
2988
    the block is immediately selected for eviction by link_block() and
 
2989
    thus not added to the LRU ring. In this case we must not touch the
 
2990
    block any more.
 
2991
  */
 
2992
  if (block->status & BLOCK_IN_EVICTION)
 
2993
    return;
 
2994
 
 
2995
  /* Here the block must be in the LRU ring. Unlink it again. */
 
2996
  assert(block->next_used && block->prev_used &&
 
2997
              *block->prev_used == block);
 
2998
  unlink_block(keycache, block);
 
2999
  if (block->temperature == BLOCK_WARM)
 
3000
    keycache->warm_blocks--;
 
3001
  block->temperature= BLOCK_COLD;
 
3002
 
 
3003
  /* Remove from file_blocks hash. */
 
3004
  unlink_changed(block);
 
3005
 
 
3006
  /* Remove reference to block from hash table. */
 
3007
  unlink_hash(keycache, block->hash_link);
 
3008
  block->hash_link= NULL;
 
3009
 
 
3010
  block->status= 0;
 
3011
  block->length= 0;
 
3012
  block->offset= keycache->key_cache_block_size;
 
3013
 
 
3014
  /* Enforced by unlink_changed(), but just to be sure. */
 
3015
  assert(!block->next_changed && !block->prev_changed);
 
3016
  /* Enforced by unlink_block(): not in LRU ring nor in free_block_list. */
 
3017
  assert(!block->next_used && !block->prev_used);
 
3018
  /* Insert the free block in the free list. */
 
3019
  block->next_used= keycache->free_block_list;
 
3020
  keycache->free_block_list= block;
 
3021
  /* Keep track of the number of currently unused blocks. */
 
3022
  keycache->blocks_unused++;
 
3023
 
 
3024
  /* All pending requests for this page must be resubmitted. */
 
3025
  release_whole_queue(&block->wqueue[COND_FOR_SAVED]);
 
3026
}
 
3027
 
 
3028
 
 
3029
static int cmp_sec_link(BLOCK_LINK **a, BLOCK_LINK **b)
 
3030
{
 
3031
  return (((*a)->hash_link->diskpos < (*b)->hash_link->diskpos) ? -1 :
 
3032
      ((*a)->hash_link->diskpos > (*b)->hash_link->diskpos) ? 1 : 0);
 
3033
}
 
3034
 
 
3035
 
 
3036
/*
 
3037
  Flush a portion of changed blocks to disk,
 
3038
  free used blocks if requested
 
3039
*/
 
3040
 
 
3041
static int flush_cached_blocks(KEY_CACHE *keycache,
 
3042
                               File file, BLOCK_LINK **cache,
 
3043
                               BLOCK_LINK **end,
 
3044
                               enum flush_type type)
 
3045
{
 
3046
  int error;
 
3047
  int last_errno= 0;
 
3048
  uint32_t count= (uint) (end-cache);
 
3049
 
 
3050
  /* Don't lock the cache during the flush */
 
3051
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
3052
  /*
 
3053
     As all blocks referred in 'cache' are marked by BLOCK_IN_FLUSH
 
3054
     we are guarunteed no thread will change them
 
3055
  */
 
3056
  my_qsort((unsigned char*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link);
 
3057
 
 
3058
  keycache_pthread_mutex_lock(&keycache->cache_lock);
 
3059
  /*
 
3060
    Note: Do not break the loop. We have registered a request on every
 
3061
    block in 'cache'. These must be unregistered by free_block() or
 
3062
    unreg_request().
 
3063
  */
 
3064
  for ( ; cache != end ; cache++)
 
3065
  {
 
3066
    BLOCK_LINK *block= *cache;
 
3067
    /*
 
3068
      If the block contents is going to be changed, we abandon the flush
 
3069
      for this block. flush_key_blocks_int() will restart its search and
 
3070
      handle the block properly.
 
3071
    */
 
3072
    if (!(block->status & BLOCK_FOR_UPDATE))
 
3073
    {
 
3074
      /* Blocks coming here must have a certain status. */
 
3075
      assert(block->hash_link);
 
3076
      assert(block->hash_link->block == block);
 
3077
      assert(block->hash_link->file == file);
 
3078
      assert((block->status & ~BLOCK_IN_EVICTION) ==
 
3079
                  (BLOCK_READ | BLOCK_IN_FLUSH | BLOCK_CHANGED | BLOCK_IN_USE));
 
3080
      block->status|= BLOCK_IN_FLUSHWRITE;
 
3081
      keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
3082
      error= (pwrite(file,
 
3083
                     block->buffer+block->offset,
 
3084
                     block->length - block->offset,
 
3085
                     block->hash_link->diskpos+ block->offset) == 0);
 
3086
      keycache_pthread_mutex_lock(&keycache->cache_lock);
 
3087
      keycache->global_cache_write++;
 
3088
      if (error)
 
3089
      {
 
3090
        block->status|= BLOCK_ERROR;
 
3091
        if (!last_errno)
 
3092
          last_errno= errno ? errno : -1;
 
3093
      }
 
3094
      block->status&= ~BLOCK_IN_FLUSHWRITE;
 
3095
      /* Block must not have changed status except BLOCK_FOR_UPDATE. */
 
3096
      assert(block->hash_link);
 
3097
      assert(block->hash_link->block == block);
 
3098
      assert(block->hash_link->file == file);
 
3099
      assert((block->status & ~(BLOCK_FOR_UPDATE | BLOCK_IN_EVICTION)) ==
 
3100
                  (BLOCK_READ | BLOCK_IN_FLUSH | BLOCK_CHANGED | BLOCK_IN_USE));
 
3101
      /*
 
3102
        Set correct status and link in right queue for free or later use.
 
3103
        free_block() must not see BLOCK_CHANGED and it may need to wait
 
3104
        for readers of the block. These should not see the block in the
 
3105
        wrong hash. If not freeing the block, we need to have it in the
 
3106
        right queue anyway.
 
3107
      */
 
3108
      link_to_file_list(keycache, block, file, 1);
 
3109
 
 
3110
    }
 
3111
    block->status&= ~BLOCK_IN_FLUSH;
 
3112
    /*
 
3113
      Let to proceed for possible waiting requests to write to the block page.
 
3114
      It might happen only during an operation to resize the key cache.
 
3115
    */
 
3116
    release_whole_queue(&block->wqueue[COND_FOR_SAVED]);
 
3117
    /* type will never be FLUSH_IGNORE_CHANGED here */
 
3118
    if (!(type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE) &&
 
3119
        !(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
3120
                           BLOCK_FOR_UPDATE)))
 
3121
    {
 
3122
      /*
 
3123
        Note that a request has been registered against the block in
 
3124
        flush_key_blocks_int().
 
3125
      */
 
3126
      free_block(keycache, block);
 
3127
    }
 
3128
    else
 
3129
    {
 
3130
      /*
 
3131
        Link the block into the LRU ring if it's the last submitted
 
3132
        request for the block. This enables eviction for the block.
 
3133
        Note that a request has been registered against the block in
 
3134
        flush_key_blocks_int().
 
3135
      */
 
3136
      unreg_request(keycache, block, 1);
 
3137
    }
 
3138
 
 
3139
  } /* end of for ( ; cache != end ; cache++) */
 
3140
  return last_errno;
 
3141
}
 
3142
 
 
3143
 
 
3144
/*
 
3145
  flush all key blocks for a file to disk, but don't do any mutex locks.
 
3146
 
 
3147
  SYNOPSIS
 
3148
    flush_key_blocks_int()
 
3149
      keycache            pointer to a key cache data structure
 
3150
      file                handler for the file to flush to
 
3151
      flush_type          type of the flush
 
3152
 
 
3153
  NOTES
 
3154
    This function doesn't do any mutex locks because it needs to be called both
 
3155
    from flush_key_blocks and flush_all_key_blocks (the later one does the
 
3156
    mutex lock in the resize_key_cache() function).
 
3157
 
 
3158
    We do only care about changed blocks that exist when the function is
 
3159
    entered. We do not guarantee that all changed blocks of the file are
 
3160
    flushed if more blocks change while this function is running.
 
3161
 
 
3162
  RETURN
 
3163
    0   ok
 
3164
    1  error
 
3165
*/
 
3166
 
 
3167
static int flush_key_blocks_int(KEY_CACHE *keycache,
 
3168
                                File file, enum flush_type type)
 
3169
{
 
3170
  BLOCK_LINK *cache_buff[FLUSH_CACHE],**cache;
 
3171
  int last_errno= 0;
 
3172
  int last_errcnt= 0;
 
3173
 
 
3174
  cache= cache_buff;
 
3175
  if (keycache->disk_blocks > 0 &&
 
3176
      (!my_disable_flush_key_blocks || type != FLUSH_KEEP))
 
3177
  {
 
3178
    /* Key cache exists and flush is not disabled */
 
3179
    int error= 0;
 
3180
    uint32_t count= FLUSH_CACHE;
 
3181
    BLOCK_LINK **pos,**end;
 
3182
    BLOCK_LINK *first_in_switch= NULL;
 
3183
    BLOCK_LINK *last_in_flush;
 
3184
    BLOCK_LINK *last_for_update;
 
3185
    BLOCK_LINK *last_in_switch;
 
3186
    BLOCK_LINK *block, *next;
 
3187
 
 
3188
    if (type != FLUSH_IGNORE_CHANGED)
 
3189
    {
 
3190
      /*
 
3191
         Count how many key blocks we have to cache to be able
 
3192
         to flush all dirty pages with minimum seek moves
 
3193
      */
 
3194
      count= 0;
 
3195
      for (block= keycache->changed_blocks[FILE_HASH(file)] ;
 
3196
           block ;
 
3197
           block= block->next_changed)
 
3198
      {
 
3199
        if ((block->hash_link->file == file) &&
 
3200
            !(block->status & BLOCK_IN_FLUSH))
 
3201
        {
 
3202
          count++;
 
3203
          assert(count<= keycache->blocks_used);
 
3204
        }
 
3205
      }
 
3206
      /*
 
3207
        Allocate a new buffer only if its bigger than the one we have.
 
3208
        Assure that we always have some entries for the case that new
 
3209
        changed blocks appear while we need to wait for something.
 
3210
      */
 
3211
      if ((count > FLUSH_CACHE) &&
 
3212
          !(cache= (BLOCK_LINK**) malloc(sizeof(BLOCK_LINK*)*count)))
 
3213
        cache= cache_buff;
 
3214
      /*
 
3215
        After a restart there could be more changed blocks than now.
 
3216
        So we should not let count become smaller than the fixed buffer.
 
3217
      */
 
3218
      if (cache == cache_buff)
 
3219
        count= FLUSH_CACHE;
 
3220
    }
 
3221
 
 
3222
    /* Retrieve the blocks and write them to a buffer to be flushed */
 
3223
restart:
 
3224
    last_in_flush= NULL;
 
3225
    last_for_update= NULL;
 
3226
    end= (pos= cache)+count;
 
3227
    for (block= keycache->changed_blocks[FILE_HASH(file)] ;
 
3228
         block ;
 
3229
         block= next)
 
3230
    {
 
3231
      next= block->next_changed;
 
3232
      if (block->hash_link->file == file)
 
3233
      {
 
3234
        if (!(block->status & (BLOCK_IN_FLUSH | BLOCK_FOR_UPDATE)))
 
3235
        {
 
3236
          /*
 
3237
            Note: The special handling of BLOCK_IN_SWITCH is obsolete
 
3238
            since we set BLOCK_IN_FLUSH if the eviction includes a
 
3239
            flush. It can be removed in a later version.
 
3240
          */
 
3241
          if (!(block->status & BLOCK_IN_SWITCH))
 
3242
          {
 
3243
            /*
 
3244
              We care only for the blocks for which flushing was not
 
3245
              initiated by another thread and which are not in eviction.
 
3246
              Registering a request on the block unlinks it from the LRU
 
3247
              ring and protects against eviction.
 
3248
            */
 
3249
            reg_requests(keycache, block, 1);
 
3250
            if (type != FLUSH_IGNORE_CHANGED)
 
3251
            {
 
3252
              /* It's not a temporary file */
 
3253
              if (pos == end)
 
3254
              {
 
3255
                /*
 
3256
                  This should happen relatively seldom. Remove the
 
3257
                  request because we won't do anything with the block
 
3258
                  but restart and pick it again in the next iteration.
 
3259
                */
 
3260
                unreg_request(keycache, block, 0);
 
3261
                /*
 
3262
                  This happens only if there is not enough
 
3263
                  memory for the big block
 
3264
                */
 
3265
                if ((error= flush_cached_blocks(keycache, file, cache,
 
3266
                                                end,type)))
 
3267
                {
 
3268
                  /* Do not loop infinitely trying to flush in vain. */
 
3269
                  if ((last_errno == error) && (++last_errcnt > 5))
 
3270
                    goto err;
 
3271
                  last_errno= error;
 
3272
                }
 
3273
                /*
 
3274
                  Restart the scan as some other thread might have changed
 
3275
                  the changed blocks chain: the blocks that were in switch
 
3276
                  state before the flush started have to be excluded
 
3277
                */
 
3278
                goto restart;
 
3279
              }
 
3280
              /*
 
3281
                Mark the block with BLOCK_IN_FLUSH in order not to let
 
3282
                other threads to use it for new pages and interfere with
 
3283
                our sequence of flushing dirty file pages. We must not
 
3284
                set this flag before actually putting the block on the
 
3285
                write burst array called 'cache'.
 
3286
              */
 
3287
              block->status|= BLOCK_IN_FLUSH;
 
3288
              /* Add block to the array for a write burst. */
 
3289
              *pos++= block;
 
3290
            }
 
3291
            else
 
3292
            {
 
3293
              /* It's a temporary file */
 
3294
              assert(!(block->status & BLOCK_REASSIGNED));
 
3295
 
 
3296
              /*
 
3297
                free_block() must not be called with BLOCK_CHANGED. Note
 
3298
                that we must not change the BLOCK_CHANGED flag outside of
 
3299
                link_to_file_list() so that it is always in the correct
 
3300
                queue and the *blocks_changed counters are correct.
 
3301
              */
 
3302
              link_to_file_list(keycache, block, file, 1);
 
3303
              if (!(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH)))
 
3304
              {
 
3305
                /* A request has been registered against the block above. */
 
3306
                free_block(keycache, block);
 
3307
              }
 
3308
              else
 
3309
              {
 
3310
                /*
 
3311
                  Link the block into the LRU ring if it's the last
 
3312
                  submitted request for the block. This enables eviction
 
3313
                  for the block. A request has been registered against
 
3314
                  the block above.
 
3315
                */
 
3316
                unreg_request(keycache, block, 1);
 
3317
              }
 
3318
            }
 
3319
          }
 
3320
          else
 
3321
          {
 
3322
            /*
 
3323
              Link the block into a list of blocks 'in switch'.
 
3324
 
 
3325
              WARNING: Here we introduce a place where a changed block
 
3326
              is not in the changed_blocks hash! This is acceptable for
 
3327
              a BLOCK_IN_SWITCH. Never try this for another situation.
 
3328
              Other parts of the key cache code rely on changed blocks
 
3329
              being in the changed_blocks hash.
 
3330
            */
 
3331
            unlink_changed(block);
 
3332
            link_changed(block, &first_in_switch);
 
3333
          }
 
3334
        }
 
3335
        else if (type != FLUSH_KEEP)
 
3336
        {
 
3337
          /*
 
3338
            During the normal flush at end of statement (FLUSH_KEEP) we
 
3339
            do not need to ensure that blocks in flush or update by
 
3340
            other threads are flushed. They will be flushed by them
 
3341
            later. In all other cases we must assure that we do not have
 
3342
            any changed block of this file in the cache when this
 
3343
            function returns.
 
3344
          */
 
3345
          if (block->status & BLOCK_IN_FLUSH)
 
3346
          {
 
3347
            /* Remember the last block found to be in flush. */
 
3348
            last_in_flush= block;
 
3349
          }
 
3350
          else
 
3351
          {
 
3352
            /* Remember the last block found to be selected for update. */
 
3353
            last_for_update= block;
 
3354
          }
 
3355
        }
 
3356
      }
 
3357
    }
 
3358
    if (pos != cache)
 
3359
    {
 
3360
      if ((error= flush_cached_blocks(keycache, file, cache, pos, type)))
 
3361
      {
 
3362
        /* Do not loop inifnitely trying to flush in vain. */
 
3363
        if ((last_errno == error) && (++last_errcnt > 5))
 
3364
          goto err;
 
3365
        last_errno= error;
 
3366
      }
 
3367
      /*
 
3368
        Do not restart here during the normal flush at end of statement
 
3369
        (FLUSH_KEEP). We have now flushed at least all blocks that were
 
3370
        changed when entering this function. In all other cases we must
 
3371
        assure that we do not have any changed block of this file in the
 
3372
        cache when this function returns.
 
3373
      */
 
3374
      if (type != FLUSH_KEEP)
 
3375
        goto restart;
 
3376
    }
 
3377
    if (last_in_flush)
 
3378
    {
 
3379
      /*
 
3380
        There are no blocks to be flushed by this thread, but blocks in
 
3381
        flush by other threads. Wait until one of the blocks is flushed.
 
3382
        Re-check the condition for last_in_flush. We may have unlocked
 
3383
        the cache_lock in flush_cached_blocks(). The state of the block
 
3384
        could have changed.
 
3385
      */
 
3386
      if (last_in_flush->status & BLOCK_IN_FLUSH)
 
3387
        wait_on_queue(&last_in_flush->wqueue[COND_FOR_SAVED],
 
3388
                      &keycache->cache_lock);
 
3389
      /* Be sure not to lose a block. They may be flushed in random order. */
 
3390
      goto restart;
 
3391
    }
 
3392
    if (last_for_update)
 
3393
    {
 
3394
      /*
 
3395
        There are no blocks to be flushed by this thread, but blocks for
 
3396
        update by other threads. Wait until one of the blocks is updated.
 
3397
        Re-check the condition for last_for_update. We may have unlocked
 
3398
        the cache_lock in flush_cached_blocks(). The state of the block
 
3399
        could have changed.
 
3400
      */
 
3401
      if (last_for_update->status & BLOCK_FOR_UPDATE)
 
3402
        wait_on_queue(&last_for_update->wqueue[COND_FOR_REQUESTED],
 
3403
                      &keycache->cache_lock);
 
3404
      /* The block is now changed. Flush it. */
 
3405
      goto restart;
 
3406
    }
 
3407
 
 
3408
    /*
 
3409
      Wait until the list of blocks in switch is empty. The threads that
 
3410
      are switching these blocks will relink them to clean file chains
 
3411
      while we wait and thus empty the 'first_in_switch' chain.
 
3412
    */
 
3413
    while (first_in_switch)
 
3414
    {
 
3415
      wait_on_queue(&first_in_switch->wqueue[COND_FOR_SAVED],
 
3416
                    &keycache->cache_lock);
 
3417
      /*
 
3418
        Do not restart here. We have flushed all blocks that were
 
3419
        changed when entering this function and were not marked for
 
3420
        eviction. Other threads have now flushed all remaining blocks in
 
3421
        the course of their eviction.
 
3422
      */
 
3423
    }
 
3424
 
 
3425
    if (! (type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE))
 
3426
    {
 
3427
      last_for_update= NULL;
 
3428
      last_in_switch= NULL;
 
3429
      uint32_t total_found= 0;
 
3430
      uint32_t found;
 
3431
 
 
3432
      /*
 
3433
        Finally free all clean blocks for this file.
 
3434
        During resize this may be run by two threads in parallel.
 
3435
      */
 
3436
      do
 
3437
      {
 
3438
        found= 0;
 
3439
        for (block= keycache->file_blocks[FILE_HASH(file)] ;
 
3440
             block ;
 
3441
             block= next)
 
3442
        {
 
3443
          /* Remember the next block. After freeing we cannot get at it. */
 
3444
          next= block->next_changed;
 
3445
 
 
3446
          /* Changed blocks cannot appear in the file_blocks hash. */
 
3447
          assert(!(block->status & BLOCK_CHANGED));
 
3448
          if (block->hash_link->file == file)
 
3449
          {
 
3450
            /* We must skip blocks that will be changed. */
 
3451
            if (block->status & BLOCK_FOR_UPDATE)
 
3452
            {
 
3453
              last_for_update= block;
 
3454
              continue;
 
3455
            }
 
3456
 
 
3457
            /*
 
3458
              We must not free blocks in eviction (BLOCK_IN_EVICTION |
 
3459
              BLOCK_IN_SWITCH) or blocks intended to be freed
 
3460
              (BLOCK_REASSIGNED).
 
3461
            */
 
3462
            if (!(block->status & (BLOCK_IN_EVICTION | BLOCK_IN_SWITCH |
 
3463
                                   BLOCK_REASSIGNED)))
 
3464
            {
 
3465
              struct st_hash_link *next_hash_link= NULL;
 
3466
              my_off_t            next_diskpos= 0;
 
3467
              File                next_file= 0;
 
3468
              uint32_t                next_status= 0;
 
3469
              uint32_t                hash_requests= 0;
 
3470
 
 
3471
              total_found++;
 
3472
              found++;
 
3473
              assert(found <= keycache->blocks_used);
 
3474
 
 
3475
              /*
 
3476
                Register a request. This unlinks the block from the LRU
 
3477
                ring and protects it against eviction. This is required
 
3478
                by free_block().
 
3479
              */
 
3480
              reg_requests(keycache, block, 1);
 
3481
 
 
3482
              /*
 
3483
                free_block() may need to wait for readers of the block.
 
3484
                This is the moment where the other thread can move the
 
3485
                'next' block from the chain. free_block() needs to wait
 
3486
                if there are requests for the block pending.
 
3487
              */
 
3488
              if (next && (hash_requests= block->hash_link->requests))
 
3489
              {
 
3490
                /* Copy values from the 'next' block and its hash_link. */
 
3491
                next_status=    next->status;
 
3492
                next_hash_link= next->hash_link;
 
3493
                next_diskpos=   next_hash_link->diskpos;
 
3494
                next_file=      next_hash_link->file;
 
3495
                assert(next == next_hash_link->block);
 
3496
              }
 
3497
 
 
3498
              free_block(keycache, block);
 
3499
              /*
 
3500
                If we had to wait and the state of the 'next' block
 
3501
                changed, break the inner loop. 'next' may no longer be
 
3502
                part of the current chain.
 
3503
 
 
3504
                We do not want to break the loop after every free_block(),
 
3505
                not even only after waits. The chain might be quite long
 
3506
                and contain blocks for many files. Traversing it again and
 
3507
                again to find more blocks for this file could become quite
 
3508
                inefficient.
 
3509
              */
 
3510
              if (next && hash_requests &&
 
3511
                  ((next_status    != next->status) ||
 
3512
                   (next_hash_link != next->hash_link) ||
 
3513
                   (next_file      != next_hash_link->file) ||
 
3514
                   (next_diskpos   != next_hash_link->diskpos) ||
 
3515
                   (next           != next_hash_link->block)))
 
3516
                break;
 
3517
            }
 
3518
            else
 
3519
            {
 
3520
              last_in_switch= block;
 
3521
            }
 
3522
          }
 
3523
        } /* end for block in file_blocks */
 
3524
      } while (found);
 
3525
 
 
3526
      /*
 
3527
        If any clean block has been found, we may have waited for it to
 
3528
        become free. In this case it could be possible that another clean
 
3529
        block became dirty. This is possible if the write request existed
 
3530
        before the flush started (BLOCK_FOR_UPDATE). Re-check the hashes.
 
3531
      */
 
3532
      if (total_found)
 
3533
        goto restart;
 
3534
 
 
3535
      /*
 
3536
        To avoid an infinite loop, wait until one of the blocks marked
 
3537
        for update is updated.
 
3538
      */
 
3539
      if (last_for_update)
 
3540
      {
 
3541
        /* We did not wait. Block must not have changed status. */
 
3542
        assert(last_for_update->status & BLOCK_FOR_UPDATE);
 
3543
        wait_on_queue(&last_for_update->wqueue[COND_FOR_REQUESTED],
 
3544
                      &keycache->cache_lock);
 
3545
        goto restart;
 
3546
      }
 
3547
 
 
3548
      /*
 
3549
        To avoid an infinite loop wait until one of the blocks marked
 
3550
        for eviction is switched.
 
3551
      */
 
3552
      if (last_in_switch)
 
3553
      {
 
3554
        /* We did not wait. Block must not have changed status. */
 
3555
        assert(last_in_switch->status & (BLOCK_IN_EVICTION |
 
3556
                                              BLOCK_IN_SWITCH |
 
3557
                                              BLOCK_REASSIGNED));
 
3558
        wait_on_queue(&last_in_switch->wqueue[COND_FOR_SAVED],
 
3559
                      &keycache->cache_lock);
 
3560
        goto restart;
 
3561
      }
 
3562
 
 
3563
    } /* if (! (type == FLUSH_KEEP || type == FLUSH_FORCE_WRITE)) */
 
3564
 
 
3565
  } /* if (keycache->disk_blocks > 0 */
 
3566
 
 
3567
err:
 
3568
  if (cache != cache_buff)
 
3569
    free((unsigned char*) cache);
 
3570
  if (last_errno)
 
3571
    errno=last_errno;                /* Return first error */
 
3572
  return(last_errno != 0);
 
3573
}
 
3574
 
 
3575
 
 
3576
/*
438
3577
  Flush all blocks for a file to disk
439
3578
 
440
3579
  SYNOPSIS
450
3589
*/
451
3590
 
452
3591
int flush_key_blocks(KEY_CACHE *keycache,
453
 
                     int file, enum flush_type type)
454
 
{
455
 
  (void)file;
456
 
  (void)type;
457
 
  assert (!keycache->key_cache_inited);
458
 
  return 0;
459
 
}
 
3592
                     File file, enum flush_type type)
 
3593
{
 
3594
  int res= 0;
 
3595
 
 
3596
  if (!keycache->key_cache_inited)
 
3597
    return(0);
 
3598
 
 
3599
  keycache_pthread_mutex_lock(&keycache->cache_lock);
 
3600
  /* While waiting for lock, keycache could have been ended. */
 
3601
  if (keycache->disk_blocks > 0)
 
3602
  {
 
3603
    inc_counter_for_resize_op(keycache);
 
3604
    res= flush_key_blocks_int(keycache, file, type);
 
3605
    dec_counter_for_resize_op(keycache);
 
3606
  }
 
3607
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
 
3608
  return(res);
 
3609
}
 
3610
 
 
3611
 
 
3612
/*
 
3613
  Flush all blocks in the key cache to disk.
 
3614
 
 
3615
  SYNOPSIS
 
3616
    flush_all_key_blocks()
 
3617
      keycache                  pointer to key cache root structure
 
3618
 
 
3619
  DESCRIPTION
 
3620
 
 
3621
    Flushing of the whole key cache is done in two phases.
 
3622
 
 
3623
    1. Flush all changed blocks, waiting for them if necessary. Loop
 
3624
    until there is no changed block left in the cache.
 
3625
 
 
3626
    2. Free all clean blocks. Normally this means free all blocks. The
 
3627
    changed blocks were flushed in phase 1 and became clean. However we
 
3628
    may need to wait for blocks that are read by other threads. While we
 
3629
    wait, a clean block could become changed if that operation started
 
3630
    before the resize operation started. To be safe we must restart at
 
3631
    phase 1.
 
3632
 
 
3633
    When we can run through the changed_blocks and file_blocks hashes
 
3634
    without finding a block any more, then we are done.
 
3635
 
 
3636
    Note that we hold keycache->cache_lock all the time unless we need
 
3637
    to wait for something.
 
3638
 
 
3639
  RETURN
 
3640
    0           OK
 
3641
    != 0        Error
 
3642
*/
 
3643
 
 
3644
static int flush_all_key_blocks(KEY_CACHE *keycache)
 
3645
{
 
3646
  BLOCK_LINK    *block;
 
3647
  uint32_t          total_found;
 
3648
  uint32_t          found;
 
3649
  uint32_t          idx;
 
3650
 
 
3651
  do
 
3652
  {
 
3653
    safe_mutex_assert_owner(&keycache->cache_lock);
 
3654
    total_found= 0;
 
3655
 
 
3656
    /*
 
3657
      Phase1: Flush all changed blocks, waiting for them if necessary.
 
3658
      Loop until there is no changed block left in the cache.
 
3659
    */
 
3660
    do
 
3661
    {
 
3662
      found= 0;
 
3663
      /* Step over the whole changed_blocks hash array. */
 
3664
      for (idx= 0; idx < CHANGED_BLOCKS_HASH; idx++)
 
3665
      {
 
3666
        /*
 
3667
          If an array element is non-empty, use the first block from its
 
3668
          chain to find a file for flush. All changed blocks for this
 
3669
          file are flushed. So the same block will not appear at this
 
3670
          place again with the next iteration. New writes for blocks are
 
3671
          not accepted during the flush. If multiple files share the
 
3672
          same hash bucket, one of them will be flushed per iteration
 
3673
          of the outer loop of phase 1.
 
3674
        */
 
3675
        if ((block= keycache->changed_blocks[idx]))
 
3676
        {
 
3677
          found++;
 
3678
          /*
 
3679
            Flush dirty blocks but do not free them yet. They can be used
 
3680
            for reading until all other blocks are flushed too.
 
3681
          */
 
3682
          if (flush_key_blocks_int(keycache, block->hash_link->file,
 
3683
                                   FLUSH_FORCE_WRITE))
 
3684
            return(1);
 
3685
        }
 
3686
      }
 
3687
 
 
3688
    } while (found);
 
3689
 
 
3690
    /*
 
3691
      Phase 2: Free all clean blocks. Normally this means free all
 
3692
      blocks. The changed blocks were flushed in phase 1 and became
 
3693
      clean. However we may need to wait for blocks that are read by
 
3694
      other threads. While we wait, a clean block could become changed
 
3695
      if that operation started before the resize operation started. To
 
3696
      be safe we must restart at phase 1.
 
3697
    */
 
3698
    do
 
3699
    {
 
3700
      found= 0;
 
3701
      /* Step over the whole file_blocks hash array. */
 
3702
      for (idx= 0; idx < CHANGED_BLOCKS_HASH; idx++)
 
3703
      {
 
3704
        /*
 
3705
          If an array element is non-empty, use the first block from its
 
3706
          chain to find a file for flush. All blocks for this file are
 
3707
          freed. So the same block will not appear at this place again
 
3708
          with the next iteration. If multiple files share the
 
3709
          same hash bucket, one of them will be flushed per iteration
 
3710
          of the outer loop of phase 2.
 
3711
        */
 
3712
        if ((block= keycache->file_blocks[idx]))
 
3713
        {
 
3714
          total_found++;
 
3715
          found++;
 
3716
          if (flush_key_blocks_int(keycache, block->hash_link->file,
 
3717
                                   FLUSH_RELEASE))
 
3718
            return(1);
 
3719
        }
 
3720
      }
 
3721
 
 
3722
    } while (found);
 
3723
 
 
3724
    /*
 
3725
      If any clean block has been found, we may have waited for it to
 
3726
      become free. In this case it could be possible that another clean
 
3727
      block became dirty. This is possible if the write request existed
 
3728
      before the resize started (BLOCK_FOR_UPDATE). Re-check the hashes.
 
3729
    */
 
3730
  } while (total_found);
 
3731
  return(0);
 
3732
}
 
3733
 
 
3734
 
 
3735
/*
 
3736
  Reset the counters of a key cache.
 
3737
 
 
3738
  SYNOPSIS
 
3739
    reset_key_cache_counters()
 
3740
 
 
3741
  DESCRIPTION
 
3742
   This procedure is used by process_key_caches() to reset the key_cache.
 
3743
 
 
3744
  RETURN
 
3745
    0 on success (always because it can't fail)
 
3746
*/
 
3747
 
 
3748
void reset_key_cache_counters()
 
3749
{
 
3750
  dflt_key_cache->global_blocks_changed= 0;   /* Key_blocks_not_flushed */
 
3751
  dflt_key_cache->global_cache_r_requests= 0; /* Key_read_requests */
 
3752
  dflt_key_cache->global_cache_read= 0;       /* Key_reads */
 
3753
  dflt_key_cache->global_cache_w_requests= 0; /* Key_write_requests */
 
3754
  dflt_key_cache->global_cache_write= 0;      /* Key_writes */
 
3755
}
 
3756
 
 
3757
#if defined(KEYCACHE_TIMEOUT)
 
3758
 
 
3759
 
 
3760
static inline
 
3761
unsigned int hash_link_number(HASH_LINK *hash_link, KEY_CACHE *keycache)
 
3762
{
 
3763
  return ((unsigned int) (((char*)hash_link-(char *) keycache->hash_link_root)/
 
3764
                  sizeof(HASH_LINK)));
 
3765
}
 
3766
 
 
3767
static inline
 
3768
unsigned int block_number(BLOCK_LINK *block, KEY_CACHE *keycache)
 
3769
{
 
3770
  return ((unsigned int) (((char*)block-(char *)keycache->block_root)/
 
3771
                  sizeof(BLOCK_LINK)));
 
3772
}
 
3773
 
 
3774
 
 
3775
#define KEYCACHE_DUMP_FILE  "keycache_dump.txt"
 
3776
#define MAX_QUEUE_LEN  100
 
3777
 
 
3778
 
 
3779
static void keycache_dump(KEY_CACHE *keycache)
 
3780
{
 
3781
  FILE *keycache_dump_file=fopen(KEYCACHE_DUMP_FILE, "w");
 
3782
  struct st_my_thread_var *last;
 
3783
  struct st_my_thread_var *thread;
 
3784
  BLOCK_LINK *block;
 
3785
  HASH_LINK *hash_link;
 
3786
  KEYCACHE_PAGE *page;
 
3787
  uint32_t i;
 
3788
 
 
3789
  fprintf(keycache_dump_file, "thread:%u\n", thread->id);
 
3790
 
 
3791
  i=0;
 
3792
  thread=last=waiting_for_hash_link.last_thread;
 
3793
  fprintf(keycache_dump_file, "queue of threads waiting for hash link\n");
 
3794
  if (thread)
 
3795
    do
 
3796
    {
 
3797
      thread=thread->next;
 
3798
      page= (KEYCACHE_PAGE *) thread->opt_info;
 
3799
      fprintf(keycache_dump_file,
 
3800
              "thread:%u, (file,filepos)=(%u,%lu)\n",
 
3801
              thread->id,(uint) page->file,(uint32_t) page->filepos);
 
3802
      if (++i == MAX_QUEUE_LEN)
 
3803
        break;
 
3804
    }
 
3805
    while (thread != last);
 
3806
 
 
3807
  i=0;
 
3808
  thread=last=waiting_for_block.last_thread;
 
3809
  fprintf(keycache_dump_file, "queue of threads waiting for block\n");
 
3810
  if (thread)
 
3811
    do
 
3812
    {
 
3813
      thread=thread->next;
 
3814
      hash_link= (HASH_LINK *) thread->opt_info;
 
3815
      fprintf(keycache_dump_file,
 
3816
              "thread:%u hash_link:%u (file,filepos)=(%u,%u)\n",
 
3817
              thread->id, (uint) hash_link_number(hash_link, keycache),
 
3818
              (uint) hash_link->file,(uint32_t) hash_link->diskpos);
 
3819
      if (++i == MAX_QUEUE_LEN)
 
3820
        break;
 
3821
    }
 
3822
    while (thread != last);
 
3823
 
 
3824
  for (i=0 ; i< keycache->blocks_used ; i++)
 
3825
  {
 
3826
    int j;
 
3827
    block= &keycache->block_root[i];
 
3828
    hash_link= block->hash_link;
 
3829
    fprintf(keycache_dump_file,
 
3830
            "block:%u hash_link:%d status:%x #requests=%u "
 
3831
            "waiting_for_readers:%d\n",
 
3832
            i, (int) (hash_link ? hash_link_number(hash_link, keycache) : -1),
 
3833
            block->status, block->requests, block->condvar ? 1 : 0);
 
3834
    for (j=0 ; j < 2; j++)
 
3835
    {
 
3836
      KEYCACHE_WQUEUE *wqueue=&block->wqueue[j];
 
3837
      thread= last= wqueue->last_thread;
 
3838
      fprintf(keycache_dump_file, "queue #%d\n", j);
 
3839
      if (thread)
 
3840
      {
 
3841
        do
 
3842
        {
 
3843
          thread=thread->next;
 
3844
          fprintf(keycache_dump_file,
 
3845
                  "thread:%u\n", thread->id);
 
3846
          if (++i == MAX_QUEUE_LEN)
 
3847
            break;
 
3848
        }
 
3849
        while (thread != last);
 
3850
      }
 
3851
    }
 
3852
  }
 
3853
  fprintf(keycache_dump_file, "LRU chain:");
 
3854
  block= keycache= used_last;
 
3855
  if (block)
 
3856
  {
 
3857
    do
 
3858
    {
 
3859
      block= block->next_used;
 
3860
      fprintf(keycache_dump_file,
 
3861
              "block:%u, ", block_number(block, keycache));
 
3862
    }
 
3863
    while (block != keycache->used_last);
 
3864
  }
 
3865
  fprintf(keycache_dump_file, "\n");
 
3866
 
 
3867
  fclose(keycache_dump_file);
 
3868
}
 
3869
 
 
3870
static int keycache_pthread_cond_wait(pthread_cond_t *cond,
 
3871
                                      pthread_mutex_t *mutex)
 
3872
{
 
3873
  int rc;
 
3874
  struct timeval  now;            /* time when we started waiting        */
 
3875
  struct timespec timeout;        /* timeout value for the wait function */
 
3876
  struct timezone tz;
 
3877
 
 
3878
  /* Get current time */
 
3879
  gettimeofday(&now, &tz);
 
3880
  /* Prepare timeout value */
 
3881
  timeout.tv_sec= now.tv_sec + KEYCACHE_TIMEOUT;
 
3882
 /*
 
3883
   timeval uses microseconds.
 
3884
   timespec uses nanoseconds.
 
3885
   1 nanosecond = 1000 micro seconds
 
3886
 */
 
3887
  timeout.tv_nsec= now.tv_usec * 1000;
 
3888
  rc= pthread_cond_timedwait(cond, mutex, &timeout);
 
3889
  if (rc == ETIMEDOUT || rc == ETIME)
 
3890
  {
 
3891
    keycache_dump();
 
3892
  }
 
3893
 
 
3894
  assert(rc != ETIMEDOUT);
 
3895
  return rc;
 
3896
}
 
3897
#endif /* defined(KEYCACHE_TIMEOUT) */