1 /*
  2  * Copyright (c) 2001, 2024, 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_SERIAL_DEFNEWGENERATION_HPP
 26 #define SHARE_GC_SERIAL_DEFNEWGENERATION_HPP
 27 
 28 #include "gc/serial/cSpaceCounters.hpp"
 29 #include "gc/serial/generation.hpp"
 30 #include "gc/serial/tenuredGeneration.hpp"
 31 #include "gc/shared/ageTable.hpp"
 32 #include "gc/shared/copyFailedInfo.hpp"
 33 #include "gc/shared/gc_globals.hpp"
 34 #include "gc/shared/generationCounters.hpp"
 35 #include "gc/shared/preservedMarks.hpp"
 36 #include "gc/shared/stringdedup/stringDedup.hpp"
 37 #include "gc/shared/tlab_globals.hpp"
 38 #include "utilities/align.hpp"
 39 #include "utilities/stack.hpp"
 40 
 41 class ContiguousSpace;
 42 class CSpaceCounters;
 43 class OldGenScanClosure;
 44 class YoungGenScanClosure;
 45 class DefNewTracer;
 46 class ScanWeakRefClosure;
 47 class SerialHeap;
 48 class STWGCTimer;
 49 
 50 // DefNewGeneration is a young generation containing eden, from- and
 51 // to-space.
 52 
 53 class DefNewGeneration: public Generation {
 54   friend class VMStructs;
 55 
 56   TenuredGeneration* _old_gen;
 57 
 58   uint        _tenuring_threshold;   // Tenuring threshold for next collection.
 59   AgeTable    _age_table;
 60   // Size of object to pretenure in words; command line provides bytes
 61   size_t      _pretenure_size_threshold_words;
 62 
 63   // ("Weak") Reference processing support
 64   SpanSubjectToDiscoveryClosure _span_based_discoverer;
 65   ReferenceProcessor* _ref_processor;
 66 
 67   AgeTable*   age_table() { return &_age_table; }
 68 
 69   // Initialize state to optimistically assume no promotion failure will
 70   // happen.
 71   void   init_assuming_no_promotion_failure();
 72   // True iff a promotion has failed in the current collection.
 73   bool   _promotion_failed;
 74   bool   promotion_failed() { return _promotion_failed; }
 75   PromotionFailedInfo _promotion_failed_info;
 76 
 77   // Handling promotion failure.  A young generation collection
 78   // can fail if a live object cannot be copied out of its
 79   // location in eden or from-space during the collection.  If
 80   // a collection fails, the young generation is left in a
 81   // consistent state such that it can be collected by a
 82   // full collection.
 83   //   Before the collection
 84   //     Objects are in eden or from-space
 85   //     All roots into the young generation point into eden or from-space.
 86   //
 87   //   After a failed collection
 88   //     Objects may be in eden, from-space, or to-space
 89   //     An object A in eden or from-space may have a copy B
 90   //       in to-space.  If B exists, all roots that once pointed
 91   //       to A must now point to B.
 92   //     All objects in the young generation are unmarked.
 93   //     Eden, from-space, and to-space will all be collected by
 94   //       the full collection.
 95   void handle_promotion_failure(oop);
 96 
 97   // In the absence of promotion failure, we wouldn't look at "from-space"
 98   // objects after a young-gen collection.  When promotion fails, however,
 99   // the subsequent full collection will look at from-space objects:
100   // therefore we must remove their forwarding pointers.
101   void remove_forwarding_pointers();
102 
103   virtual void restore_preserved_marks();
104 
105   // Preserved marks
106   PreservedMarksSet _preserved_marks_set;
107 
108   Stack<oop, mtGC> _promo_failure_scan_stack;
109   void drain_promo_failure_scan_stack(void);
110   bool _promo_failure_drain_in_progress;
111 
112   // Performance Counters
113   GenerationCounters*  _gen_counters;
114   CSpaceCounters*      _eden_counters;
115   CSpaceCounters*      _from_counters;
116   CSpaceCounters*      _to_counters;
117 
118   // sizing information
119   size_t               _max_eden_size;
120   size_t               _max_survivor_size;
121 
122   // Allocation support
123   bool _should_allocate_from_space;
124   bool should_allocate_from_space() const {
125     return _should_allocate_from_space;
126   }
127   void clear_should_allocate_from_space() {
128     _should_allocate_from_space = false;
129   }
130   void set_should_allocate_from_space() {
131     _should_allocate_from_space = true;
132   }
133 
134   // Tenuring
135   void adjust_desired_tenuring_threshold();
136 
137   // Spaces
138   ContiguousSpace* _eden_space;
139   ContiguousSpace* _from_space;
140   ContiguousSpace* _to_space;
141 
142   STWGCTimer* _gc_timer;
143 
144   DefNewTracer* _gc_tracer;
145 
146   StringDedup::Requests _string_dedup_requests;
147 
148   // Return the size of a survivor space if this generation were of size
149   // gen_size.
150   size_t compute_survivor_size(size_t gen_size, size_t alignment) const {
151     size_t n = gen_size / (SurvivorRatio + 2);
152     return n > alignment ? align_down(n, alignment) : alignment;
153   }
154 
155  public:
156   DefNewGeneration(ReservedSpace rs,
157                    size_t initial_byte_size,
158                    size_t min_byte_size,
159                    size_t max_byte_size,
160                    const char* policy="Serial young collection pauses");
161 
162   // allocate and initialize ("weak") refs processing support
163   void ref_processor_init();
164   ReferenceProcessor* ref_processor() { return _ref_processor; }
165 
166   // Accessing spaces
167   ContiguousSpace* eden() const           { return _eden_space; }
168   ContiguousSpace* from() const           { return _from_space; }
169   ContiguousSpace* to()   const           { return _to_space;   }
170 
171   // Space enquiries
172   size_t capacity() const;
173   size_t used() const;
174   size_t free() const;
175   size_t max_capacity() const;
176   size_t capacity_before_gc() const;
177 
178   // Returns "TRUE" iff "p" points into the used areas in each space of young-gen.
179   bool is_in(const void* p) const;
180 
181   // Return an estimate of the maximum allocation that could be performed
182   // in the generation without triggering any collection or expansion
183   // activity.  It is "unsafe" because no locks are taken; the result
184   // should be treated as an approximation, not a guarantee, for use in
185   // heuristic resizing decisions.
186   size_t unsafe_max_alloc_nogc() const;
187 
188   size_t contiguous_available() const;
189 
190   size_t max_eden_size() const              { return _max_eden_size; }
191   size_t max_survivor_size() const          { return _max_survivor_size; }
192 
193   // Thread-local allocation buffers
194   size_t tlab_capacity() const;
195   size_t tlab_used() const;
196   size_t unsafe_max_tlab_alloc() const;
197 
198   // Grow the generation by the specified number of bytes.
199   // The size of bytes is assumed to be properly aligned.
200   // Return true if the expansion was successful.
201   bool expand(size_t bytes);
202 
203 
204   // Iteration
205   void object_iterate(ObjectClosure* blk);
206 
207   HeapWord* block_start(const void* p) const;
208 
209   // Allocation support
210   bool should_allocate(size_t word_size, bool is_tlab) {
211     assert(UseTLAB || !is_tlab, "Should not allocate tlab");
212     assert(word_size != 0, "precondition");
213 
214     size_t overflow_limit    = (size_t)1 << (BitsPerSize_t - LogHeapWordSize);
215 
216     const bool overflows     = word_size >= overflow_limit;
217     const bool check_too_big = _pretenure_size_threshold_words > 0;
218     const bool not_too_big   = word_size < _pretenure_size_threshold_words;
219     const bool size_ok       = is_tlab || !check_too_big || not_too_big;
220 
221     bool result = !overflows &&
222                   size_ok;
223 
224     return result;
225   }
226 
227   HeapWord* allocate(size_t word_size, bool is_tlab);
228   HeapWord* allocate_from_space(size_t word_size);
229 
230   HeapWord* par_allocate(size_t word_size, bool is_tlab);
231 
232   void gc_epilogue(bool full);
233 
234   // For Old collection (part of running Full GC), the DefNewGeneration can
235   // contribute the free part of "to-space" as the scratch space.
236   void contribute_scratch(void*& scratch, size_t& num_words);
237 
238   // Reset for contribution of "to-space".
239   void reset_scratch();
240 
241   // GC support
242   void compute_new_size();
243 
244   // Returns true if the collection is likely to be safely
245   // completed. Even if this method returns true, a collection
246   // may not be guaranteed to succeed, and the system should be
247   // able to safely unwind and recover from that failure, albeit
248   // at some additional cost.
249   bool collection_attempt_is_safe();
250 
251   bool collect(bool clear_all_soft_refs);
252 
253   HeapWord* expand_and_allocate(size_t size, bool is_tlab);
254 
255   oop copy_to_survivor_space(oop old);
256   uint tenuring_threshold() { return _tenuring_threshold; }
257 
258   // Performance Counter support
259   void update_counters();
260 
261   // Printing
262   virtual const char* name() const;
263   virtual const char* short_name() const { return "DefNew"; }
264 
265   void print_on(outputStream* st) const;
266 
267   void verify();
268 
269   bool promo_failure_scan_is_complete() const {
270     return _promo_failure_scan_stack.is_empty();
271   }
272 
273   DefNewTracer* gc_tracer() const { return _gc_tracer; }
274 
275  protected:
276   // If clear_space is true, clear the survivor spaces.  Eden is
277   // cleared if the minimum size of eden is 0.  If mangle_space
278   // is true, also mangle the space in debug mode.
279   void compute_space_boundaries(uintx minimum_eden_size,
280                                 bool clear_space,
281                                 bool mangle_space);
282 
283   // Return adjusted new size for NewSizeThreadIncrease.
284   // If any overflow happens, revert to previous new size.
285   size_t adjust_for_thread_increase(size_t new_size_candidate,
286                                     size_t new_size_before,
287                                     size_t alignment,
288                                     size_t thread_increase_size) const;
289 
290   size_t calculate_thread_increase_size(int threads_count) const;
291 
292 
293   // Scavenge support
294   void swap_spaces();
295 };
296 
297 #endif // SHARE_GC_SERIAL_DEFNEWGENERATION_HPP