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   bool _root;
 225 
 226   HeapWord* _new_top;
 227 
 228   size_t _critical_pins;
 229 
 230   // Seq numbers are used to drive heuristics decisions for collection.
 231   // They are set when the region is used for allocation.
 232   uint64_t  _seqnum_first_alloc_mutator;
 233   uint64_t  _seqnum_first_alloc_gc;
 234   uint64_t  _seqnum_last_alloc_mutator;
 235   uint64_t  _seqnum_last_alloc_gc;
 236 
 237   RegionState _state;
 238   double _empty_time;
 239 
 240   // If the region has been initially committed. It has been committed before
 241   // it can be idled
 242   bool   _initialized;
 243 
 244   ShenandoahPacer* _pacer;
 245 
 246 public:
 247   ShenandoahHeapRegion(ShenandoahHeap* heap, HeapWord* start, size_t size_words, size_t index, bool committed);
 248 
 249   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
 250 
 251   double empty_time() {
 252     return _empty_time;
 253   }
 254 
 255   inline static size_t required_regions(size_t bytes) {
 256     return (bytes + ShenandoahHeapRegion::region_size_bytes() - 1) >> ShenandoahHeapRegion::region_size_bytes_shift();
 257   }
 258 
 259   inline static size_t region_size_bytes() {
 260     return ShenandoahHeapRegion::RegionSizeBytes;
 261   }
 262 
 263   inline static size_t region_size_words() {
 264     return ShenandoahHeapRegion::RegionSizeWords;
 265   }
 266 
 267   inline static size_t region_size_bytes_shift() {
 268     return ShenandoahHeapRegion::RegionSizeBytesShift;
 269   }
 270 
 271   inline static size_t region_size_words_shift() {
 272     return ShenandoahHeapRegion::RegionSizeWordsShift;
 273   }
 274 
 275   inline static size_t region_size_bytes_mask() {
 276     return ShenandoahHeapRegion::RegionSizeBytesMask;
 277   }
 278 
 279   inline static size_t region_size_words_mask() {
 280     return ShenandoahHeapRegion::RegionSizeWordsMask;
 281   }
 282 
 283   // Convert to jint with sanity checking
 284   inline static jint region_size_bytes_jint() {
 285     assert (ShenandoahHeapRegion::RegionSizeBytes <= (size_t)max_jint, "sanity");
 286     return (jint)ShenandoahHeapRegion::RegionSizeBytes;
 287   }
 288 
 289   // Convert to jint with sanity checking
 290   inline static jint region_size_words_jint() {
 291     assert (ShenandoahHeapRegion::RegionSizeWords <= (size_t)max_jint, "sanity");
 292     return (jint)ShenandoahHeapRegion::RegionSizeWords;
 293   }
 294 
 295   // Convert to jint with sanity checking
 296   inline static jint region_size_bytes_shift_jint() {
 297     assert (ShenandoahHeapRegion::RegionSizeBytesShift <= (size_t)max_jint, "sanity");
 298     return (jint)ShenandoahHeapRegion::RegionSizeBytesShift;
 299   }
 300 
 301   // Convert to jint with sanity checking
 302   inline static jint region_size_words_shift_jint() {
 303     assert (ShenandoahHeapRegion::RegionSizeWordsShift <= (size_t)max_jint, "sanity");
 304     return (jint)ShenandoahHeapRegion::RegionSizeWordsShift;
 305   }
 306 
 307   inline static size_t humongous_threshold_bytes() {
 308     return ShenandoahHeapRegion::HumongousThresholdBytes;
 309   }
 310 
 311   inline static size_t humongous_threshold_words() {
 312     return ShenandoahHeapRegion::HumongousThresholdWords;
 313   }
 314 
 315   inline static size_t max_tlab_size_bytes() {
 316     return ShenandoahHeapRegion::MaxTLABSizeBytes;
 317   }
 318 
 319   static uint64_t seqnum_current_alloc() {
 320     // Last used seq number
 321     return AllocSeqNum - 1;
 322   }
 323 
 324   size_t region_number() const;
 325 
 326   // Allocation (return NULL if full)
 327   inline HeapWord* allocate(size_t word_size, ShenandoahHeap::AllocType type);
 328   HeapWord* allocate(size_t word_size) {
 329     // ContiguousSpace wants us to have this method. But it is an error to call this with Shenandoah.
 330     ShouldNotCallThis();
 331     return NULL;
 332   }
 333 
 334   // Roll back the previous allocation of an object with specified size.
 335   // Returns TRUE when successful, FALSE if not successful or not supported.
 336   bool rollback_allocation(uint size);
 337 
 338   void clear_live_data();
 339   void set_live_data(size_t s);
 340 
 341   // Increase live data for newly allocated region
 342   inline void increase_live_data_alloc_words(size_t s);
 343 
 344   // Increase live data for region scanned with GC
 345   inline void increase_live_data_gc_words(size_t s);
 346 
 347   bool has_live() const;
 348   size_t get_live_data_bytes() const;
 349   size_t get_live_data_words() const;
 350 
 351   void print_on(outputStream* st) const;
 352 
 353   size_t garbage() const;
 354 
 355   void recycle();
 356 
 357   void oop_iterate(ExtendedOopClosure* cl);
 358 
 359   HeapWord* block_start_const(const void* p) const;
 360 
 361   // Just before GC we need to fill the current region.
 362   void fill_region();
 363 
 364   bool in_collection_set() const;
 365 
 366   // Find humongous start region that this region belongs to
 367   ShenandoahHeapRegion* humongous_start_region() const;
 368 
 369   virtual CompactibleSpace* next_compaction_space() const;
 370 
 371   // Override for scan_and_forward support.
 372   void prepare_for_compaction(CompactPoint* cp);
 373   void adjust_pointers();
 374   void compact();
 375 
 376   void set_new_top(HeapWord* new_top) { _new_top = new_top; }
 377   HeapWord* new_top() const { return _new_top; }
 378 
 379   void set_root(bool r) {
 380     _root = r;
 381   }
 382   bool is_root() const {
 383     return _root;
 384   }
 385 
 386   inline void adjust_alloc_metadata(ShenandoahHeap::AllocType type, size_t);
 387   void reset_alloc_metadata_to_shared();
 388   void reset_alloc_metadata();
 389   size_t get_shared_allocs() const;
 390   size_t get_tlab_allocs() const;
 391   size_t get_gclab_allocs() const;
 392 
 393   uint64_t seqnum_first_alloc() const {
 394     if (_seqnum_first_alloc_mutator == 0) return _seqnum_first_alloc_gc;
 395     if (_seqnum_first_alloc_gc == 0)      return _seqnum_first_alloc_mutator;
 396     return MIN2(_seqnum_first_alloc_mutator, _seqnum_first_alloc_gc);
 397   }
 398 
 399   uint64_t seqnum_last_alloc() const {
 400     return MAX2(_seqnum_last_alloc_mutator, _seqnum_last_alloc_gc);
 401   }
 402 
 403   uint64_t seqnum_first_alloc_mutator() const {
 404     return _seqnum_first_alloc_mutator;
 405   }
 406 
 407   uint64_t seqnum_last_alloc_mutator()  const {
 408     return _seqnum_last_alloc_mutator;
 409   }
 410 
 411   uint64_t seqnum_first_alloc_gc() const {
 412     return _seqnum_first_alloc_gc;
 413   }
 414 
 415   uint64_t seqnum_last_alloc_gc()  const {
 416     return _seqnum_last_alloc_gc;
 417   }
 418 
 419 private:
 420   void do_commit();
 421   void do_uncommit();
 422 
 423   void oop_iterate_objects(ExtendedOopClosure* cl);
 424   void oop_iterate_humongous(ExtendedOopClosure* cl);
 425 
 426   inline void internal_increase_live_data(size_t s);
 427 };
 428 
 429 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP