1 /*
   2  * Copyright (c) 2001, 2010, 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_INTERFACE_COLLECTEDHEAP_HPP
  26 #define SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_HPP
  27 
  28 #include "gc_interface/gcCause.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/barrierSet.hpp"
  31 #include "runtime/handles.hpp"
  32 #include "runtime/perfData.hpp"
  33 #include "runtime/safepoint.hpp"
  34 
  35 // A "CollectedHeap" is an implementation of a java heap for HotSpot.  This
  36 // is an abstract class: there may be many different kinds of heaps.  This
  37 // class defines the functions that a heap must implement, and contains
  38 // infrastructure common to all heaps.
  39 
  40 class BarrierSet;
  41 class ThreadClosure;
  42 class AdaptiveSizePolicy;
  43 class Thread;
  44 class CollectorPolicy;
  45 
  46 //
  47 // CollectedHeap
  48 //   SharedHeap
  49 //     GenCollectedHeap
  50 //     G1CollectedHeap
  51 //   ParallelScavengeHeap
  52 //
  53 class CollectedHeap : public CHeapObj {
  54   friend class VMStructs;
  55   friend class IsGCActiveMark; // Block structured external access to _is_gc_active
  56   friend class constantPoolCacheKlass; // allocate() method inserts is_conc_safe
  57 
  58 #ifdef ASSERT
  59   static int       _fire_out_of_memory_count;
  60 #endif
  61 
  62   // Used for filler objects (static, but initialized in ctor).
  63   static size_t _filler_array_max_size;
  64 
  65   // Used in support of ReduceInitialCardMarks; only consulted if COMPILER2 is being used
  66   bool _defer_initial_card_mark;
  67 
  68  protected:
  69   MemRegion _reserved;
  70   BarrierSet* _barrier_set;
  71   bool _is_gc_active;
  72   int _n_par_threads;
  73 
  74   unsigned int _total_collections;          // ... started
  75   unsigned int _total_full_collections;     // ... started
  76   NOT_PRODUCT(volatile size_t _promotion_failure_alot_count;)
  77   NOT_PRODUCT(volatile size_t _promotion_failure_alot_gc_number;)
  78 
  79   // Reason for current garbage collection.  Should be set to
  80   // a value reflecting no collection between collections.
  81   GCCause::Cause _gc_cause;
  82   GCCause::Cause _gc_lastcause;
  83   PerfStringVariable* _perf_gc_cause;
  84   PerfStringVariable* _perf_gc_lastcause;
  85 
  86   // Constructor
  87   CollectedHeap();
  88 
  89   // Do common initializations that must follow instance construction,
  90   // for example, those needing virtual calls.
  91   // This code could perhaps be moved into initialize() but would
  92   // be slightly more awkward because we want the latter to be a
  93   // pure virtual.
  94   void pre_initialize();
  95 
  96   // Create a new tlab
  97   virtual HeapWord* allocate_new_tlab(size_t size);
  98 
  99   // Accumulate statistics on all tlabs.
 100   virtual void accumulate_statistics_all_tlabs();
 101 
 102   // Reinitialize tlabs before resuming mutators.
 103   virtual void resize_all_tlabs();
 104 
 105  protected:
 106   // Allocate from the current thread's TLAB, with broken-out slow path.
 107   inline static HeapWord* allocate_from_tlab(Thread* thread, size_t size);
 108   static HeapWord* allocate_from_tlab_slow(Thread* thread, size_t size);
 109 
 110   // Allocate an uninitialized block of the given size, or returns NULL if
 111   // this is impossible.
 112   inline static HeapWord* common_mem_allocate_noinit(size_t size, bool is_noref, TRAPS);
 113 
 114   // Like allocate_init, but the block returned by a successful allocation
 115   // is guaranteed initialized to zeros.
 116   inline static HeapWord* common_mem_allocate_init(size_t size, bool is_noref, TRAPS);
 117 
 118   // Same as common_mem version, except memory is allocated in the permanent area
 119   // If there is no permanent area, revert to common_mem_allocate_noinit
 120   inline static HeapWord* common_permanent_mem_allocate_noinit(size_t size, TRAPS);
 121 
 122   // Same as common_mem version, except memory is allocated in the permanent area
 123   // If there is no permanent area, revert to common_mem_allocate_init
 124   inline static HeapWord* common_permanent_mem_allocate_init(size_t size, TRAPS);
 125 
 126   // Helper functions for (VM) allocation.
 127   inline static void post_allocation_setup_common(KlassHandle klass,
 128                                                   HeapWord* obj, size_t size);
 129   inline static void post_allocation_setup_no_klass_install(KlassHandle klass,
 130                                                             HeapWord* objPtr,
 131                                                             size_t size);
 132 
 133   inline static void post_allocation_setup_obj(KlassHandle klass,
 134                                                HeapWord* obj, size_t size);
 135 
 136   inline static void post_allocation_setup_array(KlassHandle klass,
 137                                                  HeapWord* obj, size_t size,
 138                                                  int length);
 139 
 140   // Clears an allocated object.
 141   inline static void init_obj(HeapWord* obj, size_t size);
 142 
 143   // Filler object utilities.
 144   static inline size_t filler_array_hdr_size();
 145   static inline size_t filler_array_min_size();
 146   static inline size_t filler_array_max_size();
 147 
 148   DEBUG_ONLY(static void fill_args_check(HeapWord* start, size_t words);)
 149   DEBUG_ONLY(static void zap_filler_array(HeapWord* start, size_t words, bool zap = true);)
 150 
 151   // Fill with a single array; caller must ensure filler_array_min_size() <=
 152   // words <= filler_array_max_size().
 153   static inline void fill_with_array(HeapWord* start, size_t words, bool zap = true);
 154 
 155   // Fill with a single object (either an int array or a java.lang.Object).
 156   static inline void fill_with_object_impl(HeapWord* start, size_t words, bool zap = true);
 157 
 158   // Verification functions
 159   virtual void check_for_bad_heap_word_value(HeapWord* addr, size_t size)
 160     PRODUCT_RETURN;
 161   virtual void check_for_non_bad_heap_word_value(HeapWord* addr, size_t size)
 162     PRODUCT_RETURN;
 163   debug_only(static void check_for_valid_allocation_state();)
 164 
 165  public:
 166   enum Name {
 167     Abstract,
 168     SharedHeap,
 169     GenCollectedHeap,
 170     ParallelScavengeHeap,
 171     G1CollectedHeap
 172   };
 173 
 174   virtual CollectedHeap::Name kind() const { return CollectedHeap::Abstract; }
 175 
 176   /**
 177    * Returns JNI error code JNI_ENOMEM if memory could not be allocated,
 178    * and JNI_OK on success.
 179    */
 180   virtual jint initialize() = 0;
 181 
 182   // In many heaps, there will be a need to perform some initialization activities
 183   // after the Universe is fully formed, but before general heap allocation is allowed.
 184   // This is the correct place to place such initialization methods.
 185   virtual void post_initialize() = 0;
 186 
 187   MemRegion reserved_region() const { return _reserved; }
 188   address base() const { return (address)reserved_region().start(); }
 189 
 190   // Future cleanup here. The following functions should specify bytes or
 191   // heapwords as part of their signature.
 192   virtual size_t capacity() const = 0;
 193   virtual size_t used() const = 0;
 194 
 195   // Return "true" if the part of the heap that allocates Java
 196   // objects has reached the maximal committed limit that it can
 197   // reach, without a garbage collection.
 198   virtual bool is_maximal_no_gc() const = 0;
 199 
 200   virtual size_t permanent_capacity() const = 0;
 201   virtual size_t permanent_used() const = 0;
 202 
 203   // Support for java.lang.Runtime.maxMemory():  return the maximum amount of
 204   // memory that the vm could make available for storing 'normal' java objects.
 205   // This is based on the reserved address space, but should not include space
 206   // that the vm uses internally for bookkeeping or temporary storage (e.g.,
 207   // perm gen space or, in the case of the young gen, one of the survivor
 208   // spaces).
 209   virtual size_t max_capacity() const = 0;
 210 
 211   // Returns "TRUE" if "p" points into the reserved area of the heap.
 212   bool is_in_reserved(const void* p) const {
 213     return _reserved.contains(p);
 214   }
 215 
 216   bool is_in_reserved_or_null(const void* p) const {
 217     return p == NULL || is_in_reserved(p);
 218   }
 219 
 220   // Returns "TRUE" if "p" points to the head of an allocated object in the
 221   // heap. Since this method can be expensive in general, we restrict its
 222   // use to assertion checking only.
 223   virtual bool is_in(const void* p) const = 0;
 224 
 225   bool is_in_or_null(const void* p) const {
 226     return p == NULL || is_in(p);
 227   }
 228 
 229   // Let's define some terms: a "closed" subset of a heap is one that
 230   //
 231   // 1) contains all currently-allocated objects, and
 232   //
 233   // 2) is closed under reference: no object in the closed subset
 234   //    references one outside the closed subset.
 235   //
 236   // Membership in a heap's closed subset is useful for assertions.
 237   // Clearly, the entire heap is a closed subset, so the default
 238   // implementation is to use "is_in_reserved".  But this may not be too
 239   // liberal to perform useful checking.  Also, the "is_in" predicate
 240   // defines a closed subset, but may be too expensive, since "is_in"
 241   // verifies that its argument points to an object head.  The
 242   // "closed_subset" method allows a heap to define an intermediate
 243   // predicate, allowing more precise checking than "is_in_reserved" at
 244   // lower cost than "is_in."
 245 
 246   // One important case is a heap composed of disjoint contiguous spaces,
 247   // such as the Garbage-First collector.  Such heaps have a convenient
 248   // closed subset consisting of the allocated portions of those
 249   // contiguous spaces.
 250 
 251   // Return "TRUE" iff the given pointer points into the heap's defined
 252   // closed subset (which defaults to the entire heap).
 253   virtual bool is_in_closed_subset(const void* p) const {
 254     return is_in_reserved(p);
 255   }
 256 
 257   bool is_in_closed_subset_or_null(const void* p) const {
 258     return p == NULL || is_in_closed_subset(p);
 259   }
 260 
 261   // XXX is_permanent() and is_in_permanent() should be better named
 262   // to distinguish one from the other.
 263 
 264   // Returns "TRUE" if "p" is allocated as "permanent" data.
 265   // If the heap does not use "permanent" data, returns the same
 266   // value is_in_reserved() would return.
 267   // NOTE: this actually returns true if "p" is in reserved space
 268   // for the space not that it is actually allocated (i.e. in committed
 269   // space). If you need the more conservative answer use is_permanent().
 270   virtual bool is_in_permanent(const void *p) const = 0;
 271 
 272   bool is_in_permanent_or_null(const void *p) const {
 273     return p == NULL || is_in_permanent(p);
 274   }
 275 
 276   // Returns "TRUE" if "p" is in the committed area of  "permanent" data.
 277   // If the heap does not use "permanent" data, returns the same
 278   // value is_in() would return.
 279   virtual bool is_permanent(const void *p) const = 0;
 280 
 281   bool is_permanent_or_null(const void *p) const {
 282     return p == NULL || is_permanent(p);
 283   }
 284 
 285   // An object is scavengable if its location may move during a scavenge.
 286   // (A scavenge is a GC which is not a full GC.)
 287   // Currently, this just means it is not perm (and not null).
 288   // This could change if we rethink what's in perm-gen.
 289   bool is_scavengable(const void *p) const {
 290     return !is_in_permanent_or_null(p);
 291   }
 292 
 293   // Returns "TRUE" if "p" is a method oop in the
 294   // current heap, with high probability. This predicate
 295   // is not stable, in general.
 296   bool is_valid_method(oop p) const;
 297 
 298   void set_gc_cause(GCCause::Cause v) {
 299      if (UsePerfData) {
 300        _gc_lastcause = _gc_cause;
 301        _perf_gc_lastcause->set_value(GCCause::to_string(_gc_lastcause));
 302        _perf_gc_cause->set_value(GCCause::to_string(v));
 303      }
 304     _gc_cause = v;
 305   }
 306   GCCause::Cause gc_cause() { return _gc_cause; }
 307 
 308   // Number of threads currently working on GC tasks.
 309   int n_par_threads() { return _n_par_threads; }
 310 
 311   // May be overridden to set additional parallelism.
 312   virtual void set_par_threads(int t) { _n_par_threads = t; };
 313 
 314   // Preload classes into the shared portion of the heap, and then dump
 315   // that data to a file so that it can be loaded directly by another
 316   // VM (then terminate).
 317   virtual void preload_and_dump(TRAPS) { ShouldNotReachHere(); }
 318 
 319   // General obj/array allocation facilities.
 320   inline static oop obj_allocate(KlassHandle klass, int size, TRAPS);
 321   inline static oop array_allocate(KlassHandle klass, int size, int length, TRAPS);
 322   inline static oop large_typearray_allocate(KlassHandle klass, int size, int length, TRAPS);
 323 
 324   // Special obj/array allocation facilities.
 325   // Some heaps may want to manage "permanent" data uniquely. These default
 326   // to the general routines if the heap does not support such handling.
 327   inline static oop permanent_obj_allocate(KlassHandle klass, int size, TRAPS);
 328   // permanent_obj_allocate_no_klass_install() does not do the installation of
 329   // the klass pointer in the newly created object (as permanent_obj_allocate()
 330   // above does).  This allows for a delay in the installation of the klass
 331   // pointer that is needed during the create of klassKlass's.  The
 332   // method post_allocation_install_obj_klass() is used to install the
 333   // klass pointer.
 334   inline static oop permanent_obj_allocate_no_klass_install(KlassHandle klass,
 335                                                             int size,
 336                                                             TRAPS);
 337   inline static void post_allocation_install_obj_klass(KlassHandle klass,
 338                                                        oop obj,
 339                                                        int size);
 340   inline static oop permanent_array_allocate(KlassHandle klass, int size, int length, TRAPS);
 341 
 342   // Raw memory allocation facilities
 343   // The obj and array allocate methods are covers for these methods.
 344   // The permanent allocation method should default to mem_allocate if
 345   // permanent memory isn't supported.
 346   virtual HeapWord* mem_allocate(size_t size,
 347                                  bool is_noref,
 348                                  bool is_tlab,
 349                                  bool* gc_overhead_limit_was_exceeded) = 0;
 350   virtual HeapWord* permanent_mem_allocate(size_t size) = 0;
 351 
 352   // The boundary between a "large" and "small" array of primitives, in words.
 353   virtual size_t large_typearray_limit() = 0;
 354 
 355   // Utilities for turning raw memory into filler objects.
 356   //
 357   // min_fill_size() is the smallest region that can be filled.
 358   // fill_with_objects() can fill arbitrary-sized regions of the heap using
 359   // multiple objects.  fill_with_object() is for regions known to be smaller
 360   // than the largest array of integers; it uses a single object to fill the
 361   // region and has slightly less overhead.
 362   static size_t min_fill_size() {
 363     return size_t(align_object_size(oopDesc::header_size()));
 364   }
 365 
 366   static void fill_with_objects(HeapWord* start, size_t words, bool zap = true);
 367 
 368   static void fill_with_object(HeapWord* start, size_t words, bool zap = true);
 369   static void fill_with_object(MemRegion region, bool zap = true) {
 370     fill_with_object(region.start(), region.word_size(), zap);
 371   }
 372   static void fill_with_object(HeapWord* start, HeapWord* end, bool zap = true) {
 373     fill_with_object(start, pointer_delta(end, start), zap);
 374   }
 375 
 376   // Some heaps may offer a contiguous region for shared non-blocking
 377   // allocation, via inlined code (by exporting the address of the top and
 378   // end fields defining the extent of the contiguous allocation region.)
 379 
 380   // This function returns "true" iff the heap supports this kind of
 381   // allocation.  (Default is "no".)
 382   virtual bool supports_inline_contig_alloc() const {
 383     return false;
 384   }
 385   // These functions return the addresses of the fields that define the
 386   // boundaries of the contiguous allocation area.  (These fields should be
 387   // physically near to one another.)
 388   virtual HeapWord** top_addr() const {
 389     guarantee(false, "inline contiguous allocation not supported");
 390     return NULL;
 391   }
 392   virtual HeapWord** end_addr() const {
 393     guarantee(false, "inline contiguous allocation not supported");
 394     return NULL;
 395   }
 396 
 397   // Some heaps may be in an unparseable state at certain times between
 398   // collections. This may be necessary for efficient implementation of
 399   // certain allocation-related activities. Calling this function before
 400   // attempting to parse a heap ensures that the heap is in a parsable
 401   // state (provided other concurrent activity does not introduce
 402   // unparsability). It is normally expected, therefore, that this
 403   // method is invoked with the world stopped.
 404   // NOTE: if you override this method, make sure you call
 405   // super::ensure_parsability so that the non-generational
 406   // part of the work gets done. See implementation of
 407   // CollectedHeap::ensure_parsability and, for instance,
 408   // that of GenCollectedHeap::ensure_parsability().
 409   // The argument "retire_tlabs" controls whether existing TLABs
 410   // are merely filled or also retired, thus preventing further
 411   // allocation from them and necessitating allocation of new TLABs.
 412   virtual void ensure_parsability(bool retire_tlabs);
 413 
 414   // Return an estimate of the maximum allocation that could be performed
 415   // without triggering any collection or expansion activity.  In a
 416   // generational collector, for example, this is probably the largest
 417   // allocation that could be supported (without expansion) in the youngest
 418   // generation.  It is "unsafe" because no locks are taken; the result
 419   // should be treated as an approximation, not a guarantee, for use in
 420   // heuristic resizing decisions.
 421   virtual size_t unsafe_max_alloc() = 0;
 422 
 423   // Section on thread-local allocation buffers (TLABs)
 424   // If the heap supports thread-local allocation buffers, it should override
 425   // the following methods:
 426   // Returns "true" iff the heap supports thread-local allocation buffers.
 427   // The default is "no".
 428   virtual bool supports_tlab_allocation() const {
 429     return false;
 430   }
 431   // The amount of space available for thread-local allocation buffers.
 432   virtual size_t tlab_capacity(Thread *thr) const {
 433     guarantee(false, "thread-local allocation buffers not supported");
 434     return 0;
 435   }
 436   // An estimate of the maximum allocation that could be performed
 437   // for thread-local allocation buffers without triggering any
 438   // collection or expansion activity.
 439   virtual size_t unsafe_max_tlab_alloc(Thread *thr) const {
 440     guarantee(false, "thread-local allocation buffers not supported");
 441     return 0;
 442   }
 443 
 444   // Can a compiler initialize a new object without store barriers?
 445   // This permission only extends from the creation of a new object
 446   // via a TLAB up to the first subsequent safepoint. If such permission
 447   // is granted for this heap type, the compiler promises to call
 448   // defer_store_barrier() below on any slow path allocation of
 449   // a new object for which such initializing store barriers will
 450   // have been elided.
 451   virtual bool can_elide_tlab_store_barriers() const = 0;
 452 
 453   // If a compiler is eliding store barriers for TLAB-allocated objects,
 454   // there is probably a corresponding slow path which can produce
 455   // an object allocated anywhere.  The compiler's runtime support
 456   // promises to call this function on such a slow-path-allocated
 457   // object before performing initializations that have elided
 458   // store barriers. Returns new_obj, or maybe a safer copy thereof.
 459   virtual oop new_store_pre_barrier(JavaThread* thread, oop new_obj);
 460 
 461   // Answers whether an initializing store to a new object currently
 462   // allocated at the given address doesn't need a store
 463   // barrier. Returns "true" if it doesn't need an initializing
 464   // store barrier; answers "false" if it does.
 465   virtual bool can_elide_initializing_store_barrier(oop new_obj) = 0;
 466 
 467   // If a compiler is eliding store barriers for TLAB-allocated objects,
 468   // we will be informed of a slow-path allocation by a call
 469   // to new_store_pre_barrier() above. Such a call precedes the
 470   // initialization of the object itself, and no post-store-barriers will
 471   // be issued. Some heap types require that the barrier strictly follows
 472   // the initializing stores. (This is currently implemented by deferring the
 473   // barrier until the next slow-path allocation or gc-related safepoint.)
 474   // This interface answers whether a particular heap type needs the card
 475   // mark to be thus strictly sequenced after the stores.
 476   virtual bool card_mark_must_follow_store() const = 0;
 477 
 478   // If the CollectedHeap was asked to defer a store barrier above,
 479   // this informs it to flush such a deferred store barrier to the
 480   // remembered set.
 481   virtual void flush_deferred_store_barrier(JavaThread* thread);
 482 
 483   // Can a compiler elide a store barrier when it writes
 484   // a permanent oop into the heap?  Applies when the compiler
 485   // is storing x to the heap, where x->is_perm() is true.
 486   virtual bool can_elide_permanent_oop_store_barriers() const = 0;
 487 
 488   // Does this heap support heap inspection (+PrintClassHistogram?)
 489   virtual bool supports_heap_inspection() const = 0;
 490 
 491   // Perform a collection of the heap; intended for use in implementing
 492   // "System.gc".  This probably implies as full a collection as the
 493   // "CollectedHeap" supports.
 494   virtual void collect(GCCause::Cause cause) = 0;
 495 
 496   // This interface assumes that it's being called by the
 497   // vm thread. It collects the heap assuming that the
 498   // heap lock is already held and that we are executing in
 499   // the context of the vm thread.
 500   virtual void collect_as_vm_thread(GCCause::Cause cause) = 0;
 501 
 502   // Returns the barrier set for this heap
 503   BarrierSet* barrier_set() { return _barrier_set; }
 504 
 505   // Returns "true" iff there is a stop-world GC in progress.  (I assume
 506   // that it should answer "false" for the concurrent part of a concurrent
 507   // collector -- dld).
 508   bool is_gc_active() const { return _is_gc_active; }
 509 
 510   // Total number of GC collections (started)
 511   unsigned int total_collections() const { return _total_collections; }
 512   unsigned int total_full_collections() const { return _total_full_collections;}
 513 
 514   // Increment total number of GC collections (started)
 515   // Should be protected but used by PSMarkSweep - cleanup for 1.4.2
 516   void increment_total_collections(bool full = false) {
 517     _total_collections++;
 518     if (full) {
 519       increment_total_full_collections();
 520     }
 521   }
 522 
 523   void increment_total_full_collections() { _total_full_collections++; }
 524 
 525   // Return the AdaptiveSizePolicy for the heap.
 526   virtual AdaptiveSizePolicy* size_policy() = 0;
 527 
 528   // Return the CollectorPolicy for the heap
 529   virtual CollectorPolicy* collector_policy() const = 0;
 530 
 531   // Iterate over all the ref-containing fields of all objects, calling
 532   // "cl.do_oop" on each. This includes objects in permanent memory.
 533   virtual void oop_iterate(OopClosure* cl) = 0;
 534 
 535   // Iterate over all objects, calling "cl.do_object" on each.
 536   // This includes objects in permanent memory.
 537   virtual void object_iterate(ObjectClosure* cl) = 0;
 538 
 539   // Similar to object_iterate() except iterates only
 540   // over live objects.
 541   virtual void safe_object_iterate(ObjectClosure* cl) = 0;
 542 
 543   // Behaves the same as oop_iterate, except only traverses
 544   // interior pointers contained in permanent memory. If there
 545   // is no permanent memory, does nothing.
 546   virtual void permanent_oop_iterate(OopClosure* cl) = 0;
 547 
 548   // Behaves the same as object_iterate, except only traverses
 549   // object contained in permanent memory. If there is no
 550   // permanent memory, does nothing.
 551   virtual void permanent_object_iterate(ObjectClosure* cl) = 0;
 552 
 553   // NOTE! There is no requirement that a collector implement these
 554   // functions.
 555   //
 556   // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
 557   // each address in the (reserved) heap is a member of exactly
 558   // one block.  The defining characteristic of a block is that it is
 559   // possible to find its size, and thus to progress forward to the next
 560   // block.  (Blocks may be of different sizes.)  Thus, blocks may
 561   // represent Java objects, or they might be free blocks in a
 562   // free-list-based heap (or subheap), as long as the two kinds are
 563   // distinguishable and the size of each is determinable.
 564 
 565   // Returns the address of the start of the "block" that contains the
 566   // address "addr".  We say "blocks" instead of "object" since some heaps
 567   // may not pack objects densely; a chunk may either be an object or a
 568   // non-object.
 569   virtual HeapWord* block_start(const void* addr) const = 0;
 570 
 571   // Requires "addr" to be the start of a chunk, and returns its size.
 572   // "addr + size" is required to be the start of a new chunk, or the end
 573   // of the active area of the heap.
 574   virtual size_t block_size(const HeapWord* addr) const = 0;
 575 
 576   // Requires "addr" to be the start of a block, and returns "TRUE" iff
 577   // the block is an object.
 578   virtual bool block_is_obj(const HeapWord* addr) const = 0;
 579 
 580   // Returns the longest time (in ms) that has elapsed since the last
 581   // time that any part of the heap was examined by a garbage collection.
 582   virtual jlong millis_since_last_gc() = 0;
 583 
 584   // Perform any cleanup actions necessary before allowing a verification.
 585   virtual void prepare_for_verify() = 0;
 586 
 587   // Generate any dumps preceding or following a full gc
 588   void pre_full_gc_dump();
 589   void post_full_gc_dump();
 590 
 591   virtual void print() const = 0;
 592   virtual void print_on(outputStream* st) const = 0;
 593 
 594   // Print all GC threads (other than the VM thread)
 595   // used by this heap.
 596   virtual void print_gc_threads_on(outputStream* st) const = 0;
 597   void print_gc_threads() { print_gc_threads_on(tty); }
 598   // Iterator for all GC threads (other than VM thread)
 599   virtual void gc_threads_do(ThreadClosure* tc) const = 0;
 600 
 601   // Print any relevant tracing info that flags imply.
 602   // Default implementation does nothing.
 603   virtual void print_tracing_info() const = 0;
 604 
 605   // Heap verification
 606   virtual void verify(bool allow_dirty, bool silent, bool option) = 0;
 607 
 608   // Non product verification and debugging.
 609 #ifndef PRODUCT
 610   // Support for PromotionFailureALot.  Return true if it's time to cause a
 611   // promotion failure.  The no-argument version uses
 612   // this->_promotion_failure_alot_count as the counter.
 613   inline bool promotion_should_fail(volatile size_t* count);
 614   inline bool promotion_should_fail();
 615 
 616   // Reset the PromotionFailureALot counters.  Should be called at the end of a
 617   // GC in which promotion failure ocurred.
 618   inline void reset_promotion_should_fail(volatile size_t* count);
 619   inline void reset_promotion_should_fail();
 620 #endif  // #ifndef PRODUCT
 621 
 622 #ifdef ASSERT
 623   static int fired_fake_oom() {
 624     return (CIFireOOMAt > 1 && _fire_out_of_memory_count >= CIFireOOMAt);
 625   }
 626 #endif
 627 
 628  public:
 629   // This is a convenience method that is used in cases where
 630   // the actual number of GC worker threads is not pertinent but
 631   // only whether there more than 0.  Use of this method helps
 632   // reduce the occurrence of ParallelGCThreads to uses where the
 633   // actual number may be germane.
 634   static bool use_parallel_gc_threads() { return ParallelGCThreads > 0; }
 635 };
 636 
 637 // Class to set and reset the GC cause for a CollectedHeap.
 638 
 639 class GCCauseSetter : StackObj {
 640   CollectedHeap* _heap;
 641   GCCause::Cause _previous_cause;
 642  public:
 643   GCCauseSetter(CollectedHeap* heap, GCCause::Cause cause) {
 644     assert(SafepointSynchronize::is_at_safepoint(),
 645            "This method manipulates heap state without locking");
 646     _heap = heap;
 647     _previous_cause = _heap->gc_cause();
 648     _heap->set_gc_cause(cause);
 649   }
 650 
 651   ~GCCauseSetter() {
 652     assert(SafepointSynchronize::is_at_safepoint(),
 653           "This method manipulates heap state without locking");
 654     _heap->set_gc_cause(_previous_cause);
 655   }
 656 };
 657 
 658 #endif // SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_HPP