1 /*
  2  * Copyright (c) 2018, 2019, 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 #ifndef SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHHEURISTICS_HPP
 26 #define SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHHEURISTICS_HPP
 27 
 28 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 29 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "runtime/globals_extension.hpp"
 32 
 33 #define SHENANDOAH_ERGO_DISABLE_FLAG(name)                                  \
 34   do {                                                                      \
 35     if (FLAG_IS_DEFAULT(name) && (name)) {                                  \
 36       log_info(gc)("Heuristics ergonomically sets -XX:-" #name);            \
 37       FLAG_SET_DEFAULT(name, false);                                        \
 38     }                                                                       \
 39   } while (0)
 40 
 41 #define SHENANDOAH_ERGO_ENABLE_FLAG(name)                                   \
 42   do {                                                                      \
 43     if (FLAG_IS_DEFAULT(name) && !(name)) {                                 \
 44       log_info(gc)("Heuristics ergonomically sets -XX:+" #name);            \
 45       FLAG_SET_DEFAULT(name, true);                                         \
 46     }                                                                       \
 47   } while (0)
 48 
 49 #define SHENANDOAH_ERGO_OVERRIDE_DEFAULT(name, value)                       \
 50   do {                                                                      \
 51     if (FLAG_IS_DEFAULT(name)) {                                            \
 52       log_info(gc)("Heuristics ergonomically sets -XX:" #name "=" #value);  \
 53       FLAG_SET_DEFAULT(name, value);                                        \
 54     }                                                                       \
 55   } while (0)
 56 
 57 class ShenandoahCollectionSet;
 58 class ShenandoahHeapRegion;
 59 class ShenandoahGeneration;
 60 class ShenandoahOldHeuristics;
 61 
 62 class ShenandoahHeuristics : public CHeapObj<mtGC> {
 63   static const intx Concurrent_Adjust   = -1; // recover from penalties
 64   static const intx Degenerated_Penalty = 10; // how much to penalize average GC duration history on Degenerated GC
 65   static const intx Full_Penalty        = 20; // how much to penalize average GC duration history on Full GC
 66 
 67 protected:
 68   typedef struct {
 69     ShenandoahHeapRegion* _region;
 70     size_t _garbage;
 71   } RegionData;
 72 
 73   ShenandoahGeneration* _generation;
 74 
 75   // if (_generation->generation_mode() == GLOBAL) _region_data represents
 76   //  the results of most recently completed global marking pass
 77   // if (_generation->generation_mode() == OLD) _region_data represents
 78   //  the results of most recently completed old-gen marking pass
 79   // if (_generation->generation_mode() == YOUNG) _region_data represents
 80   //  the resulits of most recently completed young-gen marking pass
 81   //
 82   // Note that there is some redundancy represented in _region_data because
 83   // each instance is an array large enough to hold all regions.  However,
 84   // any region in young-gen is not in old-gen.  And any time we are
 85   // making use of the GLOBAL data, there is no need to maintain the
 86   // YOUNG or OLD data.  Consider this redundancy of data structure to
 87   // have negligible cost unless proven otherwise.
 88   RegionData* _region_data;
 89 
 90   uint _degenerated_cycles_in_a_row;
 91   uint _successful_cycles_in_a_row;
 92 
 93   size_t _guaranteed_gc_interval;
 94 
 95   double _cycle_start;
 96   double _last_cycle_end;
 97 
 98   size_t _gc_times_learned;
 99   intx _gc_time_penalties;
100   TruncatedSeq* _gc_time_history;
101 
102   size_t _live_memory_last_cycle;
103   size_t _live_memory_penultimate_cycle;
104 
105   // There may be many threads that contend to set this flag
106   ShenandoahSharedFlag _metaspace_oom;
107 
108   static int compare_by_garbage(RegionData a, RegionData b);
109 
110   // TODO: We need to enhance this API to give visibility to accompanying old-gen evacuation effort.
111   // In the case that the old-gen evacuation effort is small or zero, the young-gen heuristics
112   // should feel free to dedicate increased efforts to young-gen evacuation.
113 
114   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set,
115                                                      RegionData* data, size_t data_size,
116                                                      size_t free) = 0;
117 
118   void adjust_penalty(intx step);
119 
120   bool in_generation(ShenandoahHeapRegion* region);
121 
122 public:
123   ShenandoahHeuristics(ShenandoahGeneration* generation);
124   virtual ~ShenandoahHeuristics();
125 
126   void record_metaspace_oom()     { _metaspace_oom.set(); }
127   void clear_metaspace_oom()      { _metaspace_oom.unset(); }
128   bool has_metaspace_oom() const  { return _metaspace_oom.is_set(); }
129 
130   void set_guaranteed_gc_interval(size_t guaranteed_gc_interval) {
131     _guaranteed_gc_interval = guaranteed_gc_interval;
132   }
133 
134   uint degenerated_cycles_in_a_row() {
135     return _degenerated_cycles_in_a_row;
136   }
137 
138   virtual void record_cycle_start();
139 
140   virtual void record_cycle_end();
141 
142   virtual bool should_start_gc();
143 
144   virtual bool should_degenerate_cycle();
145 
146   virtual void record_success_concurrent(bool abbreviated);
147 
148   virtual void record_success_degenerated();
149 
150   virtual void record_success_full();
151 
152   virtual void record_allocation_failure_gc();
153 
154   virtual void record_requested_gc();
155 
156   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set, ShenandoahOldHeuristics* old_heuristics);
157 
158   virtual bool can_unload_classes();
159   virtual bool can_unload_classes_normal();
160   virtual bool should_unload_classes();
161 
162   virtual const char* name() = 0;
163   virtual bool is_diagnostic() = 0;
164   virtual bool is_experimental() = 0;
165   virtual void initialize();
166 
167   double time_since_last_gc() const;
168 
169   void save_last_live_memory(size_t live_memory);
170   size_t get_last_live_memory();
171   size_t get_penultimate_live_memory();
172 };
173 
174 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHHEURISTICS_HPP