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