Path: blob/master/src/hotspot/share/gc/g1/g1AllocRegion.cpp
40957 views
/*1* Copyright (c) 2011, 2021, 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/g1/g1AllocRegion.inline.hpp"26#include "gc/g1/g1EvacStats.inline.hpp"27#include "gc/g1/g1CollectedHeap.inline.hpp"28#include "gc/shared/tlab_globals.hpp"29#include "logging/log.hpp"30#include "logging/logStream.hpp"31#include "memory/resourceArea.hpp"32#include "runtime/orderAccess.hpp"33#include "utilities/align.hpp"3435G1CollectedHeap* G1AllocRegion::_g1h = NULL;36HeapRegion* G1AllocRegion::_dummy_region = NULL;3738void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {39assert(_dummy_region == NULL, "should be set once");40assert(dummy_region != NULL, "pre-condition");41assert(dummy_region->free() == 0, "pre-condition");4243// Make sure that any allocation attempt on this region will fail44// and will not trigger any asserts.45assert(dummy_region->allocate_no_bot_updates(1) == NULL, "should fail");46assert(dummy_region->allocate(1) == NULL, "should fail");47DEBUG_ONLY(size_t assert_tmp);48assert(dummy_region->par_allocate_no_bot_updates(1, 1, &assert_tmp) == NULL, "should fail");49assert(dummy_region->par_allocate(1, 1, &assert_tmp) == NULL, "should fail");5051_g1h = g1h;52_dummy_region = dummy_region;53}5455size_t G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region) {56assert(alloc_region != NULL && alloc_region != _dummy_region,57"pre-condition");58size_t result = 0;5960// Other threads might still be trying to allocate using a CAS out61// of the region we are trying to retire, as they can do so without62// holding the lock. So, we first have to make sure that noone else63// can allocate out of it by doing a maximal allocation. Even if our64// CAS attempt fails a few times, we'll succeed sooner or later65// given that failed CAS attempts mean that the region is getting66// closed to being full.67size_t free_word_size = alloc_region->free() / HeapWordSize;6869// This is the minimum free chunk we can turn into a dummy70// object. If the free space falls below this, then noone can71// allocate in this region anyway (all allocation requests will be72// of a size larger than this) so we won't have to perform the dummy73// allocation.74size_t min_word_size_to_fill = CollectedHeap::min_fill_size();7576while (free_word_size >= min_word_size_to_fill) {77HeapWord* dummy = par_allocate(alloc_region, free_word_size);78if (dummy != NULL) {79// If the allocation was successful we should fill in the space.80CollectedHeap::fill_with_object(dummy, free_word_size);81alloc_region->set_pre_dummy_top(dummy);82result += free_word_size * HeapWordSize;83break;84}8586free_word_size = alloc_region->free() / HeapWordSize;87// It's also possible that someone else beats us to the88// allocation and they fill up the region. In that case, we can89// just get out of the loop.90}91result += alloc_region->free();9293assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,94"post-condition");95return result;96}9798size_t G1AllocRegion::retire_internal(HeapRegion* alloc_region, bool fill_up) {99// We never have to check whether the active region is empty or not,100// and potentially free it if it is, given that it's guaranteed that101// it will never be empty.102size_t waste = 0;103assert_alloc_region(!alloc_region->is_empty(),104"the alloc region should never be empty");105106if (fill_up) {107waste = fill_up_remaining_space(alloc_region);108}109110assert_alloc_region(alloc_region->used() >= _used_bytes_before, "invariant");111size_t allocated_bytes = alloc_region->used() - _used_bytes_before;112retire_region(alloc_region, allocated_bytes);113_used_bytes_before = 0;114115return waste;116}117118size_t G1AllocRegion::retire(bool fill_up) {119assert_alloc_region(_alloc_region != NULL, "not initialized properly");120121size_t waste = 0;122123trace("retiring");124HeapRegion* alloc_region = _alloc_region;125if (alloc_region != _dummy_region) {126waste = retire_internal(alloc_region, fill_up);127reset_alloc_region();128}129trace("retired");130131return waste;132}133134HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,135bool force) {136assert_alloc_region(_alloc_region == _dummy_region, "pre-condition");137assert_alloc_region(_used_bytes_before == 0, "pre-condition");138139trace("attempting region allocation");140HeapRegion* new_alloc_region = allocate_new_region(word_size, force);141if (new_alloc_region != NULL) {142new_alloc_region->reset_pre_dummy_top();143// Need to do this before the allocation144_used_bytes_before = new_alloc_region->used();145HeapWord* result = allocate(new_alloc_region, word_size);146assert_alloc_region(result != NULL, "the allocation should succeeded");147148OrderAccess::storestore();149// Note that we first perform the allocation and then we store the150// region in _alloc_region. This is the reason why an active region151// can never be empty.152update_alloc_region(new_alloc_region);153trace("region allocation successful");154return result;155} else {156trace("region allocation failed");157return NULL;158}159ShouldNotReachHere();160}161162void G1AllocRegion::init() {163trace("initializing");164assert_alloc_region(_alloc_region == NULL && _used_bytes_before == 0, "pre-condition");165assert_alloc_region(_dummy_region != NULL, "should have been set");166_alloc_region = _dummy_region;167_count = 0;168trace("initialized");169}170171void G1AllocRegion::set(HeapRegion* alloc_region) {172trace("setting");173// We explicitly check that the region is not empty to make sure we174// maintain the "the alloc region cannot be empty" invariant.175assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");176assert_alloc_region(_alloc_region == _dummy_region &&177_used_bytes_before == 0 && _count == 0,178"pre-condition");179180_used_bytes_before = alloc_region->used();181_alloc_region = alloc_region;182_count += 1;183trace("set");184}185186void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {187trace("update");188// We explicitly check that the region is not empty to make sure we189// maintain the "the alloc region cannot be empty" invariant.190assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");191192_alloc_region = alloc_region;193_count += 1;194trace("updated");195}196197HeapRegion* G1AllocRegion::release() {198trace("releasing");199HeapRegion* alloc_region = _alloc_region;200retire(false /* fill_up */);201assert_alloc_region(_alloc_region == _dummy_region, "post-condition of retire()");202_alloc_region = NULL;203trace("released");204return (alloc_region == _dummy_region) ? NULL : alloc_region;205}206207#ifndef PRODUCT208void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {209// All the calls to trace that set either just the size or the size210// and the result are considered part of detailed tracing and are211// skipped during other tracing.212213Log(gc, alloc, region) log;214215if (!log.is_debug()) {216return;217}218219bool detailed_info = log.is_trace();220221if ((actual_word_size == 0 && result == NULL) || detailed_info) {222ResourceMark rm;223LogStream ls_trace(log.trace());224LogStream ls_debug(log.debug());225outputStream* out = detailed_info ? &ls_trace : &ls_debug;226227out->print("%s: %u ", _name, _count);228229if (_alloc_region == NULL) {230out->print("NULL");231} else if (_alloc_region == _dummy_region) {232out->print("DUMMY");233} else {234out->print(HR_FORMAT, HR_FORMAT_PARAMS(_alloc_region));235}236237out->print(" : %s", str);238239if (detailed_info) {240if (result != NULL) {241out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT " actual " SIZE_FORMAT " " PTR_FORMAT,242min_word_size, desired_word_size, actual_word_size, p2i(result));243} else if (min_word_size != 0) {244out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT, min_word_size, desired_word_size);245}246}247out->cr();248}249}250#endif // PRODUCT251252G1AllocRegion::G1AllocRegion(const char* name,253bool bot_updates,254uint node_index)255: _alloc_region(NULL),256_count(0),257_used_bytes_before(0),258_bot_updates(bot_updates),259_name(name),260_node_index(node_index)261{ }262263HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size,264bool force) {265return _g1h->new_mutator_alloc_region(word_size, force, _node_index);266}267268void MutatorAllocRegion::retire_region(HeapRegion* alloc_region,269size_t allocated_bytes) {270_g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes);271}272273void MutatorAllocRegion::init() {274assert(_retained_alloc_region == NULL, "Pre-condition");275G1AllocRegion::init();276_wasted_bytes = 0;277}278279bool MutatorAllocRegion::should_retain(HeapRegion* region) {280size_t free_bytes = region->free();281if (free_bytes < MinTLABSize) {282return false;283}284285if (_retained_alloc_region != NULL &&286free_bytes < _retained_alloc_region->free()) {287return false;288}289290return true;291}292293size_t MutatorAllocRegion::retire(bool fill_up) {294size_t waste = 0;295trace("retiring");296HeapRegion* current_region = get();297if (current_region != NULL) {298// Retain the current region if it fits a TLAB and has more299// free than the currently retained region.300if (should_retain(current_region)) {301trace("mutator retained");302if (_retained_alloc_region != NULL) {303waste = retire_internal(_retained_alloc_region, true);304}305_retained_alloc_region = current_region;306} else {307waste = retire_internal(current_region, fill_up);308}309reset_alloc_region();310}311312_wasted_bytes += waste;313trace("retired");314return waste;315}316317size_t MutatorAllocRegion::used_in_alloc_regions() {318size_t used = 0;319HeapRegion* hr = get();320if (hr != NULL) {321used += hr->used();322}323324hr = _retained_alloc_region;325if (hr != NULL) {326used += hr->used();327}328return used;329}330331HeapRegion* MutatorAllocRegion::release() {332HeapRegion* ret = G1AllocRegion::release();333334// The retained alloc region must be retired and this must be335// done after the above call to release the mutator alloc region,336// since it might update the _retained_alloc_region member.337if (_retained_alloc_region != NULL) {338_wasted_bytes += retire_internal(_retained_alloc_region, false);339_retained_alloc_region = NULL;340}341log_debug(gc, alloc, region)("Mutator Allocation stats, regions: %u, wasted size: " SIZE_FORMAT "%s (%4.1f%%)",342count(),343byte_size_in_proper_unit(_wasted_bytes),344proper_unit_for_byte_size(_wasted_bytes),345percent_of(_wasted_bytes, count() * HeapRegion::GrainBytes));346return ret;347}348349HeapRegion* G1GCAllocRegion::allocate_new_region(size_t word_size,350bool force) {351assert(!force, "not supported for GC alloc regions");352return _g1h->new_gc_alloc_region(word_size, _purpose, _node_index);353}354355void G1GCAllocRegion::retire_region(HeapRegion* alloc_region,356size_t allocated_bytes) {357_g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, _purpose);358}359360size_t G1GCAllocRegion::retire(bool fill_up) {361HeapRegion* retired = get();362size_t end_waste = G1AllocRegion::retire(fill_up);363// Do not count retirement of the dummy allocation region.364if (retired != NULL) {365_stats->add_region_end_waste(end_waste / HeapWordSize);366}367return end_waste;368}369370HeapRegion* OldGCAllocRegion::release() {371HeapRegion* cur = get();372if (cur != NULL) {373// Determine how far we are from the next card boundary. If it is smaller than374// the minimum object size we can allocate into, expand into the next card.375HeapWord* top = cur->top();376HeapWord* aligned_top = align_up(top, BOTConstants::N_bytes);377378size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize);379380if (to_allocate_words != 0) {381// We are not at a card boundary. Fill up, possibly into the next, taking the382// end of the region and the minimum object size into account.383to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize),384MAX2(to_allocate_words, G1CollectedHeap::min_fill_size()));385386// Skip allocation if there is not enough space to allocate even the smallest387// possible object. In this case this region will not be retained, so the388// original problem cannot occur.389if (to_allocate_words >= G1CollectedHeap::min_fill_size()) {390HeapWord* dummy = attempt_allocation(to_allocate_words);391CollectedHeap::fill_with_object(dummy, to_allocate_words);392}393}394}395return G1AllocRegion::release();396}397398399