1 /* 2 * Copyright (c) 2016, 2020, 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 #include "precompiled.hpp" 25 #include "gc/g1/g1CollectedHeap.inline.hpp" 26 #include "gc/g1/g1IHOPControl.hpp" 27 #include "gc/g1/g1OldGenAllocationTracker.hpp" 28 #include "gc/g1/g1Predictions.hpp" 29 #include "unittest.hpp" 30 31 static void test_update_allocation_tracker(G1OldGenAllocationTracker* alloc_tracker, 32 double alloc_time, size_t alloc_amount) { 33 alloc_tracker->add_allocated_bytes_since_last_gc(alloc_amount); 34 alloc_tracker->reset_after_young_gc(alloc_time, (size_t)0); 35 } 36 37 static void test_update(G1IHOPControl* ctrl, 38 G1OldGenAllocationTracker* alloc_tracker, 39 double alloc_time, size_t alloc_amount, 40 size_t young_size, double mark_time) { 41 for (int i = 0; i < 100; i++) { 42 test_update_allocation_tracker(alloc_tracker, alloc_time, alloc_amount); 43 ctrl->update_allocation_info(young_size); 44 ctrl->update_marking_length(mark_time); 45 } 46 } 47 48 // @requires UseG1GC 49 TEST_VM(G1StaticIHOPControl, simple) { 50 // Test requires G1 51 if (!UseG1GC) { 52 return; 53 } 54 55 const size_t initial_ihop = 45; 56 57 G1OldGenAllocationTracker alloc_tracker; 58 G1StaticIHOPControl ctrl(initial_ihop, &alloc_tracker); 59 ctrl.update_target_occupancy(100); 60 61 size_t threshold = ctrl.get_conc_mark_start_threshold(); 62 EXPECT_EQ(initial_ihop, threshold); 63 64 test_update_allocation_tracker(&alloc_tracker, 100.0, 100); 65 ctrl.update_allocation_info(100); 66 threshold = ctrl.get_conc_mark_start_threshold(); 67 EXPECT_EQ(initial_ihop, threshold); 68 69 ctrl.update_marking_length(1000.0); 70 threshold = ctrl.get_conc_mark_start_threshold(); 71 EXPECT_EQ(initial_ihop, threshold); 72 73 // Whatever we pass, the IHOP value must stay the same. 74 test_update(&ctrl, &alloc_tracker, 2, 10, 10, 3); 75 threshold = ctrl.get_conc_mark_start_threshold(); 76 77 EXPECT_EQ(initial_ihop, threshold); 78 79 test_update(&ctrl, &alloc_tracker, 12, 10, 10, 3); 80 threshold = ctrl.get_conc_mark_start_threshold(); 81 82 EXPECT_EQ(initial_ihop, threshold); 83 } 84 85 // @requires UseG1GC 86 TEST_VM(G1AdaptiveIHOPControl, simple) { 87 // Test requires G1 88 if (!UseG1GC) { 89 return; 90 } 91 92 const size_t initial_threshold = 45; 93 const size_t young_size = 10; 94 const size_t target_size = 100; 95 96 // The final IHOP value is always 97 // target_size - (young_size + alloc_amount/alloc_time * marking_time) 98 99 G1OldGenAllocationTracker alloc_tracker; 100 G1Predictions pred(0.95); 101 G1AdaptiveIHOPControl ctrl(initial_threshold, &alloc_tracker, &pred, 0, 0); 102 ctrl.update_target_occupancy(target_size); 103 104 // First "load". 105 const size_t alloc_time1 = 2; 106 const size_t alloc_amount1 = 10; 107 const size_t marking_time1 = 2; 108 const size_t settled_ihop1 = target_size 109 - (young_size + alloc_amount1 / alloc_time1 * marking_time1); 110 111 size_t threshold; 112 threshold = ctrl.get_conc_mark_start_threshold(); 113 114 EXPECT_EQ(initial_threshold, threshold); 115 116 for (size_t i = 0; i < G1AdaptiveIHOPNumInitialSamples - 1; i++) { 117 test_update_allocation_tracker(&alloc_tracker, alloc_time1, alloc_amount1); 118 ctrl.update_allocation_info(young_size); 119 ctrl.update_marking_length(marking_time1); 120 // Not enough data yet. 121 threshold = ctrl.get_conc_mark_start_threshold(); 122 123 ASSERT_EQ(initial_threshold, threshold) << "on step " << i; 124 } 125 126 test_update(&ctrl, &alloc_tracker, alloc_time1, alloc_amount1, young_size, marking_time1); 127 128 threshold = ctrl.get_conc_mark_start_threshold(); 129 130 EXPECT_EQ(settled_ihop1, threshold); 131 132 // Second "load". A bit higher allocation rate. 133 const size_t alloc_time2 = 2; 134 const size_t alloc_amount2 = 30; 135 const size_t marking_time2 = 2; 136 const size_t settled_ihop2 = target_size 137 - (young_size + alloc_amount2 / alloc_time2 * marking_time2); 138 139 test_update(&ctrl, &alloc_tracker, alloc_time2, alloc_amount2, young_size, marking_time2); 140 141 threshold = ctrl.get_conc_mark_start_threshold(); 142 143 EXPECT_LT(threshold, settled_ihop1); 144 145 // Third "load". Very high (impossible) allocation rate. 146 const size_t alloc_time3 = 1; 147 const size_t alloc_amount3 = 50; 148 const size_t marking_time3 = 2; 149 const size_t settled_ihop3 = 0; 150 151 test_update(&ctrl, &alloc_tracker, alloc_time3, alloc_amount3, young_size, marking_time3); 152 threshold = ctrl.get_conc_mark_start_threshold(); 153 154 EXPECT_EQ(settled_ihop3, threshold); 155 156 // And back to some arbitrary value. 157 test_update(&ctrl, &alloc_tracker, alloc_time2, alloc_amount2, young_size, marking_time2); 158 159 threshold = ctrl.get_conc_mark_start_threshold(); 160 161 EXPECT_GT(threshold, settled_ihop3); 162 }