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/shenandoah/shenandoahHeap.hpp"
  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 
  60 class ShenandoahHeuristics : public CHeapObj<mtGC> {
  61   static const intx Concurrent_Adjust   =  1; // recover from penalties
  62   static const intx Degenerated_Penalty = 10; // how much to penalize average GC duration history on Degenerated GC
  63   static const intx Full_Penalty        = 20; // how much to penalize average GC duration history on Full GC
  64 
  65 protected:
  66   typedef struct {
  67     ShenandoahHeapRegion* _region;
  68     size_t _garbage;
  69     uint64_t _seqnum_last_alloc;
  70   } RegionData;
  71 
  72   bool _update_refs_early;
  73   bool _update_refs_adaptive;
  74 
  75   RegionData* _region_data;
  76   size_t _region_data_size;
  77 
  78   uint _degenerated_cycles_in_a_row;
  79   uint _successful_cycles_in_a_row;
  80 
  81   size_t _bytes_in_cset;
  82 
  83   double _cycle_start;
  84   double _last_cycle_end;
  85 
  86   size_t _gc_times_learned;
  87   size_t _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   static int compare_by_garbage_then_alloc_seq_ascending(RegionData a, RegionData b);
  95   static int compare_by_alloc_seq_ascending(RegionData a, RegionData b);
  96   static int compare_by_alloc_seq_descending(RegionData a, RegionData b);
  97 
  98   RegionData* get_region_data_cache(size_t num);
  99 
 100   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set,
 101                                                      RegionData* data, size_t data_size,
 102                                                      size_t free) = 0;
 103 
 104 public:
 105   ShenandoahHeuristics();
 106   virtual ~ShenandoahHeuristics();
 107 
 108   void record_gc_start();
 109 
 110   void record_gc_end();
 111 
 112   void record_metaspace_oom()     { _metaspace_oom.set(); }
 113   void clear_metaspace_oom()      { _metaspace_oom.unset(); }
 114   bool has_metaspace_oom() const  { return _metaspace_oom.is_set(); }
 115 
 116   virtual void record_cycle_start();
 117 
 118   virtual void record_cycle_end();
 119 
 120   virtual void record_phase_time(ShenandoahPhaseTimings::Phase phase, double secs);
 121 
 122   virtual bool should_start_normal_gc() const;
 123 
 124   virtual bool should_start_update_refs();
 125 
 126   virtual bool should_start_traversal_gc();
 127 
 128   virtual bool can_do_traversal_gc();
 129 
 130   virtual bool should_degenerate_cycle();
 131 
 132   virtual void record_success_concurrent();
 133 
 134   virtual void record_success_degenerated();
 135 
 136   virtual void record_success_full();
 137 
 138   virtual void record_allocation_failure_gc();
 139 
 140   virtual void record_requested_gc();
 141 
 142   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set);
 143 
 144   virtual bool can_process_references();
 145   virtual bool should_process_references();
 146 
 147   virtual bool can_unload_classes();
 148   virtual bool can_unload_classes_normal();
 149   virtual bool should_unload_classes();
 150 
 151   virtual const char* name() = 0;
 152   virtual bool is_diagnostic() = 0;
 153   virtual bool is_experimental() = 0;
 154   virtual void initialize();
 155 
 156   double time_since_last_gc() const;
 157 };
 158 
 159 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEURISTICS_HPP