1 /* 2 * Copyright (c) 2013, 2017, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP 25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP 26 27 #include "memory/space.hpp" 28 29 class VMStructs; 30 31 class ShenandoahHeapRegion : public ContiguousSpace { 32 friend class VMStructs; 33 private: 34 /* 35 Region state is described by a state machine. Transitions are guarded by 36 heap lock, which allows changing the state of several regions atomically. 37 Region states can be logically aggregated in groups. 38 39 "Empty": 40 ................................................................. 41 . . 42 . . 43 . Uncommitted <------- Committed <------------------------\ 44 . | | . | 45 . \---------v-----------/ . | 46 . | . | 47 .........................|....................................... | 48 | | 49 "Active": | | 50 .........................|....................................... | 51 . | . | 52 . /-----------------^-------------------\ . | 53 . | | . | 54 . v v "Humongous": . | 55 . Regular ---\-----\ ..................O................ . | 56 . | ^ | | . | . . | 57 . | | | | . *---------\ . . | 58 . v | | | . v v . . | 59 . Pinned Cset | . HStart <--> H/Start H/Cont . . | 60 . ^ / | | . Pinned v | . . | 61 . | / | | . *<--------/ . . | 62 . | v | | . | . . | 63 . CsetPinned | | ..................O................ . | 64 . | | | . | 65 . \-----\---v-------------------/ . | 66 . | . | 67 .........................|....................................... | 68 | | 69 "Trash": | | 70 .........................|....................................... | 71 . | . | 72 . v . | 73 . Trash ---------------------------------------/ 74 . . 75 . . 76 ................................................................. 77 78 Transition from "Empty" to "Active" is first allocation. It can go from {Uncommitted, Committed} 79 to {Regular, "Humongous"}. The allocation may happen in Regular regions too, but not in Humongous. 80 81 Transition from "Active" to "Trash" is reclamation. It can go from CSet during the normal cycle, 82 and from {Regular, "Humongous"} for immediate reclamation. The existence of Trash state allows 83 quick reclamation without actual cleaning up. 84 85 Transition from "Trash" to "Empty" is recycling. It cleans up the regions and corresponding metadata. 86 Can be done asynchronously and in bulk. 87 88 Note how internal transitions disallow logic bugs: 89 a) No region can go Empty, unless properly reclaimed/recycled; 90 b) No region can go Uncommitted, unless reclaimed/recycled first; 91 c) Only Regular regions can go to CSet; 92 d) Pinned cannot go Trash, thus it could never be reclaimed until unpinned; 93 e) Pinned cannot go CSet, thus it never moves; 94 f) Humongous cannot be used for regular allocations; 95 g) Humongous cannot go CSet, thus it never moves; 96 h) Humongous start can go pinned, and thus can be protected from moves (humongous continuations should 97 follow associated humongous starts, not pinnable/movable by themselves); 98 i) Empty cannot go Trash, avoiding useless work; 99 j) ... 100 */ 101 102 enum RegionState { 103 _empty_uncommitted, // region is empty and has memory uncommitted 104 _empty_committed, // region is empty and has memory committed 105 _regular, // region is for regular allocations 106 _humongous_start, // region is the humongous start 107 _humongous_cont, // region is the humongous continuation 108 _pinned_humongous_start, // region is both humongous start and pinned 109 _cset, // region is in collection set 110 _pinned, // region is pinned 111 _pinned_cset, // region is pinned and in cset (evac failure path) 112 _trash, // region contains only trash 113 }; 114 115 const char* region_state_to_string(RegionState s) const { 116 switch (s) { 117 case _empty_uncommitted: return "Empty Uncommitted"; 118 case _empty_committed: return "Empty Committed"; 119 case _regular: return "Regular"; 120 case _humongous_start: return "Humongous Start"; 121 case _humongous_cont: return "Humongous Continuation"; 122 case _pinned_humongous_start: return "Humongous Start, Pinned"; 123 case _cset: return "Collection Set"; 124 case _pinned: return "Pinned"; 125 case _pinned_cset: return "Collection Set, Pinned"; 126 case _trash: return "Trash"; 127 default: 128 ShouldNotReachHere(); 129 return ""; 130 } 131 } 132 133 // This method protects from accidental changes in enum order: 134 int region_state_to_ordinal(RegionState s) const { 135 switch (s) { 136 case _empty_uncommitted: return 0; 137 case _empty_committed: return 1; 138 case _regular: return 2; 139 case _humongous_start: return 3; 140 case _humongous_cont: return 4; 141 case _cset: return 5; 142 case _pinned: return 6; 143 case _trash: return 7; 144 case _pinned_cset: return 8; 145 case _pinned_humongous_start: return 9; 146 default: 147 ShouldNotReachHere(); 148 return -1; 149 } 150 } 151 152 void report_illegal_transition(const char* method); 153 154 public: 155 // Allowed transitions from the outside code: 156 void make_regular_allocation(); 157 void make_regular_bypass(); 158 void make_humongous_start(); 159 void make_humongous_cont(); 160 void make_humongous_start_bypass(); 161 void make_humongous_cont_bypass(); 162 void make_pinned(); 163 void make_unpinned(); 164 void make_cset(); 165 void make_trash(); 166 void make_empty(); 167 void make_uncommitted(); 168 void make_committed_bypass(); 169 170 // Individual states: 171 bool is_empty_uncommitted() const { return _state == _empty_uncommitted; } 172 bool is_empty_committed() const { return _state == _empty_committed; } 173 bool is_regular() const { return _state == _regular; } 174 bool is_humongous_continuation() const { return _state == _humongous_cont; } 175 176 // Participation in logical groups: 177 bool is_empty() const { return is_empty_committed() || is_empty_uncommitted(); } 178 bool is_active() const { return !is_empty() && !is_trash(); } 179 bool is_trash() const { return _state == _trash; } 180 bool is_humongous_start() const { return _state == _humongous_start || _state == _pinned_humongous_start; } 181 bool is_humongous() const { return is_humongous_start() || is_humongous_continuation(); } 182 bool is_committed() const { return !is_empty_uncommitted(); } 183 bool is_cset() const { return _state == _cset || _state == _pinned_cset; } 184 bool is_pinned() const { return _state == _pinned || _state == _pinned_cset || _state == _pinned_humongous_start; } 185 186 // Macro-properties: 187 bool is_alloc_allowed() const { return is_empty() || is_regular() || _state == _pinned; } 188 bool is_move_allowed() const { return is_regular() || _state == _cset || (ShenandoahHumongousMoves && _state == _humongous_start); } 189 190 RegionState state() const { return _state; } 191 int state_ordinal() const { return region_state_to_ordinal(_state); } 192 193 private: 194 static size_t RegionCount; 195 static size_t RegionSizeBytes; 196 static size_t RegionSizeWords; 197 static size_t RegionSizeBytesShift; 198 static size_t RegionSizeWordsShift; 199 static size_t RegionSizeBytesMask; 200 static size_t RegionSizeWordsMask; 201 static size_t HumongousThresholdBytes; 202 static size_t HumongousThresholdWords; 203 static size_t MaxTLABSizeBytes; 204 static size_t MaxTLABSizeWords; 205 206 private: 207 ShenandoahHeap* _heap; 208 size_t _region_number; 209 volatile jint _live_data; 210 MemRegion _reserved; 211 212 size_t _tlab_allocs; 213 size_t _gclab_allocs; 214 size_t _shared_allocs; 215 216 HeapWord* _new_top; 217 218 size_t _critical_pins; 219 220 RegionState _state; 221 double _empty_time; 222 223 ShenandoahPacer* _pacer; 224 225 public: 226 ShenandoahHeapRegion(ShenandoahHeap* heap, HeapWord* start, size_t size_words, size_t index, bool committed); 227 228 static void setup_sizes(size_t initial_heap_size, size_t max_heap_size); 229 230 double empty_time() { 231 return _empty_time; 232 } 233 234 inline static size_t required_regions(size_t bytes) { 235 return (bytes + ShenandoahHeapRegion::region_size_bytes() - 1) >> ShenandoahHeapRegion::region_size_bytes_shift(); 236 } 237 238 inline static size_t region_count() { 239 return ShenandoahHeapRegion::RegionCount; 240 } 241 242 inline static size_t region_size_bytes() { 243 return ShenandoahHeapRegion::RegionSizeBytes; 244 } 245 246 inline static size_t region_size_words() { 247 return ShenandoahHeapRegion::RegionSizeWords; 248 } 249 250 inline static size_t region_size_bytes_shift() { 251 return ShenandoahHeapRegion::RegionSizeBytesShift; 252 } 253 254 inline static size_t region_size_words_shift() { 255 return ShenandoahHeapRegion::RegionSizeWordsShift; 256 } 257 258 inline static size_t region_size_bytes_mask() { 259 return ShenandoahHeapRegion::RegionSizeBytesMask; 260 } 261 262 inline static size_t region_size_words_mask() { 263 return ShenandoahHeapRegion::RegionSizeWordsMask; 264 } 265 266 // Convert to jint with sanity checking 267 inline static jint region_size_bytes_jint() { 268 assert (ShenandoahHeapRegion::RegionSizeBytes <= (size_t)max_jint, "sanity"); 269 return (jint)ShenandoahHeapRegion::RegionSizeBytes; 270 } 271 272 // Convert to jint with sanity checking 273 inline static jint region_size_words_jint() { 274 assert (ShenandoahHeapRegion::RegionSizeWords <= (size_t)max_jint, "sanity"); 275 return (jint)ShenandoahHeapRegion::RegionSizeWords; 276 } 277 278 // Convert to jint with sanity checking 279 inline static jint region_size_bytes_shift_jint() { 280 assert (ShenandoahHeapRegion::RegionSizeBytesShift <= (size_t)max_jint, "sanity"); 281 return (jint)ShenandoahHeapRegion::RegionSizeBytesShift; 282 } 283 284 // Convert to jint with sanity checking 285 inline static jint region_size_words_shift_jint() { 286 assert (ShenandoahHeapRegion::RegionSizeWordsShift <= (size_t)max_jint, "sanity"); 287 return (jint)ShenandoahHeapRegion::RegionSizeWordsShift; 288 } 289 290 inline static size_t humongous_threshold_bytes() { 291 return ShenandoahHeapRegion::HumongousThresholdBytes; 292 } 293 294 inline static size_t humongous_threshold_words() { 295 return ShenandoahHeapRegion::HumongousThresholdWords; 296 } 297 298 inline static size_t max_tlab_size_bytes() { 299 return ShenandoahHeapRegion::MaxTLABSizeBytes; 300 } 301 302 inline static size_t max_tlab_size_words() { 303 return ShenandoahHeapRegion::MaxTLABSizeWords; 304 } 305 306 size_t region_number() const; 307 308 // Allocation (return NULL if full) 309 inline HeapWord* allocate(size_t word_size, ShenandoahHeap::AllocType type); 310 HeapWord* allocate(size_t word_size) { 311 // ContiguousSpace wants us to have this method. But it is an error to call this with Shenandoah. 312 ShouldNotCallThis(); 313 return NULL; 314 } 315 316 // Roll back the previous allocation of an object with specified size. 317 // Returns TRUE when successful, FALSE if not successful or not supported. 318 bool rollback_allocation(uint size); 319 320 void clear_live_data(); 321 void set_live_data(size_t s); 322 323 // Increase live data for newly allocated region 324 inline void increase_live_data_alloc_words(size_t s); 325 326 // Increase live data for region scanned with GC 327 inline void increase_live_data_gc_words(size_t s); 328 329 bool has_live() const; 330 size_t get_live_data_bytes() const; 331 size_t get_live_data_words() const; 332 333 void print_on(outputStream* st) const; 334 335 size_t garbage() const; 336 337 void recycle(); 338 339 void oop_iterate_skip_unreachable(ExtendedOopClosure* cl, bool skip_unreachable_objects); 340 341 HeapWord* object_iterate_careful(ObjectClosureCareful* cl); 342 343 HeapWord* block_start_const(const void* p) const; 344 345 // Just before GC we need to fill the current region. 346 void fill_region(); 347 348 bool in_collection_set() const; 349 350 // Find humongous start region that this region belongs to 351 ShenandoahHeapRegion* humongous_start_region() const; 352 353 void set_new_top(HeapWord* new_top) { _new_top = new_top; } 354 HeapWord* new_top() const { return _new_top; } 355 356 inline void adjust_alloc_metadata(ShenandoahHeap::AllocType type, size_t); 357 void reset_alloc_metadata_to_shared(); 358 void reset_alloc_metadata(); 359 size_t get_shared_allocs() const; 360 size_t get_tlab_allocs() const; 361 size_t get_gclab_allocs() const; 362 363 private: 364 void do_commit(); 365 void do_uncommit(); 366 367 inline void internal_increase_live_data(size_t s); 368 }; 369 370 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP