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