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 STWGCTimer* _gc_timer; 137 138 DefNewTracer* _gc_tracer; 139 140 StringDedup::Requests _string_dedup_requests; 141 142 // Return the size of a survivor space if this generation were of size 143 // gen_size. 144 size_t compute_survivor_size(size_t gen_size, size_t alignment) const { 145 size_t n = gen_size / (SurvivorRatio + 2); 146 return n > alignment ? align_down(n, alignment) : alignment; 147 } 148 149 public: 150 DefNewGeneration(ReservedSpace rs, 151 size_t initial_byte_size, 152 size_t min_byte_size, 153 size_t max_byte_size, 154 const char* policy="Serial young collection pauses"); 155 156 // allocate and initialize ("weak") refs processing support 157 void ref_processor_init(); 158 ReferenceProcessor* ref_processor() { return _ref_processor; } 159 160 // Accessing spaces 161 ContiguousSpace* eden() const { return _eden_space; } 162 ContiguousSpace* from() const { return _from_space; } 163 ContiguousSpace* to() const { return _to_space; } 164 165 // Space enquiries 166 size_t capacity() const; 167 size_t used() const; 168 size_t free() const; 169 size_t max_capacity() const; 170 size_t capacity_before_gc() const; 171 172 // Returns "TRUE" iff "p" points into the used areas in each space of young-gen. 173 bool is_in(const void* p) const; 174 175 // Return an estimate of the maximum allocation that could be performed 176 // in the generation without triggering any collection or expansion 177 // activity. It is "unsafe" because no locks are taken; the result 178 // should be treated as an approximation, not a guarantee, for use in 179 // heuristic resizing decisions. 180 size_t unsafe_max_alloc_nogc() const; 181 182 size_t contiguous_available() const; 183 184 size_t max_eden_size() const { return _max_eden_size; } 185 size_t max_survivor_size() const { return _max_survivor_size; } 186 187 // Thread-local allocation buffers 188 size_t tlab_capacity() const; 189 size_t tlab_used() const; 190 size_t unsafe_max_tlab_alloc() const; 191 192 // Grow the generation by the specified number of bytes. 193 // The size of bytes is assumed to be properly aligned. 194 // Return true if the expansion was successful. 195 bool expand(size_t bytes); 196 197 198 // Iteration 199 void object_iterate(ObjectClosure* blk); 200 201 HeapWord* block_start(const void* p) const; 202 203 // Allocation support 204 bool should_allocate(size_t word_size, bool is_tlab) { 205 assert(UseTLAB || !is_tlab, "Should not allocate tlab"); 206 assert(word_size != 0, "precondition"); 207 208 size_t overflow_limit = (size_t)1 << (BitsPerSize_t - LogHeapWordSize); 209 210 const bool overflows = word_size >= overflow_limit; 211 const bool check_too_big = _pretenure_size_threshold_words > 0; 212 const bool not_too_big = word_size < _pretenure_size_threshold_words; 213 const bool size_ok = is_tlab || !check_too_big || not_too_big; 214 215 bool result = !overflows && 216 size_ok; 217 218 return result; 219 } 220 221 HeapWord* allocate(size_t word_size, bool is_tlab); 222 HeapWord* allocate_from_space(size_t word_size); 223 224 HeapWord* par_allocate(size_t word_size, bool is_tlab); 225 226 void gc_epilogue(bool full); 227 228 // For Old collection (part of running Full GC), the DefNewGeneration can 229 // contribute the free part of "to-space" as the scratch space. 230 void contribute_scratch(void*& scratch, size_t& num_words); 231 232 // Reset for contribution of "to-space". 233 void reset_scratch(); 234 235 // GC support 236 void compute_new_size(); 237 238 // Returns true if the collection is likely to be safely 239 // completed. Even if this method returns true, a collection 240 // may not be guaranteed to succeed, and the system should be 241 // able to safely unwind and recover from that failure, albeit 242 // at some additional cost. 243 bool collection_attempt_is_safe(); 244 245 bool collect(bool clear_all_soft_refs); 246 247 HeapWord* expand_and_allocate(size_t size, bool is_tlab); 248 249 oop copy_to_survivor_space(oop old); 250 uint tenuring_threshold() { return _tenuring_threshold; } 251 252 // Performance Counter support 253 void update_counters(); 254 255 // Printing 256 virtual const char* name() const; 257 virtual const char* short_name() const { return "DefNew"; } 258 259 void print_on(outputStream* st) const; 260 261 void verify(); 262 263 bool promo_failure_scan_is_complete() const { 264 return _promo_failure_scan_stack.is_empty(); 265 } 266 267 DefNewTracer* gc_tracer() const { return _gc_tracer; } 268 269 protected: 270 // If clear_space is true, clear the survivor spaces. Eden is 271 // cleared if the minimum size of eden is 0. If mangle_space 272 // is true, also mangle the space in debug mode. 273 void compute_space_boundaries(uintx minimum_eden_size, 274 bool clear_space, 275 bool mangle_space); 276 277 // Return adjusted new size for NewSizeThreadIncrease. 278 // If any overflow happens, revert to previous new size. 279 size_t adjust_for_thread_increase(size_t new_size_candidate, 280 size_t new_size_before, 281 size_t alignment, 282 size_t thread_increase_size) const; 283 284 size_t calculate_thread_increase_size(int threads_count) const; 285 286 287 // Scavenge support 288 void swap_spaces(); 289 }; 290 291 #endif // SHARE_GC_SERIAL_DEFNEWGENERATION_HPP