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/heuristics/shenandoahSpaceInfo.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.hpp"
 30 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 31 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 32 #include "memory/allocation.hpp"
 33 #include "runtime/globals_extension.hpp"
 34 
 35 #define SHENANDOAH_ERGO_DISABLE_FLAG(name)                                  \
 36   do {                                                                      \
 37     if (FLAG_IS_DEFAULT(name) && (name)) {                                  \
 38       log_info(gc)("Heuristics ergonomically sets -XX:-" #name);            \
 39       FLAG_SET_DEFAULT(name, false);                                        \
 40     }                                                                       \
 41   } while (0)
 42 
 43 #define SHENANDOAH_ERGO_ENABLE_FLAG(name)                                   \
 44   do {                                                                      \
 45     if (FLAG_IS_DEFAULT(name) && !(name)) {                                 \
 46       log_info(gc)("Heuristics ergonomically sets -XX:+" #name);            \
 47       FLAG_SET_DEFAULT(name, true);                                         \
 48     }                                                                       \
 49   } while (0)
 50 
 51 #define SHENANDOAH_ERGO_OVERRIDE_DEFAULT(name, value)                       \
 52   do {                                                                      \
 53     if (FLAG_IS_DEFAULT(name)) {                                            \
 54       log_info(gc)("Heuristics ergonomically sets -XX:" #name "=" #value);  \
 55       FLAG_SET_DEFAULT(name, value);                                        \
 56     }                                                                       \
 57   } while (0)
 58 
 59 class ShenandoahCollectionSet;
 60 class ShenandoahHeapRegion;
 61 
 62 /*
 63  * Shenandoah heuristics are primarily responsible for deciding when to start
 64  * a collection cycle and choosing which regions will be evacuated during the
 65  * cycle.
 66  */
 67 class ShenandoahHeuristics : public CHeapObj<mtGC> {
 68   static const intx Concurrent_Adjust   = -1; // recover from penalties
 69   static const intx Degenerated_Penalty = 10; // how much to penalize average GC duration history on Degenerated GC
 70   static const intx Full_Penalty        = 20; // how much to penalize average GC duration history on Full GC
 71 
 72 protected:
 73   typedef struct {
 74     ShenandoahHeapRegion* _region;
 75     size_t _garbage;
 76   } RegionData;
 77 
 78   // Source of information about the memory space managed by this heuristic
 79   ShenandoahSpaceInfo* _space_info;
 80 
 81   RegionData* _region_data;
 82 
 83   double _cycle_start;
 84   double _last_cycle_end;
 85 
 86   size_t _gc_times_learned;
 87   intx _gc_time_penalties;
 88   TruncatedSeq* _gc_time_history;
 89 
 90   // There may be many threads that contend to set this flag
 91   ShenandoahSharedFlag _metaspace_oom;
 92 
 93   static int compare_by_garbage(RegionData a, RegionData b);
 94 
 95   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set,
 96                                                      RegionData* data, size_t data_size,
 97                                                      size_t free) = 0;
 98 
 99   void adjust_penalty(intx step);
100 
101 public:
102   ShenandoahHeuristics(ShenandoahSpaceInfo* space_info);
103   virtual ~ShenandoahHeuristics();
104 
105   void record_metaspace_oom()     { _metaspace_oom.set(); }
106   void clear_metaspace_oom()      { _metaspace_oom.unset(); }
107   bool has_metaspace_oom() const  { return _metaspace_oom.is_set(); }
108 
109   virtual void record_cycle_start();
110 
111   virtual void record_cycle_end();
112 
113   virtual bool should_start_gc();
114 
115   virtual bool should_degenerate_cycle();
116 
117   virtual void record_success_concurrent();
118 
119   virtual void record_success_degenerated();
120 
121   virtual void record_success_full();
122 
123   virtual void record_allocation_failure_gc();
124 
125   virtual void record_requested_gc();
126 
127   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set);
128 
129   virtual bool can_unload_classes();
130   virtual bool should_unload_classes();
131 
132   virtual const char* name() = 0;
133   virtual bool is_diagnostic() = 0;
134   virtual bool is_experimental() = 0;
135   virtual void initialize();
136 
137   double time_since_last_gc() const;
138 };
139 
140 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHHEURISTICS_HPP