1 /*
   2  * Copyright (c) 2011, 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_G1ALLOCREGION_HPP
  26 #define SHARE_VM_GC_G1_G1ALLOCREGION_HPP
  27 
  28 #include "gc/g1/heapRegion.hpp"
  29 #include "gc/g1/g1EvacStats.hpp"
  30 #include "gc/g1/g1InCSetState.hpp"
  31 
  32 class G1CollectedHeap;
  33 
  34 // 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing
  35 #define G1_ALLOC_REGION_TRACING 0
  36 
  37 // A class that holds a region that is active in satisfying allocation
  38 // requests, potentially issued in parallel. When the active region is
  39 // full it will be retired and replaced with a new one. The
  40 // implementation assumes that fast-path allocations will be lock-free
  41 // and a lock will need to be taken when the active region needs to be
  42 // replaced.
  43 
  44 class G1AllocRegion VALUE_OBJ_CLASS_SPEC {
  45 
  46 private:
  47   // The active allocating region we are currently allocating out
  48   // of. The invariant is that if this object is initialized (i.e.,
  49   // init() has been called and release() has not) then _alloc_region
  50   // is either an active allocating region or the dummy region (i.e.,
  51   // it can never be NULL) and this object can be used to satisfy
  52   // allocation requests. If this object is not initialized
  53   // (i.e. init() has not been called or release() has been called)
  54   // then _alloc_region is NULL and this object should not be used to
  55   // satisfy allocation requests (it was done this way to force the
  56   // correct use of init() and release()).
  57   HeapRegion* volatile _alloc_region;
  58 
  59   // Allocation context associated with this alloc region.
  60   AllocationContext_t _allocation_context;
  61 
  62   // It keeps track of the distinct number of regions that are used
  63   // for allocation in the active interval of this object, i.e.,
  64   // between a call to init() and a call to release(). The count
  65   // mostly includes regions that are freshly allocated, as well as
  66   // the region that is re-used using the set() method. This count can
  67   // be used in any heuristics that might want to bound how many
  68   // distinct regions this object can used during an active interval.
  69   uint _count;
  70 
  71   // When we set up a new active region we save its used bytes in this
  72   // field so that, when we retire it, we can calculate how much space
  73   // we allocated in it.
  74   size_t _used_bytes_before;
  75 
  76   // When true, indicates that allocate calls should do BOT updates.
  77   const bool _bot_updates;
  78 
  79   // Useful for debugging and tracing.
  80   const char* _name;
  81 
  82   // A dummy region (i.e., it's been allocated specially for this
  83   // purpose and it is not part of the heap) that is full (i.e., top()
  84   // == end()). When we don't have a valid active region we make
  85   // _alloc_region point to this. This allows us to skip checking
  86   // whether the _alloc_region is NULL or not.
  87   static HeapRegion* _dummy_region;
  88 
  89   // Some of the methods below take a bot_updates parameter. Its value
  90   // should be the same as the _bot_updates field. The idea is that
  91   // the parameter will be a constant for a particular alloc region
  92   // and, given that these methods will be hopefully inlined, the
  93   // compiler should compile out the test.
  94 
  95   // Perform a non-MT-safe allocation out of the given region.
  96   static inline HeapWord* allocate(HeapRegion* alloc_region,
  97                                    size_t word_size,
  98                                    bool bot_updates);
  99 
 100   // Perform a MT-safe allocation out of the given region.
 101   static inline HeapWord* par_allocate(HeapRegion* alloc_region,
 102                                        size_t word_size,
 103                                        bool bot_updates);
 104   // Perform a MT-safe allocation out of the given region, with the given
 105   // minimum and desired size. Returns the actual size allocated (between
 106   // minimum and desired size) in actual_word_size if the allocation has been
 107   // successful.
 108   static inline HeapWord* par_allocate(HeapRegion* alloc_region,
 109                                        size_t min_word_size,
 110                                        size_t desired_word_size,
 111                                        size_t* actual_word_size,
 112                                        bool bot_updates);
 113 
 114   // Ensure that the region passed as a parameter has been filled up
 115   // so that noone else can allocate out of it any more.
 116   // Returns the number of bytes that have been wasted by filled up
 117   // the space.
 118   static size_t fill_up_remaining_space(HeapRegion* alloc_region,
 119                                         bool bot_updates);
 120 
 121   // After a region is allocated by alloc_new_region, this
 122   // method is used to set it as the active alloc_region
 123   void update_alloc_region(HeapRegion* alloc_region);
 124 
 125   // Allocate a new active region and use it to perform a word_size
 126   // allocation. The force parameter will be passed on to
 127   // G1CollectedHeap::allocate_new_alloc_region() and tells it to try
 128   // to allocate a new region even if the max has been reached.
 129   HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);
 130 
 131 protected:
 132   // Retire the active allocating region. If fill_up is true then make
 133   // sure that the region is full before we retire it so that no one
 134   // else can allocate out of it.
 135   // Returns the number of bytes that have been filled up during retire.
 136   virtual size_t retire(bool fill_up);
 137 
 138   // For convenience as subclasses use it.
 139   static G1CollectedHeap* _g1h;
 140 
 141   virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;
 142   virtual void retire_region(HeapRegion* alloc_region,
 143                              size_t allocated_bytes) = 0;
 144 
 145   G1AllocRegion(const char* name, bool bot_updates);
 146 
 147 public:
 148   static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);
 149 
 150   HeapRegion* get() const {
 151     HeapRegion * hr = _alloc_region;
 152     // Make sure that the dummy region does not escape this class.
 153     return (hr == _dummy_region) ? NULL : hr;
 154   }
 155 
 156   void set_allocation_context(AllocationContext_t context) { _allocation_context = context; }
 157   AllocationContext_t  allocation_context() { return _allocation_context; }
 158 
 159   uint count() { return _count; }
 160 
 161   // The following two are the building blocks for the allocation method.
 162 
 163   // First-level allocation: Should be called without holding a
 164   // lock. It will try to allocate lock-free out of the active region,
 165   // or return NULL if it was unable to.
 166   inline HeapWord* attempt_allocation(size_t word_size,
 167                                       bool bot_updates);
 168   // Perform an allocation out of the current allocation region, with the given
 169   // minimum and desired size. Returns the actual size allocated (between
 170   // minimum and desired size) in actual_word_size if the allocation has been
 171   // successful.
 172   // Should be called without holding a lock. It will try to allocate lock-free
 173   // out of the active region, or return NULL if it was unable to.
 174   inline HeapWord* attempt_allocation(size_t min_word_size,
 175                                       size_t desired_word_size,
 176                                       size_t* actual_word_size,
 177                                       bool bot_updates);
 178 
 179   // Second-level allocation: Should be called while holding a
 180   // lock. It will try to first allocate lock-free out of the active
 181   // region or, if it's unable to, it will try to replace the active
 182   // alloc region with a new one. We require that the caller takes the
 183   // appropriate lock before calling this so that it is easier to make
 184   // it conform to its locking protocol.
 185   inline HeapWord* attempt_allocation_locked(size_t word_size,
 186                                              bool bot_updates);
 187   // Same as attempt_allocation_locked(size_t, bool), but allowing specification
 188   // of minimum word size of the block in min_word_size, and the maximum word
 189   // size of the allocation in desired_word_size. The actual size of the block is
 190   // returned in actual_word_size.
 191   inline HeapWord* attempt_allocation_locked(size_t min_word_size,
 192                                              size_t desired_word_size,
 193                                              size_t* actual_word_size,
 194                                              bool bot_updates);
 195 
 196   // Should be called to allocate a new region even if the max of this
 197   // type of regions has been reached. Should only be called if other
 198   // allocation attempts have failed and we are not holding a valid
 199   // active region.
 200   inline HeapWord* attempt_allocation_force(size_t word_size,
 201                                             bool bot_updates);
 202 
 203   // Should be called before we start using this object.
 204   void init();
 205 
 206   // This can be used to set the active region to a specific
 207   // region. (Use Example: we try to retain the last old GC alloc
 208   // region that we've used during a GC and we can use set() to
 209   // re-instate it at the beginning of the next GC.)
 210   void set(HeapRegion* alloc_region);
 211 
 212   // Should be called when we want to release the active region which
 213   // is returned after it's been retired.
 214   virtual HeapRegion* release();
 215 
 216 #if G1_ALLOC_REGION_TRACING
 217   void trace(const char* str,
 218              size_t min_word_size = 0,
 219              size_t desired_word_size = 0,
 220              size_t actual_word_size = 0,
 221              HeapWord* result = NULL);
 222 #else // G1_ALLOC_REGION_TRACING
 223   void trace(const char* str,
 224              size_t min_word_size = 0,
 225              size_t desired_word_size = 0,
 226              size_t actual_word_size = 0,
 227              HeapWord* result = NULL) { }
 228 #endif // G1_ALLOC_REGION_TRACING
 229 };
 230 
 231 class MutatorAllocRegion : public G1AllocRegion {
 232 protected:
 233   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 234   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 235 public:
 236   MutatorAllocRegion()
 237     : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }
 238 };
 239 
 240 // Common base class for allocation regions used during GC.
 241 class G1GCAllocRegion : public G1AllocRegion {
 242 protected:
 243   G1EvacStats* _stats;
 244   InCSetState::in_cset_state_t _purpose;
 245 
 246   virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
 247   virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
 248 
 249   virtual size_t retire(bool fill_up);
 250 public:
 251   G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats, InCSetState::in_cset_state_t purpose)
 252   : G1AllocRegion(name, bot_updates), _stats(stats), _purpose(purpose) {
 253     assert(stats != NULL, "Must pass non-NULL PLAB statistics");
 254   }
 255 };
 256 
 257 class SurvivorGCAllocRegion : public G1GCAllocRegion {
 258 public:
 259   SurvivorGCAllocRegion(G1EvacStats* stats)
 260   : G1GCAllocRegion("Survivor GC Alloc Region", false /* bot_updates */, stats, InCSetState::Young) { }
 261 };
 262 
 263 class OldGCAllocRegion : public G1GCAllocRegion {
 264 public:
 265   OldGCAllocRegion(G1EvacStats* stats)
 266   : G1GCAllocRegion("Old GC Alloc Region", true /* bot_updates */, stats, InCSetState::Old) { }
 267 
 268   // This specialization of release() makes sure that the last card that has
 269   // been allocated into has been completely filled by a dummy object.  This
 270   // avoids races when remembered set scanning wants to update the BOT of the
 271   // last card in the retained old gc alloc region, and allocation threads
 272   // allocating into that card at the same time.
 273   virtual HeapRegion* release();
 274 };
 275 
 276 #endif // SHARE_VM_GC_G1_G1ALLOCREGION_HPP