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