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