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