pgman.cpp 54.7 KB
Newer Older
unknown's avatar
unknown committed
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.
unknown's avatar
unknown committed
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

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

#include "pgman.hpp"
#include <signaldata/FsRef.hpp>
#include <signaldata/FsConf.hpp>
#include <signaldata/FsReadWriteReq.hpp>
#include <signaldata/PgmanContinueB.hpp>
#include <signaldata/LCP.hpp>

#include <dbtup/Dbtup.hpp>

#include <DebuggerNames.hpp>

/**
 * Requests that make page dirty
 */
#define DIRTY_FLAGS (Page_request::COMMIT_REQ | \
                     Page_request::DIRTY_REQ | \
                     Page_request::ALLOC_REQ)

// todo use this
#ifdef VM_TRACE
#define dbg(x) \
  do { if (! debugFlag) break; debugOut << "PGMAN: " << x << endl; } while (0)
#else
#define dbg(x)
#endif

unknown's avatar
unknown committed
42
static bool g_dbg_lcp = false;
43 44 45
#if 1
#define DBG_LCP(x)
#else
unknown's avatar
unknown committed
46
#define DBG_LCP(x) if(g_dbg_lcp) ndbout << x
47 48
#endif

49 50
Pgman::Pgman(Block_context& ctx) :
  SimulatedBlock(PGMAN, ctx),
unknown's avatar
unknown committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  m_file_map(m_data_buffer_pool),
  m_page_hashlist(m_page_entry_pool),
  m_page_stack(m_page_entry_pool),
  m_page_queue(m_page_entry_pool)
#ifdef VM_TRACE
  ,debugOut(* new NullOutputStream())
  ,debugFlag(false)
#endif
{
  BLOCK_CONSTRUCTOR(Pgman);

  // Add received signals
  addRecSignal(GSN_STTOR, &Pgman::execSTTOR);
  addRecSignal(GSN_READ_CONFIG_REQ, &Pgman::execREAD_CONFIG_REQ);
  addRecSignal(GSN_DUMP_STATE_ORD, &Pgman::execDUMP_STATE_ORD);
  addRecSignal(GSN_CONTINUEB, &Pgman::execCONTINUEB);
  addRecSignal(GSN_FSREADREF, &Pgman::execFSREADREF, true);
  addRecSignal(GSN_FSREADCONF, &Pgman::execFSREADCONF);
  addRecSignal(GSN_FSWRITEREF, &Pgman::execFSWRITEREF, true);
  addRecSignal(GSN_FSWRITECONF, &Pgman::execFSWRITECONF);

  addRecSignal(GSN_LCP_FRAG_ORD, &Pgman::execLCP_FRAG_ORD);
  addRecSignal(GSN_END_LCP_REQ, &Pgman::execEND_LCP_REQ);
unknown's avatar
unknown committed
74
  
unknown's avatar
unknown committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  // loop status
  m_stats_loop_on = false;
  m_busy_loop_on = false;
  m_cleanup_loop_on = false;
  m_lcp_loop_on = false;

  // LCP variables
  m_last_lcp = 0;
  m_last_lcp_complete = 0;
  m_lcp_curr_bucket = ~(Uint32)0;
  m_lcp_outstanding = 0;

  // clean-up variables
  m_cleanup_ptr.i = RNIL;

  // should be a factor larger than number of pool pages
unknown's avatar
unknown committed
91
  m_data_buffer_pool.setSize(16);
unknown's avatar
unknown committed
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
  m_page_hashlist.setSize(512);
  
  for (Uint32 k = 0; k < Page_entry::SUBLIST_COUNT; k++)
    m_page_sublist[k] = new Page_sublist(m_page_entry_pool);
}

Pgman::~Pgman()
{
  for (Uint32 k = 0; k < Page_entry::SUBLIST_COUNT; k++)
    delete m_page_sublist[k];
}

BLOCK_FUNCTIONS(Pgman)

void 
Pgman::execREAD_CONFIG_REQ(Signal* signal)
{
  jamEntry();

  const ReadConfigReq * req = (ReadConfigReq*)signal->getDataPtr();

  Uint32 ref = req->senderRef;
  Uint32 senderData = req->senderData;

  const ndb_mgm_configuration_iterator * p = 
117
    m_ctx.m_config.getOwnConfigIterator();
unknown's avatar
unknown committed
118 119
  ndbrequire(p != 0);

unknown's avatar
unknown committed
120 121 122 123 124 125
  Uint64 page_buffer = 64*1024*1024;
  ndb_mgm_get_int64_parameter(p, CFG_DB_DISK_PAGE_BUFFER_MEMORY, &page_buffer);
  
  if (page_buffer > 0)
  {
    page_buffer /= GLOBAL_PAGE_SIZE; // in pages
126
    m_page_entry_pool.setSize(100*page_buffer);
unknown's avatar
unknown committed
127 128 129
    m_param.m_max_pages = page_buffer;
    m_param.m_max_hot_pages = (page_buffer * 9) / 10;
  }
unknown's avatar
unknown committed
130 131 132 133

  Pool_context pc;
  pc.m_block = this;
  m_page_request_pool.wo_pool_init(RT_PGMAN_PAGE_REQUEST, pc);
unknown's avatar
unknown committed
134
  
unknown's avatar
unknown committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148
  ReadConfigConf * conf = (ReadConfigConf*)signal->getDataPtrSend();
  conf->senderRef = reference();
  conf->senderData = senderData;
  sendSignal(ref, GSN_READ_CONFIG_CONF, signal, 
	     ReadConfigConf::SignalLength, JBB);
}

Pgman::Param::Param() :
  m_max_pages(64),      // smallish for testing
  m_max_hot_pages(56),
  m_max_loop_count(256),
  m_max_io_waits(64),
  m_stats_loop_delay(1000),
  m_cleanup_loop_delay(200),
149
  m_lcp_loop_delay(0)
unknown's avatar
unknown committed
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
{
}

Pgman::Stats::Stats() :
  m_num_pages(0),
  m_page_hits(0),
  m_page_faults(0),
  m_current_io_waits(0)
{
}

void
Pgman::execSTTOR(Signal* signal)
{
  jamEntry();

  const Uint32 startPhase  = signal->theData[1];

  switch (startPhase) {
  case 1:
    {
      Lgman* lgman = (Lgman*)globalData.getBlock(LGMAN);
      new (&m_lgman) Logfile_client(this, lgman, 0);
      c_tup = (Dbtup*)globalData.getBlock(DBTUP);
    }
    break;
  case 3:
    {
      // start forever loops
      do_stats_loop(signal);
      do_cleanup_loop(signal);
      m_stats_loop_on = true;
      m_cleanup_loop_on = true;
    }
    break;
  case 7:
    break;
  default:
    break;
  }

  sendSTTORRY(signal);
}

void
Pgman::sendSTTORRY(Signal* signal)
{
  signal->theData[0] = 0;
  signal->theData[3] = 1;
  signal->theData[4] = 3;
  signal->theData[5] = 7;
  signal->theData[6] = 255; // No more start phases from missra
  sendSignal(NDBCNTR_REF, GSN_STTORRY, signal, 7, JBB);
}

void
Pgman::execCONTINUEB(Signal* signal)
{
  jamEntry();
  Uint32 data1 = signal->theData[1];

  switch (signal->theData[0]) {
  case PgmanContinueB::STATS_LOOP:
    jam();
    do_stats_loop(signal);
    break;
  case PgmanContinueB::BUSY_LOOP:
    jam();
    do_busy_loop(signal);
    break;
  case PgmanContinueB::CLEANUP_LOOP:
    jam();
    do_cleanup_loop(signal);
    break;
  case PgmanContinueB::LCP_LOOP:
    jam();
    do_lcp_loop(signal);
    break;
unknown's avatar
unknown committed
228
  case PgmanContinueB::LCP_LOCKED:
unknown's avatar
unknown committed
229 230 231 232
  {
    jam();
    Ptr<Page_entry> ptr;
    Page_sublist& pl = *m_page_sublist[Page_entry::SL_LOCKED];
unknown's avatar
unknown committed
233
    if (data1 != RNIL)
unknown's avatar
unknown committed
234
    {
unknown's avatar
unknown committed
235 236
      pl.getPtr(ptr, data1);
      process_lcp_locked(signal, ptr);
unknown's avatar
unknown committed
237 238 239
    }
    else
    {
unknown's avatar
unknown committed
240 241
      signal->theData[0] = m_end_lcp_req.senderData;
      sendSignal(m_end_lcp_req.senderRef, GSN_END_LCP_CONF, signal, 1, JBB);
unknown's avatar
unknown committed
242 243 244
    }
    return;
  }
unknown's avatar
unknown committed
245 246 247 248 249 250 251 252 253 254 255 256 257
  default:
    ndbrequire(false);
    break;
  }
}

// page entry

Pgman::Page_entry::Page_entry(Uint32 file_no, Uint32 page_no) :
  m_state(0),
  m_file_no(file_no),
  m_page_no(page_no),
  m_real_page_i(RNIL),
unknown's avatar
unknown committed
258
  m_copy_page_i(RNIL),
unknown's avatar
unknown committed
259 260
  m_lsn(0),
  m_last_lcp(0),
unknown's avatar
unknown committed
261
  m_dirty_count(0),
unknown's avatar
unknown committed
262 263 264 265 266 267 268 269
  m_busy_count(0),
  m_requests()
{
}

// page lists

Uint32
unknown's avatar
unknown committed
270
Pgman::get_sublist_no(Page_state state)
unknown's avatar
unknown committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
{
  if (state == 0)
  {
    return ZNIL;
  }
  if (state & Page_entry::REQUEST)
  {
    if (! (state & Page_entry::BOUND))
    {
      return Page_entry::SL_BIND;
    }
    if (! (state & Page_entry::MAPPED))
    {
      if (! (state & Page_entry::PAGEIN))
      {
        return Page_entry::SL_MAP;
      }
      return Page_entry::SL_MAP_IO;
    }
    if (! (state & Page_entry::PAGEOUT))
    {
      return Page_entry::SL_CALLBACK;
    }
    return Page_entry::SL_CALLBACK_IO;
  }
  if (state & Page_entry::BUSY)
  {
    return Page_entry::SL_BUSY;
  }
  if (state & Page_entry::LOCKED)
  {
    return Page_entry::SL_LOCKED;
  }
  return Page_entry::SL_OTHER;
}

void
unknown's avatar
unknown committed
308
Pgman::set_page_state(Ptr<Page_entry> ptr, Page_state new_state)
unknown's avatar
unknown committed
309 310 311 312 313 314
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >set_page_state: state=" << hex << new_state << endl;
  debugOut << "PGMAN: " << ptr << ": before" << endl;
#endif

unknown's avatar
unknown committed
315
  Page_state old_state = ptr.p->m_state;
unknown's avatar
unknown committed
316 317 318 319 320 321 322 323 324 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 351 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  if (old_state != new_state)
  {
    Uint32 old_list_no = get_sublist_no(old_state);
    Uint32 new_list_no = get_sublist_no(new_state);
    if (old_state != 0)
    {
      ndbrequire(old_list_no != ZNIL);
      if (old_list_no != new_list_no)
      {
        Page_sublist& old_list = *m_page_sublist[old_list_no];
        old_list.remove(ptr);
      }
    }
    if (new_state != 0)
    {
      ndbrequire(new_list_no != ZNIL);
      if (old_list_no != new_list_no)
      {
        Page_sublist& new_list = *m_page_sublist[new_list_no];
        new_list.add(ptr);
      }
    }
    ptr.p->m_state = new_state;
  }

#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << ": after" << endl;
  debugOut << "PGMAN: <set_page_state" << endl;
#endif
}

// seize/release pages and entries

bool
Pgman::seize_cache_page(Ptr<GlobalPage>& gptr)
{
  // page cache has no own pool yet
  bool ok = m_global_page_pool.seize(gptr);

  // zero is reserved as return value for queued request
  if (ok && gptr.i == 0)
    ok = m_global_page_pool.seize(gptr);

  if (ok)
  {
    ndbrequire(m_stats.m_num_pages < m_param.m_max_pages);
    m_stats.m_num_pages++;
  }
  return ok;
}

void
Pgman::release_cache_page(Uint32 i)
{
  m_global_page_pool.release(i);

  ndbrequire(m_stats.m_num_pages != 0);
  m_stats.m_num_pages--;
}

bool
Pgman::find_page_entry(Ptr<Page_entry>& ptr, Uint32 file_no, Uint32 page_no)
{
  Page_entry key;
  key.m_file_no = file_no;
  key.m_page_no = page_no;
  
  if (m_page_hashlist.find(ptr, key))
  {
#ifdef VM_TRACE
    debugOut << "PGMAN: find_page_entry" << endl;
    debugOut << "PGMAN: " << ptr << endl;
#endif
    return true;
  }
  return false;
}

Uint32
Pgman::seize_page_entry(Ptr<Page_entry>& ptr, Uint32 file_no, Uint32 page_no)
{
  if (m_page_entry_pool.seize(ptr))
  {
    new (ptr.p) Page_entry(file_no, page_no);
    m_page_hashlist.add(ptr);

#ifdef VM_TRACE
    ptr.p->m_this = this;
    debugOut << "PGMAN: seize_page_entry" << endl;
    debugOut << "PGMAN: " << ptr << endl;
#endif

    return true;
  }
  return false;
}

bool
Pgman::get_page_entry(Ptr<Page_entry>& ptr, Uint32 file_no, Uint32 page_no)
{
  if (find_page_entry(ptr, file_no, page_no))
  {
    ndbrequire(ptr.p->m_state != 0);
    m_stats.m_page_hits++;
    return true;
  }

  if (seize_page_entry(ptr, file_no, page_no))
  {
    ndbrequire(ptr.p->m_state == 0);
    m_stats.m_page_faults++;
    return true;
  }

430 431
  ndbrequire(false);
  
unknown's avatar
unknown committed
432 433 434 435 436 437 438 439 440 441
  return false;
}

void
Pgman::release_page_entry(Ptr<Page_entry>& ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: release_page_entry" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif
unknown's avatar
unknown committed
442
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
443 444 445 446 447 448 449

  ndbrequire(ptr.p->m_requests.isEmpty());

  ndbrequire(! (state & Page_entry::ONSTACK));
  ndbrequire(! (state & Page_entry::ONQUEUE));
  ndbrequire(ptr.p->m_real_page_i == RNIL);

unknown's avatar
unknown committed
450 451 452
  if (! (state & Page_entry::LOCKED))
    ndbrequire(! (state & Page_entry::REQUEST));
  
unknown's avatar
unknown committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  set_page_state(ptr, 0);
  m_page_hashlist.remove(ptr);
  m_page_entry_pool.release(ptr);
}

// LIRS

/*
 * After the hot entry at stack bottom is removed, additional entries
 * are removed until next hot entry is found.  There are 3 cases for the
 * removed entry:  1) a bound entry is already on queue 2) an unbound
 * entry with open requests enters queue at bind time 3) an unbound
 * entry without requests is returned to entry pool.
 */
void
Pgman::lirs_stack_prune()
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >lirs_stack_prune" << endl;
#endif
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;
  Ptr<Page_entry> ptr;

  while (pl_stack.first(ptr))      // first is stack bottom
  {
unknown's avatar
unknown committed
479
    Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    if (state & Page_entry::HOT)
    {
      jam();
      break;
    }

#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << ": prune from stack" << endl;
#endif

    pl_stack.remove(ptr);
    state &= ~ Page_entry::ONSTACK;
    set_page_state(ptr, state);

    if (state & Page_entry::BOUND)
    {
      jam();
      ndbrequire(state & Page_entry::ONQUEUE);
    }
    else if (state & Page_entry::REQUEST)
    {
      // enters queue at bind
      jam();
      ndbrequire(! (state & Page_entry::ONQUEUE));
    }
    else
    {
      jam();
      release_page_entry(ptr);
    }
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <lirs_stack_prune" << endl;
#endif
}

/*
 * Remove the hot entry at stack bottom and make it cold and do stack
 * pruning.  There are 2 cases for the removed entry:  1) a bound entry
 * is moved to queue 2) an unbound entry must have requests and enters
 * queue at bind time.
 */
void
Pgman::lirs_stack_pop()
{
#ifdef VM_TRACE
  debugOut << "PGMAN: lirs_stack_pop" << endl;
#endif
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;

  Ptr<Page_entry> ptr;
  bool ok = pl_stack.first(ptr);
  ndbrequire(ok);
unknown's avatar
unknown committed
534
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << ": pop from stack" << endl;
#endif

  ndbrequire(state & Page_entry::HOT);
  ndbrequire(state & Page_entry::ONSTACK);
  pl_stack.remove(ptr);
  state &= ~ Page_entry::HOT;
  state &= ~ Page_entry::ONSTACK;
  ndbrequire(! (state & Page_entry::ONQUEUE));

  if (state & Page_entry::BOUND)
  {
    jam();
    pl_queue.add(ptr);
    state |= Page_entry::ONQUEUE;
  }
  else
  {
    // enters queue at bind
    jam();
    ndbrequire(state & Page_entry::REQUEST);
  }

  set_page_state(ptr, state);
  lirs_stack_prune();
}

/*
 * Update LIRS lists when page is referenced.
 */
void
Pgman::lirs_reference(Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >lirs_reference" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;

unknown's avatar
unknown committed
577
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  ndbrequire(! (state & Page_entry::LOCKED));

  // even non-LIRS cache pages are counted on l.h.s.
  if (m_stats.m_num_pages >= m_param.m_max_hot_pages)
  {
    if (state & Page_entry::HOT)
    {
      // case 1
      jam();
      ndbrequire(state & Page_entry::ONSTACK);
      bool at_bottom = ! pl_stack.hasPrev(ptr);
      pl_stack.remove(ptr);
      pl_stack.add(ptr);
      if (at_bottom)
      {
        jam();
        lirs_stack_prune();
      }
    }
    else if (state & Page_entry::ONSTACK)
    {
      // case 2a 3a
      jam();
      pl_stack.remove(ptr);
      if (! pl_stack.isEmpty())
      {
        jam();
        lirs_stack_pop();
      }
      pl_stack.add(ptr);
      state |= Page_entry::HOT;
      if (state & Page_entry::ONQUEUE)
      {
        jam();
        move_cleanup_ptr(ptr);
        pl_queue.remove(ptr);
        state &= ~ Page_entry::ONQUEUE;
      }
    }
    else
    {
      // case 2b 3b
      jam();
      pl_stack.add(ptr);
      state |= Page_entry::ONSTACK;
      if (state & Page_entry::ONQUEUE)
      {
        jam();
        move_cleanup_ptr(ptr);
        pl_queue.remove(ptr);
      }
      if (state & Page_entry::BOUND)
      {
        jam();
        pl_queue.add(ptr);
        state |= Page_entry::ONQUEUE;
      }
      else
      {
        // enters queue at bind
        jam();
      }
    }
  }
  else
  {
#ifdef VM_TRACE
    debugOut << "PGMAN: filling up initial hot pages: "
             << m_stats.m_num_pages << " of "
             << m_param.m_max_hot_pages << endl;
#endif
    jam();
    if (state & Page_entry::ONSTACK)
    {
      jam();
      pl_stack.remove(ptr);
    }
    pl_stack.add(ptr);
    state |= Page_entry::ONSTACK;
    state |= Page_entry::HOT;
  }

  set_page_state(ptr, state);
#ifdef VM_TRACE
  debugOut << "PGMAN: <lirs_reference" << endl;
#endif
}

// continueB loops

void
Pgman::do_stats_loop(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: do_stats_loop" << endl;
  verify_all();
#endif
  Uint32 delay = m_param.m_stats_loop_delay;
  signal->theData[0] = PgmanContinueB::STATS_LOOP;
  sendSignalWithDelay(PGMAN_REF, GSN_CONTINUEB, signal, delay, 1);
}

void
Pgman::do_busy_loop(Signal* signal, bool direct)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >do_busy_loop on=" << m_busy_loop_on
           << " direct=" << direct << endl;
#endif
  Uint32 restart = false;
  if (direct)
  {
    // may not cover the calling entry
    (void)process_bind(signal);
    (void)process_map(signal);
    // callback must be queued
    if (! m_busy_loop_on)
    {
      restart = true;
      m_busy_loop_on = true;
    }
  }
  else
  {
    ndbrequire(m_busy_loop_on);
    restart += process_bind(signal);
    restart += process_map(signal);
    restart += process_callback(signal);
    if (! restart)
    {
      m_busy_loop_on = false;
    }
  }
  if (restart)
  {
    signal->theData[0] = PgmanContinueB::BUSY_LOOP;
    sendSignal(PGMAN_REF, GSN_CONTINUEB, signal, 1, JBB);
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <do_busy_loop on=" << m_busy_loop_on
           << " restart=" << restart << endl;
#endif
}

void
Pgman::do_cleanup_loop(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: do_cleanup_loop" << endl;
#endif
  process_cleanup(signal);

  Uint32 delay = m_param.m_cleanup_loop_delay;
  signal->theData[0] = PgmanContinueB::CLEANUP_LOOP;
  sendSignalWithDelay(PGMAN_REF, GSN_CONTINUEB, signal, delay, 1);
}

void
Pgman::do_lcp_loop(Signal* signal, bool direct)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >do_lcp_loop on=" << m_lcp_loop_on
           << " direct=" << direct << endl;
#endif
  Uint32 restart = false;
  if (direct)
  {
    ndbrequire(! m_lcp_loop_on);
    restart = true;
    m_lcp_loop_on = true;
  }
  else
  {
    ndbrequire(m_lcp_loop_on);
    restart += process_lcp(signal);
    if (! restart)
    {
      m_lcp_loop_on = false;
    }
  }
  if (restart)
  {
    Uint32 delay = m_param.m_lcp_loop_delay;
    signal->theData[0] = PgmanContinueB::LCP_LOOP;
762 763 764 765
    if (delay)
      sendSignalWithDelay(PGMAN_REF, GSN_CONTINUEB, signal, delay, 1);
    else
      sendSignal(PGMAN_REF, GSN_CONTINUEB, signal, 1, JBB);
unknown's avatar
unknown committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <do_lcp_loop on=" << m_lcp_loop_on
           << " restart=" << restart << endl;
#endif
}

// busy loop

bool
Pgman::process_bind(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >process_bind" << endl;
#endif
  int max_count = 32;
  Page_sublist& pl_bind = *m_page_sublist[Page_entry::SL_BIND];

  while (! pl_bind.isEmpty() && --max_count >= 0)
  {
    jam();
    Ptr<Page_entry> ptr;
    pl_bind.first(ptr);
    if (! process_bind(signal, ptr))
    {
      jam();
      break;
    }
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <process_bind" << endl;
#endif
  return ! pl_bind.isEmpty();
}

bool
Pgman::process_bind(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << " : process_bind" << endl;
#endif
  Page_sublist& pl_bind = *m_page_sublist[Page_entry::SL_BIND];
  Page_queue& pl_queue = m_page_queue;
  Ptr<GlobalPage> gptr;

  if (m_stats.m_num_pages < m_param.m_max_pages)
  {
    jam();
    bool ok = seize_cache_page(gptr);
    // to handle failure requires some changes in LIRS
    ndbrequire(ok);
  }
  else
  {
    jam();
    Ptr<Page_entry> clean_ptr;
    if (! pl_queue.first(clean_ptr))
    {
      jam();
#ifdef VM_TRACE
      debugOut << "PGMAN: bind failed: queue empty" << endl;
#endif
      // XXX busy loop
      return false;
    }
unknown's avatar
unknown committed
831
    Page_state clean_state = clean_ptr.p->m_state;
unknown's avatar
unknown committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    // under unusual circumstances it could still be paging in
    if (! (clean_state & Page_entry::MAPPED) ||
        clean_state & Page_entry::DIRTY ||
        clean_state & Page_entry::REQUEST)
    {
      jam();
#ifdef VM_TRACE
      debugOut << "PGMAN: bind failed: queue front not evictable" << endl;
      debugOut << "PGMAN: " << clean_ptr << endl;
#endif
      // XXX busy loop
      return false;
    }

#ifdef VM_TRACE
    debugOut << "PGMAN: " << clean_ptr << " : evict" << endl;
#endif

unknown's avatar
unknown committed
850
    ndbassert(clean_ptr.p->m_dirty_count == 0);
unknown's avatar
unknown committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    ndbrequire(clean_state & Page_entry::ONQUEUE);
    ndbrequire(clean_state & Page_entry::BOUND);
    ndbrequire(clean_state & Page_entry::MAPPED);

    move_cleanup_ptr(clean_ptr);
    pl_queue.remove(clean_ptr);
    clean_state &= ~ Page_entry::ONQUEUE;

    gptr.i = clean_ptr.p->m_real_page_i;

    clean_ptr.p->m_real_page_i = RNIL;
    clean_state &= ~ Page_entry::BOUND;
    clean_state &= ~ Page_entry::MAPPED;

    set_page_state(clean_ptr, clean_state);

    if (! (clean_state & Page_entry::ONSTACK))
      release_page_entry(clean_ptr);

    m_global_page_pool.getPtr(gptr);
  }

unknown's avatar
unknown committed
873
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

  ptr.p->m_real_page_i = gptr.i;
  state |= Page_entry::BOUND;
  if (state & Page_entry::EMPTY)
  {
    jam();
    state |= Page_entry::MAPPED;
  }

  if (! (state & Page_entry::LOCKED) &&
      ! (state & Page_entry::ONQUEUE) &&
      ! (state & Page_entry::HOT))
  {
    jam();

#ifdef VM_TRACE
    debugOut << "PGMAN: " << ptr << " : add to queue at bind" << endl;
#endif

    pl_queue.add(ptr);
    state |= Page_entry::ONQUEUE;
  }

  set_page_state(ptr, state);
  return true;
}

bool
Pgman::process_map(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >process_map" << endl;
#endif
  int max_count = m_param.m_max_io_waits - m_stats.m_current_io_waits;
  if (max_count > 0)
    max_count = max_count / 2 + 1;
  Page_sublist& pl_map = *m_page_sublist[Page_entry::SL_MAP];

  while (! pl_map.isEmpty() && --max_count >= 0)
  {
    jam();
    Ptr<Page_entry> ptr;
    pl_map.first(ptr);
    if (! process_map(signal, ptr))
    {
      jam();
      break;
    }
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <process_map" << endl;
#endif
  return ! pl_map.isEmpty();
}

bool
Pgman::process_map(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << " : process_map" << endl;
#endif
  pagein(signal, ptr);
  return true;
}

bool
Pgman::process_callback(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >process_callback" << endl;
#endif
  int max_count = 1;
  Page_sublist& pl_callback = *m_page_sublist[Page_entry::SL_CALLBACK];

948 949 950 951
  Ptr<Page_entry> ptr;
  pl_callback.first(ptr);

  while (! ptr.isNull() && --max_count >= 0)
unknown's avatar
unknown committed
952 953
  {
    jam();
954 955 956 957
    Ptr<Page_entry> curr = ptr;
    pl_callback.next(ptr);
    
    if (! process_callback(signal, curr))
unknown's avatar
unknown committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    {
      jam();
      break;
    }
  }
#ifdef VM_TRACE
  debugOut << "PGMAN: <process_callback" << endl;
#endif
  return ! pl_callback.isEmpty();
}

bool
Pgman::process_callback(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << " : process_callback" << endl;
#endif
  int max_count = 1;
unknown's avatar
unknown committed
976
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
977 978 979 980 981 982 983 984 985 986 987

  while (! ptr.p->m_requests.isEmpty() && --max_count >= 0)
  {
    jam();
    SimulatedBlock* b;
    Callback callback;
    {
      /**
       * Make sure list is in own scope if callback will access this
       * list again (destructor restores list head).
       */
unknown's avatar
unknown committed
988
      Local_page_request_list req_list(m_page_request_pool, ptr.p->m_requests);
unknown's avatar
unknown committed
989 990 991 992 993 994
      Ptr<Page_request> req_ptr;

      req_list.first(req_ptr);
#ifdef VM_TRACE
      debugOut << "PGMAN: " << req_ptr << " : process_callback" << endl;
#endif
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

#ifdef ERROR_INSERT
      if (req_ptr.p->m_flags & Page_request::DELAY_REQ)
      {
	Uint64 now = NdbTick_CurrentMillisecond();
	if (now < req_ptr.p->m_delay_until_time)
	{
	  break;
	}
      }
#endif
      
unknown's avatar
unknown committed
1007 1008 1009 1010 1011 1012 1013
      b = globalData.getBlock(req_ptr.p->m_block);
      callback = req_ptr.p->m_callback;
      
      if (req_ptr.p->m_flags & DIRTY_FLAGS)
      {
        jam();
        state |= Page_entry::DIRTY;
unknown's avatar
unknown committed
1014 1015
	ndbassert(ptr.p->m_dirty_count);
	ptr.p->m_dirty_count --;
unknown's avatar
unknown committed
1016
      }
unknown's avatar
unknown committed
1017 1018

      req_list.releaseFirst(req_ptr);
unknown's avatar
unknown committed
1019 1020 1021
    }
    ndbrequire(state & Page_entry::BOUND);
    ndbrequire(state & Page_entry::MAPPED);
unknown's avatar
unknown committed
1022
    
unknown's avatar
unknown committed
1023 1024 1025 1026 1027
    // callback may re-enter PGMAN and change page state
    set_page_state(ptr, state);
    b->execute(signal, callback, ptr.p->m_real_page_i);
    state = ptr.p->m_state;
  }
unknown's avatar
unknown committed
1028
  
unknown's avatar
unknown committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  if (ptr.p->m_requests.isEmpty())
  {
    jam();
    state &= ~ Page_entry::REQUEST;
  }
  set_page_state(ptr, state);
  return true;
}

// cleanup loop

bool
Pgman::process_cleanup(Signal* signal)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: >process_cleanup" << endl;
#endif
  Page_queue& pl_queue = m_page_queue;

  // XXX for now start always from beginning
  m_cleanup_ptr.i = RNIL;

  if (m_cleanup_ptr.i == RNIL && ! pl_queue.first(m_cleanup_ptr))
  {
    jam();
#ifdef VM_TRACE
    debugOut << "PGMAN: <process_cleanup: empty queue" << endl;
#endif
    return false;
  }

  int max_loop_count = m_param.m_max_loop_count;
  int max_count = m_param.m_max_io_waits - m_stats.m_current_io_waits;

  if (max_count > 0)
  {
    max_count = max_count / 2 + 1;
    /*
     * Possibly add code here to avoid writing too rapidly.  May be
     * unnecessary since only cold pages are cleaned.
     */
  }

  Ptr<Page_entry> ptr = m_cleanup_ptr;
  while (max_loop_count != 0 && max_count != 0)
  {
unknown's avatar
unknown committed
1075
    Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    ndbrequire(! (state & Page_entry::LOCKED));
    if (state & Page_entry::BUSY)
    {
#ifdef VM_TRACE
      debugOut << "PGMAN: process_cleanup: break on busy page" << endl;
      debugOut << "PGMAN: " << ptr << endl;
#endif
      break;
    }
    if (state & Page_entry::DIRTY &&
        ! (state & Page_entry::PAGEIN) &&
        ! (state & Page_entry::PAGEOUT))
    {
#ifdef VM_TRACE
      debugOut << "PGMAN: " << ptr << " : process_cleanup" << endl;
#endif
unknown's avatar
unknown committed
1092 1093
      c_tup->disk_page_unmap_callback(ptr.p->m_real_page_i, 
				      ptr.p->m_dirty_count);
unknown's avatar
unknown committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
      pageout(signal, ptr);
      max_count--;
    }
    if (! pl_queue.hasNext(ptr))
      break;
    pl_queue.next(ptr);
    max_loop_count--;
  }
  m_cleanup_ptr = ptr;
#ifdef VM_TRACE
  debugOut << "PGMAN: <process_cleanup" << endl;
#endif
  return true;
}

/*
 * Call this before queue.remove(ptr).  If the removed entry is the
 * clean-up pointer, move it towards front.
 */
void
Pgman::move_cleanup_ptr(Ptr<Page_entry> ptr)
{
  Page_queue& pl_queue = m_page_queue;
  if (ptr.i == m_cleanup_ptr.i)
  {
    jam();
    pl_queue.prev(m_cleanup_ptr);
  }
}

// LCP

unknown's avatar
unknown committed
1126

unknown's avatar
unknown committed
1127 1128 1129 1130 1131 1132
void
Pgman::execLCP_FRAG_ORD(Signal* signal)
{
  LcpFragOrd* ord = (LcpFragOrd*)signal->getDataPtr();
  ndbrequire(ord->lcpId >= m_last_lcp_complete + 1 || m_last_lcp_complete == 0);
  m_last_lcp = ord->lcpId;
unknown's avatar
unknown committed
1133
  DBG_LCP("Pgman::execLCP_FRAG_ORD lcp: " << m_last_lcp << endl);
unknown's avatar
unknown committed
1134
  
unknown's avatar
unknown committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
#ifdef VM_TRACE
  debugOut
    << "PGMAN: execLCP_FRAG_ORD"
    << " this=" << m_last_lcp << " last_complete=" << m_last_lcp_complete
    << " bucket=" << m_lcp_curr_bucket << endl;
#endif
}

void
Pgman::execEND_LCP_REQ(Signal* signal)
{
  EndLcpReq* req = (EndLcpReq*)signal->getDataPtr();
  m_end_lcp_req = *req;

1149 1150
  DBG_LCP("execEND_LCP_REQ" << endl);

unknown's avatar
unknown committed
1151 1152 1153
  ndbrequire(!m_lcp_outstanding);
  m_lcp_curr_bucket = 0;
  
unknown's avatar
unknown committed
1154 1155 1156 1157 1158 1159 1160 1161 1162
#ifdef VM_TRACE
  debugOut
    << "PGMAN: execEND_LCP_REQ"
    << " this=" << m_last_lcp << " last_complete=" << m_last_lcp_complete
    << " bucket=" << m_lcp_curr_bucket
    << " outstanding=" << m_lcp_outstanding << endl;
#endif

  m_last_lcp_complete = m_last_lcp;
unknown's avatar
unknown committed
1163 1164
  
  do_lcp_loop(signal, true);
unknown's avatar
unknown committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
}

bool
Pgman::process_lcp(Signal* signal)
{
  Page_hashlist& pl_hash = m_page_hashlist;
  int max_count = m_param.m_max_io_waits - m_stats.m_current_io_waits;
  if (max_count > 0)
    max_count = max_count / 2 + 1;

#ifdef VM_TRACE
  debugOut
    << "PGMAN: process_lcp"
    << " this=" << m_last_lcp << " last_complete=" << m_last_lcp_complete
    << " bucket=" << m_lcp_curr_bucket
    << " outstanding=" << m_lcp_outstanding << endl;
#endif

  // start or re-start from beginning of current hash bucket
  if (m_lcp_curr_bucket != ~(Uint32)0)
  {
    Page_hashlist::Iterator iter;
    pl_hash.next(m_lcp_curr_bucket, iter);
unknown's avatar
unknown committed
1188 1189
    Uint32 loop = 0;
    while (iter.curr.i != RNIL && 
unknown's avatar
unknown committed
1190
	   m_lcp_outstanding < (Uint32) max_count &&
unknown's avatar
unknown committed
1191
	   (loop ++ < 32 || iter.bucket == m_lcp_curr_bucket))
unknown's avatar
unknown committed
1192 1193
    {
      Ptr<Page_entry>& ptr = iter.curr;
unknown's avatar
unknown committed
1194
      Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1195
      
unknown's avatar
unknown committed
1196
      DBG_LCP("LCP " << ptr << " - ");
1197
      
unknown's avatar
unknown committed
1198
      if (ptr.p->m_last_lcp < m_last_lcp &&
unknown's avatar
unknown committed
1199 1200
          (state & Page_entry::DIRTY) &&
	  (! (state & Page_entry::LOCKED)))
unknown's avatar
unknown committed
1201 1202 1203 1204 1205 1206 1207 1208
      {
        if(! (state & Page_entry::BOUND))
        {
          ndbout << ptr << endl;
          ndbrequire(false);
        }
        if (state & Page_entry::BUSY)
        {
1209
	  DBG_LCP(" BUSY" << endl);
unknown's avatar
unknown committed
1210
          break;  // wait for it
unknown's avatar
unknown committed
1211 1212
        } 
	else if (state & Page_entry::PAGEOUT)
unknown's avatar
unknown committed
1213
        {
1214
	  DBG_LCP(" PAGEOUT -> state |= LCP" << endl);
unknown's avatar
unknown committed
1215 1216 1217 1218
          set_page_state(ptr, state | Page_entry::LCP);
        }
        else
        {
1219
	  DBG_LCP(" pageout()" << endl);
unknown's avatar
unknown committed
1220
          ptr.p->m_state |= Page_entry::LCP;
unknown's avatar
unknown committed
1221 1222
	  c_tup->disk_page_unmap_callback(ptr.p->m_real_page_i, 
					  ptr.p->m_dirty_count);
unknown's avatar
unknown committed
1223 1224 1225 1226 1227
          pageout(signal, ptr);
        }
        ptr.p->m_last_lcp = m_last_lcp;
        m_lcp_outstanding++;
      }
unknown's avatar
unknown committed
1228 1229 1230 1231
      else
      {
	DBG_LCP(" NOT DIRTY" << endl);
      }	
unknown's avatar
unknown committed
1232 1233
      pl_hash.next(iter);
    }
unknown's avatar
unknown committed
1234
    
unknown's avatar
unknown committed
1235 1236 1237 1238 1239
    m_lcp_curr_bucket = (iter.curr.i != RNIL ? iter.bucket : ~(Uint32)0);
  }

  if (m_lcp_curr_bucket == ~(Uint32)0  && !m_lcp_outstanding)
  {
unknown's avatar
unknown committed
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    Ptr<Page_entry> ptr;
    Page_sublist& pl = *m_page_sublist[Page_entry::SL_LOCKED];
    if (pl.first(ptr))
    {
      process_lcp_locked(signal, ptr);
    }
    else
    {
      signal->theData[0] = m_end_lcp_req.senderData;
      sendSignal(m_end_lcp_req.senderRef, GSN_END_LCP_CONF, signal, 1, JBB);
    }
unknown's avatar
unknown committed
1251 1252
    return false;
  }
unknown's avatar
unknown committed
1253
  
unknown's avatar
unknown committed
1254 1255 1256
  return true;
}

unknown's avatar
unknown committed
1257 1258 1259
void
Pgman::process_lcp_locked(Signal* signal, Ptr<Page_entry> ptr)
{
unknown's avatar
unknown committed
1260 1261
  CRASH_INSERTION(11006);

unknown's avatar
unknown committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
  ptr.p->m_last_lcp = m_last_lcp;
  if (ptr.p->m_state & Page_entry::DIRTY)
  {
    Ptr<GlobalPage> org, copy;
    ndbrequire(m_global_page_pool.seize(copy));
    m_global_page_pool.getPtr(org, ptr.p->m_real_page_i);
    memcpy(copy.p, org.p, sizeof(GlobalPage));
    ptr.p->m_copy_page_i = copy.i;
    
    m_lcp_outstanding++;
    ptr.p->m_state |= Page_entry::LCP;
    pageout(signal, ptr);
    return;
  }
  
  Page_sublist& pl = *m_page_sublist[Page_entry::SL_LOCKED];
  pl.next(ptr);
  
  signal->theData[0] = PgmanContinueB::LCP_LOCKED;
  signal->theData[1] = ptr.i;
  sendSignal(reference(), GSN_CONTINUEB, signal, 2, JBB);
}

void
Pgman::process_lcp_locked_fswriteconf(Signal* signal, Ptr<Page_entry> ptr)
{
  Ptr<GlobalPage> org, copy;
  m_global_page_pool.getPtr(copy, ptr.p->m_copy_page_i);
  m_global_page_pool.getPtr(org, ptr.p->m_real_page_i);
  memcpy(org.p, copy.p, sizeof(GlobalPage));
  m_global_page_pool.release(copy);

  ptr.p->m_copy_page_i = RNIL;

  Page_sublist& pl = *m_page_sublist[Page_entry::SL_LOCKED];
  pl.next(ptr);
  
  signal->theData[0] = PgmanContinueB::LCP_LOCKED;
  signal->theData[1] = ptr.i;
  sendSignal(reference(), GSN_CONTINUEB, signal, 2, JBB);
}

unknown's avatar
unknown committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
// page read and write

void
Pgman::pagein(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: pagein" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif

  ndbrequire(! (ptr.p->m_state & Page_entry::PAGEIN));
  set_page_state(ptr, ptr.p->m_state | Page_entry::PAGEIN);

  fsreadreq(signal, ptr);
  m_stats.m_current_io_waits++;
}

void
Pgman::fsreadconf(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: fsreadconf" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif
  ndbrequire(ptr.p->m_state & Page_entry::PAGEIN);
unknown's avatar
unknown committed
1329
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1330 1331 1332 1333 1334 1335

  state &= ~ Page_entry::PAGEIN;
  state &= ~ Page_entry::EMPTY;
  state |= Page_entry::MAPPED;
  set_page_state(ptr, state);

unknown's avatar
unknown committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
  {
    /**
     * Update lsn record on page
     *   as it can be modified/flushed wo/ update_lsn has been called
     *   (e.g. prealloc) and it then would get lsn 0, which is bad
     *   when running undo and following SR
     */
    Ptr<GlobalPage> pagePtr;
    m_global_page_pool.getPtr(pagePtr, ptr.p->m_real_page_i);
    File_formats::Datafile::Data_page* page =
      (File_formats::Datafile::Data_page*)pagePtr.p;
    
    Uint64 lsn = 0;
    lsn += page->m_page_header.m_page_lsn_hi; lsn <<= 32;
    lsn += page->m_page_header.m_page_lsn_lo;
    ptr.p->m_lsn = lsn;
  }
  
unknown's avatar
unknown committed
1354 1355 1356
  ndbrequire(m_stats.m_current_io_waits > 0);
  m_stats.m_current_io_waits--;

unknown's avatar
unknown committed
1357
  ptr.p->m_last_lcp = m_last_lcp_complete;
unknown's avatar
unknown committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
  do_busy_loop(signal, true);
}

void
Pgman::pageout(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: pageout" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif
  
unknown's avatar
unknown committed
1369
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1370 1371 1372 1373 1374 1375
  ndbrequire(state & Page_entry::BOUND);
  ndbrequire(state & Page_entry::MAPPED);
  ndbrequire(! (state & Page_entry::BUSY));
  ndbrequire(! (state & Page_entry::PAGEOUT));

  state |= Page_entry::PAGEOUT;
unknown's avatar
unknown committed
1376

unknown's avatar
unknown committed
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
  // update lsn on page prior to write
  Ptr<GlobalPage> pagePtr;
  m_global_page_pool.getPtr(pagePtr, ptr.p->m_real_page_i);
  File_formats::Datafile::Data_page* page =
    (File_formats::Datafile::Data_page*)pagePtr.p;
  page->m_page_header.m_page_lsn_hi = ptr.p->m_lsn >> 32;
  page->m_page_header.m_page_lsn_lo = ptr.p->m_lsn & 0xFFFFFFFF;

  // undo WAL
  Logfile_client::Request req;
  req.m_callback.m_callbackData = ptr.i;
  req.m_callback.m_callbackFunction = safe_cast(&Pgman::logsync_callback);
  int ret = m_lgman.sync_lsn(signal, ptr.p->m_lsn, &req, 0);
  if (ret > 0)
  {
    fswritereq(signal, ptr);
    m_stats.m_current_io_waits++;
  }
  else
  {
    ndbrequire(ret == 0);
    state |= Page_entry::LOGSYNC;
  }
  set_page_state(ptr, state);
}

void
Pgman::logsync_callback(Signal* signal, Uint32 ptrI, Uint32 res)
{
  Ptr<Page_entry> ptr;
  m_page_entry_pool.getPtr(ptr, ptrI);

#ifdef VM_TRACE
  debugOut << "PGMAN: logsync_callback" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif

  // it is OK to be "busy" at this point (the commit is queued)
unknown's avatar
unknown committed
1415
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
  ndbrequire(state & Page_entry::PAGEOUT);
  ndbrequire(state & Page_entry::LOGSYNC);
  state &= ~ Page_entry::LOGSYNC;
  set_page_state(ptr, state);

  fswritereq(signal, ptr);
  m_stats.m_current_io_waits++;
}

void
Pgman::fswriteconf(Signal* signal, Ptr<Page_entry> ptr)
{
#ifdef VM_TRACE
  debugOut << "PGMAN: fswriteconf" << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif

unknown's avatar
unknown committed
1433
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
  ndbrequire(state & Page_entry::PAGEOUT);

  state &= ~ Page_entry::PAGEOUT;
  state &= ~ Page_entry::EMPTY;
  state &= ~ Page_entry::DIRTY;

  ndbrequire(m_stats.m_current_io_waits > 0);
  m_stats.m_current_io_waits--;

  if (state & Page_entry::LCP)
  {
    ndbrequire(m_lcp_outstanding);
    m_lcp_outstanding--;
unknown's avatar
unknown committed
1447
    state &= ~ Page_entry::LCP;
unknown's avatar
unknown committed
1448 1449 1450 1451 1452
    
    if (ptr.p->m_copy_page_i != RNIL)
    {
      process_lcp_locked_fswriteconf(signal, ptr);
    }
unknown's avatar
unknown committed
1453
  }
unknown's avatar
unknown committed
1454
  
unknown's avatar
unknown committed
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
  set_page_state(ptr, state);
  do_busy_loop(signal, true);
}

// file system interface

void
Pgman::fsreadreq(Signal* signal, Ptr<Page_entry> ptr)
{
  File_map::ConstDataBufferIterator it;
unknown's avatar
unknown committed
1465 1466
  bool ret = m_file_map.first(it) && m_file_map.next(it, ptr.p->m_file_no);
  ndbrequire(ret);
unknown's avatar
unknown committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
  Uint32 fd = * it.data;

  ndbrequire(ptr.p->m_page_no > 0);

  FsReadWriteReq* req = (FsReadWriteReq*)signal->getDataPtrSend();
  req->filePointer = fd;
  req->userReference = reference();
  req->userPointer = ptr.i;
  req->varIndex = ptr.p->m_page_no;
  req->numberOfPages = 1;
  req->operationFlag = 0;
  FsReadWriteReq::setFormatFlag(req->operationFlag,
				FsReadWriteReq::fsFormatGlobalPage);
  req->data.pageData[0] = ptr.p->m_real_page_i;
  sendSignal(NDBFS_REF, GSN_FSREADREQ, signal,
unknown's avatar
unknown committed
1482
	     FsReadWriteReq::FixedLength + 1, JBA);
unknown's avatar
unknown committed
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
}

void
Pgman::execFSREADCONF(Signal* signal)
{
  jamEntry();
  FsConf* conf = (FsConf*)signal->getDataPtr();
  Ptr<Page_entry> ptr;
  m_page_entry_pool.getPtr(ptr, conf->userPointer);

  fsreadconf(signal, ptr);
}

void
Pgman::execFSREADREF(Signal* signal)
{
  jamEntry();
  SimulatedBlock::execFSREADREF(signal);
  ndbrequire(false);
}

void
Pgman::fswritereq(Signal* signal, Ptr<Page_entry> ptr)
{
  File_map::ConstDataBufferIterator it;
  m_file_map.first(it);
  m_file_map.next(it, ptr.p->m_file_no);
  Uint32 fd = * it.data;

  ndbrequire(ptr.p->m_page_no > 0);

  FsReadWriteReq* req = (FsReadWriteReq*)signal->getDataPtrSend();
  req->filePointer = fd;
  req->userReference = reference();
  req->userPointer = ptr.i;
  req->varIndex = ptr.p->m_page_no;
  req->numberOfPages = 1;
  req->operationFlag = 0;
  FsReadWriteReq::setFormatFlag(req->operationFlag,
				FsReadWriteReq::fsFormatGlobalPage);
  req->data.pageData[0] = ptr.p->m_real_page_i;
unknown's avatar
unknown committed
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

#if ERROR_INSERT_CODE
  if (ptr.p->m_state & Page_entry::LOCKED)
  {
    sendSignalWithDelay(NDBFS_REF, GSN_FSWRITEREQ, signal,
			3000, FsReadWriteReq::FixedLength + 1);
    ndbout_c("pageout locked (3s)");
    return;
  }
#endif
  
unknown's avatar
unknown committed
1535
  sendSignal(NDBFS_REF, GSN_FSWRITEREQ, signal,
unknown's avatar
unknown committed
1536
	     FsReadWriteReq::FixedLength + 1, JBA);
unknown's avatar
unknown committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
}

void
Pgman::execFSWRITECONF(Signal* signal)
{
  jamEntry();
  FsConf* conf = (FsConf*)signal->getDataPtr();
  Ptr<Page_entry> ptr;
  m_page_entry_pool.getPtr(ptr, conf->userPointer);

  fswriteconf(signal, ptr);
}


void
Pgman::execFSWRITEREF(Signal* signal)
{
  jamEntry();
  SimulatedBlock::execFSWRITEREF(signal);
  ndbrequire(false);
}

// client methods

int
Pgman::get_page(Signal* signal, Ptr<Page_entry> ptr, Page_request page_req)
{
#ifdef VM_TRACE
  Ptr<Page_request> tmp = { &page_req, RNIL};
  debugOut << "PGMAN: >get_page" << endl;
  debugOut << "PGMAN: " << ptr << endl;
  debugOut << "PGMAN: " << tmp << endl;
#endif
  Uint32 req_flags = page_req.m_flags;

  if (req_flags & Page_request::EMPTY_PAGE)
  {
    // Only one can "init" a page at a time
    //ndbrequire(ptr.p->m_requests.isEmpty());
  }

unknown's avatar
unknown committed
1578
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
  bool is_new = (state == 0);
  bool busy_count = false;

  if (req_flags & Page_request::LOCK_PAGE)
  {
    jam();
    state |= Page_entry::LOCKED;
  }
  
  if (req_flags & Page_request::ALLOC_REQ)
  {
    jam();
  }
  else if (req_flags & Page_request::COMMIT_REQ)
  {
    busy_count = true;
    state |= Page_entry::BUSY;
  }
  else if ((req_flags & Page_request::OP_MASK) != ZREAD)
  {
    jam();
  }

  // update LIRS
  if (! (state & Page_entry::LOCKED) &&
      ! (req_flags & Page_request::CORR_REQ))
  {
    jam();
    set_page_state(ptr, state);
    lirs_reference(ptr);
    state = ptr.p->m_state;
  }

unknown's avatar
unknown committed
1612 1613 1614 1615 1616
  const Page_state LOCKED = Page_entry::LOCKED | Page_entry::MAPPED;
  if ((state & LOCKED) == LOCKED && 
      ! (req_flags & Page_request::UNLOCK_PAGE))
  {
    ptr.p->m_state |= (req_flags & DIRTY_FLAGS ? Page_entry::DIRTY : 0);
unknown's avatar
unknown committed
1617
    if (ptr.p->m_copy_page_i != RNIL)
unknown's avatar
unknown committed
1618
    {
unknown's avatar
unknown committed
1619
      return ptr.p->m_copy_page_i;
unknown's avatar
unknown committed
1620 1621 1622 1623 1624
    }
    
    return ptr.p->m_real_page_i;
  }
  
unknown's avatar
unknown committed
1625
  bool only_request = ptr.p->m_requests.isEmpty();
unknown's avatar
unknown committed
1626
#ifdef ERROR_INSERT
1627 1628 1629 1630 1631
  if (req_flags & Page_request::DELAY_REQ)
  {
    jam();
    only_request = false;
  }
unknown's avatar
unknown committed
1632
#endif  
unknown's avatar
unknown committed
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
  if (only_request &&
      state & Page_entry::MAPPED)
  {
    if (! (state & Page_entry::PAGEOUT))
    {
      if (req_flags & DIRTY_FLAGS)
	state |= Page_entry::DIRTY;
      
      ptr.p->m_busy_count += busy_count;
      set_page_state(ptr, state);
      
#ifdef VM_TRACE
      debugOut << "PGMAN: <get_page: immediate" << endl;
#endif

      ndbrequire(ptr.p->m_real_page_i != RNIL);
      return ptr.p->m_real_page_i;
    }
  }
unknown's avatar
unknown committed
1652 1653

  if (! (req_flags & (Page_request::LOCK_PAGE | Page_request::UNLOCK_PAGE)))
unknown's avatar
unknown committed
1654 1655 1656 1657 1658 1659 1660
  {
    ndbrequire(! (state & Page_entry::LOCKED));
  }

  // queue the request
  Ptr<Pgman::Page_request> req_ptr;
  {
unknown's avatar
unknown committed
1661
    Local_page_request_list req_list(m_page_request_pool, ptr.p->m_requests);
unknown's avatar
unknown committed
1662
    if (! (req_flags & Page_request::ALLOC_REQ))
unknown's avatar
unknown committed
1663
      req_list.seizeLast(req_ptr);
unknown's avatar
unknown committed
1664
    else
unknown's avatar
unknown committed
1665
      req_list.seizeFirst(req_ptr);
unknown's avatar
unknown committed
1666
  }
unknown's avatar
unknown committed
1667
  
unknown's avatar
unknown committed
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
  if (req_ptr.i == RNIL)
  {
    if (is_new)
    {
      release_page_entry(ptr);
    }
    return -1;
  }

  req_ptr.p->m_block = page_req.m_block;
  req_ptr.p->m_flags = page_req.m_flags;
  req_ptr.p->m_callback = page_req.m_callback;
1680 1681 1682 1683
#ifdef ERROR_INSERT
  req_ptr.p->m_delay_until_time = page_req.m_delay_until_time;
#endif
  
unknown's avatar
unknown committed
1684 1685 1686 1687 1688 1689 1690 1691
  state |= Page_entry::REQUEST;
  if (only_request && req_flags & Page_request::EMPTY_PAGE)
  {
    state |= Page_entry::EMPTY;
  }

  if (req_flags & Page_request::UNLOCK_PAGE)
  {
unknown's avatar
unknown committed
1692
    // keep it locked
unknown's avatar
unknown committed
1693 1694 1695
  }
  
  ptr.p->m_busy_count += busy_count;
unknown's avatar
unknown committed
1696
  ptr.p->m_dirty_count += !!(req_flags & DIRTY_FLAGS);
unknown's avatar
unknown committed
1697
  set_page_state(ptr, state);
unknown's avatar
unknown committed
1698
  
unknown's avatar
unknown committed
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
  do_busy_loop(signal, true);

#ifdef VM_TRACE
  debugOut << "PGMAN: " << req_ptr << endl;
  debugOut << "PGMAN: <get_page: queued" << endl;
#endif
  return 0;
}

void
Pgman::update_lsn(Ptr<Page_entry> ptr, Uint32 block, Uint64 lsn)
{
#ifdef VM_TRACE
  const char* bname = getBlockName(block, "?");
  debugOut << "PGMAN: >update_lsn: block=" << bname << " lsn=" << lsn << endl;
  debugOut << "PGMAN: " << ptr << endl;
#endif

unknown's avatar
unknown committed
1717
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
  ptr.p->m_lsn = lsn;
  
  if (state & Page_entry::BUSY)
  {
    ndbrequire(ptr.p->m_busy_count != 0);
    if (--ptr.p->m_busy_count == 0)
    {
      state &= ~ Page_entry::BUSY;
    }
  }
  
  state |= Page_entry::DIRTY;
  set_page_state(ptr, state);
  
#ifdef VM_TRACE
  debugOut << "PGMAN: " << ptr << endl;
  debugOut << "PGMAN: <update_lsn" << endl;
#endif
}

Uint32
Pgman::create_data_file()
{
  File_map::DataBufferIterator it;
  if(m_file_map.first(it))
  {
    do 
    {
      if(*it.data == RNIL)
      {
	*it.data = (1u << 31) | it.pos;
	return it.pos;
      }
    } while(m_file_map.next(it));
  }

  Uint32 file_no = m_file_map.getSize();
  Uint32 fd = (1u << 31) | file_no;

  if (m_file_map.append(&fd, 1))
  {
    return file_no;
  }
  return RNIL;
}

Uint32
Pgman::alloc_data_file(Uint32 file_no)
{
  Uint32 sz = m_file_map.getSize();
  if (file_no >= sz)
  {
    Uint32 len = file_no - sz + 1;
    Uint32 fd = RNIL;
    while (len--)
    {
      if (! m_file_map.append(&fd, 1))
	return RNIL;
    }
  }

  File_map::DataBufferIterator it;
  m_file_map.first(it);
  m_file_map.next(it, file_no);
  if (* it.data != RNIL)
    return RNIL;

  *it.data = (1u << 31) | file_no;
  return file_no;
}

void
Pgman::map_file_no(Uint32 file_no, Uint32 fd)
{
  File_map::DataBufferIterator it;
  m_file_map.first(it);
  m_file_map.next(it, file_no);

  assert(*it.data == ((1u << 31) | file_no));
  *it.data = fd;
}

void
Pgman::free_data_file(Uint32 file_no, Uint32 fd)
{
  File_map::DataBufferIterator it;
  m_file_map.first(it);
  m_file_map.next(it, file_no);
  
  if (fd == RNIL)
  {
    ndbrequire(*it.data == ((1u << 31) | file_no));
  }
  else
  {
    ndbrequire(*it.data == fd);
  }
  *it.data = RNIL;
}

int
Pgman::drop_page(Ptr<Page_entry> ptr)
{
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;

unknown's avatar
unknown committed
1824
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
  if (! (state & (Page_entry::PAGEIN | Page_entry::PAGEOUT)))
  {
    ndbrequire(state & Page_entry::BOUND);
    ndbrequire(state & Page_entry::MAPPED);

    if (state & Page_entry::ONSTACK)
    {
      jam();
      pl_stack.remove(ptr);
      state &= ~ Page_entry::ONSTACK;
    }

    if (state & Page_entry::ONQUEUE)
    {
      jam();
      pl_queue.remove(ptr);
      state &= ~ Page_entry::ONQUEUE;
    }

    if (ptr.p->m_real_page_i != RNIL)
    {
      jam();
      release_cache_page(ptr.p->m_real_page_i);
      ptr.p->m_real_page_i = RNIL;
    }

    set_page_state(ptr, state);
    release_page_entry(ptr);
    return 1;
  }
  
  ndbrequire(false);
  return -1;
}

// debug

#ifdef VM_TRACE

void
Pgman::verify_page_entry(Ptr<Page_entry> ptr)
{
  Uint32 ptrI = ptr.i;
unknown's avatar
unknown committed
1868
  Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956

  bool has_req = state & Page_entry::REQUEST;
  bool has_req2 = ! ptr.p->m_requests.isEmpty();
  ndbrequire(has_req == has_req2 || dump_page_lists(ptrI));

  bool is_bound = state & Page_entry::BOUND;
  bool is_bound2 = ptr.p->m_real_page_i != RNIL;
  ndbrequire(is_bound == is_bound2 || dump_page_lists(ptrI));

  bool is_mapped = state & Page_entry::MAPPED;
  // mapped implies bound
  ndbrequire(! is_mapped || is_bound || dump_page_lists(ptrI));
  // bound is mapped or has open requests
  ndbrequire(! is_bound || is_mapped || has_req || dump_page_lists(ptrI));

  bool on_stack = state & Page_entry::ONSTACK;
  bool is_hot = state & Page_entry::HOT;
  // hot entry must be on stack
  ndbrequire(! is_hot || on_stack || dump_page_lists(ptrI));

  bool on_queue = state & Page_entry::ONQUEUE;
  // hot entry is not on queue
  ndbrequire(! is_hot || ! on_queue || dump_page_lists(ptrI));

  bool is_locked = state & Page_entry::LOCKED;
  bool on_queue2 = ! is_locked && ! is_hot && is_bound;
  ndbrequire(on_queue == on_queue2 || dump_page_lists(ptrI));

  // entries waiting to enter queue
  bool to_queue = ! is_locked && ! is_hot && ! is_bound && has_req;

  // page is either LOCKED or under LIRS
  bool is_lirs = on_stack || to_queue || on_queue;
  ndbrequire(is_locked == ! is_lirs || dump_page_lists(ptrI));

  bool pagein = state & Page_entry::PAGEIN;
  bool pageout = state & Page_entry::PAGEOUT;
  // cannot read and write at same time
  ndbrequire(! pagein || ! pageout || dump_page_lists(ptrI));

  Uint32 no = get_sublist_no(state);
  switch (no) {
  case Page_entry::SL_BIND:
    ndbrequire(! pagein && ! pageout || dump_page_lists(ptrI));
    break;
  case Page_entry::SL_MAP:
    ndbrequire(! pagein && ! pageout || dump_page_lists(ptrI));
    break;
  case Page_entry::SL_MAP_IO:
    ndbrequire(pagein && ! pageout || dump_page_lists(ptrI));
    break;
  case Page_entry::SL_CALLBACK:
    ndbrequire(! pagein && ! pageout || dump_page_lists(ptrI));
    break;
  case Page_entry::SL_CALLBACK_IO:
    ndbrequire(! pagein && pageout || dump_page_lists(ptrI));
    break;
  case Page_entry::SL_BUSY:
    break;
  case Page_entry::SL_LOCKED:
    break;
  case Page_entry::SL_OTHER:
    break;
  default:
    ndbrequire(false || dump_page_lists(ptrI));
    break;
  }
}

void
Pgman::verify_page_lists()
{
  Page_hashlist& pl_hash = m_page_hashlist;
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;
  Ptr<Page_entry> ptr;

  Uint32 stack_count = 0;
  Uint32 queue_count = 0;
  Uint32 queuewait_count = 0;
  Uint32 locked_bound_count = 0;

  Page_hashlist::Iterator iter;
  pl_hash.next(0, iter);
  while (iter.curr.i != RNIL)
  {
    verify_page_entry(iter.curr);

unknown's avatar
unknown committed
1957
    Page_state state = iter.curr.p->m_state;
unknown's avatar
unknown committed
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    if (state & Page_entry::ONSTACK)
      stack_count++;
    if (state & Page_entry::ONQUEUE)
      queue_count++;
    if (! (state & Page_entry::LOCKED) &&
        ! (state & Page_entry::HOT) &&
        (state & Page_entry::REQUEST) &&
        ! (state & Page_entry::BOUND))
      queuewait_count++;
    if (state & Page_entry::LOCKED &&
        state & Page_entry::BOUND)
      locked_bound_count++;
    pl_hash.next(iter);
  }

  ndbrequire(stack_count == pl_stack.count() || dump_page_lists());
  ndbrequire(queue_count == pl_queue.count() || dump_page_lists());

  Uint32 hot_bound_count = 0;
  Uint32 cold_bound_count = 0;

  Uint32 i1 = RNIL;
  for (pl_stack.first(ptr); ptr.i != RNIL; pl_stack.next(ptr))
  {
    ndbrequire(i1 != ptr.i);
    i1 = ptr.i;
unknown's avatar
unknown committed
1984
    Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    ndbrequire(state & Page_entry::ONSTACK || dump_page_lists());
    if (! pl_stack.hasPrev(ptr))
      ndbrequire(state & Page_entry::HOT || dump_page_lists());
    if (state & Page_entry::HOT &&
        state & Page_entry::BOUND)
      hot_bound_count++;
  }

  Uint32 i2 = RNIL;
  for (pl_queue.first(ptr); ptr.i != RNIL; pl_queue.next(ptr))
  {
    ndbrequire(i2 != ptr.i);
    i2 = ptr.i;
unknown's avatar
unknown committed
1998
    Page_state state = ptr.p->m_state;
unknown's avatar
unknown committed
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
    ndbrequire(state & Page_entry::ONQUEUE || dump_page_lists());
    ndbrequire(state & Page_entry::BOUND || dump_page_lists());
    cold_bound_count++;
  }

  Uint32 tot_bound_count =
    locked_bound_count + hot_bound_count + cold_bound_count;
  ndbrequire(m_stats.m_num_pages == tot_bound_count || dump_page_lists());

  Uint32 k;
  Uint32 entry_count = 0;

  for (k = 0; k < Page_entry::SUBLIST_COUNT; k++)
  {
    const Page_sublist& pl = *m_page_sublist[k];
    for (pl.first(ptr); ptr.i != RNIL; pl.next(ptr))
    {
      ndbrequire(get_sublist_no(ptr.p->m_state) == k || dump_page_lists());
      entry_count++;
    }
  }

  ndbrequire(entry_count == pl_hash.count() || dump_page_lists());

  debugOut << "PGMAN: loop"
           << " stats=" << m_stats_loop_on
           << " busy=" << m_busy_loop_on
           << " cleanup=" << m_cleanup_loop_on
           << " lcp=" << m_lcp_loop_on << endl;

  debugOut << "PGMAN:"
           << " entry:" << pl_hash.count()
           << " cache:" << m_stats.m_num_pages
           << "(" << locked_bound_count << "L)"
           << " stack:" << pl_stack.count()
           << " queue:" << pl_queue.count()
           << " queuewait:" << queuewait_count << endl;

  debugOut << "PGMAN:";
  for (k = 0; k < Page_entry::SUBLIST_COUNT; k++)
  {
    const Page_sublist& pl = *m_page_sublist[k];
    debugOut << " " << get_sublist_name(k) << ":" << pl.count();
  }
  debugOut << endl;
}

void
Pgman::verify_all()
{
  Page_sublist& pl_bind = *m_page_sublist[Page_entry::SL_BIND];
  Page_sublist& pl_map = *m_page_sublist[Page_entry::SL_MAP];
  Page_sublist& pl_callback = *m_page_sublist[Page_entry::SL_CALLBACK];

  if (! pl_bind.isEmpty() || ! pl_map.isEmpty() || ! pl_callback.isEmpty())
  {
    ndbrequire(m_busy_loop_on || dump_page_lists());
  }
  verify_page_lists();
}

bool
Pgman::dump_page_lists(Uint32 ptrI)
{
  if (! debugFlag)
    open_debug_file(1);

  debugOut << "PGMAN: page list dump" << endl;
  if (ptrI != RNIL)
    debugOut << "PGMAN: error on PE [" << ptrI << "]" << endl;

  Page_hashlist& pl_hash = m_page_hashlist;
  Page_stack& pl_stack = m_page_stack;
  Page_queue& pl_queue = m_page_queue;
  Ptr<Page_entry> ptr;
  Uint32 n;
  char buf[40];

  debugOut << "hash:" << endl;
  Page_hashlist::Iterator iter;
  pl_hash.next(0, iter);
  n = 0;
  while (iter.curr.i != RNIL)
  {
    sprintf(buf, "%03d", n++);
    debugOut << buf << " " << iter.curr << endl;
    pl_hash.next(iter);
  }

  debugOut << "stack:" << endl;
  n = 0;
  for (pl_stack.first(ptr); ptr.i != RNIL; pl_stack.next(ptr))
  {
    sprintf(buf, "%03d", n++);
    debugOut << buf << " " << ptr << endl;
  }

  debugOut << "queue:" << endl;
  n = 0;
  for (pl_queue.first(ptr); ptr.i != RNIL; pl_queue.next(ptr))
  {
    sprintf(buf, "%03d", n++);
    debugOut << buf << " " << ptr << endl;
  }

  Uint32 k;
  for (k = 0; k < Page_entry::SUBLIST_COUNT; k++)
  {
    debugOut << get_sublist_name(k) << ":" << endl;
    const Page_sublist& pl = *m_page_sublist[k];
    for (pl.first(ptr); ptr.i != RNIL; pl.next(ptr))
    {
      sprintf(buf, "%03d", n++);
    debugOut << buf << " " << ptr << endl;
    }
  }

  if (! debugFlag)
    open_debug_file(0);

  return false;
}

#endif

const char*
Pgman::get_sublist_name(Uint32 list_no)
{
  switch (list_no) {
  case Page_entry::SL_BIND:
    return "bind";
  case Page_entry::SL_MAP:
    return "map";
  case Page_entry::SL_MAP_IO:
    return "map_io";
  case Page_entry::SL_CALLBACK:
    return "callback";
  case Page_entry::SL_CALLBACK_IO:
    return "callback_io";
  case Page_entry::SL_BUSY:
    return "busy";
  case Page_entry::SL_LOCKED:
    return "locked";
  case Page_entry::SL_OTHER:
    return "other";
  }
  return "?";
}

NdbOut&
operator<<(NdbOut& out, Ptr<Pgman::Page_request> ptr)
{
  const Pgman::Page_request& pr = *ptr.p;
  const char* bname = getBlockName(pr.m_block, "?");
  out << "PR";
  if (ptr.i != RNIL)
    out << " [" << dec << ptr.i << "]";
  out << " block=" << bname;
  out << " flags=" << hex << pr.m_flags;
  out << "," << dec << (pr.m_flags & Pgman::Page_request::OP_MASK);
  {
    if (pr.m_flags & Pgman::Page_request::LOCK_PAGE)
      out << ",lock_page";
    if (pr.m_flags & Pgman::Page_request::EMPTY_PAGE)
      out << ",empty_page";
    if (pr.m_flags & Pgman::Page_request::ALLOC_REQ)
      out << ",alloc_req";
    if (pr.m_flags & Pgman::Page_request::COMMIT_REQ)
      out << ",commit_req";
    if (pr.m_flags & Pgman::Page_request::DIRTY_REQ)
      out << ",dirty_req";
    if (pr.m_flags & Pgman::Page_request::CORR_REQ)
      out << ",corr_req";
  }
  return out;
}

NdbOut&
operator<<(NdbOut& out, Ptr<Pgman::Page_entry> ptr)
{
  const Pgman::Page_entry pe = *ptr.p;
  Uint32 list_no = Pgman::get_sublist_no(pe.m_state);
  out << "PE [" << dec << ptr.i << "]";
  out << " state=" << hex << pe.m_state;
  {
    if (pe.m_state & Pgman::Page_entry::REQUEST)
      out << ",request";
    if (pe.m_state & Pgman::Page_entry::EMPTY)
      out << ",empty";
    if (pe.m_state & Pgman::Page_entry::BOUND)
      out << ",bound";
    if (pe.m_state & Pgman::Page_entry::MAPPED)
      out << ",mapped";
    if (pe.m_state & Pgman::Page_entry::DIRTY)
      out << ",dirty";
    if (pe.m_state & Pgman::Page_entry::USED)
      out << ",used";
    if (pe.m_state & Pgman::Page_entry::BUSY)
      out << ",busy";
    if (pe.m_state & Pgman::Page_entry::LOCKED)
      out << ",locked";
    if (pe.m_state & Pgman::Page_entry::PAGEIN)
      out << ",pagein";
    if (pe.m_state & Pgman::Page_entry::PAGEOUT)
      out << ",pageout";
    if (pe.m_state & Pgman::Page_entry::LOGSYNC)
      out << ",logsync";
    if (pe.m_state & Pgman::Page_entry::LCP)
      out << ",lcp";
    if (pe.m_state & Pgman::Page_entry::HOT)
      out << ",hot";
    if (pe.m_state & Pgman::Page_entry::ONSTACK)
      out << ",onstack";
    if (pe.m_state & Pgman::Page_entry::ONQUEUE)
      out << ",onqueue";
  }
  out << " list=";
  if (list_no == ZNIL)
    out << "NONE";
  else
  {
    out << dec << list_no;
    out << "," << Pgman::get_sublist_name(list_no);
  }
  out << " diskpage=" << dec << pe.m_file_no << "," << pe.m_page_no;
  if (pe.m_real_page_i == RNIL)
    out << " realpage=RNIL";
  else
    out << " realpage=" << dec << pe.m_real_page_i;
  out << " lsn=" << dec << pe.m_lsn;
  out << " busy_count=" << dec << pe.m_busy_count;
#ifdef VM_TRACE
  {
unknown's avatar
unknown committed
2232 2233
    Pgman::Local_page_request_list 
      req_list(ptr.p->m_this->m_page_request_pool, ptr.p->m_requests);
unknown's avatar
unknown committed
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
    if (! req_list.isEmpty())
    {
      Ptr<Pgman::Page_request> req_ptr;
      out << " req:";
      for (req_list.first(req_ptr); req_ptr.i != RNIL; req_list.next(req_ptr))
      {
        out << " " << req_ptr;
      }
    }
  }
#endif
  return out;
}

#ifdef VM_TRACE
void
Pgman::open_debug_file(Uint32 flag)
{
  if (flag)
  {
    FILE* f = globalSignalLoggers.getOutputStream();
    debugOut = *new NdbOut(*new FileOutputStream(f));
  }
  else
  {
    debugOut = *new NdbOut(*new NullOutputStream());
  }
}
#endif

void
Pgman::execDUMP_STATE_ORD(Signal* signal)
{
  jamEntry();
  Page_hashlist& pl_hash = m_page_hashlist;
#ifdef VM_TRACE
  if (signal->theData[0] == 11000 && signal->getLength() == 2)
  {
    Uint32 flag = signal->theData[1];
    open_debug_file(flag);
    debugFlag = flag;
  }
#endif

  if (signal->theData[0] == 11001)
  {
    // XXX print hash list if no sublist
    Uint32 list = 0;
    if (signal->getLength() > 1)
      list = signal->theData[1];

    Page_sublist& pl = *m_page_sublist[list];
    Ptr<Page_entry> ptr;
    
    for (pl.first(ptr); ptr.i != RNIL; pl.next(ptr))
    {
      ndbout << ptr << endl;
      infoEvent(" PE [ file: %d page: %d ] state: %x lsn: %lld lcp: %d busy: %d req-list: %d",
		ptr.p->m_file_no, ptr.p->m_page_no,
		ptr.p->m_state, ptr.p->m_lsn, ptr.p->m_last_lcp,
		ptr.p->m_busy_count,
		!ptr.p->m_requests.isEmpty());
    }
  }

  if (signal->theData[0] == 11002 && signal->getLength() == 3)
  {
    Page_entry key;
    key.m_file_no = signal->theData[1];
    key.m_page_no = signal->theData[2];
    
    Ptr<Page_entry> ptr;
    if (pl_hash.find(ptr, key))
    {
      ndbout << "pageout " << ptr << endl;
unknown's avatar
unknown committed
2309 2310
      c_tup->disk_page_unmap_callback(ptr.p->m_real_page_i, 
				      ptr.p->m_dirty_count);
unknown's avatar
unknown committed
2311 2312 2313
      pageout(signal, ptr);
    }
  }
unknown's avatar
unknown committed
2314
  
unknown's avatar
unknown committed
2315

unknown's avatar
unknown committed
2316 2317 2318 2319 2320
  if (signal->theData[0] == 11003)
  {
#ifdef VM_TRACE
    verify_page_lists();
    dump_page_lists();
unknown's avatar
unknown committed
2321 2322
#else
    ndbout << "Only in VM_TRACE builds" << endl;
unknown's avatar
unknown committed
2323 2324
#endif
  }
unknown's avatar
unknown committed
2325 2326 2327

  if (signal->theData[0] == 11004)
  {
unknown's avatar
unknown committed
2328
    ndbout << "Dump LCP bucket m_lcp_outstanding: " << m_lcp_outstanding;
unknown's avatar
unknown committed
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
    if (m_lcp_curr_bucket != ~(Uint32)0)
    {
      Page_hashlist::Iterator iter;
      pl_hash.next(m_lcp_curr_bucket, iter);
      
      ndbout_c(" %d", m_lcp_curr_bucket);

      while (iter.curr.i != RNIL && iter.bucket == m_lcp_curr_bucket)
      {
	Ptr<Page_entry>& ptr = iter.curr;
	ndbout << ptr << endl;
	pl_hash.next(iter);
      }

      ndbout_c("-- done");
    }
    else
    {
      ndbout_c(" == ~0");
    }
  }

  if (signal->theData[0] == 11005)
  {
    g_dbg_lcp = ~g_dbg_lcp;
  }
unknown's avatar
unknown committed
2355 2356 2357 2358 2359

  if (signal->theData[0] == 11006)
  {
    SET_ERROR_INSERT_VALUE(11006);
  }
unknown's avatar
unknown committed
2360 2361 2362 2363 2364 2365 2366 2367 2368
}

// page cache client

Page_cache_client::Page_cache_client(SimulatedBlock* block, Pgman* pgman)
{
  m_block = block->number();
  m_pgman = pgman;
}