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 GCTaskManager;
 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   // The task manager
 72   static GCTaskManager* _gc_task_manager;
 73 
 74   GCMemoryManager* _young_manager;
 75   GCMemoryManager* _old_manager;
 76 
 77   MemoryPool* _eden_pool;
 78   MemoryPool* _survivor_pool;
 79   MemoryPool* _old_pool;
 80 
 81   WorkGang _workers;
 82 
 83   virtual void initialize_serviceability();
 84 
 85   void trace_heap(GCWhen::Type when, const GCTracer* tracer);
 86 
 87  protected:
 88   static inline size_t total_invocations();
 89   HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size);
 90 
 91   inline bool should_alloc_in_eden(size_t size) const;
 92   inline void death_march_check(HeapWord* const result, size_t size);
 93   HeapWord* mem_allocate_old_gen(size_t size);
 94 
 95  public:
 96   ParallelScavengeHeap() :
 97     CollectedHeap(),
 98     _gens(NULL),
 99     _death_march_count(0),
100     _young_manager(NULL),
101     _old_manager(NULL),
102     _eden_pool(NULL),
103     _survivor_pool(NULL),
104     _old_pool(NULL),
105     _workers("GC Thread", ParallelGCThreads,
106         true /* are_GC_task_threads */,
107         false /* are_ConcurrentGC_threads */) { }
108 
109   // For use by VM operations
110   enum CollectionType {
111     Scavenge,
112     MarkSweep
113   };
114 
115   virtual Name kind() const {
116     return CollectedHeap::Parallel;
117   }
118 
119   virtual const char* name() const {
120     return "Parallel";
121   }
122 
123   virtual SoftRefPolicy* soft_ref_policy() { return &_soft_ref_policy; }
124 
125   virtual GrowableArray<GCMemoryManager*> memory_managers();
126   virtual GrowableArray<MemoryPool*> memory_pools();
127 
128   static PSYoungGen* young_gen() { return _young_gen; }
129   static PSOldGen* old_gen()     { return _old_gen; }
130 
131   virtual PSAdaptiveSizePolicy* size_policy() { return _size_policy; }
132 
133   static PSGCAdaptivePolicyCounters* gc_policy_counters() { return _gc_policy_counters; }
134 
135   static ParallelScavengeHeap* heap();
136 
137   static GCTaskManager* const gc_task_manager() { return _gc_task_manager; }
138 
139   CardTableBarrierSet* barrier_set();
140   PSCardTable* card_table();
141 
142   AdjoiningGenerations* gens() { return _gens; }
143 
144   // Returns JNI_OK on success
145   virtual jint initialize();
146 
147   void post_initialize();
148   void update_counters();
149 
150   size_t capacity() const;
151   size_t used() const;
152 
153   // Return "true" if all generations have reached the
154   // maximal committed limit that they can reach, without a garbage
155   // collection.
156   virtual bool is_maximal_no_gc() const;
157 
158   virtual void register_nmethod(nmethod* nm);
159   virtual void unregister_nmethod(nmethod* nm);
160   virtual void verify_nmethod(nmethod* nm);
161   virtual void flush_nmethod(nmethod* nm);
162 
163   void prune_scavengable_nmethods();
164 
165   size_t max_capacity() const;
166 
167   // Whether p is in the allocated part of the heap
168   bool is_in(const void* p) const;
169 
170   bool is_in_reserved(const void* p) const;
171 
172   bool is_in_young(oop p);  // reserved part
173   bool is_in_old(oop p);    // reserved part
174 
175   // Memory allocation.   "gc_time_limit_was_exceeded" will
176   // be set to true if the adaptive size policy determine that
177   // an excessive amount of time is being spent doing collections
178   // and caused a NULL to be returned.  If a NULL is not returned,
179   // "gc_time_limit_was_exceeded" has an undefined meaning.
180   HeapWord* mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded);
181 
182   // Allocation attempt(s) during a safepoint. It should never be called
183   // to allocate a new TLAB as this allocation might be satisfied out
184   // of the old generation.
185   HeapWord* failed_mem_allocate(size_t size);
186 
187   // Support for System.gc()
188   void collect(GCCause::Cause cause);
189 
190   // These also should be called by the vm thread at a safepoint (e.g., from a
191   // VM operation).
192   //
193   // The first collects the young generation only, unless the scavenge fails; it
194   // will then attempt a full gc.  The second collects the entire heap; if
195   // maximum_compaction is true, it will compact everything and clear all soft
196   // references.
197   inline void invoke_scavenge();
198 
199   // Perform a full collection
200   virtual void do_full_collection(bool clear_all_soft_refs);
201 
202   bool supports_inline_contig_alloc() const { return !UseNUMA; }
203 
204   HeapWord* volatile* top_addr() const { return !UseNUMA ? young_gen()->top_addr() : (HeapWord* volatile*)-1; }
205   HeapWord** end_addr() const { return !UseNUMA ? young_gen()->end_addr() : (HeapWord**)-1; }
206 
207   void ensure_parsability(bool retire_tlabs);
208   void resize_all_tlabs();
209 
210   bool supports_tlab_allocation() const { return true; }
211 
212   size_t tlab_capacity(Thread* thr) const;
213   size_t tlab_used(Thread* thr) const;
214   size_t unsafe_max_tlab_alloc(Thread* thr) const;
215 
216   void object_iterate(ObjectClosure* cl);
217   void safe_object_iterate(ObjectClosure* cl) { object_iterate(cl); }
218 
219   HeapWord* block_start(const void* addr) const;
220   bool block_is_obj(const HeapWord* addr) const;
221 
222   jlong millis_since_last_gc();
223 
224   void prepare_for_verify();
225   PSHeapSummary create_ps_heap_summary();
226   virtual void print_on(outputStream* st) const;
227   virtual void print_on_error(outputStream* st) const;
228   virtual void print_gc_threads_on(outputStream* st) const;
229   virtual void gc_threads_do(ThreadClosure* tc) const;
230   virtual void print_tracing_info() const;
231 
232   void verify(VerifyOption option /* ignored */);
233 
234   // Resize the young generation.  The reserved space for the
235   // generation may be expanded in preparation for the resize.
236   void resize_young_gen(size_t eden_size, size_t survivor_size);
237 
238   // Resize the old generation.  The reserved space for the
239   // generation may be expanded in preparation for the resize.
240   void resize_old_gen(size_t desired_free_space);
241 
242   // Save the tops of the spaces in all generations
243   void record_gen_tops_before_GC() PRODUCT_RETURN;
244 
245   // Mangle the unused parts of all spaces in the heap
246   void gen_mangle_unused_area() PRODUCT_RETURN;
247 
248   // Call these in sequential code around the processing of strong roots.
249   class ParStrongRootsScope : public MarkScope {
250    public:
251     ParStrongRootsScope();
252     ~ParStrongRootsScope();
253   };
254 
255   GCMemoryManager* old_gc_manager() const { return _old_manager; }
256   GCMemoryManager* young_gc_manager() const { return _young_manager; }
257 
258   WorkGang& workers() {
259     return _workers;
260   }
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