annotate src/share/vm/gc_implementation/g1/g1AllocRegion.cpp @ 5188:5afc5a089c2c

8014555: G1: Memory ordering problem with Conc refinement and card marking Summary: Add a StoreLoad barrier in the G1 post-barrier to fix a race with concurrent refinement. Reviewed-by: brutisso, tschatzl, roland
author mgerdin
date Tue, 08 Oct 2013 17:35:51 +0200
parents f44782f04dd4
children
rev   line source
tonyp@2417 1 /*
tonyp@3953 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
tonyp@2417 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
tonyp@2417 4 *
tonyp@2417 5 * This code is free software; you can redistribute it and/or modify it
tonyp@2417 6 * under the terms of the GNU General Public License version 2 only, as
tonyp@2417 7 * published by the Free Software Foundation.
tonyp@2417 8 *
tonyp@2417 9 * This code is distributed in the hope that it will be useful, but WITHOUT
tonyp@2417 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
tonyp@2417 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
tonyp@2417 12 * version 2 for more details (a copy is included in the LICENSE file that
tonyp@2417 13 * accompanied this code).
tonyp@2417 14 *
tonyp@2417 15 * You should have received a copy of the GNU General Public License version
tonyp@2417 16 * 2 along with this work; if not, write to the Free Software Foundation,
tonyp@2417 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
tonyp@2417 18 *
tonyp@2417 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
tonyp@2417 20 * or visit www.oracle.com if you need additional information or have any
tonyp@2417 21 * questions.
tonyp@2417 22 *
tonyp@2417 23 */
tonyp@2417 24
tonyp@2417 25 #include "precompiled.hpp"
tonyp@2417 26 #include "gc_implementation/g1/g1AllocRegion.inline.hpp"
tonyp@2417 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
tonyp@2417 28
tonyp@2417 29 G1CollectedHeap* G1AllocRegion::_g1h = NULL;
tonyp@2417 30 HeapRegion* G1AllocRegion::_dummy_region = NULL;
tonyp@2417 31
tonyp@2417 32 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
tonyp@2417 33 assert(_dummy_region == NULL, "should be set once");
tonyp@2417 34 assert(dummy_region != NULL, "pre-condition");
tonyp@2417 35 assert(dummy_region->free() == 0, "pre-condition");
tonyp@2417 36
tonyp@2417 37 // Make sure that any allocation attempt on this region will fail
tonyp@2417 38 // and will not trigger any asserts.
tonyp@2417 39 assert(allocate(dummy_region, 1, false) == NULL, "should fail");
tonyp@2417 40 assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
tonyp@2417 41 assert(allocate(dummy_region, 1, true) == NULL, "should fail");
tonyp@2417 42 assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
tonyp@2417 43
tonyp@2417 44 _g1h = g1h;
tonyp@2417 45 _dummy_region = dummy_region;
tonyp@2417 46 }
tonyp@2417 47
tonyp@2417 48 void G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
tonyp@2417 49 bool bot_updates) {
tonyp@2417 50 assert(alloc_region != NULL && alloc_region != _dummy_region,
tonyp@2417 51 "pre-condition");
tonyp@2417 52
tonyp@2417 53 // Other threads might still be trying to allocate using a CAS out
tonyp@2417 54 // of the region we are trying to retire, as they can do so without
tonyp@2417 55 // holding the lock. So, we first have to make sure that noone else
tonyp@2417 56 // can allocate out of it by doing a maximal allocation. Even if our
tonyp@2417 57 // CAS attempt fails a few times, we'll succeed sooner or later
tonyp@2417 58 // given that failed CAS attempts mean that the region is getting
tonyp@2417 59 // closed to being full.
tonyp@2417 60 size_t free_word_size = alloc_region->free() / HeapWordSize;
tonyp@2417 61
tonyp@2417 62 // This is the minimum free chunk we can turn into a dummy
tonyp@2417 63 // object. If the free space falls below this, then noone can
tonyp@2417 64 // allocate in this region anyway (all allocation requests will be
tonyp@2417 65 // of a size larger than this) so we won't have to perform the dummy
tonyp@2417 66 // allocation.
tonyp@2417 67 size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
tonyp@2417 68
tonyp@2417 69 while (free_word_size >= min_word_size_to_fill) {
tonyp@2417 70 HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
tonyp@2417 71 if (dummy != NULL) {
tonyp@2417 72 // If the allocation was successful we should fill in the space.
tonyp@2417 73 CollectedHeap::fill_with_object(dummy, free_word_size);
tonyp@2417 74 alloc_region->set_pre_dummy_top(dummy);
tonyp@2417 75 break;
tonyp@2417 76 }
tonyp@2417 77
tonyp@2417 78 free_word_size = alloc_region->free() / HeapWordSize;
tonyp@2417 79 // It's also possible that someone else beats us to the
tonyp@2417 80 // allocation and they fill up the region. In that case, we can
tonyp@2417 81 // just get out of the loop.
tonyp@2417 82 }
tonyp@2417 83 assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
tonyp@2417 84 "post-condition");
tonyp@2417 85 }
tonyp@2417 86
tonyp@2417 87 void G1AllocRegion::retire(bool fill_up) {
tonyp@2417 88 assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));
tonyp@2417 89
tonyp@2417 90 trace("retiring");
tonyp@2417 91 HeapRegion* alloc_region = _alloc_region;
tonyp@2417 92 if (alloc_region != _dummy_region) {
tonyp@2417 93 // We never have to check whether the active region is empty or not,
tonyp@2417 94 // and potentially free it if it is, given that it's guaranteed that
tonyp@2417 95 // it will never be empty.
tonyp@2417 96 assert(!alloc_region->is_empty(),
tonyp@2417 97 ar_ext_msg(this, "the alloc region should never be empty"));
tonyp@2417 98
tonyp@2417 99 if (fill_up) {
tonyp@2417 100 fill_up_remaining_space(alloc_region, _bot_updates);
tonyp@2417 101 }
tonyp@2417 102
tonyp@2417 103 assert(alloc_region->used() >= _used_bytes_before,
tonyp@2417 104 ar_ext_msg(this, "invariant"));
tonyp@2417 105 size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
tonyp@2417 106 retire_region(alloc_region, allocated_bytes);
tonyp@2417 107 _used_bytes_before = 0;
tonyp@2417 108 _alloc_region = _dummy_region;
tonyp@2417 109 }
tonyp@2417 110 trace("retired");
tonyp@2417 111 }
tonyp@2417 112
tonyp@2417 113 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
tonyp@2417 114 bool force) {
tonyp@2417 115 assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));
tonyp@2417 116 assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));
tonyp@2417 117
tonyp@2417 118 trace("attempting region allocation");
tonyp@2417 119 HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
tonyp@2417 120 if (new_alloc_region != NULL) {
tonyp@2417 121 new_alloc_region->reset_pre_dummy_top();
tonyp@2417 122 // Need to do this before the allocation
tonyp@2417 123 _used_bytes_before = new_alloc_region->used();
tonyp@2417 124 HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
tonyp@2417 125 assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));
tonyp@2417 126
tonyp@2417 127 OrderAccess::storestore();
tonyp@2417 128 // Note that we first perform the allocation and then we store the
tonyp@2417 129 // region in _alloc_region. This is the reason why an active region
tonyp@2417 130 // can never be empty.
tonyp@2417 131 _alloc_region = new_alloc_region;
tonyp@2825 132 _count += 1;
tonyp@2417 133 trace("region allocation successful");
tonyp@2417 134 return result;
tonyp@2417 135 } else {
tonyp@2417 136 trace("region allocation failed");
tonyp@2417 137 return NULL;
tonyp@2417 138 }
tonyp@2417 139 ShouldNotReachHere();
tonyp@2417 140 }
tonyp@2417 141
tonyp@2417 142 void G1AllocRegion::fill_in_ext_msg(ar_ext_msg* msg, const char* message) {
tonyp@3953 143 msg->append("[%s] %s c: %u b: %s r: "PTR_FORMAT" u: "SIZE_FORMAT,
tonyp@2825 144 _name, message, _count, BOOL_TO_STR(_bot_updates),
tonyp@2417 145 _alloc_region, _used_bytes_before);
tonyp@2417 146 }
tonyp@2417 147
tonyp@2417 148 void G1AllocRegion::init() {
tonyp@2417 149 trace("initializing");
tonyp@2417 150 assert(_alloc_region == NULL && _used_bytes_before == 0,
tonyp@2417 151 ar_ext_msg(this, "pre-condition"));
tonyp@2825 152 assert(_dummy_region != NULL, ar_ext_msg(this, "should have been set"));
tonyp@2417 153 _alloc_region = _dummy_region;
tonyp@2825 154 _count = 0;
tonyp@2417 155 trace("initialized");
tonyp@2417 156 }
tonyp@2417 157
tonyp@2825 158 void G1AllocRegion::set(HeapRegion* alloc_region) {
tonyp@2825 159 trace("setting");
tonyp@2825 160 // We explicitly check that the region is not empty to make sure we
tonyp@2825 161 // maintain the "the alloc region cannot be empty" invariant.
tonyp@2825 162 assert(alloc_region != NULL && !alloc_region->is_empty(),
tonyp@2825 163 ar_ext_msg(this, "pre-condition"));
tonyp@2825 164 assert(_alloc_region == _dummy_region &&
tonyp@2825 165 _used_bytes_before == 0 && _count == 0,
tonyp@2825 166 ar_ext_msg(this, "pre-condition"));
tonyp@2825 167
tonyp@2825 168 _used_bytes_before = alloc_region->used();
tonyp@2825 169 _alloc_region = alloc_region;
tonyp@2825 170 _count += 1;
tonyp@2825 171 trace("set");
tonyp@2825 172 }
tonyp@2825 173
tonyp@2417 174 HeapRegion* G1AllocRegion::release() {
tonyp@2417 175 trace("releasing");
tonyp@2417 176 HeapRegion* alloc_region = _alloc_region;
tonyp@2417 177 retire(false /* fill_up */);
tonyp@2825 178 assert(_alloc_region == _dummy_region,
tonyp@2825 179 ar_ext_msg(this, "post-condition of retire()"));
tonyp@2417 180 _alloc_region = NULL;
tonyp@2417 181 trace("released");
tonyp@2417 182 return (alloc_region == _dummy_region) ? NULL : alloc_region;
tonyp@2417 183 }
tonyp@2417 184
tonyp@2417 185 #if G1_ALLOC_REGION_TRACING
tonyp@2417 186 void G1AllocRegion::trace(const char* str, size_t word_size, HeapWord* result) {
tonyp@2417 187 // All the calls to trace that set either just the size or the size
tonyp@2417 188 // and the result are considered part of level 2 tracing and are
tonyp@2417 189 // skipped during level 1 tracing.
tonyp@2417 190 if ((word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
tonyp@2417 191 const size_t buffer_length = 128;
tonyp@2417 192 char hr_buffer[buffer_length];
tonyp@2417 193 char rest_buffer[buffer_length];
tonyp@2417 194
tonyp@2417 195 HeapRegion* alloc_region = _alloc_region;
tonyp@2417 196 if (alloc_region == NULL) {
tonyp@2417 197 jio_snprintf(hr_buffer, buffer_length, "NULL");
tonyp@2417 198 } else if (alloc_region == _dummy_region) {
tonyp@2417 199 jio_snprintf(hr_buffer, buffer_length, "DUMMY");
tonyp@2417 200 } else {
tonyp@2417 201 jio_snprintf(hr_buffer, buffer_length,
tonyp@2417 202 HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));
tonyp@2417 203 }
tonyp@2417 204
tonyp@2417 205 if (G1_ALLOC_REGION_TRACING > 1) {
tonyp@2417 206 if (result != NULL) {
tonyp@2417 207 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT" "PTR_FORMAT,
tonyp@2417 208 word_size, result);
tonyp@2417 209 } else if (word_size != 0) {
tonyp@2417 210 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT, word_size);
tonyp@2417 211 } else {
tonyp@2417 212 jio_snprintf(rest_buffer, buffer_length, "");
tonyp@2417 213 }
tonyp@2417 214 } else {
tonyp@2417 215 jio_snprintf(rest_buffer, buffer_length, "");
tonyp@2417 216 }
tonyp@2417 217
tonyp@3953 218 tty->print_cr("[%s] %u %s : %s %s",
tonyp@2825 219 _name, _count, hr_buffer, str, rest_buffer);
tonyp@2417 220 }
tonyp@2417 221 }
tonyp@2417 222 #endif // G1_ALLOC_REGION_TRACING
tonyp@2417 223
tonyp@2417 224 G1AllocRegion::G1AllocRegion(const char* name,
tonyp@2417 225 bool bot_updates)
tonyp@2417 226 : _name(name), _bot_updates(bot_updates),
tonyp@2825 227 _alloc_region(NULL), _count(0), _used_bytes_before(0) { }
tonyp@2417 228