1 /*
   2  * Copyright (c) 2013, 2020, Red Hat, Inc. 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 
  27 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahControlThread.hpp"
  30 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  33 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  34 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  35 #include "gc/shenandoah/shenandoahUtils.hpp"
  36 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  37 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  38 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  39 #include "memory/iterator.hpp"
  40 #include "memory/universe.hpp"
  41 #include "runtime/atomic.hpp"
  42 
  43 ShenandoahControlThread::ShenandoahControlThread() :
  44   ConcurrentGCThread(),
  45   _alloc_failure_waiters_lock(Mutex::leaf, "ShenandoahAllocFailureGC_lock", true, Monitor::_safepoint_check_always),
  46   _gc_waiters_lock(Mutex::leaf, "ShenandoahRequestedGC_lock", true, Monitor::_safepoint_check_always),
  47   _periodic_task(this),
  48   _requested_gc_cause(GCCause::_no_cause_specified),
  49   _degen_point(ShenandoahHeap::_degenerated_outside_cycle),
  50   _allocs_seen(0) {
  51 
  52   reset_gc_id();
  53   create_and_start();
  54   _periodic_task.enroll();
  55   _periodic_satb_flush_task.enroll();
  56   if (ShenandoahPacing) {
  57     _periodic_pacer_notify_task.enroll();
  58   }
  59 }
  60 
  61 ShenandoahControlThread::~ShenandoahControlThread() {
  62   // This is here so that super is called.
  63 }
  64 
  65 void ShenandoahPeriodicTask::task() {
  66   _thread->handle_force_counters_update();
  67   _thread->handle_counters_update();
  68 }
  69 
  70 void ShenandoahPeriodicSATBFlushTask::task() {
  71   ShenandoahHeap::heap()->force_satb_flush_all_threads();
  72 }
  73 
  74 void ShenandoahPeriodicPacerNotify::task() {
  75   assert(ShenandoahPacing, "Should not be here otherwise");
  76   ShenandoahHeap::heap()->pacer()->notify_waiters();
  77 }
  78 
  79 void ShenandoahControlThread::run_service() {
  80   ShenandoahHeap* heap = ShenandoahHeap::heap();
  81 
  82   GCMode default_mode = concurrent_normal;
  83   GCCause::Cause default_cause = GCCause::_shenandoah_concurrent_gc;
  84   int sleep = ShenandoahControlIntervalMin;
  85 
  86   double last_shrink_time = os::elapsedTime();
  87   double last_sleep_adjust_time = os::elapsedTime();
  88 
  89   // Shrink period avoids constantly polling regions for shrinking.
  90   // Having a period 10x lower than the delay would mean we hit the
  91   // shrinking with lag of less than 1/10-th of true delay.
  92   // ShenandoahUncommitDelay is in msecs, but shrink_period is in seconds.
  93   double shrink_period = (double)ShenandoahUncommitDelay / 1000 / 10;
  94 
  95   ShenandoahCollectorPolicy* policy = heap->shenandoah_policy();
  96   ShenandoahHeuristics* heuristics = heap->heuristics();
  97   while (!in_graceful_shutdown() && !should_terminate()) {
  98     // Figure out if we have pending requests.
  99     bool alloc_failure_pending = _alloc_failure_gc.is_set();
 100     bool explicit_gc_requested = _gc_requested.is_set() &&  is_explicit_gc(_requested_gc_cause);
 101     bool implicit_gc_requested = _gc_requested.is_set() && !is_explicit_gc(_requested_gc_cause);
 102 
 103     // This control loop iteration have seen this much allocations.
 104     size_t allocs_seen = Atomic::xchg(&_allocs_seen, (size_t)0);
 105 
 106     // Choose which GC mode to run in. The block below should select a single mode.
 107     GCMode mode = none;
 108     GCCause::Cause cause = GCCause::_last_gc_cause;
 109     ShenandoahHeap::ShenandoahDegenPoint degen_point = ShenandoahHeap::_degenerated_unset;
 110 
 111     if (alloc_failure_pending) {
 112       // Allocation failure takes precedence: we have to deal with it first thing
 113       log_info(gc)("Trigger: Handle Allocation Failure");
 114 
 115       cause = GCCause::_allocation_failure;
 116 
 117       // Consume the degen point, and seed it with default value
 118       degen_point = _degen_point;
 119       _degen_point = ShenandoahHeap::_degenerated_outside_cycle;
 120 
 121       if (ShenandoahDegeneratedGC && heuristics->should_degenerate_cycle()) {
 122         heuristics->record_allocation_failure_gc();
 123         policy->record_alloc_failure_to_degenerated(degen_point);
 124         mode = stw_degenerated;
 125       } else {
 126         heuristics->record_allocation_failure_gc();
 127         policy->record_alloc_failure_to_full();
 128         mode = stw_full;
 129       }
 130 
 131     } else if (explicit_gc_requested) {
 132       cause = _requested_gc_cause;
 133       log_info(gc)("Trigger: Explicit GC request (%s)", GCCause::to_string(cause));
 134 
 135       heuristics->record_requested_gc();
 136 
 137       if (ExplicitGCInvokesConcurrent) {
 138         policy->record_explicit_to_concurrent();
 139         mode = default_mode;
 140         // Unload and clean up everything
 141         heap->set_process_references(heuristics->can_process_references());
 142         heap->set_unload_classes(heuristics->can_unload_classes());
 143       } else {
 144         policy->record_explicit_to_full();
 145         mode = stw_full;
 146       }
 147     } else if (implicit_gc_requested) {
 148       cause = _requested_gc_cause;
 149       log_info(gc)("Trigger: Implicit GC request (%s)", GCCause::to_string(cause));
 150 
 151       heuristics->record_requested_gc();
 152 
 153       if (ShenandoahImplicitGCInvokesConcurrent) {
 154         policy->record_implicit_to_concurrent();
 155         mode = default_mode;
 156 
 157         // Unload and clean up everything
 158         heap->set_process_references(heuristics->can_process_references());
 159         heap->set_unload_classes(heuristics->can_unload_classes());
 160       } else {
 161         policy->record_implicit_to_full();
 162         mode = stw_full;
 163       }
 164     } else {
 165       // Potential normal cycle: ask heuristics if it wants to act
 166       if (heuristics->should_start_gc()) {
 167         mode = default_mode;
 168         cause = default_cause;
 169       }
 170 
 171       // Ask policy if this cycle wants to process references or unload classes
 172       heap->set_process_references(heuristics->should_process_references());
 173       heap->set_unload_classes(heuristics->should_unload_classes());
 174     }
 175 
 176     // Blow all soft references on this cycle, if handling allocation failure,
 177     // either implicit or explicit GC request,  or we are requested to do so unconditionally.
 178     if (alloc_failure_pending || implicit_gc_requested || explicit_gc_requested || ShenandoahAlwaysClearSoftRefs) {
 179       heap->soft_ref_policy()->set_should_clear_all_soft_refs(true);
 180     }
 181 
 182     bool gc_requested = (mode != none);
 183     assert (!gc_requested || cause != GCCause::_last_gc_cause, "GC cause should be set");
 184 
 185     if (gc_requested) {
 186       // GC is starting, bump the internal ID
 187       update_gc_id();
 188 
 189       heap->reset_bytes_allocated_since_gc_start();
 190 
 191       // Use default constructor to snapshot the Metaspace state before GC.
 192       metaspace::MetaspaceSizesSnapshot meta_sizes;
 193 
 194       // If GC was requested, we are sampling the counters even without actual triggers
 195       // from allocation machinery. This captures GC phases more accurately.
 196       set_forced_counters_update(true);
 197 
 198       // If GC was requested, we better dump freeset data for performance debugging
 199       {
 200         ShenandoahHeapLocker locker(heap->lock());
 201         heap->free_set()->log_status();
 202       }
 203 
 204       switch (mode) {
 205         case concurrent_normal:
 206           service_concurrent_normal_cycle(cause);
 207           break;
 208         case stw_degenerated:
 209           service_stw_degenerated_cycle(cause, degen_point);
 210           break;
 211         case stw_full:
 212           service_stw_full_cycle(cause);
 213           break;
 214         default:
 215           ShouldNotReachHere();
 216       }
 217 
 218       // If this was the requested GC cycle, notify waiters about it
 219       if (explicit_gc_requested || implicit_gc_requested) {
 220         notify_gc_waiters();
 221       }
 222 
 223       // If this was the allocation failure GC cycle, notify waiters about it
 224       if (alloc_failure_pending) {
 225         notify_alloc_failure_waiters();
 226       }
 227 
 228       // Report current free set state at the end of cycle, whether
 229       // it is a normal completion, or the abort.
 230       {
 231         ShenandoahHeapLocker locker(heap->lock());
 232         heap->free_set()->log_status();
 233 
 234         // Notify Universe about new heap usage. This has implications for
 235         // global soft refs policy, and we better report it every time heap
 236         // usage goes down.
 237         Universe::update_heap_info_at_gc();
 238 
 239         // Signal that we have completed a visit to all live objects.
 240         Universe::heap()->record_whole_heap_examined_timestamp();
 241       }
 242 
 243       // Disable forced counters update, and update counters one more time
 244       // to capture the state at the end of GC session.
 245       handle_force_counters_update();
 246       set_forced_counters_update(false);
 247 
 248       // Retract forceful part of soft refs policy
 249       heap->soft_ref_policy()->set_should_clear_all_soft_refs(false);
 250 
 251       // Clear metaspace oom flag, if current cycle unloaded classes
 252       if (heap->unload_classes()) {
 253         heuristics->clear_metaspace_oom();
 254       }
 255 
 256       // Commit worker statistics to cycle data
 257       heap->phase_timings()->flush_par_workers_to_cycle();
 258       if (ShenandoahPacing) {
 259         heap->pacer()->flush_stats_to_cycle();
 260       }
 261 
 262       // Print GC stats for current cycle
 263       {
 264         LogTarget(Info, gc, stats) lt;
 265         if (lt.is_enabled()) {
 266           ResourceMark rm;
 267           LogStream ls(lt);
 268           heap->phase_timings()->print_cycle_on(&ls);
 269           if (ShenandoahPacing) {
 270             heap->pacer()->print_cycle_on(&ls);
 271           }
 272         }
 273       }
 274 
 275       // Commit statistics to globals
 276       heap->phase_timings()->flush_cycle_to_global();
 277 
 278       // Print Metaspace change following GC (if logging is enabled).
 279       MetaspaceUtils::print_metaspace_change(meta_sizes);
 280 
 281       // GC is over, we are at idle now
 282       if (ShenandoahPacing) {
 283         heap->pacer()->setup_for_idle();
 284       }
 285     } else {
 286       // Allow allocators to know we have seen this much regions
 287       if (ShenandoahPacing && (allocs_seen > 0)) {
 288         heap->pacer()->report_alloc(allocs_seen);
 289       }
 290     }
 291 
 292     double current = os::elapsedTime();
 293 
 294     if (ShenandoahUncommit && (explicit_gc_requested || (current - last_shrink_time > shrink_period))) {
 295       // Try to uncommit enough stale regions. Explicit GC tries to uncommit everything.
 296       // Regular paths uncommit only occasionally.
 297       double shrink_before = explicit_gc_requested ?
 298                              current :
 299                              current - (ShenandoahUncommitDelay / 1000.0);
 300       service_uncommit(shrink_before);
 301       heap->phase_timings()->flush_cycle_to_global();
 302       last_shrink_time = current;
 303     }
 304 
 305     // Wait before performing the next action. If allocation happened during this wait,
 306     // we exit sooner, to let heuristics re-evaluate new conditions. If we are at idle,
 307     // back off exponentially.
 308     if (_heap_changed.try_unset()) {
 309       sleep = ShenandoahControlIntervalMin;
 310     } else if ((current - last_sleep_adjust_time) * 1000 > ShenandoahControlIntervalAdjustPeriod){
 311       sleep = MIN2<int>(ShenandoahControlIntervalMax, MAX2(1, sleep * 2));
 312       last_sleep_adjust_time = current;
 313     }
 314     os::naked_short_sleep(sleep);
 315   }
 316 
 317   // Wait for the actual stop(), can't leave run_service() earlier.
 318   while (!should_terminate()) {
 319     os::naked_short_sleep(ShenandoahControlIntervalMin);
 320   }
 321 }
 322 
 323 void ShenandoahControlThread::service_concurrent_normal_cycle(GCCause::Cause cause) {
 324   // Normal cycle goes via all concurrent phases. If allocation failure (af) happens during
 325   // any of the concurrent phases, it first degrades to Degenerated GC and completes GC there.
 326   // If second allocation failure happens during Degenerated GC cycle (for example, when GC
 327   // tries to evac something and no memory is available), cycle degrades to Full GC.
 328   //
 329   // There are also a shortcut through the normal cycle: immediate garbage shortcut, when
 330   // heuristics says there are no regions to compact, and all the collection comes from immediately
 331   // reclaimable regions.
 332   //
 333   // ................................................................................................
 334   //
 335   //                                    (immediate garbage shortcut)                Concurrent GC
 336   //                             /-------------------------------------------\
 337   //                             |                                           |
 338   //                             |                                           |
 339   //                             |                                           |
 340   //                             |                                           v
 341   // [START] ----> Conc Mark ----o----> Conc Evac --o--> Conc Update-Refs ---o----> [END]
 342   //                   |                    |                 |              ^
 343   //                   | (af)               | (af)            | (af)         |
 344   // ..................|....................|.................|..............|.......................
 345   //                   |                    |                 |              |
 346   //                   |                    |                 |              |      Degenerated GC
 347   //                   v                    v                 v              |
 348   //               STW Mark ----------> STW Evac ----> STW Update-Refs ----->o
 349   //                   |                    |                 |              ^
 350   //                   | (af)               | (af)            | (af)         |
 351   // ..................|....................|.................|..............|.......................
 352   //                   |                    |                 |              |
 353   //                   |                    v                 |              |      Full GC
 354   //                   \------------------->o<----------------/              |
 355   //                                        |                                |
 356   //                                        v                                |
 357   //                                      Full GC  --------------------------/
 358   //
 359   ShenandoahHeap* heap = ShenandoahHeap::heap();
 360 
 361   if (check_cancellation_or_degen(ShenandoahHeap::_degenerated_outside_cycle)) return;
 362 
 363   GCIdMark gc_id_mark;
 364   ShenandoahGCSession session(cause);
 365 
 366   TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
 367 
 368   // Reset for upcoming marking
 369   heap->entry_reset();
 370 
 371   // Start initial mark under STW
 372   heap->vmop_entry_init_mark();
 373 
 374   // Continue concurrent mark
 375   heap->entry_mark();
 376   if (check_cancellation_or_degen(ShenandoahHeap::_degenerated_mark)) return;
 377 
 378   // If not cancelled, can try to concurrently pre-clean
 379   heap->entry_preclean();
 380 
 381   // Complete marking under STW, and start evacuation
 382   heap->vmop_entry_final_mark();
 383 
 384   // Process weak roots that might still point to regions that would be broken by cleanup
 385   if (heap->is_concurrent_weak_root_in_progress()) {
 386     heap->entry_weak_roots();
 387   }
 388 
 389   // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
 390   // the space. This would be the last action if there is nothing to evacuate.
 391   heap->entry_cleanup_early();
 392 
 393   {
 394     ShenandoahHeapLocker locker(heap->lock());
 395     heap->free_set()->log_status();
 396   }
 397 
 398   // Perform concurrent class unloading
 399   if (heap->is_concurrent_weak_root_in_progress()) {
 400     heap->entry_class_unloading();
 401   }
 402 
 403   // Processing strong roots
 404   // This may be skipped if there is nothing to update/evacuate.
 405   // If so, strong_root_in_progress would be unset.
 406   if (heap->is_concurrent_strong_root_in_progress()) {
 407     heap->entry_strong_roots();
 408   }
 409 
 410   // Continue the cycle with evacuation and optional update-refs.
 411   // This may be skipped if there is nothing to evacuate.
 412   // If so, evac_in_progress would be unset by collection set preparation code.
 413   if (heap->is_evacuation_in_progress()) {
 414     // Concurrently evacuate
 415     heap->entry_evac();
 416     if (check_cancellation_or_degen(ShenandoahHeap::_degenerated_evac)) return;
 417 
 418     // Perform update-refs phase.
 419     heap->vmop_entry_init_updaterefs();
 420     heap->entry_updaterefs();
 421     if (check_cancellation_or_degen(ShenandoahHeap::_degenerated_updaterefs)) return;
 422 
 423     heap->vmop_entry_final_updaterefs();
 424 
 425     // Update references freed up collection set, kick the cleanup to reclaim the space.
 426     heap->entry_cleanup_complete();
 427   } else {
 428     // Concurrent weak/strong root flags are unset concurrently. We depend on updateref GC safepoints
 429     // to ensure the changes are visible to all mutators before gc cycle is completed.
 430     // In case of no evacuation, updateref GC safepoints are skipped. Therefore, we will need
 431     // to perform thread handshake to ensure their consistences.
 432     heap->entry_rendezvous_roots();
 433   }
 434 
 435   // Cycle is complete
 436   heap->heuristics()->record_success_concurrent();
 437   heap->shenandoah_policy()->record_success_concurrent();
 438 }
 439 
 440 bool ShenandoahControlThread::check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point) {
 441   ShenandoahHeap* heap = ShenandoahHeap::heap();
 442   if (heap->cancelled_gc()) {
 443     assert (is_alloc_failure_gc() || in_graceful_shutdown(), "Cancel GC either for alloc failure GC, or gracefully exiting");
 444     if (!in_graceful_shutdown()) {
 445       assert (_degen_point == ShenandoahHeap::_degenerated_outside_cycle,
 446               "Should not be set yet: %s", ShenandoahHeap::degen_point_to_string(_degen_point));
 447       _degen_point = point;
 448     }
 449     return true;
 450   }
 451   return false;
 452 }
 453 
 454 void ShenandoahControlThread::stop_service() {
 455   // Nothing to do here.
 456 }
 457 
 458 void ShenandoahControlThread::service_stw_full_cycle(GCCause::Cause cause) {
 459   GCIdMark gc_id_mark;
 460   ShenandoahGCSession session(cause);
 461 
 462   ShenandoahHeap* heap = ShenandoahHeap::heap();
 463   heap->vmop_entry_full(cause);
 464 
 465   heap->heuristics()->record_success_full();
 466   heap->shenandoah_policy()->record_success_full();
 467 }
 468 
 469 void ShenandoahControlThread::service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point) {
 470   assert (point != ShenandoahHeap::_degenerated_unset, "Degenerated point should be set");
 471 
 472   GCIdMark gc_id_mark;
 473   ShenandoahGCSession session(cause);
 474 
 475   ShenandoahHeap* heap = ShenandoahHeap::heap();
 476   heap->vmop_degenerated(point);
 477 
 478   heap->heuristics()->record_success_degenerated();
 479   heap->shenandoah_policy()->record_success_degenerated();
 480 }
 481 
 482 void ShenandoahControlThread::service_uncommit(double shrink_before) {
 483   ShenandoahHeap* heap = ShenandoahHeap::heap();
 484 
 485   // Determine if there is work to do. This avoids taking heap lock if there is
 486   // no work available, avoids spamming logs with superfluous logging messages,
 487   // and minimises the amount of work while locks are taken.
 488 
 489   if (heap->committed() <= heap->min_capacity()) return;
 490 
 491   bool has_work = false;
 492   for (size_t i = 0; i < heap->num_regions(); i++) {
 493     ShenandoahHeapRegion *r = heap->get_region(i);
 494     if (r->is_empty_committed() && (r->empty_time() < shrink_before)) {
 495       has_work = true;
 496       break;
 497     }
 498   }
 499 
 500   if (has_work) {
 501     heap->entry_uncommit(shrink_before);
 502   }
 503 }
 504 
 505 bool ShenandoahControlThread::is_explicit_gc(GCCause::Cause cause) const {
 506   return GCCause::is_user_requested_gc(cause) ||
 507          GCCause::is_serviceability_requested_gc(cause);
 508 }
 509 
 510 void ShenandoahControlThread::request_gc(GCCause::Cause cause) {
 511   assert(GCCause::is_user_requested_gc(cause) ||
 512          GCCause::is_serviceability_requested_gc(cause) ||
 513          cause == GCCause::_metadata_GC_clear_soft_refs ||
 514          cause == GCCause::_full_gc_alot ||
 515          cause == GCCause::_wb_full_gc ||
 516          cause == GCCause::_scavenge_alot,
 517          "only requested GCs here");
 518 
 519   if (is_explicit_gc(cause)) {
 520     if (!DisableExplicitGC) {
 521       handle_requested_gc(cause);
 522     }
 523   } else {
 524     handle_requested_gc(cause);
 525   }
 526 }
 527 
 528 void ShenandoahControlThread::handle_requested_gc(GCCause::Cause cause) {
 529   // Make sure we have at least one complete GC cycle before unblocking
 530   // from the explicit GC request.
 531   //
 532   // This is especially important for weak references cleanup and/or native
 533   // resources (e.g. DirectByteBuffers) machinery: when explicit GC request
 534   // comes very late in the already running cycle, it would miss lots of new
 535   // opportunities for cleanup that were made available before the caller
 536   // requested the GC.
 537 
 538   MonitorLocker ml(&_gc_waiters_lock);
 539   size_t current_gc_id = get_gc_id();
 540   size_t required_gc_id = current_gc_id + 1;
 541   while (current_gc_id < required_gc_id) {
 542     _gc_requested.set();
 543     _requested_gc_cause = cause;
 544     ml.wait();
 545     current_gc_id = get_gc_id();
 546   }
 547 }
 548 
 549 void ShenandoahControlThread::handle_alloc_failure(ShenandoahAllocRequest& req) {
 550   ShenandoahHeap* heap = ShenandoahHeap::heap();
 551 
 552   assert(current()->is_Java_thread(), "expect Java thread here");
 553 
 554   if (try_set_alloc_failure_gc()) {
 555     // Only report the first allocation failure
 556     log_info(gc)("Failed to allocate %s, " SIZE_FORMAT "%s",
 557                  req.type_string(),
 558                  byte_size_in_proper_unit(req.size() * HeapWordSize), proper_unit_for_byte_size(req.size() * HeapWordSize));
 559 
 560     // Now that alloc failure GC is scheduled, we can abort everything else
 561     heap->cancel_gc(GCCause::_allocation_failure);
 562   }
 563 
 564   MonitorLocker ml(&_alloc_failure_waiters_lock);
 565   while (is_alloc_failure_gc()) {
 566     ml.wait();
 567   }
 568 }
 569 
 570 void ShenandoahControlThread::handle_alloc_failure_evac(size_t words) {
 571   ShenandoahHeap* heap = ShenandoahHeap::heap();
 572 
 573   if (try_set_alloc_failure_gc()) {
 574     // Only report the first allocation failure
 575     log_info(gc)("Failed to allocate " SIZE_FORMAT "%s for evacuation",
 576                  byte_size_in_proper_unit(words * HeapWordSize), proper_unit_for_byte_size(words * HeapWordSize));
 577   }
 578 
 579   // Forcefully report allocation failure
 580   heap->cancel_gc(GCCause::_shenandoah_allocation_failure_evac);
 581 }
 582 
 583 void ShenandoahControlThread::notify_alloc_failure_waiters() {
 584   _alloc_failure_gc.unset();
 585   MonitorLocker ml(&_alloc_failure_waiters_lock);
 586   ml.notify_all();
 587 }
 588 
 589 bool ShenandoahControlThread::try_set_alloc_failure_gc() {
 590   return _alloc_failure_gc.try_set();
 591 }
 592 
 593 bool ShenandoahControlThread::is_alloc_failure_gc() {
 594   return _alloc_failure_gc.is_set();
 595 }
 596 
 597 void ShenandoahControlThread::notify_gc_waiters() {
 598   _gc_requested.unset();
 599   MonitorLocker ml(&_gc_waiters_lock);
 600   ml.notify_all();
 601 }
 602 
 603 void ShenandoahControlThread::handle_counters_update() {
 604   if (_do_counters_update.is_set()) {
 605     _do_counters_update.unset();
 606     ShenandoahHeap::heap()->monitoring_support()->update_counters();
 607   }
 608 }
 609 
 610 void ShenandoahControlThread::handle_force_counters_update() {
 611   if (_force_counters_update.is_set()) {
 612     _do_counters_update.unset(); // reset these too, we do update now!
 613     ShenandoahHeap::heap()->monitoring_support()->update_counters();
 614   }
 615 }
 616 
 617 void ShenandoahControlThread::notify_heap_changed() {
 618   // This is called from allocation path, and thus should be fast.
 619 
 620   // Update monitoring counters when we took a new region. This amortizes the
 621   // update costs on slow path.
 622   if (_do_counters_update.is_unset()) {
 623     _do_counters_update.set();
 624   }
 625   // Notify that something had changed.
 626   if (_heap_changed.is_unset()) {
 627     _heap_changed.set();
 628   }
 629 }
 630 
 631 void ShenandoahControlThread::pacing_notify_alloc(size_t words) {
 632   assert(ShenandoahPacing, "should only call when pacing is enabled");
 633   Atomic::add(&_allocs_seen, words);
 634 }
 635 
 636 void ShenandoahControlThread::set_forced_counters_update(bool value) {
 637   _force_counters_update.set_cond(value);
 638 }
 639 
 640 void ShenandoahControlThread::reset_gc_id() {
 641   Atomic::store(&_gc_id, (size_t)0);
 642 }
 643 
 644 void ShenandoahControlThread::update_gc_id() {
 645   Atomic::inc(&_gc_id);
 646 }
 647 
 648 size_t ShenandoahControlThread::get_gc_id() {
 649   return Atomic::load(&_gc_id);
 650 }
 651 
 652 void ShenandoahControlThread::print() const {
 653   print_on(tty);
 654 }
 655 
 656 void ShenandoahControlThread::print_on(outputStream* st) const {
 657   st->print("Shenandoah Concurrent Thread");
 658   Thread::print_on(st);
 659   st->cr();
 660 }
 661 
 662 void ShenandoahControlThread::start() {
 663   create_and_start();
 664 }
 665 
 666 void ShenandoahControlThread::prepare_for_graceful_shutdown() {
 667   _graceful_shutdown.set();
 668 }
 669 
 670 bool ShenandoahControlThread::in_graceful_shutdown() {
 671   return _graceful_shutdown.is_set();
 672 }