src/share/vm/gc_implementation/g1/heapRegionSeq.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/heapRegion.hpp"
aoqi@0 27 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
aoqi@0 28 #include "gc_implementation/g1/heapRegionSet.hpp"
aoqi@0 29 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 30 #include "memory/allocation.hpp"
aoqi@0 31
aoqi@0 32 // Private
aoqi@0 33
aoqi@0 34 uint HeapRegionSeq::find_contiguous_from(uint from, uint num) {
aoqi@0 35 uint len = length();
aoqi@0 36 assert(num > 1, "use this only for sequences of length 2 or greater");
aoqi@0 37 assert(from <= len,
aoqi@0 38 err_msg("from: %u should be valid and <= than %u", from, len));
aoqi@0 39
aoqi@0 40 uint curr = from;
aoqi@0 41 uint first = G1_NULL_HRS_INDEX;
aoqi@0 42 uint num_so_far = 0;
aoqi@0 43 while (curr < len && num_so_far < num) {
aoqi@0 44 if (at(curr)->is_empty()) {
aoqi@0 45 if (first == G1_NULL_HRS_INDEX) {
aoqi@0 46 first = curr;
aoqi@0 47 num_so_far = 1;
aoqi@0 48 } else {
aoqi@0 49 num_so_far += 1;
aoqi@0 50 }
aoqi@0 51 } else {
aoqi@0 52 first = G1_NULL_HRS_INDEX;
aoqi@0 53 num_so_far = 0;
aoqi@0 54 }
aoqi@0 55 curr += 1;
aoqi@0 56 }
aoqi@0 57 assert(num_so_far <= num, "post-condition");
aoqi@0 58 if (num_so_far == num) {
aoqi@0 59 // we found enough space for the humongous object
aoqi@0 60 assert(from <= first && first < len, "post-condition");
aoqi@0 61 assert(first < curr && (curr - first) == num, "post-condition");
aoqi@0 62 for (uint i = first; i < first + num; ++i) {
aoqi@0 63 assert(at(i)->is_empty(), "post-condition");
aoqi@0 64 }
aoqi@0 65 return first;
aoqi@0 66 } else {
aoqi@0 67 // we failed to find enough space for the humongous object
aoqi@0 68 return G1_NULL_HRS_INDEX;
aoqi@0 69 }
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 // Public
aoqi@0 73
aoqi@0 74 void HeapRegionSeq::initialize(HeapWord* bottom, HeapWord* end) {
aoqi@0 75 assert((uintptr_t) bottom % HeapRegion::GrainBytes == 0,
aoqi@0 76 "bottom should be heap region aligned");
aoqi@0 77 assert((uintptr_t) end % HeapRegion::GrainBytes == 0,
aoqi@0 78 "end should be heap region aligned");
aoqi@0 79
aoqi@0 80 _next_search_index = 0;
aoqi@0 81 _allocated_length = 0;
aoqi@0 82
aoqi@0 83 _regions.initialize(bottom, end, HeapRegion::GrainBytes);
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 MemRegion HeapRegionSeq::expand_by(HeapWord* old_end,
aoqi@0 87 HeapWord* new_end,
aoqi@0 88 FreeRegionList* list) {
aoqi@0 89 assert(old_end < new_end, "don't call it otherwise");
aoqi@0 90 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 91
aoqi@0 92 HeapWord* next_bottom = old_end;
aoqi@0 93 assert(heap_bottom() <= next_bottom, "invariant");
aoqi@0 94 while (next_bottom < new_end) {
aoqi@0 95 assert(next_bottom < heap_end(), "invariant");
aoqi@0 96 uint index = length();
aoqi@0 97
aoqi@0 98 assert(index < max_length(), "otherwise we cannot expand further");
aoqi@0 99 if (index == 0) {
aoqi@0 100 // We have not allocated any regions so far
aoqi@0 101 assert(next_bottom == heap_bottom(), "invariant");
aoqi@0 102 } else {
aoqi@0 103 // next_bottom should match the end of the last/previous region
aoqi@0 104 assert(next_bottom == at(index - 1)->end(), "invariant");
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 if (index == _allocated_length) {
aoqi@0 108 // We have to allocate a new HeapRegion.
aoqi@0 109 HeapRegion* new_hr = g1h->new_heap_region(index, next_bottom);
aoqi@0 110 if (new_hr == NULL) {
aoqi@0 111 // allocation failed, we bail out and return what we have done so far
aoqi@0 112 return MemRegion(old_end, next_bottom);
aoqi@0 113 }
aoqi@0 114 assert(_regions.get_by_index(index) == NULL, "invariant");
aoqi@0 115 _regions.set_by_index(index, new_hr);
aoqi@0 116 increment_allocated_length();
aoqi@0 117 }
aoqi@0 118 // Have to increment the length first, otherwise we will get an
aoqi@0 119 // assert failure at(index) below.
aoqi@0 120 increment_length();
aoqi@0 121 HeapRegion* hr = at(index);
aoqi@0 122 list->add_as_tail(hr);
aoqi@0 123
aoqi@0 124 next_bottom = hr->end();
aoqi@0 125 }
aoqi@0 126 assert(next_bottom == new_end, "post-condition");
aoqi@0 127 return MemRegion(old_end, next_bottom);
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 uint HeapRegionSeq::free_suffix() {
aoqi@0 131 uint res = 0;
aoqi@0 132 uint index = length();
aoqi@0 133 while (index > 0) {
aoqi@0 134 index -= 1;
aoqi@0 135 if (!at(index)->is_empty()) {
aoqi@0 136 break;
aoqi@0 137 }
aoqi@0 138 res += 1;
aoqi@0 139 }
aoqi@0 140 return res;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 uint HeapRegionSeq::find_contiguous(uint num) {
aoqi@0 144 assert(num > 1, "use this only for sequences of length 2 or greater");
aoqi@0 145 assert(_next_search_index <= length(),
aoqi@0 146 err_msg("_next_search_index: %u should be valid and <= than %u",
aoqi@0 147 _next_search_index, length()));
aoqi@0 148
aoqi@0 149 uint start = _next_search_index;
aoqi@0 150 uint res = find_contiguous_from(start, num);
aoqi@0 151 if (res == G1_NULL_HRS_INDEX && start > 0) {
aoqi@0 152 // Try starting from the beginning. If _next_search_index was 0,
aoqi@0 153 // no point in doing this again.
aoqi@0 154 res = find_contiguous_from(0, num);
aoqi@0 155 }
aoqi@0 156 if (res != G1_NULL_HRS_INDEX) {
aoqi@0 157 assert(res < length(), err_msg("res: %u should be valid", res));
aoqi@0 158 _next_search_index = res + num;
aoqi@0 159 assert(_next_search_index <= length(),
aoqi@0 160 err_msg("_next_search_index: %u should be valid and <= than %u",
aoqi@0 161 _next_search_index, length()));
aoqi@0 162 }
aoqi@0 163 return res;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 void HeapRegionSeq::iterate(HeapRegionClosure* blk) const {
aoqi@0 167 iterate_from((HeapRegion*) NULL, blk);
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 void HeapRegionSeq::iterate_from(HeapRegion* hr, HeapRegionClosure* blk) const {
aoqi@0 171 uint hr_index = 0;
aoqi@0 172 if (hr != NULL) {
aoqi@0 173 hr_index = hr->hrs_index();
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 uint len = length();
aoqi@0 177 for (uint i = hr_index; i < len; i += 1) {
aoqi@0 178 bool res = blk->doHeapRegion(at(i));
aoqi@0 179 if (res) {
aoqi@0 180 blk->incomplete();
aoqi@0 181 return;
aoqi@0 182 }
aoqi@0 183 }
aoqi@0 184 for (uint i = 0; i < hr_index; i += 1) {
aoqi@0 185 bool res = blk->doHeapRegion(at(i));
aoqi@0 186 if (res) {
aoqi@0 187 blk->incomplete();
aoqi@0 188 return;
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 uint HeapRegionSeq::shrink_by(uint num_regions_to_remove) {
aoqi@0 194 // Reset this in case it's currently pointing into the regions that
aoqi@0 195 // we just removed.
aoqi@0 196 _next_search_index = 0;
aoqi@0 197
aoqi@0 198 assert(length() > 0, "the region sequence should not be empty");
aoqi@0 199 assert(length() <= _allocated_length, "invariant");
aoqi@0 200 assert(_allocated_length > 0, "we should have at least one region committed");
aoqi@0 201 assert(num_regions_to_remove < length(), "We should never remove all regions");
aoqi@0 202
aoqi@0 203 uint i = 0;
aoqi@0 204 for (; i < num_regions_to_remove; i++) {
aoqi@0 205 HeapRegion* cur = at(length() - 1);
aoqi@0 206
aoqi@0 207 if (!cur->is_empty()) {
aoqi@0 208 // We have to give up if the region can not be moved
aoqi@0 209 break;
aoqi@0 210 }
aoqi@0 211 assert(!cur->isHumongous(), "Humongous regions should not be empty");
aoqi@0 212
aoqi@0 213 decrement_length();
aoqi@0 214 }
aoqi@0 215 return i;
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 #ifndef PRODUCT
aoqi@0 219 void HeapRegionSeq::verify_optional() {
aoqi@0 220 guarantee(length() <= _allocated_length,
aoqi@0 221 err_msg("invariant: _length: %u _allocated_length: %u",
aoqi@0 222 length(), _allocated_length));
aoqi@0 223 guarantee(_allocated_length <= max_length(),
aoqi@0 224 err_msg("invariant: _allocated_length: %u _max_length: %u",
aoqi@0 225 _allocated_length, max_length()));
aoqi@0 226 guarantee(_next_search_index <= length(),
aoqi@0 227 err_msg("invariant: _next_search_index: %u _length: %u",
aoqi@0 228 _next_search_index, length()));
aoqi@0 229
aoqi@0 230 HeapWord* prev_end = heap_bottom();
aoqi@0 231 for (uint i = 0; i < _allocated_length; i += 1) {
aoqi@0 232 HeapRegion* hr = _regions.get_by_index(i);
aoqi@0 233 guarantee(hr != NULL, err_msg("invariant: i: %u", i));
aoqi@0 234 guarantee(hr->bottom() == prev_end,
aoqi@0 235 err_msg("invariant i: %u "HR_FORMAT" prev_end: "PTR_FORMAT,
aoqi@0 236 i, HR_FORMAT_PARAMS(hr), p2i(prev_end)));
aoqi@0 237 guarantee(hr->hrs_index() == i,
aoqi@0 238 err_msg("invariant: i: %u hrs_index(): %u", i, hr->hrs_index()));
aoqi@0 239 if (i < length()) {
aoqi@0 240 // Asserts will fire if i is >= _length
aoqi@0 241 HeapWord* addr = hr->bottom();
aoqi@0 242 guarantee(addr_to_region(addr) == hr, "sanity");
aoqi@0 243 guarantee(addr_to_region_unsafe(addr) == hr, "sanity");
aoqi@0 244 } else {
aoqi@0 245 guarantee(hr->is_empty(), "sanity");
aoqi@0 246 guarantee(!hr->isHumongous(), "sanity");
aoqi@0 247 // using assert instead of guarantee here since containing_set()
aoqi@0 248 // is only available in non-product builds.
aoqi@0 249 assert(hr->containing_set() == NULL, "sanity");
aoqi@0 250 }
aoqi@0 251 if (hr->startsHumongous()) {
aoqi@0 252 prev_end = hr->orig_end();
aoqi@0 253 } else {
aoqi@0 254 prev_end = hr->end();
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257 for (uint i = _allocated_length; i < max_length(); i += 1) {
aoqi@0 258 guarantee(_regions.get_by_index(i) == NULL, err_msg("invariant i: %u", i));
aoqi@0 259 }
aoqi@0 260 }
aoqi@0 261 #endif // PRODUCT

mercurial