1 /* 2 * Copyright Amazon.com Inc. 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_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP 26 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP 27 28 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp" 29 #include "gc/shenandoah/shenandoahAllocRequest.hpp" 30 #include "gc/shenandoah/shenandoahGeneration.hpp" 31 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp" 32 #include "gc/shenandoah/shenandoahScanRemembered.hpp" 33 #include "gc/shenandoah/shenandoahSharedVariables.hpp" 34 35 class ShenandoahHeapRegion; 36 class ShenandoahHeapRegionClosure; 37 class ShenandoahOldHeuristics; 38 39 class ShenandoahOldGeneration : public ShenandoahGeneration { 40 private: 41 ShenandoahHeapRegion** _coalesce_and_fill_region_array; 42 ShenandoahOldHeuristics* _old_heuristics; 43 44 // After determining the desired size of the old generation (see compute_old_generation_balance), this 45 // quantity represents the number of regions above (surplus) or below (deficit) that size. 46 // This value is computed prior to the actual exchange of any regions. A positive value represents 47 // a surplus of old regions which will be transferred from old _to_ young. A negative value represents 48 // a deficit of regions that will be replenished by a transfer _from_ young to old. 49 ssize_t _region_balance; 50 51 // Set when evacuation in the old generation fails. When this is set, the control thread will initiate a 52 // full GC instead of a futile degenerated cycle. 53 ShenandoahSharedFlag _failed_evacuation; 54 55 // Bytes reserved within old-gen to hold the results of promotion. This is separate from 56 // and in addition to the evacuation reserve for intra-generation evacuations (ShenandoahGeneration::_evacuation_reserve). 57 size_t _promoted_reserve; 58 59 // Bytes of old-gen memory expended on promotions. This may be modified concurrently 60 // by mutators and gc workers when promotion LABs are retired during evacuation. It 61 // is therefore always accessed through atomic operations. This is increased when a 62 // PLAB is allocated for promotions. The value is decreased by the amount of memory 63 // remaining in a PLAB when it is retired. 64 size_t _promoted_expended; 65 66 // Represents the quantity of live bytes we expect to promote in place during the next 67 // evacuation cycle. This value is used by the young heuristic to trigger mixed collections. 68 // It is also used when computing the optimum size for the old generation. 69 size_t _promotion_potential; 70 71 // When a region is selected to be promoted in place, the remaining free memory is filled 72 // in to prevent additional allocations (preventing premature promotion of newly allocated 73 // objects. This field records the total amount of padding used for such regions. 74 size_t _pad_for_promote_in_place; 75 76 // During construction of the collection set, we keep track of regions that are eligible 77 // for promotion in place. These fields track the count of those humongous and regular regions. 78 // This data is used to force the evacuation phase even when the collection set is otherwise 79 // empty. 80 size_t _promotable_humongous_regions; 81 size_t _promotable_regular_regions; 82 83 // True if old regions may be safely traversed by the remembered set scan. 84 bool _is_parseable; 85 86 bool coalesce_and_fill(); 87 88 public: 89 ShenandoahOldGeneration(uint max_queues, size_t max_capacity, size_t soft_max_capacity); 90 91 virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode) override; 92 93 const char* name() const override { 94 return "OLD"; 95 } 96 97 ShenandoahOldHeuristics* heuristics() const override { 98 return _old_heuristics; 99 } 100 101 // See description in field declaration 102 void set_promoted_reserve(size_t new_val); 103 size_t get_promoted_reserve() const; 104 105 // The promotion reserve is increased when rebuilding the free set transfers a region to the old generation 106 void augment_promoted_reserve(size_t increment); 107 108 // This zeros out the expended promotion count after the promotion reserve is computed 109 void reset_promoted_expended(); 110 111 // This is incremented when allocations are made to copy promotions into the old generation 112 size_t expend_promoted(size_t increment); 113 114 // This is used to return unused memory from a retired promotion LAB 115 size_t unexpend_promoted(size_t decrement); 116 117 // This is used on the allocation path to gate promotions that would exceed the reserve 118 size_t get_promoted_expended() const; 119 120 // Test if there is enough memory reserved for this promotion 121 bool can_promote(size_t requested_bytes) const { 122 size_t promotion_avail = get_promoted_reserve(); 123 size_t promotion_expended = get_promoted_expended(); 124 return promotion_expended + requested_bytes <= promotion_avail; 125 } 126 127 // Test if there is enough memory available in the old generation to accommodate this request. 128 // The request will be subject to constraints on promotion and evacuation reserves. 129 bool can_allocate(const ShenandoahAllocRequest& req) const; 130 131 // Updates the promotion expenditure tracking and configures whether the plab may be used 132 // for promotions and evacuations, or just evacuations. 133 void configure_plab_for_current_thread(const ShenandoahAllocRequest &req); 134 135 // See description in field declaration 136 void set_region_balance(ssize_t balance) { _region_balance = balance; } 137 ssize_t get_region_balance() const { return _region_balance; } 138 // See description in field declaration 139 void set_promotion_potential(size_t val) { _promotion_potential = val; }; 140 size_t get_promotion_potential() const { return _promotion_potential; }; 141 142 // See description in field declaration 143 void set_pad_for_promote_in_place(size_t pad) { _pad_for_promote_in_place = pad; } 144 size_t get_pad_for_promote_in_place() const { return _pad_for_promote_in_place; } 145 146 // See description in field declaration 147 void set_expected_humongous_region_promotions(size_t region_count) { _promotable_humongous_regions = region_count; } 148 void set_expected_regular_region_promotions(size_t region_count) { _promotable_regular_regions = region_count; } 149 bool has_in_place_promotions() const { return (_promotable_humongous_regions + _promotable_regular_regions) > 0; } 150 151 // Class unloading may render the card table offsets unusable, if they refer to unmarked objects 152 bool is_parseable() const { return _is_parseable; } 153 void set_parseable(bool parseable); 154 155 // This will signal the heuristic to trigger an old generation collection 156 void handle_failed_transfer(); 157 158 // This will signal the control thread to run a full GC instead of a futile degenerated gc 159 void handle_failed_evacuation(); 160 161 // This logs that an evacuation to the old generation has failed 162 void handle_failed_promotion(Thread* thread, size_t size); 163 164 // A successful evacuation re-dirties the cards and registers the object with the remembered set 165 void handle_evacuation(HeapWord* obj, size_t words, bool promotion); 166 167 // Clear the flag after it is consumed by the control thread 168 bool clear_failed_evacuation() { 169 return _failed_evacuation.try_unset(); 170 } 171 172 // Transition to the next state after mixed evacuations have completed 173 void complete_mixed_evacuations(); 174 175 // Abandon any future mixed collections. This is invoked when all old regions eligible for 176 // inclusion in a mixed evacuation are pinned. This should be rare. 177 void abandon_mixed_evacuations(); 178 179 private: 180 RememberedScanner* _card_scan; 181 182 public: 183 RememberedScanner* card_scan() { return _card_scan; } 184 185 // Clear cards for given region 186 void clear_cards_for(ShenandoahHeapRegion* region); 187 188 // Mark card for this location as dirty 189 void mark_card_as_dirty(void* location); 190 191 void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) override; 192 193 void parallel_region_iterate_free(ShenandoahHeapRegionClosure* cl) override; 194 195 void heap_region_iterate(ShenandoahHeapRegionClosure* cl) override; 196 197 bool contains(ShenandoahHeapRegion* region) const override; 198 bool contains(oop obj) const override; 199 200 void set_concurrent_mark_in_progress(bool in_progress) override; 201 bool is_concurrent_mark_in_progress() override; 202 203 bool entry_coalesce_and_fill(); 204 void prepare_for_mixed_collections_after_global_gc(); 205 void prepare_gc() override; 206 void prepare_regions_and_collection_set(bool concurrent) override; 207 void record_success_concurrent(bool abbreviated) override; 208 void cancel_marking() override; 209 210 // Cancels old gc and transitions to the idle state 211 void cancel_gc(); 212 213 // We leave the SATB barrier on for the entirety of the old generation 214 // marking phase. In some cases, this can cause a write to a perfectly 215 // reachable oop to enqueue a pointer that later becomes garbage (because 216 // it points at an object that is later chosen for the collection set). There are 217 // also cases where the referent of a weak reference ends up in the SATB 218 // and is later collected. In these cases the oop in the SATB buffer becomes 219 // invalid and the _next_ cycle will crash during its marking phase. To 220 // avoid this problem, we "purge" the SATB buffers during the final update 221 // references phase if (and only if) an old generation mark is in progress. 222 // At this stage we can safely determine if any of the oops in the SATB 223 // buffer belong to trashed regions (before they are recycled). As it 224 // happens, flushing a SATB queue also filters out oops which have already 225 // been marked - which is the case for anything that is being evacuated 226 // from the collection set. 227 // 228 // Alternatively, we could inspect the state of the heap and the age of the 229 // object at the barrier, but we reject this approach because it is likely 230 // the performance impact would be too severe. 231 void transfer_pointers_from_satb(); 232 233 // True if there are old regions waiting to be selected for a mixed collection 234 bool has_unprocessed_collection_candidates(); 235 236 bool is_doing_mixed_evacuations() const { 237 return state() == EVACUATING || state() == EVACUATING_AFTER_GLOBAL; 238 } 239 240 bool is_preparing_for_mark() const { 241 return state() == FILLING; 242 } 243 244 bool is_idle() const { 245 return state() == WAITING_FOR_BOOTSTRAP; 246 } 247 248 bool is_bootstrapping() const { 249 return state() == BOOTSTRAPPING; 250 } 251 252 // Amount of live memory (bytes) in regions waiting for mixed collections 253 size_t unprocessed_collection_candidates_live_memory(); 254 255 // Abandon any regions waiting for mixed collections 256 void abandon_collection_candidates(); 257 258 public: 259 enum State { 260 FILLING, WAITING_FOR_BOOTSTRAP, BOOTSTRAPPING, MARKING, EVACUATING, EVACUATING_AFTER_GLOBAL 261 }; 262 263 #ifdef ASSERT 264 bool validate_waiting_for_bootstrap(); 265 #endif 266 267 private: 268 State _state; 269 270 static const size_t FRACTIONAL_DENOMINATOR = 65536; 271 272 // During initialization of the JVM, we search for the correct old-gen size by initially performing old-gen 273 // collection when old-gen usage is 50% more (INITIAL_GROWTH_BEFORE_COMPACTION) than the initial old-gen size 274 // estimate (3.125% of heap). The next old-gen trigger occurs when old-gen grows 25% larger than its live 275 // memory at the end of the first old-gen collection. Then we trigger again when old-gen grows 12.5% 276 // more than its live memory at the end of the previous old-gen collection. Thereafter, we trigger each time 277 // old-gen grows more than 12.5% following the end of its previous old-gen collection. 278 static const size_t INITIAL_GROWTH_BEFORE_COMPACTION = FRACTIONAL_DENOMINATOR / 2; // 50.0% 279 280 // INITIAL_LIVE_FRACTION represents the initial guess of how large old-gen should be. We estimate that old-gen 281 // needs to consume 6.25% of the total heap size. And we "pretend" that we start out with this amount of live 282 // old-gen memory. The first old-collection trigger will occur when old-gen occupies 50% more than this initial 283 // approximation of the old-gen memory requirement, in other words when old-gen usage is 150% of 6.25%, which 284 // is 9.375% of the total heap size. 285 static const uint16_t INITIAL_LIVE_FRACTION = FRACTIONAL_DENOMINATOR / 16; // 6.25% 286 287 size_t _live_bytes_after_last_mark; 288 289 // How much growth in usage before we trigger old collection, per FRACTIONAL_DENOMINATOR (65_536) 290 size_t _growth_before_compaction; 291 const size_t _min_growth_before_compaction; // Default is 12.5% 292 293 void validate_transition(State new_state) NOT_DEBUG_RETURN; 294 295 public: 296 State state() const { 297 return _state; 298 } 299 300 const char* state_name() const { 301 return state_name(_state); 302 } 303 304 void transition_to(State new_state); 305 306 size_t get_live_bytes_after_last_mark() const; 307 void set_live_bytes_after_last_mark(size_t new_live); 308 309 size_t usage_trigger_threshold() const; 310 311 bool can_start_gc() { 312 return _state == WAITING_FOR_BOOTSTRAP; 313 } 314 315 static const char* state_name(State state); 316 317 }; 318 319 320 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP