1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   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 "memory/allocation.hpp"
  30 #include "runtime/globals_extension.hpp"
  31 
  32 #define SHENANDOAH_ERGO_DISABLE_FLAG(name)                                  \
  33   do {                                                                      \
  34     if (FLAG_IS_DEFAULT(name) && (name)) {                                  \
  35       log_info(gc)("Heuristics ergonomically sets -XX:-" #name);            \
  36       FLAG_SET_DEFAULT(name, false);                                        \
  37     }                                                                       \
  38   } while (0)
  39 
  40 #define SHENANDOAH_ERGO_ENABLE_FLAG(name)                                   \
  41   do {                                                                      \
  42     if (FLAG_IS_DEFAULT(name) && !(name)) {                                 \
  43       log_info(gc)("Heuristics ergonomically sets -XX:+" #name);            \
  44       FLAG_SET_DEFAULT(name, true);                                         \
  45     }                                                                       \
  46   } while (0)
  47 
  48 #define SHENANDOAH_ERGO_OVERRIDE_DEFAULT(name, value)                       \
  49   do {                                                                      \
  50     if (FLAG_IS_DEFAULT(name)) {                                            \
  51       log_info(gc)("Heuristics ergonomically sets -XX:" #name "=" #value);  \
  52       FLAG_SET_DEFAULT(name, value);                                        \
  53     }                                                                       \
  54   } while (0)
  55 
  56 
  57 class ShenandoahCollectionSet;
  58 class ShenandoahHeapRegion;
  59 
  60 class ShenandoahHeuristics : public CHeapObj<mtGC> {
  61 protected:
  62   typedef struct {
  63     ShenandoahHeapRegion* _region;
  64     size_t _garbage;
  65     uint64_t _seqnum_last_alloc;
  66   } RegionData;
  67 
  68   typedef struct {
  69     ShenandoahHeapRegion* _region;
  70     size_t _connections;
  71   } RegionConnections;
  72 
  73   bool _update_refs_early;
  74   bool _update_refs_adaptive;
  75 
  76   RegionData* _region_data;
  77   size_t _region_data_size;
  78 
  79   RegionConnections* _region_connects;
  80   size_t _region_connects_size;
  81 
  82   uint _degenerated_cycles_in_a_row;
  83   uint _successful_cycles_in_a_row;
  84 
  85   size_t _bytes_in_cset;
  86 
  87   double _last_cycle_end;
  88 
  89   static int compare_by_garbage(RegionData a, RegionData b);
  90   static int compare_by_alloc_seq_ascending(RegionData a, RegionData b);
  91   static int compare_by_alloc_seq_descending(RegionData a, RegionData b);
  92   static int compare_by_connects(RegionConnections a, RegionConnections b);
  93 
  94   RegionData* get_region_data_cache(size_t num);
  95 
  96   RegionConnections* get_region_connects_cache(size_t num);
  97 
  98   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set,
  99                                                      RegionData* data, size_t data_size,
 100                                                      size_t free) = 0;
 101 
 102 public:
 103 
 104   ShenandoahHeuristics();
 105   virtual ~ShenandoahHeuristics();
 106 
 107   void record_gc_start();
 108 
 109   void record_gc_end();
 110 
 111   virtual void record_cycle_start();
 112 
 113   virtual void record_cycle_end();
 114 
 115   virtual void record_phase_time(ShenandoahPhaseTimings::Phase phase, double secs);
 116 
 117   virtual void print_thresholds();
 118 
 119   virtual bool should_start_normal_gc() const = 0;
 120 
 121   virtual bool should_start_update_refs();
 122 
 123   virtual bool update_refs() const;
 124 
 125   virtual bool should_degenerate_cycle();
 126 
 127   virtual void record_success_concurrent();
 128 
 129   virtual void record_success_degenerated();
 130 
 131   virtual void record_success_full();
 132 
 133   virtual void record_allocation_failure_gc();
 134 
 135   virtual void record_explicit_gc();
 136 
 137   virtual void record_peak_occupancy();
 138 
 139   virtual void start_choose_collection_set() {
 140   }
 141   virtual void end_choose_collection_set() {
 142   }
 143 
 144   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set);
 145 
 146   virtual bool should_process_references();
 147 
 148   virtual bool should_unload_classes();
 149 
 150   virtual const char* name() = 0;
 151   virtual bool is_diagnostic() = 0;
 152   virtual bool is_experimental() = 0;
 153   virtual void initialize();
 154 
 155 };
 156 
 157 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEURISTICS_HPP