2280N/A/*
3681N/A * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2280N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2280N/A *
2280N/A * This code is free software; you can redistribute it and/or modify it
2280N/A * under the terms of the GNU General Public License version 2 only, as
2280N/A * published by the Free Software Foundation.
2280N/A *
2280N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2280N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2280N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2280N/A * version 2 for more details (a copy is included in the LICENSE file that
2280N/A * accompanied this code).
2280N/A *
2280N/A * You should have received a copy of the GNU General Public License version
2280N/A * 2 along with this work; if not, write to the Free Software Foundation,
2280N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2280N/A *
2280N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2280N/A * or visit www.oracle.com if you need additional information or have any
2280N/A * questions.
2280N/A *
2280N/A */
2280N/A
2280N/A#include "precompiled.hpp"
2280N/A#include "gc_implementation/g1/g1AllocRegion.inline.hpp"
2280N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
2280N/A
2280N/AG1CollectedHeap* G1AllocRegion::_g1h = NULL;
2280N/AHeapRegion* G1AllocRegion::_dummy_region = NULL;
2280N/A
2280N/Avoid G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
2280N/A assert(_dummy_region == NULL, "should be set once");
2280N/A assert(dummy_region != NULL, "pre-condition");
2280N/A assert(dummy_region->free() == 0, "pre-condition");
2280N/A
2280N/A // Make sure that any allocation attempt on this region will fail
2280N/A // and will not trigger any asserts.
2280N/A assert(allocate(dummy_region, 1, false) == NULL, "should fail");
2280N/A assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
2280N/A assert(allocate(dummy_region, 1, true) == NULL, "should fail");
2280N/A assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
2280N/A
2280N/A _g1h = g1h;
2280N/A _dummy_region = dummy_region;
2280N/A}
2280N/A
2280N/Avoid G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
2280N/A bool bot_updates) {
2280N/A assert(alloc_region != NULL && alloc_region != _dummy_region,
2280N/A "pre-condition");
2280N/A
2280N/A // Other threads might still be trying to allocate using a CAS out
2280N/A // of the region we are trying to retire, as they can do so without
2280N/A // holding the lock. So, we first have to make sure that noone else
2280N/A // can allocate out of it by doing a maximal allocation. Even if our
2280N/A // CAS attempt fails a few times, we'll succeed sooner or later
2280N/A // given that failed CAS attempts mean that the region is getting
2280N/A // closed to being full.
2280N/A size_t free_word_size = alloc_region->free() / HeapWordSize;
2280N/A
2280N/A // This is the minimum free chunk we can turn into a dummy
2280N/A // object. If the free space falls below this, then noone can
2280N/A // allocate in this region anyway (all allocation requests will be
2280N/A // of a size larger than this) so we won't have to perform the dummy
2280N/A // allocation.
2280N/A size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
2280N/A
2280N/A while (free_word_size >= min_word_size_to_fill) {
2280N/A HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
2280N/A if (dummy != NULL) {
2280N/A // If the allocation was successful we should fill in the space.
2280N/A CollectedHeap::fill_with_object(dummy, free_word_size);
2280N/A alloc_region->set_pre_dummy_top(dummy);
2280N/A break;
2280N/A }
2280N/A
2280N/A free_word_size = alloc_region->free() / HeapWordSize;
2280N/A // It's also possible that someone else beats us to the
2280N/A // allocation and they fill up the region. In that case, we can
2280N/A // just get out of the loop.
2280N/A }
2280N/A assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
2280N/A "post-condition");
2280N/A}
2280N/A
2280N/Avoid G1AllocRegion::retire(bool fill_up) {
2280N/A assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));
2280N/A
2280N/A trace("retiring");
2280N/A HeapRegion* alloc_region = _alloc_region;
2280N/A if (alloc_region != _dummy_region) {
2280N/A // We never have to check whether the active region is empty or not,
2280N/A // and potentially free it if it is, given that it's guaranteed that
2280N/A // it will never be empty.
2280N/A assert(!alloc_region->is_empty(),
2280N/A ar_ext_msg(this, "the alloc region should never be empty"));
2280N/A
2280N/A if (fill_up) {
2280N/A fill_up_remaining_space(alloc_region, _bot_updates);
2280N/A }
2280N/A
2280N/A assert(alloc_region->used() >= _used_bytes_before,
2280N/A ar_ext_msg(this, "invariant"));
2280N/A size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
2280N/A retire_region(alloc_region, allocated_bytes);
2280N/A _used_bytes_before = 0;
2280N/A _alloc_region = _dummy_region;
2280N/A }
2280N/A trace("retired");
2280N/A}
2280N/A
2280N/AHeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
2280N/A bool force) {
2280N/A assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));
2280N/A assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));
2280N/A
2280N/A trace("attempting region allocation");
2280N/A HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
2280N/A if (new_alloc_region != NULL) {
2280N/A new_alloc_region->reset_pre_dummy_top();
2280N/A // Need to do this before the allocation
2280N/A _used_bytes_before = new_alloc_region->used();
2280N/A HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
2280N/A assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));
2280N/A
2280N/A OrderAccess::storestore();
2280N/A // Note that we first perform the allocation and then we store the
2280N/A // region in _alloc_region. This is the reason why an active region
2280N/A // can never be empty.
2280N/A _alloc_region = new_alloc_region;
2655N/A _count += 1;
2280N/A trace("region allocation successful");
2280N/A return result;
2280N/A } else {
2280N/A trace("region allocation failed");
2280N/A return NULL;
2280N/A }
2280N/A ShouldNotReachHere();
2280N/A}
2280N/A
2280N/Avoid G1AllocRegion::fill_in_ext_msg(ar_ext_msg* msg, const char* message) {
3681N/A msg->append("[%s] %s c: %u b: %s r: "PTR_FORMAT" u: "SIZE_FORMAT,
2655N/A _name, message, _count, BOOL_TO_STR(_bot_updates),
2280N/A _alloc_region, _used_bytes_before);
2280N/A}
2280N/A
2280N/Avoid G1AllocRegion::init() {
2280N/A trace("initializing");
2280N/A assert(_alloc_region == NULL && _used_bytes_before == 0,
2280N/A ar_ext_msg(this, "pre-condition"));
2655N/A assert(_dummy_region != NULL, ar_ext_msg(this, "should have been set"));
2280N/A _alloc_region = _dummy_region;
2655N/A _count = 0;
2280N/A trace("initialized");
2280N/A}
2280N/A
2655N/Avoid G1AllocRegion::set(HeapRegion* alloc_region) {
2655N/A trace("setting");
2655N/A // We explicitly check that the region is not empty to make sure we
2655N/A // maintain the "the alloc region cannot be empty" invariant.
2655N/A assert(alloc_region != NULL && !alloc_region->is_empty(),
2655N/A ar_ext_msg(this, "pre-condition"));
2655N/A assert(_alloc_region == _dummy_region &&
2655N/A _used_bytes_before == 0 && _count == 0,
2655N/A ar_ext_msg(this, "pre-condition"));
2655N/A
2655N/A _used_bytes_before = alloc_region->used();
2655N/A _alloc_region = alloc_region;
2655N/A _count += 1;
2655N/A trace("set");
2655N/A}
2655N/A
2280N/AHeapRegion* G1AllocRegion::release() {
2280N/A trace("releasing");
2280N/A HeapRegion* alloc_region = _alloc_region;
2280N/A retire(false /* fill_up */);
2655N/A assert(_alloc_region == _dummy_region,
2655N/A ar_ext_msg(this, "post-condition of retire()"));
2280N/A _alloc_region = NULL;
2280N/A trace("released");
2280N/A return (alloc_region == _dummy_region) ? NULL : alloc_region;
2280N/A}
2280N/A
2280N/A#if G1_ALLOC_REGION_TRACING
2280N/Avoid G1AllocRegion::trace(const char* str, size_t word_size, HeapWord* result) {
2280N/A // All the calls to trace that set either just the size or the size
2280N/A // and the result are considered part of level 2 tracing and are
2280N/A // skipped during level 1 tracing.
2280N/A if ((word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
2280N/A const size_t buffer_length = 128;
2280N/A char hr_buffer[buffer_length];
2280N/A char rest_buffer[buffer_length];
2280N/A
2280N/A HeapRegion* alloc_region = _alloc_region;
2280N/A if (alloc_region == NULL) {
2280N/A jio_snprintf(hr_buffer, buffer_length, "NULL");
2280N/A } else if (alloc_region == _dummy_region) {
2280N/A jio_snprintf(hr_buffer, buffer_length, "DUMMY");
2280N/A } else {
2280N/A jio_snprintf(hr_buffer, buffer_length,
2280N/A HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));
2280N/A }
2280N/A
2280N/A if (G1_ALLOC_REGION_TRACING > 1) {
2280N/A if (result != NULL) {
2280N/A jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT" "PTR_FORMAT,
2280N/A word_size, result);
2280N/A } else if (word_size != 0) {
2280N/A jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT, word_size);
2280N/A } else {
2280N/A jio_snprintf(rest_buffer, buffer_length, "");
2280N/A }
2280N/A } else {
2280N/A jio_snprintf(rest_buffer, buffer_length, "");
2280N/A }
2280N/A
3681N/A tty->print_cr("[%s] %u %s : %s %s",
2655N/A _name, _count, hr_buffer, str, rest_buffer);
2280N/A }
2280N/A}
2280N/A#endif // G1_ALLOC_REGION_TRACING
2280N/A
2280N/AG1AllocRegion::G1AllocRegion(const char* name,
2280N/A bool bot_updates)
2280N/A : _name(name), _bot_updates(bot_updates),
2655N/A _alloc_region(NULL), _count(0), _used_bytes_before(0) { }
2280N/A