1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/parallelScavenge/asPSYoungGen.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,469 @@ 1.4 +/* 1.5 + * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_asPSYoungGen.cpp.incl" 1.30 + 1.31 +ASPSYoungGen::ASPSYoungGen(size_t init_byte_size, 1.32 + size_t minimum_byte_size, 1.33 + size_t byte_size_limit) : 1.34 + PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 1.35 + _gen_size_limit(byte_size_limit) { 1.36 +} 1.37 + 1.38 + 1.39 +ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs, 1.40 + size_t init_byte_size, 1.41 + size_t minimum_byte_size, 1.42 + size_t byte_size_limit) : 1.43 + //PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit), 1.44 + PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit), 1.45 + _gen_size_limit(byte_size_limit) { 1.46 + 1.47 + assert(vs->committed_size() == init_byte_size, "Cannot replace with"); 1.48 + 1.49 + _virtual_space = vs; 1.50 +} 1.51 + 1.52 +void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs, 1.53 + size_t alignment) { 1.54 + assert(_init_gen_size != 0, "Should have a finite size"); 1.55 + _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment); 1.56 + if (!_virtual_space->expand_by(_init_gen_size)) { 1.57 + vm_exit_during_initialization("Could not reserve enough space for " 1.58 + "object heap"); 1.59 + } 1.60 +} 1.61 + 1.62 +void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) { 1.63 + initialize_virtual_space(rs, alignment); 1.64 + initialize_work(); 1.65 +} 1.66 + 1.67 +size_t ASPSYoungGen::available_for_expansion() { 1.68 + 1.69 + size_t current_committed_size = virtual_space()->committed_size(); 1.70 + assert((gen_size_limit() >= current_committed_size), 1.71 + "generation size limit is wrong"); 1.72 + ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 1.73 + size_t result = gen_size_limit() - current_committed_size; 1.74 + size_t result_aligned = align_size_down(result, heap->young_gen_alignment()); 1.75 + return result_aligned; 1.76 +} 1.77 + 1.78 +// Return the number of bytes the young gen is willing give up. 1.79 +// 1.80 +// Future implementations could check the survivors and if to_space is in the 1.81 +// right place (below from_space), take a chunk from to_space. 1.82 +size_t ASPSYoungGen::available_for_contraction() { 1.83 + 1.84 + size_t uncommitted_bytes = virtual_space()->uncommitted_size(); 1.85 + if (uncommitted_bytes != 0) { 1.86 + return uncommitted_bytes; 1.87 + } 1.88 + 1.89 + if (eden_space()->is_empty()) { 1.90 + // Respect the minimum size for eden and for the young gen as a whole. 1.91 + ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 1.92 + const size_t eden_alignment = heap->intra_generation_alignment(); 1.93 + const size_t gen_alignment = heap->young_gen_alignment(); 1.94 + 1.95 + assert(eden_space()->capacity_in_bytes() >= eden_alignment, 1.96 + "Alignment is wrong"); 1.97 + size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment; 1.98 + eden_avail = align_size_down(eden_avail, gen_alignment); 1.99 + 1.100 + assert(virtual_space()->committed_size() >= min_gen_size(), 1.101 + "minimum gen size is wrong"); 1.102 + size_t gen_avail = virtual_space()->committed_size() - min_gen_size(); 1.103 + assert(virtual_space()->is_aligned(gen_avail), "not aligned"); 1.104 + 1.105 + const size_t max_contraction = MIN2(eden_avail, gen_avail); 1.106 + // See comment for ASPSOldGen::available_for_contraction() 1.107 + // for reasons the "increment" fraction is used. 1.108 + PSAdaptiveSizePolicy* policy = heap->size_policy(); 1.109 + size_t result = policy->eden_increment_aligned_down(max_contraction); 1.110 + size_t result_aligned = align_size_down(result, gen_alignment); 1.111 + if (PrintAdaptiveSizePolicy && Verbose) { 1.112 + gclog_or_tty->print_cr("ASPSYoungGen::available_for_contraction: %d K", 1.113 + result_aligned/K); 1.114 + gclog_or_tty->print_cr(" max_contraction %d K", max_contraction/K); 1.115 + gclog_or_tty->print_cr(" eden_avail %d K", eden_avail/K); 1.116 + gclog_or_tty->print_cr(" gen_avail %d K", gen_avail/K); 1.117 + } 1.118 + return result_aligned; 1.119 + 1.120 + } 1.121 + 1.122 + return 0; 1.123 +} 1.124 + 1.125 +// The current implementation only considers to the end of eden. 1.126 +// If to_space is below from_space, to_space is not considered. 1.127 +// to_space can be. 1.128 +size_t ASPSYoungGen::available_to_live() { 1.129 + ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 1.130 + const size_t alignment = heap->intra_generation_alignment(); 1.131 + 1.132 + // Include any space that is committed but is not in eden. 1.133 + size_t available = pointer_delta(eden_space()->bottom(), 1.134 + virtual_space()->low(), 1.135 + sizeof(char)); 1.136 + 1.137 + const size_t eden_capacity = eden_space()->capacity_in_bytes(); 1.138 + if (eden_space()->is_empty() && eden_capacity > alignment) { 1.139 + available += eden_capacity - alignment; 1.140 + } 1.141 + return available; 1.142 +} 1.143 + 1.144 +// Similar to PSYoungGen::resize_generation() but 1.145 +// allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size 1.146 +// expands at the low end of the virtual space 1.147 +// moves the boundary between the generations in order to expand 1.148 +// some additional diagnostics 1.149 +// If no additional changes are required, this can be deleted 1.150 +// and the changes factored back into PSYoungGen::resize_generation(). 1.151 +bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) { 1.152 + const size_t alignment = virtual_space()->alignment(); 1.153 + size_t orig_size = virtual_space()->committed_size(); 1.154 + bool size_changed = false; 1.155 + 1.156 + // There used to be a guarantee here that 1.157 + // (eden_size + 2*survivor_size) <= _max_gen_size 1.158 + // This requirement is enforced by the calculation of desired_size 1.159 + // below. It may not be true on entry since the size of the 1.160 + // eden_size is no bounded by the generation size. 1.161 + 1.162 + assert(max_size() == reserved().byte_size(), "max gen size problem?"); 1.163 + assert(min_gen_size() <= orig_size && orig_size <= max_size(), 1.164 + "just checking"); 1.165 + 1.166 + // Adjust new generation size 1.167 + const size_t eden_plus_survivors = 1.168 + align_size_up(eden_size + 2 * survivor_size, alignment); 1.169 + size_t desired_size = MAX2(MIN2(eden_plus_survivors, gen_size_limit()), 1.170 + min_gen_size()); 1.171 + assert(desired_size <= gen_size_limit(), "just checking"); 1.172 + 1.173 + if (desired_size > orig_size) { 1.174 + // Grow the generation 1.175 + size_t change = desired_size - orig_size; 1.176 + if (!virtual_space()->expand_by(change)) { 1.177 + return false; 1.178 + } 1.179 + size_changed = true; 1.180 + } else if (desired_size < orig_size) { 1.181 + size_t desired_change = orig_size - desired_size; 1.182 + 1.183 + // How much is available for shrinking. 1.184 + size_t available_bytes = limit_gen_shrink(desired_change); 1.185 + size_t change = MIN2(desired_change, available_bytes); 1.186 + virtual_space()->shrink_by(change); 1.187 + size_changed = true; 1.188 + } else { 1.189 + if (Verbose && PrintGC) { 1.190 + if (orig_size == gen_size_limit()) { 1.191 + gclog_or_tty->print_cr("ASPSYoung generation size at maximum: " 1.192 + SIZE_FORMAT "K", orig_size/K); 1.193 + } else if (orig_size == min_gen_size()) { 1.194 + gclog_or_tty->print_cr("ASPSYoung generation size at minium: " 1.195 + SIZE_FORMAT "K", orig_size/K); 1.196 + } 1.197 + } 1.198 + } 1.199 + 1.200 + if (size_changed) { 1.201 + reset_after_change(); 1.202 + if (Verbose && PrintGC) { 1.203 + size_t current_size = virtual_space()->committed_size(); 1.204 + gclog_or_tty->print_cr("ASPSYoung generation size changed: " 1.205 + SIZE_FORMAT "K->" SIZE_FORMAT "K", 1.206 + orig_size/K, current_size/K); 1.207 + } 1.208 + } 1.209 + 1.210 + guarantee(eden_plus_survivors <= virtual_space()->committed_size() || 1.211 + virtual_space()->committed_size() == max_size(), "Sanity"); 1.212 + 1.213 + return true; 1.214 +} 1.215 + 1.216 +// Similar to PSYoungGen::resize_spaces() but 1.217 +// eden always starts at the low end of the committed virtual space 1.218 +// current implementation does not allow holes between the spaces 1.219 +// _young_generation_boundary has to be reset because it changes. 1.220 +// so additional verification 1.221 +void ASPSYoungGen::resize_spaces(size_t requested_eden_size, 1.222 + size_t requested_survivor_size) { 1.223 + assert(requested_eden_size > 0 && requested_survivor_size > 0, 1.224 + "just checking"); 1.225 + 1.226 + space_invariants(); 1.227 + 1.228 + // We require eden and to space to be empty 1.229 + if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) { 1.230 + return; 1.231 + } 1.232 + 1.233 + if (PrintAdaptiveSizePolicy && Verbose) { 1.234 + gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: " 1.235 + SIZE_FORMAT 1.236 + ", requested_survivor_size: " SIZE_FORMAT ")", 1.237 + requested_eden_size, requested_survivor_size); 1.238 + gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") " 1.239 + SIZE_FORMAT, 1.240 + eden_space()->bottom(), 1.241 + eden_space()->end(), 1.242 + pointer_delta(eden_space()->end(), 1.243 + eden_space()->bottom(), 1.244 + sizeof(char))); 1.245 + gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") " 1.246 + SIZE_FORMAT, 1.247 + from_space()->bottom(), 1.248 + from_space()->end(), 1.249 + pointer_delta(from_space()->end(), 1.250 + from_space()->bottom(), 1.251 + sizeof(char))); 1.252 + gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") " 1.253 + SIZE_FORMAT, 1.254 + to_space()->bottom(), 1.255 + to_space()->end(), 1.256 + pointer_delta( to_space()->end(), 1.257 + to_space()->bottom(), 1.258 + sizeof(char))); 1.259 + } 1.260 + 1.261 + // There's nothing to do if the new sizes are the same as the current 1.262 + if (requested_survivor_size == to_space()->capacity_in_bytes() && 1.263 + requested_survivor_size == from_space()->capacity_in_bytes() && 1.264 + requested_eden_size == eden_space()->capacity_in_bytes()) { 1.265 + if (PrintAdaptiveSizePolicy && Verbose) { 1.266 + gclog_or_tty->print_cr(" capacities are the right sizes, returning"); 1.267 + } 1.268 + return; 1.269 + } 1.270 + 1.271 + char* eden_start = (char*)virtual_space()->low(); 1.272 + char* eden_end = (char*)eden_space()->end(); 1.273 + char* from_start = (char*)from_space()->bottom(); 1.274 + char* from_end = (char*)from_space()->end(); 1.275 + char* to_start = (char*)to_space()->bottom(); 1.276 + char* to_end = (char*)to_space()->end(); 1.277 + 1.278 + assert(eden_start < from_start, "Cannot push into from_space"); 1.279 + 1.280 + ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 1.281 + const size_t alignment = heap->intra_generation_alignment(); 1.282 + 1.283 + // Check whether from space is below to space 1.284 + if (from_start < to_start) { 1.285 + // Eden, from, to 1.286 + if (PrintAdaptiveSizePolicy && Verbose) { 1.287 + gclog_or_tty->print_cr(" Eden, from, to:"); 1.288 + } 1.289 + 1.290 + // Set eden 1.291 + // Compute how big eden can be, then adjust end. 1.292 + // See comment in PSYoungGen::resize_spaces() on 1.293 + // calculating eden_end. 1.294 + const size_t eden_size = MIN2(requested_eden_size, 1.295 + pointer_delta(from_start, 1.296 + eden_start, 1.297 + sizeof(char))); 1.298 + eden_end = eden_start + eden_size; 1.299 + assert(eden_end >= eden_start, "addition overflowed") 1.300 + 1.301 + // To may resize into from space as long as it is clear of live data. 1.302 + // From space must remain page aligned, though, so we need to do some 1.303 + // extra calculations. 1.304 + 1.305 + // First calculate an optimal to-space 1.306 + to_end = (char*)virtual_space()->high(); 1.307 + to_start = (char*)pointer_delta(to_end, 1.308 + (char*)requested_survivor_size, 1.309 + sizeof(char)); 1.310 + 1.311 + // Does the optimal to-space overlap from-space? 1.312 + if (to_start < (char*)from_space()->end()) { 1.313 + assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 1.314 + 1.315 + // Calculate the minimum offset possible for from_end 1.316 + size_t from_size = 1.317 + pointer_delta(from_space()->top(), from_start, sizeof(char)); 1.318 + 1.319 + // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME! 1.320 + if (from_size == 0) { 1.321 + from_size = alignment; 1.322 + } else { 1.323 + from_size = align_size_up(from_size, alignment); 1.324 + } 1.325 + 1.326 + from_end = from_start + from_size; 1.327 + assert(from_end > from_start, "addition overflow or from_size problem"); 1.328 + 1.329 + guarantee(from_end <= (char*)from_space()->end(), 1.330 + "from_end moved to the right"); 1.331 + 1.332 + // Now update to_start with the new from_end 1.333 + to_start = MAX2(from_end, to_start); 1.334 + } 1.335 + 1.336 + guarantee(to_start != to_end, "to space is zero sized"); 1.337 + 1.338 + if (PrintAdaptiveSizePolicy && Verbose) { 1.339 + gclog_or_tty->print_cr(" [eden_start .. eden_end): " 1.340 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.341 + eden_start, 1.342 + eden_end, 1.343 + pointer_delta(eden_end, eden_start, sizeof(char))); 1.344 + gclog_or_tty->print_cr(" [from_start .. from_end): " 1.345 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.346 + from_start, 1.347 + from_end, 1.348 + pointer_delta(from_end, from_start, sizeof(char))); 1.349 + gclog_or_tty->print_cr(" [ to_start .. to_end): " 1.350 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.351 + to_start, 1.352 + to_end, 1.353 + pointer_delta( to_end, to_start, sizeof(char))); 1.354 + } 1.355 + } else { 1.356 + // Eden, to, from 1.357 + if (PrintAdaptiveSizePolicy && Verbose) { 1.358 + gclog_or_tty->print_cr(" Eden, to, from:"); 1.359 + } 1.360 + 1.361 + // To space gets priority over eden resizing. Note that we position 1.362 + // to space as if we were able to resize from space, even though from 1.363 + // space is not modified. 1.364 + // Giving eden priority was tried and gave poorer performance. 1.365 + to_end = (char*)pointer_delta(virtual_space()->high(), 1.366 + (char*)requested_survivor_size, 1.367 + sizeof(char)); 1.368 + to_end = MIN2(to_end, from_start); 1.369 + to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size, 1.370 + sizeof(char)); 1.371 + // if the space sizes are to be increased by several times then 1.372 + // 'to_start' will point beyond the young generation. In this case 1.373 + // 'to_start' should be adjusted. 1.374 + to_start = MAX2(to_start, eden_start + alignment); 1.375 + 1.376 + // Compute how big eden can be, then adjust end. 1.377 + // See comment in PSYoungGen::resize_spaces() on 1.378 + // calculating eden_end. 1.379 + const size_t eden_size = MIN2(requested_eden_size, 1.380 + pointer_delta(to_start, 1.381 + eden_start, 1.382 + sizeof(char))); 1.383 + eden_end = eden_start + eden_size; 1.384 + assert(eden_end >= eden_start, "addition overflowed") 1.385 + 1.386 + // Don't let eden shrink down to 0 or less. 1.387 + eden_end = MAX2(eden_end, eden_start + alignment); 1.388 + to_start = MAX2(to_start, eden_end); 1.389 + 1.390 + if (PrintAdaptiveSizePolicy && Verbose) { 1.391 + gclog_or_tty->print_cr(" [eden_start .. eden_end): " 1.392 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.393 + eden_start, 1.394 + eden_end, 1.395 + pointer_delta(eden_end, eden_start, sizeof(char))); 1.396 + gclog_or_tty->print_cr(" [ to_start .. to_end): " 1.397 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.398 + to_start, 1.399 + to_end, 1.400 + pointer_delta( to_end, to_start, sizeof(char))); 1.401 + gclog_or_tty->print_cr(" [from_start .. from_end): " 1.402 + "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT, 1.403 + from_start, 1.404 + from_end, 1.405 + pointer_delta(from_end, from_start, sizeof(char))); 1.406 + } 1.407 + } 1.408 + 1.409 + 1.410 + guarantee((HeapWord*)from_start <= from_space()->bottom(), 1.411 + "from start moved to the right"); 1.412 + guarantee((HeapWord*)from_end >= from_space()->top(), 1.413 + "from end moved into live data"); 1.414 + assert(is_object_aligned((intptr_t)eden_start), "checking alignment"); 1.415 + assert(is_object_aligned((intptr_t)from_start), "checking alignment"); 1.416 + assert(is_object_aligned((intptr_t)to_start), "checking alignment"); 1.417 + 1.418 + MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end); 1.419 + MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); 1.420 + MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end); 1.421 + 1.422 + // Let's make sure the call to initialize doesn't reset "top"! 1.423 + DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();) 1.424 + 1.425 + // For PrintAdaptiveSizePolicy block below 1.426 + size_t old_from = from_space()->capacity_in_bytes(); 1.427 + size_t old_to = to_space()->capacity_in_bytes(); 1.428 + 1.429 + eden_space()->initialize(edenMR, true); 1.430 + to_space()->initialize(toMR , true); 1.431 + from_space()->initialize(fromMR, false); // Note, not cleared! 1.432 + PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 1.433 + 1.434 + assert(from_space()->top() == old_from_top, "from top changed!"); 1.435 + 1.436 + if (PrintAdaptiveSizePolicy) { 1.437 + ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); 1.438 + assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); 1.439 + 1.440 + gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: " 1.441 + "collection: %d " 1.442 + "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> " 1.443 + "(" SIZE_FORMAT ", " SIZE_FORMAT ") ", 1.444 + heap->total_collections(), 1.445 + old_from, old_to, 1.446 + from_space()->capacity_in_bytes(), 1.447 + to_space()->capacity_in_bytes()); 1.448 + gclog_or_tty->cr(); 1.449 + } 1.450 + space_invariants(); 1.451 +} 1.452 + 1.453 +void ASPSYoungGen::reset_after_change() { 1.454 + assert_locked_or_safepoint(Heap_lock); 1.455 + 1.456 + _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 1.457 + (HeapWord*)virtual_space()->high_boundary()); 1.458 + PSScavenge::reference_processor()->set_span(_reserved); 1.459 + 1.460 + HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low(); 1.461 + HeapWord* eden_bottom = eden_space()->bottom(); 1.462 + if (new_eden_bottom != eden_bottom) { 1.463 + MemRegion eden_mr(new_eden_bottom, eden_space()->end()); 1.464 + eden_space()->initialize(eden_mr, true); 1.465 + PSScavenge::set_young_generation_boundary(eden_space()->bottom()); 1.466 + } 1.467 + MemRegion cmr((HeapWord*)virtual_space()->low(), 1.468 + (HeapWord*)virtual_space()->high()); 1.469 + Universe::heap()->barrier_set()->resize_covered_region(cmr); 1.470 + 1.471 + space_invariants(); 1.472 +}