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