< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp

Print this page

  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  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
 28 

 29 #include "gc/shared/markBitMap.hpp"
 30 #include "gc/shared/softRefPolicy.hpp"
 31 #include "gc/shared/collectedHeap.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 "memory/metaspace.hpp"
 40 #include "services/memoryManager.hpp"
 41 #include "utilities/globalDefinitions.hpp"
 42 #include "utilities/stack.hpp"
 43 
 44 class ConcurrentGCTimer;
 45 class ObjectIterateScanRootClosure;

 46 class ShenandoahCollectorPolicy;
 47 class ShenandoahControlThread;

 48 class ShenandoahGCSession;
 49 class ShenandoahGCStateResetter;



 50 class ShenandoahHeuristics;


 51 class ShenandoahMarkingContext;
 52 class ShenandoahMode;
 53 class ShenandoahPhaseTimings;
 54 class ShenandoahHeap;
 55 class ShenandoahHeapRegion;
 56 class ShenandoahHeapRegionClosure;
 57 class ShenandoahCollectionSet;
 58 class ShenandoahFreeSet;
 59 class ShenandoahConcurrentMark;
 60 class ShenandoahFullGC;
 61 class ShenandoahMonitoringSupport;

 62 class ShenandoahPacer;
 63 class ShenandoahReferenceProcessor;
 64 class ShenandoahVerifier;
 65 class ShenandoahWorkerThreads;
 66 class VMStructs;
 67 
 68 // Used for buffering per-region liveness data.
 69 // Needed since ShenandoahHeapRegion uses atomics to update liveness.
 70 // The ShenandoahHeap array has max-workers elements, each of which is an array of
 71 // uint16_t * max_regions. The choice of uint16_t is not accidental:
 72 // there is a tradeoff between static/dynamic footprint that translates
 73 // into cache pressure (which is already high during marking), and
 74 // too many atomic updates. uint32_t is too large, uint8_t is too small.
 75 typedef uint16_t ShenandoahLiveData;
 76 #define SHENANDOAH_LIVEDATA_MAX ((ShenandoahLiveData)-1)
 77 
 78 class ShenandoahRegionIterator : public StackObj {
 79 private:
 80   ShenandoahHeap* _heap;
 81 

 91   ShenandoahRegionIterator(ShenandoahHeap* heap);
 92 
 93   // Reset iterator to default state
 94   void reset();
 95 
 96   // Returns next region, or null if there are no more regions.
 97   // This is multi-thread-safe.
 98   inline ShenandoahHeapRegion* next();
 99 
100   // This is *not* MT safe. However, in the absence of multithreaded access, it
101   // can be used to determine if there is more work to do.
102   bool has_next() const;
103 };
104 
105 class ShenandoahHeapRegionClosure : public StackObj {
106 public:
107   virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
108   virtual bool is_thread_safe() { return false; }
109 };
110 










111 typedef ShenandoahLock    ShenandoahHeapLock;
112 typedef ShenandoahLocker  ShenandoahHeapLocker;
113 typedef Stack<oop, mtGC>  ShenandoahScanObjectStack;
114 
115 // Shenandoah GC is low-pause concurrent GC that uses Brooks forwarding pointers
116 // to encode forwarding data. See BrooksPointer for details on forwarding data encoding.
117 // See ShenandoahControlThread for GC cycle structure.
118 //
119 class ShenandoahHeap : public CollectedHeap {
120   friend class ShenandoahAsserts;
121   friend class VMStructs;
122   friend class ShenandoahGCSession;
123   friend class ShenandoahGCStateResetter;
124   friend class ShenandoahParallelObjectIterator;
125   friend class ShenandoahSafepoint;
126   // Supported GC
127   friend class ShenandoahConcurrentGC;

128   friend class ShenandoahDegenGC;
129   friend class ShenandoahFullGC;
130   friend class ShenandoahUnload;
131 
132 // ---------- Locks that guard important data structures in Heap
133 //
134 private:
135   ShenandoahHeapLock _lock;

136 
137 public:
138   ShenandoahHeapLock* lock() {
139     return &_lock;
140   }
141 

















142 // ---------- Initialization, termination, identification, printing routines
143 //
144 public:
145   static ShenandoahHeap* heap();
146 
147   const char* name()          const override { return "Shenandoah"; }
148   ShenandoahHeap::Name kind() const override { return CollectedHeap::Shenandoah; }
149 
150   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
151   jint initialize() override;
152   void post_initialize() override;
153   void initialize_mode();
154   void initialize_heuristics();
155 
156   void initialize_serviceability() override;
157 
158   void print_on(outputStream* st)              const override;
159   void print_extended_on(outputStream *st)     const override;
160   void print_tracing_info()                    const override;
161   void print_heap_regions_on(outputStream* st) const;
162 
163   void stop() override;
164 
165   void prepare_for_verify() override;
166   void verify(VerifyOption vo) override;
167 



168 // WhiteBox testing support.
169   bool supports_concurrent_gc_breakpoints() const override {
170     return true;
171   }
172 
173 // ---------- Heap counters and metrics
174 //
175 private:
176            size_t _initial_size;
177            size_t _minimum_size;





178   volatile size_t _soft_max_size;
179   shenandoah_padding(0);
180   volatile size_t _used;
181   volatile size_t _committed;
182   volatile size_t _bytes_allocated_since_gc_start;
183   shenandoah_padding(1);
184 


185 public:
186   void increase_used(size_t bytes);
187   void decrease_used(size_t bytes);
188   void set_used(size_t bytes);

189 
190   void increase_committed(size_t bytes);
191   void decrease_committed(size_t bytes);
192   void increase_allocated(size_t bytes);
193 
194   size_t bytes_allocated_since_gc_start();
195   void reset_bytes_allocated_since_gc_start();
196 
197   size_t min_capacity()      const;
198   size_t max_capacity()      const override;
199   size_t soft_max_capacity() const;
200   size_t initial_capacity()  const;
201   size_t capacity()          const override;
202   size_t used()              const override;
203   size_t committed()         const;
204 
205   void set_soft_max_capacity(size_t v);
206 
207 // ---------- Workers handling
208 //
209 private:
210   uint _max_workers;
211   ShenandoahWorkerThreads* _workers;
212   ShenandoahWorkerThreads* _safepoint_workers;
213 
214 public:
215   uint max_workers();
216   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
217 
218   WorkerThreads* workers() const;
219   WorkerThreads* safepoint_workers() override;
220 
221   void gc_threads_do(ThreadClosure* tcl) const override;
222 
223 // ---------- Heap regions handling machinery
224 //
225 private:
226   MemRegion _heap_region;
227   bool      _heap_region_special;
228   size_t    _num_regions;
229   ShenandoahHeapRegion** _regions;

230   ShenandoahRegionIterator _update_refs_iterator;
231 
232 public:
233 
234   inline HeapWord* base() const { return _heap_region.start(); }
235 
236   inline size_t num_regions() const { return _num_regions; }
237   inline bool is_heap_region_special() { return _heap_region_special; }
238 
239   inline ShenandoahHeapRegion* const heap_region_containing(const void* addr) const;
240   inline size_t heap_region_index_containing(const void* addr) const;
241 
242   inline ShenandoahHeapRegion* const get_region(size_t region_idx) const;
243 
244   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
245   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
246 


247 // ---------- GC state machinery
248 //
249 // GC state describes the important parts of collector state, that may be
250 // used to make barrier selection decisions in the native and generated code.
251 // Multiple bits can be set at once.
252 //
253 // Important invariant: when GC state is zero, the heap is stable, and no barriers
254 // are required.
255 //
256 public:
257   enum GCStateBitPos {
258     // Heap has forwarded objects: needs LRB barriers.
259     HAS_FORWARDED_BITPOS   = 0,
260 
261     // Heap is under marking: needs SATB barriers.

262     MARKING_BITPOS    = 1,
263 
264     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
265     EVACUATION_BITPOS = 2,
266 
267     // Heap is under updating: needs no additional barriers.
268     UPDATEREFS_BITPOS = 3,
269 
270     // Heap is under weak-reference/roots processing: needs weak-LRB barriers.
271     WEAK_ROOTS_BITPOS  = 4,






272   };
273 
274   enum GCState {
275     STABLE        = 0,
276     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
277     MARKING       = 1 << MARKING_BITPOS,
278     EVACUATION    = 1 << EVACUATION_BITPOS,
279     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
280     WEAK_ROOTS    = 1 << WEAK_ROOTS_BITPOS,


281   };
282 
283 private:
284   ShenandoahSharedBitmap _gc_state;
285   ShenandoahSharedFlag   _degenerated_gc_in_progress;
286   ShenandoahSharedFlag   _full_gc_in_progress;
287   ShenandoahSharedFlag   _full_gc_move_in_progress;
288   ShenandoahSharedFlag   _progress_last_gc;
289   ShenandoahSharedFlag   _concurrent_strong_root_in_progress;
290 































291   void set_gc_state_all_threads(char state);
292   void set_gc_state_mask(uint mask, bool value);
293 
294 public:
295   char gc_state() const;
296 
297   void set_concurrent_mark_in_progress(bool in_progress);


298   void set_evacuation_in_progress(bool in_progress);
299   void set_update_refs_in_progress(bool in_progress);
300   void set_degenerated_gc_in_progress(bool in_progress);
301   void set_full_gc_in_progress(bool in_progress);
302   void set_full_gc_move_in_progress(bool in_progress);
303   void set_has_forwarded_objects(bool cond);
304   void set_concurrent_strong_root_in_progress(bool cond);
305   void set_concurrent_weak_root_in_progress(bool cond);
306 


307   inline bool is_stable() const;
308   inline bool is_idle() const;

309   inline bool is_concurrent_mark_in_progress() const;


310   inline bool is_update_refs_in_progress() const;
311   inline bool is_evacuation_in_progress() const;
312   inline bool is_degenerated_gc_in_progress() const;
313   inline bool is_full_gc_in_progress() const;
314   inline bool is_full_gc_move_in_progress() const;
315   inline bool has_forwarded_objects() const;
316 
317   inline bool is_stw_gc_in_progress() const;
318   inline bool is_concurrent_strong_root_in_progress() const;
319   inline bool is_concurrent_weak_root_in_progress() const;





































320 
321 private:


322   enum CancelState {
323     // Normal state. GC has not been cancelled and is open for cancellation.
324     // Worker threads can suspend for safepoint.
325     CANCELLABLE,
326 
327     // GC has been cancelled. Worker threads can not suspend for
328     // safepoint but must finish their work as soon as possible.
329     CANCELLED
330   };
331 

332   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;





333   bool try_cancel_gc();
334 
335 public:
336 
337   inline bool cancelled_gc() const;
338   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
339 
340   inline void clear_cancelled_gc();
341 

342   void cancel_gc(GCCause::Cause cause);
343 
344 public:
345   // Elastic heap support
346   void entry_uncommit(double shrink_before, size_t shrink_until);
347   void op_uncommit(double shrink_before, size_t shrink_until);
348 
349 private:
350   // GC support
351   // Reset bitmap, prepare regions for new GC cycle
352   void prepare_gc();
353   void prepare_regions_and_collection_set(bool concurrent);
354   // Evacuation
355   void evacuate_collection_set(bool concurrent);
356   // Concurrent root processing
357   void prepare_concurrent_roots();
358   void finish_concurrent_roots();
359   // Concurrent class unloading support
360   void do_class_unloading();
361   // Reference updating
362   void prepare_update_heap_references(bool concurrent);
363   void update_heap_references(bool concurrent);
364   // Final update region states
365   void update_heap_region_states(bool concurrent);
366   void rebuild_free_set(bool concurrent);
367 
368   void rendezvous_threads();
369   void recycle_trash();
370 public:

371   void notify_gc_progress()    { _progress_last_gc.set();   }
372   void notify_gc_no_progress() { _progress_last_gc.unset(); }
373 
374 //
375 // Mark support
376 private:




377   ShenandoahControlThread*   _control_thread;

378   ShenandoahCollectorPolicy* _shenandoah_policy;
379   ShenandoahMode*            _gc_mode;
380   ShenandoahHeuristics*      _heuristics;
381   ShenandoahFreeSet*         _free_set;
382   ShenandoahPacer*           _pacer;
383   ShenandoahVerifier*        _verifier;
384 
385   ShenandoahPhaseTimings*    _phase_timings;



386 
387   ShenandoahControlThread*   control_thread()          { return _control_thread;    }
388 
389 public:










390   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
391   ShenandoahMode*            mode()              const { return _gc_mode;           }
392   ShenandoahHeuristics*      heuristics()        const { return _heuristics;        }
393   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
394   ShenandoahPacer*           pacer()             const { return _pacer;             }
395 
396   ShenandoahPhaseTimings*    phase_timings()     const { return _phase_timings;     }




397 
398   ShenandoahVerifier*        verifier();
399 
400 // ---------- VM subsystem bindings
401 //
402 private:
403   ShenandoahMonitoringSupport* _monitoring_support;
404   MemoryPool*                  _memory_pool;



405   GCMemoryManager              _stw_memory_manager;
406   GCMemoryManager              _cycle_memory_manager;
407   ConcurrentGCTimer*           _gc_timer;
408   SoftRefPolicy                _soft_ref_policy;
409 
410   // For exporting to SA
411   int                          _log_min_obj_alignment_in_bytes;
412 public:
413   ShenandoahMonitoringSupport* monitoring_support()          { return _monitoring_support;    }
414   GCMemoryManager* cycle_memory_manager()                    { return &_cycle_memory_manager; }
415   GCMemoryManager* stw_memory_manager()                      { return &_stw_memory_manager;   }
416   SoftRefPolicy* soft_ref_policy()                  override { return &_soft_ref_policy;      }
417 
418   GrowableArray<GCMemoryManager*> memory_managers() override;
419   GrowableArray<MemoryPool*> memory_pools() override;
420   MemoryUsage memory_usage() override;
421   GCTracer* tracer();
422   ConcurrentGCTimer* gc_timer() const;
423 
424 // ---------- Reference processing
425 //
426 private:
427   ShenandoahReferenceProcessor* const _ref_processor;
428 
429 public:
430   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
431 
432 // ---------- Class Unloading
433 //
434 private:

435   ShenandoahSharedFlag _unload_classes;
436   ShenandoahUnload     _unloader;
437 
438 public:
439   void set_unload_classes(bool uc);
440   bool unload_classes() const;
441 
442   // Perform STW class unloading and weak root cleaning
443   void parallel_cleaning(bool full_gc);
444 
445 private:
446   void stw_unload_classes(bool full_gc);
447   void stw_process_weak_roots(bool full_gc);
448   void stw_weak_refs(bool full_gc);
449 



450   // Heap iteration support
451   void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
452   bool prepare_aux_bitmap_for_iteration();
453   void reclaim_aux_bitmap_for_iteration();
454 
455 // ---------- Generic interface hooks
456 // Minor things that super-interface expects us to implement to play nice with
457 // the rest of runtime. Some of the things here are not required to be implemented,
458 // and can be stubbed out.
459 //
460 public:
461   bool is_maximal_no_gc() const override shenandoah_not_implemented_return(false);
462 
463   bool is_in(const void* p) const override;










464 
465   bool requires_barriers(stackChunkOop obj) const override;
466 
467   MemRegion reserved_region() const { return _reserved; }
468   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
469 
470   void collect(GCCause::Cause cause) override;
471   void do_full_collection(bool clear_all_soft_refs) override;
472 
473   // Used for parsing heap during error printing
474   HeapWord* block_start(const void* addr) const;
475   bool block_is_obj(const HeapWord* addr) const;
476   bool print_location(outputStream* st, void* addr) const override;
477 
478   // Used for native heap walkers: heap dumpers, mostly
479   void object_iterate(ObjectClosure* cl) override;
480   // Parallel heap iteration support
481   ParallelObjectIteratorImpl* parallel_object_iterator(uint workers) override;
482 
483   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.

497   void verify_nmethod(nmethod* nm) override {}
498 
499 // ---------- Pinning hooks
500 //
501 public:
502   // Shenandoah supports per-object (per-region) pinning
503   void pin_object(JavaThread* thread, oop obj) override;
504   void unpin_object(JavaThread* thread, oop obj) override;
505 
506   void sync_pinned_region_status();
507   void assert_pinned_region_status() NOT_DEBUG_RETURN;
508 
509 // ---------- Concurrent Stack Processing support
510 //
511 public:
512   bool uses_stack_watermark_barrier() const override { return true; }
513 
514 // ---------- Allocation support
515 //
516 private:
517   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region);





518   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
519   HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
520   HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
521 




522 public:
523   HeapWord* allocate_memory(ShenandoahAllocRequest& request);
524   HeapWord* mem_allocate(size_t size, bool* what) override;
525   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
526                                                size_t size,
527                                                Metaspace::MetadataType mdtype) override;
528 
529   void notify_mutator_alloc_words(size_t words, bool waste);
530 
531   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override;
532   size_t tlab_capacity(Thread *thr) const override;
533   size_t unsafe_max_tlab_alloc(Thread *thread) const override;
534   size_t max_tlab_size() const override;
535   size_t tlab_used(Thread* ignored) const override;
536 
537   void ensure_parsability(bool retire_labs) override;
538 
539   void labs_make_parsable();
540   void tlabs_retire(bool resize);
541   void gclabs_retire(bool resize);
542 






543 // ---------- Marking support
544 //
545 private:
546   ShenandoahMarkingContext* _marking_context;
547   MemRegion  _bitmap_region;
548   MemRegion  _aux_bitmap_region;
549   MarkBitMap _verification_bit_map;
550   MarkBitMap _aux_bit_map;
551 
552   size_t _bitmap_size;
553   size_t _bitmap_regions_per_slice;
554   size_t _bitmap_bytes_per_slice;
555 
556   size_t _pretouch_heap_page_size;
557   size_t _pretouch_bitmap_page_size;
558 
559   bool _bitmap_region_special;
560   bool _aux_bitmap_region_special;
561 
562   ShenandoahLiveData** _liveness_cache;
563 
564 public:
565   inline ShenandoahMarkingContext* complete_marking_context() const;
566   inline ShenandoahMarkingContext* marking_context() const;
567   inline void mark_complete_marking_context();
568   inline void mark_incomplete_marking_context();
569 
570   template<class T>
571   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
572 
573   template<class T>
574   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
575 
576   template<class T>
577   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
578 
579   void reset_mark_bitmap();
580 
581   // SATB barriers hooks
582   inline bool requires_marking(const void* entry) const;
583 
584   // Support for bitmap uncommits
585   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
586   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
587   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
588 
589   // Liveness caching support
590   ShenandoahLiveData* get_liveness_cache(uint worker_id);
591   void flush_liveness_cache(uint worker_id);
592 
593   size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
594 
595 // ---------- Evacuation support
596 //
597 private:
598   ShenandoahCollectionSet* _collection_set;
599   ShenandoahEvacOOMHandler _oom_evac_handler;





600 
601 public:


602   static address in_cset_fast_test_addr();
603 
604   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
605 
606   // Checks if object is in the collection set.
607   inline bool in_collection_set(oop obj) const;
608 
609   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
610   inline bool in_collection_set_loc(void* loc) const;
611 
612   // Evacuates object src. Returns the evacuated object, either evacuated
613   // by this thread, or by some other thread.
614   inline oop evacuate_object(oop src, Thread* thread);
615 
616   // Call before/after evacuation.
617   inline void enter_evacuation(Thread* t);
618   inline void leave_evacuation(Thread* t);
619 

















620 // ---------- Helper functions
621 //
622 public:
623   template <class T>
624   inline void conc_update_with_forwarded(T* p);
625 
626   template <class T>
627   inline void update_with_forwarded(T* p);
628 
629   static inline void atomic_update_oop(oop update,       oop* addr,       oop compare);
630   static inline void atomic_update_oop(oop update, narrowOop* addr,       oop compare);
631   static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
632 
633   static inline bool atomic_update_oop_check(oop update,       oop* addr,       oop compare);
634   static inline bool atomic_update_oop_check(oop update, narrowOop* addr,       oop compare);
635   static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
636 
637   static inline void atomic_clear_oop(      oop* addr,       oop compare);
638   static inline void atomic_clear_oop(narrowOop* addr,       oop compare);
639   static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
640 
641   void trash_humongous_region_at(ShenandoahHeapRegion *r);











642 
643 private:
644   void trash_cset_regions();
645 
646 // ---------- Testing helpers functions
647 //
648 private:
649   ShenandoahSharedFlag _inject_alloc_failure;
650 
651   void try_inject_alloc_failure();
652   bool should_inject_alloc_failure();
653 };
654 
655 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP

  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/ageTable.hpp"
 31 #include "gc/shared/markBitMap.hpp"
 32 #include "gc/shared/softRefPolicy.hpp"
 33 #include "gc/shared/collectedHeap.hpp"
 34 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
 35 #include "gc/shenandoah/heuristics/shenandoahSpaceInfo.hpp"
 36 #include "gc/shenandoah/shenandoahAsserts.hpp"
 37 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
 38 #include "gc/shenandoah/shenandoahAsserts.hpp"
 39 #include "gc/shenandoah/shenandoahLock.hpp"
 40 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
 41 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 42 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 43 #include "gc/shenandoah/shenandoahMmuTracker.hpp"
 44 #include "gc/shenandoah/shenandoahPadding.hpp"
 45 #include "gc/shenandoah/shenandoahScanRemembered.hpp"
 46 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 47 #include "gc/shenandoah/shenandoahUnload.hpp"
 48 #include "memory/metaspace.hpp"
 49 #include "services/memoryManager.hpp"
 50 #include "utilities/globalDefinitions.hpp"
 51 #include "utilities/stack.hpp"
 52 
 53 class ConcurrentGCTimer;
 54 class ObjectIterateScanRootClosure;
 55 class PLAB;
 56 class ShenandoahCollectorPolicy;
 57 class ShenandoahControlThread;
 58 class ShenandoahRegulatorThread;
 59 class ShenandoahGCSession;
 60 class ShenandoahGCStateResetter;
 61 class ShenandoahGeneration;
 62 class ShenandoahYoungGeneration;
 63 class ShenandoahOldGeneration;
 64 class ShenandoahHeuristics;
 65 class ShenandoahOldHeuristics;
 66 class ShenandoahYoungHeuristics;
 67 class ShenandoahMarkingContext;

 68 class ShenandoahPhaseTimings;
 69 class ShenandoahHeap;
 70 class ShenandoahHeapRegion;
 71 class ShenandoahHeapRegionClosure;
 72 class ShenandoahCollectionSet;
 73 class ShenandoahFreeSet;
 74 class ShenandoahConcurrentMark;
 75 class ShenandoahFullGC;
 76 class ShenandoahMonitoringSupport;
 77 class ShenandoahMode;
 78 class ShenandoahPacer;
 79 class ShenandoahReferenceProcessor;
 80 class ShenandoahVerifier;
 81 class ShenandoahWorkerThreads;
 82 class VMStructs;
 83 
 84 // Used for buffering per-region liveness data.
 85 // Needed since ShenandoahHeapRegion uses atomics to update liveness.
 86 // The ShenandoahHeap array has max-workers elements, each of which is an array of
 87 // uint16_t * max_regions. The choice of uint16_t is not accidental:
 88 // there is a tradeoff between static/dynamic footprint that translates
 89 // into cache pressure (which is already high during marking), and
 90 // too many atomic updates. uint32_t is too large, uint8_t is too small.
 91 typedef uint16_t ShenandoahLiveData;
 92 #define SHENANDOAH_LIVEDATA_MAX ((ShenandoahLiveData)-1)
 93 
 94 class ShenandoahRegionIterator : public StackObj {
 95 private:
 96   ShenandoahHeap* _heap;
 97 

107   ShenandoahRegionIterator(ShenandoahHeap* heap);
108 
109   // Reset iterator to default state
110   void reset();
111 
112   // Returns next region, or null if there are no more regions.
113   // This is multi-thread-safe.
114   inline ShenandoahHeapRegion* next();
115 
116   // This is *not* MT safe. However, in the absence of multithreaded access, it
117   // can be used to determine if there is more work to do.
118   bool has_next() const;
119 };
120 
121 class ShenandoahHeapRegionClosure : public StackObj {
122 public:
123   virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
124   virtual bool is_thread_safe() { return false; }
125 };
126 
127 template<ShenandoahGenerationType GENERATION>
128 class ShenandoahGenerationRegionClosure : public ShenandoahHeapRegionClosure {
129  public:
130   explicit ShenandoahGenerationRegionClosure(ShenandoahHeapRegionClosure* cl) : _cl(cl) {}
131   void heap_region_do(ShenandoahHeapRegion* r);
132   virtual bool is_thread_safe() { return _cl->is_thread_safe(); }
133  private:
134   ShenandoahHeapRegionClosure* _cl;
135 };
136 
137 typedef ShenandoahLock    ShenandoahHeapLock;
138 typedef ShenandoahLocker  ShenandoahHeapLocker;
139 typedef Stack<oop, mtGC>  ShenandoahScanObjectStack;
140 
141 // Shenandoah GC is low-pause concurrent GC that uses Brooks forwarding pointers
142 // to encode forwarding data. See BrooksPointer for details on forwarding data encoding.
143 // See ShenandoahControlThread for GC cycle structure.
144 //
145 class ShenandoahHeap : public CollectedHeap {
146   friend class ShenandoahAsserts;
147   friend class VMStructs;
148   friend class ShenandoahGCSession;
149   friend class ShenandoahGCStateResetter;
150   friend class ShenandoahParallelObjectIterator;
151   friend class ShenandoahSafepoint;
152   // Supported GC
153   friend class ShenandoahConcurrentGC;
154   friend class ShenandoahOldGC;
155   friend class ShenandoahDegenGC;
156   friend class ShenandoahFullGC;
157   friend class ShenandoahUnload;
158 
159 // ---------- Locks that guard important data structures in Heap
160 //
161 private:
162   ShenandoahHeapLock _lock;
163   ShenandoahGeneration* _gc_generation;
164 
165 public:
166   ShenandoahHeapLock* lock() {
167     return &_lock;
168   }
169 
170   ShenandoahGeneration* active_generation() const {
171     // last or latest generation might be a better name here.
172     return _gc_generation;
173   }
174 
175   void set_gc_generation(ShenandoahGeneration* generation) {
176     _gc_generation = generation;
177   }
178 
179   ShenandoahHeuristics* heuristics();
180   ShenandoahOldHeuristics* old_heuristics();
181   ShenandoahYoungHeuristics* young_heuristics();
182 
183   bool doing_mixed_evacuations();
184   bool is_old_bitmap_stable() const;
185   bool is_gc_generation_young() const;
186 
187 // ---------- Initialization, termination, identification, printing routines
188 //
189 public:
190   static ShenandoahHeap* heap();
191 
192   const char* name()          const override { return "Shenandoah"; }
193   ShenandoahHeap::Name kind() const override { return CollectedHeap::Shenandoah; }
194 
195   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
196   jint initialize() override;
197   void post_initialize() override;
198   void initialize_heuristics_generations();
199   virtual void print_init_logger() const;

200   void initialize_serviceability() override;
201 
202   void print_on(outputStream* st)              const override;
203   void print_extended_on(outputStream *st)     const override;
204   void print_tracing_info()                    const override;
205   void print_heap_regions_on(outputStream* st) const;
206 
207   void stop() override;
208 
209   void prepare_for_verify() override;
210   void verify(VerifyOption vo) override;
211 
212   bool verify_generation_usage(bool verify_old, size_t old_regions, size_t old_bytes, size_t old_waste,
213                                bool verify_young, size_t young_regions, size_t young_bytes, size_t young_waste);
214 
215 // WhiteBox testing support.
216   bool supports_concurrent_gc_breakpoints() const override {
217     return true;
218   }
219 
220 // ---------- Heap counters and metrics
221 //
222 private:
223   size_t _initial_size;
224   size_t _minimum_size;
225   size_t _promotion_potential;
226   size_t _pad_for_promote_in_place;    // bytes of filler
227   size_t _promotable_humongous_regions;
228   size_t _regular_regions_promoted_in_place;
229 
230   volatile size_t _soft_max_size;
231   shenandoah_padding(0);

232   volatile size_t _committed;

233   shenandoah_padding(1);
234 
235   void increase_used(const ShenandoahAllocRequest& req);
236 
237 public:
238   void increase_used(ShenandoahGeneration* generation, size_t bytes);
239   void decrease_used(ShenandoahGeneration* generation, size_t bytes);
240   void increase_humongous_waste(ShenandoahGeneration* generation, size_t bytes);
241   void decrease_humongous_waste(ShenandoahGeneration* generation, size_t bytes);
242 
243   void increase_committed(size_t bytes);
244   void decrease_committed(size_t bytes);

245 

246   void reset_bytes_allocated_since_gc_start();
247 
248   size_t min_capacity()      const;
249   size_t max_capacity()      const override;
250   size_t soft_max_capacity() const;
251   size_t initial_capacity()  const;
252   size_t capacity()          const override;
253   size_t used()              const override;
254   size_t committed()         const;
255 
256   void set_soft_max_capacity(size_t v);
257 
258 // ---------- Workers handling
259 //
260 private:
261   uint _max_workers;
262   ShenandoahWorkerThreads* _workers;
263   ShenandoahWorkerThreads* _safepoint_workers;
264 
265 public:
266   uint max_workers();
267   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
268 
269   WorkerThreads* workers() const;
270   WorkerThreads* safepoint_workers() override;
271 
272   void gc_threads_do(ThreadClosure* tcl) const override;
273 
274 // ---------- Heap regions handling machinery
275 //
276 private:
277   MemRegion _heap_region;
278   bool      _heap_region_special;
279   size_t    _num_regions;
280   ShenandoahHeapRegion** _regions;
281   uint8_t* _affiliations;       // Holds array of enum ShenandoahAffiliation, including FREE status in non-generational mode
282   ShenandoahRegionIterator _update_refs_iterator;
283 
284 public:
285 
286   inline HeapWord* base() const { return _heap_region.start(); }
287 
288   inline size_t num_regions() const { return _num_regions; }
289   inline bool is_heap_region_special() { return _heap_region_special; }
290 
291   inline ShenandoahHeapRegion* heap_region_containing(const void* addr) const;
292   inline size_t heap_region_index_containing(const void* addr) const;
293 
294   inline ShenandoahHeapRegion* get_region(size_t region_idx) const;
295 
296   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
297   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
298 
299   inline ShenandoahMmuTracker* mmu_tracker() { return &_mmu_tracker; };
300 
301 // ---------- GC state machinery
302 //
303 // GC state describes the important parts of collector state, that may be
304 // used to make barrier selection decisions in the native and generated code.
305 // Multiple bits can be set at once.
306 //
307 // Important invariant: when GC state is zero, the heap is stable, and no barriers
308 // are required.
309 //
310 public:
311   enum GCStateBitPos {
312     // Heap has forwarded objects: needs LRB barriers.
313     HAS_FORWARDED_BITPOS   = 0,
314 
315     // Heap is under marking: needs SATB barriers.
316     // For generational mode, it means either young or old marking, or both.
317     MARKING_BITPOS    = 1,
318 
319     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
320     EVACUATION_BITPOS = 2,
321 
322     // Heap is under updating: needs no additional barriers.
323     UPDATEREFS_BITPOS = 3,
324 
325     // Heap is under weak-reference/roots processing: needs weak-LRB barriers.
326     WEAK_ROOTS_BITPOS  = 4,
327 
328     // Young regions are under marking, need SATB barriers.
329     YOUNG_MARKING_BITPOS = 5,
330 
331     // Old regions are under marking, need SATB barriers.
332     OLD_MARKING_BITPOS = 6
333   };
334 
335   enum GCState {
336     STABLE        = 0,
337     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
338     MARKING       = 1 << MARKING_BITPOS,
339     EVACUATION    = 1 << EVACUATION_BITPOS,
340     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
341     WEAK_ROOTS    = 1 << WEAK_ROOTS_BITPOS,
342     YOUNG_MARKING = 1 << YOUNG_MARKING_BITPOS,
343     OLD_MARKING   = 1 << OLD_MARKING_BITPOS
344   };
345 
346 private:
347   ShenandoahSharedBitmap _gc_state;
348   ShenandoahSharedFlag   _degenerated_gc_in_progress;
349   ShenandoahSharedFlag   _full_gc_in_progress;
350   ShenandoahSharedFlag   _full_gc_move_in_progress;
351   ShenandoahSharedFlag   _progress_last_gc;
352   ShenandoahSharedFlag   _concurrent_strong_root_in_progress;
353 
354   // TODO: Revisit the following comment.  It may not accurately represent the true behavior when evacuations fail due to
355   // difficulty finding memory to hold evacuated objects.
356   //
357   // Note that the typical total expenditure on evacuation is less than the associated evacuation reserve because we generally
358   // reserve ShenandoahEvacWaste (> 1.0) times the anticipated evacuation need.  In the case that there is an excessive amount
359   // of waste, it may be that one thread fails to grab a new GCLAB, this does not necessarily doom the associated evacuation
360   // effort.  If this happens, the requesting thread blocks until some other thread manages to evacuate the offending object.
361   // Only after "all" threads fail to evacuate an object do we consider the evacuation effort to have failed.
362 
363   size_t _promoted_reserve;            // Bytes reserved within old-gen to hold the results of promotion
364   volatile size_t _promoted_expended;  // Bytes of old-gen memory expended on promotions
365 
366   size_t _old_evac_reserve;            // Bytes reserved within old-gen to hold evacuated objects from old-gen collection set
367   size_t _young_evac_reserve;          // Bytes reserved within young-gen to hold evacuated objects from young-gen collection set
368 
369   ShenandoahAgeCensus* _age_census;    // Age census used for adapting tenuring threshold in generational mode
370 
371   // At the end of final mark, but before we begin evacuating, heuristics calculate how much memory is required to
372   // hold the results of evacuating to young-gen and to old-gen.  These quantitites, stored in _promoted_reserve,
373   // _old_evac_reserve, and _young_evac_reserve, are consulted prior to rebuilding the free set (ShenandoahFreeSet)
374   // in preparation for evacuation.  When the free set is rebuilt, we make sure to reserve sufficient memory in the
375   // collector and old_collector sets to hold if _has_evacuation_reserve_quantities is true.  The other time we
376   // rebuild the freeset is at the end of GC, as we prepare to idle GC until the next trigger.  In this case,
377   // _has_evacuation_reserve_quantities is false because we don't yet know how much memory will need to be evacuated
378   // in the next GC cycle.  When _has_evacuation_reserve_quantities is false, the free set rebuild operation reserves
379   // for the collector and old_collector sets based on alternative mechanisms, such as ShenandoahEvacReserve,
380   // ShenandoahOldEvacReserve, and ShenandoahOldCompactionReserve.  In a future planned enhancement, the reserve
381   // for old_collector set when not _has_evacuation_reserve_quantities is based in part on anticipated promotion as
382   // determined by analysis of live data found during the previous GC pass which is one less than the current tenure age.
383   bool _has_evacuation_reserve_quantities;
384 
385   void set_gc_state_all_threads(char state);
386   void set_gc_state_mask(uint mask, bool value);
387 
388 public:
389   char gc_state() const;
390 
391   void set_evacuation_reserve_quantities(bool is_valid);
392   void set_concurrent_young_mark_in_progress(bool in_progress);
393   void set_concurrent_old_mark_in_progress(bool in_progress);
394   void set_evacuation_in_progress(bool in_progress);
395   void set_update_refs_in_progress(bool in_progress);
396   void set_degenerated_gc_in_progress(bool in_progress);
397   void set_full_gc_in_progress(bool in_progress);
398   void set_full_gc_move_in_progress(bool in_progress);
399   void set_has_forwarded_objects(bool cond);
400   void set_concurrent_strong_root_in_progress(bool cond);
401   void set_concurrent_weak_root_in_progress(bool cond);
402 
403   void set_aging_cycle(bool cond);
404 
405   inline bool is_stable() const;
406   inline bool is_idle() const;
407   inline bool has_evacuation_reserve_quantities() const;
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   inline bool is_aging_cycle() const;
423 
424   inline void clear_promotion_potential() { _promotion_potential = 0; };
425   inline void set_promotion_potential(size_t val) { _promotion_potential = val; };
426   inline size_t get_promotion_potential() { return _promotion_potential; };
427 
428   inline void set_pad_for_promote_in_place(size_t pad) { _pad_for_promote_in_place = pad; }
429   inline size_t get_pad_for_promote_in_place() { return _pad_for_promote_in_place; }
430 
431   inline void reserve_promotable_humongous_regions(size_t region_count) { _promotable_humongous_regions = region_count; }
432   inline void reserve_promotable_regular_regions(size_t region_count) { _regular_regions_promoted_in_place = region_count; }
433 
434   inline size_t get_promotable_humongous_regions() { return _promotable_humongous_regions; }
435   inline size_t get_regular_regions_promoted_in_place() { return _regular_regions_promoted_in_place; }
436 
437   // Returns previous value
438   inline size_t set_promoted_reserve(size_t new_val);
439   inline size_t get_promoted_reserve() const;
440   inline void augment_promo_reserve(size_t increment);
441 
442   inline void reset_promoted_expended();
443   inline size_t expend_promoted(size_t increment);
444   inline size_t unexpend_promoted(size_t decrement);
445   inline size_t get_promoted_expended();
446 
447   // Returns previous value
448   inline size_t set_old_evac_reserve(size_t new_val);
449   inline size_t get_old_evac_reserve() const;
450   inline void augment_old_evac_reserve(size_t increment);
451 
452   // Returns previous value
453   inline size_t set_young_evac_reserve(size_t new_val);
454   inline size_t get_young_evac_reserve() const;
455 
456   // Return the age census object for young gen (in generational mode)
457   inline ShenandoahAgeCensus* age_census() const;
458 
459 private:
460   void manage_satb_barrier(bool active);
461 
462   enum CancelState {
463     // Normal state. GC has not been cancelled and is open for cancellation.
464     // Worker threads can suspend for safepoint.
465     CANCELLABLE,
466 
467     // GC has been cancelled. Worker threads can not suspend for
468     // safepoint but must finish their work as soon as possible.
469     CANCELLED
470   };
471 
472   double _cancel_requested_time;
473   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;
474 
475   // Returns true if cancel request was successfully communicated.
476   // Returns false if some other thread already communicated cancel
477   // request.  A true return value does not mean GC has been
478   // cancelled, only that the process of cancelling GC has begun.
479   bool try_cancel_gc();
480 
481 public:

482   inline bool cancelled_gc() const;
483   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
484 
485   inline void clear_cancelled_gc(bool clear_oom_handler = true);
486 
487   void cancel_concurrent_mark();
488   void cancel_gc(GCCause::Cause cause);
489 
490 public:
491   // Elastic heap support
492   void entry_uncommit(double shrink_before, size_t shrink_until);
493   void op_uncommit(double shrink_before, size_t shrink_until);
494 
495 private:
496   // GC support



497   // Evacuation
498   void evacuate_collection_set(bool concurrent);
499   // Concurrent root processing
500   void prepare_concurrent_roots();
501   void finish_concurrent_roots();
502   // Concurrent class unloading support
503   void do_class_unloading();
504   // Reference updating
505   void prepare_update_heap_references(bool concurrent);
506   void update_heap_references(bool concurrent);
507   // Final update region states
508   void update_heap_region_states(bool concurrent);

509 
510   void rendezvous_threads();
511   void recycle_trash();
512 public:
513   void rebuild_free_set(bool concurrent);
514   void notify_gc_progress()    { _progress_last_gc.set();   }
515   void notify_gc_no_progress() { _progress_last_gc.unset(); }
516 
517 //
518 // Mark support
519 private:
520   ShenandoahYoungGeneration* _young_generation;
521   ShenandoahGeneration*      _global_generation;
522   ShenandoahOldGeneration*   _old_generation;
523 
524   ShenandoahControlThread*   _control_thread;
525   ShenandoahRegulatorThread* _regulator_thread;
526   ShenandoahCollectorPolicy* _shenandoah_policy;
527   ShenandoahMode*            _gc_mode;

528   ShenandoahFreeSet*         _free_set;
529   ShenandoahPacer*           _pacer;
530   ShenandoahVerifier*        _verifier;
531 
532   ShenandoahPhaseTimings*       _phase_timings;
533   ShenandoahEvacuationTracker*  _evac_tracker;
534   ShenandoahMmuTracker          _mmu_tracker;
535   ShenandoahGenerationSizer     _generation_sizer;
536 
537   ShenandoahRegulatorThread* regulator_thread()        { return _regulator_thread;  }
538 
539 public:
540   ShenandoahControlThread*   control_thread()          { return _control_thread;    }
541   ShenandoahYoungGeneration* young_generation()  const { return _young_generation;  }
542   ShenandoahGeneration*      global_generation() const { return _global_generation; }
543   ShenandoahOldGeneration*   old_generation()    const { return _old_generation;    }
544   ShenandoahGeneration*      generation_for(ShenandoahAffiliation affiliation) const;
545   const ShenandoahGenerationSizer* generation_sizer()  const { return &_generation_sizer;  }
546 
547   size_t max_size_for(ShenandoahGeneration* generation) const;
548   size_t min_size_for(ShenandoahGeneration* generation) const;
549 
550   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
551   ShenandoahMode*            mode()              const { return _gc_mode;           }

552   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
553   ShenandoahPacer*           pacer()             const { return _pacer;             }
554 
555   ShenandoahPhaseTimings*      phase_timings()   const { return _phase_timings;     }
556   ShenandoahEvacuationTracker* evac_tracker()    const { return  _evac_tracker;     }
557 
558   void on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation);
559   void on_cycle_end(ShenandoahGeneration* generation);
560 
561   ShenandoahVerifier*        verifier();
562 
563 // ---------- VM subsystem bindings
564 //
565 private:
566   ShenandoahMonitoringSupport* _monitoring_support;
567   MemoryPool*                  _memory_pool;
568   MemoryPool*                  _young_gen_memory_pool;
569   MemoryPool*                  _old_gen_memory_pool;
570 
571   GCMemoryManager              _stw_memory_manager;
572   GCMemoryManager              _cycle_memory_manager;
573   ConcurrentGCTimer*           _gc_timer;
574   SoftRefPolicy                _soft_ref_policy;
575 
576   // For exporting to SA
577   int                          _log_min_obj_alignment_in_bytes;
578 public:
579   ShenandoahMonitoringSupport* monitoring_support() const    { return _monitoring_support;    }
580   GCMemoryManager* cycle_memory_manager()                    { return &_cycle_memory_manager; }
581   GCMemoryManager* stw_memory_manager()                      { return &_stw_memory_manager;   }
582   SoftRefPolicy* soft_ref_policy()                  override { return &_soft_ref_policy;      }
583 
584   GrowableArray<GCMemoryManager*> memory_managers() override;
585   GrowableArray<MemoryPool*> memory_pools() override;
586   MemoryUsage memory_usage() override;
587   GCTracer* tracer();
588   ConcurrentGCTimer* gc_timer() const;
589 








590 // ---------- Class Unloading
591 //
592 private:
593   ShenandoahSharedFlag  _is_aging_cycle;
594   ShenandoahSharedFlag _unload_classes;
595   ShenandoahUnload     _unloader;
596 
597 public:
598   void set_unload_classes(bool uc);
599   bool unload_classes() const;
600 
601   // Perform STW class unloading and weak root cleaning
602   void parallel_cleaning(bool full_gc);
603 
604 private:
605   void stw_unload_classes(bool full_gc);
606   void stw_process_weak_roots(bool full_gc);
607   void stw_weak_refs(bool full_gc);
608 
609   inline void assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
610                                           ShenandoahAffiliation new_affiliation);
611 
612   // Heap iteration support
613   void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
614   bool prepare_aux_bitmap_for_iteration();
615   void reclaim_aux_bitmap_for_iteration();
616 
617 // ---------- Generic interface hooks
618 // Minor things that super-interface expects us to implement to play nice with
619 // the rest of runtime. Some of the things here are not required to be implemented,
620 // and can be stubbed out.
621 //
622 public:
623   bool is_maximal_no_gc() const override shenandoah_not_implemented_return(false);
624 
625   inline bool is_in(const void* p) const override;
626 
627   inline bool is_in_active_generation(oop obj) const;
628   inline bool is_in_young(const void* p) const;
629   inline bool is_in_old(const void* p) const;
630   inline bool is_old(oop pobj) const;
631 
632   inline ShenandoahAffiliation region_affiliation(const ShenandoahHeapRegion* r);
633   inline void set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation);
634 
635   inline ShenandoahAffiliation region_affiliation(size_t index);
636 
637   bool requires_barriers(stackChunkOop obj) const override;
638 
639   MemRegion reserved_region() const { return _reserved; }
640   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
641 
642   void collect(GCCause::Cause cause) override;
643   void do_full_collection(bool clear_all_soft_refs) override;
644 
645   // Used for parsing heap during error printing
646   HeapWord* block_start(const void* addr) const;
647   bool block_is_obj(const HeapWord* addr) const;
648   bool print_location(outputStream* st, void* addr) const override;
649 
650   // Used for native heap walkers: heap dumpers, mostly
651   void object_iterate(ObjectClosure* cl) override;
652   // Parallel heap iteration support
653   ParallelObjectIteratorImpl* parallel_object_iterator(uint workers) override;
654 
655   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.

669   void verify_nmethod(nmethod* nm) override {}
670 
671 // ---------- Pinning hooks
672 //
673 public:
674   // Shenandoah supports per-object (per-region) pinning
675   void pin_object(JavaThread* thread, oop obj) override;
676   void unpin_object(JavaThread* thread, oop obj) override;
677 
678   void sync_pinned_region_status();
679   void assert_pinned_region_status() NOT_DEBUG_RETURN;
680 
681 // ---------- Concurrent Stack Processing support
682 //
683 public:
684   bool uses_stack_watermark_barrier() const override { return true; }
685 
686 // ---------- Allocation support
687 //
688 private:
689   // How many bytes to transfer between old and young after we have finished recycling collection set regions?
690   size_t _old_regions_surplus;
691   size_t _old_regions_deficit;
692 
693   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region, bool is_promotion);
694 
695   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
696   HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
697   HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
698 
699   inline HeapWord* allocate_from_plab(Thread* thread, size_t size, bool is_promotion);
700   HeapWord* allocate_from_plab_slow(Thread* thread, size_t size, bool is_promotion);
701   HeapWord* allocate_new_plab(size_t min_size, size_t word_size, size_t* actual_size);
702 
703 public:
704   HeapWord* allocate_memory(ShenandoahAllocRequest& request, bool is_promotion);
705   HeapWord* mem_allocate(size_t size, bool* what) override;
706   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
707                                                size_t size,
708                                                Metaspace::MetadataType mdtype) override;
709 
710   void notify_mutator_alloc_words(size_t words, size_t waste);
711 
712   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override;
713   size_t tlab_capacity(Thread *thr) const override;
714   size_t unsafe_max_tlab_alloc(Thread *thread) const override;
715   size_t max_tlab_size() const override;
716   size_t tlab_used(Thread* ignored) const override;
717 
718   void ensure_parsability(bool retire_labs) override;
719 
720   void labs_make_parsable();
721   void tlabs_retire(bool resize);
722   void gclabs_retire(bool resize);
723 
724   inline void set_old_region_surplus(size_t surplus) { _old_regions_surplus = surplus; };
725   inline void set_old_region_deficit(size_t deficit) { _old_regions_deficit = deficit; };
726 
727   inline size_t get_old_region_surplus() { return _old_regions_surplus; };
728   inline size_t get_old_region_deficit() { return _old_regions_deficit; };
729 
730 // ---------- Marking support
731 //
732 private:
733   ShenandoahMarkingContext* _marking_context;
734   MemRegion  _bitmap_region;
735   MemRegion  _aux_bitmap_region;
736   MarkBitMap _verification_bit_map;
737   MarkBitMap _aux_bit_map;
738 
739   size_t _bitmap_size;
740   size_t _bitmap_regions_per_slice;
741   size_t _bitmap_bytes_per_slice;
742 
743   size_t _pretouch_heap_page_size;
744   size_t _pretouch_bitmap_page_size;
745 
746   bool _bitmap_region_special;
747   bool _aux_bitmap_region_special;
748 
749   ShenandoahLiveData** _liveness_cache;
750 
751 public:
752   inline ShenandoahMarkingContext* complete_marking_context() const;
753   inline ShenandoahMarkingContext* marking_context() const;


754 
755   template<class T>
756   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
757 
758   template<class T>
759   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
760 
761   template<class T>
762   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
763 


764   // SATB barriers hooks
765   inline bool requires_marking(const void* entry) const;
766 
767   // Support for bitmap uncommits
768   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
769   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
770   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
771 
772   // Liveness caching support
773   ShenandoahLiveData* get_liveness_cache(uint worker_id);
774   void flush_liveness_cache(uint worker_id);
775 
776   size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
777 
778 // ---------- Evacuation support
779 //
780 private:
781   ShenandoahCollectionSet* _collection_set;
782   ShenandoahEvacOOMHandler _oom_evac_handler;
783   ShenandoahSharedFlag _old_gen_oom_evac;
784 
785   inline oop try_evacuate_object(oop src, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
786   void handle_old_evacuation(HeapWord* obj, size_t words, bool promotion);
787   void handle_old_evacuation_failure();
788 
789 public:
790   void report_promotion_failure(Thread* thread, size_t size);
791 
792   static address in_cset_fast_test_addr();
793 
794   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
795 
796   // Checks if object is in the collection set.
797   inline bool in_collection_set(oop obj) const;
798 
799   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
800   inline bool in_collection_set_loc(void* loc) const;
801 
802   // Evacuates or promotes object src. Returns the evacuated object, either evacuated
803   // by this thread, or by some other thread.
804   inline oop evacuate_object(oop src, Thread* thread);
805 
806   // Call before/after evacuation.
807   inline void enter_evacuation(Thread* t);
808   inline void leave_evacuation(Thread* t);
809 
810   inline bool clear_old_evacuation_failure();
811 
812 // ---------- Generational support
813 //
814 private:
815   RememberedScanner* _card_scan;
816 
817 public:
818   inline RememberedScanner* card_scan() { return _card_scan; }
819   void clear_cards_for(ShenandoahHeapRegion* region);
820   void mark_card_as_dirty(void* location);
821   void retire_plab(PLAB* plab);
822   void retire_plab(PLAB* plab, Thread* thread);
823   void cancel_old_gc();
824 
825   void adjust_generation_sizes_for_next_cycle(size_t old_xfer_limit, size_t young_cset_regions, size_t old_cset_regions);
826 
827 // ---------- Helper functions
828 //
829 public:
830   template <class T>
831   inline void conc_update_with_forwarded(T* p);
832 
833   template <class T>
834   inline void update_with_forwarded(T* p);
835 
836   static inline void atomic_update_oop(oop update,       oop* addr,       oop compare);
837   static inline void atomic_update_oop(oop update, narrowOop* addr,       oop compare);
838   static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
839 
840   static inline bool atomic_update_oop_check(oop update,       oop* addr,       oop compare);
841   static inline bool atomic_update_oop_check(oop update, narrowOop* addr,       oop compare);
842   static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
843 
844   static inline void atomic_clear_oop(      oop* addr,       oop compare);
845   static inline void atomic_clear_oop(narrowOop* addr,       oop compare);
846   static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
847 
848   size_t trash_humongous_region_at(ShenandoahHeapRegion *r);
849 
850   static inline void increase_object_age(oop obj, uint additional_age);
851 
852   // Return the object's age, or a sentinel value when the age can't
853   // necessarily be determined because of concurrent locking by the
854   // mutator
855   static inline uint get_object_age(oop obj);
856 
857   void transfer_old_pointers_from_satb();
858 
859   void log_heap_status(const char *msg) const;
860 
861 private:
862   void trash_cset_regions();
863 
864 // ---------- Testing helpers functions
865 //
866 private:
867   ShenandoahSharedFlag _inject_alloc_failure;
868 
869   void try_inject_alloc_failure();
870   bool should_inject_alloc_failure();
871 };
872 
873 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
< prev index next >