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