1 /*
   2  * Copyright (c) 2010, 2016, 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_CMS_PROMOTIONINFO_HPP
  26 #define SHARE_VM_GC_CMS_PROMOTIONINFO_HPP
  27 
  28 #include "gc/cms/freeChunk.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 // Forward declarations
  32 class CompactibleFreeListSpace;
  33 
  34 class PromotedObject VALUE_OBJ_CLASS_SPEC {
  35  private:
  36   enum {
  37     promoted_mask  = right_n_bits(2),   // i.e. 0x3
  38     displaced_mark = nth_bit(2),        // i.e. 0x4
  39     next_mask      = ~(right_n_bits(3)) // i.e. ~(0x7)
  40   };
  41 
  42   // Below, we want _narrow_next in the "higher" 32 bit slot,
  43   // whose position will depend on endian-ness of the platform.
  44   // This is so that there is no interference with the
  45   // cms_free_bit occupying bit position 7 (lsb == 0)
  46   // when we are using compressed oops; see FreeChunk::is_free().
  47   // We cannot move the cms_free_bit down because currently
  48   // biased locking code assumes that age bits are contiguous
  49   // with the lock bits. Even if that assumption were relaxed,
  50   // the least position we could move this bit to would be
  51   // to bit position 3, which would require 16 byte alignment.
  52   typedef struct {
  53 #ifdef VM_LITTLE_ENDIAN
  54     LP64_ONLY(narrowOop _pad;)
  55               narrowOop _narrow_next;
  56 #else
  57               narrowOop _narrow_next;
  58     LP64_ONLY(narrowOop _pad;)
  59 #endif
  60   } Data;
  61 
  62   union {
  63     intptr_t _next;
  64     Data     _data;
  65   };
  66  public:
  67   PromotedObject* next() const;
  68   void setNext(PromotedObject* x);
  69   inline void setPromotedMark() {
  70     _next |= promoted_mask;
  71     assert(!((FreeChunk*)this)->is_free(), "Error");
  72   }
  73   inline bool hasPromotedMark() const {
  74     assert(!((FreeChunk*)this)->is_free(), "Error");
  75     return (_next & promoted_mask) == promoted_mask;
  76   }
  77   inline void setDisplacedMark() {
  78     _next |= displaced_mark;
  79     assert(!((FreeChunk*)this)->is_free(), "Error");
  80   }
  81   inline bool hasDisplacedMark() const {
  82     assert(!((FreeChunk*)this)->is_free(), "Error");
  83     return (_next & displaced_mark) != 0;
  84   }
  85   inline void clear_next()        {
  86     _next = 0;
  87     assert(!((FreeChunk*)this)->is_free(), "Error");
  88   }
  89   debug_only(void *next_addr() { return (void *) &_next; })
  90 };
  91 
  92 class SpoolBlock: public FreeChunk {
  93   friend class PromotionInfo;
  94  protected:
  95   SpoolBlock*  nextSpoolBlock;
  96   size_t       bufferSize;        // number of usable words in this block
  97   markOop*     displacedHdr;      // the displaced headers start here
  98 
  99   // Note about bufferSize: it denotes the number of entries available plus 1;
 100   // legal indices range from 1 through BufferSize - 1.  See the verification
 101   // code verify() that counts the number of displaced headers spooled.
 102   size_t computeBufferSize() {
 103     return (size() * sizeof(HeapWord) - sizeof(*this)) / sizeof(markOop);
 104   }
 105 
 106  public:
 107   void init() {
 108     bufferSize = computeBufferSize();
 109     displacedHdr = (markOop*)&displacedHdr;
 110     nextSpoolBlock = NULL;
 111   }
 112 
 113   void print_on(outputStream* st) const;
 114   void print() const { print_on(tty); }
 115 };
 116 
 117 class PromotionInfo VALUE_OBJ_CLASS_SPEC {
 118   bool            _tracking;      // set if tracking
 119   CompactibleFreeListSpace* _space; // the space to which this belongs
 120   PromotedObject* _promoHead;     // head of list of promoted objects
 121   PromotedObject* _promoTail;     // tail of list of promoted objects
 122   SpoolBlock*     _spoolHead;     // first spooling block
 123   SpoolBlock*     _spoolTail;     // last  non-full spooling block or null
 124   SpoolBlock*     _splice_point;  // when _spoolTail is null, holds list tail
 125   SpoolBlock*     _spareSpool;    // free spool buffer
 126   size_t          _firstIndex;    // first active index in
 127                                   // first spooling block (_spoolHead)
 128   size_t          _nextIndex;     // last active index + 1 in last
 129                                   // spooling block (_spoolTail)
 130  private:
 131   // ensure that spooling space exists; return true if there is spooling space
 132   bool ensure_spooling_space_work();
 133 
 134  public:
 135   PromotionInfo() :
 136     _tracking(0), _space(NULL),
 137     _promoHead(NULL), _promoTail(NULL),
 138     _spoolHead(NULL), _spoolTail(NULL),
 139     _spareSpool(NULL), _firstIndex(1),
 140     _nextIndex(1) {}
 141 
 142   bool noPromotions() const {
 143     assert(_promoHead != NULL || _promoTail == NULL, "list inconsistency");
 144     return _promoHead == NULL;
 145   }
 146   void startTrackingPromotions();
 147   void stopTrackingPromotions();
 148   bool tracking() const          { return _tracking;  }
 149   void track(PromotedObject* trackOop);      // keep track of a promoted oop
 150   // The following variant must be used when trackOop is not fully
 151   // initialized and has a NULL klass:
 152   void track(PromotedObject* trackOop, Klass* klassOfOop); // keep track of a promoted oop
 153   void setSpace(CompactibleFreeListSpace* sp) { _space = sp; }
 154   CompactibleFreeListSpace* space() const     { return _space; }
 155   markOop nextDisplacedHeader(); // get next header & forward spool pointer
 156   void    saveDisplacedHeader(markOop hdr);
 157                                  // save header and forward spool
 158 
 159   inline size_t refillSize() const;
 160 
 161   SpoolBlock* getSpoolBlock();   // return a free spooling block
 162   inline bool has_spooling_space() {
 163     return _spoolTail != NULL && _spoolTail->bufferSize > _nextIndex;
 164   }
 165   // ensure that spooling space exists
 166   bool ensure_spooling_space() {
 167     return has_spooling_space() || ensure_spooling_space_work();
 168   }
 169   #define PROMOTED_OOPS_ITERATE_DECL(OopClosureType, nv_suffix)  \
 170     void promoted_oops_iterate##nv_suffix(OopClosureType* cl);
 171   ALL_SINCE_SAVE_MARKS_CLOSURES(PROMOTED_OOPS_ITERATE_DECL)
 172   #undef PROMOTED_OOPS_ITERATE_DECL
 173   void promoted_oops_iterate(OopsInGenClosure* cl) {
 174     promoted_oops_iterate_v(cl);
 175   }
 176   void verify()  const;
 177   void reset() {
 178     _promoHead = NULL;
 179     _promoTail = NULL;
 180     _spoolHead = NULL;
 181     _spoolTail = NULL;
 182     _spareSpool = NULL;
 183     _firstIndex = 0;
 184     _nextIndex = 0;
 185 
 186   }
 187 
 188   void print_on(outputStream* st) const;
 189 };
 190 
 191 
 192 #endif // SHARE_VM_GC_CMS_PROMOTIONINFO_HPP