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