1 /*
   2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_GC_PARALLEL_PARALLELSCAVENGEHEAP_HPP
  26 #define SHARE_GC_PARALLEL_PARALLELSCAVENGEHEAP_HPP
  27 
  28 #include "gc/parallel/generationSizer.hpp"
  29 #include "gc/parallel/objectStartArray.hpp"
  30 #include "gc/parallel/psGCAdaptivePolicyCounters.hpp"
  31 #include "gc/parallel/psOldGen.hpp"
  32 #include "gc/parallel/psYoungGen.hpp"
  33 #include "gc/shared/cardTableBarrierSet.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "gc/shared/collectorPolicy.hpp"
  36 #include "gc/shared/gcPolicyCounters.hpp"
  37 #include "gc/shared/gcWhen.hpp"
  38 #include "gc/shared/softRefPolicy.hpp"
  39 #include "gc/shared/strongRootsScope.hpp"
  40 #include "memory/metaspace.hpp"
  41 #include "utilities/growableArray.hpp"
  42 #include "utilities/ostream.hpp"
  43 
  44 class AdjoiningGenerations;
  45 class GCHeapSummary;
  46 class GCTaskManager;
  47 class MemoryManager;
  48 class MemoryPool;
  49 class PSAdaptiveSizePolicy;
  50 class PSCardTable;
  51 class PSHeapSummary;
  52 
  53 class ParallelScavengeHeap : public CollectedHeap {
  54   friend class VMStructs;
  55  private:
  56   static PSYoungGen* _young_gen;
  57   static PSOldGen*   _old_gen;
  58 
  59   // Sizing policy for entire heap
  60   static PSAdaptiveSizePolicy*       _size_policy;
  61   static PSGCAdaptivePolicyCounters* _gc_policy_counters;
  62 
  63   GenerationSizer* _collector_policy;
  64 
  65   SoftRefPolicy _soft_ref_policy;
  66 
  67   // Collection of generations that are adjacent in the
  68   // space reserved for the heap.
  69   AdjoiningGenerations* _gens;
  70   unsigned int _death_march_count;
  71 
  72   // The task manager
  73   static GCTaskManager* _gc_task_manager;
  74 
  75   GCMemoryManager* _young_manager;
  76   GCMemoryManager* _old_manager;
  77 
  78   MemoryPool* _eden_pool;
  79   MemoryPool* _survivor_pool;
  80   MemoryPool* _old_pool;
  81 
  82   virtual void initialize_serviceability();
  83 
  84   void trace_heap(GCWhen::Type when, const GCTracer* tracer);
  85 
  86  protected:
  87   static inline size_t total_invocations();
  88   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
  89 
  90   inline bool should_alloc_in_eden(size_t size) const;
  91   inline void death_march_check(HeapWord* const result, size_t size);
  92   HeapWord* mem_allocate_old_gen(size_t size);
  93 
  94  public:
  95   ParallelScavengeHeap(GenerationSizer* policy) :
  96     CollectedHeap(), _collector_policy(policy), _death_march_count(0) { }
  97 
  98   // For use by VM operations
  99   enum CollectionType {
 100     Scavenge,
 101     MarkSweep
 102   };
 103 
 104   virtual Name kind() const {
 105     return CollectedHeap::Parallel;
 106   }
 107 
 108   virtual const char* name() const {
 109     return "Parallel";
 110   }
 111 
 112   virtual CollectorPolicy* collector_policy() const { return _collector_policy; }
 113 
 114   virtual GenerationSizer* ps_collector_policy() const { return _collector_policy; }
 115 
 116   virtual SoftRefPolicy* soft_ref_policy() { return &_soft_ref_policy; }
 117 
 118   virtual GrowableArray<GCMemoryManager*> memory_managers();
 119   virtual GrowableArray<MemoryPool*> memory_pools();
 120 
 121   static PSYoungGen* young_gen() { return _young_gen; }
 122   static PSOldGen* old_gen()     { return _old_gen; }
 123 
 124   virtual PSAdaptiveSizePolicy* size_policy() { return _size_policy; }
 125 
 126   static PSGCAdaptivePolicyCounters* gc_policy_counters() { return _gc_policy_counters; }
 127 
 128   static ParallelScavengeHeap* heap();
 129 
 130   static GCTaskManager* const gc_task_manager() { return _gc_task_manager; }
 131 
 132   CardTableBarrierSet* barrier_set();
 133   PSCardTable* card_table();
 134 
 135   AdjoiningGenerations* gens() { return _gens; }
 136 
 137   // Returns JNI_OK on success
 138   virtual jint initialize();
 139 
 140   void post_initialize();
 141   void update_counters();
 142 
 143   // The alignment used for the various areas
 144   size_t space_alignment()      { return _collector_policy->space_alignment(); }
 145   size_t generation_alignment() { return _collector_policy->gen_alignment(); }
 146 
 147   // Return the (conservative) maximum heap alignment
 148   static size_t conservative_max_heap_alignment() {
 149     return CollectorPolicy::compute_heap_alignment();
 150   }
 151 
 152   size_t capacity() const;
 153   size_t used() const;
 154 
 155   // Return "true" if all generations have reached the
 156   // maximal committed limit that they can reach, without a garbage
 157   // collection.
 158   virtual bool is_maximal_no_gc() const;
 159 
 160   // Return true if the reference points to an object that
 161   // can be moved in a partial collection.  For currently implemented
 162   // generational collectors that means during a collection of
 163   // the young gen.
 164   virtual bool is_scavengable(oop obj);
 165   virtual void register_nmethod(nmethod* nm);
 166   virtual void verify_nmethod(nmethod* nmethod);
 167 
 168   size_t max_capacity() const;
 169 
 170   // Whether p is in the allocated part of the heap
 171   bool is_in(const void* p) const;
 172 
 173   bool is_in_reserved(const void* p) const;
 174 
 175   bool is_in_young(oop p);  // reserved part
 176   bool is_in_old(oop p);    // reserved part
 177 
 178   // Memory allocation.   "gc_time_limit_was_exceeded" will
 179   // be set to true if the adaptive size policy determine that
 180   // an excessive amount of time is being spent doing collections
 181   // and caused a NULL to be returned.  If a NULL is not returned,
 182   // "gc_time_limit_was_exceeded" has an undefined meaning.
 183   HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
 184 
 185   // Allocation attempt(s) during a safepoint. It should never be called
 186   // to allocate a new TLAB as this allocation might be satisfied out
 187   // of the old generation.
 188   HeapWord* failed_mem_allocate(size_t size);
 189 
 190   // Support for System.gc()
 191   void collect(GCCause::Cause cause);
 192 
 193   // These also should be called by the vm thread at a safepoint (e.g., from a
 194   // VM operation).
 195   //
 196   // The first collects the young generation only, unless the scavenge fails; it
 197   // will then attempt a full gc.  The second collects the entire heap; if
 198   // maximum_compaction is true, it will compact everything and clear all soft
 199   // references.
 200   inline void invoke_scavenge();
 201 
 202   // Perform a full collection
 203   virtual void do_full_collection(bool clear_all_soft_refs);
 204 
 205   bool supports_inline_contig_alloc() const { return !UseNUMA; }
 206 
 207   HeapWord* volatile* top_addr() const { return !UseNUMA ? young_gen()->top_addr() : (HeapWord* volatile*)-1; }
 208   HeapWord** end_addr() const { return !UseNUMA ? young_gen()->end_addr() : (HeapWord**)-1; }
 209 
 210   void ensure_parsability(bool retire_tlabs);
 211   void resize_all_tlabs();
 212 
 213   bool supports_tlab_allocation() const { return true; }
 214 
 215   size_t tlab_capacity(Thread* thr) const;
 216   size_t tlab_used(Thread* thr) const;
 217   size_t unsafe_max_tlab_alloc(Thread* thr) const;
 218 
 219   void object_iterate(ObjectClosure* cl);
 220   void safe_object_iterate(ObjectClosure* cl) { object_iterate(cl); }
 221 
 222   HeapWord* block_start(const void* addr) const;
 223   size_t block_size(const HeapWord* addr) const;
 224   bool block_is_obj(const HeapWord* addr) const;
 225 
 226   jlong millis_since_last_gc();
 227 
 228   void prepare_for_verify();
 229   PSHeapSummary create_ps_heap_summary();
 230   virtual void print_on(outputStream* st) const;
 231   virtual void print_on_error(outputStream* st) const;
 232   virtual void print_gc_threads_on(outputStream* st) const;
 233   virtual void gc_threads_do(ThreadClosure* tc) const;
 234   virtual void print_tracing_info() const;
 235 
 236   void verify(VerifyOption option /* ignored */);
 237 
 238   // Resize the young generation.  The reserved space for the
 239   // generation may be expanded in preparation for the resize.
 240   void resize_young_gen(size_t eden_size, size_t survivor_size);
 241 
 242   // Resize the old generation.  The reserved space for the
 243   // generation may be expanded in preparation for the resize.
 244   void resize_old_gen(size_t desired_free_space);
 245 
 246   // Save the tops of the spaces in all generations
 247   void record_gen_tops_before_GC() PRODUCT_RETURN;
 248 
 249   // Mangle the unused parts of all spaces in the heap
 250   void gen_mangle_unused_area() PRODUCT_RETURN;
 251 
 252   // Call these in sequential code around the processing of strong roots.
 253   class ParStrongRootsScope : public MarkScope {
 254    public:
 255     ParStrongRootsScope();
 256     ~ParStrongRootsScope();
 257   };
 258 
 259   GCMemoryManager* old_gc_manager() const { return _old_manager; }
 260   GCMemoryManager* young_gc_manager() const { return _young_manager; }
 261 };
 262 
 263 // Simple class for storing info about the heap at the start of GC, to be used
 264 // after GC for comparison/printing.
 265 class PreGCValues {
 266 public:
 267   PreGCValues(ParallelScavengeHeap* heap) :
 268       _heap_used(heap->used()),
 269       _young_gen_used(heap->young_gen()->used_in_bytes()),
 270       _old_gen_used(heap->old_gen()->used_in_bytes()),
 271       _metadata_used(MetaspaceUtils::used_bytes()) { };
 272 
 273   size_t heap_used() const      { return _heap_used; }
 274   size_t young_gen_used() const { return _young_gen_used; }
 275   size_t old_gen_used() const   { return _old_gen_used; }
 276   size_t metadata_used() const  { return _metadata_used; }
 277 
 278 private:
 279   size_t _heap_used;
 280   size_t _young_gen_used;
 281   size_t _old_gen_used;
 282   size_t _metadata_used;
 283 };
 284 
 285 // Class that can be used to print information about the
 286 // adaptive size policy at intervals specified by
 287 // AdaptiveSizePolicyOutputInterval.  Only print information
 288 // if an adaptive size policy is in use.
 289 class AdaptiveSizePolicyOutput : AllStatic {
 290   static bool enabled() {
 291     return UseParallelGC &&
 292            UseAdaptiveSizePolicy &&
 293            log_is_enabled(Debug, gc, ergo);
 294   }
 295  public:
 296   static void print() {
 297     if (enabled()) {
 298       ParallelScavengeHeap::heap()->size_policy()->print();
 299     }
 300   }
 301 
 302   static void print(AdaptiveSizePolicy* size_policy, uint count) {
 303     bool do_print =
 304         enabled() &&
 305         (AdaptiveSizePolicyOutputInterval > 0) &&
 306         (count % AdaptiveSizePolicyOutputInterval) == 0;
 307 
 308     if (do_print) {
 309       size_policy->print();
 310     }
 311   }
 312 };
 313 
 314 #endif // SHARE_GC_PARALLEL_PARALLELSCAVENGEHEAP_HPP