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