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