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