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/shenandoahPhaseTimings.hpp"
 30 #include "utilities/numberSeq.hpp"
 31 
 32 class ShenandoahAllocationRate : public CHeapObj<mtGC> {
 33  public:
 34   explicit ShenandoahAllocationRate();
 35   void allocation_counter_reset();
 36 
 37   double sample(size_t allocated);
 38 
 39   double instantaneous_rate(size_t allocated) const;
 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 class ShenandoahAdaptiveHeuristics : public ShenandoahHeuristics {
 55 public:
 56   ShenandoahAdaptiveHeuristics(ShenandoahGeneration* generation);
 57 
 58   virtual ~ShenandoahAdaptiveHeuristics();
 59 
 60   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 61                                                      RegionData* data, size_t size,
 62                                                      size_t actual_free);
 63 
 64   void record_cycle_start();
 65   void record_success_concurrent();
 66   void record_success_degenerated();
 67   void record_success_full();
 68 
 69   virtual bool should_start_gc();
 70 
 71   virtual const char* name()     { return "Adaptive"; }
 72   virtual bool is_diagnostic()   { return false; }
 73   virtual bool is_experimental() { return false; }
 74 
 75  private:
 76   // These are used to adjust the margin of error and the spike threshold
 77   // in response to GC cycle outcomes. These values are shared, but the
 78   // margin of error and spike threshold trend in opposite directions.
 79   const static double FULL_PENALTY_SD;
 80   const static double DEGENERATE_PENALTY_SD;
 81 
 82   const static double MINIMUM_CONFIDENCE;
 83   const static double MAXIMUM_CONFIDENCE;
 84 
 85   const static double LOWEST_EXPECTED_AVAILABLE_AT_END;
 86   const static double HIGHEST_EXPECTED_AVAILABLE_AT_END;
 87 
 88   friend class ShenandoahAllocationRate;
 89 
 90   // Used to record the last trigger that signaled to start a GC.
 91   // This itself is used to decide whether or not to adjust the margin of
 92   // error for the average cycle time and allocation rate or the allocation
 93   // spike detection threshold.
 94   enum Trigger {
 95     SPIKE, RATE, OTHER
 96   };
 97 
 98   void adjust_last_trigger_parameters(double amount);
 99   void adjust_margin_of_error(double amount);
100   void adjust_spike_threshold(double amount);
101 
102   ShenandoahAllocationRate _allocation_rate;
103 
104   // The margin of error expressed in standard deviations to add to our
105   // average cycle time and allocation rate. As this value increases we
106   // tend to over estimate the rate at which mutators will deplete the
107   // heap. In other words, erring on the side of caution will trigger more
108   // concurrent GCs.
109   double _margin_of_error_sd;
110 
111   // The allocation spike threshold is expressed in standard deviations.
112   // If the standard deviation of the most recent sample of the allocation
113   // rate exceeds this threshold, a GC cycle is started. As this value
114   // decreases the sensitivity to allocation spikes increases. In other
115   // words, lowering the spike threshold will tend to increase the number
116   // of concurrent GCs.
117   double _spike_threshold_sd;
118 
119   // Remember which trigger is responsible for the last GC cycle. When the
120   // outcome of the cycle is evaluated we will adjust the parameters for the
121   // corresponding triggers. Note that successful outcomes will raise
122   // the spike threshold and lower the margin of error.
123   Trigger _last_trigger;
124 
125   // Keep track of the available memory at the end of a GC cycle. This
126   // establishes what is 'normal' for the application and is used as a
127   // source of feedback to adjust trigger parameters.
128   TruncatedSeq _available;
129 };
130 
131 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP