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