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 "gc/shared/space.hpp" 28 #include "memory/universe.hpp" 29 #include "utilities/sizes.hpp" 30 31 class VMStructs; 32 class ShenandoahHeap; 33 34 class ShenandoahHeapRegion : public ContiguousSpace { 35 friend class VMStructs; 36 private: 37 /* 38 Region state is described by a state machine. Transitions are guarded by 39 heap lock, which allows changing the state of several regions atomically. 40 Region states can be logically aggregated in groups. 41 42 "Empty": 43 ................................................................. 44 . . 45 . . 46 . Uncommitted <------- Committed <------------------------\ 47 . | | . | 48 . \---------v-----------/ . | 49 . | . | 50 .........................|....................................... | 51 | | 52 "Active": | | 53 .........................|....................................... | 54 . | . | 55 . /-----------------^-------------------\ . | 56 . | | . | 57 . v v "Humongous": . | 58 . Regular ---\-----\ ..................O................ . | 59 . | ^ | | . | . . | 60 . | | | | . *---------\ . . | 61 . v | | | . v v . . | 62 . Pinned Cset | . HStart <--> H/Start H/Cont . . | 63 . ^ / | | . Pinned v | . . | 64 . | / | | . *<--------/ . . | 65 . | v | | . | . . | 66 . CsetPinned | | ..................O................ . | 67 . | | | . | 68 . \-----\---v-------------------/ . | 69 . | . | 70 .........................|....................................... | 71 | | 72 "Trash": | | 73 .........................|....................................... | 74 . | . | 75 . v . | 76 . Trash ---------------------------------------/ 77 . . 78 . . 79 ................................................................. 80 81 Transition from "Empty" to "Active" is first allocation. It can go from {Uncommitted, Committed} 82 to {Regular, "Humongous"}. The allocation may happen in Regular regions too, but not in Humongous. 83 84 Transition from "Active" to "Trash" is reclamation. It can go from CSet during the normal cycle, 85 and from {Regular, "Humongous"} for immediate reclamation. The existence of Trash state allows 86 quick reclamation without actual cleaning up. 87 88 Transition from "Trash" to "Empty" is recycling. It cleans up the regions and corresponding metadata. 89 Can be done asynchronously and in bulk. 90 91 Note how internal transitions disallow logic bugs: 92 a) No region can go Empty, unless properly reclaimed/recycled; 93 b) No region can go Uncommitted, unless reclaimed/recycled first; 94 c) Only Regular regions can go to CSet; 95 d) Pinned cannot go Trash, thus it could never be reclaimed until unpinned; 96 e) Pinned cannot go CSet, thus it never moves; 97 f) Humongous cannot be used for regular allocations; 98 g) Humongous cannot go CSet, thus it never moves; 99 h) Humongous start can go pinned, and thus can be protected from moves (humongous continuations should 100 follow associated humongous starts, not pinnable/movable by themselves); 101 i) Empty cannot go Trash, avoiding useless work; 102 j) ... 103 */ 104 105 enum RegionState { 106 _empty_uncommitted, // region is empty and has memory uncommitted 107 _empty_committed, // region is empty and has memory committed 108 _regular, // region is for regular allocations 109 _humongous_start, // region is the humongous start 110 _humongous_cont, // region is the humongous continuation 111 _pinned_humongous_start, // region is both humongous start and pinned 112 _cset, // region is in collection set 113 _pinned, // region is pinned 114 _pinned_cset, // region is pinned and in cset (evac failure path) 115 _trash, // region contains only trash 116 }; 117 118 const char* region_state_to_string(RegionState s) const { 119 switch (s) { 120 case _empty_uncommitted: return "Empty Uncommitted"; 121 case _empty_committed: return "Empty Committed"; 122 case _regular: return "Regular"; 123 case _humongous_start: return "Humongous Start"; 124 case _humongous_cont: return "Humongous Continuation"; 125 case _pinned_humongous_start: return "Humongous Start, Pinned"; 126 case _cset: return "Collection Set"; 127 case _pinned: return "Pinned"; 128 case _pinned_cset: return "Collection Set, Pinned"; 129 case _trash: return "Trash"; 130 default: 131 ShouldNotReachHere(); 132 return ""; 133 } 134 } 135 136 // This method protects from accidental changes in enum order: 137 int region_state_to_ordinal(RegionState s) const { 138 switch (s) { 139 case _empty_uncommitted: return 0; 140 case _empty_committed: return 1; 141 case _regular: return 2; 142 case _humongous_start: return 3; 143 case _humongous_cont: return 4; 144 case _cset: return 5; 145 case _pinned: return 6; 146 case _trash: return 7; 147 case _pinned_cset: return 8; 148 case _pinned_humongous_start: return 9; 149 default: 150 ShouldNotReachHere(); 151 return -1; 152 } 153 } 154 155 void report_illegal_transition(const char* method); 156 157 void make_idle(); 158 void activate_region(); 159 160 bool can_idle_region() const; 161 public: 162 // Allowed transitions from the outside code: 163 void make_regular_allocation(); 164 void make_regular_bypass(); 165 void make_humongous_start(); 166 void make_humongous_cont(); 167 void make_humongous_start_bypass(); 168 void make_humongous_cont_bypass(); 169 void make_pinned(); 170 void make_unpinned(); 171 void make_cset(); 172 void make_trash(); 173 void make_empty(); 174 void make_uncommitted(); 175 void make_committed_bypass(); 176 177 // Individual states: 178 bool is_empty_uncommitted() const { return _state == _empty_uncommitted; } 179 bool is_empty_committed() const { return _state == _empty_committed; } 180 bool is_regular() const { return _state == _regular; } 181 bool is_humongous_continuation() const { return _state == _humongous_cont; } 182 183 // Participation in logical groups: 184 bool is_empty() const { return is_empty_committed() || is_empty_uncommitted(); } 185 bool is_active() const { return !is_empty() && !is_trash(); } 186 bool is_trash() const { return _state == _trash; } 187 bool is_humongous_start() const { return _state == _humongous_start || _state == _pinned_humongous_start; } 188 bool is_humongous() const { return is_humongous_start() || is_humongous_continuation(); } 189 bool is_committed() const { return !is_empty_uncommitted(); } 190 bool is_cset() const { return _state == _cset || _state == _pinned_cset; } 191 bool is_pinned() const { return _state == _pinned || _state == _pinned_cset || _state == _pinned_humongous_start; } 192 193 // Macro-properties: 194 bool is_alloc_allowed() const { return is_empty() || is_regular() || _state == _pinned; } 195 bool is_move_allowed() const { return is_regular() || _state == _cset || (ShenandoahHumongousMoves && _state == _humongous_start); } 196 197 RegionState state() const { return _state; } 198 int state_ordinal() const { return region_state_to_ordinal(_state); } 199 200 private: 201 static size_t RegionSizeBytes; 202 static size_t RegionSizeWords; 203 static size_t RegionSizeBytesShift; 204 static size_t RegionSizeWordsShift; 205 static size_t RegionSizeBytesMask; 206 static size_t RegionSizeWordsMask; 207 static size_t HumongousThresholdBytes; 208 static size_t HumongousThresholdWords; 209 static size_t MaxTLABSizeBytes; 210 211 // Global alloaction counter, increased for each allocation 212 // under Shenandoah heap lock 213 static uint64_t AllocSeqNum; 214 215 ShenandoahHeap* _heap; 216 size_t _region_number; 217 volatile size_t _live_data; 218 MemRegion _reserved; 219 220 size_t _tlab_allocs; 221 size_t _gclab_allocs; 222 size_t _shared_allocs; 223 224 HeapWord* _new_top; 225 226 size_t _critical_pins; 227 228 // Seq numbers are used to drive heuristics decisions for collection. 229 // They are set when the region is used for allocation. 230 uint64_t _seqnum_first_alloc_mutator; 231 uint64_t _seqnum_first_alloc_gc; 232 uint64_t _seqnum_last_alloc_mutator; 233 uint64_t _seqnum_last_alloc_gc; 234 235 RegionState _state; 236 double _empty_time; 237 238 // If the region has been initially committed. It has been committed before 239 // it can be idled 240 bool _initialized; 241 242 ShenandoahPacer* _pacer; 243 244 public: 245 ShenandoahHeapRegion(ShenandoahHeap* heap, HeapWord* start, size_t size_words, size_t index, bool committed); 246 247 static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size); 248 249 double empty_time() { 250 return _empty_time; 251 } 252 253 inline static size_t required_regions(size_t bytes) { 254 return (bytes + ShenandoahHeapRegion::region_size_bytes() - 1) >> ShenandoahHeapRegion::region_size_bytes_shift(); 255 } 256 257 inline static size_t region_size_bytes() { 258 return ShenandoahHeapRegion::RegionSizeBytes; 259 } 260 261 inline static size_t region_size_words() { 262 return ShenandoahHeapRegion::RegionSizeWords; 263 } 264 265 inline static size_t region_size_bytes_shift() { 266 return ShenandoahHeapRegion::RegionSizeBytesShift; 267 } 268 269 inline static size_t region_size_words_shift() { 270 return ShenandoahHeapRegion::RegionSizeWordsShift; 271 } 272 273 inline static size_t region_size_bytes_mask() { 274 return ShenandoahHeapRegion::RegionSizeBytesMask; 275 } 276 277 inline static size_t region_size_words_mask() { 278 return ShenandoahHeapRegion::RegionSizeWordsMask; 279 } 280 281 // Convert to jint with sanity checking 282 inline static jint region_size_bytes_jint() { 283 assert (ShenandoahHeapRegion::RegionSizeBytes <= (size_t)max_jint, "sanity"); 284 return (jint)ShenandoahHeapRegion::RegionSizeBytes; 285 } 286 287 // Convert to jint with sanity checking 288 inline static jint region_size_words_jint() { 289 assert (ShenandoahHeapRegion::RegionSizeWords <= (size_t)max_jint, "sanity"); 290 return (jint)ShenandoahHeapRegion::RegionSizeWords; 291 } 292 293 // Convert to jint with sanity checking 294 inline static jint region_size_bytes_shift_jint() { 295 assert (ShenandoahHeapRegion::RegionSizeBytesShift <= (size_t)max_jint, "sanity"); 296 return (jint)ShenandoahHeapRegion::RegionSizeBytesShift; 297 } 298 299 // Convert to jint with sanity checking 300 inline static jint region_size_words_shift_jint() { 301 assert (ShenandoahHeapRegion::RegionSizeWordsShift <= (size_t)max_jint, "sanity"); 302 return (jint)ShenandoahHeapRegion::RegionSizeWordsShift; 303 } 304 305 inline static size_t humongous_threshold_bytes() { 306 return ShenandoahHeapRegion::HumongousThresholdBytes; 307 } 308 309 inline static size_t humongous_threshold_words() { 310 return ShenandoahHeapRegion::HumongousThresholdWords; 311 } 312 313 inline static size_t max_tlab_size_bytes() { 314 return ShenandoahHeapRegion::MaxTLABSizeBytes; 315 } 316 317 static uint64_t seqnum_current_alloc() { 318 // Last used seq number 319 return AllocSeqNum - 1; 320 } 321 322 size_t region_number() const; 323 324 // Allocation (return NULL if full) 325 inline HeapWord* allocate(size_t word_size, ShenandoahHeap::AllocType type); 326 HeapWord* allocate(size_t word_size) { 327 // ContiguousSpace wants us to have this method. But it is an error to call this with Shenandoah. 328 ShouldNotCallThis(); 329 return NULL; 330 } 331 332 // Roll back the previous allocation of an object with specified size. 333 // Returns TRUE when successful, FALSE if not successful or not supported. 334 bool rollback_allocation(uint size); 335 336 void clear_live_data(); 337 void set_live_data(size_t s); 338 339 // Increase live data for newly allocated region 340 inline void increase_live_data_alloc_words(size_t s); 341 342 // Increase live data for region scanned with GC 343 inline void increase_live_data_gc_words(size_t s); 344 345 bool has_live() const; 346 size_t get_live_data_bytes() const; 347 size_t get_live_data_words() const; 348 349 void print_on(outputStream* st) const; 350 351 size_t garbage() const; 352 353 void recycle(); 354 355 void oop_iterate(ExtendedOopClosure* cl); 356 357 HeapWord* block_start_const(const void* p) const; 358 359 // Just before GC we need to fill the current region. 360 void fill_region(); 361 362 bool in_collection_set() const; 363 364 // Find humongous start region that this region belongs to 365 ShenandoahHeapRegion* humongous_start_region() const; 366 367 virtual CompactibleSpace* next_compaction_space() const; 368 369 // Override for scan_and_forward support. 370 void prepare_for_compaction(CompactPoint* cp); 371 void adjust_pointers(); 372 void compact(); 373 374 void set_new_top(HeapWord* new_top) { _new_top = new_top; } 375 HeapWord* new_top() const { return _new_top; } 376 377 inline void adjust_alloc_metadata(ShenandoahHeap::AllocType type, size_t); 378 void reset_alloc_metadata_to_shared(); 379 void reset_alloc_metadata(); 380 size_t get_shared_allocs() const; 381 size_t get_tlab_allocs() const; 382 size_t get_gclab_allocs() const; 383 384 uint64_t seqnum_first_alloc() const { 385 if (_seqnum_first_alloc_mutator == 0) return _seqnum_first_alloc_gc; 386 if (_seqnum_first_alloc_gc == 0) return _seqnum_first_alloc_mutator; 387 return MIN2(_seqnum_first_alloc_mutator, _seqnum_first_alloc_gc); 388 } 389 390 uint64_t seqnum_last_alloc() const { 391 return MAX2(_seqnum_last_alloc_mutator, _seqnum_last_alloc_gc); 392 } 393 394 uint64_t seqnum_first_alloc_mutator() const { 395 return _seqnum_first_alloc_mutator; 396 } 397 398 uint64_t seqnum_last_alloc_mutator() const { 399 return _seqnum_last_alloc_mutator; 400 } 401 402 uint64_t seqnum_first_alloc_gc() const { 403 return _seqnum_first_alloc_gc; 404 } 405 406 uint64_t seqnum_last_alloc_gc() const { 407 return _seqnum_last_alloc_gc; 408 } 409 410 private: 411 void do_commit(); 412 void do_uncommit(); 413 414 void oop_iterate_objects(ExtendedOopClosure* cl); 415 void oop_iterate_humongous(ExtendedOopClosure* cl); 416 417 inline void internal_increase_live_data(size_t s); 418 }; 419 420 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP