Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1AllocRegion.cpp
38920 views
/*1* Copyright (c) 2011, 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/g1/g1AllocRegion.inline.hpp"26#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"27#include "runtime/orderAccess.inline.hpp"2829G1CollectedHeap* G1AllocRegion::_g1h = NULL;30HeapRegion* G1AllocRegion::_dummy_region = NULL;3132void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {33assert(_dummy_region == NULL, "should be set once");34assert(dummy_region != NULL, "pre-condition");35assert(dummy_region->free() == 0, "pre-condition");3637// Make sure that any allocation attempt on this region will fail38// and will not trigger any asserts.39assert(allocate(dummy_region, 1, false) == NULL, "should fail");40assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");41assert(allocate(dummy_region, 1, true) == NULL, "should fail");42assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");4344_g1h = g1h;45_dummy_region = dummy_region;46}4748void G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,49bool bot_updates) {50assert(alloc_region != NULL && alloc_region != _dummy_region,51"pre-condition");5253// Other threads might still be trying to allocate using a CAS out54// of the region we are trying to retire, as they can do so without55// holding the lock. So, we first have to make sure that noone else56// can allocate out of it by doing a maximal allocation. Even if our57// CAS attempt fails a few times, we'll succeed sooner or later58// given that failed CAS attempts mean that the region is getting59// closed to being full.60size_t free_word_size = alloc_region->free() / HeapWordSize;6162// This is the minimum free chunk we can turn into a dummy63// object. If the free space falls below this, then noone can64// allocate in this region anyway (all allocation requests will be65// of a size larger than this) so we won't have to perform the dummy66// allocation.67size_t min_word_size_to_fill = CollectedHeap::min_fill_size();6869while (free_word_size >= min_word_size_to_fill) {70HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);71if (dummy != NULL) {72// If the allocation was successful we should fill in the space.73CollectedHeap::fill_with_object(dummy, free_word_size);74alloc_region->set_pre_dummy_top(dummy);75break;76}7778free_word_size = alloc_region->free() / HeapWordSize;79// It's also possible that someone else beats us to the80// allocation and they fill up the region. In that case, we can81// just get out of the loop.82}83assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,84"post-condition");85}8687void G1AllocRegion::retire(bool fill_up) {88assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));8990trace("retiring");91HeapRegion* alloc_region = _alloc_region;92if (alloc_region != _dummy_region) {93// We never have to check whether the active region is empty or not,94// and potentially free it if it is, given that it's guaranteed that95// it will never be empty.96assert(!alloc_region->is_empty(),97ar_ext_msg(this, "the alloc region should never be empty"));9899if (fill_up) {100fill_up_remaining_space(alloc_region, _bot_updates);101}102103assert(alloc_region->used() >= _used_bytes_before,104ar_ext_msg(this, "invariant"));105size_t allocated_bytes = alloc_region->used() - _used_bytes_before;106retire_region(alloc_region, allocated_bytes);107_used_bytes_before = 0;108_alloc_region = _dummy_region;109}110trace("retired");111}112113HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,114bool force) {115assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));116assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));117118trace("attempting region allocation");119HeapRegion* new_alloc_region = allocate_new_region(word_size, force);120if (new_alloc_region != NULL) {121new_alloc_region->reset_pre_dummy_top();122// Need to do this before the allocation123_used_bytes_before = new_alloc_region->used();124HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);125assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));126127OrderAccess::storestore();128// Note that we first perform the allocation and then we store the129// region in _alloc_region. This is the reason why an active region130// can never be empty.131update_alloc_region(new_alloc_region);132trace("region allocation successful");133return result;134} else {135trace("region allocation failed");136return NULL;137}138ShouldNotReachHere();139}140141void G1AllocRegion::fill_in_ext_msg(ar_ext_msg* msg, const char* message) {142msg->append("[%s] %s c: %u b: %s r: " PTR_FORMAT " u: " SIZE_FORMAT,143_name, message, _count, BOOL_TO_STR(_bot_updates),144p2i(_alloc_region), _used_bytes_before);145}146147void G1AllocRegion::init() {148trace("initializing");149assert(_alloc_region == NULL && _used_bytes_before == 0,150ar_ext_msg(this, "pre-condition"));151assert(_dummy_region != NULL, ar_ext_msg(this, "should have been set"));152_alloc_region = _dummy_region;153_count = 0;154trace("initialized");155}156157void G1AllocRegion::set(HeapRegion* alloc_region) {158trace("setting");159// We explicitly check that the region is not empty to make sure we160// maintain the "the alloc region cannot be empty" invariant.161assert(alloc_region != NULL && !alloc_region->is_empty(),162ar_ext_msg(this, "pre-condition"));163assert(_alloc_region == _dummy_region &&164_used_bytes_before == 0 && _count == 0,165ar_ext_msg(this, "pre-condition"));166167_used_bytes_before = alloc_region->used();168_alloc_region = alloc_region;169_count += 1;170trace("set");171}172173void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {174trace("update");175// We explicitly check that the region is not empty to make sure we176// maintain the "the alloc region cannot be empty" invariant.177assert(alloc_region != NULL && !alloc_region->is_empty(),178ar_ext_msg(this, "pre-condition"));179180_alloc_region = alloc_region;181_alloc_region->set_allocation_context(allocation_context());182_count += 1;183trace("updated");184}185186HeapRegion* G1AllocRegion::release() {187trace("releasing");188HeapRegion* alloc_region = _alloc_region;189retire(false /* fill_up */);190assert(_alloc_region == _dummy_region,191ar_ext_msg(this, "post-condition of retire()"));192_alloc_region = NULL;193trace("released");194return (alloc_region == _dummy_region) ? NULL : alloc_region;195}196197#if G1_ALLOC_REGION_TRACING198void G1AllocRegion::trace(const char* str, size_t word_size, HeapWord* result) {199// All the calls to trace that set either just the size or the size200// and the result are considered part of level 2 tracing and are201// skipped during level 1 tracing.202if ((word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {203const size_t buffer_length = 128;204char hr_buffer[buffer_length];205char rest_buffer[buffer_length];206207HeapRegion* alloc_region = _alloc_region;208if (alloc_region == NULL) {209jio_snprintf(hr_buffer, buffer_length, "NULL");210} else if (alloc_region == _dummy_region) {211jio_snprintf(hr_buffer, buffer_length, "DUMMY");212} else {213jio_snprintf(hr_buffer, buffer_length,214HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));215}216217if (G1_ALLOC_REGION_TRACING > 1) {218if (result != NULL) {219jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT " " PTR_FORMAT,220word_size, result);221} else if (word_size != 0) {222jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT, word_size);223} else {224jio_snprintf(rest_buffer, buffer_length, "");225}226} else {227jio_snprintf(rest_buffer, buffer_length, "");228}229230tty->print_cr("[%s] %u %s : %s %s",231_name, _count, hr_buffer, str, rest_buffer);232}233}234#endif // G1_ALLOC_REGION_TRACING235236G1AllocRegion::G1AllocRegion(const char* name,237bool bot_updates)238: _name(name), _bot_updates(bot_updates),239_alloc_region(NULL), _count(0), _used_bytes_before(0),240_allocation_context(AllocationContext::system()) { }241242243HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size,244bool force) {245return _g1h->new_mutator_alloc_region(word_size, force);246}247248void MutatorAllocRegion::retire_region(HeapRegion* alloc_region,249size_t allocated_bytes) {250_g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes);251}252253HeapRegion* SurvivorGCAllocRegion::allocate_new_region(size_t word_size,254bool force) {255assert(!force, "not supported for GC alloc regions");256return _g1h->new_gc_alloc_region(word_size, count(), InCSetState::Young);257}258259void SurvivorGCAllocRegion::retire_region(HeapRegion* alloc_region,260size_t allocated_bytes) {261_g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, InCSetState::Young);262}263264HeapRegion* OldGCAllocRegion::allocate_new_region(size_t word_size,265bool force) {266assert(!force, "not supported for GC alloc regions");267return _g1h->new_gc_alloc_region(word_size, count(), InCSetState::Old);268}269270void OldGCAllocRegion::retire_region(HeapRegion* alloc_region,271size_t allocated_bytes) {272_g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, InCSetState::Old);273}274275HeapRegion* OldGCAllocRegion::release() {276HeapRegion* cur = get();277if (cur != NULL) {278// Determine how far we are from the next card boundary. If it is smaller than279// the minimum object size we can allocate into, expand into the next card.280HeapWord* top = cur->top();281HeapWord* aligned_top = (HeapWord*)align_ptr_up(top, G1BlockOffsetSharedArray::N_bytes);282283size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize);284285if (to_allocate_words != 0) {286// We are not at a card boundary. Fill up, possibly into the next, taking the287// end of the region and the minimum object size into account.288to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize),289MAX2(to_allocate_words, G1CollectedHeap::min_fill_size()));290291// Skip allocation if there is not enough space to allocate even the smallest292// possible object. In this case this region will not be retained, so the293// original problem cannot occur.294if (to_allocate_words >= G1CollectedHeap::min_fill_size()) {295HeapWord* dummy = attempt_allocation(to_allocate_words, true /* bot_updates */);296CollectedHeap::fill_with_object(dummy, to_allocate_words);297}298}299}300return G1AllocRegion::release();301}302303304305306