duke@435: /* trims@1907: * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_adjoiningGenerations.cpp.incl" duke@435: duke@435: // If boundary moving is being used, create the young gen and old duke@435: // gen with ASPSYoungGen and ASPSOldGen, respectively. Revert to duke@435: // the old behavior otherwise (with PSYoungGen and PSOldGen). duke@435: duke@435: AdjoiningGenerations::AdjoiningGenerations(ReservedSpace old_young_rs, duke@435: size_t init_low_byte_size, duke@435: size_t min_low_byte_size, duke@435: size_t max_low_byte_size, duke@435: size_t init_high_byte_size, duke@435: size_t min_high_byte_size, duke@435: size_t max_high_byte_size, duke@435: size_t alignment) : duke@435: _virtual_spaces(old_young_rs, min_low_byte_size, duke@435: min_high_byte_size, alignment) { duke@435: assert(min_low_byte_size <= init_low_byte_size && duke@435: init_low_byte_size <= max_low_byte_size, "Parameter check"); duke@435: assert(min_high_byte_size <= init_high_byte_size && duke@435: init_high_byte_size <= max_high_byte_size, "Parameter check"); duke@435: // Create the generations differently based on the option to duke@435: // move the boundary. duke@435: if (UseAdaptiveGCBoundary) { duke@435: // Initialize the adjoining virtual spaces. Then pass the duke@435: // a virtual to each generation for initialization of the duke@435: // generation. duke@435: duke@435: // Does the actual creation of the virtual spaces duke@435: _virtual_spaces.initialize(max_low_byte_size, duke@435: init_low_byte_size, duke@435: init_high_byte_size); duke@435: duke@435: // Place the young gen at the high end. Passes in the virtual space. duke@435: _young_gen = new ASPSYoungGen(_virtual_spaces.high(), duke@435: _virtual_spaces.high()->committed_size(), duke@435: min_high_byte_size, duke@435: _virtual_spaces.high_byte_size_limit()); duke@435: duke@435: // Place the old gen at the low end. Passes in the virtual space. duke@435: _old_gen = new ASPSOldGen(_virtual_spaces.low(), duke@435: _virtual_spaces.low()->committed_size(), duke@435: min_low_byte_size, duke@435: _virtual_spaces.low_byte_size_limit(), duke@435: "old", 1); duke@435: duke@435: young_gen()->initialize_work(); duke@435: assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(), duke@435: "Consistency check"); duke@435: assert(old_young_rs.size() >= young_gen()->gen_size_limit(), duke@435: "Consistency check"); duke@435: duke@435: old_gen()->initialize_work("old", 1); duke@435: assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(), duke@435: "Consistency check"); duke@435: assert(old_young_rs.size() >= old_gen()->gen_size_limit(), duke@435: "Consistency check"); duke@435: } else { duke@435: duke@435: // Layout the reserved space for the generations. duke@435: ReservedSpace old_rs = duke@435: virtual_spaces()->reserved_space().first_part(max_low_byte_size); duke@435: ReservedSpace heap_rs = duke@435: virtual_spaces()->reserved_space().last_part(max_low_byte_size); duke@435: ReservedSpace young_rs = heap_rs.first_part(max_high_byte_size); duke@435: assert(young_rs.size() == heap_rs.size(), "Didn't reserve all of the heap"); duke@435: duke@435: // Create the generations. Virtual spaces are not passed in. duke@435: _young_gen = new PSYoungGen(init_high_byte_size, duke@435: min_high_byte_size, duke@435: max_high_byte_size); duke@435: _old_gen = new PSOldGen(init_low_byte_size, duke@435: min_low_byte_size, duke@435: max_low_byte_size, duke@435: "old", 1); duke@435: duke@435: // The virtual spaces are created by the initialization of the gens. duke@435: _young_gen->initialize(young_rs, alignment); duke@435: assert(young_gen()->gen_size_limit() == young_rs.size(), duke@435: "Consistency check"); duke@435: _old_gen->initialize(old_rs, alignment, "old", 1); duke@435: assert(old_gen()->gen_size_limit() == old_rs.size(), "Consistency check"); duke@435: } duke@435: } duke@435: duke@435: size_t AdjoiningGenerations::reserved_byte_size() { duke@435: return virtual_spaces()->reserved_space().size(); duke@435: } duke@435: duke@435: duke@435: // Make checks on the current sizes of the generations and duke@435: // the contraints on the sizes of the generations. Push duke@435: // up the boundary within the contraints. A partial duke@435: // push can occur. duke@435: void AdjoiningGenerations::request_old_gen_expansion(size_t expand_in_bytes) { duke@435: assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); duke@435: duke@435: assert_lock_strong(ExpandHeap_lock); duke@435: assert_locked_or_safepoint(Heap_lock); duke@435: duke@435: // These sizes limit the amount the boundaries can move. Effectively, duke@435: // the generation says how much it is willing to yield to the other duke@435: // generation. duke@435: const size_t young_gen_available = young_gen()->available_for_contraction(); duke@435: const size_t old_gen_available = old_gen()->available_for_expansion(); duke@435: const size_t alignment = virtual_spaces()->alignment(); duke@435: size_t change_in_bytes = MIN3(young_gen_available, duke@435: old_gen_available, duke@435: align_size_up_(expand_in_bytes, alignment)); duke@435: duke@435: if (change_in_bytes == 0) { duke@435: return; duke@435: } duke@435: duke@435: if (TraceAdaptiveGCBoundary) { duke@435: gclog_or_tty->print_cr("Before expansion of old gen with boundary move"); duke@435: gclog_or_tty->print_cr(" Requested change: 0x%x Attempted change: 0x%x", duke@435: expand_in_bytes, change_in_bytes); duke@435: if (!PrintHeapAtGC) { duke@435: Universe::print_on(gclog_or_tty); duke@435: } duke@435: gclog_or_tty->print_cr(" PSOldGen max size: " SIZE_FORMAT "K", duke@435: old_gen()->max_gen_size()/K); duke@435: } duke@435: duke@435: // Move the boundary between the generations up (smaller young gen). duke@435: if (virtual_spaces()->adjust_boundary_up(change_in_bytes)) { duke@435: young_gen()->reset_after_change(); duke@435: old_gen()->reset_after_change(); duke@435: } duke@435: duke@435: // The total reserved for the generations should match the sum duke@435: // of the two even if the boundary is moving. duke@435: assert(reserved_byte_size() == duke@435: old_gen()->max_gen_size() + young_gen()->max_size(), duke@435: "Space is missing"); duke@435: young_gen()->space_invariants(); duke@435: old_gen()->space_invariants(); duke@435: duke@435: if (TraceAdaptiveGCBoundary) { duke@435: gclog_or_tty->print_cr("After expansion of old gen with boundary move"); duke@435: if (!PrintHeapAtGC) { duke@435: Universe::print_on(gclog_or_tty); duke@435: } duke@435: gclog_or_tty->print_cr(" PSOldGen max size: " SIZE_FORMAT "K", duke@435: old_gen()->max_gen_size()/K); duke@435: } duke@435: } duke@435: duke@435: // See comments on request_old_gen_expansion() duke@435: bool AdjoiningGenerations::request_young_gen_expansion(size_t expand_in_bytes) { duke@435: assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); duke@435: duke@435: // If eden is not empty, the boundary can be moved but no advantage duke@435: // can be made of the move since eden cannot be moved. duke@435: if (!young_gen()->eden_space()->is_empty()) { duke@435: return false; duke@435: } duke@435: duke@435: duke@435: bool result = false; duke@435: const size_t young_gen_available = young_gen()->available_for_expansion(); duke@435: const size_t old_gen_available = old_gen()->available_for_contraction(); duke@435: const size_t alignment = virtual_spaces()->alignment(); duke@435: size_t change_in_bytes = MIN3(young_gen_available, duke@435: old_gen_available, duke@435: align_size_up_(expand_in_bytes, alignment)); duke@435: duke@435: if (change_in_bytes == 0) { duke@435: return false; duke@435: } duke@435: duke@435: if (TraceAdaptiveGCBoundary) { duke@435: gclog_or_tty->print_cr("Before expansion of young gen with boundary move"); duke@435: gclog_or_tty->print_cr(" Requested change: 0x%x Attempted change: 0x%x", duke@435: expand_in_bytes, change_in_bytes); duke@435: if (!PrintHeapAtGC) { duke@435: Universe::print_on(gclog_or_tty); duke@435: } duke@435: gclog_or_tty->print_cr(" PSYoungGen max size: " SIZE_FORMAT "K", duke@435: young_gen()->max_size()/K); duke@435: } duke@435: duke@435: // Move the boundary between the generations down (smaller old gen). duke@435: MutexLocker x(ExpandHeap_lock); duke@435: if (virtual_spaces()->adjust_boundary_down(change_in_bytes)) { duke@435: young_gen()->reset_after_change(); duke@435: old_gen()->reset_after_change(); duke@435: result = true; duke@435: } duke@435: duke@435: // The total reserved for the generations should match the sum duke@435: // of the two even if the boundary is moving. duke@435: assert(reserved_byte_size() == duke@435: old_gen()->max_gen_size() + young_gen()->max_size(), duke@435: "Space is missing"); duke@435: young_gen()->space_invariants(); duke@435: old_gen()->space_invariants(); duke@435: duke@435: if (TraceAdaptiveGCBoundary) { duke@435: gclog_or_tty->print_cr("After expansion of young gen with boundary move"); duke@435: if (!PrintHeapAtGC) { duke@435: Universe::print_on(gclog_or_tty); duke@435: } duke@435: gclog_or_tty->print_cr(" PSYoungGen max size: " SIZE_FORMAT "K", duke@435: young_gen()->max_size()/K); duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: // Additional space is needed in the old generation. Try to move the boundary duke@435: // up to meet the need. Moves boundary up only duke@435: void AdjoiningGenerations::adjust_boundary_for_old_gen_needs( duke@435: size_t desired_free_space) { duke@435: assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); duke@435: duke@435: // Stress testing. duke@435: if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 1) { duke@435: MutexLocker x(ExpandHeap_lock); duke@435: request_old_gen_expansion(virtual_spaces()->alignment() * 3 / 2); duke@435: } duke@435: duke@435: // Expand only if the entire generation is already committed. duke@435: if (old_gen()->virtual_space()->uncommitted_size() == 0) { duke@435: if (old_gen()->free_in_bytes() < desired_free_space) { duke@435: MutexLocker x(ExpandHeap_lock); duke@435: request_old_gen_expansion(desired_free_space); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // See comment on adjust_boundary_for_old_gen_needss(). duke@435: // Adjust boundary down only. duke@435: void AdjoiningGenerations::adjust_boundary_for_young_gen_needs(size_t eden_size, duke@435: size_t survivor_size) { duke@435: duke@435: assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check"); duke@435: duke@435: // Stress testing. duke@435: if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 0) { duke@435: request_young_gen_expansion(virtual_spaces()->alignment() * 3 / 2); duke@435: eden_size = young_gen()->eden_space()->capacity_in_bytes(); duke@435: } duke@435: duke@435: // Expand only if the entire generation is already committed. duke@435: if (young_gen()->virtual_space()->uncommitted_size() == 0) { duke@435: size_t desired_size = eden_size + 2 * survivor_size; duke@435: const size_t committed = young_gen()->virtual_space()->committed_size(); duke@435: if (desired_size > committed) { duke@435: request_young_gen_expansion(desired_size - committed); duke@435: } duke@435: } duke@435: }