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 
 29 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 30 
 31 class ShenandoahCollectionSet;
 32 class ShenandoahHeapRegion;
 33 class ShenandoahOldGeneration;
 34 
 35 class ShenandoahOldHeuristics : public ShenandoahHeuristics {
 36 
 37 private:
 38 
 39   static uint NOT_FOUND;
 40 
 41   // After final marking of the old generation, this heuristic will select
 42   // a set of candidate regions to be included in subsequent mixed collections.
 43   // The regions are sorted into a `_region_data` array (declared in base
 44   // class) in decreasing order of garbage. The heuristic will give priority
 45   // to regions containing more garbage.
 46 
 47   // The following members are used to keep track of which candidate regions
 48   // have yet to be added to a mixed collection. There is also some special
 49   // handling for pinned regions, described further below.
 50 
 51   // Pinned regions may not be included in the collection set. Any old regions
 52   // which were pinned at the time when old regions were added to the mixed
 53   // collection will have been skipped. These regions are still contain garbage,
 54   // so we want to include them at the start of the list of candidates for the
 55   // _next_ mixed collection cycle. This variable is the index of the _first_
 56   // old region which is pinned when the mixed collection set is formed.
 57   uint _first_pinned_candidate;
 58 
 59   // This is the index of the last region which is above the garbage threshold.
 60   // No regions after this will be considered for inclusion in a mixed collection
 61   // set.
 62   uint _last_old_collection_candidate;
 63 
 64   // This index points to the first candidate in line to be added to the mixed
 65   // collection set. It is updated as regions are added to the collection set.
 66   uint _next_old_collection_candidate;
 67 
 68   // This is the last index in the array of old regions which were active at
 69   // the end of old final mark.
 70   uint _last_old_region;
 71 
 72   // This can be the 'static' or 'adaptive' heuristic.
 73   ShenandoahHeuristics* _trigger_heuristic;
 74 
 75   // Flag is set when promotion failure is detected (by gc thread), and cleared when
 76   // old generation collection begins (by control thread).
 77   volatile bool _promotion_failed;
 78 
 79   // Keep a pointer to our generation that we can use without down casting a protected member from the base class.
 80   ShenandoahOldGeneration* _old_generation;
 81 
 82  protected:
 83   virtual void choose_collection_set_from_regiondata(ShenandoahCollectionSet* set, RegionData* data, size_t data_size,
 84                                                      size_t free) override;
 85 
 86 public:
 87   ShenandoahOldHeuristics(ShenandoahOldGeneration* generation, ShenandoahHeuristics* trigger_heuristic);
 88 
 89   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set, ShenandoahOldHeuristics* old_heuristics) override;
 90 
 91   // Prepare for evacuation of old-gen regions by capturing the mark results of a recently completed concurrent mark pass.
 92   void prepare_for_old_collections();
 93 
 94   // Return true iff the collection set is primed with at least one old-gen region.
 95   bool prime_collection_set(ShenandoahCollectionSet* set);
 96 
 97   // How many old-collection candidates have not yet been processed?
 98   uint unprocessed_old_collection_candidates();
 99 
100   // How many old or hidden collection candidates have not yet been processed?
101   uint last_old_collection_candidate_index();
102 
103   // Return the next old-collection candidate in order of decreasing amounts of garbage.  (We process most-garbage regions
104   // first.)  This does not consume the candidate.  If the candidate is selected for inclusion in a collection set, then
105   // the candidate is consumed by invoking consume_old_collection_candidate().
106   ShenandoahHeapRegion* next_old_collection_candidate();
107 
108   // Adjust internal state to reflect that one fewer old-collection candidate remains to be processed.
109   void consume_old_collection_candidate();
110 
111   // How many old-collection regions were identified at the end of the most recent old-gen mark to require their
112   // unmarked objects to be coalesced and filled?
113   uint last_old_region_index() const;
114 
115   // Fill in buffer with all of the old-collection regions that were identified at the end of the most recent old-gen
116   // mark to require their unmarked objects to be coalesced and filled.  The buffer array must have at least
117   // last_old_region_index() entries, or memory may be corrupted when this function overwrites the
118   // end of the array.
119   unsigned int get_coalesce_and_fill_candidates(ShenandoahHeapRegion** buffer);
120 
121   // If a GLOBAL gc occurs, it will collect the entire heap which invalidates any collection candidates being
122   // held by this heuristic for supplying mixed collections.
123   void abandon_collection_candidates();
124 
125   // Notify the heuristic of promotion failures. The promotion attempt will be skipped and the object will
126   // be evacuated into the young generation. The collection should complete normally, but we want to schedule
127   // an old collection as soon as possible.
128   void handle_promotion_failure();
129 
130   virtual void record_cycle_start() override;
131 
132   virtual void record_cycle_end() override;
133 
134   virtual bool should_start_gc() override;
135 
136   virtual bool should_degenerate_cycle() override;
137 
138   virtual void record_success_concurrent(bool abbreviated) override;
139 
140   virtual void record_success_degenerated() override;
141 
142   virtual void record_success_full() override;
143 
144   virtual void record_allocation_failure_gc() override;
145 
146   virtual void record_requested_gc() override;
147 
148   virtual void reset_gc_learning() override;
149 
150   virtual bool can_unload_classes() override;
151 
152   virtual bool can_unload_classes_normal() override;
153 
154   virtual bool should_unload_classes() override;
155 
156   virtual const char* name() override;
157 
158   virtual bool is_diagnostic() override;
159 
160   virtual bool is_experimental() override;
161 
162  private:
163   void slide_pinned_regions_to_front();
164 };
165 
166 #endif // SHARE_GC_SHENANDOAH_HEURISTICS_SHENANDOAHOLDHEURISTICS_HPP