ysr@777: /* tonyp@2453: * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/heapRegionSeq.hpp" stefank@2314: #include "memory/allocation.hpp" ysr@777: ysr@777: // Local to this file. ysr@777: ysr@777: static int orderRegions(HeapRegion** hr1p, HeapRegion** hr2p) { ysr@777: if ((*hr1p)->end() <= (*hr2p)->bottom()) return -1; ysr@777: else if ((*hr2p)->end() <= (*hr1p)->bottom()) return 1; ysr@777: else if (*hr1p == *hr2p) return 0; ysr@777: else { ysr@777: assert(false, "We should never compare distinct overlapping regions."); ysr@777: } ysr@777: return 0; ysr@777: } ysr@777: iveresov@828: HeapRegionSeq::HeapRegionSeq(const size_t max_size) : ysr@777: _alloc_search_start(0), ysr@777: // The line below is the worst bit of C++ hackery I've ever written ysr@777: // (Detlefs, 11/23). You should think of it as equivalent to ysr@777: // "_regions(100, true)": initialize the growable array and inform it kvn@2043: // that it should allocate its elem array(s) on the C heap. kvn@2043: // kvn@2043: // The first argument, however, is actually a comma expression kvn@2043: // (set_allocation_type(this, C_HEAP), 100). The purpose of the kvn@2043: // set_allocation_type() call is to replace the default allocation kvn@2043: // type for embedded objects STACK_OR_EMBEDDED with C_HEAP. It will kvn@2043: // allow to pass the assert in GenericGrowableArray() which checks kvn@2043: // that a growable array object must be on C heap if elements are. kvn@2043: // kvn@2043: // Note: containing object is allocated on C heap since it is CHeapObj. kvn@2043: // kvn@2043: _regions((ResourceObj::set_allocation_type((address)&_regions, kvn@2043: ResourceObj::C_HEAP), iveresov@828: (int)max_size), ysr@777: true), ysr@777: _next_rr_candidate(0), ysr@777: _seq_bottom(NULL) ysr@777: {} ysr@777: ysr@777: // Private methods. ysr@777: apetrusenko@1112: void HeapRegionSeq::print_empty_runs() { ysr@777: int empty_run = 0; ysr@777: int n_empty = 0; ysr@777: int empty_run_start; ysr@777: for (int i = 0; i < _regions.length(); i++) { ysr@777: HeapRegion* r = _regions.at(i); ysr@777: if (r->continuesHumongous()) continue; apetrusenko@1112: if (r->is_empty()) { ysr@777: assert(!r->isHumongous(), "H regions should not be empty."); ysr@777: if (empty_run == 0) empty_run_start = i; ysr@777: empty_run++; ysr@777: n_empty++; ysr@777: } else { ysr@777: if (empty_run > 0) { ysr@777: gclog_or_tty->print(" %d:%d", empty_run_start, empty_run); ysr@777: empty_run = 0; ysr@777: } ysr@777: } ysr@777: } ysr@777: if (empty_run > 0) { ysr@777: gclog_or_tty->print(" %d:%d", empty_run_start, empty_run); ysr@777: } ysr@777: gclog_or_tty->print_cr(" [tot = %d]", n_empty); ysr@777: } ysr@777: ysr@777: int HeapRegionSeq::find(HeapRegion* hr) { ysr@777: // FIXME: optimized for adjacent regions of fixed size. ysr@777: int ind = hr->hrs_index(); ysr@777: if (ind != -1) { ysr@777: assert(_regions.at(ind) == hr, "Mismatch"); ysr@777: } ysr@777: return ind; ysr@777: } ysr@777: ysr@777: ysr@777: // Public methods. ysr@777: ysr@777: void HeapRegionSeq::insert(HeapRegion* hr) { iveresov@828: assert(!_regions.is_full(), "Too many elements in HeapRegionSeq"); ysr@777: if (_regions.length() == 0 ysr@777: || _regions.top()->end() <= hr->bottom()) { ysr@777: hr->set_hrs_index(_regions.length()); ysr@777: _regions.append(hr); ysr@777: } else { ysr@777: _regions.append(hr); ysr@777: _regions.sort(orderRegions); ysr@777: for (int i = 0; i < _regions.length(); i++) { ysr@777: _regions.at(i)->set_hrs_index(i); ysr@777: } ysr@777: } ysr@777: char* bot = (char*)_regions.at(0)->bottom(); ysr@777: if (_seq_bottom == NULL || bot < _seq_bottom) _seq_bottom = bot; ysr@777: } ysr@777: ysr@777: size_t HeapRegionSeq::length() { ysr@777: return _regions.length(); ysr@777: } ysr@777: ysr@777: size_t HeapRegionSeq::free_suffix() { ysr@777: size_t res = 0; ysr@777: int first = _regions.length() - 1; ysr@777: int cur = first; ysr@777: while (cur >= 0 && ysr@777: (_regions.at(cur)->is_empty() ysr@777: && (first == cur ysr@777: || (_regions.at(cur+1)->bottom() == ysr@777: _regions.at(cur)->end())))) { ysr@777: res++; ysr@777: cur--; ysr@777: } ysr@777: return res; ysr@777: } ysr@777: tonyp@2472: int HeapRegionSeq::find_contiguous_from(int from, size_t num) { tonyp@2472: assert(num > 1, "pre-condition"); tonyp@2472: assert(0 <= from && from <= _regions.length(), tonyp@2472: err_msg("from: %d should be valid and <= than %d", tonyp@2472: from, _regions.length())); tonyp@2472: tonyp@2472: int curr = from; tonyp@2472: int first = -1; tonyp@2472: size_t num_so_far = 0; tonyp@2472: while (curr < _regions.length() && num_so_far < num) { tonyp@2472: HeapRegion* curr_hr = _regions.at(curr); tonyp@2472: if (curr_hr->is_empty()) { tonyp@2472: if (first == -1) { tonyp@2472: first = curr; tonyp@2472: num_so_far = 1; tonyp@2472: } else { tonyp@2472: num_so_far += 1; tonyp@2472: } tonyp@2472: } else { tonyp@2472: first = -1; tonyp@2472: num_so_far = 0; tonyp@2472: } tonyp@2472: curr += 1; tonyp@2472: } tonyp@2472: tonyp@2472: assert(num_so_far <= num, "post-condition"); tonyp@2472: if (num_so_far == num) { tonyp@2643: // we found enough space for the humongous object tonyp@2472: assert(from <= first && first < _regions.length(), "post-condition"); tonyp@2472: assert(first < curr && (curr - first) == (int) num, "post-condition"); tonyp@2472: for (int i = first; i < first + (int) num; ++i) { tonyp@2472: assert(_regions.at(i)->is_empty(), "post-condition"); tonyp@2472: } tonyp@2472: return first; tonyp@2472: } else { tonyp@2472: // we failed to find enough space for the humongous object tonyp@2472: return -1; tonyp@2472: } tonyp@2472: } tonyp@2472: tonyp@2472: int HeapRegionSeq::find_contiguous(size_t num) { tonyp@2472: assert(num > 1, "otherwise we should not be calling this"); tonyp@2472: assert(0 <= _alloc_search_start && _alloc_search_start <= _regions.length(), tonyp@2472: err_msg("_alloc_search_start: %d should be valid and <= than %d", tonyp@2472: _alloc_search_start, _regions.length())); tonyp@2472: tonyp@2472: int start = _alloc_search_start; tonyp@2472: int res = find_contiguous_from(start, num); tonyp@2472: if (res == -1 && start != 0) { tonyp@2472: // Try starting from the beginning. If _alloc_search_start was 0, tonyp@2472: // no point in doing this again. tonyp@2472: res = find_contiguous_from(0, num); tonyp@2472: } tonyp@2472: if (res != -1) { tonyp@2472: assert(0 <= res && res < _regions.length(), tonyp@2472: err_msg("res: %d should be valid", res)); tonyp@2472: _alloc_search_start = res + (int) num; tonyp@2492: assert(0 < _alloc_search_start && _alloc_search_start <= _regions.length(), tonyp@2492: err_msg("_alloc_search_start: %d should be valid", tonyp@2492: _alloc_search_start)); tonyp@2472: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: void HeapRegionSeq::iterate(HeapRegionClosure* blk) { ysr@777: iterate_from((HeapRegion*)NULL, blk); ysr@777: } ysr@777: ysr@777: // The first argument r is the heap region at which iteration begins. ysr@777: // This operation runs fastest when r is NULL, or the heap region for ysr@777: // which a HeapRegionClosure most recently returned true, or the ysr@777: // heap region immediately to its right in the sequence. In all ysr@777: // other cases a linear search is required to find the index of r. ysr@777: ysr@777: void HeapRegionSeq::iterate_from(HeapRegion* r, HeapRegionClosure* blk) { ysr@777: ysr@777: // :::: FIXME :::: ysr@777: // Static cache value is bad, especially when we start doing parallel ysr@777: // remembered set update. For now just don't cache anything (the ysr@777: // code in the def'd out blocks). ysr@777: ysr@777: #if 0 ysr@777: static int cached_j = 0; ysr@777: #endif ysr@777: int len = _regions.length(); ysr@777: int j = 0; ysr@777: // Find the index of r. ysr@777: if (r != NULL) { ysr@777: #if 0 ysr@777: assert(cached_j >= 0, "Invariant."); ysr@777: if ((cached_j < len) && (r == _regions.at(cached_j))) { ysr@777: j = cached_j; ysr@777: } else if ((cached_j + 1 < len) && (r == _regions.at(cached_j + 1))) { ysr@777: j = cached_j + 1; ysr@777: } else { ysr@777: j = find(r); ysr@777: #endif ysr@777: if (j < 0) { ysr@777: j = 0; ysr@777: } ysr@777: #if 0 ysr@777: } ysr@777: #endif ysr@777: } ysr@777: int i; ysr@777: for (i = j; i < len; i += 1) { ysr@777: int res = blk->doHeapRegion(_regions.at(i)); ysr@777: if (res) { ysr@777: #if 0 ysr@777: cached_j = i; ysr@777: #endif ysr@777: blk->incomplete(); ysr@777: return; ysr@777: } ysr@777: } ysr@777: for (i = 0; i < j; i += 1) { ysr@777: int res = blk->doHeapRegion(_regions.at(i)); ysr@777: if (res) { ysr@777: #if 0 ysr@777: cached_j = i; ysr@777: #endif ysr@777: blk->incomplete(); ysr@777: return; ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void HeapRegionSeq::iterate_from(int idx, HeapRegionClosure* blk) { ysr@777: int len = _regions.length(); ysr@777: int i; ysr@777: for (i = idx; i < len; i++) { ysr@777: if (blk->doHeapRegion(_regions.at(i))) { ysr@777: blk->incomplete(); ysr@777: return; ysr@777: } ysr@777: } ysr@777: for (i = 0; i < idx; i++) { ysr@777: if (blk->doHeapRegion(_regions.at(i))) { ysr@777: blk->incomplete(); ysr@777: return; ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: MemRegion HeapRegionSeq::shrink_by(size_t shrink_bytes, ysr@777: size_t& num_regions_deleted) { tonyp@2472: // Reset this in case it's currently pointing into the regions that tonyp@2472: // we just removed. tonyp@2472: _alloc_search_start = 0; tonyp@2472: ysr@777: assert(shrink_bytes % os::vm_page_size() == 0, "unaligned"); ysr@777: assert(shrink_bytes % HeapRegion::GrainBytes == 0, "unaligned"); ysr@777: ysr@777: if (_regions.length() == 0) { ysr@777: num_regions_deleted = 0; ysr@777: return MemRegion(); ysr@777: } ysr@777: int j = _regions.length() - 1; ysr@777: HeapWord* end = _regions.at(j)->end(); ysr@777: HeapWord* last_start = end; ysr@777: while (j >= 0 && shrink_bytes > 0) { ysr@777: HeapRegion* cur = _regions.at(j); ysr@777: // We have to leave humongous regions where they are, ysr@777: // and work around them. ysr@777: if (cur->isHumongous()) { ysr@777: return MemRegion(last_start, end); ysr@777: } ysr@777: assert(cur == _regions.top(), "Should be top"); ysr@777: if (!cur->is_empty()) break; ysr@777: shrink_bytes -= cur->capacity(); ysr@777: num_regions_deleted++; ysr@777: _regions.pop(); ysr@777: last_start = cur->bottom(); ysr@777: // We need to delete these somehow, but can't currently do so here: if ysr@777: // we do, the ZF thread may still access the deleted region. We'll ysr@777: // leave this here as a reminder that we have to do something about ysr@777: // this. ysr@777: // delete cur; ysr@777: j--; ysr@777: } ysr@777: return MemRegion(last_start, end); ysr@777: } ysr@777: ysr@777: class PrintHeapRegionClosure : public HeapRegionClosure { ysr@777: public: ysr@777: bool doHeapRegion(HeapRegion* r) { ysr@777: gclog_or_tty->print(PTR_FORMAT ":", r); ysr@777: r->print(); ysr@777: return false; ysr@777: } ysr@777: }; ysr@777: ysr@777: void HeapRegionSeq::print() { ysr@777: PrintHeapRegionClosure cl; ysr@777: iterate(&cl); ysr@777: }