1 /*
   2  * Copyright (c) 2014, 2020, 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_G1HEAPREGIONATTR_HPP
  26 #define SHARE_GC_G1_G1HEAPREGIONATTR_HPP
  27 
  28 #include "gc/g1/g1BiasedArray.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 
  31 // Per-region attributes often used during garbage collection to avoid costly
  32 // lookups for that information all over the place.
  33 struct G1HeapRegionAttr {
  34 public:
  35 #if defined(_M_ARM64)&& defined(_MSC_VER) && _MSC_VER <= 1927
  36   // workaround for MSCV ARM64 bug
  37   // https://developercommunity.visualstudio.com/content/problem/1079221/arm64-bad-code-generation-around-signed-char-arith.html
  38   typedef int32_t region_type_t;
  39 #else
  40   typedef int8_t region_type_t;
  41 #endif
  42   typedef uint8_t needs_remset_update_t;
  43 
  44 private:
  45   needs_remset_update_t _needs_remset_update;
  46   region_type_t _type;
  47 
  48 public:
  49   // Selection of the values for the _type field were driven to micro-optimize the
  50   // encoding and frequency of the checks.
  51   // The most common check for a given reference is whether the region is in the
  52   // collection set or not, and which generation this region is in.
  53   // The selected encoding allows us to use a single check (> NotInCSet) for the
  54   // former.
  55   //
  56   // The other values are used for objects in regions requiring various special handling,
  57   // eager reclamation of humongous objects or optional regions.
  58   static const region_type_t Optional     =  -3;    // The region is optional not in the current collection set.
  59   static const region_type_t Humongous    =  -2;    // The region is a humongous candidate not in the current collection set.
  60   static const region_type_t NotInCSet    =  -1;    // The region is not in the collection set.
  61   static const region_type_t Young        =   0;    // The region is in the collection set and a young region.
  62   static const region_type_t Old          =   1;    // The region is in the collection set and an old region.
  63   static const region_type_t Num          =   2;
  64 
  65   G1HeapRegionAttr(region_type_t type = NotInCSet, bool needs_remset_update = false) :
  66     _needs_remset_update(needs_remset_update), _type(type) {
  67 
  68     assert(is_valid(), "Invalid type %d", _type);
  69   }
  70 
  71   region_type_t type() const           { return _type; }
  72 
  73   const char* get_type_str() const {
  74     switch (type()) {
  75       case Optional: return "Optional";
  76       case Humongous: return "Humongous";
  77       case NotInCSet: return "NotInCSet";
  78       case Young: return "Young";
  79       case Old: return "Old";
  80       default: ShouldNotReachHere(); return "";
  81     }
  82   }
  83 
  84   bool needs_remset_update() const     { return _needs_remset_update != 0; }
  85 
  86   void set_old()                       { _type = Old; }
  87   void clear_humongous()               {
  88     assert(is_humongous() || !is_in_cset(), "must be");
  89     _type = NotInCSet;
  90   }
  91   void set_has_remset(bool value)      { _needs_remset_update = value ? 1 : 0; }
  92 
  93   bool is_in_cset_or_humongous() const { return is_in_cset() || is_humongous(); }
  94   bool is_in_cset() const              { return type() >= Young; }
  95 
  96   bool is_humongous() const            { return type() == Humongous; }
  97   bool is_young() const                { return type() == Young; }
  98   bool is_old() const                  { return type() == Old; }
  99   bool is_optional() const             { return type() == Optional; }
 100 
 101 #ifdef ASSERT
 102   bool is_default() const              { return type() == NotInCSet; }
 103   bool is_valid() const                { return (type() >= Optional && type() < Num); }
 104   bool is_valid_gen() const            { return (type() >= Young && type() <= Old); }
 105 #endif
 106 };
 107 
 108 // Table for all regions in the heap for above.
 109 //
 110 // We use this to speed up reference processing during young collection and
 111 // quickly reclaim humongous objects. For the latter, at the start of GC, by adding
 112 // it as a humongous region we enable special handling for that region. During the
 113 // reference iteration closures, when we see a humongous region, we then simply mark
 114 // it as referenced, i.e. live, and remove it from this table to prevent further
 115 // processing on it.
 116 //
 117 // This means that this does NOT completely correspond to the information stored
 118 // in a HeapRegion, but only to what is interesting for the current young collection.
 119 class G1HeapRegionAttrBiasedMappedArray : public G1BiasedMappedArray<G1HeapRegionAttr> {
 120  protected:
 121   G1HeapRegionAttr default_value() const { return G1HeapRegionAttr(G1HeapRegionAttr::NotInCSet); }
 122  public:
 123   void set_optional(uintptr_t index, bool needs_remset_update) {
 124     assert(get_by_index(index).is_default(),
 125            "Region attributes at index " INTPTR_FORMAT " should be default but is %s", index, get_by_index(index).get_type_str());
 126     set_by_index(index, G1HeapRegionAttr(G1HeapRegionAttr::Optional, needs_remset_update));
 127   }
 128 
 129   void set_humongous(uintptr_t index, bool needs_remset_update) {
 130     assert(get_by_index(index).is_default(),
 131            "Region attributes at index " INTPTR_FORMAT " should be default but is %s", index, get_by_index(index).get_type_str());
 132     set_by_index(index, G1HeapRegionAttr(G1HeapRegionAttr::Humongous, needs_remset_update));
 133   }
 134 
 135   void clear_humongous(uintptr_t index) {
 136     get_ref_by_index(index)->clear_humongous();
 137   }
 138 
 139   void set_has_remset(uintptr_t index, bool needs_remset_update) {
 140     get_ref_by_index(index)->set_has_remset(needs_remset_update);
 141   }
 142 
 143   void set_in_young(uintptr_t index) {
 144     assert(get_by_index(index).is_default(),
 145            "Region attributes at index " INTPTR_FORMAT " should be default but is %s", index, get_by_index(index).get_type_str());
 146     set_by_index(index, G1HeapRegionAttr(G1HeapRegionAttr::Young, true));
 147   }
 148 
 149   void set_in_old(uintptr_t index, bool needs_remset_update) {
 150     assert(get_by_index(index).is_default(),
 151            "Region attributes at index " INTPTR_FORMAT " should be default but is %s", index, get_by_index(index).get_type_str());
 152     set_by_index(index, G1HeapRegionAttr(G1HeapRegionAttr::Old, needs_remset_update));
 153   }
 154 
 155   bool is_in_cset_or_humongous(HeapWord* addr) const { return at(addr).is_in_cset_or_humongous(); }
 156   bool is_in_cset(HeapWord* addr) const { return at(addr).is_in_cset(); }
 157   bool is_in_cset(const HeapRegion* hr) const { return get_by_index(hr->hrm_index()).is_in_cset(); }
 158   G1HeapRegionAttr at(HeapWord* addr) const { return get_by_address(addr); }
 159   void clear() { G1BiasedMappedArray<G1HeapRegionAttr>::clear(); }
 160   void clear(const HeapRegion* hr) { return set_by_index(hr->hrm_index(), G1HeapRegionAttr(G1HeapRegionAttr::NotInCSet)); }
 161 };
 162 
 163 #endif // SHARE_GC_G1_G1HEAPREGIONATTR_HPP