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