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