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

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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"
aoqi@0 28
aoqi@0 29 G1CollectedHeap* G1AllocRegion::_g1h = NULL;
aoqi@0 30 HeapRegion* G1AllocRegion::_dummy_region = NULL;
aoqi@0 31
aoqi@0 32 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
aoqi@0 33 assert(_dummy_region == NULL, "should be set once");
aoqi@0 34 assert(dummy_region != NULL, "pre-condition");
aoqi@0 35 assert(dummy_region->free() == 0, "pre-condition");
aoqi@0 36
aoqi@0 37 // Make sure that any allocation attempt on this region will fail
aoqi@0 38 // and will not trigger any asserts.
aoqi@0 39 assert(allocate(dummy_region, 1, false) == NULL, "should fail");
aoqi@0 40 assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
aoqi@0 41 assert(allocate(dummy_region, 1, true) == NULL, "should fail");
aoqi@0 42 assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
aoqi@0 43
aoqi@0 44 _g1h = g1h;
aoqi@0 45 _dummy_region = dummy_region;
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 void G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
aoqi@0 49 bool bot_updates) {
aoqi@0 50 assert(alloc_region != NULL && alloc_region != _dummy_region,
aoqi@0 51 "pre-condition");
aoqi@0 52
aoqi@0 53 // Other threads might still be trying to allocate using a CAS out
aoqi@0 54 // of the region we are trying to retire, as they can do so without
aoqi@0 55 // holding the lock. So, we first have to make sure that noone else
aoqi@0 56 // can allocate out of it by doing a maximal allocation. Even if our
aoqi@0 57 // CAS attempt fails a few times, we'll succeed sooner or later
aoqi@0 58 // given that failed CAS attempts mean that the region is getting
aoqi@0 59 // closed to being full.
aoqi@0 60 size_t free_word_size = alloc_region->free() / HeapWordSize;
aoqi@0 61
aoqi@0 62 // This is the minimum free chunk we can turn into a dummy
aoqi@0 63 // object. If the free space falls below this, then noone can
aoqi@0 64 // allocate in this region anyway (all allocation requests will be
aoqi@0 65 // of a size larger than this) so we won't have to perform the dummy
aoqi@0 66 // allocation.
aoqi@0 67 size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
aoqi@0 68
aoqi@0 69 while (free_word_size >= min_word_size_to_fill) {
aoqi@0 70 HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
aoqi@0 71 if (dummy != NULL) {
aoqi@0 72 // If the allocation was successful we should fill in the space.
aoqi@0 73 CollectedHeap::fill_with_object(dummy, free_word_size);
aoqi@0 74 alloc_region->set_pre_dummy_top(dummy);
aoqi@0 75 break;
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 free_word_size = alloc_region->free() / HeapWordSize;
aoqi@0 79 // It's also possible that someone else beats us to the
aoqi@0 80 // allocation and they fill up the region. In that case, we can
aoqi@0 81 // just get out of the loop.
aoqi@0 82 }
aoqi@0 83 assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
aoqi@0 84 "post-condition");
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 void G1AllocRegion::retire(bool fill_up) {
aoqi@0 88 assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));
aoqi@0 89
aoqi@0 90 trace("retiring");
aoqi@0 91 HeapRegion* alloc_region = _alloc_region;
aoqi@0 92 if (alloc_region != _dummy_region) {
aoqi@0 93 // We never have to check whether the active region is empty or not,
aoqi@0 94 // and potentially free it if it is, given that it's guaranteed that
aoqi@0 95 // it will never be empty.
aoqi@0 96 assert(!alloc_region->is_empty(),
aoqi@0 97 ar_ext_msg(this, "the alloc region should never be empty"));
aoqi@0 98
aoqi@0 99 if (fill_up) {
aoqi@0 100 fill_up_remaining_space(alloc_region, _bot_updates);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 assert(alloc_region->used() >= _used_bytes_before,
aoqi@0 104 ar_ext_msg(this, "invariant"));
aoqi@0 105 size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
aoqi@0 106 retire_region(alloc_region, allocated_bytes);
aoqi@0 107 _used_bytes_before = 0;
aoqi@0 108 _alloc_region = _dummy_region;
aoqi@0 109 }
aoqi@0 110 trace("retired");
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
aoqi@0 114 bool force) {
aoqi@0 115 assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));
aoqi@0 116 assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));
aoqi@0 117
aoqi@0 118 trace("attempting region allocation");
aoqi@0 119 HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
aoqi@0 120 if (new_alloc_region != NULL) {
aoqi@0 121 new_alloc_region->reset_pre_dummy_top();
aoqi@0 122 // Need to do this before the allocation
aoqi@0 123 _used_bytes_before = new_alloc_region->used();
aoqi@0 124 HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
aoqi@0 125 assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));
aoqi@0 126
aoqi@0 127 OrderAccess::storestore();
aoqi@0 128 // Note that we first perform the allocation and then we store the
aoqi@0 129 // region in _alloc_region. This is the reason why an active region
aoqi@0 130 // can never be empty.
aoqi@0 131 _alloc_region = new_alloc_region;
aoqi@0 132 _count += 1;
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
aoqi@0 174 HeapRegion* G1AllocRegion::release() {
aoqi@0 175 trace("releasing");
aoqi@0 176 HeapRegion* alloc_region = _alloc_region;
aoqi@0 177 retire(false /* fill_up */);
aoqi@0 178 assert(_alloc_region == _dummy_region,
aoqi@0 179 ar_ext_msg(this, "post-condition of retire()"));
aoqi@0 180 _alloc_region = NULL;
aoqi@0 181 trace("released");
aoqi@0 182 return (alloc_region == _dummy_region) ? NULL : alloc_region;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 #if G1_ALLOC_REGION_TRACING
aoqi@0 186 void G1AllocRegion::trace(const char* str, size_t word_size, HeapWord* result) {
aoqi@0 187 // All the calls to trace that set either just the size or the size
aoqi@0 188 // and the result are considered part of level 2 tracing and are
aoqi@0 189 // skipped during level 1 tracing.
aoqi@0 190 if ((word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
aoqi@0 191 const size_t buffer_length = 128;
aoqi@0 192 char hr_buffer[buffer_length];
aoqi@0 193 char rest_buffer[buffer_length];
aoqi@0 194
aoqi@0 195 HeapRegion* alloc_region = _alloc_region;
aoqi@0 196 if (alloc_region == NULL) {
aoqi@0 197 jio_snprintf(hr_buffer, buffer_length, "NULL");
aoqi@0 198 } else if (alloc_region == _dummy_region) {
aoqi@0 199 jio_snprintf(hr_buffer, buffer_length, "DUMMY");
aoqi@0 200 } else {
aoqi@0 201 jio_snprintf(hr_buffer, buffer_length,
aoqi@0 202 HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 if (G1_ALLOC_REGION_TRACING > 1) {
aoqi@0 206 if (result != NULL) {
aoqi@0 207 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT" "PTR_FORMAT,
aoqi@0 208 word_size, result);
aoqi@0 209 } else if (word_size != 0) {
aoqi@0 210 jio_snprintf(rest_buffer, buffer_length, SIZE_FORMAT, word_size);
aoqi@0 211 } else {
aoqi@0 212 jio_snprintf(rest_buffer, buffer_length, "");
aoqi@0 213 }
aoqi@0 214 } else {
aoqi@0 215 jio_snprintf(rest_buffer, buffer_length, "");
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 tty->print_cr("[%s] %u %s : %s %s",
aoqi@0 219 _name, _count, hr_buffer, str, rest_buffer);
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222 #endif // G1_ALLOC_REGION_TRACING
aoqi@0 223
aoqi@0 224 G1AllocRegion::G1AllocRegion(const char* name,
aoqi@0 225 bool bot_updates)
aoqi@0 226 : _name(name), _bot_updates(bot_updates),
aoqi@0 227 _alloc_region(NULL), _count(0), _used_bytes_before(0) { }
aoqi@0 228

mercurial