0N/A/*
2362N/A * Copyright (c) 2001, 2012, 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#include "precompiled.hpp"
0N/A#include "gc_implementation/g1/heapRegion.hpp"
0N/A#include "gc_implementation/g1/heapRegionSeq.inline.hpp"
0N/A#include "gc_implementation/g1/heapRegionSets.hpp"
0N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
0N/A#include "memory/allocation.hpp"
0N/A
0N/A// Private
0N/A
0N/Auint HeapRegionSeq::find_contiguous_from(uint from, uint num) {
0N/A uint len = length();
0N/A assert(num > 1, "use this only for sequences of length 2 or greater");
0N/A assert(from <= len,
0N/A err_msg("from: %u should be valid and <= than %u", from, len));
0N/A
0N/A uint curr = from;
0N/A uint first = G1_NULL_HRS_INDEX;
0N/A uint num_so_far = 0;
0N/A while (curr < len && num_so_far < num) {
0N/A if (at(curr)->is_empty()) {
0N/A if (first == G1_NULL_HRS_INDEX) {
0N/A first = curr;
0N/A num_so_far = 1;
0N/A } else {
0N/A num_so_far += 1;
0N/A }
0N/A } else {
0N/A first = G1_NULL_HRS_INDEX;
0N/A num_so_far = 0;
0N/A }
0N/A curr += 1;
0N/A }
0N/A assert(num_so_far <= num, "post-condition");
0N/A if (num_so_far == num) {
0N/A // we found enough space for the humongous object
0N/A assert(from <= first && first < len, "post-condition");
0N/A assert(first < curr && (curr - first) == num, "post-condition");
0N/A for (uint i = first; i < first + num; ++i) {
0N/A assert(at(i)->is_empty(), "post-condition");
0N/A }
0N/A return first;
0N/A } else {
0N/A // we failed to find enough space for the humongous object
0N/A return G1_NULL_HRS_INDEX;
0N/A }
0N/A}
0N/A
0N/A// Public
0N/A
0N/Avoid HeapRegionSeq::initialize(HeapWord* bottom, HeapWord* end,
0N/A uint max_length) {
0N/A assert((uintptr_t) bottom % HeapRegion::GrainBytes == 0,
0N/A "bottom should be heap region aligned");
0N/A assert((uintptr_t) end % HeapRegion::GrainBytes == 0,
0N/A "end should be heap region aligned");
0N/A
0N/A _length = 0;
0N/A _heap_bottom = bottom;
0N/A _heap_end = end;
0N/A _region_shift = HeapRegion::LogOfHRGrainBytes;
0N/A _next_search_index = 0;
0N/A _allocated_length = 0;
0N/A _max_length = max_length;
0N/A
0N/A _regions = NEW_C_HEAP_ARRAY(HeapRegion*, max_length, mtGC);
0N/A memset(_regions, 0, (size_t) max_length * sizeof(HeapRegion*));
0N/A _regions_biased = _regions - ((uintx) bottom >> _region_shift);
0N/A
0N/A assert(&_regions[0] == &_regions_biased[addr_to_index_biased(bottom)],
0N/A "bottom should be included in the region with index 0");
0N/A}
0N/A
0N/AMemRegion HeapRegionSeq::expand_by(HeapWord* old_end,
0N/A HeapWord* new_end,
0N/A FreeRegionList* list) {
0N/A assert(old_end < new_end, "don't call it otherwise");
0N/A G1CollectedHeap* g1h = G1CollectedHeap::heap();
0N/A
0N/A HeapWord* next_bottom = old_end;
0N/A assert(_heap_bottom <= next_bottom, "invariant");
0N/A while (next_bottom < new_end) {
0N/A assert(next_bottom < _heap_end, "invariant");
0N/A uint index = length();
0N/A
0N/A assert(index < _max_length, "otherwise we cannot expand further");
0N/A if (index == 0) {
0N/A // We have not allocated any regions so far
0N/A assert(next_bottom == _heap_bottom, "invariant");
0N/A } else {
0N/A // next_bottom should match the end of the last/previous region
0N/A assert(next_bottom == at(index - 1)->end(), "invariant");
0N/A }
0N/A
0N/A if (index == _allocated_length) {
0N/A // We have to allocate a new HeapRegion.
0N/A HeapRegion* new_hr = g1h->new_heap_region(index, next_bottom);
0N/A if (new_hr == NULL) {
0N/A // allocation failed, we bail out and return what we have done so far
0N/A return MemRegion(old_end, next_bottom);
0N/A }
0N/A assert(_regions[index] == NULL, "invariant");
0N/A _regions[index] = new_hr;
0N/A increment_length(&_allocated_length);
0N/A }
0N/A // Have to increment the length first, otherwise we will get an
0N/A // assert failure at(index) below.
0N/A increment_length(&_length);
0N/A HeapRegion* hr = at(index);
0N/A list->add_as_tail(hr);
0N/A
0N/A next_bottom = hr->end();
0N/A }
0N/A assert(next_bottom == new_end, "post-condition");
0N/A return MemRegion(old_end, next_bottom);
0N/A}
0N/A
0N/Auint HeapRegionSeq::free_suffix() {
0N/A uint res = 0;
0N/A uint index = length();
0N/A while (index > 0) {
0N/A index -= 1;
0N/A if (!at(index)->is_empty()) {
0N/A break;
0N/A }
0N/A res += 1;
0N/A }
0N/A return res;
0N/A}
0N/A
0N/Auint HeapRegionSeq::find_contiguous(uint num) {
0N/A assert(num > 1, "use this only for sequences of length 2 or greater");
0N/A assert(_next_search_index <= length(),
0N/A err_msg("_next_search_index: %u should be valid and <= than %u",
0N/A _next_search_index, length()));
0N/A
0N/A uint start = _next_search_index;
0N/A uint res = find_contiguous_from(start, num);
0N/A if (res == G1_NULL_HRS_INDEX && start > 0) {
0N/A // Try starting from the beginning. If _next_search_index was 0,
0N/A // no point in doing this again.
0N/A res = find_contiguous_from(0, num);
0N/A }
0N/A if (res != G1_NULL_HRS_INDEX) {
0N/A assert(res < length(), err_msg("res: %u should be valid", res));
0N/A _next_search_index = res + num;
0N/A assert(_next_search_index <= length(),
0N/A err_msg("_next_search_index: %u should be valid and <= than %u",
0N/A _next_search_index, length()));
0N/A }
0N/A return res;
0N/A}
0N/A
0N/Avoid HeapRegionSeq::iterate(HeapRegionClosure* blk) const {
0N/A iterate_from((HeapRegion*) NULL, blk);
0N/A}
0N/A
0N/Avoid HeapRegionSeq::iterate_from(HeapRegion* hr, HeapRegionClosure* blk) const {
0N/A uint hr_index = 0;
0N/A if (hr != NULL) {
0N/A hr_index = hr->hrs_index();
0N/A }
0N/A
0N/A uint len = length();
0N/A for (uint i = hr_index; i < len; i += 1) {
0N/A bool res = blk->doHeapRegion(at(i));
0N/A if (res) {
0N/A blk->incomplete();
0N/A return;
0N/A }
0N/A }
0N/A for (uint i = 0; i < hr_index; i += 1) {
0N/A bool res = blk->doHeapRegion(at(i));
0N/A if (res) {
0N/A blk->incomplete();
0N/A return;
0N/A }
0N/A }
0N/A}
0N/A
0N/AMemRegion HeapRegionSeq::shrink_by(size_t shrink_bytes,
0N/A uint* num_regions_deleted) {
0N/A // Reset this in case it's currently pointing into the regions that
0N/A // we just removed.
0N/A _next_search_index = 0;
0N/A
0N/A assert(shrink_bytes % os::vm_page_size() == 0, "unaligned");
0N/A assert(shrink_bytes % HeapRegion::GrainBytes == 0, "unaligned");
0N/A assert(length() > 0, "the region sequence should not be empty");
0N/A assert(length() <= _allocated_length, "invariant");
0N/A assert(_allocated_length > 0, "we should have at least one region committed");
0N/A
0N/A // around the loop, i will be the next region to be removed
0N/A uint i = length() - 1;
0N/A assert(i > 0, "we should never remove all regions");
0N/A // [last_start, end) is the MemRegion that covers the regions we will remove.
0N/A HeapWord* end = at(i)->end();
0N/A HeapWord* last_start = end;
0N/A *num_regions_deleted = 0;
0N/A while (shrink_bytes > 0) {
0N/A HeapRegion* cur = at(i);
0N/A // We should leave the humongous regions where they are.
0N/A if (cur->isHumongous()) break;
0N/A // We should stop shrinking if we come across a non-empty region.
0N/A if (!cur->is_empty()) break;
0N/A
0N/A i -= 1;
0N/A *num_regions_deleted += 1;
0N/A shrink_bytes -= cur->capacity();
0N/A last_start = cur->bottom();
0N/A decrement_length(&_length);
0N/A // We will reclaim the HeapRegion. _allocated_length should be
0N/A // covering this index. So, even though we removed the region from
0N/A // the active set by decreasing _length, we still have it
0N/A // available in the future if we need to re-use it.
0N/A assert(i > 0, "we should never remove all regions");
0N/A assert(length() > 0, "we should never remove all regions");
0N/A }
0N/A return MemRegion(last_start, end);
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/Avoid HeapRegionSeq::verify_optional() {
0N/A guarantee(_length <= _allocated_length,
0N/A err_msg("invariant: _length: %u _allocated_length: %u",
0N/A _length, _allocated_length));
0N/A guarantee(_allocated_length <= _max_length,
0N/A err_msg("invariant: _allocated_length: %u _max_length: %u",
0N/A _allocated_length, _max_length));
0N/A guarantee(_next_search_index <= _length,
0N/A err_msg("invariant: _next_search_index: %u _length: %u",
0N/A _next_search_index, _length));
0N/A
0N/A HeapWord* prev_end = _heap_bottom;
0N/A for (uint i = 0; i < _allocated_length; i += 1) {
0N/A HeapRegion* hr = _regions[i];
0N/A guarantee(hr != NULL, err_msg("invariant: i: %u", i));
0N/A guarantee(hr->bottom() == prev_end,
0N/A err_msg("invariant i: %u "HR_FORMAT" prev_end: "PTR_FORMAT,
0N/A i, HR_FORMAT_PARAMS(hr), prev_end));
0N/A guarantee(hr->hrs_index() == i,
0N/A err_msg("invariant: i: %u hrs_index(): %u", i, hr->hrs_index()));
0N/A if (i < _length) {
0N/A // Asserts will fire if i is >= _length
0N/A HeapWord* addr = hr->bottom();
0N/A guarantee(addr_to_region(addr) == hr, "sanity");
0N/A guarantee(addr_to_region_unsafe(addr) == hr, "sanity");
0N/A } else {
0N/A guarantee(hr->is_empty(), "sanity");
0N/A guarantee(!hr->isHumongous(), "sanity");
0N/A // using assert instead of guarantee here since containing_set()
0N/A // is only available in non-product builds.
0N/A assert(hr->containing_set() == NULL, "sanity");
0N/A }
0N/A if (hr->startsHumongous()) {
0N/A prev_end = hr->orig_end();
0N/A } else {
0N/A prev_end = hr->end();
0N/A }
0N/A }
0N/A for (uint i = _allocated_length; i < _max_length; i += 1) {
0N/A guarantee(_regions[i] == NULL, err_msg("invariant i: %u", i));
0N/A }
0N/A}
0N/A#endif // PRODUCT
0N/A