1 /* 2 * Copyright (c) 2016, 2018, 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/shared/collectorPolicy.hpp" 26 #include "runtime/arguments.hpp" 27 #include "runtime/flags/flagSetting.hpp" 28 #include "runtime/globals_extension.hpp" 29 #include "utilities/align.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 #include "utilities/macros.hpp" 32 #include "unittest.hpp" 33 34 class TestGenCollectorPolicy { 35 public: 36 37 class Executor { 38 public: 39 virtual void execute() = 0; 40 }; 41 42 class UnaryExecutor : public Executor { 43 protected: 44 const size_t param; 45 public: 46 UnaryExecutor(size_t val) : param(val) { } 47 }; 48 49 class BinaryExecutor : public Executor { 50 protected: 51 const size_t param1; 52 const size_t param2; 53 public: 54 BinaryExecutor(size_t val1, size_t val2) : param1(val1), param2(val2) { } 55 }; 56 57 class MinHeapSizeGuard { 58 private: 59 const size_t _stored_min_heap_size; 60 public: 61 MinHeapSizeGuard() : _stored_min_heap_size(Arguments::min_heap_size()) { } 62 ~MinHeapSizeGuard() { 63 Arguments::set_min_heap_size(_stored_min_heap_size); 64 } 65 }; 66 67 class TestWrapper { 68 public: 69 static void test(Executor* setter1, Executor* setter2, Executor* checker) { 70 FLAG_GUARD(InitialHeapSize); 71 FLAG_GUARD(MaxHeapSize); 72 FLAG_GUARD(MaxNewSize); 73 FLAG_GUARD(MinHeapDeltaBytes); 74 FLAG_GUARD(NewSize); 75 FLAG_GUARD(OldSize); 76 MinHeapSizeGuard min_heap_size_guard; 77 78 FLAG_SET_ERGO(size_t, InitialHeapSize, 100 * M); 79 FLAG_SET_ERGO(size_t, OldSize, 4 * M); 80 FLAG_SET_ERGO(size_t, NewSize, 1 * M); 81 FLAG_SET_ERGO(size_t, MaxNewSize, 80 * M); 82 Arguments::set_min_heap_size(40 * M); 83 84 ASSERT_NO_FATAL_FAILURE(setter1->execute()); 85 86 if (setter2 != NULL) { 87 ASSERT_NO_FATAL_FAILURE(setter2->execute()); 88 } 89 90 ASSERT_NO_FATAL_FAILURE(checker->execute()); 91 } 92 static void test(Executor* setter, Executor* checker) { 93 test(setter, NULL, checker); 94 } 95 }; 96 97 class SetNewSizeErgo : public UnaryExecutor { 98 public: 99 SetNewSizeErgo(size_t param) : UnaryExecutor(param) { } 100 void execute() { 101 FLAG_SET_ERGO(size_t, NewSize, param); 102 } 103 }; 104 105 class CheckYoungMin : public UnaryExecutor { 106 public: 107 CheckYoungMin(size_t param) : UnaryExecutor(param) { } 108 void execute() { 109 MarkSweepPolicy msp; 110 msp.initialize_all(); 111 ASSERT_LE(msp.min_young_size(), param); 112 } 113 }; 114 115 class CheckScaledYoungInitial : public Executor { 116 public: 117 void execute() { 118 size_t initial_heap_size = InitialHeapSize; 119 MarkSweepPolicy msp; 120 msp.initialize_all(); 121 122 if (InitialHeapSize > initial_heap_size) { 123 // InitialHeapSize was adapted by msp.initialize_all, e.g. due to alignment 124 // caused by 64K page size. 125 initial_heap_size = InitialHeapSize; 126 } 127 128 size_t expected = msp.scale_by_NewRatio_aligned(initial_heap_size); 129 ASSERT_EQ(expected, msp.initial_young_size()); 130 ASSERT_EQ(expected, NewSize); 131 } 132 }; 133 134 class SetNewSizeCmd : public UnaryExecutor { 135 public: 136 SetNewSizeCmd(size_t param) : UnaryExecutor(param) { } 137 void execute() { 138 FLAG_SET_CMDLINE(size_t, NewSize, param); 139 } 140 }; 141 142 class CheckYoungInitial : public UnaryExecutor { 143 public: 144 CheckYoungInitial(size_t param) : UnaryExecutor(param) { } 145 void execute() { 146 MarkSweepPolicy msp; 147 msp.initialize_all(); 148 149 ASSERT_EQ(param, msp.initial_young_size()); 150 } 151 }; 152 153 class SetOldSizeCmd : public UnaryExecutor { 154 public: 155 SetOldSizeCmd(size_t param) : UnaryExecutor(param) { } 156 void execute() { 157 FLAG_SET_CMDLINE(size_t, OldSize, param); 158 } 159 }; 160 161 class SetMaxNewSizeCmd : public BinaryExecutor { 162 public: 163 SetMaxNewSizeCmd(size_t param1, size_t param2) : BinaryExecutor(param1, param2) { } 164 void execute() { 165 size_t heap_alignment = CollectorPolicy::compute_heap_alignment(); 166 size_t new_size_value = align_up(MaxHeapSize, heap_alignment) 167 - param1 + param2; 168 FLAG_SET_CMDLINE(size_t, MaxNewSize, new_size_value); 169 } 170 }; 171 172 class CheckOldMin : public UnaryExecutor { 173 public: 174 CheckOldMin(size_t param) : UnaryExecutor(param) { } 175 void execute() { 176 MarkSweepPolicy msp; 177 msp.initialize_all(); 178 ASSERT_LE(msp.min_old_size(), param); 179 } 180 }; 181 182 class CheckOldInitial : public Executor { 183 public: 184 void execute() { 185 size_t heap_alignment = CollectorPolicy::compute_heap_alignment(); 186 187 MarkSweepPolicy msp; 188 msp.initialize_all(); 189 190 size_t expected_old_initial = align_up(InitialHeapSize, heap_alignment) 191 - MaxNewSize; 192 193 ASSERT_EQ(expected_old_initial, msp.initial_old_size()); 194 } 195 }; 196 197 class CheckOldInitialMaxNewSize : public BinaryExecutor { 198 public: 199 CheckOldInitialMaxNewSize(size_t param1, size_t param2) : BinaryExecutor(param1, param2) { } 200 void execute() { 201 size_t heap_alignment = CollectorPolicy::compute_heap_alignment(); 202 size_t new_size_value = align_up(MaxHeapSize, heap_alignment) 203 - param1 + param2; 204 205 MarkSweepPolicy msp; 206 msp.initialize_all(); 207 208 size_t expected_old_initial = align_up(MaxHeapSize, heap_alignment) 209 - new_size_value; 210 211 ASSERT_EQ(expected_old_initial, msp.initial_old_size()); 212 } 213 }; 214 }; 215 216 217 // Testing that the NewSize flag is handled correct is hard because it 218 // depends on so many other configurable variables. These tests only try to 219 // verify that there are some basic rules for NewSize honored by the policies. 220 221 // If NewSize has been ergonomically set, the collector policy 222 // should use it for min 223 TEST_VM(CollectorPolicy, young_min_ergo) { 224 TestGenCollectorPolicy::SetNewSizeErgo setter(20 * M); 225 TestGenCollectorPolicy::CheckYoungMin checker(20 * M); 226 227 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker); 228 } 229 230 // If NewSize has been ergonomically set, the collector policy 231 // should use it for min but calculate the initial young size 232 // using NewRatio. 233 TEST_VM(CollectorPolicy, young_scaled_initial_ergo) { 234 TestGenCollectorPolicy::SetNewSizeErgo setter(20 * M); 235 TestGenCollectorPolicy::CheckScaledYoungInitial checker; 236 237 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker); 238 } 239 240 241 // Since a flag has been set with FLAG_SET_CMDLINE it 242 // will be treated as it have been set on the command line for 243 // the rest of the VM lifetime. This is an irreversible change and 244 // could impact other tests so we use TEST_OTHER_VM 245 TEST_OTHER_VM(CollectorPolicy, young_cmd) { 246 // If NewSize is set on the command line, it should be used 247 // for both min and initial young size if less than min heap. 248 TestGenCollectorPolicy::SetNewSizeCmd setter(20 * M); 249 250 TestGenCollectorPolicy::CheckYoungMin checker_min(20 * M); 251 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_min); 252 253 TestGenCollectorPolicy::CheckYoungInitial checker_initial(20 * M); 254 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_initial); 255 256 // If NewSize is set on command line, but is larger than the min 257 // heap size, it should only be used for initial young size. 258 TestGenCollectorPolicy::SetNewSizeCmd setter_large(80 * M); 259 TestGenCollectorPolicy::CheckYoungInitial checker_large(80 * M); 260 TestGenCollectorPolicy::TestWrapper::test(&setter_large, &checker_large); 261 } 262 263 // Since a flag has been set with FLAG_SET_CMDLINE it 264 // will be treated as it have been set on the command line for 265 // the rest of the VM lifetime. This is an irreversible change and 266 // could impact other tests so we use TEST_OTHER_VM 267 TEST_OTHER_VM(CollectorPolicy, old_cmd) { 268 // If OldSize is set on the command line, it should be used 269 // for both min and initial old size if less than min heap. 270 TestGenCollectorPolicy::SetOldSizeCmd setter(20 * M); 271 272 TestGenCollectorPolicy::CheckOldMin checker_min(20 * M); 273 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_min); 274 275 TestGenCollectorPolicy::CheckOldInitial checker_initial; 276 TestGenCollectorPolicy::TestWrapper::test(&setter, &checker_initial); 277 278 // If MaxNewSize is large, the maximum OldSize will be less than 279 // what's requested on the command line and it should be reset 280 // ergonomically. 281 // We intentionally set MaxNewSize + OldSize > MaxHeapSize 282 TestGenCollectorPolicy::SetOldSizeCmd setter_old_size(30 * M); 283 TestGenCollectorPolicy::SetMaxNewSizeCmd setter_max_new_size(30 * M, 20 * M); 284 TestGenCollectorPolicy::CheckOldInitialMaxNewSize checker_large(30 * M, 20 * M); 285 286 TestGenCollectorPolicy::TestWrapper::test(&setter_old_size, &setter_max_new_size, &checker_large); 287 }