1 /* 2 * Copyright (c) 2021, 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_GC_SHENANDOAH_HEURISTICS_SHENANDOAHOLDHEURISTICS_HPP 26 #define SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHOLDHEURISTICS_HPP 27 28 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp" 29 #include "gc/shenandoah/shenandoahGeneration.hpp" 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp" 32 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp" 33 34 class ShenandoahOldHeuristics : public ShenandoahHeuristics { 35 36 private: 37 38 // if (_generation->generation_mode() == OLD) _old_collection_candidates 39 // represent the number of regions selected for collection following the 40 // most recently completed old-gen mark that have not yet been selected 41 // for evacuation and _next_collection_candidate is the index within 42 // _region_data of the next candidate region to be selected for evacuation. 43 // if (_generation->generation_mode() != OLD) these two variables are 44 // not used. 45 uint _old_collection_candidates; 46 uint _next_old_collection_candidate; 47 48 // At the time we select the old-gen collection set, _hidden_old_collection_candidates 49 // and _hidden_next_old_collection_candidates are set to remember the intended old-gen 50 // collection set. After all old-gen regions not in the old-gen collection set have been 51 // coalesced and filled, the content of these variables is copied to _old_collection_candidates 52 // and _next_old_collection_candidates so that evacuations can begin evacuating these regions. 53 uint _hidden_old_collection_candidates; 54 uint _hidden_next_old_collection_candidate; 55 56 // if (_generation->generation_mode() == OLD) 57 // _old_coalesce_and_fill_candidates represents the number of regions 58 // that were chosen for the garbage contained therein to be coalesced 59 // and filled and _first_coalesce_and_fill_candidate represents the 60 // the index of the first such region within the _region_data array. 61 // if (_generation->generation_mode() != OLD) these two variables are 62 // not used. 63 uint _old_coalesce_and_fill_candidates; 64 uint _first_coalesce_and_fill_candidate; 65 66 // This can be the 'static' or 'adaptive' heuristic. 67 ShenandoahHeuristics* _trigger_heuristic; 68 69 // Flag is set when promotion failure is detected (by gc thread), cleared when old generation collection begins (by control thread) 70 volatile bool _promotion_failed; 71 72 // Prepare for evacuation of old-gen regions by capturing the mark results of a recently completed concurrent mark pass. 73 void prepare_for_old_collections(); 74 75 protected: 76 virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set, RegionData* data, size_t data_size, 77 size_t free) override; 78 79 public: 80 ShenandoahOldHeuristics(ShenandoahGeneration* generation, ShenandoahHeuristics* trigger_heuristic); 81 82 virtual void choose_collection_set(ShenandoahCollectionSet* collection_set, ShenandoahOldHeuristics* old_heuristics) override; 83 84 // Return true iff the collection set is primed with at least one old-gen region. 85 bool prime_collection_set(ShenandoahCollectionSet* set); 86 87 // Having coalesced and filled all old-gen heap regions that are not part of the old-gen collection set, begin 88 // evacuating the collection set. 89 void start_old_evacuations(); 90 91 // How many old-collection candidates have not yet been processed? 92 uint unprocessed_old_collection_candidates(); 93 94 // How many old or hidden collection candidates have not yet been processed? 95 uint unprocessed_old_or_hidden_collection_candidates(); 96 97 // Return the next old-collection candidate in order of decreasing amounts of garbage. (We process most-garbage regions 98 // first.) This does not consume the candidate. If the candidate is selected for inclusion in a collection set, then 99 // the candidate is consumed by invoking consume_old_collection_candidate(). 100 ShenandoahHeapRegion* next_old_collection_candidate(); 101 102 // Adjust internal state to reflect that one fewer old-collection candidate remains to be processed. 103 void consume_old_collection_candidate(); 104 105 // How many old-collection regions were identified at the end of the most recent old-gen mark to require their 106 // unmarked objects to be coalesced and filled? 107 uint old_coalesce_and_fill_candidates(); 108 109 // Fill in buffer with all of the old-collection regions that were identified at the end of the most recent old-gen 110 // mark to require their unmarked objects to be coalesced and filled. The buffer array must have at least 111 // old_coalesce_and_fill_candidates() entries, or memory may be corrupted when this function overwrites the 112 // end of the array. 113 void get_coalesce_and_fill_candidates(ShenandoahHeapRegion** buffer); 114 115 // If a GLOBAL gc occurs, it will collect the entire heap which invalidates any collection candidates being 116 // held by this heuristic for supplying mixed collections. 117 void abandon_collection_candidates(); 118 119 // Notify the heuristic of promotion failures. The promotion attempt will be skipped and the object will 120 // be evacuated into the young generation. The collection should complete normally, but we want to schedule 121 // an old collection as soon as possible. 122 void handle_promotion_failure(); 123 124 virtual void record_cycle_start() override; 125 126 virtual void record_cycle_end() override; 127 128 virtual bool should_start_gc() override; 129 130 virtual bool should_degenerate_cycle() override; 131 132 virtual void record_success_concurrent(bool abbreviated) override; 133 134 virtual void record_success_degenerated() override; 135 136 virtual void record_success_full() override; 137 138 virtual void record_allocation_failure_gc() override; 139 140 virtual void record_requested_gc() override; 141 142 virtual bool can_unload_classes() override; 143 144 virtual bool can_unload_classes_normal() override; 145 146 virtual bool should_unload_classes() override; 147 148 virtual const char* name() override; 149 150 virtual bool is_diagnostic() override; 151 152 virtual bool is_experimental() override; 153 154 }; 155 156 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHOLDHEURISTICS_HPP