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