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

Fri, 11 Apr 2014 11:00:12 +0200

author
pliden
date
Fri, 11 Apr 2014 11:00:12 +0200
changeset 6690
1772223a25a2
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
child 7049
eec72fa4b108
permissions
-rw-r--r--

8037112: gc/g1/TestHumongousAllocInitialMark.java caused SIGSEGV
Reviewed-by: brutisso, mgerdin

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

mercurial