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

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7651
c132be0fb74d
parent 7535
7ae4e26cb1e0
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 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/g1AllocRegion.inline.hpp"
aoqi@0 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
goetz@6911 28 #include "runtime/orderAccess.inline.hpp"
aoqi@0 29
aoqi@0 30 G1CollectedHeap* G1AllocRegion::_g1h = NULL;
aoqi@0 31 HeapRegion* G1AllocRegion::_dummy_region = NULL;
aoqi@0 32
aoqi@0 33 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
aoqi@0 34 assert(_dummy_region == NULL, "should be set once");
aoqi@0 35 assert(dummy_region != NULL, "pre-condition");
aoqi@0 36 assert(dummy_region->free() == 0, "pre-condition");
aoqi@0 37
aoqi@0 38 // Make sure that any allocation attempt on this region will fail
aoqi@0 39 // and will not trigger any asserts.
aoqi@0 40 assert(allocate(dummy_region, 1, false) == NULL, "should fail");
aoqi@0 41 assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
aoqi@0 42 assert(allocate(dummy_region, 1, true) == NULL, "should fail");
aoqi@0 43 assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
aoqi@0 44
aoqi@0 45 _g1h = g1h;
aoqi@0 46 _dummy_region = dummy_region;
aoqi@0 47 }
aoqi@0 48
aoqi@0 49 void G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
aoqi@0 50 bool bot_updates) {
aoqi@0 51 assert(alloc_region != NULL && alloc_region != _dummy_region,
aoqi@0 52 "pre-condition");
aoqi@0 53
aoqi@0 54 // Other threads might still be trying to allocate using a CAS out
aoqi@0 55 // of the region we are trying to retire, as they can do so without
aoqi@0 56 // holding the lock. So, we first have to make sure that noone else
aoqi@0 57 // can allocate out of it by doing a maximal allocation. Even if our
aoqi@0 58 // CAS attempt fails a few times, we'll succeed sooner or later
aoqi@0 59 // given that failed CAS attempts mean that the region is getting
aoqi@0 60 // closed to being full.
aoqi@0 61 size_t free_word_size = alloc_region->free() / HeapWordSize;
aoqi@0 62
aoqi@0 63 // This is the minimum free chunk we can turn into a dummy
aoqi@0 64 // object. If the free space falls below this, then noone can
aoqi@0 65 // allocate in this region anyway (all allocation requests will be
aoqi@0 66 // of a size larger than this) so we won't have to perform the dummy
aoqi@0 67 // allocation.
aoqi@0 68 size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
aoqi@0 69
aoqi@0 70 while (free_word_size >= min_word_size_to_fill) {
aoqi@0 71 HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
aoqi@0 72 if (dummy != NULL) {
aoqi@0 73 // If the allocation was successful we should fill in the space.
aoqi@0 74 CollectedHeap::fill_with_object(dummy, free_word_size);
aoqi@0 75 alloc_region->set_pre_dummy_top(dummy);
aoqi@0 76 break;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 free_word_size = alloc_region->free() / HeapWordSize;
aoqi@0 80 // It's also possible that someone else beats us to the
aoqi@0 81 // allocation and they fill up the region. In that case, we can
aoqi@0 82 // just get out of the loop.
aoqi@0 83 }
aoqi@0 84 assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
aoqi@0 85 "post-condition");
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 void G1AllocRegion::retire(bool fill_up) {
aoqi@0 89 assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));
aoqi@0 90
aoqi@0 91 trace("retiring");
aoqi@0 92 HeapRegion* alloc_region = _alloc_region;
aoqi@0 93 if (alloc_region != _dummy_region) {
aoqi@0 94 // We never have to check whether the active region is empty or not,
aoqi@0 95 // and potentially free it if it is, given that it's guaranteed that
aoqi@0 96 // it will never be empty.
aoqi@0 97 assert(!alloc_region->is_empty(),
aoqi@0 98 ar_ext_msg(this, "the alloc region should never be empty"));
aoqi@0 99
aoqi@0 100 if (fill_up) {
aoqi@0 101 fill_up_remaining_space(alloc_region, _bot_updates);
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 assert(alloc_region->used() >= _used_bytes_before,
aoqi@0 105 ar_ext_msg(this, "invariant"));
aoqi@0 106 size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
aoqi@0 107 retire_region(alloc_region, allocated_bytes);
aoqi@0 108 _used_bytes_before = 0;
aoqi@0 109 _alloc_region = _dummy_region;
aoqi@0 110 }
aoqi@0 111 trace("retired");
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
aoqi@0 115 bool force) {
aoqi@0 116 assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));
aoqi@0 117 assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));
aoqi@0 118
aoqi@0 119 trace("attempting region allocation");
aoqi@0 120 HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
aoqi@0 121 if (new_alloc_region != NULL) {
aoqi@0 122 new_alloc_region->reset_pre_dummy_top();
aoqi@0 123 // Need to do this before the allocation
aoqi@0 124 _used_bytes_before = new_alloc_region->used();
aoqi@0 125 HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
aoqi@0 126 assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));
aoqi@0 127
aoqi@0 128 OrderAccess::storestore();
aoqi@0 129 // Note that we first perform the allocation and then we store the
aoqi@0 130 // region in _alloc_region. This is the reason why an active region
aoqi@0 131 // can never be empty.
sjohanss@7118 132 update_alloc_region(new_alloc_region);
aoqi@0 133 trace("region allocation successful");
aoqi@0 134 return result;
aoqi@0 135 } else {
aoqi@0 136 trace("region allocation failed");
aoqi@0 137 return NULL;
aoqi@0 138 }
aoqi@0 139 ShouldNotReachHere();
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 void G1AllocRegion::fill_in_ext_msg(ar_ext_msg* msg, const char* message) {
aoqi@0 143 msg->append("[%s] %s c: %u b: %s r: "PTR_FORMAT" u: "SIZE_FORMAT,
aoqi@0 144 _name, message, _count, BOOL_TO_STR(_bot_updates),
aoqi@0 145 p2i(_alloc_region), _used_bytes_before);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 void G1AllocRegion::init() {
aoqi@0 149 trace("initializing");
aoqi@0 150 assert(_alloc_region == NULL && _used_bytes_before == 0,
aoqi@0 151 ar_ext_msg(this, "pre-condition"));
aoqi@0 152 assert(_dummy_region != NULL, ar_ext_msg(this, "should have been set"));
aoqi@0 153 _alloc_region = _dummy_region;
aoqi@0 154 _count = 0;
aoqi@0 155 trace("initialized");
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 void G1AllocRegion::set(HeapRegion* alloc_region) {
aoqi@0 159 trace("setting");
aoqi@0 160 // We explicitly check that the region is not empty to make sure we
aoqi@0 161 // maintain the "the alloc region cannot be empty" invariant.
aoqi@0 162 assert(alloc_region != NULL && !alloc_region->is_empty(),
aoqi@0 163 ar_ext_msg(this, "pre-condition"));
aoqi@0 164 assert(_alloc_region == _dummy_region &&
aoqi@0 165 _used_bytes_before == 0 && _count == 0,
aoqi@0 166 ar_ext_msg(this, "pre-condition"));
aoqi@0 167
aoqi@0 168 _used_bytes_before = alloc_region->used();
aoqi@0 169 _alloc_region = alloc_region;
aoqi@0 170 _count += 1;
aoqi@0 171 trace("set");
aoqi@0 172 }
aoqi@0 173
sjohanss@7118 174 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {
sjohanss@7118 175 trace("update");
sjohanss@7118 176 // We explicitly check that the region is not empty to make sure we
sjohanss@7118 177 // maintain the "the alloc region cannot be empty" invariant.
sjohanss@7118 178 assert(alloc_region != NULL && !alloc_region->is_empty(),
sjohanss@7118 179 ar_ext_msg(this, "pre-condition"));
sjohanss@7118 180
sjohanss@7118 181 _alloc_region = alloc_region;
sjohanss@7118 182 _alloc_region->set_allocation_context(allocation_context());
sjohanss@7118 183 _count += 1;
sjohanss@7118 184 trace("updated");
sjohanss@7118 185 }
sjohanss@7118 186
aoqi@0 187 HeapRegion* G1AllocRegion::release() {
aoqi@0 188 trace("releasing");
aoqi@0 189 HeapRegion* alloc_region = _alloc_region;
aoqi@0 190 retire(false /* fill_up */);
aoqi@0 191 assert(_alloc_region == _dummy_region,
aoqi@0 192 ar_ext_msg(this, "post-condition of retire()"));
aoqi@0 193 _alloc_region = NULL;
aoqi@0 194 trace("released");
aoqi@0 195 return (alloc_region == _dummy_region) ? NULL : alloc_region;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 #if G1_ALLOC_REGION_TRACING
aoqi@0 199 void G1AllocRegion::trace(const char* str, size_t word_size, HeapWord* result) {
aoqi@0 200 // All the calls to trace that set either just the size or the size
aoqi@0 201 // and the result are considered part of level 2 tracing and are
aoqi@0 202 // skipped during level 1 tracing.
aoqi@0 203 if ((word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
aoqi@0 204 const size_t buffer_length = 128;
aoqi@0 205 char hr_buffer[buffer_length];
aoqi@0 206 char rest_buffer[buffer_length];
aoqi@0 207
aoqi@0 208 HeapRegion* alloc_region = _alloc_region;
aoqi@0 209 if (alloc_region == NULL) {
aoqi@0 210 jio_snprintf(hr_buffer, buffer_length, "NULL");
aoqi@0 211 } else if (alloc_region == _dummy_region) {
aoqi@0 212 jio_snprintf(hr_buffer, buffer_length, "DUMMY");
aoqi@0 213 } else {
aoqi@0 214 jio_snprintf(hr_buffer, buffer_length,
aoqi@0 215 HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 if (G1_ALLOC_REGION_TRACING > 1) {
aoqi@0 219 if (result != NULL) {
aoqi@0 220 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT" "PTR_FORMAT,
aoqi@0 221 word_size, result);
aoqi@0 222 } else if (word_size != 0) {
aoqi@0 223 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT, word_size);
aoqi@0 224 } else {
aoqi@0 225 jio_snprintf(rest_buffer, buffer_length, "");
aoqi@0 226 }
aoqi@0 227 } else {
aoqi@0 228 jio_snprintf(rest_buffer, buffer_length, "");
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 tty->print_cr("[%s] %u %s : %s %s",
aoqi@0 232 _name, _count, hr_buffer, str, rest_buffer);
aoqi@0 233 }
aoqi@0 234 }
aoqi@0 235 #endif // G1_ALLOC_REGION_TRACING
aoqi@0 236
aoqi@0 237 G1AllocRegion::G1AllocRegion(const char* name,
aoqi@0 238 bool bot_updates)
aoqi@0 239 : _name(name), _bot_updates(bot_updates),
sjohanss@7118 240 _alloc_region(NULL), _count(0), _used_bytes_before(0),
sjohanss@7118 241 _allocation_context(AllocationContext::system()) { }
aoqi@0 242
sjohanss@7118 243
sjohanss@7118 244 HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size,
sjohanss@7118 245 bool force) {
sjohanss@7118 246 return _g1h->new_mutator_alloc_region(word_size, force);
sjohanss@7118 247 }
sjohanss@7118 248
sjohanss@7118 249 void MutatorAllocRegion::retire_region(HeapRegion* alloc_region,
sjohanss@7118 250 size_t allocated_bytes) {
sjohanss@7118 251 _g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes);
sjohanss@7118 252 }
sjohanss@7118 253
sjohanss@7118 254 HeapRegion* SurvivorGCAllocRegion::allocate_new_region(size_t word_size,
sjohanss@7118 255 bool force) {
sjohanss@7118 256 assert(!force, "not supported for GC alloc regions");
tschatzl@7651 257 return _g1h->new_gc_alloc_region(word_size, count(), InCSetState::Young);
sjohanss@7118 258 }
sjohanss@7118 259
sjohanss@7118 260 void SurvivorGCAllocRegion::retire_region(HeapRegion* alloc_region,
sjohanss@7118 261 size_t allocated_bytes) {
tschatzl@7651 262 _g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, InCSetState::Young);
sjohanss@7118 263 }
sjohanss@7118 264
sjohanss@7118 265 HeapRegion* OldGCAllocRegion::allocate_new_region(size_t word_size,
sjohanss@7118 266 bool force) {
sjohanss@7118 267 assert(!force, "not supported for GC alloc regions");
tschatzl@7651 268 return _g1h->new_gc_alloc_region(word_size, count(), InCSetState::Old);
sjohanss@7118 269 }
sjohanss@7118 270
sjohanss@7118 271 void OldGCAllocRegion::retire_region(HeapRegion* alloc_region,
sjohanss@7118 272 size_t allocated_bytes) {
tschatzl@7651 273 _g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, InCSetState::Old);
sjohanss@7118 274 }
sjohanss@7118 275
sjohanss@7118 276 HeapRegion* OldGCAllocRegion::release() {
sjohanss@7118 277 HeapRegion* cur = get();
sjohanss@7118 278 if (cur != NULL) {
sjohanss@7118 279 // Determine how far we are from the next card boundary. If it is smaller than
sjohanss@7118 280 // the minimum object size we can allocate into, expand into the next card.
sjohanss@7118 281 HeapWord* top = cur->top();
sjohanss@7118 282 HeapWord* aligned_top = (HeapWord*)align_ptr_up(top, G1BlockOffsetSharedArray::N_bytes);
sjohanss@7118 283
sjohanss@7118 284 size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize);
sjohanss@7118 285
sjohanss@7118 286 if (to_allocate_words != 0) {
sjohanss@7118 287 // We are not at a card boundary. Fill up, possibly into the next, taking the
sjohanss@7118 288 // end of the region and the minimum object size into account.
sjohanss@7118 289 to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize),
sjohanss@7118 290 MAX2(to_allocate_words, G1CollectedHeap::min_fill_size()));
sjohanss@7118 291
sjohanss@7118 292 // Skip allocation if there is not enough space to allocate even the smallest
sjohanss@7118 293 // possible object. In this case this region will not be retained, so the
sjohanss@7118 294 // original problem cannot occur.
sjohanss@7118 295 if (to_allocate_words >= G1CollectedHeap::min_fill_size()) {
sjohanss@7118 296 HeapWord* dummy = attempt_allocation(to_allocate_words, true /* bot_updates */);
sjohanss@7118 297 CollectedHeap::fill_with_object(dummy, to_allocate_words);
sjohanss@7118 298 }
sjohanss@7118 299 }
sjohanss@7118 300 }
sjohanss@7118 301 return G1AllocRegion::release();
sjohanss@7118 302 }
sjohanss@7118 303
sjohanss@7118 304

mercurial