1 /* 2 * Copyright (c) 2011, 2012, 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_G1_HEAPREGIONSET_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP 27 28 #include "gc_implementation/g1/heapRegion.hpp" 29 30 // Large buffer for some cases where the output might be larger than normal. 31 #define HRS_ERR_MSG_BUFSZ 512 32 typedef FormatBuffer<HRS_ERR_MSG_BUFSZ> hrs_err_msg; 33 34 // Set verification will be forced either if someone defines 35 // HEAP_REGION_SET_FORCE_VERIFY to be 1, or in builds in which 36 // asserts are compiled in. 37 #ifndef HEAP_REGION_SET_FORCE_VERIFY 38 #define HEAP_REGION_SET_FORCE_VERIFY defined(ASSERT) 39 #endif // HEAP_REGION_SET_FORCE_VERIFY 40 41 class hrs_ext_msg; 42 43 class HRSMtSafeChecker : public CHeapObj<mtGC> { 44 public: 45 virtual bool check() = 0; 46 }; 47 48 class MasterFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: bool check(); }; 49 class SecondaryFreeRegionListMtSafeChecker : public HRSMtSafeChecker { public: bool check(); }; 50 class HumongousRegionSetMtSafeChecker : public HRSMtSafeChecker { public: bool check(); }; 51 class OldRegionSetMtSafeChecker : public HRSMtSafeChecker { public: bool check(); }; 52 53 class HeapRegionSetCount VALUE_OBJ_CLASS_SPEC { 54 friend class VMStructs; 55 uint _length; 56 size_t _capacity; 57 58 public: 59 HeapRegionSetCount() : _length(0), _capacity(0) { } 60 61 const uint length() const { return _length; } 62 const size_t capacity() const { return _capacity; } 63 64 void increment(uint length_to_add, size_t capacity_to_add) { 65 _length += length_to_add; 66 _capacity += capacity_to_add; 67 } 68 69 void decrement(const uint length_to_remove, const size_t capacity_to_remove) { 70 _length -= length_to_remove; 71 _capacity -= capacity_to_remove; 72 } 73 }; 74 75 //////////////////// HeapRegionSetBase //////////////////// 76 77 // Base class for all the classes that represent heap region sets. It 78 // contains the basic attributes that each set needs to maintain 79 // (e.g., length, region num, used bytes sum) plus any shared 80 // functionality (e.g., verification). 81 82 class HeapRegionSetBase VALUE_OBJ_CLASS_SPEC { 83 friend class VMStructs; 84 private: 85 bool _is_humongous; 86 bool _is_empty; 87 HRSMtSafeChecker* _mt_safety_checker; 88 89 protected: 90 // The number of regions added to the set. If the set contains 91 // only humongous regions, this reflects only 'starts humongous' 92 // regions and does not include 'continues humongous' ones. 93 HeapRegionSetCount _count; 94 95 const char* _name; 96 97 bool _verify_in_progress; 98 99 // verify_region() is used to ensure that the contents of a region 100 // added to / removed from a set are consistent. 101 void verify_region(HeapRegion* hr) PRODUCT_RETURN; 102 103 // Indicates whether all regions in the set should be humongous or 104 // not. Only used during verification. 105 bool regions_humongous() { return _is_humongous; } 106 107 // Indicates whether all regions in the set should be empty or 108 // not. Only used during verification. 109 bool regions_empty() { return _is_empty; } 110 111 bool check_mt_safety() { 112 if (_mt_safety_checker != NULL) { 113 _mt_safety_checker->check(); 114 } 115 return true; 116 } 117 118 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg) { } 119 120 HeapRegionSetBase(const char* name, bool humongous, bool empty, HRSMtSafeChecker* mt_safety_checker); 121 122 public: 123 const char* name() { return _name; } 124 125 uint length() { return _count.length(); } 126 127 bool is_empty() { return _count.length() == 0; } 128 129 size_t total_capacity_bytes() { 130 return _count.capacity(); 131 } 132 133 // It updates the fields of the set to reflect hr being added to 134 // the set and tags the region appropriately. 135 inline void add(HeapRegion* hr); 136 137 // It updates the fields of the set to reflect hr being removed 138 // from the set and tags the region appropriately. 139 inline void remove(HeapRegion* hr); 140 141 // fill_in_ext_msg() writes the the values of the set's attributes 142 // in the custom err_msg (hrs_ext_msg). fill_in_ext_msg_extra() 143 // allows subclasses to append further information. 144 void fill_in_ext_msg(hrs_ext_msg* msg, const char* message); 145 146 virtual void verify(); 147 void verify_start(); 148 void verify_next_region(HeapRegion* hr); 149 void verify_end(); 150 151 #if HEAP_REGION_SET_FORCE_VERIFY 152 void verify_optional() { 153 verify(); 154 } 155 #else // HEAP_REGION_SET_FORCE_VERIFY 156 void verify_optional() { } 157 #endif // HEAP_REGION_SET_FORCE_VERIFY 158 159 virtual void print_on(outputStream* out, bool print_contents = false); 160 }; 161 162 // Customized err_msg for heap region sets. Apart from a 163 // assert/guarantee-specific message it also prints out the values of 164 // the fields of the associated set. This can be very helpful in 165 // diagnosing failures. 166 class hrs_ext_msg : public hrs_err_msg { 167 public: 168 hrs_ext_msg(HeapRegionSetBase* set, const char* message) : hrs_err_msg("") { 169 set->fill_in_ext_msg(this, message); 170 } 171 }; 172 173 //////////////////// HeapRegionSet //////////////////// 174 175 #define hrs_assert_sets_match(_set1_, _set2_) \ 176 do { \ 177 assert(((_set1_)->regions_humongous() == \ 178 (_set2_)->regions_humongous()) && \ 179 ((_set1_)->regions_empty() == (_set2_)->regions_empty()), \ 180 hrs_err_msg("the contents of set %s and set %s should match", \ 181 (_set1_)->name(), (_set2_)->name())); \ 182 } while (0) 183 184 // This class represents heap region sets whose members are not 185 // explicitly tracked. It's helpful to group regions using such sets 186 // so that we can reason about all the region groups in the heap using 187 // the same interface (namely, the HeapRegionSetBase API). 188 189 class HeapRegionSet : public HeapRegionSetBase { 190 public: 191 HeapRegionSet(const char* name, bool humongous, HRSMtSafeChecker* mt_safety_checker): 192 HeapRegionSetBase(name, humongous, false /* empty */, mt_safety_checker) { } 193 194 void bulk_remove(const HeapRegionSetCount& removed) { 195 _count.decrement(removed.length(), removed.capacity()); 196 } 197 }; 198 199 //////////////////// HeapRegionLinkedList //////////////////// 200 201 // A set that links all the regions added to it in a singly-linked 202 // list. We should try to avoid doing operations that iterate over 203 // such lists in performance critical paths. Typically we should 204 // add / remove one region at a time or concatenate two lists. All 205 // those operations are done in constant time. 206 207 class FreeRegionListIterator; 208 209 class FreeRegionList : public HeapRegionSetBase { 210 friend class FreeRegionListIterator; 211 212 private: 213 HeapRegion* _head; 214 HeapRegion* _tail; 215 216 protected: 217 virtual void fill_in_ext_msg_extra(hrs_ext_msg* msg); 218 219 // See the comment for HeapRegionSetBase::clear() 220 virtual void clear(); 221 222 public: 223 FreeRegionList(const char* name, HRSMtSafeChecker* mt_safety_checker): 224 HeapRegionSetBase(name, false /* humongous */, true /* empty */, mt_safety_checker) { 225 clear(); 226 } 227 228 void verify_list(); 229 230 HeapRegion* head() { return _head; } 231 HeapRegion* tail() { return _tail; } 232 233 static uint _unrealistically_long_length; 234 static void set_unrealistically_long_length(uint len); 235 236 // It adds hr to the list as the new head. The region should not be 237 // a member of another set. 238 inline void add_as_head(HeapRegion* hr); 239 240 // It adds hr to the list as the new tail. The region should not be 241 // a member of another set. 242 inline void add_as_tail(HeapRegion* hr); 243 244 // It removes and returns the head of the list. It assumes that the 245 // list is not empty so it will return a non-NULL value. 246 inline HeapRegion* remove_head(); 247 248 // Convenience method. 249 inline HeapRegion* remove_head_or_null(); 250 251 // It moves the regions from from_list to this list and empties 252 // from_list. The new regions will appear in the same order as they 253 // were in from_list and be linked in the beginning of this list. 254 void add_as_head(FreeRegionList* from_list); 255 256 // It moves the regions from from_list to this list and empties 257 // from_list. The new regions will appear in the same order as they 258 // were in from_list and be linked in the end of this list. 259 void add_as_tail(FreeRegionList* from_list); 260 261 // It empties the list by removing all regions from it. 262 void remove_all(); 263 264 // It removes all regions in the list that are pending for removal 265 // (i.e., they have been tagged with "pending_removal"). The list 266 // must not be empty, target_count should reflect the exact number 267 // of regions that are pending for removal in the list, and 268 // target_count should be > 1 (currently, we never need to remove a 269 // single region using this). 270 void remove_all_pending(uint target_count); 271 272 virtual void verify(); 273 274 virtual void print_on(outputStream* out, bool print_contents = false); 275 }; 276 277 //////////////////// FreeRegionListIterator //////////////////// 278 279 // Iterator class that provides a convenient way to iterate over the 280 // regions of a HeapRegionLinkedList instance. 281 282 class FreeRegionListIterator : public StackObj { 283 private: 284 FreeRegionList* _list; 285 HeapRegion* _curr; 286 287 public: 288 bool more_available() { 289 return _curr != NULL; 290 } 291 292 HeapRegion* get_next() { 293 assert(more_available(), 294 "get_next() should be called when more regions are available"); 295 296 // If we are going to introduce a count in the iterator we should 297 // do the "cycle" check. 298 299 HeapRegion* hr = _curr; 300 _list->verify_region(hr); 301 _curr = hr->next(); 302 return hr; 303 } 304 305 FreeRegionListIterator(FreeRegionList* list) 306 : _curr(NULL), _list(list) { 307 _curr = list->head(); 308 } 309 }; 310 311 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONSET_HPP