~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mf_keycache.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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