1 /*
   2  * Copyright (c) 2000, 2015, 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_MEMORY_GENCOLLECTEDHEAP_HPP
  26 #define SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP
  27 
  28 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
  29 #include "memory/collectorPolicy.hpp"
  30 #include "memory/generation.hpp"
  31 #include "memory/sharedHeap.hpp"
  32 
  33 class SubTasksDone;
  34 
  35 // A "GenCollectedHeap" is a SharedHeap that uses generational
  36 // collection.  It is represented with a sequence of Generation's.
  37 class GenCollectedHeap : public SharedHeap {
  38   friend class GenCollectorPolicy;
  39   friend class Generation;
  40   friend class DefNewGeneration;
  41   friend class TenuredGeneration;
  42   friend class ConcurrentMarkSweepGeneration;
  43   friend class CMSCollector;
  44   friend class GenMarkSweep;
  45   friend class VM_GenCollectForAllocation;
  46   friend class VM_GenCollectFull;
  47   friend class VM_GenCollectFullConcurrent;
  48   friend class VM_GC_HeapInspection;
  49   friend class VM_HeapDumper;
  50   friend class HeapInspection;
  51   friend class GCCauseSetter;
  52   friend class VMStructs;
  53 public:
  54   enum SomeConstants {
  55     max_gens = 10
  56   };
  57 
  58   friend class VM_PopulateDumpSharedSpace;
  59 
  60  protected:
  61   // Fields:
  62   static GenCollectedHeap* _gch;
  63 
  64  private:
  65   int _n_gens;
  66   Generation* _gens[max_gens];
  67   GenerationSpec** _gen_specs;
  68 
  69   // The singleton Gen Remembered Set.
  70   GenRemSet* _rem_set;
  71 
  72   // The generational collector policy.
  73   GenCollectorPolicy* _gen_policy;
  74 
  75   // Indicates that the most recent previous incremental collection failed.
  76   // The flag is cleared when an action is taken that might clear the
  77   // condition that caused that incremental collection to fail.
  78   bool _incremental_collection_failed;
  79 
  80   // In support of ExplicitGCInvokesConcurrent functionality
  81   unsigned int _full_collections_completed;
  82 
  83   // Data structure for claiming the (potentially) parallel tasks in
  84   // (gen-specific) roots processing.
  85   SubTasksDone* _gen_process_roots_tasks;
  86   SubTasksDone* gen_process_roots_tasks() { return _gen_process_roots_tasks; }
  87 
  88   // In block contents verification, the number of header words to skip
  89   NOT_PRODUCT(static size_t _skip_header_HeapWords;)
  90 
  91 protected:
  92   // Helper functions for allocation
  93   HeapWord* attempt_allocation(size_t size,
  94                                bool   is_tlab,
  95                                bool   first_only);
  96 
  97   // Helper function for two callbacks below.
  98   // Considers collection of the first max_level+1 generations.
  99   void do_collection(bool   full,
 100                      bool   clear_all_soft_refs,
 101                      size_t size,
 102                      bool   is_tlab,
 103                      int    max_level);
 104 
 105   // Callback from VM_GenCollectForAllocation operation.
 106   // This function does everything necessary/possible to satisfy an
 107   // allocation request that failed in the youngest generation that should
 108   // have handled it (including collection, expansion, etc.)
 109   HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);
 110 
 111   // Callback from VM_GenCollectFull operation.
 112   // Perform a full collection of the first max_level+1 generations.
 113   virtual void do_full_collection(bool clear_all_soft_refs);
 114   void do_full_collection(bool clear_all_soft_refs, int max_level);
 115 
 116   // Does the "cause" of GC indicate that
 117   // we absolutely __must__ clear soft refs?
 118   bool must_clear_all_soft_refs();
 119 
 120 public:
 121   GenCollectedHeap(GenCollectorPolicy *policy);
 122 
 123   GCStats* gc_stats(int level) const;
 124 
 125   // Returns JNI_OK on success
 126   virtual jint initialize();
 127 
 128   // Reserve aligned space for the heap as needed by the contained generations.
 129   char* allocate(size_t alignment, ReservedSpace* heap_rs);
 130 
 131   // Does operations required after initialization has been done.
 132   void post_initialize();
 133 
 134   // Initialize ("weak") refs processing support
 135   virtual void ref_processing_init();
 136 
 137   virtual CollectedHeap::Name kind() const {
 138     return CollectedHeap::GenCollectedHeap;
 139   }
 140 
 141   // The generational collector policy.
 142   GenCollectorPolicy* gen_policy() const { return _gen_policy; }
 143   virtual CollectorPolicy* collector_policy() const { return (CollectorPolicy*) gen_policy(); }
 144 
 145   // Adaptive size policy
 146   virtual AdaptiveSizePolicy* size_policy() {
 147     return gen_policy()->size_policy();
 148   }
 149 
 150   // Return the (conservative) maximum heap alignment
 151   static size_t conservative_max_heap_alignment() {
 152     return Generation::GenGrain;
 153   }
 154 
 155   size_t capacity() const;
 156   size_t used() const;
 157 
 158   // Save the "used_region" for generations level and lower.
 159   void save_used_regions(int level);
 160 
 161   size_t max_capacity() const;
 162 
 163   HeapWord* mem_allocate(size_t size,
 164                          bool*  gc_overhead_limit_was_exceeded);
 165 
 166   // We may support a shared contiguous allocation area, if the youngest
 167   // generation does.
 168   bool supports_inline_contig_alloc() const;
 169   HeapWord** top_addr() const;
 170   HeapWord** end_addr() const;
 171 
 172   // Does this heap support heap inspection? (+PrintClassHistogram)
 173   virtual bool supports_heap_inspection() const { return true; }
 174 
 175   // Perform a full collection of the heap; intended for use in implementing
 176   // "System.gc". This implies as full a collection as the CollectedHeap
 177   // supports. Caller does not hold the Heap_lock on entry.
 178   void collect(GCCause::Cause cause);
 179 
 180   // The same as above but assume that the caller holds the Heap_lock.
 181   void collect_locked(GCCause::Cause cause);
 182 
 183   // Perform a full collection of the first max_level+1 generations.
 184   // Mostly used for testing purposes. Caller does not hold the Heap_lock on entry.
 185   void collect(GCCause::Cause cause, int max_level);
 186 
 187   // Returns "TRUE" iff "p" points into the committed areas of the heap.
 188   // The methods is_in(), is_in_closed_subset() and is_in_youngest() may
 189   // be expensive to compute in general, so, to prevent
 190   // their inadvertent use in product jvm's, we restrict their use to
 191   // assertion checking or verification only.
 192   bool is_in(const void* p) const;
 193 
 194   // override
 195   bool is_in_closed_subset(const void* p) const {
 196     if (UseConcMarkSweepGC) {
 197       return is_in_reserved(p);
 198     } else {
 199       return is_in(p);
 200     }
 201   }
 202 
 203   // Returns true if the reference is to an object in the reserved space
 204   // for the young generation.
 205   // Assumes the the young gen address range is less than that of the old gen.
 206   bool is_in_young(oop p);
 207 
 208 #ifdef ASSERT
 209   virtual bool is_in_partial_collection(const void* p);
 210 #endif
 211 
 212   virtual bool is_scavengable(const void* addr) {
 213     return is_in_young((oop)addr);
 214   }
 215 
 216   // Iteration functions.
 217   void oop_iterate(ExtendedOopClosure* cl);
 218   void object_iterate(ObjectClosure* cl);
 219   void safe_object_iterate(ObjectClosure* cl);
 220   Space* space_containing(const void* addr) const;
 221 
 222   // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
 223   // each address in the (reserved) heap is a member of exactly
 224   // one block.  The defining characteristic of a block is that it is
 225   // possible to find its size, and thus to progress forward to the next
 226   // block.  (Blocks may be of different sizes.)  Thus, blocks may
 227   // represent Java objects, or they might be free blocks in a
 228   // free-list-based heap (or subheap), as long as the two kinds are
 229   // distinguishable and the size of each is determinable.
 230 
 231   // Returns the address of the start of the "block" that contains the
 232   // address "addr".  We say "blocks" instead of "object" since some heaps
 233   // may not pack objects densely; a chunk may either be an object or a
 234   // non-object.
 235   virtual HeapWord* block_start(const void* addr) const;
 236 
 237   // Requires "addr" to be the start of a chunk, and returns its size.
 238   // "addr + size" is required to be the start of a new chunk, or the end
 239   // of the active area of the heap. Assumes (and verifies in non-product
 240   // builds) that addr is in the allocated part of the heap and is
 241   // the start of a chunk.
 242   virtual size_t block_size(const HeapWord* addr) const;
 243 
 244   // Requires "addr" to be the start of a block, and returns "TRUE" iff
 245   // the block is an object. Assumes (and verifies in non-product
 246   // builds) that addr is in the allocated part of the heap and is
 247   // the start of a chunk.
 248   virtual bool block_is_obj(const HeapWord* addr) const;
 249 
 250   // Section on TLAB's.
 251   virtual bool supports_tlab_allocation() const;
 252   virtual size_t tlab_capacity(Thread* thr) const;
 253   virtual size_t tlab_used(Thread* thr) const;
 254   virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
 255   virtual HeapWord* allocate_new_tlab(size_t size);
 256 
 257   // Can a compiler initialize a new object without store barriers?
 258   // This permission only extends from the creation of a new object
 259   // via a TLAB up to the first subsequent safepoint.
 260   virtual bool can_elide_tlab_store_barriers() const {
 261     return true;
 262   }
 263 
 264   virtual bool card_mark_must_follow_store() const {
 265     return UseConcMarkSweepGC;
 266   }
 267 
 268   // We don't need barriers for stores to objects in the
 269   // young gen and, a fortiori, for initializing stores to
 270   // objects therein. This applies to DefNew+Tenured and ParNew+CMS
 271   // only and may need to be re-examined in case other
 272   // kinds of collectors are implemented in the future.
 273   virtual bool can_elide_initializing_store_barrier(oop new_obj) {
 274     // We wanted to assert that:-
 275     // assert(UseSerialGC || UseConcMarkSweepGC,
 276     //       "Check can_elide_initializing_store_barrier() for this collector");
 277     // but unfortunately the flag UseSerialGC need not necessarily always
 278     // be set when DefNew+Tenured are being used.
 279     return is_in_young(new_obj);
 280   }
 281 
 282   // The "requestor" generation is performing some garbage collection
 283   // action for which it would be useful to have scratch space.  The
 284   // requestor promises to allocate no more than "max_alloc_words" in any
 285   // older generation (via promotion say.)   Any blocks of space that can
 286   // be provided are returned as a list of ScratchBlocks, sorted by
 287   // decreasing size.
 288   ScratchBlock* gather_scratch(Generation* requestor, size_t max_alloc_words);
 289   // Allow each generation to reset any scratch space that it has
 290   // contributed as it needs.
 291   void release_scratch();
 292 
 293   // Ensure parsability: override
 294   virtual void ensure_parsability(bool retire_tlabs);
 295 
 296   // Time in ms since the longest time a collector ran in
 297   // in any generation.
 298   virtual jlong millis_since_last_gc();
 299 
 300   // Total number of full collections completed.
 301   unsigned int total_full_collections_completed() {
 302     assert(_full_collections_completed <= _total_full_collections,
 303            "Can't complete more collections than were started");
 304     return _full_collections_completed;
 305   }
 306 
 307   // Update above counter, as appropriate, at the end of a stop-world GC cycle
 308   unsigned int update_full_collections_completed();
 309   // Update above counter, as appropriate, at the end of a concurrent GC cycle
 310   unsigned int update_full_collections_completed(unsigned int count);
 311 
 312   // Update "time of last gc" for all constituent generations
 313   // to "now".
 314   void update_time_of_last_gc(jlong now) {
 315     for (int i = 0; i < _n_gens; i++) {
 316       _gens[i]->update_time_of_last_gc(now);
 317     }
 318   }
 319 
 320   // Update the gc statistics for each generation.
 321   // "level" is the level of the latest collection.
 322   void update_gc_stats(int current_level, bool full) {
 323     for (int i = 0; i < _n_gens; i++) {
 324       _gens[i]->update_gc_stats(current_level, full);
 325     }
 326   }
 327 
 328   // Override.
 329   bool no_gc_in_progress() { return !is_gc_active(); }
 330 
 331   // Override.
 332   void prepare_for_verify();
 333 
 334   // Override.
 335   void verify(bool silent, VerifyOption option);
 336 
 337   // Override.
 338   virtual void print_on(outputStream* st) const;
 339   virtual void print_gc_threads_on(outputStream* st) const;
 340   virtual void gc_threads_do(ThreadClosure* tc) const;
 341   virtual void print_tracing_info() const;
 342   virtual void print_on_error(outputStream* st) const;
 343 
 344   // PrintGC, PrintGCDetails support
 345   void print_heap_change(size_t prev_used) const;
 346 
 347   // The functions below are helper functions that a subclass of
 348   // "CollectedHeap" can use in the implementation of its virtual
 349   // functions.
 350 
 351   class GenClosure : public StackObj {
 352    public:
 353     virtual void do_generation(Generation* gen) = 0;
 354   };
 355 
 356   // Apply "cl.do_generation" to all generations in the heap
 357   // If "old_to_young" determines the order.
 358   void generation_iterate(GenClosure* cl, bool old_to_young);
 359 
 360   void space_iterate(SpaceClosure* cl);
 361 
 362   // Return "true" if all generations have reached the
 363   // maximal committed limit that they can reach, without a garbage
 364   // collection.
 365   virtual bool is_maximal_no_gc() const;
 366 
 367   // Return the generation before "gen".
 368   Generation* prev_gen(Generation* gen) const {
 369     int l = gen->level();
 370     guarantee(l > 0, "Out of bounds");
 371     return _gens[l-1];
 372   }
 373 
 374   // Return the generation after "gen".
 375   Generation* next_gen(Generation* gen) const {
 376     int l = gen->level() + 1;
 377     guarantee(l < _n_gens, "Out of bounds");
 378     return _gens[l];
 379   }
 380 
 381   Generation* get_gen(int i) const {
 382     guarantee(i >= 0 && i < _n_gens, "Out of bounds");
 383     return _gens[i];
 384   }
 385 
 386   int n_gens() const {
 387     assert(_n_gens == gen_policy()->number_of_generations(), "Sanity");
 388     return _n_gens;
 389   }
 390 
 391   // This function returns the "GenRemSet" object that allows us to scan
 392   // generations in a fully generational heap.
 393   GenRemSet* rem_set() { return _rem_set; }
 394 
 395   // Convenience function to be used in situations where the heap type can be
 396   // asserted to be this type.
 397   static GenCollectedHeap* heap();
 398 
 399   void set_par_threads(uint t);
 400 
 401   // Invoke the "do_oop" method of one of the closures "not_older_gens"
 402   // or "older_gens" on root locations for the generation at
 403   // "level".  (The "older_gens" closure is used for scanning references
 404   // from older generations; "not_older_gens" is used everywhere else.)
 405   // If "younger_gens_as_roots" is false, younger generations are
 406   // not scanned as roots; in this case, the caller must be arranging to
 407   // scan the younger generations itself.  (For example, a generation might
 408   // explicitly mark reachable objects in younger generations, to avoid
 409   // excess storage retention.)
 410   // The "so" argument determines which of the roots
 411   // the closure is applied to:
 412   // "SO_None" does none;
 413  private:
 414   void gen_process_roots(int level,
 415                          bool younger_gens_as_roots,
 416                          bool activate_scope,
 417                          SharedHeap::ScanningOption so,
 418                          OopsInGenClosure* not_older_gens,
 419                          OopsInGenClosure* weak_roots,
 420                          OopsInGenClosure* older_gens,
 421                          CLDClosure* cld_closure,
 422                          CLDClosure* weak_cld_closure,
 423                          CodeBlobClosure* code_closure);
 424 
 425  public:
 426   static const bool StrongAndWeakRoots = false;
 427   static const bool StrongRootsOnly    = true;
 428 
 429   void gen_process_roots(int level,
 430                          bool younger_gens_as_roots,
 431                          bool activate_scope,
 432                          SharedHeap::ScanningOption so,
 433                          bool only_strong_roots,
 434                          OopsInGenClosure* not_older_gens,
 435                          OopsInGenClosure* older_gens,
 436                          CLDClosure* cld_closure);
 437 
 438   // Apply "root_closure" to all the weak roots of the system.
 439   // These include JNI weak roots, string table,
 440   // and referents of reachable weak refs.
 441   void gen_process_weak_roots(OopClosure* root_closure);
 442 
 443   // Set the saved marks of generations, if that makes sense.
 444   // In particular, if any generation might iterate over the oops
 445   // in other generations, it should call this method.
 446   void save_marks();
 447 
 448   // Apply "cur->do_oop" or "older->do_oop" to all the oops in objects
 449   // allocated since the last call to save_marks in generations at or above
 450   // "level".  The "cur" closure is
 451   // applied to references in the generation at "level", and the "older"
 452   // closure to older generations.
 453 #define GCH_SINCE_SAVE_MARKS_ITERATE_DECL(OopClosureType, nv_suffix)    \
 454   void oop_since_save_marks_iterate(int level,                          \
 455                                     OopClosureType* cur,                \
 456                                     OopClosureType* older);
 457 
 458   ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DECL)
 459 
 460 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DECL
 461 
 462   // Returns "true" iff no allocations have occurred in any generation at
 463   // "level" or above since the last
 464   // call to "save_marks".
 465   bool no_allocs_since_save_marks(int level);
 466 
 467   // Returns true if an incremental collection is likely to fail.
 468   // We optionally consult the young gen, if asked to do so;
 469   // otherwise we base our answer on whether the previous incremental
 470   // collection attempt failed with no corrective action as of yet.
 471   bool incremental_collection_will_fail(bool consult_young) {
 472     // Assumes a 2-generation system; the first disjunct remembers if an
 473     // incremental collection failed, even when we thought (second disjunct)
 474     // that it would not.
 475     assert(heap()->collector_policy()->is_generation_policy(),
 476            "the following definition may not be suitable for an n(>2)-generation system");
 477     return incremental_collection_failed() ||
 478            (consult_young && !get_gen(0)->collection_attempt_is_safe());
 479   }
 480 
 481   // If a generation bails out of an incremental collection,
 482   // it sets this flag.
 483   bool incremental_collection_failed() const {
 484     return _incremental_collection_failed;
 485   }
 486   void set_incremental_collection_failed() {
 487     _incremental_collection_failed = true;
 488   }
 489   void clear_incremental_collection_failed() {
 490     _incremental_collection_failed = false;
 491   }
 492 
 493   // Promotion of obj into gen failed.  Try to promote obj to higher
 494   // gens in ascending order; return the new location of obj if successful.
 495   // Otherwise, try expand-and-allocate for obj in both the young and old
 496   // generation; return the new location of obj if successful.  Otherwise, return NULL.
 497   oop handle_failed_promotion(Generation* old_gen,
 498                               oop obj,
 499                               size_t obj_size);
 500 
 501 private:
 502   // Accessor for memory state verification support
 503   NOT_PRODUCT(
 504     static size_t skip_header_HeapWords() { return _skip_header_HeapWords; }
 505   )
 506 
 507   // Override
 508   void check_for_non_bad_heap_word_value(HeapWord* addr,
 509     size_t size) PRODUCT_RETURN;
 510 
 511   // For use by mark-sweep.  As implemented, mark-sweep-compact is global
 512   // in an essential way: compaction is performed across generations, by
 513   // iterating over spaces.
 514   void prepare_for_compaction();
 515 
 516   // Perform a full collection of the first max_level+1 generations.
 517   // This is the low level interface used by the public versions of
 518   // collect() and collect_locked(). Caller holds the Heap_lock on entry.
 519   void collect_locked(GCCause::Cause cause, int max_level);
 520 
 521   // Returns success or failure.
 522   bool create_cms_collector();
 523 
 524   // In support of ExplicitGCInvokesConcurrent functionality
 525   bool should_do_concurrent_full_gc(GCCause::Cause cause);
 526   void collect_mostly_concurrent(GCCause::Cause cause);
 527 
 528   // Save the tops of the spaces in all generations
 529   void record_gen_tops_before_GC() PRODUCT_RETURN;
 530 
 531 protected:
 532   virtual void gc_prologue(bool full);
 533   virtual void gc_epilogue(bool full);
 534 };
 535 
 536 #endif // SHARE_VM_MEMORY_GENCOLLECTEDHEAP_HPP