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