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_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
  27 
  28 #include "gc_implementation/g1/g1AllocationContext.hpp"
  29 #include "gc_implementation/g1/g1AllocRegion.hpp"
  30 #include "gc_implementation/g1/g1InCSetState.hpp"
  31 #include "gc_implementation/shared/plab.hpp"
  32 #include "gc_interface/collectedHeap.hpp"
  33 
  34 class EvacuationInfo;
  35 
  36 // Base class for G1 allocators.
  37 class G1Allocator : public CHeapObj<mtGC> {
  38   friend class VMStructs;
  39 protected:
  40   G1CollectedHeap* _g1h;
  41 
  42   // Outside of GC pauses, the number of bytes used in all regions other
  43   // than the current allocation region.
  44   size_t _summary_bytes_used;
  45 
  46 public:
  47    G1Allocator(G1CollectedHeap* heap) :
  48      _g1h(heap), _summary_bytes_used(0) { }
  49 
  50    static G1Allocator* create_allocator(G1CollectedHeap* g1h);
  51 
  52    virtual void init_mutator_alloc_region() = 0;
  53    virtual void release_mutator_alloc_region() = 0;
  54 
  55    virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
  56    virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
  57    virtual void abandon_gc_alloc_regions() = 0;
  58 
  59    virtual MutatorAllocRegion*    mutator_alloc_region(AllocationContext_t context) = 0;
  60    virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0;
  61    virtual OldGCAllocRegion*      old_gc_alloc_region(AllocationContext_t context) = 0;
  62    virtual size_t                 used() = 0;
  63    virtual bool                   is_retained_old_region(HeapRegion* hr) = 0;
  64 
  65    void                           reuse_retained_old_region(EvacuationInfo& evacuation_info,
  66                                                             OldGCAllocRegion* old,
  67                                                             HeapRegion** retained);
  68 
  69    size_t used_unlocked() const {
  70      return _summary_bytes_used;
  71    }
  72 
  73    void increase_used(size_t bytes) {
  74      _summary_bytes_used += bytes;
  75    }
  76 
  77    void decrease_used(size_t bytes) {
  78      assert(_summary_bytes_used >= bytes,
  79             err_msg("invariant: _summary_bytes_used: "SIZE_FORMAT" should be >= bytes: "SIZE_FORMAT,
  80                 _summary_bytes_used, bytes));
  81      _summary_bytes_used -= bytes;
  82    }
  83 
  84    void set_used(size_t bytes) {
  85      _summary_bytes_used = bytes;
  86    }
  87 
  88    virtual HeapRegion* new_heap_region(uint hrs_index,
  89                                        G1BlockOffsetSharedArray* sharedOffsetArray,
  90                                        MemRegion mr) {
  91      return new HeapRegion(hrs_index, sharedOffsetArray, mr);
  92    }
  93 };
  94 
  95 // The default allocator for G1.
  96 class G1DefaultAllocator : public G1Allocator {
  97 protected:
  98   // Alloc region used to satisfy mutator allocation requests.
  99   MutatorAllocRegion _mutator_alloc_region;
 100 
 101   // Alloc region used to satisfy allocation requests by the GC for
 102   // survivor objects.
 103   SurvivorGCAllocRegion _survivor_gc_alloc_region;
 104 
 105   // Alloc region used to satisfy allocation requests by the GC for
 106   // old objects.
 107   OldGCAllocRegion _old_gc_alloc_region;
 108 
 109   HeapRegion* _retained_old_gc_alloc_region;
 110 public:
 111   G1DefaultAllocator(G1CollectedHeap* heap) : G1Allocator(heap), _retained_old_gc_alloc_region(NULL) { }
 112 
 113   virtual void init_mutator_alloc_region();
 114   virtual void release_mutator_alloc_region();
 115 
 116   virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
 117   virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info);
 118   virtual void abandon_gc_alloc_regions();
 119 
 120   virtual bool is_retained_old_region(HeapRegion* hr) {
 121     return _retained_old_gc_alloc_region == hr;
 122   }
 123 
 124   virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) {
 125     return &_mutator_alloc_region;
 126   }
 127 
 128   virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) {
 129     return &_survivor_gc_alloc_region;
 130   }
 131 
 132   virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) {
 133     return &_old_gc_alloc_region;
 134   }
 135 
 136   virtual size_t used() {
 137     assert(Heap_lock->owner() != NULL,
 138            "Should be owned on this thread's behalf.");
 139     size_t result = _summary_bytes_used;
 140 
 141     // Read only once in case it is set to NULL concurrently
 142     HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get();
 143     if (hr != NULL) {
 144       result += hr->used();
 145     }
 146     return result;
 147   }
 148 };
 149 
 150 class G1PLAB: public PLAB {
 151 private:
 152   bool _retired;
 153 
 154 public:
 155   G1PLAB(size_t gclab_word_size);
 156   virtual ~G1PLAB() {
 157     guarantee(_retired, "Allocation buffer has not been retired");
 158   }
 159 
 160   virtual void set_buf(HeapWord* buf) {
 161     PLAB::set_buf(buf);
 162     _retired = false;
 163   }
 164 
 165   virtual void retire() {
 166     if (_retired) {
 167       return;
 168     }
 169     PLAB::retire();
 170     _retired = true;
 171   }
 172 };
 173 
 174 class G1ParGCAllocator : public CHeapObj<mtGC> {
 175   friend class G1ParScanThreadState;
 176 protected:
 177   G1CollectedHeap* _g1h;
 178 
 179   // The survivor alignment in effect in bytes.
 180   // == 0 : don't align survivors
 181   // != 0 : align survivors to that alignment
 182   // These values were chosen to favor the non-alignment case since some
 183   // architectures have a special compare against zero instructions.
 184   const uint _survivor_alignment_bytes;
 185 
 186   size_t _alloc_buffer_waste;
 187   size_t _undo_waste;
 188 
 189   void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
 190   void add_to_undo_waste(size_t waste)         { _undo_waste += waste; }
 191 
 192   virtual void retire_alloc_buffers() = 0;
 193   virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0;
 194 
 195   // Calculate the survivor space object alignment in bytes. Returns that or 0 if
 196   // there are no restrictions on survivor alignment.
 197   static uint calc_survivor_alignment_bytes() {
 198     assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity");
 199     if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) {
 200       // No need to align objects in the survivors differently, return 0
 201       // which means "survivor alignment is not used".
 202       return 0;
 203     } else {
 204       assert(SurvivorAlignmentInBytes > 0, "sanity");
 205       return SurvivorAlignmentInBytes;
 206     }
 207   }
 208 
 209 public:
 210   G1ParGCAllocator(G1CollectedHeap* g1h) :
 211     _g1h(g1h), _survivor_alignment_bytes(calc_survivor_alignment_bytes()),
 212     _alloc_buffer_waste(0), _undo_waste(0) {
 213   }
 214 
 215   static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h);
 216 
 217   size_t alloc_buffer_waste() { return _alloc_buffer_waste; }
 218   size_t undo_waste() {return _undo_waste; }
 219 
 220   // Allocate word_sz words in dest, either directly into the regions or by
 221   // allocating a new PLAB. Returns the address of the allocated memory, NULL if
 222   // not successful.
 223   HeapWord* allocate_direct_or_new_plab(InCSetState dest,
 224                                         size_t word_sz,
 225                                         AllocationContext_t context);
 226 
 227   // Allocate word_sz words in the PLAB of dest.  Returns the address of the
 228   // allocated memory, NULL if not successful.
 229   HeapWord* plab_allocate(InCSetState dest,
 230                           size_t word_sz,
 231                           AllocationContext_t context) {
 232     G1PLAB* buffer = alloc_buffer(dest, context);
 233     if (_survivor_alignment_bytes == 0) {
 234       return buffer->allocate(word_sz);
 235     } else {
 236       return buffer->allocate_aligned(word_sz, _survivor_alignment_bytes);
 237     }
 238   }
 239 
 240   HeapWord* allocate(InCSetState dest, size_t word_sz,
 241                      AllocationContext_t context) {
 242     HeapWord* const obj = plab_allocate(dest, word_sz, context);
 243     if (obj != NULL) {
 244       return obj;
 245     }
 246     return allocate_direct_or_new_plab(dest, word_sz, context);
 247   }
 248 
 249   void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context) {
 250     if (alloc_buffer(dest, context)->contains(obj)) {
 251       assert(alloc_buffer(dest, context)->contains(obj + word_sz - 1),
 252              "should contain whole object");
 253       alloc_buffer(dest, context)->undo_allocation(obj, word_sz);
 254     } else {
 255       CollectedHeap::fill_with_object(obj, word_sz);
 256       add_to_undo_waste(word_sz);
 257     }
 258   }
 259 };
 260 
 261 class G1DefaultParGCAllocator : public G1ParGCAllocator {
 262   G1PLAB  _surviving_alloc_buffer;
 263   G1PLAB  _tenured_alloc_buffer;
 264   G1PLAB* _alloc_buffers[InCSetState::Num];
 265 
 266 public:
 267   G1DefaultParGCAllocator(G1CollectedHeap* g1h);
 268 
 269   virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) {
 270     assert(dest.is_valid(),
 271            err_msg("Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value()));
 272     assert(_alloc_buffers[dest.value()] != NULL,
 273            err_msg("Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value()));
 274     return _alloc_buffers[dest.value()];
 275   }
 276 
 277   virtual void retire_alloc_buffers() ;
 278 };
 279 
 280 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP