1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   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 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEURISTICS_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEURISTICS_HPP
  26 
  27 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahPhaseTimings.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahSharedVariables.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "runtime/globals_extension.hpp"
  32 #include "runtime/java.hpp"
  33 
  34 #define SHENANDOAH_ERGO_DISABLE_FLAG(name)                                  \
  35   do {                                                                      \
  36     if (FLAG_IS_DEFAULT(name) && (name)) {                                  \
  37       log_info(gc)("Heuristics ergonomically sets -XX:-" #name);            \
  38       FLAG_SET_DEFAULT(name, false);                                        \
  39     }                                                                       \
  40   } while (0)
  41 
  42 #define SHENANDOAH_ERGO_ENABLE_FLAG(name)                                   \
  43   do {                                                                      \
  44     if (FLAG_IS_DEFAULT(name) && !(name)) {                                 \
  45       log_info(gc)("Heuristics ergonomically sets -XX:+" #name);            \
  46       FLAG_SET_DEFAULT(name, true);                                         \
  47     }                                                                       \
  48   } while (0)
  49 
  50 #define SHENANDOAH_ERGO_OVERRIDE_DEFAULT(name, value)                       \
  51   do {                                                                      \
  52     if (FLAG_IS_DEFAULT(name)) {                                            \
  53       log_info(gc)("Heuristics ergonomically sets -XX:" #name "=" #value);  \
  54       FLAG_SET_DEFAULT(name, value);                                        \
  55     }                                                                       \
  56   } while (0)
  57 
  58 #define SHENANDOAH_CHECK_FLAG_SET(name)                                     \
  59   do {                                                                      \
  60     if (!name) {                                                            \
  61       err_msg message("Heuristics needs -XX:+" #name " to work correctly"); \
  62       vm_exit_during_initialization("Error", message);                      \
  63     }                                                                       \
  64   } while (0)
  65 
  66 class ShenandoahCollectionSet;
  67 class ShenandoahHeapRegion;
  68 
  69 class ShenandoahHeuristics : public CHeapObj<mtGC> {
  70   static const intx Concurrent_Adjust   =  1; // recover from penalties
  71   static const intx Degenerated_Penalty = 10; // how much to penalize average GC duration history on Degenerated GC
  72   static const intx Full_Penalty        = 20; // how much to penalize average GC duration history on Full GC
  73 
  74 protected:
  75   typedef struct {
  76     ShenandoahHeapRegion* _region;
  77     size_t _garbage;
  78     uint64_t _seqnum_last_alloc;
  79   } RegionData;
  80 
  81   bool _update_refs_early;
  82   bool _update_refs_adaptive;
  83 
  84   RegionData* _region_data;
  85   size_t _region_data_size;
  86 
  87   uint _degenerated_cycles_in_a_row;
  88   uint _successful_cycles_in_a_row;
  89 
  90   size_t _bytes_in_cset;
  91 
  92   double _cycle_start;
  93   double _last_cycle_end;
  94 
  95   size_t _gc_times_learned;
  96   size_t _gc_time_penalties;
  97   TruncatedSeq* _gc_time_history;
  98 
  99   // There may be many threads that contend to set this flag
 100   ShenandoahSharedFlag _metaspace_oom;
 101 
 102   static int compare_by_garbage(RegionData a, RegionData b);
 103   static int compare_by_garbage_then_alloc_seq_ascending(RegionData a, RegionData b);
 104   static int compare_by_alloc_seq_ascending(RegionData a, RegionData b);
 105   static int compare_by_alloc_seq_descending(RegionData a, RegionData b);
 106 
 107   RegionData* get_region_data_cache(size_t num);
 108 
 109   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set,
 110                                                      RegionData* data, size_t data_size,
 111                                                      size_t free) = 0;
 112 
 113 public:
 114   ShenandoahHeuristics();
 115   virtual ~ShenandoahHeuristics();
 116 
 117   void record_gc_start();
 118 
 119   void record_gc_end();
 120 
 121   void record_metaspace_oom()     { _metaspace_oom.set(); }
 122   void clear_metaspace_oom()      { _metaspace_oom.unset(); }
 123   bool has_metaspace_oom() const  { return _metaspace_oom.is_set(); }
 124 
 125   virtual void record_cycle_start();
 126 
 127   virtual void record_cycle_end();
 128 
 129   virtual void record_phase_time(ShenandoahPhaseTimings::Phase phase, double secs);
 130 
 131   virtual bool should_start_gc() const;
 132 
 133   virtual bool should_start_update_refs();
 134 
 135   virtual bool should_degenerate_cycle();
 136 
 137   virtual void record_success_concurrent();
 138 
 139   virtual void record_success_degenerated();
 140 
 141   virtual void record_success_full();
 142 
 143   virtual void record_allocation_failure_gc();
 144 
 145   virtual void record_requested_gc();
 146 
 147   virtual void start_choose_collection_set() {
 148   }
 149   virtual void end_choose_collection_set() {
 150   }
 151 
 152   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set);
 153 
 154   virtual bool can_process_references();
 155   virtual bool should_process_references();
 156 
 157   virtual bool can_unload_classes();
 158   virtual bool can_unload_classes_normal();
 159   virtual bool should_unload_classes();
 160 
 161   virtual const char* name() = 0;
 162   virtual bool is_diagnostic() = 0;
 163   virtual bool is_experimental() = 0;
 164   virtual void initialize();
 165 
 166   double time_since_last_gc() const;
 167 };
 168 
 169 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEURISTICS_HPP