1 /* 2 * Copyright (c) 2001, 2017, 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_VM_GC_SHARED_COLLECTEDHEAP_HPP 26 #define SHARE_VM_GC_SHARED_COLLECTEDHEAP_HPP 27 28 #include "gc/shared/gcCause.hpp" 29 #include "gc/shared/gcWhen.hpp" 30 #include "memory/allocation.hpp" 31 #include "runtime/handles.hpp" 32 #include "runtime/perfData.hpp" 33 #include "runtime/safepoint.hpp" 34 #include "utilities/events.hpp" 35 36 // A "CollectedHeap" is an implementation of a java heap for HotSpot. This 37 // is an abstract class: there may be many different kinds of heaps. This 38 // class defines the functions that a heap must implement, and contains 39 // infrastructure common to all heaps. 40 41 class AdaptiveSizePolicy; 42 class BarrierSet; 43 class CollectorPolicy; 44 class GCHeapSummary; 45 class GCTimer; 46 class GCTracer; 47 class MetaspaceSummary; 48 class Thread; 49 class ThreadClosure; 50 class VirtualSpaceSummary; 51 class nmethod; 52 53 class GCMessage : public FormatBuffer<1024> { 54 public: 55 bool is_before; 56 57 public: 58 GCMessage() {} 59 }; 60 61 class CollectedHeap; 62 63 class GCHeapLog : public EventLogBase<GCMessage> { 64 private: 65 void log_heap(CollectedHeap* heap, bool before); 66 67 public: 68 GCHeapLog() : EventLogBase<GCMessage>("GC Heap History") {} 69 70 void log_heap_before(CollectedHeap* heap) { 71 log_heap(heap, true); 72 } 73 void log_heap_after(CollectedHeap* heap) { 74 log_heap(heap, false); 75 } 76 }; 77 78 // 79 // CollectedHeap 80 // GenCollectedHeap 81 // G1CollectedHeap 82 // ParallelScavengeHeap 83 // 84 class CollectedHeap : public CHeapObj<mtInternal> { 85 friend class VMStructs; 86 friend class JVMCIVMStructs; 87 friend class IsGCActiveMark; // Block structured external access to _is_gc_active 88 89 private: 90 #ifdef ASSERT 91 static int _fire_out_of_memory_count; 92 #endif 93 94 GCHeapLog* _gc_heap_log; 95 96 MemRegion _reserved; 97 98 protected: 99 BarrierSet* _barrier_set; 100 bool _is_gc_active; 101 102 // Used for filler objects (static, but initialized in ctor). 103 static size_t _filler_array_max_size; 104 105 unsigned int _total_collections; // ... started 106 unsigned int _total_full_collections; // ... started 107 NOT_PRODUCT(volatile size_t _promotion_failure_alot_count;) 108 NOT_PRODUCT(volatile size_t _promotion_failure_alot_gc_number;) 109 110 // Reason for current garbage collection. Should be set to 111 // a value reflecting no collection between collections. 112 GCCause::Cause _gc_cause; 113 GCCause::Cause _gc_lastcause; 114 PerfStringVariable* _perf_gc_cause; 115 PerfStringVariable* _perf_gc_lastcause; 116 117 // Constructor 118 CollectedHeap(); 119 120 // Do common initializations that must follow instance construction, 121 // for example, those needing virtual calls. 122 // This code could perhaps be moved into initialize() but would 123 // be slightly more awkward because we want the latter to be a 124 // pure virtual. 125 void pre_initialize(); 126 127 // Create a new tlab. All TLAB allocations must go through this. 128 virtual HeapWord* allocate_new_tlab(size_t size); 129 130 // Accumulate statistics on all tlabs. 131 virtual void accumulate_statistics_all_tlabs(); 132 133 // Reinitialize tlabs before resuming mutators. 134 virtual void resize_all_tlabs(); 135 136 // Allocate from the current thread's TLAB, with broken-out slow path. 137 inline static HeapWord* allocate_from_tlab(Klass* klass, Thread* thread, size_t size); 138 static HeapWord* allocate_from_tlab_slow(Klass* klass, Thread* thread, size_t size); 139 140 // Allocate an uninitialized block of the given size, or returns NULL if 141 // this is impossible. 142 inline static HeapWord* common_mem_allocate_noinit(Klass* klass, size_t size, TRAPS); 143 144 // Like allocate_init, but the block returned by a successful allocation 145 // is guaranteed initialized to zeros. 146 inline static HeapWord* common_mem_allocate_init(Klass* klass, size_t size, TRAPS); 147 148 // Helper functions for (VM) allocation. 149 inline static void post_allocation_setup_common(Klass* klass, HeapWord* obj); 150 inline static void post_allocation_setup_no_klass_install(Klass* klass, 151 HeapWord* objPtr); 152 153 inline static void post_allocation_setup_obj(Klass* klass, HeapWord* obj, int size); 154 155 inline static void post_allocation_setup_array(Klass* klass, 156 HeapWord* obj, int length); 157 158 inline static void post_allocation_setup_class(Klass* klass, HeapWord* obj, int size); 159 160 // Clears an allocated object. 161 inline static void init_obj(HeapWord* obj, size_t size); 162 163 // Filler object utilities. 164 static inline size_t filler_array_hdr_size(); 165 static inline size_t filler_array_min_size(); 166 167 DEBUG_ONLY(static void fill_args_check(HeapWord* start, size_t words);) 168 DEBUG_ONLY(static void zap_filler_array(HeapWord* start, size_t words, bool zap = true);) 169 170 // Fill with a single array; caller must ensure filler_array_min_size() <= 171 // words <= filler_array_max_size(). 172 static inline void fill_with_array(HeapWord* start, size_t words, bool zap = true); 173 174 // Fill with a single object (either an int array or a java.lang.Object). 175 static inline void fill_with_object_impl(HeapWord* start, size_t words, bool zap = true); 176 177 virtual void trace_heap(GCWhen::Type when, const GCTracer* tracer); 178 179 // Verification functions 180 virtual void check_for_bad_heap_word_value(HeapWord* addr, size_t size) 181 PRODUCT_RETURN; 182 virtual void check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) 183 PRODUCT_RETURN; 184 debug_only(static void check_for_valid_allocation_state();) 185 186 public: 187 enum Name { 188 GenCollectedHeap, 189 ParallelScavengeHeap, 190 G1CollectedHeap 191 }; 192 193 static inline size_t filler_array_max_size() { 194 return _filler_array_max_size; 195 } 196 197 virtual Name kind() const = 0; 198 199 virtual const char* name() const = 0; 200 201 /** 202 * Returns JNI error code JNI_ENOMEM if memory could not be allocated, 203 * and JNI_OK on success. 204 */ 205 virtual jint initialize() = 0; 206 207 // In many heaps, there will be a need to perform some initialization activities 208 // after the Universe is fully formed, but before general heap allocation is allowed. 209 // This is the correct place to place such initialization methods. 210 virtual void post_initialize() = 0; 211 212 // Stop any onging concurrent work and prepare for exit. 213 virtual void stop() {} 214 215 // Stop and resume concurrent GC threads interfering with safepoint operations 216 virtual void safepoint_synchronize_begin() {} 217 virtual void safepoint_synchronize_end() {} 218 219 void initialize_reserved_region(HeapWord *start, HeapWord *end); 220 MemRegion reserved_region() const { return _reserved; } 221 address base() const { return (address)reserved_region().start(); } 222 223 virtual size_t capacity() const = 0; 224 virtual size_t used() const = 0; 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" if "p" points into the reserved area of the heap. 240 bool is_in_reserved(const void* p) const { 241 return _reserved.contains(p); 242 } 243 244 bool is_in_reserved_or_null(const void* p) const { 245 return p == NULL || is_in_reserved(p); 246 } 247 248 // Returns "TRUE" iff "p" points into the committed areas of the heap. 249 // This method can be expensive so avoid using it in performance critical 250 // code. 251 virtual bool is_in(const void* p) const = 0; 252 253 DEBUG_ONLY(bool is_in_or_null(const void* p) const { return p == NULL || is_in(p); }) 254 255 // Let's define some terms: a "closed" subset of a heap is one that 256 // 257 // 1) contains all currently-allocated objects, and 258 // 259 // 2) is closed under reference: no object in the closed subset 260 // references one outside the closed subset. 261 // 262 // Membership in a heap's closed subset is useful for assertions. 263 // Clearly, the entire heap is a closed subset, so the default 264 // implementation is to use "is_in_reserved". But this may not be too 265 // liberal to perform useful checking. Also, the "is_in" predicate 266 // defines a closed subset, but may be too expensive, since "is_in" 267 // verifies that its argument points to an object head. The 268 // "closed_subset" method allows a heap to define an intermediate 269 // predicate, allowing more precise checking than "is_in_reserved" at 270 // lower cost than "is_in." 271 272 // One important case is a heap composed of disjoint contiguous spaces, 273 // such as the Garbage-First collector. Such heaps have a convenient 274 // closed subset consisting of the allocated portions of those 275 // contiguous spaces. 276 277 // Return "TRUE" iff the given pointer points into the heap's defined 278 // closed subset (which defaults to the entire heap). 279 virtual bool is_in_closed_subset(const void* p) const { 280 return is_in_reserved(p); 281 } 282 283 bool is_in_closed_subset_or_null(const void* p) const { 284 return p == NULL || is_in_closed_subset(p); 285 } 286 287 // An object is scavengable if its location may move during a scavenge. 288 // (A scavenge is a GC which is not a full GC.) 289 virtual bool is_scavengable(const void *p) = 0; 290 291 void set_gc_cause(GCCause::Cause v) { 292 if (UsePerfData) { 293 _gc_lastcause = _gc_cause; 294 _perf_gc_lastcause->set_value(GCCause::to_string(_gc_lastcause)); 295 _perf_gc_cause->set_value(GCCause::to_string(v)); 296 } 297 _gc_cause = v; 298 } 299 GCCause::Cause gc_cause() { return _gc_cause; } 300 301 // General obj/array allocation facilities. 302 inline static oop obj_allocate(Klass* klass, int size, TRAPS); 303 inline static oop array_allocate(Klass* klass, int size, int length, TRAPS); 304 inline static oop array_allocate_nozero(Klass* klass, int size, int length, TRAPS); 305 inline static oop class_allocate(Klass* klass, int size, TRAPS); 306 307 // Raw memory allocation facilities 308 // The obj and array allocate methods are covers for these methods. 309 // mem_allocate() should never be 310 // called to allocate TLABs, only individual objects. 311 virtual HeapWord* mem_allocate(size_t size, 312 bool* gc_overhead_limit_was_exceeded) = 0; 313 314 // Utilities for turning raw memory into filler objects. 315 // 316 // min_fill_size() is the smallest region that can be filled. 317 // fill_with_objects() can fill arbitrary-sized regions of the heap using 318 // multiple objects. fill_with_object() is for regions known to be smaller 319 // than the largest array of integers; it uses a single object to fill the 320 // region and has slightly less overhead. 321 static size_t min_fill_size() { 322 return size_t(align_object_size(oopDesc::header_size())); 323 } 324 325 static void fill_with_objects(HeapWord* start, size_t words, bool zap = true); 326 327 static void fill_with_object(HeapWord* start, size_t words, bool zap = true); 328 static void fill_with_object(MemRegion region, bool zap = true) { 329 fill_with_object(region.start(), region.word_size(), zap); 330 } 331 static void fill_with_object(HeapWord* start, HeapWord* end, bool zap = true) { 332 fill_with_object(start, pointer_delta(end, start), zap); 333 } 334 335 // Return the address "addr" aligned by "alignment_in_bytes" if such 336 // an address is below "end". Return NULL otherwise. 337 inline static HeapWord* align_allocation_or_fail(HeapWord* addr, 338 HeapWord* end, 339 unsigned short alignment_in_bytes); 340 341 // Some heaps may offer a contiguous region for shared non-blocking 342 // allocation, via inlined code (by exporting the address of the top and 343 // end fields defining the extent of the contiguous allocation region.) 344 345 // This function returns "true" iff the heap supports this kind of 346 // allocation. (Default is "no".) 347 virtual bool supports_inline_contig_alloc() const { 348 return false; 349 } 350 // These functions return the addresses of the fields that define the 351 // boundaries of the contiguous allocation area. (These fields should be 352 // physically near to one another.) 353 virtual HeapWord* volatile* top_addr() const { 354 guarantee(false, "inline contiguous allocation not supported"); 355 return NULL; 356 } 357 virtual HeapWord** end_addr() const { 358 guarantee(false, "inline contiguous allocation not supported"); 359 return NULL; 360 } 361 362 // Some heaps may be in an unparseable state at certain times between 363 // collections. This may be necessary for efficient implementation of 364 // certain allocation-related activities. Calling this function before 365 // attempting to parse a heap ensures that the heap is in a parsable 366 // state (provided other concurrent activity does not introduce 367 // unparsability). It is normally expected, therefore, that this 368 // method is invoked with the world stopped. 369 // NOTE: if you override this method, make sure you call 370 // super::ensure_parsability so that the non-generational 371 // part of the work gets done. See implementation of 372 // CollectedHeap::ensure_parsability and, for instance, 373 // that of GenCollectedHeap::ensure_parsability(). 374 // The argument "retire_tlabs" controls whether existing TLABs 375 // are merely filled or also retired, thus preventing further 376 // allocation from them and necessitating allocation of new TLABs. 377 virtual void ensure_parsability(bool retire_tlabs); 378 379 // Section on thread-local allocation buffers (TLABs) 380 // If the heap supports thread-local allocation buffers, it should override 381 // the following methods: 382 // Returns "true" iff the heap supports thread-local allocation buffers. 383 // The default is "no". 384 virtual bool supports_tlab_allocation() const = 0; 385 386 // The amount of space available for thread-local allocation buffers. 387 virtual size_t tlab_capacity(Thread *thr) const = 0; 388 389 // The amount of used space for thread-local allocation buffers for the given thread. 390 virtual size_t tlab_used(Thread *thr) const = 0; 391 392 virtual size_t max_tlab_size() const; 393 394 virtual void verify_nmethod_roots(nmethod* nmethod); 395 396 // An estimate of the maximum allocation that could be performed 397 // for thread-local allocation buffers without triggering any 398 // collection or expansion activity. 399 virtual size_t unsafe_max_tlab_alloc(Thread *thr) const { 400 guarantee(false, "thread-local allocation buffers not supported"); 401 return 0; 402 } 403 404 // Perform a collection of the heap; intended for use in implementing 405 // "System.gc". This probably implies as full a collection as the 406 // "CollectedHeap" supports. 407 virtual void collect(GCCause::Cause cause) = 0; 408 409 // Perform a full collection 410 virtual void do_full_collection(bool clear_all_soft_refs) = 0; 411 412 // This interface assumes that it's being called by the 413 // vm thread. It collects the heap assuming that the 414 // heap lock is already held and that we are executing in 415 // the context of the vm thread. 416 virtual void collect_as_vm_thread(GCCause::Cause cause); 417 418 // Returns the barrier set for this heap 419 BarrierSet* barrier_set() { return _barrier_set; } 420 void set_barrier_set(BarrierSet* barrier_set); 421 422 // Returns "true" iff there is a stop-world GC in progress. (I assume 423 // that it should answer "false" for the concurrent part of a concurrent 424 // collector -- dld). 425 bool is_gc_active() const { return _is_gc_active; } 426 427 // Total number of GC collections (started) 428 unsigned int total_collections() const { return _total_collections; } 429 unsigned int total_full_collections() const { return _total_full_collections;} 430 431 // Increment total number of GC collections (started) 432 // Should be protected but used by PSMarkSweep - cleanup for 1.4.2 433 void increment_total_collections(bool full = false) { 434 _total_collections++; 435 if (full) { 436 increment_total_full_collections(); 437 } 438 } 439 440 void increment_total_full_collections() { _total_full_collections++; } 441 442 // Return the AdaptiveSizePolicy for the heap. 443 virtual AdaptiveSizePolicy* size_policy() = 0; 444 445 // Return the CollectorPolicy for the heap 446 virtual CollectorPolicy* collector_policy() const = 0; 447 448 // Iterate over all objects, calling "cl.do_object" on each. 449 virtual void object_iterate(ObjectClosure* cl) = 0; 450 451 // Similar to object_iterate() except iterates only 452 // over live objects. 453 virtual void safe_object_iterate(ObjectClosure* cl) = 0; 454 455 // NOTE! There is no requirement that a collector implement these 456 // functions. 457 // 458 // A CollectedHeap is divided into a dense sequence of "blocks"; that is, 459 // each address in the (reserved) heap is a member of exactly 460 // one block. The defining characteristic of a block is that it is 461 // possible to find its size, and thus to progress forward to the next 462 // block. (Blocks may be of different sizes.) Thus, blocks may 463 // represent Java objects, or they might be free blocks in a 464 // free-list-based heap (or subheap), as long as the two kinds are 465 // distinguishable and the size of each is determinable. 466 467 // Returns the address of the start of the "block" that contains the 468 // address "addr". We say "blocks" instead of "object" since some heaps 469 // may not pack objects densely; a chunk may either be an object or a 470 // non-object. 471 virtual HeapWord* block_start(const void* addr) const = 0; 472 473 // Requires "addr" to be the start of a chunk, and returns its size. 474 // "addr + size" is required to be the start of a new chunk, or the end 475 // of the active area of the heap. 476 virtual size_t block_size(const HeapWord* addr) const = 0; 477 478 // Requires "addr" to be the start of a block, and returns "TRUE" iff 479 // the block is an object. 480 virtual bool block_is_obj(const HeapWord* addr) const = 0; 481 482 // Returns the longest time (in ms) that has elapsed since the last 483 // time that any part of the heap was examined by a garbage collection. 484 virtual jlong millis_since_last_gc() = 0; 485 486 // Perform any cleanup actions necessary before allowing a verification. 487 virtual void prepare_for_verify() = 0; 488 489 // Generate any dumps preceding or following a full gc 490 private: 491 void full_gc_dump(GCTimer* timer, bool before); 492 public: 493 void pre_full_gc_dump(GCTimer* timer); 494 void post_full_gc_dump(GCTimer* timer); 495 496 VirtualSpaceSummary create_heap_space_summary(); 497 GCHeapSummary create_heap_summary(); 498 499 MetaspaceSummary create_metaspace_summary(); 500 501 // Print heap information on the given outputStream. 502 virtual void print_on(outputStream* st) const = 0; 503 // The default behavior is to call print_on() on tty. 504 virtual void print() const { 505 print_on(tty); 506 } 507 // Print more detailed heap information on the given 508 // outputStream. The default behavior is to call print_on(). It is 509 // up to each subclass to override it and add any additional output 510 // it needs. 511 virtual void print_extended_on(outputStream* st) const { 512 print_on(st); 513 } 514 515 virtual void print_on_error(outputStream* st) const; 516 517 // Print all GC threads (other than the VM thread) 518 // used by this heap. 519 virtual void print_gc_threads_on(outputStream* st) const = 0; 520 // The default behavior is to call print_gc_threads_on() on tty. 521 void print_gc_threads() { 522 print_gc_threads_on(tty); 523 } 524 // Iterator for all GC threads (other than VM thread) 525 virtual void gc_threads_do(ThreadClosure* tc) const = 0; 526 527 // Print any relevant tracing info that flags imply. 528 // Default implementation does nothing. 529 virtual void print_tracing_info() const = 0; 530 531 void print_heap_before_gc(); 532 void print_heap_after_gc(); 533 534 // Registering and unregistering an nmethod (compiled code) with the heap. 535 // Override with specific mechanism for each specialized heap type. 536 virtual void register_nmethod(nmethod* nm); 537 virtual void unregister_nmethod(nmethod* nm); 538 539 void trace_heap_before_gc(const GCTracer* gc_tracer); 540 void trace_heap_after_gc(const GCTracer* gc_tracer); 541 542 // Heap verification 543 virtual void verify(VerifyOption option) = 0; 544 545 // Return true if concurrent phase control (via 546 // request_concurrent_phase_control) is supported by this collector. 547 // The default implementation returns false. 548 virtual bool supports_concurrent_phase_control() const; 549 550 // Return a NULL terminated array of concurrent phase names provided 551 // by this collector. Supports Whitebox testing. These are the 552 // names recognized by request_concurrent_phase(). The default 553 // implementation returns an array of one NULL element. 554 virtual const char* const* concurrent_phases() const; 555 556 // Request the collector enter the indicated concurrent phase, and 557 // wait until it does so. Supports WhiteBox testing. Only one 558 // request may be active at a time. Phases are designated by name; 559 // the set of names and their meaning is GC-specific. Once the 560 // requested phase has been reached, the collector will attempt to 561 // avoid transitioning to a new phase until a new request is made. 562 // [Note: A collector might not be able to remain in a given phase. 563 // For example, a full collection might cancel an in-progress 564 // concurrent collection.] 565 // 566 // Returns true when the phase is reached. Returns false for an 567 // unknown phase. The default implementation returns false. 568 virtual bool request_concurrent_phase(const char* phase); 569 570 // Non product verification and debugging. 571 #ifndef PRODUCT 572 // Support for PromotionFailureALot. Return true if it's time to cause a 573 // promotion failure. The no-argument version uses 574 // this->_promotion_failure_alot_count as the counter. 575 inline bool promotion_should_fail(volatile size_t* count); 576 inline bool promotion_should_fail(); 577 578 // Reset the PromotionFailureALot counters. Should be called at the end of a 579 // GC in which promotion failure occurred. 580 inline void reset_promotion_should_fail(volatile size_t* count); 581 inline void reset_promotion_should_fail(); 582 #endif // #ifndef PRODUCT 583 584 #ifdef ASSERT 585 static int fired_fake_oom() { 586 return (CIFireOOMAt > 1 && _fire_out_of_memory_count >= CIFireOOMAt); 587 } 588 #endif 589 590 public: 591 // Copy the current allocation context statistics for the specified contexts. 592 // For each context in contexts, set the corresponding entries in the totals 593 // and accuracy arrays to the current values held by the statistics. Each 594 // array should be of length len. 595 // Returns true if there are more stats available. 596 virtual bool copy_allocation_context_stats(const jint* contexts, 597 jlong* totals, 598 jbyte* accuracy, 599 jint len) { 600 return false; 601 } 602 603 }; 604 605 // Class to set and reset the GC cause for a CollectedHeap. 606 607 class GCCauseSetter : StackObj { 608 CollectedHeap* _heap; 609 GCCause::Cause _previous_cause; 610 public: 611 GCCauseSetter(CollectedHeap* heap, GCCause::Cause cause) { 612 assert(SafepointSynchronize::is_at_safepoint(), 613 "This method manipulates heap state without locking"); 614 _heap = heap; 615 _previous_cause = _heap->gc_cause(); 616 _heap->set_gc_cause(cause); 617 } 618 619 ~GCCauseSetter() { 620 assert(SafepointSynchronize::is_at_safepoint(), 621 "This method manipulates heap state without locking"); 622 _heap->set_gc_cause(_previous_cause); 623 } 624 }; 625 626 #endif // SHARE_VM_GC_SHARED_COLLECTEDHEAP_HPP