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