1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. 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 #include "precompiled.hpp"
  26 #include "gc/g1/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1ErgoVerbose.hpp"
  28 #include "gc/g1/g1IHOPControl.hpp"
  29 #include "gc/g1/g1Predictions.hpp"
  30 #include "gc/shared/gcTrace.hpp"
  31 
  32 G1IHOPControl::G1IHOPControl(double initial_ihop_percent, size_t target_occupancy) :
  33   _initial_ihop_percent(initial_ihop_percent),
  34   _target_occupancy(target_occupancy),
  35   _last_allocated_bytes(0),
  36   _last_allocation_time_s(0.0)
  37 {
  38   assert(_initial_ihop_percent >= 0.0 && _initial_ihop_percent <= 100.0, "Initial IHOP value must be between 0 and 100 but is %.3f", initial_ihop_percent);
  39 }
  40 
  41 void G1IHOPControl::update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size) {
  42   assert(allocation_time_s >= 0.0, "Allocation time must be positive but is %.3f", allocation_time_s);
  43 
  44   _last_allocation_time_s = allocation_time_s;
  45   _last_allocated_bytes = allocated_bytes;
  46 }
  47 
  48 void G1IHOPControl::print() {
  49   size_t cur_conc_mark_start_threshold = get_conc_mark_start_threshold();
  50   ergo_verbose6(ErgoIHOP,
  51                 "basic information",
  52                 ergo_format_reason("value update")
  53                 ergo_format_byte_perc("threshold")
  54                 ergo_format_byte("target occupancy")
  55                 ergo_format_byte("current occupancy")
  56                 ergo_format_double("recent old gen allocation rate")
  57                 ergo_format_double("recent marking phase length"),
  58                 cur_conc_mark_start_threshold,
  59                 cur_conc_mark_start_threshold * 100.0 / _target_occupancy,
  60                 _target_occupancy,
  61                 G1CollectedHeap::heap()->used(),
  62                 _last_allocation_time_s > 0.0 ? _last_allocated_bytes / _last_allocation_time_s : 0.0,
  63                 last_marking_length_s());
  64 }
  65 
  66 void G1IHOPControl::send_trace_event(G1NewTracer* tracer) {
  67   tracer->report_basic_ihop_statistics(get_conc_mark_start_threshold(),
  68                                        _target_occupancy,
  69                                        G1CollectedHeap::heap()->used(),
  70                                        _last_allocated_bytes,
  71                                        _last_allocation_time_s,
  72                                        last_marking_length_s());
  73 }
  74 
  75 G1StaticIHOPControl::G1StaticIHOPControl(double ihop_percent, size_t target_occupancy) :
  76   G1IHOPControl(ihop_percent, target_occupancy),
  77   _last_marking_length_s(0.0) {
  78   assert(_target_occupancy > 0, "Target occupancy must be larger than zero.");
  79 }
  80 
  81 #ifndef PRODUCT
  82 static void test_update(G1IHOPControl* ctrl, double alloc_time, size_t alloc_amount, size_t young_size, double mark_time) {
  83   for (int i = 0; i < 100; i++) {
  84     ctrl->update_allocation_info(alloc_time, alloc_amount, young_size);
  85     ctrl->update_marking_length(mark_time);
  86   }
  87 }
  88 
  89 void G1StaticIHOPControl::test() {
  90   size_t const initial_ihop = 45;
  91 
  92   G1StaticIHOPControl ctrl(initial_ihop, 100);
  93 
  94   size_t threshold = ctrl.get_conc_mark_start_threshold();
  95   assert(threshold == initial_ihop,
  96          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
  97 
  98   ctrl.update_allocation_info(100.0, 100, 100);
  99   threshold = ctrl.get_conc_mark_start_threshold();
 100   assert(threshold == initial_ihop,
 101          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
 102 
 103   ctrl.update_marking_length(1000.0);
 104   threshold = ctrl.get_conc_mark_start_threshold();
 105   assert(threshold == initial_ihop,
 106          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
 107 
 108   // Whatever we pass, the IHOP value must stay the same.
 109   test_update(&ctrl, 2, 10, 10, 3);
 110   threshold = ctrl.get_conc_mark_start_threshold();
 111   assert(threshold == initial_ihop,
 112          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
 113 
 114   test_update(&ctrl, 12, 10, 10, 3);
 115   threshold = ctrl.get_conc_mark_start_threshold();
 116   assert(threshold == initial_ihop,
 117          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_ihop, threshold);
 118 }
 119 #endif
 120 
 121 G1AdaptiveIHOPControl::G1AdaptiveIHOPControl(double ihop_percent,
 122                                              size_t initial_target_occupancy,
 123                                              G1Predictions const* predictor,
 124                                              size_t heap_reserve_percent,
 125                                              size_t heap_waste_percent) :
 126   G1IHOPControl(ihop_percent, initial_target_occupancy),
 127   _predictor(predictor),
 128   _marking_times_s(10, 0.95),
 129   _allocation_rate_s(10, 0.95),
 130   _last_unrestrained_young_size(0),
 131   _heap_reserve_percent(heap_reserve_percent),
 132   _heap_waste_percent(heap_waste_percent)
 133 {
 134 }
 135 
 136 size_t G1AdaptiveIHOPControl::actual_target_threshold() const {
 137   // The actual target threshold takes the heap reserve and the expected waste in
 138   // free space  into account.
 139   // _heap_reserve is that part of the total heap capacity that is reserved for
 140   // eventual promotion failure.
 141   // _heap_waste is the amount of space will never be reclaimed in any
 142   // heap, so can not be used for allocation during marking and must always be
 143   // considered.
 144 
 145   double safe_total_heap_percentage = MIN2((double)(_heap_reserve_percent + _heap_waste_percent), 100.0);
 146 
 147   return MIN2(
 148     G1CollectedHeap::heap()->max_capacity() * (100.0 - safe_total_heap_percentage) / 100.0,
 149     _target_occupancy * (100.0 - _heap_waste_percent) / 100.0
 150     );
 151 }
 152 
 153 bool G1AdaptiveIHOPControl::have_enough_data_for_prediction() const {
 154   return ((size_t)_marking_times_s.num() >= G1AdaptiveIHOPNumInitialSamples) &&
 155          ((size_t)_allocation_rate_s.num() >= G1AdaptiveIHOPNumInitialSamples);
 156 }
 157 
 158 size_t G1AdaptiveIHOPControl::get_conc_mark_start_threshold() {
 159   if (have_enough_data_for_prediction()) {
 160     double pred_marking_time = _predictor->get_new_prediction(&_marking_times_s);
 161     double pred_promotion_rate = _predictor->get_new_prediction(&_allocation_rate_s);
 162 
 163     size_t predicted_needed_bytes_during_marking =
 164       (pred_marking_time * pred_promotion_rate +
 165       _last_unrestrained_young_size); // In reality we would need the maximum size of the young gen during marking. This is a conservative estimate.
 166 
 167     size_t internal_threshold = actual_target_threshold();
 168     size_t predicted_initiating_threshold = predicted_needed_bytes_during_marking < internal_threshold ?
 169                                             internal_threshold - predicted_needed_bytes_during_marking :
 170                                             0;
 171     return predicted_initiating_threshold;
 172   } else {
 173     // Use the initial value.
 174     return _initial_ihop_percent * _target_occupancy / 100.0;
 175   }
 176 }
 177 
 178 void G1AdaptiveIHOPControl::update_allocation_info(double allocation_time_s, size_t allocated_bytes, size_t additional_buffer_size) {
 179   G1IHOPControl::update_allocation_info(allocation_time_s, allocated_bytes, additional_buffer_size);
 180 
 181   double allocation_rate = (double) allocated_bytes / allocation_time_s;
 182   _allocation_rate_s.add(allocation_rate);
 183 
 184   _last_unrestrained_young_size = additional_buffer_size;
 185 }
 186 
 187 void G1AdaptiveIHOPControl::update_marking_length(double marking_length_s) {
 188    assert(marking_length_s >= 0.0, "Marking length must be larger than zero but is %.3f", marking_length_s);
 189   _marking_times_s.add(marking_length_s);
 190 }
 191 
 192 void G1AdaptiveIHOPControl::print() {
 193   G1IHOPControl::print();
 194   size_t actual_target = actual_target_threshold();
 195   ergo_verbose6(ErgoIHOP,
 196                 "adaptive IHOP information",
 197                 ergo_format_reason("value update")
 198                 ergo_format_byte_perc("threshold")
 199                 ergo_format_byte("internal target occupancy")
 200                 ergo_format_double("predicted old gen allocation rate")
 201                 ergo_format_double("predicted marking phase length")
 202                 ergo_format_str("prediction active"),
 203                 get_conc_mark_start_threshold(),
 204                 percent_of(get_conc_mark_start_threshold(), actual_target),
 205                 actual_target,
 206                 _predictor->get_new_prediction(&_allocation_rate_s),
 207                 _predictor->get_new_prediction(&_marking_times_s),
 208                 have_enough_data_for_prediction() ? "true" : "false"
 209                 );
 210 }
 211 
 212 void G1AdaptiveIHOPControl::send_trace_event(G1NewTracer* tracer) {
 213   G1IHOPControl::send_trace_event(tracer);
 214   tracer->report_adaptive_ihop_statistics(get_conc_mark_start_threshold(),
 215                                           actual_target_threshold(),
 216                                           G1CollectedHeap::heap()->used(),
 217                                           _last_unrestrained_young_size,
 218                                           _predictor->get_new_prediction(&_allocation_rate_s),
 219                                           _predictor->get_new_prediction(&_marking_times_s),
 220                                           have_enough_data_for_prediction());
 221 }
 222 
 223 #ifndef PRODUCT
 224 void G1AdaptiveIHOPControl::test() {
 225   size_t const initial_threshold = 45;
 226   size_t const young_size = 10;
 227   size_t const target_size = 100;
 228 
 229   // The final IHOP value is always
 230   // target_size - (young_size + alloc_amount/alloc_time * marking_time)
 231 
 232   G1Predictions pred(0.95);
 233   G1AdaptiveIHOPControl ctrl(initial_threshold, target_size, &pred, 0, 0);
 234 
 235   // First "load".
 236   size_t const alloc_time1 = 2;
 237   size_t const alloc_amount1 = 10;
 238   size_t const marking_time1 = 2;
 239   size_t const settled_ihop1 = target_size - (young_size + alloc_amount1/alloc_time1 * marking_time1);
 240 
 241   size_t threshold;
 242   threshold = ctrl.get_conc_mark_start_threshold();
 243   assert(threshold == initial_threshold,
 244          "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_threshold, threshold);
 245   for (size_t i = 0; i < G1AdaptiveIHOPNumInitialSamples - 1; i++) {
 246     ctrl.update_allocation_info(alloc_time1, alloc_amount1, young_size);
 247     ctrl.update_marking_length(marking_time1);
 248     // Not enough data yet.
 249     threshold = ctrl.get_conc_mark_start_threshold();
 250     assert(threshold == initial_threshold,
 251            "Expected IHOP threshold of " SIZE_FORMAT " but is " SIZE_FORMAT, initial_threshold, threshold);
 252   }
 253 
 254   test_update(&ctrl, alloc_time1, alloc_amount1, young_size, marking_time1);
 255 
 256   threshold = ctrl.get_conc_mark_start_threshold();
 257   assert(threshold == settled_ihop1,
 258          "Expected IHOP threshold to settle at " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop1, threshold);
 259 
 260   // Second "load". A bit higher allocation rate.
 261   size_t const alloc_time2 = 2;
 262   size_t const alloc_amount2 = 30;
 263   size_t const marking_time2 = 2;
 264   size_t const settled_ihop2 = target_size - (young_size + alloc_amount2/alloc_time2 * marking_time2);
 265 
 266   test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
 267 
 268   threshold = ctrl.get_conc_mark_start_threshold();
 269   assert(threshold < settled_ihop1,
 270          "Expected IHOP threshold to settle at a value lower than " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop1, threshold);
 271 
 272   // Third "load". Very high (impossible) allocation rate.
 273   size_t const alloc_time3 = 1;
 274   size_t const alloc_amount3 = 50;
 275   size_t const marking_time3 = 2;
 276   size_t const settled_ihop3 = 0;
 277 
 278   test_update(&ctrl, alloc_time3, alloc_amount3, young_size, marking_time3);
 279   threshold = ctrl.get_conc_mark_start_threshold();
 280 
 281   assert(threshold == settled_ihop3,
 282          "Expected IHOP threshold to settle at " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop3, threshold);
 283 
 284   // And back to some arbitrary value.
 285   test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
 286 
 287   threshold = ctrl.get_conc_mark_start_threshold();
 288   assert(threshold > settled_ihop3,
 289          "Expected IHOP threshold to settle at value larger than " SIZE_FORMAT " but is " SIZE_FORMAT, settled_ihop3, threshold);
 290 }
 291 
 292 void IHOP_test() {
 293   G1StaticIHOPControl::test();
 294 }
 295 #endif