DbtupDebug.cpp 13.1 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

   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.

   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 */


#define DBTUP_C
#include "Dbtup.hpp"
#include <RefConvert.hpp>
#include <ndb_limits.h>
#include <pc.hpp>
#include <signaldata/DropTab.hpp>
#include <signaldata/DumpStateOrd.hpp>
#include <signaldata/EventReport.hpp>
#include <Vector.hpp>

#define ljam() { jamLine(30000 + __LINE__); }
#define ljamEntry() { jamEntryLine(30000 + __LINE__); }

/* **************************************************************** */
/* ---------------------------------------------------------------- */
/* ------------------------ DEBUG MODULE -------------------------- */
/* ---------------------------------------------------------------- */
/* **************************************************************** */
void Dbtup::execDEBUG_SIG(Signal* signal) 
{
  PagePtr regPagePtr;
  ljamEntry();
  regPagePtr.i = signal->theData[0];
unknown's avatar
unknown committed
40
  c_page_pool.getPtr(regPagePtr);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}//Dbtup::execDEBUG_SIG()

#ifdef TEST_MR
#include <time.h>

void startTimer(struct timespec *tp)
{
  clock_gettime(CLOCK_REALTIME, tp);
}//startTimer()

int stopTimer(struct timespec *tp)
{
  double timer_count;
  struct timespec theStopTime;
  clock_gettime(CLOCK_REALTIME, &theStopTime);
  timer_count = (double)(1000000*((double)theStopTime.tv_sec - (double)tp->tv_sec)) + 
                (double)((double)((double)theStopTime.tv_nsec - (double)tp->tv_nsec)/(double)1000);
  return (int)timer_count;
}//stopTimer()

#endif // end TEST_MR

struct Chunk {
  Uint32 pageId;
  Uint32 pageCount;
};

void
Dbtup::reportMemoryUsage(Signal* signal, int incDec){
70
  signal->theData[0] = NDB_LE_MemoryUsage;
71 72 73
  signal->theData[1] = incDec;
  signal->theData[2] = sizeof(Page);
  signal->theData[3] = cnoOfAllocatedPages;
unknown's avatar
unknown committed
74
  signal->theData[4] = c_page_pool.getSize();
75 76 77 78
  signal->theData[5] = DBTUP;
  sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 6, JBB);
}

unknown's avatar
unknown committed
79 80 81 82
#ifdef VM_TRACE
extern Uint32 fc_left, fc_right, fc_remove;
#endif

83 84 85 86
void
Dbtup::execDUMP_STATE_ORD(Signal* signal)
{
  Uint32 type = signal->theData[0];
unknown's avatar
unknown committed
87
  if(type == DumpStateOrd::DumpPageMemory && signal->getLength() == 1){
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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
    reportMemoryUsage(signal, 0);
    return;
  }
  DumpStateOrd * const dumpState = (DumpStateOrd *)&signal->theData[0];

#if 0
  if (type == 100) {
    RelTabMemReq * const req = (RelTabMemReq *)signal->getDataPtrSend();
    req->primaryTableId = 2;
    req->secondaryTableId = RNIL;
    req->userPtr = 2;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_REL_TABMEMREQ, signal,
               RelTabMemReq::SignalLength, JBB);
    return;
  }//if
  if (type == 101) {
    RelTabMemReq * const req = (RelTabMemReq *)signal->getDataPtrSend();
    req->primaryTableId = 4;
    req->secondaryTableId = 5;
    req->userPtr = 4;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_REL_TABMEMREQ, signal,
               RelTabMemReq::SignalLength, JBB);
    return;
  }//if
  if (type == 102) {
    RelTabMemReq * const req = (RelTabMemReq *)signal->getDataPtrSend();
    req->primaryTableId = 6;
    req->secondaryTableId = 8;
    req->userPtr = 6;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_REL_TABMEMREQ, signal,
               RelTabMemReq::SignalLength, JBB);
    return;
  }//if
  if (type == 103) {
    DropTabFileReq * const req = (DropTabFileReq *)signal->getDataPtrSend();
    req->primaryTableId = 2;
    req->secondaryTableId = RNIL;
    req->userPtr = 2;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_DROP_TABFILEREQ, signal,
               DropTabFileReq::SignalLength, JBB);
    return;
  }//if
  if (type == 104) {
    DropTabFileReq * const req = (DropTabFileReq *)signal->getDataPtrSend();
    req->primaryTableId = 4;
    req->secondaryTableId = 5;
    req->userPtr = 4;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_DROP_TABFILEREQ, signal,
               DropTabFileReq::SignalLength, JBB);
    return;
  }//if
  if (type == 105) {
    DropTabFileReq * const req = (DropTabFileReq *)signal->getDataPtrSend();
    req->primaryTableId = 6;
    req->secondaryTableId = 8;
    req->userPtr = 6;
    req->userRef = DBDICT_REF;
    sendSignal(cownref, GSN_DROP_TABFILEREQ, signal,
               DropTabFileReq::SignalLength, JBB);
    return;
  }//if
#endif
#ifdef ERROR_INSERT
  if (type == DumpStateOrd::EnableUndoDelayDataWrite) {
    ndbout << "Dbtup:: delay write of datapages for table = " 
	   << dumpState->args[1]<< endl;
    c_errorInsert4000TableId = dumpState->args[1];
    SET_ERROR_INSERT_VALUE(4000);
    return;
  }//if
#endif
unknown's avatar
unknown committed
164 165 166 167 168 169 170
#if defined VM_TRACE
  if (type == 1211 || type == 1212 || type == 1213){
    Uint32 seed = time(0);
    if (signal->getLength() > 1)
      seed = signal->theData[1];
    ndbout_c("Startar modul test av Page Manager (seed: 0x%x)", seed);
    srand(seed);
171 172 173

    Vector<Chunk> chunks;
    const Uint32 LOOPS = 1000;
unknown's avatar
unknown committed
174 175 176 177
    Uint32 sum_req = 0;
    Uint32 sum_conf = 0;
    Uint32 sum_loop = 0;
    Uint32 max_loop = 0;
178 179 180 181
    for(Uint32 i = 0; i<LOOPS; i++){

      // Case
      Uint32 c = (rand() % 3);
unknown's avatar
unknown committed
182
      const Uint32 free = c_page_pool.getSize() - cnoOfAllocatedPages;
183 184 185 186 187 188 189 190 191 192 193
      
      Uint32 alloc = 0;
      if(free <= 1){
	c = 0;
	alloc = 1;
      } else 
	alloc = 1 + (rand() % (free - 1));
      
      if(chunks.size() == 0 && c == 0){
	c = 1 + rand() % 2;
      }
unknown's avatar
unknown committed
194 195 196
      
      if (type == 1211)
        ndbout_c("loop=%d case=%d free=%d alloc=%d", i, c, free, alloc);
197

unknown's avatar
unknown committed
198 199 200 201 202
      if (type == 1213)
      {
        c = 1;
        alloc = 2 + (sum_conf >> 3) + (sum_conf >> 4);
      }
203 204 205 206 207 208 209 210 211 212 213
      switch(c){ 
      case 0:{ // Release
	const int ch = rand() % chunks.size();
	Chunk chunk = chunks[ch];
	chunks.erase(ch);
	returnCommonArea(chunk.pageId, chunk.pageCount);
      }
	break;
      case 2: { // Seize(n) - fail
	alloc += free;
	// Fall through
unknown's avatar
unknown committed
214 215
        sum_req += free;
        goto doalloc;
216 217
      }
      case 1: { // Seize(n) (success)
unknown's avatar
unknown committed
218 219
        sum_req += alloc;
    doalloc:
220 221 222 223 224
	Chunk chunk;
	allocConsPages(alloc, chunk.pageCount, chunk.pageId);
	ndbrequire(chunk.pageCount <= alloc);
	if(chunk.pageCount != 0){
	  chunks.push_back(chunk);
225
	  if(chunk.pageCount != alloc) {
unknown's avatar
unknown committed
226 227 228
	    if (type == 1211)
              ndbout_c("  Tried to allocate %d - only allocated %d - free: %d",
                       alloc, chunk.pageCount, free);
229
	  }
230 231 232 233 234
	} else {
	  ndbout_c("  Failed to alloc %d pages with %d pages free",
		   alloc, free);
	}
	
unknown's avatar
unknown committed
235 236 237 238 239 240
        sum_conf += chunk.pageCount;
        Uint32 tot = fc_left + fc_right + fc_remove;
        sum_loop += tot;
        if (tot > max_loop)
          max_loop = tot;

241 242 243
	for(Uint32 i = 0; i<chunk.pageCount; i++){
	  PagePtr pagePtr;
	  pagePtr.i = chunk.pageId + i;
unknown's avatar
unknown committed
244
	  c_page_pool.getPtr(pagePtr);
unknown's avatar
unknown committed
245
	  pagePtr.p->page_state = ~ZFREE_COMMON;
246
	}
247 248 249

	if(alloc == 1 && free > 0)
	  ndbrequire(chunk.pageCount == alloc);
250 251 252 253 254 255 256 257 258
      }
	break;
      }
    }
    while(chunks.size() > 0){
      Chunk chunk = chunks.back();
      returnCommonArea(chunk.pageId, chunk.pageCount);      
      chunks.erase(chunks.size() - 1);
    }
unknown's avatar
unknown committed
259 260 261 262

    ndbout_c("Got %u%% of requested allocs, loops : %u 100*avg: %u max: %u",
             (100 * sum_conf) / sum_req, sum_loop, 100*sum_loop / LOOPS,
             max_loop);
263 264 265 266 267 268 269 270 271
  }
#endif
}//Dbtup::execDUMP_STATE_ORD()

/* ---------------------------------------------------------------- */
/* ---------      MEMORY       CHECK        ----------------------- */
/* ---------------------------------------------------------------- */
void Dbtup::execMEMCHECKREQ(Signal* signal) 
{
unknown's avatar
unknown committed
272 273 274 275 276 277 278 279 280
  TablerecPtr regTabPtr;
  regTabPtr.i = 2;
  ptrCheckGuard(regTabPtr, cnoOfTablerec, tablerec);
  if(tablerec && regTabPtr.p->tableStatus == DEFINED)
    validate_page(regTabPtr.p, 0);

#if 0
  const Dbtup::Tablerec& tab = *tup->tabptr.p;

281 282 283 284 285
  PagePtr regPagePtr;
  Uint32* data = &signal->theData[0];

  ljamEntry();
  BlockReference blockref = signal->theData[0];
unknown's avatar
unknown committed
286 287
  Uint32 i;
  for (i = 0; i < 25; i++) {
288 289 290
    ljam();
    data[i] = 0;
  }//for
unknown's avatar
unknown committed
291
  for (i = 0; i < 16; i++) {
292 293 294 295
    regPagePtr.i = cfreepageList[i];
    ljam();
    while (regPagePtr.i != RNIL) {
      ljam();
unknown's avatar
unknown committed
296 297
      ptrCheckGuard(regPagePtr, cnoOfPage, cpage);
      regPagePtr.i = regPagePtr.p->next_page;
298 299 300 301
      data[0]++;
    }//while
  }//for
  sendSignal(blockref, GSN_MEMCHECKCONF, signal, 25, JBB);
unknown's avatar
unknown committed
302
#endif
303 304 305 306 307 308 309 310 311 312 313 314 315
}//Dbtup::memCheck()

// ------------------------------------------------------------------------
// Help function to be used when debugging. Prints out a tuple page.
// printLimit is the number of bytes that is printed out from the page. A 
// page is of size 32768 bytes as of March 2003.
// ------------------------------------------------------------------------
void Dbtup::printoutTuplePage(Uint32 fragid, Uint32 pageid, Uint32 printLimit) 
{
  PagePtr tmpPageP;
  FragrecordPtr tmpFragP;
  TablerecPtr tmpTableP;

unknown's avatar
unknown committed
316
  c_page_pool.getPtr(tmpPageP, pageid);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

  tmpFragP.i = fragid;
  ptrCheckGuard(tmpFragP, cnoOfFragrec, fragrecord);

  tmpTableP.i = tmpFragP.p->fragTableId;
  ptrCheckGuard(tmpTableP, cnoOfTablerec, tablerec);

  ndbout << "Fragid: " << fragid << " Pageid: " << pageid << endl
	 << "----------------------------------------" << endl;

  ndbout << "PageHead : ";
  ndbout << endl;
}//Dbtup::printoutTuplePage

#ifdef VM_TRACE
NdbOut&
operator<<(NdbOut& out, const Dbtup::Operationrec& op)
{
  out << "[Operationrec " << hex << &op;
  // table
  out << " [fragmentPtr " << hex << op.fragmentPtr << "]";
  // type
unknown's avatar
unknown committed
339 340 341
  out << " [op_type " << dec << op.op_struct.op_type << "]";
  out << " [delete_insert_flag " << dec;
  out << op.op_struct.delete_insert_flag << "]";
342
  // state
unknown's avatar
unknown committed
343 344 345
  out << " [tuple_state " << dec << op.op_struct.tuple_state << "]";
  out << " [trans_state " << dec << op.op_struct.trans_state << "]";
  out << " [in_active_list " << dec << op.op_struct.in_active_list << "]";
346 347 348 349 350
  // links
  out << " [prevActiveOp " << hex << op.prevActiveOp << "]";
  out << " [nextActiveOp " << hex << op.nextActiveOp << "]";
  // tuples
  out << " [tupVersion " << hex << op.tupVersion << "]";
unknown's avatar
unknown committed
351 352
  out << " [m_tuple_location " << op.m_tuple_location << "]";
  out << " [m_copy_tuple_location " << op.m_copy_tuple_location << "]";
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  out << "]";
  return out;
}

// uses global tabptr
NdbOut&
operator<<(NdbOut& out, const Dbtup::Th& th)
{
  // ugly
  Dbtup* tup = (Dbtup*)globalData.getBlock(DBTUP);
  const Dbtup::Tablerec& tab = *tup->tabptr.p;
  unsigned i = 0;
  out << "[Th " << hex << &th;
  out << " [op " << hex << th.data[i++] << "]";
  out << " [version " << hex << (Uint16)th.data[i++] << "]";
unknown's avatar
unknown committed
368
  if (tab.m_bits & Dbtup::Tablerec::TR_Checksum)
369 370
    out << " [checksum " << hex << th.data[i++] << "]";
  out << " [nullbits";
unknown's avatar
unknown committed
371
  for (unsigned j = 0; j < tab.m_offsets[Dbtup::MM].m_null_words; j++)
372 373 374
    out << " " << hex << th.data[i++];
  out << "]";
  out << " [data";
unknown's avatar
unknown committed
375
  while (i < tab.m_offsets[Dbtup::MM].m_fix_header_size)
376 377 378 379 380 381
    out << " " << hex << th.data[i++];
  out << "]";
  out << "]";
  return out;
}
#endif
382

unknown's avatar
unknown committed
383
#ifdef VM_TRACE
384
template class Vector<Chunk>;
unknown's avatar
unknown committed
385
#endif
unknown's avatar
unknown committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
// uses global tabptr

NdbOut&
operator<<(NdbOut& out, const Local_key & key)
{
  out << "[ m_page_no: " << dec << key.m_page_no 
      << " m_file_no: " << dec << key.m_file_no 
      << " m_page_idx: " << dec << key.m_page_idx << "]";
  return out;
}

static
NdbOut&
operator<<(NdbOut& out, const Dbtup::Tablerec::Tuple_offsets& off)
{
  out << "[ null_words: " << (Uint32)off.m_null_words
      << " null off: " << (Uint32)off.m_null_offset
      << " disk_off: " << off.m_disk_ref_offset
unknown's avatar
ndb -  
unknown committed
404
      << " fixheadsz: " << off.m_fix_header_size
unknown's avatar
unknown committed
405 406 407 408 409 410 411 412 413 414
      << " max_var_off: " << off.m_max_var_offset
      << " ]";

  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtup::Tablerec& tab)
{
  out << "[ total_rec_size: " << tab.total_rec_size
unknown's avatar
unknown committed
415
      << " checksum: " << !!(tab.m_bits & Dbtup::Tablerec::TR_Checksum)
unknown's avatar
unknown committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
      << " attr: " << tab.m_no_of_attributes
      << " disk: " << tab.m_no_of_disk_attributes 
      << " mm: " << tab.m_offsets[Dbtup::MM]
      << " [ fix: " << tab.m_attributes[Dbtup::MM].m_no_of_fixsize
      << " var: " << tab.m_attributes[Dbtup::MM].m_no_of_varsize << "]"
    
      << " dd: " << tab.m_offsets[Dbtup::DD]
      << " [ fix: " << tab.m_attributes[Dbtup::DD].m_no_of_fixsize
      << " var: " << tab.m_attributes[Dbtup::DD].m_no_of_varsize << "]"
      << " ]"  << endl;
  return out;
}

NdbOut&
operator<<(NdbOut& out, const AttributeDescriptor& off)
{
  Uint32 word;
  memcpy(&word, &off, 4);
  return out;
}

#include "AttributeOffset.hpp"

NdbOut&
operator<<(NdbOut& out, const AttributeOffset& off)
{
  Uint32 word;
  memcpy(&word, &off, 4);
  out << "[ offset: " << AttributeOffset::getOffset(word)
      << " nullpos: " << AttributeOffset::getNullFlagPos(word);
  if(AttributeOffset::getCharsetFlag(word))
    out << " charset: %d" << AttributeOffset::getCharsetPos(word);
  out << " ]";
  return out;
}