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/objectStartArray.hpp"
  29 #include "gc/parallel/psGCAdaptivePolicyCounters.hpp"
  30 #include "gc/parallel/psOldGen.hpp"
  31 #include "gc/parallel/psYoungGen.hpp"
  32 #include "gc/shared/cardTableBarrierSet.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "gc/shared/gcPolicyCounters.hpp"
  35 #include "gc/shared/gcWhen.hpp"
  36 #include "gc/shared/preGCValues.hpp"
  37 #include "gc/shared/referenceProcessor.hpp"
  38 #include "gc/shared/softRefPolicy.hpp"
  39 #include "gc/shared/strongRootsScope.hpp"
  40 #include "gc/shared/workgroup.hpp"
  41 #include "logging/log.hpp"
  42 #include "memory/metaspace.hpp"
  43 #include "utilities/growableArray.hpp"
  44 #include "utilities/ostream.hpp"
  45 
  46 class AdjoiningGenerations;
  47 class GCHeapSummary;
  48 class MemoryManager;
  49 class MemoryPool;
  50 class PSAdaptiveSizePolicy;
  51 class PSCardTable;
  52 class PSHeapSummary;
  53 
  54 class ParallelScavengeHeap : public CollectedHeap {
  55   friend class VMStructs;
  56  private:
  57   static PSYoungGen* _young_gen;
  58   static PSOldGen*   _old_gen;
  59 
  60   // Sizing policy for entire heap
  61   static PSAdaptiveSizePolicy*       _size_policy;
  62   static PSGCAdaptivePolicyCounters* _gc_policy_counters;
  63 
  64   SoftRefPolicy _soft_ref_policy;
  65 
  66   // Collection of generations that are adjacent in the
  67   // space reserved for the heap.
  68   AdjoiningGenerations* _gens;
  69   unsigned int _death_march_count;
  70 
  71   GCMemoryManager* _young_manager;
  72   GCMemoryManager* _old_manager;
  73 
  74   MemoryPool* _eden_pool;
  75   MemoryPool* _survivor_pool;
  76   MemoryPool* _old_pool;
  77 
  78   WorkGang _workers;
  79 
  80   virtual void initialize_serviceability();
  81 
  82   void trace_heap(GCWhen::Type when, const GCTracer* tracer);
  83 
  84  protected:
  85   static inline size_t total_invocations();
  86   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
  87 
  88   inline bool should_alloc_in_eden(size_t size) const;
  89   inline void death_march_check(HeapWord* const result, size_t size);
  90   HeapWord* mem_allocate_old_gen(size_t size);
  91 
  92  public:
  93   ParallelScavengeHeap() :
  94     CollectedHeap(),
  95     _gens(NULL),
  96     _death_march_count(0),
  97     _young_manager(NULL),
  98     _old_manager(NULL),
  99     _eden_pool(NULL),
 100     _survivor_pool(NULL),
 101     _old_pool(NULL),
 102     _workers("GC Thread",
 103              ParallelGCThreads,
 104              true /* are_GC_task_threads */,
 105              false /* are_ConcurrentGC_threads */) { }
 106 
 107   // For use by VM operations
 108   enum CollectionType {
 109     Scavenge,
 110     MarkSweep
 111   };
 112 
 113   virtual Name kind() const {
 114     return CollectedHeap::Parallel;
 115   }
 116 
 117   virtual const char* name() const {
 118     return "Parallel";
 119   }
 120 
 121   virtual SoftRefPolicy* soft_ref_policy() { return &_soft_ref_policy; }
 122 
 123   virtual GrowableArray<GCMemoryManager*> memory_managers();
 124   virtual GrowableArray<MemoryPool*> memory_pools();
 125 
 126   static PSYoungGen* young_gen() { return _young_gen; }
 127   static PSOldGen* old_gen()     { return _old_gen; }
 128 
 129   virtual PSAdaptiveSizePolicy* size_policy() { return _size_policy; }
 130 
 131   static PSGCAdaptivePolicyCounters* gc_policy_counters() { return _gc_policy_counters; }
 132 
 133   static ParallelScavengeHeap* heap();
 134 
 135   CardTableBarrierSet* barrier_set();
 136   PSCardTable* card_table();
 137 
 138   AdjoiningGenerations* gens() { return _gens; }
 139 
 140   // Returns JNI_OK on success
 141   virtual jint initialize();
 142 
 143   void post_initialize();
 144   void update_counters();
 145 
 146   size_t capacity() const;
 147   size_t used() const;
 148 
 149   // Return "true" if all generations have reached the
 150   // maximal committed limit that they can reach, without a garbage
 151   // collection.
 152   virtual bool is_maximal_no_gc() const;
 153 
 154   virtual void register_nmethod(nmethod* nm);
 155   virtual void unregister_nmethod(nmethod* nm);
 156   virtual void verify_nmethod(nmethod* nm);
 157   virtual void flush_nmethod(nmethod* nm);
 158 
 159   void prune_scavengable_nmethods();
 160 
 161   size_t max_capacity() const;
 162 
 163   // Whether p is in the allocated part of the heap
 164   bool is_in(const void* p) const;
 165 
 166   bool is_in_reserved(const void* p) const;
 167 
 168   bool is_in_young(oop p);  // reserved part
 169   bool is_in_old(oop p);    // reserved part
 170 
 171   MemRegion reserved_region() const { return _reserved; }
 172   HeapWord* base() const { return _reserved.start(); }
 173 
 174   // Memory allocation.   "gc_time_limit_was_exceeded" will
 175   // be set to true if the adaptive size policy determine that
 176   // an excessive amount of time is being spent doing collections
 177   // and caused a NULL to be returned.  If a NULL is not returned,
 178   // "gc_time_limit_was_exceeded" has an undefined meaning.
 179   HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
 180 
 181   // Allocation attempt(s) during a safepoint. It should never be called
 182   // to allocate a new TLAB as this allocation might be satisfied out
 183   // of the old generation.
 184   HeapWord* failed_mem_allocate(size_t size);
 185 
 186   // Support for System.gc()
 187   void collect(GCCause::Cause cause);
 188 
 189   // These also should be called by the vm thread at a safepoint (e.g., from a
 190   // VM operation).
 191   //
 192   // The first collects the young generation only, unless the scavenge fails; it
 193   // will then attempt a full gc.  The second collects the entire heap; if
 194   // maximum_compaction is true, it will compact everything and clear all soft
 195   // references.
 196   inline void invoke_scavenge();
 197 
 198   // Perform a full collection
 199   virtual void do_full_collection(bool clear_all_soft_refs);
 200 
 201   bool supports_inline_contig_alloc() const { return !UseNUMA; }
 202 
 203   HeapWord* volatile* top_addr() const { return !UseNUMA ? young_gen()->top_addr() : (HeapWord* volatile*)-1; }
 204   HeapWord** end_addr() const { return !UseNUMA ? young_gen()->end_addr() : (HeapWord**)-1; }
 205 
 206   void ensure_parsability(bool retire_tlabs);
 207   void resize_all_tlabs();
 208 
 209   bool supports_tlab_allocation() const { return true; }
 210 
 211   size_t tlab_capacity(Thread* thr) const;
 212   size_t tlab_used(Thread* thr) const;
 213   size_t unsafe_max_tlab_alloc(Thread* thr) const;
 214 
 215   void object_iterate(ObjectClosure* cl);
 216 
 217   HeapWord* block_start(const void* addr) const;
 218   bool block_is_obj(const HeapWord* addr) const;
 219 
 220   jlong millis_since_last_gc();
 221 
 222   void prepare_for_verify();
 223   PSHeapSummary create_ps_heap_summary();
 224   virtual void print_on(outputStream* st) const;
 225   virtual void print_on_error(outputStream* st) const;
 226   virtual void print_gc_threads_on(outputStream* st) const;
 227   virtual void gc_threads_do(ThreadClosure* tc) const;
 228   virtual void print_tracing_info() const;
 229 
 230   PreGenGCValues get_pre_gc_values() const;
 231   void print_heap_change(const PreGenGCValues& pre_gc_values) const;
 232 
 233   // Used to print information about locations in the hs_err file.
 234   virtual bool print_location(outputStream* st, void* addr) 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   WorkGang& workers() {
 263     return _workers;
 264   }
 265 };
 266 
 267 // Class that can be used to print information about the
 268 // adaptive size policy at intervals specified by
 269 // AdaptiveSizePolicyOutputInterval.  Only print information
 270 // if an adaptive size policy is in use.
 271 class AdaptiveSizePolicyOutput : AllStatic {
 272   static bool enabled() {
 273     return UseParallelGC &&
 274            UseAdaptiveSizePolicy &&
 275            log_is_enabled(Debug, gc, ergo);
 276   }
 277  public:
 278   static void print() {
 279     if (enabled()) {
 280       ParallelScavengeHeap::heap()->size_policy()->print();
 281     }
 282   }
 283 
 284   static void print(AdaptiveSizePolicy* size_policy, uint count) {
 285     bool do_print =
 286         enabled() &&
 287         (AdaptiveSizePolicyOutputInterval > 0) &&
 288         (count % AdaptiveSizePolicyOutputInterval) == 0;
 289 
 290     if (do_print) {
 291       size_policy->print();
 292     }
 293   }
 294 };
 295 
 296 #endif // SHARE_GC_PARALLEL_PARALLELSCAVENGEHEAP_HPP