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/asPSYoungGen.hpp" 27 #include "gc/parallel/parallelScavengeHeap.hpp" 28 #include "gc/parallel/psMarkSweepDecorator.hpp" 29 #include "gc/parallel/psScavenge.inline.hpp" 30 #include "gc/parallel/psYoungGen.hpp" 31 #include "gc/shared/gcUtil.hpp" 32 #include "gc/shared/genArguments.hpp" 33 #include "gc/shared/spaceDecorator.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/java.hpp" 36 #include "utilities/align.hpp" 37 38 ASPSYoungGen::ASPSYoungGen(size_t init_byte_size, 39 size_t minimum_byte_size, 40 size_t byte_size_limit) : 41 PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 42 _gen_size_limit(byte_size_limit) { 43 } 44 45 46 ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs, 47 size_t init_byte_size, 48 size_t minimum_byte_size, 49 size_t byte_size_limit) : 50 //PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 51 PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit), 52 _gen_size_limit(byte_size_limit) { 53 54 assert(vs->committed_size() == init_byte_size, "Cannot replace with"); 55 56 _virtual_space = vs; 57 } 58 59 void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs, 60 size_t alignment) { 61 assert(_init_gen_size != 0, "Should have a finite size"); 62 _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment); 63 if (!_virtual_space->expand_by(_init_gen_size)) { 64 vm_exit_during_initialization("Could not reserve enough space for " 65 "object heap"); 66 } 67 } 68 69 void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) { 70 initialize_virtual_space(rs, alignment); 71 initialize_work(); 72 } 73 74 size_t ASPSYoungGen::available_for_expansion() { 75 size_t current_committed_size = virtual_space()->committed_size(); 76 assert((gen_size_limit() >= current_committed_size), 77 "generation size limit is wrong"); 78 79 size_t result = gen_size_limit() - current_committed_size; 80 size_t result_aligned = align_down(result, GenAlignment); 81 return result_aligned; 82 } 83 84 // Return the number of bytes the young gen is willing give up. 85 // 86 // Future implementations could check the survivors and if to_space is in the 87 // right place (below from_space), take a chunk from to_space. 88 size_t ASPSYoungGen::available_for_contraction() { 89 size_t uncommitted_bytes = virtual_space()->uncommitted_size(); 90 if (uncommitted_bytes != 0) { 91 return uncommitted_bytes; 92 } 93 94 if (eden_space()->is_empty()) { 95 // Respect the minimum size for eden and for the young gen as a whole. 96 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 97 const size_t eden_alignment = SpaceAlignment; 98 99 assert(eden_space()->capacity_in_bytes() >= eden_alignment, 100 "Alignment is wrong"); 101 size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment; 102 eden_avail = align_down(eden_avail, GenAlignment); 103 104 assert(virtual_space()->committed_size() >= min_gen_size(), 105 "minimum gen size is wrong"); 106 size_t gen_avail = virtual_space()->committed_size() - min_gen_size(); 107 assert(virtual_space()->is_aligned(gen_avail), "not aligned"); 108 109 const size_t max_contraction = MIN2(eden_avail, gen_avail); 110 // See comment for ASPSOldGen::available_for_contraction() 111 // for reasons the "increment" fraction is used. 112 PSAdaptiveSizePolicy* policy = heap->size_policy(); 113 size_t result = policy->eden_increment_aligned_down(max_contraction); 114 size_t result_aligned = align_down(result, GenAlignment); 115 116 log_trace(gc, ergo)("ASPSYoungGen::available_for_contraction: " SIZE_FORMAT " K", result_aligned/K); 117 log_trace(gc, ergo)(" max_contraction " SIZE_FORMAT " K", max_contraction/K); 118 log_trace(gc, ergo)(" eden_avail " SIZE_FORMAT " K", eden_avail/K); 119 log_trace(gc, ergo)(" gen_avail " SIZE_FORMAT " K", gen_avail/K); 120 121 return result_aligned; 122 } 123 124 return 0; 125 } 126 127 // The current implementation only considers to the end of eden. 128 // If to_space is below from_space, to_space is not considered. 129 // to_space can be. 130 size_t ASPSYoungGen::available_to_live() { 131 const size_t alignment = SpaceAlignment; 132 133 // Include any space that is committed but is not in eden. 134 size_t available = pointer_delta(eden_space()->bottom(), 135 virtual_space()->low(), 136 sizeof(char)); 137 138 const size_t eden_capacity = eden_space()->capacity_in_bytes(); 139 if (eden_space()->is_empty() && eden_capacity > alignment) { 140 available += eden_capacity - alignment; 141 } 142 return available; 143 } 144 145 // Similar to PSYoungGen::resize_generation() but 146 // allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size 147 // expands at the low end of the virtual space 148 // moves the boundary between the generations in order to expand 149 // some additional diagnostics 150 // If no additional changes are required, this can be deleted 151 // and the changes factored back into PSYoungGen::resize_generation(). 152 bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) { 153 const size_t alignment = virtual_space()->alignment(); 154 size_t orig_size = virtual_space()->committed_size(); 155 bool size_changed = false; 156 157 // There used to be a guarantee here that 158 // (eden_size + 2*survivor_size) <= _max_gen_size 159 // This requirement is enforced by the calculation of desired_size 160 // below. It may not be true on entry since the size of the 161 // eden_size is no bounded by the generation size. 162 163 assert(max_size() == reserved().byte_size(), "max gen size problem?"); 164 assert(min_gen_size() <= orig_size && orig_size <= max_size(), 165 "just checking"); 166 167 // Adjust new generation size 168 const size_t eden_plus_survivors = 169 align_up(eden_size + 2 * survivor_size, alignment); 170 size_t desired_size = clamp(eden_plus_survivors, min_gen_size(), gen_size_limit()); 171 assert(desired_size <= gen_size_limit(), "just checking"); 172 173 if (desired_size > orig_size) { 174 // Grow the generation 175 size_t change = desired_size - orig_size; 176 HeapWord* prev_low = (HeapWord*) virtual_space()->low(); 177 if (!virtual_space()->expand_by(change)) { 178 return false; 179 } 180 if (ZapUnusedHeapArea) { 181 // Mangle newly committed space immediately because it 182 // can be done here more simply that after the new 183 // spaces have been computed. 184 HeapWord* new_low = (HeapWord*) virtual_space()->low(); 185 assert(new_low < prev_low, "Did not grow"); 186 187 MemRegion mangle_region(new_low, prev_low); 188 SpaceMangler::mangle_region(mangle_region); 189 } 190 size_changed = true; 191 } else if (desired_size < orig_size) { 192 size_t desired_change = orig_size - desired_size; 193 194 // How much is available for shrinking. 195 size_t available_bytes = limit_gen_shrink(desired_change); 196 size_t change = MIN2(desired_change, available_bytes); 197 virtual_space()->shrink_by(change); 198 size_changed = true; 199 } else { 200 if (orig_size == gen_size_limit()) { 201 log_trace(gc)("ASPSYoung generation size at maximum: " SIZE_FORMAT "K", orig_size/K); 202 } else if (orig_size == min_gen_size()) { 203 log_trace(gc)("ASPSYoung generation size at minium: " SIZE_FORMAT "K", orig_size/K); 204 } 205 } 206 207 if (size_changed) { 208 reset_after_change(); 209 log_trace(gc)("ASPSYoung generation size changed: " SIZE_FORMAT "K->" SIZE_FORMAT "K", 210 orig_size/K, virtual_space()->committed_size()/K); 211 } 212 213 guarantee(eden_plus_survivors <= virtual_space()->committed_size() || 214 virtual_space()->committed_size() == max_size(), "Sanity"); 215 216 return true; 217 } 218 219 // Similar to PSYoungGen::resize_spaces() but 220 // eden always starts at the low end of the committed virtual space 221 // current implementation does not allow holes between the spaces 222 // _young_generation_boundary has to be reset because it changes. 223 // so additional verification 224 225 void ASPSYoungGen::resize_spaces(size_t requested_eden_size, 226 size_t requested_survivor_size) { 227 assert(UseAdaptiveSizePolicy, "sanity check"); 228 assert(requested_eden_size > 0 && requested_survivor_size > 0, 229 "just checking"); 230 231 space_invariants(); 232 233 // We require eden and to space to be empty 234 if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) { 235 return; 236 } 237 238 log_trace(gc, ergo)("PSYoungGen::resize_spaces(requested_eden_size: " 239 SIZE_FORMAT 240 ", requested_survivor_size: " SIZE_FORMAT ")", 241 requested_eden_size, requested_survivor_size); 242 log_trace(gc, ergo)(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 243 SIZE_FORMAT, 244 p2i(eden_space()->bottom()), 245 p2i(eden_space()->end()), 246 pointer_delta(eden_space()->end(), eden_space()->bottom(), sizeof(char))); 247 log_trace(gc, ergo)(" from: [" PTR_FORMAT ".." PTR_FORMAT ") " 248 SIZE_FORMAT, 249 p2i(from_space()->bottom()), 250 p2i(from_space()->end()), 251 pointer_delta(from_space()->end(), from_space()->bottom(), sizeof(char))); 252 log_trace(gc, ergo)(" to: [" PTR_FORMAT ".." PTR_FORMAT ") " 253 SIZE_FORMAT, 254 p2i(to_space()->bottom()), 255 p2i(to_space()->end()), 256 pointer_delta( to_space()->end(), to_space()->bottom(), sizeof(char))); 257 258 // There's nothing to do if the new sizes are the same as the current 259 if (requested_survivor_size == to_space()->capacity_in_bytes() && 260 requested_survivor_size == from_space()->capacity_in_bytes() && 261 requested_eden_size == eden_space()->capacity_in_bytes()) { 262 log_trace(gc, ergo)(" capacities are the right sizes, returning"); 263 return; 264 } 265 266 char* eden_start = (char*)virtual_space()->low(); 267 char* eden_end = (char*)eden_space()->end(); 268 char* from_start = (char*)from_space()->bottom(); 269 char* from_end = (char*)from_space()->end(); 270 char* to_start = (char*)to_space()->bottom(); 271 char* to_end = (char*)to_space()->end(); 272 273 assert(eden_start < from_start, "Cannot push into from_space"); 274 275 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 276 const bool maintain_minimum = 277 (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size(); 278 279 bool eden_from_to_order = from_start < to_start; 280 // Check whether from space is below to space 281 if (eden_from_to_order) { 282 // Eden, from, to 283 284 log_trace(gc, ergo)(" Eden, from, to:"); 285 286 // Set eden 287 // "requested_eden_size" is a goal for the size of eden 288 // and may not be attainable. "eden_size" below is 289 // calculated based on the location of from-space and 290 // the goal for the size of eden. from-space is 291 // fixed in place because it contains live data. 292 // The calculation is done this way to avoid 32bit 293 // overflow (i.e., eden_start + requested_eden_size 294 // may too large for representation in 32bits). 295 size_t eden_size; 296 if (maintain_minimum) { 297 // Only make eden larger than the requested size if 298 // the minimum size of the generation has to be maintained. 299 // This could be done in general but policy at a higher 300 // level is determining a requested size for eden and that 301 // should be honored unless there is a fundamental reason. 302 eden_size = pointer_delta(from_start, 303 eden_start, 304 sizeof(char)); 305 } else { 306 eden_size = MIN2(requested_eden_size, 307 pointer_delta(from_start, eden_start, sizeof(char))); 308 } 309 310 eden_end = eden_start + eden_size; 311 assert(eden_end >= eden_start, "addition overflowed"); 312 313 // To may resize into from space as long as it is clear of live data. 314 // From space must remain page aligned, though, so we need to do some 315 // extra calculations. 316 317 // First calculate an optimal to-space 318 to_end = (char*)virtual_space()->high(); 319 to_start = (char*)pointer_delta(to_end, 320 (char*)requested_survivor_size, 321 sizeof(char)); 322 323 // Does the optimal to-space overlap from-space? 324 if (to_start < (char*)from_space()->end()) { 325 // Calculate the minimum offset possible for from_end 326 size_t from_size = 327 pointer_delta(from_space()->top(), from_start, sizeof(char)); 328 329 // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME! 330 if (from_size == 0) { 331 from_size = SpaceAlignment; 332 } else { 333 from_size = align_up(from_size, SpaceAlignment); 334 } 335 336 from_end = from_start + from_size; 337 assert(from_end > from_start, "addition overflow or from_size problem"); 338 339 guarantee(from_end <= (char*)from_space()->end(), 340 "from_end moved to the right"); 341 342 // Now update to_start with the new from_end 343 to_start = MAX2(from_end, to_start); 344 } 345 346 guarantee(to_start != to_end, "to space is zero sized"); 347 348 log_trace(gc, ergo)(" [eden_start .. eden_end): " 349 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 350 p2i(eden_start), 351 p2i(eden_end), 352 pointer_delta(eden_end, eden_start, sizeof(char))); 353 log_trace(gc, ergo)(" [from_start .. from_end): " 354 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 355 p2i(from_start), 356 p2i(from_end), 357 pointer_delta(from_end, from_start, sizeof(char))); 358 log_trace(gc, ergo)(" [ to_start .. to_end): " 359 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 360 p2i(to_start), 361 p2i(to_end), 362 pointer_delta( to_end, to_start, sizeof(char))); 363 } else { 364 // Eden, to, from 365 log_trace(gc, ergo)(" Eden, to, from:"); 366 367 // To space gets priority over eden resizing. Note that we position 368 // to space as if we were able to resize from space, even though from 369 // space is not modified. 370 // Giving eden priority was tried and gave poorer performance. 371 to_end = (char*)pointer_delta(virtual_space()->high(), 372 (char*)requested_survivor_size, 373 sizeof(char)); 374 to_end = MIN2(to_end, from_start); 375 to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 376 sizeof(char)); 377 // if the space sizes are to be increased by several times then 378 // 'to_start' will point beyond the young generation. In this case 379 // 'to_start' should be adjusted. 380 to_start = MAX2(to_start, eden_start + SpaceAlignment); 381 382 // Compute how big eden can be, then adjust end. 383 // See comments above on calculating eden_end. 384 size_t eden_size; 385 if (maintain_minimum) { 386 eden_size = pointer_delta(to_start, eden_start, sizeof(char)); 387 } else { 388 eden_size = MIN2(requested_eden_size, 389 pointer_delta(to_start, eden_start, sizeof(char))); 390 } 391 eden_end = eden_start + eden_size; 392 assert(eden_end >= eden_start, "addition overflowed"); 393 394 // Don't let eden shrink down to 0 or less. 395 eden_end = MAX2(eden_end, eden_start + SpaceAlignment); 396 to_start = MAX2(to_start, eden_end); 397 398 log_trace(gc, ergo)(" [eden_start .. eden_end): " 399 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 400 p2i(eden_start), 401 p2i(eden_end), 402 pointer_delta(eden_end, eden_start, sizeof(char))); 403 log_trace(gc, ergo)(" [ to_start .. to_end): " 404 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 405 p2i(to_start), 406 p2i(to_end), 407 pointer_delta( to_end, to_start, sizeof(char))); 408 log_trace(gc, ergo)(" [from_start .. from_end): " 409 "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 410 p2i(from_start), 411 p2i(from_end), 412 pointer_delta(from_end, from_start, sizeof(char))); 413 } 414 415 416 guarantee((HeapWord*)from_start <= from_space()->bottom(), 417 "from start moved to the right"); 418 guarantee((HeapWord*)from_end >= from_space()->top(), 419 "from end moved into live data"); 420 assert(is_object_aligned(eden_start), "checking alignment"); 421 assert(is_object_aligned(from_start), "checking alignment"); 422 assert(is_object_aligned(to_start), "checking alignment"); 423 424 MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end); 425 MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); 426 MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end); 427 428 // Let's make sure the call to initialize doesn't reset "top"! 429 DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();) 430 431 // For logging block below 432 size_t old_from = from_space()->capacity_in_bytes(); 433 size_t old_to = to_space()->capacity_in_bytes(); 434 435 if (ZapUnusedHeapArea) { 436 // NUMA is a special case because a numa space is not mangled 437 // in order to not prematurely bind its address to memory to 438 // the wrong memory (i.e., don't want the GC thread to first 439 // touch the memory). The survivor spaces are not numa 440 // spaces and are mangled. 441 if (UseNUMA) { 442 if (eden_from_to_order) { 443 mangle_survivors(from_space(), fromMR, to_space(), toMR); 444 } else { 445 mangle_survivors(to_space(), toMR, from_space(), fromMR); 446 } 447 } 448 449 // If not mangling the spaces, do some checking to verify that 450 // the spaces are already mangled. 451 // The spaces should be correctly mangled at this point so 452 // do some checking here. Note that they are not being mangled 453 // in the calls to initialize(). 454 // Must check mangling before the spaces are reshaped. Otherwise, 455 // the bottom or end of one space may have moved into an area 456 // covered by another space and a failure of the check may 457 // not correctly indicate which space is not properly mangled. 458 459 HeapWord* limit = (HeapWord*) virtual_space()->high(); 460 eden_space()->check_mangled_unused_area(limit); 461 from_space()->check_mangled_unused_area(limit); 462 to_space()->check_mangled_unused_area(limit); 463 } 464 // When an existing space is being initialized, it is not 465 // mangled because the space has been previously mangled. 466 eden_space()->initialize(edenMR, 467 SpaceDecorator::Clear, 468 SpaceDecorator::DontMangle); 469 to_space()->initialize(toMR, 470 SpaceDecorator::Clear, 471 SpaceDecorator::DontMangle); 472 from_space()->initialize(fromMR, 473 SpaceDecorator::DontClear, 474 SpaceDecorator::DontMangle); 475 476 PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 477 478 assert(from_space()->top() == old_from_top, "from top changed!"); 479 480 log_trace(gc, ergo)("AdaptiveSizePolicy::survivor space sizes: " 481 "collection: %d " 482 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> " 483 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ", 484 ParallelScavengeHeap::heap()->total_collections(), 485 old_from, old_to, 486 from_space()->capacity_in_bytes(), 487 to_space()->capacity_in_bytes()); 488 489 space_invariants(); 490 } 491 void ASPSYoungGen::reset_after_change() { 492 assert_locked_or_safepoint(Heap_lock); 493 494 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 495 (HeapWord*)virtual_space()->high_boundary()); 496 PSScavenge::set_subject_to_discovery_span(_reserved); 497 498 HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low(); 499 HeapWord* eden_bottom = eden_space()->bottom(); 500 if (new_eden_bottom != eden_bottom) { 501 MemRegion eden_mr(new_eden_bottom, eden_space()->end()); 502 eden_space()->initialize(eden_mr, 503 SpaceDecorator::Clear, 504 SpaceDecorator::Mangle); 505 PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 506 } 507 MemRegion cmr((HeapWord*)virtual_space()->low(), 508 (HeapWord*)virtual_space()->high()); 509 ParallelScavengeHeap::heap()->barrier_set()->card_table()->resize_covered_region(cmr); 510 511 space_invariants(); 512 }