1 /*
   2  * Copyright (c) 2014, 2015, 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 #ifndef SHARE_VM_GC_G1_G1ALLOCATOR_HPP
  26 #define SHARE_VM_GC_G1_G1ALLOCATOR_HPP
  27 
  28 #include "gc/g1/g1AllocRegion.hpp"
  29 #include "gc/g1/g1AllocationContext.hpp"
  30 #include "gc/g1/g1InCSetState.hpp"
  31 #include "gc/shared/collectedHeap.hpp"
  32 #include "gc/shared/plab.hpp"
  33 
  34 class EvacuationInfo;
  35 
  36 // Interface to keep track of which regions G1 is currently allocating into. Provides
  37 // some accessors (e.g. allocating into them, or getting their occupancy).
  38 // Also keeps track of retained regions across GCs.
  39 class G1Allocator : public CHeapObj<mtGC> {
  40   friend class VMStructs;
  41 private:
  42   bool _survivor_is_full;
  43   bool _old_is_full;
  44 protected:
  45   G1CollectedHeap* _g1h;
  46 
  47   virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) = 0;
  48 
  49   virtual bool survivor_is_full(AllocationContext_t context) const;
  50   virtual bool old_is_full(AllocationContext_t context) const;
  51 
  52   virtual void set_survivor_full(AllocationContext_t context);
  53   virtual void set_old_full(AllocationContext_t context);
  54   
  55   // Accessors to the allocation regions.
  56   virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0;
  57   virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) = 0;
  58 
  59   // Allocation attempt during GC for a survivor object / PLAB.
  60   inline HeapWord* survivor_attempt_allocation(size_t word_size,
  61                                                AllocationContext_t context);
  62   // Allocation attempt during GC for an old object / PLAB.
  63   inline HeapWord* old_attempt_allocation(size_t word_size,
  64                                           AllocationContext_t context);
  65 public:
  66   G1Allocator(G1CollectedHeap* heap) : _g1h(heap), _survivor_is_full(false), _old_is_full(false) { }
  67   virtual ~G1Allocator() { }
  68 
  69   static G1Allocator* create_allocator(G1CollectedHeap* g1h);
  70 
  71 #ifdef ASSERT
  72   // Do we currently have an active mutator region to allocate into?
  73   bool has_mutator_alloc_region(AllocationContext_t context) { return mutator_alloc_region(context)->get() != NULL; }
  74 #endif
  75   virtual void init_mutator_alloc_region() = 0;
  76   virtual void release_mutator_alloc_region() = 0;
  77 
  78   virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
  79   virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
  80   virtual void abandon_gc_alloc_regions() = 0;
  81 
  82   // Management of retained regions.
  83 
  84   virtual bool is_retained_old_region(HeapRegion* hr) = 0;
  85   void reuse_retained_old_region(EvacuationInfo& evacuation_info,
  86                                  OldGCAllocRegion* old,
  87                                  HeapRegion** retained);
  88 
  89   // Allocate blocks of memory during mutator time.
  90 
  91   inline HeapWord* attempt_allocation(size_t word_size, AllocationContext_t context);
  92   inline HeapWord* attempt_allocation_locked(size_t word_size, AllocationContext_t context);
  93   inline HeapWord* attempt_allocation_force(size_t word_size, AllocationContext_t context);
  94 
  95   size_t unsafe_max_tlab_alloc(AllocationContext_t context);
  96 
  97   // Allocate blocks of memory during garbage collection. Will ensure an
  98   // allocation region, either by picking one or expanding the
  99   // heap, and then allocate a block of the given size. The block
 100   // may not be a humongous - it must fit into a single heap region.
 101   HeapWord* par_allocate_during_gc(InCSetState dest,
 102                                    size_t word_size,
 103                                    AllocationContext_t context);
 104 
 105   virtual size_t used_in_alloc_regions() = 0;
 106 };
 107 
 108 // The default allocation region manager for G1. Provides a single mutator, survivor
 109 // and old generation allocation region.
 110 // Can retain the (single) old generation allocation region across GCs.
 111 class G1DefaultAllocator : public G1Allocator {
 112 protected:
 113   // Alloc region used to satisfy mutator allocation requests.
 114   MutatorAllocRegion _mutator_alloc_region;
 115 
 116   // Alloc region used to satisfy allocation requests by the GC for
 117   // survivor objects.
 118   SurvivorGCAllocRegion _survivor_gc_alloc_region;
 119 
 120   // Alloc region used to satisfy allocation requests by the GC for
 121   // old objects.
 122   OldGCAllocRegion _old_gc_alloc_region;
 123 
 124   HeapRegion* _retained_old_gc_alloc_region;
 125 public:
 126   G1DefaultAllocator(G1CollectedHeap* heap);
 127 
 128   virtual void init_mutator_alloc_region();
 129   virtual void release_mutator_alloc_region();
 130 
 131   virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
 132   virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info);
 133   virtual void abandon_gc_alloc_regions();
 134 
 135   virtual bool is_retained_old_region(HeapRegion* hr) {
 136     return _retained_old_gc_alloc_region == hr;
 137   }
 138 
 139   virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) {
 140     return &_mutator_alloc_region;
 141   }
 142 
 143   virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) {
 144     return &_survivor_gc_alloc_region;
 145   }
 146 
 147   virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) {
 148     return &_old_gc_alloc_region;
 149   }
 150 
 151   virtual size_t used_in_alloc_regions() {
 152     assert(Heap_lock->owner() != NULL,
 153            "Should be owned on this thread's behalf.");
 154     size_t result = 0;
 155 
 156     // Read only once in case it is set to NULL concurrently
 157     HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get();
 158     if (hr != NULL) {
 159       result += hr->used();
 160     }
 161     return result;
 162   }
 163 };
 164 
 165 class G1PLAB: public PLAB {
 166 private:
 167   bool _retired;
 168 
 169 public:
 170   G1PLAB(size_t gclab_word_size);
 171   virtual ~G1PLAB() {
 172     guarantee(_retired, "Allocation buffer has not been retired");
 173   }
 174 
 175   // The amount of space in words wasted within the PLAB including
 176   // waste due to refills and alignment.
 177   size_t wasted() const { return _wasted; }
 178 
 179   virtual void set_buf(HeapWord* buf, size_t word_size) {
 180     PLAB::set_buf(buf, word_size);
 181     _retired = false;
 182   }
 183 
 184   virtual void retire() {
 185     if (_retired) {
 186       return;
 187     }
 188     PLAB::retire();
 189     _retired = true;
 190   }
 191 
 192   virtual void flush_and_retire_stats(PLABStats* stats) {
 193     PLAB::flush_and_retire_stats(stats);
 194     _retired = true;
 195   }
 196 };
 197 
 198 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
 199 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some
 200 // statistics.
 201 class G1PLABAllocator : public CHeapObj<mtGC> {
 202   friend class G1ParScanThreadState;
 203 protected:
 204   G1CollectedHeap* _g1h;
 205   G1Allocator* _allocator;
 206 
 207   // The survivor alignment in effect in bytes.
 208   // == 0 : don't align survivors
 209   // != 0 : align survivors to that alignment
 210   // These values were chosen to favor the non-alignment case since some
 211   // architectures have a special compare against zero instructions.
 212   const uint _survivor_alignment_bytes;
 213 
 214   // Number of words allocated directly (not counting PLAB allocation).
 215   size_t _direct_allocated[InCSetState::Num];
 216 
 217   virtual void flush_and_retire_stats() = 0;
 218   virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0;
 219 
 220   // Calculate the survivor space object alignment in bytes. Returns that or 0 if
 221   // there are no restrictions on survivor alignment.
 222   static uint calc_survivor_alignment_bytes() {
 223     assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity");
 224     if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) {
 225       // No need to align objects in the survivors differently, return 0
 226       // which means "survivor alignment is not used".
 227       return 0;
 228     } else {
 229       assert(SurvivorAlignmentInBytes > 0, "sanity");
 230       return SurvivorAlignmentInBytes;
 231     }
 232   }
 233 
 234   HeapWord* allocate_new_plab(InCSetState dest,
 235                               size_t word_sz,
 236                               AllocationContext_t context);
 237 
 238   bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
 239 public:
 240   G1PLABAllocator(G1Allocator* allocator);
 241   virtual ~G1PLABAllocator() { }
 242 
 243   static G1PLABAllocator* create_allocator(G1Allocator* allocator);
 244 
 245   virtual void waste(size_t& wasted, size_t& undo_wasted) = 0;
 246 
 247   // Allocate word_sz words in dest, either directly into the regions or by
 248   // allocating a new PLAB. Returns the address of the allocated memory, NULL if
 249   // not successful. Plab_refill_failed indicates whether an attempt to refill the
 250   // PLAB failed or not.
 251   HeapWord* allocate_direct_or_new_plab(InCSetState dest,
 252                                         size_t word_sz,
 253                                         AllocationContext_t context,
 254                                         bool* plab_refill_failed);
 255 
 256   // Allocate word_sz words in the PLAB of dest.  Returns the address of the
 257   // allocated memory, NULL if not successful.
 258   inline HeapWord* plab_allocate(InCSetState dest,
 259                                  size_t word_sz,
 260                                  AllocationContext_t context);
 261 
 262   HeapWord* allocate(InCSetState dest,
 263                      size_t word_sz,
 264                      AllocationContext_t context,
 265                      bool* refill_failed) {
 266     HeapWord* const obj = plab_allocate(dest, word_sz, context);
 267     if (obj != NULL) {
 268       return obj;
 269     }
 270     return allocate_direct_or_new_plab(dest, word_sz, context, refill_failed);
 271   }
 272 
 273   void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context);
 274 };
 275 
 276 // The default PLAB allocator for G1. Keeps the current (single) PLAB for survivor
 277 // and old generation allocation.
 278 class G1DefaultPLABAllocator : public G1PLABAllocator {
 279   G1PLAB  _surviving_alloc_buffer;
 280   G1PLAB  _tenured_alloc_buffer;
 281   G1PLAB* _alloc_buffers[InCSetState::Num];
 282 
 283 public:
 284   G1DefaultPLABAllocator(G1Allocator* _allocator);
 285 
 286   virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) {
 287     assert(dest.is_valid(),
 288            err_msg("Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value()));
 289     assert(_alloc_buffers[dest.value()] != NULL,
 290            err_msg("Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value()));
 291     return _alloc_buffers[dest.value()];
 292   }
 293 
 294   virtual void flush_and_retire_stats();
 295 
 296   virtual void waste(size_t& wasted, size_t& undo_wasted);
 297 };
 298 
 299 // G1ArchiveAllocator is used to allocate memory in archive
 300 // regions. Such regions are not modifiable by GC, being neither
 301 // scavenged nor compacted, or even marked in the object header.
 302 // They can contain no pointers to non-archive heap regions,
 303 class G1ArchiveAllocator : public CHeapObj<mtGC> {
 304 
 305 protected:
 306   G1CollectedHeap* _g1h;
 307 
 308   // The current allocation region
 309   HeapRegion* _allocation_region;
 310 
 311   // Regions allocated for the current archive range.
 312   GrowableArray<HeapRegion*> _allocated_regions;
 313 
 314   // The number of bytes used in the current range.
 315   size_t _summary_bytes_used;
 316 
 317   // Current allocation window within the current region.
 318   HeapWord* _bottom;
 319   HeapWord* _top;
 320   HeapWord* _max;
 321 
 322   // Allocate a new region for this archive allocator.
 323   // Allocation is from the top of the reserved heap downward.
 324   bool alloc_new_region();
 325 
 326 public:
 327   G1ArchiveAllocator(G1CollectedHeap* g1h) :
 328     _g1h(g1h),
 329     _allocation_region(NULL),
 330     _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,
 331                                                          ResourceObj::C_HEAP),
 332                         2), true /* C_Heap */),
 333     _summary_bytes_used(0),
 334     _bottom(NULL),
 335     _top(NULL),
 336     _max(NULL) { }
 337 
 338   virtual ~G1ArchiveAllocator() {
 339     assert(_allocation_region == NULL, "_allocation_region not NULL");
 340   }
 341 
 342   static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h);
 343 
 344   // Allocate memory for an individual object.
 345   HeapWord* archive_mem_allocate(size_t word_size);
 346 
 347   // Return the memory ranges used in the current archive, after
 348   // aligning to the requested alignment.
 349   void complete_archive(GrowableArray<MemRegion>* ranges,
 350                         size_t end_alignment_in_bytes);
 351 
 352   // The number of bytes allocated by this allocator.
 353   size_t used() {
 354     return _summary_bytes_used;
 355   }
 356 
 357   // Clear the count of bytes allocated in prior G1 regions. This
 358   // must be done when recalculate_use is used to reset the counter
 359   // for the generic allocator, since it counts bytes in all G1
 360   // regions, including those still associated with this allocator.
 361   void clear_used() {
 362     _summary_bytes_used = 0;
 363   }
 364 
 365 };
 366 
 367 #endif // SHARE_VM_GC_G1_G1ALLOCATOR_HPP