8136678: Implement adaptive sizing algorithm for IHOP
authortschatzl
Tue, 24 Nov 2015 10:35:52 +0100
changeset 34299 3fdfdda0ac1f
parent 34298 f3c9dcc5af96
child 34300 6075c1e0e913
8136678: Implement adaptive sizing algorithm for IHOP Summary: Add adaptive sizing for IHOP that maximizes throughput (starts marking as late as possible). This functionality is enabled using a new -XX:+G1AdaptiveIHOP switch (default: false). Reviewed-by: mgerdin, jmasa, pliden
hotspot/src/share/vm/gc/g1/g1CollectorPolicy.cpp
hotspot/src/share/vm/gc/g1/g1IHOPControl.cpp
hotspot/src/share/vm/gc/g1/g1IHOPControl.hpp
hotspot/src/share/vm/gc/g1/g1RemSetSummary.cpp
hotspot/src/share/vm/gc/g1/g1_globals.hpp
hotspot/src/share/vm/utilities/globalDefinitions.hpp
--- a/hotspot/src/share/vm/gc/g1/g1CollectorPolicy.cpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/gc/g1/g1CollectorPolicy.cpp	Tue Nov 24 10:35:52 2015 +0100
@@ -1239,8 +1239,16 @@
 }
 
 G1IHOPControl* G1CollectorPolicy::create_ihop_control() const {
-  return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent,
-                                 G1CollectedHeap::heap()->max_capacity());
+  if (G1UseAdaptiveIHOP) {
+    return new G1AdaptiveIHOPControl(InitiatingHeapOccupancyPercent,
+                                     G1CollectedHeap::heap()->max_capacity(),
+                                     &_predictor,
+                                     G1ReservePercent,
+                                     G1HeapWastePercent);
+  } else {
+    return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent,
+                                   G1CollectedHeap::heap()->max_capacity());
+  }
 }
 
 void G1CollectorPolicy::update_ihop_prediction(double mutator_time_s,
--- a/hotspot/src/share/vm/gc/g1/g1IHOPControl.cpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/gc/g1/g1IHOPControl.cpp	Tue Nov 24 10:35:52 2015 +0100
@@ -26,6 +26,7 @@
 #include "gc/g1/g1CollectedHeap.inline.hpp"
 #include "gc/g1/g1ErgoVerbose.hpp"
 #include "gc/g1/g1IHOPControl.hpp"
+#include "gc/g1/g1Predictions.hpp"
 
 G1IHOPControl::G1IHOPControl(double initial_ihop_percent, size_t target_occupancy) :
   _initial_ihop_percent(initial_ihop_percent),
@@ -105,6 +106,181 @@
   assert(threshold == initial_ihop,
          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
 }
+#endif
+
+G1AdaptiveIHOPControl::G1AdaptiveIHOPControl(double ihop_percent,
+                                             size_t initial_target_occupancy,
+                                             G1Predictions const* predictor,
+                                             size_t heap_reserve_percent,
+                                             size_t heap_waste_percent) :
+  G1IHOPControl(ihop_percent, initial_target_occupancy),
+  _predictor(predictor),
+  _marking_times_s(10, 0.95),
+  _allocation_rate_s(10, 0.95),
+  _last_unrestrained_young_size(0),
+  _heap_reserve_percent(heap_reserve_percent),
+  _heap_waste_percent(heap_waste_percent)
+{
+}
+
+size_t G1AdaptiveIHOPControl::actual_target_threshold() const {
+  // The actual target threshold takes the heap reserve and the expected waste in
+  // free space  into account.
+  // _heap_reserve is that part of the total heap capacity that is reserved for
+  // eventual promotion failure.
+  // _heap_waste is the amount of space will never be reclaimed in any
+  // heap, so can not be used for allocation during marking and must always be
+  // considered.
+
+  double safe_total_heap_percentage = MIN2((double)(_heap_reserve_percent + _heap_waste_percent), 100.0);
+
+  return MIN2(
+    G1CollectedHeap::heap()->max_capacity() * (100.0 - safe_total_heap_percentage) / 100.0,
+    _target_occupancy * (100.0 - _heap_waste_percent) / 100.0
+    );
+}
+
+bool G1AdaptiveIHOPControl::have_enough_data_for_prediction() const {
+  return ((size_t)_marking_times_s.num() >= G1AdaptiveIHOPNumInitialSamples) &&
+         ((size_t)_allocation_rate_s.num() >= G1AdaptiveIHOPNumInitialSamples);
+}
+
+size_t G1AdaptiveIHOPControl::get_conc_mark_start_threshold() {
+  if (have_enough_data_for_prediction()) {
+    double pred_marking_time = _predictor->get_new_prediction(&_marking_times_s);
+    double pred_promotion_rate = _predictor->get_new_prediction(&_allocation_rate_s);
+
+    size_t predicted_needed_bytes_during_marking =
+      (pred_marking_time * pred_promotion_rate +
+      _last_unrestrained_young_size); // In reality we would need the maximum size of the young gen during marking. This is a conservative estimate.
+
+    size_t internal_threshold = actual_target_threshold();
+    size_t predicted_initiating_threshold = predicted_needed_bytes_during_marking < internal_threshold ?
+                                            internal_threshold - predicted_needed_bytes_during_marking :
+                                            0;
+    return predicted_initiating_threshold;
+  } else {
+    // Use the initial value.
+    return _initial_ihop_percent * _target_occupancy / 100.0;
+  }
+}
+
+void G1AdaptiveIHOPControl::update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size) {
+  assert(allocation_time_s >= 0.0, "Allocation time must be positive but is %.3f", allocation_time_s);
+  double allocation_rate = (double) allocated_bytes / allocation_time_s;
+  _allocation_rate_s.add(allocation_rate);
+
+  _last_allocation_bytes = allocated_bytes;
+  _last_unrestrained_young_size = additional_buffer_size;
+}
+
+void G1AdaptiveIHOPControl::update_marking_length(double marking_length_s) {
+   assert(marking_length_s >= 0.0, "Marking length must be larger than zero but is %.3f", marking_length_s);
+  _marking_times_s.add(marking_length_s);
+}
+
+void G1AdaptiveIHOPControl::print() {
+  ergo_verbose6(ErgoIHOP,
+                "basic information",
+                ergo_format_reason("value update")
+                ergo_format_byte_perc("threshold")
+                ergo_format_byte("target occupancy")
+                ergo_format_byte("current occupancy")
+                ergo_format_double("recent old gen allocation rate")
+                ergo_format_double("recent marking phase length"),
+                get_conc_mark_start_threshold(),
+                percent_of(get_conc_mark_start_threshold(), _target_occupancy),
+                _target_occupancy,
+                G1CollectedHeap::heap()->used(),
+                _allocation_rate_s.last(),
+                _marking_times_s.last()
+                );
+  size_t actual_target = actual_target_threshold();
+  ergo_verbose6(ErgoIHOP,
+                "adaptive IHOP information",
+                ergo_format_reason("value update")
+                ergo_format_byte_perc("threshold")
+                ergo_format_byte("internal target occupancy")
+                ergo_format_double("predicted old gen allocation rate")
+                ergo_format_double("predicted marking phase length")
+                ergo_format_str("prediction active"),
+                get_conc_mark_start_threshold(),
+                percent_of(get_conc_mark_start_threshold(), actual_target),
+                actual_target,
+                _predictor->get_new_prediction(&_allocation_rate_s),
+                _predictor->get_new_prediction(&_marking_times_s),
+                have_enough_data_for_prediction() ? "true" : "false"
+                );
+}
+
+#ifndef PRODUCT
+void G1AdaptiveIHOPControl::test() {
+  size_t const initial_threshold = 45;
+  size_t const young_size = 10;
+  size_t const target_size = 100;
+
+  // The final IHOP value is always
+  // target_size - (young_size + alloc_amount/alloc_time * marking_time)
+
+  G1Predictions pred(0.95);
+  G1AdaptiveIHOPControl ctrl(initial_threshold, target_size, &pred, 0, 0);
+
+  // First "load".
+  size_t const alloc_time1 = 2;
+  size_t const alloc_amount1 = 10;
+  size_t const marking_time1 = 2;
+  size_t const settled_ihop1 = target_size - (young_size + alloc_amount1/alloc_time1 * marking_time1);
+
+  size_t threshold;
+  threshold = ctrl.get_conc_mark_start_threshold();
+  assert(threshold == initial_threshold,
+         "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_threshold, threshold);
+  for (size_t i = 0; i < G1AdaptiveIHOPNumInitialSamples - 1; i++) {
+    ctrl.update_allocation_info(alloc_time1, alloc_amount1, young_size);
+    ctrl.update_marking_length(marking_time1);
+    // Not enough data yet.
+    threshold = ctrl.get_conc_mark_start_threshold();
+    assert(threshold == initial_threshold,
+           "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_threshold, threshold);
+  }
+
+  test_update(&ctrl, alloc_time1, alloc_amount1, young_size, marking_time1);
+
+  threshold = ctrl.get_conc_mark_start_threshold();
+  assert(threshold == settled_ihop1,
+         "Expected IHOP threshold to settle at " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop1, threshold);
+
+  // Second "load". A bit higher allocation rate.
+  size_t const alloc_time2 = 2;
+  size_t const alloc_amount2 = 30;
+  size_t const marking_time2 = 2;
+  size_t const settled_ihop2 = target_size - (young_size + alloc_amount2/alloc_time2 * marking_time2);
+
+  test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
+
+  threshold = ctrl.get_conc_mark_start_threshold();
+  assert(threshold < settled_ihop1,
+         "Expected IHOP threshold to settle at a value lower than " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop1, threshold);
+
+  // Third "load". Very high (impossible) allocation rate.
+  size_t const alloc_time3 = 1;
+  size_t const alloc_amount3 = 50;
+  size_t const marking_time3 = 2;
+  size_t const settled_ihop3 = 0;
+
+  test_update(&ctrl, alloc_time3, alloc_amount3, young_size, marking_time3);
+  threshold = ctrl.get_conc_mark_start_threshold();
+
+  assert(threshold == settled_ihop3,
+         "Expected IHOP threshold to settle at " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop3, threshold);
+
+  // And back to some arbitrary value.
+  test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
+
+  threshold = ctrl.get_conc_mark_start_threshold();
+  assert(threshold > settled_ihop3,
+         "Expected IHOP threshold to settle at value larger than " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop3, threshold);
+}
 
 void IHOP_test() {
   G1StaticIHOPControl::test();
--- a/hotspot/src/share/vm/gc/g1/g1IHOPControl.hpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/gc/g1/g1IHOPControl.hpp	Tue Nov 24 10:35:52 2015 +0100
@@ -26,6 +26,9 @@
 #define SHARE_VM_GC_G1_G1IHOPCONTROL_HPP
 
 #include "memory/allocation.hpp"
+#include "utilities/numberSeq.hpp"
+
+class G1Predictions;
 
 // Base class for algorithms that calculate the heap occupancy at which
 // concurrent marking should start. This heap usage threshold should be relative
@@ -95,4 +98,53 @@
 #endif
 };
 
+// This algorithm tries to return a concurrent mark starting occupancy value that
+// makes sure that during marking the given target occupancy is never exceeded,
+// based on predictions of current allocation rate and time periods between
+// initial mark and the first mixed gc.
+class G1AdaptiveIHOPControl : public G1IHOPControl {
+  size_t _heap_reserve_percent; // Percentage of maximum heap capacity we should avoid to touch
+  size_t _heap_waste_percent;   // Percentage of free heap that should be considered as waste.
+
+  const G1Predictions * _predictor;
+
+  TruncatedSeq _marking_times_s;
+  TruncatedSeq _allocation_rate_s;
+
+  size_t _last_allocation_bytes; // Most recent mutator allocation since last GC.
+  // The most recent unrestrained size of the young gen. This is used as an additional
+  // factor in the calculation of the threshold, as the threshold is based on
+  // non-young gen occupancy at the end of GC. For the IHOP threshold, we need to
+  // consider the young gen size during that time too.
+  // Since we cannot know what young gen sizes are used in the future, we will just
+  // use the current one. We expect that this one will be one with a fairly large size,
+  // as there is no marking or mixed gc that could impact its size too much.
+  size_t _last_unrestrained_young_size;
+
+  bool have_enough_data_for_prediction() const;
+
+  // The "actual" target threshold the algorithm wants to keep during and at the
+  // end of marking. This is typically lower than the requested threshold, as the
+  // algorithm needs to consider restrictions by the environment.
+  size_t actual_target_threshold() const;
+ protected:
+  virtual double last_marking_length_s() const { return _marking_times_s.last(); }
+ public:
+  G1AdaptiveIHOPControl(double ihop_percent,
+                        size_t initial_target_occupancy,
+                        G1Predictions const* predictor,
+                        size_t heap_reserve_percent, // The percentage of total heap capacity that should not be tapped into.
+                        size_t heap_waste_percent);  // The percentage of the free space in the heap that we think is not usable for allocation.
+
+  virtual size_t get_conc_mark_start_threshold();
+
+  virtual void update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size);
+  virtual void update_marking_length(double marking_length_s);
+
+  virtual void print();
+#ifndef PRODUCT
+  static void test();
+#endif
+};
+
 #endif // SHARE_VM_GC_G1_G1IHOPCONTROL_HPP
--- a/hotspot/src/share/vm/gc/g1/g1RemSetSummary.cpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/gc/g1/g1RemSetSummary.cpp	Tue Nov 24 10:35:52 2015 +0100
@@ -125,14 +125,6 @@
   _sampling_thread_vtime = other->sampling_thread_vtime() - _sampling_thread_vtime;
 }
 
-static double percent_of(size_t numerator, size_t denominator) {
-  if (denominator != 0) {
-    return (double)numerator / denominator * 100.0f;
-  } else {
-    return 0.0f;
-  }
-}
-
 static size_t round_to_K(size_t value) {
   return value / K;
 }
--- a/hotspot/src/share/vm/gc/g1/g1_globals.hpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/gc/g1/g1_globals.hpp	Tue Nov 24 10:35:52 2015 +0100
@@ -33,6 +33,16 @@
 
 #define G1_FLAGS(develop, develop_pd, product, product_pd, diagnostic, experimental, notproduct, manageable, product_rw, range, constraint) \
                                                                             \
+  product(bool, G1UseAdaptiveIHOP, false,                                   \
+          "Adaptively adjust InitiatingHeapOccupancyPercent from the "      \
+          "initial value.")                                                 \
+                                                                            \
+  experimental(size_t, G1AdaptiveIHOPNumInitialSamples, 3,                  \
+          "How many completed time periods from initial mark to first "     \
+          "mixed gc are required to use the input values for prediction "   \
+          "of the optimal occupancy to start marking.")                     \
+          range(1, max_intx)                                                \
+                                                                            \
   product(uintx, G1ConfidencePercent, 50,                                   \
           "Confidence level for MMU/pause predictions")                     \
           range(0, 100)                                                     \
--- a/hotspot/src/share/vm/utilities/globalDefinitions.hpp	Tue Nov 24 10:22:36 2015 +0100
+++ b/hotspot/src/share/vm/utilities/globalDefinitions.hpp	Tue Nov 24 10:35:52 2015 +0100
@@ -565,6 +565,13 @@
   return fabs(value);
 }
 
+// Returns numerator/denominator as percentage value from 0 to 100. If denominator
+// is zero, return 0.0.
+template<typename T>
+inline double percent_of(T numerator, T denominator) {
+  return denominator != 0 ? (double)numerator / denominator * 100.0 : 0.0;
+}
+
 //----------------------------------------------------------------------------------------------------
 // Special casts
 // Cast floats into same-size integers and vice-versa w/o changing bit-pattern