--- old/src/share/vm/gc/g1/g1CollectorPolicy.cpp 2015-11-11 10:07:38.657894420 +0100 +++ new/src/share/vm/gc/g1/g1CollectorPolicy.cpp 2015-11-11 10:07:38.572891895 +0100 @@ -1245,8 +1245,25 @@ } G1IHOPControl* G1CollectorPolicy::create_ihop_control() const { - return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent, - G1CollectedHeap::heap()->max_capacity()); + if (G1UseAdaptiveIHOP) { + // The target occupancy is the total heap occupancy we want to hit. First, we + // want to avoid eating into the reserve intended for young GC (to avoid unnecessary + // throughput loss). Additionally G1 is free to not clean out up to + // G1HeapWastePercent of heap, that space also cannot be used for allocation + // while marking. + size_t safe_heap_percentage = (size_t) (G1ReservePercent + G1HeapWastePercent); + size_t target_occupancy = 0; + + if (safe_heap_percentage < 100) { + target_occupancy = G1CollectedHeap::heap()->max_capacity() * (100.0 - safe_heap_percentage) / 100.0; + } + return new G1AdaptiveIHOPControl(InitiatingHeapOccupancyPercent, + target_occupancy, + &_predictor); + } else { + return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent, + G1CollectedHeap::heap()->max_capacity()); + } } void G1CollectorPolicy::update_ihop_statistics(double marking_time, --- old/src/share/vm/gc/g1/g1IHOPControl.cpp 2015-11-11 10:07:39.139908735 +0100 +++ new/src/share/vm/gc/g1/g1IHOPControl.cpp 2015-11-11 10:07:39.059906359 +0100 @@ -50,13 +50,13 @@ ergo_format_byte("target occupancy") ergo_format_byte("current occupancy") ergo_format_double("recent old gen allocation rate") - ergo_format_ms("recent marking phase length"), + ergo_format_double("recent marking phase length"), get_conc_mark_start_threshold(), (double) get_conc_mark_start_threshold() / _target_occupancy * 100.0, _target_occupancy, G1CollectedHeap::heap()->used(), _last_allocation_time_s > 0.0 ? _last_allocated_bytes / _last_allocation_time_s : 0.0, - _last_marking_length_s * 1000.0); + _last_marking_length_s); } #ifndef PRODUCT @@ -100,8 +100,165 @@ } #endif +G1AdaptiveIHOPControl::G1AdaptiveIHOPControl(double ihop_percent, size_t initial_target_occupancy, G1Predictions const* predictor) : + G1IHOPControl(ihop_percent, initial_target_occupancy), + _predictor(predictor), + _marking_times_s(10, 0.95), + _allocation_rate_s(10, 0.95), + _prev_unrestrained_young_size(0), + _current_threshold(0.0) +{ + recalculate(); +} + +void G1AdaptiveIHOPControl::recalculate() { + 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 + + _prev_unrestrained_young_size); // In reality we would need the size of the young gen of the first mixed GC. This is a conservative estimate. + size_t predicted_initiating_threshold = predicted_needed_bytes_during_marking < _target_occupancy ? + _target_occupancy - predicted_needed_bytes_during_marking : + 0; + _current_threshold = predicted_initiating_threshold; + } else { + // Use the initial value. + _current_threshold = _ihop_percent * _target_occupancy / 100.0; + } +} + +bool G1AdaptiveIHOPControl::have_enough_data_for_prediction() const { + return (_marking_times_s.num() > 2) && (_allocation_rate_s.num() > 2); +} + +void G1AdaptiveIHOPControl::set_target_occupancy(size_t target_occupancy) { + _target_occupancy = target_occupancy; +} + +size_t G1AdaptiveIHOPControl::get_conc_mark_start_threshold() { + recalculate(); + return _current_threshold; +} + +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; + _prev_unrestrained_young_size = additional_buffer_size; +} + +void G1AdaptiveIHOPControl::update_time_to_mixed(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(), + _target_occupancy > 0 ? (double) get_conc_mark_start_threshold() / _target_occupancy * 100.0 : 0.0, + _target_occupancy, + G1CollectedHeap::heap()->used(), + _allocation_rate_s.last(), + _marking_times_s.last() + ); + ergo_verbose3(ErgoIHOP, + "adaptive IHOP information", + ergo_format_reason("value update") + ergo_format_double("predicted old gen allocation rate") + ergo_format_double("predicted marking phase length") + ergo_format_str("prediction active"), + _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); + + // 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); + ctrl.update_allocation_info(alloc_time1, alloc_amount1, young_size); + ctrl.update_time_to_mixed(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); + // Not enough data yet. + ctrl.update_allocation_info(alloc_time1, alloc_amount1, young_size); + ctrl.update_time_to_mixed(marking_time1); + 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; // target_size - (young_size + alloc_amount2/alloc_time2 * marking_time2); + + 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(); + G1AdaptiveIHOPControl::test(); } #endif --- old/src/share/vm/gc/g1/g1IHOPControl.hpp 2015-11-11 10:07:39.596922307 +0100 +++ new/src/share/vm/gc/g1/g1IHOPControl.hpp 2015-11-11 10:07:39.516919931 +0100 @@ -81,4 +81,35 @@ #endif }; +class G1AdaptiveIHOPControl : public G1IHOPControl { + G1Predictions const * _predictor; + + TruncatedSeq _marking_times_s; + TruncatedSeq _allocation_rate_s; + + size_t _last_allocation_bytes; // Most recent mutator allocation since last GC. + size_t _prev_unrestrained_young_size; + + size_t _current_threshold; + + // Updates _current_threshold according to internal state. + void recalculate(); + + bool have_enough_data_for_prediction() const; + public: + G1AdaptiveIHOPControl(double ihop_percent, size_t initial_target_occupancy, G1Predictions const* predictor); + + virtual void set_target_occupancy(size_t target_occupancy); + + 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_time_to_mixed(double marking_length_s); + + virtual void print(); +#ifndef PRODUCT + static void test(); +#endif +}; + #endif // SHARE_VM_GC_G1_G1IHOPCONTROL_HPP --- old/src/share/vm/gc/g1/g1_globals.hpp 2015-11-11 10:07:40.080936681 +0100 +++ new/src/share/vm/gc/g1/g1_globals.hpp 2015-11-11 10:07:39.996934186 +0100 @@ -33,6 +33,10 @@ #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.") \ + \ product(uintx, G1ConfidencePercent, 50, \ "Confidence level for MMU/pause predictions") \ range(0, 100) \