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   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   size_t _soft_max_capacity;
 75 
 76   ShenandoahHeuristics* _heuristics;
 77 
 78 private:
 79   // Compute evacuation budgets prior to choosing collection set.
 80   void compute_evacuation_budgets(ShenandoahHeap* heap);
 81 
 82   // Adjust evacuation budgets after choosing collection set.
 83   void adjust_evacuation_budgets(ShenandoahHeap* heap,
 84                                  ShenandoahCollectionSet* collection_set);
 85 
 86   // Preselect for possible inclusion into the collection set exactly the most
 87   // garbage-dense regions, including those that satisfy criteria 1 & 2 below,
 88   // and whose live bytes will fit within old_available budget:
 89   // Criterion 1. region age >= tenuring threshold
 90   // Criterion 2. region garbage percentage > ShenandoahOldGarbageThreshold
 91   //
 92   // Identifies regions eligible for promotion in place,
 93   // being those of at least tenuring_threshold age that have lower garbage
 94   // density.
 95   //
 96   // Updates promotion_potential and pad_for_promote_in_place fields
 97   // of the heap. Returns bytes of live object memory in the preselected
 98   // regions, which are marked in the preselected_regions() indicator
 99   // array of the heap's collection set, which should be initialized
100   // to false.
101   size_t select_aged_regions(size_t old_available);
102 
103   size_t available(size_t capacity) const;
104 
105  public:
106   ShenandoahGeneration(ShenandoahGenerationType type,
107                        uint max_workers,
108                        size_t max_capacity,
109                        size_t soft_max_capacity);
110   ~ShenandoahGeneration();
111 
112   bool is_young() const  { return _type == YOUNG; }
113   bool is_old() const    { return _type == OLD; }
114   bool is_global() const { return _type == GLOBAL || _type == NON_GEN; }
115 
116   // see description in field declaration
117   void set_evacuation_reserve(size_t new_val);
118   size_t get_evacuation_reserve() const;
119   void augment_evacuation_reserve(size_t increment);
120 
121   inline ShenandoahGenerationType type() const { return _type; }
122 
123   virtual ShenandoahHeuristics* heuristics() const { return _heuristics; }
124 
125   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
126 
127   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
128 
129   size_t soft_max_capacity() const override { return _soft_max_capacity; }
130   size_t max_capacity() const override      { return _max_capacity; }
131   virtual size_t used_regions() const;
132   virtual size_t used_regions_size() const;
133   virtual size_t free_unaffiliated_regions() const;
134   size_t used() const override { return _used; }
135   size_t available() const override;
136   size_t available_with_reserve() const;
137   size_t used_including_humongous_waste() const {
138     return used() + get_humongous_waste();
139   }
140 
141   // Returns the memory available based on the _soft_ max heap capacity (soft_max_heap - used).
142   // The soft max heap size may be adjusted lower than the max heap size to cause the trigger
143   // to believe it has less memory available than is _really_ available. Lowering the soft
144   // max heap size will cause the adaptive heuristic to run more frequent cycles.
145   size_t soft_available() const override;
146 
147   size_t bytes_allocated_since_gc_start() const override;
148   void reset_bytes_allocated_since_gc_start();
149   void increase_allocated(size_t bytes);
150 
151   // These methods change the capacity of the generation by adding or subtracting the given number of bytes from the current
152   // capacity, returning the capacity of the generation following the change.
153   size_t increase_capacity(size_t increment);
154   size_t decrease_capacity(size_t decrement);
155 
156   // Set the capacity of the generation, returning the value set
157   size_t set_capacity(size_t byte_size);
158 
159   void log_status(const char* msg) const;
160 
161   // Used directly by FullGC
162   void reset_mark_bitmap();
163 
164   // Used by concurrent and degenerated GC to reset remembered set.
165   void swap_remembered_set();
166 
167   // Update the read cards with the state of the write table (write table is not cleared).
168   void merge_write_table();
169 
170   // Called before init mark, expected to prepare regions for marking.
171   virtual void prepare_gc();
172 
173   // Called during final mark, chooses collection set, rebuilds free set.
174   virtual void prepare_regions_and_collection_set(bool concurrent);
175 
176   // Cancel marking (used by Full collect and when cancelling cycle).
177   virtual void cancel_marking();
178 
179   virtual bool contains(ShenandoahAffiliation affiliation) const = 0;
180 
181   // Return true if this region is affiliated with this generation.
182   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
183 
184   // Return true if this object is affiliated with this generation.
185   virtual bool contains(oop obj) const = 0;
186 
187   // Apply closure to all regions affiliated with this generation.
188   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
189 
190   // Apply closure to all regions affiliated with this generation (include free regions);
191   virtual void parallel_heap_region_iterate_free(ShenandoahHeapRegionClosure* cl);
192 
193   // Apply closure to all regions affiliated with this generation (single threaded).
194   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
195 
196   // This is public to support cancellation of marking when a Full cycle is started.
197   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
198 
199   // Check the bitmap only for regions belong to this generation.
200   bool is_bitmap_clear();
201 
202   // We need to track the status of marking for different generations.
203   bool is_mark_complete();
204   virtual void set_mark_complete();
205   virtual void set_mark_incomplete();
206 
207   ShenandoahMarkingContext* complete_marking_context();
208 
209   // Task queues
210   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
211   virtual void reserve_task_queues(uint workers);
212   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
213 
214   // Scan remembered set at start of concurrent young-gen marking.
215   void scan_remembered_set(bool is_concurrent);
216 
217   // Return the updated value of affiliated_region_count
218   size_t increment_affiliated_region_count();
219 
220   // Return the updated value of affiliated_region_count
221   size_t decrement_affiliated_region_count();
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