1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp"
  26 #include "gc_implementation/shenandoah/heuristics/shenandoahCompactHeuristics.hpp"
  27 #include "gc_implementation/shenandoah/shenandoahFreeSet.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahHeapRegion.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
  30 
  31 ShenandoahCompactHeuristics::ShenandoahCompactHeuristics() : ShenandoahHeuristics() {
  32   SHENANDOAH_ERGO_ENABLE_FLAG(ShenandoahUncommit);
  33   SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahAllocationThreshold,  10);
  34   SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahImmediateThreshold,   100);
  35   SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahUncommitDelay,        5000);
  36   SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahGuaranteedGCInterval, 30000);
  37   SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahGarbageThreshold,     20);
  38 }
  39 
  40 bool ShenandoahCompactHeuristics::should_start_normal_gc() const {
  41   ShenandoahHeap* heap = ShenandoahHeap::heap();
  42 
  43   size_t available = heap->free_set()->available();
  44   double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
  45   bool periodic_gc = (last_time_ms > ShenandoahGuaranteedGCInterval);
  46   size_t bytes_allocated = heap->bytes_allocated_since_gc_start();
  47   size_t threshold_bytes_allocated = heap->capacity() * ShenandoahAllocationThreshold / 100;
  48 
  49   if (available < threshold_bytes_allocated || bytes_allocated > threshold_bytes_allocated) {
  50     log_info(gc,ergo)("Concurrent marking triggered. Free: " SIZE_FORMAT "M, Allocated: " SIZE_FORMAT "M, Alloc Threshold: " SIZE_FORMAT "M",
  51                       available / M, bytes_allocated / M, threshold_bytes_allocated / M);
  52     return true;
  53   } else if (periodic_gc) {
  54     log_info(gc,ergo)("Periodic GC triggered. Time since last GC: %.0f ms, Guaranteed Interval: " UINTX_FORMAT " ms",
  55                       last_time_ms, ShenandoahGuaranteedGCInterval);
  56     return true;
  57   }
  58 
  59   return false;
  60 }
  61 
  62 void ShenandoahCompactHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
  63                                                                         RegionData* data, size_t size,
  64                                                                         size_t actual_free) {
  65 
  66   // Do not select too large CSet that would overflow the available free space
  67   size_t max_cset = actual_free * 3 / 4;
  68 
  69   log_info(gc, ergo)("CSet Selection. Actual Free: " SIZE_FORMAT "M, Max CSet: " SIZE_FORMAT "M",
  70                      actual_free / M, max_cset / M);
  71 
  72   size_t threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
  73 
  74   size_t live_cset = 0;
  75   for (size_t idx = 0; idx < size; idx++) {
  76     ShenandoahHeapRegion* r = data[idx]._region;
  77     size_t new_cset = live_cset + r->get_live_data_bytes();
  78     if (new_cset < max_cset && r->garbage() > threshold) {
  79       live_cset = new_cset;
  80       cset->add_region(r);
  81     }
  82   }
  83 }
  84 
  85 const char* ShenandoahCompactHeuristics::name() {
  86   return "compact";
  87 }
  88 
  89 bool ShenandoahCompactHeuristics::is_diagnostic() {
  90   return false;
  91 }
  92 
  93 bool ShenandoahCompactHeuristics::is_experimental() {
  94   return false;
  95 }