1 /*
  2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP
 27 #define SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP
 28 
 29 #include "runtime/globals_extension.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 32 #include "gc/shenandoah/heuristics/shenandoahSpaceInfo.hpp"
 33 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 34 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 35 #include "utilities/numberSeq.hpp"
 36 
 37 class ShenandoahAllocationRate : public CHeapObj<mtGC> {
 38  public:
 39   explicit ShenandoahAllocationRate();
 40   void allocation_counter_reset();
 41 
 42   double sample(size_t allocated);
 43 
 44   double upper_bound(double sds) const;
 45   bool is_spiking(double rate, double threshold) const;
 46  private:
 47 
 48   double instantaneous_rate(double time, size_t allocated) const;
 49 
 50   double _last_sample_time;
 51   size_t _last_sample_value;
 52   double _interval_sec;
 53   TruncatedSeq _rate;
 54   TruncatedSeq _rate_avg;
 55 };
 56 
 57 /*
 58  * The adaptive heuristic tracks the allocation behavior and average cycle
 59  * time of the application. It attempts to start a cycle with enough time
 60  * to complete before the available memory is exhausted. It errors on the
 61  * side of starting cycles early to avoid allocation failures (degenerated
 62  * cycles).
 63  *
 64  * This heuristic limits the number of regions for evacuation such that the
 65  * evacuation reserve is respected. This helps it avoid allocation failures
 66  * during evacuation. It preferentially selects regions with the most garbage.
 67  */
 68 class ShenandoahAdaptiveHeuristics : public ShenandoahHeuristics {
 69 public:
 70   ShenandoahAdaptiveHeuristics(ShenandoahSpaceInfo* space_info);
 71 
 72   virtual ~ShenandoahAdaptiveHeuristics();
 73 
 74   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 75                                                      RegionData* data, size_t size,
 76                                                      size_t actual_free);
 77 
 78   void record_cycle_start();
 79   void record_success_concurrent(bool abbreviated);
 80   void record_success_degenerated();
 81   void record_success_full();
 82 
 83   virtual bool should_start_gc();
 84 
 85   virtual const char* name()     { return "Adaptive"; }
 86   virtual bool is_diagnostic()   { return false; }
 87   virtual bool is_experimental() { return false; }
 88 
 89  private:
 90   // These are used to adjust the margin of error and the spike threshold
 91   // in response to GC cycle outcomes. These values are shared, but the
 92   // margin of error and spike threshold trend in opposite directions.
 93   const static double FULL_PENALTY_SD;
 94   const static double DEGENERATE_PENALTY_SD;
 95 
 96   const static double MINIMUM_CONFIDENCE;
 97   const static double MAXIMUM_CONFIDENCE;
 98 
 99   const static double LOWEST_EXPECTED_AVAILABLE_AT_END;
100   const static double HIGHEST_EXPECTED_AVAILABLE_AT_END;
101 
102   friend class ShenandoahAllocationRate;
103 
104   // Used to record the last trigger that signaled to start a GC.
105   // This itself is used to decide whether or not to adjust the margin of
106   // error for the average cycle time and allocation rate or the allocation
107   // spike detection threshold.
108   enum Trigger {
109     SPIKE, RATE, OTHER
110   };
111 
112   void adjust_last_trigger_parameters(double amount);
113   void adjust_margin_of_error(double amount);
114   void adjust_spike_threshold(double amount);
115 
116 protected:
117   ShenandoahAllocationRate _allocation_rate;
118 
119   // The margin of error expressed in standard deviations to add to our
120   // average cycle time and allocation rate. As this value increases we
121   // tend to overestimate the rate at which mutators will deplete the
122   // heap. In other words, erring on the side of caution will trigger more
123   // concurrent GCs.
124   double _margin_of_error_sd;
125 
126   // The allocation spike threshold is expressed in standard deviations.
127   // If the standard deviation of the most recent sample of the allocation
128   // rate exceeds this threshold, a GC cycle is started. As this value
129   // decreases the sensitivity to allocation spikes increases. In other
130   // words, lowering the spike threshold will tend to increase the number
131   // of concurrent GCs.
132   double _spike_threshold_sd;
133 
134   // Remember which trigger is responsible for the last GC cycle. When the
135   // outcome of the cycle is evaluated we will adjust the parameters for the
136   // corresponding triggers. Note that successful outcomes will raise
137   // the spike threshold and lower the margin of error.
138   Trigger _last_trigger;
139 
140   // Keep track of the available memory at the end of a GC cycle. This
141   // establishes what is 'normal' for the application and is used as a
142   // source of feedback to adjust trigger parameters.
143   TruncatedSeq _available;
144 
145   size_t min_free_threshold();
146 };
147 
148 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHADAPTIVEHEURISTICS_HPP