1 /*
  2  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  4  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
 28 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
 29 
 30 #include "gc/shared/markBitMap.hpp"
 31 #include "gc/shared/softRefPolicy.hpp"
 32 #include "gc/shared/collectedHeap.hpp"
 33 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
 34 #include "gc/shenandoah/shenandoahAsserts.hpp"
 35 #include "gc/shenandoah/shenandoahController.hpp"
 36 #include "gc/shenandoah/shenandoahLock.hpp"
 37 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
 38 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 39 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 40 #include "gc/shenandoah/shenandoahGenerationSizer.hpp"
 41 #include "gc/shenandoah/shenandoahMmuTracker.hpp"
 42 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 43 #include "gc/shenandoah/shenandoahPadding.hpp"
 44 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 45 #include "gc/shenandoah/shenandoahUnload.hpp"
 46 #include "memory/metaspace.hpp"
 47 #include "services/memoryManager.hpp"
 48 #include "utilities/globalDefinitions.hpp"
 49 #include "utilities/stack.hpp"
 50 
 51 class ConcurrentGCTimer;
 52 class ObjectIterateScanRootClosure;
 53 class ShenandoahCollectorPolicy;
 54 class ShenandoahGCSession;
 55 class ShenandoahGCStateResetter;
 56 class ShenandoahGeneration;
 57 class ShenandoahYoungGeneration;
 58 class ShenandoahOldGeneration;
 59 class ShenandoahHeuristics;
 60 class ShenandoahMarkingContext;
 61 class ShenandoahMode;
 62 class ShenandoahPhaseTimings;
 63 class ShenandoahHeap;
 64 class ShenandoahHeapRegion;
 65 class ShenandoahHeapRegionClosure;
 66 class ShenandoahCollectionSet;
 67 class ShenandoahFreeSet;
 68 class ShenandoahConcurrentMark;
 69 class ShenandoahFullGC;
 70 class ShenandoahMonitoringSupport;
 71 class ShenandoahPacer;
 72 class ShenandoahReferenceProcessor;
 73 class ShenandoahVerifier;
 74 class ShenandoahWorkerThreads;
 75 class VMStructs;
 76 
 77 // Used for buffering per-region liveness data.
 78 // Needed since ShenandoahHeapRegion uses atomics to update liveness.
 79 // The ShenandoahHeap array has max-workers elements, each of which is an array of
 80 // uint16_t * max_regions. The choice of uint16_t is not accidental:
 81 // there is a tradeoff between static/dynamic footprint that translates
 82 // into cache pressure (which is already high during marking), and
 83 // too many atomic updates. uint32_t is too large, uint8_t is too small.
 84 typedef uint16_t ShenandoahLiveData;
 85 #define SHENANDOAH_LIVEDATA_MAX ((ShenandoahLiveData)-1)
 86 
 87 class ShenandoahRegionIterator : public StackObj {
 88 private:
 89   ShenandoahHeap* _heap;
 90 
 91   shenandoah_padding(0);
 92   volatile size_t _index;
 93   shenandoah_padding(1);
 94 
 95   // No implicit copying: iterators should be passed by reference to capture the state
 96   NONCOPYABLE(ShenandoahRegionIterator);
 97 
 98 public:
 99   ShenandoahRegionIterator();
100   ShenandoahRegionIterator(ShenandoahHeap* heap);
101 
102   // Reset iterator to default state
103   void reset();
104 
105   // Returns next region, or null if there are no more regions.
106   // This is multi-thread-safe.
107   inline ShenandoahHeapRegion* next();
108 
109   // This is *not* MT safe. However, in the absence of multithreaded access, it
110   // can be used to determine if there is more work to do.
111   bool has_next() const;
112 };
113 
114 class ShenandoahHeapRegionClosure : public StackObj {
115 public:
116   virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
117   virtual bool is_thread_safe() { return false; }
118 };
119 
120 typedef ShenandoahLock    ShenandoahHeapLock;
121 typedef ShenandoahLocker  ShenandoahHeapLocker;
122 typedef Stack<oop, mtGC>  ShenandoahScanObjectStack;
123 
124 class ShenandoahSynchronizePinnedRegionStates : public ShenandoahHeapRegionClosure {
125 private:
126   ShenandoahHeapLock* const _lock;
127 
128 public:
129   ShenandoahSynchronizePinnedRegionStates();
130 
131   void heap_region_do(ShenandoahHeapRegion* r) override;
132   bool is_thread_safe() override { return true; }
133 };
134 
135 // Shenandoah GC is low-pause concurrent GC that uses Brooks forwarding pointers
136 // to encode forwarding data. See BrooksPointer for details on forwarding data encoding.
137 // See ShenandoahControlThread for GC cycle structure.
138 //
139 class ShenandoahHeap : public CollectedHeap {
140   friend class ShenandoahAsserts;
141   friend class VMStructs;
142   friend class ShenandoahGCSession;
143   friend class ShenandoahGCStateResetter;
144   friend class ShenandoahParallelObjectIterator;
145   friend class ShenandoahSafepoint;
146 
147   // Supported GC
148   friend class ShenandoahConcurrentGC;
149   friend class ShenandoahOldGC;
150   friend class ShenandoahDegenGC;
151   friend class ShenandoahFullGC;
152   friend class ShenandoahUnload;
153 
154 // ---------- Locks that guard important data structures in Heap
155 //
156 private:
157   ShenandoahHeapLock _lock;
158 
159   // Indicates the generation whose collection is in
160   // progress. Mutator threads aren't allowed to read
161   // this field.
162   ShenandoahGeneration* _gc_generation;
163 
164   // This is set and cleared by only the VMThread
165   // at each STW pause (safepoint) to the value seen in
166   // _gc_generation. This allows the value to be always consistently
167   // seen by all mutators as well as all GC worker threads.
168   // In that sense, it's a stable snapshot of _gc_generation that is
169   // updated at each STW pause associated with a ShenandoahVMOp.
170   ShenandoahGeneration* _active_generation;
171 
172 public:
173   ShenandoahHeapLock* lock() {
174     return &_lock;
175   }
176 
177   ShenandoahGeneration* gc_generation() const {
178     // We don't want this field read by a mutator thread
179     assert(!Thread::current()->is_Java_thread(), "Not allowed");
180     // value of _gc_generation field, see above
181     return _gc_generation;
182   }
183 
184   ShenandoahGeneration* active_generation() const {
185     // value of _active_generation field, see above
186     return _active_generation;
187   }
188 
189   // Set the _gc_generation field
190   void set_gc_generation(ShenandoahGeneration* generation);
191 
192   // Copy the value in the _gc_generation field into
193   // the _active_generation field: can only be called at
194   // a safepoint by the VMThread.
195   void set_active_generation();
196 
197   ShenandoahHeuristics* heuristics();
198 
199 // ---------- Initialization, termination, identification, printing routines
200 //
201 public:
202   static ShenandoahHeap* heap();
203 
204   const char* name()          const override { return "Shenandoah"; }
205   ShenandoahHeap::Name kind() const override { return CollectedHeap::Shenandoah; }
206 
207   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
208   jint initialize() override;
209   void post_initialize() override;
210   void initialize_mode();
211   virtual void initialize_heuristics();
212   virtual void print_init_logger() const;
213   void initialize_serviceability() override;
214 
215   void print_on(outputStream* st)              const override;
216   void print_extended_on(outputStream *st)     const override;
217   void print_tracing_info()                    const override;
218   void print_heap_regions_on(outputStream* st) const;
219 
220   void stop() override;
221 
222   void prepare_for_verify() override;
223   void verify(VerifyOption vo) override;
224 
225 // WhiteBox testing support.
226   bool supports_concurrent_gc_breakpoints() const override {
227     return true;
228   }
229 
230 // ---------- Heap counters and metrics
231 //
232 private:
233   size_t _initial_size;
234   size_t _minimum_size;
235 
236   volatile size_t _soft_max_size;
237   shenandoah_padding(0);
238   volatile size_t _committed;
239   shenandoah_padding(1);
240 
241   void increase_used(const ShenandoahAllocRequest& req);
242 
243 public:
244   void increase_used(ShenandoahGeneration* generation, size_t bytes);
245   void decrease_used(ShenandoahGeneration* generation, size_t bytes);
246   void increase_humongous_waste(ShenandoahGeneration* generation, size_t bytes);
247   void decrease_humongous_waste(ShenandoahGeneration* generation, size_t bytes);
248 
249   void increase_committed(size_t bytes);
250   void decrease_committed(size_t bytes);
251 
252   void reset_bytes_allocated_since_gc_start();
253 
254   size_t min_capacity()      const;
255   size_t max_capacity()      const override;
256   size_t soft_max_capacity() const;
257   size_t initial_capacity()  const;
258   size_t capacity()          const override;
259   size_t used()              const override;
260   size_t committed()         const;
261 
262   void set_soft_max_capacity(size_t v);
263 
264 // ---------- Periodic Tasks
265 //
266 private:
267   void notify_heap_changed();
268 
269 public:
270   void set_forced_counters_update(bool value);
271   void handle_force_counters_update();
272 
273 // ---------- Workers handling
274 //
275 private:
276   uint _max_workers;
277   ShenandoahWorkerThreads* _workers;
278   ShenandoahWorkerThreads* _safepoint_workers;
279 
280   virtual void initialize_controller();
281 
282 public:
283   uint max_workers();
284   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
285 
286   WorkerThreads* workers() const;
287   WorkerThreads* safepoint_workers() override;
288 
289   void gc_threads_do(ThreadClosure* tcl) const override;
290 
291 // ---------- Heap regions handling machinery
292 //
293 private:
294   MemRegion _heap_region;
295   bool      _heap_region_special;
296   size_t    _num_regions;
297   ShenandoahHeapRegion** _regions;
298   uint8_t* _affiliations;       // Holds array of enum ShenandoahAffiliation, including FREE status in non-generational mode
299 
300 public:
301 
302   inline HeapWord* base() const { return _heap_region.start(); }
303 
304   inline size_t num_regions() const { return _num_regions; }
305   inline bool is_heap_region_special() { return _heap_region_special; }
306 
307   inline ShenandoahHeapRegion* heap_region_containing(const void* addr) const;
308   inline size_t heap_region_index_containing(const void* addr) const;
309 
310   inline ShenandoahHeapRegion* get_region(size_t region_idx) const;
311 
312   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
313   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
314 
315   inline ShenandoahMmuTracker* mmu_tracker() { return &_mmu_tracker; };
316 
317 // ---------- GC state machinery
318 //
319 // GC state describes the important parts of collector state, that may be
320 // used to make barrier selection decisions in the native and generated code.
321 // Multiple bits can be set at once.
322 //
323 // Important invariant: when GC state is zero, the heap is stable, and no barriers
324 // are required.
325 //
326 public:
327   enum GCStateBitPos {
328     // Heap has forwarded objects: needs LRB barriers.
329     HAS_FORWARDED_BITPOS   = 0,
330 
331     // Heap is under marking: needs SATB barriers.
332     // For generational mode, it means either young or old marking, or both.
333     MARKING_BITPOS    = 1,
334 
335     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
336     EVACUATION_BITPOS = 2,
337 
338     // Heap is under updating: needs no additional barriers.
339     UPDATEREFS_BITPOS = 3,
340 
341     // Heap is under weak-reference/roots processing: needs weak-LRB barriers.
342     WEAK_ROOTS_BITPOS  = 4,
343 
344     // Young regions are under marking, need SATB barriers.
345     YOUNG_MARKING_BITPOS = 5,
346 
347     // Old regions are under marking, need SATB barriers.
348     OLD_MARKING_BITPOS = 6
349   };
350 
351   enum GCState {
352     STABLE        = 0,
353     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
354     MARKING       = 1 << MARKING_BITPOS,
355     EVACUATION    = 1 << EVACUATION_BITPOS,
356     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
357     WEAK_ROOTS    = 1 << WEAK_ROOTS_BITPOS,
358     YOUNG_MARKING = 1 << YOUNG_MARKING_BITPOS,
359     OLD_MARKING   = 1 << OLD_MARKING_BITPOS
360   };
361 
362 private:
363   bool _gc_state_changed;
364   ShenandoahSharedBitmap _gc_state;
365   ShenandoahSharedFlag   _heap_changed;
366   ShenandoahSharedFlag   _degenerated_gc_in_progress;
367   ShenandoahSharedFlag   _full_gc_in_progress;
368   ShenandoahSharedFlag   _full_gc_move_in_progress;
369   ShenandoahSharedFlag   _concurrent_strong_root_in_progress;
370 
371   size_t _gc_no_progress_count;
372 
373   // This updates the singlular, global gc state. This must happen on a safepoint.
374   void set_gc_state(uint mask, bool value);
375 
376 public:
377   char gc_state() const;
378 
379   // This copies the global gc state into a thread local variable for java threads.
380   // It is primarily intended to support quick access at barriers.
381   void propagate_gc_state_to_java_threads();
382 
383   // This is public to support assertions that the state hasn't been changed off of
384   // a safepoint and that any changes were propagated to java threads after the safepoint.
385   bool has_gc_state_changed() const { return _gc_state_changed; }
386 
387   // Returns true if allocations have occurred in new regions or if regions have been
388   // uncommitted since the previous calls. This call will reset the flag to false.
389   bool has_changed() {
390     return _heap_changed.try_unset();
391   }
392 
393   void set_concurrent_young_mark_in_progress(bool in_progress);
394   void set_concurrent_old_mark_in_progress(bool in_progress);
395   void set_evacuation_in_progress(bool in_progress);
396   void set_update_refs_in_progress(bool in_progress);
397   void set_degenerated_gc_in_progress(bool in_progress);
398   void set_full_gc_in_progress(bool in_progress);
399   void set_full_gc_move_in_progress(bool in_progress);
400   void set_has_forwarded_objects(bool cond);
401   void set_concurrent_strong_root_in_progress(bool cond);
402   void set_concurrent_weak_root_in_progress(bool cond);
403 
404   inline bool is_stable() const;
405   inline bool is_idle() const;
406 
407   inline bool is_concurrent_mark_in_progress() const;
408   inline bool is_concurrent_young_mark_in_progress() const;
409   inline bool is_concurrent_old_mark_in_progress() const;
410   inline bool is_update_refs_in_progress() const;
411   inline bool is_evacuation_in_progress() const;
412   inline bool is_degenerated_gc_in_progress() const;
413   inline bool is_full_gc_in_progress() const;
414   inline bool is_full_gc_move_in_progress() const;
415   inline bool has_forwarded_objects() const;
416 
417   inline bool is_stw_gc_in_progress() const;
418   inline bool is_concurrent_strong_root_in_progress() const;
419   inline bool is_concurrent_weak_root_in_progress() const;
420   bool is_prepare_for_old_mark_in_progress() const;
421 
422 private:
423   void manage_satb_barrier(bool active);
424 
425   enum CancelState {
426     // Normal state. GC has not been cancelled and is open for cancellation.
427     // Worker threads can suspend for safepoint.
428     CANCELLABLE,
429 
430     // GC has been cancelled. Worker threads can not suspend for
431     // safepoint but must finish their work as soon as possible.
432     CANCELLED
433   };
434 
435   double _cancel_requested_time;
436   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;
437 
438   // Returns true if cancel request was successfully communicated.
439   // Returns false if some other thread already communicated cancel
440   // request.  A true return value does not mean GC has been
441   // cancelled, only that the process of cancelling GC has begun.
442   bool try_cancel_gc();
443 
444 public:
445   inline bool cancelled_gc() const;
446   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
447 
448   inline void clear_cancelled_gc(bool clear_oom_handler = true);
449 
450   void cancel_concurrent_mark();
451   void cancel_gc(GCCause::Cause cause);
452 
453 public:
454   // These will uncommit empty regions if heap::committed > shrink_until
455   // and there exists at least one region which was made empty before shrink_before.
456   void maybe_uncommit(double shrink_before, size_t shrink_until);
457   void op_uncommit(double shrink_before, size_t shrink_until);
458 
459   // Returns true if the soft maximum heap has been changed using management APIs.
460   bool check_soft_max_changed();
461 
462 protected:
463   // This is shared between shConcurrentGC and shDegenerateGC so that degenerated
464   // GC can resume update refs from where the concurrent GC was cancelled. It is
465   // also used in shGenerationalHeap, which uses a different closure for update refs.
466   ShenandoahRegionIterator _update_refs_iterator;
467 
468 private:
469   // GC support
470   // Evacuation
471   virtual void evacuate_collection_set(bool concurrent);
472   // Concurrent root processing
473   void prepare_concurrent_roots();
474   void finish_concurrent_roots();
475   // Concurrent class unloading support
476   void do_class_unloading();
477   // Reference updating
478   void prepare_update_heap_references(bool concurrent);
479   virtual void update_heap_references(bool concurrent);
480   // Final update region states
481   void update_heap_region_states(bool concurrent);
482   virtual void final_update_refs_update_region_states();
483 
484   void rendezvous_threads(const char* name);
485   void recycle_trash();
486 public:
487   void rebuild_free_set(bool concurrent);
488   void notify_gc_progress();
489   void notify_gc_no_progress();
490   size_t get_gc_no_progress_count() const;
491 
492 //
493 // Mark support
494 private:
495   ShenandoahGeneration*  _global_generation;
496 
497 protected:
498   ShenandoahController*  _control_thread;
499 
500   ShenandoahYoungGeneration* _young_generation;
501   ShenandoahOldGeneration*   _old_generation;
502 
503 private:
504   ShenandoahCollectorPolicy* _shenandoah_policy;
505   ShenandoahMode*            _gc_mode;
506   ShenandoahFreeSet*         _free_set;
507   ShenandoahPacer*           _pacer;
508   ShenandoahVerifier*        _verifier;
509 
510   ShenandoahPhaseTimings*       _phase_timings;
511   ShenandoahMmuTracker          _mmu_tracker;
512 
513 public:
514   ShenandoahController*   control_thread() { return _control_thread; }
515 
516   ShenandoahGeneration*      global_generation() const { return _global_generation; }
517   ShenandoahYoungGeneration* young_generation()  const {
518     assert(mode()->is_generational(), "Young generation requires generational mode");
519     return _young_generation;
520   }
521 
522   ShenandoahOldGeneration*   old_generation()    const {
523     assert(mode()->is_generational(), "Old generation requires generational mode");
524     return _old_generation;
525   }
526 
527   ShenandoahGeneration*      generation_for(ShenandoahAffiliation affiliation) const;
528 
529   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
530   ShenandoahMode*            mode()              const { return _gc_mode;           }
531   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
532   ShenandoahPacer*           pacer()             const { return _pacer;             }
533 
534   ShenandoahPhaseTimings*    phase_timings()     const { return _phase_timings;     }
535 
536   ShenandoahEvacOOMHandler*  oom_evac_handler()        { return &_oom_evac_handler; }
537 
538   void on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation);
539   void on_cycle_end(ShenandoahGeneration* generation);
540 
541   ShenandoahVerifier*        verifier();
542 
543 // ---------- VM subsystem bindings
544 //
545 private:
546   ShenandoahMonitoringSupport* _monitoring_support;
547   MemoryPool*                  _memory_pool;
548   GCMemoryManager              _stw_memory_manager;
549   GCMemoryManager              _cycle_memory_manager;
550   ConcurrentGCTimer*           _gc_timer;
551   // For exporting to SA
552   int                          _log_min_obj_alignment_in_bytes;
553 public:
554   ShenandoahMonitoringSupport* monitoring_support() const    { return _monitoring_support;    }
555   GCMemoryManager* cycle_memory_manager()                    { return &_cycle_memory_manager; }
556   GCMemoryManager* stw_memory_manager()                      { return &_stw_memory_manager;   }
557 
558   GrowableArray<GCMemoryManager*> memory_managers() override;
559   GrowableArray<MemoryPool*> memory_pools() override;
560   MemoryUsage memory_usage() override;
561   GCTracer* tracer();
562   ConcurrentGCTimer* gc_timer() const;
563 
564 // ---------- Class Unloading
565 //
566 private:
567   ShenandoahSharedFlag _unload_classes;
568   ShenandoahUnload     _unloader;
569 
570 public:
571   void set_unload_classes(bool uc);
572   bool unload_classes() const;
573 
574   // Perform STW class unloading and weak root cleaning
575   void parallel_cleaning(bool full_gc);
576 
577 private:
578   void stw_unload_classes(bool full_gc);
579   void stw_process_weak_roots(bool full_gc);
580   void stw_weak_refs(bool full_gc);
581 
582   inline void assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
583                                           ShenandoahAffiliation new_affiliation);
584 
585   // Heap iteration support
586   void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
587   bool prepare_aux_bitmap_for_iteration();
588   void reclaim_aux_bitmap_for_iteration();
589 
590 // ---------- Generic interface hooks
591 // Minor things that super-interface expects us to implement to play nice with
592 // the rest of runtime. Some of the things here are not required to be implemented,
593 // and can be stubbed out.
594 //
595 public:
596   bool is_maximal_no_gc() const override shenandoah_not_implemented_return(false);
597 
598   // Check the pointer is in active part of Java heap.
599   // Use is_in_reserved to check if object is within heap bounds.
600   bool is_in(const void* p) const override;
601 
602   // Returns true if the given oop belongs to a generation that is actively being collected.
603   inline bool is_in_active_generation(oop obj) const;
604   inline bool is_in_young(const void* p) const;
605   inline bool is_in_old(const void* p) const;
606 
607   // Returns true iff the young generation is being collected and the given pointer
608   // is in the old generation. This is used to prevent the young collection from treating
609   // such an object as unreachable.
610   inline bool is_in_old_during_young_collection(oop obj) const;
611 
612   inline ShenandoahAffiliation region_affiliation(const ShenandoahHeapRegion* r);
613   inline void set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation);
614 
615   inline ShenandoahAffiliation region_affiliation(size_t index);
616 
617   bool requires_barriers(stackChunkOop obj) const override;
618 
619   MemRegion reserved_region() const { return _reserved; }
620   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
621 
622   void collect(GCCause::Cause cause) override;
623   void do_full_collection(bool clear_all_soft_refs) override;
624 
625   // Used for parsing heap during error printing
626   HeapWord* block_start(const void* addr) const;
627   bool block_is_obj(const HeapWord* addr) const;
628   bool print_location(outputStream* st, void* addr) const override;
629 
630   // Used for native heap walkers: heap dumpers, mostly
631   void object_iterate(ObjectClosure* cl) override;
632   // Parallel heap iteration support
633   ParallelObjectIteratorImpl* parallel_object_iterator(uint workers) override;
634 
635   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
636   void keep_alive(oop obj) override;
637 
638 // ---------- Safepoint interface hooks
639 //
640 public:
641   void safepoint_synchronize_begin() override;
642   void safepoint_synchronize_end() override;
643 
644 // ---------- Code roots handling hooks
645 //
646 public:
647   void register_nmethod(nmethod* nm) override;
648   void unregister_nmethod(nmethod* nm) override;
649   void verify_nmethod(nmethod* nm) override {}
650 
651 // ---------- Pinning hooks
652 //
653 public:
654   // Shenandoah supports per-object (per-region) pinning
655   void pin_object(JavaThread* thread, oop obj) override;
656   void unpin_object(JavaThread* thread, oop obj) override;
657 
658   void sync_pinned_region_status();
659   void assert_pinned_region_status() NOT_DEBUG_RETURN;
660 
661 // ---------- CDS archive support
662 
663   bool can_load_archived_objects() const override { return !ShenandoahCardBarrier; }
664   HeapWord* allocate_loaded_archive_space(size_t size) override;
665   void complete_loaded_archive_space(MemRegion archive_space) override;
666 
667 // ---------- Allocation support
668 //
669 protected:
670   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
671 
672 private:
673   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region);
674   HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
675   HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
676 
677 public:
678   HeapWord* allocate_memory(ShenandoahAllocRequest& request);
679   HeapWord* mem_allocate(size_t size, bool* what) override;
680   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
681                                                size_t size,
682                                                Metaspace::MetadataType mdtype) override;
683 
684   void notify_mutator_alloc_words(size_t words, size_t waste);
685 
686   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override;
687   size_t tlab_capacity(Thread *thr) const override;
688   size_t unsafe_max_tlab_alloc(Thread *thread) const override;
689   size_t max_tlab_size() const override;
690   size_t tlab_used(Thread* ignored) const override;
691 
692   void ensure_parsability(bool retire_labs) override;
693 
694   void labs_make_parsable();
695   void tlabs_retire(bool resize);
696   void gclabs_retire(bool resize);
697 
698 // ---------- Marking support
699 //
700 private:
701   ShenandoahMarkingContext* _marking_context;
702   MemRegion  _bitmap_region;
703   MemRegion  _aux_bitmap_region;
704   MarkBitMap _verification_bit_map;
705   MarkBitMap _aux_bit_map;
706 
707   size_t _bitmap_size;
708   size_t _bitmap_regions_per_slice;
709   size_t _bitmap_bytes_per_slice;
710 
711   size_t _pretouch_heap_page_size;
712   size_t _pretouch_bitmap_page_size;
713 
714   bool _bitmap_region_special;
715   bool _aux_bitmap_region_special;
716 
717   ShenandoahLiveData** _liveness_cache;
718 
719 public:
720   inline ShenandoahMarkingContext* complete_marking_context() const;
721   inline ShenandoahMarkingContext* marking_context() const;
722 
723   template<class T>
724   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
725 
726   template<class T>
727   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
728 
729   template<class T>
730   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
731 
732   // SATB barriers hooks
733   inline bool requires_marking(const void* entry) const;
734 
735   // Support for bitmap uncommits
736   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
737   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
738   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
739 
740   // Liveness caching support
741   ShenandoahLiveData* get_liveness_cache(uint worker_id);
742   void flush_liveness_cache(uint worker_id);
743 
744   size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
745 
746 // ---------- Evacuation support
747 //
748 private:
749   ShenandoahCollectionSet* _collection_set;
750   ShenandoahEvacOOMHandler _oom_evac_handler;
751 
752   oop try_evacuate_object(oop src, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
753 public:
754 
755   static address in_cset_fast_test_addr();
756 
757   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
758 
759   // Checks if object is in the collection set.
760   inline bool in_collection_set(oop obj) const;
761 
762   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
763   inline bool in_collection_set_loc(void* loc) const;
764 
765   // Evacuates or promotes object src. Returns the evacuated object, either evacuated
766   // by this thread, or by some other thread.
767   virtual oop evacuate_object(oop src, Thread* thread);
768 
769   // Call before/after evacuation.
770   inline void enter_evacuation(Thread* t);
771   inline void leave_evacuation(Thread* t);
772 
773 // ---------- Helper functions
774 //
775 public:
776   template <class T>
777   inline void conc_update_with_forwarded(T* p);
778 
779   template <class T>
780   inline void non_conc_update_with_forwarded(T* p);
781 
782   static inline void atomic_update_oop(oop update,       oop* addr,       oop compare);
783   static inline void atomic_update_oop(oop update, narrowOop* addr,       oop compare);
784   static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
785 
786   static inline bool atomic_update_oop_check(oop update,       oop* addr,       oop compare);
787   static inline bool atomic_update_oop_check(oop update, narrowOop* addr,       oop compare);
788   static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
789 
790   static inline void atomic_clear_oop(      oop* addr,       oop compare);
791   static inline void atomic_clear_oop(narrowOop* addr,       oop compare);
792   static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
793 
794   size_t trash_humongous_region_at(ShenandoahHeapRegion *r);
795 
796   static inline void increase_object_age(oop obj, uint additional_age);
797 
798   // Return the object's age, or a sentinel value when the age can't
799   // necessarily be determined because of concurrent locking by the
800   // mutator
801   static inline uint get_object_age(oop obj);
802 
803   void log_heap_status(const char *msg) const;
804 
805 private:
806   void trash_cset_regions();
807 
808 // ---------- Testing helpers functions
809 //
810 private:
811   ShenandoahSharedFlag _inject_alloc_failure;
812 
813   void try_inject_alloc_failure();
814   bool should_inject_alloc_failure();
815 };
816 
817 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP