rev 52719 : [mq]: 8159440-marking-of-promoted-objects-to-concurrent

   1 /*
   2  * Copyright (c) 2014, 2018, 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/g1Allocator.inline.hpp"
  27 #include "gc/g1/g1AllocRegion.inline.hpp"
  28 #include "gc/g1/g1EvacStats.inline.hpp"
  29 #include "gc/g1/g1CollectedHeap.inline.hpp"
  30 #include "gc/g1/g1Policy.hpp"
  31 #include "gc/g1/heapRegion.inline.hpp"
  32 #include "gc/g1/heapRegionSet.inline.hpp"
  33 #include "gc/g1/heapRegionType.hpp"
  34 #include "utilities/align.hpp"
  35 
  36 G1Allocator::G1Allocator(G1CollectedHeap* heap) :
  37   _g1h(heap),
  38   _survivor_is_full(false),
  39   _old_is_full(false),
  40   _mutator_alloc_region(),
  41   _survivor_gc_alloc_region(heap->alloc_buffer_stats(InCSetState::Young)),
  42   _old_gc_alloc_region(heap->alloc_buffer_stats(InCSetState::Old)),
  43   _retained_old_gc_alloc_region(NULL) {
  44 }
  45 
  46 void G1Allocator::init_mutator_alloc_region() {
  47   assert(_mutator_alloc_region.get() == NULL, "pre-condition");
  48   _mutator_alloc_region.init();
  49 }
  50 
  51 void G1Allocator::release_mutator_alloc_region() {
  52   _mutator_alloc_region.release();
  53   assert(_mutator_alloc_region.get() == NULL, "post-condition");
  54 }
  55 
  56 bool G1Allocator::is_retained_old_region(HeapRegion* hr) {
  57   return _retained_old_gc_alloc_region == hr;
  58 }
  59 
  60 void G1Allocator::reuse_retained_old_region(EvacuationInfo& evacuation_info,
  61                                             OldGCAllocRegion* old,
  62                                             HeapRegion** retained_old) {
  63   HeapRegion* retained_region = *retained_old;
  64   *retained_old = NULL;
  65   assert(retained_region == NULL || !retained_region->is_archive(),
  66          "Archive region should not be alloc region (index %u)", retained_region->hrm_index());
  67 
  68   // We will discard the current GC alloc region if:
  69   // a) it's in the collection set (it can happen!),
  70   // b) it's already full (no point in using it),
  71   // c) it's empty (this means that it was emptied during
  72   // a cleanup and it should be on the free list now), or
  73   // d) it's humongous (this means that it was emptied
  74   // during a cleanup and was added to the free list, but
  75   // has been subsequently used to allocate a humongous
  76   // object that may be less than the region size).
  77   if (retained_region != NULL &&
  78       !retained_region->in_collection_set() &&
  79       !(retained_region->top() == retained_region->end()) &&
  80       !retained_region->is_empty() &&
  81       !retained_region->is_humongous()) {
  82     // The retained region was added to the old region set when it was
  83     // retired. We have to remove it now, since we don't allow regions
  84     // we allocate to in the region sets. We'll re-add it later, when
  85     // it's retired again.
  86     _g1h->old_set_remove(retained_region);


  87     old->set(retained_region);
  88     _g1h->hr_printer()->reuse(retained_region);
  89     evacuation_info.set_alloc_regions_used_before(retained_region->used());
  90   }
  91 }
  92 
  93 void G1Allocator::init_gc_alloc_regions(EvacuationInfo& evacuation_info) {
  94   assert_at_safepoint_on_vm_thread();
  95 
  96   _survivor_is_full = false;
  97   _old_is_full = false;
  98 
  99   _survivor_gc_alloc_region.init();
 100   _old_gc_alloc_region.init();
 101   reuse_retained_old_region(evacuation_info,
 102                             &_old_gc_alloc_region,
 103                             &_retained_old_gc_alloc_region);
 104 }
 105 
 106 void G1Allocator::release_gc_alloc_regions(EvacuationInfo& evacuation_info) {
 107   evacuation_info.set_allocation_regions(survivor_gc_alloc_region()->count() +
 108                                          old_gc_alloc_region()->count());
 109   survivor_gc_alloc_region()->release();
 110   // If we have an old GC alloc region to release, we'll save it in
 111   // _retained_old_gc_alloc_region. If we don't
 112   // _retained_old_gc_alloc_region will become NULL. This is what we
 113   // want either way so no reason to check explicitly for either
 114   // condition.
 115   _retained_old_gc_alloc_region = old_gc_alloc_region()->release();
 116 }
 117 
 118 void G1Allocator::abandon_gc_alloc_regions() {
 119   assert(survivor_gc_alloc_region()->get() == NULL, "pre-condition");
 120   assert(old_gc_alloc_region()->get() == NULL, "pre-condition");
 121   _retained_old_gc_alloc_region = NULL;
 122 }
 123 
 124 bool G1Allocator::survivor_is_full() const {
 125   return _survivor_is_full;
 126 }
 127 
 128 bool G1Allocator::old_is_full() const {
 129   return _old_is_full;
 130 }
 131 
 132 void G1Allocator::set_survivor_full() {
 133   _survivor_is_full = true;
 134 }
 135 
 136 void G1Allocator::set_old_full() {
 137   _old_is_full = true;
 138 }
 139 
 140 size_t G1Allocator::unsafe_max_tlab_alloc() {
 141   // Return the remaining space in the cur alloc region, but not less than
 142   // the min TLAB size.
 143 
 144   // Also, this value can be at most the humongous object threshold,
 145   // since we can't allow tlabs to grow big enough to accommodate
 146   // humongous objects.
 147 
 148   HeapRegion* hr = mutator_alloc_region()->get();
 149   size_t max_tlab = _g1h->max_tlab_size() * wordSize;
 150   if (hr == NULL) {
 151     return max_tlab;
 152   } else {
 153     return MIN2(MAX2(hr->free(), (size_t) MinTLABSize), max_tlab);
 154   }
 155 }
 156 
 157 size_t G1Allocator::used_in_alloc_regions() {
 158   assert(Heap_lock->owner() != NULL, "Should be owned on this thread's behalf.");
 159   return mutator_alloc_region()->used_in_alloc_regions();
 160 }
 161 
 162 
 163 HeapWord* G1Allocator::par_allocate_during_gc(InCSetState dest,
 164                                               size_t word_size) {
 165   size_t temp = 0;
 166   HeapWord* result = par_allocate_during_gc(dest, word_size, word_size, &temp);
 167   assert(result == NULL || temp == word_size,
 168          "Requested " SIZE_FORMAT " words, but got " SIZE_FORMAT " at " PTR_FORMAT,
 169          word_size, temp, p2i(result));
 170   return result;
 171 }
 172 
 173 HeapWord* G1Allocator::par_allocate_during_gc(InCSetState dest,
 174                                               size_t min_word_size,
 175                                               size_t desired_word_size,
 176                                               size_t* actual_word_size) {
 177   switch (dest.value()) {
 178     case InCSetState::Young:
 179       return survivor_attempt_allocation(min_word_size, desired_word_size, actual_word_size);
 180     case InCSetState::Old:
 181       return old_attempt_allocation(min_word_size, desired_word_size, actual_word_size);
 182     default:
 183       ShouldNotReachHere();
 184       return NULL; // Keep some compilers happy
 185   }
 186 }
 187 
 188 HeapWord* G1Allocator::survivor_attempt_allocation(size_t min_word_size,
 189                                                    size_t desired_word_size,
 190                                                    size_t* actual_word_size) {
 191   assert(!_g1h->is_humongous(desired_word_size),
 192          "we should not be seeing humongous-size allocations in this path");
 193 
 194   HeapWord* result = survivor_gc_alloc_region()->attempt_allocation(min_word_size,
 195                                                                     desired_word_size,
 196                                                                     actual_word_size);
 197   if (result == NULL && !survivor_is_full()) {
 198     MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);
 199     result = survivor_gc_alloc_region()->attempt_allocation_locked(min_word_size,
 200                                                                    desired_word_size,
 201                                                                    actual_word_size);
 202     if (result == NULL) {
 203       set_survivor_full();
 204     }
 205   }
 206   if (result != NULL) {
 207     _g1h->dirty_young_block(result, *actual_word_size);
 208   }
 209   return result;
 210 }
 211 
 212 HeapWord* G1Allocator::old_attempt_allocation(size_t min_word_size,
 213                                               size_t desired_word_size,
 214                                               size_t* actual_word_size) {
 215   assert(!_g1h->is_humongous(desired_word_size),
 216          "we should not be seeing humongous-size allocations in this path");
 217 
 218   HeapWord* result = old_gc_alloc_region()->attempt_allocation(min_word_size,
 219                                                                desired_word_size,
 220                                                                actual_word_size);
 221   if (result == NULL && !old_is_full()) {
 222     MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);
 223     result = old_gc_alloc_region()->attempt_allocation_locked(min_word_size,
 224                                                               desired_word_size,
 225                                                               actual_word_size);
 226     if (result == NULL) {
 227       set_old_full();
 228     }
 229   }
 230   return result;
 231 }
 232 
 233 uint G1PLABAllocator::calc_survivor_alignment_bytes() {
 234   assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity");
 235   if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) {
 236     // No need to align objects in the survivors differently, return 0
 237     // which means "survivor alignment is not used".
 238     return 0;
 239   } else {
 240     assert(SurvivorAlignmentInBytes > 0, "sanity");
 241     return SurvivorAlignmentInBytes;
 242   }
 243 }
 244 
 245 G1PLABAllocator::G1PLABAllocator(G1Allocator* allocator) :
 246   _g1h(G1CollectedHeap::heap()),
 247   _allocator(allocator),
 248   _surviving_alloc_buffer(_g1h->desired_plab_sz(InCSetState::Young)),
 249   _tenured_alloc_buffer(_g1h->desired_plab_sz(InCSetState::Old)),
 250   _survivor_alignment_bytes(calc_survivor_alignment_bytes()) {
 251   for (uint state = 0; state < InCSetState::Num; state++) {
 252     _direct_allocated[state] = 0;
 253     _alloc_buffers[state] = NULL;
 254   }
 255   _alloc_buffers[InCSetState::Young] = &_surviving_alloc_buffer;
 256   _alloc_buffers[InCSetState::Old]  = &_tenured_alloc_buffer;
 257 }
 258 
 259 bool G1PLABAllocator::may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const {
 260   return (allocation_word_sz * 100 < buffer_size * ParallelGCBufferWastePct);
 261 }
 262 
 263 HeapWord* G1PLABAllocator::allocate_direct_or_new_plab(InCSetState dest,
 264                                                        size_t word_sz,
 265                                                        bool* plab_refill_failed) {
 266   size_t plab_word_size = _g1h->desired_plab_sz(dest);
 267   size_t required_in_plab = PLAB::size_required_for_allocation(word_sz);
 268 
 269   // Only get a new PLAB if the allocation fits and it would not waste more than
 270   // ParallelGCBufferWastePct in the existing buffer.
 271   if ((required_in_plab <= plab_word_size) &&
 272     may_throw_away_buffer(required_in_plab, plab_word_size)) {
 273 
 274     PLAB* alloc_buf = alloc_buffer(dest);
 275     alloc_buf->retire();
 276 
 277     size_t actual_plab_size = 0;
 278     HeapWord* buf = _allocator->par_allocate_during_gc(dest,
 279                                                        required_in_plab,
 280                                                        plab_word_size,
 281                                                        &actual_plab_size);
 282 
 283     assert(buf == NULL || ((actual_plab_size >= required_in_plab) && (actual_plab_size <= plab_word_size)),
 284            "Requested at minimum " SIZE_FORMAT ", desired " SIZE_FORMAT " words, but got " SIZE_FORMAT " at " PTR_FORMAT,
 285            required_in_plab, plab_word_size, actual_plab_size, p2i(buf));
 286 
 287     if (buf != NULL) {
 288       alloc_buf->set_buf(buf, actual_plab_size);
 289 
 290       HeapWord* const obj = alloc_buf->allocate(word_sz);
 291       assert(obj != NULL, "PLAB should have been big enough, tried to allocate "
 292                           SIZE_FORMAT " requiring " SIZE_FORMAT " PLAB size " SIZE_FORMAT,
 293                           word_sz, required_in_plab, plab_word_size);
 294       return obj;
 295     }
 296     // Otherwise.
 297     *plab_refill_failed = true;
 298   }
 299   // Try direct allocation.
 300   HeapWord* result = _allocator->par_allocate_during_gc(dest, word_sz);
 301   if (result != NULL) {
 302     _direct_allocated[dest.value()] += word_sz;
 303   }
 304   return result;
 305 }
 306 
 307 void G1PLABAllocator::undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz) {
 308   alloc_buffer(dest)->undo_allocation(obj, word_sz);
 309 }
 310 
 311 void G1PLABAllocator::flush_and_retire_stats() {
 312   for (uint state = 0; state < InCSetState::Num; state++) {
 313     PLAB* const buf = _alloc_buffers[state];
 314     if (buf != NULL) {
 315       G1EvacStats* stats = _g1h->alloc_buffer_stats(state);
 316       buf->flush_and_retire_stats(stats);
 317       stats->add_direct_allocated(_direct_allocated[state]);
 318       _direct_allocated[state] = 0;
 319     }
 320   }
 321 }
 322 
 323 void G1PLABAllocator::waste(size_t& wasted, size_t& undo_wasted) {
 324   wasted = 0;
 325   undo_wasted = 0;
 326   for (uint state = 0; state < InCSetState::Num; state++) {
 327     PLAB * const buf = _alloc_buffers[state];
 328     if (buf != NULL) {
 329       wasted += buf->waste();
 330       undo_wasted += buf->undo_waste();
 331     }
 332   }
 333 }
 334 
 335 bool G1ArchiveAllocator::_archive_check_enabled = false;
 336 G1ArchiveRegionMap G1ArchiveAllocator::_closed_archive_region_map;
 337 G1ArchiveRegionMap G1ArchiveAllocator::_open_archive_region_map;
 338 
 339 G1ArchiveAllocator* G1ArchiveAllocator::create_allocator(G1CollectedHeap* g1h, bool open) {
 340   // Create the archive allocator, and also enable archive object checking
 341   // in mark-sweep, since we will be creating archive regions.
 342   G1ArchiveAllocator* result =  new G1ArchiveAllocator(g1h, open);
 343   enable_archive_object_check();
 344   return result;
 345 }
 346 
 347 bool G1ArchiveAllocator::alloc_new_region() {
 348   // Allocate the highest free region in the reserved heap,
 349   // and add it to our list of allocated regions. It is marked
 350   // archive and added to the old set.
 351   HeapRegion* hr = _g1h->alloc_highest_free_region();
 352   if (hr == NULL) {
 353     return false;
 354   }
 355   assert(hr->is_empty(), "expected empty region (index %u)", hr->hrm_index());
 356   if (_open) {
 357     hr->set_open_archive();
 358   } else {
 359     hr->set_closed_archive();
 360   }
 361   _g1h->g1_policy()->remset_tracker()->update_at_allocate(hr);
 362   _g1h->archive_set_add(hr);
 363   _g1h->hr_printer()->alloc(hr);
 364   _allocated_regions.append(hr);
 365   _allocation_region = hr;
 366 
 367   // Set up _bottom and _max to begin allocating in the lowest
 368   // min_region_size'd chunk of the allocated G1 region.
 369   _bottom = hr->bottom();
 370   _max = _bottom + HeapRegion::min_region_size_in_words();
 371 
 372   // Tell mark-sweep that objects in this region are not to be marked.
 373   set_range_archive(MemRegion(_bottom, HeapRegion::GrainWords), _open);
 374 
 375   // Since we've modified the old set, call update_sizes.
 376   _g1h->g1mm()->update_sizes();
 377   return true;
 378 }
 379 
 380 HeapWord* G1ArchiveAllocator::archive_mem_allocate(size_t word_size) {
 381   assert(word_size != 0, "size must not be zero");
 382   if (_allocation_region == NULL) {
 383     if (!alloc_new_region()) {
 384       return NULL;
 385     }
 386   }
 387   HeapWord* old_top = _allocation_region->top();
 388   assert(_bottom >= _allocation_region->bottom(),
 389          "inconsistent allocation state: " PTR_FORMAT " < " PTR_FORMAT,
 390          p2i(_bottom), p2i(_allocation_region->bottom()));
 391   assert(_max <= _allocation_region->end(),
 392          "inconsistent allocation state: " PTR_FORMAT " > " PTR_FORMAT,
 393          p2i(_max), p2i(_allocation_region->end()));
 394   assert(_bottom <= old_top && old_top <= _max,
 395          "inconsistent allocation state: expected "
 396          PTR_FORMAT " <= " PTR_FORMAT " <= " PTR_FORMAT,
 397          p2i(_bottom), p2i(old_top), p2i(_max));
 398 
 399   // Allocate the next word_size words in the current allocation chunk.
 400   // If allocation would cross the _max boundary, insert a filler and begin
 401   // at the base of the next min_region_size'd chunk. Also advance to the next
 402   // chunk if we don't yet cross the boundary, but the remainder would be too
 403   // small to fill.
 404   HeapWord* new_top = old_top + word_size;
 405   size_t remainder = pointer_delta(_max, new_top);
 406   if ((new_top > _max) ||
 407       ((new_top < _max) && (remainder < CollectedHeap::min_fill_size()))) {
 408     if (old_top != _max) {
 409       size_t fill_size = pointer_delta(_max, old_top);
 410       CollectedHeap::fill_with_object(old_top, fill_size);
 411       _summary_bytes_used += fill_size * HeapWordSize;
 412     }
 413     _allocation_region->set_top(_max);
 414     old_top = _bottom = _max;
 415 
 416     // Check if we've just used up the last min_region_size'd chunk
 417     // in the current region, and if so, allocate a new one.
 418     if (_bottom != _allocation_region->end()) {
 419       _max = _bottom + HeapRegion::min_region_size_in_words();
 420     } else {
 421       if (!alloc_new_region()) {
 422         return NULL;
 423       }
 424       old_top = _allocation_region->bottom();
 425     }
 426   }
 427   _allocation_region->set_top(old_top + word_size);
 428   _summary_bytes_used += word_size * HeapWordSize;
 429 
 430   return old_top;
 431 }
 432 
 433 void G1ArchiveAllocator::complete_archive(GrowableArray<MemRegion>* ranges,
 434                                           size_t end_alignment_in_bytes) {
 435   assert((end_alignment_in_bytes >> LogHeapWordSize) < HeapRegion::min_region_size_in_words(),
 436          "alignment " SIZE_FORMAT " too large", end_alignment_in_bytes);
 437   assert(is_aligned(end_alignment_in_bytes, HeapWordSize),
 438          "alignment " SIZE_FORMAT " is not HeapWord (%u) aligned", end_alignment_in_bytes, HeapWordSize);
 439 
 440   // If we've allocated nothing, simply return.
 441   if (_allocation_region == NULL) {
 442     return;
 443   }
 444 
 445   // If an end alignment was requested, insert filler objects.
 446   if (end_alignment_in_bytes != 0) {
 447     HeapWord* currtop = _allocation_region->top();
 448     HeapWord* newtop = align_up(currtop, end_alignment_in_bytes);
 449     size_t fill_size = pointer_delta(newtop, currtop);
 450     if (fill_size != 0) {
 451       if (fill_size < CollectedHeap::min_fill_size()) {
 452         // If the required fill is smaller than we can represent,
 453         // bump up to the next aligned address. We know we won't exceed the current
 454         // region boundary because the max supported alignment is smaller than the min
 455         // region size, and because the allocation code never leaves space smaller than
 456         // the min_fill_size at the top of the current allocation region.
 457         newtop = align_up(currtop + CollectedHeap::min_fill_size(),
 458                           end_alignment_in_bytes);
 459         fill_size = pointer_delta(newtop, currtop);
 460       }
 461       HeapWord* fill = archive_mem_allocate(fill_size);
 462       CollectedHeap::fill_with_objects(fill, fill_size);
 463     }
 464   }
 465 
 466   // Loop through the allocated regions, and create MemRegions summarizing
 467   // the allocated address range, combining contiguous ranges. Add the
 468   // MemRegions to the GrowableArray provided by the caller.
 469   int index = _allocated_regions.length() - 1;
 470   assert(_allocated_regions.at(index) == _allocation_region,
 471          "expected region %u at end of array, found %u",
 472          _allocation_region->hrm_index(), _allocated_regions.at(index)->hrm_index());
 473   HeapWord* base_address = _allocation_region->bottom();
 474   HeapWord* top = base_address;
 475 
 476   while (index >= 0) {
 477     HeapRegion* next = _allocated_regions.at(index);
 478     HeapWord* new_base = next->bottom();
 479     HeapWord* new_top = next->top();
 480     if (new_base != top) {
 481       ranges->append(MemRegion(base_address, pointer_delta(top, base_address)));
 482       base_address = new_base;
 483     }
 484     top = new_top;
 485     index = index - 1;
 486   }
 487 
 488   assert(top != base_address, "zero-sized range, address " PTR_FORMAT, p2i(base_address));
 489   ranges->append(MemRegion(base_address, pointer_delta(top, base_address)));
 490   _allocated_regions.clear();
 491   _allocation_region = NULL;
 492 };
--- EOF ---