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