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