1 /* 2 * Copyright (c) 2003, 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 25 #include "precompiled.hpp" 26 #include "gc/parallel/adjoiningGenerations.hpp" 27 #include "gc/parallel/adjoiningVirtualSpaces.hpp" 28 #include "gc/parallel/generationSizer.hpp" 29 #include "gc/parallel/parallelScavengeHeap.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "utilities/align.hpp" 34 #include "utilities/ostream.hpp" 35 36 // If boundary moving is being used, create the young gen and old 37 // gen with ASPSYoungGen and ASPSOldGen, respectively. Revert to 38 // the old behavior otherwise (with PSYoungGen and PSOldGen). 39 40 AdjoiningGenerations::AdjoiningGenerations(ReservedSpace old_young_rs, 41 GenerationSizer* policy, 42 size_t alignment) : 43 _virtual_spaces(old_young_rs, policy->min_old_size(), 44 policy->min_young_size(), alignment) { 45 size_t init_low_byte_size = policy->initial_old_size(); 46 size_t min_low_byte_size = policy->min_old_size(); 47 size_t max_low_byte_size = policy->max_old_size(); 48 size_t init_high_byte_size = policy->initial_young_size(); 49 size_t min_high_byte_size = policy->min_young_size(); 50 size_t max_high_byte_size = policy->max_young_size(); 51 52 assert(min_low_byte_size <= init_low_byte_size && 53 init_low_byte_size <= max_low_byte_size, "Parameter check"); 54 assert(min_high_byte_size <= init_high_byte_size && 55 init_high_byte_size <= max_high_byte_size, "Parameter check"); 56 // Create the generations differently based on the option to 57 // move the boundary. 58 if (UseAdaptiveGCBoundary) { 59 // Initialize the adjoining virtual spaces. Then pass the 60 // a virtual to each generation for initialization of the 61 // generation. 62 63 if (os::has_nvdimm()) { 64 _virtual_spaces.setup_fd(old_young_rs.nvdimm_fd()); 65 #if defined(_WINDOWS) 66 log_info(gc, heap)("On Windows, ParallelOldGC with NVDIMM does not support adaptive sizing of OldGen. "); 67 init_low_byte_size = policy->max_old_size(); 68 min_low_byte_size = policy->max_old_size(); 69 max_low_byte_size = policy->max_old_size(); 70 #endif 71 } 72 // Does the actual creation of the virtual spaces 73 _virtual_spaces.initialize(max_low_byte_size, 74 init_low_byte_size, 75 init_high_byte_size); 76 77 // Place the young gen at the high end. Passes in the virtual space. 78 _young_gen = new ASPSYoungGen(_virtual_spaces.high(), 79 _virtual_spaces.high()->committed_size(), 80 min_high_byte_size, 81 _virtual_spaces.high_byte_size_limit()); 82 83 // Place the old gen at the low end. Passes in the virtual space. 84 _old_gen = new ASPSOldGen(_virtual_spaces.low(), 85 _virtual_spaces.low()->committed_size(), 86 min_low_byte_size, 87 _virtual_spaces.low_byte_size_limit(), 88 "old", 1); 89 90 if (os::has_nvdimm()) { 91 _old_gen->setup_fd(old_young_rs.nvdimm_fd()); 92 } 93 young_gen()->initialize_work(); 94 assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(), 95 "Consistency check"); 96 assert(old_young_rs.size() >= young_gen()->gen_size_limit(), 97 "Consistency check"); 98 99 old_gen()->initialize_work("old", 1); 100 assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(), 101 "Consistency check"); 102 assert(old_young_rs.size() >= old_gen()->gen_size_limit(), 103 "Consistency check"); 104 } else { 105 106 // Layout the reserved space for the generations. 107 ReservedSpace old_rs = 108 virtual_spaces()->reserved_space().first_part(max_low_byte_size); 109 ReservedSpace heap_rs = 110 virtual_spaces()->reserved_space().last_part(max_low_byte_size); 111 ReservedSpace young_rs = heap_rs.first_part(max_high_byte_size); 112 assert(young_rs.size() == heap_rs.size(), "Didn't reserve all of the heap"); 113 114 // Create the generations. Virtual spaces are not passed in. 115 _young_gen = new PSYoungGen(init_high_byte_size, 116 min_high_byte_size, 117 max_high_byte_size); 118 _old_gen = new PSOldGen(init_low_byte_size, 119 min_low_byte_size, 120 max_low_byte_size, 121 "old", 1); 122 123 // The virtual spaces are created by the initialization of the gens. 124 _young_gen->initialize(young_rs, alignment); 125 assert(young_gen()->gen_size_limit() == young_rs.size(), 126 "Consistency check"); 127 if (os::has_nvdimm()) { 128 _old_gen->setup_fd(old_young_rs.nvdimm_fd()); 129 old_rs.setup_fd(old_young_rs.nvdimm_fd()); 130 } 131 _old_gen->initialize(old_rs, alignment, "old", 1); 132 assert(old_gen()->gen_size_limit() == old_rs.size(), "Consistency check"); 133 } 134 } 135 136 size_t AdjoiningGenerations::reserved_byte_size() { 137 return virtual_spaces()->reserved_space().size(); 138 } 139 140 void log_before_expansion(bool old, size_t expand_in_bytes, size_t change_in_bytes, size_t max_size) { 141 Log(heap, ergo) log; 142 if (!log.is_debug()) { 143 return; 144 } 145 log.debug("Before expansion of %s gen with boundary move", old ? "old" : "young"); 146 log.debug(" Requested change: " SIZE_FORMAT_HEX " Attempted change: " SIZE_FORMAT_HEX, 147 expand_in_bytes, change_in_bytes); 148 ResourceMark rm; 149 LogStream ls(log.debug()); 150 ParallelScavengeHeap::heap()->print_on(&ls); 151 log.debug(" PS%sGen max size: " SIZE_FORMAT "K", old ? "Old" : "Young", max_size/K); 152 } 153 154 void log_after_expansion(bool old, size_t max_size) { 155 Log(heap, ergo) log; 156 if (!log.is_debug()) { 157 return; 158 } 159 log.debug("After expansion of %s gen with boundary move", old ? "old" : "young"); 160 ResourceMark rm; 161 LogStream ls(log.debug()); 162 ParallelScavengeHeap::heap()->print_on(&ls); 163 log.debug(" PS%sGen max size: " SIZE_FORMAT "K", old ? "Old" : "Young", max_size/K); 164 } 165 166 // Make checks on the current sizes of the generations and 167 // the constraints on the sizes of the generations. Push 168 // up the boundary within the constraints. A partial 169 // push can occur. 170 void AdjoiningGenerations::request_old_gen_expansion(size_t expand_in_bytes) { 171 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); 172 173 assert_lock_strong(ExpandHeap_lock); 174 assert_locked_or_safepoint(Heap_lock); 175 176 // These sizes limit the amount the boundaries can move. Effectively, 177 // the generation says how much it is willing to yield to the other 178 // generation. 179 const size_t young_gen_available = young_gen()->available_for_contraction(); 180 const size_t old_gen_available = old_gen()->available_for_expansion(); 181 const size_t alignment = virtual_spaces()->alignment(); 182 size_t change_in_bytes = MIN3(young_gen_available, 183 old_gen_available, 184 align_up(expand_in_bytes, alignment)); 185 186 if (change_in_bytes == 0) { 187 return; 188 } 189 190 log_before_expansion(true, expand_in_bytes, change_in_bytes, old_gen()->max_gen_size()); 191 192 // Move the boundary between the generations up (smaller young gen). 193 if (virtual_spaces()->adjust_boundary_up(change_in_bytes)) { 194 young_gen()->reset_after_change(); 195 old_gen()->reset_after_change(); 196 } 197 198 // The total reserved for the generations should match the sum 199 // of the two even if the boundary is moving. 200 assert(reserved_byte_size() == 201 old_gen()->max_gen_size() + young_gen()->max_size(), 202 "Space is missing"); 203 young_gen()->space_invariants(); 204 old_gen()->space_invariants(); 205 206 log_after_expansion(true, old_gen()->max_gen_size()); 207 } 208 209 // See comments on request_old_gen_expansion() 210 bool AdjoiningGenerations::request_young_gen_expansion(size_t expand_in_bytes) { 211 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); 212 213 // If eden is not empty, the boundary can be moved but no advantage 214 // can be made of the move since eden cannot be moved. 215 if (!young_gen()->eden_space()->is_empty()) { 216 return false; 217 } 218 219 220 bool result = false; 221 const size_t young_gen_available = young_gen()->available_for_expansion(); 222 const size_t old_gen_available = old_gen()->available_for_contraction(); 223 const size_t alignment = virtual_spaces()->alignment(); 224 size_t change_in_bytes = MIN3(young_gen_available, 225 old_gen_available, 226 align_up(expand_in_bytes, alignment)); 227 228 if (change_in_bytes == 0) { 229 return false; 230 } 231 232 log_before_expansion(false, expand_in_bytes, change_in_bytes, young_gen()->max_size()); 233 234 // Move the boundary between the generations down (smaller old gen). 235 MutexLocker x(ExpandHeap_lock); 236 if (virtual_spaces()->adjust_boundary_down(change_in_bytes)) { 237 young_gen()->reset_after_change(); 238 old_gen()->reset_after_change(); 239 result = true; 240 } 241 242 // The total reserved for the generations should match the sum 243 // of the two even if the boundary is moving. 244 assert(reserved_byte_size() == 245 old_gen()->max_gen_size() + young_gen()->max_size(), 246 "Space is missing"); 247 young_gen()->space_invariants(); 248 old_gen()->space_invariants(); 249 250 log_after_expansion(false, young_gen()->max_size()); 251 252 return result; 253 } 254 255 // Additional space is needed in the old generation. Try to move the boundary 256 // up to meet the need. Moves boundary up only 257 void AdjoiningGenerations::adjust_boundary_for_old_gen_needs( 258 size_t desired_free_space) { 259 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); 260 261 // Stress testing. 262 if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 1) { 263 MutexLocker x(ExpandHeap_lock); 264 request_old_gen_expansion(virtual_spaces()->alignment() * 3 / 2); 265 } 266 267 // Expand only if the entire generation is already committed. 268 if (old_gen()->virtual_space()->uncommitted_size() == 0) { 269 if (old_gen()->free_in_bytes() < desired_free_space) { 270 MutexLocker x(ExpandHeap_lock); 271 request_old_gen_expansion(desired_free_space); 272 } 273 } 274 } 275 276 // See comment on adjust_boundary_for_old_gen_needss(). 277 // Adjust boundary down only. 278 void AdjoiningGenerations::adjust_boundary_for_young_gen_needs(size_t eden_size, 279 size_t survivor_size) { 280 281 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); 282 283 // Stress testing. 284 if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 0) { 285 request_young_gen_expansion(virtual_spaces()->alignment() * 3 / 2); 286 eden_size = young_gen()->eden_space()->capacity_in_bytes(); 287 } 288 289 // Expand only if the entire generation is already committed. 290 if (young_gen()->virtual_space()->uncommitted_size() == 0) { 291 size_t desired_size = eden_size + 2 * survivor_size; 292 const size_t committed = young_gen()->virtual_space()->committed_size(); 293 if (desired_size > committed) { 294 request_young_gen_expansion(desired_size - committed); 295 } 296 } 297 }