1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTORPOLICY_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTORPOLICY_HPP
 28 
 29 #include "gc/shared/gcTrace.hpp"
 30 #include "gc/shenandoah/shenandoahGC.hpp"
 31 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 32 #include "gc/shenandoah/shenandoahTrace.hpp"
 33 #include "memory/allocation.hpp"
 34 #include "utilities/ostream.hpp"
 35 
 36 class ShenandoahCollectorPolicy : public CHeapObj<mtGC> {
 37 private:
 38   size_t _success_concurrent_gcs;
 39   size_t _abbreviated_concurrent_gcs;
 40   size_t _success_degenerated_gcs;
 41   size_t _abbreviated_degenerated_gcs;
 42   // Written by control thread, read by mutators
 43   volatile size_t _success_full_gcs;
 44   uint _consecutive_degenerated_gcs;
 45   uint _consecutive_degenerated_gcs_without_progress;
 46   volatile size_t _consecutive_young_gcs;
 47   size_t _mixed_gcs;
 48   size_t _success_old_gcs;
 49   size_t _interrupted_old_gcs;
 50   size_t _alloc_failure_degenerated;
 51   size_t _alloc_failure_degenerated_upgrade_to_full;
 52   size_t _alloc_failure_full;
 53   size_t _collection_cause_counts[GCCause::_last_gc_cause];
 54   size_t _degen_point_counts[ShenandoahGC::_DEGENERATED_LIMIT];
 55 
 56   ShenandoahSharedFlag _in_shutdown;
 57   ShenandoahTracer* _tracer;
 58 
 59   void reset_consecutive_degenerated_gcs() {
 60     _consecutive_degenerated_gcs = 0;
 61     _consecutive_degenerated_gcs_without_progress = 0;
 62   }
 63 
 64 public:
 65   // The most common scenario for lack of good progress following a degenerated GC is an accumulation of floating
 66   // garbage during the most recently aborted concurrent GC effort.  With generational GC, it is far more effective to
 67   // reclaim this floating garbage with another degenerated cycle (which focuses on young generation and might require
 68   // a pause of 200 ms) rather than a full GC cycle (which may require over 2 seconds with a 10 GB old generation).
 69   //
 70   // In generational mode, we'll only upgrade to full GC if we've done two degen cycles in a row and both indicated
 71   // bad progress.  In non-generational mode, we'll preserve the original behavior, which is to upgrade to full
 72   // immediately following a degenerated cycle with bad progress.  This preserves original behavior of non-generational
 73   // Shenandoah to avoid introducing "surprising new behavior."  It also makes less sense with non-generational
 74   // Shenandoah to replace a full GC with a degenerated GC, because both have similar pause times in non-generational
 75   // mode.
 76   static constexpr size_t GENERATIONAL_CONSECUTIVE_BAD_DEGEN_PROGRESS_THRESHOLD = 2;
 77 
 78   ShenandoahCollectorPolicy();
 79 
 80   void record_mixed_cycle();
 81   void record_success_old();
 82   void record_interrupted_old();
 83 
 84   // A collection cycle may be "abbreviated" if Shenandoah finds a sufficient percentage
 85   // of regions that contain no live objects (ShenandoahImmediateThreshold). These cycles
 86   // end after final mark, skipping the evacuation and reference-updating phases. Such
 87   // cycles are very efficient and are worth tracking. Note that both degenerated and
 88   // concurrent cycles can be abbreviated.
 89   void record_success_concurrent(bool is_young, bool is_abbreviated);
 90 
 91   // Record that a degenerated cycle has been completed. Note that such a cycle may or
 92   // may not make "progress". We separately track the total number of degenerated cycles,
 93   // the number of consecutive degenerated cycles and the number of consecutive cycles that
 94   // fail to make good progress.
 95   void record_degenerated(bool is_young, bool is_abbreviated, bool progress);
 96   void record_success_full();
 97   void record_alloc_failure_to_degenerated(ShenandoahGC::ShenandoahDegenPoint point);
 98   void record_alloc_failure_to_full();
 99   void record_degenerated_upgrade_to_full();
100   void record_collection_cause(GCCause::Cause cause);
101 
102   void record_shutdown();
103   bool is_at_shutdown() const;
104 
105   ShenandoahTracer* tracer() const {return _tracer;}
106 
107   void print_gc_stats(outputStream* out) const;
108 
109   size_t full_gc_count() const {
110     return _success_full_gcs + _alloc_failure_degenerated_upgrade_to_full;
111   }
112 
113   // If the heuristics find that the number of consecutive degenerated cycles is above
114   // ShenandoahFullGCThreshold, then they will initiate a Full GC upon an allocation
115   // failure.
116   size_t consecutive_degenerated_gc_count() const {
117     return _consecutive_degenerated_gcs;
118   }
119 
120   // Genshen will only upgrade to a full gc after the configured number of futile degenerated cycles.
121   bool generational_should_upgrade_degenerated_gc() const {
122     return _consecutive_degenerated_gcs_without_progress >= GENERATIONAL_CONSECUTIVE_BAD_DEGEN_PROGRESS_THRESHOLD;
123   }
124 
125   static bool is_allocation_failure(GCCause::Cause cause);
126   static bool is_shenandoah_gc(GCCause::Cause cause);
127   static bool is_requested_gc(GCCause::Cause cause);
128   static bool is_explicit_gc(GCCause::Cause cause);
129   static bool should_run_full_gc(GCCause::Cause cause);
130   static bool should_handle_requested_gc(GCCause::Cause cause);
131 
132   size_t consecutive_young_gc_count() const {
133     return _consecutive_young_gcs;
134   }
135 
136 private:
137   void update_young(bool is_young);
138 };
139 
140 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTORPOLICY_HPP