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