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