1 /* 2 * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_GC_SHARED_COLLECTEDHEAP_HPP 26 #define SHARE_GC_SHARED_COLLECTEDHEAP_HPP 27 28 #include "gc/shared/gcCause.hpp" 29 #include "gc/shared/gcWhen.hpp" 30 #include "gc/shared/verifyOption.hpp" 31 #include "memory/allocation.hpp" 32 #include "memory/metaspace.hpp" 33 #include "memory/universe.hpp" 34 #include "oops/stackChunkOop.hpp" 35 #include "runtime/handles.hpp" 36 #include "runtime/perfDataTypes.hpp" 37 #include "runtime/safepoint.hpp" 38 #include "services/memoryUsage.hpp" 39 #include "utilities/debug.hpp" 40 #include "utilities/formatBuffer.hpp" 41 #include "utilities/growableArray.hpp" 42 43 // A "CollectedHeap" is an implementation of a java heap for HotSpot. This 44 // is an abstract class: there may be many different kinds of heaps. This 45 // class defines the functions that a heap must implement, and contains 46 // infrastructure common to all heaps. 47 48 class WorkerTask; 49 class AdaptiveSizePolicy; 50 class BarrierSet; 51 class GCHeapLog; 52 class GCHeapSummary; 53 class GCTimer; 54 class GCTracer; 55 class GCMemoryManager; 56 class MemoryPool; 57 class MetaspaceSummary; 58 class ReservedHeapSpace; 59 class SoftRefPolicy; 60 class Thread; 61 class ThreadClosure; 62 class VirtualSpaceSummary; 63 class WorkerThreads; 64 class nmethod; 65 66 class ParallelObjectIteratorImpl : public CHeapObj<mtGC> { 67 public: 68 virtual ~ParallelObjectIteratorImpl() {} 69 virtual void object_iterate(ObjectClosure* cl, uint worker_id) = 0; 70 }; 71 72 // User facing parallel object iterator. This is a StackObj, which ensures that 73 // the _impl is allocated and deleted in the scope of this object. This ensures 74 // the life cycle of the implementation is as required by ThreadsListHandle, 75 // which is sometimes used by the root iterators. 76 class ParallelObjectIterator : public StackObj { 77 ParallelObjectIteratorImpl* _impl; 78 79 public: 80 ParallelObjectIterator(uint thread_num); 81 ~ParallelObjectIterator(); 82 void object_iterate(ObjectClosure* cl, uint worker_id); 83 }; 84 85 // 86 // CollectedHeap 87 // GenCollectedHeap 88 // SerialHeap 89 // G1CollectedHeap 90 // ParallelScavengeHeap 91 // ShenandoahHeap 92 // ZCollectedHeap 93 // 94 class CollectedHeap : public CHeapObj<mtGC> { 95 friend class VMStructs; 96 friend class JVMCIVMStructs; 97 friend class IsGCActiveMark; // Block structured external access to _is_gc_active 98 friend class DisableIsGCActiveMark; // Disable current IsGCActiveMark 99 friend class MemAllocator; 100 friend class ParallelObjectIterator; 101 102 private: 103 GCHeapLog* _gc_heap_log; 104 105 // Historic gc information 106 size_t _capacity_at_last_gc; 107 size_t _used_at_last_gc; 108 109 // First, set it to java_lang_Object. 110 // Then, set it to FillerObject after the FillerObject_klass loading is complete. 111 static Klass* _filler_object_klass; 112 113 protected: 114 // Not used by all GCs 115 MemRegion _reserved; 116 117 bool _is_gc_active; 118 119 // (Minimum) Alignment reserve for TLABs and PLABs. 120 static size_t _lab_alignment_reserve; 121 // Used for filler objects (static, but initialized in ctor). 122 static size_t _filler_array_max_size; 123 124 static size_t _stack_chunk_max_size; // 0 for no limit 125 126 // Last time the whole heap has been examined in support of RMI 127 // MaxObjectInspectionAge. 128 // This timestamp must be monotonically non-decreasing to avoid 129 // time-warp warnings. 130 jlong _last_whole_heap_examined_time_ns; 131 132 unsigned int _total_collections; // ... started 133 unsigned int _total_full_collections; // ... started 134 NOT_PRODUCT(volatile size_t _promotion_failure_alot_count;) 135 NOT_PRODUCT(volatile size_t _promotion_failure_alot_gc_number;) 136 137 // Reason for current garbage collection. Should be set to 138 // a value reflecting no collection between collections. 139 GCCause::Cause _gc_cause; 140 GCCause::Cause _gc_lastcause; 141 PerfStringVariable* _perf_gc_cause; 142 PerfStringVariable* _perf_gc_lastcause; 143 144 // Constructor 145 CollectedHeap(); 146 147 // Create a new tlab. All TLAB allocations must go through this. 148 // To allow more flexible TLAB allocations min_size specifies 149 // the minimum size needed, while requested_size is the requested 150 // size based on ergonomics. The actually allocated size will be 151 // returned in actual_size. 152 virtual HeapWord* allocate_new_tlab(size_t min_size, 153 size_t requested_size, 154 size_t* actual_size); 155 156 // Reinitialize tlabs before resuming mutators. 157 virtual void resize_all_tlabs(); 158 159 // Raw memory allocation facilities 160 // The obj and array allocate methods are covers for these methods. 161 // mem_allocate() should never be 162 // called to allocate TLABs, only individual objects. 163 virtual HeapWord* mem_allocate(size_t size, 164 bool* gc_overhead_limit_was_exceeded) = 0; 165 166 // Filler object utilities. 167 static inline size_t filler_array_hdr_size(); 168 static inline size_t filler_array_min_size(); 169 170 static inline void zap_filler_array_with(HeapWord* start, size_t words, juint value); 171 DEBUG_ONLY(static void fill_args_check(HeapWord* start, size_t words);) 172 DEBUG_ONLY(static void zap_filler_array(HeapWord* start, size_t words, bool zap = true);) 173 174 // Fill with a single array; caller must ensure filler_array_min_size() <= 175 // words <= filler_array_max_size(). 176 static inline void fill_with_array(HeapWord* start, size_t words, bool zap = true); 177 178 // Fill with a single object (either an int array or a java.lang.Object). 179 static inline void fill_with_object_impl(HeapWord* start, size_t words, bool zap = true); 180 181 virtual void trace_heap(GCWhen::Type when, const GCTracer* tracer); 182 183 // Verification functions 184 debug_only(static void check_for_valid_allocation_state();) 185 186 public: 187 enum Name { 188 None, 189 Serial, 190 Parallel, 191 G1, 192 Epsilon, 193 Z, 194 Shenandoah 195 }; 196 197 protected: 198 // Get a pointer to the derived heap object. Used to implement 199 // derived class heap() functions rather than being called directly. 200 template<typename T> 201 static T* named_heap(Name kind) { 202 CollectedHeap* heap = Universe::heap(); 203 assert(heap != nullptr, "Uninitialized heap"); 204 assert(kind == heap->kind(), "Heap kind %u should be %u", 205 static_cast<uint>(heap->kind()), static_cast<uint>(kind)); 206 return static_cast<T*>(heap); 207 } 208 209 public: 210 211 static inline size_t filler_array_max_size() { 212 return _filler_array_max_size; 213 } 214 215 static inline size_t stack_chunk_max_size() { 216 return _stack_chunk_max_size; 217 } 218 219 static inline Klass* filler_object_klass() { 220 return _filler_object_klass; 221 } 222 223 static inline void set_filler_object_klass(Klass* k) { 224 _filler_object_klass = k; 225 } 226 227 virtual Name kind() const = 0; 228 229 virtual const char* name() const = 0; 230 231 /** 232 * Returns JNI error code JNI_ENOMEM if memory could not be allocated, 233 * and JNI_OK on success. 234 */ 235 virtual jint initialize() = 0; 236 237 // In many heaps, there will be a need to perform some initialization activities 238 // after the Universe is fully formed, but before general heap allocation is allowed. 239 // This is the correct place to place such initialization methods. 240 virtual void post_initialize(); 241 242 // Stop any onging concurrent work and prepare for exit. 243 virtual void stop() {} 244 245 // Stop and resume concurrent GC threads interfering with safepoint operations 246 virtual void safepoint_synchronize_begin() {} 247 virtual void safepoint_synchronize_end() {} 248 249 void initialize_reserved_region(const ReservedHeapSpace& rs); 250 251 virtual size_t capacity() const = 0; 252 virtual size_t used() const = 0; 253 254 // Returns unused capacity. 255 virtual size_t unused() const; 256 257 // Historic gc information 258 size_t free_at_last_gc() const { return _capacity_at_last_gc - _used_at_last_gc; } 259 size_t used_at_last_gc() const { return _used_at_last_gc; } 260 void update_capacity_and_used_at_gc(); 261 262 // Return "true" if the part of the heap that allocates Java 263 // objects has reached the maximal committed limit that it can 264 // reach, without a garbage collection. 265 virtual bool is_maximal_no_gc() const = 0; 266 267 // Support for java.lang.Runtime.maxMemory(): return the maximum amount of 268 // memory that the vm could make available for storing 'normal' java objects. 269 // This is based on the reserved address space, but should not include space 270 // that the vm uses internally for bookkeeping or temporary storage 271 // (e.g., in the case of the young gen, one of the survivor 272 // spaces). 273 virtual size_t max_capacity() const = 0; 274 275 // Returns "TRUE" iff "p" points into the committed areas of the heap. 276 // This method can be expensive so avoid using it in performance critical 277 // code. 278 virtual bool is_in(const void* p) const = 0; 279 280 DEBUG_ONLY(bool is_in_or_null(const void* p) const { return p == nullptr || is_in(p); }) 281 282 void set_gc_cause(GCCause::Cause v); 283 GCCause::Cause gc_cause() { return _gc_cause; } 284 285 oop obj_allocate(Klass* klass, size_t size, TRAPS); 286 oop obj_buffer_allocate(Klass* klass, size_t size, TRAPS); // doesn't clear memory 287 virtual oop array_allocate(Klass* klass, size_t size, int length, bool do_zero, TRAPS); 288 oop class_allocate(Klass* klass, size_t size, TRAPS); 289 290 // Utilities for turning raw memory into filler objects. 291 // 292 // min_fill_size() is the smallest region that can be filled. 293 // fill_with_objects() can fill arbitrary-sized regions of the heap using 294 // multiple objects. fill_with_object() is for regions known to be smaller 295 // than the largest array of integers; it uses a single object to fill the 296 // region and has slightly less overhead. 297 static size_t min_fill_size() { 298 return size_t(align_object_size(oopDesc::header_size())); 299 } 300 301 static void fill_with_objects(HeapWord* start, size_t words, bool zap = true); 302 303 static void fill_with_object(HeapWord* start, size_t words, bool zap = true); 304 static void fill_with_object(MemRegion region, bool zap = true) { 305 fill_with_object(region.start(), region.word_size(), zap); 306 } 307 static void fill_with_object(HeapWord* start, HeapWord* end, bool zap = true) { 308 fill_with_object(start, pointer_delta(end, start), zap); 309 } 310 311 virtual void fill_with_dummy_object(HeapWord* start, HeapWord* end, bool zap); 312 static constexpr size_t min_dummy_object_size() { 313 return oopDesc::header_size(); 314 } 315 316 static size_t lab_alignment_reserve() { 317 assert(_lab_alignment_reserve != SIZE_MAX, "uninitialized"); 318 return _lab_alignment_reserve; 319 } 320 321 // Some heaps may be in an unparseable state at certain times between 322 // collections. This may be necessary for efficient implementation of 323 // certain allocation-related activities. Calling this function before 324 // attempting to parse a heap ensures that the heap is in a parsable 325 // state (provided other concurrent activity does not introduce 326 // unparsability). It is normally expected, therefore, that this 327 // method is invoked with the world stopped. 328 // NOTE: if you override this method, make sure you call 329 // super::ensure_parsability so that the non-generational 330 // part of the work gets done. See implementation of 331 // CollectedHeap::ensure_parsability and, for instance, 332 // that of GenCollectedHeap::ensure_parsability(). 333 // The argument "retire_tlabs" controls whether existing TLABs 334 // are merely filled or also retired, thus preventing further 335 // allocation from them and necessitating allocation of new TLABs. 336 virtual void ensure_parsability(bool retire_tlabs); 337 338 // The amount of space available for thread-local allocation buffers. 339 virtual size_t tlab_capacity(Thread *thr) const = 0; 340 341 // The amount of used space for thread-local allocation buffers for the given thread. 342 virtual size_t tlab_used(Thread *thr) const = 0; 343 344 virtual size_t max_tlab_size() const; 345 346 // An estimate of the maximum allocation that could be performed 347 // for thread-local allocation buffers without triggering any 348 // collection or expansion activity. 349 virtual size_t unsafe_max_tlab_alloc(Thread *thr) const { 350 guarantee(false, "thread-local allocation buffers not supported"); 351 return 0; 352 } 353 354 // If a GC uses a stack watermark barrier, the stack processing is lazy, concurrent, 355 // incremental and cooperative. In order for that to work well, mechanisms that stop 356 // another thread might want to ensure its roots are in a sane state. 357 virtual bool uses_stack_watermark_barrier() const { return false; } 358 359 // Perform a collection of the heap; intended for use in implementing 360 // "System.gc". This probably implies as full a collection as the 361 // "CollectedHeap" supports. 362 virtual void collect(GCCause::Cause cause) = 0; 363 364 // Perform a full collection 365 virtual void do_full_collection(bool clear_all_soft_refs) = 0; 366 367 // This interface assumes that it's being called by the 368 // vm thread. It collects the heap assuming that the 369 // heap lock is already held and that we are executing in 370 // the context of the vm thread. 371 virtual void collect_as_vm_thread(GCCause::Cause cause); 372 373 virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data, 374 size_t size, 375 Metaspace::MetadataType mdtype); 376 377 // Return true, if accesses to the object would require barriers. 378 // This is used by continuations to copy chunks of a thread stack into StackChunk object or out of a StackChunk 379 // object back into the thread stack. These chunks may contain references to objects. It is crucial that 380 // the GC does not attempt to traverse the object while we modify it, because its structure (oopmap) is changed 381 // when stack chunks are stored into it. 382 // StackChunk objects may be reused, the GC must not assume that a StackChunk object is always a freshly 383 // allocated object. 384 virtual bool requires_barriers(stackChunkOop obj) const = 0; 385 386 // Returns "true" iff there is a stop-world GC in progress. (I assume 387 // that it should answer "false" for the concurrent part of a concurrent 388 // collector -- dld). 389 bool is_gc_active() const { return _is_gc_active; } 390 391 // Total number of GC collections (started) 392 unsigned int total_collections() const { return _total_collections; } 393 unsigned int total_full_collections() const { return _total_full_collections;} 394 395 // Increment total number of GC collections (started) 396 void increment_total_collections(bool full = false) { 397 _total_collections++; 398 if (full) { 399 increment_total_full_collections(); 400 } 401 } 402 403 void increment_total_full_collections() { _total_full_collections++; } 404 405 // Return the SoftRefPolicy for the heap; 406 virtual SoftRefPolicy* soft_ref_policy() = 0; 407 408 virtual MemoryUsage memory_usage(); 409 virtual GrowableArray<GCMemoryManager*> memory_managers() = 0; 410 virtual GrowableArray<MemoryPool*> memory_pools() = 0; 411 412 // Iterate over all objects, calling "cl.do_object" on each. 413 virtual void object_iterate(ObjectClosure* cl) = 0; 414 415 protected: 416 virtual ParallelObjectIteratorImpl* parallel_object_iterator(uint thread_num) { 417 return nullptr; 418 } 419 420 public: 421 // Keep alive an object that was loaded with AS_NO_KEEPALIVE. 422 virtual void keep_alive(oop obj) {} 423 424 // Perform any cleanup actions necessary before allowing a verification. 425 virtual void prepare_for_verify() = 0; 426 427 // Returns the longest time (in ms) that has elapsed since the last 428 // time that the whole heap has been examined by a garbage collection. 429 jlong millis_since_last_whole_heap_examined(); 430 // GC should call this when the next whole heap analysis has completed to 431 // satisfy above requirement. 432 void record_whole_heap_examined_timestamp(); 433 434 private: 435 // Generate any dumps preceding or following a full gc 436 void full_gc_dump(GCTimer* timer, bool before); 437 438 virtual void initialize_serviceability() = 0; 439 440 public: 441 void pre_full_gc_dump(GCTimer* timer); 442 void post_full_gc_dump(GCTimer* timer); 443 444 virtual VirtualSpaceSummary create_heap_space_summary(); 445 GCHeapSummary create_heap_summary(); 446 447 MetaspaceSummary create_metaspace_summary(); 448 449 // GCs are free to represent the bit representation for null differently in memory, 450 // which is typically not observable when using the Access API. However, if for 451 // some reason a context doesn't allow using the Access API, then this function 452 // explicitly checks if the given memory location contains a null value. 453 virtual bool contains_null(const oop* p) const; 454 455 // Print heap information on the given outputStream. 456 virtual void print_on(outputStream* st) const = 0; 457 // The default behavior is to call print_on() on tty. 458 virtual void print() const; 459 460 // Print more detailed heap information on the given 461 // outputStream. The default behavior is to call print_on(). It is 462 // up to each subclass to override it and add any additional output 463 // it needs. 464 virtual void print_extended_on(outputStream* st) const { 465 print_on(st); 466 } 467 468 virtual void print_on_error(outputStream* st) const; 469 470 // Used to print information about locations in the hs_err file. 471 virtual bool print_location(outputStream* st, void* addr) const = 0; 472 473 // Iterator for all GC threads (other than VM thread) 474 virtual void gc_threads_do(ThreadClosure* tc) const = 0; 475 476 // Print any relevant tracing info that flags imply. 477 // Default implementation does nothing. 478 virtual void print_tracing_info() const = 0; 479 480 void print_heap_before_gc(); 481 void print_heap_after_gc(); 482 483 // Registering and unregistering an nmethod (compiled code) with the heap. 484 virtual void register_nmethod(nmethod* nm) = 0; 485 virtual void unregister_nmethod(nmethod* nm) = 0; 486 virtual void verify_nmethod(nmethod* nm) = 0; 487 488 void trace_heap_before_gc(const GCTracer* gc_tracer); 489 void trace_heap_after_gc(const GCTracer* gc_tracer); 490 491 // Heap verification 492 virtual void verify(VerifyOption option) = 0; 493 494 // Return true if concurrent gc control via WhiteBox is supported by 495 // this collector. The default implementation returns false. 496 virtual bool supports_concurrent_gc_breakpoints() const; 497 498 // Workers used in non-GC safepoints for parallel safepoint cleanup. If this 499 // method returns null, cleanup tasks are done serially in the VMThread. See 500 // `SafepointSynchronize::do_cleanup_tasks` for details. 501 // GCs using a GC worker thread pool inside GC safepoints may opt to share 502 // that pool with non-GC safepoints, avoiding creating extraneous threads. 503 // Such sharing is safe, because GC safepoints and non-GC safepoints never 504 // overlap. For example, `G1CollectedHeap::workers()` (for GC safepoints) and 505 // `G1CollectedHeap::safepoint_workers()` (for non-GC safepoints) return the 506 // same thread-pool. 507 virtual WorkerThreads* safepoint_workers() { return nullptr; } 508 509 // Support for object pinning. This is used by JNI Get*Critical() 510 // and Release*Critical() family of functions. The GC must guarantee 511 // that pinned objects never move and don't get reclaimed as garbage. 512 // These functions are potentially safepointing. 513 virtual void pin_object(JavaThread* thread, oop obj) = 0; 514 virtual void unpin_object(JavaThread* thread, oop obj) = 0; 515 516 // Support for loading objects from CDS archive into the heap 517 // (usually as a snapshot of the old generation). 518 virtual bool can_load_archived_objects() const { return false; } 519 virtual HeapWord* allocate_loaded_archive_space(size_t size) { return nullptr; } 520 virtual void complete_loaded_archive_space(MemRegion archive_space) { } 521 522 virtual bool is_oop(oop object) const; 523 // Non product verification and debugging. 524 #ifndef PRODUCT 525 // Support for PromotionFailureALot. Return true if it's time to cause a 526 // promotion failure. The no-argument version uses 527 // this->_promotion_failure_alot_count as the counter. 528 bool promotion_should_fail(volatile size_t* count); 529 bool promotion_should_fail(); 530 531 // Reset the PromotionFailureALot counters. Should be called at the end of a 532 // GC in which promotion failure occurred. 533 void reset_promotion_should_fail(volatile size_t* count); 534 void reset_promotion_should_fail(); 535 #endif // #ifndef PRODUCT 536 }; 537 538 // Class to set and reset the GC cause for a CollectedHeap. 539 540 class GCCauseSetter : StackObj { 541 CollectedHeap* _heap; 542 GCCause::Cause _previous_cause; 543 public: 544 GCCauseSetter(CollectedHeap* heap, GCCause::Cause cause) { 545 _heap = heap; 546 _previous_cause = _heap->gc_cause(); 547 _heap->set_gc_cause(cause); 548 } 549 550 ~GCCauseSetter() { 551 _heap->set_gc_cause(_previous_cause); 552 } 553 }; 554 555 #endif // SHARE_GC_SHARED_COLLECTEDHEAP_HPP