1 /*
   2  * Copyright (c) 2013, 2020, 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_SHENANDOAHHEAP_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
  27 
  28 #include "gc/shared/markBitMap.hpp"
  29 #include "gc/shared/softRefPolicy.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "gc/shenandoah/shenandoahAsserts.hpp"
  32 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  33 #include "gc/shenandoah/shenandoahLock.hpp"
  34 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
  35 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
  36 #include "gc/shenandoah/shenandoahUnload.hpp"
  37 #include "services/memoryManager.hpp"
  38 #include "utilities/globalDefinitions.hpp"
  39 
  40 class ConcurrentGCTimer;
  41 class ReferenceProcessor;
  42 class ShenandoahAllocTracker;
  43 class ShenandoahCollectorPolicy;
  44 class ShenandoahControlThread;
  45 class ShenandoahGCSession;
  46 class ShenandoahGCStateResetter;
  47 class ShenandoahHeuristics;
  48 class ShenandoahMarkingContext;
  49 class ShenandoahMarkCompact;
  50 class ShenandoahMode;
  51 class ShenandoahPhaseTimings;
  52 class ShenandoahHeap;
  53 class ShenandoahHeapRegion;
  54 class ShenandoahHeapRegionClosure;
  55 class ShenandoahCollectionSet;
  56 class ShenandoahFreeSet;
  57 class ShenandoahConcurrentMark;
  58 class ShenandoahMarkCompact;
  59 class ShenandoahMonitoringSupport;
  60 class ShenandoahPacer;
  61 class ShenandoahTraversalGC;
  62 class ShenandoahVerifier;
  63 class ShenandoahWorkGang;
  64 class VMStructs;
  65 
  66 class ShenandoahRegionIterator : public StackObj {
  67 private:
  68   ShenandoahHeap* _heap;
  69 
  70   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  71   volatile size_t _index;
  72   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  73 
  74   // No implicit copying: iterators should be passed by reference to capture the state
  75   NONCOPYABLE(ShenandoahRegionIterator);
  76 
  77 public:
  78   ShenandoahRegionIterator();
  79   ShenandoahRegionIterator(ShenandoahHeap* heap);
  80 
  81   // Reset iterator to default state
  82   void reset();
  83 
  84   // Returns next region, or NULL if there are no more regions.
  85   // This is multi-thread-safe.
  86   inline ShenandoahHeapRegion* next();
  87 
  88   // This is *not* MT safe. However, in the absence of multithreaded access, it
  89   // can be used to determine if there is more work to do.
  90   bool has_next() const;
  91 };
  92 
  93 class ShenandoahHeapRegionClosure : public StackObj {
  94 public:
  95   virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
  96   virtual bool is_thread_safe() { return false; }
  97 };
  98 
  99 #ifdef ASSERT
 100 class ShenandoahAssertToSpaceClosure : public OopClosure {
 101 private:
 102   template <class T>
 103   void do_oop_work(T* p);
 104 public:
 105   void do_oop(narrowOop* p);
 106   void do_oop(oop* p);
 107 };
 108 #endif
 109 
 110 typedef ShenandoahLock    ShenandoahHeapLock;
 111 typedef ShenandoahLocker  ShenandoahHeapLocker;
 112 
 113 // Shenandoah GC is low-pause concurrent GC that uses Brooks forwarding pointers
 114 // to encode forwarding data. See BrooksPointer for details on forwarding data encoding.
 115 // See ShenandoahControlThread for GC cycle structure.
 116 //
 117 class ShenandoahHeap : public CollectedHeap {
 118   friend class ShenandoahAsserts;
 119   friend class VMStructs;
 120   friend class ShenandoahGCSession;
 121   friend class ShenandoahGCStateResetter;
 122 
 123 // ---------- Locks that guard important data structures in Heap
 124 //
 125 private:
 126   ShenandoahHeapLock _lock;
 127 
 128 public:
 129   ShenandoahHeapLock* lock() {
 130     return &_lock;
 131   }
 132 
 133   void assert_heaplock_owned_by_current_thread()     NOT_DEBUG_RETURN;
 134   void assert_heaplock_not_owned_by_current_thread() NOT_DEBUG_RETURN;
 135   void assert_heaplock_or_safepoint()                NOT_DEBUG_RETURN;
 136 
 137 // ---------- Initialization, termination, identification, printing routines
 138 //
 139 public:
 140   static ShenandoahHeap* heap();
 141   static ShenandoahHeap* heap_no_check();
 142 
 143   const char* name()          const { return "Shenandoah"; }
 144   ShenandoahHeap::Name kind() const { return CollectedHeap::Shenandoah; }
 145 
 146   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
 147   jint initialize();
 148   void post_initialize();
 149   void initialize_heuristics();
 150 
 151   void initialize_serviceability();
 152 
 153   void print_on(outputStream* st)              const;
 154   void print_extended_on(outputStream *st)     const;
 155   void print_tracing_info()                    const;
 156   void print_gc_threads_on(outputStream* st)   const;
 157   void print_heap_regions_on(outputStream* st) const;
 158 
 159   void stop();
 160 
 161   void prepare_for_verify();
 162   void verify(VerifyOption vo);
 163 
 164 // ---------- Heap counters and metrics
 165 //
 166 private:
 167            size_t _initial_size;
 168            size_t _minimum_size;
 169   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
 170   volatile size_t _used;
 171   volatile size_t _committed;
 172   volatile size_t _bytes_allocated_since_gc_start;
 173   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
 174 
 175 public:
 176   void increase_used(size_t bytes);
 177   void decrease_used(size_t bytes);
 178   void set_used(size_t bytes);
 179 
 180   void increase_committed(size_t bytes);
 181   void decrease_committed(size_t bytes);
 182   void increase_allocated(size_t bytes);
 183 
 184   size_t bytes_allocated_since_gc_start();
 185   void reset_bytes_allocated_since_gc_start();
 186 
 187   size_t min_capacity()     const;
 188   size_t max_capacity()     const;
 189   size_t initial_capacity() const;
 190   size_t capacity()         const;
 191   size_t used()             const;
 192   size_t committed()        const;
 193 
 194 // ---------- Workers handling
 195 //
 196 private:
 197   uint _max_workers;
 198   ShenandoahWorkGang* _workers;
 199   ShenandoahWorkGang* _safepoint_workers;
 200 
 201 public:
 202   uint max_workers();
 203   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
 204 
 205   WorkGang* workers() const;
 206   WorkGang* get_safepoint_workers();
 207 
 208   void gc_threads_do(ThreadClosure* tcl) const;
 209 
 210 // ---------- Heap regions handling machinery
 211 //
 212 private:
 213   MemRegion _heap_region;
 214   bool      _heap_region_special;
 215   size_t    _num_regions;
 216   ShenandoahHeapRegion** _regions;
 217   ShenandoahRegionIterator _update_refs_iterator;
 218 
 219 public:
 220 
 221   inline HeapWord* base() const { return _heap_region.start(); }
 222 
 223   inline size_t num_regions() const { return _num_regions; }
 224   inline bool is_heap_region_special() { return _heap_region_special; }
 225 
 226   inline ShenandoahHeapRegion* const heap_region_containing(const void* addr) const;
 227   inline size_t heap_region_index_containing(const void* addr) const;
 228 
 229   inline ShenandoahHeapRegion* const get_region(size_t region_idx) const;
 230 
 231   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
 232   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
 233 
 234 // ---------- GC state machinery
 235 //
 236 // GC state describes the important parts of collector state, that may be
 237 // used to make barrier selection decisions in the native and generated code.
 238 // Multiple bits can be set at once.
 239 //
 240 // Important invariant: when GC state is zero, the heap is stable, and no barriers
 241 // are required.
 242 //
 243 public:
 244   enum GCStateBitPos {
 245     // Heap has forwarded objects: needs LRB barriers.
 246     HAS_FORWARDED_BITPOS   = 0,
 247 
 248     // Heap is under marking: needs SATB barriers.
 249     MARKING_BITPOS    = 1,
 250 
 251     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
 252     EVACUATION_BITPOS = 2,
 253 
 254     // Heap is under updating: needs no additional barriers.
 255     UPDATEREFS_BITPOS = 3,
 256 
 257     // Heap is under traversal collection
 258     TRAVERSAL_BITPOS  = 4
 259   };
 260 
 261   enum GCState {
 262     STABLE        = 0,
 263     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
 264     MARKING       = 1 << MARKING_BITPOS,
 265     EVACUATION    = 1 << EVACUATION_BITPOS,
 266     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
 267     TRAVERSAL     = 1 << TRAVERSAL_BITPOS
 268   };
 269 
 270 private:
 271   ShenandoahSharedBitmap _gc_state;
 272   ShenandoahSharedFlag   _degenerated_gc_in_progress;
 273   ShenandoahSharedFlag   _full_gc_in_progress;
 274   ShenandoahSharedFlag   _full_gc_move_in_progress;
 275   ShenandoahSharedFlag   _progress_last_gc;
 276   ShenandoahSharedFlag   _concurrent_root_in_progress;
 277 
 278   void set_gc_state_all_threads(char state);
 279   void set_gc_state_mask(uint mask, bool value);
 280 
 281 public:
 282   char gc_state() const;
 283   static address gc_state_addr();
 284 
 285   void set_concurrent_mark_in_progress(bool in_progress);
 286   void set_evacuation_in_progress(bool in_progress);
 287   void set_update_refs_in_progress(bool in_progress);
 288   void set_degenerated_gc_in_progress(bool in_progress);
 289   void set_full_gc_in_progress(bool in_progress);
 290   void set_full_gc_move_in_progress(bool in_progress);
 291   void set_concurrent_traversal_in_progress(bool in_progress);
 292   void set_has_forwarded_objects(bool cond);
 293   void set_concurrent_root_in_progress(bool cond);
 294 
 295   inline bool is_stable() const;
 296   inline bool is_idle() const;
 297   inline bool is_concurrent_mark_in_progress() const;
 298   inline bool is_update_refs_in_progress() const;
 299   inline bool is_evacuation_in_progress() const;
 300   inline bool is_degenerated_gc_in_progress() const;
 301   inline bool is_full_gc_in_progress() const;
 302   inline bool is_full_gc_move_in_progress() const;
 303   inline bool is_concurrent_traversal_in_progress() const;
 304   inline bool has_forwarded_objects() const;
 305   inline bool is_gc_in_progress_mask(uint mask) const;
 306   inline bool is_stw_gc_in_progress() const;
 307   inline bool is_concurrent_root_in_progress() const;
 308 
 309 // ---------- GC cancellation and degeneration machinery
 310 //
 311 // Cancelled GC flag is used to notify concurrent phases that they should terminate.
 312 //
 313 public:
 314   enum ShenandoahDegenPoint {
 315     _degenerated_unset,
 316     _degenerated_traversal,
 317     _degenerated_outside_cycle,
 318     _degenerated_mark,
 319     _degenerated_evac,
 320     _degenerated_updaterefs,
 321     _DEGENERATED_LIMIT
 322   };
 323 
 324   static const char* degen_point_to_string(ShenandoahDegenPoint point) {
 325     switch (point) {
 326       case _degenerated_unset:
 327         return "<UNSET>";
 328       case _degenerated_traversal:
 329         return "Traversal";
 330       case _degenerated_outside_cycle:
 331         return "Outside of Cycle";
 332       case _degenerated_mark:
 333         return "Mark";
 334       case _degenerated_evac:
 335         return "Evacuation";
 336       case _degenerated_updaterefs:
 337         return "Update Refs";
 338       default:
 339         ShouldNotReachHere();
 340         return "ERROR";
 341     }
 342   };
 343 
 344 private:
 345   enum CancelState {
 346     // Normal state. GC has not been cancelled and is open for cancellation.
 347     // Worker threads can suspend for safepoint.
 348     CANCELLABLE,
 349 
 350     // GC has been cancelled. Worker threads can not suspend for
 351     // safepoint but must finish their work as soon as possible.
 352     CANCELLED,
 353 
 354     // GC has not been cancelled and must not be cancelled. At least
 355     // one worker thread checks for pending safepoint and may suspend
 356     // if a safepoint is pending.
 357     NOT_CANCELLED
 358   };
 359 
 360   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;
 361   bool try_cancel_gc();
 362 
 363 public:
 364   static address cancelled_gc_addr();
 365 
 366   inline bool cancelled_gc() const;
 367   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
 368 
 369   inline void clear_cancelled_gc();
 370 
 371   void cancel_gc(GCCause::Cause cause);
 372 
 373 // ---------- GC operations entry points
 374 //
 375 public:
 376   // Entry points to STW GC operations, these cause a related safepoint, that then
 377   // call the entry method below
 378   void vmop_entry_init_mark();
 379   void vmop_entry_final_mark();
 380   void vmop_entry_final_evac();
 381   void vmop_entry_init_updaterefs();
 382   void vmop_entry_final_updaterefs();
 383   void vmop_entry_init_traversal();
 384   void vmop_entry_final_traversal();
 385   void vmop_entry_full(GCCause::Cause cause);
 386   void vmop_degenerated(ShenandoahDegenPoint point);
 387 
 388   // Entry methods to normally STW GC operations. These set up logging, monitoring
 389   // and workers for net VM operation
 390   void entry_init_mark();
 391   void entry_final_mark();
 392   void entry_final_evac();
 393   void entry_init_updaterefs();
 394   void entry_final_updaterefs();
 395   void entry_init_traversal();
 396   void entry_final_traversal();
 397   void entry_full(GCCause::Cause cause);
 398   void entry_degenerated(int point);
 399 
 400   // Entry methods to normally concurrent GC operations. These set up logging, monitoring
 401   // for concurrent operation.
 402   void entry_reset();
 403   void entry_mark();
 404   void entry_preclean();
 405   void entry_roots();
 406   void entry_cleanup();
 407   void entry_evac();
 408   void entry_updaterefs();
 409   void entry_traversal();
 410   void entry_uncommit(double shrink_before);
 411 
 412 private:
 413   // Actual work for the phases
 414   void op_init_mark();
 415   void op_final_mark();
 416   void op_final_evac();
 417   void op_init_updaterefs();
 418   void op_final_updaterefs();
 419   void op_init_traversal();
 420   void op_final_traversal();
 421   void op_full(GCCause::Cause cause);
 422   void op_degenerated(ShenandoahDegenPoint point);
 423   void op_degenerated_fail();
 424   void op_degenerated_futile();
 425 
 426   void op_reset();
 427   void op_mark();
 428   void op_preclean();
 429   void op_roots();
 430   void op_cleanup();
 431   void op_conc_evac();
 432   void op_stw_evac();
 433   void op_updaterefs();
 434   void op_traversal();
 435   void op_uncommit(double shrink_before);
 436 
 437   // Messages for GC trace events, they have to be immortal for
 438   // passing around the logging/tracing systems
 439   const char* init_mark_event_message() const;
 440   const char* final_mark_event_message() const;
 441   const char* conc_mark_event_message() const;
 442   const char* init_traversal_event_message() const;
 443   const char* final_traversal_event_message() const;
 444   const char* conc_traversal_event_message() const;
 445   const char* degen_event_message(ShenandoahDegenPoint point) const;
 446 
 447 // ---------- GC subsystems
 448 //
 449 private:
 450   ShenandoahControlThread*   _control_thread;
 451   ShenandoahCollectorPolicy* _shenandoah_policy;
 452   ShenandoahMode*            _gc_mode;
 453   ShenandoahHeuristics*      _heuristics;
 454   ShenandoahFreeSet*         _free_set;
 455   ShenandoahConcurrentMark*  _scm;
 456   ShenandoahTraversalGC*     _traversal_gc;
 457   ShenandoahMarkCompact*     _full_gc;
 458   ShenandoahPacer*           _pacer;
 459   ShenandoahVerifier*        _verifier;
 460 
 461   ShenandoahAllocTracker*    _alloc_tracker;
 462   ShenandoahPhaseTimings*    _phase_timings;
 463 
 464   ShenandoahControlThread*   control_thread()          { return _control_thread;    }
 465   ShenandoahMarkCompact*     full_gc()                 { return _full_gc;           }
 466 
 467 public:
 468   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
 469   ShenandoahHeuristics*      heuristics()        const { return _heuristics;        }
 470   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
 471   ShenandoahConcurrentMark*  concurrent_mark()         { return _scm;               }
 472   ShenandoahTraversalGC*     traversal_gc()      const { return _traversal_gc;      }
 473   bool                       is_traversal_mode() const { return _traversal_gc != NULL; }
 474   ShenandoahPacer*           pacer()             const { return _pacer;             }
 475 
 476   ShenandoahPhaseTimings*    phase_timings()     const { return _phase_timings;     }
 477   ShenandoahAllocTracker*    alloc_tracker()     const { return _alloc_tracker;     }
 478 
 479   ShenandoahVerifier*        verifier();
 480 
 481 // ---------- VM subsystem bindings
 482 //
 483 private:
 484   ShenandoahMonitoringSupport* _monitoring_support;
 485   MemoryPool*                  _memory_pool;
 486   GCMemoryManager              _stw_memory_manager;
 487   GCMemoryManager              _cycle_memory_manager;
 488   ConcurrentGCTimer*           _gc_timer;
 489   SoftRefPolicy                _soft_ref_policy;
 490 
 491   // For exporting to SA
 492   int                          _log_min_obj_alignment_in_bytes;
 493 public:
 494   ShenandoahMonitoringSupport* monitoring_support() { return _monitoring_support;    }
 495   GCMemoryManager* cycle_memory_manager()           { return &_cycle_memory_manager; }
 496   GCMemoryManager* stw_memory_manager()             { return &_stw_memory_manager;   }
 497   SoftRefPolicy* soft_ref_policy()                  { return &_soft_ref_policy;      }
 498 
 499   GrowableArray<GCMemoryManager*> memory_managers();
 500   GrowableArray<MemoryPool*> memory_pools();
 501   MemoryUsage memory_usage();
 502   GCTracer* tracer();
 503   GCTimer* gc_timer() const;
 504 
 505 // ---------- Reference processing
 506 //
 507 private:
 508   AlwaysTrueClosure    _subject_to_discovery;
 509   ReferenceProcessor*  _ref_processor;
 510   ShenandoahSharedFlag _process_references;
 511 
 512   void ref_processing_init();
 513 
 514 public:
 515   ReferenceProcessor* ref_processor() { return _ref_processor; }
 516   void set_process_references(bool pr);
 517   bool process_references() const;
 518 
 519 // ---------- Class Unloading
 520 //
 521 private:
 522   ShenandoahSharedFlag _unload_classes;
 523   ShenandoahUnload     _unloader;
 524 
 525 public:
 526   void set_unload_classes(bool uc);
 527   bool unload_classes() const;
 528 
 529   // Perform STW class unloading and weak root cleaning
 530   void parallel_cleaning(bool full_gc);
 531 
 532 private:
 533   void stw_unload_classes(bool full_gc);
 534   void stw_process_weak_roots(bool full_gc);
 535 
 536   // Prepare concurrent root processing
 537   void prepare_concurrent_roots();
 538   // Prepare and finish concurrent unloading
 539   void prepare_concurrent_unloading();
 540   void finish_concurrent_unloading();
 541 
 542 // ---------- Generic interface hooks
 543 // Minor things that super-interface expects us to implement to play nice with
 544 // the rest of runtime. Some of the things here are not required to be implemented,
 545 // and can be stubbed out.
 546 //
 547 public:
 548   AdaptiveSizePolicy* size_policy() shenandoah_not_implemented_return(NULL);
 549   bool is_maximal_no_gc() const shenandoah_not_implemented_return(false);
 550 
 551   bool is_in(const void* p) const;
 552 
 553   MemRegion reserved_region() const { return _reserved; }
 554   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
 555 
 556   void collect(GCCause::Cause cause);
 557   void do_full_collection(bool clear_all_soft_refs);
 558 
 559   // Used for parsing heap during error printing
 560   HeapWord* block_start(const void* addr) const;
 561   bool block_is_obj(const HeapWord* addr) const;
 562   bool print_location(outputStream* st, void* addr) const;
 563 
 564   // Used for native heap walkers: heap dumpers, mostly
 565   void object_iterate(ObjectClosure* cl);
 566 
 567   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
 568   void keep_alive(oop obj);
 569 
 570   // Used by RMI
 571   jlong millis_since_last_gc();
 572 
 573 // ---------- Safepoint interface hooks
 574 //
 575 public:
 576   void safepoint_synchronize_begin();
 577   void safepoint_synchronize_end();
 578 
 579 // ---------- Code roots handling hooks
 580 //
 581 public:
 582   void register_nmethod(nmethod* nm);
 583   void unregister_nmethod(nmethod* nm);
 584   void flush_nmethod(nmethod* nm);
 585   void verify_nmethod(nmethod* nm) {}
 586 
 587 // ---------- Pinning hooks
 588 //
 589 public:
 590   // Shenandoah supports per-object (per-region) pinning
 591   bool supports_object_pinning() const { return true; }
 592 
 593   oop pin_object(JavaThread* thread, oop obj);
 594   void unpin_object(JavaThread* thread, oop obj);
 595 
 596   void sync_pinned_region_status();
 597   void assert_pinned_region_status() NOT_DEBUG_RETURN;
 598 
 599 // ---------- Allocation support
 600 //
 601 private:
 602   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region);
 603   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
 604   HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
 605   HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
 606   void retire_and_reset_gclabs();
 607 
 608 public:
 609   HeapWord* allocate_memory(ShenandoahAllocRequest& request);
 610   HeapWord* mem_allocate(size_t size, bool* what);
 611   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
 612                                                size_t size,
 613                                                Metaspace::MetadataType mdtype);
 614 
 615   void notify_mutator_alloc_words(size_t words, bool waste);
 616 
 617   // Shenandoah supports TLAB allocation
 618   bool supports_tlab_allocation() const { return true; }
 619 
 620   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
 621   size_t tlab_capacity(Thread *thr) const;
 622   size_t unsafe_max_tlab_alloc(Thread *thread) const;
 623   size_t max_tlab_size() const;
 624   size_t tlab_used(Thread* ignored) const;
 625 
 626   void resize_tlabs();
 627 
 628   void ensure_parsability(bool retire_tlabs);
 629   void make_parsable(bool retire_tlabs);
 630 
 631 // ---------- Marking support
 632 //
 633 private:
 634   ShenandoahMarkingContext* _marking_context;
 635   MemRegion  _bitmap_region;
 636   MemRegion  _aux_bitmap_region;
 637   MarkBitMap _verification_bit_map;
 638   MarkBitMap _aux_bit_map;
 639 
 640   size_t _bitmap_size;
 641   size_t _bitmap_regions_per_slice;
 642   size_t _bitmap_bytes_per_slice;
 643 
 644   bool _bitmap_region_special;
 645   bool _aux_bitmap_region_special;
 646 
 647   // Used for buffering per-region liveness data.
 648   // Needed since ShenandoahHeapRegion uses atomics to update liveness.
 649   //
 650   // The array has max-workers elements, each of which is an array of
 651   // jushort * max_regions. The choice of jushort is not accidental:
 652   // there is a tradeoff between static/dynamic footprint that translates
 653   // into cache pressure (which is already high during marking), and
 654   // too many atomic updates. size_t/jint is too large, jbyte is too small.
 655   jushort** _liveness_cache;
 656 
 657 public:
 658   inline ShenandoahMarkingContext* complete_marking_context() const;
 659   inline ShenandoahMarkingContext* marking_context() const;
 660   inline void mark_complete_marking_context();
 661   inline void mark_incomplete_marking_context();
 662 
 663   template<class T>
 664   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
 665 
 666   template<class T>
 667   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
 668 
 669   template<class T>
 670   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
 671 
 672   void reset_mark_bitmap();
 673 
 674   // SATB barriers hooks
 675   template<bool RESOLVE>
 676   inline bool requires_marking(const void* entry) const;
 677   void force_satb_flush_all_threads();
 678 
 679   // Support for bitmap uncommits
 680   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
 681   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
 682   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
 683 
 684   // Liveness caching support
 685   jushort* get_liveness_cache(uint worker_id);
 686   void flush_liveness_cache(uint worker_id);
 687 
 688 // ---------- Evacuation support
 689 //
 690 private:
 691   ShenandoahCollectionSet* _collection_set;
 692   ShenandoahEvacOOMHandler _oom_evac_handler;
 693 
 694   void evacuate_and_update_roots();
 695 
 696 public:
 697   static address in_cset_fast_test_addr();
 698 
 699   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
 700 
 701   // Checks if object is in the collection set.
 702   inline bool in_collection_set(oop obj) const;
 703 
 704   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
 705   inline bool in_collection_set_loc(void* loc) const;
 706 
 707   // Evacuates object src. Returns the evacuated object, either evacuated
 708   // by this thread, or by some other thread.
 709   inline oop evacuate_object(oop src, Thread* thread);
 710 
 711   // Call before/after evacuation.
 712   void enter_evacuation();
 713   void leave_evacuation();
 714 
 715 // ---------- Helper functions
 716 //
 717 public:
 718   template <class T>
 719   inline oop evac_update_with_forwarded(T* p);
 720 
 721   template <class T>
 722   inline oop maybe_update_with_forwarded(T* p);
 723 
 724   template <class T>
 725   inline oop maybe_update_with_forwarded_not_null(T* p, oop obj);
 726 
 727   template <class T>
 728   inline oop update_with_forwarded_not_null(T* p, oop obj);
 729 
 730   static inline oop cas_oop(oop n, narrowOop* addr, oop c);
 731   static inline oop cas_oop(oop n, oop* addr, oop c);
 732   static inline oop cas_oop(oop n, narrowOop* addr, narrowOop c);
 733 
 734   void trash_humongous_region_at(ShenandoahHeapRegion *r);
 735 
 736   void deduplicate_string(oop str);
 737 
 738 private:
 739   void trash_cset_regions();
 740   void update_heap_references(bool concurrent);
 741 
 742 // ---------- Testing helpers functions
 743 //
 744 private:
 745   ShenandoahSharedFlag _inject_alloc_failure;
 746 
 747   void try_inject_alloc_failure();
 748   bool should_inject_alloc_failure();
 749 };
 750 
 751 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP