1 /*
   2  * Copyright (c) 2014, 2019, 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_GC_G1_G1ALLOCATOR_HPP
  26 #define SHARE_GC_G1_G1ALLOCATOR_HPP
  27 
  28 #include "gc/g1/g1AllocRegion.hpp"
  29 #include "gc/g1/g1HeapRegionAttr.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "gc/shared/plab.hpp"
  32 
  33 class G1EvacuationInfo;
  34 class G1MemoryNodeManager;
  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 
  42 private:
  43   G1CollectedHeap* _g1h;
  44   G1MemoryNodeManager* _mnm;
  45 
  46   bool _survivor_is_full;
  47   bool _old_is_full;
  48 
  49   // The number of MutatorAllocRegions used, one per memory node.
  50   size_t _num_alloc_regions;
  51 
  52   // Alloc region used to satisfy mutator allocation requests.
  53   MutatorAllocRegion* _mutator_alloc_regions;
  54 
  55   // Alloc region used to satisfy allocation requests by the GC for
  56   // survivor objects.
  57   SurvivorGCAllocRegion _survivor_gc_alloc_region;
  58 
  59   // Alloc region used to satisfy allocation requests by the GC for
  60   // old objects.
  61   OldGCAllocRegion _old_gc_alloc_region;
  62 
  63   HeapRegion* _retained_old_gc_alloc_region;
  64 
  65   bool survivor_is_full() const;
  66   bool old_is_full() const;
  67 
  68   void set_survivor_full();
  69   void set_old_full();
  70 
  71   void reuse_retained_old_region(G1EvacuationInfo& evacuation_info,
  72                                  OldGCAllocRegion* old,
  73                                  HeapRegion** retained);
  74 
  75   // Accessors to the allocation regions.
  76   inline MutatorAllocRegion* mutator_alloc_region(uint node_index);
  77   inline SurvivorGCAllocRegion* survivor_gc_alloc_region();
  78   inline OldGCAllocRegion* old_gc_alloc_region();
  79 
  80   // Allocation attempt during GC for a survivor object / PLAB.
  81   HeapWord* survivor_attempt_allocation(size_t min_word_size,
  82                                         size_t desired_word_size,
  83                                         size_t* actual_word_size);
  84 
  85   // Allocation attempt during GC for an old object / PLAB.
  86   HeapWord* old_attempt_allocation(size_t min_word_size,
  87                                    size_t desired_word_size,
  88                                    size_t* actual_word_size);
  89 
  90   // Node index of current thread.
  91   inline uint current_node_index() const;
  92 
  93 public:
  94   G1Allocator(G1CollectedHeap* heap);
  95   ~G1Allocator();
  96 
  97 #ifdef ASSERT
  98   // Do we currently have an active mutator region to allocate into?
  99   bool has_mutator_alloc_region();
 100 #endif
 101 
 102   void init_mutator_alloc_regions();
 103   void release_mutator_alloc_regions();
 104 
 105   void init_gc_alloc_regions(G1EvacuationInfo& evacuation_info);
 106   void release_gc_alloc_regions(G1EvacuationInfo& evacuation_info);
 107   void abandon_gc_alloc_regions();
 108   bool is_retained_old_region(HeapRegion* hr);
 109 
 110   // Allocate blocks of memory during mutator time.
 111 
 112   inline HeapWord* attempt_allocation(size_t min_word_size,
 113                                       size_t desired_word_size,
 114                                       size_t* actual_word_size);
 115   inline HeapWord* attempt_allocation_locked(size_t word_size);
 116   inline HeapWord* attempt_allocation_force(size_t word_size);
 117 
 118   size_t unsafe_max_tlab_alloc();
 119   size_t used_in_alloc_regions();
 120 
 121   // Allocate blocks of memory during garbage collection. Will ensure an
 122   // allocation region, either by picking one or expanding the
 123   // heap, and then allocate a block of the given size. The block
 124   // may not be a humongous - it must fit into a single heap region.
 125   HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
 126                                    size_t word_size);
 127 
 128   HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
 129                                    size_t min_word_size,
 130                                    size_t desired_word_size,
 131                                    size_t* actual_word_size);
 132 };
 133 
 134 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
 135 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some
 136 // statistics.
 137 class G1PLABAllocator : public CHeapObj<mtGC> {
 138   friend class G1ParScanThreadState;
 139 private:
 140   G1CollectedHeap* _g1h;
 141   G1Allocator* _allocator;
 142 
 143   PLAB  _surviving_alloc_buffer;
 144   PLAB  _tenured_alloc_buffer;
 145   PLAB* _alloc_buffers[G1HeapRegionAttr::Num];
 146 
 147   // The survivor alignment in effect in bytes.
 148   // == 0 : don't align survivors
 149   // != 0 : align survivors to that alignment
 150   // These values were chosen to favor the non-alignment case since some
 151   // architectures have a special compare against zero instructions.
 152   const uint _survivor_alignment_bytes;
 153 
 154   // Number of words allocated directly (not counting PLAB allocation).
 155   size_t _direct_allocated[G1HeapRegionAttr::Num];
 156 
 157   void flush_and_retire_stats();
 158   inline PLAB* alloc_buffer(G1HeapRegionAttr dest);
 159 
 160   // Calculate the survivor space object alignment in bytes. Returns that or 0 if
 161   // there are no restrictions on survivor alignment.
 162   static uint calc_survivor_alignment_bytes();
 163 
 164   bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
 165 public:
 166   G1PLABAllocator(G1Allocator* allocator);
 167 
 168   size_t waste() const;
 169   size_t undo_waste() const;
 170 
 171   // Allocate word_sz words in dest, either directly into the regions or by
 172   // allocating a new PLAB. Returns the address of the allocated memory, NULL if
 173   // not successful. Plab_refill_failed indicates whether an attempt to refill the
 174   // PLAB failed or not.
 175   HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,
 176                                         size_t word_sz,
 177                                         bool* plab_refill_failed);
 178 
 179   // Allocate word_sz words in the PLAB of dest.  Returns the address of the
 180   // allocated memory, NULL if not successful.
 181   inline HeapWord* plab_allocate(G1HeapRegionAttr dest,
 182                                  size_t word_sz);
 183 
 184   inline HeapWord* allocate(G1HeapRegionAttr dest,
 185                             size_t word_sz,
 186                             bool* refill_failed);
 187 
 188   void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz);
 189 };
 190 
 191 // G1ArchiveRegionMap is a boolean array used to mark G1 regions as
 192 // archive regions.  This allows a quick check for whether an object
 193 // should not be marked because it is in an archive region.
 194 class G1ArchiveRegionMap : public G1BiasedMappedArray<bool> {
 195 protected:
 196   bool default_value() const { return false; }
 197 };
 198 
 199 // G1ArchiveAllocator is used to allocate memory in archive
 200 // regions. Such regions are not scavenged nor compacted by GC.
 201 // There are two types of archive regions, which are
 202 // differ in the kind of references allowed for the contained objects:
 203 //
 204 // - 'Closed' archive region contain no references outside of other
 205 //   closed archive regions. The region is immutable by GC. GC does
 206 //   not mark object header in 'closed' archive region.
 207 // - An 'open' archive region allow references to any other regions,
 208 //   including closed archive, open archive and other java heap regions.
 209 //   GC can adjust pointers and mark object header in 'open' archive region.
 210 class G1ArchiveAllocator : public CHeapObj<mtGC> {
 211 protected:
 212   bool _open; // Indicate if the region is 'open' archive.
 213   G1CollectedHeap* _g1h;
 214 
 215   // The current allocation region
 216   HeapRegion* _allocation_region;
 217 
 218   // Regions allocated for the current archive range.
 219   GrowableArray<HeapRegion*> _allocated_regions;
 220 
 221   // The number of bytes used in the current range.
 222   size_t _summary_bytes_used;
 223 
 224   // Current allocation window within the current region.
 225   HeapWord* _bottom;
 226   HeapWord* _top;
 227   HeapWord* _max;
 228 
 229   // Allocate a new region for this archive allocator.
 230   // Allocation is from the top of the reserved heap downward.
 231   bool alloc_new_region();
 232 
 233 public:
 234   G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) :
 235     _open(open),
 236     _g1h(g1h),
 237     _allocation_region(NULL),
 238     _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,
 239                                                          ResourceObj::C_HEAP),
 240                         2), true /* C_Heap */),
 241     _summary_bytes_used(0),
 242     _bottom(NULL),
 243     _top(NULL),
 244     _max(NULL) { }
 245 
 246   virtual ~G1ArchiveAllocator() {
 247     assert(_allocation_region == NULL, "_allocation_region not NULL");
 248   }
 249 
 250   static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open);
 251 
 252   // Allocate memory for an individual object.
 253   HeapWord* archive_mem_allocate(size_t word_size);
 254 
 255   // Return the memory ranges used in the current archive, after
 256   // aligning to the requested alignment.
 257   void complete_archive(GrowableArray<MemRegion>* ranges,
 258                         size_t end_alignment_in_bytes);
 259 
 260   // The number of bytes allocated by this allocator.
 261   size_t used() {
 262     return _summary_bytes_used;
 263   }
 264 
 265   // Clear the count of bytes allocated in prior G1 regions. This
 266   // must be done when recalculate_use is used to reset the counter
 267   // for the generic allocator, since it counts bytes in all G1
 268   // regions, including those still associated with this allocator.
 269   void clear_used() {
 270     _summary_bytes_used = 0;
 271   }
 272 
 273   // Create the _archive_region_map which is used to identify archive objects.
 274   static inline void enable_archive_object_check();
 275 
 276   // Mark regions containing the specified address range as archive/non-archive.
 277   static inline void set_range_archive(MemRegion range, bool open);
 278   static inline void clear_range_archive(MemRegion range, bool open);
 279 
 280   // Check if the object is in closed archive
 281   static inline bool is_closed_archive_object(oop object);
 282   // Check if the object is in open archive
 283   static inline bool is_open_archive_object(oop object);
 284   // Check if the object is either in closed archive or open archive
 285   static inline bool is_archived_object(oop object);
 286 
 287 private:
 288   static bool _archive_check_enabled;
 289   static G1ArchiveRegionMap  _closed_archive_region_map;
 290   static G1ArchiveRegionMap  _open_archive_region_map;
 291 
 292   // Check if an object is in a closed archive region using the _closed_archive_region_map.
 293   static inline bool in_closed_archive_range(oop object);
 294   // Check if an object is in open archive region using the _open_archive_region_map.
 295   static inline bool in_open_archive_range(oop object);
 296 
 297   // Check if archive object checking is enabled, to avoid calling in_open/closed_archive_range
 298   // unnecessarily.
 299   static inline bool archive_check_enabled();
 300 };
 301 
 302 #endif // SHARE_GC_G1_G1ALLOCATOR_HPP