1 /*
  2  * Copyright (c) 2013, 2021, 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/mode/shenandoahGenerationalMode.hpp"
 32 #include "gc/shenandoah/shenandoahAsserts.hpp"
 33 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
 34 #include "gc/shenandoah/shenandoahLock.hpp"
 35 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
 36 #include "gc/shenandoah/shenandoahPadding.hpp"
 37 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 38 #include "gc/shenandoah/shenandoahUnload.hpp"
 39 #include "gc/shenandoah/shenandoahScanRemembered.hpp"
 40 #include "memory/metaspace.hpp"
 41 #include "services/memoryManager.hpp"
 42 #include "utilities/globalDefinitions.hpp"
 43 #include "utilities/stack.hpp"
 44 
 45 class ConcurrentGCTimer;
 46 class ObjectIterateScanRootClosure;
 47 class PLAB;
 48 class ShenandoahCollectorPolicy;
 49 class ShenandoahControlThread;
 50 class ShenandoahRegulatorThread;
 51 class ShenandoahGCSession;
 52 class ShenandoahGCStateResetter;
 53 class ShenandoahGeneration;
 54 class ShenandoahYoungGeneration;
 55 class ShenandoahHeuristics;
 56 class ShenandoahOldHeuristics;
 57 class ShenandoahMarkingContext;
 58 class ShenandoahPhaseTimings;
 59 class ShenandoahHeap;
 60 class ShenandoahHeapRegion;
 61 class ShenandoahHeapRegionClosure;
 62 class ShenandoahCollectionSet;
 63 class ShenandoahFreeSet;
 64 class ShenandoahConcurrentMark;
 65 class ShenandoahFullGC;
 66 class ShenandoahMonitoringSupport;
 67 class ShenandoahPacer;
 68 class ShenandoahReferenceProcessor;
 69 class ShenandoahVerifier;
 70 class ShenandoahWorkerThreads;
 71 class VMStructs;
 72 
 73 // Used for buffering per-region liveness data.
 74 // Needed since ShenandoahHeapRegion uses atomics to update liveness.
 75 // The ShenandoahHeap array has max-workers elements, each of which is an array of
 76 // uint16_t * max_regions. The choice of uint16_t is not accidental:
 77 // there is a tradeoff between static/dynamic footprint that translates
 78 // into cache pressure (which is already high during marking), and
 79 // too many atomic updates. uint32_t is too large, uint8_t is too small.
 80 typedef uint16_t ShenandoahLiveData;
 81 #define SHENANDOAH_LIVEDATA_MAX ((ShenandoahLiveData)-1)
 82 
 83 class ShenandoahRegionIterator : public StackObj {
 84 private:
 85   ShenandoahHeap* _heap;
 86 
 87   shenandoah_padding(0);
 88   volatile size_t _index;
 89   shenandoah_padding(1);
 90 
 91   // No implicit copying: iterators should be passed by reference to capture the state
 92   NONCOPYABLE(ShenandoahRegionIterator);
 93 
 94 public:
 95   ShenandoahRegionIterator();
 96   ShenandoahRegionIterator(ShenandoahHeap* heap);
 97 
 98   // Reset iterator to default state
 99   void reset();
100 
101   // Returns next region, or NULL if there are no more regions.
102   // This is multi-thread-safe.
103   inline ShenandoahHeapRegion* next();
104 
105   // This is *not* MT safe. However, in the absence of multithreaded access, it
106   // can be used to determine if there is more work to do.
107   bool has_next() const;
108 };
109 
110 class ShenandoahHeapRegionClosure : public StackObj {
111 public:
112   virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
113   virtual bool is_thread_safe() { return false; }
114 };
115 
116 template<GenerationMode GENERATION>
117 class ShenandoahGenerationRegionClosure : public ShenandoahHeapRegionClosure {
118  public:
119   explicit ShenandoahGenerationRegionClosure(ShenandoahHeapRegionClosure* cl) : _cl(cl) {}
120   void heap_region_do(ShenandoahHeapRegion* r);
121   virtual bool is_thread_safe() { return _cl->is_thread_safe(); }
122  private:
123   ShenandoahHeapRegionClosure* _cl;
124 };
125 
126 typedef ShenandoahLock    ShenandoahHeapLock;
127 typedef ShenandoahLocker  ShenandoahHeapLocker;
128 typedef Stack<oop, mtGC>  ShenandoahScanObjectStack;
129 
130 // Shenandoah GC is low-pause concurrent GC that uses Brooks forwarding pointers
131 // to encode forwarding data. See BrooksPointer for details on forwarding data encoding.
132 // See ShenandoahControlThread for GC cycle structure.
133 //
134 class ShenandoahHeap : public CollectedHeap {
135   friend class ShenandoahAsserts;
136   friend class VMStructs;
137   friend class ShenandoahGCSession;
138   friend class ShenandoahGCStateResetter;
139   friend class ShenandoahParallelObjectIterator;
140   friend class ShenandoahSafepoint;
141   // Supported GC
142   friend class ShenandoahConcurrentGC;
143   friend class ShenandoahOldGC;
144   friend class ShenandoahDegenGC;
145   friend class ShenandoahFullGC;
146   friend class ShenandoahUnload;
147 
148 // ---------- Locks that guard important data structures in Heap
149 //
150 private:
151   ShenandoahHeapLock _lock;
152   ShenandoahGeneration* _gc_generation;
153 
154   bool _prep_for_mixed_evac_in_progress; // true iff we are concurrently coalescing and filling old-gen HeapRegions
155 
156 public:
157   ShenandoahHeapLock* lock() {
158     return &_lock;
159   }
160 
161   ShenandoahGeneration* active_generation() const {
162     // last or latest generation might be a better name here.
163     return _gc_generation;
164   }
165 
166   void set_gc_generation(ShenandoahGeneration* generation) {
167     _gc_generation = generation;
168   }
169 
170   ShenandoahOldHeuristics* old_heuristics();
171 
172   bool doing_mixed_evacuations();
173 
174   bool is_gc_generation_young() const;
175 
176 // ---------- Initialization, termination, identification, printing routines
177 //
178 public:
179   static ShenandoahHeap* heap();
180 
181   const char* name()          const { return "Shenandoah"; }
182   ShenandoahHeap::Name kind() const { return CollectedHeap::Shenandoah; }
183 
184   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
185   jint initialize();
186   void post_initialize();
187   void initialize_mode();
188   void initialize_heuristics();
189   void initialize_generations();
190 
191   void initialize_serviceability();
192 
193   void print_on(outputStream* st)              const;
194   void print_extended_on(outputStream *st)     const;
195   void print_tracing_info()                    const;
196   void print_heap_regions_on(outputStream* st) const;
197 
198   void stop();
199 
200   void prepare_for_verify();
201   void verify(VerifyOption vo);
202   void verify_rem_set_at_mark();
203   void verify_rem_set_at_update_ref();
204   void verify_rem_set_after_full_gc();
205 
206 // WhiteBox testing support.
207   bool supports_concurrent_gc_breakpoints() const {
208     return true;
209   }
210 
211 // ---------- Heap counters and metrics
212 //
213 private:
214            size_t _initial_size;
215            size_t _minimum_size;
216   volatile size_t _soft_max_size;
217   shenandoah_padding(0);
218   volatile size_t _used;
219   volatile size_t _committed;
220   shenandoah_padding(1);
221 
222   static size_t young_generation_capacity(size_t total_capacity);
223   void help_verify_region_rem_set(ShenandoahHeapRegion* r, ShenandoahMarkingContext* ctx,
224                                   HeapWord* from, HeapWord* top, HeapWord* update_watermark, const char* message);
225 
226 public:
227   void increase_used(size_t bytes);
228   void decrease_used(size_t bytes);
229   void set_used(size_t bytes);
230 
231   void increase_committed(size_t bytes);
232   void decrease_committed(size_t bytes);
233 
234   void reset_bytes_allocated_since_gc_start();
235 
236   size_t min_capacity()      const;
237   size_t max_capacity()      const;
238   size_t soft_max_capacity() const;
239   size_t initial_capacity()  const;
240   size_t capacity()          const;
241   size_t used()              const;
242   size_t committed()         const;
243 
244   void set_soft_max_capacity(size_t v);
245 
246 // ---------- Workers handling
247 //
248 private:
249   uint _max_workers;
250   ShenandoahWorkerThreads* _workers;
251   ShenandoahWorkerThreads* _safepoint_workers;
252 
253 public:
254   uint max_workers();
255   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
256 
257   WorkerThreads* workers() const;
258   WorkerThreads* safepoint_workers();
259 
260   void gc_threads_do(ThreadClosure* tcl) const;
261 
262 // ---------- Heap regions handling machinery
263 //
264 private:
265   MemRegion _heap_region;
266   bool      _heap_region_special;
267   size_t    _num_regions;
268   ShenandoahHeapRegion** _regions;
269   ShenandoahRegionIterator _update_refs_iterator;
270 
271 public:
272 
273   inline HeapWord* base() const { return _heap_region.start(); }
274 
275   inline size_t num_regions() const { return _num_regions; }
276   inline bool is_heap_region_special() { return _heap_region_special; }
277 
278   inline ShenandoahHeapRegion* const heap_region_containing(const void* addr) const;
279   inline size_t heap_region_index_containing(const void* addr) const;
280 
281   inline ShenandoahHeapRegion* const get_region(size_t region_idx) const;
282 
283   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
284   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
285 
286 // ---------- GC state machinery
287 //
288 // GC state describes the important parts of collector state, that may be
289 // used to make barrier selection decisions in the native and generated code.
290 // Multiple bits can be set at once.
291 //
292 // Important invariant: when GC state is zero, the heap is stable, and no barriers
293 // are required.
294 //
295 public:
296   enum GCStateBitPos {
297     // Heap has forwarded objects: needs LRB barriers.
298     HAS_FORWARDED_BITPOS   = 0,
299 
300     // Young regions are under marking: needs SATB barriers.
301     YOUNG_MARKING_BITPOS    = 1,
302 
303     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
304     EVACUATION_BITPOS = 2,
305 
306     // Heap is under updating: needs no additional barriers.
307     UPDATEREFS_BITPOS = 3,
308 
309     // Heap is under weak-reference/roots processing: needs weak-LRB barriers.
310     WEAK_ROOTS_BITPOS  = 4,
311 
312     // Old regions are under marking, still need SATB barriers.
313     OLD_MARKING_BITPOS = 5
314   };
315 
316   enum GCState {
317     STABLE        = 0,
318     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
319     YOUNG_MARKING = 1 << YOUNG_MARKING_BITPOS,
320     EVACUATION    = 1 << EVACUATION_BITPOS,
321     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
322     WEAK_ROOTS    = 1 << WEAK_ROOTS_BITPOS,
323     OLD_MARKING   = 1 << OLD_MARKING_BITPOS
324   };
325 
326 private:
327   ShenandoahSharedBitmap _gc_state;
328   ShenandoahSharedFlag   _degenerated_gc_in_progress;
329   ShenandoahSharedFlag   _full_gc_in_progress;
330   ShenandoahSharedFlag   _full_gc_move_in_progress;
331   ShenandoahSharedFlag   _progress_last_gc;
332   ShenandoahSharedFlag   _concurrent_strong_root_in_progress;
333 
334   // _alloc_supplement_reserve is a supplemental budget for new_memory allocations.  During evacuation and update-references,
335   // mutator allocation requests are "authorized" iff young_gen->available() plus _alloc_supplement_reserve minus
336   // _young_evac_reserve is greater than request size.  The values of _alloc_supplement_reserve and _young_evac_reserve
337   // are zero except during evacuation and update-reference phases of GC.  Both of these values are established at
338   // the start of evacuation, and they remain constant throughout the duration of these two phases of GC.  Since these
339   // two values are constant throughout each GC phases, we introduce a new service into ShenandoahGeneration.  This service
340   // provides adjusted_available() based on an adjusted capacity.  At the start of evacuation, we adjust young capacity by
341   // adding the amount to be borrowed from old-gen and subtracting the _young_evac_reserve, we adjust old capacity by
342   // subtracting the amount to be loaned to young-gen.
343   //
344   // We always use adjusted capacities to determine permission to allocate within young and to promote into old.  Note
345   // that adjusted capacities equal traditional capacities except during evacuation and update refs.
346   //
347   // During evacuation, we assure that _old_evac_expended does not exceed _old_evac_reserve.
348   //
349   // At the end of update references, we perform the following bookkeeping activities:
350   //
351   // 1. Unadjust the capacity within young-gen and old-gen to undo the effects of borrowing memory from old-gen.  Note that
352   //    the entirety of the collection set is now available, so allocation capacity naturally increase at this time.
353   // 2. Clear (reset to zero) _alloc_supplement_reserve, _young_evac_reserve, _old_evac_reserve, and _promotion_reserve
354   //
355   // _young_evac_reserve and _old_evac_reserve are only non-zero during evacuation and update-references.
356   //
357   // Allocation of old GCLABs assures that _old_evac_expended + request-size < _old_evac_reserved.  If the allocation
358   //  is authorized, increment _old_evac_expended by request size.  This allocation ignores old_gen->available().
359   //
360   // Note that the typical total expenditure on evacuation is less than the associated evacuation reserve because we generally
361   // reserve ShenandoahEvacWaste (> 1.0) times the anticipated evacuation need.  In the case that there is an excessive amount
362   // of waste, it may be that one thread fails to grab a new GCLAB, this does not necessarily doom the associated evacuation
363   // effort.  If this happens, the requesting thread blocks until some other thread manages to evacuate the offending object.
364   // Only after "all" threads fail to evacuate an object do we consider the evacuation effort to have failed.
365 
366   intptr_t _alloc_supplement_reserve;  // Bytes reserved for young allocations during evac and update refs
367   size_t _promotion_reserve;           // Bytes reserved within old-gen to hold the results of promotion
368 
369 
370   size_t _old_evac_reserve;            // Bytes reserved within old-gen to hold evacuated objects from old-gen collection set
371   size_t _old_evac_expended;           // Bytes of old-gen memory expended on old-gen evacuations
372 
373   size_t _young_evac_reserve;          // Bytes reserved within young-gen to hold evacuated objects from young-gen collection set
374 
375   size_t _captured_old_usage;          // What was old usage (bytes) when last captured?
376 
377   size_t _previous_promotion;          // Bytes promoted during previous evacuation
378 
379   bool _upgraded_to_full;
380 
381   void set_gc_state_all_threads(char state);
382   void set_gc_state_mask(uint mask, bool value);
383 
384 
385 
386 public:
387   char gc_state() const;
388   static address gc_state_addr();
389 
390   void set_concurrent_young_mark_in_progress(bool in_progress);
391   void set_concurrent_old_mark_in_progress(bool in_progress);
392   void set_evacuation_in_progress(bool in_progress);
393   void set_update_refs_in_progress(bool in_progress);
394   void set_degenerated_gc_in_progress(bool in_progress);
395   void set_full_gc_in_progress(bool in_progress);
396   void set_full_gc_move_in_progress(bool in_progress);
397   void set_has_forwarded_objects(bool cond);
398   void set_concurrent_strong_root_in_progress(bool cond);
399   void set_concurrent_weak_root_in_progress(bool cond);
400   void set_concurrent_prep_for_mixed_evacuation_in_progress(bool cond);
401   void set_aging_cycle(bool cond);
402 
403   inline bool is_stable() const;
404   inline bool is_idle() const;
405   inline bool is_concurrent_mark_in_progress() const;
406   inline bool is_concurrent_young_mark_in_progress() const;
407   inline bool is_concurrent_old_mark_in_progress() const;
408   inline bool is_update_refs_in_progress() const;
409   inline bool is_evacuation_in_progress() const;
410   inline bool is_degenerated_gc_in_progress() const;
411   inline bool is_full_gc_in_progress() const;
412   inline bool is_full_gc_move_in_progress() const;
413   inline bool has_forwarded_objects() const;
414   inline bool is_gc_in_progress_mask(uint mask) const;
415   inline bool is_stw_gc_in_progress() const;
416   inline bool is_concurrent_strong_root_in_progress() const;
417   inline bool is_concurrent_weak_root_in_progress() const;
418   bool is_concurrent_prep_for_mixed_evacuation_in_progress();
419   inline bool is_aging_cycle() const;
420   inline bool upgraded_to_full() { return _upgraded_to_full; }
421   inline void start_conc_gc() { _upgraded_to_full = false; }
422   inline void record_upgrade_to_full() { _upgraded_to_full = true; }
423 
424   inline size_t capture_old_usage(size_t usage);
425   inline void set_previous_promotion(size_t promoted_bytes);
426   inline size_t get_previous_promotion() const;
427 
428   // Returns previous value
429   inline size_t set_promotion_reserve(size_t new_val);
430   inline size_t get_promotion_reserve() const;
431 
432   // Returns previous value
433   inline size_t set_old_evac_reserve(size_t new_val);
434   inline size_t get_old_evac_reserve() const;
435 
436   inline void reset_old_evac_expended();
437   inline size_t expend_old_evac(size_t increment);
438   inline size_t get_old_evac_expended() const;
439 
440   // Returns previous value
441   inline size_t set_young_evac_reserve(size_t new_val);
442   inline size_t get_young_evac_reserve() const;
443 
444   // Returns previous value.  This is a signed value because it is the amount borrowed minus the amount reserved for
445   // young-gen evacuation.  In case we cannot borrow much, this value might be negative.
446   inline intptr_t set_alloc_supplement_reserve(intptr_t new_val);
447   inline intptr_t get_alloc_supplement_reserve() const;
448 
449 private:
450   void manage_satb_barrier(bool active);
451 
452   enum CancelState {
453     // Normal state. GC has not been cancelled and is open for cancellation.
454     // Worker threads can suspend for safepoint.
455     CANCELLABLE,
456 
457     // GC has been cancelled. Worker threads can not suspend for
458     // safepoint but must finish their work as soon as possible.
459     CANCELLED,
460 
461     // GC has not been cancelled and must not be cancelled. At least
462     // one worker thread checks for pending safepoint and may suspend
463     // if a safepoint is pending.
464     NOT_CANCELLED
465   };
466 
467   double _cancel_requested_time;
468   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;
469 
470   // Returns true if cancel request was successfully communicated.
471   // Returns false if some other thread already communicated cancel
472   // request.  A true return value does not mean GC has been
473   // cancelled, only that the process of cancelling GC has begun.
474   bool try_cancel_gc();
475 
476 public:
477   inline bool cancelled_gc() const;
478   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
479 
480   inline void clear_cancelled_gc(bool clear_oom_handler = true);
481 
482   void cancel_concurrent_mark();
483   void cancel_gc(GCCause::Cause cause);
484 
485 public:
486   // Elastic heap support
487   void entry_uncommit(double shrink_before, size_t shrink_until);
488   void op_uncommit(double shrink_before, size_t shrink_until);
489 
490 private:
491   // GC support
492   // Evacuation
493   void evacuate_collection_set(bool concurrent);
494   // Concurrent root processing
495   void prepare_concurrent_roots();
496   void finish_concurrent_roots();
497   // Concurrent class unloading support
498   void do_class_unloading();
499   // Reference updating
500   void prepare_update_heap_references(bool concurrent);
501   void update_heap_references(bool concurrent);
502   // Final update region states
503   void update_heap_region_states(bool concurrent);
504   void rebuild_free_set(bool concurrent);
505 
506   void rendezvous_threads();
507   void recycle_trash();
508 public:
509   void notify_gc_progress()    { _progress_last_gc.set();   }
510   void notify_gc_no_progress() { _progress_last_gc.unset(); }
511 
512 //
513 // Mark support
514 private:
515   ShenandoahYoungGeneration* _young_generation;
516   ShenandoahGeneration*      _global_generation;
517   ShenandoahGeneration*      _old_generation;
518 
519   ShenandoahControlThread*   _control_thread;
520   ShenandoahRegulatorThread* _regulator_thread;
521   ShenandoahCollectorPolicy* _shenandoah_policy;
522   ShenandoahMode*            _gc_mode;
523   ShenandoahFreeSet*         _free_set;
524   ShenandoahPacer*           _pacer;
525   ShenandoahVerifier*        _verifier;
526 
527   ShenandoahPhaseTimings*    _phase_timings;
528 
529   ShenandoahControlThread*   control_thread()          { return _control_thread;    }
530   ShenandoahRegulatorThread* regulator_thread()        { return _regulator_thread;  }
531 
532 public:
533   ShenandoahYoungGeneration* young_generation()  const { return _young_generation;  }
534   ShenandoahGeneration*      global_generation() const { return _global_generation; }
535   ShenandoahGeneration*      old_generation()    const { return _old_generation;    }
536   ShenandoahGeneration*      generation_for(ShenandoahRegionAffiliation affiliation) const;
537 
538   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
539   ShenandoahMode*            mode()              const { return _gc_mode;           }
540   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
541   ShenandoahPacer*           pacer()             const { return _pacer;             }
542 
543   ShenandoahPhaseTimings*    phase_timings()     const { return _phase_timings;     }
544 
545   ShenandoahVerifier*        verifier();
546 
547 // ---------- VM subsystem bindings
548 //
549 private:
550   ShenandoahMonitoringSupport* _monitoring_support;
551   MemoryPool*                  _memory_pool;
552   MemoryPool*                  _young_gen_memory_pool;
553   MemoryPool*                  _old_gen_memory_pool;
554 
555   GCMemoryManager              _stw_memory_manager;
556   GCMemoryManager              _cycle_memory_manager;
557   ConcurrentGCTimer*           _gc_timer;
558   SoftRefPolicy                _soft_ref_policy;
559 
560   // For exporting to SA
561   int                          _log_min_obj_alignment_in_bytes;
562 public:
563   ShenandoahMonitoringSupport* monitoring_support() { return _monitoring_support;    }
564   GCMemoryManager* cycle_memory_manager()           { return &_cycle_memory_manager; }
565   GCMemoryManager* stw_memory_manager()             { return &_stw_memory_manager;   }
566   SoftRefPolicy* soft_ref_policy()                  { return &_soft_ref_policy;      }
567 
568   GrowableArray<GCMemoryManager*> memory_managers();
569   GrowableArray<MemoryPool*> memory_pools();
570   MemoryUsage memory_usage();
571   GCTracer* tracer();
572   ConcurrentGCTimer* gc_timer() const;
573 
574 // ---------- Class Unloading
575 //
576 private:
577   ShenandoahSharedFlag  _is_aging_cycle;
578   ShenandoahSharedFlag _unload_classes;
579   ShenandoahUnload     _unloader;
580 
581 public:
582   void set_unload_classes(bool uc);
583   bool unload_classes() const;
584 
585   // Perform STW class unloading and weak root cleaning
586   void parallel_cleaning(bool full_gc);
587 
588 private:
589   void stw_unload_classes(bool full_gc);
590   void stw_process_weak_roots(bool full_gc);
591   void stw_weak_refs(bool full_gc);
592 
593   // Heap iteration support
594   void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
595   bool prepare_aux_bitmap_for_iteration();
596   void reclaim_aux_bitmap_for_iteration();
597 
598 // ---------- Generic interface hooks
599 // Minor things that super-interface expects us to implement to play nice with
600 // the rest of runtime. Some of the things here are not required to be implemented,
601 // and can be stubbed out.
602 //
603 public:
604   AdaptiveSizePolicy* size_policy() shenandoah_not_implemented_return(NULL);
605   bool is_maximal_no_gc() const shenandoah_not_implemented_return(false);
606 
607   bool is_in(const void* p) const;
608 
609   bool is_in_active_generation(oop obj) const;
610   bool is_in_young(const void* p) const;
611   bool is_in_old(const void* p) const;
612   inline bool is_old(oop pobj) const;
613 
614   MemRegion reserved_region() const { return _reserved; }
615   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
616 
617   void collect(GCCause::Cause cause);
618   void do_full_collection(bool clear_all_soft_refs);
619 
620   // Used for parsing heap during error printing
621   HeapWord* block_start(const void* addr) const;
622   bool block_is_obj(const HeapWord* addr) const;
623   bool print_location(outputStream* st, void* addr) const;
624 
625   // Used for native heap walkers: heap dumpers, mostly
626   void object_iterate(ObjectClosure* cl);
627   // Parallel heap iteration support
628   virtual ParallelObjectIteratorImpl* parallel_object_iterator(uint workers);
629 
630   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
631   void keep_alive(oop obj);
632 
633 // ---------- Safepoint interface hooks
634 //
635 public:
636   void safepoint_synchronize_begin();
637   void safepoint_synchronize_end();
638 
639 // ---------- Code roots handling hooks
640 //
641 public:
642   void register_nmethod(nmethod* nm);
643   void unregister_nmethod(nmethod* nm);
644   void flush_nmethod(nmethod* nm);
645   void verify_nmethod(nmethod* nm) {}
646 
647 // ---------- Pinning hooks
648 //
649 public:
650   // Shenandoah supports per-object (per-region) pinning
651   bool supports_object_pinning() const { return true; }
652 
653   oop pin_object(JavaThread* thread, oop obj);
654   void unpin_object(JavaThread* thread, oop obj);
655 
656   void sync_pinned_region_status();
657   void assert_pinned_region_status() NOT_DEBUG_RETURN;
658 
659 // ---------- Concurrent Stack Processing support
660 //
661 public:
662   bool uses_stack_watermark_barrier() const { return true; }
663 
664 // ---------- Allocation support
665 //
666 private:
667   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region, bool is_promotion);
668 
669   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
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   inline HeapWord* allocate_from_plab(Thread* thread, size_t size, bool is_promotion);
674   HeapWord* allocate_from_plab_slow(Thread* thread, size_t size, bool is_promotion);
675   HeapWord* allocate_new_plab(size_t min_size, size_t word_size, size_t* actual_size);
676 
677 public:
678   HeapWord* allocate_memory(ShenandoahAllocRequest& request, bool is_promotion);
679   HeapWord* mem_allocate(size_t size, bool* what);
680   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
681                                                size_t size,
682                                                Metaspace::MetadataType mdtype);
683 
684   void notify_mutator_alloc_words(size_t words, bool waste);
685 
686   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
687   size_t tlab_capacity(Thread *thr) const;
688   size_t unsafe_max_tlab_alloc(Thread *thread) const;
689   size_t max_tlab_size() const;
690   size_t tlab_used(Thread* ignored) const;
691 
692   void ensure_parsability(bool retire_labs);
693 
694   void labs_make_parsable();
695   void tlabs_retire(bool resize);
696   void gclabs_retire(bool resize);
697 
698   void set_young_lab_region_flags();
699 
700 // ---------- Marking support
701 //
702 private:
703   ShenandoahMarkingContext* _marking_context;
704   MemRegion  _bitmap_region;
705   MemRegion  _aux_bitmap_region;
706   MarkBitMap _verification_bit_map;
707   MarkBitMap _aux_bit_map;
708 
709   size_t _bitmap_size;
710   size_t _bitmap_regions_per_slice;
711   size_t _bitmap_bytes_per_slice;
712 
713   size_t _pretouch_heap_page_size;
714   size_t _pretouch_bitmap_page_size;
715 
716   bool _bitmap_region_special;
717   bool _aux_bitmap_region_special;
718 
719   ShenandoahLiveData** _liveness_cache;
720 
721 public:
722   inline ShenandoahMarkingContext* complete_marking_context() const;
723   inline ShenandoahMarkingContext* marking_context() const;
724 
725   template<class T>
726   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
727 
728   template<class T>
729   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
730 
731   template<class T>
732   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
733 
734   // SATB barriers hooks
735   inline bool requires_marking(const void* entry) const;
736 
737   // Support for bitmap uncommits
738   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
739   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
740   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
741 
742   // Liveness caching support
743   ShenandoahLiveData* get_liveness_cache(uint worker_id);
744   void flush_liveness_cache(uint worker_id);
745 
746   size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
747 
748 // ---------- Evacuation support
749 //
750 private:
751   ShenandoahCollectionSet* _collection_set;
752   ShenandoahEvacOOMHandler _oom_evac_handler;
753   ShenandoahSharedFlag _old_gen_oom_evac;
754 
755   inline oop try_evacuate_object(oop src, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahRegionAffiliation target_gen);
756   void handle_old_evacuation(HeapWord* obj, size_t words, bool promotion);
757   void handle_old_evacuation_failure();
758   void handle_promotion_failure();
759 
760 public:
761   static address in_cset_fast_test_addr();
762 
763   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
764 
765   // Checks if object is in the collection set.
766   inline bool in_collection_set(oop obj) const;
767 
768   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
769   inline bool in_collection_set_loc(void* loc) const;
770 
771   // Evacuates or promotes object src. Returns the evacuated object, either evacuated
772   // by this thread, or by some other thread.
773   inline oop evacuate_object(oop src, Thread* thread);
774 
775   // Call before/after evacuation.
776   inline void enter_evacuation(Thread* t);
777   inline void leave_evacuation(Thread* t);
778 
779   inline bool clear_old_evacuation_failure();
780 
781 // ---------- Generational support
782 //
783 private:
784   RememberedScanner* _card_scan;
785 
786 public:
787   inline RememberedScanner* card_scan() { return _card_scan; }
788   void clear_cards_for(ShenandoahHeapRegion* region);
789   void dirty_cards(HeapWord* start, HeapWord* end);
790   void clear_cards(HeapWord* start, HeapWord* end);
791   void mark_card_as_dirty(void* location);
792   void retire_plab(PLAB* plab);
793   void cancel_old_gc();
794   bool is_old_gc_active();
795   void coalesce_and_fill_old_regions();
796 
797 // ---------- Helper functions
798 //
799 public:
800   template <class T>
801   inline void conc_update_with_forwarded(T* p);
802 
803   template <class T>
804   inline void update_with_forwarded(T* p);
805 
806   static inline void atomic_update_oop(oop update,       oop* addr,       oop compare);
807   static inline void atomic_update_oop(oop update, narrowOop* addr,       oop compare);
808   static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
809 
810   static inline bool atomic_update_oop_check(oop update,       oop* addr,       oop compare);
811   static inline bool atomic_update_oop_check(oop update, narrowOop* addr,       oop compare);
812   static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
813 
814   static inline void atomic_clear_oop(      oop* addr,       oop compare);
815   static inline void atomic_clear_oop(narrowOop* addr,       oop compare);
816   static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
817 
818   size_t trash_humongous_region_at(ShenandoahHeapRegion *r);
819 
820   static inline void increase_object_age(oop obj, uint additional_age);
821 
822   void transfer_old_pointers_from_satb();
823 private:
824   void trash_cset_regions();
825 
826 // ---------- Testing helpers functions
827 //
828 private:
829   ShenandoahSharedFlag _inject_alloc_failure;
830 
831   void try_inject_alloc_failure();
832   bool should_inject_alloc_failure();
833 };
834 
835 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP