1 /*
2 * Copyright (c) 2023, 2025, 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/collectedHeap.hpp"
31 #include "gc/shared/markBitMap.hpp"
32 #include "gc/shenandoah/mode/shenandoahMode.hpp"
33 #include "gc/shenandoah/shenandoahAllocRate.hpp"
34 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
35 #include "gc/shenandoah/shenandoahAsserts.hpp"
36 #include "gc/shenandoah/shenandoahController.hpp"
37 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
38 #include "gc/shenandoah/shenandoahGenerationType.hpp"
39 #include "gc/shenandoah/shenandoahLock.hpp"
40 #include "gc/shenandoah/shenandoahMmuTracker.hpp"
41 #include "gc/shenandoah/shenandoahPadding.hpp"
42 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
43 #include "gc/shenandoah/shenandoahUnload.hpp"
44 #include "memory/metaspace.hpp"
45 #include "services/memoryManager.hpp"
46 #include "utilities/globalDefinitions.hpp"
47 #include "utilities/stack.hpp"
48
49 class ConcurrentGCTimer;
50 class ObjectIterateScanRootClosure;
51 class ShenandoahCollectorPolicy;
52 class ShenandoahGCSession;
53 class ShenandoahGCStateResetter;
54 class ShenandoahGeneration;
55 class ShenandoahYoungGeneration;
56 class ShenandoahOldGeneration;
57 class ShenandoahHeuristics;
58 class ShenandoahMarkingContext;
59 class ShenandoahMode;
60 class ShenandoahPhaseTimings;
61 class ShenandoahHeap;
62 class ShenandoahHeapRegion;
63 class ShenandoahHeapRegionClosure;
64 class ShenandoahCollectionSet;
65 class ShenandoahFreeSet;
66 class ShenandoahConcurrentMark;
67 class ShenandoahFullGC;
68 class ShenandoahMonitoringSupport;
69 class ShenandoahReferenceProcessor;
70 class ShenandoahUncommitThread;
71 class ShenandoahVerifier;
72 class ShenandoahWorkerThreads;
73 class VMStructs;
74
75 // Used for buffering per-region liveness data.
76 // Needed since ShenandoahHeapRegion uses atomics to update liveness.
77 // The ShenandoahHeap array has max-workers elements, each of which is an array of
78 // uint16_t * max_regions. The choice of uint16_t is not accidental:
79 // there is a tradeoff between static/dynamic footprint that translates
80 // into cache pressure (which is already high during marking), and
81 // too many atomic updates. uint32_t is too large, uint8_t is too small.
82 typedef uint16_t ShenandoahLiveData;
83 #define SHENANDOAH_LIVEDATA_MAX ((ShenandoahLiveData)-1)
84
85 class ShenandoahRegionIterator : public StackObj {
86 private:
87 ShenandoahHeap* _heap;
88
89 shenandoah_padding(0);
90 Atomic<size_t> _index;
91 shenandoah_padding(1);
92
93 // No implicit copying: iterators should be passed by reference to capture the state
94 NONCOPYABLE(ShenandoahRegionIterator);
95
96 public:
97 ShenandoahRegionIterator();
98 ShenandoahRegionIterator(ShenandoahHeap* heap);
99
100 // Reset iterator to default state
101 void reset();
102
103 // Returns next region, or null if there are no more regions.
104 // This is multi-thread-safe.
105 inline ShenandoahHeapRegion* next();
106
107 // This is *not* MT safe. However, in the absence of multithreaded access, it
108 // can be used to determine if there is more work to do.
109 bool has_next() const;
110 };
111
112 class ShenandoahHeapRegionClosure : public StackObj {
113 public:
114 virtual void heap_region_do(ShenandoahHeapRegion* r) = 0;
115 virtual size_t parallel_region_stride() { return ShenandoahParallelRegionStride; }
116 virtual bool is_thread_safe() { return false; }
117 };
118
119 typedef ShenandoahLock ShenandoahHeapLock;
120 // ShenandoahHeapLocker implements locker to assure mutually exclusive access to the global heap data structures.
121 // Asserts in the implementation detect potential deadlock usage with regards the rebuild lock that is present
122 // in ShenandoahFreeSet. Whenever both locks are acquired, this lock should be acquired before the
123 // ShenandoahFreeSet rebuild lock.
124 class ShenandoahHeapLocker : public StackObj {
125 private:
126 ShenandoahHeapLock* _lock;
127 public:
128 ShenandoahHeapLocker(ShenandoahHeapLock* lock, bool allow_block_for_safepoint = false);
129
130 ~ShenandoahHeapLocker() {
131 _lock->unlock();
132 }
133 };
134
135 typedef Stack<oop, mtGC> ShenandoahScanObjectStack;
136
137 // Shenandoah GC is low-pause concurrent GC that uses a load reference barrier
138 // for concurent evacuation and a snapshot-at-the-beginning write barrier for
139 // concurrent marking. See ShenandoahControlThread for GC cycle structure.
140 //
141 class ShenandoahHeap : public CollectedHeap {
142 friend class ShenandoahAsserts;
143 friend class VMStructs;
144 friend class ShenandoahGCSession;
145 friend class ShenandoahGCStateResetter;
146 friend class ShenandoahParallelObjectIterator;
147 friend class ShenandoahSafepoint;
148
149 // Supported GC
150 friend class ShenandoahConcurrentGC;
151 friend class ShenandoahOldGC;
152 friend class ShenandoahDegenGC;
153 friend class ShenandoahFullGC;
154 friend class ShenandoahUnload;
155
156 // ---------- Locks that guard important data structures in Heap
157 //
158 private:
159 ShenandoahHeapLock _lock;
160
161 // This is set and cleared by only the VMThread
162 // at each STW pause (safepoint) to the value given to the VM operation.
163 // This allows the value to be always consistently
164 // seen by all mutators as well as all GC worker threads.
165 ShenandoahGeneration* _active_generation;
166
167 protected:
168 void print_tracing_info() const override;
169 void stop() override;
170
171 public:
172 ShenandoahHeapLock* lock() {
173 return &_lock;
174 }
175
176 ShenandoahGeneration* active_generation() const {
177 // value of _active_generation field, see above
178 return _active_generation;
179 }
180
181 // Update the _active_generation field: can only be called at a safepoint by the VMThread.
182 void set_active_generation(ShenandoahGeneration* generation);
183
184 ShenandoahHeuristics* heuristics();
185
186 // ---------- Initialization, termination, identification, printing routines
187 //
188 public:
189 static ShenandoahHeap* heap();
190
191 const char* name() const override { return "Shenandoah"; }
192 ShenandoahHeap::Name kind() const override { return CollectedHeap::Shenandoah; }
193
194 ShenandoahHeap(ShenandoahCollectorPolicy* policy);
195 jint initialize() override;
196 void post_initialize() override;
197 virtual void initialize_generations();
198 void initialize_mode();
199 virtual void initialize_heuristics();
200 virtual void post_initialize_heuristics();
201 virtual void print_init_logger() const;
202 void initialize_serviceability() override;
203
204 void print_heap_on(outputStream* st) const override;
205 void print_gc_on(outputStream* st) const override;
206 void print_heap_regions_on(outputStream* st) const;
207
208 // Flushes cycle timings to global timings and prints the phase timings for the last completed cycle.
209 void process_gc_stats() const;
210
211 void prepare_for_verify() override;
212 void verify(VerifyOption vo) override;
213
214 // WhiteBox testing support.
215 bool supports_concurrent_gc_breakpoints() const override {
216 return true;
217 }
218
219 // ---------- Heap counters and metrics
220 //
221 private:
222 size_t _initial_size;
223 size_t _minimum_size;
224
225 Atomic<size_t> _soft_max_size;
226 shenandoah_padding(0);
227 Atomic<size_t> _committed;
228 shenandoah_padding(1);
229
230 ShenandoahAllocationRate _alloc_rate;
231 ShenandoahDecayAllocRate _alloc_rate_decay;
232
233 public:
234 void increase_committed(size_t bytes);
235 void decrease_committed(size_t bytes);
236
237 size_t min_capacity() const;
238 size_t max_capacity() const override;
239 size_t soft_max_capacity() const;
240 size_t initial_capacity() const;
241 size_t capacity() const override;
242 size_t used() const override;
243 size_t committed() const;
244
245 void set_soft_max_capacity(size_t v);
246
247 ShenandoahAllocationRate& alloc_rate() {
248 return _alloc_rate;
249 }
250
251 // ---------- Periodic Tasks
252 //
253 public:
254 // Notify heuristics and region state change logger that the state of the heap has changed
255 void notify_heap_changed();
256
257 // Force counters to update
258 void set_forced_counters_update(bool value);
259
260 // Update counters if forced flag is set
261 void handle_force_counters_update();
262
263 // ---------- Workers handling
264 //
265 private:
266 uint _max_workers;
267 ShenandoahWorkerThreads* _workers;
268 ShenandoahWorkerThreads* _safepoint_workers;
269
270 virtual void initialize_controller();
271
272 public:
273 uint max_workers();
274 void assert_gc_workers(uint nworker) NOT_DEBUG_RETURN;
275
276 WorkerThreads* workers() const;
277 WorkerThreads* safepoint_workers() override;
278
279 void gc_threads_do(ThreadClosure* tcl) const override;
280
281 // ---------- Heap regions handling machinery
282 //
283 private:
284 MemRegion _heap_region;
285 bool _heap_region_special;
286 size_t _num_regions;
287 ShenandoahHeapRegion** _regions;
288 uint8_t* _affiliations; // Holds array of enum ShenandoahAffiliation, including FREE status in non-generational mode
289
290 public:
291
292 inline HeapWord* base() const { return _heap_region.start(); }
293 inline HeapWord* end() const { return _heap_region.end(); }
294
295 inline size_t num_regions() const { return _num_regions; }
296 inline bool is_heap_region_special() { return _heap_region_special; }
297
298 inline ShenandoahHeapRegion* heap_region_containing(const void* addr) const;
299 inline size_t heap_region_index_containing(const void* addr) const;
300
301 inline ShenandoahHeapRegion* get_region(size_t region_idx) const;
302
303 void heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
304 void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const;
305 void heap_region_iterator(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 UPDATE_REFS_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 UPDATE_REFS = 1 << UPDATE_REFS_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 Atomic<size_t> _gc_no_progress_count;
364
365 // This updates the singular, global gc state. This call must happen on a safepoint.
366 void set_gc_state_at_safepoint(uint mask, bool value);
367
368 // This also updates the global gc state, but does not need to be called on a safepoint.
369 // Critically, this method will _not_ flag that the global gc state has changed and threads
370 // will continue to use their thread local copy. This is expected to be used in conjunction
371 // with a handshake operation to propagate the new gc state.
372 void set_gc_state_concurrent(uint mask, bool value);
373
374 public:
375 // This returns the raw value of the singular, global gc state.
376 inline char gc_state() const;
377
378 // Compares the given state against either the global gc state, or the thread local state.
379 // The global gc state may change on a safepoint and is the correct value to use until
380 // the global gc state has been propagated to all threads (after which, this method will
381 // compare against the thread local state). The thread local gc state may also be changed
382 // by a handshake operation, in which case, this function continues using the updated thread
383 // local value.
384 inline bool is_gc_state(GCState state) const;
385
386 // This copies the global gc state into a thread local variable for all threads.
387 // The thread local gc state is primarily intended to support quick access at barriers.
388 // All threads are updated because in some cases the control thread or the vm thread may
389 // need to execute the load reference barrier.
390 void propagate_gc_state_to_all_threads();
391
392 // This is public to support assertions that the state hasn't been changed off of
393 // a safepoint and that any changes were propagated to threads after the safepoint.
394 bool has_gc_state_changed() const { return _gc_state_changed; }
395
396 // Returns true if allocations have occurred in new regions or if regions have been
397 // uncommitted since the previous calls. This call will reset the flag to false.
398 bool has_changed() {
399 return _heap_changed.try_unset();
400 }
401
402 virtual void start_idle_span();
403
404 void set_concurrent_young_mark_in_progress(bool in_progress);
405 void set_concurrent_old_mark_in_progress(bool in_progress);
406 void set_evacuation_in_progress(bool in_progress);
407 void set_update_refs_in_progress(bool in_progress);
408 void set_degenerated_gc_in_progress(bool in_progress);
409 void set_full_gc_in_progress(bool in_progress);
410 void set_full_gc_move_in_progress(bool in_progress);
411 void set_has_forwarded_objects(bool cond);
412 void set_concurrent_strong_root_in_progress(bool cond);
413 void set_concurrent_weak_root_in_progress(bool cond);
414
415 inline bool is_idle() const;
416 inline bool is_concurrent_mark_in_progress() const;
417 inline bool is_concurrent_young_mark_in_progress() const;
418 inline bool is_concurrent_old_mark_in_progress() const;
419 inline bool is_update_refs_in_progress() const;
420 inline bool is_evacuation_in_progress() const;
421 inline bool is_degenerated_gc_in_progress() const;
422 inline bool is_full_gc_in_progress() const;
423 inline bool is_full_gc_move_in_progress() const;
424 inline bool has_forwarded_objects() const;
425
426 inline bool is_stw_gc_in_progress() const;
427 inline bool is_concurrent_strong_root_in_progress() const;
428 inline bool is_concurrent_weak_root_in_progress() const;
429 bool is_prepare_for_old_mark_in_progress() const;
430
431 private:
432 void manage_satb_barrier(bool active);
433
434 // Records the time of the first successful cancellation request. This is used to measure
435 // the responsiveness of the heuristic when starting a cycle.
436 double _cancel_requested_time;
437
438 // Indicates the reason the current GC has been cancelled (GCCause::_no_gc means the gc is not cancelled).
439 ShenandoahSharedEnumFlag<GCCause::Cause> _cancelled_gc;
440
441 // Returns true if cancel request was successfully communicated.
442 // Returns false if some other thread already communicated cancel
443 // request. A true return value does not mean GC has been
444 // cancelled, only that the process of cancelling GC has begun.
445 bool try_cancel_gc(GCCause::Cause cause);
446
447 public:
448 // True if gc has been cancelled
449 inline bool cancelled_gc() const;
450
451 // Used by workers in the GC cycle to detect cancellation and honor STS requirements
452 inline bool check_cancelled_gc_and_yield(bool sts_active = true);
453
454 // This indicates the reason the last GC cycle was cancelled.
455 inline GCCause::Cause cancelled_cause() const;
456
457 // Clears the cancellation cause and resets the oom handler
458 inline void clear_cancelled_gc();
459
460 // Clears the cancellation cause iff the current cancellation reason equals the given
461 // expected cancellation cause. Does not reset the oom handler.
462 inline GCCause::Cause clear_cancellation(GCCause::Cause expected);
463
464 void cancel_concurrent_mark();
465
466 // Returns true if and only if this call caused a gc to be cancelled.
467 bool cancel_gc(GCCause::Cause cause);
468
469 // Returns true if the soft maximum heap has been changed using management APIs.
470 bool check_soft_max_changed();
471
472 protected:
473 // This is shared between shConcurrentGC and shDegenerateGC so that degenerated
474 // GC can resume update refs from where the concurrent GC was cancelled. It is
475 // also used in shGenerationalHeap, which uses a different closure for update refs.
476 ShenandoahRegionIterator _update_refs_iterator;
477
478 private:
479 inline void reset_cancellation_time();
480
481 // GC support
482 // Evacuation
483 virtual void evacuate_collection_set(ShenandoahGeneration* generation, bool concurrent);
484 // Concurrent root processing
485 void prepare_concurrent_roots();
486 void finish_concurrent_roots();
487 // Concurrent class unloading support
488 void do_class_unloading();
489 // Reference updating
490 void prepare_update_heap_references();
491
492 // Retires LABs used for evacuation
493 void concurrent_prepare_for_update_refs();
494
495 // Turn off weak roots flag, purge old satb buffers in generational mode
496 void op_final_roots(bool at_gc_end);
497
498 virtual void update_heap_references(ShenandoahGeneration* generation, bool concurrent);
499 // Final update region states
500 void update_heap_region_states(bool concurrent);
501 virtual void final_update_refs_update_region_states();
502
503 void rendezvous_threads(const char* name);
504 void recycle_trash();
505 public:
506 // The following two functions rebuild the free set at the end of GC, in preparation for an idle phase.
507 void rebuild_free_set(bool concurrent);
508 void rebuild_free_set_within_phase();
509 void notify_gc_progress();
510 void notify_gc_no_progress();
511 size_t get_gc_no_progress_count() const;
512
513 // The uncommit thread targets soft max heap, notify this thread when that value has changed.
514 void notify_soft_max_changed();
515
516 // An explicit GC request may have freed regions, notify the uncommit thread.
517 void notify_explicit_gc_requested();
518
519 private:
520 ShenandoahGeneration* _global_generation;
521
522 protected:
523 // The control thread presides over concurrent collection cycles
524 ShenandoahController* _control_thread;
525
526 // The uncommit thread periodically attempts to uncommit regions that have been empty for longer than ShenandoahUncommitDelay
527 ShenandoahUncommitThread* _uncommit_thread;
528
529 ShenandoahYoungGeneration* _young_generation;
530 ShenandoahOldGeneration* _old_generation;
531
532 private:
533 ShenandoahCollectorPolicy* _shenandoah_policy;
534 ShenandoahMode* _gc_mode;
535 ShenandoahFreeSet* _free_set;
536 ShenandoahVerifier* _verifier;
537
538 ShenandoahPhaseTimings* _phase_timings;
539 ShenandoahMmuTracker _mmu_tracker;
540
541 public:
542 ShenandoahController* control_thread() const { return _control_thread; }
543
544 ShenandoahGeneration* global_generation() const { return _global_generation; }
545 ShenandoahYoungGeneration* young_generation() const {
546 assert(mode()->is_generational(), "Young generation requires generational mode");
547 return _young_generation;
548 }
549
550 ShenandoahOldGeneration* old_generation() const {
551 assert(ShenandoahCardBarrier, "Card mark barrier should be on");
552 return _old_generation;
553 }
554
555 ShenandoahGeneration* generation_for(ShenandoahAffiliation affiliation) const;
556
557 ShenandoahCollectorPolicy* shenandoah_policy() const { return _shenandoah_policy; }
558 ShenandoahMode* mode() const { return _gc_mode; }
559 ShenandoahFreeSet* free_set() const { return _free_set; }
560
561 ShenandoahPhaseTimings* phase_timings() const { return _phase_timings; }
562
563 ShenandoahEvacuationTracker* evac_tracker() const {
564 return _evac_tracker;
565 }
566
567 void on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation, bool is_degenerated, bool is_out_of_cycle);
568 void on_cycle_end(ShenandoahGeneration* generation);
569
570 ShenandoahVerifier* verifier();
571
572 // ---------- VM subsystem bindings
573 //
574 private:
575 ShenandoahMonitoringSupport* _monitoring_support;
576 MemoryPool* _memory_pool;
577 GCMemoryManager _stw_memory_manager;
578 GCMemoryManager _cycle_memory_manager;
579 ConcurrentGCTimer* _gc_timer;
580 // For exporting to SA
581 int _log_min_obj_alignment_in_bytes;
582 public:
583 ShenandoahMonitoringSupport* monitoring_support() const { return _monitoring_support; }
584 GCMemoryManager* cycle_memory_manager() { return &_cycle_memory_manager; }
585 GCMemoryManager* stw_memory_manager() { return &_stw_memory_manager; }
586
587 GrowableArray<GCMemoryManager*> memory_managers() override;
588 GrowableArray<MemoryPool*> memory_pools() override;
589 MemoryUsage memory_usage() override;
590 GCTracer* tracer();
591 ConcurrentGCTimer* gc_timer() const;
592
593 // ---------- Class Unloading
594 //
595 private:
596 ShenandoahSharedFlag _unload_classes;
597 ShenandoahUnload _unloader;
598
599 public:
600 void set_unload_classes(bool uc);
601 bool unload_classes() const;
602
603 // Perform STW class unloading and weak root cleaning
604 void parallel_cleaning(ShenandoahGeneration* generation, bool full_gc);
605
606 private:
607 void stw_unload_classes(bool full_gc);
608 void stw_process_weak_roots(bool full_gc);
609 void stw_weak_refs(ShenandoahGeneration* generation, bool full_gc);
610
611 inline void assert_lock_for_affiliation(ShenandoahAffiliation orig_affiliation,
612 ShenandoahAffiliation new_affiliation);
613
614 // Heap iteration support
615 void scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops);
616 bool prepare_aux_bitmap_for_iteration();
617 void reclaim_aux_bitmap_for_iteration();
618
619 // ---------- Generic interface hooks
620 // Minor things that super-interface expects us to implement to play nice with
621 // the rest of runtime. Some of the things here are not required to be implemented,
622 // and can be stubbed out.
623 //
624 public:
625 // Check the pointer is in active part of Java heap.
626 // Use is_in_reserved to check if object is within heap bounds.
627 bool is_in(const void* p) const override;
628
629 // Returns true if the given oop belongs to a generation that is actively being collected.
630 inline bool is_in_active_generation(oop obj) const;
631 inline bool is_in_young(const void* p) const;
632 inline bool is_in_old(const void* p) const;
633
634 // Returns true iff the young generation is being collected and the given pointer
635 // is in the old generation. This is used to prevent the young collection from treating
636 // such an object as unreachable.
637 inline bool is_in_old_during_young_collection(oop obj) const;
638
639 inline ShenandoahAffiliation region_affiliation(const ShenandoahHeapRegion* r) const;
640 inline void set_affiliation(ShenandoahHeapRegion* r, ShenandoahAffiliation new_affiliation);
641
642 inline ShenandoahAffiliation region_affiliation(size_t index) const;
643
644 bool requires_barriers(stackChunkOop obj) const override;
645
646 MemRegion reserved_region() const { return _reserved; }
647 bool is_in_reserved(const void* addr) const { return _reserved.contains(addr); }
648
649 void collect_as_vm_thread(GCCause::Cause cause) override;
650 void collect(GCCause::Cause cause) override;
651 void do_full_collection(bool clear_all_soft_refs) override;
652
653 // Used for parsing heap during error printing
654 HeapWord* block_start(const void* addr) const;
655 bool block_is_obj(const HeapWord* addr) const;
656 bool print_location(outputStream* st, void* addr) const override;
657
658 // Used for native heap walkers: heap dumpers, mostly
659 void object_iterate(ObjectClosure* cl) override;
660 // Parallel heap iteration support
661 ParallelObjectIteratorImpl* parallel_object_iterator(uint workers) override;
662
663 // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
664 void keep_alive(oop obj) override;
665
666 // ---------- Safepoint interface hooks
667 //
668 public:
669 void safepoint_synchronize_begin() override;
670 void safepoint_synchronize_end() override;
671
672 // ---------- Code roots handling hooks
673 //
674 public:
675 void register_nmethod(nmethod* nm) override;
676 void unregister_nmethod(nmethod* nm) override;
677 void verify_nmethod(nmethod* nm) override {}
678
679 // ---------- Pinning hooks
680 //
681 public:
682 // Shenandoah supports per-object (per-region) pinning
683 void pin_object(JavaThread* thread, oop obj) override;
684 void unpin_object(JavaThread* thread, oop obj) override;
685
686 void sync_pinned_region_status();
687 void assert_pinned_region_status() const NOT_DEBUG_RETURN;
688 void assert_pinned_region_status(ShenandoahGeneration* generation) const NOT_DEBUG_RETURN;
689
690 // ---------- CDS archive support
691
692 bool can_load_archived_objects() const override { return true; }
693 HeapWord* allocate_loaded_archive_space(size_t size) override;
694 void complete_loaded_archive_space(MemRegion archive_space) override;
695
696 // ---------- Allocation support
697 //
698 protected:
699 inline HeapWord* allocate_from_gclab(Thread* thread, size_t size);
700
701 private:
702 HeapWord* allocate_memory_under_lock(ShenandoahAllocRequest& request, bool& in_new_region);
703 HeapWord* allocate_from_gclab_slow(Thread* thread, size_t size);
704 HeapWord* allocate_new_gclab(size_t min_size, size_t word_size, size_t* actual_size);
705
706 // We want to retry an unsuccessful attempt at allocation until at least a full gc.
707 bool should_retry_allocation(size_t original_full_gc_count) const;
708
709 public:
710 HeapWord* allocate_memory(ShenandoahAllocRequest& request);
711 HeapWord* mem_allocate(size_t size) override;
712 oop array_allocate(Klass* klass, size_t size, int length, bool do_zero, TRAPS) override;
713 MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
714 size_t size,
715 Metaspace::MetadataType mdtype) override;
716
717 HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override;
718 size_t tlab_capacity() const override;
719 size_t unsafe_max_tlab_alloc() const override;
720 size_t max_tlab_size() const override;
721 size_t tlab_used() const override;
722
723 void ensure_parsability(bool retire_labs) override;
724
725 void labs_make_parsable();
726 void tlabs_retire(bool resize);
727 void gclabs_retire(bool resize);
728
729 // ---------- Marking support
730 //
731 private:
732 ShenandoahMarkingContext* _marking_context;
733 MemRegion _bitmap_region;
734 MemRegion _aux_bitmap_region;
735 MarkBitMap _verification_bit_map;
736 MarkBitMap _aux_bit_map;
737
738 size_t _bitmap_size;
739 size_t _bitmap_regions_per_slice;
740 size_t _bitmap_bytes_per_slice;
741
742 size_t _pretouch_heap_page_size;
743 size_t _pretouch_bitmap_page_size;
744
745 bool _bitmap_region_special;
746 bool _aux_bitmap_region_special;
747
748 ShenandoahLiveData** _liveness_cache;
749
750 public:
751 // Return the marking context regardless of the completeness status.
752 inline ShenandoahMarkingContext* marking_context() const;
753
754 template<class T>
755 inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl);
756
757 template<class T>
758 inline void marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
759
760 template<class T>
761 inline void marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit);
762
763 // SATB barriers hooks
764 inline bool requires_marking(const void* entry) const;
765
766 // Support for bitmap uncommits
767 void commit_bitmap_slice(ShenandoahHeapRegion *r);
768 void uncommit_bitmap_slice(ShenandoahHeapRegion *r);
769 bool is_bitmap_region_special() { return _bitmap_region_special; }
770 bool is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self = false);
771
772 // During concurrent reset, the control thread will zero out the mark bitmaps for committed regions.
773 // This cannot happen when the uncommit thread is simultaneously trying to uncommit regions and their bitmaps.
774 // To prevent these threads from working at the same time, we provide these methods for the control thread to
775 // prevent the uncommit thread from working while a collection cycle is in progress.
776
777 // Forbid uncommits (will stop and wait if regions are being uncommitted)
778 void forbid_uncommit();
779
780 // Allow the uncommit thread to process regions
781 void allow_uncommit();
782 #ifdef ASSERT
783 bool is_uncommit_in_progress();
784 #endif
785
786 // Liveness caching support
787 ShenandoahLiveData* get_liveness_cache(uint worker_id);
788 void flush_liveness_cache(uint worker_id);
789
790 size_t pretouch_heap_page_size() { return _pretouch_heap_page_size; }
791
792 // ---------- Evacuation support
793 //
794 private:
795 ShenandoahCollectionSet* _collection_set;
796
797 oop try_evacuate_object(oop src, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
798
799 protected:
800 // Used primarily to look for failed evacuation attempts.
801 ShenandoahEvacuationTracker* _evac_tracker;
802
803 public:
804 static address in_cset_fast_test_addr();
805
806 ShenandoahCollectionSet* collection_set() const { return _collection_set; }
807
808 // Checks if object is in the collection set.
809 inline bool in_collection_set(oop obj) const;
810
811 // Checks if location is in the collection set. Can be interior pointer, not the oop itself.
812 inline bool in_collection_set_loc(void* loc) const;
813
814 // Evacuates or promotes object src. Returns the evacuated object, either evacuated
815 // by this thread, or by some other thread. On allocation failure, installs the
816 // self-forwarded bit on src, flags src's region, and returns src.
817 virtual oop evacuate_object(oop src, Thread* thread);
818
819 // Parallel scan of flagged cset regions to clear self-forwarded bits on live
820 // objects. Must be called at a safepoint; intended for the degenerated and
821 // full GC entry paths.
822 void un_self_forward_cset_regions();
823
824 DEBUG_ONLY(void assert_no_self_forwards() const;)
825
826 // ---------- Helper functions
827 //
828 public:
829 template <class T>
830 inline void conc_update_with_forwarded(T* p);
831
832 template <class T>
833 inline void non_conc_update_with_forwarded(T* p);
834
835 static inline void atomic_update_oop(oop update, oop* addr, oop compare);
836 static inline void atomic_update_oop(oop update, narrowOop* addr, oop compare);
837 static inline void atomic_update_oop(oop update, narrowOop* addr, narrowOop compare);
838
839 static inline bool atomic_update_oop_check(oop update, oop* addr, oop compare);
840 static inline bool atomic_update_oop_check(oop update, narrowOop* addr, oop compare);
841 static inline bool atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare);
842
843 static inline void atomic_clear_oop( oop* addr, oop compare);
844 static inline void atomic_clear_oop(narrowOop* addr, oop compare);
845 static inline void atomic_clear_oop(narrowOop* addr, narrowOop compare);
846
847 size_t trash_humongous_region_at(ShenandoahHeapRegion *r) const;
848
849 static inline void increase_object_age(oop obj, uint additional_age);
850
851 // Return the object's age, or a sentinel value when the age can't
852 // necessarily be determined because of concurrent locking by the
853 // mutator
854 static inline uint get_object_age(oop obj);
855
856 void log_heap_status(const char *msg) const;
857
858 private:
859 void trash_cset_regions();
860
861 // ---------- Testing helpers functions
862 //
863 private:
864 ShenandoahSharedFlag _inject_alloc_failure;
865
866 void try_inject_alloc_failure();
867 bool should_inject_alloc_failure();
868 };
869
870 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_HPP