sql_test.cc 11.1 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Write some debug info */


#include "mysql_priv.h"
#include "sql_select.h"
#include <hash.h>
unknown's avatar
unknown committed
24
#include <thr_alarm.h>
25
#include <malloc.h>
unknown's avatar
unknown committed
26 27 28 29

/* Intern key cache variables */
extern "C" pthread_mutex_t THR_LOCK_keycache;

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static const char *lock_descriptions[] =
{
  "No lock",
  "Low priority read lock",
  "Shared Read lock",
  "High priority read lock",
  "Read lock  without concurrent inserts",
  "Write lock that allows other writers",
  "Write lock, but allow reading",
  "Concurrent insert lock",
  "Lock Used by delayed insert",
  "Low priority write lock",
  "High priority write lock",
  "Highest priority write lock"
};


unknown's avatar
unknown committed
47 48 49 50 51 52 53 54
#ifndef DBUG_OFF

void
print_where(COND *cond,const char *info)
{
  if (cond)
  {
    char buff[256];
unknown's avatar
unknown committed
55
    String str(buff,(uint32) sizeof(buff), system_charset_info);
unknown's avatar
unknown committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    str.length(0);
    cond->print(&str);
    str.append('\0');
    DBUG_LOCK_FILE;
    (void) fprintf(DBUG_FILE,"\nWHERE:(%s) ",info);
    (void) fputs(str.ptr(),DBUG_FILE);
    (void) fputc('\n',DBUG_FILE);
    DBUG_UNLOCK_FILE;
  }
}
	/* This is for debugging purposes */


void print_cached_tables(void)
{
  uint idx,count,unused;
  TABLE *start_link,*lnk;

  VOID(pthread_mutex_lock(&LOCK_open));
unknown's avatar
unknown committed
75
  puts("DB             Table                            Version  Thread  L.thread  Open  Lock");
unknown's avatar
unknown committed
76 77 78 79

  for (idx=unused=0 ; idx < open_cache.records ; idx++)
  {
    TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
unknown's avatar
unknown committed
80
    printf("%-14.14s %-32s%6ld%8ld%10ld%6d  %s\n",
unknown's avatar
unknown committed
81 82 83
	   entry->table_cache_key,entry->real_name,entry->version,
	   entry->in_use ? entry->in_use->thread_id : 0L,
	   entry->in_use ? entry->in_use->dbug_thread_id : 0L,
unknown's avatar
unknown committed
84
	   entry->db_stat ? 1 : 0, entry->in_use ? lock_descriptions[(int)entry->reginfo.lock_type] : "Not in use");
unknown's avatar
unknown committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (!entry->in_use)
      unused++;
  }
  count=0;
  if ((start_link=lnk=unused_tables))
  {
    do
    {
      if (lnk != lnk->next->prev || lnk != lnk->prev->next)
      {
	printf("unused_links isn't linked properly\n");
	return;
      }
    } while (count++ < open_cache.records && (lnk=lnk->next) != start_link);
    if (lnk != start_link)
    {
      printf("Unused_links aren't connected\n");
    }
  }
  if (count != unused)
105
    printf("Unused_links (%d) doesn't match open_cache: %d\n", count,unused);
unknown's avatar
unknown committed
106 107 108 109 110 111 112 113 114
  printf("\nCurrent refresh version: %ld\n",refresh_version);
  if (hash_check(&open_cache))
    printf("Error: File hash table is corrupted\n");
  fflush(stdout);
  VOID(pthread_mutex_unlock(&LOCK_open));
  return;
}


115
void TEST_filesort(SORT_FIELD *sortorder,uint s_length)
unknown's avatar
unknown committed
116 117
{
  char buff[256],buff2[256];
unknown's avatar
unknown committed
118 119
  String str(buff,sizeof(buff),system_charset_info);
  String out(buff2,sizeof(buff2),system_charset_info);
unknown's avatar
unknown committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  const char *sep;
  DBUG_ENTER("TEST_filesort");

  out.length(0);
  for (sep=""; s_length-- ; sortorder++, sep=" ")
  {
    out.append(sep);
    if (sortorder->reverse)
      out.append('-');
    if (sortorder->field)
    {
      if (sortorder->field->table_name)
      {
	out.append(sortorder->field->table_name);
	out.append('.');
      }
      out.append(sortorder->field->field_name ? sortorder->field->field_name:
		 "tmp_table_column");
    }
    else
    {
      str.length(0);
      sortorder->item->print(&str);
      out.append(str);
    }
  }
  out.append('\0');				// Purify doesn't like c_ptr()
  DBUG_LOCK_FILE;
  VOID(fputs("\nInfo about FILESORT\n",DBUG_FILE));
  fprintf(DBUG_FILE,"Sortorder: %s\n",out.ptr());
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}


void
TEST_join(JOIN *join)
{
  uint i,ref;
  DBUG_ENTER("TEST_join");

  DBUG_LOCK_FILE;
  VOID(fputs("\nInfo about JOIN\n",DBUG_FILE));
  for (i=0 ; i < join->tables ; i++)
  {
    JOIN_TAB *tab=join->join_tab+i;
    TABLE *form=tab->table;
    fprintf(DBUG_FILE,"%-16.16s  type: %-7s  q_keys: %4d  refs: %d  key: %d  len: %d\n",
	    form->table_name,
	    join_type_str[tab->type],
	    tab->keys,
	    tab->ref.key_parts,
	    tab->ref.key,
	    tab->ref.key_length);
    if (tab->select)
    {
      if (tab->use_quick == 2)
	fprintf(DBUG_FILE,
		"                  quick select checked for each record (keys: %d)\n",
		(int) tab->select->quick_keys);
      else if (tab->select->quick)
	fprintf(DBUG_FILE,"                  quick select used on key %s, length: %d\n",
		form->key_info[tab->select->quick->index].name,
		tab->select->quick->max_used_key_length);
      else
	VOID(fputs("                  select used\n",DBUG_FILE));
    }
    if (tab->ref.key_parts)
    {
      VOID(fputs("                  refs: ",DBUG_FILE));
      for (ref=0 ; ref < tab->ref.key_parts ; ref++)
      {
	Item *item=tab->ref.items[ref];
	fprintf(DBUG_FILE,"%s  ", item->full_name());
      }
      VOID(fputc('\n',DBUG_FILE));
    }
  }
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}

#endif

unknown's avatar
unknown committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
typedef struct st_debug_lock
{
  ulong thread_id;
  char table_name[FN_REFLEN];
  bool waiting;
  const char *lock_text;
  enum thr_lock_type type;
} TABLE_LOCK_INFO;

static int dl_compare(TABLE_LOCK_INFO *a,TABLE_LOCK_INFO *b)
{
  if (a->thread_id > b->thread_id)
    return 1;
  if (a->thread_id < b->thread_id)
    return -1;
  if (a->waiting == b->waiting)
    return 0;
  else if (a->waiting)
    return -1;
  return 1;
}

static void push_locks_into_array(DYNAMIC_ARRAY *ar, THR_LOCK_DATA *data, bool wait, const char *text)
{
  if (data)
  {
    TABLE *table=(TABLE *)data->debug_print_param;
    if (table && table->tmp_table == NO_TMP_TABLE)
    {
      TABLE_LOCK_INFO table_lock_info;
      table_lock_info.thread_id=table->in_use->thread_id;
      memcpy(table_lock_info.table_name, table->table_cache_key, table->key_length);
      table_lock_info.table_name[strlen(table_lock_info.table_name)]='.';
      table_lock_info.waiting=wait;
      table_lock_info.lock_text=text;
      table_lock_info.type=table->reginfo.lock_type; // obtainable also from THR_LOCK_DATA
      VOID(push_dynamic(ar,(gptr) &table_lock_info));
    }
  }
}
/* 
   Regarding MERGE tables:

For now, the best option is to use the common TABLE *pointer for all
cases;  The drawback is that for MERGE tables we will see many locks
for the merge tables even if some of them are for individual tables.

The way to solve this is to add to 'THR_LOCK' structure a pointer to
the filename and use this when printing the data.
(We can for now ignore this and just print the same name for all merge
table parts;  Please add the above as a comment to the display_lock
function so that we can easily add this if we ever need this.

*/

static void display_table_locks (void) 
{
  LIST *list;
  DYNAMIC_ARRAY saved_table_locks;

  VOID(my_init_dynamic_array(&saved_table_locks,sizeof(TABLE_LOCK_INFO),open_cache.records + 20,50));
  VOID(pthread_mutex_lock(&THR_LOCK_lock));
  for (list=thr_lock_thread_list ; list ; list=rest(list))
  {
    THR_LOCK *lock=(THR_LOCK*) list->data;

    VOID(pthread_mutex_lock(&lock->mutex));
    push_locks_into_array(&saved_table_locks, lock->write.data, false, "Locked - write");
    push_locks_into_array(&saved_table_locks, lock->write_wait.data, true, "Waiting - write");
    push_locks_into_array(&saved_table_locks, lock->read.data, false, "Locked - read");
    push_locks_into_array(&saved_table_locks, lock->read_wait.data, true, "Waiting - read");
    VOID(pthread_mutex_unlock(&lock->mutex));
  }
  VOID(pthread_mutex_unlock(&THR_LOCK_lock));
  if (!saved_table_locks.elements) goto end;
  
  qsort((gptr) dynamic_element(&saved_table_locks,0,TABLE_LOCK_INFO *),saved_table_locks.elements,sizeof(TABLE_LOCK_INFO),(qsort_cmp) dl_compare);
  freeze_size(&saved_table_locks);

  puts("\nThread database.table_name          Locked/Waiting        Lock_type\n");
  
unknown's avatar
unknown committed
285
  unsigned int i;
286
  for (i=0 ; i < saved_table_locks.elements ; i++)
unknown's avatar
unknown committed
287 288 289 290 291 292 293 294 295 296 297
  {
    TABLE_LOCK_INFO *dl_ptr=dynamic_element(&saved_table_locks,i,TABLE_LOCK_INFO*);
    printf("%-8ld%-28.28s%-22s%s\n",
	   dl_ptr->thread_id,dl_ptr->table_name,dl_ptr->lock_text,lock_descriptions[(int)dl_ptr->type]);
  }
  puts("\n\n");
end:
  delete_dynamic(&saved_table_locks);
}


unknown's avatar
unknown committed
298 299
void mysql_print_status(THD *thd)
{
unknown's avatar
unknown committed
300
  char current_dir[FN_REFLEN];
unknown's avatar
unknown committed
301
  printf("\nStatus information:\n\n");
unknown's avatar
unknown committed
302 303
  my_getwd(current_dir, sizeof(current_dir),MYF(0));
  printf("Current dir: %s\n", current_dir);
unknown's avatar
unknown committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  if (thd)
    thd->proc_info="locks";
  thr_print_locks();				// Write some debug info
#ifndef DBUG_OFF
  if (thd)
    thd->proc_info="table cache";
  print_cached_tables();
#endif
  /* Print key cache status */
  if (thd)
    thd->proc_info="key cache";
  pthread_mutex_lock(&THR_LOCK_keycache);
  printf("key_cache status:\n\
blocks used:%10lu\n\
not flushed:%10lu\n\
w_requests: %10lu\n\
writes:     %10lu\n\
r_requests: %10lu\n\
reads:      %10lu\n",
323 324
	 my_blocks_used,my_blocks_changed,my_cache_w_requests,
	 my_cache_write,my_cache_r_requests,my_cache_read);
unknown's avatar
unknown committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  pthread_mutex_unlock(&THR_LOCK_keycache);

  if (thd)
    thd->proc_info="status";
  pthread_mutex_lock(&LOCK_status);
  printf("\nhandler status:\n\
read_key:   %10lu\n\
read_next:  %10lu\n\
read_rnd    %10lu\n\
read_first: %10lu\n\
write:      %10lu\n\
delete      %10lu\n\
update:     %10lu\n",
	 ha_read_key_count, ha_read_next_count,
	 ha_read_rnd_count, ha_read_first_count,
	 ha_write_count, ha_delete_count, ha_update_count);
  pthread_mutex_unlock(&LOCK_status);
  printf("\nTable status:\n\
Opened tables: %10lu\n\
Open tables:   %10lu\n\
Open files:    %10lu\n\
Open streams:  %10lu\n",
	 opened_tables,
	 (ulong) cached_tables(),
	 (ulong) my_file_opened,
	 (ulong) my_stream_opened);
unknown's avatar
unknown committed
351 352

  ALARM_INFO alarm_info;
353
#ifndef DONT_USE_THR_ALARM
unknown's avatar
unknown committed
354 355 356 357 358 359 360 361
  thr_alarm_info(&alarm_info);
  printf("\nAlarm status:\n\
Active alarms:   %u\n\
Max used alarms: %u\n\
Next alarm time: %lu\n",
	 alarm_info.active_alarms,
	 alarm_info.max_used_alarms,
	 alarm_info.next_alarm_time);
362
#endif
unknown's avatar
unknown committed
363
  display_table_locks();
unknown's avatar
unknown committed
364 365 366
  fflush(stdout);
  if (thd)
    thd->proc_info="malloc";
unknown's avatar
unknown committed
367
  my_checkmalloc();
unknown's avatar
unknown committed
368
  TERMINATE(stdout);				// Write malloc information
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

#ifdef HAVE_MALLINFO
  struct mallinfo info= mallinfo();
  printf("\nMemory status:\n\
Non-mmapped space allocated from system: %d\n\
Number of free chunks:			 %d\n\
Number of fastbin blocks:		 %d\n\
Number of mmapped regions:		 %d\n\
Space in mmapped regions:		 %d\n\
Maximum total allocated space:		 %d\n\
Space available in freed fastbin blocks: %d\n\
Total allocated space:			 %d\n\
Total free space:			 %d\n\
Top-most, releasable space:		 %d\n",
	 (int) info.arena,
	 (int) info.ordblks,
	 (int) info.smblks,
	 (int) info.hblks,
	 (int) info.hblkhd,
	 (int) info.usmblks,
	 (int) info.fsmblks,
	 (int) info.uordblks,
	 (int) info.fordblks,
	 (int) info.keepcost);
#endif
  puts("");
unknown's avatar
unknown committed
395 396 397
  if (thd)
    thd->proc_info=0;
}