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/parGCAllocBuffer.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(uint no_of_gc_workers, 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(uint no_of_gc_workers, 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 G1PLABWasteStat VALUE_OBJ_CLASS_SPEC {
 151 private:
 152   size_t _wasted;
 153   size_t _undo_wasted;
 154 public:
 155 
 156   G1PLABWasteStat() : _wasted(0), _undo_wasted(0) {
 157   }
 158 
 159   void add_wasted(size_t word_sz) {
 160     _wasted += word_sz;
 161   }
 162 
 163   void add_undo_wasted(size_t word_sz) {
 164     _undo_wasted += word_sz;
 165   }
 166 
 167   size_t wasted() {
 168     return _wasted;
 169   }
 170 
 171   size_t undo_wasted() {
 172     return _undo_wasted;
 173   }
 174 };
 175 
 176 class G1ParGCAllocBuffer: public ParGCAllocBuffer {
 177 private:
 178   bool _retired;
 179 
 180 public:
 181   G1ParGCAllocBuffer(size_t gclab_word_size);
 182   virtual ~G1ParGCAllocBuffer() {
 183     guarantee(_retired, "Allocation buffer has not been retired");
 184   }
 185 
 186   virtual void set_buf(HeapWord* buf) {
 187     ParGCAllocBuffer::set_buf(buf);
 188     _retired = false;
 189   }
 190 
 191   virtual void retire() {
 192     if (_retired) {
 193       return;
 194     }
 195     ParGCAllocBuffer::retire();
 196     _retired = true;
 197   }
 198 };
 199 
 200 class G1ParGCAllocator : public CHeapObj<mtGC> {
 201   friend class G1ParScanThreadState;
 202 protected:
 203   G1CollectedHeap* _g1h;
 204 
 205   // The survivor alignment in effect in bytes.
 206   // == 0 : don't align survivors
 207   // != 0 : align survivors to that alignment
 208   // These values were chosen to favor the non-alignment case since some
 209   // architectures have a special compare against zero instructions.
 210   const uint _survivor_alignment_bytes;
 211 
 212   virtual void retire_alloc_buffers() = 0;
 213   virtual G1ParGCAllocBuffer* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0;
 214 
 215   // Calculate the survivor space object alignment in bytes. Returns that or 0 if
 216   // there are no restrictions on survivor alignment.
 217   static uint calc_survivor_alignment_bytes() {
 218     assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity");
 219     if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) {
 220       // No need to align objects in the survivors differently, return 0
 221       // which means "survivor alignment is not used".
 222       return 0;
 223     } else {
 224       assert(SurvivorAlignmentInBytes > 0, "sanity");
 225       return SurvivorAlignmentInBytes;
 226     }
 227   }
 228 
 229 public:
 230   G1ParGCAllocator(G1CollectedHeap* g1h) :
 231     _g1h(g1h), _survivor_alignment_bytes(calc_survivor_alignment_bytes()) {
 232   }
 233 
 234   static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h);
 235 
 236   virtual G1PLABWasteStat wasted() = 0;
 237   
 238   // Allocate word_sz words in dest, either directly into the regions or by
 239   // allocating a new PLAB. Returns the address of the allocated memory, NULL if
 240   // not successful.
 241   HeapWord* allocate_direct_or_new_plab(InCSetState dest,
 242                                         size_t word_sz,
 243                                         AllocationContext_t context);
 244 
 245   // Allocate word_sz words in the PLAB of dest.  Returns the address of the
 246   // allocated memory, NULL if not successful.
 247   HeapWord* plab_allocate(InCSetState dest,
 248                           size_t word_sz,
 249                           AllocationContext_t context) {
 250     G1ParGCAllocBuffer* buffer = alloc_buffer(dest, context);
 251     if (_survivor_alignment_bytes == 0) {
 252       return buffer->allocate(word_sz);
 253     } else {
 254       return buffer->allocate_aligned(word_sz, _survivor_alignment_bytes);
 255     }
 256   }
 257 
 258   HeapWord* allocate(InCSetState dest, size_t word_sz,
 259                      AllocationContext_t context) {
 260     HeapWord* const obj = plab_allocate(dest, word_sz, context);
 261     if (obj != NULL) {
 262       return obj;
 263     }
 264     return allocate_direct_or_new_plab(dest, word_sz, context);
 265   }
 266 
 267   void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context) {
 268     alloc_buffer(dest, context)->undo_allocation(obj, word_sz);
 269   }
 270 };
 271 
 272 class G1DefaultParGCAllocator : public G1ParGCAllocator {
 273   G1ParGCAllocBuffer  _surviving_alloc_buffer;
 274   G1ParGCAllocBuffer  _tenured_alloc_buffer;
 275   G1ParGCAllocBuffer* _alloc_buffers[InCSetState::Num];
 276 
 277 public:
 278   G1DefaultParGCAllocator(G1CollectedHeap* g1h);
 279 
 280   virtual G1ParGCAllocBuffer* alloc_buffer(InCSetState dest, AllocationContext_t context) {
 281     assert(dest.is_valid(),
 282            err_msg("Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value()));
 283     assert(_alloc_buffers[dest.value()] != NULL,
 284            err_msg("Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value()));
 285     return _alloc_buffers[dest.value()];
 286   }
 287 
 288   virtual void retire_alloc_buffers();
 289 
 290   virtual G1PLABWasteStat wasted();
 291 };
 292 
 293 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP