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 protected:
 50   // Usage
 51   size_t _affiliated_region_count;
 52   volatile size_t _used;
 53   volatile size_t _bytes_allocated_since_gc_start;
 54   size_t _max_capacity;
 55   size_t _soft_max_capacity;
 56 
 57   size_t _adjusted_capacity;
 58 
 59   ShenandoahHeuristics* _heuristics;
 60 
 61 private:
 62   // Compute evacuation budgets prior to choosing collection set.
 63   void compute_evacuation_budgets(ShenandoahHeap* heap, bool* preselected_regions, ShenandoahCollectionSet* collection_set,
 64                                   size_t &old_regions_loaned_for_young_evac, size_t &regions_available_to_loan,
 65                                   size_t &minimum_evacuation_reserve, size_t &consumed_by_advance_promotion);
 66 
 67   // Adjust evacuation budgets after choosing collection set.
 68   void adjust_evacuation_budgets(ShenandoahHeap* heap, ShenandoahCollectionSet* collection_set,
 69                                  size_t old_regions_loaned_for_young_evac, size_t regions_available_to_loan,
 70                                  size_t minimum_evacuation_reserve, 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   inline GenerationMode generation_mode() const { return _generation_mode; }
 77 
 78   inline ShenandoahHeuristics* heuristics() const { return _heuristics; }
 79 
 80   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
 81 
 82   virtual const char* name() const = 0;
 83 
 84   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
 85 
 86   virtual size_t soft_max_capacity() const { return _soft_max_capacity; }
 87   virtual size_t max_capacity() const      { return _max_capacity; }
 88   virtual size_t used_regions() const;
 89   virtual size_t used_regions_size() const;
 90   virtual size_t free_unaffiliated_regions() const;
 91   virtual size_t used() const { return _used; }
 92   virtual size_t available() const;
 93 
 94   // During evacuation and update-refs, some memory may be shifted between generations.  In particular, memory
 95   // may be loaned by old-gen to young-gen based on the promise the loan will be promptly repaid from the memory reclaimed
 96   // when the current collection set is recycled.  The capacity adjustment also takes into consideration memory that is
 97   // set aside within each generation to hold the results of evacuation, but not promotion, into that region.  Promotions
 98   // into old-gen are bounded by adjusted_available() whereas evacuations into old-gen are pre-committed.
 99   virtual size_t adjusted_available() const;
100 
101   // Both of following return new value of available
102   virtual size_t adjust_available(intptr_t adjustment);
103   virtual size_t unadjust_available();
104 
105   size_t bytes_allocated_since_gc_start();
106   void reset_bytes_allocated_since_gc_start();
107   void increase_allocated(size_t bytes);
108 
109   void set_soft_max_capacity(size_t soft_max_capacity) {
110     _soft_max_capacity = soft_max_capacity;
111   }
112 
113   void log_status() const;
114 
115   // Used directly by FullGC
116   void reset_mark_bitmap();
117 
118   // Used by concurrent and degenerated GC to reset remembered set.
119   void swap_remembered_set();
120 
121   // Update the read cards with the state of the write table (write table is not cleared).
122   void merge_write_table();
123 
124   // Called before init mark, expected to prepare regions for marking.
125   virtual void prepare_gc();
126 
127   // Called during final mark, chooses collection set, rebuilds free set.
128   virtual void prepare_regions_and_collection_set(bool concurrent);
129 
130   // Cancel marking (used by Full collect and when cancelling cycle).
131   virtual void cancel_marking();
132 
133   // Return true if this region is affiliated with this generation.
134   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
135 
136   // Return true if this object is affiliated with this generation.
137   virtual bool contains(oop obj) const = 0;
138 
139   // Apply closure to all regions affiliated with this generation.
140   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
141 
142   // Apply closure to all regions affiliated with this generation (single threaded).
143   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
144 
145   // This is public to support cancellation of marking when a Full cycle is started.
146   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
147 
148   // Check the bitmap only for regions belong to this generation.
149   bool is_bitmap_clear();
150 
151   // We need to track the status of marking for different generations.
152   bool is_mark_complete();
153   void set_mark_complete();
154   void set_mark_incomplete();
155 
156   ShenandoahMarkingContext* complete_marking_context();
157 
158   // Task queues
159   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
160   virtual void reserve_task_queues(uint workers);
161   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
162 
163   // Scan remembered set at start of concurrent young-gen marking. */
164   void scan_remembered_set(bool is_concurrent);
165 
166   void increment_affiliated_region_count();
167   void decrement_affiliated_region_count();
168 
169   void clear_used();
170   void increase_used(size_t bytes);
171   void decrease_used(size_t bytes);
172 
173   virtual bool is_concurrent_mark_in_progress() = 0;
174   void confirm_heuristics_mode();
175 
176   virtual void record_success_concurrent(bool abbreviated);
177   virtual void record_success_degenerated();
178 };
179 
180 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP