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/referenceProcessor.hpp"
 37 #include "gc/shared/softRefPolicy.hpp"
 38 #include "gc/shared/strongRootsScope.hpp"
 39 #include "gc/shared/workgroup.hpp"
 40 #include "logging/log.hpp"
 41 #include "memory/metaspace.hpp"
 42 #include "utilities/growableArray.hpp"
 43 #include "utilities/ostream.hpp"
 44 
 45 class AdjoiningGenerations;
 46 class GCHeapSummary;
 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   SoftRefPolicy _soft_ref_policy;
 64 
 65   // Collection of generations that are adjacent in the
 66   // space reserved for the heap.
 67   AdjoiningGenerations* _gens;
 68   unsigned int _death_march_count;
 69 
 70   GCMemoryManager* _young_manager;
 71   GCMemoryManager* _old_manager;
 72 
 73   MemoryPool* _eden_pool;
 74   MemoryPool* _survivor_pool;
 75   MemoryPool* _old_pool;
 76 
 77   WorkGang _workers;
 78 
 79   virtual void initialize_serviceability();
 80 
 81   void trace_heap(GCWhen::Type when, const GCTracer* tracer);
 82 
 83  protected:
 84   static inline size_t total_invocations();
 85   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
 86 
 87   inline bool should_alloc_in_eden(size_t size) const;
 88   inline void death_march_check(HeapWord* const result, size_t size);
 89   HeapWord* mem_allocate_old_gen(size_t size);
 90 
 91  public:
 92   ParallelScavengeHeap() :
 93     CollectedHeap(),
 94     _gens(NULL),
 95     _death_march_count(0),
 96     _young_manager(NULL),
 97     _old_manager(NULL),
 98     _eden_pool(NULL),
 99     _survivor_pool(NULL),
100     _old_pool(NULL),
101     _workers("GC Thread", ParallelGCThreads,
102         true /* are_GC_task_threads */,
103         false /* are_ConcurrentGC_threads */) { }
104 
105   // For use by VM operations
106   enum CollectionType {
107     Scavenge,
108     MarkSweep
109   };
110 
111   virtual Name kind() const {
112     return CollectedHeap::Parallel;
113   }
114 
115   virtual const char* name() const {
116     return "Parallel";
117   }
118 
119   virtual SoftRefPolicy* soft_ref_policy() { return &_soft_ref_policy; }
120 
121   virtual GrowableArray<GCMemoryManager*> memory_managers();
122   virtual GrowableArray<MemoryPool*> memory_pools();
123 
124   static PSYoungGen* young_gen() { return _young_gen; }
125   static PSOldGen* old_gen()     { return _old_gen; }
126 
127   virtual PSAdaptiveSizePolicy* size_policy() { return _size_policy; }
128 
129   static PSGCAdaptivePolicyCounters* gc_policy_counters() { return _gc_policy_counters; }
130 
131   static ParallelScavengeHeap* heap();
132 
133   CardTableBarrierSet* barrier_set();
134   PSCardTable* card_table();
135 
136   AdjoiningGenerations* gens() { return _gens; }
137 
138   // Returns JNI_OK on success
139   virtual jint initialize();
140 
141   void post_initialize();
142   void update_counters();
143 
144   size_t capacity() const;
145   size_t used() const;
146 
147   // Return "true" if all generations have reached the
148   // maximal committed limit that they can reach, without a garbage
149   // collection.
150   virtual bool is_maximal_no_gc() const;
151 
152   virtual void register_nmethod(nmethod* nm);
153   virtual void unregister_nmethod(nmethod* nm);
154   virtual void verify_nmethod(nmethod* nm);
155   virtual void flush_nmethod(nmethod* nm);
156 
157   void prune_scavengable_nmethods();
158 
159   size_t max_capacity() const;
160 
161   // Whether p is in the allocated part of the heap
162   bool is_in(const void* p) const;
163 
164   bool is_in_reserved(const void* p) const;
165 
166   bool is_in_young(oop p);  // reserved part
167   bool is_in_old(oop p);    // reserved part
168 
169   // Memory allocation.   "gc_time_limit_was_exceeded" will
170   // be set to true if the adaptive size policy determine that
171   // an excessive amount of time is being spent doing collections
172   // and caused a NULL to be returned.  If a NULL is not returned,
173   // "gc_time_limit_was_exceeded" has an undefined meaning.
174   HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
175 
176   // Allocation attempt(s) during a safepoint. It should never be called
177   // to allocate a new TLAB as this allocation might be satisfied out
178   // of the old generation.
179   HeapWord* failed_mem_allocate(size_t size);
180 
181   // Support for System.gc()
182   void collect(GCCause::Cause cause);
183 
184   // These also should be called by the vm thread at a safepoint (e.g., from a
185   // VM operation).
186   //
187   // The first collects the young generation only, unless the scavenge fails; it
188   // will then attempt a full gc.  The second collects the entire heap; if
189   // maximum_compaction is true, it will compact everything and clear all soft
190   // references.
191   inline void invoke_scavenge();
192 
193   // Perform a full collection
194   virtual void do_full_collection(bool clear_all_soft_refs);
195 
196   bool supports_inline_contig_alloc() const { return !UseNUMA; }
197 
198   HeapWord* volatile* top_addr() const { return !UseNUMA ? young_gen()->top_addr() : (HeapWord* volatile*)-1; }
199   HeapWord** end_addr() const { return !UseNUMA ? young_gen()->end_addr() : (HeapWord**)-1; }
200 
201   void ensure_parsability(bool retire_tlabs);
202   void resize_all_tlabs();
203 
204   bool supports_tlab_allocation() const { return true; }
205 
206   size_t tlab_capacity(Thread* thr) const;
207   size_t tlab_used(Thread* thr) const;
208   size_t unsafe_max_tlab_alloc(Thread* thr) const;
209 
210   void object_iterate(ObjectClosure* cl);
211   void safe_object_iterate(ObjectClosure* cl) { object_iterate(cl); }
212 
213   HeapWord* block_start(const void* addr) const;
214   bool block_is_obj(const HeapWord* addr) const;
215 
216   jlong millis_since_last_gc();
217 
218   void prepare_for_verify();
219   PSHeapSummary create_ps_heap_summary();
220   virtual void print_on(outputStream* st) const;
221   virtual void print_on_error(outputStream* st) const;
222   virtual void print_gc_threads_on(outputStream* st) const;
223   virtual void gc_threads_do(ThreadClosure* tc) const;
224   virtual void print_tracing_info() const;
225 
226   void verify(VerifyOption option /* ignored */);
227 
228   // Resize the young generation.  The reserved space for the
229   // generation may be expanded in preparation for the resize.
230   void resize_young_gen(size_t eden_size, size_t survivor_size);
231 
232   // Resize the old generation.  The reserved space for the
233   // generation may be expanded in preparation for the resize.
234   void resize_old_gen(size_t desired_free_space);
235 
236   // Save the tops of the spaces in all generations
237   void record_gen_tops_before_GC() PRODUCT_RETURN;
238 
239   // Mangle the unused parts of all spaces in the heap
240   void gen_mangle_unused_area() PRODUCT_RETURN;
241 
242   // Call these in sequential code around the processing of strong roots.
243   class ParStrongRootsScope : public MarkScope {
244    public:
245     ParStrongRootsScope();
246     ~ParStrongRootsScope();
247   };
248 
249   GCMemoryManager* old_gc_manager() const { return _old_manager; }
250   GCMemoryManager* young_gc_manager() const { return _young_manager; }
251 
252   WorkGang& workers() {
253     return _workers;
254   }
255 };
256 
257 // Simple class for storing info about the heap at the start of GC, to be used
258 // after GC for comparison/printing.
259 class PreGCValues {
260 public:
261   PreGCValues(ParallelScavengeHeap* heap) :
262       _heap_used(heap->used()),
263       _young_gen_used(heap->young_gen()->used_in_bytes()),
264       _old_gen_used(heap->old_gen()->used_in_bytes()),
265       _metadata_used(MetaspaceUtils::used_bytes()) { };
266 
267   size_t heap_used() const      { return _heap_used; }
268   size_t young_gen_used() const { return _young_gen_used; }
269   size_t old_gen_used() const   { return _old_gen_used; }
270   size_t metadata_used() const  { return _metadata_used; }
271 
272 private:
273   size_t _heap_used;
274   size_t _young_gen_used;
275   size_t _old_gen_used;
276   size_t _metadata_used;
277 };
278 
279 // Class that can be used to print information about the
280 // adaptive size policy at intervals specified by
281 // AdaptiveSizePolicyOutputInterval.  Only print information
282 // if an adaptive size policy is in use.
283 class AdaptiveSizePolicyOutput : AllStatic {
284   static bool enabled() {
285     return UseParallelGC &&
286            UseAdaptiveSizePolicy &&
287            log_is_enabled(Debug, gc, ergo);
288   }
289  public:
290   static void print() {
291     if (enabled()) {
292       ParallelScavengeHeap::heap()->size_policy()->print();
293     }
294   }
295 
296   static void print(AdaptiveSizePolicy* size_policy, uint count) {
297     bool do_print =
298         enabled() &&
299         (AdaptiveSizePolicyOutputInterval > 0) &&
300         (count % AdaptiveSizePolicyOutputInterval) == 0;
301 
302     if (do_print) {
303       size_policy->print();
304     }
305   }
306 };
307 
308 #endif // SHARE_GC_PARALLEL_PARALLELSCAVENGEHEAP_HPP