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 
 38 class ShenandoahGeneration : public CHeapObj<mtGC> {
 39 private:
 40   GenerationMode const _generation_mode;
 41 
 42   // Marking task queues and completeness
 43   ShenandoahObjToScanQueueSet* _task_queues;
 44   ShenandoahSharedFlag _is_marking_complete;
 45 
 46   ShenandoahReferenceProcessor* const _ref_processor;
 47 
 48 protected:
 49   // Usage
 50   size_t _affiliated_region_count;
 51   volatile size_t _used;
 52   volatile size_t _bytes_allocated_since_gc_start;
 53   size_t _max_capacity;
 54   size_t _soft_max_capacity;
 55 
 56   ShenandoahHeuristics* _heuristics;
 57  public:
 58   ShenandoahGeneration(GenerationMode generation_mode, uint max_workers, size_t max_capacity, size_t soft_max_capacity);
 59   ~ShenandoahGeneration();
 60 
 61   inline GenerationMode generation_mode() const { return _generation_mode; }
 62 
 63   inline ShenandoahHeuristics* heuristics() const { return _heuristics; }
 64 
 65   ShenandoahReferenceProcessor* ref_processor() { return _ref_processor; }
 66 
 67   virtual const char* name() const = 0;
 68 
 69   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode);
 70 
 71   virtual size_t soft_max_capacity() const { return _soft_max_capacity; }
 72   virtual size_t max_capacity() const      { return _max_capacity; }
 73   virtual size_t used_regions_size() const;
 74   virtual size_t used() const { return _used; }
 75   virtual size_t available() const;
 76 
 77   size_t bytes_allocated_since_gc_start();
 78   void reset_bytes_allocated_since_gc_start();
 79   void increase_allocated(size_t bytes);
 80 
 81   void set_soft_max_capacity(size_t soft_max_capacity) {
 82     _soft_max_capacity = soft_max_capacity;
 83   }
 84 
 85   void log_status() const;
 86 
 87   // Used directly by FullGC
 88   void reset_mark_bitmap();
 89 
 90   // Used by concurrent and degenerated GC to reset remembered set.
 91   void swap_remembered_set();
 92 
 93   // Used by concurrent and degenerated GC to reset regions.
 94   virtual void prepare_gc(bool do_old_gc_bootstrap);
 95 
 96   // Return true iff prepared collection set includes at least one old-gen HeapRegion.
 97   virtual bool prepare_regions_and_collection_set(bool concurrent);
 98 
 99   // Cancel marking (used by Full collect and when cancelling cycle).
100   void cancel_marking();
101 
102   // Return true if this region is affiliated with this generation.
103   virtual bool contains(ShenandoahHeapRegion* region) const = 0;
104 
105   // Return true if this object is affiliated with this generation.
106   virtual bool contains(oop obj) const = 0;
107 
108   // Apply closure to all regions affiliated with this generation.
109   virtual void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
110 
111   // Apply closure to all regions affiliated with this generation (single threaded).
112   virtual void heap_region_iterate(ShenandoahHeapRegionClosure* cl) = 0;
113 
114   // This is public to support cancellation of marking when a Full cycle is started.
115   virtual void set_concurrent_mark_in_progress(bool in_progress) = 0;
116 
117   // Check the bitmap only for regions belong to this generation.
118   bool is_bitmap_clear();
119 
120   // We need to track the status of marking for different generations.
121   bool is_mark_complete();
122   void set_mark_complete();
123   void set_mark_incomplete();
124 
125   ShenandoahMarkingContext* complete_marking_context();
126 
127   // Task queues
128   ShenandoahObjToScanQueueSet* task_queues() const { return _task_queues; }
129   virtual void reserve_task_queues(uint workers);
130   virtual ShenandoahObjToScanQueueSet* old_gen_task_queues() const;
131 
132   // Scan remembered set at start of concurrent young-gen marking. */
133   void scan_remembered_set();
134 
135   void increment_affiliated_region_count();
136   void decrement_affiliated_region_count();
137 
138   void clear_used();
139   void increase_used(size_t bytes);
140   void decrease_used(size_t bytes);
141 
142 protected:
143 
144   virtual bool is_concurrent_mark_in_progress() = 0;
145 
146 private:
147   void confirm_heuristics_mode();
148 };
149 
150 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHGENERATION_HPP