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 "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/orderAccess.inline.hpp"
  33 #include "utilities/align.hpp"
  34 
  35 G1CollectedHeap* G1AllocRegion::_g1h = NULL;
  36 HeapRegion* G1AllocRegion::_dummy_region = NULL;
  37 
  38 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) {
  39   assert(_dummy_region == NULL, "should be set once");
  40   assert(dummy_region != NULL, "pre-condition");
  41   assert(dummy_region->free() == 0, "pre-condition");
  42 
  43   // Make sure that any allocation attempt on this region will fail
  44   // and will not trigger any asserts.
  45   assert(allocate(dummy_region, 1, false) == NULL, "should fail");
  46   assert(par_allocate(dummy_region, 1, false) == NULL, "should fail");
  47   assert(allocate(dummy_region, 1, true) == NULL, "should fail");
  48   assert(par_allocate(dummy_region, 1, true) == NULL, "should fail");
  49 
  50   _g1h = g1h;
  51   _dummy_region = dummy_region;
  52 }
  53 
  54 size_t G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region,
  55                                               bool bot_updates) {
  56   assert(alloc_region != NULL && alloc_region != _dummy_region,
  57          "pre-condition");
  58   size_t result = 0;
  59 
  60   // Other threads might still be trying to allocate using a CAS out
  61   // of the region we are trying to retire, as they can do so without
  62   // holding the lock. So, we first have to make sure that noone else
  63   // can allocate out of it by doing a maximal allocation. Even if our
  64   // CAS attempt fails a few times, we'll succeed sooner or later
  65   // given that failed CAS attempts mean that the region is getting
  66   // closed to being full.
  67   size_t free_word_size = alloc_region->free() / HeapWordSize;
  68 
  69   // This is the minimum free chunk we can turn into a dummy
  70   // object. If the free space falls below this, then noone can
  71   // allocate in this region anyway (all allocation requests will be
  72   // of a size larger than this) so we won't have to perform the dummy
  73   // allocation.
  74   size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
  75 
  76   while (free_word_size >= min_word_size_to_fill) {
  77     HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates);
  78     if (dummy != NULL) {
  79       // If the allocation was successful we should fill in the space.
  80       CollectedHeap::fill_with_object(dummy, free_word_size);
  81       alloc_region->set_pre_dummy_top(dummy);
  82       result += free_word_size * HeapWordSize;
  83       break;
  84     }
  85 
  86     free_word_size = alloc_region->free() / HeapWordSize;
  87     // It's also possible that someone else beats us to the
  88     // allocation and they fill up the region. In that case, we can
  89     // just get out of the loop.
  90   }
  91   result += alloc_region->free();
  92 
  93   assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
  94          "post-condition");
  95   return result;
  96 }
  97 
  98 size_t G1AllocRegion::retire(bool fill_up) {
  99   assert_alloc_region(_alloc_region != NULL, "not initialized properly");
 100 
 101   size_t result = 0;
 102 
 103   trace("retiring");
 104   HeapRegion* alloc_region = _alloc_region;
 105   if (alloc_region != _dummy_region) {
 106     // We never have to check whether the active region is empty or not,
 107     // and potentially free it if it is, given that it's guaranteed that
 108     // it will never be empty.
 109     assert_alloc_region(!alloc_region->is_empty(),
 110                            "the alloc region should never be empty");
 111 
 112     if (fill_up) {
 113       result = fill_up_remaining_space(alloc_region, _bot_updates);
 114     }
 115 
 116     assert_alloc_region(alloc_region->used() >= _used_bytes_before, "invariant");
 117     size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
 118     retire_region(alloc_region, allocated_bytes);
 119     _used_bytes_before = 0;
 120     _alloc_region = _dummy_region;
 121   }
 122   trace("retired");
 123 
 124   return result;
 125 }
 126 
 127 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
 128                                                        bool force) {
 129   assert_alloc_region(_alloc_region == _dummy_region, "pre-condition");
 130   assert_alloc_region(_used_bytes_before == 0, "pre-condition");
 131 
 132   trace("attempting region allocation");
 133   HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
 134   if (new_alloc_region != NULL) {
 135     new_alloc_region->reset_pre_dummy_top();
 136     // Need to do this before the allocation
 137     _used_bytes_before = new_alloc_region->used();
 138     HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
 139     assert_alloc_region(result != NULL, "the allocation should succeeded");
 140 
 141     OrderAccess::storestore();
 142     // Note that we first perform the allocation and then we store the
 143     // region in _alloc_region. This is the reason why an active region
 144     // can never be empty.
 145     update_alloc_region(new_alloc_region);
 146     trace("region allocation successful");
 147     return result;
 148   } else {
 149     trace("region allocation failed");
 150     return NULL;
 151   }
 152   ShouldNotReachHere();
 153 }
 154 
 155 void G1AllocRegion::init() {
 156   trace("initializing");
 157   assert_alloc_region(_alloc_region == NULL && _used_bytes_before == 0, "pre-condition");
 158   assert_alloc_region(_dummy_region != NULL, "should have been set");
 159   _alloc_region = _dummy_region;
 160   _count = 0;
 161   trace("initialized");
 162 }
 163 
 164 void G1AllocRegion::set(HeapRegion* alloc_region) {
 165   trace("setting");
 166   // We explicitly check that the region is not empty to make sure we
 167   // maintain the "the alloc region cannot be empty" invariant.
 168   assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");
 169   assert_alloc_region(_alloc_region == _dummy_region &&
 170                          _used_bytes_before == 0 && _count == 0,
 171                          "pre-condition");
 172 
 173   _used_bytes_before = alloc_region->used();
 174   _alloc_region = alloc_region;
 175   _count += 1;
 176   trace("set");
 177 }
 178 
 179 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {
 180   trace("update");
 181   // We explicitly check that the region is not empty to make sure we
 182   // maintain the "the alloc region cannot be empty" invariant.
 183   assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");
 184 
 185   _alloc_region = alloc_region;
 186   _alloc_region->set_allocation_context(allocation_context());
 187   _count += 1;
 188   trace("updated");
 189 }
 190 
 191 HeapRegion* G1AllocRegion::release() {
 192   trace("releasing");
 193   HeapRegion* alloc_region = _alloc_region;
 194   retire(false /* fill_up */);
 195   assert_alloc_region(_alloc_region == _dummy_region, "post-condition of retire()");
 196   _alloc_region = NULL;
 197   trace("released");
 198   return (alloc_region == _dummy_region) ? NULL : alloc_region;
 199 }
 200 
 201 #ifndef PRODUCT
 202 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {
 203   // All the calls to trace that set either just the size or the size
 204   // and the result are considered part of detailed tracing and are
 205   // skipped during other tracing.
 206 
 207   Log(gc, alloc, region) log;
 208 
 209   if (!log.is_debug()) {
 210     return;
 211   }
 212 
 213   bool detailed_info = log.is_trace();
 214 
 215   if ((actual_word_size == 0 && result == NULL) || detailed_info) {
 216     ResourceMark rm;
 217     LogStream ls_trace(log.trace());
 218     LogStream ls_debug(log.debug());
 219     outputStream* out = detailed_info ? &ls_trace : &ls_debug;
 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 = align_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 }