~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mf_keycache.c

Round 2 - removal custom KEYCACHE_DEBUG and THREAD_TRACE from keycache

Show diffs side-by-side

added added

removed removed

Lines of Context:
231
231
                                     (ulong) (f)) & (keycache->hash_entries-1))
232
232
#define FILE_HASH(f)                 ((uint) (f) & (CHANGED_BLOCKS_HASH-1))
233
233
 
234
 
#define DEFAULT_KEYCACHE_DEBUG_LOG  "keycache_debug.log"
235
 
 
236
 
#if defined(KEYCACHE_DEBUG) && ! defined(KEYCACHE_DEBUG_LOG)
237
 
#define KEYCACHE_DEBUG_LOG  DEFAULT_KEYCACHE_DEBUG_LOG
238
 
#endif
239
 
 
240
 
#if defined(KEYCACHE_DEBUG_LOG)
241
 
static FILE *keycache_debug_log=NULL;
242
 
static void keycache_debug_print _VARARGS((const char *fmt,...));
243
 
#define KEYCACHE_DEBUG_OPEN                                                   \
244
 
          if (!keycache_debug_log)                                            \
245
 
          {                                                                   \
246
 
            keycache_debug_log= fopen(KEYCACHE_DEBUG_LOG, "w");               \
247
 
            (void) setvbuf(keycache_debug_log, NULL, _IOLBF, BUFSIZ);         \
248
 
          }
249
 
 
250
 
#define KEYCACHE_DEBUG_CLOSE                                                  \
251
 
          if (keycache_debug_log)                                             \
252
 
          {                                                                   \
253
 
            fclose(keycache_debug_log);                                       \
254
 
            keycache_debug_log= 0;                                            \
255
 
          }
256
 
#else
257
 
#define KEYCACHE_DEBUG_OPEN
258
 
#define KEYCACHE_DEBUG_CLOSE
259
 
#endif /* defined(KEYCACHE_DEBUG_LOG) */
 
234
#define BLOCK_NUMBER(b)                                                       \
 
235
  ((uint) (((char*)(b)-(char *) keycache->block_root)/sizeof(BLOCK_LINK)))
 
236
#define HASH_LINK_NUMBER(h)                                                   \
 
237
  ((uint) (((char*)(h)-(char *) keycache->hash_link_root)/sizeof(HASH_LINK)))
260
238
 
261
239
#if defined(KEYCACHE_DEBUG_LOG) && defined(KEYCACHE_DEBUG)
262
 
#define KEYCACHE_DBUG_PRINT(l, m)                                             \
263
 
            { if (keycache_debug_log) fprintf(keycache_debug_log, "%s: ", l); \
264
 
              keycache_debug_print m; }
265
 
 
266
240
#define KEYCACHE_DBUG_ASSERT(a)                                               \
267
241
            { if (! (a) && keycache_debug_log) fclose(keycache_debug_log);    \
268
242
              assert(a); }
269
243
#else
270
 
#define KEYCACHE_DBUG_PRINT(l, m)  DBUG_PRINT(l, m)
271
244
#define KEYCACHE_DBUG_ASSERT(a)    DBUG_ASSERT(a)
272
245
#endif /* defined(KEYCACHE_DEBUG_LOG) && defined(KEYCACHE_DEBUG) */
273
246
 
274
 
#if defined(KEYCACHE_DEBUG) || !defined(DBUG_OFF)
275
 
static long keycache_thread_id;
276
 
#define KEYCACHE_THREAD_TRACE(l)                                              \
277
 
             KEYCACHE_DBUG_PRINT(l,("|thread %ld",keycache_thread_id))
278
 
 
279
 
#define KEYCACHE_THREAD_TRACE_BEGIN(l)                                        \
280
 
            { struct st_my_thread_var *thread_var= my_thread_var;             \
281
 
              keycache_thread_id= thread_var->id;                             \
282
 
              KEYCACHE_DBUG_PRINT(l,("[thread %ld",keycache_thread_id)) }
283
 
 
284
 
#define KEYCACHE_THREAD_TRACE_END(l)                                          \
285
 
            KEYCACHE_DBUG_PRINT(l,("]thread %ld",keycache_thread_id))
286
 
#else
287
 
#define KEYCACHE_THREAD_TRACE_BEGIN(l)
288
 
#define KEYCACHE_THREAD_TRACE_END(l)
289
 
#define KEYCACHE_THREAD_TRACE(l)
290
 
#endif /* defined(KEYCACHE_DEBUG) || !defined(DBUG_OFF) */
291
 
 
292
 
#define BLOCK_NUMBER(b)                                                       \
293
 
  ((uint) (((char*)(b)-(char *) keycache->block_root)/sizeof(BLOCK_LINK)))
294
 
#define HASH_LINK_NUMBER(h)                                                   \
295
 
  ((uint) (((char*)(h)-(char *) keycache->hash_link_root)/sizeof(HASH_LINK)))
296
 
 
297
247
#if (defined(KEYCACHE_TIMEOUT)) || defined(KEYCACHE_DEBUG)
298
248
static int keycache_pthread_cond_wait(pthread_cond_t *cond,
299
249
                                      pthread_mutex_t *mutex);
301
251
#define  keycache_pthread_cond_wait pthread_cond_wait
302
252
#endif
303
253
 
304
 
#if defined(KEYCACHE_DEBUG)
305
 
static int keycache_pthread_mutex_lock(pthread_mutex_t *mutex);
306
 
static void keycache_pthread_mutex_unlock(pthread_mutex_t *mutex);
307
 
static int keycache_pthread_cond_signal(pthread_cond_t *cond);
308
 
#else
309
254
#define keycache_pthread_mutex_lock pthread_mutex_lock
310
255
#define keycache_pthread_mutex_unlock pthread_mutex_unlock
311
256
#define keycache_pthread_cond_signal pthread_cond_signal
312
 
#endif /* defined(KEYCACHE_DEBUG) */
313
257
 
314
 
#if !defined(DBUG_OFF)
315
 
#if defined(inline)
316
 
#undef inline
317
 
#endif
318
 
#define inline  /* disabled inline for easier debugging */
319
258
static int fail_block(BLOCK_LINK *block);
320
259
static int fail_hlink(HASH_LINK *hlink);
321
260
static int cache_empty(KEY_CACHE *keycache);
322
 
#endif
323
261
 
324
262
static inline uint next_power(uint value)
325
263
{
361
299
  int error;
362
300
  DBUG_ASSERT(key_cache_block_size >= 512);
363
301
 
364
 
  KEYCACHE_DEBUG_OPEN;
365
302
  if (keycache->key_cache_inited && keycache->disk_blocks > 0)
366
303
  {
367
 
    DBUG_RETURN(0);
 
304
    return(0);
368
305
  }
369
306
 
370
307
  keycache->global_cache_w_requests= keycache->global_cache_r_requests= 0;
481
418
  }
482
419
 
483
420
  keycache->blocks= keycache->disk_blocks > 0 ? keycache->disk_blocks : 0;
484
 
  DBUG_RETURN((int) keycache->disk_blocks);
 
421
  return((int) keycache->disk_blocks);
485
422
 
486
423
err:
487
424
  error= my_errno;
499
436
  }
500
437
  my_errno= error;
501
438
  keycache->can_be_used= 0;
502
 
  DBUG_RETURN(0);
 
439
  return(0);
503
440
}
504
441
 
505
442
 
539
476
  int blocks;
540
477
 
541
478
  if (!keycache->key_cache_inited)
542
 
    DBUG_RETURN(keycache->disk_blocks);
 
479
    return(keycache->disk_blocks);
543
480
 
544
481
  if(key_cache_block_size == keycache->key_cache_block_size &&
545
482
     use_mem == keycache->key_cache_mem_size)
546
483
  {
547
484
    change_key_cache_param(keycache, division_limit, age_threshold);
548
 
    DBUG_RETURN(keycache->disk_blocks);
 
485
    return(keycache->disk_blocks);
549
486
  }
550
487
 
551
488
  keycache_pthread_mutex_lock(&keycache->cache_lock);
626
563
  release_whole_queue(&keycache->resize_queue);
627
564
 
628
565
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
629
 
  DBUG_RETURN(blocks);
 
566
  return(blocks);
630
567
}
631
568
 
632
569
 
678
615
    keycache->age_threshold=   (keycache->disk_blocks *
679
616
                                age_threshold / 100);
680
617
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
681
 
  DBUG_VOID_RETURN;
 
618
  return;
682
619
}
683
620
 
684
621
 
697
634
void end_key_cache(KEY_CACHE *keycache, my_bool cleanup)
698
635
{
699
636
  if (!keycache->key_cache_inited)
700
 
    DBUG_VOID_RETURN;
 
637
    return;
701
638
 
702
639
  if (keycache->disk_blocks > 0)
703
640
  {
717
654
  {
718
655
    pthread_mutex_destroy(&keycache->cache_lock);
719
656
    keycache->key_cache_inited= keycache->can_be_used= 0;
720
 
    KEYCACHE_DEBUG_CLOSE;
721
657
  }
722
 
  DBUG_VOID_RETURN;
 
658
  return;
723
659
} /* end_key_cache */
724
660
 
725
661
 
1118
1054
      probably easier to read.
1119
1055
    */
1120
1056
    block->status|= BLOCK_IN_EVICTION;
1121
 
    KEYCACHE_THREAD_TRACE("link_block: after signaling");
1122
1057
    return;
1123
1058
  }
1124
1059
  pins= hot ? &keycache->used_ins : &keycache->used_last;
1138
1073
    keycache->used_last= keycache->used_ins= block->next_used= block;
1139
1074
    block->prev_used= &block->next_used;
1140
1075
  }
1141
 
  KEYCACHE_THREAD_TRACE("link_block");
1142
1076
#if defined(KEYCACHE_DEBUG)
1143
1077
  keycache->blocks_available++;
1144
1078
  KEYCACHE_DBUG_ASSERT((ulong) keycache->blocks_available <=
1192
1126
  block->prev_used= NULL;
1193
1127
#endif
1194
1128
 
1195
 
  KEYCACHE_THREAD_TRACE("unlink_block");
1196
1129
#if defined(KEYCACHE_DEBUG)
1197
1130
  KEYCACHE_DBUG_ASSERT(keycache->blocks_available != 0);
1198
1131
  keycache->blocks_available--;
1551
1484
    unusable. This will be detected only after "goto restart".
1552
1485
  */
1553
1486
  if (!keycache->can_be_used)
1554
 
    DBUG_RETURN(0);
 
1487
    return(0);
1555
1488
 
1556
1489
  /*
1557
1490
    Find the hash_link for the requested file block (file, filepos). We
1616
1549
        */
1617
1550
        hash_link->requests--;
1618
1551
        unlink_hash(keycache, hash_link);
1619
 
        DBUG_RETURN(0);
 
1552
        return(0);
1620
1553
      }
1621
1554
 
1622
1555
      /*
1713
1646
      DBUG_ASSERT((hash_link->file == file) &&
1714
1647
                  (hash_link->diskpos == filepos) &&
1715
1648
                  (block->hash_link == hash_link));
1716
 
      DBUG_RETURN(block);
 
1649
      return(block);
1717
1650
    }
1718
1651
 
1719
1652
    /*
1762
1695
      DBUG_ASSERT((hash_link->file == file) &&
1763
1696
                  (hash_link->diskpos == filepos) &&
1764
1697
                  (block->hash_link == hash_link));
1765
 
      DBUG_RETURN(block);
 
1698
      return(block);
1766
1699
    }
1767
1700
 
1768
1701
    /*
1830
1763
               (block->hash_link->file == file) &&
1831
1764
               (block->hash_link->diskpos == filepos));
1832
1765
    }
1833
 
    DBUG_RETURN(0);
 
1766
    return(0);
1834
1767
  }
1835
1768
 
1836
1769
  if (page_status == PAGE_READ &&
2206
2139
               (block->hash_link->diskpos == filepos)));
2207
2140
  *page_st=page_status;
2208
2141
 
2209
 
  KEYCACHE_THREAD_TRACE("find_key_block:end");
2210
 
  DBUG_RETURN(block);
 
2142
  return(block);
2211
2143
}
2212
2144
 
2213
2145
 
2243
2175
 
2244
2176
  /* On entry cache_lock is locked */
2245
2177
 
2246
 
  KEYCACHE_THREAD_TRACE("read_block");
2247
2178
  if (primary)
2248
2179
  {
2249
2180
    /*
2511
2442
    dec_counter_for_resize_op(keycache);
2512
2443
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
2513
2444
  }
2514
 
  DBUG_RETURN(error ? (uchar*) 0 : start);
 
2445
  return(error ? (uchar*) 0 : start);
2515
2446
}
2516
2447
 
2517
2448
 
2739
2670
      dec_counter_for_resize_op(keycache);
2740
2671
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
2741
2672
  }
2742
 
  DBUG_RETURN(error);
 
2673
  return(error);
2743
2674
}
2744
2675
 
2745
2676
 
2789
2720
    keycache->global_cache_w_requests++;
2790
2721
    keycache->global_cache_write++;
2791
2722
    if (pwrite(file, buff, length, filepos) == 0)
2792
 
      DBUG_RETURN(1);
 
2723
      return(1);
2793
2724
    /* purecov: end */
2794
2725
  }
2795
2726
 
3037
2968
    dec_counter_for_resize_op(keycache);
3038
2969
    keycache_pthread_mutex_unlock(&keycache->cache_lock);
3039
2970
  }
3040
 
  DBUG_RETURN(error);
 
2971
  return(error);
3041
2972
}
3042
2973
 
3043
2974
 
3742
3673
    my_free((uchar*) cache, MYF(0));
3743
3674
  if (last_errno)
3744
3675
    errno=last_errno;                /* Return first error */
3745
 
  DBUG_RETURN(last_errno != 0);
 
3676
  return(last_errno != 0);
3746
3677
}
3747
3678
 
3748
3679
 
3767
3698
  int res= 0;
3768
3699
 
3769
3700
  if (!keycache->key_cache_inited)
3770
 
    DBUG_RETURN(0);
 
3701
    return(0);
3771
3702
 
3772
3703
  keycache_pthread_mutex_lock(&keycache->cache_lock);
3773
3704
  /* While waiting for lock, keycache could have been ended. */
3778
3709
    dec_counter_for_resize_op(keycache);
3779
3710
  }
3780
3711
  keycache_pthread_mutex_unlock(&keycache->cache_lock);
3781
 
  DBUG_RETURN(res);
 
3712
  return(res);
3782
3713
}
3783
3714
 
3784
3715
 
3854
3785
          */
3855
3786
          if (flush_key_blocks_int(keycache, block->hash_link->file,
3856
3787
                                   FLUSH_FORCE_WRITE))
3857
 
            DBUG_RETURN(1);
 
3788
            return(1);
3858
3789
        }
3859
3790
      }
3860
3791
 
3888
3819
          found++;
3889
3820
          if (flush_key_blocks_int(keycache, block->hash_link->file,
3890
3821
                                   FLUSH_RELEASE))
3891
 
            DBUG_RETURN(1);
 
3822
            return(1);
3892
3823
        }
3893
3824
      }
3894
3825
 
3911
3842
  }
3912
3843
#endif
3913
3844
 
3914
 
  DBUG_RETURN(0);
 
3845
  return(0);
3915
3846
}
3916
3847
 
3917
3848
 
3936
3867
{
3937
3868
  if (!key_cache->key_cache_inited)
3938
3869
  {
3939
 
    DBUG_RETURN(0);
 
3870
    return(0);
3940
3871
  }
3941
3872
  key_cache->global_blocks_changed= 0;   /* Key_blocks_not_flushed */
3942
3873
  key_cache->global_cache_r_requests= 0; /* Key_read_requests */
3943
3874
  key_cache->global_cache_read= 0;       /* Key_reads */
3944
3875
  key_cache->global_cache_w_requests= 0; /* Key_write_requests */
3945
3876
  key_cache->global_cache_write= 0;      /* Key_writes */
3946
 
  DBUG_RETURN(0);
 
3877
  return(0);
3947
3878
}
3948
3879
 
3949
3880
#if defined(KEYCACHE_TIMEOUT)
4068
3999
   1 nanosecond = 1000 micro seconds
4069
4000
 */
4070
4001
  timeout.tv_nsec= now.tv_usec * 1000;
4071
 
  KEYCACHE_THREAD_TRACE_END("started waiting");
4072
4002
#if defined(KEYCACHE_DEBUG)
4073
4003
  cnt++;
4074
4004
  if (cnt % 100 == 0)
4076
4006
    fflush(keycache_debug_log);
4077
4007
#endif
4078
4008
  rc= pthread_cond_timedwait(cond, mutex, &timeout);
4079
 
  KEYCACHE_THREAD_TRACE_BEGIN("finished waiting");
4080
4009
  if (rc == ETIMEDOUT || rc == ETIME)
4081
4010
  {
4082
4011
#if defined(KEYCACHE_DEBUG)
4100
4029
                                      pthread_mutex_t *mutex)
4101
4030
{
4102
4031
  int rc;
4103
 
  KEYCACHE_THREAD_TRACE_END("started waiting");
4104
4032
  rc= pthread_cond_wait(cond, mutex);
4105
 
  KEYCACHE_THREAD_TRACE_BEGIN("finished waiting");
4106
4033
  return rc;
4107
4034
}
4108
4035
#endif
4115
4042
{
4116
4043
  int rc;
4117
4044
  rc= pthread_mutex_lock(mutex);
4118
 
  KEYCACHE_THREAD_TRACE_BEGIN("");
4119
4045
  return rc;
4120
4046
}
4121
4047
 
4122
4048
 
4123
4049
static void keycache_pthread_mutex_unlock(pthread_mutex_t *mutex)
4124
4050
{
4125
 
  KEYCACHE_THREAD_TRACE_END("");
4126
4051
  pthread_mutex_unlock(mutex);
4127
4052
}
4128
4053
 
4130
4055
static int keycache_pthread_cond_signal(pthread_cond_t *cond)
4131
4056
{
4132
4057
  int rc;
4133
 
  KEYCACHE_THREAD_TRACE("signal");
4134
4058
  rc= pthread_cond_signal(cond);
4135
4059
  return rc;
4136
4060
}