1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1AllocRegion.inline.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "runtime/orderAccess.inline.hpp"
  29 
  30 G1CollectedHeap* G1AllocRegion::_g1h = NULL;
  31 HeapRegion* G1AllocRegion::_dummy_region = NULL;
  32 
  33 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
  34   assert(_dummy_region == NULL, "should be set once");
  35   assert(dummy_region != NULL, "pre-condition");
  36   assert(dummy_region->free() == 0, "pre-condition");
  37 
  38   // Make sure that any allocation attempt on this region will fail
  39   // and will not trigger any asserts.
  40   assert(allocate(dummy_region, 1, false) == NULL, "should fail");
  41   assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
  42   assert(allocate(dummy_region, 1, true) == NULL, "should fail");
  43   assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
  44 
  45   _g1h = g1h;
  46   _dummy_region = dummy_region;
  47 }
  48 
  49 size_t G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
  50                                               bool bot_updates) {
  51   assert(alloc_region != NULL && alloc_region != _dummy_region,
  52          "pre-condition");
  53   size_t result = 0;
  54 
  55   // Other threads might still be trying to allocate using a CAS out
  56   // of the region we are trying to retire, as they can do so without
  57   // holding the lock. So, we first have to make sure that noone else
  58   // can allocate out of it by doing a maximal allocation. Even if our
  59   // CAS attempt fails a few times, we'll succeed sooner or later
  60   // given that failed CAS attempts mean that the region is getting
  61   // closed to being full.
  62   size_t free_word_size = alloc_region->free() / HeapWordSize;
  63 
  64   // This is the minimum free chunk we can turn into a dummy
  65   // object. If the free space falls below this, then noone can
  66   // allocate in this region anyway (all allocation requests will be
  67   // of a size larger than this) so we won't have to perform the dummy
  68   // allocation.
  69   size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
  70 
  71   while (free_word_size >= min_word_size_to_fill) {
  72     HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
  73     if (dummy != NULL) {
  74       // If the allocation was successful we should fill in the space.
  75       CollectedHeap::fill_with_object(dummy, free_word_size);
  76       alloc_region->set_pre_dummy_top(dummy);
  77       result += free_word_size * HeapWordSize;
  78       break;
  79     }
  80 
  81     free_word_size = alloc_region->free() / HeapWordSize;
  82     // It's also possible that someone else beats us to the
  83     // allocation and they fill up the region. In that case, we can
  84     // just get out of the loop.
  85   }
  86   result += alloc_region->free();
  87 
  88   assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
  89          "post-condition");
  90   return result;
  91 }
  92 
  93 size_t G1AllocRegion::retire(bool fill_up) {
  94   G1_ALLOC_REGION_ASSERT(_alloc_region != NULL, "not initialized properly");
  95 
  96   size_t result = 0;
  97 
  98   trace("retiring");
  99   HeapRegion* alloc_region = _alloc_region;
 100   if (alloc_region != _dummy_region) {
 101     // We never have to check whether the active region is empty or not,
 102     // and potentially free it if it is, given that it's guaranteed that
 103     // it will never be empty.
 104     G1_ALLOC_REGION_ASSERT(!alloc_region->is_empty(),
 105                            "the alloc region should never be empty");
 106 
 107     if (fill_up) {
 108       result = fill_up_remaining_space(alloc_region, _bot_updates);
 109     }
 110 
 111     G1_ALLOC_REGION_ASSERT(alloc_region->used() >= _used_bytes_before, "invariant");
 112     size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
 113     retire_region(alloc_region, allocated_bytes);
 114     _used_bytes_before = 0;
 115     _alloc_region = _dummy_region;
 116   }
 117   trace("retired");
 118 
 119   return result;
 120 }
 121 
 122 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
 123                                                        bool force) {
 124   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region, "pre-condition");
 125   G1_ALLOC_REGION_ASSERT(_used_bytes_before == 0, "pre-condition");
 126 
 127   trace("attempting region allocation");
 128   HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
 129   if (new_alloc_region != NULL) {
 130     new_alloc_region->reset_pre_dummy_top();
 131     // Need to do this before the allocation
 132     _used_bytes_before = new_alloc_region->used();
 133     HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
 134     G1_ALLOC_REGION_ASSERT(result != NULL, "the allocation should succeeded");
 135 
 136     OrderAccess::storestore();
 137     // Note that we first perform the allocation and then we store the
 138     // region in _alloc_region. This is the reason why an active region
 139     // can never be empty.
 140     update_alloc_region(new_alloc_region);
 141     trace("region allocation successful");
 142     return result;
 143   } else {
 144     trace("region allocation failed");
 145     return NULL;
 146   }
 147   ShouldNotReachHere();
 148 }
 149 
 150 void G1AllocRegion::init() {
 151   trace("initializing");
 152   G1_ALLOC_REGION_ASSERT(_alloc_region == NULL && _used_bytes_before == 0, "pre-condition");
 153   G1_ALLOC_REGION_ASSERT(_dummy_region != NULL, "should have been set");
 154   _alloc_region = _dummy_region;
 155   _count = 0;
 156   trace("initialized");
 157 }
 158 
 159 void G1AllocRegion::set(HeapRegion* alloc_region) {
 160   trace("setting");
 161   // We explicitly check that the region is not empty to make sure we
 162   // maintain the "the alloc region cannot be empty" invariant.
 163   G1_ALLOC_REGION_ASSERT(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");
 164   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region &&
 165                          _used_bytes_before == 0 && _count == 0,
 166                          "pre-condition");
 167 
 168   _used_bytes_before = alloc_region->used();
 169   _alloc_region = alloc_region;
 170   _count += 1;
 171   trace("set");
 172 }
 173 
 174 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {
 175   trace("update");
 176   // We explicitly check that the region is not empty to make sure we
 177   // maintain the "the alloc region cannot be empty" invariant.
 178   G1_ALLOC_REGION_ASSERT(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");
 179 
 180   _alloc_region = alloc_region;
 181   _alloc_region->set_allocation_context(allocation_context());
 182   _count += 1;
 183   trace("updated");
 184 }
 185 
 186 HeapRegion* G1AllocRegion::release() {
 187   trace("releasing");
 188   HeapRegion* alloc_region = _alloc_region;
 189   retire(false /* fill_up */);
 190   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region, "post-condition of retire()");
 191   _alloc_region = NULL;
 192   trace("released");
 193   return (alloc_region == _dummy_region) ? NULL : alloc_region;
 194 }
 195 
 196 #if G1_ALLOC_REGION_TRACING
 197 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {
 198   // All the calls to trace that set either just the size or the size
 199   // and the result are considered part of level 2 tracing and are
 200   // skipped during level 1 tracing.
 201   if ((actual_word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
 202     const size_t buffer_length = 128;
 203     char hr_buffer[buffer_length];
 204     char rest_buffer[buffer_length];
 205 
 206     HeapRegion* alloc_region = _alloc_region;
 207     if (alloc_region == NULL) {
 208       jio_snprintf(hr_buffer, buffer_length, "NULL");
 209     } else if (alloc_region == _dummy_region) {
 210       jio_snprintf(hr_buffer, buffer_length, "DUMMY");
 211     } else {
 212       jio_snprintf(hr_buffer, buffer_length,
 213                    HR_FORMAT, HR_FORMAT_PARAMS(alloc_region));
 214     }
 215 
 216     if (G1_ALLOC_REGION_TRACING > 1) {
 217       if (result != NULL) {
 218         jio_snprintf(rest_buffer, buffer_length, "min " SIZE_FORMAT " desired " SIZE_FORMAT " actual " SIZE_FORMAT " " PTR_FORMAT,
 219                      min_word_size, desired_word_size, actual_word_size, result);
 220       } else if (min_word_size != 0) {
 221         jio_snprintf(rest_buffer, buffer_length, "min " SIZE_FORMAT " desired " SIZE_FORMAT, min_word_size, desired_word_size);
 222       } else {
 223         jio_snprintf(rest_buffer, buffer_length, "");
 224       }
 225     } else {
 226       jio_snprintf(rest_buffer, buffer_length, "");
 227     }
 228 
 229     tty->print_cr("[%s] %u %s : %s %s",
 230                   _name, _count, hr_buffer, str, rest_buffer);
 231   }
 232 }
 233 #endif // G1_ALLOC_REGION_TRACING
 234 
 235 G1AllocRegion::G1AllocRegion(const char* name,
 236                              bool bot_updates)
 237   : _name(name), _bot_updates(bot_updates),
 238     _alloc_region(NULL), _count(0), _used_bytes_before(0),
 239     _allocation_context(AllocationContext::system()) { }
 240 
 241 
 242 HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size,
 243                                                     bool force) {
 244   return _g1h->new_mutator_alloc_region(word_size, force);
 245 }
 246 
 247 void MutatorAllocRegion::retire_region(HeapRegion* alloc_region,
 248                                        size_t allocated_bytes) {
 249   _g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes);
 250 }
 251 
 252 HeapRegion* G1GCAllocRegion::allocate_new_region(size_t word_size,
 253                                                  bool force) {
 254   assert(!force, "not supported for GC alloc regions");
 255   return _g1h->new_gc_alloc_region(word_size, count(), _purpose);
 256 }
 257 
 258 void G1GCAllocRegion::retire_region(HeapRegion* alloc_region,
 259                                     size_t allocated_bytes) {
 260   _g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, _purpose);
 261 }
 262 
 263 size_t G1GCAllocRegion::retire(bool fill_up) {
 264   HeapRegion* retired = get();
 265   size_t end_waste = G1AllocRegion::retire(fill_up);
 266   // Do not count retirement of the dummy allocation region.
 267   if (retired != NULL) {
 268     _stats->add_region_end_waste(end_waste / HeapWordSize);
 269   }
 270   return end_waste;
 271 }
 272 
 273 HeapRegion* OldGCAllocRegion::release() {
 274   HeapRegion* cur = get();
 275   if (cur != NULL) {
 276     // Determine how far we are from the next card boundary. If it is smaller than
 277     // the minimum object size we can allocate into, expand into the next card.
 278     HeapWord* top = cur->top();
 279     HeapWord* aligned_top = (HeapWord*)align_ptr_up(top, G1BlockOffsetSharedArray::N_bytes);
 280 
 281     size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize);
 282 
 283     if (to_allocate_words != 0) {
 284       // We are not at a card boundary. Fill up, possibly into the next, taking the
 285       // end of the region and the minimum object size into account.
 286       to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize),
 287                                MAX2(to_allocate_words, G1CollectedHeap::min_fill_size()));
 288 
 289       // Skip allocation if there is not enough space to allocate even the smallest
 290       // possible object. In this case this region will not be retained, so the
 291       // original problem cannot occur.
 292       if (to_allocate_words >= G1CollectedHeap::min_fill_size()) {
 293         HeapWord* dummy = attempt_allocation(to_allocate_words, true /* bot_updates */);
 294         CollectedHeap::fill_with_object(dummy, to_allocate_words);
 295       }
 296     }
 297   }
 298   return G1AllocRegion::release();
 299 }