Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/parNew/asParNewGeneration.cpp
38921 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"26#include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"27#include "gc_implementation/parNew/asParNewGeneration.hpp"28#include "gc_implementation/parNew/parNewGeneration.hpp"29#include "gc_implementation/shared/markSweep.inline.hpp"30#include "gc_implementation/shared/spaceDecorator.hpp"31#include "memory/defNewGeneration.inline.hpp"32#include "memory/referencePolicy.hpp"33#include "oops/markOop.inline.hpp"34#include "oops/oop.pcgc.inline.hpp"3536ASParNewGeneration::ASParNewGeneration(ReservedSpace rs,37size_t initial_byte_size,38size_t min_byte_size,39int level) :40ParNewGeneration(rs, initial_byte_size, level),41_min_gen_size(min_byte_size) {}4243const char* ASParNewGeneration::name() const {44return "adaptive size par new generation";45}4647void ASParNewGeneration::adjust_desired_tenuring_threshold() {48assert(UseAdaptiveSizePolicy,49"Should only be used with UseAdaptiveSizePolicy");50}5152void ASParNewGeneration::resize(size_t eden_size, size_t survivor_size) {53// Resize the generation if needed. If the generation resize54// reports false, do not attempt to resize the spaces.55if (resize_generation(eden_size, survivor_size)) {56// Then we lay out the spaces inside the generation57resize_spaces(eden_size, survivor_size);5859space_invariants();6061if (PrintAdaptiveSizePolicy && Verbose) {62gclog_or_tty->print_cr("Young generation size: "63"desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT64" used: " SIZE_FORMAT " capacity: " SIZE_FORMAT65" gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,66eden_size, survivor_size, used(), capacity(),67max_gen_size(), min_gen_size());68}69}70}7172size_t ASParNewGeneration::available_to_min_gen() {73assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant");74return virtual_space()->committed_size() - min_gen_size();75}7677// This method assumes that from-space has live data and that78// any shrinkage of the young gen is limited by location of79// from-space.80size_t ASParNewGeneration::available_to_live() const {81#undef SHRINKS_AT_END_OF_EDEN82#ifdef SHRINKS_AT_END_OF_EDEN83size_t delta_in_survivor = 0;84ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();85const size_t space_alignment = heap->intra_heap_alignment();86const size_t gen_alignment = heap->object_heap_alignment();8788MutableSpace* space_shrinking = NULL;89if (from_space()->end() > to_space()->end()) {90space_shrinking = from_space();91} else {92space_shrinking = to_space();93}9495// Include any space that is committed but not included in96// the survivor spaces.97assert(((HeapWord*)virtual_space()->high()) >= space_shrinking->end(),98"Survivor space beyond high end");99size_t unused_committed = pointer_delta(virtual_space()->high(),100space_shrinking->end(), sizeof(char));101102if (space_shrinking->is_empty()) {103// Don't let the space shrink to 0104assert(space_shrinking->capacity_in_bytes() >= space_alignment,105"Space is too small");106delta_in_survivor = space_shrinking->capacity_in_bytes() - space_alignment;107} else {108delta_in_survivor = pointer_delta(space_shrinking->end(),109space_shrinking->top(),110sizeof(char));111}112113size_t delta_in_bytes = unused_committed + delta_in_survivor;114delta_in_bytes = align_size_down(delta_in_bytes, gen_alignment);115return delta_in_bytes;116#else117// The only space available for shrinking is in to-space if it118// is above from-space.119if (to()->bottom() > from()->bottom()) {120const size_t alignment = os::vm_page_size();121if (to()->capacity() < alignment) {122return 0;123} else {124return to()->capacity() - alignment;125}126} else {127return 0;128}129#endif130}131132// Return the number of bytes available for resizing down the young133// generation. This is the minimum of134// input "bytes"135// bytes to the minimum young gen size136// bytes to the size currently being used + some small extra137size_t ASParNewGeneration::limit_gen_shrink (size_t bytes) {138// Allow shrinkage into the current eden but keep eden large enough139// to maintain the minimum young gen size140bytes = MIN3(bytes, available_to_min_gen(), available_to_live());141return align_size_down(bytes, os::vm_page_size());142}143144// Note that the the alignment used is the OS page size as145// opposed to an alignment associated with the virtual space146// (as is done in the ASPSYoungGen/ASPSOldGen)147bool ASParNewGeneration::resize_generation(size_t eden_size,148size_t survivor_size) {149const size_t alignment = os::vm_page_size();150size_t orig_size = virtual_space()->committed_size();151bool size_changed = false;152153// There used to be this guarantee there.154// guarantee ((eden_size + 2*survivor_size) <= _max_gen_size, "incorrect input arguments");155// Code below forces this requirement. In addition the desired eden156// size and disired survivor sizes are desired goals and may157// exceed the total generation size.158159assert(min_gen_size() <= orig_size && orig_size <= max_gen_size(),160"just checking");161162// Adjust new generation size163const size_t eden_plus_survivors =164align_size_up(eden_size + 2 * survivor_size, alignment);165size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_gen_size()),166min_gen_size());167assert(desired_size <= max_gen_size(), "just checking");168169if (desired_size > orig_size) {170// Grow the generation171size_t change = desired_size - orig_size;172assert(change % alignment == 0, "just checking");173if (expand(change)) {174return false; // Error if we fail to resize!175}176size_changed = true;177} else if (desired_size < orig_size) {178size_t desired_change = orig_size - desired_size;179assert(desired_change % alignment == 0, "just checking");180181desired_change = limit_gen_shrink(desired_change);182183if (desired_change > 0) {184virtual_space()->shrink_by(desired_change);185reset_survivors_after_shrink();186187size_changed = true;188}189} else {190if (Verbose && PrintGC) {191if (orig_size == max_gen_size()) {192gclog_or_tty->print_cr("ASParNew generation size at maximum: "193SIZE_FORMAT "K", orig_size/K);194} else if (orig_size == min_gen_size()) {195gclog_or_tty->print_cr("ASParNew generation size at minium: "196SIZE_FORMAT "K", orig_size/K);197}198}199}200201if (size_changed) {202MemRegion cmr((HeapWord*)virtual_space()->low(),203(HeapWord*)virtual_space()->high());204GenCollectedHeap::heap()->barrier_set()->resize_covered_region(cmr);205206if (Verbose && PrintGC) {207size_t current_size = virtual_space()->committed_size();208gclog_or_tty->print_cr("ASParNew generation size changed: "209SIZE_FORMAT "K->" SIZE_FORMAT "K",210orig_size/K, current_size/K);211}212}213214guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||215virtual_space()->committed_size() == max_gen_size(), "Sanity");216217return true;218}219220void ASParNewGeneration::reset_survivors_after_shrink() {221222GenCollectedHeap* gch = GenCollectedHeap::heap();223HeapWord* new_end = (HeapWord*)virtual_space()->high();224225if (from()->end() > to()->end()) {226assert(new_end >= from()->end(), "Shrinking past from-space");227} else {228assert(new_end >= to()->bottom(), "Shrink was too large");229// Was there a shrink of the survivor space?230if (new_end < to()->end()) {231MemRegion mr(to()->bottom(), new_end);232to()->initialize(mr,233SpaceDecorator::DontClear,234SpaceDecorator::DontMangle);235}236}237}238void ASParNewGeneration::resize_spaces(size_t requested_eden_size,239size_t requested_survivor_size) {240assert(UseAdaptiveSizePolicy, "sanity check");241assert(requested_eden_size > 0 && requested_survivor_size > 0,242"just checking");243CollectedHeap* heap = Universe::heap();244assert(heap->kind() == CollectedHeap::GenCollectedHeap, "Sanity");245246247// We require eden and to space to be empty248if ((!eden()->is_empty()) || (!to()->is_empty())) {249return;250}251252size_t cur_eden_size = eden()->capacity();253254if (PrintAdaptiveSizePolicy && Verbose) {255gclog_or_tty->print_cr("ASParNew::resize_spaces(requested_eden_size: "256SIZE_FORMAT257", requested_survivor_size: " SIZE_FORMAT ")",258requested_eden_size, requested_survivor_size);259gclog_or_tty->print_cr(" eden: [" PTR_FORMAT ".." PTR_FORMAT ") "260SIZE_FORMAT,261p2i(eden()->bottom()),262p2i(eden()->end()),263pointer_delta(eden()->end(),264eden()->bottom(),265sizeof(char)));266gclog_or_tty->print_cr(" from: [" PTR_FORMAT ".." PTR_FORMAT ") "267SIZE_FORMAT,268p2i(from()->bottom()),269p2i(from()->end()),270pointer_delta(from()->end(),271from()->bottom(),272sizeof(char)));273gclog_or_tty->print_cr(" to: [" PTR_FORMAT ".." PTR_FORMAT ") "274SIZE_FORMAT,275p2i(to()->bottom()),276p2i(to()->end()),277pointer_delta( to()->end(),278to()->bottom(),279sizeof(char)));280}281282// There's nothing to do if the new sizes are the same as the current283if (requested_survivor_size == to()->capacity() &&284requested_survivor_size == from()->capacity() &&285requested_eden_size == eden()->capacity()) {286if (PrintAdaptiveSizePolicy && Verbose) {287gclog_or_tty->print_cr(" capacities are the right sizes, returning");288}289return;290}291292char* eden_start = (char*)eden()->bottom();293char* eden_end = (char*)eden()->end();294char* from_start = (char*)from()->bottom();295char* from_end = (char*)from()->end();296char* to_start = (char*)to()->bottom();297char* to_end = (char*)to()->end();298299const size_t alignment = os::vm_page_size();300const bool maintain_minimum =301(requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();302303// Check whether from space is below to space304if (from_start < to_start) {305// Eden, from, to306if (PrintAdaptiveSizePolicy && Verbose) {307gclog_or_tty->print_cr(" Eden, from, to:");308}309310// Set eden311// "requested_eden_size" is a goal for the size of eden312// and may not be attainable. "eden_size" below is313// calculated based on the location of from-space and314// the goal for the size of eden. from-space is315// fixed in place because it contains live data.316// The calculation is done this way to avoid 32bit317// overflow (i.e., eden_start + requested_eden_size318// may too large for representation in 32bits).319size_t eden_size;320if (maintain_minimum) {321// Only make eden larger than the requested size if322// the minimum size of the generation has to be maintained.323// This could be done in general but policy at a higher324// level is determining a requested size for eden and that325// should be honored unless there is a fundamental reason.326eden_size = pointer_delta(from_start,327eden_start,328sizeof(char));329} else {330eden_size = MIN2(requested_eden_size,331pointer_delta(from_start, eden_start, sizeof(char)));332}333334eden_size = align_size_down(eden_size, alignment);335eden_end = eden_start + eden_size;336assert(eden_end >= eden_start, "addition overflowed");337338// To may resize into from space as long as it is clear of live data.339// From space must remain page aligned, though, so we need to do some340// extra calculations.341342// First calculate an optimal to-space343to_end = (char*)virtual_space()->high();344to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,345sizeof(char));346347// Does the optimal to-space overlap from-space?348if (to_start < (char*)from()->end()) {349// Calculate the minimum offset possible for from_end350size_t from_size = pointer_delta(from()->top(), from_start, sizeof(char));351352// Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!353if (from_size == 0) {354from_size = alignment;355} else {356from_size = align_size_up(from_size, alignment);357}358359from_end = from_start + from_size;360assert(from_end > from_start, "addition overflow or from_size problem");361362guarantee(from_end <= (char*)from()->end(), "from_end moved to the right");363364// Now update to_start with the new from_end365to_start = MAX2(from_end, to_start);366} else {367// If shrinking, move to-space down to abut the end of from-space368// so that shrinking will move to-space down. If not shrinking369// to-space is moving up to allow for growth on the next expansion.370if (requested_eden_size <= cur_eden_size) {371to_start = from_end;372if (to_start + requested_survivor_size > to_start) {373to_end = to_start + requested_survivor_size;374}375}376// else leave to_end pointing to the high end of the virtual space.377}378379guarantee(to_start != to_end, "to space is zero sized");380381if (PrintAdaptiveSizePolicy && Verbose) {382gclog_or_tty->print_cr(" [eden_start .. eden_end): "383"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,384p2i(eden_start),385p2i(eden_end),386pointer_delta(eden_end, eden_start, sizeof(char)));387gclog_or_tty->print_cr(" [from_start .. from_end): "388"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,389p2i(from_start),390p2i(from_end),391pointer_delta(from_end, from_start, sizeof(char)));392gclog_or_tty->print_cr(" [ to_start .. to_end): "393"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,394p2i(to_start),395p2i(to_end),396pointer_delta( to_end, to_start, sizeof(char)));397}398} else {399// Eden, to, from400if (PrintAdaptiveSizePolicy && Verbose) {401gclog_or_tty->print_cr(" Eden, to, from:");402}403404// Calculate the to-space boundaries based on405// the start of from-space.406to_end = from_start;407to_start = (char*)pointer_delta(from_start,408(char*)requested_survivor_size,409sizeof(char));410// Calculate the ideal eden boundaries.411// eden_end is already at the bottom of the generation412assert(eden_start == virtual_space()->low(),413"Eden is not starting at the low end of the virtual space");414if (eden_start + requested_eden_size >= eden_start) {415eden_end = eden_start + requested_eden_size;416} else {417eden_end = to_start;418}419420// Does eden intrude into to-space? to-space421// gets priority but eden is not allowed to shrink422// to 0.423if (eden_end > to_start) {424eden_end = to_start;425}426427// Don't let eden shrink down to 0 or less.428eden_end = MAX2(eden_end, eden_start + alignment);429assert(eden_start + alignment >= eden_start, "Overflow");430431size_t eden_size;432if (maintain_minimum) {433// Use all the space available.434eden_end = MAX2(eden_end, to_start);435eden_size = pointer_delta(eden_end, eden_start, sizeof(char));436eden_size = MIN2(eden_size, cur_eden_size);437} else {438eden_size = pointer_delta(eden_end, eden_start, sizeof(char));439}440eden_size = align_size_down(eden_size, alignment);441assert(maintain_minimum || eden_size <= requested_eden_size,442"Eden size is too large");443assert(eden_size >= alignment, "Eden size is too small");444eden_end = eden_start + eden_size;445446// Move to-space down to eden.447if (requested_eden_size < cur_eden_size) {448to_start = eden_end;449if (to_start + requested_survivor_size > to_start) {450to_end = MIN2(from_start, to_start + requested_survivor_size);451} else {452to_end = from_start;453}454}455456// eden_end may have moved so again make sure457// the to-space and eden don't overlap.458to_start = MAX2(eden_end, to_start);459460// from-space461size_t from_used = from()->used();462if (requested_survivor_size > from_used) {463if (from_start + requested_survivor_size >= from_start) {464from_end = from_start + requested_survivor_size;465}466if (from_end > virtual_space()->high()) {467from_end = virtual_space()->high();468}469}470471assert(to_start >= eden_end, "to-space should be above eden");472if (PrintAdaptiveSizePolicy && Verbose) {473gclog_or_tty->print_cr(" [eden_start .. eden_end): "474"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,475p2i(eden_start),476p2i(eden_end),477pointer_delta(eden_end, eden_start, sizeof(char)));478gclog_or_tty->print_cr(" [ to_start .. to_end): "479"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,480p2i(to_start),481p2i(to_end),482pointer_delta( to_end, to_start, sizeof(char)));483gclog_or_tty->print_cr(" [from_start .. from_end): "484"[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,485p2i(from_start),486p2i(from_end),487pointer_delta(from_end, from_start, sizeof(char)));488}489}490491492guarantee((HeapWord*)from_start <= from()->bottom(),493"from start moved to the right");494guarantee((HeapWord*)from_end >= from()->top(),495"from end moved into live data");496assert(is_object_aligned((intptr_t)eden_start), "checking alignment");497assert(is_object_aligned((intptr_t)from_start), "checking alignment");498assert(is_object_aligned((intptr_t)to_start), "checking alignment");499500MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);501MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end);502MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);503504// Let's make sure the call to initialize doesn't reset "top"!505HeapWord* old_from_top = from()->top();506507// For PrintAdaptiveSizePolicy block below508size_t old_from = from()->capacity();509size_t old_to = to()->capacity();510511// If not clearing the spaces, do some checking to verify that512// the spaces are already mangled.513514// Must check mangling before the spaces are reshaped. Otherwise,515// the bottom or end of one space may have moved into another516// a failure of the check may not correctly indicate which space517// is not properly mangled.518if (ZapUnusedHeapArea) {519HeapWord* limit = (HeapWord*) virtual_space()->high();520eden()->check_mangled_unused_area(limit);521from()->check_mangled_unused_area(limit);522to()->check_mangled_unused_area(limit);523}524525// The call to initialize NULL's the next compaction space526eden()->initialize(edenMR,527SpaceDecorator::Clear,528SpaceDecorator::DontMangle);529eden()->set_next_compaction_space(from());530to()->initialize(toMR ,531SpaceDecorator::Clear,532SpaceDecorator::DontMangle);533from()->initialize(fromMR,534SpaceDecorator::DontClear,535SpaceDecorator::DontMangle);536537assert(from()->top() == old_from_top, "from top changed!");538539if (PrintAdaptiveSizePolicy) {540GenCollectedHeap* gch = GenCollectedHeap::heap();541assert(gch->kind() == CollectedHeap::GenCollectedHeap, "Sanity");542543gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "544"collection: %d "545"(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "546"(" SIZE_FORMAT ", " SIZE_FORMAT ") ",547gch->total_collections(),548old_from, old_to,549from()->capacity(),550to()->capacity());551gclog_or_tty->cr();552}553}554555void ASParNewGeneration::compute_new_size() {556GenCollectedHeap* gch = GenCollectedHeap::heap();557assert(gch->kind() == CollectedHeap::GenCollectedHeap,558"not a CMS generational heap");559560561CMSAdaptiveSizePolicy* size_policy =562(CMSAdaptiveSizePolicy*)gch->gen_policy()->size_policy();563assert(size_policy->is_gc_cms_adaptive_size_policy(),564"Wrong type of size policy");565566size_t survived = from()->used();567if (!survivor_overflow()) {568// Keep running averages on how much survived569size_policy->avg_survived()->sample(survived);570} else {571size_t promoted =572(size_t) next_gen()->gc_stats()->avg_promoted()->last_sample();573assert(promoted < gch->capacity(), "Conversion problem?");574size_t survived_guess = survived + promoted;575size_policy->avg_survived()->sample(survived_guess);576}577578size_t survivor_limit = max_survivor_size();579_tenuring_threshold =580size_policy->compute_survivor_space_size_and_threshold(581_survivor_overflow,582_tenuring_threshold,583survivor_limit);584size_policy->avg_young_live()->sample(used());585size_policy->avg_eden_live()->sample(eden()->used());586587size_policy->compute_eden_space_size(eden()->capacity(), max_gen_size());588589resize(size_policy->calculated_eden_size_in_bytes(),590size_policy->calculated_survivor_size_in_bytes());591592if (UsePerfData) {593CMSGCAdaptivePolicyCounters* counters =594(CMSGCAdaptivePolicyCounters*) gch->collector_policy()->counters();595assert(counters->kind() ==596GCPolicyCounters::CMSGCAdaptivePolicyCountersKind,597"Wrong kind of counters");598counters->update_tenuring_threshold(_tenuring_threshold);599counters->update_survivor_overflowed(_survivor_overflow);600counters->update_young_capacity(capacity());601}602}603604605#ifndef PRODUCT606// Changes from PSYoungGen version607// value of "alignment"608void ASParNewGeneration::space_invariants() {609const size_t alignment = os::vm_page_size();610611// Currently, our eden size cannot shrink to zero612guarantee(eden()->capacity() >= alignment, "eden too small");613guarantee(from()->capacity() >= alignment, "from too small");614guarantee(to()->capacity() >= alignment, "to too small");615616// Relationship of spaces to each other617char* eden_start = (char*)eden()->bottom();618char* eden_end = (char*)eden()->end();619char* from_start = (char*)from()->bottom();620char* from_end = (char*)from()->end();621char* to_start = (char*)to()->bottom();622char* to_end = (char*)to()->end();623624guarantee(eden_start >= virtual_space()->low(), "eden bottom");625guarantee(eden_start < eden_end, "eden space consistency");626guarantee(from_start < from_end, "from space consistency");627guarantee(to_start < to_end, "to space consistency");628629// Check whether from space is below to space630if (from_start < to_start) {631// Eden, from, to632guarantee(eden_end <= from_start, "eden/from boundary");633guarantee(from_end <= to_start, "from/to boundary");634guarantee(to_end <= virtual_space()->high(), "to end");635} else {636// Eden, to, from637guarantee(eden_end <= to_start, "eden/to boundary");638guarantee(to_end <= from_start, "to/from boundary");639guarantee(from_end <= virtual_space()->high(), "from end");640}641642// More checks that the virtual space is consistent with the spaces643assert(virtual_space()->committed_size() >=644(eden()->capacity() +645to()->capacity() +646from()->capacity()), "Committed size is inconsistent");647assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(),648"Space invariant");649char* eden_top = (char*)eden()->top();650char* from_top = (char*)from()->top();651char* to_top = (char*)to()->top();652assert(eden_top <= virtual_space()->high(), "eden top");653assert(from_top <= virtual_space()->high(), "from top");654assert(to_top <= virtual_space()->high(), "to top");655}656#endif657658659