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                                   bool* preselected_regions,
 76                                   ShenandoahCollectionSet* collection_set,
 77                                   size_t& consumed_by_advance_promotion);
 78 
 79   // Adjust evacuation budgets after choosing collection set.
 80   void adjust_evacuation_budgets(ShenandoahHeap* heap,
 81                                  ShenandoahCollectionSet* collection_set,
 82                                  size_t consumed_by_advance_promotion);
 83 
 84   // Preselect for inclusion into the collection set regions whose age is
 85   // at or above tenure age and which contain more than ShenandoahOldGarbageThreshold
 86   // amounts of garbage.
 87   //
 88   // Returns bytes of old-gen memory consumed by selected aged regions
 89   size_t select_aged_regions(size_t old_available,
 90                              size_t num_regions, bool
 91                              candidate_regions_for_promotion_by_copy[]);
 92 
 93   size_t available(size_t capacity) const;
 94 
 95  public:
 96   ShenandoahGeneration(ShenandoahGenerationType type,
 97                        uint max_workers,
 98                        size_t max_capacity,
 99                        size_t soft_max_capacity);
100   ~ShenandoahGeneration();
101 
102   bool is_young() const  { return _type == YOUNG; }
103   bool is_old() const    { return _type == OLD; }
104   bool is_global() const { return _type == GLOBAL_GEN || _type == GLOBAL_NON_GEN; }
105 
106   inline ShenandoahGenerationType type() const { return _type; }
107 
108   inline ShenandoahHeuristics* heuristics() const { return _heuristics; }
109 
110   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
111 
112   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
113 
114   size_t soft_max_capacity() const override { return _soft_max_capacity; }
115   size_t max_capacity() const override      { return _max_capacity; }
116   virtual size_t used_regions() const;
117   virtual size_t used_regions_size() const;
118   virtual size_t free_unaffiliated_regions() const;
119   size_t used() const override { return _used; }
120   size_t available() const override;
121   size_t available_with_reserve() const;
122 
123   // Returns the memory available based on the _soft_ max heap capacity (soft_max_heap - used).
124   // The soft max heap size may be adjusted lower than the max heap size to cause the trigger
125   // to believe it has less memory available than is _really_ available. Lowering the soft
126   // max heap size will cause the adaptive heuristic to run more frequent cycles.
127   size_t soft_available() const override;
128 
129   size_t bytes_allocated_since_gc_start() const override;
130   void reset_bytes_allocated_since_gc_start();
131   void increase_allocated(size_t bytes);
132 
133   // These methods change the capacity of the region by adding or subtracting the given number of bytes from the current
134   // capacity.
135   void increase_capacity(size_t increment);
136   void decrease_capacity(size_t decrement);
137 
138   void set_soft_max_capacity(size_t soft_max_capacity) {
139     _soft_max_capacity = soft_max_capacity;
140   }
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   virtual void record_success_degenerated();
221 };
222 
223 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP