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_SHENANDOAHADAPTIVEHEURISTICS_HPP
 26 #define SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP
 27 
 28 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 29 #include "gc/shenandoah/heuristics/shenandoahSpaceInfo.hpp"
 30 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 31 #include "utilities/numberSeq.hpp"
 32 
 33 class ShenandoahAllocationRate : public CHeapObj<mtGC> {
 34  public:
 35   explicit ShenandoahAllocationRate();
 36   void allocation_counter_reset();
 37 
 38   double sample(size_t allocated);
 39 
 40   double upper_bound(double sds) const;
 41   bool is_spiking(double rate, double threshold) const;
 42 
 43  private:
 44 
 45   double instantaneous_rate(double time, size_t allocated) const;
 46 
 47   double _last_sample_time;
 48   size_t _last_sample_value;
 49   double _interval_sec;
 50   TruncatedSeq _rate;
 51   TruncatedSeq _rate_avg;
 52 };
 53 
 54 /*
 55  * The adaptive heuristic tracks the allocation behavior and average cycle
 56  * time of the application. It attempts to start a cycle with enough time
 57  * to complete before the available memory is exhausted. It errors on the
 58  * side of starting cycles early to avoid allocation failures (degenerated
 59  * cycles).
 60  *
 61  * This heuristic limits the number of regions for evacuation such that the
 62  * evacuation reserve is respected. This helps it avoid allocation failures
 63  * during evacuation. It preferentially selects regions with the most garbage.
 64  */
 65 class ShenandoahAdaptiveHeuristics : public ShenandoahHeuristics {
 66 public:
 67   ShenandoahAdaptiveHeuristics(ShenandoahSpaceInfo* space_info);
 68 
 69   virtual ~ShenandoahAdaptiveHeuristics();
 70 
 71   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 72                                                      RegionData* data, size_t size,
 73                                                      size_t actual_free);
 74 
 75   void record_cycle_start();
 76   void record_success_concurrent();
 77   void record_success_degenerated();
 78   void record_success_full();
 79 
 80   virtual bool should_start_gc();
 81 
 82   virtual const char* name()     { return "Adaptive"; }
 83   virtual bool is_diagnostic()   { return false; }
 84   virtual bool is_experimental() { return false; }
 85 
 86  private:
 87   // These are used to adjust the margin of error and the spike threshold
 88   // in response to GC cycle outcomes. These values are shared, but the
 89   // margin of error and spike threshold trend in opposite directions.
 90   const static double FULL_PENALTY_SD;
 91   const static double DEGENERATE_PENALTY_SD;
 92 
 93   const static double MINIMUM_CONFIDENCE;
 94   const static double MAXIMUM_CONFIDENCE;
 95 
 96   const static double LOWEST_EXPECTED_AVAILABLE_AT_END;
 97   const static double HIGHEST_EXPECTED_AVAILABLE_AT_END;
 98 
 99   friend class ShenandoahAllocationRate;
100 
101   // Used to record the last trigger that signaled to start a GC.
102   // This itself is used to decide whether or not to adjust the margin of
103   // error for the average cycle time and allocation rate or the allocation
104   // spike detection threshold.
105   enum Trigger {
106     SPIKE, RATE, OTHER
107   };
108 
109   void adjust_last_trigger_parameters(double amount);
110   void adjust_margin_of_error(double amount);
111   void adjust_spike_threshold(double amount);
112 
113 protected:
114   ShenandoahAllocationRate _allocation_rate;
115 
116   // The margin of error expressed in standard deviations to add to our
117   // average cycle time and allocation rate. As this value increases we
118   // tend to overestimate the rate at which mutators will deplete the
119   // heap. In other words, erring on the side of caution will trigger more
120   // concurrent GCs.
121   double _margin_of_error_sd;
122 
123   // The allocation spike threshold is expressed in standard deviations.
124   // If the standard deviation of the most recent sample of the allocation
125   // rate exceeds this threshold, a GC cycle is started. As this value
126   // decreases the sensitivity to allocation spikes increases. In other
127   // words, lowering the spike threshold will tend to increase the number
128   // of concurrent GCs.
129   double _spike_threshold_sd;
130 
131   // Remember which trigger is responsible for the last GC cycle. When the
132   // outcome of the cycle is evaluated we will adjust the parameters for the
133   // corresponding triggers. Note that successful outcomes will raise
134   // the spike threshold and lower the margin of error.
135   Trigger _last_trigger;
136 
137   // Keep track of the available memory at the end of a GC cycle. This
138   // establishes what is 'normal' for the application and is used as a
139   // source of feedback to adjust trigger parameters.
140   TruncatedSeq _available;
141 };
142 
143 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP