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