1 /*
   2  * Copyright (c) 2001, 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_PARNEW_PARGCALLOCBUFFER_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP
  27 
  28 #include "gc_implementation/shared/gcUtil.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 // Forward declarations.
  34 class PLABStats;
  35 
  36 // A per-thread allocation buffer used during GC.
  37 class ParGCAllocBuffer: public CHeapObj<mtGC> {
  38 protected:
  39   char head[32];
  40   size_t _word_sz;          // In HeapWord units
  41   HeapWord* _bottom;
  42   HeapWord* _top;
  43   HeapWord* _end;           // Last allocatable address + 1
  44   HeapWord* _hard_end;      // _end + AlignmentReserve
  45   // In support of ergonomic sizing of PLAB's
  46   size_t    _allocated;     // in HeapWord units
  47   size_t    _wasted;        // in HeapWord units
  48   char tail[32];
  49   static size_t AlignmentReserve;
  50 
  51 public:
  52   // Initializes the buffer to be empty, but with the given "word_sz".
  53   // Must get initialized with "set_buf" for an allocation to succeed.
  54   ParGCAllocBuffer(size_t word_sz);
  55   virtual ~ParGCAllocBuffer() {}
  56 
  57   // Minimum PLAB size.
  58   static size_t min_size();
  59   // Maximum PLAB size.
  60   static size_t max_size();
  61 
  62   // If an allocation of the given "word_sz" can be satisfied within the
  63   // buffer, do the allocation, returning a pointer to the start of the
  64   // allocated block.  If the allocation request cannot be satisfied,
  65   // return NULL.
  66   HeapWord* allocate(size_t word_sz) {
  67     HeapWord* res = _top;
  68     if (pointer_delta(_end, _top) >= word_sz) {
  69       _top = _top + word_sz;
  70       return res;
  71     } else {
  72       return NULL;
  73     }
  74   }
  75 
  76   // Allocate the object aligned to "alignment_in_bytes".
  77   HeapWord* allocate_aligned(size_t word_sz, unsigned short alignment_in_bytes);
  78 
  79   // Undo the last allocation in the buffer, which is required to be of the
  80   // "obj" of the given "word_sz".
  81   void undo_allocation(HeapWord* obj, size_t word_sz) {
  82     assert(pointer_delta(_top, _bottom) >= word_sz, "Bad undo");
  83     assert(pointer_delta(_top, obj)     == word_sz, "Bad undo");
  84     _top = obj;
  85   }
  86 
  87   // The total (word) size of the buffer, including both allocated and
  88   // unallocated space.
  89   size_t word_sz() { return _word_sz; }
  90 
  91   // Should only be done if we are about to reset with a new buffer of the
  92   // given size.
  93   void set_word_size(size_t new_word_sz) {
  94     assert(new_word_sz > AlignmentReserve, "Too small");
  95     _word_sz = new_word_sz;
  96   }
  97 
  98   // The number of words of unallocated space remaining in the buffer.
  99   size_t words_remaining() {
 100     assert(_end >= _top, "Negative buffer");
 101     return pointer_delta(_end, _top, HeapWordSize);
 102   }
 103 
 104   bool contains(void* addr) {
 105     return (void*)_bottom <= addr && addr < (void*)_hard_end;
 106   }
 107 
 108   // Sets the space of the buffer to be [buf, space+word_sz()).
 109   virtual void set_buf(HeapWord* buf) {
 110     _bottom   = buf;
 111     _top      = _bottom;
 112     _hard_end = _bottom + word_sz();
 113     _end      = _hard_end - AlignmentReserve;
 114     assert(_end >= _top, "Negative buffer");
 115     // In support of ergonomic sizing
 116     _allocated += word_sz();
 117   }
 118 
 119   // Flush allocation statistics into the given PLABStats supporting ergonomic
 120   // sizing of PLAB's and retire the current buffer.
 121   void flush_and_retire_stats(PLABStats* stats);
 122 
 123   // Force future allocations to fail and queries for contains()
 124   // to return false
 125   void invalidate() {
 126     _end    = _hard_end;
 127     _wasted += pointer_delta(_end, _top);  // unused  space
 128     _top    = _end;      // force future allocations to fail
 129     _bottom = _end;      // force future contains() queries to return false
 130   }
 131 
 132   // Fills in the unallocated portion of the buffer with a garbage object and update
 133   // statistics.
 134   virtual void retire();
 135 
 136   void print() PRODUCT_RETURN;
 137 };
 138 
 139 // PLAB book-keeping.
 140 class PLABStats VALUE_OBJ_CLASS_SPEC {
 141   size_t _allocated;      // Total allocated
 142   size_t _wasted;         // of which wasted (internal fragmentation)
 143   size_t _unused;         // Unused in last buffer
 144   size_t _desired_plab_sz;// Output of filter (below), suitably trimmed and quantized
 145   AdaptiveWeightedAverage
 146          _filter;         // Integrator with decay
 147 
 148   void reset() {
 149     _allocated = 0;
 150     _wasted    = 0;
 151     _unused    = 0;
 152   }
 153  public:
 154   PLABStats(size_t desired_plab_sz_, unsigned wt) :
 155     _allocated(0),
 156     _wasted(0),
 157     _unused(0),
 158     _desired_plab_sz(desired_plab_sz_),
 159     _filter(wt)
 160   { }
 161 
 162   static const size_t min_size() {
 163     return ParGCAllocBuffer::min_size();
 164   }
 165 
 166   static const size_t max_size() {
 167     return ParGCAllocBuffer::max_size();
 168   }
 169 
 170   size_t desired_plab_sz() {
 171     return _desired_plab_sz;
 172   }
 173 
 174   // Updates the current desired PLAB size. Computes the new desired PLAB size,
 175   // updates _desired_plab_sz and clears sensor accumulators.
 176   void adjust_desired_plab_sz(uint no_of_gc_workers);
 177 
 178   void add_allocated(size_t v) {
 179     Atomic::add_ptr(v, &_allocated);
 180   }
 181 
 182   void add_unused(size_t v) {
 183     Atomic::add_ptr(v, &_unused);
 184   }
 185 
 186   void add_wasted(size_t v) {
 187     Atomic::add_ptr(v, &_wasted);
 188   }
 189 };
 190 
 191 #endif // SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP