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/shenandoahGenerationType.hpp"
 31 #include "gc/shenandoah/shenandoahLock.hpp"
 32 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
 33 
 34 class ShenandoahCollectionSet;
 35 class ShenandoahHeap;
 36 class ShenandoahHeapRegion;
 37 class ShenandoahHeapRegionClosure;
 38 class ShenandoahHeuristics;
 39 class ShenandoahMode;
 40 class ShenandoahReferenceProcessor;
 41 
 42 
 43 class ShenandoahGeneration : public CHeapObj<mtGC>, public ShenandoahSpaceInfo {
 44   friend class VMStructs;
 45 private:
 46   ShenandoahGenerationType const _type;
 47 
 48   // Marking task queues and completeness
 49   ShenandoahObjToScanQueueSet* _task_queues;
 50   ShenandoahSharedFlag _is_marking_complete;
 51 
 52   ShenandoahReferenceProcessor* const _ref_processor;
 53 
 54   size_t _affiliated_region_count;
 55 
 56   // How much free memory is left in the last region of humongous objects.
 57   // This is _not_ included in used, but it _is_ deducted from available,
 58   // which gives the heuristics a more accurate view of how much memory remains
 59   // for allocation. This figure is also included the heap status logging.
 60   // The units are bytes. The value is only changed on a safepoint or under the
 61   // heap lock.
 62   size_t _humongous_waste;
 63 
 64   // Bytes reserved within this generation to hold evacuated objects from the collection set
 65   size_t _evacuation_reserve;
 66 
 67 protected:
 68   // Usage
 69 
 70   volatile size_t _used;
 71   volatile size_t _bytes_allocated_since_gc_start;
 72   size_t _max_capacity;
 73   size_t _soft_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                        size_t soft_max_capacity);
109   ~ShenandoahGeneration();
110 
111   bool is_young() const  { return _type == YOUNG; }
112   bool is_old() const    { return _type == OLD; }
113   bool is_global() const { return _type == GLOBAL || _type == NON_GEN; }
114 
115   // see description in field declaration
116   void set_evacuation_reserve(size_t new_val);
117   size_t get_evacuation_reserve() const;
118   void augment_evacuation_reserve(size_t increment);
119 
120   inline ShenandoahGenerationType type() const { return _type; }
121 
122   virtual ShenandoahHeuristics* heuristics() const { return _heuristics; }
123 
124   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
125 
126   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
127 
128   size_t soft_max_capacity() const override { return _soft_max_capacity; }
129   size_t max_capacity() const override      { return _max_capacity; }
130   virtual size_t used_regions() const;
131   virtual size_t used_regions_size() const;
132   virtual size_t free_unaffiliated_regions() const;
133   size_t used() const override { return _used; }
134   size_t available() const override;
135   size_t available_with_reserve() const;
136   size_t used_including_humongous_waste() const {
137     return used() + get_humongous_waste();
138   }
139 
140   // Returns the memory available based on the _soft_ max heap capacity (soft_max_heap - used).
141   // The soft max heap size may be adjusted lower than the max heap size to cause the trigger
142   // to believe it has less memory available than is _really_ available. Lowering the soft
143   // max heap size will cause the adaptive heuristic to run more frequent cycles.
144   size_t soft_available() const override;
145 
146   size_t bytes_allocated_since_gc_start() const override;
147   void reset_bytes_allocated_since_gc_start();
148   void increase_allocated(size_t bytes);
149 
150   // These methods change the capacity of the generation by adding or subtracting the given number of bytes from the current
151   // capacity, returning the capacity of the generation following the change.
152   size_t increase_capacity(size_t increment);
153   size_t decrease_capacity(size_t decrement);
154 
155   // Set the capacity of the generation, returning the value set
156   size_t set_capacity(size_t byte_size);
157 
158   void log_status(const char* msg) const;
159 
160   // Used directly by FullGC
161   void reset_mark_bitmap();
162 
163   // Used by concurrent and degenerated GC to reset remembered set.
164   void swap_remembered_set();
165 
166   // Update the read cards with the state of the write table (write table is not cleared).
167   void merge_write_table();
168 
169   // Called before init mark, expected to prepare regions for marking.
170   virtual void prepare_gc();
171 
172   // Called during final mark, chooses collection set, rebuilds free set.
173   virtual void prepare_regions_and_collection_set(bool concurrent);
174 
175   // Cancel marking (used by Full collect and when cancelling cycle).
176   virtual void cancel_marking();
177 
178   // Return true if this region is affiliated with this generation.
179   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
180 
181   // Return true if this object is affiliated with this generation.
182   virtual bool contains(oop obj) const = 0;
183 
184   // Apply closure to all regions affiliated with this generation.
185   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
186 
187   // Apply closure to all regions affiliated with this generation (include free regions);
188   virtual void parallel_region_iterate_free(ShenandoahHeapRegionClosure* cl);
189 
190   // Apply closure to all regions affiliated with this generation (single threaded).
191   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
192 
193   // This is public to support cancellation of marking when a Full cycle is started.
194   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
195 
196   // Check the bitmap only for regions belong to this generation.
197   bool is_bitmap_clear();
198 
199   // We need to track the status of marking for different generations.
200   bool is_mark_complete();
201   virtual void set_mark_complete();
202   virtual void set_mark_incomplete();
203 
204   ShenandoahMarkingContext* complete_marking_context();
205 
206   // Task queues
207   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
208   virtual void reserve_task_queues(uint workers);
209   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
210 
211   // Scan remembered set at start of concurrent young-gen marking.
212   void scan_remembered_set(bool is_concurrent);
213 
214   // Return the updated value of affiliated_region_count
215   size_t increment_affiliated_region_count();
216 
217   // Return the updated value of affiliated_region_count
218   size_t decrement_affiliated_region_count();
219 
220   // Return the updated value of affiliated_region_count
221   size_t increase_affiliated_region_count(size_t delta);
222 
223   // Return the updated value of affiliated_region_count
224   size_t decrease_affiliated_region_count(size_t delta);
225 
226   void establish_usage(size_t num_regions, size_t num_bytes, size_t humongous_waste);
227 
228   void increase_used(size_t bytes);
229   void decrease_used(size_t bytes);
230 
231   void increase_humongous_waste(size_t bytes);
232   void decrease_humongous_waste(size_t bytes);
233   size_t get_humongous_waste() const { return _humongous_waste; }
234 
235   virtual bool is_concurrent_mark_in_progress() = 0;
236   void confirm_heuristics_mode();
237 
238   virtual void record_success_concurrent(bool abbreviated);
239 };
240 
241 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP