rev 49944 : imported patch 8191471-region-logging-waste
rev 49946 : imported patch 8191471-g1-retained-mutator-region
rev 49947 : imported patch 8191471-tschatzl-comments
rev 49948 : imported patch 8191471-tschatzl-comments-move-wasted

   1 /*
   2  * Copyright (c) 2011, 2017, 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(dummy_region->allocate_no_bot_updates(1) == NULL, "should fail");
  46   assert(dummy_region->allocate(1) == NULL, "should fail");
  47   DEBUG_ONLY(size_t assert_tmp);
  48   assert(dummy_region->par_allocate_no_bot_updates(1, 1, &assert_tmp) == NULL, "should fail");
  49   assert(dummy_region->par_allocate(1, 1, &assert_tmp) == NULL, "should fail");
  50 
  51   _g1h = g1h;
  52   _dummy_region = dummy_region;
  53 }
  54 
  55 size_t G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region) {
  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);
  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);
 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);
 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   _count += 1;
 187   trace("updated");
 188 }
 189 
 190 HeapRegion* G1AllocRegion::release() {
 191   trace("releasing");
 192   HeapRegion* alloc_region = _alloc_region;
 193   retire(false /* fill_up */);
 194   assert_alloc_region(_alloc_region == _dummy_region, "post-condition of retire()");
 195   _alloc_region = NULL;
 196   trace("released");
 197   return (alloc_region == _dummy_region) ? NULL : alloc_region;
 198 }
 199 
 200 #ifndef PRODUCT
 201 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {
 202   // All the calls to trace that set either just the size or the size
 203   // and the result are considered part of detailed tracing and are
 204   // skipped during other tracing.
 205 
 206   Log(gc, alloc, region) log;
 207 
 208   if (!log.is_debug()) {
 209     return;
 210   }
 211 
 212   bool detailed_info = log.is_trace();
 213 
 214   if ((actual_word_size == 0 && result == NULL) || detailed_info) {
 215     ResourceMark rm;
 216     LogStream ls_trace(log.trace());
 217     LogStream ls_debug(log.debug());
 218     outputStream* out = detailed_info ? &ls_trace : &ls_debug;
 219 
 220     out->print("%s: %u ", _name, _count);
 221 
 222     if (_alloc_region == NULL) {
 223       out->print("NULL");
 224     } else if (_alloc_region == _dummy_region) {
 225       out->print("DUMMY");
 226     } else {
 227       out->print(HR_FORMAT, HR_FORMAT_PARAMS(_alloc_region));
 228     }
 229 
 230     out->print(" : %s", str);
 231 
 232     if (detailed_info) {
 233       if (result != NULL) {
 234         out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT " actual " SIZE_FORMAT " " PTR_FORMAT,
 235                      min_word_size, desired_word_size, actual_word_size, p2i(result));
 236       } else if (min_word_size != 0) {
 237         out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT, min_word_size, desired_word_size);
 238       }
 239     }
 240     out->cr();
 241   }
 242 }
 243 #endif // PRODUCT
 244 
 245 G1AllocRegion::G1AllocRegion(const char* name,
 246                              bool bot_updates)
 247   : _name(name), _bot_updates(bot_updates),
 248     _alloc_region(NULL), _count(0), _used_bytes_before(0) { }

 249 
 250 
 251 HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size,
 252                                                     bool force) {
 253   return _g1h->new_mutator_alloc_region(word_size, force);
 254 }
 255 
 256 void MutatorAllocRegion::retire_region(HeapRegion* alloc_region,
 257                                        size_t allocated_bytes) {
 258   _g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes);












































































 259 }
 260 
 261 HeapRegion* G1GCAllocRegion::allocate_new_region(size_t word_size,
 262                                                  bool force) {
 263   assert(!force, "not supported for GC alloc regions");
 264   return _g1h->new_gc_alloc_region(word_size, _purpose);
 265 }
 266 
 267 void G1GCAllocRegion::retire_region(HeapRegion* alloc_region,
 268                                     size_t allocated_bytes) {
 269   _g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, _purpose);
 270 }
 271 
 272 size_t G1GCAllocRegion::retire(bool fill_up) {
 273   HeapRegion* retired = get();
 274   size_t end_waste = G1AllocRegion::retire(fill_up);
 275   // Do not count retirement of the dummy allocation region.
 276   if (retired != NULL) {
 277     _stats->add_region_end_waste(end_waste / HeapWordSize);
 278   }
 279   return end_waste;
 280 }
 281 
 282 HeapRegion* OldGCAllocRegion::release() {
 283   HeapRegion* cur = get();
 284   if (cur != NULL) {
 285     // Determine how far we are from the next card boundary. If it is smaller than
 286     // the minimum object size we can allocate into, expand into the next card.
 287     HeapWord* top = cur->top();
 288     HeapWord* aligned_top = align_up(top, BOTConstants::N_bytes);
 289 
 290     size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize);
 291 
 292     if (to_allocate_words != 0) {
 293       // We are not at a card boundary. Fill up, possibly into the next, taking the
 294       // end of the region and the minimum object size into account.
 295       to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize),
 296                                MAX2(to_allocate_words, G1CollectedHeap::min_fill_size()));
 297 
 298       // Skip allocation if there is not enough space to allocate even the smallest
 299       // possible object. In this case this region will not be retained, so the
 300       // original problem cannot occur.
 301       if (to_allocate_words >= G1CollectedHeap::min_fill_size()) {
 302         HeapWord* dummy = attempt_allocation(to_allocate_words);
 303         CollectedHeap::fill_with_object(dummy, to_allocate_words);
 304       }
 305     }
 306   }
 307   return G1AllocRegion::release();
 308 }
--- EOF ---