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_GC_SHENANDOAH_SHENANDOAHGENERATIONALHEAP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALHEAP
 27 
 28 #include "gc/shenandoah/shenandoahAsserts.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.hpp"
 30 #include "memory/universe.hpp"
 31 
 32 class PLAB;
 33 class ShenandoahRegulatorThread;
 34 class ShenandoahGenerationalControlThread;
 35 class ShenandoahAgeCensus;
 36 
 37 class ShenandoahGenerationalHeap : public ShenandoahHeap {
 38 public:
 39   explicit ShenandoahGenerationalHeap(ShenandoahCollectorPolicy* policy);
 40   void post_initialize() override;
 41   void initialize_heuristics() override;
 42 
 43   static ShenandoahGenerationalHeap* heap() {
 44     shenandoah_assert_generational();
 45     CollectedHeap* heap = Universe::heap();
 46     return cast(heap);
 47   }
 48 
 49   static ShenandoahGenerationalHeap* cast(CollectedHeap* heap) {
 50     shenandoah_assert_generational();
 51     return checked_cast<ShenandoahGenerationalHeap*>(heap);
 52   }
 53 
 54   void print_init_logger() const override;
 55   void print_tracing_info() const override;
 56 
 57   size_t unsafe_max_tlab_alloc(Thread *thread) const override;
 58 
 59 private:
 60   // ---------- Evacuations and Promotions
 61   //
 62   // True when regions and objects should be aged during the current cycle
 63   ShenandoahSharedFlag  _is_aging_cycle;
 64   // Age census used for adapting tenuring threshold
 65   ShenandoahAgeCensus* _age_census;
 66   // Used primarily to look for failed evacuation attempts.
 67   ShenandoahEvacuationTracker*  _evac_tracker;
 68 
 69 public:
 70   void set_aging_cycle(bool cond) {
 71     _is_aging_cycle.set_cond(cond);
 72   }
 73 
 74   inline bool is_aging_cycle() const {
 75     return _is_aging_cycle.is_set();
 76   }
 77 
 78   // Return the age census object for young gen
 79   ShenandoahAgeCensus* age_census() const {
 80     return _age_census;
 81   }
 82 
 83   inline bool is_tenurable(const ShenandoahHeapRegion* r) const;
 84 
 85   ShenandoahEvacuationTracker* evac_tracker() const {
 86     return _evac_tracker;
 87   }
 88 
 89   // Ages regions that haven't been used for allocations in the current cycle.
 90   // Resets ages for regions that have been used for allocations.
 91   void update_region_ages(ShenandoahMarkingContext* ctx);
 92 
 93   oop evacuate_object(oop p, Thread* thread) override;
 94   oop try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
 95   void evacuate_collection_set(bool concurrent) override;
 96   void promote_regions_in_place(bool concurrent);
 97 
 98   size_t plab_min_size() const { return _min_plab_size; }
 99   size_t plab_max_size() const { return _max_plab_size; }
100 
101   void retire_plab(PLAB* plab);
102   void retire_plab(PLAB* plab, Thread* thread);
103 
104   // ---------- Update References
105   //
106   void update_heap_references(bool concurrent) override;
107   void final_update_refs_update_region_states() override;
108 
109 private:
110   HeapWord* allocate_from_plab(Thread* thread, size_t size, bool is_promotion);
111   HeapWord* allocate_from_plab_slow(Thread* thread, size_t size, bool is_promotion);
112   HeapWord* allocate_new_plab(size_t min_size, size_t word_size, size_t* actual_size);
113 
114   const size_t _min_plab_size;
115   const size_t _max_plab_size;
116 
117   static size_t calculate_min_plab();
118   static size_t calculate_max_plab();
119 
120 public:
121   // ---------- Serviceability
122   //
123   void initialize_serviceability() override;
124   GrowableArray<MemoryPool*> memory_pools() override;
125 
126   ShenandoahRegulatorThread* regulator_thread() const { return _regulator_thread;  }
127 
128   void gc_threads_do(ThreadClosure* tcl) const override;
129 
130   void stop() override;
131 
132   bool requires_barriers(stackChunkOop obj) const override;
133 
134   // Used for logging the result of a region transfer outside the heap lock
135   struct TransferResult {
136     bool success;
137     size_t region_count;
138     const char* region_destination;
139 
140     void print_on(const char* when, outputStream* ss) const;
141   };
142 
143   const ShenandoahGenerationSizer* generation_sizer()  const { return &_generation_sizer;  }
144 
145   // Zeros out the evacuation and promotion reserves
146   void reset_generation_reserves();
147 
148   // Computes the optimal size for the old generation, represented as a surplus or deficit of old regions
149   void compute_old_generation_balance(size_t old_xfer_limit, size_t old_cset_regions);
150 
151   // Transfers surplus old regions to young, or takes regions from young to satisfy old region deficit
152   TransferResult balance_generations();
153 
154   // Balances generations, coalesces and fills old regions if necessary
155   void complete_degenerated_cycle();
156   void complete_concurrent_cycle();
157 private:
158   void initialize_controller() override;
159   void entry_global_coalesce_and_fill();
160 
161   // Makes old regions parsable. This will also rebuild card offsets, which is necessary if classes were unloaded
162   void coalesce_and_fill_old_regions(bool concurrent);
163 
164   ShenandoahRegulatorThread* _regulator_thread;
165 
166   MemoryPool* _young_gen_memory_pool;
167   MemoryPool* _old_gen_memory_pool;
168 
169   ShenandoahGenerationSizer     _generation_sizer;
170 };
171 
172 #endif //SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALHEAP