List:Internals« Previous MessageNext Message »
From:sanja Date:August 30 2005 1:25am
Subject:bk commit into 4.0 tree (bell:1.2142) BUG#12848
View as plain text  
Below is the list of changes that have just been committed into a local
4.0 repository of bell. When bell does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.2142 05/08/30 02:25:31 bell@stripped +2 -0
  support of concurent query cache resizing (BUG#12848)

  sql/sql_cache.h
    1.23 05/08/30 02:25:30 bell@stripped +1 -1
    removed uneed parameter (now it is always under guard mutex protection or called from
destruction)

  sql/sql_cache.cc
    1.66 05/08/30 02:25:29 bell@stripped +80 -35
    support of concurent query cache resizing:
      - resizing made atomic
      - check stack size after each quard mutex lock

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	bell
# Host:	sanja.is.com.ua
# Root:	/home/bell/mysql/bk/work-4.0

--- 1.65/sql/sql_cache.cc	2005-08-10 18:58:47 +03:00
+++ 1.66/sql/sql_cache.cc	2005-08-30 02:25:29 +03:00
@@ -563,13 +563,18 @@
 {
   DBUG_ENTER("query_cache_insert");
 
-#ifndef DBUG_OFF
-  // Check if we have called query_cache.wreck() (which disables the cache)
-  if (query_cache.query_cache_size == 0)
+  STRUCT_LOCK(&query_cache.structure_guard_mutex);
+  /*
+    It is very unlikely that following condition is TRUE (it is possible
+    only if other thread is resizing cache), so we check it only after guard
+    mutex lock
+  */
+  if (unlikely(query_cache.query_cache_size == 0))
+  {
+    STRUCT_UNLOCK(&query_cache.structure_guard_mutex);
     DBUG_VOID_RETURN;
-#endif
+  }
 
-  STRUCT_LOCK(&query_cache.structure_guard_mutex);
   Query_cache_block *query_block = ((Query_cache_block*)
 				    net->query_cache_query);
   if (query_block)
@@ -612,14 +617,20 @@
 {
   DBUG_ENTER("query_cache_abort");
 
-#ifndef DBUG_OFF
-  // Check if we have called query_cache.wreck() (which disables the cache)
-  if (query_cache.query_cache_size == 0)
-    DBUG_VOID_RETURN;
-#endif
   if (net->query_cache_query != 0)	// Quick check on unlocked structure
   {
     STRUCT_LOCK(&query_cache.structure_guard_mutex);
+    /*
+      It is very unlikely that following condition is TRUE (it is possible
+      only if other thread is resizing cache), so we check it only after guard
+      mutex lock
+    */
+    if (unlikely(query_cache.query_cache_size == 0))
+    {
+      STRUCT_UNLOCK(&query_cache.structure_guard_mutex);
+      DBUG_VOID_RETURN;
+    }
+
     Query_cache_block *query_block = ((Query_cache_block*)
 				       net->query_cache_query);
     if (query_block)			// Test if changed by other thread
@@ -641,14 +652,20 @@
 {
   DBUG_ENTER("query_cache_end_of_result");
 
-#ifndef DBUG_OFF
-  // Check if we have called query_cache.wreck() (which disables the cache)
-  if (query_cache.query_cache_size == 0) DBUG_VOID_RETURN;
-#endif
-
   if (net->query_cache_query != 0)	// Quick check on unlocked structure
   {
     STRUCT_LOCK(&query_cache.structure_guard_mutex);
+    /*
+      It is very unlikely that following condition is TRUE (it is possible
+      only if other thread is resizing cache), so we check it only after guard
+      mutex lock
+    */
+    if (unlikely(query_cache.query_cache_size == 0))
+    {
+      STRUCT_UNLOCK(&query_cache.structure_guard_mutex);
+      DBUG_VOID_RETURN;
+    }
+
     Query_cache_block *query_block = ((Query_cache_block*)
 				      net->query_cache_query);
     if (query_block)
@@ -728,9 +745,15 @@
   DBUG_ENTER("Query_cache::resize");
   DBUG_PRINT("qcache", ("from %lu to %lu",query_cache_size,
 			query_cache_size_arg));
-  free_cache(0);
+  if (!initialized)
+    init();
+  STRUCT_LOCK(&structure_guard_mutex);
+  if (query_cache_size > 0)
+    free_cache();
   query_cache_size= query_cache_size_arg;
-  DBUG_RETURN(::query_cache_size= init_cache());
+  ::query_cache_size= init_cache();
+  STRUCT_UNLOCK(&structure_guard_mutex);
+  DBUG_RETURN(::query_cache_size);
 }
 
 
@@ -1307,7 +1330,7 @@
   }
   else
   {
-    free_cache(1);
+    free_cache();
     pthread_mutex_destroy(&structure_guard_mutex);
     initialized = 0;
   }
@@ -1336,8 +1359,6 @@
   int align;
 
   DBUG_ENTER("Query_cache::init_cache");
-  if (!initialized)
-    init();
   approx_additional_data_size = (sizeof(Query_cache) +
 				 sizeof(gptr)*(def_query_hash_size+
 					       def_table_hash_size));
@@ -1395,14 +1416,9 @@
     goto err;
   query_cache_size -= additional_data_size;
 
-  STRUCT_LOCK(&structure_guard_mutex);
-
-  if (!(cache = (byte *)
-	 my_malloc_lock(query_cache_size+additional_data_size, MYF(0))))
-  {
-    STRUCT_UNLOCK(&structure_guard_mutex);
+  if (!(cache= (byte *)
+        my_malloc_lock(query_cache_size+additional_data_size, MYF(0))))
     goto err;
-  }
 
   DBUG_PRINT("qcache", ("cache length %lu, min unit %lu, %u bins",
 		      query_cache_size, min_allocation_unit, mem_bin_num));
@@ -1482,7 +1498,6 @@
 
   queries_in_cache = 0;
   queries_blocks = 0;
-  STRUCT_UNLOCK(&structure_guard_mutex);
   DBUG_RETURN(query_cache_size +
 	      additional_data_size + approx_additional_data_size);
 
@@ -1509,14 +1524,11 @@
 }
 
 
-void Query_cache::free_cache(my_bool destruction)
+void Query_cache::free_cache()
 {
   DBUG_ENTER("Query_cache::free_cache");
   if (query_cache_size > 0)
   {
-    if (!destruction)
-      STRUCT_LOCK(&structure_guard_mutex);
-
     flush_cache();
 #ifndef DBUG_OFF
     if (bins[0].free_blocks == 0)
@@ -1538,8 +1550,6 @@
     make_disabled();
     hash_free(&queries);
     hash_free(&tables);
-    if (!destruction)
-      STRUCT_UNLOCK(&structure_guard_mutex);
   }
   DBUG_VOID_RETURN;
 }
@@ -2149,7 +2159,19 @@
   }
 
   if (!under_guard)
+  {
     STRUCT_LOCK(&structure_guard_mutex);
+    /*
+      It is very unlikely that following condition is TRUE (it is possible
+      only if other thread is resizing cache), so we check it only after
+      guard mutex lock
+    */
+    if (unlikely(query_cache.query_cache_size == 0))
+    {
+      STRUCT_UNLOCK(&structure_guard_mutex);
+      DBUG_RETURN(0);
+    }
+  }
 
   /* Free old queries until we have enough memory to store this block */
   Query_cache_block *block;
@@ -2606,6 +2628,17 @@
 {
   DBUG_ENTER("Query_cache::pack_cache");
   STRUCT_LOCK(&structure_guard_mutex);
+  /*
+    It is very unlikely that following condition is TRUE (it is possible
+    only if other thread is resizing cache), so we check it only after
+    guard mutex lock
+  */
+  if (unlikely(query_cache_size == 0))
+  {
+    STRUCT_UNLOCK(&structure_guard_mutex);
+    DBUG_VOID_RETURN;
+  }
+
   DBUG_EXECUTE("check_querycache",query_cache.check_integrity(1););
 
   byte *border = 0;
@@ -2913,7 +2946,7 @@
   DBUG_ENTER("Query_cache::join_results");
 
   STRUCT_LOCK(&structure_guard_mutex);
-  if (queries_blocks != 0)
+  if (query_cache_size > 0 && queries_blocks != 0)
   {
     Query_cache_block *block = queries_blocks;
     do
@@ -3209,7 +3242,19 @@
     DBUG_RETURN(0);
   }
   if (!not_locked)
+  {
     STRUCT_LOCK(&structure_guard_mutex);
+    /*
+      It is very unlikely that following condition is TRUE (it is possible
+      only if other thread is resizing cache), so we check it only after
+      guard mutex lock
+    */
+    if (unlikely(query_cache_size == 0))
+    {
+      STRUCT_UNLOCK(&query_cache.structure_guard_mutex);
+      DBUG_RETURN(0);
+    }
+  }
 
   if (hash_check(&queries))
   {

--- 1.22/sql/sql_cache.h	2004-10-21 23:56:02 +03:00
+++ 1.23/sql/sql_cache.h	2005-08-30 02:25:30 +03:00
@@ -313,7 +313,7 @@
   void init();
   ulong init_cache();
   void make_disabled();
-  void free_cache(my_bool destruction);
+  void free_cache();
   Query_cache_block *write_block_data(ulong data_len, gptr data,
 				       ulong header_len,
 				       Query_cache_block::block_type type,
Thread
bk commit into 4.0 tree (bell:1.2142) BUG#12848sanja30 Aug