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