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   ShenandoahEvacuationTracker* evac_tracker() const {
 84     return _evac_tracker;
 85   }
 86 
 87   // Ages regions that haven't been used for allocations in the current cycle.
 88   // Resets ages for regions that have been used for allocations.
 89   void update_region_ages(ShenandoahMarkingContext* ctx);
 90 
 91   oop evacuate_object(oop p, Thread* thread) override;
 92   oop try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region, ShenandoahAffiliation target_gen);
 93   void evacuate_collection_set(bool concurrent) override;
 94   void promote_regions_in_place(bool concurrent);
 95 
 96   size_t plab_min_size() const { return _min_plab_size; }
 97   size_t plab_max_size() const { return _max_plab_size; }
 98 
 99   void retire_plab(PLAB* plab);
100   void retire_plab(PLAB* plab, Thread* thread);
101 
102   // ---------- Update References
103   //
104   void update_heap_references(bool concurrent) override;
105   void final_update_refs_update_region_states() override;
106 
107 private:
108   HeapWord* allocate_from_plab(Thread* thread, size_t size, bool is_promotion);
109   HeapWord* allocate_from_plab_slow(Thread* thread, size_t size, bool is_promotion);
110   HeapWord* allocate_new_plab(size_t min_size, size_t word_size, size_t* actual_size);
111 
112   const size_t _min_plab_size;
113   const size_t _max_plab_size;
114 
115   static size_t calculate_min_plab();
116   static size_t calculate_max_plab();
117 
118 public:
119   // ---------- Serviceability
120   //
121   void initialize_serviceability() override;
122   GrowableArray<MemoryPool*> memory_pools() override;
123 
124   ShenandoahRegulatorThread* regulator_thread() const { return _regulator_thread;  }
125 
126   void gc_threads_do(ThreadClosure* tcl) const override;
127 
128   void stop() override;
129 
130   // Used for logging the result of a region transfer outside the heap lock
131   struct TransferResult {
132     bool success;
133     size_t region_count;
134     const char* region_destination;
135 
136     void print_on(const char* when, outputStream* ss) const;
137   };
138 
139   const ShenandoahGenerationSizer* generation_sizer()  const { return &_generation_sizer;  }
140 
141   // Zeros out the evacuation and promotion reserves
142   void reset_generation_reserves();
143 
144   // Computes the optimal size for the old generation, represented as a surplus or deficit of old regions
145   void compute_old_generation_balance(size_t old_xfer_limit, size_t old_cset_regions);
146 
147   // Transfers surplus old regions to young, or takes regions from young to satisfy old region deficit
148   TransferResult balance_generations();
149 
150   // Balances generations, coalesces and fills old regions if necessary
151   void complete_degenerated_cycle();
152   void complete_concurrent_cycle();
153 private:
154   void initialize_controller() override;
155   void entry_global_coalesce_and_fill();
156 
157   // Makes old regions parsable. This will also rebuild card offsets, which is necessary if classes were unloaded
158   void coalesce_and_fill_old_regions(bool concurrent);
159 
160   ShenandoahRegulatorThread* _regulator_thread;
161 
162   MemoryPool* _young_gen_memory_pool;
163   MemoryPool* _old_gen_memory_pool;
164 
165   ShenandoahGenerationSizer     _generation_sizer;
166 };
167 
168 #endif //SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALHEAP