1 /*
  2  * Copyright (c) 2020, 2021 Amazon.com, Inc. and/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/mode/shenandoahGenerationalMode.hpp"
 31 #include "gc/shenandoah/shenandoahLock.hpp"
 32 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
 33 
 34 class ShenandoahHeapRegion;
 35 class ShenandoahHeapRegionClosure;
 36 class ShenandoahReferenceProcessor;
 37 class ShenandoahHeap;
 38 
 39 class ShenandoahGeneration : public CHeapObj<mtGC> {
 40 private:
 41   GenerationMode const _generation_mode;
 42 
 43   // Marking task queues and completeness
 44   ShenandoahObjToScanQueueSet* _task_queues;
 45   ShenandoahSharedFlag _is_marking_complete;
 46 
 47   ShenandoahReferenceProcessor* const _ref_processor;
 48 
 49   double _collection_thread_time_s;
 50 
 51 protected:
 52   // Usage
 53   size_t _affiliated_region_count;
 54   volatile size_t _used;
 55   volatile size_t _bytes_allocated_since_gc_start;
 56   size_t _max_capacity;
 57   size_t _soft_max_capacity;
 58 
 59   size_t _adjusted_capacity;
 60 
 61   ShenandoahHeuristics* _heuristics;
 62 
 63 private:
 64   // Compute evacuation budgets prior to choosing collection set.
 65   void compute_evacuation_budgets(ShenandoahHeap* heap, bool* preselected_regions, ShenandoahCollectionSet* collection_set,
 66                                   size_t &consumed_by_advance_promotion);
 67 
 68   // Adjust evacuation budgets after choosing collection set.
 69   void adjust_evacuation_budgets(ShenandoahHeap* heap, ShenandoahCollectionSet* collection_set,
 70                                  size_t consumed_by_advance_promotion);
 71 
 72  public:
 73   ShenandoahGeneration(GenerationMode generation_mode, uint max_workers, size_t max_capacity, size_t soft_max_capacity);
 74   ~ShenandoahGeneration();
 75 
 76   bool is_young() const  { return _generation_mode == YOUNG; }
 77   bool is_old() const    { return _generation_mode == OLD; }
 78   bool is_global() const { return _generation_mode == GLOBAL; }
 79 
 80   inline GenerationMode generation_mode() const { return _generation_mode; }
 81 
 82   inline ShenandoahHeuristics* heuristics() const { return _heuristics; }
 83 
 84   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
 85 
 86   virtual const char* name() const = 0;
 87 
 88   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
 89 
 90   virtual size_t soft_max_capacity() const { return _soft_max_capacity; }
 91   virtual size_t max_capacity() const      { return _max_capacity; }
 92   virtual size_t used_regions() const;
 93   virtual size_t used_regions_size() const;
 94   virtual size_t free_unaffiliated_regions() const;
 95   virtual size_t used() const { return _used; }
 96   virtual size_t available() const;
 97 
 98   // During evacuation and update-refs, some memory may be shifted between generations.  In particular, memory
 99   // may be loaned by old-gen to young-gen based on the promise the loan will be promptly repaid from the memory reclaimed
100   // when the current collection set is recycled.  The capacity adjustment also takes into consideration memory that is
101   // set aside within each generation to hold the results of evacuation, but not promotion, into that region.  Promotions
102   // into old-gen are bounded by adjusted_available() whereas evacuations into old-gen are pre-committed.
103   virtual size_t adjusted_available() const;
104   virtual size_t adjusted_capacity() const;
105 
106   // This is the number of FREE regions that are eligible to be affiliated with this generation according to the current
107   // adjusted capacity.
108   virtual size_t adjusted_unaffiliated_regions() const;
109 
110   // Both of following return new value of available
111   virtual size_t adjust_available(intptr_t adjustment);
112   virtual size_t unadjust_available();
113 
114   size_t bytes_allocated_since_gc_start();
115   void reset_bytes_allocated_since_gc_start();
116   void increase_allocated(size_t bytes);
117 
118   // These methods change the capacity of the region by adding or subtracting the given number of bytes from the current
119   // capacity.
120   void increase_capacity(size_t increment);
121   void decrease_capacity(size_t decrement);
122 
123   void set_soft_max_capacity(size_t soft_max_capacity) {
124     _soft_max_capacity = soft_max_capacity;
125   }
126 
127   void log_status(const char* msg) const;
128 
129   // Used directly by FullGC
130   void reset_mark_bitmap();
131 
132   // Used by concurrent and degenerated GC to reset remembered set.
133   void swap_remembered_set();
134 
135   // Update the read cards with the state of the write table (write table is not cleared).
136   void merge_write_table();
137 
138   // Called before init mark, expected to prepare regions for marking.
139   virtual void prepare_gc();
140 
141   // Called during final mark, chooses collection set, rebuilds free set.
142   virtual void prepare_regions_and_collection_set(bool concurrent);
143 
144   // Cancel marking (used by Full collect and when cancelling cycle).
145   virtual void cancel_marking();
146 
147   // Return true if this region is affiliated with this generation.
148   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
149 
150   // Return true if this object is affiliated with this generation.
151   virtual bool contains(oop obj) const = 0;
152 
153   // Apply closure to all regions affiliated with this generation.
154   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
155 
156   // Apply closure to all regions affiliated with this generation (single threaded).
157   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
158 
159   // This is public to support cancellation of marking when a Full cycle is started.
160   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
161 
162   // Check the bitmap only for regions belong to this generation.
163   bool is_bitmap_clear();
164 
165   // We need to track the status of marking for different generations.
166   bool is_mark_complete();
167   void set_mark_complete();
168   void set_mark_incomplete();
169 
170   ShenandoahMarkingContext* complete_marking_context();
171 
172   // Task queues
173   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
174   virtual void reserve_task_queues(uint workers);
175   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
176 
177   // Scan remembered set at start of concurrent young-gen marking.
178   void scan_remembered_set(bool is_concurrent);
179 
180   // Return the updated value of affiliated_region_count
181   size_t increment_affiliated_region_count();
182 
183   // Return the updated value of affiliated_region_count
184   size_t decrement_affiliated_region_count();
185 
186   void clear_used();
187   void increase_used(size_t bytes);
188   void decrease_used(size_t bytes);
189 
190   virtual bool is_concurrent_mark_in_progress() = 0;
191   void confirm_heuristics_mode();
192 
193   virtual void record_success_concurrent(bool abbreviated);
194   virtual void record_success_degenerated();
195 
196   // Record the total on-cpu time a thread has spent collecting this
197   // generation. This is only called by the control thread (at the start
198   // of a collection) and by the VM thread at the end of the collection,
199   // so there are no locking concerns.
200   virtual void add_collection_time(double time_seconds);
201 
202   // This returns the accumulated collection time and resets it to zero.
203   // This is used to decide which generation should be resized.
204   double reset_collection_time();
205 };
206 
207 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP