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/shenandoahController.hpp"
 40 #include "gc/shenandoah/shenandoahLock.hpp"
 41 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
 42 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 43 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 44 #include "gc/shenandoah/shenandoahMmuTracker.hpp"
 45 #include "gc/shenandoah/shenandoahPadding.hpp"
 46 #include "gc/shenandoah/shenandoahScanRemembered.hpp"
 47 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 48 #include "gc/shenandoah/shenandoahUnload.hpp"
 49 #include "memory/metaspace.hpp"
 50 #include "services/memoryManager.hpp"
 51 #include "utilities/globalDefinitions.hpp"
 52 #include "utilities/stack.hpp"
 53 
 54 class ConcurrentGCTimer;
 55 class ObjectIterateScanRootClosure;
 56 class PLAB;
 57 class ShenandoahCollectorPolicy;
 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 
 98   shenandoah_padding(0);
 99   volatile size_t _index;
100   shenandoah_padding(1);
101 
102   // No implicit copying: iterators should be passed by reference to capture the state
103   NONCOPYABLE(ShenandoahRegionIterator);
104 
105 public:
106   ShenandoahRegionIterator();
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 
153   // Supported GC
154   friend class ShenandoahConcurrentGC;
155   friend class ShenandoahOldGC;
156   friend class ShenandoahDegenGC;
157   friend class ShenandoahFullGC;
158   friend class ShenandoahUnload;
159 
160 // ---------- Locks that guard important data structures in Heap
161 //
162 private:
163   ShenandoahHeapLock _lock;
164   ShenandoahGeneration* _gc_generation;
165 
166 public:
167   ShenandoahHeapLock* lock() {
168     return &_lock;
169   }
170 
171   ShenandoahGeneration* active_generation() const {
172     // last or latest generation might be a better name here.
173     return _gc_generation;
174   }
175 
176   void set_gc_generation(ShenandoahGeneration* generation) {
177     _gc_generation = generation;
178   }
179 
180   ShenandoahHeuristics* heuristics();
181   ShenandoahOldHeuristics* old_heuristics();
182   ShenandoahYoungHeuristics* young_heuristics();
183 
184   bool doing_mixed_evacuations();
185   bool is_old_bitmap_stable() const;
186   bool is_gc_generation_young() const;
187 
188 // ---------- Initialization, termination, identification, printing routines
189 //
190 public:
191   static ShenandoahHeap* heap();
192 
193   const char* name()          const override { return "Shenandoah"; }
194   ShenandoahHeap::Name kind() const override { return CollectedHeap::Shenandoah; }
195 
196   ShenandoahHeap(ShenandoahCollectorPolicy* policy);
197   jint initialize() override;
198   void post_initialize() override;
199   void initialize_heuristics_generations();
200   virtual void print_init_logger() const;

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

229   volatile size_t _committed;

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

242 

243   void reset_bytes_allocated_since_gc_start();
244 
245   size_t min_capacity()      const;
246   size_t max_capacity()      const override;
247   size_t soft_max_capacity() const;
248   size_t initial_capacity()  const;
249   size_t capacity()          const override;
250   size_t used()              const override;
251   size_t committed()         const;
252 
253   void set_soft_max_capacity(size_t v);
254 
255 // ---------- Periodic Tasks
256 //
257 private:
258   void notify_heap_changed();
259 
260 public:
261   void set_forced_counters_update(bool value);
262   void handle_force_counters_update();
263 
264 // ---------- Workers handling
265 //
266 private:
267   uint _max_workers;
268   ShenandoahWorkerThreads* _workers;
269   ShenandoahWorkerThreads* _safepoint_workers;
270 
271   virtual void initialize_controller();
272 
273 public:
274   uint max_workers();
275   void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
276 
277   WorkerThreads* workers() const;
278   WorkerThreads* safepoint_workers() override;
279 
280   void gc_threads_do(ThreadClosure* tcl) const override;
281 
282 // ---------- Heap regions handling machinery
283 //
284 private:
285   MemRegion _heap_region;
286   bool      _heap_region_special;
287   size_t    _num_regions;
288   ShenandoahHeapRegion** _regions;
289   uint8_t* _affiliations;       // Holds array of enum ShenandoahAffiliation, including FREE status in non-generational mode
290   ShenandoahRegionIterator _update_refs_iterator;
291 
292 public:
293 
294   inline HeapWord* base() const { return _heap_region.start(); }
295 
296   inline size_t num_regions() const { return _num_regions; }
297   inline bool is_heap_region_special() { return _heap_region_special; }
298 
299   inline ShenandoahHeapRegion* heap_region_containing(const void* addr) const;
300   inline size_t heap_region_index_containing(const void* addr) const;
301 
302   inline ShenandoahHeapRegion* get_region(size_t region_idx) const;
303 
304   void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
305   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
306 
307   inline ShenandoahMmuTracker* mmu_tracker() { return &_mmu_tracker; };
308 
309 // ---------- GC state machinery
310 //
311 // GC state describes the important parts of collector state, that may be
312 // used to make barrier selection decisions in the native and generated code.
313 // Multiple bits can be set at once.
314 //
315 // Important invariant: when GC state is zero, the heap is stable, and no barriers
316 // are required.
317 //
318 public:
319   enum GCStateBitPos {
320     // Heap has forwarded objects: needs LRB barriers.
321     HAS_FORWARDED_BITPOS   = 0,
322 
323     // Heap is under marking: needs SATB barriers.
324     // For generational mode, it means either young or old marking, or both.
325     MARKING_BITPOS    = 1,
326 
327     // Heap is under evacuation: needs LRB barriers. (Set together with HAS_FORWARDED)
328     EVACUATION_BITPOS = 2,
329 
330     // Heap is under updating: needs no additional barriers.
331     UPDATEREFS_BITPOS = 3,
332 
333     // Heap is under weak-reference/roots processing: needs weak-LRB barriers.
334     WEAK_ROOTS_BITPOS  = 4,
335 
336     // Young regions are under marking, need SATB barriers.
337     YOUNG_MARKING_BITPOS = 5,
338 
339     // Old regions are under marking, need SATB barriers.
340     OLD_MARKING_BITPOS = 6
341   };
342 
343   enum GCState {
344     STABLE        = 0,
345     HAS_FORWARDED = 1 << HAS_FORWARDED_BITPOS,
346     MARKING       = 1 << MARKING_BITPOS,
347     EVACUATION    = 1 << EVACUATION_BITPOS,
348     UPDATEREFS    = 1 << UPDATEREFS_BITPOS,
349     WEAK_ROOTS    = 1 << WEAK_ROOTS_BITPOS,
350     YOUNG_MARKING = 1 << YOUNG_MARKING_BITPOS,
351     OLD_MARKING   = 1 << OLD_MARKING_BITPOS
352   };
353 
354 private:
355   bool _gc_state_changed;
356   ShenandoahSharedBitmap _gc_state;
357   ShenandoahSharedFlag   _heap_changed;
358   ShenandoahSharedFlag   _degenerated_gc_in_progress;
359   ShenandoahSharedFlag   _full_gc_in_progress;
360   ShenandoahSharedFlag   _full_gc_move_in_progress;

361   ShenandoahSharedFlag   _concurrent_strong_root_in_progress;
362 
363   size_t _gc_no_progress_count;
364 
365   // This updates the singlular, global gc state. This must happen on a safepoint.
366   void set_gc_state(uint mask, bool value);
367 
368   ShenandoahAgeCensus* _age_census;    // Age census used for adapting tenuring threshold in generational mode
369 
370 public:
371   char gc_state() const;
372 
373   // This copies the global gc state into a thread local variable for java threads.
374   // It is primarily intended to support quick access at barriers.
375   void propagate_gc_state_to_java_threads();
376 
377   // This is public to support assertions that the state hasn't been changed off of
378   // a safepoint and that any changes were propagated to java threads after the safepoint.
379   bool has_gc_state_changed() const { return _gc_state_changed; }
380 
381   // Returns true if allocations have occurred in new regions or if regions have been
382   // uncommitted since the previous calls. This call will reset the flag to false.
383   bool has_changed() {
384     return _heap_changed.try_unset();
385   }
386 
387   void set_concurrent_young_mark_in_progress(bool in_progress);
388   void set_concurrent_old_mark_in_progress(bool in_progress);
389   void set_evacuation_in_progress(bool in_progress);
390   void set_update_refs_in_progress(bool in_progress);
391   void set_degenerated_gc_in_progress(bool in_progress);
392   void set_full_gc_in_progress(bool in_progress);
393   void set_full_gc_move_in_progress(bool in_progress);
394   void set_has_forwarded_objects(bool cond);
395   void set_concurrent_strong_root_in_progress(bool cond);
396   void set_concurrent_weak_root_in_progress(bool cond);
397 
398   void set_aging_cycle(bool cond);
399 
400   inline bool is_stable() const;
401   inline bool is_idle() const;
402 
403   inline bool is_concurrent_mark_in_progress() const;
404   inline bool is_concurrent_young_mark_in_progress() const;
405   inline bool is_concurrent_old_mark_in_progress() const;
406   inline bool is_update_refs_in_progress() const;
407   inline bool is_evacuation_in_progress() const;
408   inline bool is_degenerated_gc_in_progress() const;
409   inline bool is_full_gc_in_progress() const;
410   inline bool is_full_gc_move_in_progress() const;
411   inline bool has_forwarded_objects() const;
412 
413   inline bool is_stw_gc_in_progress() const;
414   inline bool is_concurrent_strong_root_in_progress() const;
415   inline bool is_concurrent_weak_root_in_progress() const;
416   bool is_prepare_for_old_mark_in_progress() const;
417   inline bool is_aging_cycle() const;
418 
419   // Return the age census object for young gen (in generational mode)
420   inline ShenandoahAgeCensus* age_census() const;
421 
422 private:
423   void manage_satb_barrier(bool active);
424 
425   enum CancelState {
426     // Normal state. GC has not been cancelled and is open for cancellation.
427     // Worker threads can suspend for safepoint.
428     CANCELLABLE,
429 
430     // GC has been cancelled. Worker threads can not suspend for
431     // safepoint but must finish their work as soon as possible.
432     CANCELLED
433   };
434 
435   double _cancel_requested_time;
436   ShenandoahSharedEnumFlag<CancelState> _cancelled_gc;
437 
438   // Returns true if cancel request was successfully communicated.
439   // Returns false if some other thread already communicated cancel
440   // request.  A true return value does not mean GC has been
441   // cancelled, only that the process of cancelling GC has begun.
442   bool try_cancel_gc();
443 
444 public:

445   inline bool cancelled_gc() const;
446   inline bool check_cancelled_gc_and_yield(bool sts_active = true);
447 
448   inline void clear_cancelled_gc(bool clear_oom_handler = true);
449 
450   void cancel_concurrent_mark();
451   void cancel_gc(GCCause::Cause cause);
452 
453 public:
454   // These will uncommit empty regions if heap::committed > shrink_until
455   // and there exists at least one region which was made empty before shrink_before.
456   void maybe_uncommit(double shrink_before, size_t shrink_until);
457   void op_uncommit(double shrink_before, size_t shrink_until);
458 
459   // Returns true if the soft maximum heap has been changed using management APIs.
460   bool check_soft_max_changed();
461 
462 private:
463   // GC support



464   // Evacuation
465   void evacuate_collection_set(bool concurrent);
466   // Concurrent root processing
467   void prepare_concurrent_roots();
468   void finish_concurrent_roots();
469   // Concurrent class unloading support
470   void do_class_unloading();
471   // Reference updating
472   void prepare_update_heap_references(bool concurrent);
473   void update_heap_references(bool concurrent);
474   // Final update region states
475   void update_heap_region_states(bool concurrent);

476 
477   void rendezvous_threads();
478   void recycle_trash();
479 public:
480   void rebuild_free_set(bool concurrent);
481   void notify_gc_progress();
482   void notify_gc_no_progress();
483   size_t get_gc_no_progress_count() const;
484 
485 //
486 // Mark support
487 private:
488   ShenandoahYoungGeneration* _young_generation;
489   ShenandoahGeneration*      _global_generation;
490   ShenandoahOldGeneration*   _old_generation;
491 
492 protected:
493   ShenandoahController*  _control_thread;
494 
495 private:
496   ShenandoahCollectorPolicy* _shenandoah_policy;
497   ShenandoahMode*            _gc_mode;

498   ShenandoahFreeSet*         _free_set;
499   ShenandoahPacer*           _pacer;
500   ShenandoahVerifier*        _verifier;
501 
502   ShenandoahPhaseTimings*       _phase_timings;
503   ShenandoahEvacuationTracker*  _evac_tracker;
504   ShenandoahMmuTracker          _mmu_tracker;
505   ShenandoahGenerationSizer     _generation_sizer;
506 
507 public:
508   ShenandoahController*   control_thread() { return _control_thread; }
509 
510   ShenandoahYoungGeneration* young_generation()  const { return _young_generation;  }
511   ShenandoahGeneration*      global_generation() const { return _global_generation; }
512   ShenandoahOldGeneration*   old_generation()    const { return _old_generation;    }
513   ShenandoahGeneration*      generation_for(ShenandoahAffiliation affiliation) const;
514   const ShenandoahGenerationSizer* generation_sizer()  const { return &_generation_sizer;  }
515 
516   ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
517   ShenandoahMode*            mode()              const { return _gc_mode;           }

518   ShenandoahFreeSet*         free_set()          const { return _free_set;          }
519   ShenandoahPacer*           pacer()             const { return _pacer;             }
520 
521   ShenandoahPhaseTimings*      phase_timings()   const { return _phase_timings;     }
522   ShenandoahEvacuationTracker* evac_tracker()    const { return _evac_tracker;      }
523 
524   void on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation);
525   void on_cycle_end(ShenandoahGeneration* generation);
526 
527   ShenandoahVerifier*        verifier();
528 
529 // ---------- VM subsystem bindings
530 //
531 private:
532   ShenandoahMonitoringSupport* _monitoring_support;
533   MemoryPool*                  _memory_pool;
534   GCMemoryManager              _stw_memory_manager;
535   GCMemoryManager              _cycle_memory_manager;
536   ConcurrentGCTimer*           _gc_timer;
537   SoftRefPolicy                _soft_ref_policy;
538 
539   // For exporting to SA
540   int                          _log_min_obj_alignment_in_bytes;
541 public:
542   ShenandoahMonitoringSupport* monitoring_support() const    { return _monitoring_support;    }
543   GCMemoryManager* cycle_memory_manager()                    { return &_cycle_memory_manager; }
544   GCMemoryManager* stw_memory_manager()                      { return &_stw_memory_manager;   }
545   SoftRefPolicy* soft_ref_policy()                  override { return &_soft_ref_policy;      }
546 
547   GrowableArray<GCMemoryManager*> memory_managers() override;
548   GrowableArray<MemoryPool*> memory_pools() override;
549   MemoryUsage memory_usage() override;
550   GCTracer* tracer();
551   ConcurrentGCTimer* gc_timer() const;
552 








553 // ---------- Class Unloading
554 //
555 private:
556   ShenandoahSharedFlag  _is_aging_cycle;
557   ShenandoahSharedFlag _unload_classes;
558   ShenandoahUnload     _unloader;
559 
560 public:
561   void set_unload_classes(bool uc);
562   bool unload_classes() const;
563 
564   // Perform STW class unloading and weak root cleaning
565   void parallel_cleaning(bool full_gc);
566 
567 private:
568   void stw_unload_classes(bool full_gc);
569   void stw_process_weak_roots(bool full_gc);
570   void stw_weak_refs(bool full_gc);
571 
572   inline void assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
573                                           ShenandoahAffiliation new_affiliation);
574 
575   // Heap iteration support
576   void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
577   bool prepare_aux_bitmap_for_iteration();
578   void reclaim_aux_bitmap_for_iteration();
579 
580 // ---------- Generic interface hooks
581 // Minor things that super-interface expects us to implement to play nice with
582 // the rest of runtime. Some of the things here are not required to be implemented,
583 // and can be stubbed out.
584 //
585 public:
586   bool is_maximal_no_gc() const override shenandoah_not_implemented_return(false);
587 
588   inline bool is_in(const void* p) const override;
589 
590   inline bool is_in_active_generation(oop obj) const;
591   inline bool is_in_young(const void* p) const;
592   inline bool is_in_old(const void* p) const;
593   inline bool is_old(oop pobj) const;
594 
595   inline ShenandoahAffiliation region_affiliation(const ShenandoahHeapRegion* r);
596   inline void set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation);
597 
598   inline ShenandoahAffiliation region_affiliation(size_t index);
599 
600   bool requires_barriers(stackChunkOop obj) const override;
601 
602   MemRegion reserved_region() const { return _reserved; }
603   bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
604 
605   void collect(GCCause::Cause cause) override;
606   void do_full_collection(bool clear_all_soft_refs) override;
607 
608   // Used for parsing heap during error printing
609   HeapWord* block_start(const void* addr) const;
610   bool block_is_obj(const HeapWord* addr) const;
611   bool print_location(outputStream* st, void* addr) const override;
612 
613   // Used for native heap walkers: heap dumpers, mostly
614   void object_iterate(ObjectClosure* cl) override;
615   // Parallel heap iteration support
616   ParallelObjectIteratorImpl* parallel_object_iterator(uint workers) override;
617 
618   // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
619   void keep_alive(oop obj) override;
620 
621 // ---------- Safepoint interface hooks
622 //
623 public:
624   void safepoint_synchronize_begin() override;
625   void safepoint_synchronize_end() override;
626 
627 // ---------- Code roots handling hooks
628 //
629 public:
630   void register_nmethod(nmethod* nm) override;
631   void unregister_nmethod(nmethod* nm) override;
632   void verify_nmethod(nmethod* nm) override {}
633 
634 // ---------- Pinning hooks
635 //
636 public:
637   // Shenandoah supports per-object (per-region) pinning
638   void pin_object(JavaThread* thread, oop obj) override;
639   void unpin_object(JavaThread* thread, oop obj) override;
640 
641   void sync_pinned_region_status();
642   void assert_pinned_region_status() NOT_DEBUG_RETURN;
643 
644 // ---------- Concurrent Stack Processing support
645 //
646 public:
647   bool uses_stack_watermark_barrier() const override { return true; }
648 
649 // ---------- Allocation support
650 //
651 private:
652   HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region, bool is_promotion);
653 
654   inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
655   HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
656   HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
657 
658   inline HeapWord* allocate_from_plab(Thread* thread, size_t size, bool is_promotion);
659   HeapWord* allocate_from_plab_slow(Thread* thread, size_t size, bool is_promotion);
660   HeapWord* allocate_new_plab(size_t min_size, size_t word_size, size_t* actual_size);
661 
662 public:
663   HeapWord* allocate_memory(ShenandoahAllocRequest& request, bool is_promotion);
664   HeapWord* mem_allocate(size_t size, bool* what) override;
665   MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
666                                                size_t size,
667                                                Metaspace::MetadataType mdtype) override;
668 
669   void notify_mutator_alloc_words(size_t words, size_t waste);
670 
671   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override;
672   size_t tlab_capacity(Thread *thr) const override;
673   size_t unsafe_max_tlab_alloc(Thread *thread) const override;
674   size_t max_tlab_size() const override;
675   size_t tlab_used(Thread* ignored) const override;
676 
677   void ensure_parsability(bool retire_labs) override;
678 
679   void labs_make_parsable();
680   void tlabs_retire(bool resize);
681   void gclabs_retire(bool resize);
682 
683 // ---------- Marking support
684 //
685 private:
686   ShenandoahMarkingContext* _marking_context;
687   MemRegion  _bitmap_region;
688   MemRegion  _aux_bitmap_region;
689   MarkBitMap _verification_bit_map;
690   MarkBitMap _aux_bit_map;
691 
692   size_t _bitmap_size;
693   size_t _bitmap_regions_per_slice;
694   size_t _bitmap_bytes_per_slice;
695 
696   size_t _pretouch_heap_page_size;
697   size_t _pretouch_bitmap_page_size;
698 
699   bool _bitmap_region_special;
700   bool _aux_bitmap_region_special;
701 
702   ShenandoahLiveData** _liveness_cache;
703 
704 public:
705   inline ShenandoahMarkingContext* complete_marking_context() const;
706   inline ShenandoahMarkingContext* marking_context() const;


707 
708   template<class T>
709   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
710 
711   template<class T>
712   inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
713 
714   template<class T>
715   inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
716 


717   // SATB barriers hooks
718   inline bool requires_marking(const void* entry) const;
719 
720   // Support for bitmap uncommits
721   bool commit_bitmap_slice(ShenandoahHeapRegion *r);
722   bool uncommit_bitmap_slice(ShenandoahHeapRegion *r);
723   bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
724 
725   // Liveness caching support
726   ShenandoahLiveData* get_liveness_cache(uint worker_id);
727   void flush_liveness_cache(uint worker_id);
728 
729   size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
730 
731 // ---------- Evacuation support
732 //
733 private:
734   ShenandoahCollectionSet* _collection_set;
735   ShenandoahEvacOOMHandler _oom_evac_handler;
736 
737   oop try_evacuate_object(oop src, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
738 public:
739 
740   static address in_cset_fast_test_addr();
741 
742   ShenandoahCollectionSet* collection_set() const { return _collection_set; }
743 
744   // Checks if object is in the collection set.
745   inline bool in_collection_set(oop obj) const;
746 
747   // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
748   inline bool in_collection_set_loc(void* loc) const;
749 
750   // Evacuates or promotes object src. Returns the evacuated object, either evacuated
751   // by this thread, or by some other thread.
752   inline oop evacuate_object(oop src, Thread* thread);
753 
754   // Call before/after evacuation.
755   inline void enter_evacuation(Thread* t);
756   inline void leave_evacuation(Thread* t);
757 
758 // ---------- Generational support
759 //
760 private:
761   RememberedScanner* _card_scan;
762 
763 public:
764   inline RememberedScanner* card_scan() { return _card_scan; }
765   void clear_cards_for(ShenandoahHeapRegion* region);
766   void mark_card_as_dirty(void* location);
767   void retire_plab(PLAB* plab);
768   void retire_plab(PLAB* plab, Thread* thread);
769   void cancel_old_gc();
770 
771 // ---------- Helper functions
772 //
773 public:
774   template <class T>
775   inline void conc_update_with_forwarded(T* p);
776 
777   template <class T>
778   inline void update_with_forwarded(T* p);
779 
780   static inline void atomic_update_oop(oop update,       oop* addr,       oop compare);
781   static inline void atomic_update_oop(oop update, narrowOop* addr,       oop compare);
782   static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
783 
784   static inline bool atomic_update_oop_check(oop update,       oop* addr,       oop compare);
785   static inline bool atomic_update_oop_check(oop update, narrowOop* addr,       oop compare);
786   static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
787 
788   static inline void atomic_clear_oop(      oop* addr,       oop compare);
789   static inline void atomic_clear_oop(narrowOop* addr,       oop compare);
790   static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
791 
792   size_t trash_humongous_region_at(ShenandoahHeapRegion *r);
793 
794   static inline void increase_object_age(oop obj, uint additional_age);
795 
796   // Return the object's age, or a sentinel value when the age can't
797   // necessarily be determined because of concurrent locking by the
798   // mutator
799   static inline uint get_object_age(oop obj);
800 
801   void transfer_old_pointers_from_satb();
802 
803   void log_heap_status(const char *msg) const;
804 
805 private:
806   void trash_cset_regions();
807 
808 // ---------- Testing helpers functions
809 //
810 private:
811   ShenandoahSharedFlag _inject_alloc_failure;
812 
813   void try_inject_alloc_failure();
814   bool should_inject_alloc_failure();
815 };
816 
817 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP
--- EOF ---