1 /* 2 * Copyright (c) 2001, 2022, 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 "runtime/handles.hpp" 35 #include "runtime/perfDataTypes.hpp" 36 #include "runtime/safepoint.hpp" 37 #include "services/memoryUsage.hpp" 38 #include "utilities/debug.hpp" 39 #include "utilities/formatBuffer.hpp" 40 #include "utilities/growableArray.hpp" 41 42 // A "CollectedHeap" is an implementation of a java heap for HotSpot. This 43 // is an abstract class: there may be many different kinds of heaps. This 44 // class defines the functions that a heap must implement, and contains 45 // infrastructure common to all heaps. 46 47 class AbstractGangTask; 48 class AdaptiveSizePolicy; 49 class BarrierSet; 50 class GCHeapLog; 51 class GCHeapSummary; 52 class GCTimer; 53 class GCTracer; 54 class GCMemoryManager; 55 class MemoryPool; 56 class MetaspaceSummary; 57 class ReservedHeapSpace; 58 class SoftRefPolicy; 59 class Thread; 60 class ThreadClosure; 61 class VirtualSpaceSummary; 62 class WorkGang; 63 class nmethod; 64 65 class ParallelObjectIterator : public CHeapObj<mtGC> { 66 public: 67 virtual void object_iterate(ObjectClosure* cl, uint worker_id) = 0; 68 virtual ~ParallelObjectIterator() {} 69 }; 70 71 // 72 // CollectedHeap 73 // GenCollectedHeap 74 // SerialHeap 75 // G1CollectedHeap 76 // ParallelScavengeHeap 77 // ShenandoahHeap 78 // ZCollectedHeap 79 // 80 class CollectedHeap : public CHeapObj<mtGC> { 81 friend class VMStructs; 82 friend class JVMCIVMStructs; 83 friend class IsGCActiveMark; // Block structured external access to _is_gc_active 84 friend class MemAllocator; 85 86 private: 87 GCHeapLog* _gc_heap_log; 88 89 // Historic gc information 90 size_t _capacity_at_last_gc; 91 size_t _used_at_last_gc; 92 93 protected: 94 // Not used by all GCs 95 MemRegion _reserved; 96 97 bool _is_gc_active; 98 99 // Used for filler objects (static, but initialized in ctor). 100 static size_t _filler_array_max_size; 101 102 // Last time the whole heap has been examined in support of RMI 103 // MaxObjectInspectionAge. 104 // This timestamp must be monotonically non-decreasing to avoid 105 // time-warp warnings. 106 jlong _last_whole_heap_examined_time_ns; 107 108 unsigned int _total_collections; // ... started 109 unsigned int _total_full_collections; // ... started 110 NOT_PRODUCT(volatile size_t _promotion_failure_alot_count;) 111 NOT_PRODUCT(volatile size_t _promotion_failure_alot_gc_number;) 112 113 // Reason for current garbage collection. Should be set to 114 // a value reflecting no collection between collections. 115 GCCause::Cause _gc_cause; 116 GCCause::Cause _gc_lastcause; 117 PerfStringVariable* _perf_gc_cause; 118 PerfStringVariable* _perf_gc_lastcause; 119 120 // Constructor 121 CollectedHeap(); 122 123 // Create a new tlab. All TLAB allocations must go through this. 124 // To allow more flexible TLAB allocations min_size specifies 125 // the minimum size needed, while requested_size is the requested 126 // size based on ergonomics. The actually allocated size will be 127 // returned in actual_size. 128 virtual HeapWord* allocate_new_tlab(size_t min_size, 129 size_t requested_size, 130 size_t* actual_size); 131 132 // Reinitialize tlabs before resuming mutators. 133 virtual void resize_all_tlabs(); 134 135 // Raw memory allocation facilities 136 // The obj and array allocate methods are covers for these methods. 137 // mem_allocate() should never be 138 // called to allocate TLABs, only individual objects. 139 virtual HeapWord* mem_allocate(size_t size, 140 bool* gc_overhead_limit_was_exceeded) = 0; 141 142 // Filler object utilities. 143 static inline size_t filler_array_min_size(); 144 145 DEBUG_ONLY(static void fill_args_check(HeapWord* start, size_t words);) 146 DEBUG_ONLY(static void zap_filler_array(HeapWord* start, size_t words, bool zap = true);) 147 148 // Fill with a single array; caller must ensure filler_array_min_size() <= 149 // words <= filler_array_max_size(). 150 static inline void fill_with_array(HeapWord* start, size_t words, bool zap = true); 151 152 // Fill with a single object (either an int array or a java.lang.Object). 153 static inline void fill_with_object_impl(HeapWord* start, size_t words, bool zap = true); 154 155 virtual void trace_heap(GCWhen::Type when, const GCTracer* tracer); 156 157 // Verification functions 158 virtual void check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) 159 PRODUCT_RETURN; 160 debug_only(static void check_for_valid_allocation_state();) 161 162 public: 163 enum Name { 164 None, 165 Serial, 166 Parallel, 167 G1, 168 Epsilon, 169 Z, 170 Shenandoah 171 }; 172 173 protected: 174 // Get a pointer to the derived heap object. Used to implement 175 // derived class heap() functions rather than being called directly. 176 template<typename T> 177 static T* named_heap(Name kind) { 178 CollectedHeap* heap = Universe::heap(); 179 assert(heap != NULL, "Uninitialized heap"); 180 assert(kind == heap->kind(), "Heap kind %u should be %u", 181 static_cast<uint>(heap->kind()), static_cast<uint>(kind)); 182 return static_cast<T*>(heap); 183 } 184 185 public: 186 187 static inline size_t filler_array_max_size() { 188 return _filler_array_max_size; 189 } 190 191 virtual Name kind() const = 0; 192 193 virtual const char* name() const = 0; 194 195 /** 196 * Returns JNI error code JNI_ENOMEM if memory could not be allocated, 197 * and JNI_OK on success. 198 */ 199 virtual jint initialize() = 0; 200 201 // In many heaps, there will be a need to perform some initialization activities 202 // after the Universe is fully formed, but before general heap allocation is allowed. 203 // This is the correct place to place such initialization methods. 204 virtual void post_initialize(); 205 206 // Stop any onging concurrent work and prepare for exit. 207 virtual void stop() {} 208 209 // Stop and resume concurrent GC threads interfering with safepoint operations 210 virtual void safepoint_synchronize_begin() {} 211 virtual void safepoint_synchronize_end() {} 212 213 void initialize_reserved_region(const ReservedHeapSpace& rs); 214 215 virtual size_t capacity() const = 0; 216 virtual size_t used() const = 0; 217 218 // Returns unused capacity. 219 virtual size_t unused() const; 220 221 // Historic gc information 222 size_t free_at_last_gc() const { return _capacity_at_last_gc - _used_at_last_gc; } 223 size_t used_at_last_gc() const { return _used_at_last_gc; } 224 void update_capacity_and_used_at_gc(); 225 226 // Return "true" if the part of the heap that allocates Java 227 // objects has reached the maximal committed limit that it can 228 // reach, without a garbage collection. 229 virtual bool is_maximal_no_gc() const = 0; 230 231 // Support for java.lang.Runtime.maxMemory(): return the maximum amount of 232 // memory that the vm could make available for storing 'normal' java objects. 233 // This is based on the reserved address space, but should not include space 234 // that the vm uses internally for bookkeeping or temporary storage 235 // (e.g., in the case of the young gen, one of the survivor 236 // spaces). 237 virtual size_t max_capacity() const = 0; 238 239 // Returns "TRUE" iff "p" points into the committed areas of the heap. 240 // This method can be expensive so avoid using it in performance critical 241 // code. 242 virtual bool is_in(const void* p) const = 0; 243 244 DEBUG_ONLY(bool is_in_or_null(const void* p) const { return p == NULL || is_in(p); }) 245 246 virtual uint32_t hash_oop(oop obj) const; 247 248 void set_gc_cause(GCCause::Cause v); 249 GCCause::Cause gc_cause() { return _gc_cause; } 250 251 oop obj_allocate(Klass* klass, int size, TRAPS); 252 virtual oop array_allocate(Klass* klass, int size, int length, bool do_zero, TRAPS); 253 oop class_allocate(Klass* klass, int size, TRAPS); 254 255 // Utilities for turning raw memory into filler objects. 256 // 257 // min_fill_size() is the smallest region that can be filled. 258 // fill_with_objects() can fill arbitrary-sized regions of the heap using 259 // multiple objects. fill_with_object() is for regions known to be smaller 260 // than the largest array of integers; it uses a single object to fill the 261 // region and has slightly less overhead. 262 static size_t min_fill_size() { 263 return size_t(align_object_size(oopDesc::header_size())); 264 } 265 266 static void fill_with_objects(HeapWord* start, size_t words, bool zap = true); 267 268 static void fill_with_object(HeapWord* start, size_t words, bool zap = true); 269 static void fill_with_object(MemRegion region, bool zap = true) { 270 fill_with_object(region.start(), region.word_size(), zap); 271 } 272 static void fill_with_object(HeapWord* start, HeapWord* end, bool zap = true) { 273 fill_with_object(start, pointer_delta(end, start), zap); 274 } 275 276 virtual void fill_with_dummy_object(HeapWord* start, HeapWord* end, bool zap); 277 virtual size_t min_dummy_object_size() const; 278 size_t tlab_alloc_reserve() const; 279 280 // Some heaps may offer a contiguous region for shared non-blocking 281 // allocation, via inlined code (by exporting the address of the top and 282 // end fields defining the extent of the contiguous allocation region.) 283 284 // This function returns "true" iff the heap supports this kind of 285 // allocation. (Default is "no".) 286 virtual bool supports_inline_contig_alloc() const { 287 return false; 288 } 289 // These functions return the addresses of the fields that define the 290 // boundaries of the contiguous allocation area. (These fields should be 291 // physically near to one another.) 292 virtual HeapWord* volatile* top_addr() const { 293 guarantee(false, "inline contiguous allocation not supported"); 294 return NULL; 295 } 296 virtual HeapWord** end_addr() const { 297 guarantee(false, "inline contiguous allocation not supported"); 298 return NULL; 299 } 300 301 // Some heaps may be in an unparseable state at certain times between 302 // collections. This may be necessary for efficient implementation of 303 // certain allocation-related activities. Calling this function before 304 // attempting to parse a heap ensures that the heap is in a parsable 305 // state (provided other concurrent activity does not introduce 306 // unparsability). It is normally expected, therefore, that this 307 // method is invoked with the world stopped. 308 // NOTE: if you override this method, make sure you call 309 // super::ensure_parsability so that the non-generational 310 // part of the work gets done. See implementation of 311 // CollectedHeap::ensure_parsability and, for instance, 312 // that of GenCollectedHeap::ensure_parsability(). 313 // The argument "retire_tlabs" controls whether existing TLABs 314 // are merely filled or also retired, thus preventing further 315 // allocation from them and necessitating allocation of new TLABs. 316 virtual void ensure_parsability(bool retire_tlabs); 317 318 // The amount of space available for thread-local allocation buffers. 319 virtual size_t tlab_capacity(Thread *thr) const = 0; 320 321 // The amount of used space for thread-local allocation buffers for the given thread. 322 virtual size_t tlab_used(Thread *thr) const = 0; 323 324 virtual size_t max_tlab_size() const; 325 326 // An estimate of the maximum allocation that could be performed 327 // for thread-local allocation buffers without triggering any 328 // collection or expansion activity. 329 virtual size_t unsafe_max_tlab_alloc(Thread *thr) const { 330 guarantee(false, "thread-local allocation buffers not supported"); 331 return 0; 332 } 333 334 // If a GC uses a stack watermark barrier, the stack processing is lazy, concurrent, 335 // incremental and cooperative. In order for that to work well, mechanisms that stop 336 // another thread might want to ensure its roots are in a sane state. 337 virtual bool uses_stack_watermark_barrier() const { return false; } 338 339 // Perform a collection of the heap; intended for use in implementing 340 // "System.gc". This probably implies as full a collection as the 341 // "CollectedHeap" supports. 342 virtual void collect(GCCause::Cause cause) = 0; 343 344 // Perform a full collection 345 virtual void do_full_collection(bool clear_all_soft_refs) = 0; 346 347 // This interface assumes that it's being called by the 348 // vm thread. It collects the heap assuming that the 349 // heap lock is already held and that we are executing in 350 // the context of the vm thread. 351 virtual void collect_as_vm_thread(GCCause::Cause cause); 352 353 virtual MetaWord* satisfy_failed_metadata_allocation(ClassLoaderData* loader_data, 354 size_t size, 355 Metaspace::MetadataType mdtype); 356 357 // Returns "true" iff there is a stop-world GC in progress. (I assume 358 // that it should answer "false" for the concurrent part of a concurrent 359 // collector -- dld). 360 bool is_gc_active() const { return _is_gc_active; } 361 362 // Total number of GC collections (started) 363 unsigned int total_collections() const { return _total_collections; } 364 unsigned int total_full_collections() const { return _total_full_collections;} 365 366 // Increment total number of GC collections (started) 367 void increment_total_collections(bool full = false) { 368 _total_collections++; 369 if (full) { 370 increment_total_full_collections(); 371 } 372 } 373 374 void increment_total_full_collections() { _total_full_collections++; } 375 376 // Return the SoftRefPolicy for the heap; 377 virtual SoftRefPolicy* soft_ref_policy() = 0; 378 379 virtual MemoryUsage memory_usage(); 380 virtual GrowableArray<GCMemoryManager*> memory_managers() = 0; 381 virtual GrowableArray<MemoryPool*> memory_pools() = 0; 382 383 // Iterate over all objects, calling "cl.do_object" on each. 384 virtual void object_iterate(ObjectClosure* cl) = 0; 385 386 virtual ParallelObjectIterator* parallel_object_iterator(uint thread_num) { 387 return NULL; 388 } 389 390 // Keep alive an object that was loaded with AS_NO_KEEPALIVE. 391 virtual void keep_alive(oop obj) {} 392 393 // Perform any cleanup actions necessary before allowing a verification. 394 virtual void prepare_for_verify() = 0; 395 396 // Returns the longest time (in ms) that has elapsed since the last 397 // time that the whole heap has been examined by a garbage collection. 398 jlong millis_since_last_whole_heap_examined(); 399 // GC should call this when the next whole heap analysis has completed to 400 // satisfy above requirement. 401 void record_whole_heap_examined_timestamp(); 402 403 private: 404 // Generate any dumps preceding or following a full gc 405 void full_gc_dump(GCTimer* timer, bool before); 406 407 virtual void initialize_serviceability() = 0; 408 409 public: 410 void pre_full_gc_dump(GCTimer* timer); 411 void post_full_gc_dump(GCTimer* timer); 412 413 virtual VirtualSpaceSummary create_heap_space_summary(); 414 GCHeapSummary create_heap_summary(); 415 416 MetaspaceSummary create_metaspace_summary(); 417 418 // Print heap information on the given outputStream. 419 virtual void print_on(outputStream* st) const = 0; 420 // The default behavior is to call print_on() on tty. 421 virtual void print() const; 422 423 // Print more detailed heap information on the given 424 // outputStream. The default behavior is to call print_on(). It is 425 // up to each subclass to override it and add any additional output 426 // it needs. 427 virtual void print_extended_on(outputStream* st) const { 428 print_on(st); 429 } 430 431 virtual void print_on_error(outputStream* st) const; 432 433 // Used to print information about locations in the hs_err file. 434 virtual bool print_location(outputStream* st, void* addr) const = 0; 435 436 // Iterator for all GC threads (other than VM thread) 437 virtual void gc_threads_do(ThreadClosure* tc) const = 0; 438 439 // Print any relevant tracing info that flags imply. 440 // Default implementation does nothing. 441 virtual void print_tracing_info() const = 0; 442 443 void print_heap_before_gc(); 444 void print_heap_after_gc(); 445 446 // Registering and unregistering an nmethod (compiled code) with the heap. 447 virtual void register_nmethod(nmethod* nm) = 0; 448 virtual void unregister_nmethod(nmethod* nm) = 0; 449 // Callback for when nmethod is about to be deleted. 450 virtual void flush_nmethod(nmethod* nm) = 0; 451 virtual void verify_nmethod(nmethod* nm) = 0; 452 453 void trace_heap_before_gc(const GCTracer* gc_tracer); 454 void trace_heap_after_gc(const GCTracer* gc_tracer); 455 456 // Heap verification 457 virtual void verify(VerifyOption option) = 0; 458 459 // Return true if concurrent gc control via WhiteBox is supported by 460 // this collector. The default implementation returns false. 461 virtual bool supports_concurrent_gc_breakpoints() const; 462 463 // Provides a thread pool to SafepointSynchronize to use 464 // for parallel safepoint cleanup. 465 // GCs that use a GC worker thread pool may want to share 466 // it for use during safepoint cleanup. This is only possible 467 // if the GC can pause and resume concurrent work (e.g. G1 468 // concurrent marking) for an intermittent non-GC safepoint. 469 // If this method returns NULL, SafepointSynchronize will 470 // perform cleanup tasks serially in the VMThread. 471 virtual WorkGang* safepoint_workers() { return NULL; } 472 473 // Support for object pinning. This is used by JNI Get*Critical() 474 // and Release*Critical() family of functions. If supported, the GC 475 // must guarantee that pinned objects never move. 476 virtual bool supports_object_pinning() const; 477 virtual oop pin_object(JavaThread* thread, oop obj); 478 virtual void unpin_object(JavaThread* thread, oop obj); 479 480 // Is the given object inside a CDS archive area? 481 virtual bool is_archived_object(oop object) const; 482 483 virtual bool is_oop(oop object) const; 484 // Non product verification and debugging. 485 #ifndef PRODUCT 486 // Support for PromotionFailureALot. Return true if it's time to cause a 487 // promotion failure. The no-argument version uses 488 // this->_promotion_failure_alot_count as the counter. 489 bool promotion_should_fail(volatile size_t* count); 490 bool promotion_should_fail(); 491 492 // Reset the PromotionFailureALot counters. Should be called at the end of a 493 // GC in which promotion failure occurred. 494 void reset_promotion_should_fail(volatile size_t* count); 495 void reset_promotion_should_fail(); 496 #endif // #ifndef PRODUCT 497 }; 498 499 // Class to set and reset the GC cause for a CollectedHeap. 500 501 class GCCauseSetter : StackObj { 502 CollectedHeap* _heap; 503 GCCause::Cause _previous_cause; 504 public: 505 GCCauseSetter(CollectedHeap* heap, GCCause::Cause cause) { 506 _heap = heap; 507 _previous_cause = _heap->gc_cause(); 508 _heap->set_gc_cause(cause); 509 } 510 511 ~GCCauseSetter() { 512 _heap->set_gc_cause(_previous_cause); 513 } 514 }; 515 516 #endif // SHARE_GC_SHARED_COLLECTEDHEAP_HPP