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_SHENANDOAHGENERATION_HPP
 26 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "gc/shenandoah/heuristics/shenandoahSpaceInfo.hpp"
 30 #include "gc/shenandoah/shenandoahAffiliation.hpp"
 31 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 32 #include "gc/shenandoah/shenandoahLock.hpp"
 33 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
 34 
 35 class ShenandoahCollectionSet;
 36 class ShenandoahHeap;
 37 class ShenandoahHeapRegion;
 38 class ShenandoahHeapRegionClosure;
 39 class ShenandoahHeuristics;
 40 class ShenandoahMode;
 41 class ShenandoahReferenceProcessor;
 42 
 43 
 44 class ShenandoahGeneration : public CHeapObj<mtGC>, public ShenandoahSpaceInfo {
 45   friend class VMStructs;
 46 private:
 47   ShenandoahGenerationType const _type;
 48 
 49   // Marking task queues and completeness
 50   ShenandoahObjToScanQueueSet* _task_queues;
 51   ShenandoahSharedFlag _is_marking_complete;
 52 
 53   ShenandoahReferenceProcessor* const _ref_processor;
 54 
 55   volatile size_t _affiliated_region_count;
 56 
 57   // How much free memory is left in the last region of humongous objects.
 58   // This is _not_ included in used, but it _is_ deducted from available,
 59   // which gives the heuristics a more accurate view of how much memory remains
 60   // for allocation. This figure is also included the heap status logging.
 61   // The units are bytes. The value is only changed on a safepoint or under the
 62   // heap lock.
 63   size_t _humongous_waste;
 64 
 65   // Bytes reserved within this generation to hold evacuated objects from the collection set
 66   size_t _evacuation_reserve;
 67 
 68 protected:
 69   // Usage
 70 
 71   volatile size_t _used;
 72   volatile size_t _bytes_allocated_since_gc_start;
 73   size_t _max_capacity;
 74 
 75   ShenandoahHeuristics* _heuristics;
 76 
 77 private:
 78   // Compute evacuation budgets prior to choosing collection set.
 79   void compute_evacuation_budgets(ShenandoahHeap* heap);
 80 
 81   // Adjust evacuation budgets after choosing collection set.
 82   void adjust_evacuation_budgets(ShenandoahHeap* heap,
 83                                  ShenandoahCollectionSet* collection_set);
 84 
 85   // Preselect for possible inclusion into the collection set exactly the most
 86   // garbage-dense regions, including those that satisfy criteria 1 & 2 below,
 87   // and whose live bytes will fit within old_available budget:
 88   // Criterion 1. region age >= tenuring threshold
 89   // Criterion 2. region garbage percentage > ShenandoahOldGarbageThreshold
 90   //
 91   // Identifies regions eligible for promotion in place,
 92   // being those of at least tenuring_threshold age that have lower garbage
 93   // density.
 94   //
 95   // Updates promotion_potential and pad_for_promote_in_place fields
 96   // of the heap. Returns bytes of live object memory in the preselected
 97   // regions, which are marked in the preselected_regions() indicator
 98   // array of the heap's collection set, which should be initialized
 99   // to false.
100   size_t select_aged_regions(size_t old_available);
101 
102   size_t available(size_t capacity) const;
103 
104  public:
105   ShenandoahGeneration(ShenandoahGenerationType type,
106                        uint max_workers,
107                        size_t max_capacity);
108   ~ShenandoahGeneration();
109 
110   bool is_young() const  { return _type == YOUNG; }
111   bool is_old() const    { return _type == OLD; }
112   bool is_global() const { return _type == GLOBAL || _type == NON_GEN; }
113 
114   // see description in field declaration
115   void set_evacuation_reserve(size_t new_val);
116   size_t get_evacuation_reserve() const;
117   void augment_evacuation_reserve(size_t increment);
118 
119   inline ShenandoahGenerationType type() const { return _type; }
120 
121   virtual ShenandoahHeuristics* heuristics() const { return _heuristics; }
122 
123   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
124 
125   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
126 
127   size_t max_capacity() const override      { return _max_capacity; }
128   virtual size_t used_regions() const;
129   virtual size_t used_regions_size() const;
130   virtual size_t free_unaffiliated_regions() const;
131   size_t used() const override { return Atomic::load(&_used); }
132   size_t available() const override;
133   size_t available_with_reserve() const;
134   size_t used_including_humongous_waste() const {
135     return used() + get_humongous_waste();
136   }
137 
138   // Returns the memory available based on the _soft_ max heap capacity (soft_max_heap - used).
139   // The soft max heap size may be adjusted lower than the max heap size to cause the trigger
140   // to believe it has less memory available than is _really_ available. Lowering the soft
141   // max heap size will cause the adaptive heuristic to run more frequent cycles.
142   size_t soft_available() const override;
143 
144   size_t bytes_allocated_since_gc_start() const override;
145   void reset_bytes_allocated_since_gc_start();
146   void increase_allocated(size_t bytes);
147 
148   // These methods change the capacity of the generation by adding or subtracting the given number of bytes from the current
149   // capacity, returning the capacity of the generation following the change.
150   size_t increase_capacity(size_t increment);
151   size_t decrease_capacity(size_t decrement);
152 
153   // Set the capacity of the generation, returning the value set
154   size_t set_capacity(size_t byte_size);
155 
156   void log_status(const char* msg) const;
157 
158   // Used directly by FullGC
159   template <bool FOR_CURRENT_CYCLE, bool FULL_GC = false>
160   void reset_mark_bitmap();
161 
162   // Used by concurrent and degenerated GC to reset remembered set.
163   void swap_card_tables();
164 
165   // Update the read cards with the state of the write table (write table is not cleared).
166   void merge_write_table();
167 
168   // Called before init mark, expected to prepare regions for marking.
169   virtual void prepare_gc();
170 
171   // Called during final mark, chooses collection set, rebuilds free set.
172   virtual void prepare_regions_and_collection_set(bool concurrent);
173 
174   // Cancel marking (used by Full collect and when cancelling cycle).
175   virtual void cancel_marking();
176 
177   virtual bool contains(ShenandoahAffiliation affiliation) const = 0;
178 
179   // Return true if this region is affiliated with this generation.
180   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
181 
182   // Return true if this object is affiliated with this generation.
183   virtual bool contains(oop obj) const = 0;
184 
185   // Apply closure to all regions affiliated with this generation.
186   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
187 
188   // Apply closure to all regions affiliated with this generation (include free regions);
189   virtual void parallel_heap_region_iterate_free(ShenandoahHeapRegionClosure* cl);
190 
191   // Apply closure to all regions affiliated with this generation (single threaded).
192   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
193 
194   // This is public to support cancellation of marking when a Full cycle is started.
195   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
196 
197   // Check the bitmap only for regions belong to this generation.
198   bool is_bitmap_clear();
199 
200   // We need to track the status of marking for different generations.
201   bool is_mark_complete() { return _is_marking_complete.is_set(); }
202   virtual void set_mark_complete();
203   virtual void set_mark_incomplete();
204 
205   ShenandoahMarkingContext* complete_marking_context();
206 
207   // Task queues
208   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
209   virtual void reserve_task_queues(uint workers);
210   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
211 
212   // Scan remembered set at start of concurrent young-gen marking.
213   void scan_remembered_set(bool is_concurrent);
214 
215   // Return the updated value of affiliated_region_count
216   size_t increment_affiliated_region_count();
217 
218   // Return the updated value of affiliated_region_count
219   size_t decrement_affiliated_region_count();
220   // Same as decrement_affiliated_region_count, but w/o the need to hold heap lock before being called.
221   size_t decrement_affiliated_region_count_without_lock();
222 
223   // Return the updated value of affiliated_region_count
224   size_t increase_affiliated_region_count(size_t delta);
225 
226   // Return the updated value of affiliated_region_count
227   size_t decrease_affiliated_region_count(size_t delta);
228 
229   void establish_usage(size_t num_regions, size_t num_bytes, size_t humongous_waste);
230 
231   void increase_used(size_t bytes);
232   void decrease_used(size_t bytes);
233 
234   void increase_humongous_waste(size_t bytes);
235   void decrease_humongous_waste(size_t bytes);
236   size_t get_humongous_waste() const { return _humongous_waste; }
237 
238   virtual bool is_concurrent_mark_in_progress() = 0;
239   void confirm_heuristics_mode();
240 
241   virtual void record_success_concurrent(bool abbreviated);
242 };
243 
244 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP