1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
  27 #include "gc_implementation/parallelScavenge/gcTaskThread.hpp"
  28 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "runtime/mutex.hpp"
  32 #include "runtime/mutexLocker.hpp"
  33 #include "runtime/orderAccess.inline.hpp"
  34 
  35 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  36 
  37 //
  38 // GCTask
  39 //
  40 
  41 const char* GCTask::Kind::to_string(kind value) {
  42   const char* result = "unknown GCTask kind";
  43   switch (value) {
  44   default:
  45     result = "unknown GCTask kind";
  46     break;
  47   case unknown_task:
  48     result = "unknown task";
  49     break;
  50   case ordinary_task:
  51     result = "ordinary task";
  52     break;
  53   case barrier_task:
  54     result = "barrier task";
  55     break;
  56   case noop_task:
  57     result = "noop task";
  58     break;
  59   case idle_task:
  60     result = "idle task";
  61     break;
  62   }
  63   return result;
  64 };
  65 
  66 GCTask::GCTask() :
  67   _kind(Kind::ordinary_task),
  68   _affinity(GCTaskManager::sentinel_worker()){
  69   initialize();
  70 }
  71 
  72 GCTask::GCTask(Kind::kind kind) :
  73   _kind(kind),
  74   _affinity(GCTaskManager::sentinel_worker()) {
  75   initialize();
  76 }
  77 
  78 GCTask::GCTask(uint affinity) :
  79   _kind(Kind::ordinary_task),
  80   _affinity(affinity) {
  81   initialize();
  82 }
  83 
  84 GCTask::GCTask(Kind::kind kind, uint affinity) :
  85   _kind(kind),
  86   _affinity(affinity) {
  87   initialize();
  88 }
  89 
  90 void GCTask::initialize() {
  91   _older = NULL;
  92   _newer = NULL;
  93 }
  94 
  95 void GCTask::destruct() {
  96   assert(older() == NULL, "shouldn't have an older task");
  97   assert(newer() == NULL, "shouldn't have a newer task");
  98   // Nothing to do.
  99 }
 100 
 101 NOT_PRODUCT(
 102 void GCTask::print(const char* message) const {
 103   tty->print(INTPTR_FORMAT " <- " INTPTR_FORMAT "(%u) -> " INTPTR_FORMAT,
 104              newer(), this, affinity(), older());
 105 }
 106 )
 107 
 108 //
 109 // GCTaskQueue
 110 //
 111 
 112 GCTaskQueue* GCTaskQueue::create() {
 113   GCTaskQueue* result = new GCTaskQueue(false);
 114   if (TraceGCTaskQueue) {
 115     tty->print_cr("GCTaskQueue::create()"
 116                   " returns " INTPTR_FORMAT, result);
 117   }
 118   return result;
 119 }
 120 
 121 GCTaskQueue* GCTaskQueue::create_on_c_heap() {
 122   GCTaskQueue* result = new(ResourceObj::C_HEAP, mtGC) GCTaskQueue(true);
 123   if (TraceGCTaskQueue) {
 124     tty->print_cr("GCTaskQueue::create_on_c_heap()"
 125                   " returns " INTPTR_FORMAT,
 126                   result);
 127   }
 128   return result;
 129 }
 130 
 131 GCTaskQueue::GCTaskQueue(bool on_c_heap) :
 132   _is_c_heap_obj(on_c_heap) {
 133   initialize();
 134   if (TraceGCTaskQueue) {
 135     tty->print_cr("[" INTPTR_FORMAT "]"
 136                   " GCTaskQueue::GCTaskQueue() constructor",
 137                   this);
 138   }
 139 }
 140 
 141 void GCTaskQueue::destruct() {
 142   // Nothing to do.
 143 }
 144 
 145 void GCTaskQueue::destroy(GCTaskQueue* that) {
 146   if (TraceGCTaskQueue) {
 147     tty->print_cr("[" INTPTR_FORMAT "]"
 148                   " GCTaskQueue::destroy()"
 149                   "  is_c_heap_obj:  %s",
 150                   that,
 151                   that->is_c_heap_obj() ? "true" : "false");
 152   }
 153   // That instance may have been allocated as a CHeapObj,
 154   // in which case we have to free it explicitly.
 155   if (that != NULL) {
 156     that->destruct();
 157     assert(that->is_empty(), "should be empty");
 158     if (that->is_c_heap_obj()) {
 159       FreeHeap(that);
 160     }
 161   }
 162 }
 163 
 164 void GCTaskQueue::initialize() {
 165   set_insert_end(NULL);
 166   set_remove_end(NULL);
 167   set_length(0);
 168 }
 169 
 170 // Enqueue one task.
 171 void GCTaskQueue::enqueue(GCTask* task) {
 172   if (TraceGCTaskQueue) {
 173     tty->print_cr("[" INTPTR_FORMAT "]"
 174                   " GCTaskQueue::enqueue(task: "
 175                   INTPTR_FORMAT ")",
 176                   this, task);
 177     print("before:");
 178   }
 179   assert(task != NULL, "shouldn't have null task");
 180   assert(task->older() == NULL, "shouldn't be on queue");
 181   assert(task->newer() == NULL, "shouldn't be on queue");
 182   task->set_newer(NULL);
 183   task->set_older(insert_end());
 184   if (is_empty()) {
 185     set_remove_end(task);
 186   } else {
 187     insert_end()->set_newer(task);
 188   }
 189   set_insert_end(task);
 190   increment_length();
 191   verify_length();
 192   if (TraceGCTaskQueue) {
 193     print("after:");
 194   }
 195 }
 196 
 197 // Enqueue a whole list of tasks.  Empties the argument list.
 198 void GCTaskQueue::enqueue(GCTaskQueue* list) {
 199   if (TraceGCTaskQueue) {
 200     tty->print_cr("[" INTPTR_FORMAT "]"
 201                   " GCTaskQueue::enqueue(list: "
 202                   INTPTR_FORMAT ")",
 203                   this, list);
 204     print("before:");
 205     list->print("list:");
 206   }
 207   if (list->is_empty()) {
 208     // Enqueueing the empty list: nothing to do.
 209     return;
 210   }
 211   uint list_length = list->length();
 212   if (is_empty()) {
 213     // Enqueueing to empty list: just acquire elements.
 214     set_insert_end(list->insert_end());
 215     set_remove_end(list->remove_end());
 216     set_length(list_length);
 217   } else {
 218     // Prepend argument list to our queue.
 219     list->remove_end()->set_older(insert_end());
 220     insert_end()->set_newer(list->remove_end());
 221     set_insert_end(list->insert_end());
 222     set_length(length() + list_length);
 223     // empty the argument list.
 224   }
 225   list->initialize();
 226   if (TraceGCTaskQueue) {
 227     print("after:");
 228     list->print("list:");
 229   }
 230   verify_length();
 231 }
 232 
 233 // Dequeue one task.
 234 GCTask* GCTaskQueue::dequeue() {
 235   if (TraceGCTaskQueue) {
 236     tty->print_cr("[" INTPTR_FORMAT "]"
 237                   " GCTaskQueue::dequeue()", this);
 238     print("before:");
 239   }
 240   assert(!is_empty(), "shouldn't dequeue from empty list");
 241   GCTask* result = remove();
 242   assert(result != NULL, "shouldn't have NULL task");
 243   if (TraceGCTaskQueue) {
 244     tty->print_cr("    return: " INTPTR_FORMAT, result);
 245     print("after:");
 246   }
 247   return result;
 248 }
 249 
 250 // Dequeue one task, preferring one with affinity.
 251 GCTask* GCTaskQueue::dequeue(uint affinity) {
 252   if (TraceGCTaskQueue) {
 253     tty->print_cr("[" INTPTR_FORMAT "]"
 254                   " GCTaskQueue::dequeue(%u)", this, affinity);
 255     print("before:");
 256   }
 257   assert(!is_empty(), "shouldn't dequeue from empty list");
 258   // Look down to the next barrier for a task with this affinity.
 259   GCTask* result = NULL;
 260   for (GCTask* element = remove_end();
 261        element != NULL;
 262        element = element->newer()) {
 263     if (element->is_barrier_task()) {
 264       // Don't consider barrier tasks, nor past them.
 265       result = NULL;
 266       break;
 267     }
 268     if (element->affinity() == affinity) {
 269       result = remove(element);
 270       break;
 271     }
 272   }
 273   // If we didn't find anything with affinity, just take the next task.
 274   if (result == NULL) {
 275     result = remove();
 276   }
 277   if (TraceGCTaskQueue) {
 278     tty->print_cr("    return: " INTPTR_FORMAT, result);
 279     print("after:");
 280   }
 281   return result;
 282 }
 283 
 284 GCTask* GCTaskQueue::remove() {
 285   // Dequeue from remove end.
 286   GCTask* result = remove_end();
 287   assert(result != NULL, "shouldn't have null task");
 288   assert(result->older() == NULL, "not the remove_end");
 289   set_remove_end(result->newer());
 290   if (remove_end() == NULL) {
 291     assert(insert_end() == result, "not a singleton");
 292     set_insert_end(NULL);
 293   } else {
 294     remove_end()->set_older(NULL);
 295   }
 296   result->set_newer(NULL);
 297   decrement_length();
 298   assert(result->newer() == NULL, "shouldn't be on queue");
 299   assert(result->older() == NULL, "shouldn't be on queue");
 300   verify_length();
 301   return result;
 302 }
 303 
 304 GCTask* GCTaskQueue::remove(GCTask* task) {
 305   // This is slightly more work, and has slightly fewer asserts
 306   // than removing from the remove end.
 307   assert(task != NULL, "shouldn't have null task");
 308   GCTask* result = task;
 309   if (result->newer() != NULL) {
 310     result->newer()->set_older(result->older());
 311   } else {
 312     assert(insert_end() == result, "not youngest");
 313     set_insert_end(result->older());
 314   }
 315   if (result->older() != NULL) {
 316     result->older()->set_newer(result->newer());
 317   } else {
 318     assert(remove_end() == result, "not oldest");
 319     set_remove_end(result->newer());
 320   }
 321   result->set_newer(NULL);
 322   result->set_older(NULL);
 323   decrement_length();
 324   verify_length();
 325   return result;
 326 }
 327 
 328 NOT_PRODUCT(
 329 // Count the elements in the queue and verify the length against
 330 // that count.
 331 void GCTaskQueue::verify_length() const {
 332   uint count = 0;
 333   for (GCTask* element = insert_end();
 334        element != NULL;
 335        element = element->older()) {
 336 
 337     count++;
 338   }
 339   assert(count == length(), "Length does not match queue");
 340 }
 341 
 342 void GCTaskQueue::print(const char* message) const {
 343   tty->print_cr("[" INTPTR_FORMAT "] GCTaskQueue:"
 344                 "  insert_end: " INTPTR_FORMAT
 345                 "  remove_end: " INTPTR_FORMAT
 346                 "  length:       %d"
 347                 "  %s",
 348                 this, insert_end(), remove_end(), length(), message);
 349   uint count = 0;
 350   for (GCTask* element = insert_end();
 351        element != NULL;
 352        element = element->older()) {
 353     element->print("    ");
 354     count++;
 355     tty->cr();
 356   }
 357   tty->print("Total tasks: %d", count);
 358 }
 359 )
 360 
 361 //
 362 // SynchronizedGCTaskQueue
 363 //
 364 
 365 SynchronizedGCTaskQueue::SynchronizedGCTaskQueue(GCTaskQueue* queue_arg,
 366                                                  Monitor *       lock_arg) :
 367   _unsynchronized_queue(queue_arg),
 368   _lock(lock_arg) {
 369   assert(unsynchronized_queue() != NULL, "null queue");
 370   assert(lock() != NULL, "null lock");
 371 }
 372 
 373 SynchronizedGCTaskQueue::~SynchronizedGCTaskQueue() {
 374   // Nothing to do.
 375 }
 376 
 377 //
 378 // GCTaskManager
 379 //
 380 GCTaskManager::GCTaskManager(uint workers) :
 381   _workers(workers),
 382   _active_workers(0),
 383   _idle_workers(0),
 384   _ndc(NULL) {
 385   initialize();
 386 }
 387 
 388 GCTaskManager::GCTaskManager(uint workers, NotifyDoneClosure* ndc) :
 389   _workers(workers),
 390   _active_workers(0),
 391   _idle_workers(0),
 392   _ndc(ndc) {
 393   initialize();
 394 }
 395 
 396 void GCTaskManager::initialize() {
 397   if (TraceGCTaskManager) {
 398     tty->print_cr("GCTaskManager::initialize: workers: %u", workers());
 399   }
 400   assert(workers() != 0, "no workers");
 401   _monitor = new Monitor(Mutex::barrier,                // rank
 402                          "GCTaskManager monitor",       // name
 403                          Mutex::_allow_vm_block_flag,   // allow_vm_block
 404                          Monitor::_safepoint_check_never);
 405   // The queue for the GCTaskManager must be a CHeapObj.
 406   GCTaskQueue* unsynchronized_queue = GCTaskQueue::create_on_c_heap();
 407   _queue = SynchronizedGCTaskQueue::create(unsynchronized_queue, lock());
 408   _noop_task = NoopGCTask::create_on_c_heap();
 409   _idle_inactive_task = WaitForBarrierGCTask::create_on_c_heap();
 410   _resource_flag = NEW_C_HEAP_ARRAY(bool, workers(), mtGC);
 411   {
 412     // Set up worker threads.
 413     //     Distribute the workers among the available processors,
 414     //     unless we were told not to, or if the os doesn't want to.
 415     uint* processor_assignment = NEW_C_HEAP_ARRAY(uint, workers(), mtGC);
 416     if (!BindGCTaskThreadsToCPUs ||
 417         !os::distribute_processes(workers(), processor_assignment)) {
 418       for (uint a = 0; a < workers(); a += 1) {
 419         processor_assignment[a] = sentinel_worker();
 420       }
 421     }
 422     _thread = NEW_C_HEAP_ARRAY(GCTaskThread*, workers(), mtGC);
 423     for (uint t = 0; t < workers(); t += 1) {
 424       set_thread(t, GCTaskThread::create(this, t, processor_assignment[t]));
 425     }
 426     if (TraceGCTaskThread) {
 427       tty->print("GCTaskManager::initialize: distribution:");
 428       for (uint t = 0; t < workers(); t += 1) {
 429         tty->print("  %u", processor_assignment[t]);
 430       }
 431       tty->cr();
 432     }
 433     FREE_C_HEAP_ARRAY(uint, processor_assignment, mtGC);
 434   }
 435   reset_busy_workers();
 436   set_unblocked();
 437   for (uint w = 0; w < workers(); w += 1) {
 438     set_resource_flag(w, false);
 439   }
 440   reset_delivered_tasks();
 441   reset_completed_tasks();
 442   reset_noop_tasks();
 443   reset_barriers();
 444   reset_emptied_queue();
 445   for (uint s = 0; s < workers(); s += 1) {
 446     thread(s)->start();
 447   }
 448 }
 449 
 450 GCTaskManager::~GCTaskManager() {
 451   assert(busy_workers() == 0, "still have busy workers");
 452   assert(queue()->is_empty(), "still have queued work");
 453   NoopGCTask::destroy(_noop_task);
 454   _noop_task = NULL;
 455   WaitForBarrierGCTask::destroy(_idle_inactive_task);
 456   _idle_inactive_task = NULL;
 457   if (_thread != NULL) {
 458     for (uint i = 0; i < workers(); i += 1) {
 459       GCTaskThread::destroy(thread(i));
 460       set_thread(i, NULL);
 461     }
 462     FREE_C_HEAP_ARRAY(GCTaskThread*, _thread, mtGC);
 463     _thread = NULL;
 464   }
 465   if (_resource_flag != NULL) {
 466     FREE_C_HEAP_ARRAY(bool, _resource_flag, mtGC);
 467     _resource_flag = NULL;
 468   }
 469   if (queue() != NULL) {
 470     GCTaskQueue* unsynchronized_queue = queue()->unsynchronized_queue();
 471     GCTaskQueue::destroy(unsynchronized_queue);
 472     SynchronizedGCTaskQueue::destroy(queue());
 473     _queue = NULL;
 474   }
 475   if (monitor() != NULL) {
 476     delete monitor();
 477     _monitor = NULL;
 478   }
 479 }
 480 
 481 void GCTaskManager::set_active_gang() {
 482   _active_workers =
 483     AdaptiveSizePolicy::calc_active_workers(workers(),
 484                                  active_workers(),
 485                                  Threads::number_of_non_daemon_threads());
 486 
 487   assert(!all_workers_active() || active_workers() == ParallelGCThreads,
 488          err_msg("all_workers_active() is  incorrect: "
 489                  "active %d  ParallelGCThreads %d", active_workers(),
 490                  ParallelGCThreads));
 491   if (TraceDynamicGCThreads) {
 492     gclog_or_tty->print_cr("GCTaskManager::set_active_gang(): "
 493                            "all_workers_active()  %d  workers %d  "
 494                            "active  %d  ParallelGCThreads " UINTX_FORMAT,
 495                            all_workers_active(), workers(),  active_workers(),
 496                            ParallelGCThreads);
 497   }
 498 }
 499 
 500 // Create IdleGCTasks for inactive workers.
 501 // Creates tasks in a ResourceArea and assumes
 502 // an appropriate ResourceMark.
 503 void GCTaskManager::task_idle_workers() {
 504   {
 505     int more_inactive_workers = 0;
 506     {
 507       // Stop any idle tasks from exiting their IdleGCTask's
 508       // and get the count for additional IdleGCTask's under
 509       // the GCTaskManager's monitor so that the "more_inactive_workers"
 510       // count is correct.
 511       MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 512       _idle_inactive_task->set_should_wait(true);
 513       // active_workers are a number being requested.  idle_workers
 514       // are the number currently idle.  If all the workers are being
 515       // requested to be active but some are already idle, reduce
 516       // the number of active_workers to be consistent with the
 517       // number of idle_workers.  The idle_workers are stuck in
 518       // idle tasks and will no longer be release (since a new GC
 519       // is starting).  Try later to release enough idle_workers
 520       // to allow the desired number of active_workers.
 521       more_inactive_workers =
 522         workers() - active_workers() - idle_workers();
 523       if (more_inactive_workers < 0) {
 524         int reduced_active_workers = active_workers() + more_inactive_workers;
 525         set_active_workers(reduced_active_workers);
 526         more_inactive_workers = 0;
 527       }
 528       if (TraceDynamicGCThreads) {
 529         gclog_or_tty->print_cr("JT: %d  workers %d  active  %d  "
 530                                 "idle %d  more %d",
 531                                 Threads::number_of_non_daemon_threads(),
 532                                 workers(),
 533                                 active_workers(),
 534                                 idle_workers(),
 535                                 more_inactive_workers);
 536       }
 537     }
 538     GCTaskQueue* q = GCTaskQueue::create();
 539     for(uint i = 0; i < (uint) more_inactive_workers; i++) {
 540       q->enqueue(IdleGCTask::create_on_c_heap());
 541       increment_idle_workers();
 542     }
 543     assert(workers() == active_workers() + idle_workers(),
 544       "total workers should equal active + inactive");
 545     add_list(q);
 546     // GCTaskQueue* q was created in a ResourceArea so a
 547     // destroy() call is not needed.
 548   }
 549 }
 550 
 551 void  GCTaskManager::release_idle_workers() {
 552   {
 553     MutexLockerEx ml(monitor(),
 554       Mutex::_no_safepoint_check_flag);
 555     _idle_inactive_task->set_should_wait(false);
 556     monitor()->notify_all();
 557   // Release monitor
 558   }
 559 }
 560 
 561 void GCTaskManager::print_task_time_stamps() {
 562   for(uint i=0; i<ParallelGCThreads; i++) {
 563     GCTaskThread* t = thread(i);
 564     t->print_task_time_stamps();
 565   }
 566 }
 567 
 568 void GCTaskManager::print_threads_on(outputStream* st) {
 569   uint num_thr = workers();
 570   for (uint i = 0; i < num_thr; i++) {
 571     thread(i)->print_on(st);
 572     st->cr();
 573   }
 574 }
 575 
 576 void GCTaskManager::threads_do(ThreadClosure* tc) {
 577   assert(tc != NULL, "Null ThreadClosure");
 578   uint num_thr = workers();
 579   for (uint i = 0; i < num_thr; i++) {
 580     tc->do_thread(thread(i));
 581   }
 582 }
 583 
 584 GCTaskThread* GCTaskManager::thread(uint which) {
 585   assert(which < workers(), "index out of bounds");
 586   assert(_thread[which] != NULL, "shouldn't have null thread");
 587   return _thread[which];
 588 }
 589 
 590 void GCTaskManager::set_thread(uint which, GCTaskThread* value) {
 591   assert(which < workers(), "index out of bounds");
 592   assert(value != NULL, "shouldn't have null thread");
 593   _thread[which] = value;
 594 }
 595 
 596 void GCTaskManager::add_task(GCTask* task) {
 597   assert(task != NULL, "shouldn't have null task");
 598   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 599   if (TraceGCTaskManager) {
 600     tty->print_cr("GCTaskManager::add_task(" INTPTR_FORMAT " [%s])",
 601                   task, GCTask::Kind::to_string(task->kind()));
 602   }
 603   queue()->enqueue(task);
 604   // Notify with the lock held to avoid missed notifies.
 605   if (TraceGCTaskManager) {
 606     tty->print_cr("    GCTaskManager::add_task (%s)->notify_all",
 607                   monitor()->name());
 608   }
 609   (void) monitor()->notify_all();
 610   // Release monitor().
 611 }
 612 
 613 void GCTaskManager::add_list(GCTaskQueue* list) {
 614   assert(list != NULL, "shouldn't have null task");
 615   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 616   if (TraceGCTaskManager) {
 617     tty->print_cr("GCTaskManager::add_list(%u)", list->length());
 618   }
 619   queue()->enqueue(list);
 620   // Notify with the lock held to avoid missed notifies.
 621   if (TraceGCTaskManager) {
 622     tty->print_cr("    GCTaskManager::add_list (%s)->notify_all",
 623                   monitor()->name());
 624   }
 625   (void) monitor()->notify_all();
 626   // Release monitor().
 627 }
 628 
 629 // GC workers wait in get_task() for new work to be added
 630 // to the GCTaskManager's queue.  When new work is added,
 631 // a notify is sent to the waiting GC workers which then
 632 // compete to get tasks.  If a GC worker wakes up and there
 633 // is no work on the queue, it is given a noop_task to execute
 634 // and then loops to find more work.
 635 
 636 GCTask* GCTaskManager::get_task(uint which) {
 637   GCTask* result = NULL;
 638   // Grab the queue lock.
 639   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 640   // Wait while the queue is block or
 641   // there is nothing to do, except maybe release resources.
 642   while (is_blocked() ||
 643          (queue()->is_empty() && !should_release_resources(which))) {
 644     if (TraceGCTaskManager) {
 645       tty->print_cr("GCTaskManager::get_task(%u)"
 646                     "  blocked: %s"
 647                     "  empty: %s"
 648                     "  release: %s",
 649                     which,
 650                     is_blocked() ? "true" : "false",
 651                     queue()->is_empty() ? "true" : "false",
 652                     should_release_resources(which) ? "true" : "false");
 653       tty->print_cr("    => (%s)->wait()",
 654                     monitor()->name());
 655     }
 656     monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
 657   }
 658   // We've reacquired the queue lock here.
 659   // Figure out which condition caused us to exit the loop above.
 660   if (!queue()->is_empty()) {
 661     if (UseGCTaskAffinity) {
 662       result = queue()->dequeue(which);
 663     } else {
 664       result = queue()->dequeue();
 665     }
 666     if (result->is_barrier_task()) {
 667       assert(which != sentinel_worker(),
 668              "blocker shouldn't be bogus");
 669       set_blocking_worker(which);
 670     }
 671   } else {
 672     // The queue is empty, but we were woken up.
 673     // Just hand back a Noop task,
 674     // in case someone wanted us to release resources, or whatever.
 675     result = noop_task();
 676     increment_noop_tasks();
 677   }
 678   assert(result != NULL, "shouldn't have null task");
 679   if (TraceGCTaskManager) {
 680     tty->print_cr("GCTaskManager::get_task(%u) => " INTPTR_FORMAT " [%s]",
 681                   which, result, GCTask::Kind::to_string(result->kind()));
 682     tty->print_cr("     %s", result->name());
 683   }
 684   if (!result->is_idle_task()) {
 685     increment_busy_workers();
 686     increment_delivered_tasks();
 687   }
 688   return result;
 689   // Release monitor().
 690 }
 691 
 692 void GCTaskManager::note_completion(uint which) {
 693   MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
 694   if (TraceGCTaskManager) {
 695     tty->print_cr("GCTaskManager::note_completion(%u)", which);
 696   }
 697   // If we are blocked, check if the completing thread is the blocker.
 698   if (blocking_worker() == which) {
 699     assert(blocking_worker() != sentinel_worker(),
 700            "blocker shouldn't be bogus");
 701     increment_barriers();
 702     set_unblocked();
 703   }
 704   increment_completed_tasks();
 705   uint active = decrement_busy_workers();
 706   if ((active == 0) && (queue()->is_empty())) {
 707     increment_emptied_queue();
 708     if (TraceGCTaskManager) {
 709       tty->print_cr("    GCTaskManager::note_completion(%u) done", which);
 710     }
 711     // Notify client that we are done.
 712     NotifyDoneClosure* ndc = notify_done_closure();
 713     if (ndc != NULL) {
 714       ndc->notify(this);
 715     }
 716   }
 717   if (TraceGCTaskManager) {
 718     tty->print_cr("    GCTaskManager::note_completion(%u) (%s)->notify_all",
 719                   which, monitor()->name());
 720     tty->print_cr("  "
 721                   "  blocked: %s"
 722                   "  empty: %s"
 723                   "  release: %s",
 724                   is_blocked() ? "true" : "false",
 725                   queue()->is_empty() ? "true" : "false",
 726                   should_release_resources(which) ? "true" : "false");
 727     tty->print_cr("  "
 728                   "  delivered: %u"
 729                   "  completed: %u"
 730                   "  barriers: %u"
 731                   "  emptied: %u",
 732                   delivered_tasks(),
 733                   completed_tasks(),
 734                   barriers(),
 735                   emptied_queue());
 736   }
 737   // Tell everyone that a task has completed.
 738   (void) monitor()->notify_all();
 739   // Release monitor().
 740 }
 741 
 742 uint GCTaskManager::increment_busy_workers() {
 743   assert(queue()->own_lock(), "don't own the lock");
 744   _busy_workers += 1;
 745   return _busy_workers;
 746 }
 747 
 748 uint GCTaskManager::decrement_busy_workers() {
 749   assert(queue()->own_lock(), "don't own the lock");
 750   assert(_busy_workers > 0, "About to make a mistake");
 751   _busy_workers -= 1;
 752   return _busy_workers;
 753 }
 754 
 755 void GCTaskManager::release_all_resources() {
 756   // If you want this to be done atomically, do it in a BarrierGCTask.
 757   for (uint i = 0; i < workers(); i += 1) {
 758     set_resource_flag(i, true);
 759   }
 760 }
 761 
 762 bool GCTaskManager::should_release_resources(uint which) {
 763   // This can be done without a lock because each thread reads one element.
 764   return resource_flag(which);
 765 }
 766 
 767 void GCTaskManager::note_release(uint which) {
 768   // This can be done without a lock because each thread writes one element.
 769   set_resource_flag(which, false);
 770 }
 771 
 772 // "list" contains tasks that are ready to execute.  Those
 773 // tasks are added to the GCTaskManager's queue of tasks and
 774 // then the GC workers are notified that there is new work to
 775 // do.
 776 //
 777 // Typically different types of tasks can be added to the "list".
 778 // For example in PSScavenge OldToYoungRootsTask, SerialOldToYoungRootsTask,
 779 // ScavengeRootsTask, and StealTask tasks are all added to the list
 780 // and then the GC workers are notified of new work.  The tasks are
 781 // handed out in the order in which they are added to the list
 782 // (although execution is not necessarily in that order).  As long
 783 // as any tasks are running the GCTaskManager will wait for execution
 784 // to complete.  GC workers that execute a stealing task remain in
 785 // the stealing task until all stealing tasks have completed.  The load
 786 // balancing afforded by the stealing tasks work best if the stealing
 787 // tasks are added last to the list.
 788 
 789 void GCTaskManager::execute_and_wait(GCTaskQueue* list) {
 790   WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
 791   list->enqueue(fin);
 792   // The barrier task will be read by one of the GC
 793   // workers once it is added to the list of tasks.
 794   // Be sure that is globally visible before the
 795   // GC worker reads it (which is after the task is added
 796   // to the list of tasks below).
 797   OrderAccess::storestore();
 798   add_list(list);
 799   fin->wait_for(true /* reset */);
 800   // We have to release the barrier tasks!
 801   WaitForBarrierGCTask::destroy(fin);
 802 }
 803 
 804 bool GCTaskManager::resource_flag(uint which) {
 805   assert(which < workers(), "index out of bounds");
 806   return _resource_flag[which];
 807 }
 808 
 809 void GCTaskManager::set_resource_flag(uint which, bool value) {
 810   assert(which < workers(), "index out of bounds");
 811   _resource_flag[which] = value;
 812 }
 813 
 814 //
 815 // NoopGCTask
 816 //
 817 
 818 NoopGCTask* NoopGCTask::create() {
 819   NoopGCTask* result = new NoopGCTask(false);
 820   return result;
 821 }
 822 
 823 NoopGCTask* NoopGCTask::create_on_c_heap() {
 824   NoopGCTask* result = new(ResourceObj::C_HEAP, mtGC) NoopGCTask(true);
 825   return result;
 826 }
 827 
 828 void NoopGCTask::destroy(NoopGCTask* that) {
 829   if (that != NULL) {
 830     that->destruct();
 831     if (that->is_c_heap_obj()) {
 832       FreeHeap(that);
 833     }
 834   }
 835 }
 836 
 837 void NoopGCTask::destruct() {
 838   // This has to know it's superclass structure, just like the constructor.
 839   this->GCTask::destruct();
 840   // Nothing else to do.
 841 }
 842 
 843 //
 844 // IdleGCTask
 845 //
 846 
 847 IdleGCTask* IdleGCTask::create() {
 848   IdleGCTask* result = new IdleGCTask(false);
 849   assert(UseDynamicNumberOfGCThreads,
 850     "Should only be used with dynamic GC thread");
 851   return result;
 852 }
 853 
 854 IdleGCTask* IdleGCTask::create_on_c_heap() {
 855   IdleGCTask* result = new(ResourceObj::C_HEAP, mtGC) IdleGCTask(true);
 856   assert(UseDynamicNumberOfGCThreads,
 857     "Should only be used with dynamic GC thread");
 858   return result;
 859 }
 860 
 861 void IdleGCTask::do_it(GCTaskManager* manager, uint which) {
 862   WaitForBarrierGCTask* wait_for_task = manager->idle_inactive_task();
 863   if (TraceGCTaskManager) {
 864     tty->print_cr("[" INTPTR_FORMAT "]"
 865                   " IdleGCTask:::do_it()"
 866       "  should_wait: %s",
 867       this, wait_for_task->should_wait() ? "true" : "false");
 868   }
 869   MutexLockerEx ml(manager->monitor(), Mutex::_no_safepoint_check_flag);
 870   if (TraceDynamicGCThreads) {
 871     gclog_or_tty->print_cr("--- idle %d", which);
 872   }
 873   // Increment has to be done when the idle tasks are created.
 874   // manager->increment_idle_workers();
 875   manager->monitor()->notify_all();
 876   while (wait_for_task->should_wait()) {
 877     if (TraceGCTaskManager) {
 878       tty->print_cr("[" INTPTR_FORMAT "]"
 879                     " IdleGCTask::do_it()"
 880         "  [" INTPTR_FORMAT "] (%s)->wait()",
 881         this, manager->monitor(), manager->monitor()->name());
 882     }
 883     manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
 884   }
 885   manager->decrement_idle_workers();
 886   if (TraceDynamicGCThreads) {
 887     gclog_or_tty->print_cr("--- release %d", which);
 888   }
 889   if (TraceGCTaskManager) {
 890     tty->print_cr("[" INTPTR_FORMAT "]"
 891                   " IdleGCTask::do_it() returns"
 892       "  should_wait: %s",
 893       this, wait_for_task->should_wait() ? "true" : "false");
 894   }
 895   // Release monitor().
 896 }
 897 
 898 void IdleGCTask::destroy(IdleGCTask* that) {
 899   if (that != NULL) {
 900     that->destruct();
 901     if (that->is_c_heap_obj()) {
 902       FreeHeap(that);
 903     }
 904   }
 905 }
 906 
 907 void IdleGCTask::destruct() {
 908   // This has to know it's superclass structure, just like the constructor.
 909   this->GCTask::destruct();
 910   // Nothing else to do.
 911 }
 912 
 913 //
 914 // BarrierGCTask
 915 //
 916 
 917 void BarrierGCTask::do_it(GCTaskManager* manager, uint which) {
 918   // Wait for this to be the only busy worker.
 919   // ??? I thought of having a StackObj class
 920   //     whose constructor would grab the lock and come to the barrier,
 921   //     and whose destructor would release the lock,
 922   //     but that seems like too much mechanism for two lines of code.
 923   MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
 924   do_it_internal(manager, which);
 925   // Release manager->lock().
 926 }
 927 
 928 void BarrierGCTask::do_it_internal(GCTaskManager* manager, uint which) {
 929   // Wait for this to be the only busy worker.
 930   assert(manager->monitor()->owned_by_self(), "don't own the lock");
 931   assert(manager->is_blocked(), "manager isn't blocked");
 932   while (manager->busy_workers() > 1) {
 933     if (TraceGCTaskManager) {
 934       tty->print_cr("BarrierGCTask::do_it(%u) waiting on %u workers",
 935                     which, manager->busy_workers());
 936     }
 937     manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
 938   }
 939 }
 940 
 941 void BarrierGCTask::destruct() {
 942   this->GCTask::destruct();
 943   // Nothing else to do.
 944 }
 945 
 946 //
 947 // ReleasingBarrierGCTask
 948 //
 949 
 950 void ReleasingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
 951   MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
 952   do_it_internal(manager, which);
 953   manager->release_all_resources();
 954   // Release manager->lock().
 955 }
 956 
 957 void ReleasingBarrierGCTask::destruct() {
 958   this->BarrierGCTask::destruct();
 959   // Nothing else to do.
 960 }
 961 
 962 //
 963 // NotifyingBarrierGCTask
 964 //
 965 
 966 void NotifyingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
 967   MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
 968   do_it_internal(manager, which);
 969   NotifyDoneClosure* ndc = notify_done_closure();
 970   if (ndc != NULL) {
 971     ndc->notify(manager);
 972   }
 973   // Release manager->lock().
 974 }
 975 
 976 void NotifyingBarrierGCTask::destruct() {
 977   this->BarrierGCTask::destruct();
 978   // Nothing else to do.
 979 }
 980 
 981 //
 982 // WaitForBarrierGCTask
 983 //
 984 WaitForBarrierGCTask* WaitForBarrierGCTask::create() {
 985   WaitForBarrierGCTask* result = new WaitForBarrierGCTask(false);
 986   return result;
 987 }
 988 
 989 WaitForBarrierGCTask* WaitForBarrierGCTask::create_on_c_heap() {
 990   WaitForBarrierGCTask* result =
 991     new (ResourceObj::C_HEAP, mtGC) WaitForBarrierGCTask(true);
 992   return result;
 993 }
 994 
 995 WaitForBarrierGCTask::WaitForBarrierGCTask(bool on_c_heap) :
 996   _is_c_heap_obj(on_c_heap) {
 997   _monitor = MonitorSupply::reserve();
 998   set_should_wait(true);
 999   if (TraceGCTaskManager) {
1000     tty->print_cr("[" INTPTR_FORMAT "]"
1001                   " WaitForBarrierGCTask::WaitForBarrierGCTask()"
1002                   "  monitor: " INTPTR_FORMAT,
1003                   this, monitor());
1004   }
1005 }
1006 
1007 void WaitForBarrierGCTask::destroy(WaitForBarrierGCTask* that) {
1008   if (that != NULL) {
1009     if (TraceGCTaskManager) {
1010       tty->print_cr("[" INTPTR_FORMAT "]"
1011                     " WaitForBarrierGCTask::destroy()"
1012                     "  is_c_heap_obj: %s"
1013                     "  monitor: " INTPTR_FORMAT,
1014                     that,
1015                     that->is_c_heap_obj() ? "true" : "false",
1016                     that->monitor());
1017     }
1018     that->destruct();
1019     if (that->is_c_heap_obj()) {
1020       FreeHeap(that);
1021     }
1022   }
1023 }
1024 
1025 void WaitForBarrierGCTask::destruct() {
1026   assert(monitor() != NULL, "monitor should not be NULL");
1027   if (TraceGCTaskManager) {
1028     tty->print_cr("[" INTPTR_FORMAT "]"
1029                   " WaitForBarrierGCTask::destruct()"
1030                   "  monitor: " INTPTR_FORMAT,
1031                   this, monitor());
1032   }
1033   this->BarrierGCTask::destruct();
1034   // Clean up that should be in the destructor,
1035   // except that ResourceMarks don't call destructors.
1036    if (monitor() != NULL) {
1037      MonitorSupply::release(monitor());
1038   }
1039   _monitor = (Monitor*) 0xDEAD000F;
1040 }
1041 
1042 void WaitForBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
1043   if (TraceGCTaskManager) {
1044     tty->print_cr("[" INTPTR_FORMAT "]"
1045                   " WaitForBarrierGCTask::do_it() waiting for idle"
1046                   "  monitor: " INTPTR_FORMAT,
1047                   this, monitor());
1048   }
1049   {
1050     // First, wait for the barrier to arrive.
1051     MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
1052     do_it_internal(manager, which);
1053     // Release manager->lock().
1054   }
1055   {
1056     // Then notify the waiter.
1057     MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
1058     set_should_wait(false);
1059     // Waiter doesn't miss the notify in the wait_for method
1060     // since it checks the flag after grabbing the monitor.
1061     if (TraceGCTaskManager) {
1062       tty->print_cr("[" INTPTR_FORMAT "]"
1063                     " WaitForBarrierGCTask::do_it()"
1064                     "  [" INTPTR_FORMAT "] (%s)->notify_all()",
1065                     this, monitor(), monitor()->name());
1066     }
1067     monitor()->notify_all();
1068     // Release monitor().
1069   }
1070 }
1071 
1072 void WaitForBarrierGCTask::wait_for(bool reset) {
1073   if (TraceGCTaskManager) {
1074     tty->print_cr("[" INTPTR_FORMAT "]"
1075                   " WaitForBarrierGCTask::wait_for()"
1076       "  should_wait: %s",
1077       this, should_wait() ? "true" : "false");
1078   }
1079   {
1080     // Grab the lock and check again.
1081     MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
1082     while (should_wait()) {
1083       if (TraceGCTaskManager) {
1084         tty->print_cr("[" INTPTR_FORMAT "]"
1085                       " WaitForBarrierGCTask::wait_for()"
1086           "  [" INTPTR_FORMAT "] (%s)->wait()",
1087           this, monitor(), monitor()->name());
1088       }
1089       monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
1090     }
1091     // Reset the flag in case someone reuses this task.
1092     if (reset) {
1093       set_should_wait(true);
1094     }
1095     if (TraceGCTaskManager) {
1096       tty->print_cr("[" INTPTR_FORMAT "]"
1097                     " WaitForBarrierGCTask::wait_for() returns"
1098         "  should_wait: %s",
1099         this, should_wait() ? "true" : "false");
1100     }
1101     // Release monitor().
1102   }
1103 }
1104 
1105 Mutex*                   MonitorSupply::_lock     = NULL;
1106 GrowableArray<Monitor*>* MonitorSupply::_freelist = NULL;
1107 
1108 Monitor* MonitorSupply::reserve() {
1109   Monitor* result = NULL;
1110   // Lazy initialization: possible race.
1111   if (lock() == NULL) {
1112     _lock = new Mutex(Mutex::barrier,                  // rank
1113                       "MonitorSupply mutex",           // name
1114                       Mutex::_allow_vm_block_flag);    // allow_vm_block
1115   }
1116   {
1117     MutexLockerEx ml(lock());
1118     // Lazy initialization.
1119     if (freelist() == NULL) {
1120       _freelist =
1121         new(ResourceObj::C_HEAP, mtGC) GrowableArray<Monitor*>(ParallelGCThreads,
1122                                                          true);
1123     }
1124     if (! freelist()->is_empty()) {
1125       result = freelist()->pop();
1126     } else {
1127       result = new Monitor(Mutex::barrier,                  // rank
1128                            "MonitorSupply monitor",         // name
1129                            Mutex::_allow_vm_block_flag,     // allow_vm_block
1130                            Monitor::_safepoint_check_never);
1131         }
1132     guarantee(result != NULL, "shouldn't return NULL");
1133     assert(!result->is_locked(), "shouldn't be locked");
1134     // release lock().
1135   }
1136   return result;
1137 }
1138 
1139 void MonitorSupply::release(Monitor* instance) {
1140   assert(instance != NULL, "shouldn't release NULL");
1141   assert(!instance->is_locked(), "shouldn't be locked");
1142   {
1143     MutexLockerEx ml(lock());
1144     freelist()->push(instance);
1145     // release lock().
1146   }
1147 }