DbtuxDebug.cpp 13.6 KB
Newer Older
1 2 3 4 5 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 40 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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 DBTUX_DEBUG_CPP
#include "Dbtux.hpp"

/*
 * 12001 log file 0-close 1-open 2-append 3-append to signal log
 * 12002 log flags 1-meta 2-maint 4-tree 8-scan
 */
void
Dbtux::execDUMP_STATE_ORD(Signal* signal)
{
  jamEntry();
#ifdef VM_TRACE
  if (signal->theData[0] == DumpStateOrd::TuxLogToFile) {
    unsigned flag = signal->theData[1];
    const char* const tuxlog = "tux.log";
    FILE* slFile = globalSignalLoggers.getOutputStream();
    if (flag <= 3) {
      if (debugFile != 0) {
        if (debugFile != slFile)
          fclose(debugFile);
        debugFile = 0;
        debugOut = *new NdbOut(*new NullOutputStream());
      }
      if (flag == 1)
        debugFile = fopen(tuxlog, "w");
      if (flag == 2)
        debugFile = fopen(tuxlog, "a");
      if (flag == 3)
        debugFile = slFile;
      if (debugFile != 0)
        debugOut = *new NdbOut(*new FileOutputStream(debugFile));
    }
    return;
  }
  if (signal->theData[0] == DumpStateOrd::TuxSetLogFlags) {
    debugFlags = signal->theData[1];
    return;
  }
  if (signal->theData[0] == DumpStateOrd::TuxMetaDataJunk) {
    // read table definition
    Uint32 tableId = signal->theData[1];
    Uint32 tableVersion = signal->theData[2];
    int ret;
    MetaData md(this);
    MetaData::Table table;
    MetaData::Attribute attribute;
    infoEvent("md: read table %u %u", tableId, tableVersion);
    if ((ret = md.lock(false)) < 0) {
      infoEvent("md.lock error %d", ret);
      return;
    }
    if ((ret = md.getTable(table, tableId, tableVersion)) < 0) {
      infoEvent("md.getTable error %d", ret);
      // lock is released by destructor
      return;
    }
    infoEvent("md: %s type=%d attrs=%u", table.tableName, table.tableType, table.noOfAttributes);
    for (Uint32 i = 0; i < table.noOfAttributes; i++) {
      if ((ret = md.getAttribute(attribute, table, i)) < 0) {
        infoEvent("mg.getAttribute %u error %d", i, ret);
        // lock is released by destructor
        return;
      }
      infoEvent("md: %d %s", attribute.attributeId, attribute.attributeName);
    }
    if ((ret = md.unlock(false)) < 0) {
      infoEvent("md.unlock error %d", ret);
      return;
    }
    return;
  }
#endif
}

#ifdef VM_TRACE

void
Dbtux::printTree(Signal* signal, Frag& frag, NdbOut& out)
{
  TreeHead& tree = frag.m_tree;
  PrintPar par;
  strcpy(par.m_path, ".");
  par.m_side = 2;
100
  par.m_parent = NullTupLoc;
101
  printNode(frag, out, tree.m_root, par);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  out.m_out->flush();
  if (! par.m_ok) {
    if (debugFile == 0) {
      signal->theData[0] = 12001;
      signal->theData[1] = 1;
      execDUMP_STATE_ORD(signal);
      if (debugFile != 0) {
        printTree(signal, frag, debugOut);
      }
    }
    ndbrequire(false);
  }
}

void
117
Dbtux::printNode(Frag& frag, NdbOut& out, TupLoc loc, PrintPar& par)
118
{
119
  if (loc == NullTupLoc) {
120 121 122 123
    par.m_depth = 0;
    return;
  }
  TreeHead& tree = frag.m_tree;
124
  NodeHandle node(frag);
125
  selectNode(node, loc);
126
  out << par.m_path << " " << node << endl;
127 128 129 130 131 132 133
  // check children
  PrintPar cpar[2];
  ndbrequire(strlen(par.m_path) + 1 < sizeof(par.m_path));
  for (unsigned i = 0; i <= 1; i++) {
    sprintf(cpar[i].m_path, "%s%c", par.m_path, "LR"[i]);
    cpar[i].m_side = i;
    cpar[i].m_depth = 0;
134
    cpar[i].m_parent = loc;
135
    printNode(frag, out, node.getLink(i), cpar[i]);
136 137 138 139
    if (! cpar[i].m_ok) {
      par.m_ok = false;
    }
  }
140
  static const char* const sep = " *** ";
141
  // check child-parent links
142
  if (node.getLink(2) != par.m_parent) {
143
    par.m_ok = false;
144
    out << par.m_path << sep;
145
    out << "parent loc " << hex << node.getLink(2);
146 147
    out << " should be " << hex << par.m_parent << endl;
  }
148
  if (node.getSide() != par.m_side) {
149
    par.m_ok = false;
150
    out << par.m_path << sep;
151
    out << "side " << dec << node.getSide();
152 153 154 155
    out << " should be " << dec << par.m_side << endl;
  }
  // check balance
  const int balance = -cpar[0].m_depth + cpar[1].m_depth;
156
  if (node.getBalance() != balance) {
157
    par.m_ok = false;
158
    out << par.m_path << sep;
159
    out << "balance " << node.getBalance();
160 161
    out << " should be " << balance << endl;
  }
162
  if (abs(node.getBalance()) > 1) {
163
    par.m_ok = false;
164
    out << par.m_path << sep;
165
    out << "balance " << node.getBalance() << " is invalid" << endl;
166 167
  }
  // check occupancy
168
  if (node.getOccup() == 0 || node.getOccup() > tree.m_maxOccup) {
169
    par.m_ok = false;
170
    out << par.m_path << sep;
171
    out << "occupancy " << node.getOccup();
172
    out << " zero or greater than max " << tree.m_maxOccup << endl;
173 174
  }
  // check for occupancy of interior node
175
  if (node.getChilds() == 2 && node.getOccup() < tree.m_minOccup) {
176
    par.m_ok = false;
177
    out << par.m_path << sep;
178
    out << "occupancy " << node.getOccup() << " of interior node";
179 180
    out << " less than min " << tree.m_minOccup << endl;
  }
181 182
#ifdef dbtux_totally_groks_t_trees
  // check missed semi-leaf/leaf merge
183
  for (unsigned i = 0; i <= 1; i++) {
184 185
    if (node.getLink(i) != NullTupLoc &&
        node.getLink(1 - i) == NullTupLoc &&
186 187
        // our semi-leaf seems to satify interior minOccup condition
        node.getOccup() < tree.m_minOccup) {
188
      par.m_ok = false;
189
      out << par.m_path << sep;
190 191 192
      out << "missed merge with child " << i << endl;
    }
  }
193
#endif
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  // check inline prefix
  { ConstData data1 = node.getPref();
    Uint32 data2[MaxPrefSize];
    memset(data2, DataFillByte, MaxPrefSize << 2);
    readKeyAttrs(frag, node.getMinMax(0), 0, c_searchKey);
    copyAttrs(frag, c_searchKey, data2, tree.m_prefSize);
    for (unsigned n = 0; n < tree.m_prefSize; n++) {
      if (data1[n] != data2[n]) {
        par.m_ok = false;
        out << par.m_path << sep;
        out << "inline prefix mismatch word " << n;
        out << " value " << hex << data1[n];
        out << " should be " << hex << data2[n] << endl;
        break;
      }
    }
  }
  // check ordering within node
  for (unsigned j = 1; j < node.getOccup(); j++) {
    const TreeEnt ent1 = node.getEnt(j - 1);
    const TreeEnt ent2 = node.getEnt(j);
215 216
    unsigned start = 0;
    readKeyAttrs(frag, ent1, start, c_searchKey);
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
    readKeyAttrs(frag, ent2, start, c_entryKey);
    int ret = cmpSearchKey(frag, start, c_searchKey, c_entryKey);
    if (ret == 0)
      ret = ent1.cmp(ent2);
    if (ret != -1) {
      par.m_ok = false;
      out << par.m_path << sep;
      out << " disorder within node at pos " << j << endl;
    }
  }
  // check ordering wrt subtrees
  for (unsigned i = 0; i <= 1; i++) {
    if (node.getLink(i) == NullTupLoc)
      continue;
    const TreeEnt ent1 = cpar[i].m_minmax[1 - i];
    const TreeEnt ent2 = node.getMinMax(i);
    unsigned start = 0;
    readKeyAttrs(frag, ent1, start, c_searchKey);
    readKeyAttrs(frag, ent2, start, c_entryKey);
    int ret = cmpSearchKey(frag, start, c_searchKey, c_entryKey);
    if (ret == 0)
      ret = ent1.cmp(ent2);
    if (ret != (i == 0 ? -1 : +1)) {
      par.m_ok = false;
      out << par.m_path << sep;
      out << " disorder wrt subtree " << i << endl;
    }
  }
245 246
  // return values
  par.m_depth = 1 + max(cpar[0].m_depth, cpar[1].m_depth);
247
  par.m_occup = node.getOccup();
248 249 250 251 252 253
  for (unsigned i = 0; i <= 1; i++) {
    if (node.getLink(i) == NullTupLoc)
      par.m_minmax[i] = node.getMinMax(i);
    else
      par.m_minmax[i] = cpar[i].m_minmax[i];
  }
254 255
}

256 257 258 259 260 261
NdbOut&
operator<<(NdbOut& out, const Dbtux::TupLoc& loc)
{
  if (loc == Dbtux::NullTupLoc) {
    out << "null";
  } else {
262 263
    out << dec << loc.getPageId();
    out << "." << dec << loc.getPageOffset();
264 265 266 267
  }
  return out;
}

268 269 270 271
NdbOut&
operator<<(NdbOut& out, const Dbtux::TreeEnt& ent)
{
  out << dec << ent.m_fragBit;
272
  out << "-" << ent.m_tupLoc;
273 274 275 276 277 278 279 280
  out << "-" << dec << ent.m_tupVersion;
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::TreeNode& node)
{
  out << "[TreeNode " << hex << &node;
281 282 283
  out << " [left " << node.m_link[0] << "]";
  out << " [right " << node.m_link[1] << "]";
  out << " [up " << node.m_link[2] << "]";
284 285
  out << " [side " << dec << node.m_side << "]";
  out << " [occup " << dec << node.m_occup << "]";
286
  out << " [balance " << dec << (int)node.m_balance - 1 << "]";
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  out << " [nodeScan " << hex << node.m_nodeScan << "]";
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::TreeHead& tree)
{
  out << "[TreeHead " << hex << &tree;
  out << " [nodeSize " << dec << tree.m_nodeSize << "]";
  out << " [prefSize " << dec << tree.m_prefSize << "]";
  out << " [minOccup " << dec << tree.m_minOccup << "]";
  out << " [maxOccup " << dec << tree.m_maxOccup << "]";
  out << " [AccHead " << dec << tree.getSize(Dbtux::AccHead) << "]";
  out << " [AccPref " << dec << tree.getSize(Dbtux::AccPref) << "]";
  out << " [AccFull " << dec << tree.getSize(Dbtux::AccFull) << "]";
  out << " [root " << hex << tree.m_root << "]";
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::TreePos& pos)
{
  out << "[TreePos " << hex << &pos;
312
  out << " [loc " << pos.m_loc << "]";
313 314 315 316 317 318 319 320 321 322
  out << " [pos " << dec << pos.m_pos << "]";
  out << " [dir " << dec << pos.m_dir << "]";
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::DescAttr& descAttr)
{
  out << "[DescAttr " << hex << &descAttr;
323
  out << " [attrDesc " << hex << descAttr.m_attrDesc;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  out << " [primaryAttrId " << dec << descAttr.m_primaryAttrId << "]";
  out << " [typeId " << dec << descAttr.m_typeId << "]";
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::ScanOp& scan)
{
  out << "[ScanOp " << hex << &scan;
  out << " [state " << dec << scan.m_state << "]";
  out << " [lockwait " << dec << scan.m_lockwait << "]";
  out << " [indexId " << dec << scan.m_indexId << "]";
  out << " [fragId " << dec << scan.m_fragId << "]";
  out << " [transId " << hex << scan.m_transId1 << " " << scan.m_transId2 << "]";
  out << " [savePointId " << dec << scan.m_savePointId << "]";
  out << " [accLockOp " << hex << scan.m_accLockOp << "]";
  out << " [accLockOps";
342
  for (unsigned i = 0; i < scan.m_maxAccLockOps; i++) {
343 344 345 346 347 348
    if (scan.m_accLockOps[i] != RNIL)
      out << " " << hex << scan.m_accLockOps[i];
  }
  out << "]";
  out << " [readCommitted " << dec << scan.m_readCommitted << "]";
  out << " [lockMode " << dec << scan.m_lockMode << "]";
349
  out << " [descending " << dec << scan.m_descending << "]";
350
  out << " [pos " << scan.m_scanPos << "]";
pekka@mysql.com's avatar
pekka@mysql.com committed
351
  out << " [ent " << scan.m_scanEnt << "]";
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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 395 396 397 398 399 400 401 402
  for (unsigned i = 0; i <= 1; i++) {
    out << " [bound " << dec << i;
    Dbtux::ScanBound& bound = *scan.m_bound[i];
    Dbtux::ScanBoundIterator iter;
    bound.first(iter);
    for (unsigned j = 0; j < bound.getSize(); j++) {
      out << " " << hex << *iter.data;
      bound.next(iter);
    }
    out << "]";
  }
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::Index& index)
{
  out << "[Index " << hex << &index;
  out << " [tableId " << dec << index.m_tableId << "]";
  out << " [numFrags " << dec << index.m_numFrags << "]";
  for (unsigned i = 0; i < index.m_numFrags; i++) {
    out << " [frag " << dec << i << " ";
    // dangerous and wrong
    Dbtux* tux = (Dbtux*)globalData.getBlock(DBTUX);
    const Dbtux::Frag& frag = *tux->c_fragPool.getPtr(index.m_fragPtrI[i]);
    out << frag;
    out << "]";
  }
  out << " [descPage " << hex << index.m_descPage << "]";
  out << " [descOff " << dec << index.m_descOff << "]";
  out << " [numAttrs " << dec << index.m_numAttrs << "]";
  out << "]";
  return out;
}

NdbOut&
operator<<(NdbOut& out, const Dbtux::Frag& frag)
{
  out << "[Frag " << hex << &frag;
  out << " [tableId " << dec << frag.m_tableId << "]";
  out << " [indexId " << dec << frag.m_indexId << "]";
  out << " [fragId " << dec << frag.m_fragId << "]";
  out << " [descPage " << hex << frag.m_descPage << "]";
  out << " [descOff " << dec << frag.m_descOff << "]";
  out << " [numAttrs " << dec << frag.m_numAttrs << "]";
  out << " [tree " << frag.m_tree << "]";
  out << "]";
  return out;
}

403 404 405 406 407 408 409 410 411 412 413 414 415
NdbOut&
operator<<(NdbOut& out, const Dbtux::FragOp& fragOp)
{
  out << "[FragOp " << hex << &fragOp;
  out << " [userPtr " << dec << fragOp.m_userPtr << "]";
  out << " [indexId " << dec << fragOp.m_indexId << "]";
  out << " [fragId " << dec << fragOp.m_fragId << "]";
  out << " [fragNo " << dec << fragOp.m_fragNo << "]";
  out << " numAttrsRecvd " << dec << fragOp.m_numAttrsRecvd << "]";
  out << "]";
  return out;
}

416 417 418 419 420 421
NdbOut&
operator<<(NdbOut& out, const Dbtux::NodeHandle& node)
{
  const Dbtux::Frag& frag = node.m_frag;
  const Dbtux::TreeHead& tree = frag.m_tree;
  out << "[NodeHandle " << hex << &node;
422
  out << " [loc " << node.m_loc << "]";
423
  out << " [node " << *node.m_node << "]";
424 425 426 427 428 429 430 431 432 433 434 435 436 437
  const Uint32* data;
  out << " [pref";
  data = (const Uint32*)node.m_node + Dbtux::NodeHeadSize;
  for (unsigned j = 0; j < tree.m_prefSize; j++)
    out << " " << hex << data[j];
  out << "]";
  out << " [entList";
  unsigned numpos = node.m_node->m_occup;
  data = (const Uint32*)node.m_node + Dbtux::NodeHeadSize + tree.m_prefSize;
  const Dbtux::TreeEnt* entList = (const Dbtux::TreeEnt*)data;
  // print entries in logical order
  for (unsigned pos = 1; pos <= numpos; pos++)
    out << " " << entList[pos % numpos];
  out << "]";
438 439 440 441 442
  out << "]";
  return out;
}

#endif